Files
MicrOBU/obu-firmware/test/pcap_gn_tally.py
T
Ashin Walpola 75d6d3b85c Receive signed ITS messages and forward each at its declared length
Signed packets. A GeoNetworking Basic Header NextHeader of 2 means a
TS 103 097 (IEEE 1609.2) envelope follows, with the Common Header
inside it. gn_unwrap_its rejected all of these, and most real traffic is
signed: the 2026-08-17 capture holds 157 signed frames from 15 source
MACs against 2 unsecured stations. It now opens a COER-encoded
signedData, or a bare unsecuredData, and parses the inner packet as
before. The inner packet comes first inside tbsData, so the certificate
and signature are never parsed, and the signature is not verified - the
firmware has no trust store. Such messages reach the phone with the new
V2X_RX flags bit1, signed but not verified. The app reads only bit0 and
is unaffected until it learns the flag. Encrypted payloads, nested
signing and the legacy v1.2.1 envelope are still rejected. All 157
recorded signed frames have the layout this reads, in all three COER
length forms, and asn1tools decodes every envelope to the same inner
packet.

Payload bounds. Every frame recorded through the ESP32-C5's promiscuous
RX, about 15 000 of them, ends in 8 bytes that are not part of the
802.11 frame and not a valid FCS. obu-firmware reads frames through the
same API and took the rest of the frame as the message, so it forwarded
those 8 bytes to the phone after every message. UPER decoders stop where
the message ends, so nothing visibly broke, but the bytes cost serial
bandwidth and 8 bytes of the DENM's headroom, and they stayed attached
wherever raw payloads were stored or passed on. The payload is now
exactly what the Common Header's payload-length field declares, which is
also what separates a signed message from its signature.

A frame longer than main.c's 800-byte capture buffer is now reported as
truncated instead of being forwarded cut off, and counted as an oversize
drop through the new serial_link_note_oversize_drop, as it was when the
cut-off frame failed serial_link's size check.

Host tests in obu-firmware/test/host build the firmware sources
unmodified with MSYS2 gcc; `make` runs all three.
- test_chain: frames from the firmware's TX code checked byte by byte
  against EN 302 636-4-1 and parsed back, including hand-built signed
  frames, the payload-length rule, the RX trailer, and every truncation
  length against a no-access guard page. 1731 checks, 0 failures.
- test_replay and check_replay.py: all 15 145 recorded frames through
  gn_unwrap_its, cut to 800 bytes as on the board, and re-derived
  independently in Python with the envelope decoded by asn1tools. They
  agree on every record; 15 131 accepted, 157 of them signed. 11 043 of
  the 11 106 distinct messages re-encode byte-identically. The other 63
  fail the same way with the old 8 bytes put back, so the boundary is
  not the cause: 5 are our own CAMs from before the 2026-08-20
  yawRateConfidence fix, and the rest, from other stations, are a
  follow-up in TODO.md.
- fuzz_gn_unwrap: random edits of every recorded frame, each run against
  the guard page. 50 000 000 iterations, no crash.

obu-firmware/test/pcap_gn_tally.py tallies GeoNetworking header fields
per station over captures; it is how the other stations' lifetimes were
measured. TODO.md collects what is still open, including the on-air
check for this change: it builds on IDF 6.1 but has not been flashed.
2026-09-11 20:19:40 +02:00

105 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Tally GeoNetworking header fields per sending station across .pcap captures.
Written to check the GN lifetime byte on air (see TODO.md), and it answers the general question
"what do real stations put in this header" too: one row per source MAC, packet type, BTP port and
lifetime byte, with a frame count.
python obu-firmware/test/pcap_gn_tally.py its-g5-receiver-firmware/recordings/*.pcap
Handles linktype 127 (radiotap, what its-g5-receiver-firmware records) and 105 (bare 802.11).
Standard library only. Pseudonym MACs rotate, so one vehicle can appear as several rows. The
pcap-over-serial dump path corrupts roughly 0.3% of frames, so a stray odd row is tooling noise.
"""
import collections
import glob
import struct
import sys
LLC_SNAP_GN = b"\xaa\xaa\x03\x00\x00\x00\x89\x47"
# (HeaderType, HeaderSubtype) from the GN Common Header -> name, EN 302 636-4-1 table 9.
HEADER_TYPES = {
(1, 0): "beacon",
(4, 0): "GBC-circle",
(4, 1): "GBC-rect",
(4, 2): "GBC-ellipse",
(5, 0): "SHB",
}
# Extended header length, i.e. the distance from the end of the Common Header to BTP-B.
EXT_LEN = {"SHB": 28, "GBC-circle": 44, "GBC-rect": 44, "GBC-ellipse": 44}
def lifetime_seconds(raw):
# Multiplier in the upper 6 bits, base in the lower 2: 50 ms, 1 s, 10 s, 100 s.
return (raw >> 2) * (0.05, 1, 10, 100)[raw & 3]
def frames(path):
with open(path, "rb") as f:
data = f.read()
if len(data) < 24:
return
magic = struct.unpack("<I", data[:4])[0]
endian = "<" if magic in (0xA1B2C3D4, 0xA1B23C4D) else ">"
linktype = struct.unpack(endian + "I", data[20:24])[0]
off = 24
while off + 16 <= len(data):
incl = struct.unpack(endian + "I", data[off + 8:off + 12])[0]
pkt = data[off + 16:off + 16 + incl]
off += 16 + incl
if linktype == 127:
if len(pkt) < 4:
continue
pkt = pkt[struct.unpack("<H", pkt[2:4])[0]:]
elif linktype != 105:
continue
yield pkt
def gn_fields(f):
if len(f) < 24 or (f[0] >> 2) & 3 != 2:
return None # Data frames only
o = 24 + (2 if f[0] & 0x80 else 0) # QoS Data carries a 2-byte QoS Control field
if f[o:o + 8] != LLC_SNAP_GN or len(f) < o + 12:
return None
o += 8
src = f[10:16].hex(":")
version, next_header, lifetime = f[o] >> 4, f[o] & 0x0F, f[o + 2]
port = "-"
if next_header == 2:
kind = "secured" # Common Header is inside the security envelope
elif next_header == 1 and len(f) >= o + 12:
c = o + 4
ht = (f[c + 1] >> 4, f[c + 1] & 0x0F)
kind = HEADER_TYPES.get(ht, "type %d/%d" % ht)
ext = EXT_LEN.get(kind)
btp = c + 8 + (ext or 0)
if ext and len(f) >= btp + 2:
port = str(struct.unpack(">H", f[btp:btp + 2])[0])
else:
kind = "nh=%d" % next_header
return src, version, kind, port, lifetime
def main(argv):
# PowerShell does not expand wildcards itself, so do it here.
paths = [p for arg in argv for p in (glob.glob(arg) or [arg])]
if not paths:
sys.exit(__doc__)
tally = collections.Counter()
for path in paths:
for frame in frames(path):
fields = gn_fields(frame)
if fields:
tally[fields] += 1
print("%-17s %3s %-11s %5s %8s %8s %7s"
% ("source", "ver", "packet", "port", "lifetime", "seconds", "frames"))
for (src, ver, kind, port, lt), n in sorted(tally.items()):
print("%-17s %3d %-11s %5s %8s %8g %7d"
% (src, ver, kind, port, "0x%02x" % lt, lifetime_seconds(lt), n))
if __name__ == "__main__":
main(sys.argv[1:])