Files
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

100 lines
3.7 KiB
C

// Replays recorded air traffic through gn_unwrap_its, one pcap record at a time and exactly as the
// promiscuous RX callback hands each frame over (the recordings come through the same API), and
// writes what came out to a TSV that check_replay.py verifies independently. See README.md.
//
// Usage: test_replay out.tsv capture.pcap [capture.pcap ...]
//
// One row per pcap record:
// file, record, accepted, port, signed, truncated, area, area_lat, area_lon, area_dist, payload
// with everything after `accepted` empty for rejected frames and the payload in hex. Each frame
// is placed against the guard page, and an accepted payload that is not inside its frame is a
// failure here already.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "gn_unwrap.h"
#include "test_util.h"
// main.c's RX_FRAME_MAX_LEN: the promiscuous callback copies at most this many bytes of a frame,
// so it is the most gn_unwrap_its ever sees on the board. Keep in step with main.c.
#define RX_FRAME_MAX_LEN 800
typedef struct {
FILE *out;
const char *path;
long records, capped, accepted, cam, denm, spatem, signed_frames, truncated, bad;
} replay_t;
static void on_frame(const uint8_t *frame, int len, int index, void *ctx)
{
replay_t *r = ctx;
r->records++;
if (len > RX_FRAME_MAX_LEN) {
len = RX_FRAME_MAX_LEN;
r->capped++;
}
tu_set_context("replay %s record %d (%d bytes)", r->path, index, len);
const uint8_t *g = tu_guarded(frame, len);
gn_rx_t rx;
const bool ok = gn_unwrap_its(g, len, &rx);
fprintf(r->out, "%s\t%d\t%d", r->path, index, ok);
if (ok) {
const uintptr_t lo = (uintptr_t)g;
const uintptr_t p = (uintptr_t)rx.payload;
if (p < lo || rx.payload_len <= 0 || p + (uintptr_t)rx.payload_len > lo + (uintptr_t)len) {
fprintf(stderr, "FAIL %s: payload outside the frame\n", tu_context());
r->bad++;
} else {
fprintf(r->out, "\t%u\t%d\t%d\t%d\t%ld\t%ld\t%u\t", rx.btp_dest_port, rx.signed_unverified,
rx.truncated, rx.has_geo_area, (long)rx.geo_area_lat_tenmicrodeg,
(long)rx.geo_area_lon_tenmicrodeg, rx.geo_area_distance_a_m);
for (int i = 0; i < rx.payload_len; i++) {
fprintf(r->out, "%02x", rx.payload[i]);
}
}
r->accepted++;
r->cam += rx.btp_dest_port == 2001;
r->denm += rx.btp_dest_port == 2002;
r->spatem += rx.btp_dest_port == 2004;
r->signed_frames += rx.signed_unverified;
r->truncated += rx.truncated;
}
fputc('\n', r->out);
}
int main(int argc, char **argv)
{
if (argc < 3) {
fprintf(stderr, "usage: test_replay out.tsv capture.pcap [capture.pcap ...]\n");
return 2;
}
tu_install_crash_handler();
tu_guard_init();
replay_t r = {0};
r.out = fopen(argv[1], "w");
if (!r.out) {
fprintf(stderr, "cannot write %s\n", argv[1]);
return 2;
}
fprintf(r.out, "file\trecord\taccepted\tport\tsigned\ttruncated\tarea\tarea_lat\tarea_lon\tarea_dist\tpayload\n");
int unreadable = 0;
for (int i = 2; i < argc; i++) {
r.path = argv[i];
if (tu_pcap_foreach(argv[i], on_frame, &r) < 0) {
fprintf(stderr, "cannot read %s as a pcap\n", argv[i]);
unreadable++;
}
}
fclose(r.out);
printf("test_replay: %ld records (%ld cut to %d bytes as main.c does), %ld accepted (CAM %ld, "
"DENM %ld, SPATEM %ld; %ld signed, %ld truncated), %ld bad\n",
r.records, r.capped, RX_FRAME_MAX_LEN, r.accepted, r.cam, r.denm, r.spatem,
r.signed_frames, r.truncated, r.bad);
return (r.bad || unreadable) ? 1 : 0;
}