Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions docs/packet_filter_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ This allows up to **20 Group Text packets every 60 seconds**.

Setting the limit to **0** disables rate limiting for that packet type.

Group Text (type 05) is counted last: after the channel block list, the
malformed scan, the age limit and the sender/text rules. Traffic those already
drop never uses up the group-text budget that the remaining channels share.

## Soft cutoff

By default a rate limit is a hard cutoff: once a type reaches its limit inside
Expand Down Expand Up @@ -513,6 +517,9 @@ The channel hash is one byte, so about one channel in 256 shares it with
MAC also checks out under the key of `#bot` (the key the apps derive from the
name). Other channels with the same hash byte are still forwarded.

A channel name is at most 31 characters, including the `#`. A longer name is
refused with `Failed`.

Block on the hash byte alone, including every other channel that shares it:

```text
Expand Down Expand Up @@ -625,7 +632,9 @@ When enabled, Group Text packets are checked for:

The ±1 week check is skipped while the repeater's clock has not been set (see
[Message age limit](#message-age-limit)); a zero timestamp is always rejected.
The malformed scan reads the Public channel only.
The malformed scan reads the Public channel only. A packet the Public key cannot
open (a private channel whose hash byte happens to equal Public's, `11`) is not
judged and passes.

Default:

Expand Down Expand Up @@ -1015,16 +1024,19 @@ Nothing is dropped while dry-run is on; the counters show what *would* be.
| `> Filter: command error` | Unrecognised subcommand |
| `> Filter: error <type> range is 0-11` | Packet type outside `00`–`11` |
| `> Filter: error <max_hops> range is 0-64` | Hop limit outside `0`–`64` |
| `> Filter: error <limit> and <soft> range is 0-65535` | Rate limit or soft cutoff out of range |
| `> Filter: error <secs> must not be negative` | Negative rate window |
| `> Filter: error hash bytes range is 1-3` | Minimum path hash size outside `1`–`3` |
| `> Filter: error <hours> range is 0-720` | Advert window outside `0`–`720` |
| `> Filter: error <minutes> range is 1-10080, or off` | Message age limit out of range or not a number |
| `> Filter: error path prefix is 2-8 hex digits` | Path prefix empty, odd-length, longer than 4 bytes, or not hex |
| `> Filter: error <secs> range is 0-65535, <prob> range is 1-100` | Sender/text rule argument out of range or not a number |
| `> Filter: error <pattern> max 15 chars` / `max 23 chars` | Sender name or text pattern too long |
| `> Filter: syntax error 'filter malformed <on \| off>'` | Wrong argument |
| `> Filter: syntax error 'filter dryrun <on \| off>'` | Wrong argument |
| `> Filter: syntax error 'filter hops <type> <max_hops>'` | Wrong number of arguments |
| `> Filter: syntax error 'filter rate <type> <limit> <secs>'` | Wrong number of arguments |
| `> Filter: syntax error 'filter channel [list \| add [hash] \| remove] <#name \| Public>'` | Wrong number of arguments |
| `Failed` | `filter channel add` with the list full, or `remove` with no such channel; `filter path add` with the list full or the prefix already blocked, or `remove` with no such prefix; the same for `filter sender`, `filter text` and `filter watch` (`watch add Public` also fails: it is always watched) |
| `Failed` | `filter channel add` with the list full or a name over 31 characters, or `remove` with no such channel; `filter path add` with the list full or the prefix already blocked, or `remove` with no such prefix; the same for `filter sender`, `filter text` and `filter watch` (`watch add Public` also fails: it is always watched) |

An unknown `filter stats` topic is not an error: it returns the list of topics.
84 changes: 52 additions & 32 deletions examples/simple_repeater/Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ bool Filter::allowPacketForward(const mesh::Packet* packet) {
return drop(packet);
}
}
// rate limiter (with optional probabilistic soft cutoff)
if (!_limiters[type].allow(_rtc->getCurrentTime(), nextRandom())) {
// rate limiter (with optional probabilistic soft cutoff). Group text is
// limited last instead, after the channel block and the content rules, so
// traffic those already drop never uses up the shared group-text budget.
if (type != PAYLOAD_TYPE_GRP_TXT && overRateLimit(type)) {
FilterStat::recordRate(_cnt, type);
if (have_src) FilterStat::recordSrc(_cnt, src);
return drop(packet);
Expand All @@ -100,7 +102,7 @@ bool Filter::allowPacketForward(const mesh::Packet* packet) {
// only when the MAC also checks out under its key, unless it was added as
// hash only. The MAC is computed only on a hash match.
for (int i=0; i<FILTER_CHANNEL_COUNT; i++) {
ChannelDetails &ch = _prefs.filter_channels[i];
FilterChannel &ch = _prefs.filter_channels[i];
if (ch.name[0] == '\0') continue;
if (channel_hash != ch.channel.hash[0]) continue;
if (!_prefs.channel_hash_only[i] && !channelKeyMatch(packet, ch.channel.secret)) continue;
Expand All @@ -118,8 +120,9 @@ bool Filter::allowPacketForward(const mesh::Packet* packet) {
int len = mesh::Utils::MACThenDecrypt(secret != nullptr ? secret : PUBLIC_CHANNEL_SECRET, data,
&packet->payload[PATH_HASH_SIZE], packet->payload_len - PATH_HASH_SIZE);

// malformed
if (want_malformed) {
// malformed; len == 0 means the Public key did not open it (a private
// channel whose hash collides with Public's), so there is no content to judge
if (want_malformed && len > 0) {
uint8_t reason;
if (!validMessageContent(data, len, &reason)) {
FilterStat::recordMalformed(_cnt, reason);
Expand Down Expand Up @@ -160,6 +163,11 @@ bool Filter::allowPacketForward(const mesh::Packet* packet) {
}
}
}

if (overRateLimit(type)) {
FilterStat::recordRate(_cnt, type);
return drop(packet);
}
}

// allowed
Expand All @@ -174,6 +182,10 @@ bool Filter::channelKeyMatch(const mesh::Packet* packet, const uint8_t* secret)
packet->payload_len - PATH_HASH_SIZE) > 0;
}

bool Filter::overRateLimit(uint8_t type) {
return !_limiters[type].allow(_rtc->getCurrentTime(), nextRandom());
}

// The 1-byte identity hash of whoever originated the packet, where the payload
// carries one. ACK and TRACE hold no identity and group traffic is encrypted,
// so those drops only ever show up in the totals.
Expand Down Expand Up @@ -315,15 +327,15 @@ void Filter::handleCommand(FILESYSTEM* fs, char* command, char* reply) {
} else if (strcmp(parts[1], "hops") == 0) {

if (n == 4) {
uint8_t type = atoi(parts[2]);
uint8_t count = atoi(parts[3]);
long type = atol(parts[2]);
long count = atol(parts[3]);

if (type < 0 || type >= PAYLOAD_TYPE_COUNT) {
strcpy(reply, "> Filter: error <type> range is 0-11");
} else if (count < 0 || count > 64) {
strcpy(reply, "> Filter: error <max_hops> range is 0-64");
} else {
_prefs.payload_prefs[type].hops_max = count;
_prefs.payload_prefs[type].hops_max = (uint8_t)count;
save(fs);
strcpy(reply, "> Filter: OK");
}
Expand All @@ -335,20 +347,24 @@ void Filter::handleCommand(FILESYSTEM* fs, char* command, char* reply) {
} else if (strcmp(parts[1], "rate") == 0) {

if (n == 5 || n == 6) {
uint8_t type = atoi(parts[2]);
uint16_t limit = atoi(parts[3]);
uint32_t secs = atoi(parts[4]);
uint16_t soft = (n == 6) ? atoi(parts[5]) : 0; // omitted -> hard cutoff
long type = atol(parts[2]);
long limit = atol(parts[3]);
long secs = atol(parts[4]);
long soft = (n == 6) ? atol(parts[5]) : 0; // omitted -> hard cutoff

if (type < 0 || type >= PAYLOAD_TYPE_COUNT) {
strcpy(reply, "> Filter: error <type> range is 0-11");
} else if (limit < 0 || limit > 65535 || soft < 0 || soft > 65535) {
strcpy(reply, "> Filter: error <limit> and <soft> range is 0-65535");
} else if (secs < 0) {
strcpy(reply, "> Filter: error <secs> must not be negative");
} else if (soft != 0 && soft >= limit) {
strcpy(reply, "> Filter: error <soft> must be less than <limit>");
} else {
_prefs.payload_prefs[type].rate_limit = limit;
_prefs.payload_prefs[type].rate_secs = secs;
_prefs.soft_limit[type] = soft;
_limiters[type].init(limit, secs, soft);
_prefs.payload_prefs[type].rate_limit = (uint16_t)limit;
_prefs.payload_prefs[type].rate_secs = (uint32_t)secs;
_prefs.soft_limit[type] = (uint16_t)soft;
_limiters[type].init((uint16_t)limit, (uint32_t)secs, (uint16_t)soft);
save(fs);
strcpy(reply, "> Filter: OK");
}
Expand Down Expand Up @@ -388,11 +404,11 @@ void Filter::handleCommand(FILESYSTEM* fs, char* command, char* reply) {

// hash
} else if (strcmp(parts[1], "hash") == 0) {
uint8_t count = atoi(parts[2]);
long count = atol(parts[2]);
if (count < 1 || count > 3) {
strcpy(reply, "> Filter: error hash bytes range is 1-3");
} else {
_prefs.minimal_hash_bytes = count;
_prefs.minimal_hash_bytes = (uint8_t)count;
save(fs);
strcpy(reply, "> Filter: OK");
}
Expand All @@ -407,6 +423,8 @@ void Filter::handleCommand(FILESYSTEM* fs, char* command, char* reply) {
_prefs.filter_malformed = false;
strcpy(reply, "> Filter: malformed scan off");
save(fs);
} else {
strcpy(reply, "> Filter: syntax error 'filter malformed <on | off>'");
}

// dryrun
Expand Down Expand Up @@ -573,20 +591,20 @@ bool Filter::hasContentRules(void) const {
const uint8_t* Filter::watchedSecret(uint8_t channel_hash) const {
if (channel_hash == PUBLIC_CHANNEL_HASH) return PUBLIC_CHANNEL_SECRET;
for (int i = 0; i < FILTER_WATCH_COUNT; i++) {
const ChannelDetails& ch = _prefs.watch_channels[i];
const FilterChannel& ch = _prefs.watch_channels[i];
if (ch.name[0] == '\0') continue;
if (ch.channel.hash[0] == channel_hash) return ch.channel.secret;
}
return nullptr;
}

bool Filter::addWatch(const char* name) {
if (name == nullptr || name[0] == '\0' || strlen(name) >= sizeof(ChannelDetails::name)) return false;
if (name == nullptr || name[0] == '\0' || strlen(name) >= sizeof(FilterChannel::name)) return false;
if (strcmp(name, "Public") == 0) return false; // always watched

int free_slot = -1;
for (int i = 0; i < FILTER_WATCH_COUNT; i++) {
ChannelDetails& ch = _prefs.watch_channels[i];
FilterChannel& ch = _prefs.watch_channels[i];
if (ch.name[0] == '\0') {
if (free_slot < 0) free_slot = i;
} else if (strcmp(ch.name, name) == 0) {
Expand All @@ -595,7 +613,7 @@ bool Filter::addWatch(const char* name) {
}
if (free_slot < 0) return false;

ChannelDetails& ch = _prefs.watch_channels[free_slot];
FilterChannel& ch = _prefs.watch_channels[free_slot];
strncpy(ch.name, name, sizeof(ch.name) - 1);
ch.name[sizeof(ch.name) - 1] = '\0';
getChannelHash(name, &ch.channel);
Expand All @@ -605,7 +623,7 @@ bool Filter::addWatch(const char* name) {
bool Filter::removeWatch(const char* name) {
if (name == nullptr || name[0] == '\0') return false;
for (int i = 0; i < FILTER_WATCH_COUNT; i++) {
ChannelDetails& ch = _prefs.watch_channels[i];
FilterChannel& ch = _prefs.watch_channels[i];
if (strcmp(ch.name, name) == 0) {
ch.name[0] = '\0';
return true;
Expand All @@ -624,7 +642,7 @@ void Filter::listWatchNames(char* out_buf, size_t out_size) {
b.add("Public (11)");
listed++;
for (int i = 0; i < FILTER_WATCH_COUNT; i++) {
const ChannelDetails& ch = _prefs.watch_channels[i];
const FilterChannel& ch = _prefs.watch_channels[i];
if (ch.name[0] == '\0') continue;
mesh::Utils::toHex(channel_hex, ch.channel.hash, 1);
b.add(",%s (%s)", ch.name, channel_hex);
Expand Down Expand Up @@ -652,13 +670,15 @@ void Filter::formatResponse(char *reply, ResponseType rtype) {
b.markTruncated("..");
}

// Adding a channel that is already on the list switches its match mode.
// Adding a channel that is already on the list switches its match mode. A name
// that does not fit the slot is refused: load() derives the key from the stored
// name, so a cut-off name would block another channel after a reboot.
bool Filter::addChannel(const char* name, bool hash_only) {
if (name == nullptr || name[0] == '\0') return false;
if (name == nullptr || name[0] == '\0' || strlen(name) >= sizeof(FilterChannel::name)) return false;

int free_slot = -1;
for (int i=0; i<FILTER_CHANNEL_COUNT; i++) {
ChannelDetails &ch = _prefs.filter_channels[i];
FilterChannel &ch = _prefs.filter_channels[i];
if (ch.name[0] == '\0') {
if (free_slot < 0) free_slot = i;
} else if (strncmp(ch.name, name, sizeof(ch.name)-1) == 0) {
Expand All @@ -668,7 +688,7 @@ bool Filter::addChannel(const char* name, bool hash_only) {
}
if (free_slot < 0) return false;

ChannelDetails &ch = _prefs.filter_channels[free_slot];
FilterChannel &ch = _prefs.filter_channels[free_slot];
strncpy(ch.name, name, sizeof(ch.name)-1);
getChannelHash(name, &ch.channel);
_prefs.channel_hash_only[free_slot] = hash_only;
Expand All @@ -679,7 +699,7 @@ bool Filter::removeChannel(const char* name) {
if (name == nullptr || name[0] == '\0') return false;

for (int i=0; i<FILTER_CHANNEL_COUNT; i++) {
ChannelDetails &ch = _prefs.filter_channels[i];
FilterChannel &ch = _prefs.filter_channels[i];
if (strcmp(ch.name, name) == 0) {
ch.name[0] = '\0';
_prefs.channel_hash_only[i] = 0;
Expand Down Expand Up @@ -791,7 +811,7 @@ bool Filter::load(FILESYSTEM* fs) {
memset(_prefs.text_rules, 0, sizeof(_prefs.text_rules));
}
if (!FilterStat::fieldLoaded(got, offsetof(FilterPrefs, watch_channels), sizeof(_prefs.watch_channels))) {
for (int i = 0; i < FILTER_WATCH_COUNT; i++) _prefs.watch_channels[i] = ChannelDetails();
for (int i = 0; i < FILTER_WATCH_COUNT; i++) _prefs.watch_channels[i] = FilterChannel();
}
if (!FilterStat::fieldLoaded(got, offsetof(FilterPrefs, age_mins), sizeof(_prefs.age_mins))) {
_prefs.age_mins = 0;
Expand Down Expand Up @@ -826,13 +846,13 @@ bool Filter::load(FILESYSTEM* fs) {
if (tr.prob == 0 || tr.prob > 100) tr.prob = 100;
}
for (int i = 0; i < FILTER_CHANNEL_COUNT; i++) {
ChannelDetails& ch = _prefs.filter_channels[i];
FilterChannel& ch = _prefs.filter_channels[i];
ch.name[sizeof(ch.name) - 1] = '\0';
if (ch.name[0] != '\0') getChannelHash(ch.name, &ch.channel); // the MAC check needs the right key
_prefs.channel_hash_only[i] = constrain(_prefs.channel_hash_only[i], 0, 1);
}
for (int i = 0; i < FILTER_WATCH_COUNT; i++) {
ChannelDetails& ch = _prefs.watch_channels[i];
FilterChannel& ch = _prefs.watch_channels[i];
ch.name[sizeof(ch.name) - 1] = '\0';
if (ch.name[0] != '\0') getChannelHash(ch.name, &ch.channel); // never trust a stored key
}
Expand Down
11 changes: 7 additions & 4 deletions examples/simple_repeater/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ static const uint8_t PUBLIC_CHANNEL_SECRET[PUB_KEY_SIZE] = { 0x8B, 0x33, 0x87, 0
0, 0, 0, 0, 0, 0, 0, 0 };
static const uint8_t PUBLIC_CHANNEL_HASH = 0x11;

struct ChannelDetails {
struct FilterChannel {
mesh::GroupChannel channel;
char name[32];

ChannelDetails() {
FilterChannel() {
name[0] = '\0';
memset(channel.hash, 0, sizeof(channel.hash));
memset(channel.secret, 0, sizeof(channel.secret));
Expand All @@ -53,7 +53,7 @@ struct FilterPrefs {
[PAYLOAD_TYPE_MULTIPART] = { .hops_max = 8, .rate_limit = 5, .rate_secs = 60 },
[PAYLOAD_TYPE_CONTROL] = { .hops_max = 8, .rate_limit = 5, .rate_secs = 60 },
};
ChannelDetails filter_channels[FILTER_CHANNEL_COUNT];
FilterChannel filter_channels[FILTER_CHANNEL_COUNT];
uint8_t minimal_hash_bytes = 1;
uint8_t filter_malformed = false;
// Appended at the end of the struct so older /filter_prefs files (which lack
Expand All @@ -66,7 +66,7 @@ struct FilterPrefs {
PathPrefix path_block[FILTER_PATH_COUNT] = {}; // blocked path-ID prefixes
SenderRule sender_rules[FILTER_RULE_COUNT] = {}; // group-text sender rules (ordered)
TextRule text_rules[FILTER_RULE_COUNT] = {}; // group-text text rules (ordered)
ChannelDetails watch_channels[FILTER_WATCH_COUNT]; // channels decrypted for the rules, besides Public
FilterChannel watch_channels[FILTER_WATCH_COUNT]; // channels decrypted for the rules, besides Public
uint16_t age_mins = 0; // drop group text older than this, 0 = off
// Per filter_channels slot: 1 = match on the channel hash byte alone, 0 = the
// MAC must also check out under the channel's key. Files without this field
Expand All @@ -91,6 +91,9 @@ class Filter {
// to every reason alike. Returns the forwarding verdict.
bool drop(const mesh::Packet *packet);

// Consumes one slot of this payload type's rate budget; true when over it.
bool overRateLimit(uint8_t type);

// Rebuild the per-type rate limiters from _prefs. Every path that replaces
// the prefs (defaults, reset, load) must end here, or the limiters keep
// enforcing whatever they held before (all-zero = no limit at all).
Expand Down
Loading
Loading