From 6850d2e8ad20ee3c372c57f8220477cc4f90151f Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 3 Dec 2026 20:28:14 +0100 Subject: [PATCH] Repeater: `filter path add ` stops forwarding floods that came through a given relay MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changelog-cs: Repeater: `filter path add ` přestane přeposílat floody, které prošly daným repeaterem Changelog-topic: filter-path Co-Authored-By: Claude Opus 5.5 --- README.md | 7 ++ docs/tth-commands.md | 19 +++- examples/simple_repeater/ChannelFilter.h | 101 ++++++++++++++++++- examples/simple_repeater/MyMesh.cpp | 8 ++ examples/simple_repeater/MyMesh.h | 3 + src/helpers/ChannelGate.h | 58 +++++++++++ src/helpers/CommonCLI.h | 10 ++ test/test_channel_gate/test_channel_gate.cpp | 71 +++++++++++++ 8 files changed, 273 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7dcc4c0a5..acf4b0f59 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ vypnuté, dokud je nezapnete. bez klíče, takže funguje i na soukromých kanálech; příjem se nemění, jen přeposílání. S `s` se kanál neodřízne, ale přiškrtí (jeden paket za `s` sekund). +* **Blokace přeposílání podle repeateru v cestě** – `filter path add ` + (prefix veřejného klíče, 2–4 bajty): nepřeposílá flood, který prošel + uvedeným repeaterem. Jednobajtové hopy se nikdy neshodují, protože jeden + bajt sdílí většina repeaterů meshe. * **Limit stáří zprávy** – `filter age ` / `filter age off` (volitelné, 1–10080 min): nepřeposílá skupinové zprávy na dešifrovaných kanálech, jejichž čas od odesílatele je starší; bez nastavených hodin repeateru neplatí. Posuzuje @@ -394,6 +398,9 @@ off until you turn them on. * **Channel-hash deny-forward** -- `filter hash add [secs]`: keyless, works on private channels too; reception is unaffected, only forwarding. With `secs` the channel is throttled (one packet per `secs`) instead of cut. +* **Relay deny-forward** -- `filter path add ` (pubkey prefix, 2-4 + bytes): do not forward a flood that came through that repeater. 1-byte hops + never match, as one byte is shared by most repeaters of a mesh. * **Message age limit** -- `filter age ` / `filter age off` (opt-in, 1-10080 min): do not forward group texts on the decrypted channels whose sender timestamp is older; inactive while the repeater clock is unset. It judges the sender's diff --git a/docs/tth-commands.md b/docs/tth-commands.md index bb348a4c9..2e240b7aa 100644 --- a/docs/tth-commands.md +++ b/docs/tth-commands.md @@ -85,6 +85,23 @@ lives on it before adding one. A throttle's window lives in RAM: the first match after a boot passes. +### Relay deny-forward -- `filter path` (every repeater) + +Do not re-air a flood whose path so far names a given relay: the tool for a +broken or hostile repeater whose owner cannot be reached. An entry is a pubkey +prefix of 2-4 bytes (4-8 hex digits). A hop hash is as wide as the packet's +SENDER chose (1-3 bytes), so a hop matches when it and the entry agree on the +shorter of the two -- and a **1-byte hop never matches**, since one byte is +shared by most repeaters of a mesh. Floods sent with 1-byte paths therefore +pass. Only flood packets; reception is unaffected, only forwarding. + +| command | what | +|---|---| +| `filter path [list]` | drops and the list | +| `filter path add ` | deny a relay by its pubkey prefix, e.g. `c420` (up to 8 entries) | +| `filter path del ` | remove one entry | +| `filter path clear` | empty the list | + ### Message age -- `filter age` (repeater with channel keys) | command | what | @@ -101,7 +118,7 @@ clock reads before 2026; a clock running ahead makes everything look old. | command | what | |---|---| -| `filter dryrun [on\|off]` | count every `filter` verdict (sender, hash, age) but forward anyway -- try a rule on a live node before it bites; without an argument shows the state. Default: off | +| `filter dryrun [on\|off]` | count every `filter` verdict (sender, hash, path, age) but forward anyway -- try a rule on a live node before it bites; without an argument shows the state. Default: off | ## Radio watchdogs and board upkeep (all) diff --git a/examples/simple_repeater/ChannelFilter.h b/examples/simple_repeater/ChannelFilter.h index 530e7e0f6..e61d0d2ce 100644 --- a/examples/simple_repeater/ChannelFilter.h +++ b/examples/simple_repeater/ChannelFilter.h @@ -82,7 +82,7 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max return true; } - strcpy(reply, "Err - filter [add|del |list|clear|hash|age|dryrun]"); + strcpy(reply, "Err - filter [add|del |list|clear|hash|path|age|dryrun]"); return true; } @@ -114,7 +114,7 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max // state is RAM only (first match after boot passes) and only a pass moves it; // see HashThrottle in src/helpers/ChannelGate.h. Interval 0 = block, as before. // -// `filter dryrun on` counts every verdict of `filter` (hash, age, sender deny) +// `filter dryrun on` counts every verdict of `filter` (hash, path, age, sender deny) // but forwards anyway -- the way to try a new rule on a live mast. bool MyMesh::filterHashDenied(const mesh::Packet* packet) { @@ -230,7 +230,102 @@ bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply return true; } -// `filter dryrun [on|off]` -- verdicts of `filter` (hash, age, sender deny) +// --------------------------------------------------------------------------- +// Deny-forward by RELAY -- `filter path` (tth #4), independent of channel keys. +// +// A flood whose path so far names a denied relay is not re-aired by this node. +// Matching rules and why 1-byte hops never match: pathDenyMatch() in +// src/helpers/ChannelGate.h. Only FLOOD packets: a direct packet's path is the +// route still ahead, and it was addressed to us on purpose. Reception and +// rxlog/pktfeed capture are untouched, as with the other filters. +// Persisted in prefs (f_dpath, f_dpath_l, f_dpath_n); honours `filter dryrun`. + +static_assert(sizeof(NodePrefs::deny_path) == PATH_DENY_SLOTS * PATH_DENY_MAX_BYTES, "deny_path shape"); +static_assert(sizeof(NodePrefs::deny_path_len) == PATH_DENY_SLOTS, "deny_path_len shape"); + +bool MyMesh::filterPathDenied(const mesh::Packet* packet) { + if (_prefs.deny_path_n == 0 || !packet->isRouteFlood()) return false; + int at = pathDenyMatch(packet->path, packet->getPathHashSize(), packet->getPathHashCount(), + _prefs.deny_path, _prefs.deny_path_len, _prefs.deny_path_n); + if (at < 0) return false; + filter_path_dropped++; + return !_prefs.filter_dry_run; +} + +static int pathDenyFormat(char* dest, int max, const uint8_t* b, uint8_t len) { + int n = 0; + for (uint8_t i = 0; i < len && n < max; i++) n += snprintf(&dest[n], max - n, "%02X", b[i]); + return n; +} + +// `filter path [list]` -- drops + list; `filter path add ` -- deny a relay +// by its pubkey prefix (4..8 hex digits); `filter path del `; `filter path clear`. +bool MyMesh::filterPathHandleCommand(const char* command, char* reply, int reply_max) { + if (memcmp(command, "filter path", 11) != 0 || (command[11] != 0 && command[11] != ' ')) return false; + + const char* arg = (command[11] == ' ') ? &command[12] : ""; + uint8_t n = _prefs.deny_path_n; + if (n > PATH_DENY_SLOTS) n = PATH_DENY_SLOTS; + + if (strcmp(arg, "clear") == 0) { + _prefs.deny_path_n = 0; + memset(_prefs.deny_path, 0, sizeof(_prefs.deny_path)); + memset(_prefs.deny_path_len, 0, sizeof(_prefs.deny_path_len)); + savePrefs(); + strcpy(reply, "OK - path deny list cleared"); + return true; + } + + bool is_add = strncmp(arg, "add ", 4) == 0; + bool is_del = strncmp(arg, "del ", 4) == 0; + if (is_add || is_del) { + uint8_t b[PATH_DENY_MAX_BYTES]; + uint8_t len = pathDenyParse(arg + 4, b); + if (len == 0) { + strcpy(reply, "Err - 2..4 byte hex prefix, e.g. c420"); + return true; + } + int at = -1; + for (int i = 0; i < n; i++) { + if (_prefs.deny_path_len[i] == len && memcmp(_prefs.deny_path[i], b, len) == 0) { at = i; break; } + } + if (is_add) { + if (at >= 0) { strcpy(reply, "Err - already listed"); return true; } + if (n >= PATH_DENY_SLOTS) { strcpy(reply, "Err - list full"); return true; } + memset(_prefs.deny_path[n], 0, PATH_DENY_MAX_BYTES); + memcpy(_prefs.deny_path[n], b, len); + _prefs.deny_path_len[n] = len; + n++; + } else { + if (at < 0) { strcpy(reply, "Err - not listed"); return true; } + for (int i = at; i + 1 < n; i++) { + memcpy(_prefs.deny_path[i], _prefs.deny_path[i + 1], PATH_DENY_MAX_BYTES); + _prefs.deny_path_len[i] = _prefs.deny_path_len[i + 1]; + } + --n; + memset(_prefs.deny_path[n], 0, PATH_DENY_MAX_BYTES); + _prefs.deny_path_len[n] = 0; + } + _prefs.deny_path_n = n; + savePrefs(); + // fall through to the listing as the reply + } else if (arg[0] != 0 && strcmp(arg, "list") != 0) { + strcpy(reply, "Err - filter path [add |del |list|clear]"); + return true; + } + + int len = snprintf(reply, reply_max, "filter path: dropped %u%s, deny:", filter_path_dropped, + _prefs.filter_dry_run ? " DRY-RUN" : ""); + if (n == 0 && len < reply_max) len += snprintf(&reply[len], reply_max - len, " (nic)"); + for (int i = 0; i < n && len + 1 < reply_max; i++) { + reply[len++] = ' '; + reply[len] = 0; + len += pathDenyFormat(&reply[len], reply_max - len, _prefs.deny_path[i], _prefs.deny_path_len[i]); + } + return true; +} + +// `filter dryrun [on|off]` -- verdicts of `filter` (hash, path, age, sender deny) // are counted but the packet is forwarded anyway. Persisted (f_dry). // `filter age [|off]` -- refuse to forward a group text the node can // DECRYPT (channels of FILTER_CHANNEL_PSKS) whose sender timestamp is older diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 54f81baa1..6afbc455f 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -875,6 +875,11 @@ bool MyMesh::allowPacketForward(const mesh::Packet *packet) { MESH_DEBUG_PRINTLN("allowPacketForward: channel hash %02X on deny list", packet->payload[0]); return false; } + // CUSTOM (TeTeHacko): `filter path` -- a flood that came through a denied relay. + if (filterPathDenied(packet)) { + MESH_DEBUG_PRINTLN("allowPacketForward: denied relay in path"); + return false; + } return true; } @@ -1567,6 +1572,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc _prefs.radio_silence_min = RADIO_SILENCE_MIN_DEFAULT; filter_hash_dropped = 0; filter_hash_passed = 0; + filter_path_dropped = 0; _prefs.interference_threshold = 0; // disabled _prefs.cad_enabled = 0; // hardware CAD before TX (off by default; 'set cad on') _prefs.loop_detect = LOOP_DETECT_MINIMAL; @@ -2009,6 +2015,8 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply // CUSTOM (TeTeHacko): `filter hash [add|del |clear]` — keyless channel-hash // deny list. Before filterHandleCommand(), which would claim any `filter ...`. if (filterHashHandleCommand(command, reply, reply_max)) return; + // CUSTOM (TeTeHacko): `filter path [add|del |clear]` -- relay deny list. + if (filterPathHandleCommand(command, reply, reply_max)) return; // CUSTOM (TeTeHacko): `filter dryrun [on|off]`, `filter age [|off]`. Also // before filterHandleCommand(), which would take `dryrun`/`age` for names. if (filterMiscHandleCommand(command, reply, reply_max)) return; diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index ac1636ddb..065c1a3b7 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -216,6 +216,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { uint32_t filter_hash_dropped; // forwards vetoed by the channel-hash deny list uint32_t filter_hash_passed; // throttled entries: forwards let through within budget HashThrottle hash_throttle; // per-entry windows of `filter hash add ` + uint32_t filter_path_dropped; // floods vetoed because a denied relay is in their path unsigned long next_fair_sender_normal_refill, next_fair_sender_low_refill, next_fair_group_refill; uint32_t pending_discover_tag; unsigned long pending_discover_until; @@ -337,6 +338,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { // CUSTOM (TeTeHacko): keyless deny-forward by channel hash. Definitions in ChannelFilter.h. bool filterHashDenied(const mesh::Packet* packet); bool filterHashHandleCommand(const char* command, char* reply, int reply_max); + bool filterPathDenied(const mesh::Packet* packet); + bool filterPathHandleCommand(const char* command, char* reply, int reply_max); // `filter dryrun [on|off]` and `filter age [|off]`. Definitions in ChannelFilter.h. bool filterMiscHandleCommand(const char* command, char* reply, int reply_max); // Refill interval in ms: persisted pref (seconds) if set, else the build default. diff --git a/src/helpers/ChannelGate.h b/src/helpers/ChannelGate.h index 6cb927fba..5e61a103f 100644 --- a/src/helpers/ChannelGate.h +++ b/src/helpers/ChannelGate.h @@ -82,3 +82,61 @@ inline MsgAgeVerdict msgAgeCheck(uint32_t sender_ts, uint32_t now, uint16_t max_ if (sender_ts >= now) return MSG_AGE_FRESH; return (now - sender_ts > (uint32_t)max_minutes * 60u) ? MSG_AGE_OLD : MSG_AGE_FRESH; } + +// --------------------------------------------------------------------------- +// `filter path add ` (tth #4): refuse to re-air a flood that came THROUGH +// a given relay, i.e. whose path so far carries a hop hash matching a denied +// pubkey prefix. The tool for a broken or hostile repeater whose owner cannot +// be reached (CZ mesh: 2-byte paths rewritten to 1 byte with a nonsense +// length, reflooded as new). Idea from DutchMeshCore `filter path add` +// (d26421b0), MeshSydney and upstream PR #2343. +// +// A hop hash is a prefix of the relay's pubkey, and its width is chosen by +// the SENDER of the packet (path_len bits 6-7). One byte collides with most +// of the mesh (671 of 711 repeaters share a byte with another), so: +// * an entry has 2..PATH_DENY_MAX_BYTES bytes -- a 1-byte entry is refused; +// * a 1-byte hop hash never matches, whatever the entry -- blocking every +// packet that names any node sharing the culprit's first byte would cut +// innocent relays; +// * a hop matches when its hash and the entry agree on the shorter of the +// two. A 2-byte hop vs a 4-byte entry therefore still compares only 2 +// bytes; collisions at 2 bytes are rare, not impossible. + +#define PATH_DENY_SLOTS 8 // = rows of NodePrefs::deny_path +#define PATH_DENY_MAX_BYTES 4 // = columns of NodePrefs::deny_path + +// Index of the first denied entry that some hop of `path` matches, else -1. +// path: hop_count hashes of hash_size bytes each (Packet::path). +inline int pathDenyMatch(const uint8_t* path, uint8_t hash_size, uint8_t hop_count, + const uint8_t prefixes[][PATH_DENY_MAX_BYTES], const uint8_t* lens, uint8_t n) { + if (hash_size < 2 || n == 0) return -1; + if (n > PATH_DENY_SLOTS) n = PATH_DENY_SLOTS; + for (uint8_t h = 0; h < hop_count; h++) { + const uint8_t* hop = &path[h * hash_size]; + for (uint8_t i = 0; i < n; i++) { + uint8_t k = lens[i]; + if (k < 2 || k > PATH_DENY_MAX_BYTES) continue; // unused / corrupt slot + if (k > hash_size) k = hash_size; + if (memcmp(hop, prefixes[i], k) == 0) return i; + } + } + return -1; +} + +// "a1b2" / "0xA1B2C3" -> bytes; even number of hex digits, 2..PATH_DENY_MAX_BYTES +// bytes. Returns the byte count, 0 = invalid. +inline uint8_t pathDenyParse(const char* s, uint8_t out[PATH_DENY_MAX_BYTES]) { + if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) s += 2; + size_t len = strlen(s); + if (len % 2 != 0 || len < 4 || len > 2 * PATH_DENY_MAX_BYTES) return 0; + for (size_t i = 0; i < len; i++) { + char c = s[i]; + uint8_t v; + if (c >= '0' && c <= '9') v = c - '0'; + else if (c >= 'a' && c <= 'f') v = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') v = c - 'A' + 10; + else return 0; + if (i % 2 == 0) out[i / 2] = v << 4; else out[i / 2] |= v; + } + return (uint8_t)(len / 2); +} diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index a9785dbbd..a300b8d0e 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -92,6 +92,11 @@ public: uint8_t deny_hash[8]; uint8_t deny_hash_n = 0; uint16_t deny_hash_secs[8]; // per entry: 0 = block, else pass one per N s (throttle) + // relays whose floods this node refuses to re-air: pubkey prefixes of 2..4 + // bytes, matched against the hop hashes of the path (`filter path`). + uint8_t deny_path[8][4]; + uint8_t deny_path_len[8]; // significant bytes per row, 0 = unused + uint8_t deny_path_n = 0; uint16_t filter_age_min = 0; // drop decrypted group texts older than this; 0 = off uint8_t filter_dry_run = 0; // count `filter` verdicts, forward anyway // Battery state as an icon on the advertised name (see BatteryIcon.h). @@ -273,6 +278,9 @@ private: def("f_dhash", (void*)_parent->deny_hash, sizeof(_parent->deny_hash)); def("f_dhash_n", _parent->deny_hash_n); def("f_dhash_s", (void*)_parent->deny_hash_secs, sizeof(_parent->deny_hash_secs)); + def("f_dpath", (void*)_parent->deny_path, sizeof(_parent->deny_path)); + def("f_dpath_l", (void*)_parent->deny_path_len, sizeof(_parent->deny_path_len)); + def("f_dpath_n", _parent->deny_path_n); def("f_age", _parent->filter_age_min); def("f_dry", _parent->filter_dry_run); def("fair_gcap", _parent->fair_group_cap); @@ -333,6 +341,8 @@ public: filter_deny[0] = 0; memset(deny_hash, 0, sizeof(deny_hash)); memset(deny_hash_secs, 0, sizeof(deny_hash_secs)); + memset(deny_path, 0, sizeof(deny_path)); + memset(deny_path_len, 0, sizeof(deny_path_len)); } CommonRadioPrefs* getRadioPrefs() { return &radio; } diff --git a/test/test_channel_gate/test_channel_gate.cpp b/test/test_channel_gate/test_channel_gate.cpp index 72f402e16..35bf1d252 100644 --- a/test/test_channel_gate/test_channel_gate.cpp +++ b/test/test_channel_gate/test_channel_gate.cpp @@ -100,3 +100,74 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } + +// ── pathDenyMatch / pathDenyParse (filter path, tth #4) ────────────────────── + +static uint8_t deny[PATH_DENY_SLOTS][PATH_DENY_MAX_BYTES]; +static uint8_t deny_len[PATH_DENY_SLOTS]; + +static uint8_t setDeny(std::initializer_list hexes) { + memset(deny, 0, sizeof(deny)); + memset(deny_len, 0, sizeof(deny_len)); + uint8_t n = 0; + for (const char* h : hexes) deny_len[n] = pathDenyParse(h, deny[n]), n++; + return n; +} + +TEST(PathDeny, ParseAcceptsTwoToFourBytes) { + uint8_t b[PATH_DENY_MAX_BYTES]; + EXPECT_EQ(pathDenyParse("a1b2", b), 2); + EXPECT_EQ(b[0], 0xA1); EXPECT_EQ(b[1], 0xB2); + EXPECT_EQ(pathDenyParse("0xA1B2C3", b), 3); + EXPECT_EQ(b[2], 0xC3); + EXPECT_EQ(pathDenyParse("a1b2c3d4", b), 4); +} + +TEST(PathDeny, ParseRejectsOneByteOddAndJunk) { + uint8_t b[PATH_DENY_MAX_BYTES]; + EXPECT_EQ(pathDenyParse("a1", b), 0); // 1 byte collides with most of the mesh + EXPECT_EQ(pathDenyParse("a1b", b), 0); // odd + EXPECT_EQ(pathDenyParse("a1b2c3d4e5", b), 0); // too long + EXPECT_EQ(pathDenyParse("a1g2", b), 0); + EXPECT_EQ(pathDenyParse("", b), 0); + EXPECT_EQ(pathDenyParse("0x", b), 0); +} + +TEST(PathDeny, TwoByteHopMatchesAnywhereInPath) { + uint8_t n = setDeny({"c420"}); + const uint8_t path[] = {0x5F, 0x14, 0xC4, 0x20, 0x02, 0x5C}; + EXPECT_EQ(pathDenyMatch(path, 2, 3, deny, deny_len, n), 0); + EXPECT_EQ(pathDenyMatch(path, 2, 1, deny, deny_len, n), -1); // not reached yet +} + +TEST(PathDeny, OneByteHopNeverMatches) { + uint8_t n = setDeny({"c420"}); + const uint8_t path[] = {0xC4, 0x20}; + EXPECT_EQ(pathDenyMatch(path, 1, 2, deny, deny_len, n), -1); +} + +TEST(PathDeny, ComparesTheShorterOfHopAndEntry) { + uint8_t n = setDeny({"c420aabb"}); + const uint8_t two[] = {0xC4, 0x20}; + EXPECT_EQ(pathDenyMatch(two, 2, 1, deny, deny_len, n), 0); + const uint8_t three_ok[] = {0xC4, 0x20, 0xAA}; + EXPECT_EQ(pathDenyMatch(three_ok, 3, 1, deny, deny_len, n), 0); + const uint8_t three_other[] = {0xC4, 0x20, 0xAB}; + EXPECT_EQ(pathDenyMatch(three_other, 3, 1, deny, deny_len, n), -1); +} + +TEST(PathDeny, ReturnsWhichEntryAndSkipsEmptySlots) { + uint8_t n = setDeny({"1111", "2222"}); + deny_len[0] = 0; // corrupt/unused slot must not match everything + const uint8_t path[] = {0x22, 0x22}; + EXPECT_EQ(pathDenyMatch(path, 2, 1, deny, deny_len, n), 1); + const uint8_t other[] = {0x11, 0x11}; + EXPECT_EQ(pathDenyMatch(other, 2, 1, deny, deny_len, n), -1); +} + +TEST(PathDeny, EmptyListOrEmptyPath) { + uint8_t n = setDeny({"c420"}); + const uint8_t path[] = {0xC4, 0x20}; + EXPECT_EQ(pathDenyMatch(path, 2, 1, deny, deny_len, 0), -1); + EXPECT_EQ(pathDenyMatch(path, 2, 0, deny, deny_len, n), -1); +}