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 15f474486a
commit 40bfe956e2
Signed by: boba
GPG key ID: 253067914055423B
14 changed files with 1044 additions and 11 deletions

View file

@ -2,8 +2,8 @@
mod sections;
pub use sections::{
BackendConfig, BanConfig, BindConfig, LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, StoreConfig,
WorkerConfig, XdpConfig,
BackendConfig, BanConfig, BindConfig, DetectConfig, DetectPrefixConfig, LimitsConfig, LoggingConfig, MetricsConfig,
PowConfig, StoreConfig, WorkerConfig, XdpConfig,
};
use serde::Deserialize;
@ -32,6 +32,8 @@ pub struct Config {
#[serde(default)]
pub pow: PowConfig,
#[serde(default)]
pub detect: DetectConfig,
#[serde(default)]
pub whitelist: Vec<String>,
}
@ -70,6 +72,9 @@ impl Config {
anyhow::bail!("invalid whitelist entry: {entry}");
}
}
if self.detect.prefix.enabled && self.detect.prefix.syn_threshold == 0 {
anyhow::bail!("detect.prefix.syn_threshold must be positive when enabled");
}
Ok(())
}
}
@ -140,4 +145,43 @@ upstreams = ["not-an-addr"]
let result = Config::parse_str("whitelist = [\"999.999.1.1\"]");
assert!(result.is_err());
}
#[test]
fn prefix_detect_defaults_disabled() {
let config = Config::parse_str("").expect("empty config should parse");
assert!(!config.detect.prefix.enabled);
assert_eq!(config.detect.prefix.syn_threshold, 500);
assert_eq!(config.detect.prefix.window_secs, 10);
assert_eq!(config.detect.prefix.min_unique_sources, 16);
}
#[test]
fn prefix_detect_section_parses() {
let config = Config::parse_str(
r#"
[detect.prefix]
enabled = true
syn_threshold = 1000
window_secs = 5
min_unique_sources = 8
"#,
)
.expect("detect.prefix section should parse");
assert!(config.detect.prefix.enabled);
assert_eq!(config.detect.prefix.syn_threshold, 1000);
assert_eq!(config.detect.prefix.window_secs, 5);
assert_eq!(config.detect.prefix.min_unique_sources, 8);
}
#[test]
fn prefix_detect_zero_threshold_rejected_when_enabled() {
let result = Config::parse_str(
r#"
[detect.prefix]
enabled = true
syn_threshold = 0
"#,
);
assert!(result.is_err());
}
}