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

@ -31,15 +31,6 @@ impl Source {
Source::Tracker(tracker) => tracker.snapshot().await,
}
}
/// Whether the source reports distinct source addresses per prefix.
fn counts_unique_sources(&self) -> bool {
match self {
#[cfg(feature = "xdp")]
Source::Xdp(_) => false,
Source::Tracker(_) => true,
}
}
}
/// Verdict enforcement settings shared by every snapshot tick.
@ -82,7 +73,6 @@ pub fn spawn(
ttl: ban_ttl,
trusted: trusted_peer,
};
let extreme_threshold = detect.syn_threshold.saturating_mul(4);
tokio::spawn(async move {
let mut tick = tokio::time::interval(window);
@ -99,7 +89,7 @@ pub fn spawn(
match source.snapshot().await {
Ok(snaps) => {
let verdicts = detector.evaluate(&snaps);
apply_verdicts(&verdicts, &source, &policy, extreme_threshold);
apply_verdicts(&verdicts, &source, &policy);
},
Err(e) => tracing::debug!("prefix stats unavailable: {e}"),
}
@ -119,7 +109,7 @@ fn build_source(tracker: Option<Arc<SubnetTracker>>, xdp: Option<Arc<Mutex<XdpFi
tracker.map(|t| Source::Tracker(TrackerSource { tracker: t }))
}
fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolicy, extreme_threshold: u64) {
fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolicy) {
for pv in verdicts {
metrics::SUBNET_VERDICTS
.with_label_values(&[verdict_name(pv.verdict)])
@ -131,18 +121,6 @@ fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolic
verdict = verdict_name(pv.verdict),
"subnet verdict"
);
if pv.verdict == SubnetVerdict::Challenge
&& !source.counts_unique_sources()
&& pv.snapshot.syn_count >= extreme_threshold
{
// XDP does not count distinct sources, so the spoof gate can never
// confirm `Block` on that path; make the cap visible instead of silent.
tracing::info!(
prefix = %pv.prefix,
syn = pv.snapshot.syn_count,
"spoof gate suppressed block: XDP has no source diversity data; escalating at L7 only"
);
}
}
match source {
#[cfg(feature = "xdp")]

View file

@ -9,30 +9,41 @@ pub struct PrefixStatsVal {
pub syn_count: u64,
pub pkt_count: u64,
pub last_seen_ns: u64,
/// 64-bit bitmap of hashed source addresses; popcount estimates the number
/// of distinct sources in the window.
pub source_bits: u64,
}
impl PrefixStatsVal {
/// Parses the raw 24-byte map value.
/// Parses the raw 32-byte map value.
#[must_use]
pub fn from_bytes(bytes: [u8; 24]) -> Self {
pub fn from_bytes(bytes: [u8; 32]) -> Self {
let read_u64 =
|range: std::ops::Range<usize>| u64::from_le_bytes(bytes[range].try_into().expect("8-byte field"));
Self {
syn_count: read_u64(0..8),
pkt_count: read_u64(8..16),
last_seen_ns: read_u64(16..24),
source_bits: read_u64(24..32),
}
}
/// Serialises the value into the raw 24-byte map layout.
/// Serialises the value into the raw 32-byte map layout.
#[must_use]
pub fn to_bytes(self) -> [u8; 24] {
let mut out = [0u8; 24];
pub fn to_bytes(self) -> [u8; 32] {
let mut out = [0u8; 32];
out[0..8].copy_from_slice(&self.syn_count.to_le_bytes());
out[8..16].copy_from_slice(&self.pkt_count.to_le_bytes());
out[16..24].copy_from_slice(&self.last_seen_ns.to_le_bytes());
out[24..32].copy_from_slice(&self.source_bits.to_le_bytes());
out
}
/// Estimated number of distinct sources seen in this window.
#[must_use]
pub fn unique_sources(&self) -> u64 {
u64::from(self.source_bits.count_ones())
}
}
/// Aggregated counters for one prefix over a detection window.
@ -178,26 +189,10 @@ impl PrefixStatsSource for XdpPrefixStats {
syn_count: val.syn_count,
pkt_count: val.pkt_count,
last_seen_ns: val.last_seen_ns,
unique_sources: 0,
unique_sources: val.unique_sources(),
},
))
})
.collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stats_val_roundtrip() {
let val = PrefixStatsVal {
syn_count: 42,
pkt_count: 100,
last_seen_ns: 1_234_567_890,
};
assert_eq!(PrefixStatsVal::from_bytes(val.to_bytes()), val);
assert_eq!(size_of::<PrefixStatsVal>(), 24);
}
}

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)
}