sigrok-iso7816-stream sets GSMTAP_FLAG_BAD_FCS (0x01) in the GSMTAP header `res` byte on desynced TPDUs (mis-framed exchanges kept for forensics). Propagate that flag through the whole pipeline so such packets are explicitly surfaced instead of decoding as garbage: - gsmtap.py: GSMTAP_FLAG_BAD_FCS const; GsmtapReceiver.read_packet() now returns (sub_type, data, flags) from the res byte. - server/capture.py: GsmtapListener forwards the flags to the DB. - pcap.py: _parse_gsmtap_hdr/_scan_gsmtap/extract_gsmtap and parse_pcap/parse_pcapng now yield a (ts, type, payload, flags) tuple, so the flag survives a PCAP/PCAPNG round-trip. - server/server.py: PCAP import stores the flags. - server/decode.py: gsmtap_flag_names() maps bit 0x01 to 'bad FCS (desynced)'; merged into decoded errors for tpdu/atr/pps and rst/vcc, so the frontend ⚠ badge + Errors row flag the entry. User-visible: desynced TPDUs show a ⚠ "bad FCS (desynced)" marker in the trace and detail views. Tests: +4 (decode BAD_FCS markers, PCAP flag round-trip); existing tests updated for the new tuple arities. Version 1.22.0, SW cache simtrace-analyser-v32.
112 lines
3.6 KiB
Python
112 lines
3.6 KiB
Python
"""GSMTAP UDP sender for Wireshark integration.
|
|
|
|
GSMTAP header format (big-endian, 16 bytes). Based on libosmocore's gsmtap.h.
|
|
"""
|
|
|
|
import struct
|
|
import socket
|
|
|
|
GSMTAP_VERSION = 0x02
|
|
GSMTAP_HDR_LEN = 4 # in 32-bit words (16 bytes)
|
|
GSMTAP_TYPE_SIM = 0x04
|
|
|
|
GSMTAP_SIM_APDU = 0x00
|
|
GSMTAP_SIM_ATR = 0x01
|
|
GSMTAP_SIM_PPS_REQ = 0x02
|
|
GSMTAP_SIM_PPS_RSP = 0x03
|
|
# Custom sub-types from the sigrok-iso7816-stream decoder
|
|
# (github.com/anttro/sigrok_iso7816_stream): line-level events.
|
|
GSMTAP_SIM_RST_EVENT = 0x10
|
|
GSMTAP_SIM_VCC_EVENT = 0x11
|
|
|
|
# Per-packet flags carried in the GSMTAP header `res` byte (libosmocore
|
|
# gsmtap.h). sigrok-iso7816-stream sets BAD_FCS on a desynced TPDU.
|
|
GSMTAP_FLAG_BAD_FCS = 0x01
|
|
|
|
GSMTAP_UDP_PORT = 4729
|
|
|
|
_GSMTAP_HDR_FMT = '!BBBBHBBIBBBB' # 16 bytes, big-endian
|
|
_GSMTAP_HDR_SIZE = struct.calcsize(_GSMTAP_HDR_FMT)
|
|
|
|
|
|
def build_gsmtap_packet(sub_type, data, slot_nr=0):
|
|
"""Build a GSMTAP packet as bytes.
|
|
|
|
Returns *gsmtap_hdr* + *data* as a single bytes object.
|
|
"""
|
|
hdr = struct.pack(
|
|
_GSMTAP_HDR_FMT,
|
|
GSMTAP_VERSION, # version
|
|
GSMTAP_HDR_LEN, # hdr_len (in 32-bit words)
|
|
GSMTAP_TYPE_SIM, # type
|
|
0, # timeslot
|
|
0, # arfcn
|
|
0, # noise_db
|
|
0, # signal_db
|
|
0, # frame_number
|
|
sub_type, # sub_type
|
|
0, # antenna_nr
|
|
slot_nr, # sub_slot
|
|
0, # res
|
|
)
|
|
return hdr + data
|
|
|
|
|
|
class GsmtapSender:
|
|
"""Send ATR/APDU data as GSMTAP UDP packets."""
|
|
|
|
def __init__(self, host='127.0.0.1', port=GSMTAP_UDP_PORT):
|
|
self._addr = (host, port)
|
|
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
def send_atr(self, data, slot_nr=0):
|
|
packet = build_gsmtap_packet(GSMTAP_SIM_ATR, data, slot_nr)
|
|
self._sock.sendto(packet, self._addr)
|
|
|
|
def send_apdu(self, data, slot_nr=0):
|
|
packet = build_gsmtap_packet(GSMTAP_SIM_APDU, data, slot_nr)
|
|
self._sock.sendto(packet, self._addr)
|
|
|
|
def close(self):
|
|
self._sock.close()
|
|
|
|
|
|
class GsmtapReceiver:
|
|
"""Receive GSMTAP-SIM packets over UDP (compatible with simtrace2-sniff)."""
|
|
|
|
def __init__(self, bind_ip='127.0.0.1', bind_port=GSMTAP_UDP_PORT):
|
|
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
self._sock.bind((bind_ip, bind_port))
|
|
self._sock.settimeout(1.0)
|
|
|
|
def read_packet(self):
|
|
"""Block until a GSMTAP-SIM packet arrives.
|
|
|
|
Returns ``(sub_type, data, flags)`` on success or ``(None, None, 0)``
|
|
on timeout. *sub_type* is ``GSMTAP_SIM_ATR``/``GSMTAP_SIM_APDU`` or
|
|
a custom sub-type; *flags* are the per-packet bits from the GSMTAP
|
|
header ``res`` byte (see ``GSMTAP_FLAG_*``).
|
|
Raises ``ValueError`` for non-SIM or unknown GSMTAP packets.
|
|
"""
|
|
try:
|
|
packet, addr = self._sock.recvfrom(65536)
|
|
except (socket.timeout, OSError):
|
|
return None, None, 0
|
|
|
|
if len(packet) < _GSMTAP_HDR_SIZE:
|
|
raise ValueError(f'GSMTAP packet too short: {len(packet)} bytes')
|
|
|
|
(version, hdr_len, pkt_type, _timeslot, _arfcn, _noise, _signal,
|
|
_frame, sub_type, _antenna, sub_slot, res) = \
|
|
struct.unpack(_GSMTAP_HDR_FMT, packet[:_GSMTAP_HDR_SIZE])
|
|
|
|
if pkt_type != GSMTAP_TYPE_SIM:
|
|
raise ValueError(f'Not a GSMTAP SIM packet (type={pkt_type})')
|
|
|
|
data = packet[_GSMTAP_HDR_SIZE:]
|
|
return sub_type, data, res
|
|
|
|
def close(self):
|
|
self._sock.close()
|