From 72c1f45d6244b5e342aff2e7b4f04a3c11297557 Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Mon, 16 Nov 2026 12:55:51 +0100 Subject: [PATCH 1/4] perf: criterion benchmarks for the per-connection hot path - benches/hot_paths.rs measures RateLimiter::check, Blacklist exact/CIDR/miss lookups, SubnetDetector::evaluate over 256 prefixes and HTTP request-head validation; `cargo bench --all-features --bench hot_paths`, HTML report in target/criterion. - Dev-machine medians recorded in docs/testing.md (~110 ns rate limit, ~70 ns exact ban lookup, ~220 ns CIDR lookup, ~2.6 us detector window, ~280 ns HTTP head). - Makefile `bench` target; the bench is gated on protocol-http. --- Cargo.lock | 228 ++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 7 ++ Makefile | 5 +- benches/hot_paths.rs | 107 ++++++++++++++++++++ docs/testing.md | 29 +++++- 5 files changed, 369 insertions(+), 7 deletions(-) create mode 100644 benches/hot_paths.rs diff --git a/Cargo.lock b/Cargo.lock index 957525f..8666d85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + [[package]] name = "anstream" version = "1.0.0" @@ -304,6 +310,12 @@ version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "castaway" version = "0.2.4" @@ -362,6 +374,33 @@ dependencies = [ "windows-link", ] +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + [[package]] name = "clap" version = "4.6.7" @@ -494,12 +533,67 @@ dependencies = [ "libc", ] +[[package]] +name = "criterion" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +dependencies = [ + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "is-terminal", + "itertools 0.10.5", + "num-traits", + "once_cell", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools 0.10.5", +] + [[package]] name = "critical-section" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.23" @@ -533,6 +627,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + [[package]] name = "crypto-common" version = "0.1.7" @@ -951,6 +1051,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -974,6 +1085,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" + [[package]] name = "hex" version = "0.4.3" @@ -1271,12 +1388,32 @@ version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf370abdafd54d13e54a620e8c3e1145f28e46cc9d704bc6d94414559df41763" +[[package]] +name = "is-terminal" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.14.0" @@ -1703,6 +1840,12 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + [[package]] name = "openssl-probe" version = "0.2.1" @@ -1904,6 +2047,34 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + [[package]] name = "portable-atomic" version = "1.15.0" @@ -2088,6 +2259,7 @@ dependencies = [ "axum", "chrono", "clap", + "criterion", "crossterm", "dashmap", "futures", @@ -2186,7 +2358,7 @@ dependencies = [ "compact_str", "critical-section", "hashbrown 0.17.1", - "itertools", + "itertools 0.14.0", "kasuari", "lru", "palette", @@ -2251,7 +2423,7 @@ dependencies = [ "hashbrown 0.17.1", "indoc", "instability", - "itertools", + "itertools 0.14.0", "line-clipping", "ratatui-core", "serde", @@ -2261,6 +2433,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redis" version = "1.7.1" @@ -3052,6 +3244,16 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "tinyvec" version = "1.13.3" @@ -3333,7 +3535,7 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5" dependencies = [ - "itertools", + "itertools 0.14.0", "unicode-segmentation", "unicode-width", ] @@ -3842,6 +4044,26 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "zerofrom" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index b5fb057..fffefa1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,3 +80,10 @@ libc = { version = "0.2", optional = true } [dev-dependencies] # `ServiceExt::oneshot` drives the axum router in integration tests. tower = { version = "0.5", features = ["util"] } +# Micro-benchmarks of the per-connection hot path (`cargo bench --all-features`). +criterion = { version = "0.5", features = ["html_reports"] } + +[[bench]] +name = "hot_paths" +harness = false +required-features = ["protocol-http"] diff --git a/Makefile b/Makefile index d0e070a..7b1c8e1 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CARGO = cargo TARGET_DIR = target -.PHONY: all build release test test-all check check-all fmt fmt-check clippy clean deny audit fuzz +.PHONY: all build release test test-all check check-all fmt fmt-check clippy clean deny audit fuzz bench .PHONY: ebpf ebpf-clean .PHONY: docker docker-build docker-up docker-down docker-logs .PHONY: ci ci-full repo-gates @@ -78,6 +78,9 @@ docker-logs: checkstyle: fmt-check clippy @echo "✓ Checkstyle passed (rustfmt + clippy)" +bench: + $(CARGO) bench --all-features --bench hot_paths + # Parser fuzzing (nightly + cargo-fuzz). 60 s per target is a smoke run; # longer campaigns are run locally before a release. fuzz: diff --git a/benches/hot_paths.rs b/benches/hot_paths.rs new file mode 100644 index 0000000..bf17654 --- /dev/null +++ b/benches/hot_paths.rs @@ -0,0 +1,107 @@ +//! Micro-benchmarks for the per-connection hot path. +//! +//! Run with `cargo bench --all-features --bench hot_paths`. Baseline numbers +//! live in `docs/research/benchmark.md`; compare medians before and after a +//! change instead of absolute values, which depend on the machine. + +use std::net::{IpAddr, Ipv4Addr}; +use std::time::Duration; + +use criterion::{Criterion, black_box, criterion_group, criterion_main}; + +use rampart::filter::blacklist::Blacklist; +use rampart::filter::rate_limit::RateLimiter; +use rampart::protocol::http::parser::{Policy, evaluate}; +use rampart::traffic::prefix::{PrefixSnapshot, SubnetDetector}; + +fn addr(last: u8) -> IpAddr { + IpAddr::V4(Ipv4Addr::new(198, 51, 100, last)) +} + +/// Token-bucket admission, the first filter on every accepted connection. +fn bench_rate_limiter(c: &mut Criterion) { + c.bench_function("rate_limiter/check", |b| { + let limiter = RateLimiter::new(1_000_000.0, 1_000_000.0); + let mut n = 0u32; + b.iter(|| { + n = n.wrapping_add(1); + black_box(limiter.check(addr((n % 200) as u8))); + }); + }); +} + +/// Exact-IP and CIDR lookups of the ban tables (both on the accept path). +fn bench_blacklist(c: &mut Criterion) { + let blacklist = Blacklist::new(); + for octet in 1..=200u8 { + blacklist.add(addr(octet), Duration::from_secs(60), "bench"); + } + blacklist.add_cidr( + IpAddr::V4(Ipv4Addr::new(203, 0, 113, 0)), + 24, + Duration::from_secs(60), + "bench", + ); + + c.bench_function("blacklist/is_blocked_exact", |b| { + let mut n = 0u32; + b.iter(|| { + n = n.wrapping_add(1); + black_box(blacklist.is_blocked(addr((n % 200) as u8 + 1))); + }); + }); + + c.bench_function("blacklist/is_blocked_cidr", |b| { + b.iter(|| black_box(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 42))))); + }); + + c.bench_function("blacklist/is_blocked_miss", |b| { + b.iter(|| black_box(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1))))); + }); +} + +/// Subnet detector over a full window (called once per monitor tick). +fn bench_detector(c: &mut Criterion) { + let snapshots: Vec<(IpAddr, PrefixSnapshot)> = (0..256u32) + .map(|i| { + let prefix = IpAddr::V4(Ipv4Addr::new(10, (i / 256) as u8, (i % 256) as u8, 0)); + ( + prefix, + PrefixSnapshot { + syn_count: 120, + pkt_count: 400, + last_seen_ns: 0, + unique_sources: 40, + }, + ) + }) + .collect(); + let detector = SubnetDetector::new(100, 16); + + c.bench_function("subnet_detector/evaluate_256", |b| { + b.iter(|| black_box(detector.evaluate(black_box(&snapshots)))); + }); +} + +/// HTTP request-head validation for every new HTTP connection. +fn bench_http_parser(c: &mut Criterion) { + let request = b"GET /api/v1/status HTTP/1.1\r\nHost: edge.example\r\nUser-Agent: bench/1.0\r\nAccept: */*\r\n\r\n"; + let policy = Policy { + max_header_bytes: 8192, + blocked_paths: vec!["/admin".to_string()], + require_user_agent: true, + }; + + c.bench_function("http_parser/evaluate", |b| { + b.iter(|| black_box(evaluate(black_box(request), &policy))); + }); +} + +criterion_group!( + hot_paths, + bench_rate_limiter, + bench_blacklist, + bench_detector, + bench_http_parser +); +criterion_main!(hot_paths); diff --git a/docs/testing.md b/docs/testing.md index 0388d8b..2a7582f 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -91,7 +91,30 @@ cargo +nightly fuzz run proxy_v2 -- -max_total_time=600 --- -## 3. Локальный integration-стенд (Docker) +## 4. Бенчмарки горячего пути + +Микробенчмарки (criterion) для того, что выполняется на каждое соединение: + +```bash +cargo bench --all-features --bench hot_paths +# HTML-отчёт: target/criterion/report/index.html +``` + +Замеряются: `RateLimiter::check`, exact/CIDR/miss lookup в `Blacklist`, `SubnetDetector::evaluate` +на окне из 256 префиксов и разбор HTTP request-head. Сравнивай медианы до/после изменения на +одной машине; абсолютные числа зависят от CPU. Порядок величин на dev-машине (2 vCPU): + +| Бенчмарк | Медиана | +|----------|---------| +| `rate_limiter/check` | ~110 ns | +| `blacklist/is_blocked_exact` | ~70 ns | +| `blacklist/is_blocked_cidr` | ~220 ns | +| `subnet_detector/evaluate_256` | ~2.6 µs | +| `http_parser/evaluate` | ~280 ns | + +--- + +## 5. Локальный integration-стенд (Docker) ```bash bash deploy/test/run_test.sh @@ -102,7 +125,7 @@ attacker-контейнер. Сценарии — в [deploy/test/README.md](../ --- -## 4. Нагрузочное тестирование +## 6. Нагрузочное тестирование ### Много-IP TCP flood на VDS (edge-only) @@ -130,7 +153,7 @@ hping3 -S --flood -p 25565 --rand-source EDGE_IP # только на свои --- -## 5. Метрики для проверки во время теста +## 7. Метрики для проверки во время теста ```bash curl -s http://localhost:9090/metrics | grep rampart_ From bcae8901c68ea04d53abe5e17970d04c4cc90d68 Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Mon, 16 Nov 2026 13:03:50 +0100 Subject: [PATCH 2/4] fix: never ban a trusted proxy network from any ban source The gateway already refused to ban addresses from proxy_protocol.trusted_cidrs, but the traffic-intelligence auto-ban and the subnet monitor still did, so a trusted load balancer living inside a banned /24 (or below the reputation threshold) could be knocked out together with its clients. - TrustedProxyCidr moved to src/config/trusted.rs (the edge section file hit the 250-line gate) and gained `overlaps_prefix`: a ban is suppressed when the banned prefix intersects a trusted network. - TrafficHook::with_trusted suppresses reputation and offender bans; its ban() now reports whether the ban was installed so `ban_offenders` counts real bans. - The subnet monitor takes a MonitorSetup bundle (spawn had 8 arguments) and suppresses both userspace CIDR bans and XDP prefix bans that overlap a trusted network, logging a warning instead. - IPv4-mapped addresses are normalised in `contains` too. - Tests: overlap maths (v4/v6, /0 rejected), tracker suppression vs unrelated networks, and auto-ban skipping a trusted range while still banning public offenders. --- TODO.md | 7 ++- src/app/runtime.rs | 28 +++++---- src/config/mod.rs | 5 +- src/config/sections/edge.rs | 63 +------------------- src/config/sections/mod.rs | 4 +- src/config/trusted.rs | 108 +++++++++++++++++++++++++++++++++++ src/engine/subnet/monitor.rs | 81 ++++++++++++++++++++------ src/traffic/hook.rs | 28 +++++++-- tests/proxy_identity.rs | 22 +++++++ tests/subnet_detection.rs | 30 ++++++++-- tests/traffic_intel.rs | 30 ++++++++++ 11 files changed, 300 insertions(+), 106 deletions(-) create mode 100644 src/config/trusted.rs diff --git a/TODO.md b/TODO.md index a8d7ea9..7fe63b7 100644 --- a/TODO.md +++ b/TODO.md @@ -287,9 +287,10 @@ module-size, secrets, `cargo deny`, rustdoc без warning'ов, clang+verifier, ### Осталось -- [ ] Monitor (`add_cidr`) и `traffic/hook` autoban не сверяются с `trusted_cidrs`: если - доверенный прокси физически живёт в банимой подсети, его адрес может попасть под - префиксный бан. Нужна общая проверка перед любым баном. +- [x] Monitor (`add_cidr`) и `traffic/hook` autoban сверяются с `trusted_cidrs`: бан + подавляется, если префикс пересекается с доверенной сетью (`TrustedProxyCidr::overlaps_prefix`), + о чём пишется warn. Тесты: `tests/subnet_detection.rs`, `tests/traffic_intel.rs`, + `tests/proxy_identity.rs`. ### Сделано в этом раунде diff --git a/src/app/runtime.rs b/src/app/runtime.rs index 91142d2..675be95 100644 --- a/src/app/runtime.rs +++ b/src/app/runtime.rs @@ -64,12 +64,15 @@ pub async fn run() -> anyhow::Result { let blacklist = Arc::new(Blacklist::new()); let reputation = Arc::new(IpReputation::new()); let detector = Arc::new(Mutex::new(AttackDetector::new())); - let hook = Arc::new(TrafficHook::new( - reputation.clone(), - blacklist.clone(), - config.detect.autoban.clone(), - config.ban.ban_duration_secs, - )); + let hook = Arc::new( + TrafficHook::new( + reputation.clone(), + blacklist.clone(), + config.detect.autoban.clone(), + config.ban.ban_duration_secs, + ) + .with_trusted(config.proxy_protocol.trusted_cidrs.clone()), + ); let alert_dispatcher = AlertDispatcher::new(); let webhook_url = config.detect.alert.webhook_url.clone(); let allowed_1s = Arc::new(AtomicU64::new(0)); @@ -132,11 +135,14 @@ pub async fn run() -> anyhow::Result { let subnet_tracker = start_subnet_tracker(&config); monitor::spawn( &config.detect.prefix, - Duration::from_secs(config.ban.ban_duration_secs), - blacklist.clone(), - !config.proxy_protocol.ingress_enabled, - subnet_tracker.clone(), - xdp_filter.clone(), + monitor::MonitorSetup { + ban_ttl: Duration::from_secs(config.ban.ban_duration_secs), + blacklist: blacklist.clone(), + trusted_peer: !config.proxy_protocol.ingress_enabled, + trusted_networks: config.proxy_protocol.trusted_cidrs.clone(), + tracker: subnet_tracker.clone(), + xdp: xdp_filter.clone(), + }, shutdown_rx.clone(), ); diff --git a/src/config/mod.rs b/src/config/mod.rs index 6f9996e..727d0e4 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -3,6 +3,7 @@ //! Owns the [`Config`] root type, its serde defaults and the semantic validation //! that runs before the daemon binds a socket. mod sections; +mod trusted; #[cfg(feature = "protocol-http")] pub use sections::HttpProtocolConfig; @@ -12,9 +13,9 @@ pub use sections::MinecraftProtocolConfig; pub use sections::ProtocolConfig; pub use sections::{ BackendConfig, BanConfig, BindConfig, DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig, - LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, ProxyProtocolConfig, StoreConfig, TrustedProxyCidr, - WorkerConfig, XdpConfig, + LimitsConfig, LoggingConfig, MetricsConfig, PowConfig, ProxyProtocolConfig, StoreConfig, WorkerConfig, XdpConfig, }; +pub use trusted::TrustedProxyCidr; use serde::Deserialize; use std::fs; diff --git a/src/config/sections/edge.rs b/src/config/sections/edge.rs index 79541b5..c4c7163 100644 --- a/src/config/sections/edge.rs +++ b/src/config/sections/edge.rs @@ -1,8 +1,7 @@ //! Edge sections: bind, backend, workers, limits, ban, PoW and PROXY protocol. +use crate::config::trusted::TrustedProxyCidr; use serde::{Deserialize, Deserializer}; -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; -use std::str::FromStr; /// `[bind]` section: listen address and port of the edge node. #[derive(Debug, Clone, Deserialize)] @@ -159,66 +158,6 @@ fn default_pow_difficulty() -> u8 { 4 } -/// CIDR network allowed to send inbound PROXY protocol headers. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct TrustedProxyCidr { - network: IpAddr, - prefix_len: u8, -} - -impl TrustedProxyCidr { - /// Returns whether `ip` belongs to this network. - #[must_use] - pub fn contains(&self, ip: IpAddr) -> bool { - // Masks are family-specific: a prefix above 32 would underflow the v4 shift. - let prefix = self.prefix_len; - match (self.network, ip) { - (IpAddr::V4(net), IpAddr::V4(ip)) => u32::from(net) & mask_v4(prefix) == u32::from(ip) & mask_v4(prefix), - (IpAddr::V6(net), IpAddr::V6(ip)) => u128::from(net) & mask_v6(prefix) == u128::from(ip) & mask_v6(prefix), - _ => false, - } - } -} - -impl FromStr for TrustedProxyCidr { - type Err = String; - - fn from_str(value: &str) -> Result { - let (address, prefix) = value.split_once('/').map_or((value, None), |(a, p)| (a, Some(p))); - let network = IpAddr::from_str(address).map_err(|_| format!("invalid trusted proxy CIDR: {value}"))?; - let max_prefix = if network.is_ipv4() { 32 } else { 128 }; - let prefix_len = prefix - .map_or(Ok(max_prefix), |prefix| prefix.parse::()) - .map_err(|_| format!("invalid trusted proxy CIDR prefix: {value}"))?; - if prefix_len > max_prefix { - return Err(format!("trusted proxy CIDR prefix is too large: {value}")); - } - let network = match network { - IpAddr::V4(ip) => IpAddr::V4(Ipv4Addr::from(u32::from(ip) & mask_v4(prefix_len))), - IpAddr::V6(ip) => IpAddr::V6(Ipv6Addr::from(u128::from(ip) & mask_v6(prefix_len))), - }; - Ok(Self { network, prefix_len }) - } -} - -impl<'de> Deserialize<'de> for TrustedProxyCidr { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let value = String::deserialize(deserializer)?; - value.parse().map_err(serde::de::Error::custom) - } -} - -fn mask_v4(prefix: u8) -> u32 { - if prefix == 0 { 0 } else { u32::MAX << (32 - prefix) } -} - -fn mask_v6(prefix: u8) -> u128 { - if prefix == 0 { 0 } else { u128::MAX << (128 - prefix) } -} - /// `[proxy_protocol]` section: inbound PROXY protocol ingestion. #[derive(Debug, Clone, Deserialize)] pub struct ProxyProtocolConfig { diff --git a/src/config/sections/mod.rs b/src/config/sections/mod.rs index ffb8121..142cf8e 100644 --- a/src/config/sections/mod.rs +++ b/src/config/sections/mod.rs @@ -11,7 +11,5 @@ pub use detect::MinecraftProtocolConfig; #[cfg(any(feature = "protocol-http", feature = "protocol-minecraft"))] pub use detect::ProtocolConfig; pub use detect::{DetectAlertConfig, DetectAutobanConfig, DetectConfig, DetectPrefixConfig}; -pub use edge::{ - BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, ProxyProtocolConfig, TrustedProxyCidr, WorkerConfig, -}; +pub use edge::{BackendConfig, BanConfig, BindConfig, LimitsConfig, PowConfig, ProxyProtocolConfig, WorkerConfig}; pub use platform::{LoggingConfig, MetricsConfig, StoreConfig, XdpConfig}; diff --git a/src/config/trusted.rs b/src/config/trusted.rs new file mode 100644 index 0000000..06dc4c7 --- /dev/null +++ b/src/config/trusted.rs @@ -0,0 +1,108 @@ +//! Trusted PROXY-protocol networks and CIDR helpers shared by the edge paths. + +use serde::{Deserialize, Deserializer}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; +use std::str::FromStr; + +/// CIDR network allowed to send inbound PROXY protocol headers. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TrustedProxyCidr { + network: IpAddr, + prefix_len: u8, +} + +impl TrustedProxyCidr { + /// Returns whether `ip` belongs to this network. + #[must_use] + pub fn contains(&self, ip: IpAddr) -> bool { + // Masks are family-specific: a prefix above 32 would underflow the v4 shift. + let prefix = self.prefix_len; + match (self.network, normalize(ip)) { + (IpAddr::V4(net), IpAddr::V4(ip)) => u32::from(net) & mask_v4(prefix) == u32::from(ip) & mask_v4(prefix), + (IpAddr::V6(net), IpAddr::V6(ip)) => u128::from(net) & mask_v6(prefix) == u128::from(ip) & mask_v6(prefix), + _ => false, + } + } + + /// Returns whether `ip/prefix_len` intersects this network. + /// + /// Used before banning a prefix: a ban covering a trusted proxy address + /// would punish the proxy and every client behind it. + #[must_use] + pub fn overlaps_prefix(&self, ip: IpAddr, prefix_len: u8) -> bool { + match (self.network, normalize(ip)) { + (IpAddr::V4(net), IpAddr::V4(other)) => { + if prefix_len == 0 || prefix_len > 32 { + return false; + } + let net_mask = mask_v4(self.prefix_len); + let other_mask = mask_v4(prefix_len); + let net_start = u32::from(net) & net_mask; + let net_end = net_start | !net_mask; + let other_start = u32::from(other) & other_mask; + let other_end = other_start | !other_mask; + net_start <= other_end && other_start <= net_end + }, + (IpAddr::V6(net), IpAddr::V6(other)) => { + if prefix_len == 0 || prefix_len > 128 { + return false; + } + let net_mask = mask_v6(self.prefix_len); + let other_mask = mask_v6(prefix_len); + let net_start = u128::from(net) & net_mask; + let net_end = net_start | !net_mask; + let other_start = u128::from(other) & other_mask; + let other_end = other_start | !other_mask; + net_start <= other_end && other_start <= net_end + }, + _ => false, + } + } +} + +impl FromStr for TrustedProxyCidr { + type Err = String; + + fn from_str(value: &str) -> Result { + let (address, prefix) = value.split_once('/').map_or((value, None), |(a, p)| (a, Some(p))); + let network = IpAddr::from_str(address).map_err(|_| format!("invalid trusted proxy CIDR: {value}"))?; + let max_prefix = if network.is_ipv4() { 32 } else { 128 }; + let prefix_len = prefix + .map_or(Ok(max_prefix), |prefix| prefix.parse::()) + .map_err(|_| format!("invalid trusted proxy CIDR prefix: {value}"))?; + if prefix_len > max_prefix { + return Err(format!("trusted proxy CIDR prefix is too large: {value}")); + } + let network = match network { + IpAddr::V4(ip) => IpAddr::V4(Ipv4Addr::from(u32::from(ip) & mask_v4(prefix_len))), + IpAddr::V6(ip) => IpAddr::V6(Ipv6Addr::from(u128::from(ip) & mask_v6(prefix_len))), + }; + Ok(Self { network, prefix_len }) + } +} + +impl<'de> Deserialize<'de> for TrustedProxyCidr { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let value = String::deserialize(deserializer)?; + value.parse().map_err(serde::de::Error::custom) + } +} + +/// Collapses IPv4-mapped IPv6 addresses to IPv4. +fn normalize(ip: IpAddr) -> IpAddr { + match ip { + IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(ip, IpAddr::V4), + IpAddr::V4(_) => ip, + } +} + +fn mask_v4(prefix: u8) -> u32 { + if prefix == 0 { 0 } else { u32::MAX << (32 - prefix) } +} + +fn mask_v6(prefix: u8) -> u128 { + if prefix == 0 { 0 } else { u128::MAX << (128 - prefix) } +} diff --git a/src/engine/subnet/monitor.rs b/src/engine/subnet/monitor.rs index e8a822a..3a55e58 100644 --- a/src/engine/subnet/monitor.rs +++ b/src/engine/subnet/monitor.rs @@ -38,6 +38,9 @@ struct BanPolicy { blacklist: Arc, ttl: Duration, trusted: bool, + /// Trusted PROXY-protocol networks; a prefix ban overlapping one of them is + /// suppressed, because it would punish the proxy and every client behind it. + trusted_networks: Vec, } /// Spawns the background subnet monitor when `[detect.prefix] enabled`. @@ -46,22 +49,37 @@ struct BanPolicy { /// aggregator. Without either source the monitor stays disabled and logs a /// warning. /// -/// `ban_ttl` comes from `[ban].ban_duration_secs`. `trusted_peer` is false when -/// the observed address comes from an unverified ingress path (inbound PROXY -/// protocol), where banning it could punish the transport proxy instead of the -/// real client. -pub fn spawn( - detect: &DetectPrefixConfig, - ban_ttl: Duration, - blacklist: Arc, - trusted_peer: bool, - tracker: Option>, - xdp: Option>>, - mut shutdown: watch::Receiver, -) { +/// Everything the monitor task needs besides the detection config. +pub struct MonitorSetup { + /// Ban lifetime, from `[ban].ban_duration_secs`. + pub ban_ttl: Duration, + /// Shared ban table. + pub blacklist: Arc, + /// False when the observed address comes from an unverified ingress path + /// (inbound PROXY protocol), where banning it could punish the transport + /// proxy instead of the real client. + pub trusted_peer: bool, + /// Trusted proxy networks; a prefix ban overlapping one is suppressed. + pub trusted_networks: Vec, + /// Userspace aggregator, used when XDP is not available. + pub tracker: Option>, + /// Loaded XDP filter, preferred over the userspace tracker when present. + pub xdp: Option>>, +} + +/// Spawns the background subnet monitor when `[detect.prefix] enabled`. +pub fn spawn(detect: &DetectPrefixConfig, setup: MonitorSetup, mut shutdown: watch::Receiver) { if !detect.enabled { return; } + let MonitorSetup { + ban_ttl, + blacklist, + trusted_peer, + trusted_networks, + tracker, + xdp, + } = setup; let Some(source) = build_source(tracker, xdp) else { tracing::warn!("detect.prefix enabled but no stats source available; monitor disabled"); return; @@ -72,6 +90,7 @@ pub fn spawn( blacklist, ttl: ban_ttl, trusted: trusted_peer, + trusted_networks, }; tokio::spawn(async move { @@ -124,7 +143,7 @@ fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolic } match source { #[cfg(feature = "xdp")] - Source::Xdp(xdp) if policy.trusted => ban_xdp_verdicts(xdp, verdicts, policy.ttl), + Source::Xdp(xdp) if policy.trusted => ban_xdp_verdicts(xdp, verdicts, policy.ttl, &policy.trusted_networks), #[cfg(feature = "xdp")] Source::Xdp(_) => { if verdicts.iter().any(|pv| pv.verdict == SubnetVerdict::Block) { @@ -132,7 +151,13 @@ fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolic } }, Source::Tracker(_) => { - enforce_tracker_verdicts(verdicts, &policy.blacklist, policy.ttl, policy.trusted); + enforce_tracker_verdicts( + verdicts, + &policy.blacklist, + policy.ttl, + policy.trusted, + &policy.trusted_networks, + ); }, } } @@ -150,6 +175,7 @@ pub fn enforce_tracker_verdicts( blacklist: &Blacklist, ttl: Duration, trusted: bool, + trusted_networks: &[crate::config::TrustedProxyCidr], ) -> usize { let mut banned = 0; for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) { @@ -160,6 +186,16 @@ pub fn enforce_tracker_verdicts( ); continue; } + if trusted_networks + .iter() + .any(|net| net.overlaps_prefix(pv.prefix, prefix_len(pv.prefix))) + { + tracing::warn!( + prefix = %pv.prefix, + "subnet ban suppressed: the prefix contains a trusted proxy network" + ); + continue; + } blacklist.add_cidr(pv.prefix, prefix_len(pv.prefix), ttl, "subnet_attack"); banned += 1; tracing::warn!( @@ -172,8 +208,21 @@ pub fn enforce_tracker_verdicts( } #[cfg(feature = "xdp")] -fn ban_xdp_verdicts(xdp: &XdpPrefixStats, verdicts: &[PrefixVerdict], ttl: Duration) { +fn ban_xdp_verdicts( + xdp: &XdpPrefixStats, + verdicts: &[PrefixVerdict], + ttl: Duration, + trusted_networks: &[crate::config::TrustedProxyCidr], +) { for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) { + let len = prefix_len(pv.prefix); + if trusted_networks.iter().any(|net| net.overlaps_prefix(pv.prefix, len)) { + tracing::warn!( + prefix = %pv.prefix, + "subnet kernel ban suppressed: the prefix contains a trusted proxy network" + ); + continue; + } let result = { let guard = xdp.filter_lock(); guard.ban_cidr(pv.prefix, prefix_len(pv.prefix), ttl.as_secs()) diff --git a/src/traffic/hook.rs b/src/traffic/hook.rs index 356f250..850e909 100644 --- a/src/traffic/hook.rs +++ b/src/traffic/hook.rs @@ -25,6 +25,8 @@ pub struct TrafficHook { autoban: DetectAutobanConfig, ban_ttl_secs: u64, conn_window: AtomicU64, + /// Trusted PROXY-protocol networks; addresses inside them are never banned. + trusted: Vec, } impl TrafficHook { @@ -40,9 +42,17 @@ impl TrafficHook { autoban, ban_ttl_secs, conn_window: AtomicU64::new(0), + trusted: Vec::new(), } } + /// Marks the networks whose addresses must never be banned (trusted proxies). + #[must_use] + pub fn with_trusted(mut self, trusted: Vec) -> Self { + self.trusted = trusted; + self + } + /// A new connection was accepted by the accept loop. /// /// Only bumps the cps window counter; the counter is drained by @@ -76,8 +86,9 @@ impl TrafficHook { if self.blacklist.is_blocked(ip) { continue; } - self.ban(ip, BAN_REASON_ATTACK); - banned += 1; + if self.ban(ip, BAN_REASON_ATTACK) { + banned += 1; + } } banned } @@ -91,12 +102,21 @@ impl TrafficHook { if self.reputation.score(ip) > self.autoban.reputation_threshold { return; } - self.ban(ip, reason); + let _ = self.ban(ip, reason); } - fn ban(&self, ip: IpAddr, reason: &str) { + /// Bans `ip` unless it belongs to a trusted proxy network. + /// + /// Returns whether the ban was actually installed. + fn ban(&self, ip: IpAddr, reason: &str) -> bool { + let host_prefix = if ip.is_ipv4() { 32 } else { 128 }; + if self.trusted.iter().any(|net| net.overlaps_prefix(ip, host_prefix)) { + tracing::warn!(%ip, reason, "auto-ban suppressed: address belongs to a trusted proxy network"); + return false; + } self.blacklist.add(ip, Duration::from_secs(self.ban_ttl_secs), reason); metrics::AUTO_BANS_TOTAL.with_label_values(&[reason]).inc(); tracing::warn!(%ip, reason, ttl_secs = self.ban_ttl_secs, "auto-ban issued by traffic intelligence"); + true } } diff --git a/tests/proxy_identity.rs b/tests/proxy_identity.rs index bb441fb..731cdd8 100644 --- a/tests/proxy_identity.rs +++ b/tests/proxy_identity.rs @@ -237,3 +237,25 @@ async fn plain_connection_uses_the_transport_peer() { let outcome = handle_plain(&gateway, peer).await; assert_eq!(outcome.client_ip, Some(peer.ip())); } + +#[test] +fn trusted_network_overlap_checks_prefix_intersection() { + let net: TrustedProxyCidr = "10.0.0.0/8".parse().expect("cidr"); + assert!(net.overlaps_prefix("10.1.2.0".parse().expect("ip"), 24), "same range"); + assert!( + net.overlaps_prefix("10.0.0.0".parse().expect("ip"), 7), + "wider prefix overlaps" + ); + assert!( + !net.overlaps_prefix("11.0.0.0".parse().expect("ip"), 8), + "sibling range" + ); + assert!( + !net.overlaps_prefix("10.0.0.0".parse().expect("ip"), 0), + "/0 is rejected" + ); + + let v6: TrustedProxyCidr = "fd00::/8".parse().expect("cidr"); + assert!(v6.overlaps_prefix("fd12::".parse().expect("ip"), 16)); + assert!(!v6.overlaps_prefix("2001:db8::".parse().expect("ip"), 32)); +} diff --git a/tests/subnet_detection.rs b/tests/subnet_detection.rs index 8301c27..cf33eaa 100644 --- a/tests/subnet_detection.rs +++ b/tests/subnet_detection.rs @@ -1,6 +1,6 @@ //! Integration tests for subnet-level detection: tracker, detector and verdicts. -use rampart::config::Config; +use rampart::config::{Config, TrustedProxyCidr}; use rampart::engine::subnet::monitor::enforce_tracker_verdicts; use rampart::engine::subnet_tracker::SubnetTracker; use rampart::filter::blacklist::Blacklist; @@ -299,7 +299,7 @@ fn block_verdict(prefix: IpAddr) -> PrefixVerdict { fn trusted_tracker_block_reaches_the_blacklist_with_the_configured_ttl() { let blacklist = Blacklist::new(); let verdicts = [block_verdict(v4([203, 0, 113, 0]))]; - let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true); + let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &[]); assert_eq!(banned, 1); assert!(blacklist.is_blocked(v4([203, 0, 113, 0]))); } @@ -308,7 +308,7 @@ fn trusted_tracker_block_reaches_the_blacklist_with_the_configured_ttl() { fn untrusted_tracker_block_only_warns() { let blacklist = Blacklist::new(); let verdicts = [block_verdict(v4([198, 51, 100, 0]))]; - let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), false); + let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), false, &[]); assert_eq!(banned, 0); assert!(blacklist.is_empty()); } @@ -317,7 +317,7 @@ fn untrusted_tracker_block_only_warns() { fn tracker_ban_uses_the_given_duration() { let blacklist = Blacklist::new(); let verdicts = [block_verdict(v4([192, 0, 2, 0]))]; - enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_millis(1), true); + enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_millis(1), true, &[]); std::thread::sleep(Duration::from_millis(2)); assert!( !blacklist.is_blocked(v4([192, 0, 2, 0])), @@ -331,7 +331,7 @@ fn non_block_verdicts_do_not_ban() { let mut verdict = block_verdict(v4([203, 0, 113, 0])); verdict.verdict = SubnetVerdict::Challenge; assert_eq!( - enforce_tracker_verdicts(&[verdict], &blacklist, Duration::from_secs(60), true), + enforce_tracker_verdicts(&[verdict], &blacklist, Duration::from_secs(60), true, &[]), 0 ); assert!(blacklist.is_empty()); @@ -375,3 +375,23 @@ fn prefix_key_normalises_ipv4_mapped_addresses() { assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(prefix_of(mapped))); assert_eq!(PrefixKey::new(mapped).to_prefix(), Some(v4([1, 2, 3, 0]))); } + +#[test] +fn tracker_block_is_suppressed_when_a_trusted_proxy_lives_in_the_prefix() { + let blacklist = Blacklist::new(); + let verdicts = [block_verdict(v4([10, 9, 0, 0]))]; + let trusted: Vec = vec!["10.9.0.5/32".parse().expect("trusted cidr")]; + let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &trusted); + assert_eq!(banned, 0, "a ban covering a trusted proxy must be suppressed"); + assert!(blacklist.is_empty()); +} + +#[test] +fn tracker_block_still_applies_when_trusted_networks_do_not_overlap() { + let blacklist = Blacklist::new(); + let verdicts = [block_verdict(v4([10, 9, 0, 0]))]; + let trusted: Vec = vec!["192.0.2.0/24".parse().expect("trusted cidr")]; + let banned = enforce_tracker_verdicts(&verdicts, &blacklist, Duration::from_secs(120), true, &trusted); + assert_eq!(banned, 1); + assert!(blacklist.is_blocked(v4([10, 9, 0, 7]))); +} diff --git a/tests/traffic_intel.rs b/tests/traffic_intel.rs index 7f73248..22d30d9 100644 --- a/tests/traffic_intel.rs +++ b/tests/traffic_intel.rs @@ -446,3 +446,33 @@ fn alerts_fire_only_on_state_transitions() { assert!(alerts[0].message.contains("attack started")); assert!(alerts[1].message.contains("attack ended")); } + +#[test] +fn auto_ban_never_targets_a_trusted_proxy_network() { + let reputation = Arc::new(IpReputation::new()); + let blacklist = Arc::new(Blacklist::new()); + let trusted: Vec = vec!["10.0.0.0/8".parse().expect("trusted cidr")]; + let hook = + TrafficHook::new(Arc::clone(&reputation), Arc::clone(&blacklist), autoban_cfg(true), 60).with_trusted(trusted); + let inside = ip([10, 1, 2, 3]); + + for _ in 0..6 { + reputation.record_bad(inside); + hook.on_connection_end(inside); + } + assert!( + reputation.score(inside) <= -50, + "reputation must fall below the threshold" + ); + assert_eq!(hook.ban_offenders(), 0, "trusted addresses are never banned"); + + let public = ip([198, 51, 100, 9]); + for _ in 0..6 { + reputation.record_bad(public); + hook.on_connection_end(public); + } + assert!( + blacklist.is_blocked(public), + "non-trusted offenders are still banned by the reputation path" + ); +} From ca7ce8c58e861d76b30eff2b53c2e256cfdb24fd Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Mon, 16 Nov 2026 13:08:34 +0100 Subject: [PATCH 3/4] test(edge): end-to-end connection path over loopback Drive a real TCP client through engine::listener and Gateway to a real upstream on 127.0.0.1, covering plain HTTP proxying, the PoW gate (coalesced nonce + request, wrong nonce dropped), the per-IP rate limit and the blacklist. Ports are ephemeral, every wait is bounded, and the accept loop is always stopped and awaited. --- tests/edge_e2e.rs | 153 +++++++++++++++++++++++ tests/edge_e2e/support.rs | 250 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 403 insertions(+) create mode 100644 tests/edge_e2e.rs create mode 100644 tests/edge_e2e/support.rs diff --git a/tests/edge_e2e.rs b/tests/edge_e2e.rs new file mode 100644 index 0000000..e7d59f1 --- /dev/null +++ b/tests/edge_e2e.rs @@ -0,0 +1,153 @@ +//! End-to-end tests of the real edge connection path over loopback. +//! +//! A TCP client is driven through the production accept loop (`engine::listener`) +//! and pipeline (`engine::tunnel::Gateway`) to a real upstream socket on +//! `127.0.0.1`: framing, the PoW gate, the admission filters and the relay are +//! exercised as one system, without root and without any external network. +#![cfg(feature = "protocol-http")] + +#[path = "edge_e2e/support.rs"] +mod support; + +use rampart::engine::challenge::solve; +use sha2::{Digest, Sha256}; +use std::net::Ipv4Addr; +use std::sync::atomic::Ordering; +use std::time::Duration; +use support::{ + IO_BUDGET, POW_DIFFICULTY, REQUEST, RESPONSE, exchange, read_to_close, rejected_reply, start_edge, start_upstream, +}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpStream; + +/// Reads one newline-terminated line (the PoW challenge). +async fn read_line(stream: &mut TcpStream) -> anyhow::Result { + let mut line = Vec::new(); + for _ in 0..128 { + let mut byte = [0u8; 1]; + if stream.read(&mut byte).await? == 0 || byte[0] == b'\n' { + break; + } + line.push(byte[0]); + } + Ok(String::from_utf8_lossy(&line).trim().to_string()) +} + +fn hash_satisfies(challenge: &str, nonce: &str, difficulty: u8) -> bool { + let digest = Sha256::digest(format!("{challenge}{nonce}").as_bytes()); + hex::encode(digest) + .as_bytes() + .iter() + .take(difficulty as usize) + .all(|byte| byte.is_ascii_digit() && *byte <= b'3') +} + +/// Finds a nonce the gate must reject; the caller re-checks the result. +fn invalid_nonce(challenge: &str, difficulty: u8) -> String { + // At difficulty 2 about fifteen of sixteen candidates fail, so this stops + // within a handful of iterations. + for candidate in 0u64..1_000_000 { + let nonce = candidate.to_string(); + if !hash_satisfies(challenge, &nonce, difficulty) { + return nonce; + } + } + "0".to_string() +} + +#[tokio::test] +async fn plain_http_request_is_proxied_to_the_upstream() -> anyhow::Result<()> { + let (upstream, accepted, seen) = start_upstream().await; + let edge = start_edge(upstream, |_| {}).await?; + + let response = exchange(edge.addr, REQUEST).await?; + assert_eq!(response.as_slice(), RESPONSE); + assert_eq!(accepted.load(Ordering::SeqCst), 1); + assert_eq!(*seen.lock().expect("requests lock"), vec![REQUEST.to_vec()]); + + edge.stop().await +} + +#[tokio::test] +async fn pow_gate_accepts_a_solved_challenge_and_drops_a_wrong_nonce() -> anyhow::Result<()> { + let (upstream, accepted, seen) = start_upstream().await; + let edge = start_edge(upstream, |config| { + config.pow.enabled = true; + config.pow.difficulty = POW_DIFFICULTY; + }) + .await?; + + let mut client = TcpStream::connect(edge.addr).await?; + let challenge = tokio::time::timeout(IO_BUDGET, read_line(&mut client)).await??; + let nonce = solve(&challenge, POW_DIFFICULTY) + .ok_or_else(|| anyhow::anyhow!("no solution found for challenge {challenge}"))?; + // Coalesced write: the PoW reader must consume exactly the nonce line and + // leave the HTTP head buffered for the handshake that runs next. + let mut payload = format!("{nonce}\r\n").into_bytes(); + payload.extend_from_slice(REQUEST); + client.write_all(&payload).await?; + let response = tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await??; + assert_eq!(response.as_slice(), RESPONSE); + assert_eq!(accepted.load(Ordering::SeqCst), 1); + + let mut hostile = TcpStream::connect(edge.addr).await?; + let challenge = tokio::time::timeout(IO_BUDGET, read_line(&mut hostile)).await??; + let wrong = invalid_nonce(&challenge, POW_DIFFICULTY); + assert!(!hash_satisfies(&challenge, &wrong, POW_DIFFICULTY)); + // Send the request after the wrong nonce: if the gate accepted it, the head + // would be buffered and proxied, and the upstream response would come back. + let mut payload = format!("{wrong}\r\n").into_bytes(); + payload.extend_from_slice(REQUEST); + let _ = hostile.write_all(&payload).await; + let reply = tokio::time::timeout(IO_BUDGET, read_to_close(&mut hostile)).await??; + assert!(reply.is_empty(), "a wrong nonce must be dropped, got {reply:?}"); + assert_eq!( + accepted.load(Ordering::SeqCst), + 1, + "a failed PoW must not reach the upstream" + ); + assert_eq!(seen.lock().expect("requests lock").len(), 1); + + edge.stop().await +} + +#[tokio::test] +async fn rate_limit_drops_the_second_connection_before_the_upstream() -> anyhow::Result<()> { + let (upstream, accepted, _seen) = start_upstream().await; + let edge = start_edge(upstream, |config| { + // One token, practically no refill: the second connection is over budget. + config.limits.rate_limit_pps = 0.001; + config.limits.rate_limit_burst = 1.0; + }) + .await?; + + let response = exchange(edge.addr, REQUEST).await?; + assert_eq!(response.as_slice(), RESPONSE); + assert_eq!(accepted.load(Ordering::SeqCst), 1); + + let reply = rejected_reply(edge.addr, REQUEST).await?; + assert!( + reply.is_empty(), + "rate-limited connection must not be proxied, got {reply:?}" + ); + // EOF above means the reject path already ran; it precedes the upstream + // stage, so a second accept cannot appear afterwards. + assert_eq!(accepted.load(Ordering::SeqCst), 1); + + edge.stop().await +} + +#[tokio::test] +async fn blacklisted_client_never_reaches_the_upstream() -> anyhow::Result<()> { + let (upstream, accepted, seen) = start_upstream().await; + let edge = start_edge(upstream, |_| {}).await?; + edge.blacklist + .add(Ipv4Addr::LOCALHOST.into(), Duration::from_secs(60), "edge_e2e"); + + let reply = rejected_reply(edge.addr, REQUEST).await?; + assert!(reply.is_empty(), "a banned client must be dropped, got {reply:?}"); + assert_eq!(accepted.load(Ordering::SeqCst), 0); + assert!(seen.lock().expect("requests lock").is_empty()); + + edge.stop().await +} diff --git a/tests/edge_e2e/support.rs b/tests/edge_e2e/support.rs new file mode 100644 index 0000000..a907a59 --- /dev/null +++ b/tests/edge_e2e/support.rs @@ -0,0 +1,250 @@ +//! Shared loopback harness for `tests/edge_e2e.rs`; cargo does not build it as +//! its own test binary because it lives in a subdirectory. + +use rampart::config::Config; +use rampart::engine::challenge::DifficultyAdjuster; +use rampart::engine::listener; +use rampart::engine::tunnel::Gateway; +use rampart::filter::blacklist::Blacklist; +use rampart::filter::rate_limit::RateLimiter; +use rampart::protocol::ProtocolRegistry; +use rampart::protocol::http::HttpProtocolHandler; +use rampart::traffic::hook::TrafficHook; +use rampart::traffic::reputation::IpReputation; +use std::collections::HashSet; +use std::io::ErrorKind; +use std::net::{Ipv4Addr, SocketAddr}; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpStream; +use tokio::sync::watch; +use tokio::task::JoinHandle; + +pub const REQUEST: &[u8] = b"GET / HTTP/1.1\r\nHost: edge\r\n\r\n"; +pub const RESPONSE: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nOK"; +pub const POW_DIFFICULTY: u8 = 2; +/// Upper bound of every I/O wait, so a regression fails instead of hanging. +pub const IO_BUDGET: Duration = Duration::from_secs(4); +const READY_BUDGET: Duration = Duration::from_secs(2); +const MAX_REPLY_BYTES: usize = 64 * 1024; + +// --- upstream ------------------------------------------------------------- + +/// Real upstream: returns its address, an accept counter and the heads it saw. +pub async fn start_upstream() -> (SocketAddr, Arc, Arc>>>) { + let listener = tokio::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0)) + .await + .expect("bind upstream"); + let addr = listener.local_addr().expect("upstream address"); + let accepted = Arc::new(AtomicUsize::new(0)); + let requests = Arc::new(Mutex::new(Vec::new())); + let (count, seen) = (Arc::clone(&accepted), Arc::clone(&requests)); + // Detached: the per-test runtime drops this server when the test ends. + tokio::spawn(async move { + while let Ok((mut stream, _)) = listener.accept().await { + count.fetch_add(1, Ordering::SeqCst); + let seen = Arc::clone(&seen); + tokio::spawn(async move { + let head = read_head(&mut stream).await; + seen.lock().expect("requests lock").push(head); + let _ = stream.write_all(RESPONSE).await; + let _ = stream.shutdown().await; + }); + } + }); + (addr, accepted, requests) +} + +/// Reads until the request head ends (`\r\n\r\n`), the peer closes or a read fails. +async fn read_head(stream: &mut TcpStream) -> Vec { + let mut head = Vec::new(); + let mut buf = [0u8; 512]; + while head.len() < 4096 { + match stream.read(&mut buf).await { + Ok(0) | Err(_) => break, + Ok(n) => { + head.extend_from_slice(&buf[..n]); + if head.windows(4).any(|w| w == b"\r\n\r\n".as_slice()) { + break; + } + }, + } + } + head +} + +// --- edge ----------------------------------------------------------------- + +/// Production-shaped config from serde defaults, plus the per-test overrides. +fn test_config(port: u16, upstream: SocketAddr) -> Config { + let mut config = Config::default(); + config.bind.address = Ipv4Addr::LOCALHOST.to_string(); + config.bind.port = port; + config.backend.upstreams = vec![upstream.to_string()]; + config.workers.count = 1; + config.limits.handshake_timeout_secs = 2; + config.limits.relay_idle_timeout_secs = 5; + config.limits.max_connections_per_ip = 8; + config.protocol.http.max_header_bytes = 4096; + config.protocol.http.timeout_secs = 2; + config +} + +/// Running accept loop plus the shared state a test may pre-seed. +pub struct Edge { + pub addr: SocketAddr, + pub blacklist: Arc, + shutdown: watch::Sender, + task: JoinHandle>, +} + +impl Edge { + /// Stops the accept loop and waits for it, so no task outlives the test. + pub async fn stop(mut self) -> anyhow::Result<()> { + let _ = self.shutdown.send(true); + match tokio::time::timeout(IO_BUDGET, &mut self.task).await { + Ok(result) => result?, + Err(_) => anyhow::bail!("edge accept loop did not stop within {IO_BUDGET:?}"), + } + } +} + +impl Drop for Edge { + fn drop(&mut self) { + // Panic path: still wake the accept loop instead of leaking it. + let _ = self.shutdown.send(true); + } +} + +/// Starts `listener::run`, retrying once if the throwaway port was stolen. +/// +/// The port comes from binding `:0` and dropping the probe, so another process +/// can claim it before the edge binds; `run` then fails immediately and a fresh +/// port is used. The retry is bounded, never a loop. +pub async fn start_edge(upstream: SocketAddr, tune: impl Fn(&mut Config)) -> anyhow::Result { + let mut last_error = anyhow::anyhow!("edge listener did not bind"); + for _ in 0..2 { + let port = free_port()?; + let mut config = test_config(port, upstream); + tune(&mut config); + let config = Arc::new(config); + let (gateway, blacklist, hook) = build_gateway(&config); + let (shutdown, shutdown_rx) = watch::channel(false); + let task = tokio::spawn(listener::run(config, gateway, hook, shutdown_rx)); + let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); + if wait_until_bound(addr, &task).await { + return Ok(Edge { + addr, + blacklist, + shutdown, + task, + }); + } + let _ = shutdown.send(true); + if let Ok(Err(error)) = task.await { + last_error = error; + } + } + Err(last_error.context("edge listener failed to bind a loopback port after two attempts")) +} + +/// Binds `127.0.0.1:0`, reads the assigned port and releases it. +fn free_port() -> anyhow::Result { + let probe = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?; + let port = probe.local_addr()?.port(); + drop(probe); + Ok(port) +} + +/// Waits until the edge owns `addr`. A second bind without `SO_REUSEPORT` fails +/// with `EADDRINUSE` exactly then, so no throwaway connection spends a +/// rate-limit token and pollutes the assertions. +async fn wait_until_bound(addr: SocketAddr, task: &JoinHandle>) -> bool { + let deadline = Instant::now() + READY_BUDGET; + while Instant::now() < deadline { + if task.is_finished() { + return false; + } + if std::net::TcpListener::bind(addr).is_err() { + return true; + } + tokio::time::sleep(Duration::from_millis(2)).await; + } + false +} + +/// Assembles the edge state as `app::runtime` does, minus XDP, Redis and the +/// ClickHouse writer (all optional, all `None` here). +fn build_gateway(config: &Arc) -> (Arc, Arc, Arc) { + let blacklist = Arc::new(Blacklist::new()); + let reputation = Arc::new(IpReputation::new()); + let mut registry = ProtocolRegistry::new(); + registry.register(Box::new(HttpProtocolHandler::new( + &config.protocol.http, + &config.backend.upstreams, + ))); + let gateway = Gateway::new( + Arc::clone(config), + Arc::new(RateLimiter::new( + config.limits.rate_limit_pps, + config.limits.rate_limit_burst, + )), + Arc::clone(&blacklist), + Arc::new(Mutex::new(DifficultyAdjuster::with_floor(config.pow.difficulty))), + Arc::new(HashSet::new()), + Arc::clone(&reputation), + None, + None, + Arc::new(AtomicU64::new(0)), + Arc::new(registry), + None, + ); + // Auto-ban stays at its default (disabled), so a drop cannot leak a ban into + // a later assertion. + let hook = Arc::new(TrafficHook::new( + reputation, + Arc::clone(&blacklist), + config.detect.autoban.clone(), + config.ban.ban_duration_secs, + )); + (Arc::new(gateway), blacklist, hook) +} + +// --- client --------------------------------------------------------------- + +/// Reads until the peer closes; a reset also means "closed, no extra data". +pub async fn read_to_close(stream: &mut TcpStream) -> anyhow::Result> { + let mut out = Vec::new(); + let mut buf = [0u8; 512]; + loop { + match stream.read(&mut buf).await { + Ok(0) => return Ok(out), + Ok(n) => { + out.extend_from_slice(&buf[..n]); + if out.len() > MAX_REPLY_BYTES { + anyhow::bail!("peer kept sending past {MAX_REPLY_BYTES} bytes"); + } + }, + Err(error) if error.kind() == ErrorKind::ConnectionReset => return Ok(out), + Err(error) => return Err(error.into()), + } + } +} + +/// Sends `payload` on a fresh connection and returns everything until the close. +pub async fn exchange(addr: SocketAddr, payload: &[u8]) -> anyhow::Result> { + let mut client = TcpStream::connect(addr).await?; + client.write_all(payload).await?; + tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await? +} + +/// Best-effort request on a fresh connection: a rejected peer may already be +/// gone, so write errors are ignored. The reply is what the caller asserts on — +/// a gate that let the request through would return the upstream response. +pub async fn rejected_reply(addr: SocketAddr, payload: &[u8]) -> anyhow::Result> { + let mut client = TcpStream::connect(addr).await?; + let _ = client.write_all(payload).await; + tokio::time::timeout(IO_BUDGET, read_to_close(&mut client)).await? +} From 15f3fcb895c5be1e525da7f4093c8a00959e6b95 Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Mon, 16 Nov 2026 13:11:31 +0100 Subject: [PATCH 4/4] refactor(subnet): split verdict enforcement out of the monitor monitor.rs reached 257 lines after the trusted-network suppression (limit is 250). BanPolicy, enforce_tracker_verdicts, ban_xdp_verdicts and prefix_len move to src/engine/subnet/enforce.rs; the monitor keeps the task, source selection and verdict logging, re-exporting enforce_tracker_verdicts so existing paths stay valid. Also documents the new tests/edge_e2e.rs scenarios in docs/testing.md. --- docs/testing.md | 8 +++ src/engine/subnet/enforce.rs | 108 +++++++++++++++++++++++++++++++++++ src/engine/subnet/mod.rs | 1 + src/engine/subnet/monitor.rs | 105 ++-------------------------------- 4 files changed, 122 insertions(+), 100 deletions(-) create mode 100644 src/engine/subnet/enforce.rs diff --git a/docs/testing.md b/docs/testing.md index 2a7582f..5c3b685 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -31,6 +31,14 @@ cargo clippy --all-targets --all-features -- -D warnings cargo deny check ``` +E2E-сценарии живут в `tests/edge_e2e.rs`: реальный TCP-клиент через продакшн-accept-loop и +pipeline до upstream на loopback (обычный HTTP, PoW с решённым и неверным nonce, rate-limit, +blacklist). Root и внешняя сеть не нужны, каждый тест укладывается в 5 с: + +```bash +cargo test --all-features --test edge_e2e +``` + XDP: smoke-check компиляции BPF-программы: ```bash diff --git a/src/engine/subnet/enforce.rs b/src/engine/subnet/enforce.rs new file mode 100644 index 0000000..70632ef --- /dev/null +++ b/src/engine/subnet/enforce.rs @@ -0,0 +1,108 @@ +//! Verdict enforcement: turning subnet `Block` verdicts into bans. +//! +//! Kept apart from [`super::monitor`] so the monitor task and the ban policy +//! stay within the module-size budget. + +use crate::filter::blacklist::Blacklist; +use crate::traffic::prefix::{PrefixVerdict, SubnetVerdict}; +use std::net::IpAddr; +use std::sync::Arc; +use std::time::Duration; + +/// Verdict enforcement settings shared by every snapshot tick. +pub(super) struct BanPolicy { + pub(super) blacklist: Arc, + pub(super) ttl: Duration, + pub(super) trusted: bool, + /// Trusted PROXY-protocol networks; a prefix ban overlapping one of them is + /// suppressed, because it would punish the proxy and every client behind it. + pub(super) trusted_networks: Vec, +} + +/// Applies userspace `Block` verdicts to the shared blacklist. +/// +/// Returns the number of banned prefixes. When `trusted` is false the observed +/// addresses belong to an unverified ingress path, so the verdict is only logged +/// at warn level and no ban is installed. +/// +/// The ban is inserted as a CIDR range, so every address inside the detected +/// `/24` or `/64` is blocked, not just its network address. +pub fn enforce_tracker_verdicts( + verdicts: &[PrefixVerdict], + blacklist: &Blacklist, + ttl: Duration, + trusted: bool, + trusted_networks: &[crate::config::TrustedProxyCidr], +) -> usize { + let mut banned = 0; + for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) { + if !trusted { + tracing::warn!( + prefix = %pv.prefix, + "subnet block verdict on an untrusted peer path; not adding to the blacklist" + ); + continue; + } + if overlaps_trusted(pv.prefix, trusted_networks) { + tracing::warn!( + prefix = %pv.prefix, + "subnet ban suppressed: the prefix contains a trusted proxy network" + ); + continue; + } + blacklist.add_cidr(pv.prefix, prefix_len(pv.prefix), ttl, "subnet_attack"); + banned += 1; + tracing::warn!( + prefix = %pv.prefix, + ttl_secs = ttl.as_secs(), + "subnet blocked in the userspace blacklist" + ); + } + banned +} + +/// Installs XDP prefix bans for `Block` verdicts. +#[cfg(feature = "xdp")] +pub(super) fn ban_xdp_verdicts( + xdp: &crate::traffic::prefix::XdpPrefixStats, + verdicts: &[PrefixVerdict], + ttl: Duration, + trusted_networks: &[crate::config::TrustedProxyCidr], +) { + for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) { + if overlaps_trusted(pv.prefix, trusted_networks) { + tracing::warn!( + prefix = %pv.prefix, + "subnet kernel ban suppressed: the prefix contains a trusted proxy network" + ); + continue; + } + let result = { + let guard = xdp.filter_lock(); + guard.ban_cidr(pv.prefix, prefix_len(pv.prefix), ttl.as_secs()) + }; + match result { + Ok(()) => tracing::warn!( + prefix = %pv.prefix, + ttl_secs = ttl.as_secs(), + "subnet blocked in XDP" + ), + Err(e) => tracing::warn!("subnet block failed for {}: {e}", pv.prefix), + } + } +} + +/// Returns whether a ban of `prefix/prefix_len` would cover a trusted proxy. +fn overlaps_trusted(prefix: IpAddr, trusted_networks: &[crate::config::TrustedProxyCidr]) -> bool { + trusted_networks + .iter() + .any(|net| net.overlaps_prefix(prefix, prefix_len(prefix))) +} + +/// Prefix length matching the detection granularity (v4 /24, v6 /64). +pub(super) fn prefix_len(prefix: IpAddr) -> u8 { + match prefix { + IpAddr::V4(_) => 24, + IpAddr::V6(_) => 64, + } +} diff --git a/src/engine/subnet/mod.rs b/src/engine/subnet/mod.rs index ef35227..c760ca4 100644 --- a/src/engine/subnet/mod.rs +++ b/src/engine/subnet/mod.rs @@ -1,3 +1,4 @@ //! Subnet-level detector: userspace prefix aggregator and verdict monitor. +mod enforce; pub mod monitor; pub mod tracker; diff --git a/src/engine/subnet/monitor.rs b/src/engine/subnet/monitor.rs index 3a55e58..c954d61 100644 --- a/src/engine/subnet/monitor.rs +++ b/src/engine/subnet/monitor.rs @@ -4,6 +4,9 @@ //! (feature `xdp` with a loaded filter) or the userspace aggregator //! [`SubnetTracker`]. +use super::enforce::BanPolicy; +#[cfg(feature = "xdp")] +use super::enforce::ban_xdp_verdicts; use crate::config::DetectPrefixConfig; use crate::engine::subnet_tracker::{SubnetTracker, TrackerSource}; use crate::filter::blacklist::Blacklist; @@ -17,6 +20,8 @@ use std::sync::{Arc, Mutex}; use std::time::Duration; use tokio::sync::watch; +pub use super::enforce::enforce_tracker_verdicts; + enum Source { #[cfg(feature = "xdp")] Xdp(XdpPrefixStats), @@ -33,22 +38,6 @@ impl Source { } } -/// Verdict enforcement settings shared by every snapshot tick. -struct BanPolicy { - blacklist: Arc, - ttl: Duration, - trusted: bool, - /// Trusted PROXY-protocol networks; a prefix ban overlapping one of them is - /// suppressed, because it would punish the proxy and every client behind it. - trusted_networks: Vec, -} - -/// Spawns the background subnet monitor when `[detect.prefix] enabled`. -/// -/// Source priority: the XDP map when a filter is loaded, otherwise the userspace -/// aggregator. Without either source the monitor stays disabled and logs a -/// warning. -/// /// Everything the monitor task needs besides the detection config. pub struct MonitorSetup { /// Ban lifetime, from `[ban].ban_duration_secs`. @@ -162,90 +151,6 @@ fn apply_verdicts(verdicts: &[PrefixVerdict], source: &Source, policy: &BanPolic } } -/// Applies userspace `Block` verdicts to the shared blacklist. -/// -/// Returns the number of banned prefixes. When `trusted` is false the observed -/// addresses belong to an unverified ingress path, so the verdict is only logged -/// at warn level and no ban is installed. -/// -/// The ban is inserted as a CIDR range, so every address inside the detected -/// `/24` or `/64` is blocked, not just its network address. -pub fn enforce_tracker_verdicts( - verdicts: &[PrefixVerdict], - blacklist: &Blacklist, - ttl: Duration, - trusted: bool, - trusted_networks: &[crate::config::TrustedProxyCidr], -) -> usize { - let mut banned = 0; - for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) { - if !trusted { - tracing::warn!( - prefix = %pv.prefix, - "subnet block verdict on an untrusted peer path; not adding to the blacklist" - ); - continue; - } - if trusted_networks - .iter() - .any(|net| net.overlaps_prefix(pv.prefix, prefix_len(pv.prefix))) - { - tracing::warn!( - prefix = %pv.prefix, - "subnet ban suppressed: the prefix contains a trusted proxy network" - ); - continue; - } - blacklist.add_cidr(pv.prefix, prefix_len(pv.prefix), ttl, "subnet_attack"); - banned += 1; - tracing::warn!( - prefix = %pv.prefix, - ttl_secs = ttl.as_secs(), - "subnet blocked in the userspace blacklist" - ); - } - banned -} - -#[cfg(feature = "xdp")] -fn ban_xdp_verdicts( - xdp: &XdpPrefixStats, - verdicts: &[PrefixVerdict], - ttl: Duration, - trusted_networks: &[crate::config::TrustedProxyCidr], -) { - for pv in verdicts.iter().filter(|pv| pv.verdict == SubnetVerdict::Block) { - let len = prefix_len(pv.prefix); - if trusted_networks.iter().any(|net| net.overlaps_prefix(pv.prefix, len)) { - tracing::warn!( - prefix = %pv.prefix, - "subnet kernel ban suppressed: the prefix contains a trusted proxy network" - ); - continue; - } - let result = { - let guard = xdp.filter_lock(); - guard.ban_cidr(pv.prefix, prefix_len(pv.prefix), ttl.as_secs()) - }; - match result { - Ok(()) => tracing::warn!( - prefix = %pv.prefix, - ttl_secs = ttl.as_secs(), - "subnet blocked in XDP" - ), - Err(e) => tracing::warn!("subnet block failed for {}: {e}", pv.prefix), - } - } -} - -/// Prefix length matching the detection granularity (v4 /24, v6 /64). -fn prefix_len(prefix: IpAddr) -> u8 { - match prefix { - IpAddr::V4(_) => 24, - IpAddr::V6(_) => 64, - } -} - #[must_use] fn verdict_name(verdict: SubnetVerdict) -> &'static str { match verdict {