- userspace wrote epoch ns while the kernel compares ban expiry against bpf_ktime_get_ns(), so gateway/detector bans never expired (v4 and v6). xdp::monotonic_ns() now reads CLOCK_MONOTONIC. - universal_filter.c required sizeof(tcphdr) (20 bytes) of L4 for every protocol, so short UDP datagrams took an early XDP_PASS and bypassed the blacklist (v4 and v6). Minimum now depends on the protocol. - tests/xdp_ban_clock.rs guards the clock choice. - xdp_ipv6_smoke.sh: v6 pass/ban/unban, short datagrams, v4 short datagrams and real 2s ban expiry on a live kernel.
29 lines
1.2 KiB
Rust
29 lines
1.2 KiB
Rust
//! Часы бана: 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}"
|
||
);
|
||
}
|