Add CLI listings for rate-limited flood adverts
List limited public-key prefixes, reasons, and wait times in bounded pages, with full-key details for targeted clearing. Queries preserve quotas and recovery timers. Validated with 71 native tests, Full repeater and room-server builds, and a 909.500 MHz RF test covering 10 forwarded adverts, 2 blocked adverts, and forwarding after targeted clear.
This commit is contained in:
1 parent
602dbfe3d9
commit
00163f2c2b
9 files changed
+374
-1
No files matched your search
@@ -2461,6 +2461,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
// Observer-only top-level commands (ota check/update, tls.bundletest, alert test)
|
||||
// live in CommonCLI_Observer.cpp.
|
||||
if (handleObserverCommand(sender_timestamp, command, reply)) return;
|
||||
if (mesh::cli::handleFloodAdvertGet(_callbacks->getFloodAdvertLimiter(), command, reply, millis())) return;
|
||||
if (mesh::cli::handleFloodAdvertClear(_callbacks->getFloodAdvertLimiter(), command, reply)) return;
|
||||
if (memcmp(command, "poweroff", 8) == 0 || memcmp(command, "shutdown", 8) == 0) {
|
||||
_board->powerOff(); // doesn't return
|
||||
|
||||
@@ -2,10 +2,82 @@
|
||||
|
||||
#include "FloodAdvertLimiter.h"
|
||||
#include <Utils.h>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace mesh {
|
||||
namespace cli {
|
||||
|
||||
inline const char* floodAdvertLimitReason(uint8_t reasons) {
|
||||
if (reasons & FloodAdvertLimiter::BadListRule) return "bad";
|
||||
if (reasons & FloodAdvertLimiter::ReceiveHistory) return "history";
|
||||
return "quota";
|
||||
}
|
||||
|
||||
// Keep pages inside the 160-byte reply shared by USB and authenticated LoRa CLI.
|
||||
// Row numbers refer to the current filtered list, not permanent table slots.
|
||||
inline bool handleFloodAdvertGet(FloodAdvertLimiter* limiter, const char* command,
|
||||
char* reply, uint32_t now) {
|
||||
static const char prefix[] = "get flood.advert";
|
||||
const size_t prefix_len = sizeof(prefix) - 1;
|
||||
if (strncmp(command, prefix, prefix_len) != 0) return false;
|
||||
const char* arg = command + prefix_len;
|
||||
if (*arg && *arg != ' ' && *arg != '\t' && *arg != '\r' && *arg != '\n') return false;
|
||||
while (*arg == ' ' || *arg == '\t') ++arg;
|
||||
const bool detail = strncmp(arg, "key", 3) == 0 && (arg[3] == ' ' || arg[3] == '\t');
|
||||
if (detail) {
|
||||
arg += 3;
|
||||
while (*arg == ' ' || *arg == '\t') ++arg;
|
||||
}
|
||||
unsigned number = 1;
|
||||
if (*arg >= '0' && *arg <= '9') {
|
||||
number = 0;
|
||||
do {
|
||||
number = number * 10 + unsigned(*arg++ - '0');
|
||||
if (number > 65535) break;
|
||||
} while (*arg >= '0' && *arg <= '9');
|
||||
} else if (detail) {
|
||||
number = 0;
|
||||
}
|
||||
while (*arg == ' ' || *arg == '\t' || *arg == '\r' || *arg == '\n') ++arg;
|
||||
if (*arg || number == 0 || number > 65535) {
|
||||
strcpy(reply, "ERR: get flood.advert [page] | key <index>");
|
||||
return true;
|
||||
}
|
||||
if (!limiter) {
|
||||
strcpy(reply, "ERR: advert limiter unavailable on this role");
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr size_t PAGE_SIZE = 3;
|
||||
FloodAdvertLimiter::LimitedEntry rows[PAGE_SIZE];
|
||||
const size_t offset = detail ? number - 1 : (number - 1) * PAGE_SIZE;
|
||||
const size_t total = limiter->listLimited(now, offset, rows, detail ? 1 : PAGE_SIZE);
|
||||
if (total == 0 && !detail && number == 1) {
|
||||
strcpy(reply, "> no rate-limited adverts");
|
||||
} else if (offset >= total) {
|
||||
strcpy(reply, "ERR: advert list index out of range");
|
||||
} else if (detail) {
|
||||
const auto& row = rows[0];
|
||||
char key[PUB_KEY_SIZE * 2 + 1];
|
||||
Utils::toHex(key, row.key, PUB_KEY_SIZE);
|
||||
snprintf(reply, 160, "> %s\n%s wait=%lus sent=%u/%u hops=%u recovery=%lus",
|
||||
key, floodAdvertLimitReason(row.reasons), (unsigned long)((row.wait_ms + 999) / 1000),
|
||||
unsigned(row.forwarded), unsigned(row.quota), unsigned(row.hops),
|
||||
(unsigned long)((row.recovery_ms + 999) / 1000));
|
||||
} else {
|
||||
size_t used = snprintf(reply, 160, "> page %u/%u limited=%u", number,
|
||||
unsigned((total + PAGE_SIZE - 1) / PAGE_SIZE), unsigned(total));
|
||||
for (size_t i = 0; i < PAGE_SIZE && offset + i < total && used < 159; ++i) {
|
||||
char key[FloodAdvertLimiter::PREFIX_BYTES * 2 + 1];
|
||||
Utils::toHex(key, rows[i].key, FloodAdvertLimiter::PREFIX_BYTES);
|
||||
used += snprintf(reply + used, 160 - used, "\n%u %s %s wait=%lus",
|
||||
unsigned(offset + i + 1), key, floodAdvertLimitReason(rows[i].reasons),
|
||||
(unsigned long)((rows[i].wait_ms + 999) / 1000));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Dispatch only after the role's ordinary CLI authorization checks. Requiring
|
||||
// an explicit "all" avoids treating missing/malformed keys as a global reset.
|
||||
inline bool handleFloodAdvertClear(FloodAdvertLimiter* limiter, const char* command, char* reply) {
|
||||
|
||||
@@ -183,4 +183,51 @@ bool FloodAdvertLimiter::isBad(const uint8_t* key, uint32_t now) {
|
||||
return entry && (entry->flags & BAD);
|
||||
}
|
||||
|
||||
size_t FloodAdvertLimiter::listLimited(uint32_t now, size_t offset,
|
||||
LimitedEntry* dest, size_t count) {
|
||||
refresh(now);
|
||||
size_t total = 0;
|
||||
for (size_t i = 0; i < _capacity; ++i) {
|
||||
const Entry& entry = _entries[i];
|
||||
if (!(entry.flags & ACTIVE)) continue;
|
||||
unsigned forwarded = 0;
|
||||
uint8_t hops = entry.min_hops;
|
||||
for (size_t j = 0; j < _capacity; ++j) {
|
||||
const Entry& other = _entries[j];
|
||||
if (!(other.flags & ACTIVE) || memcmp(other.key, entry.key, PREFIX_BYTES) != 0) continue;
|
||||
if (other.min_hops < hops) hops = other.min_hops;
|
||||
for (uint16_t bits = other.forwarded; bits; bits &= bits - 1) ++forwarded;
|
||||
}
|
||||
const uint8_t quota = allowance(hops);
|
||||
uint8_t reasons = 0;
|
||||
if (forwarded >= quota) reasons |= WindowQuota;
|
||||
// Once receive history fills, a new distinct payload is refused even if
|
||||
// another forwarding filter prevented earlier payloads spending quota.
|
||||
if (entry.count == HASH_SLOTS) reasons |= ReceiveHistory;
|
||||
if (entry.flags & BAD) reasons |= BadListRule;
|
||||
if (!reasons) continue;
|
||||
|
||||
if (dest && total >= offset && total - offset < count) {
|
||||
LimitedEntry& out = dest[total - offset];
|
||||
memcpy(out.key, entry.key, PUB_KEY_SIZE);
|
||||
out.forwarded = forwarded;
|
||||
out.quota = quota;
|
||||
out.hops = hops;
|
||||
out.reasons = reasons;
|
||||
out.wait_ms = reasons & (WindowQuota | ReceiveHistory)
|
||||
? WINDOW_MS - uint32_t(now - entry.window_start) : 0;
|
||||
out.recovery_ms = 0;
|
||||
if (reasons & BadListRule) {
|
||||
out.recovery_ms = RECOVERY_MS - uint32_t(now - entry.last_violation);
|
||||
if (entry.flags & HAS_FORWARDED) {
|
||||
const uint32_t bad_wait = BAD_INTERVAL_MS - uint32_t(now - entry.last_forwarded);
|
||||
if (bad_wait > out.wait_ms) out.wait_ms = bad_wait;
|
||||
}
|
||||
}
|
||||
}
|
||||
++total;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
} // namespace mesh
|
||||
@@ -41,6 +41,17 @@ public:
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
enum LimitReason : uint8_t { WindowQuota = 1, ReceiveHistory = 2, BadListRule = 4 };
|
||||
struct LimitedEntry {
|
||||
uint8_t key[PUB_KEY_SIZE];
|
||||
uint32_t wait_ms;
|
||||
uint32_t recovery_ms;
|
||||
uint16_t forwarded;
|
||||
uint8_t quota;
|
||||
uint8_t hops;
|
||||
uint8_t reasons;
|
||||
};
|
||||
|
||||
void reset();
|
||||
void clear(const uint8_t* full_key);
|
||||
void tick(uint32_t now);
|
||||
@@ -54,6 +65,10 @@ public:
|
||||
Decision check(const uint8_t* key, const uint8_t* hash, uint32_t now);
|
||||
void commit(const uint8_t* key, const uint8_t* hash, uint32_t now);
|
||||
bool isBad(const uint8_t* key, uint32_t now);
|
||||
// Return the total number of limited full keys and copy the requested slice.
|
||||
// Queries only expire elapsed history; they never consume quota, refresh
|
||||
// last-heard, or add abuse evidence. Prefix collisions remain separate keys.
|
||||
size_t listLimited(uint32_t now, size_t offset, LimitedEntry* dest, size_t count);
|
||||
|
||||
protected:
|
||||
FloodAdvertLimiter(Entry* entries, size_t capacity) : _entries(entries), _capacity(capacity) {}
|
||||
|
||||
Reference in new issue
Block a user