feat(xdp): estimate distinct sources in prefix stats, fix the detection window

The subnet detector's spoof gate needs the number of distinct sources in a
prefix, but the XDP path reported 0, so a /24 flooded from a botnet could never
reach the Block verdict and the whole subnet detector was inert with XDP on.

- prefix_stats carries a 64-bit bitmap of hashed source addresses (interface
  ID for IPv6, address for IPv4); userspace counts the set bits.
- read_prefix_stats now resets each entry after reading: the counters are
  documented as per-window and compared against fixed thresholds, but were
  cumulative, so with enough traffic every active prefix eventually looked
  like an attack.
- The monitor no longer suppresses Block on the XDP path and no longer carries
  the now-dead "no source diversity" branch.

ABI: the prefix_stats value grows 24 -> 32 bytes; the xdp_object_maps test
pins the new size.
This commit is contained in:
Loki 2026-11-16 12:36:19 +01:00
parent c1a076a4a3
commit 9baae25acb
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
8 changed files with 88 additions and 56 deletions

View file

@ -152,7 +152,7 @@ fn mapped_ipv6_treated_as_ipv4() {
#[test]
fn prefix_key_roundtrip_and_layout() {
assert_eq!(size_of::<PrefixKey>(), 24);
assert_eq!(size_of::<PrefixStatsVal>(), 24);
assert_eq!(size_of::<PrefixStatsVal>(), 32);
let key = PrefixKey::new(v4([10, 1, 2, 3]));
assert_eq!(key.family, rampart::traffic::prefix::PREFIX_FAMILY_V4);
assert_eq!(key.to_prefix(), Some(v4([10, 1, 2, 0])));

View file

@ -0,0 +1,25 @@
//! Layout of the `prefix_stats` BPF map value: the 32-byte little-endian
//! encoding round-trips and the struct size matches the kernel side.
use rampart::traffic::prefix::PrefixStatsVal;
#[test]
fn stats_val_roundtrip() {
let val = PrefixStatsVal {
syn_count: 42,
pkt_count: 100,
last_seen_ns: 1_234_567_890,
source_bits: 0b1011,
};
assert_eq!(PrefixStatsVal::from_bytes(val.to_bytes()), val);
assert_eq!(size_of::<PrefixStatsVal>(), 32);
}
#[test]
fn unique_sources_counts_set_bits() {
let val = PrefixStatsVal {
source_bits: 0b1011,
..PrefixStatsVal::default()
};
assert_eq!(val.unique_sources(), 3);
}

View file

@ -69,7 +69,7 @@ fn map_key_and_value_sizes_match_the_rust_encoding() {
("blacklist6_map", 20, 8),
("whitelist_map", 8, 1),
("whitelist6_map", 20, 1),
("prefix_stats", 24, 24),
("prefix_stats", 24, 32),
("stats_map", 4, 8),
];
for (name, key_size, value_size) in expected {