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