fix(xdp): ban expiry on CLOCK_MONOTONIC; per-protocol L4 minimum

- userspace wrote epoch ns while the kernel compares ban expiry against
  bpf_ktime_get_ns(), so gateway/detector bans never expired (v4 and v6).
  xdp::monotonic_ns() now reads CLOCK_MONOTONIC.
- universal_filter.c required sizeof(tcphdr) (20 bytes) of L4 for every
  protocol, so short UDP datagrams took an early XDP_PASS and bypassed
  the blacklist (v4 and v6). Minimum now depends on the protocol.
- tests/xdp_ban_clock.rs guards the clock choice.
- xdp_ipv6_smoke.sh: v6 pass/ban/unban, short datagrams, v4 short
  datagrams and real 2s ban expiry on a live kernel.
This commit is contained in:
Loki 2026-11-15 23:08:05 +01:00
parent 59d5a088b2
commit 295ab81fb4
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
7 changed files with 173 additions and 78 deletions

View file

@ -3,11 +3,21 @@ 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
@ -29,7 +39,7 @@ pub fn blacklist6_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] {
key
}
/// Absolute ban expiry in ns-since-epoch, saturating so an absurd duration
/// 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))
@ -51,7 +61,7 @@ impl XdpFilter {
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(())
}
@ -59,7 +69,7 @@ impl XdpFilter {
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);
let expiry_ns = blacklist_expiry_ns(monotonic_ns(), duration_secs);
map.update(&key, &expiry_ns.to_le_bytes(), MapFlags::ANY)?;
Ok(())
}

View file

@ -16,7 +16,7 @@ mod attach;
pub(crate) mod events;
mod maps;
pub use maps::blacklist6_key;
pub use maps::{blacklist6_key, monotonic_ns};
pub struct XdpFilter {
obj: Option<Object>,

View file

@ -24,6 +24,8 @@ pub use filter::blacklist6_key;
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;