fix(xdp): correct payload length, attach/detach, whitelist and ban upkeep

BPF program (the first two made the filter wrong on real hardware):
- Compute the TCP payload length from ip->tot_len (IPv4) / payload_len
  (IPv6) instead of data_end. Ethernet padding on a real NIC added 6 bogus
  payload bytes, broke expected_seq and dropped every connection after the
  fourth segment. scripts/xdp_pkt_test.sh reproduces it on synthetic frames
  and fails against the old code.
- Read map values (conn->fails, sent_at) before bpf_map_delete_elem; the LRU
  element is freed by the delete.
- Parse up to two VLAN/QinQ tags before L3; fragments are checked against the
  blacklist and passed instead of bypassing all filtering; first fragments are
  no longer dropped.
- Unknown non-SYN flows PASS: the program is a stateless filter fed by the
  conntrack state machine, not a firewall, so spoofed SYNs cannot evict
  established flows into a drop and replies to the daemon's own outbound TCP
  keep working.
- Reset conn->fails on in-order segments, honour SYN payload (TFO), fix the
  IPv6 SYN-ACK address swap, count every DROP path, drop SYN with IP options.
- Expired prefix bans are no longer deleted with the wrong LPM key; userspace
  reaps them with the exact key.

Loader:
- Remember the effective attach flags (native vs generic) and use them for
  detach/query, verifying the interface is clean afterwards. The old code
  always detached the native slot, leaving a generic program attached after
  shutdown.
- DetachGuard rolls back the attach if anything after it fails.
- ban_cidr validates the prefix length and masks host bits.
- Whitelist maps (v4 and the new v6 LPM trie) are populated from the config at
  startup and fail fast on error.
- Stats read all 13 counters, and a test asserts every map's key/value size
  against the Rust encodings.

Tests: xdp_pkt_test (padding, VLAN/QinQ, fragments), xdp_loader (attach flags,
rollback, detach), xdp_ban_keys, xdp_whitelist_keys, xdp_services, live
xdp_attach_smoke (ignored, needs root).
This commit is contained in:
Loki 2026-11-16 12:06:05 +01:00
parent a8a3057719
commit 14dcaccdbf
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
27 changed files with 1578 additions and 408 deletions

View file

@ -51,11 +51,12 @@ fn register_http(registry: &mut ProtocolRegistry, config: &Config) {
}
#[cfg(feature = "xdp")]
pub(crate) fn start_xdp(
pub fn start_xdp(
config: &Arc<Config>,
shutdown_rx: &watch::Receiver<bool>,
blacklist: &Arc<Blacklist>,
) -> anyhow::Result<Option<Arc<Mutex<XdpFilter>>>> {
use anyhow::Context as _;
use std::time::Duration;
use crate::xdp::XdpMetrics;
@ -82,12 +83,28 @@ pub(crate) fn start_xdp(
);
}
guard.load()?;
// The kernel filter only honours its LPM whitelist, so a configured
// `whitelist` that fails to load is a startup error: silently dropping
// the entries would expose trusted peers to the XDP rules.
let whitelist = build_whitelist(config)?;
for ip in whitelist.iter() {
guard
.whitelist_add(*ip)
.with_context(|| format!("failed to load XDP whitelist entry {ip}"))?;
}
tracing::info!(
entries = whitelist.len(),
"configured whitelist loaded into the XDP LPM maps"
);
}
let xdp_metrics = XdpMetrics::register()?;
let sd = shutdown_rx.clone();
let shared_thread = shared.clone();
std::thread::spawn(move || {
// The loop ticks every 5 s; sweep the ban maps once a minute.
const REAP_EVERY_TICKS: u32 = 12;
let mut tick: u32 = 0;
while !*sd.borrow() {
let guard = match shared_thread.lock() {
Ok(g) => g,
@ -97,6 +114,17 @@ pub(crate) fn start_xdp(
if let Ok(stats) = guard.get_stats() {
xdp_metrics.update(&stats);
}
tick += 1;
if tick >= REAP_EVERY_TICKS {
tick = 0;
// The packet path only expires the exact /32 or /128 key it
// looked up, so wider prefix bans stay resident until swept.
match guard.reap_expired_bans() {
Ok(0) => {},
Ok(n) => tracing::info!(reaped = n, "expired XDP prefix bans removed"),
Err(e) => tracing::warn!(error = %e, "XDP expired-ban reaper failed"),
}
}
drop(guard);
std::thread::sleep(Duration::from_secs(5));
}
@ -108,7 +136,7 @@ pub(crate) fn start_xdp(
}
#[cfg(not(feature = "xdp"))]
pub(crate) fn start_xdp(
pub fn start_xdp(
_config: &Arc<Config>,
_shutdown_rx: &watch::Receiver<bool>,
_blacklist: &Arc<Blacklist>,
@ -153,7 +181,7 @@ fn ensure_store_features(config: &Config) -> anyhow::Result<()> {
/// # Errors
/// Fails on an invalid entry or when the config requires store features that are
/// not compiled in.
pub(crate) fn build_whitelist(config: &Config) -> anyhow::Result<Arc<HashSet<IpAddr>>> {
pub fn build_whitelist(config: &Config) -> anyhow::Result<Arc<HashSet<IpAddr>>> {
ensure_store_features(config)?;
let mut set = HashSet::with_capacity(config.whitelist.len());
for entry in &config.whitelist {
@ -164,51 +192,3 @@ pub(crate) fn build_whitelist(config: &Config) -> anyhow::Result<Arc<HashSet<IpA
}
Ok(Arc::new(set))
}
#[cfg(test)]
mod tests {
use super::*;
fn config_with_redis() -> Config {
Config {
store: crate::config::StoreConfig {
redis_url: Some("redis://127.0.0.1:6379/0".to_string()),
..Default::default()
},
..Default::default()
}
}
#[cfg(not(feature = "store-redis"))]
#[test]
fn redis_config_without_feature_fails_fast() {
let err = ensure_store_features(&config_with_redis()).expect_err("must fail without store-redis");
assert!(err.to_string().contains("built without store-redis feature"));
}
#[cfg(not(feature = "store-redis"))]
#[test]
fn empty_store_config_passes_without_feature() {
assert!(ensure_store_features(&Config::default()).is_ok());
}
#[cfg(feature = "store-redis")]
#[test]
fn guard_is_noop_with_feature() {
assert!(ensure_store_features(&config_with_redis()).is_ok());
}
/// XDP auto-ban wiring: `start_xdp` must accept the `Arc<Blacklist>` and, with
/// `[xdp]` disabled, touch neither the kernel nor the blacklist. The
/// `set_autoban` -> `load()` order inside the enabled branch cannot be checked
/// without a kernel.
#[test]
fn start_xdp_accepts_blacklist_and_skips_kernel_when_disabled() {
let (_tx, rx) = watch::channel(false);
let blacklist = Arc::new(Blacklist::new());
let started = start_xdp(&Arc::new(Config::default()), &rx, &blacklist)
.expect("xdp disabled: no kernel work and no error");
assert!(started.is_none());
assert!(blacklist.is_empty());
}
}

View file

@ -1,27 +1,25 @@
use anyhow::{Context, Result, bail};
use libbpf_rs::{MapCore, Object, OpenObject, RingBuffer, RingBufferBuilder};
use std::ffi::CString;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;
use std::os::unix::io::AsFd;
use anyhow::{Context, Result, bail};
use libbpf_rs::{Object, OpenObject, Program, Xdp, XdpFlags};
use crate::filter::blacklist::Blacklist;
use crate::xdp::globals::{RODATA_MAP_NAME, XdpGlobals};
use crate::xdp::metrics::XDP_AUTOBANS_TOTAL;
use super::events::{AutoBanConfig, auto_ban_decision, parse_xdp_event};
/// Name of the XDP program inside the compiled object.
const XDP_PROG_NAME: &str = "rampart_universal_filter";
/// Builds the NUL-terminated C string libc's `if_nametoindex` requires.
/// Rejects interface names containing an interior NUL byte instead of
/// silently truncating at it (the `&str`-as-`*const c_char` UB we removed).
pub(super) fn interface_cstr(name: &str) -> Result<CString> {
pub fn interface_cstr(name: &str) -> Result<CString> {
CString::new(name).with_context(|| format!("interface name {name:?} contains an interior NUL byte"))
}
/// The internal `.rodata` data map is matched by suffix: libbpf names it
/// `<obj_name>.rodata`, and with `open_memory` and no explicit `.name()` the object is
/// called `memory` — an exact comparison broke on a live kernel (smoke test).
pub(super) fn is_rodata_map(name: &str) -> bool {
pub fn is_rodata_map(name: &str) -> bool {
name.ends_with(".rodata")
}
@ -57,108 +55,148 @@ pub(super) fn patch_rodata(open_obj: &mut OpenObject, globals: &XdpGlobals) -> R
.with_context(|| format!("failed to set initial value of '{RODATA_MAP_NAME}'"))
}
/// `XDP_ATTACHED_*` values from `linux/netlink.h` (`IFLA_XDP_ATTACHED`).
///
/// The order is NONE, DRV, SKB, HW, MULTI — hardware offload is 3, not 2.
const XDP_ATTACHED_DRV: u8 = 1;
const XDP_ATTACHED_SKB: u8 = 2;
const XDP_ATTACHED_HW: u8 = 3;
const XDP_ATTACHED_MULTI: u8 = 4;
/// Honest name of the attach mode, derived from the `attach_mode` field of
/// `bpf_xdp_query_opts` (kernel `enum xdp_attached_mode`: NONE=0, DRV=1, OFFLOAD=2, SKB=3).
pub(super) fn xdp_attach_mode_name(mode: u8) -> &'static str {
/// `bpf_xdp_query_opts` (kernel `enum xdp_attach_mode`).
pub fn xdp_attach_mode_name(mode: u8) -> &'static str {
match mode {
1 => "native (drv)",
2 => "hardware offload",
3 => "generic (skb)",
XDP_ATTACHED_DRV => "native (drv)",
XDP_ATTACHED_SKB => "generic (skb)",
XDP_ATTACHED_HW => "hardware offload",
XDP_ATTACHED_MULTI => "multiple programs",
_ => "none",
}
}
/// Builds the ringbuf callback for `events_ringbuf`.
/// Detach flags matching the mode the kernel actually attached the program in.
///
/// The callback must be `'static`, so all shared state (`Arc<Blacklist>`, a copy of
/// `AutoBanConfig`) is captured by `move` clones. When `blacklist` is `None` (feature not
/// wired) or the policy is disabled, events are only parsed and logged in a structured
/// way; no bans are written. The hot path does no `format!` — only `tracing` fields and
/// `&'static str` reasons.
pub(super) fn build_ringbuf(
obj: &Object,
blacklist: Option<Arc<Blacklist>>,
cfg: AutoBanConfig,
) -> Result<RingBuffer<'static>> {
let map = obj
.maps()
.find(|m| m.name() == "events_ringbuf")
.context("events_ringbuf not found")?;
let mut builder = RingBufferBuilder::new();
builder.add(&map, move |data: &[u8]| {
let Some(event) = parse_xdp_event(data) else {
tracing::debug!(
len = data.len(),
"xdp ringbuf frame ignored (too short or unknown type)"
);
return 0;
};
tracing::debug!(
kind = ?event.kind,
src_ip = %event.src_ip,
metadata = event.metadata,
"xdp event"
/// The mode is discovered after attach via `bpf_xdp_query`; using the flags
/// that were *requested* is wrong when the kernel picked a different mode
/// (for example generic after a failed native attach).
fn detach_flags_for_mode(mode: u8) -> Option<XdpFlags> {
match mode {
XDP_ATTACHED_DRV => Some(XdpFlags::DRV_MODE),
XDP_ATTACHED_SKB => Some(XdpFlags::SKB_MODE),
XDP_ATTACHED_HW => Some(XdpFlags::HW_MODE),
XDP_ATTACHED_MULTI => None,
// NONE: the query failed after a successful attach.
_ => None,
}
}
/// Attaches with the native-first mode ladder and returns the detach flags.
fn attach_with_fallback(xdp: &Xdp<'_>, ifindex: i32, interface: &str) -> Result<XdpFlags> {
if let Err(e) = xdp.attach(ifindex, XdpFlags::NONE) {
tracing::warn!(
interface = %interface,
error = %e,
"native XDP attach failed, falling back to generic (skb) mode"
);
let Some(blacklist) = &blacklist else {
return 0;
};
if let Some(ban) = auto_ban_decision(&event, &cfg) {
let addr = IpAddr::V4(ban.ip);
// `Blacklist::add` returns `()` (it ignores `DashMap::insert`) and blacklist.rs is
// out of our edit scope, so "is this a new ban" is decided through the public
// `is_blocked` before the overwrite. This dedups the log line and the metric by
// active ban, not by the fact of insertion.
let already_banned = blacklist.is_blocked(addr);
blacklist.add(addr, Duration::from_secs(ban.duration_secs), ban.reason);
if already_banned {
tracing::debug!(src_ip = %ban.ip, reason = ban.reason, "xdp auto-ban refreshed");
} else {
XDP_AUTOBANS_TOTAL.inc();
tracing::info!(
src_ip = %ban.ip,
reason = ban.reason,
duration_secs = ban.duration_secs,
"xdp auto-ban issued"
);
}
xdp.attach(ifindex, XdpFlags::SKB_MODE)
.context("XDP attach failed in both native and generic modes")?;
Ok(XdpFlags::SKB_MODE)
} else {
Ok(XdpFlags::NONE)
}
}
/// Finds the loader's XDP program in a loaded object.
fn find_xdp_program<'obj>(obj: &'obj Object) -> Result<Program<'obj>> {
obj.progs()
.find(|p| p.name() == XDP_PROG_NAME)
.with_context(|| format!("XDP program '{XDP_PROG_NAME}' not found"))
}
/// Detaches the program if it is dropped while still armed.
///
/// Any error between a successful attach and the moment `XdpFilter` takes
/// ownership leaves an XDP program attached that drops traffic but has no
/// userspace reader (an orphaned filter). This guard makes that window fail
/// safe: unless [`DetachGuard::disarm`] was called, dropping it detaches.
pub(super) struct DetachGuard<'obj> {
prog: Program<'obj>,
ifindex: i32,
/// Raw `XdpFlags` bits: `XdpFlags` itself is neither `Copy` nor `Clone`.
flags_bits: u32,
armed: bool,
}
impl<'obj> DetachGuard<'obj> {
fn new(prog: Program<'obj>, ifindex: i32, flags_bits: u32) -> Self {
Self {
prog,
ifindex,
flags_bits,
armed: true,
}
0
})?;
Ok(builder.build()?)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interface_cstr_accepts_plain_names() {
let c = interface_cstr("eth0").expect("valid interface name");
assert_eq!(c.as_bytes(), b"eth0");
assert_eq!(c.as_bytes_with_nul(), b"eth0\0");
}
#[test]
fn interface_cstr_rejects_interior_nul() {
assert!(interface_cstr("et\0h0").is_err());
}
#[test]
fn rodata_map_matches_libbpf_naming() {
// libbpf builds the name as `<obj><section>` without rewriting the dot
// (libbpf.c ~1911); with open_memory and no name the object is called "memory":
assert!(is_rodata_map("rampart.rodata"));
assert!(is_rodata_map("memory.rodata"));
assert!(is_rodata_map(".rodata"));
assert!(!is_rodata_map("rampart.bss"));
assert!(!is_rodata_map("stats_map"));
}
#[test]
fn attach_mode_names_are_honest() {
assert_eq!(xdp_attach_mode_name(1), "native (drv)");
assert_eq!(xdp_attach_mode_name(3), "generic (skb)");
assert_eq!(xdp_attach_mode_name(2), "hardware offload");
assert_eq!(xdp_attach_mode_name(0), "none");
/// Marks the attach as committed; dropping the guard is then a no-op.
pub(super) fn disarm(mut self) {
self.armed = false;
}
}
impl Drop for DetachGuard<'_> {
fn drop(&mut self) {
if !self.armed {
return;
}
if let Err(e) = Xdp::new(self.prog.as_fd()).detach(self.ifindex, XdpFlags::from_bits_truncate(self.flags_bits))
{
tracing::error!(
ifindex = self.ifindex,
error = %e,
"XDP attach rollback failed: the program may still be attached"
);
} else {
tracing::warn!(ifindex = self.ifindex, "XDP attach rolled back after a setup failure");
}
}
}
/// Attaches the program and returns `(detach flags, mode name, rollback guard)`.
///
/// The returned guard must be disarmed only after every fallible step that
/// follows the attach has succeeded. The attach mode is queried from the
/// kernel (not assumed from the requested flags) so that a generic-mode
/// fallback is detached from the generic slot.
pub(super) fn attach_program<'obj>(
obj: &'obj Object,
ifindex: i32,
interface: &str,
) -> Result<(u32, &'static str, DetachGuard<'obj>)> {
let prog = find_xdp_program(obj)?;
let requested = {
let xdp = Xdp::new(prog.as_fd());
attach_with_fallback(&xdp, ifindex, interface)?
};
let mode = match Xdp::new(prog.as_fd()).query(ifindex, XdpFlags::NONE) {
Ok(query) => query.attach_mode,
Err(e) => {
tracing::warn!(
interface = %interface,
error = %e,
"XDP attach-mode query failed; assuming the requested flags"
);
0
},
};
// Fall back to the requested flags when the query failed or the interface
// reports several programs (MULTI): that is the best detach handle we have.
let flags_bits = detach_flags_for_mode(mode).unwrap_or(requested).bits();
let guard = DetachGuard::new(prog, ifindex, flags_bits);
let name = if mode == 0 {
"unknown"
} else {
xdp_attach_mode_name(mode)
};
Ok((flags_bits, name, guard))
}

View file

@ -1,11 +1,19 @@
//! Parsing and policy for XDP ringbuf events (`struct xdp_event`).
//!
//! Pure functions (no I/O, no libbpf) so that they can be covered by unit tests without
//! a kernel or Redis. The callback wiring lives in [`super::attach`].
//! Parsing and the auto-ban policy are pure functions covered by
//! `tests/xdp_events.rs`; [`build_ringbuf`] wires the same policy into the libbpf
//! ringbuf callback.
use std::net::Ipv4Addr;
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Context, Result};
use libbpf_rs::{MapCore, Object, RingBuffer, RingBufferBuilder};
use crate::config::XdpConfig;
use crate::filter::blacklist::Blacklist;
use crate::xdp::metrics::XDP_AUTOBANS_TOTAL;
/// Exact size of `struct xdp_event` (see `xdp/core/common.h:61-66`):
/// `__u32 type` @0, `__u32 src_ip` @4, `__u32 metadata` @8,
@ -166,5 +174,64 @@ pub fn auto_ban_decision(event: &XdpEvent, config: &AutoBanConfig) -> Option<Ban
}
}
/// Builds the ringbuf callback for `events_ringbuf`.
///
/// The callback must be `'static`, so all shared state (`Arc<Blacklist>`, a copy of
/// `AutoBanConfig`) is captured by `move` clones. When `blacklist` is `None` (feature not
/// wired) or the policy is disabled, events are only parsed and logged in a structured
/// way; no bans are written. The hot path does no `format!` — only `tracing` fields and
/// `&'static str` reasons.
pub(super) fn build_ringbuf(
obj: &Object,
blacklist: Option<Arc<Blacklist>>,
cfg: AutoBanConfig,
) -> Result<RingBuffer<'static>> {
let map = obj
.maps()
.find(|m| m.name() == "events_ringbuf")
.context("events_ringbuf not found")?;
let mut builder = RingBufferBuilder::new();
builder.add(&map, move |data: &[u8]| {
let Some(event) = parse_xdp_event(data) else {
tracing::debug!(
len = data.len(),
"xdp ringbuf frame ignored (too short or unknown type)"
);
return 0;
};
tracing::debug!(
kind = ?event.kind,
src_ip = %event.src_ip,
metadata = event.metadata,
"xdp event"
);
let Some(blacklist) = &blacklist else {
return 0;
};
if let Some(ban) = auto_ban_decision(&event, &cfg) {
let addr = IpAddr::V4(ban.ip);
// `Blacklist::add` returns `()` (it ignores `DashMap::insert`) and blacklist.rs is
// out of our edit scope, so "is this a new ban" is decided through the public
// `is_blocked` before the overwrite. This dedups the log line and the metric by
// active ban, not by the fact of insertion.
let already_banned = blacklist.is_blocked(addr);
blacklist.add(addr, Duration::from_secs(ban.duration_secs), ban.reason);
if already_banned {
tracing::debug!(src_ip = %ban.ip, reason = ban.reason, "xdp auto-ban refreshed");
} else {
XDP_AUTOBANS_TOTAL.inc();
tracing::info!(
src_ip = %ban.ip,
reason = ban.reason,
duration_secs = ban.duration_secs,
"xdp auto-ban issued"
);
}
}
0
})?;
Ok(builder.build()?)
}
// Parser and policy coverage lives in `tests/xdp_events.rs` (this module is kept under
// the 250-line limit; `src/xdp/filter/` already has the maximum of 4 files per directory).

View file

@ -1,58 +1,117 @@
use super::XdpFilter;
use anyhow::Result;
use libbpf_rs::{MapCore, MapFlags};
use std::net::{IpAddr, Ipv4Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
/// 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.
use anyhow::{Result, bail};
use libbpf_rs::{MapCore, MapFlags};
use super::XdpFilter;
use crate::xdp::probe::{blacklist_expiry_ns, monotonic_ns};
/// Encodes an LPM-trie key for `blacklist_map` and `whitelist_map`:
/// prefix length as a little-endian u32 in bytes 0..4 and the four IPv4
/// octets in network order in bytes 4..8 (`struct lpm_key` in `common.h`).
#[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))
pub fn blacklist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] {
ipv4_lpm_key(prefix_len, v4)
}
/// Encodes an LPM-trie key for `blacklist_map`: prefix length in byte 0 and the
/// four IPv4 octets in bytes 4..8 (bytes 1..3 are padding the kernel ignores).
pub(super) fn blacklist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] {
let mut key = [0u8; 8];
key[0] = prefix_len;
key[4..8].copy_from_slice(v4);
key
/// Encodes an LPM-trie key for `whitelist_map`; the layout is identical to
/// [`blacklist_key`] because both C maps use `struct lpm_key`.
#[must_use]
pub fn whitelist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] {
ipv4_lpm_key(prefix_len, v4)
}
/// 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`).
#[must_use]
pub fn blacklist6_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] {
ipv6_lpm_key(prefix_len, v6)
}
/// Encodes an LPM-trie key for `whitelist6_map`; identical to
/// [`blacklist6_key`] because both C maps use `struct lpm_key6`.
#[must_use]
pub fn whitelist6_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] {
ipv6_lpm_key(prefix_len, v6)
}
fn ipv4_lpm_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] {
let mut key = [0u8; 8];
key[0..4].copy_from_slice(&u32::from(prefix_len).to_le_bytes());
key[4..8].copy_from_slice(v4);
key
}
fn ipv6_lpm_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] {
let mut key = [0u8; 20];
key[0] = prefix_len;
key[0..4].copy_from_slice(&u32::from(prefix_len).to_le_bytes());
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))
/// Validates a CIDR prefix length and clears the host bits of `prefix`.
///
/// The kernel's LPM trie compares keys bit by bit, so `10.1.2.3/24` and
/// `10.1.2.0/24` are different keys for the same range: without masking, two
/// bans of the same subnet never share an entry and expiry bookkeeping falls
/// apart. A zero-length prefix is rejected because "ban the entire address
/// space" is never an intentional operator request.
///
/// # Errors
/// `prefix_len` is 0 or exceeds 32 for IPv4 / 128 for IPv6.
pub fn normalize_cidr(prefix: IpAddr, prefix_len: u8) -> Result<IpAddr> {
match prefix {
IpAddr::V4(addr) => {
ensure_prefix_len(prefix_len, 32, "IPv4")?;
Ok(IpAddr::V4(Ipv4Addr::from(mask_v4(addr, prefix_len))))
},
IpAddr::V6(addr) => {
ensure_prefix_len(prefix_len, 128, "IPv6")?;
Ok(IpAddr::V6(Ipv6Addr::from(mask_v6(addr, prefix_len))))
},
}
}
fn ensure_prefix_len(prefix_len: u8, max: u8, family: &str) -> Result<()> {
if prefix_len == 0 || prefix_len > max {
bail!("invalid {family} prefix length {prefix_len}: expected 1..={max}");
}
Ok(())
}
fn mask_v4(addr: Ipv4Addr, prefix_len: u8) -> [u8; 4] {
let bits = u32::from(addr);
let mask = u32::MAX << (32 - u32::from(prefix_len));
(bits & mask).to_be_bytes()
}
fn mask_v6(addr: Ipv6Addr, prefix_len: u8) -> [u8; 16] {
let mut octets = addr.octets();
let full_bytes = usize::from(prefix_len) / 8;
let remainder = prefix_len % 8;
for byte in octets.iter_mut().skip(full_bytes) {
*byte = 0;
}
if remainder != 0 {
// `full_bytes` is always < 16 here: a partial byte follows the full ones.
octets[full_bytes] &= 0xffu8 << (8 - remainder);
}
octets
}
impl XdpFilter {
/// Adds a CIDR ban to the matching LPM trie: `blacklist_map` for IPv4,
/// `blacklist6_map` for IPv6.
///
/// The prefix is masked first (see [`normalize_cidr`]), so every ban of
/// the same subnet addresses the same kernel key.
///
/// # Errors
/// XDP not loaded, map missing from the object, or a BPF update failure.
/// Invalid prefix length, 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<()> {
match prefix {
match normalize_cidr(prefix, prefix_len)? {
IpAddr::V4(net) => self.blacklist_update(prefix_len, &net.octets(), duration_secs),
IpAddr::V6(net) => self.blacklist6_update(prefix_len, &net.octets(), duration_secs),
}
@ -74,6 +133,113 @@ impl XdpFilter {
Ok(())
}
/// Adds one address to the kernel whitelist: `/32` for IPv4, `/128` for IPv6.
///
/// # Errors
/// XDP not loaded, the matching whitelist map is missing, or a BPF update
/// failure.
pub fn whitelist_add(&self, ip: IpAddr) -> Result<()> {
let prefix_len = match ip {
IpAddr::V4(_) => 32,
IpAddr::V6(_) => 128,
};
self.whitelist_add_cidr(ip, prefix_len)
}
/// Adds a CIDR to the matching whitelist LPM trie: `whitelist_map` for
/// IPv4, `whitelist6_map` for IPv6.
///
/// The C filter treats a present key as "trusted" and ignores the value,
/// so the stored `u8` is always `1`.
///
/// # Errors
/// Invalid prefix length, XDP not loaded, map missing from the object, or
/// a BPF update failure.
pub fn whitelist_add_cidr(&self, prefix: IpAddr, prefix_len: u8) -> Result<()> {
match normalize_cidr(prefix, prefix_len)? {
IpAddr::V4(net) => {
let map = self.find_map("whitelist_map")?;
map.update(&whitelist_key(prefix_len, &net.octets()), &[1u8], MapFlags::ANY)?;
},
IpAddr::V6(net) => {
let map = self.find_map("whitelist6_map")?;
map.update(&whitelist6_key(prefix_len, &net.octets()), &[1u8], MapFlags::ANY)?;
},
}
Ok(())
}
/// Removes one address from the kernel whitelist.
///
/// # Errors
/// XDP not loaded, the matching whitelist map is missing, or a BPF delete
/// failure.
pub fn whitelist_remove(&self, ip: IpAddr) -> Result<()> {
match ip {
IpAddr::V4(addr) => {
let map = self.find_map("whitelist_map")?;
map.delete(&whitelist_key(32, &addr.octets()))?;
},
IpAddr::V6(addr) => {
let map = self.find_map("whitelist6_map")?;
map.delete(&whitelist6_key(128, &addr.octets()))?;
},
}
Ok(())
}
/// Removes expired entries from `blacklist_map` and `blacklist6_map`.
///
/// The packet path can only expire the exact `/32` or `/128` key it looked
/// up, so a ban on a wider prefix (for example a `/24`) stays resident and
/// keeps blocking sources until it is deleted here. Call periodically.
///
/// Expiry semantics are the kernel's: an entry is alive while
/// `expiry > bpf_ktime_get_ns()`. There is no "permanent" sentinel; `0` is
/// already expired and is deleted, while a permanent ban is stored as
/// `u64::MAX` and naturally survives every comparison. Keys are collected
/// before deletion because the LPM iterator is not stable across deletes,
/// and the expiry is re-read immediately before the delete so a
/// concurrent refresh wins over the reaper.
///
/// # Errors
/// XDP not loaded, a blacklist map is missing, or a BPF lookup/delete fails.
pub fn reap_expired_bans(&self) -> Result<usize> {
let now = monotonic_ns();
let reaped = self.reap_map("blacklist_map", now)? + self.reap_map("blacklist6_map", now)?;
Ok(reaped)
}
fn reap_map(&self, name: &str, now: u64) -> Result<usize> {
let map = self.find_map(name)?;
let keys: Vec<Vec<u8>> = map.keys().collect();
let mut reaped = 0;
for key in keys {
let Some(value) = map.lookup(&key, MapFlags::ANY)? else {
continue;
};
let Ok(raw) = <[u8; 8]>::try_from(value.as_slice()) else {
continue;
};
if u64::from_le_bytes(raw) > now {
continue;
}
// A ban may have been refreshed after the first read; never delete a fresh one.
let Some(value) = map.lookup(&key, MapFlags::ANY)? else {
continue;
};
let Ok(raw) = <[u8; 8]>::try_from(value.as_slice()) else {
continue;
};
if u64::from_le_bytes(raw) > monotonic_ns() {
continue;
}
map.delete(&key)?;
reaped += 1;
}
Ok(reaped)
}
pub fn unban_ip(&self, ip: Ipv4Addr) -> Result<()> {
let map = self.find_map("blacklist_map")?;
let key = blacklist_key(32, &ip.octets());
@ -81,25 +247,3 @@ impl XdpFilter {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn blacklist_key_puts_prefix_len_first_and_octets_last() {
assert_eq!(blacklist_key(24, &[10, 0, 1, 200]), [24, 0, 0, 0, 10, 0, 1, 200]);
assert_eq!(blacklist_key(32, &[127, 0, 0, 1]), [32, 0, 0, 0, 127, 0, 0, 1]);
}
#[test]
fn blacklist_expiry_converts_secs_to_ns() {
assert_eq!(blacklist_expiry_ns(1_000, 2), 1_000 + 2_000_000_000);
}
#[test]
fn blacklist_expiry_saturates_instead_of_wrapping() {
assert_eq!(blacklist_expiry_ns(u64::MAX, 1), u64::MAX);
assert_eq!(blacklist_expiry_ns(0, u64::MAX), u64::MAX);
}
}

View file

@ -1,27 +1,32 @@
use anyhow::{Context, Result, bail};
use libbpf_rs::{MapCore, MapFlags, Object, ObjectBuilder, RingBuffer, Xdp, XdpFlags};
use anyhow::{Context, Result};
use libbpf_rs::{MapCore, Object, ObjectBuilder, RingBuffer, Xdp, XdpFlags};
use std::os::unix::io::AsFd;
use std::sync::Arc;
use super::XdpStats;
use super::globals::XdpGlobals;
use crate::config::XdpConfig;
use crate::filter::blacklist::Blacklist;
use crate::traffic::prefix::{PrefixKey, PrefixStatsVal};
use attach::{build_ringbuf, interface_cstr, patch_rodata, xdp_attach_mode_name};
use events::AutoBanConfig;
use attach::{attach_program, patch_rodata};
use events::{AutoBanConfig, build_ringbuf};
mod attach;
pub(crate) mod events;
mod maps;
pub use maps::{blacklist6_key, monotonic_ns};
pub use attach::{interface_cstr, is_rodata_map, xdp_attach_mode_name};
pub use maps::{blacklist_key, blacklist6_key, normalize_cidr, whitelist_key, whitelist6_key};
pub struct XdpFilter {
obj: Option<Object>,
ringbuf: Option<RingBuffer<'static>>,
ifindex: i32,
/// Raw `XdpFlags` bits of the slot the program was actually attached to
/// (libbpf-rs's `XdpFlags` is neither `Copy` nor `Clone`, so the bits are
/// stored and rebuilt with `from_bits_truncate`). The kernel may pick
/// generic (skb) mode after a failed native attach; detaching the native
/// slot in that case would leave an orphaned filter behind.
attach_flags: Option<u32>,
interface: String,
globals: XdpGlobals,
blacklist: Option<Arc<Blacklist>>,
@ -34,6 +39,7 @@ impl XdpFilter {
obj: None,
ringbuf: None,
ifindex: 0,
attach_flags: None,
interface: interface.to_string(),
globals: XdpGlobals::default(),
blacklist: None,
@ -56,6 +62,16 @@ impl XdpFilter {
self.autoban = AutoBanConfig::from_xdp(config);
}
/// Loads the object, attaches it and registers the ringbuf reader.
///
/// Fallible setup (`build_ringbuf`) runs before the attach, so a failure
/// there cannot leave a filter behind. Everything after the attach is
/// covered by an `attach::DetachGuard`: any early return detaches instead
/// of leaving an orphaned program that drops traffic without a reader.
///
/// # Errors
/// The environment probe, object load, map setup or attach failed. On an
/// attach failure no program is left installed.
pub fn load(&mut self) -> Result<()> {
// Diagnostics before loading: fail fast on an unsupported kernel or driver.
super::probe::preflight(&self.interface)?;
@ -80,68 +96,65 @@ impl XdpFilter {
let ifindex = unsafe { libc::if_nametoindex(c_interface.as_ptr()) };
if ifindex == 0 {
let err = std::io::Error::last_os_error();
bail!("interface '{}' not found: {err}", self.interface);
anyhow::bail!("interface '{}' not found: {err}", self.interface);
}
let prog = obj
.progs()
.find(|p| p.name() == "rampart_universal_filter")
.context("XDP program 'rampart_universal_filter' not found")?;
let xdp = Xdp::new(prog.as_fd());
// Mode ladder: try native (drv) first; if the driver cannot handle it, fall back to
// generic (skb) with a warning (finding #1 of the competitive analysis: report the
// mode as it is, no window dressing).
let ifindex = ifindex as i32;
if let Err(e) = xdp.attach(ifindex, XdpFlags::NONE) {
tracing::warn!(
interface = %self.interface,
error = %e,
"native XDP attach failed, falling back to generic (skb) mode"
);
xdp.attach(ifindex, XdpFlags::SKB_MODE)
.context("XDP attach failed in both native and generic modes")?;
}
let mode = xdp
.query(ifindex, XdpFlags::NONE)
.map(|q| xdp_attach_mode_name(q.attach_mode))
.unwrap_or("unknown");
// The ringbuf is the only fallible post-attach step; building it first
// removes the failure mode entirely instead of relying on rollback.
super::metrics::init();
let rbuf = build_ringbuf(&obj, self.blacklist.clone(), self.autoban)?;
let (attach_flags, mode, guard) = attach_program(&obj, ifindex as i32, &self.interface)?;
guard.disarm();
self.obj = Some(obj);
self.ringbuf = Some(rbuf);
self.ifindex = ifindex;
self.ifindex = ifindex as i32;
self.attach_flags = Some(attach_flags);
tracing::info!("XDP filter attached to {} (mode: {mode})", self.interface);
Ok(())
}
/// Detaches the program from the interface and drops the ringbuf.
///
/// The stored attach flags select the slot the kernel actually used, and
/// the interface is queried afterwards: a detach that silently did
/// nothing (the old `XdpFlags::NONE` on a generic-mode program) must not
/// be reported as success.
///
/// # Errors
/// The detach failed or the interface still reports an attached program.
pub fn unload(&mut self) -> Result<()> {
let was_attached = self.obj.is_some();
let mut result = Ok(());
if let Some(obj) = self.obj.as_ref() {
result = self.detach_program(obj);
}
let Some(obj) = self.obj.as_ref() else {
self.ringbuf = None;
self.ifindex = 0;
self.attach_flags = None;
return Ok(());
};
let Some(flags) = self.attach_flags else {
anyhow::bail!("XDP state is inconsistent: object present without attach flags");
};
let result = self.detach_program(obj, flags);
self.ringbuf = None;
self.obj = None;
self.ifindex = 0;
if was_attached {
if result.is_ok() {
self.obj = None;
self.ifindex = 0;
self.attach_flags = None;
tracing::info!("XDP filter detached from {}", self.interface);
}
result
}
/// Detaches the XDP program using the real program fd owned by the loaded
/// `Object` (mirroring `load`'s `Xdp::new(prog.as_fd()).attach(..)`). A
/// detach failure while a filter is active is surfaced loudly and returned,
/// never swallowed — otherwise the kernel keeps dropping packets we no
/// longer intend to filter.
fn detach_program(&self, obj: &Object) -> Result<()> {
/// `Object`. A detach failure while a filter is active is surfaced loudly
/// and returned, never swallowed — otherwise the kernel keeps dropping
/// packets we no longer intend to filter.
fn detach_program(&self, obj: &Object, flags: u32) -> Result<()> {
let prog = obj
.progs()
.find(|p| p.name() == "rampart_universal_filter")
.context("XDP program 'rampart_universal_filter' not found while detaching")?;
if let Err(e) = Xdp::new(prog.as_fd()).detach(self.ifindex, XdpFlags::NONE) {
let xdp = Xdp::new(prog.as_fd());
if let Err(e) = xdp.detach(self.ifindex, XdpFlags::from_bits_truncate(flags)) {
tracing::error!(
interface = %self.interface,
ifindex = self.ifindex,
@ -150,6 +163,19 @@ impl XdpFilter {
);
return Err(e).with_context(|| format!("failed to detach XDP from '{}'", self.interface));
}
// Query every mode (`NONE`), not just the one we detached: only an
// attach mode of NONE proves the interface is clean.
let remaining = xdp
.query(self.ifindex, XdpFlags::NONE)
.context("failed to verify the XDP detach (query)")?
.attach_mode;
if remaining != 0 {
let mode = xdp_attach_mode_name(remaining);
anyhow::bail!(
"XDP program is still attached to '{}' after detach (mode: {mode})",
self.interface
);
}
Ok(())
}
@ -159,7 +185,7 @@ impl XdpFilter {
}
}
fn find_map<'a>(&'a self, name: &str) -> Result<impl MapCore + 'a> {
pub(crate) fn find_map<'a>(&'a self, name: &str) -> Result<impl MapCore + 'a> {
self.obj
.as_ref()
.context("XDP not loaded")?
@ -167,53 +193,6 @@ impl XdpFilter {
.find(|m| m.name() == name)
.with_context(|| format!("map '{}' not found", name))
}
/// Reads the `prefix_stats` map (aggregates per /24 and /64 prefix).
///
/// # Errors
/// XDP is not loaded, the map is missing from the object, or a BPF read fails.
pub fn read_prefix_stats(&self) -> Result<Vec<(PrefixKey, PrefixStatsVal)>> {
let map = self.find_map("prefix_stats")?;
let mut out = Vec::new();
for key in map.keys() {
let Ok(kb) = <[u8; 24]>::try_from(key.as_slice()) else {
continue;
};
let Some(val) = map.lookup(&kb, MapFlags::ANY).context("prefix_stats lookup failed")? else {
continue;
};
let Ok(vb) = <[u8; 24]>::try_from(val.as_slice()) else {
continue;
};
out.push((PrefixKey::from_bytes(kb), PrefixStatsVal::from_bytes(vb)));
}
Ok(out)
}
pub fn get_stats(&self) -> Result<XdpStats> {
let map = self.find_map("stats_map")?;
let sum = |idx: u32| -> u64 {
let key = idx.to_le_bytes();
match map.lookup(&key, MapFlags::ANY) {
Ok(Some(v)) => v
.chunks_exact(8)
.map(|c| u64::from_le_bytes(c.try_into().expect("chunk size 8")))
.sum(),
_ => 0,
}
};
Ok(XdpStats {
total: sum(0),
tcp: sum(1),
whitelist: sum(2),
blacklist: sum(3),
syn_throttle: sum(4),
passed: sum(5),
dropped: sum(6),
udp: sum(7),
rate_limit: sum(8),
})
}
}
// SAFETY: `XdpFilter` is only ever accessed from the single thread that owns it,

View file

@ -6,7 +6,9 @@
mod probe;
pub use probe::{
AttachMode, EnvironmentReport, FilesystemProbe, KernelVersion, MIN_KERNEL, SystemProbe, XdpStats,
AttachMode, EnvironmentReport, FilesystemProbe, KernelVersion, MIN_KERNEL, STAT_BLACKLIST, STAT_CHALLENGE_FAILED,
STAT_CHALLENGE_SENT, STAT_CHALLENGE_VERIFIED, STAT_DROP, STAT_FRAGMENT_BYPASS, STAT_PASS, STAT_RATE_LIMIT,
STAT_SYN_THROTTLE, STAT_TCP, STAT_TOTAL, STAT_UDP, STAT_WHITELIST, SystemProbe, XdpStats,
driver_supports_native_xdp,
};
@ -21,18 +23,17 @@ mod metrics;
#[cfg(feature = "xdp")]
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;
pub use filter::{
XdpFilter, blacklist_key, blacklist6_key, interface_cstr, is_rodata_map, normalize_cidr, whitelist_key,
whitelist6_key, xdp_attach_mode_name,
};
#[cfg(feature = "xdp")]
pub use probe::XdpMetrics;
pub use probe::{XdpMetrics, blacklist_expiry_ns, monotonic_ns};
#[cfg(not(feature = "xdp"))]
mod noop;

View file

@ -23,6 +23,12 @@ impl XdpFilter {
Ok(())
}
/// Nothing to reap without the kernel filter; the maintenance loop can call
/// this unconditionally.
pub fn reap_expired_bans(&self) -> Result<usize> {
Ok(0)
}
/// Honest failure: reading `prefix_stats` is not wired up without the `xdp` feature.
///
/// # Errors

30
src/xdp/probe/clock.rs Normal file
View file

@ -0,0 +1,30 @@
//! Monotonic clock shared by the XDP userspace path.
//!
//! The kernel writes `bpf_ktime_get_ns()` values (CLOCK_MONOTONIC nanoseconds
//! since boot) into the ban maps, and the packet path compares expiry
//! against the same clock. Userspace must therefore never use `SystemTime`
//! (epoch nanoseconds, about 50 years larger) for bans.
/// 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))
}
/// Absolute ban expiry on the monotonic clock, saturating so an absurd duration
/// clamps to `u64::MAX` rather than wrapping into the past.
#[must_use]
pub fn blacklist_expiry_ns(now_ns: u64, duration_secs: u64) -> u64 {
now_ns.saturating_add(duration_secs.saturating_mul(1_000_000_000))
}

View file

@ -13,6 +13,10 @@ pub struct XdpMetrics {
dropped: IntGauge,
udp: IntGauge,
rate_limit: IntGauge,
challenge_sent: IntGauge,
challenge_verified: IntGauge,
challenge_failed: IntGauge,
fragment_bypass: IntGauge,
}
impl XdpMetrics {
@ -33,6 +37,13 @@ impl XdpMetrics {
dropped: Self::new_gauge("rampart_xdp_dropped", "Packets dropped by filter")?,
udp: Self::new_gauge("rampart_xdp_udp", "UDP packets processed")?,
rate_limit: Self::new_gauge("rampart_xdp_rate_limit", "Packets rate-limited by window counter")?,
challenge_sent: Self::new_gauge("rampart_xdp_challenge_sent", "RST challenges sent")?,
challenge_verified: Self::new_gauge("rampart_xdp_challenge_verified", "RST challenges verified")?,
challenge_failed: Self::new_gauge("rampart_xdp_challenge_failed", "RST challenges failed")?,
fragment_bypass: Self::new_gauge(
"rampart_xdp_fragment_bypass",
"Packets bypassing L4 checks as fragments",
)?,
};
tracing::info!("XDP Prometheus metrics registered");
Ok(m)
@ -48,6 +59,10 @@ impl XdpMetrics {
self.dropped.set(stats.dropped as i64);
self.udp.set(stats.udp as i64);
self.rate_limit.set(stats.rate_limit as i64);
self.challenge_sent.set(stats.challenge_sent as i64);
self.challenge_verified.set(stats.challenge_verified as i64);
self.challenge_failed.set(stats.challenge_failed as i64);
self.fragment_bypass.set(stats.fragment_bypass as i64);
tracing::debug!("XDP metrics updated");
}
}

View file

@ -1,4 +1,4 @@
//! XDP environment probing: diagnostics, statistics and metrics.
//! XDP environment probing: diagnostics, statistics, metrics and the shared clock.
mod diagnostics;
#[cfg(feature = "xdp")]
@ -7,8 +7,17 @@ pub use diagnostics::{
AttachMode, EnvironmentReport, FilesystemProbe, KernelVersion, MIN_KERNEL, SystemProbe, driver_supports_native_xdp,
};
#[cfg(feature = "xdp")]
mod clock;
#[cfg(feature = "xdp")]
pub use clock::{blacklist_expiry_ns, monotonic_ns};
mod stats;
pub use stats::XdpStats;
pub use stats::{
STAT_BLACKLIST, STAT_CHALLENGE_FAILED, STAT_CHALLENGE_SENT, STAT_CHALLENGE_VERIFIED, STAT_DROP,
STAT_FRAGMENT_BYPASS, STAT_PASS, STAT_RATE_LIMIT, STAT_SYN_THROTTLE, STAT_TCP, STAT_TOTAL, STAT_UDP,
STAT_WHITELIST, XdpStats,
};
#[cfg(feature = "xdp")]
mod metrics;

View file

@ -1,3 +1,47 @@
//! XDP program counters and the readers for the kernel statistics maps.
//!
//! `stats_map` is a `BPF_MAP_TYPE_PERCPU_ARRAY` keyed by the `STAT_*` indices
//! from `xdp/core/maps.h`; every element holds one `__u64` per CPU, so a
//! lookup returns a blob whose 8-byte chunks must be summed.
//! `prefix_stats` is a plain LRU hash read as whole key/value records.
#[cfg(feature = "xdp")]
use anyhow::{Context, Result};
#[cfg(feature = "xdp")]
use libbpf_rs::{MapCore, MapFlags};
#[cfg(feature = "xdp")]
use crate::traffic::prefix::{PrefixKey, PrefixStatsVal};
#[cfg(feature = "xdp")]
use crate::xdp::XdpFilter;
/// `STAT_TOTAL`: every packet that reached the filter.
pub const STAT_TOTAL: u32 = 0;
/// `STAT_TCP`: TCP packets seen.
pub const STAT_TCP: u32 = 1;
/// `STAT_WHITELIST`: packets passed by the whitelist check.
pub const STAT_WHITELIST: u32 = 2;
/// `STAT_BLACKLIST`: packets dropped by the blacklist check.
pub const STAT_BLACKLIST: u32 = 3;
/// `STAT_SYN_THROTTLE`: SYNs dropped by the per-source throttle.
pub const STAT_SYN_THROTTLE: u32 = 4;
/// `STAT_PASS`: packets passed to the kernel stack.
pub const STAT_PASS: u32 = 5;
/// `STAT_DROP`: packets dropped by any rule.
pub const STAT_DROP: u32 = 6;
/// `STAT_UDP`: UDP packets seen.
pub const STAT_UDP: u32 = 7;
/// `STAT_RATE_LIMIT`: packets dropped by a rate limiter.
pub const STAT_RATE_LIMIT: u32 = 8;
/// `STAT_CHALLENGE_SENT`: RST challenges sent to suspicious sources.
pub const STAT_CHALLENGE_SENT: u32 = 9;
/// `STAT_CHALLENGE_VERIFIED`: sources that answered a challenge correctly.
pub const STAT_CHALLENGE_VERIFIED: u32 = 10;
/// `STAT_CHALLENGE_FAILED`: sources that failed a challenge.
pub const STAT_CHALLENGE_FAILED: u32 = 11;
/// `STAT_FRAGMENT_BYPASS`: packets passed because their L4 header is unavailable (fragments).
pub const STAT_FRAGMENT_BYPASS: u32 = 12;
/// XDP program counters; the indices follow `STAT_*` from `xdp/core/maps.h`.
#[derive(Debug, Clone, Copy, Default)]
pub struct XdpStats {
@ -10,4 +54,67 @@ pub struct XdpStats {
pub dropped: u64,
pub udp: u64,
pub rate_limit: u64,
pub challenge_sent: u64,
pub challenge_verified: u64,
pub challenge_failed: u64,
pub fragment_bypass: u64,
}
#[cfg(feature = "xdp")]
impl XdpFilter {
/// Reads `stats_map` and sums the per-CPU values.
///
/// # Errors
/// XDP is not loaded, or the map is missing from the object (individual
/// lookup failures are reported as zero rather than aborting the report).
pub fn get_stats(&self) -> Result<XdpStats> {
let map = self.find_map("stats_map")?;
let sum = |idx: u32| -> u64 {
let key = idx.to_le_bytes();
match map.lookup(&key, MapFlags::ANY) {
Ok(Some(v)) => v
.chunks_exact(8)
.map(|c| u64::from_le_bytes(c.try_into().expect("chunk size 8")))
.sum(),
_ => 0,
}
};
Ok(XdpStats {
total: sum(STAT_TOTAL),
tcp: sum(STAT_TCP),
whitelist: sum(STAT_WHITELIST),
blacklist: sum(STAT_BLACKLIST),
syn_throttle: sum(STAT_SYN_THROTTLE),
passed: sum(STAT_PASS),
dropped: sum(STAT_DROP),
udp: sum(STAT_UDP),
rate_limit: sum(STAT_RATE_LIMIT),
challenge_sent: sum(STAT_CHALLENGE_SENT),
challenge_verified: sum(STAT_CHALLENGE_VERIFIED),
challenge_failed: sum(STAT_CHALLENGE_FAILED),
fragment_bypass: sum(STAT_FRAGMENT_BYPASS),
})
}
/// Reads the `prefix_stats` map (aggregates per /24 and /64 prefix).
///
/// # Errors
/// XDP is not loaded, the map is missing from the object, or a BPF read fails.
pub fn read_prefix_stats(&self) -> Result<Vec<(PrefixKey, PrefixStatsVal)>> {
let map = self.find_map("prefix_stats")?;
let mut out = Vec::new();
for key in map.keys() {
let Ok(kb) = <[u8; 24]>::try_from(key.as_slice()) else {
continue;
};
let Some(val) = map.lookup(&kb, MapFlags::ANY).context("prefix_stats lookup failed")? else {
continue;
};
let Ok(vb) = <[u8; 24]>::try_from(val.as_slice()) else {
continue;
};
out.push((PrefixKey::from_bytes(kb), PrefixStatsVal::from_bytes(vb)));
}
Ok(out)
}
}