feat: subnet-level attack detection (prefix_stats)

- XDP: prefix_stats LRU map, per-/24 (v4) and /64 (v6) SYN/packet counters,
  incremented post-blacklist/throttle (xdp/core/prefix_stats.h)
- userspace: SubnetDetector escalation ladder Monitor->StrictLimit->Challenge->Block
  with spoof-gate (Block requires >= min_unique_sources, CGNAT-safe)
- config: [detect.prefix] section (enabled=false by default)
- fallback without XDP: engine SubnetTracker aggregates connections per-prefix
- fix: PrefixStatsVal::from_bytes for xdp feature build; prefix_len 24 vs 64

cargo build/clippy(-D warnings, all features)/test green: 83 tests
This commit is contained in:
Loki 2026-08-24 09:47:21 +02:00
parent 08e26df73f
commit 5d7cdc24e3
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
14 changed files with 1044 additions and 11 deletions

View file

@ -1,5 +1,7 @@
use anyhow::Result;
use std::net::Ipv4Addr;
use anyhow::{Ok, Result};
use std::net::{IpAddr, Ipv4Addr};
use crate::traffic::prefix::{PrefixKey, PrefixStatsVal};
pub struct XdpFilter;
@ -17,9 +19,21 @@ impl XdpFilter {
pub fn ban_ip(&self, _ip: Ipv4Addr, _duration_secs: u64) -> Result<()> {
Ok(())
}
pub fn ban_cidr(&self, _prefix: IpAddr, _prefix_len: u8, _duration_secs: u64) -> Result<()> {
Ok(())
}
pub fn unban_ip(&self, _ip: Ipv4Addr) -> Result<()> {
Ok(())
}
/// Честная ошибка: чтение `prefix_stats` не подключено без feature `xdp`.
///
/// # Errors
/// Всегда — сборка без XDP не имеет доступа к картам ядра.
pub fn read_prefix_stats(&self) -> Result<Vec<(PrefixKey, PrefixStatsVal)>> {
anyhow::bail!("prefix_stats reading not wired yet: built without xdp feature")
}
pub fn get_stats(&self) -> Result<super::XdpStats> {
Ok(super::XdpStats::default())
}