Repeater: filter flow holds 24 rules and an r rule can match one requester on any destination

Changelog: Repeater: `filter flow` holds 24 rules (was 16), and an `r` rule with a 3-byte key (source + first hop) blocks one requester on any destination
Changelog-cs: Repeater: `filter flow` pojme 24 pravidel (bylo 16) a pravidlo `r` s 3bajtovým klíčem (zdroj + první hop) zablokuje jednoho tazatele pro libovolný cíl
Changelog-topic: filter-flow
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-12-03 23:16:43 +01:00
1 parent 5fe599c5b6
commit ae8d5057da
8 files changed
+163 -21

No files matched your search

+4 -4
View File
@@ -46,9 +46,9 @@ vypnuté, dokud je nezapnete.
a `#hashtag` kanály), přestane přeposílat zprávy uvedených jmen odesílatelů
(`filter add <name>`, `filter del <name>`, `filter list`). Jméno se dá podvrhnout: je to brzda na utrženého
bota, ne bezpečnost.
* **Pravidla blokace přeposílání – `filter flow`** (max 16, jen flood, příjem
* **Pravidla blokace přeposílání – `filter flow`** (max 24, jen flood, příjem
se nemění, jen přeposílání, bez klíče, takže funguje i na soukromých
kanálech): druh `c` bajt kanálu, `r` cíl+zdroj (volitelně + první hop), `n`
kanálech): druh `c` bajt kanálu, `r` cíl+zdroj (volitelně + první hop) nebo zdroj+první hop (3 B, libovolný cíl), `n`
cíl ANON_REQ (login flood), `a` prefix klíče z advertu, `p` repeater v cestě
(2–4 bajty; jednobajtové hopy se nikdy neshodují). Každé pravidlo má režim
(`block`, `throttle <s>` = jeden paket za `s` sekund, `dry` = jen počítá),
@@ -398,9 +398,9 @@ off until you turn them on.
`#hashtag` channels), stop forwarding messages of listed sender names
(`filter add <name>`, `filter del <name>`, `filter list`). Names are spoofable: it contains a runaway bot,
it is not security.
* **Forward-deny rules -- `filter flow`** (max 16, floods only; reception
* **Forward-deny rules -- `filter flow`** (max 24, floods only; reception
is unaffected, only forwarding; keyless, so private channels too): kind `c`
channel byte, `r` dest+src (optionally + first hop), `n` ANON_REQ dest (login
channel byte, `r` dest+src (optionally + first hop) or src+first hop (3 B, any destination), `n` ANON_REQ dest (login
flood), `a` advert pubkey prefix, `p` relay in the path (2-4 bytes; 1-byte
hops never match). Each rule has a mode (`block`, `throttle <s>` = one packet
per `s` seconds, `dry` = count only), its own counters, an `auto` flag and an
+9 -4
View File
@@ -71,7 +71,7 @@ message: anyone can rename around it.
### Forward-deny rules -- `filter flow` (every repeater)
One table of keyless rules (max 16) that replaces the former `filter hash` and
One table of keyless rules (max 24) that replaces the former `filter hash` and
`filter path` (both remain as aliases, below). A rule makes this node not
re-air a matching **flood** packet; reception, rxlog and the pktfeed are
unaffected. Match is on what is in the clear:
@@ -79,11 +79,16 @@ unaffected. Match is on what is in the clear:
| kind | key (hex) | matches |
|---|---|---|
| `c` | 1 B channel byte | GRP_TXT / GRP_DATA. One byte is coarse: every other channel hashing to it is hit as well |
| `r` | `dest src` (2 B), optionally + first hop (4 B) | REQ / RESPONSE / TXT_MSG / PATH |
| `r` | `dest src` (2 B), optionally + first hop (4 B); or `src first-hop` (3 B), **any destination** | REQ / RESPONSE / TXT_MSG / PATH |
| `n` | `dest` (1 B), optionally + first hop (3 B) | ANON_REQ (login flood) |
| `a` | pubkey prefix, 4-8 B | ADVERT |
| `p` | relay pubkey prefix, 2-4 B | any hop of the path (a hop matches on the shorter of its width and the entry) |
The 3-byte `r` form is `src` (1 B) + first hop (2 B): `payload[1] == src` and the
path's first hop equals the two hop bytes, whatever the destination. One
requester asking many destinations through the same first relay thus takes one
entry instead of one per destination. The 2 B and 4 B forms are unchanged.
"First hop" is the 2-byte hash at the start of the path. **0-hop packets and
1-byte hops never match a rule that has it**, and kind `p` never matches a
1-byte hop (one byte is shared by most repeaters of a mesh).
@@ -171,8 +176,8 @@ keep working; they act on static kind `c` / `p` entries.
| `filter path add <hex>` | `filter flow add p <hex>` (2-4 bytes = 4-8 hex digits) |
| `filter path del <hex>` / `clear` | remove one entry / all STATIC `p` entries |
The table is shared: 16 entries in all, where `filter hash` had 8 and
`filter path` 8. The list order is now the sorted order, not the order of adding.
The table is shared: 24 entries in all (it was 16 before; prefs records
`f_fl0..f_fl23`), where `filter hash` had 8 and `filter path` 8. The list order is now the sorted order, not the order of adding.
### Message age -- `filter age` (repeater with channel keys)
+1 -1
View File
@@ -100,7 +100,7 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max
// matches. Check the analyzer before denying.
// * Only forwarding by THIS node, only FLOOD: reception and rxlog/pktfeed
// capture happen before routeRecvPacket(), so the evidence trail is intact.
// * Persisted in prefs (f_fl0..f_fl15, one packed record each). The expiry
// * Persisted in prefs (f_fl0..f_fl23, one packed record each). The expiry
// countdown is written only on add/change -- no periodic savePrefs() on a
// solar node.
// * `filter dryrun on` keeps every verdict count-only.
+2 -2
View File
@@ -101,7 +101,7 @@ public:
// zero = unused (FlowRules.h flowPack). One key per record: the config reader
// holds a token of at most 63 bytes. The legacy deny_hash/deny_path above are
// only read, to migrate once at boot (examples/simple_repeater/ChannelFilter.h).
uint8_t flow_rec[16][22];
uint8_t flow_rec[24][22];
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).
@@ -286,7 +286,7 @@ private:
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);
for (int i = 0; i < 16; i++) {
for (int i = 0; i < 24; i++) {
char k[8];
snprintf(k, sizeof(k), "f_fl%d", i);
def(k, (void*)_parent->flow_rec[i], sizeof(_parent->flow_rec[i]));
+17 -8
View File
@@ -10,17 +10,20 @@
// KINDS (FLOOD packets only; matched on what is in the clear)
// c channel byte GRP_TXT / GRP_DATA payload[0] 1 B
// r dest src [+hop] REQ / RESPONSE / TXT_MSG / PATH payload[0..1] 2 B or 4 B
// r src +hop same payload types, payload[1] only, ANY dest 3 B
// n dest [+hop] ANON_REQ payload[0] (login flood) 1 B or 3 B
// a pubkey prefix ADVERT payload[0..] 4..8 B
// p relay in the path any hop of path[] 2..4 B
// "+hop" = the FIRST hop of the path, 2 bytes. 0-hop packets and packets with
// "+hop" = the FIRST hop of the path, 2 bytes. The 3 B form of `r` is src(1) +
// hop(2): one requester asking many destinations through the same first relay
// takes ONE entry instead of one per destination. 0-hop packets and packets with
// 1-byte hops never match a rule that has it (a 1-byte hash collides with most
// of the mesh). Kind p ignores 1-byte hops entirely, for the same reason.
//
// ENTRY: mode (block | throttle <s> | dry), flag `auto` (1 bit: set by an agent,
// as opposed to static = inventory/hand), optional expiry, RAM counters.
// * Entries are kept SORTED by (kind, key) -- kind as its ASCII letter, key
// bytewise, shorter key first on a common prefix. Max FLOW_MAX = 16. The
// bytewise, shorter key first on a common prefix. Max FLOW_MAX = 24. The
// index in that array is the `slot_id`; it moves on add/del.
// * EXPIRY (two clocks, whichever comes first): absolute unix `expires`
// (honoured only while the node clock is >= FLOW_CLOCK_VALID_FROM) and the
@@ -49,7 +52,7 @@
#define FLOW_NOINLINE
#endif
#define FLOW_MAX 16
#define FLOW_MAX 24
#define FLOW_KEY_MAX 8
#define FLOW_REC_LEN 22 // packed record, see flowPack()
#define FLOW_TTL_MAX 2000000UL // ~23 days; keeps the ms countdown wrap-safe
@@ -108,7 +111,7 @@ inline bool flowClockValid(uint32_t now_unix) { return now_unix >= FLOW_CLOCK_VA
inline bool flowKeyLenOk(uint8_t kind, uint8_t klen) {
switch (kind) {
case FLOW_KIND_C: return klen == 1;
case FLOW_KIND_R: return klen == 2 || klen == 4;
case FLOW_KIND_R: return klen == 2 || klen == 3 || klen == 4;
case FLOW_KIND_N: return klen == 1 || klen == 3;
case FLOW_KIND_A: return klen >= 4 && klen <= FLOW_KEY_MAX;
case FLOW_KIND_P: return klen >= 2 && klen <= 4;
@@ -182,6 +185,12 @@ inline FLOW_NOINLINE bool flowRuleMatches(const FlowRule& r, const FlowPkt& p) {
case FLOW_KIND_C:
return p.cls == FLOW_PKT_GROUP && p.payload_len >= 1 && p.payload[0] == r.key[0];
case FLOW_KIND_R:
if (r.klen == 3) { // src + first hop, any destination
if (p.cls != FLOW_PKT_ADDR || p.payload_len < 2 || p.payload[1] != r.key[0]) return false;
if (p.hash_size < 2 || p.hop_count < 1 || p.path == NULL) return false;
return memcmp(p.path, &r.key[1], 2) == 0;
}
// fall through
case FLOW_KIND_N: {
uint8_t base = (r.kind == FLOW_KIND_R) ? 2 : 1;
if (p.cls != (r.kind == FLOW_KIND_R ? FLOW_PKT_ADDR : FLOW_PKT_ANON) || p.payload_len < base) return false;
@@ -327,10 +336,10 @@ public:
// (`filter dryrun`) keeps all counting but never drops.
FLOW_NOINLINE bool verdict(const FlowPkt& p, uint32_t now_ms, bool global_dry) {
int drop_at = -1;
uint16_t matched = 0;
uint32_t matched = 0;
for (uint8_t i = 0; i < n; i++) {
if (!flowRuleMatches(e[i].r, p)) continue;
matched |= (uint16_t)(1u << i);
matched |= (uint32_t)1u << i;
if (e[i].r.mode == FLOW_DRY) { e[i].dropped++; continue; }
if (drop_at < 0 && !windowOpen(i, now_ms)) drop_at = i;
}
@@ -339,7 +348,7 @@ public:
return !global_dry;
}
for (uint8_t i = 0; i < n; i++) {
if (!(matched & (1u << i)) || e[i].r.mode != FLOW_THROTTLE) continue;
if (!(matched & ((uint32_t)1u << i)) || e[i].r.mode != FLOW_THROTTLE) continue;
e[i].armed = 1;
e[i].last_ms = now_ms;
e[i].passed++;
@@ -431,7 +440,7 @@ inline const char* flowParseKey(const char** p, uint8_t* kind, uint8_t* key, uin
n = flowToken(p, &t);
*klen = flowParseHex(t, n, key, FLOW_KEY_MAX);
if (*klen == 0 || !flowKeyLenOk(*kind, *klen)) {
return "Err - key: c 1B, r 2|4B, n 1|3B, a 4-8B, p 2-4B (hex)";
return "Err - key: c 1B, r 2|3|4B, n 1|3B, a 4-8B, p 2-4B (hex)";
}
return NULL;
}
+77 -2
View File
@@ -42,6 +42,12 @@ TEST(FlowCrc, VectorsMatchPythonReference) { // test/vectors/flow_rules_vector
v.add(R('p', "5f14c420"), 0);
EXPECT_EQ(0x8FC9, v.crc(true, &c)); EXPECT_EQ(2, c);
EXPECT_EQ(0x6CEB, v.crc(false, &c)); EXPECT_EQ(2, c);
FlowTable w; // case r_three_byte_src_hop_any_dest: 3 B r rules
w.add(R('r', "8b2cf8bf"), 0); w.add(R('r', "2cec10", FLOW_DRY), 0);
w.add(R('r', "8b2c", FLOW_THROTTLE, 120), 0); w.add(R('r', "2cf8bf", FLOW_BLOCK, 0, true, 1800000000), 0);
EXPECT_EQ(0x4897, w.crc(true, &c)); EXPECT_EQ(1, c);
EXPECT_EQ(0x73B7, w.crc(false, &c)); EXPECT_EQ(3, c);
}
TEST(FlowCrc, CountersAndRemainDoNotCount) {
@@ -87,7 +93,8 @@ TEST(FlowTable, SortedAndUnique) {
EXPECT_EQ(0xFF, t.e[0].r.key[0]);
}
TEST(FlowTable, MaxSixteen) {
TEST(FlowTable, MaxTwentyFour) {
EXPECT_EQ(24, FLOW_MAX);
FlowTable t;
for (int i = 0; i < FLOW_MAX; i++) {
uint8_t k = (uint8_t)(0x10 + i);
@@ -104,7 +111,8 @@ TEST(FlowTable, BadRulesRefused) {
EXPECT_EQ(FLOW_BAD, t.add(R('c', "4343"), 0));
EXPECT_EQ(FLOW_BAD, t.add(R('p', "43"), 0));
EXPECT_EQ(FLOW_BAD, t.add(R('a', "010203"), 0));
EXPECT_EQ(FLOW_BAD, t.add(R('r', "010203"), 0));
EXPECT_EQ(FLOW_BAD, t.add(R('r', "0102030405"), 0));
EXPECT_EQ(FLOW_BAD, t.add(R('r', "01"), 0));
EXPECT_EQ(FLOW_BAD, t.add(R('c', "43", FLOW_THROTTLE, 0), 0));
}
@@ -158,6 +166,39 @@ TEST(FlowMatch, DestSrcAndFirstHop) {
EXPECT_FALSE(u.verdict(pkt(FLOW_PKT_ADDR, other, 3, hop2, 2, 2), 0, false));
}
TEST(FlowMatch, SrcAndFirstHopAnyDest) {
FlowTable t; t.add(R('r', "2cf8bf"), 0); // src 2C, first hop F8BF, any dest
uint8_t hop[] = {0xf8, 0xbf, 0x11, 0x11}, hopx[] = {0xf8, 0xc0}, hop1[] = {0xf8};
uint8_t d1[] = {0x8b, 0x2c, 9, 9}, d2[] = {0xeb, 0x2c, 9, 9}, d3[] = {0x6d, 0x2c};
EXPECT_TRUE(t.verdict(pkt(FLOW_PKT_ADDR, d1, 4, hop, 2, 2), 0, false));
EXPECT_TRUE(t.verdict(pkt(FLOW_PKT_ADDR, d2, 4, hop, 2, 1), 0, false));
EXPECT_TRUE(t.verdict(pkt(FLOW_PKT_ADDR, d3, 2, hop, 2, 1), 0, false));
EXPECT_EQ(3u, t.e[0].dropped);
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_ADDR, d1, 4, hopx, 2, 1), 0, false)); // other first hop
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_ADDR, d1, 4), 0, false)); // 0 hops
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_ADDR, d1, 4, hop1, 1, 1), 0, false)); // 1-byte hop
uint8_t other[] = {0x8b, 0x2d, 9, 9};
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_ADDR, other, 4, hop, 2, 1), 0, false)); // other src
uint8_t dsrc[] = {0x2c, 0x8b, 9, 9}; // 2C as DEST, not src
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_ADDR, dsrc, 4, hop, 2, 1), 0, false));
uint8_t one[] = {0x8b};
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_ADDR, one, 1, hop, 2, 1), 0, false)); // too short
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_GROUP, d1, 4, hop, 2, 1), 0, false)); // wrong class
EXPECT_EQ(3u, t.e[0].dropped);
// the first hop is the FIRST: a later hop equal to it does not count
uint8_t late[] = {0x01, 0x02, 0xf8, 0xbf};
EXPECT_FALSE(t.verdict(pkt(FLOW_PKT_ADDR, d1, 4, late, 2, 2), 0, false));
}
TEST(FlowMatch, ThreeByteAndTwoByteCoexist) {
FlowTable t; t.add(R('r', "2cf8bf"), 0); t.add(R('r', "8b2c"), 0); t.add(R('r', "8b2cf8bf"), 0);
ASSERT_EQ(3, t.n);
EXPECT_EQ(3, t.e[0].r.klen); EXPECT_EQ(2, t.e[1].r.klen); EXPECT_EQ(4, t.e[2].r.klen); // bytewise: 2cf8bf, 8b2c, 8b2cf8bf
uint8_t k3[] = {0x2c, 0xf8, 0xbf}, k2[] = {0x2c, 0xf8};
EXPECT_EQ(0, t.find('r', k3, 3));
EXPECT_EQ(-1, t.find('r', k2, 2));
}
TEST(FlowMatch, AnonRequest) {
FlowTable t; t.add(R('n', "5a"), 0); t.add(R('n', "6b02f8"), 0);
uint8_t pl[] = {0x5a, 1, 2, 3}, pl2[] = {0x6b, 1, 2};
@@ -302,6 +343,8 @@ TEST(FlowParse, AddForms) {
EXPECT_EQ(FLOW_THROTTLE, s.r.mode); EXPECT_EQ(120, s.r.secs);
EXPECT_EQ(FLOW_FLAG_AUTO, s.r.flags); EXPECT_TRUE(s.has_ttl); EXPECT_EQ(604800u, s.ttl);
EXPECT_EQ(4, s.r.klen);
EXPECT_EQ(nullptr, flowParseAdd("r 2cf8bf throttle 60", &s));
EXPECT_EQ(3, s.r.klen); EXPECT_EQ('r', s.r.kind);
EXPECT_EQ(nullptr, flowParseAdd("p 0xC420 dry expires 1800000000", &s));
EXPECT_EQ(FLOW_DRY, s.r.mode); EXPECT_TRUE(s.has_expires);
EXPECT_EQ(nullptr, flowParseAdd("a 11223344 auto", &s));
@@ -313,6 +356,10 @@ TEST(FlowParse, Rejects) {
EXPECT_NE(nullptr, flowParseAdd("c 4343", &s));
EXPECT_NE(nullptr, flowParseAdd("p 43", &s));
EXPECT_NE(nullptr, flowParseAdd("c 4", &s));
EXPECT_NE(nullptr, flowParseAdd("r 2c", &s));
EXPECT_NE(nullptr, flowParseAdd("r 2cf8bf01aa", &s));
EXPECT_EQ(nullptr, flowParseAdd("r 2cf8bfaa", &s)); // 4 B is fine, 5 B is not
EXPECT_STREQ("Err - key: c 1B, r 2|3|4B, n 1|3B, a 4-8B, p 2-4B (hex)", flowParseAdd("r 2cf8bf01aa", &s));
EXPECT_NE(nullptr, flowParseAdd("c 43 throttle", &s));
EXPECT_NE(nullptr, flowParseAdd("c 43 throttle 0", &s));
EXPECT_NE(nullptr, flowParseAdd("c 43 block dry", &s));
@@ -338,6 +385,34 @@ TEST(FlowParse, ResolveExpiry) {
// ── replies ──────────────────────────────────────────────────────────────────
TEST(FlowTable, TwentyFourFillAndTwentyFifthRefused) {
FlowTable t;
for (int i = 0; i < 24; i++) {
FlowRule r = R('r', "000000"); // 3 B r rules, 24 different sources
r.key[0] = (uint8_t)(0x30 + i);
ASSERT_EQ(FLOW_OK, t.add(r, 0)) << i;
}
ASSERT_EQ(24, t.n);
FlowRule x = R('r', "000000"); x.key[0] = 0x7f;
EXPECT_EQ(FLOW_FULL, t.add(x, 0));
EXPECT_EQ(24, t.n);
// the verdict bitmask must reach slot 23: every rule can still match and be charged
uint8_t hop[] = {0, 0};
for (int i = 0; i < 24; i++) {
uint8_t pl[] = {0x01, (uint8_t)(0x30 + i)};
EXPECT_TRUE(t.verdict(pkt(FLOW_PKT_ADDR, pl, 2, hop, 2, 1), 0, false)) << i;
}
for (int i = 0; i < 24; i++) EXPECT_EQ(1u, t.e[i].dropped) << i;
// a throttle rule in slot 23 (past bit 16) must arm its window
FlowTable u;
for (int i = 0; i < 23; i++) { FlowRule r = R('c', "00", FLOW_DRY); r.key[0] = (uint8_t)i; u.add(r, 0); }
u.add(R('r', "300000", FLOW_THROTTLE, 60), 0);
ASSERT_EQ(24, u.n);
uint8_t pl[] = {0x01, 0x30};
EXPECT_FALSE(u.verdict(pkt(FLOW_PKT_ADDR, pl, 2, hop, 2, 1), 0, false)); // passes, arms the window
EXPECT_TRUE(u.verdict(pkt(FLOW_PKT_ADDR, pl, 2, hop, 2, 1), 1000, false)); // window closed
}
TEST(FlowReply, SummaryShape) {
FlowTable t; t.add(R('c', "43", FLOW_BLOCK, 0, true), 0); t.add(R('c', "44"), 0);
char b[64];
+44
View File
@@ -143,6 +143,50 @@
"canonical": "63012e01012c0000000070045f14c42000000000000000",
"crc16": "6CEB"
}
},
{
"name": "r_three_byte_src_hop_any_dest",
"entries": [
{
"kind": "r",
"key": "2cf8bf",
"mode": "block",
"auto": true,
"expires": 1800000000
},
{
"kind": "r",
"key": "8b2c",
"mode": "throttle",
"secs": 120
},
{
"kind": "r",
"key": "2cec10",
"mode": "dry"
},
{
"kind": "r",
"key": "8b2cf8bf",
"mode": "block"
}
],
"sorted": [
"r2cec10",
"r2cf8bf",
"r8b2c",
"r8b2cf8bf"
],
"auto": {
"count": 1,
"canonical": "72032cf8bf0000006b49d200",
"crc16": "4897"
},
"static": {
"count": 3,
"canonical": "72032cec100200000000000072028b2c0100780000000072048b2cf8bf00000000000000",
"crc16": "73B7"
}
}
]
}
+9
View File
@@ -69,6 +69,15 @@ CASES = [
{"kind": "p", "key": "5f14c420", "mode": "block"},
],
),
(
"r_three_byte_src_hop_any_dest",
[
{"kind": "r", "key": "2cf8bf", "mode": "block", "auto": True, "expires": 1800000000},
{"kind": "r", "key": "8b2c", "mode": "throttle", "secs": 120},
{"kind": "r", "key": "2cec10", "mode": "dry"},
{"kind": "r", "key": "8b2cf8bf", "mode": "block"},
],
),
]