feat(xdp): IPv6 parity, monotonic ban expiry, per-protocol L4 minimum
IPv6 parity: - blacklist6_map (LPM trie, 20-byte key) and is_blacklisted6(); the native_v6 path now passes the port gate and the L4 checks instead of returning XDP_PASS early. - ban_cidr() covers V4 and V6; gateway bans by /32 or /128; subnet monitor bans v6 /64 prefixes; ban_ip() removed (dead code is a bug). - v6 TCP feeds prefix stats through prefix_key_from_v6 (subnet detector now sees IPv6); UDP policy=drop and detect_tcp_bypass apply to v6. - tests/xdp_ban_v6.rs and scripts/xdp_ipv6_smoke.sh (real netns/verifier). Bug fixes (both families): - Ban expiry compared SystemTime epoch-ns against bpf_ktime_get_ns(), so bans never expired. Userspace now writes CLOCK_MONOTONIC via xdp::monotonic_ns(); regression in tests/xdp_ban_clock.rs. - The L4 gate required sizeof(struct tcphdr) for every protocol, so short UDP datagrams bypassed the port gate and the blacklist. The minimum is now per-protocol (UDP 8 bytes); smoke covers it.
This commit is contained in:
parent
a90f0da226
commit
b64c329125
12 changed files with 420 additions and 36 deletions
|
|
@ -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}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,23 @@
|
|||
use super::XdpFilter;
|
||||
use anyhow::{Result, bail};
|
||||
use anyhow::Result;
|
||||
use libbpf_rs::{MapCore, MapFlags};
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
|
||||
pub(super) fn unix_ns() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos() as u64
|
||||
/// Monotonic clock in ns since boot — the same clock as `bpf_ktime_get_ns()`,
|
||||
/// which is what the XDP program compares ban expiry against. `SystemTime`
|
||||
/// (epoch ns) is ~50 years larger, so bans written with it never expired.
|
||||
///
|
||||
/// # Panics
|
||||
/// Never: `clock_gettime(CLOCK_MONOTONIC)` cannot fail on Linux.
|
||||
#[must_use]
|
||||
pub fn monotonic_ns() -> u64 {
|
||||
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
|
||||
// SAFETY: `ts` is a valid, writable `timespec` and CLOCK_MONOTONIC is
|
||||
// always available on Linux (the only target of the `xdp` feature).
|
||||
unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) };
|
||||
let secs = u64::try_from(ts.tv_sec).unwrap_or(0);
|
||||
secs.saturating_mul(1_000_000_000)
|
||||
.saturating_add(u64::try_from(ts.tv_nsec).unwrap_or(0))
|
||||
}
|
||||
|
||||
/// Encodes an LPM-trie key for `blacklist_map`: prefix length in byte 0 and the
|
||||
|
|
@ -19,29 +29,47 @@ pub(super) fn blacklist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] {
|
|||
key
|
||||
}
|
||||
|
||||
/// Absolute ban expiry in ns-since-epoch, saturating so an absurd duration
|
||||
/// 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 on the monotonic clock, 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 {
|
||||
now_ns.saturating_add(duration_secs.saturating_mul(1_000_000_000))
|
||||
}
|
||||
|
||||
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<()> {
|
||||
let map = self.find_map("blacklist_map")?;
|
||||
let key = blacklist_key(prefix_len, v4);
|
||||
let expiry_ns = blacklist_expiry_ns(unix_ns(), duration_secs);
|
||||
let expiry_ns = blacklist_expiry_ns(monotonic_ns(), duration_secs);
|
||||
map.update(&key, &expiry_ns.to_le_bytes(), MapFlags::ANY)?;
|
||||
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(monotonic_ns(), duration_secs);
|
||||
map.update(&key, &expiry_ns.to_le_bytes(), MapFlags::ANY)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ mod attach;
|
|||
pub(crate) mod events;
|
||||
mod maps;
|
||||
|
||||
pub use maps::{blacklist6_key, monotonic_ns};
|
||||
|
||||
pub struct XdpFilter {
|
||||
obj: Option<Object>,
|
||||
ringbuf: Option<RingBuffer<'static>>,
|
||||
|
|
|
|||
|
|
@ -19,9 +19,13 @@ 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,
|
||||
};
|
||||
#[cfg(feature = "xdp")]
|
||||
pub use filter::monotonic_ns;
|
||||
|
||||
#[cfg(feature = "xdp")]
|
||||
pub use probe::XdpMetrics;
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue