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

+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;
}