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

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"));
}