simple_repeater: filter hash -- runtime deny-forward podle hashe kanálu

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-10-14 20:34:19 +02:00
1 parent a79860e9c9
commit 4ca0850369
4 files changed
+113

No files matched your search

+90
View File
@@ -87,3 +87,93 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max
}
#endif // FILTER_CHANNEL_PSKS
// ---------------------------------------------------------------------------
// Keyless deny-forward by CHANNEL HASH -- independent of FILTER_CHANNEL_PSKS.
//
// The deny list above needs the PSK, so it can never touch a private channel.
// This one needs nothing: payload[0] of GRP_TXT/GRP_DATA is the channel hash,
// in the clear, and a repeater can refuse to re-air every packet carrying it.
// Born 14. 10. 2026 for 0x43 (one private chat+bot = 43 % of the airtime on
// the CZ mesh, reflooded all the way to remote-mast); FairnessLimiter only
// throttled it blindly, and its group bucket (hash & 0x1F) collides with
// 0x23/0x63/... anyway.
//
// * Content-neutral and coarse: the hash is 1 byte, so a deny entry also
// hits every OTHER channel whose key happens to hash to the same byte.
// Check the analyzer for what else lives on a byte before denying it.
// * Only forwarding by THIS node: reception and rxlog/pktfeed capture
// (CoreScope feed) happen before routeRecvPacket(), so the evidence trail
// stays intact.
// * Runtime-editable over RF admin and persisted in prefs (f_dhash/f_dhash_n),
// so an incident response is `filter hash add 43`, not a mast reflash.
bool MyMesh::filterHashDenied(const mesh::Packet* packet) const {
uint8_t n = _prefs.deny_hash_n;
if (n == 0) return false;
uint8_t type = packet->getPayloadType();
if (type != PAYLOAD_TYPE_GRP_TXT && type != PAYLOAD_TYPE_GRP_DATA) return false;
if (packet->payload_len < 1) return false;
if (n > sizeof(_prefs.deny_hash)) n = sizeof(_prefs.deny_hash);
for (int i = 0; i < n; i++) {
if (_prefs.deny_hash[i] == packet->payload[0]) return true;
}
return false;
}
// `filter hash` -- show list + drop counter; `filter hash add|del <hex>` --
// edit (1 byte, e.g. `43` or `0x43`); `filter hash clear` -- empty the list.
bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply_max) {
if (memcmp(command, "filter hash", 11) != 0 || (command[11] != 0 && command[11] != ' ')) return false;
const char* arg = (command[11] == ' ') ? &command[12] : "";
uint8_t n = _prefs.deny_hash_n;
if (n > sizeof(_prefs.deny_hash)) n = sizeof(_prefs.deny_hash);
if (strcmp(arg, "clear") == 0) {
_prefs.deny_hash_n = 0;
memset(_prefs.deny_hash, 0, sizeof(_prefs.deny_hash));
savePrefs();
strcpy(reply, "OK - hash 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) {
const char* hex = arg + 4;
if (hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X')) hex += 2;
char* end;
long v = strtol(hex, &end, 16);
if (end == hex || *end != 0 || end - hex > 2 || v < 0 || v > 0xFF) {
strcpy(reply, "Err - 1 byte hex, e.g. 43");
return true;
}
int at = -1;
for (int i = 0; i < n; i++) {
if (_prefs.deny_hash[i] == (uint8_t)v) { at = i; break; }
}
if (is_add) {
if (at >= 0) { strcpy(reply, "Err - already listed"); return true; }
if (n >= sizeof(_prefs.deny_hash)) { strcpy(reply, "Err - list full"); return true; }
_prefs.deny_hash[n++] = (uint8_t)v;
} else {
if (at < 0) { strcpy(reply, "Err - not listed"); return true; }
for (int i = at; i + 1 < n; i++) _prefs.deny_hash[i] = _prefs.deny_hash[i + 1];
_prefs.deny_hash[--n] = 0;
}
_prefs.deny_hash_n = n;
savePrefs();
// fall through to the listing as the reply
} else if (arg[0] != 0 && strcmp(arg, "list") != 0) {
strcpy(reply, "Err - filter hash [add|del <hex>|list|clear]");
return true;
}
int len = snprintf(reply, reply_max, "filter hash: dropped %u, deny:", filter_hash_dropped);
if (n == 0 && len < reply_max) snprintf(&reply[len], reply_max - len, " (nic)");
for (int i = 0; i < n && len < reply_max; i++) {
len += snprintf(&reply[len], reply_max - len, " %02X", _prefs.deny_hash[i]);
}
return true;
}
+12
View File
@@ -699,6 +699,14 @@ void MyMesh::sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, ui
bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
if (_prefs.disable_fwd) return false;
// CUSTOM (TeTeHacko): keyless channel-hash deny list (`filter hash`). Before
// takeForwardingRateLimit() in routeRecvPacket(), so a vetoed packet does not
// eat FairnessLimiter tokens either.
if (filterHashDenied(packet)) {
filter_hash_dropped++;
MESH_DEBUG_PRINTLN("allowPacketForward: channel hash %02X on deny list", packet->payload[0]);
return false;
}
if (packet->isRouteFlood()
&& mesh::isFloodHopLimitExceeded(packet, _prefs.flood_max, _prefs.flood_max_unscoped, _prefs.flood_max_advert)) {
return false;
@@ -1347,6 +1355,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
#define FAIRNESS_ENABLED_DEFAULT 1
#endif
_prefs.fairness_enabled = FAIRNESS_ENABLED_DEFAULT;
filter_hash_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;
@@ -1763,6 +1772,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
// CUSTOM (TeTeHacko): `bot` — read-only channel bot diagnostics. See ChannelBot.h.
if (botHandleCommand(command, reply)) return;
#endif
// CUSTOM (TeTeHacko): `filter hash [add|del <hex>|clear]` — keyless channel-hash
// deny list. Before filterHandleCommand(), which would claim any `filter ...`.
if (filterHashHandleCommand(command, reply, reply_max)) return;
#ifdef FILTER_CHANNEL_PSKS
// CUSTOM (TeTeHacko): `filter add|del|list|clear` — runtime deny list for
// channel messages this repeater refuses to forward. See ChannelFilter.h.
+4
View File
@@ -209,6 +209,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
// CUSTOM (TeTeHacko): token-bucket limiter over FORWARDED traffic (port of
// upstream proposal #1502). Kill switch: _prefs.fairness_enabled.
FairnessLimiter fairness_limiter;
uint32_t filter_hash_dropped; // forwards vetoed by the channel-hash deny list
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;
@@ -321,6 +322,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
#endif
// CUSTOM (TeTeHacko): `fairness [on|off|grid|cap ...|refill ...]` CLI. Defined in MyMesh.cpp.
bool fairnessHandleCommand(const char* command, char* reply, int reply_max);
// CUSTOM (TeTeHacko): keyless deny-forward by channel hash. Definitions in ChannelFilter.h.
bool filterHashDenied(const mesh::Packet* packet) const;
bool filterHashHandleCommand(const char* command, char* reply, int reply_max);
// Refill interval in ms: persisted pref (seconds) if set, else the build default.
uint32_t fairGroupRefillMs() const {
return _prefs.fair_group_refill_s ? (uint32_t)_prefs.fair_group_refill_s * 1000 : FAIRNESS_GROUP_REFILL_MS;
+7
View File
@@ -77,6 +77,10 @@ public:
// just leave them at defaults.
uint8_t fairness_enabled = 0; // FairnessLimiter runtime kill switch
char filter_deny[128]; // channel-filter deny list, comma-separated node names
// channel hashes (payload[0] of GRP_TXT/GRP_DATA) this node refuses to forward,
// keyless -- works on private channels too. See ChannelFilter.h (`filter hash`).
uint8_t deny_hash[8];
uint8_t deny_hash_n = 0;
// Battery state as an icon on the advertised name (see BatteryIcon.h).
// Off by default: it costs bytes of the name, so it has to be asked for.
uint8_t batt_icon = 0; // boolean
@@ -204,6 +208,8 @@ private:
def("loop", _parent->loop_detect);
def("fair_en", _parent->fairness_enabled);
def("f_deny", _parent->filter_deny, sizeof(_parent->filter_deny));
def("f_dhash", (void*)_parent->deny_hash, sizeof(_parent->deny_hash));
def("f_dhash_n", _parent->deny_hash_n);
def("fair_gcap", _parent->fair_group_cap);
def("fair_scap", _parent->fair_sender_cap);
def("fair_acap", _parent->fair_advert_cap);
@@ -256,6 +262,7 @@ public:
bridge_secret[0] = 0;
owner_info[0] = 0;
filter_deny[0] = 0;
memset(deny_hash, 0, sizeof(deny_hash));
}
CommonRadioPrefs* getRadioPrefs() { return &radio; }