Files
2025-02-18 19:07:45 -07:00

338 lines
12 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local crc16_x25 = require("crc16_x25")
local dia_fm0 = require("prodigy_dia_fm0")
local tocs = require("prodigy_tocs")
-- Define a field extractor for the TCP stream number:
local tcp_stream_f = Field.new("tcp.stream")
local tcp_dstport_f = Field.new("tcp.dstport")
------------------------------
-- Global table for layer 3 reassembly (keyed by conversation)
------------------------------
local reassembly = {}
------------------------------
-- Prodigy Link Layer Protocol (PLLP, aka TCS from "Trintex Communications Subsystem")
-- (for use on TCP port 25235)
--
-- Packet layout:
-- 0 1 2 3 4 5 N N+1 N+2
-- +------+-------+-----------+---------+------+---------------------+---------+-----+
-- | STX | Count | Complement| Sequence| Type | Payload (count+1) | Checksum (LE, 2 bytes) |
-- +------+-------+-----------+---------+------+---------------------+---------+-----+
--
-- The checksum is the CRC-16/X-25 computed over all bytes after the STX (i.e. from Count through the end of the Payload).
------------------------------
local tcs_proto = Proto("TCS", "Prodigy Link Layer Protocol")
------------------------------
-- Define TCS packet types
------------------------------
local TCS_TYPES = {
[0] = 'UD1NAK',
[1] = 'UD1ACK',
[2] = 'ACKPKT',
[3] = 'NAKCCE',
[4] = 'NAKNCC',
[5] = 'RXMITP',
[6] = 'WACKPK',
[7] = 'TXABOD',
[8] = 'UD2NAK',
[9] = 'UD2ACK'
}
------------------------------
-- Define protocol fields
------------------------------
tcs_proto.fields.stx = ProtoField.uint8("tcs.stx", "STX", base.HEX)
tcs_proto.fields.count = ProtoField.uint8("tcs.count", "Count", base.DEC)
tcs_proto.fields.complement = ProtoField.uint8("tcs.complement", "Complement", base.HEX)
tcs_proto.fields.sequence = ProtoField.uint8("tcs.sequence", "Sequence", base.DEC)
tcs_proto.fields.tcs_type = ProtoField.uint8("tcs.type", "Type", base.HEX, TCS_TYPES)
tcs_proto.fields.data = ProtoField.bytes("tcs.data", "Payload")
tcs_proto.fields.target = ProtoField.uint8("tcs.data", "Target Sequence", base.DEC)
tcs_proto.fields.crc = ProtoField.uint16("tcs.crc", "Checksum", base.HEX)
tcs_proto.fields.checksum_status = ProtoField.string("tcs.checksum.status", "Checksum Status")
tcs_proto.fields.direction = ProtoField.string("tcs.direction", "Direction", base.ASCII)
------------------------------
-- Determine full TCS packet length.
--
-- A full packet consists of:
-- 1 byte STX +
-- 1 byte Count +
-- 1 byte Complement +
-- 1 byte Sequence +
-- 1 byte Type +
-- (Count+1) bytes Payload +
-- 2 bytes Checksum
--
-- Total = Count + 8.
------------------------------
local function get_tcs_length(tvb, pinfo, offset)
local remaining = tvb:len() - offset
if remaining < 2 then
pinfo.desegment_len = 2 - remaining
return 0
end
local count = tvb:range(offset + 1, 1):uint()
local expected_len = count + 8
if remaining < expected_len then
pinfo.desegment_len = expected_len - remaining
return 0
end
return expected_len
end
local function dissect_tocs(state, subtree, pinfo, conv_key)
local tocs_tv = ByteArray.tvb(state.buffer, "Reassembled TOCS")
local tocs_len = tocs_tv(4, 2):uint()
local total_tocs_len = 6 + tocs_len
if state.buffer:len() >= total_tocs_len then
local tocs_subtree = subtree:add(tocs.proto, tocs_tv(0, total_tocs_len))
tocs.proto.dissector(ByteArray.tvb(tocs_tv(0, total_tocs_len):bytes()), pinfo, tocs_subtree)
subtree:append_text(" (TOCS complete)")
--reassembly[conv_key] = nil
end
end
local function dissect_dia_fm0(state, subtree, pinfo, conv_key)
local fm0_tv = ByteArray.tvb(state.buffer, "Reassembled DIA")
local text_len = fm0_tv(14, 2):uint()
local total_dia_len = 16 + text_len
if state.buffer:len() >= total_dia_len then
local dia_subtree = subtree:add(dia_fm0.proto, fm0_tv(0, total_dia_len))
dia_fm0.proto.dissector(ByteArray.tvb(fm0_tv(0, total_dia_len):bytes()), pinfo, dia_subtree)
subtree:append_text(" (DIA complete)")
--reassembly[conv_key] = nil
end
end
local function try_reassemble_payload(state, subtree, pinfo, conv_key)
-- We can’t reassemble unless we’ve seen a valid UD1ACK setting the start sequence.
if not state.start_seq then
return false
end
-- Gather and sort fragments by sequence (taking wrap-around into account).
local fragments = state.fragments
local seqs = {}
for seq in pairs(fragments) do
table.insert(seqs, seq)
end
table.sort(seqs, function(a, b)
return ((a - state.start_seq) % 256) < ((b - state.start_seq) % 256)
end)
-- Concatenate the fragments in order.
local full_payload = ByteArray.new()
for _, seq in ipairs(seqs) do
full_payload:append(fragments[seq])
end
-- Save the reassembled buffer into the state so that the helper functions work as before.
state.buffer = full_payload
local payload_len = full_payload:len()
if payload_len >= 1 then
local tvb_payload = ByteArray.tvb(full_payload, "Reassembled Layer 3")
local first_byte = tvb_payload(0,1):uint()
if first_byte == 0 and payload_len >= 6 then
-- For TOCS, the length field is at offset 4 (2 bytes) and the total length is 6 + tocs_len.
local tocs_len = tvb_payload(4,2):uint()
local total_tocs_len = 6 + tocs_len
if payload_len >= total_tocs_len then
-- We have enough data; let the TOCS dissector process it.
dissect_tocs(state, subtree, pinfo, conv_key)
return true
end
elseif payload_len >= 16 then
-- For DIA/FM0, we require at least 16 bytes.
local text_len = tvb_payload(14,2):uint()
local total_dia_len = 16 + text_len
if payload_len >= total_dia_len then
-- We have enough data; let the DIA/FM0 dissector process it.
dissect_dia_fm0(state, subtree, pinfo, conv_key)
return true
end
end
end
return false
end
------------------------------
-- Dissector for one complete TCS packet.
------------------------------
local function dissect_tcs_pdu(tvb, pinfo, root)
local stx = tvb(0, 1):uint()
local count = tvb(1, 1):uint()
local complement = tvb(2, 1):uint()
local sequence = tvb(3, 1):uint()
local tcs_type = tvb(4, 1):uint()
local payload_len = count + 1
local payload = tvb:range(5, payload_len)
local crc_offset = 5 + payload_len
local crc = tvb(crc_offset, 2):le_uint()
local subtree = root:add(tcs_proto, tvb, "TCS")
subtree:add(tcs_proto.fields.stx, tvb(0, 1))
subtree:add(tcs_proto.fields.count, tvb(1, 1))
subtree:add(tcs_proto.fields.complement, tvb(2, 1))
subtree:add(tcs_proto.fields.sequence, tvb(3, 1))
subtree:add(tcs_proto.fields.tcs_type, tvb(4, 1))
subtree:add(tcs_proto.fields.direction)
local arrow = ""
local tcp_dstport_field = tcp_dstport_f()
if tcp_dstport_field then
local dst_port = tonumber(tostring(tcp_dstport_field))
if dst_port == 25235 then
arrow = "RS ▶ DS" -- "→" -- right arrow
else
arrow = "RS ◀ DS" -- "←" -- left arrow
end
else
arrow = "N/A"
end
subtree:add(tcs_proto.fields.direction, arrow)
if tcs_type > 1 and tcs_type < 8 then
-- If it's a protocol packet, show the sequence that it references
subtree:add(tcs_proto.fields.target, tvb(5,1))
else
-- Otherwise, show the data
subtree:add(tcs_proto.fields.data, tvb(5, payload_len))
end
subtree:add(tcs_proto.fields.crc, tvb(crc_offset, 2))
if stx ~= 0x02 then
subtree:add_expert_info(PI_MALFORMED, PI_WARN, "Invalid STX (expected 0x02)")
end
if complement ~= bit.band(bit.bnot(count), 0xFF) then
subtree:add_expert_info(PI_CHECKSUM, PI_WARN, "Complement field mismatch")
end
-- Compute CRC over bytes from offset 1 (Count) through the end of the Payload.
local cs_status = "OK"
local crc_data = tvb:range(1, count + 5):bytes():raw()
local crc_calc = crc16_x25.calculate(crc_data)
if crc_calc ~= crc then
cs_status = "Bad"
subtree:add_expert_info(PI_CHECKSUM, PI_WARN,
string.format("CRC check failed: computed 0x%04X, expected 0x%04X", crc_calc, crc))
end
subtree:add(tcs_proto.fields.checksum_status, cs_status)
-- Set the Info column based on the TCS type:
if tcs_type == 0 or tcs_type == 1 or tcs_type == 8 or tcs_type == 9 then
pinfo.cols.info:set(string.format("Seq=%d Type=%s Len=%d",
sequence, TCS_TYPES[tcs_type] or ("Unknown(" .. tcs_type .. ")"), count))
else
local first_data_byte = payload:range(0, 1):uint()
pinfo.cols.info:set(string.format("Seq=%d Type=%s (%d)",
sequence, TCS_TYPES[tcs_type] or ("Unknown(" .. tcs_type .. ")"), first_data_byte))
end
-- TODO need to reassemble both DIA and TOCS
-----------------------------------------------
-- Layer 3 reassembly logic:
--
-- A Layer 3 packet starts with a TCS packet of type UD1ACK (1) or UD1NAK (0)
-- and is continued by TCS packets of type UD2ACK (9) or UD2NAK (8), respectively.
-- The reassembled layer 3 packet is the concatenation of the TCS payload bytes.
--
-- When at least 16 bytes are available we can examine the layer 3 header’s
-- relevant length field and compute the total expected layer 3 packet length.
-----------------------------------------------
-- Retrieve or create the state for this conversation.
local stream_field = tcp_stream_f()
local conv_key = stream_field and tostring(stream_field.value) or tostring(pinfo.number)
local state = reassembly[conv_key]
if not state then
-- Create a new state container even before we know if the first packet is valid.
state = {
fragments = {}, -- will hold payloads keyed by sequence number
start_seq = nil, -- will be set when a valid UD1ACK is received
frame_numbers = {}
}
reassembly[conv_key] = state
end
-- If the CRC check fails, do nothing to state, just report error.
if cs_status ~= "OK" then
subtree:append_text(" (CRC error; fragment not added)")
return tvb:len()
end
-- Valid packet handling:
if tcs_type == 1 or tcs_type == 0 then
-- UD1ACK: Should be the start of a layer 3 packet.
-- Set the start_seq if not already set.
state.start_seq = sequence
subtree:append_text(" (Layer 3 start)")
-- Store this fragment.
state.fragments[sequence] = payload:bytes()
table.insert(state.frame_numbers, pinfo.number)
elseif tcs_type == 9 or tcs_type == 8 then
-- Continuation packet.
state.fragments[sequence] = payload:bytes()
table.insert(state.frame_numbers, pinfo.number)
subtree:append_text(" (Layer 3 continuation)")
end
-- Attempt to reassemble if we suspect we have enough data.
-- (Your logic here might check if the concatenated fragments, in order,
-- yield a payload that is at least long enough to inspect the layer 3 header.)
if try_reassemble_payload(state, subtree, pinfo, conv_key) then
reassembly[conv_key] = nil
end
--return tvb:len()
end
------------------------------
-- Main dissector function.
--
-- Walks through the TCP segment and reassembles one or more TCS packets.
------------------------------
function tcs_proto.dissector(tvb, pinfo, root)
pinfo.cols.protocol = "TCS"
local offset = 0
local tvb_len = tvb:len()
while offset < tvb_len do
local pdu_len = get_tcs_length(tvb, pinfo, offset)
if pdu_len == 0 then
return
end
local sub_tvb = tvb:range(offset, pdu_len)
dissect_tcs_pdu(sub_tvb, pinfo, root)
offset = offset + pdu_len
end
end
------------------------------
-- Register the dissector for TCP port 25235.
------------------------------
local tcp_port = DissectorTable.get("tcp.port")
tcp_port:add(25235, tcs_proto)