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).
41 lines
1.4 KiB
Rust
41 lines
1.4 KiB
Rust
//! 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");
|
|
}
|