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.
This commit is contained in:
Loki 2026-11-16 13:03:50 +01:00
parent 72c1f45d62
commit bcae8901c6
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
11 changed files with 300 additions and 106 deletions

View file

@ -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<crate::config::TrustedProxyCidr>,
}
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<crate::config::TrustedProxyCidr>) -> 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
}
}