feat(xdp): IPv6 parity — blacklist6_map, v6 bans, v6 prefix stats

- C: lpm_key6 + blacklist6_map (LPM_TRIE, 20-byte key); native IPv6 no
  longer returns XDP_PASS before filtering: same L4/port gates, then
  blacklist6, UDP drop-policy, TCP flag checks and /64 prefix stats.
- Rust: blacklist6_key/blacklist6_update; ban_cidr handles V4 and V6;
  gateway xdp_ban uses ban_cidr (/32 or /128); dead ban_ip removed.
- tests/xdp_ban_v6.rs (key layout) and scripts/xdp_ipv6_smoke.sh
  (real kernel: pass -> ban drops -> unban passes).
- TODO: record done items and two findings (userspace ban expiry clock
  mismatch; short-L4 bypass of blacklist).
This commit is contained in:
Loki 2026-11-15 23:02:12 +01:00
parent cacd01c657
commit 59d5a088b2
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
11 changed files with 309 additions and 36 deletions

View file

@ -202,12 +202,14 @@ impl Gateway {
let Some(xdp) = &self.xdp else {
return;
};
let IpAddr::V4(ip_v4) = ip else {
return;
};
match xdp.lock().expect("xdp lock poisoned").ban_ip(ip_v4, duration_secs) {
Ok(()) => tracing::debug!("xdp ban {ip_v4} for {duration_secs}s"),
Err(e) => tracing::warn!("xdp ban failed for {ip_v4}: {e}"),
let prefix_len = if ip.is_ipv4() { 32 } else { 128 };
match xdp
.lock()
.expect("xdp lock poisoned")
.ban_cidr(ip, prefix_len, duration_secs)
{
Ok(()) => tracing::debug!("xdp ban {ip} for {duration_secs}s"),
Err(e) => tracing::warn!("xdp ban failed for {ip}: {e}"),
}
}

View file

@ -1,5 +1,5 @@
use super::XdpFilter;
use anyhow::{Result, bail};
use anyhow::Result;
use libbpf_rs::{MapCore, MapFlags};
use std::net::{IpAddr, Ipv4Addr};
@ -19,6 +19,16 @@ pub(super) fn blacklist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] {
key
}
/// Encodes an LPM-trie key for `blacklist6_map`: prefix length as a
/// little-endian u32 in bytes 0..4 and the 16-byte IPv6 address in network
/// order in bytes 4..20 (20 bytes total, matching the C `struct lpm_key6`).
pub fn blacklist6_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] {
let mut key = [0u8; 20];
key[0] = prefix_len;
key[4..20].copy_from_slice(v6);
key
}
/// Absolute ban expiry in ns-since-epoch, saturating so an absurd duration
/// clamps to `u64::MAX` rather than wrapping into the past.
pub(super) fn blacklist_expiry_ns(now_ns: u64, duration_secs: u64) -> u64 {
@ -26,16 +36,16 @@ pub(super) fn blacklist_expiry_ns(now_ns: u64, duration_secs: u64) -> u64 {
}
impl XdpFilter {
pub fn ban_ip(&self, ip: Ipv4Addr, duration_secs: u64) -> Result<()> {
self.blacklist_update(32, &ip.octets(), duration_secs)
}
/// Adds a CIDR ban to the LPM trie. IPv6 enforcement is not supported yet.
/// Adds a CIDR ban to the matching LPM trie: `blacklist_map` for IPv4,
/// `blacklist6_map` for IPv6.
///
/// # Errors
/// XDP not loaded, map missing from the object, or a BPF update failure.
pub fn ban_cidr(&self, prefix: IpAddr, prefix_len: u8, duration_secs: u64) -> Result<()> {
let IpAddr::V4(net) = prefix else {
bail!("ipv6 cidr bans not supported yet");
};
self.blacklist_update(prefix_len, &net.octets(), duration_secs)
match prefix {
IpAddr::V4(net) => self.blacklist_update(prefix_len, &net.octets(), duration_secs),
IpAddr::V6(net) => self.blacklist6_update(prefix_len, &net.octets(), duration_secs),
}
}
fn blacklist_update(&self, prefix_len: u8, v4: &[u8; 4], duration_secs: u64) -> Result<()> {
@ -46,6 +56,14 @@ impl XdpFilter {
Ok(())
}
fn blacklist6_update(&self, prefix_len: u8, v6: &[u8; 16], duration_secs: u64) -> Result<()> {
let map = self.find_map("blacklist6_map")?;
let key = blacklist6_key(prefix_len, v6);
let expiry_ns = blacklist_expiry_ns(unix_ns(), duration_secs);
map.update(&key, &expiry_ns.to_le_bytes(), MapFlags::ANY)?;
Ok(())
}
pub fn unban_ip(&self, ip: Ipv4Addr) -> Result<()> {
let map = self.find_map("blacklist_map")?;
let key = blacklist_key(32, &ip.octets());

View file

@ -16,6 +16,8 @@ mod attach;
pub(crate) mod events;
mod maps;
pub use maps::blacklist6_key;
pub struct XdpFilter {
obj: Option<Object>,
ringbuf: Option<RingBuffer<'static>>,

View file

@ -19,6 +19,8 @@ mod filter;
#[cfg(feature = "xdp")]
pub use filter::XdpFilter;
#[cfg(feature = "xdp")]
pub use filter::blacklist6_key;
#[cfg(feature = "xdp")]
pub use filter::events::{
AutoBanConfig, BanAction, XDP_EVENT_LEN, XdpEvent, XdpEventKind, auto_ban_decision, parse_xdp_event,
};

View file

@ -16,9 +16,6 @@ impl XdpFilter {
Ok(())
}
pub fn drain_events(&self) {}
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(())
}