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:
parent
a8a3057719
commit
14dcaccdbf
27 changed files with 1578 additions and 408 deletions
72
tests/xdp_attach_smoke.rs
Normal file
72
tests/xdp_attach_smoke.rs
Normal 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");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue