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

@ -96,7 +96,12 @@ impl XdpFilter {
})
}
/// Reads the `prefix_stats` map (aggregates per /24 and /64 prefix).
/// Reads and resets the `prefix_stats` map (aggregates per /24 and /64).
///
/// The counters are per-window by contract: the detector compares them
/// against fixed thresholds, so leaving them cumulative would eventually
/// flag every prefix. Packets that arrive between the read and the reset
/// are lost, which is acceptable for statistics.
///
/// # Errors
/// XDP is not loaded, the map is missing from the object, or a BPF read fails.
@ -110,10 +115,13 @@ impl XdpFilter {
let Some(val) = map.lookup(&kb, MapFlags::ANY).context("prefix_stats lookup failed")? else {
continue;
};
let Ok(vb) = <[u8; 24]>::try_from(val.as_slice()) else {
let Ok(vb) = <[u8; 32]>::try_from(val.as_slice()) else {
continue;
};
out.push((PrefixKey::from_bytes(kb), PrefixStatsVal::from_bytes(vb)));
if let Err(e) = map.update(&kb, &[0u8; 32], MapFlags::ANY) {
tracing::debug!(prefix = ?PrefixKey::from_bytes(kb), error = %e, "prefix_stats reset failed");
}
}
Ok(out)
}