Fragment large V2X_RX messages and decode road-RSU SPATEMs
The firmware dropped every received ITS message over 494 bytes (one link message), so a road RSU's 888-byte SPATEM and 1198-byte MAPEM never reached the phone. V2X_RX now goes out in link fragments up to 2300 bytes, best effort and non-blocking; messages up to 494 bytes are unchanged. The app's SPATEM decoder then refused those messages because they carry intersection and movement names and advisory speed lists. It now reads past both, checked against asn1tools on 718 distinct messages. Adds the pure fragmenter with host tests and a pcap replay, a Kotlin cross-check on the firmware's own fragments, tools/rx_size_compare.py, docs/08-large-message-reception.md and station-link revision 3.
This commit is contained in:
@@ -27,11 +27,11 @@ CFLAGS = -std=c11 -O2 -g -Wall -Wextra -Wpedantic -Werror -I$(FW) -I. \
|
||||
FW_SRCS = $(FW)/geonet.c dot11p.c $(FW)/gn_unwrap.c
|
||||
DEPS = test_util.c test_util.h dot11p.h $(FW_SRCS) $(wildcard $(FW)/*.h)
|
||||
|
||||
.PHONY: all check chain replay fuzz battery firmware corpus clean
|
||||
.PHONY: all check chain replay fuzz battery firmware fragments corpus clean
|
||||
|
||||
all: check
|
||||
|
||||
check: chain replay fuzz battery firmware
|
||||
check: chain replay fuzz battery firmware fragments
|
||||
|
||||
$(BUILD):
|
||||
mkdir -p $@
|
||||
@@ -83,6 +83,23 @@ $(BUILD)/test_firmware$(EXE): test_firmware.cpp $(FW_CXX_SRCS) $(wildcard $(FW)/
|
||||
firmware: $(BUILD)/test_firmware$(EXE)
|
||||
$(BUILD)/test_firmware$(EXE)
|
||||
|
||||
# Recorded traffic through unwrap -> V2X_RX body -> fragments -> reassembly (the large MAPEMs and SPATEMs of a road RSU).
|
||||
# Needs a capture: the CiT node's 2026-11-23 recording has 1198-byte MAPEMs and 888-byte SPATEMs.
|
||||
FRAG_PCAPS = $(wildcard ../../../databases/its5_20261123_150746.pcap) $(RECORDINGS)
|
||||
|
||||
$(BUILD)/fw_c_test_util.o: test_util.c test_util.h | $(BUILD)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
$(BUILD)/test_v2x_fragments$(EXE): test_v2x_fragments.cpp $(BUILD)/fw_c_test_util.o $(BUILD)/fw_c_gn_unwrap.o $(FW)/link_protocol.cpp $(wildcard $(FW)/*.hpp) | $(BUILD)
|
||||
$(CXX) $(CXXFLAGS) -std=c++20 -I. -fsanitize=undefined -fsanitize-undefined-trap-on-error -o $@ test_v2x_fragments.cpp $(FW)/link_protocol.cpp $(BUILD)/fw_c_test_util.o $(BUILD)/fw_c_gn_unwrap.o
|
||||
|
||||
fragments: $(BUILD)/test_v2x_fragments$(EXE)
|
||||
ifeq ($(FRAG_PCAPS),)
|
||||
@echo "fragments: no captures, skipped"
|
||||
else
|
||||
$(BUILD)/test_v2x_fragments$(EXE) $(FRAG_PCAPS)
|
||||
endif
|
||||
|
||||
# Corpus for the app's GnUnwrapTest (the Kotlin port of gn_unwrap.c): recorded frames, cut and corrupted copies of
|
||||
# them, with the verdict and fields of the firmware's own code. Writes ../../../app/src/test/resources/gn_unwrap_corpus.tsv
|
||||
CORPUS_OUT = ../../../app/src/test/resources/gn_unwrap_corpus.tsv
|
||||
|
||||
@@ -8,7 +8,7 @@ installed 2026-09-11).
|
||||
|
||||
**Firmware unit tests (`make firmware`, `test_firmware.cpp`, 40 tests):** the station-link codec byte-exact against
|
||||
the app's `StationLinkTest` hex, the 131-octet STATUS offsets, request reassembly, the V2X_RX extension
|
||||
(`main/v2x_rx.hpp`; the 494-octet limit, the 14-octet prefix, TX code -> air frame -> RX path), radio operating
|
||||
(`main/v2x_rx.hpp`; the 494-octet limit, the 14-octet prefix, TX code -> air frame -> RX path; the plan for large messages and `link_fragment.hpp`'s fragments, round-tripped through two reassemblers), radio operating
|
||||
points, the BLE TX guard, the ITS clock, CCA counters, BLE security and the battery policy. It prints PASS or FAIL
|
||||
per test and a `RESULT:` line, and exits non-zero on a failure. It cannot test the radio, BLE, NVS or USB: those are
|
||||
on the bench list in `TODO.md`.
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "battery_policy.hpp"
|
||||
#include "cca_window.hpp"
|
||||
#include "its_clock.hpp"
|
||||
#include "link_fragment.hpp"
|
||||
#include "link_protocol.hpp"
|
||||
#include "radio_policy.hpp"
|
||||
#include "raw_capture_policy.hpp"
|
||||
@@ -369,6 +370,125 @@ TEST(the_494_octet_limit_is_exact) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(v2x_rx_plan_keeps_small_messages_single_and_fragments_large_ones) {
|
||||
gn_rx_t rx {};
|
||||
const std::uint8_t byte = 0;
|
||||
rx.payload = &byte;
|
||||
const auto plan = [&](int len, bool raw_on, bool truncated = false) {
|
||||
rx.payload_len = len; rx.truncated = truncated;
|
||||
return link::v2x_rx_plan(rx, raw_on);
|
||||
};
|
||||
using P = link::V2xRxPlan;
|
||||
CHECK(plan(1, false) == P::single);
|
||||
CHECK(plan(58, false) == P::single);
|
||||
CHECK(plan(494, false) == P::single);
|
||||
CHECK(plan(495, false) == P::fragmented);
|
||||
CHECK(plan(888, false) == P::fragmented);
|
||||
CHECK(plan(1198, false) == P::fragmented);
|
||||
CHECK(plan(2300, false) == P::fragmented);
|
||||
CHECK(plan(2301, false) == P::drop);
|
||||
CHECK(plan(0, false) == P::drop);
|
||||
CHECK(plan(-1, false) == P::drop);
|
||||
CHECK(plan(600, false, true) == P::drop); // truncated is never forwarded
|
||||
// raw capture on: the app reads RAW_FRAME, so a large message is not sent twice; small ones are as before
|
||||
CHECK(plan(494, true) == P::single);
|
||||
CHECK(plan(495, true) == P::covered_by_raw);
|
||||
CHECK(plan(1198, true) == P::covered_by_raw);
|
||||
CHECK(plan(2301, true) == P::drop);
|
||||
}
|
||||
|
||||
// What a phone does with the fragments, written here without link::RequestReassembly.
|
||||
static bool put_back(const std::vector<link::Fragment>& fragments, Bytes& out) {
|
||||
out.clear();
|
||||
std::size_t total = 0;
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
||||
const auto& b = fragments[i].body;
|
||||
if (b.size() <= link::fragment_header_size) return false;
|
||||
const std::size_t t = b[0] | (std::size_t(b[1]) << 8), offset = b[2] | (std::size_t(b[3]) << 8);
|
||||
if (fragments[i].flags != (i == 0 ? 1 : i + 1 == fragments.size() ? 3 : 2)) return false;
|
||||
if (i == 0) total = t;
|
||||
if (t != total || offset != out.size()) return false;
|
||||
out.insert(out.end(), b.begin() + link::fragment_header_size, b.end());
|
||||
}
|
||||
return out.size() == total;
|
||||
}
|
||||
|
||||
TEST(fragments_of_a_large_v2x_rx_reassemble_exactly_on_both_transports) {
|
||||
for (const int len : {495, 504, 888, 1198, 2300}) {
|
||||
Bytes mapem(len);
|
||||
for (std::size_t i = 0; i < mapem.size(); ++i) mapem[i] = static_cast<std::uint8_t>(i * 31 + len);
|
||||
Bytes frame;
|
||||
CHECK(air_frame(mapem, 2003, frame));
|
||||
gn_rx_t rx;
|
||||
CHECK(gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx));
|
||||
CHECK_EQ(rx.payload_len, len);
|
||||
const auto body = link::encode_v2x_rx(rx, -48);
|
||||
for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) { // serial / MTU 517, MTU 247
|
||||
std::vector<link::Fragment> fragments;
|
||||
const bool split = link::split_fragments(body, limit, fragments, 16);
|
||||
CHECK(split);
|
||||
if (!split) continue;
|
||||
CHECK(fragments.size() >= 2);
|
||||
Bytes rebuilt;
|
||||
CHECK(put_back(fragments, rebuilt));
|
||||
CHECK(rebuilt == body);
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
||||
Bytes wire;
|
||||
CHECK(link::encode(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 5}, fragments[i].body}, wire));
|
||||
CHECK(wire.size() <= limit);
|
||||
if (i + 1 < fragments.size()) CHECK_EQ(wire.size(), limit); // full except the last
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(fragment_counts_for_the_bench_messages_at_the_full_mtu) {
|
||||
// 14 octets of prefix + the UPER payload; a fragment carries 512 - 4 - 4 = 504 octets of it
|
||||
std::vector<link::Fragment> f;
|
||||
CHECK(link::split_fragments(Bytes(14 + 888), 512, f)); CHECK_EQ(f.size(), 2u);
|
||||
CHECK(link::split_fragments(Bytes(14 + 1198), 512, f)); CHECK_EQ(f.size(), 3u);
|
||||
CHECK(link::split_fragments(Bytes(14 + 495), 512, f)); CHECK_EQ(f.size(), 2u);
|
||||
CHECK_STR(to_hex(Bytes(f[0].body.begin(), f[0].body.begin() + 4)), std::string("fd010000")); // total 509, offset 0
|
||||
CHECK_STR(to_hex(Bytes(f[1].body.begin(), f[1].body.begin() + 4)), std::string("fd01f801")); // total 509, offset 504
|
||||
}
|
||||
|
||||
TEST(a_message_that_fits_one_link_message_is_never_fragmented) {
|
||||
std::vector<link::Fragment> f;
|
||||
CHECK(!link::split_fragments(Bytes(508), 512, f)); // 508 + 4 = 512: exactly one message
|
||||
CHECK(f.empty());
|
||||
CHECK(link::split_fragments(Bytes(509), 512, f));
|
||||
CHECK(!link::split_fragments(Bytes(240), 244, f)); // MTU 247: 240 + 4 = 244
|
||||
CHECK(link::split_fragments(Bytes(241), 244, f));
|
||||
}
|
||||
|
||||
TEST(fragmenting_refuses_what_a_transport_cannot_carry_sensibly) {
|
||||
std::vector<link::Fragment> f;
|
||||
CHECK(!link::split_fragments(Bytes(1212), 20, f, 8)); // ATT MTU 23: 12 octets a fragment, 101 fragments
|
||||
CHECK(f.empty());
|
||||
CHECK(!link::split_fragments(Bytes(1212), 8, f)); // no room for data at all
|
||||
CHECK(!link::split_fragments(Bytes(65536), 512, f)); // total is a u16
|
||||
CHECK(link::split_fragments(Bytes(65535), 512, f));
|
||||
CHECK(link::split_fragments(Bytes(1212), 244, f, 6)); // exactly 6 fragments of 236
|
||||
CHECK(!link::split_fragments(Bytes(1212), 244, f, 5));
|
||||
}
|
||||
|
||||
TEST(a_lost_fragment_loses_that_message_only) {
|
||||
std::vector<link::Fragment> a, b;
|
||||
Bytes first(1212, 0xA1), second(1212, 0xB2);
|
||||
CHECK(link::split_fragments(first, 512, a));
|
||||
CHECK(link::split_fragments(second, 512, b));
|
||||
link::RequestReassembly phone;
|
||||
std::optional<link::Message> done;
|
||||
for (std::size_t i = 0; i < a.size(); ++i) {
|
||||
if (i == 1) continue; // the middle fragment never arrives
|
||||
done = phone.feed(link::Message {{link::Opcode::V2X_RX, a[i].flags, 1}, a[i].body}, 1, i);
|
||||
CHECK(!done);
|
||||
}
|
||||
for (std::size_t i = 0; i < b.size(); ++i) done = phone.feed(link::Message {{link::Opcode::V2X_RX, b[i].flags, 2}, b[i].body}, 1, 10 + i);
|
||||
CHECK(done.has_value());
|
||||
if (done) CHECK(done->body == second);
|
||||
}
|
||||
|
||||
TEST(ports_outside_the_its_set_are_not_forwarded) {
|
||||
Bytes frame;
|
||||
CHECK(air_frame(Bytes(20, 1), 1234, frame));
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
// Replays recorded air traffic through the whole V2X_RX send path that needs no ESP32: gn_unwrap_its ->
|
||||
// v2x_rx_plan -> encode_v2x_rx -> split_fragments -> link::encode of every fragment -> reassembly, and checks
|
||||
// that what a phone would reassemble is byte for byte the body that was sent. Messages that fit one link message
|
||||
// must stay one message and be byte-identical to the unfragmented encoding.
|
||||
//
|
||||
// Usage: test_v2x_fragments capture.pcap [capture.pcap ...]
|
||||
//
|
||||
// The reassembly here is written independently of link::RequestReassembly (which is also run, for bodies it
|
||||
// accepts) and of the app's Kotlin LinkReassembly (checked by LinkReassemblyTest on the same fragments).
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
extern "C" {
|
||||
#include "gn_unwrap.h"
|
||||
#include "test_util.h"
|
||||
}
|
||||
#include "link_fragment.hpp"
|
||||
#include "request_reassembly.hpp"
|
||||
#include "v2x_rx.hpp"
|
||||
|
||||
using namespace microbu;
|
||||
using link::Bytes;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int rx_frame_max_len = 2346; // c5_radio.cpp's Raw::bytes, as in test_replay.c
|
||||
|
||||
struct Stats {
|
||||
long records = 0, accepted = 0, single = 0, fragmented = 0, dropped = 0, fragments = 0, failures = 0;
|
||||
std::map<std::pair<unsigned, std::size_t>, long> by_port_size;
|
||||
};
|
||||
|
||||
FILE* dump = nullptr; // --dump out.tsv
|
||||
std::set<std::pair<unsigned, std::size_t>> dumped;
|
||||
|
||||
bool fail(Stats& s, const char* what, int index) {
|
||||
std::fprintf(stderr, "FAIL record %d: %s\n", index, what);
|
||||
++s.failures;
|
||||
return false;
|
||||
}
|
||||
|
||||
// What a phone does with the link messages of one body: independent of every other reassembler.
|
||||
bool reassemble(const std::vector<link::Message>& messages, Bytes& out) {
|
||||
out.clear();
|
||||
std::size_t total = 0;
|
||||
for (std::size_t i = 0; i < messages.size(); ++i) {
|
||||
const auto& m = messages[i];
|
||||
const auto& b = m.body;
|
||||
if (b.size() <= link::fragment_header_size) return false;
|
||||
const std::size_t t = b[0] | (std::size_t(b[1]) << 8), offset = b[2] | (std::size_t(b[3]) << 8);
|
||||
const std::uint8_t want = i == 0 ? 1 : (i + 1 == messages.size() ? 3 : 2);
|
||||
if (m.header.flags != want || m.header.opcode != link::Opcode::V2X_RX) return false;
|
||||
if (i == 0) total = t;
|
||||
if (t != total || offset != out.size()) return false;
|
||||
out.insert(out.end(), b.begin() + link::fragment_header_size, b.end());
|
||||
}
|
||||
return out.size() == total;
|
||||
}
|
||||
|
||||
void check(Stats& s, const gn_rx_t& rx, int index) {
|
||||
++s.accepted;
|
||||
const auto plan = link::v2x_rx_plan(rx, false);
|
||||
if (link::v2x_rx_plan(rx, true) == link::V2xRxPlan::fragmented) { fail(s, "raw capture on still fragments", index); return; }
|
||||
if (plan == link::V2xRxPlan::drop) { ++s.dropped; return; }
|
||||
const Bytes body = link::encode_v2x_rx(rx, -60);
|
||||
if (body.size() != link::v2x_rx_prefix + static_cast<std::size_t>(rx.payload_len)) { fail(s, "body size", index); return; }
|
||||
++s.by_port_size[{rx.btp_dest_port, static_cast<std::size_t>(rx.payload_len)}];
|
||||
|
||||
if (plan == link::V2xRxPlan::single) {
|
||||
++s.single;
|
||||
std::vector<link::Fragment> none;
|
||||
Bytes wire;
|
||||
if (link::split_fragments(body, link::maximum_message, none)) { fail(s, "a single message was fragmented", index); return; }
|
||||
if (!link::encode(link::Message {{link::Opcode::V2X_RX, 0, 7}, body}, wire) || wire.size() != body.size() + link::header_size)
|
||||
fail(s, "single message does not encode", index);
|
||||
return;
|
||||
}
|
||||
++s.fragmented;
|
||||
if (dump && dumped.insert({rx.btp_dest_port, static_cast<std::size_t>(rx.payload_len)}).second) {
|
||||
// For the app's LinkReassemblyTest: the first message of each large size, its body and the link messages
|
||||
// (hex) both transports would carry. `port size limit body wire wire ...`
|
||||
for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) {
|
||||
std::vector<link::Fragment> fragments;
|
||||
if (!link::split_fragments(body, limit, fragments, 8)) continue;
|
||||
std::fprintf(dump, "%u\t%d\t%zu\t", rx.btp_dest_port, rx.payload_len, limit);
|
||||
for (const auto b : body) std::fprintf(dump, "%02x", b);
|
||||
for (const auto& f : fragments) {
|
||||
Bytes wire;
|
||||
link::encode(link::Message {{link::Opcode::V2X_RX, f.flags, 77}, f.body}, wire);
|
||||
std::fputc('\t', dump);
|
||||
for (const auto b : wire) std::fprintf(dump, "%02x", b);
|
||||
}
|
||||
std::fputc('\n', dump);
|
||||
}
|
||||
}
|
||||
// 512: serial and the full BLE MTU 517. 244: ATT MTU 247. Both must carry the message; 20 (MTU 23) must refuse it.
|
||||
for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) {
|
||||
std::vector<link::Fragment> fragments;
|
||||
if (!link::split_fragments(body, limit, fragments, 8)) { fail(s, "split refused", index); return; }
|
||||
std::vector<link::Message> messages;
|
||||
link::RequestReassembly oracle;
|
||||
std::optional<link::Message> done;
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
||||
link::Message m {{link::Opcode::V2X_RX, fragments[i].flags, 9}, fragments[i].body};
|
||||
Bytes wire;
|
||||
if (!link::encode(m, wire) || wire.size() > limit) { fail(s, "fragment exceeds the transport limit", index); return; }
|
||||
link::Message back;
|
||||
if (!link::decode(wire, back)) { fail(s, "fragment does not decode", index); return; }
|
||||
messages.push_back(back);
|
||||
done = oracle.feed(back, 0, i);
|
||||
++s.fragments;
|
||||
}
|
||||
Bytes rebuilt;
|
||||
if (!reassemble(messages, rebuilt) || rebuilt != body) { fail(s, "independent reassembly differs", index); return; }
|
||||
if (body.size() <= link::maximum_request_body && (!done || done->body != body)) { fail(s, "RequestReassembly differs", index); return; }
|
||||
// One fragment lost anywhere but at the end, then the next message: nothing completes, nothing is poisoned.
|
||||
if (fragments.size() > 2) {
|
||||
link::RequestReassembly lossy;
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
||||
if (i == 1) continue;
|
||||
const auto got = lossy.feed(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 9}, fragments[i].body}, 0, i);
|
||||
if (got) { fail(s, "a message with a lost fragment completed", index); return; }
|
||||
}
|
||||
if (body.size() <= link::maximum_request_body) {
|
||||
std::optional<link::Message> next;
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i)
|
||||
next = lossy.feed(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 10}, fragments[i].body}, 0, 100 + i);
|
||||
if (!next || next->body != body) { fail(s, "the message after a lost fragment did not complete", index); return; }
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<link::Fragment> too_small;
|
||||
if (link::split_fragments(body, 20, too_small, 8)) fail(s, "MTU 23 should skip a large message", index);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int first = 1;
|
||||
if (argc > 2 && std::strcmp(argv[1], "--dump") == 0) {
|
||||
dump = std::fopen(argv[2], "w");
|
||||
if (!dump) { std::fprintf(stderr, "cannot write %s\n", argv[2]); return 2; }
|
||||
first = 3;
|
||||
}
|
||||
if (argc <= first) { std::fprintf(stderr, "usage: test_v2x_fragments [--dump out.tsv] capture.pcap [capture.pcap ...]\n"); return 2; }
|
||||
tu_install_crash_handler();
|
||||
tu_guard_init();
|
||||
Stats stats;
|
||||
for (int a = first; a < argc; ++a) {
|
||||
struct Ctx { Stats* s; const char* path; } ctx {&stats, argv[a]};
|
||||
const int n = tu_pcap_foreach(argv[a], [](const uint8_t* frame, int len, int index, void* c) {
|
||||
auto* x = static_cast<Ctx*>(c);
|
||||
++x->s->records;
|
||||
if (len > rx_frame_max_len) len = rx_frame_max_len;
|
||||
tu_set_context("fragments %s record %d (%d bytes)", x->path, index, len);
|
||||
gn_rx_t rx;
|
||||
if (gn_unwrap_its(tu_guarded(frame, len), len, &rx)) check(*x->s, rx, index);
|
||||
}, &ctx);
|
||||
if (n < 0) { std::fprintf(stderr, "cannot read %s as a pcap\n", argv[a]); return 2; }
|
||||
}
|
||||
std::printf("test_v2x_fragments: %ld records, %ld accepted: %ld single, %ld fragmented (%ld fragments built for "
|
||||
"two transports), %ld dropped, %ld failures\n",
|
||||
stats.records, stats.accepted, stats.single, stats.fragmented, stats.fragments, stats.dropped, stats.failures);
|
||||
for (const auto& [key, count] : stats.by_port_size)
|
||||
if (key.second > link::v2x_rx_max_payload) std::printf(" port %u, %zu bytes: %ld\n", key.first, key.second, count);
|
||||
return stats.failures ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user