feat(xdp): IPv6 parity, monotonic ban expiry, per-protocol L4 minimum
IPv6 parity: - blacklist6_map (LPM trie, 20-byte key) and is_blacklisted6(); the native_v6 path now passes the port gate and the L4 checks instead of returning XDP_PASS early. - ban_cidr() covers V4 and V6; gateway bans by /32 or /128; subnet monitor bans v6 /64 prefixes; ban_ip() removed (dead code is a bug). - v6 TCP feeds prefix stats through prefix_key_from_v6 (subnet detector now sees IPv6); UDP policy=drop and detect_tcp_bypass apply to v6. - tests/xdp_ban_v6.rs and scripts/xdp_ipv6_smoke.sh (real netns/verifier). Bug fixes (both families): - Ban expiry compared SystemTime epoch-ns against bpf_ktime_get_ns(), so bans never expired. Userspace now writes CLOCK_MONOTONIC via xdp::monotonic_ns(); regression in tests/xdp_ban_clock.rs. - The L4 gate required sizeof(struct tcphdr) for every protocol, so short UDP datagrams bypassed the port gate and the blacklist. The minimum is now per-protocol (UDP 8 bytes); smoke covers it.
This commit is contained in:
parent
a90f0da226
commit
b64c329125
12 changed files with 420 additions and 36 deletions
29
tests/xdp_ban_clock.rs
Normal file
29
tests/xdp_ban_clock.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
//! Часы бана: XDP сравнивает expiry с `bpf_ktime_get_ns()` (монотонные ns
|
||||
//! с загрузки), поэтому userspace обязан писать то же самое. Регрессия:
|
||||
//! раньше писался `SystemTime` (epoch ns ≈ 1.7e18), и бан не истекал никогда.
|
||||
#![cfg(feature = "xdp")]
|
||||
|
||||
use rampart::xdp::monotonic_ns;
|
||||
|
||||
fn clock_monotonic_ns() -> u64 {
|
||||
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
|
||||
// SAFETY: `ts` — валидный writable `timespec`; CLOCK_MONOTONIC на Linux
|
||||
// поддерживается всегда (xdp-фича собирается только под Linux).
|
||||
let rc = unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) };
|
||||
assert_eq!(rc, 0, "clock_gettime(CLOCK_MONOTONIC) failed");
|
||||
let secs = u64::try_from(ts.tv_sec).expect("tv_sec fits u64");
|
||||
let nanos = u64::try_from(ts.tv_nsec).expect("tv_nsec fits u64");
|
||||
secs * 1_000_000_000 + nanos
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ban_clock_is_monotonic_not_wall_clock() {
|
||||
let expected = clock_monotonic_ns();
|
||||
let got = monotonic_ns();
|
||||
|
||||
let diff = got.abs_diff(expected);
|
||||
assert!(
|
||||
diff < 5_000_000_000,
|
||||
"monotonic_ns must read CLOCK_MONOTONIC: got {got}, expected ~{expected}"
|
||||
);
|
||||
}
|
||||
38
tests/xdp_ban_v6.rs
Normal file
38
tests/xdp_ban_v6.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
//! Раскладка ключа `blacklist6_map` (LPM-trie): префикс-длина в байтах 0..4
|
||||
//! little-endian + 16 байт адреса в network order (байты 4..20).
|
||||
//! Ядро не нужно: проверяется чистая функция кодирования ключа.
|
||||
#![cfg(feature = "xdp")]
|
||||
|
||||
use rampart::xdp::blacklist6_key;
|
||||
|
||||
const ADDR_2001_DB8_1: [u8; 16] = [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
|
||||
|
||||
#[test]
|
||||
fn key_layout_for_128_prefix() {
|
||||
let key = blacklist6_key(128, &ADDR_2001_DB8_1);
|
||||
|
||||
assert_eq!(key.len(), 20);
|
||||
assert_eq!(key[0], 128, "prefixlen goes first");
|
||||
assert_eq!(key[1..4], [0, 0, 0], "u32 LE padding stays zero");
|
||||
assert_eq!(key[4..20], ADDR_2001_DB8_1, "address kept in network order");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_layout_for_64_prefix() {
|
||||
let key = blacklist6_key(64, &ADDR_2001_DB8_1);
|
||||
|
||||
assert_eq!(key[0], 64);
|
||||
assert_eq!(key[1..4], [0, 0, 0]);
|
||||
assert_eq!(key[4..20], ADDR_2001_DB8_1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leading_zero_address_is_not_confused_with_prefix_len() {
|
||||
// ::1 — 15 нулевых байт перед единицей: ключ не должен «съесть» ни одного.
|
||||
let key = blacklist6_key(128, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
|
||||
|
||||
assert_eq!(key[0], 128, "prefixlen is not overwritten by the address");
|
||||
assert_eq!(key[1..4], [0, 0, 0]);
|
||||
assert_eq!(key[4..19], [0u8; 15]);
|
||||
assert_eq!(key[19], 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue