fix(xdp): ban expiry on CLOCK_MONOTONIC; per-protocol L4 minimum

- 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.
This commit is contained in:
Loki 2026-11-15 23:08:05 +01:00
parent 59d5a088b2
commit 295ab81fb4
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
7 changed files with 173 additions and 78 deletions

29
tests/xdp_ban_clock.rs Normal file
View 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}"
);
}