From 9471751d37f6ae42a9c01767b33def5bccefd16c Mon Sep 17 00:00:00 2001 From: Igor Gudich Date: Sat, 26 Sep 2026 09:29:34 +0200 Subject: [PATCH 1/4] ef: add llc header for 802.2 framing Frames carrying an 802.2 LLC header are not Ethernet II: the type/length field holds a length, not an EtherType. Without an llc header the only way to express one is to hand-compute the length and pass the whole LLC payload as an opaque hex blob: eth ... et 0x0026 data hex 4242030000... which cannot be matched per field and has to be recomputed by hand whenever anything above it changes size. llc_fill_defaults sums its own 3 bytes plus every header above it and writes the result into the "et" field of the header below, following ipv4_fill_defaults for the summation and ef-icmp.c for reaching one step down the stack. That covers a VLAN tag between eth and llc too, since the tag carries the same field name last. Nothing was needed to stop eth_fill_defaults from clobbering the length: frame_to_buf runs frame_fill_defaults from the top of the stack downwards, so by the time ether_type_fill_defaults looks at "et" the value is already set and it returns early. An explicit "et" on the command line still wins, for the same reason. A computed length above 1500 is refused rather than written. 802.3 reads 1536 and up as an EtherType, so "llc stp data repeat 1500 0" would otherwise emit 0x0602 and a receiver would misparse it, and past 65535 the value would not fit parse_bytes(buf, 2) at all. An explicit "et" still wins, so a test can build the malformed frame on purpose. DSAP, SSAP and control default to the STP SAPs (0x42/0x42/0x03), the only current user. --- CMakeLists.txt | 2 + fuzz/ef.dict | 8 +++ src/ef-llc.c | 75 +++++++++++++++++++++++++ src/ef.c | 4 ++ src/ef.h | 1 + test/test-llc.cxx | 137 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 227 insertions(+) create mode 100644 src/ef-llc.c create mode 100644 test/test-llc.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index bbabf46..699fbd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ add_library(libef STATIC src/ef-igmp.c src/ef-ipv4.c src/ef-ipv6.c + src/ef-llc.c src/ef-mld.c src/ef-mrp.c src/ef-oam.c @@ -127,6 +128,7 @@ add_executable(ef-tests test/test-mld-zero-width.cxx test/test-rate-limit.cxx test/test-rx-ign.cxx + test/test-llc.cxx ) target_compile_options(ef-tests PRIVATE ${EF_SANITIZE_FLAGS}) diff --git a/fuzz/ef.dict b/fuzz/ef.dict index 65f33e8..1c50d6c 100644 --- a/fuzz/ef.dict +++ b/fuzz/ef.dict @@ -46,6 +46,9 @@ "mrp_topo" "mrp_tst" +# Headers LLC +"llc" + # Headers PTP "ptp-announce" "ptp-follow-up" @@ -168,6 +171,11 @@ "hdr-flagField" "hdr-logMessageInterval" +# Field names LLC +"dsap" +"ssap" +"ctrl" + # Field names OAM "opcode" "mel" diff --git a/src/ef-llc.c b/src/ef-llc.c new file mode 100644 index 0000000..9a7e638 --- /dev/null +++ b/src/ef-llc.c @@ -0,0 +1,75 @@ +#include +#include "ef.h" + +// IEEE 802.2 Logical Link Control. llc writes the 802.3 length into the +// "et" field of the header below it: eth, or the outermost VLAN tag. +static int llc_fill_defaults(struct frame *f, int stack_idx) { + char buf[16]; + int i, len = 0; + hdr_t *ll; + field_t *et; + + if (stack_idx < 1) + return 0; + + ll = f->stack[stack_idx - 1]; + et = find_field(ll, "et"); + + // An explicit "et" on the command line wins over the computed length. + if (!et || et->val) + return 0; + + for (i = stack_idx; i < f->stack_size; ++i) + len += f->stack[i]->size; + + // 802.3 clause 3.2.6: a type/length value above 1500 is not a length, + // and from 1536 up it is read as an EtherType. Refuse to emit one rather + // than silently write a frame the receiver will misparse. + if (len > 1500) { + pe("llc: 802.3 length %d exceeds 1500, set \"et\" explicitly\n", + len); + return -1; + } + + snprintf(buf, 16, "%d", len); + buf[15] = 0; + et->val = parse_bytes(buf, 2); + + return 0; +} + +static field_t LLC_FIELDS[] = { + { .name = "dsap", + .help = "Destination Service Access Point, e.g. 0x42 for STP", + .bit_width = 8 }, + { .name = "ssap", + .help = "Source Service Access Point, e.g. 0x42 for STP", + .bit_width = 8 }, + { .name = "ctrl", + .help = "Control, one byte for U-format frames, e.g. 0x03 for UI", + .bit_width = 8 }, +}; + +static hdr_t HDR_LLC = { + .name = "llc", + .help = "IEEE 802.2 LLC, sets the 802.3 length below it, e.g. llc stp", + .fields = LLC_FIELDS, + .fields_size = sizeof(LLC_FIELDS) / sizeof(LLC_FIELDS[0]), + .frame_fill_defaults = llc_fill_defaults, + .parser = hdr_parse_fields, +}; + +void llc_init() { + def_offset(&HDR_LLC); + def_val(&HDR_LLC, "dsap", "0x42"); + def_val(&HDR_LLC, "ssap", "0x42"); + def_val(&HDR_LLC, "ctrl", "0x03"); + + hdr_tmpls[HDR_TMPL_LLC] = &HDR_LLC; +} + +void llc_uninit() { + uninit_frame_data(&HDR_LLC); + + hdr_tmpls[HDR_TMPL_LLC] = 0; +} diff --git a/src/ef.c b/src/ef.c index 2d7060f..f398511 100644 --- a/src/ef.c +++ b/src/ef.c @@ -595,6 +595,7 @@ void profinet_init(); void opcua_init(); void coap_init(); void sv_init(); +void llc_init(); void init() __attribute__ ((constructor)); void init() { @@ -617,6 +618,7 @@ void init() { opcua_init(); coap_init(); sv_init(); + llc_init(); } void ifh_uninit(); @@ -638,6 +640,7 @@ void profinet_uninit(); void opcua_uninit(); void coap_uninit(); void sv_uninit(); +void llc_uninit(); void uninit() __attribute__ ((destructor)); void uninit() { @@ -660,5 +663,6 @@ void uninit() { opcua_uninit(); coap_uninit(); sv_uninit(); + llc_uninit(); } diff --git a/src/ef.h b/src/ef.h index cfbeb5a..d168247 100644 --- a/src/ef.h +++ b/src/ef.h @@ -307,6 +307,7 @@ typedef enum { HDR_TMPL_COAP_OPTIONS, HDR_TMPL_COAP_PARMS, HDR_TMPL_SV, + HDR_TMPL_LLC, HDR_TMPL_SIZE, } hdr_tmpl_t; diff --git a/test/test-llc.cxx b/test/test-llc.cxx new file mode 100644 index 0000000..c91a3ff --- /dev/null +++ b/test/test-llc.cxx @@ -0,0 +1,137 @@ +#include "ef.h" +#include "ef-test.h" +#include "catch_single_include.hxx" + +// An 802.2 LLC header is preceded by a length, not an EtherType. The tests +// below pin the length llc computes into the header below it, and the cases +// where it must keep its hands off that field. + +TEST_CASE("llc: 802.3 length counts llc plus everything above it", "[llc]") { + struct { + const char *what; + std::vector spec; + uint8_t hi, lo; + } cases[] = { + // llc(3) on its own + {"bare llc", {"eth", "llc"}, 0x00, 0x03}, + // llc(3) + 4 bytes of payload + {"llc + payload", {"eth", "llc", "data", "hex", "deadbeef"}, + 0x00, 0x07}, + // llc(3) + 46 bytes of payload, past the 60-byte pad boundary + {"llc + long payload", + {"eth", "llc", "data", "hex", + "000102030405060708090a0b0c0d0e0f" + "101112131415161718191a1b1c1d1e1f" + "202122232425262728292a2b2c2d"}, + 0x00, 0x31}, + }; + + for (auto &c : cases) { + SECTION(c.what) { + frame_t *f = parse_frame_wrap(c.spec); + REQUIRE(f != nullptr); + + buf_t *b = frame_to_buf(f); + REQUIRE(b != nullptr); + + // The length replaces the EtherType in the eth header, bytes 12:13. + CHECK(b->data[12] == c.hi); + CHECK(b->data[13] == c.lo); + + bfree(b); + frame_free(f); + } + } +} + +TEST_CASE("llc: default dsap/ssap/ctrl are the STP SAPs", "[llc]") { + frame_t *f = parse_frame_wrap({"eth", "llc"}); + REQUIRE(f != nullptr); + + buf_t *b = frame_to_buf(f); + REQUIRE(b != nullptr); + + CHECK(b->data[14] == 0x42); + CHECK(b->data[15] == 0x42); + CHECK(b->data[16] == 0x03); + + bfree(b); + frame_free(f); +} + +TEST_CASE("llc: an explicit et on the eth header wins", "[llc]") { + frame_t *f = parse_frame_wrap({"eth", "et", "0x1234", "llc"}); + REQUIRE(f != nullptr); + + buf_t *b = frame_to_buf(f); + REQUIRE(b != nullptr); + + // llc_fill_defaults must not overwrite a value the user asked for. + CHECK(b->data[12] == 0x12); + CHECK(b->data[13] == 0x34); + + bfree(b); + frame_free(f); +} + +TEST_CASE("llc: the length lands in the outermost vlan tag", "[llc]") { + frame_t *f = parse_frame_wrap({"eth", "ctag", "vid", "100", "llc", + "data", "hex", "deadbeef"}); + REQUIRE(f != nullptr); + + buf_t *b = frame_to_buf(f); + REQUIRE(b != nullptr); + + // eth.et becomes the ctag TPID, and the ctag's own "et" field at bytes + // 16:17 carries the 802.3 length of llc(3) + payload(4). + CHECK(b->data[12] == 0x81); + CHECK(b->data[13] == 0x00); + CHECK(b->data[16] == 0x00); + CHECK(b->data[17] == 0x07); + + bfree(b); + frame_free(f); +} + +TEST_CASE("llc: a length past 1500 is refused, not misencoded", "[llc]") { + // 1497 bytes of payload is the most an llc(3) frame can carry. One more + // would put 1501 in the length field, and 1536 or more reads as an + // EtherType; 65538 does not even fit in the field. + frame_t *max = parse_frame_wrap({"eth", "llc", "data", "repeat", "1497", + "0"}); + frame_t *over = parse_frame_wrap({"eth", "llc", "data", "repeat", "1498", + "0"}); + frame_t *wrap = parse_frame_wrap({"eth", "llc", "data", "repeat", + "65535", "0"}); + REQUIRE(max != nullptr); + REQUIRE(over != nullptr); + REQUIRE(wrap != nullptr); + + buf_t *b = frame_to_buf(max); + REQUIRE(b != nullptr); + CHECK(b->data[12] == 0x05); + CHECK(b->data[13] == 0xdc); + bfree(b); + + CHECK(frame_to_buf(over) == nullptr); + CHECK(frame_to_buf(wrap) == nullptr); + + frame_free(max); + frame_free(over); + frame_free(wrap); +} + +TEST_CASE("llc: an explicit et lifts the length limit", "[llc]") { + // A test that wants the malformed frame on purpose can still have it. + frame_t *f = parse_frame_wrap({"eth", "et", "0x0602", "llc", + "data", "repeat", "1500", "0"}); + REQUIRE(f != nullptr); + + buf_t *b = frame_to_buf(f); + REQUIRE(b != nullptr); + CHECK(b->data[12] == 0x06); + CHECK(b->data[13] == 0x02); + + bfree(b); + frame_free(f); +} From c44881f034d05d79a2337a0cdc2e5c76d4048a3e Mon Sep 17 00:00:00 2001 From: Igor Gudich Date: Sat, 26 Sep 2026 09:30:46 +0200 Subject: [PATCH 2/4] ef: add stp and stp_tcn headers for 802.1D BPDUs IEEE 802.1D-1998 clause 9.3 Bridge Protocol Data Units, on top of the llc header added in the previous commit: ef hex eth dmac 01:80:c2:00:00:00 smac 00:11:22:33:44:55 \ llc stp root_prio 0x8000 tc 1 Configuration and TCN are two header templates rather than one header with the trailing fields suppressed. The shrink-the-header trick in ef-sv.c and ef-mld.c drops a trailing optional part of one message; a TCN is a different message whose only content is the four-byte common part, and suppressing 31 of 35 fields would leave every config field name accepted on a TCN command line and silently ignored. The MRP precedent of one template per message type is the better fit. Each bridge identifier is split into its priority and MAC halves (root_prio/root_mac, bridge_prio/bridge_mac) so either can be asserted on its own. The four timer fields are exposed as their raw 16-bit wire value rather than converted to seconds, so a test can write the exact octets; the unit is in each field's help text. Their defaults, and those of the bridge identifiers, describe a root bridge advertising the table 8-3 recommended timers. tc and tcack are 1-bit fields aliased onto bit 0 and bit 7 of flags via explicit bit_offset, so either the whole byte or a single flag can be set. Under rx matching the two compose: "flags ign tc 1" masks the byte down to bit 0 and still requires it set. --- CMakeLists.txt | 2 + fuzz/corpus-argc-frame/eth_llc_stp | Bin 0 -> 48 bytes fuzz/corpus-argc-frame/eth_llc_stp_tcn | Bin 0 -> 47 bytes fuzz/ef.dict | 18 ++++ src/ef-stp.c | 120 +++++++++++++++++++++ src/ef.c | 4 + src/ef.h | 2 + test/test-stp.cxx | 141 +++++++++++++++++++++++++ 8 files changed, 287 insertions(+) create mode 100644 fuzz/corpus-argc-frame/eth_llc_stp create mode 100644 fuzz/corpus-argc-frame/eth_llc_stp_tcn create mode 100644 src/ef-stp.c create mode 100644 test/test-stp.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 699fbd7..879cbe2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,7 @@ add_library(libef STATIC src/ef-profinet.c src/ef-ptp.c src/ef-rate.c + src/ef-stp.c src/ef-sv.c src/ef-txring.c src/ef-udp.c @@ -129,6 +130,7 @@ add_executable(ef-tests test/test-rate-limit.cxx test/test-rx-ign.cxx test/test-llc.cxx + test/test-stp.cxx ) target_compile_options(ef-tests PRIVATE ${EF_SANITIZE_FLAGS}) diff --git a/fuzz/corpus-argc-frame/eth_llc_stp b/fuzz/corpus-argc-frame/eth_llc_stp new file mode 100644 index 0000000000000000000000000000000000000000..71c4226b8eda8f3543b6711ab3ee0f3cc9397fad GIT binary patch literal 48 wcmYc-$zVvyO-yDmFtoBTuu3+vGBAK3hGLMgm6ahwPEImIaY+F~Niu^W05et$S^xk5 literal 0 HcmV?d00001 diff --git a/fuzz/corpus-argc-frame/eth_llc_stp_tcn b/fuzz/corpus-argc-frame/eth_llc_stp_tcn new file mode 100644 index 0000000000000000000000000000000000000000..b25bcab43dd317e6062564dbb549118a457ef008 GIT binary patch literal 47 vcmYc-$zVvyO-yDmFtoBTuu3+vGBAK3hGLMgm6ahwPEImIaY;dZNpc + +// IEEE 802.1D-1998 clause 9.3 BPDUs. A BPDU is not an Ethernet II frame: the +// type/length field carries a length, followed by an 802.2 LLC header and the +// BPDU body. These tests pin the byte layout of both BPDU types. + +// ef pads short frames to 60 bytes. Append the zeros rather than spelling them +// out, so the expectation says "this body, then padding" and cannot drift. +static std::string padded(const std::string &body) { + size_t bytes = body.size() / 2; + REQUIRE(body.size() % 2 == 0); + REQUIRE(bytes <= 60); + return body + std::string((60 - bytes) * 2, '0'); +} + +// The frame a real bridge emits, byte for byte. +static const char *bpdu_config_body = + "0180c20000000011223344550026" // eth, 802.3 length 38 + "424203" // llc dsap/ssap/ctrl + "0000" "00" "00" "00" // proto, ver, type, flags + "8000" "001122334400" // root id + "00000000" // root path cost + "8000" "001122334400" // bridge id + "8001" // port id + "0000" "1400" "0200" "0f00"; // msg age, max age, hello, fwd + +TEST_CASE("stp: config BPDU byte layout matches a real bridge", "[stp]") { + frame_t *f = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", + "stp", "root_mac", "00:11:22:33:44:00", + "bridge_mac", "00:11:22:33:44:00", + }); + REQUIRE(f != nullptr); + + // hexstr() frees the buf it is handed. + CHECK(hexstr(frame_to_buf(f)) == padded(bpdu_config_body)); + + frame_free(f); +} + +TEST_CASE("stp: every config field can be set explicitly", "[stp]") { + frame_t *f = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "dsap", "0x42", "ssap", "0x42", "ctrl", "0x03", + "stp", "proto", "0x0000", "ver", "0", "type", "0", "flags", "0", + "root_prio", "0x8000", "root_mac", "00:11:22:33:44:00", + "root_cost", "0", + "bridge_prio", "0x8000", "bridge_mac", "00:11:22:33:44:00", + "port", "0x8001", + "msg_age", "0x0000", "max_age", "0x1400", + "hello", "0x0200", "fwd_delay", "0x0f00", + }); + REQUIRE(f != nullptr); + + // Spelling out every field must land on the same bytes as the defaults. + CHECK(hexstr(frame_to_buf(f)) == padded(bpdu_config_body)); + + frame_free(f); +} + +TEST_CASE("stp: tc and tcack are bit 0 and bit 7 of flags", "[stp]") { + frame_t *tc = parse_frame_wrap({"eth", "llc", "stp", "tc", "1"}); + frame_t *ack = parse_frame_wrap({"eth", "llc", "stp", "tcack", "1"}); + frame_t *both = parse_frame_wrap({"eth", "llc", "stp", "flags", "0x81"}); + REQUIRE(tc != nullptr); + REQUIRE(ack != nullptr); + REQUIRE(both != nullptr); + + buf_t *b_tc = frame_to_buf(tc); + buf_t *b_ack = frame_to_buf(ack); + buf_t *b_both = frame_to_buf(both); + REQUIRE(b_tc != nullptr); + REQUIRE(b_ack != nullptr); + REQUIRE(b_both != nullptr); + + // flags sits at eth(14) + llc(3) + proto/ver/type(4) = offset 21. + CHECK(b_tc->data[21] == 0x01); + CHECK(b_ack->data[21] == 0x80); + CHECK(b_both->data[21] == 0x81); + + bfree(b_tc); + bfree(b_ack); + bfree(b_both); + frame_free(tc); + frame_free(ack); + frame_free(both); +} + +TEST_CASE("stp: TCN BPDU is a four-byte body", "[stp]") { + frame_t *f = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "stp_tcn", + }); + REQUIRE(f != nullptr); + + CHECK(hexstr(frame_to_buf(f)) == + padded("0180c20000000011223344550007" // eth, 802.3 length 7 + "424203" // llc + "00000080")); // proto, ver, type=TCN + + frame_free(f); +} + +TEST_CASE("stp: a config BPDU can be matched field by field", "[stp]") { + // What an "rx" command builds: a value buf and a mask with every + // specified field set to all-ones. Mirrors argc_cmd() in ef-args.c. + frame_t *exact = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "stp", "tc", "1", + }); + frame_t *masked = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "stp", "flags", "ign", "tc", "1", + }); + REQUIRE(exact != nullptr); + REQUIRE(masked != nullptr); + CHECK(masked->has_mask == 1); + + buf_t *b_exact = frame_to_buf(exact); + buf_t *b_masked = frame_to_buf(masked); + buf_t *mask = frame_mask_to_buf(masked); + REQUIRE(b_exact != nullptr); + REQUIRE(b_masked != nullptr); + REQUIRE(mask != nullptr); + + // "flags ign tc 1" must still pin tc, so only bit 0 of the flags byte + // is compared and the received value must have it set. + CHECK(mask->data[21] == 0x81); + CHECK(bequal_mask(b_exact, b_masked, mask, 0) == 1); + + bfree(b_exact); + bfree(b_masked); + bfree(mask); + frame_free(exact); + frame_free(masked); +} From f2b70a57b795c2090031715916a9e8bc9e2dc5f1 Mon Sep 17 00:00:00 2001 From: Igor Gudich Date: Mon, 19 Oct 2026 13:24:39 +0200 Subject: [PATCH 3/4] ef: model the whole bpdu flags octet Only bit 0 (tc) and bit 7 (tcack) were named. The six bits between them are what distinguishes the rapid protocol from classic STP -- they carry the proposal/agreement handshake of 802.1Q clause 13.35 and the sender's port role -- and none of that could be expressed except by spelling out the whole byte: ef hex eth llc stp flags 0x3e which says nothing about which bits matter. The eight fields of 802.1Q table 13-5 are now all named: proposal, role, learning, forwarding and agreement alongside the existing two. role is two bits wide rather than two more one-bit aliases, since the pair is a single enumerated value: 0 unknown, 1 alternate or backup, 2 root, 3 designated. Every alias carries rx_match_skip so that it only enters the receive mask when it is named. Without it an alias is masked in by default, and "flags ign" cannot narrow the byte: it already produced a mask of 0x81 rather than the 0x01 its own test comment described, and with six more aliases would have produced 0xff, making the "ign" useless. The assertion is corrected to the value the comment always claimed. rx_match_skip alone would make "tc ign" a no-op: an ignored field writes nothing to the mask, so the flags byte around it would still be compared in full and a BPDU differing only in tc rejected. The aliases are therefore marked as overlays, and an overlay given "ign" (tracked by a new rx_match_ign) writes zeros over its bits once the rest of the mask is built. "tc ign" compares every flag bit but tc, and "flags ign agreement 1" still compares agreement alone. Only overlays do this. IFH headers overlap fields too, but as alternative encodings of the same bits, and there an ignored field must not erase the mask of one that is compared: "ifh-oc1 ign b1-rew-op 0 b0-masq-port ign" keeps b1-rew-op compared in full. Every header without overlay fields gets the mask it had before, byte for byte. --- fuzz/ef.dict | 5 ++ src/ef-stp.c | 41 +++++++++- src/ef.c | 20 ++++- src/ef.h | 7 ++ test/test-stp.cxx | 202 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 269 insertions(+), 6 deletions(-) diff --git a/fuzz/ef.dict b/fuzz/ef.dict index 8a43bb0..f89ec11 100644 --- a/fuzz/ef.dict +++ b/fuzz/ef.dict @@ -182,6 +182,11 @@ # Field names STP "tc" +"proposal" +"role" +"learning" +"forwarding" +"agreement" "tcack" "root_prio" "root_mac" diff --git a/src/ef-stp.c b/src/ef-stp.c index 5d91818..57a859c 100644 --- a/src/ef-stp.c +++ b/src/ef-stp.c @@ -14,16 +14,51 @@ static field_t STP_FIELDS[] = { .help = "BPDU type, 0x00 for configuration", .bit_width = 8 }, { .name = "flags", - .help = "Flags, bit 0 is topology change, bit 7 topology change ack", + .help = "Flags, see the eight single-bit fields below", .bit_width = 8 }, { .name = "tc", .help = "Topology change flag, bit 0 of flags", .bit_width = 1, - .bit_offset = 39 }, + .bit_offset = 39, + .rx_match_skip = 1, + .overlay = 1 }, + { .name = "proposal", + .help = "Proposal flag, bit 1 of flags", + .bit_width = 1, + .bit_offset = 38, + .rx_match_skip = 1, + .overlay = 1 }, + { .name = "role", + .help = "Port role, bits 2-3 of flags: 0 unknown, 1 alternate or " + "backup, 2 root, 3 designated", + .bit_width = 2, + .bit_offset = 36, + .rx_match_skip = 1, + .overlay = 1 }, + { .name = "learning", + .help = "Learning flag, bit 4 of flags", + .bit_width = 1, + .bit_offset = 35, + .rx_match_skip = 1, + .overlay = 1 }, + { .name = "forwarding", + .help = "Forwarding flag, bit 5 of flags", + .bit_width = 1, + .bit_offset = 34, + .rx_match_skip = 1, + .overlay = 1 }, + { .name = "agreement", + .help = "Agreement flag, bit 6 of flags", + .bit_width = 1, + .bit_offset = 33, + .rx_match_skip = 1, + .overlay = 1 }, { .name = "tcack", .help = "Topology change acknowledgement flag, bit 7 of flags", .bit_width = 1, - .bit_offset = 32 }, + .bit_offset = 32, + .rx_match_skip = 1, + .overlay = 1 }, { .name = "root_prio", .help = "Root identifier priority, e.g. 0x8000", .bit_width = 16 }, diff --git a/src/ef.c b/src/ef.c index 7b4a0cb..1642e30 100644 --- a/src/ef.c +++ b/src/ef.c @@ -349,6 +349,7 @@ int hdr_parse_fields(frame_t *frame, struct hdr *hdr, int offset, if (field_ignore) { field_ignore = 0; f->rx_match_skip = 1; + f->rx_match_ign = 1; i += 1; continue; } @@ -369,6 +370,7 @@ int hdr_parse_fields(frame_t *frame, struct hdr *hdr, int offset, if (strcmp(argv[i], "ign") == 0 || strcmp(argv[i], "ignore") == 0) { frame->has_mask = 1; f->rx_match_skip = 1; + f->rx_match_ign = 1; i += 1; continue; } @@ -397,6 +399,7 @@ int hdr_parse_fields(frame_t *frame, struct hdr *hdr, int offset, i += 1; } f->rx_match_skip = 0; + f->rx_match_ign = 0; } return i; @@ -447,7 +450,22 @@ static int hdr_copy_to_buf_(hdr_t *hdr, size_t offset, buf_t *buf, int mask) { } } - return i; + // An overlay field (a flag bit inside a flags byte) that was given "ign" + // must take its bits back out of the byte around it, which the loop + // above may have masked in. Only overlays do this: elsewhere, overlapping + // fields are alternative encodings and an ignored one must not erase the + // bits of another that is compared. + for (i = 0, f = hdr->fields; mask && i < hdr->fields_size; ++i, ++f) { + if (!f->overlay || !f->rx_match_ign) + continue; + + maskb = balloc(BIT_TO_BYTE(f->bit_width)); + hdr_write_field(buf, offset, f, maskb); + bfree(maskb); + maskb = 0; + } + + return hdr->fields_size; } int hdr_copy_to_buf_mask(hdr_t *hdr, size_t offset, buf_t *buf) { diff --git a/src/ef.h b/src/ef.h index 4e988ac..e0120e4 100644 --- a/src/ef.h +++ b/src/ef.h @@ -163,6 +163,13 @@ typedef struct field { const char *name; const char *help; int rx_match_skip; + // Set when the field itself was given "ign". rx_match_skip alone only + // keeps a field from adding to the rx mask; an overlay field also has to + // take its bits back out of the field it sits inside. + int rx_match_ign; + // The field is a view of bits that another field of the header already + // covers, e.g. a flag bit inside a flags byte. See hdr_copy_to_buf_mask. + int overlay; int bit_width; int bit_offset; buf_t *def; diff --git a/test/test-stp.cxx b/test/test-stp.cxx index 06ec036..2400365 100644 --- a/test/test-stp.cxx +++ b/test/test-stp.cxx @@ -91,6 +91,203 @@ TEST_CASE("stp: tc and tcack are bit 0 and bit 7 of flags", "[stp]") { frame_free(both); } +// The six bits between tc and tcack, which only the rapid protocol sets. +// 802.1Q table 13-5, read against the flags octet at offset 21. +TEST_CASE("stp: every flag bit lands in its own bit of the flags octet", + "[stp]") { + struct { const char *field; const char *value; uint8_t expect; } cases[] = { + { "tc", "1", 0x01 }, + { "proposal", "1", 0x02 }, + { "role", "1", 0x04 }, // alternate or backup + { "role", "2", 0x08 }, // root + { "role", "3", 0x0c }, // designated + { "learning", "1", 0x10 }, + { "forwarding", "1", 0x20 }, + { "agreement", "1", 0x40 }, + { "tcack", "1", 0x80 }, + }; + + for (auto &c : cases) { + frame_t *f = parse_frame_wrap({"eth", "llc", "stp", c.field, c.value}); + REQUIRE(f != nullptr); + + buf_t *b = frame_to_buf(f); + REQUIRE(b != nullptr); + + // Only the named bit may be set, so the whole octet is the expectation. + INFO("stp " << c.field << " " << c.value); + CHECK(b->data[21] == c.expect); + + bfree(b); + frame_free(f); + } +} + +TEST_CASE("stp: a designated port proposing sets four flag bits", "[stp]") { + // What a designated port offers at the start of the clause 13.35 + // handshake: proposal, role designated, learning and forwarding. + frame_t *named = parse_frame_wrap({ + "eth", "llc", + "stp", "proposal", "1", "role", "3", + "learning", "1", "forwarding", "1", + }); + frame_t *byte = parse_frame_wrap({"eth", "llc", "stp", "flags", "0x3e"}); + REQUIRE(named != nullptr); + REQUIRE(byte != nullptr); + + buf_t *b_named = frame_to_buf(named); + buf_t *b_byte = frame_to_buf(byte); + REQUIRE(b_named != nullptr); + REQUIRE(b_byte != nullptr); + + // Naming the bits and spelling out the byte must agree. + CHECK(b_named->data[21] == 0x3e); + CHECK(bequal(b_named, b_byte) == 1); + + bfree(b_named); + bfree(b_byte); + frame_free(named); + frame_free(byte); +} + +TEST_CASE("stp: an unnamed flag bit stays out of the rx mask", "[stp]") { + // Each bit alias overlays a byte that is itself a field, so an alias + // carries rx_match_skip and only enters the mask when it is named. + // Without that, "flags ign" could not narrow the byte at all. + frame_t *one = parse_frame_wrap({ + "eth", "llc", "stp", "flags", "ign", "agreement", "1", + }); + frame_t *two = parse_frame_wrap({ + "eth", "llc", "stp", "flags", "ign", "agreement", "1", "role", "3", + }); + REQUIRE(one != nullptr); + REQUIRE(two != nullptr); + + buf_t *m_one = frame_mask_to_buf(one); + buf_t *m_two = frame_mask_to_buf(two); + REQUIRE(m_one != nullptr); + REQUIRE(m_two != nullptr); + + CHECK(m_one->data[21] == 0x40); // agreement alone + CHECK(m_two->data[21] == 0x4c); // agreement and the two role bits + + bfree(m_one); + bfree(m_two); + frame_free(one); + frame_free(two); +} + +TEST_CASE("stp: ignoring a flag bit takes it out of the rx mask", "[stp]") { + // "flags" is itself in the mask unless ignored, so an ignored bit alias + // has to clear its own bits from it rather than merely add nothing. + struct { const char *field; uint8_t expect; } cases[] = { + { "tc", 0xfe }, + { "proposal", 0xfd }, + { "role", 0xf3 }, + { "learning", 0xef }, + { "forwarding", 0xdf }, + { "agreement", 0xbf }, + { "tcack", 0x7f }, + }; + + for (const char *hdr : {"stp"}) { + for (auto &c : cases) { + frame_t *f = parse_frame_wrap({"eth", "llc", hdr, c.field, "ign"}); + REQUIRE(f != nullptr); + CHECK(f->has_mask == 1); + + buf_t *mask = frame_mask_to_buf(f); + REQUIRE(mask != nullptr); + + INFO(hdr << " " << c.field << " ign"); + CHECK(mask->data[21] == c.expect); + // The rest of the BPDU is still compared. + CHECK(mask->data[20] == 0xff); + CHECK(mask->data[22] == 0xff); + + bfree(mask); + frame_free(f); + } + } +} + +TEST_CASE("stp: a BPDU differing only in an ignored bit still matches", + "[stp]") { + // "tc ign" must accept a BPDU whatever its tc bit, which it cannot do if + // the flags byte around it is still compared in full. + frame_t *expect = parse_frame_wrap({"eth", "llc", "stp", "tc", "ign"}); + frame_t *rx_tc = parse_frame_wrap({"eth", "llc", "stp", "tc", "1"}); + frame_t *rx_ack = parse_frame_wrap({"eth", "llc", "stp", "tcack", "1"}); + REQUIRE(expect != nullptr); + REQUIRE(rx_tc != nullptr); + REQUIRE(rx_ack != nullptr); + + buf_t *b_expect = frame_to_buf(expect); + buf_t *mask = frame_mask_to_buf(expect); + buf_t *b_tc = frame_to_buf(rx_tc); + buf_t *b_ack = frame_to_buf(rx_ack); + REQUIRE(b_expect != nullptr); + REQUIRE(mask != nullptr); + REQUIRE(b_tc != nullptr); + REQUIRE(b_ack != nullptr); + + CHECK(bequal_mask(b_tc, b_expect, mask, 0) == 1); + // Ignoring tc must not loosen the other seven bits. + CHECK(bequal_mask(b_ack, b_expect, mask, 0) == 0); + + bfree(b_expect); + bfree(mask); + bfree(b_tc); + bfree(b_ack); + frame_free(expect); + frame_free(rx_tc); + frame_free(rx_ack); +} + +TEST_CASE("stp: an ignored bit clears the mask whatever the field order", + "[stp]") { + // flags comes before tc in the field list, but the bit must come out of + // an explicitly-set flags byte however the command line orders them. + frame_t *a = parse_frame_wrap({"eth", "llc", "stp", "flags", "0x81", + "tc", "ign"}); + frame_t *b = parse_frame_wrap({"eth", "llc", "stp", "tc", "ign", + "flags", "0x81"}); + REQUIRE(a != nullptr); + REQUIRE(b != nullptr); + + buf_t *m_a = frame_mask_to_buf(a); + buf_t *m_b = frame_mask_to_buf(b); + REQUIRE(m_a != nullptr); + REQUIRE(m_b != nullptr); + + CHECK(m_a->data[21] == 0xfe); + CHECK(m_b->data[21] == 0xfe); + + bfree(m_a); + bfree(m_b); + frame_free(a); + frame_free(b); +} + +TEST_CASE("stp: ignoring an overlapping non-overlay field changes nothing", + "[stp]") { + // IFH fields overlap as alternative encodings of the same bits, not as + // views of a wider field. Ignoring one there must not erase the mask of + // another that is compared: b1-rew-op stays compared in full. + frame_t *f = parse_frame_wrap({"ifh-oc1", "ign", "b1-rew-op", "0", + "b0-masq-port", "ign", "eth"}); + REQUIRE(f != nullptr); + + buf_t *mask = frame_mask_to_buf(f); + REQUIRE(mask != nullptr); + + CHECK(mask->data[0] == 0x3f); + CHECK(mask->data[1] == 0xe0); + + bfree(mask); + frame_free(f); +} + TEST_CASE("stp: TCN BPDU is a four-byte body", "[stp]") { frame_t *f = parse_frame_wrap({ "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", @@ -129,8 +326,9 @@ TEST_CASE("stp: a config BPDU can be matched field by field", "[stp]") { REQUIRE(mask != nullptr); // "flags ign tc 1" must still pin tc, so only bit 0 of the flags byte - // is compared and the received value must have it set. - CHECK(mask->data[21] == 0x81); + // is compared and the received value must have it set. The other seven + // bit aliases carry rx_match_skip, so an unnamed one adds nothing. + CHECK(mask->data[21] == 0x01); CHECK(bequal_mask(b_exact, b_masked, mask, 0) == 1); bfree(b_exact); From affed37b4d80abef44f7eb230f3b317a2c7a844c Mon Sep 17 00:00:00 2001 From: Igor Gudich Date: Mon, 19 Oct 2026 13:29:26 +0200 Subject: [PATCH 4/4] ef: add stp_rst header for 802.1Q RST BPDUs The rapid protocol's own BPDU could not be built. Setting "ver 2" on the stp header changed the version octet and nothing else, leaving a 35-octet body where 802.1Q clause 14.4 requires 36 -- the configuration BPDU's 35 followed by the Version 1 Length, transmitted as 0. The receive rule in that clause is version 2, type 0x02 and 36 or more octets, so what the stp header produced was discarded as malformed by anything implementing it: ef hex eth dmac 01:80:c2:00:00:00 smac 00:11:22:33:44:55 \ llc stp_rst role 3 proposal 1 learning 1 forwarding 1 A third template rather than a longer HDR_STP, for two reasons. A permanently 36-byte HDR_STP would make every configuration BPDU malformed instead. Growing it on demand from a frame_fill_defaults hook is not available either: a hook can shrink a header, which is what ef-sv.c does to drop a trailing TLV, but frame_mask_to_buf() never runs those hooks, so the value buf and the mask buf would disagree on the header's size and rx matching would write past the shorter one. The existing argument for keeping stp and stp_tcn apart therefore carries over unchanged, and the MRP precedent of one template per message type now holds for three. The field list is duplicated rather than shared, following ef-mrp.c and ef-oam.c, where the common fields of four and five templates are spelled out in each. A shared array cannot work here regardless: def_offset() assigns bit_offset per header, so one array behind two templates would have the second pass overwrite the first's offsets. ver and type default to 2 and 0x02, and v1_len to 0; everything else carries the same defaults as the configuration BPDU, so the two differ on the wire only where the standard says they should. --- fuzz/corpus-argc-frame/eth_llc_stp_rst | Bin 0 -> 65 bytes fuzz/ef.dict | 2 + src/ef-stp.c | 117 ++++++++++++++++ src/ef.h | 1 + test/test-stp.cxx | 176 ++++++++++++++++++++++++- 5 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 fuzz/corpus-argc-frame/eth_llc_stp_rst diff --git a/fuzz/corpus-argc-frame/eth_llc_stp_rst b/fuzz/corpus-argc-frame/eth_llc_stp_rst new file mode 100644 index 0000000000000000000000000000000000000000..b91a490ae4bccb0b8ed10e57ceaa07a5a8e3b456 GIT binary patch literal 65 zcmXBIK?;CC2nEo8B&|(&nHX%Kg`ueTXNZBfdb3|V-%t?CMWTlgZK%IR7-ovsize - 17 == 35); + CHECK(b_rst->size - 17 == 36); + + // The 802.3 length counts the LLC header plus the body, so it moves too. + CHECK(b_cfg->data[12] == 0x00); + CHECK(b_cfg->data[13] == 0x26); + CHECK(b_rst->data[12] == 0x00); + CHECK(b_rst->data[13] == 0x27); + + bfree(b_rst); + bfree(b_cfg); + frame_free(rst); + frame_free(cfg); +} + +TEST_CASE("stp: every RST field can be set explicitly", "[stp]") { + frame_t *f = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "dsap", "0x42", "ssap", "0x42", "ctrl", "0x03", + "stp_rst", "proto", "0x0000", "ver", "2", "type", "0x02", "flags", "0", + "root_prio", "0x8000", "root_mac", "00:11:22:33:44:00", + "root_cost", "0", + "bridge_prio", "0x8000", "bridge_mac", "00:11:22:33:44:00", + "port", "0x8001", + "msg_age", "0x0000", "max_age", "0x1400", + "hello", "0x0200", "fwd_delay", "0x0f00", + "v1_len", "0", + }); + REQUIRE(f != nullptr); + + // Spelling out every field must land on the same bytes as the defaults. + CHECK(hexstr(frame_to_buf(f)) == padded(bpdu_rst_body)); + + frame_free(f); +} + +TEST_CASE("stp: RST flags sit in the same octet as a config BPDU's", "[stp]") { + // The clause 13.35 handshake on the wire. flags is at eth(14) + llc(3) + + // proto/ver/type(4) = offset 21, unmoved by the extra trailing octet. + frame_t *propose = parse_frame_wrap({ + "eth", "llc", + "stp_rst", "proposal", "1", "role", "3", + "learning", "1", "forwarding", "1", + }); + frame_t *agree = parse_frame_wrap({ + "eth", "llc", "stp_rst", "agreement", "1", "role", "2", + }); + REQUIRE(propose != nullptr); + REQUIRE(agree != nullptr); + + buf_t *b_propose = frame_to_buf(propose); + buf_t *b_agree = frame_to_buf(agree); + REQUIRE(b_propose != nullptr); + REQUIRE(b_agree != nullptr); + + // A designated port proposing, learning and forwarding. + CHECK(b_propose->data[21] == 0x3e); + // A root port agreeing. + CHECK(b_agree->data[21] == 0x48); + + bfree(b_propose); + bfree(b_agree); + frame_free(propose); + frame_free(agree); +} + TEST_CASE("stp: a config BPDU can be matched field by field", "[stp]") { // What an "rx" command builds: a value buf and a mask with every // specified field set to all-ones. Mirrors argc_cmd() in ef-args.c. @@ -337,3 +455,59 @@ TEST_CASE("stp: a config BPDU can be matched field by field", "[stp]") { frame_free(exact); frame_free(masked); } + +TEST_CASE("stp: an RST BPDU can be matched field by field", "[stp]") { + // The same as the config BPDU case, on the 36-octet template, where the + // mask must reach the trailing v1_len octet as well. + frame_t *exact = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "stp_rst", "agreement", "1", "role", "2", + }); + frame_t *masked = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "stp_rst", "flags", "ign", "agreement", "1", + }); + frame_t *other = parse_frame_wrap({ + "eth", "dmac", "01:80:c2:00:00:00", "smac", "00:11:22:33:44:55", + "llc", "stp_rst", "role", "2", + }); + REQUIRE(exact != nullptr); + REQUIRE(masked != nullptr); + REQUIRE(other != nullptr); + CHECK(masked->has_mask == 1); + + buf_t *b_exact = frame_to_buf(exact); + buf_t *b_masked = frame_to_buf(masked); + buf_t *b_other = frame_to_buf(other); + buf_t *mask = frame_mask_to_buf(masked); + REQUIRE(b_exact != nullptr); + REQUIRE(b_masked != nullptr); + REQUIRE(b_other != nullptr); + REQUIRE(mask != nullptr); + + // Padding would fill a short mask out to 60 bytes with ones, so only an + // unpadded mask shows that it really ends at v1_len, the last octet of + // eth(14) + llc(3) + 36, and that v1_len is compared. + { + NoPadGuard no_pad; + buf_t *unpadded = frame_mask_to_buf(masked); + REQUIRE(unpadded != nullptr); + CHECK(unpadded->size == 53); + CHECK(unpadded->data[52] == 0xff); + bfree(unpadded); + } + + // Only agreement is compared in the flags octet, so a root port + // agreeing matches and one that does not agree is rejected. + CHECK(mask->data[21] == 0x40); + CHECK(bequal_mask(b_exact, b_masked, mask, 0) == 1); + CHECK(bequal_mask(b_other, b_masked, mask, 0) == 0); + + bfree(b_exact); + bfree(b_masked); + bfree(b_other); + bfree(mask); + frame_free(exact); + frame_free(masked); + frame_free(other); +}