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

72
tests/xdp_attach_smoke.rs Normal file
View file

@ -0,0 +1,72 @@
//! Opt-in live attach check for the XDP loader.
//!
//! Ignored by default: it needs root (CAP_BPF/CAP_NET_ADMIN) and a disposable
//! interface, best inside a throwaway network namespace. Run it through
//! `/tmp/opencode/loaderchk.sh` (creates a veth pair in a netns) or manually:
//!
//! ```text
//! sudo ip netns exec <ns> env RAMPART_XDP_TEST_IFACE=veth-test \
//! target/debug/deps/xdp_attach_smoke-<hash> --ignored --nocapture
//! ```
//!
//! It covers the regressions this suite is about: the attach mode is
//! remembered for detach (a generic-mode attach must not leave an orphaned
//! filter behind), whitelist entries load into both LPM tries, a live ban
//! survives the reaper, an expired prefix ban is reaped, and `unload` reports
//! a clean interface.
#![cfg(feature = "xdp")]
use std::net::IpAddr;
use rampart::xdp::XdpFilter;
fn ip(text: &str) -> IpAddr {
text.parse().expect("test literal must be a valid address")
}
#[test]
#[ignore = "needs root/CAP_NET_ADMIN and a disposable interface in a network namespace"]
fn attach_ban_whitelist_reap_and_detach() {
let interface = std::env::var("RAMPART_XDP_TEST_IFACE")
.expect("set RAMPART_XDP_TEST_IFACE to a disposable interface (inside a netns)");
let mut filter = XdpFilter::new(&interface);
filter.load().expect("XDP load and attach must succeed");
filter
.whitelist_add(ip("192.0.2.7"))
.expect("IPv4 whitelist entry must load into whitelist_map");
filter
.whitelist_add(ip("2001:db8::7"))
.expect("IPv6 whitelist entry must load into whitelist6_map");
// A live prefix ban must not be removed by the periodic reaper.
filter
.ban_cidr(ip("10.1.2.3"), 24, 600)
.expect("CIDR ban must be accepted");
assert_eq!(
filter.reap_expired_bans().expect("reaper must run"),
0,
"a ban that expires in 10 minutes must survive the reaper"
);
// A zero-duration ban is already expired on the monotonic clock.
filter
.ban_cidr(ip("10.9.8.7"), 24, 0)
.expect("zero-duration ban must be accepted");
assert!(
filter.reap_expired_bans().expect("reaper must run") >= 1,
"the expired prefix ban must be removed"
);
// unload() uses the remembered attach flags and verifies mode NONE.
filter
.unload()
.expect("detach must succeed and leave the interface clean");
drop(filter);
// A second attach proves no orphaned program remained on the interface.
let mut again = XdpFilter::new(&interface);
again.load().expect("re-attach on the same interface must succeed");
again.unload().expect("second detach must succeed");
}

100
tests/xdp_ban_keys.rs Normal file
View file

@ -0,0 +1,100 @@
//! Ban-key encoding for the IPv4 LPM trie and CIDR normalization.
//!
//! No kernel needed: these exercise the pure functions the loader uses before
//! touching `blacklist_map`. Regression class: two bans of the same /24 with
//! different host bits used to produce distinct LPM keys, and a prefix length
//! of 0 or above the address width was accepted silently.
#![cfg(feature = "xdp")]
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use rampart::xdp::{blacklist_expiry_ns, blacklist_key, normalize_cidr};
#[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_key_encodes_prefix_len_as_little_endian_u32() {
let key = blacklist_key(8, &[10, 1, 2, 3]);
assert_eq!(u32::from_le_bytes(key[0..4].try_into().expect("4 bytes")), 8);
}
#[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);
}
#[test]
fn normalize_cidr_masks_host_bits_for_a_slash_24() {
let net = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 24).expect("valid /24");
assert_eq!(net, IpAddr::V4(Ipv4Addr::new(10, 1, 2, 0)));
}
#[test]
fn without_masking_host_bits_would_produce_different_ban_keys() {
// This is the regression: these two keys denote the same /24 but differ.
assert_ne!(blacklist_key(24, &[10, 1, 2, 3]), blacklist_key(24, &[10, 1, 2, 0]));
}
#[test]
fn a_whole_subnet_shares_one_ban_key_after_normalization() {
let first = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 24).expect("valid /24");
let second = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 99)), 24).expect("valid /24");
let (IpAddr::V4(first), IpAddr::V4(second)) = (first, second) else {
return;
};
assert_eq!(blacklist_key(24, &first.octets()), blacklist_key(24, &second.octets()));
}
#[test]
fn normalize_cidr_keeps_slash_32_unchanged() {
let addr = IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3));
assert_eq!(normalize_cidr(addr, 32).expect("valid /32"), addr);
}
#[test]
fn normalize_cidr_masks_host_bits_for_ipv6() {
let addr = IpAddr::V6("2001:db8:1:2:3:4:5:6".parse::<Ipv6Addr>().expect("literal"));
let net = normalize_cidr(addr, 64).expect("valid /64");
assert_eq!(net, IpAddr::V6("2001:db8:1:2::".parse::<Ipv6Addr>().expect("literal")));
}
#[test]
fn normalize_cidr_masks_partial_bytes() {
let net = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 255)), 20).expect("valid /20");
assert_eq!(net, IpAddr::V4(Ipv4Addr::new(10, 1, 0, 0)));
}
#[test]
fn normalize_cidr_keeps_slash_128_unchanged() {
let addr = IpAddr::V6("2001:db8::1".parse::<Ipv6Addr>().expect("literal"));
assert_eq!(normalize_cidr(addr, 128).expect("valid /128"), addr);
}
#[test]
fn normalize_cidr_rejects_prefix_zero() {
let err = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 0).expect_err("prefix 0 is invalid");
assert!(err.to_string().contains("invalid IPv4 prefix length 0"));
}
#[test]
fn normalize_cidr_rejects_ipv4_prefix_above_32() {
let err = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 33).expect_err("prefix 33 is invalid");
assert!(err.to_string().contains("invalid IPv4 prefix length 33"));
}
#[test]
fn normalize_cidr_rejects_ipv6_prefix_above_128() {
let addr = IpAddr::V6("2001:db8::1".parse::<Ipv6Addr>().expect("literal"));
let err = normalize_cidr(addr, 129).expect_err("prefix 129 is invalid");
assert!(err.to_string().contains("invalid IPv6 prefix length 129"));
}

41
tests/xdp_loader.rs Normal file
View file

@ -0,0 +1,41 @@
//! Pure helpers of the XDP loader: interface-name conversion, `.rodata` map
//! naming and the honest attach-mode names.
//!
//! These used to be inline tests in `src/xdp/filter/attach.rs`. They need no
//! kernel; the real attach path is exercised by the netns smoke scripts.
#![cfg(feature = "xdp")]
use rampart::xdp::{interface_cstr, is_rodata_map, xdp_attach_mode_name};
#[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() {
// Kernel order (linux/netlink.h): NONE=0, DRV=1, SKB=2, HW=3, MULTI=4.
assert_eq!(xdp_attach_mode_name(1), "native (drv)");
assert_eq!(xdp_attach_mode_name(2), "generic (skb)");
assert_eq!(xdp_attach_mode_name(3), "hardware offload");
assert_eq!(xdp_attach_mode_name(0), "none");
assert_eq!(xdp_attach_mode_name(4), "multiple programs");
}

View file

@ -29,6 +29,7 @@ fn every_map_the_loader_uses_exists_in_the_object() {
"blacklist_map",
"blacklist6_map",
"whitelist_map",
"whitelist6_map",
"udp_rate_limit",
"udp_rate_limit6",
] {
@ -56,3 +57,34 @@ fn rodata_map_value_size_matches_loader_image() {
"sizeof(struct rampart_config) in config.h must equal sizeof(XdpGlobals)"
);
}
#[test]
fn map_key_and_value_sizes_match_the_rust_encoding() {
let obj = open_object();
// (map, key size, value size) as encoded by src/xdp/filter/maps.rs and
// src/xdp/probe/stats.rs. The C structs come from xdp/core/common.h,
// xdp/core/prefix_stats.h and xdp/core/maps.h.
let expected = [
("blacklist_map", 8, 8),
("blacklist6_map", 20, 8),
("whitelist_map", 8, 1),
("whitelist6_map", 20, 1),
("prefix_stats", 24, 24),
("stats_map", 4, 8),
];
for (name, key_size, value_size) in expected {
let map = obj.maps().find(|m| m.name().to_string_lossy() == name);
assert!(map.is_some(), "map '{name}' is missing from the XDP object");
let map = map.expect("map existence asserted above");
assert_eq!(
map.key_size(),
key_size,
"map '{name}' key size drifted from the Rust encoding"
);
assert_eq!(
map.value_size(),
value_size,
"map '{name}' value size drifted from the Rust encoding"
);
}
}

79
tests/xdp_services.rs Normal file
View file

@ -0,0 +1,79 @@
//! Edge service wiring: whitelist parsing and the XDP startup path.
//!
//! These used to be inline `#[cfg(test)]` tests in `src/app/services.rs`; the
//! project keeps tests out of source files (TODO.md §4). Kernel-touching
//! behaviour is not exercised here: `start_xdp` is called with `[xdp]`
//! disabled, and the whitelist-map load needs a live interface.
use std::net::IpAddr;
use std::sync::Arc;
use rampart::app::services::{build_whitelist, start_xdp};
use rampart::config::{Config, StoreConfig};
use rampart::filter::blacklist::Blacklist;
use tokio::sync::watch;
fn config_with_redis() -> Config {
Config {
store: 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 = build_whitelist(&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!(build_whitelist(&Config::default()).is_ok());
}
#[cfg(feature = "store-redis")]
#[test]
fn redis_config_passes_with_store_feature() {
assert!(build_whitelist(&config_with_redis()).is_ok());
}
#[test]
fn build_whitelist_parses_both_address_families() {
let config = Config {
whitelist: vec!["192.0.2.10".to_string(), "2001:db8::1".to_string()],
..Default::default()
};
let set = build_whitelist(&config).expect("valid whitelist entries");
assert!(set.contains(&"192.0.2.10".parse::<IpAddr>().expect("ipv4 literal")));
assert!(set.contains(&"2001:db8::1".parse::<IpAddr>().expect("ipv6 literal")));
assert_eq!(set.len(), 2);
}
#[test]
fn build_whitelist_rejects_a_malformed_entry() {
let config = Config {
whitelist: vec!["definitely-not-an-ip".to_string()],
..Default::default()
};
let err = build_whitelist(&config).expect_err("malformed entry must fail");
assert!(err.to_string().contains("invalid whitelist entry"));
}
/// 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

@ -0,0 +1,42 @@
//! Whitelist LPM key layouts (`whitelist_map` / `whitelist6_map`).
//!
//! The kernel keys are `struct lpm_key` (8 bytes) and `struct lpm_key6`
//! (20 bytes): a little-endian u32 prefix length followed by the address in
//! network order. A drift here silently whitelists the wrong range.
#![cfg(feature = "xdp")]
use rampart::xdp::{whitelist_key, whitelist6_key};
const ADDR_V4: [u8; 4] = [10, 1, 2, 0];
const ADDR_V6_2001_DB8: [u8; 16] = [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
#[test]
fn whitelist_key_is_prefix_len_then_address() {
let key = whitelist_key(24, &ADDR_V4);
assert_eq!(key.len(), 8);
assert_eq!(u32::from_le_bytes(key[0..4].try_into().expect("4 bytes")), 24);
assert_eq!(key[4..8], ADDR_V4, "address kept in network order");
}
#[test]
fn whitelist_key_matches_the_blacklist_layout() {
assert_eq!(whitelist_key(32, &ADDR_V4), rampart::xdp::blacklist_key(32, &ADDR_V4));
}
#[test]
fn whitelist6_key_is_u32_prefix_len_then_16_address_bytes() {
let key = whitelist6_key(64, &ADDR_V6_2001_DB8);
assert_eq!(key.len(), 20);
assert_eq!(u32::from_le_bytes(key[0..4].try_into().expect("4 bytes")), 64);
assert_eq!(key[4..20], ADDR_V6_2001_DB8, "address kept in network order");
}
#[test]
fn whitelist6_key_does_not_confuse_leading_zeros_with_the_prefix_len() {
let key = whitelist6_key(128, &[0u8; 16]);
assert_eq!(u32::from_le_bytes(key[0..4].try_into().expect("4 bytes")), 128);
assert_eq!(key[4..20], [0u8; 16]);
}