From 14dcaccdbfa1b3042edf5e0df7f1b7b84af86dd4 Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Mon, 16 Nov 2026 12:06:05 +0100 Subject: [PATCH] fix(xdp): correct payload length, attach/detach, whitelist and ban upkeep 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). --- docs/testing.md | 14 +- scripts/xdp_ipv6_smoke.sh | 29 +- scripts/xdp_pkt_test.sh | 241 ++++++++++++++++ src/app/services.rs | 82 +++--- src/filter/{blacklist.rs => blacklist/mod.rs} | 0 src/xdp/filter/attach.rs | 242 +++++++++------- src/xdp/filter/events.rs | 73 ++++- src/xdp/filter/maps.rs | 252 +++++++++++++---- src/xdp/filter/mod.rs | 169 +++++------- src/xdp/mod.rs | 15 +- src/xdp/noop.rs | 6 + src/xdp/probe/clock.rs | 30 ++ src/xdp/probe/metrics.rs | 15 + src/xdp/probe/mod.rs | 13 +- src/xdp/probe/stats.rs | 107 ++++++++ tests/xdp_attach_smoke.rs | 72 +++++ tests/xdp_ban_keys.rs | 100 +++++++ tests/xdp_loader.rs | 41 +++ tests/xdp_object_maps.rs | 32 +++ tests/xdp_services.rs | 79 ++++++ tests/xdp_whitelist_keys.rs | 42 +++ xdp/core/common.h | 14 + xdp/core/maps.h | 30 +- xdp/core/stats.h | 7 +- xdp/core/syn_challenge.h | 17 +- xdp/core/universal_filter.c | 259 +++++++++++++----- xdp/hooks/hook_api.h | 5 +- 27 files changed, 1578 insertions(+), 408 deletions(-) create mode 100755 scripts/xdp_pkt_test.sh rename src/filter/{blacklist.rs => blacklist/mod.rs} (100%) create mode 100644 src/xdp/probe/clock.rs create mode 100644 tests/xdp_attach_smoke.rs create mode 100644 tests/xdp_ban_keys.rs create mode 100644 tests/xdp_loader.rs create mode 100644 tests/xdp_services.rs create mode 100644 tests/xdp_whitelist_keys.rs diff --git a/docs/testing.md b/docs/testing.md index b08a62c..9659e80 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -45,8 +45,18 @@ sudo bash scripts/xdp_ipv6_smoke.sh ``` Поднимает netns + veth, грузит программу через verifier и проверяет: бан IPv6 и IPv4 -(включая короткие UDP-датаграммы), снятие бана, истечение TTL по монотонным часам и -`udp_policy = "rate-limit"` для IPv6 (политика патчится в `.rodata` до загрузки). +(включая короткие UDP-датаграммы), снятие бана, whitelist поверх активного бана, истечение +TTL по монотонным часам и `udp_policy = "rate-limit"` для IPv6 (политика патчится в +`.rodata` до загрузки). + +```bash +sudo bash scripts/xdp_pkt_test.sh +``` + +Проверяет то, что veth-смок поймать не может по построению: Ethernet-паддинг (длина +TCP-payload по `ip->tot_len`), VLAN/QinQ-теги и фрагменты — забаненный источник не должен +проходить ни тегированным, ни фрагментированным. + `scripts/xdp_netns_smoke.sh` дополнительно гоняет реальный бинарь `rampart` end-to-end (требует `hping3`). diff --git a/scripts/xdp_ipv6_smoke.sh b/scripts/xdp_ipv6_smoke.sh index eb0ce6e..cb01455 100755 --- a/scripts/xdp_ipv6_smoke.sh +++ b/scripts/xdp_ipv6_smoke.sh @@ -9,7 +9,8 @@ # to fall through to XDP_PASS past the blacklist because the length check # was sized for TCP; # 4. the same holds for IPv4, through the shared L4 gate; -# 5. expiry: a ban with a monotonic TTL really expires in the kernel; it +# 5. a /128 entry in whitelist6_map bypasses an active ban; +# 6. expiry: a ban with a monotonic TTL really expires in the kernel; it # used to live forever because userspace wrote epoch-ns while the C side # compared against bpf_ktime_get_ns(). # @@ -74,19 +75,21 @@ discover_maps() { IDS=$(bpftool prog show pinned "$PIN" | grep -o 'map_ids [0-9,]*' | cut -d' ' -f2 | tr ',' ' ') B4="" B6="" + W6="" ST="" for id in $IDS; do desc=$(bpftool map show id "$id" 2>/dev/null) case "$desc" in *"name blacklist6_map"*) B6=$id ;; *"name blacklist_map"*) B4=$id ;; + *"name whitelist6_map"*) W6=$id ;; *"name stats_map"*) ST=$id ;; esac done - [ -n "$B4" ] && [ -n "$B6" ] && [ -n "$ST" ] + [ -n "$B4" ] && [ -n "$B6" ] && [ -n "$W6" ] && [ -n "$ST" ] } discover_maps || { echo "карты не найдены"; exit 1; } -ok "blacklist_map id=$B4, blacklist6_map id=$B6, stats_map id=$ST" +ok "blacklist_map id=$B4, blacklist6_map id=$B6, whitelist6_map id=$W6, stats_map id=$ST" # LPM keys: prefixlen (u32 LE) followed by the address in network order. KEY6="80 00 00 00 fd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02" @@ -114,7 +117,8 @@ stats() { bpftool -j map dump id "$ST" | python3 -c " import json, sys names = {0: 'total', 1: 'tcp', 2: 'whitelist', 3: 'blacklist', 4: 'syn_thr', - 5: 'pass', 6: 'drop', 7: 'udp', 8: 'rate', 9: 'chal_sent'} + 5: 'pass', 6: 'drop', 7: 'udp', 8: 'rate', 9: 'chal_sent', + 12: 'frag_bypass'} out = {} for e in json.load(sys.stdin): k = names.get(e['formatted']['key']) @@ -175,6 +179,23 @@ drop1=$(metric drop) || fail "v4 короткая датаграмма прошла мимо бана ($(stats))" bpftool map delete id "$B4" key hex $KEY4 || fail "v4 map delete" +# Phase 2b: the IPv6 whitelist wins over an active ban and is consulted before +# the port gate and conntrack, so an allowlisted source truly bypasses +# filtering. +WL6_KEY="80 00 00 00 fd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02" +bpftool map update id "$B6" key hex $KEY6 value hex ff ff ff ff ff ff ff ff || fail "v6 ban for whitelist phase" +bpftool map update id "$W6" key hex $WL6_KEY value hex 01 || fail "whitelist6 map update" +drop0=$(metric drop); total0=$(metric total); wl0=$(metric whitelist) +for _ in 1 2 3; do send6 100; done +sleep 0.5 +drop1=$(metric drop); total1=$(metric total); wl1=$(metric whitelist) +[ "${total1:-0}" -gt "${total0:-0}" ] && [ "${drop1:-0}" -eq "${drop0:-0}" ] \ + && [ "${wl1:-0}" -gt "${wl0:-0}" ] \ + && ok "v6 whitelist обходит активный бан (total $total0 -> $total1, drop $drop0 -> $drop1, whitelist $wl0 -> $wl1)" \ + || fail "v6 whitelist не обходит бан ($(stats))" +bpftool map delete id "$W6" key hex $WL6_KEY || fail "whitelist6 map delete" +bpftool map delete id "$B6" key hex $KEY6 || fail "v6 map delete after whitelist phase" + # Phase 3: ban expiry on the monotonic clock. A TTL written in epoch-ns would # never expire against bpf_ktime_get_ns(), so this phase pins the clock choice. TTL=2 diff --git a/scripts/xdp_pkt_test.sh b/scripts/xdp_pkt_test.sh new file mode 100755 index 0000000..f905a04 --- /dev/null +++ b/scripts/xdp_pkt_test.sh @@ -0,0 +1,241 @@ +#!/usr/bin/env bash +# Packet-level smoke test for L2/L3 edge cases that ordinary sockets cannot +# produce: VLAN/QinQ tags, IPv4 fragments and Ethernet padding. +# +# What it verifies (each phase is a regression for a fix in universal_filter.c): +# 1. VLAN, QinQ and plain datagrams from a banned source are all dropped — +# tags are peeled before L3 and the blacklist still runs; +# 2. first (MF) and non-first fragments from a banned source are dropped by +# the source blacklist; +# 3. fragments from a clean source pass and bump STAT_FRAGMENT_BYPASS; +# 4. TCP payload length comes from ip->tot_len, not from the frame end: a +# segment followed by 16 bytes of Ethernet padding advances expected_seq +# by the 4 declared payload bytes only (read back from conntrack_map). +# +# Requires root/CAP_BPF+CAP_NET_ADMIN, bpftool, clang, python3. +# Run: sudo bash scripts/xdp_pkt_test.sh + +set -u + +NS=rampart_pkt_smoke +DEV0=rthp0 +DEV1=rtapp0 +HOST4=10.88.0.1 +NS4=10.88.0.2 +PORT=4444 +OBJ=$(mktemp /tmp/rampart_pkt_smoke.XXXXXX.o) +PIN=/sys/fs/bpf/rampart_pkt_smoke +WORK=$(mktemp -d /tmp/rampart_pkt_work.XXXXXX) +PASS=1 + +fail() { echo "FAIL: $1"; PASS=0; } +ok() { echo "ok: $1"; } + +# Detach and remove every artefact, so a rerun starts from a clean state. +cleanup() { + bpftool net detach xdp dev "$DEV0" 2>/dev/null + rm -f "$PIN" "$OBJ" + rm -rf "$WORK" + ip link del "$DEV0" 2>/dev/null + ip netns del "$NS" 2>/dev/null +} +trap cleanup EXIT + +[ "$(id -u)" = 0 ] || { echo "нужен root (CAP_BPF+CAP_NET_ADMIN)"; exit 2; } +cd "$(dirname "$0")/.." || exit 2 + +clang -O2 -g -target bpf -mcpu=v3 -c xdp/core/universal_filter.c -o "$OBJ" \ + -Ixdp -Ixdp/core -Ixdp/hooks -I/usr/include/x86_64-linux-gnu \ + || { echo "clang: не собралось"; exit 1; } +ok "clang: universal_filter.c собран" + +ip netns add "$NS" || { echo "не удалось создать netns"; exit 2; } +ip link add "$DEV0" type veth peer name "$DEV1" || exit 2 +ip link set "$DEV1" netns "$NS" +ip addr add "$HOST4/24" dev "$DEV0" +ip link set "$DEV0" up +ip -n "$NS" addr add "$NS4/24" dev "$DEV1" +ip -n "$NS" link set "$DEV1" up +ip -n "$NS" link set lo up +ok "netns $NS + veth ($HOST4 <-> $NS4)" + +# Loading through the verifier is itself a check. +bpftool prog load "$OBJ" "$PIN" type xdp || { echo "verifier отверг программу"; exit 1; } +bpftool net attach xdp pinned "$PIN" dev "$DEV0" || exit 1 +ok "verifier принял программу, attach на $DEV0" + +discover_maps() { + IDS=$(bpftool prog show pinned "$PIN" | grep -o 'map_ids [0-9,]*' | cut -d' ' -f2 | tr ',' ' ') + B4="" + CT="" + ST="" + for id in $IDS; do + desc=$(bpftool map show id "$id" 2>/dev/null) + case "$desc" in + *"name blacklist_map"*) B4=$id ;; + *"name conntrack_map"*) CT=$id ;; + *"name stats_map"*) ST=$id ;; + esac + done + [ -n "$B4" ] && [ -n "$CT" ] && [ -n "$ST" ] +} +discover_maps || { echo "карты не найдены"; exit 1; } +ok "blacklist_map id=$B4, conntrack_map id=$CT, stats_map id=$ST" + +# Crafted-frame sender. argv: iface src dst kind args... +# The frames are emitted with AF_PACKET from inside the namespace, so they +# arrive on DEV0 exactly as a NIC would deliver them. Checksums of TCP/UDP are +# irrelevant: XDP runs before the stack and only the IP header is inspected. +cat > "$WORK/send.py" <<'PY' +import socket, struct, sys + +iface, src, dst, kind = sys.argv[1:5] +SPORT = 40000 + +DST_MAC = bytes.fromhex("020000000001") +SRC_MAC = bytes.fromhex("020000000002") + + +def ip_checksum(hdr): + if len(hdr) % 2: + hdr += b"\0" + s = 0 + for i in range(0, len(hdr), 2): + s += (hdr[i] << 8) | hdr[i + 1] + while s >> 16: + s = (s & 0xFFFF) + (s >> 16) + return (~s) & 0xFFFF + + +def ip4(proto, payload, frag_off=0): + total = 20 + len(payload) + args = (0x45, 0, total, 0x1234, frag_off, 64, proto, 0, + socket.inet_aton(src), socket.inet_aton(dst)) + hdr = struct.pack("!BBHHHBBH4s4s", *args) + args = args[:7] + (ip_checksum(hdr),) + args[8:] + return struct.pack("!BBHHHBBH4s4s", *args) + payload + + +def l2(eth_type, tags): + hdr = DST_MAC + SRC_MAC + if tags >= 1: + hdr += struct.pack("!HH", 0x8100 if tags == 1 else 0x88A8, 100) + if tags >= 2: + hdr += struct.pack("!HH", 0x8100, 200) + return hdr + struct.pack("!H", eth_type) + + +def emit(eth_type, l3, tags, pad): + frame = l2(eth_type, tags) + l3 + if len(frame) < pad: + frame += b"\0" * (pad - len(frame)) + sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) + sock.bind((iface, 0)) + sock.send(frame) + + +def udp(args): + sport, dport, tags, pad = (int(a, 0) for a in args) + data = b"x" * 8 + hdr = struct.pack("!HHHH", sport, dport, 8 + len(data), 0) + emit(0x0800, ip4(17, hdr + data), tags, pad) + + +def frag(args): + sport, dport, offset8, mf = (int(a, 0) for a in args) + data = b"y" * 8 + hdr = struct.pack("!HHHH", sport, dport, 16, 0) + frag_off = (offset8 & 0x1FFF) | (0x2000 if mf else 0) + emit(0x0800, ip4(17, hdr + data, frag_off), 0, 0) + + +def tcp(args): + sport, dport, seq, ack, flags, data_len, pad = (int(a, 0) for a in args) + data = b"z" * data_len + hdr = struct.pack("!HHIIBBHHH", sport, dport, seq, ack, 0x50, flags, + 0xFFFF, 0, 0) + emit(0x0800, ip4(6, hdr + data), 0, pad) + + +{"udp": udp, "frag": frag, "tcp": tcp}[kind](sys.argv[5:]) +PY + +send() { ip netns exec "$NS" python3 "$WORK/send.py" "$DEV1" "$NS4" "$HOST4" "$@"; } + +# Sum the per-CPU counters and print only the non-zero ones. +stats() { + bpftool -j map dump id "$ST" | python3 -c " +import json, sys +names = {0: 'total', 3: 'blacklist', 5: 'pass', 6: 'drop', 12: 'frag_bypass'} +out = {} +for e in json.load(sys.stdin): + k = names.get(e['formatted']['key']) + if k is not None: + out[k] = sum(v['value'] for v in e['formatted']['values']) +print(' '.join(f'{k}={v}' for k, v in out.items() if v) or 'все нули') +" +} +metric() { stats | tr ' ' '\n' | sed -n "s/^$1=//p" | head -1; } + +# /32 LPM key for $NS4 and a ban that never expires in this run. +KEY4="20 00 00 00 0a 58 00 02" +BAN="ff ff ff ff ff ff ff ff" +bpftool map update id "$B4" key hex $KEY4 value hex $BAN || fail "ban $NS4" + +# Phase 1: tagged traffic from a banned source (plain frame as control). +drop0=$(metric drop) +send udp 5000 $PORT 1 0 +send udp 5000 $PORT 2 0 +send udp 5000 $PORT 0 0 +sleep 0.3 +drop1=$(metric drop) +[ "${drop1:-0}" -ge $((${drop0:-0} + 3)) ] \ + && ok "VLAN/QinQ/plain UDP от забаненного источника дропаются (drop $drop0 -> $drop1)" \ + || fail "tagged traffic прошёл мимо бана (drop $drop0 -> $drop1, $(stats))" + +# Phase 2: fragments from a banned source, first (MF) and later ones. +drop0=$(metric drop) +send frag 5001 $PORT 0 1 +send frag 5002 $PORT 1 0 +send frag 5003 $PORT 1 1 +sleep 0.3 +drop1=$(metric drop) +[ "${drop1:-0}" -ge $((${drop0:-0} + 3)) ] \ + && ok "фрагменты (MF и offset>0) от забаненного источника дропаются (drop $drop0 -> $drop1)" \ + || fail "фрагменты прошли мимо бана (drop $drop0 -> $drop1, $(stats))" + +bpftool map delete id "$B4" key hex $KEY4 || fail "unban $NS4" + +# Phase 3: fragments from a clean source pass and are counted separately. +pass0=$(metric pass) +frag0=$(metric frag_bypass) +send frag 5004 $PORT 0 1 +send frag 5005 $PORT 1 0 +send frag 5006 $PORT 2 0 +sleep 0.3 +pass1=$(metric pass) +frag1=$(metric frag_bypass) +[ "${pass1:-0}" -ge $((${pass0:-0} + 3)) ] && [ "${frag1:-0}" -ge $((${frag0:-0} + 3)) ] \ + && ok "чистые фрагменты проходят и считаются (pass $pass0 -> $pass1, frag $frag0 -> $frag1)" \ + || fail "чистые фрагменты не прошли/не посчитались (pass $pass0 -> $pass1, frag $frag0 -> $frag1, $(stats))" + +# Phase 4: a padded frame must not inflate the TCP payload. ISN=1000, so after +# the handshake expected_seq is 1001; a 4-byte payload must leave 1005, not +# 1001 + 4 + 16 padding = 1021. +send tcp 40000 $PORT 1000 0 0x02 0 0 # SYN +send tcp 40000 $PORT 1001 2000 0x10 0 0 # ACK completes the handshake +send tcp 40000 $PORT 1001 2000 0x18 4 74 # 4 payload bytes, 16 pad bytes +sleep 0.3 +got=$(bpftool -j map dump id "$CT" | python3 -c " +import json, sys +for e in json.load(sys.stdin): + v = bytes(int(x, 0) for x in e['value']) + print(int.from_bytes(v[4:8], 'little')) + break +") +[ "${got:-0}" = "1005" ] \ + && ok "expected_seq учёл 4 байта payload, а не 20 байт кадра (expected_seq=$got)" \ + || fail "expected_seq=$got, ожидалось 1005 (паддинг попал в payload)" + +[ "$PASS" = 1 ] && echo "PASS: VLAN/QinQ, фрагменты и паддинг обрабатываются корректно" || echo "FAIL" +[ "$PASS" = 1 ] diff --git a/src/app/services.rs b/src/app/services.rs index 8f75167..69f39cb 100644 --- a/src/app/services.rs +++ b/src/app/services.rs @@ -51,11 +51,12 @@ fn register_http(registry: &mut ProtocolRegistry, config: &Config) { } #[cfg(feature = "xdp")] -pub(crate) fn start_xdp( +pub fn start_xdp( config: &Arc, shutdown_rx: &watch::Receiver, blacklist: &Arc, ) -> anyhow::Result>>> { + use anyhow::Context as _; use std::time::Duration; use crate::xdp::XdpMetrics; @@ -82,12 +83,28 @@ pub(crate) fn start_xdp( ); } guard.load()?; + // The kernel filter only honours its LPM whitelist, so a configured + // `whitelist` that fails to load is a startup error: silently dropping + // the entries would expose trusted peers to the XDP rules. + let whitelist = build_whitelist(config)?; + for ip in whitelist.iter() { + guard + .whitelist_add(*ip) + .with_context(|| format!("failed to load XDP whitelist entry {ip}"))?; + } + tracing::info!( + entries = whitelist.len(), + "configured whitelist loaded into the XDP LPM maps" + ); } let xdp_metrics = XdpMetrics::register()?; let sd = shutdown_rx.clone(); let shared_thread = shared.clone(); std::thread::spawn(move || { + // The loop ticks every 5 s; sweep the ban maps once a minute. + const REAP_EVERY_TICKS: u32 = 12; + let mut tick: u32 = 0; while !*sd.borrow() { let guard = match shared_thread.lock() { Ok(g) => g, @@ -97,6 +114,17 @@ pub(crate) fn start_xdp( if let Ok(stats) = guard.get_stats() { xdp_metrics.update(&stats); } + tick += 1; + if tick >= REAP_EVERY_TICKS { + tick = 0; + // The packet path only expires the exact /32 or /128 key it + // looked up, so wider prefix bans stay resident until swept. + match guard.reap_expired_bans() { + Ok(0) => {}, + Ok(n) => tracing::info!(reaped = n, "expired XDP prefix bans removed"), + Err(e) => tracing::warn!(error = %e, "XDP expired-ban reaper failed"), + } + } drop(guard); std::thread::sleep(Duration::from_secs(5)); } @@ -108,7 +136,7 @@ pub(crate) fn start_xdp( } #[cfg(not(feature = "xdp"))] -pub(crate) fn start_xdp( +pub fn start_xdp( _config: &Arc, _shutdown_rx: &watch::Receiver, _blacklist: &Arc, @@ -153,7 +181,7 @@ fn ensure_store_features(config: &Config) -> anyhow::Result<()> { /// # Errors /// Fails on an invalid entry or when the config requires store features that are /// not compiled in. -pub(crate) fn build_whitelist(config: &Config) -> anyhow::Result>> { +pub fn build_whitelist(config: &Config) -> anyhow::Result>> { ensure_store_features(config)?; let mut set = HashSet::with_capacity(config.whitelist.len()); for entry in &config.whitelist { @@ -164,51 +192,3 @@ pub(crate) fn build_whitelist(config: &Config) -> anyhow::Result Config { - Config { - store: crate::config::StoreConfig { - redis_url: Some("redis://127.0.0.1:6379/0".to_string()), - ..Default::default() - }, - ..Default::default() - } - } - - #[cfg(not(feature = "store-redis"))] - #[test] - fn redis_config_without_feature_fails_fast() { - let err = ensure_store_features(&config_with_redis()).expect_err("must fail without store-redis"); - assert!(err.to_string().contains("built without store-redis feature")); - } - - #[cfg(not(feature = "store-redis"))] - #[test] - fn empty_store_config_passes_without_feature() { - assert!(ensure_store_features(&Config::default()).is_ok()); - } - - #[cfg(feature = "store-redis")] - #[test] - fn guard_is_noop_with_feature() { - assert!(ensure_store_features(&config_with_redis()).is_ok()); - } - - /// XDP auto-ban wiring: `start_xdp` must accept the `Arc` and, with - /// `[xdp]` disabled, touch neither the kernel nor the blacklist. The - /// `set_autoban` -> `load()` order inside the enabled branch cannot be checked - /// without a kernel. - #[test] - fn start_xdp_accepts_blacklist_and_skips_kernel_when_disabled() { - let (_tx, rx) = watch::channel(false); - let blacklist = Arc::new(Blacklist::new()); - let started = start_xdp(&Arc::new(Config::default()), &rx, &blacklist) - .expect("xdp disabled: no kernel work and no error"); - assert!(started.is_none()); - assert!(blacklist.is_empty()); - } -} diff --git a/src/filter/blacklist.rs b/src/filter/blacklist/mod.rs similarity index 100% rename from src/filter/blacklist.rs rename to src/filter/blacklist/mod.rs diff --git a/src/xdp/filter/attach.rs b/src/xdp/filter/attach.rs index cc7f35d..e51230e 100644 --- a/src/xdp/filter/attach.rs +++ b/src/xdp/filter/attach.rs @@ -1,27 +1,25 @@ -use anyhow::{Context, Result, bail}; -use libbpf_rs::{MapCore, Object, OpenObject, RingBuffer, RingBufferBuilder}; use std::ffi::CString; -use std::net::IpAddr; -use std::sync::Arc; -use std::time::Duration; +use std::os::unix::io::AsFd; + +use anyhow::{Context, Result, bail}; +use libbpf_rs::{Object, OpenObject, Program, Xdp, XdpFlags}; -use crate::filter::blacklist::Blacklist; use crate::xdp::globals::{RODATA_MAP_NAME, XdpGlobals}; -use crate::xdp::metrics::XDP_AUTOBANS_TOTAL; -use super::events::{AutoBanConfig, auto_ban_decision, parse_xdp_event}; +/// Name of the XDP program inside the compiled object. +const XDP_PROG_NAME: &str = "rampart_universal_filter"; /// Builds the NUL-terminated C string libc's `if_nametoindex` requires. /// Rejects interface names containing an interior NUL byte instead of /// silently truncating at it (the `&str`-as-`*const c_char` UB we removed). -pub(super) fn interface_cstr(name: &str) -> Result { +pub fn interface_cstr(name: &str) -> Result { CString::new(name).with_context(|| format!("interface name {name:?} contains an interior NUL byte")) } /// The internal `.rodata` data map is matched by suffix: libbpf names it /// `.rodata`, and with `open_memory` and no explicit `.name()` the object is /// called `memory` — an exact comparison broke on a live kernel (smoke test). -pub(super) fn is_rodata_map(name: &str) -> bool { +pub fn is_rodata_map(name: &str) -> bool { name.ends_with(".rodata") } @@ -57,108 +55,148 @@ pub(super) fn patch_rodata(open_obj: &mut OpenObject, globals: &XdpGlobals) -> R .with_context(|| format!("failed to set initial value of '{RODATA_MAP_NAME}'")) } +/// `XDP_ATTACHED_*` values from `linux/netlink.h` (`IFLA_XDP_ATTACHED`). +/// +/// The order is NONE, DRV, SKB, HW, MULTI — hardware offload is 3, not 2. +const XDP_ATTACHED_DRV: u8 = 1; +const XDP_ATTACHED_SKB: u8 = 2; +const XDP_ATTACHED_HW: u8 = 3; +const XDP_ATTACHED_MULTI: u8 = 4; + /// Honest name of the attach mode, derived from the `attach_mode` field of -/// `bpf_xdp_query_opts` (kernel `enum xdp_attached_mode`: NONE=0, DRV=1, OFFLOAD=2, SKB=3). -pub(super) fn xdp_attach_mode_name(mode: u8) -> &'static str { +/// `bpf_xdp_query_opts` (kernel `enum xdp_attach_mode`). +pub fn xdp_attach_mode_name(mode: u8) -> &'static str { match mode { - 1 => "native (drv)", - 2 => "hardware offload", - 3 => "generic (skb)", + XDP_ATTACHED_DRV => "native (drv)", + XDP_ATTACHED_SKB => "generic (skb)", + XDP_ATTACHED_HW => "hardware offload", + XDP_ATTACHED_MULTI => "multiple programs", _ => "none", } } -/// Builds the ringbuf callback for `events_ringbuf`. +/// Detach flags matching the mode the kernel actually attached the program in. /// -/// The callback must be `'static`, so all shared state (`Arc`, a copy of -/// `AutoBanConfig`) is captured by `move` clones. When `blacklist` is `None` (feature not -/// wired) or the policy is disabled, events are only parsed and logged in a structured -/// way; no bans are written. The hot path does no `format!` — only `tracing` fields and -/// `&'static str` reasons. -pub(super) fn build_ringbuf( - obj: &Object, - blacklist: Option>, - cfg: AutoBanConfig, -) -> Result> { - let map = obj - .maps() - .find(|m| m.name() == "events_ringbuf") - .context("events_ringbuf not found")?; - let mut builder = RingBufferBuilder::new(); - builder.add(&map, move |data: &[u8]| { - let Some(event) = parse_xdp_event(data) else { - tracing::debug!( - len = data.len(), - "xdp ringbuf frame ignored (too short or unknown type)" - ); - return 0; - }; - tracing::debug!( - kind = ?event.kind, - src_ip = %event.src_ip, - metadata = event.metadata, - "xdp event" +/// The mode is discovered after attach via `bpf_xdp_query`; using the flags +/// that were *requested* is wrong when the kernel picked a different mode +/// (for example generic after a failed native attach). +fn detach_flags_for_mode(mode: u8) -> Option { + match mode { + XDP_ATTACHED_DRV => Some(XdpFlags::DRV_MODE), + XDP_ATTACHED_SKB => Some(XdpFlags::SKB_MODE), + XDP_ATTACHED_HW => Some(XdpFlags::HW_MODE), + XDP_ATTACHED_MULTI => None, + // NONE: the query failed after a successful attach. + _ => None, + } +} + +/// Attaches with the native-first mode ladder and returns the detach flags. +fn attach_with_fallback(xdp: &Xdp<'_>, ifindex: i32, interface: &str) -> Result { + if let Err(e) = xdp.attach(ifindex, XdpFlags::NONE) { + tracing::warn!( + interface = %interface, + error = %e, + "native XDP attach failed, falling back to generic (skb) mode" ); - let Some(blacklist) = &blacklist else { - return 0; - }; - if let Some(ban) = auto_ban_decision(&event, &cfg) { - let addr = IpAddr::V4(ban.ip); - // `Blacklist::add` returns `()` (it ignores `DashMap::insert`) and blacklist.rs is - // out of our edit scope, so "is this a new ban" is decided through the public - // `is_blocked` before the overwrite. This dedups the log line and the metric by - // active ban, not by the fact of insertion. - let already_banned = blacklist.is_blocked(addr); - blacklist.add(addr, Duration::from_secs(ban.duration_secs), ban.reason); - if already_banned { - tracing::debug!(src_ip = %ban.ip, reason = ban.reason, "xdp auto-ban refreshed"); - } else { - XDP_AUTOBANS_TOTAL.inc(); - tracing::info!( - src_ip = %ban.ip, - reason = ban.reason, - duration_secs = ban.duration_secs, - "xdp auto-ban issued" - ); - } + xdp.attach(ifindex, XdpFlags::SKB_MODE) + .context("XDP attach failed in both native and generic modes")?; + Ok(XdpFlags::SKB_MODE) + } else { + Ok(XdpFlags::NONE) + } +} + +/// Finds the loader's XDP program in a loaded object. +fn find_xdp_program<'obj>(obj: &'obj Object) -> Result> { + obj.progs() + .find(|p| p.name() == XDP_PROG_NAME) + .with_context(|| format!("XDP program '{XDP_PROG_NAME}' not found")) +} + +/// Detaches the program if it is dropped while still armed. +/// +/// Any error between a successful attach and the moment `XdpFilter` takes +/// ownership leaves an XDP program attached that drops traffic but has no +/// userspace reader (an orphaned filter). This guard makes that window fail +/// safe: unless [`DetachGuard::disarm`] was called, dropping it detaches. +pub(super) struct DetachGuard<'obj> { + prog: Program<'obj>, + ifindex: i32, + /// Raw `XdpFlags` bits: `XdpFlags` itself is neither `Copy` nor `Clone`. + flags_bits: u32, + armed: bool, +} + +impl<'obj> DetachGuard<'obj> { + fn new(prog: Program<'obj>, ifindex: i32, flags_bits: u32) -> Self { + Self { + prog, + ifindex, + flags_bits, + armed: true, } - 0 - })?; - Ok(builder.build()?) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[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 `
` 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() { - assert_eq!(xdp_attach_mode_name(1), "native (drv)"); - assert_eq!(xdp_attach_mode_name(3), "generic (skb)"); - assert_eq!(xdp_attach_mode_name(2), "hardware offload"); - assert_eq!(xdp_attach_mode_name(0), "none"); + /// Marks the attach as committed; dropping the guard is then a no-op. + pub(super) fn disarm(mut self) { + self.armed = false; } } + +impl Drop for DetachGuard<'_> { + fn drop(&mut self) { + if !self.armed { + return; + } + if let Err(e) = Xdp::new(self.prog.as_fd()).detach(self.ifindex, XdpFlags::from_bits_truncate(self.flags_bits)) + { + tracing::error!( + ifindex = self.ifindex, + error = %e, + "XDP attach rollback failed: the program may still be attached" + ); + } else { + tracing::warn!(ifindex = self.ifindex, "XDP attach rolled back after a setup failure"); + } + } +} + +/// Attaches the program and returns `(detach flags, mode name, rollback guard)`. +/// +/// The returned guard must be disarmed only after every fallible step that +/// follows the attach has succeeded. The attach mode is queried from the +/// kernel (not assumed from the requested flags) so that a generic-mode +/// fallback is detached from the generic slot. +pub(super) fn attach_program<'obj>( + obj: &'obj Object, + ifindex: i32, + interface: &str, +) -> Result<(u32, &'static str, DetachGuard<'obj>)> { + let prog = find_xdp_program(obj)?; + let requested = { + let xdp = Xdp::new(prog.as_fd()); + attach_with_fallback(&xdp, ifindex, interface)? + }; + let mode = match Xdp::new(prog.as_fd()).query(ifindex, XdpFlags::NONE) { + Ok(query) => query.attach_mode, + Err(e) => { + tracing::warn!( + interface = %interface, + error = %e, + "XDP attach-mode query failed; assuming the requested flags" + ); + 0 + }, + }; + // Fall back to the requested flags when the query failed or the interface + // reports several programs (MULTI): that is the best detach handle we have. + let flags_bits = detach_flags_for_mode(mode).unwrap_or(requested).bits(); + let guard = DetachGuard::new(prog, ifindex, flags_bits); + let name = if mode == 0 { + "unknown" + } else { + xdp_attach_mode_name(mode) + }; + Ok((flags_bits, name, guard)) +} diff --git a/src/xdp/filter/events.rs b/src/xdp/filter/events.rs index 809fe5a..fca5996 100644 --- a/src/xdp/filter/events.rs +++ b/src/xdp/filter/events.rs @@ -1,11 +1,19 @@ //! Parsing and policy for XDP ringbuf events (`struct xdp_event`). //! -//! Pure functions (no I/O, no libbpf) so that they can be covered by unit tests without -//! a kernel or Redis. The callback wiring lives in [`super::attach`]. +//! Parsing and the auto-ban policy are pure functions covered by +//! `tests/xdp_events.rs`; [`build_ringbuf`] wires the same policy into the libbpf +//! ringbuf callback. -use std::net::Ipv4Addr; +use std::net::{IpAddr, Ipv4Addr}; +use std::sync::Arc; +use std::time::Duration; + +use anyhow::{Context, Result}; +use libbpf_rs::{MapCore, Object, RingBuffer, RingBufferBuilder}; use crate::config::XdpConfig; +use crate::filter::blacklist::Blacklist; +use crate::xdp::metrics::XDP_AUTOBANS_TOTAL; /// Exact size of `struct xdp_event` (see `xdp/core/common.h:61-66`): /// `__u32 type` @0, `__u32 src_ip` @4, `__u32 metadata` @8, @@ -166,5 +174,64 @@ pub fn auto_ban_decision(event: &XdpEvent, config: &AutoBanConfig) -> Option`, a copy of +/// `AutoBanConfig`) is captured by `move` clones. When `blacklist` is `None` (feature not +/// wired) or the policy is disabled, events are only parsed and logged in a structured +/// way; no bans are written. The hot path does no `format!` — only `tracing` fields and +/// `&'static str` reasons. +pub(super) fn build_ringbuf( + obj: &Object, + blacklist: Option>, + cfg: AutoBanConfig, +) -> Result> { + let map = obj + .maps() + .find(|m| m.name() == "events_ringbuf") + .context("events_ringbuf not found")?; + let mut builder = RingBufferBuilder::new(); + builder.add(&map, move |data: &[u8]| { + let Some(event) = parse_xdp_event(data) else { + tracing::debug!( + len = data.len(), + "xdp ringbuf frame ignored (too short or unknown type)" + ); + return 0; + }; + tracing::debug!( + kind = ?event.kind, + src_ip = %event.src_ip, + metadata = event.metadata, + "xdp event" + ); + let Some(blacklist) = &blacklist else { + return 0; + }; + if let Some(ban) = auto_ban_decision(&event, &cfg) { + let addr = IpAddr::V4(ban.ip); + // `Blacklist::add` returns `()` (it ignores `DashMap::insert`) and blacklist.rs is + // out of our edit scope, so "is this a new ban" is decided through the public + // `is_blocked` before the overwrite. This dedups the log line and the metric by + // active ban, not by the fact of insertion. + let already_banned = blacklist.is_blocked(addr); + blacklist.add(addr, Duration::from_secs(ban.duration_secs), ban.reason); + if already_banned { + tracing::debug!(src_ip = %ban.ip, reason = ban.reason, "xdp auto-ban refreshed"); + } else { + XDP_AUTOBANS_TOTAL.inc(); + tracing::info!( + src_ip = %ban.ip, + reason = ban.reason, + duration_secs = ban.duration_secs, + "xdp auto-ban issued" + ); + } + } + 0 + })?; + Ok(builder.build()?) +} + // Parser and policy coverage lives in `tests/xdp_events.rs` (this module is kept under // the 250-line limit; `src/xdp/filter/` already has the maximum of 4 files per directory). diff --git a/src/xdp/filter/maps.rs b/src/xdp/filter/maps.rs index f23432e..af8cefa 100644 --- a/src/xdp/filter/maps.rs +++ b/src/xdp/filter/maps.rs @@ -1,58 +1,117 @@ -use super::XdpFilter; -use anyhow::Result; -use libbpf_rs::{MapCore, MapFlags}; -use std::net::{IpAddr, Ipv4Addr}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; -/// Monotonic clock in ns since boot — the same clock as `bpf_ktime_get_ns()`, -/// which is what the XDP program compares ban expiry against. `SystemTime` -/// (epoch ns) is ~50 years larger, so bans written with it never expired. -/// -/// # Panics -/// Never: `clock_gettime(CLOCK_MONOTONIC)` cannot fail on Linux. +use anyhow::{Result, bail}; +use libbpf_rs::{MapCore, MapFlags}; + +use super::XdpFilter; +use crate::xdp::probe::{blacklist_expiry_ns, monotonic_ns}; + +/// Encodes an LPM-trie key for `blacklist_map` and `whitelist_map`: +/// prefix length as a little-endian u32 in bytes 0..4 and the four IPv4 +/// octets in network order in bytes 4..8 (`struct lpm_key` in `common.h`). #[must_use] -pub fn monotonic_ns() -> u64 { - let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 }; - // SAFETY: `ts` is a valid, writable `timespec` and CLOCK_MONOTONIC is - // always available on Linux (the only target of the `xdp` feature). - unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) }; - let secs = u64::try_from(ts.tv_sec).unwrap_or(0); - secs.saturating_mul(1_000_000_000) - .saturating_add(u64::try_from(ts.tv_nsec).unwrap_or(0)) +pub fn blacklist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] { + ipv4_lpm_key(prefix_len, v4) } -/// Encodes an LPM-trie key for `blacklist_map`: prefix length in byte 0 and the -/// four IPv4 octets in bytes 4..8 (bytes 1..3 are padding the kernel ignores). -pub(super) fn blacklist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] { - let mut key = [0u8; 8]; - key[0] = prefix_len; - key[4..8].copy_from_slice(v4); - key +/// Encodes an LPM-trie key for `whitelist_map`; the layout is identical to +/// [`blacklist_key`] because both C maps use `struct lpm_key`. +#[must_use] +pub fn whitelist_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] { + ipv4_lpm_key(prefix_len, v4) } /// Encodes an LPM-trie key for `blacklist6_map`: prefix length as a /// little-endian u32 in bytes 0..4 and the 16-byte IPv6 address in network /// order in bytes 4..20 (20 bytes total, matching the C `struct lpm_key6`). +#[must_use] pub fn blacklist6_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] { + ipv6_lpm_key(prefix_len, v6) +} + +/// Encodes an LPM-trie key for `whitelist6_map`; identical to +/// [`blacklist6_key`] because both C maps use `struct lpm_key6`. +#[must_use] +pub fn whitelist6_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] { + ipv6_lpm_key(prefix_len, v6) +} + +fn ipv4_lpm_key(prefix_len: u8, v4: &[u8; 4]) -> [u8; 8] { + let mut key = [0u8; 8]; + key[0..4].copy_from_slice(&u32::from(prefix_len).to_le_bytes()); + key[4..8].copy_from_slice(v4); + key +} + +fn ipv6_lpm_key(prefix_len: u8, v6: &[u8; 16]) -> [u8; 20] { let mut key = [0u8; 20]; - key[0] = prefix_len; + key[0..4].copy_from_slice(&u32::from(prefix_len).to_le_bytes()); key[4..20].copy_from_slice(v6); key } -/// Absolute ban expiry on the monotonic clock, saturating so an absurd duration -/// clamps to `u64::MAX` rather than wrapping into the past. -pub(super) fn blacklist_expiry_ns(now_ns: u64, duration_secs: u64) -> u64 { - now_ns.saturating_add(duration_secs.saturating_mul(1_000_000_000)) +/// Validates a CIDR prefix length and clears the host bits of `prefix`. +/// +/// The kernel's LPM trie compares keys bit by bit, so `10.1.2.3/24` and +/// `10.1.2.0/24` are different keys for the same range: without masking, two +/// bans of the same subnet never share an entry and expiry bookkeeping falls +/// apart. A zero-length prefix is rejected because "ban the entire address +/// space" is never an intentional operator request. +/// +/// # Errors +/// `prefix_len` is 0 or exceeds 32 for IPv4 / 128 for IPv6. +pub fn normalize_cidr(prefix: IpAddr, prefix_len: u8) -> Result { + match prefix { + IpAddr::V4(addr) => { + ensure_prefix_len(prefix_len, 32, "IPv4")?; + Ok(IpAddr::V4(Ipv4Addr::from(mask_v4(addr, prefix_len)))) + }, + IpAddr::V6(addr) => { + ensure_prefix_len(prefix_len, 128, "IPv6")?; + Ok(IpAddr::V6(Ipv6Addr::from(mask_v6(addr, prefix_len)))) + }, + } +} + +fn ensure_prefix_len(prefix_len: u8, max: u8, family: &str) -> Result<()> { + if prefix_len == 0 || prefix_len > max { + bail!("invalid {family} prefix length {prefix_len}: expected 1..={max}"); + } + Ok(()) +} + +fn mask_v4(addr: Ipv4Addr, prefix_len: u8) -> [u8; 4] { + let bits = u32::from(addr); + let mask = u32::MAX << (32 - u32::from(prefix_len)); + (bits & mask).to_be_bytes() +} + +fn mask_v6(addr: Ipv6Addr, prefix_len: u8) -> [u8; 16] { + let mut octets = addr.octets(); + let full_bytes = usize::from(prefix_len) / 8; + let remainder = prefix_len % 8; + for byte in octets.iter_mut().skip(full_bytes) { + *byte = 0; + } + if remainder != 0 { + // `full_bytes` is always < 16 here: a partial byte follows the full ones. + octets[full_bytes] &= 0xffu8 << (8 - remainder); + } + octets } impl XdpFilter { /// Adds a CIDR ban to the matching LPM trie: `blacklist_map` for IPv4, /// `blacklist6_map` for IPv6. /// + /// The prefix is masked first (see [`normalize_cidr`]), so every ban of + /// the same subnet addresses the same kernel key. + /// /// # Errors - /// XDP not loaded, map missing from the object, or a BPF update failure. + /// Invalid prefix length, XDP not loaded, map missing from the object, or + /// a BPF update failure. pub fn ban_cidr(&self, prefix: IpAddr, prefix_len: u8, duration_secs: u64) -> Result<()> { - match prefix { + match normalize_cidr(prefix, prefix_len)? { IpAddr::V4(net) => self.blacklist_update(prefix_len, &net.octets(), duration_secs), IpAddr::V6(net) => self.blacklist6_update(prefix_len, &net.octets(), duration_secs), } @@ -74,6 +133,113 @@ impl XdpFilter { Ok(()) } + /// Adds one address to the kernel whitelist: `/32` for IPv4, `/128` for IPv6. + /// + /// # Errors + /// XDP not loaded, the matching whitelist map is missing, or a BPF update + /// failure. + pub fn whitelist_add(&self, ip: IpAddr) -> Result<()> { + let prefix_len = match ip { + IpAddr::V4(_) => 32, + IpAddr::V6(_) => 128, + }; + self.whitelist_add_cidr(ip, prefix_len) + } + + /// Adds a CIDR to the matching whitelist LPM trie: `whitelist_map` for + /// IPv4, `whitelist6_map` for IPv6. + /// + /// The C filter treats a present key as "trusted" and ignores the value, + /// so the stored `u8` is always `1`. + /// + /// # Errors + /// Invalid prefix length, XDP not loaded, map missing from the object, or + /// a BPF update failure. + pub fn whitelist_add_cidr(&self, prefix: IpAddr, prefix_len: u8) -> Result<()> { + match normalize_cidr(prefix, prefix_len)? { + IpAddr::V4(net) => { + let map = self.find_map("whitelist_map")?; + map.update(&whitelist_key(prefix_len, &net.octets()), &[1u8], MapFlags::ANY)?; + }, + IpAddr::V6(net) => { + let map = self.find_map("whitelist6_map")?; + map.update(&whitelist6_key(prefix_len, &net.octets()), &[1u8], MapFlags::ANY)?; + }, + } + Ok(()) + } + + /// Removes one address from the kernel whitelist. + /// + /// # Errors + /// XDP not loaded, the matching whitelist map is missing, or a BPF delete + /// failure. + pub fn whitelist_remove(&self, ip: IpAddr) -> Result<()> { + match ip { + IpAddr::V4(addr) => { + let map = self.find_map("whitelist_map")?; + map.delete(&whitelist_key(32, &addr.octets()))?; + }, + IpAddr::V6(addr) => { + let map = self.find_map("whitelist6_map")?; + map.delete(&whitelist6_key(128, &addr.octets()))?; + }, + } + Ok(()) + } + + /// Removes expired entries from `blacklist_map` and `blacklist6_map`. + /// + /// The packet path can only expire the exact `/32` or `/128` key it looked + /// up, so a ban on a wider prefix (for example a `/24`) stays resident and + /// keeps blocking sources until it is deleted here. Call periodically. + /// + /// Expiry semantics are the kernel's: an entry is alive while + /// `expiry > bpf_ktime_get_ns()`. There is no "permanent" sentinel; `0` is + /// already expired and is deleted, while a permanent ban is stored as + /// `u64::MAX` and naturally survives every comparison. Keys are collected + /// before deletion because the LPM iterator is not stable across deletes, + /// and the expiry is re-read immediately before the delete so a + /// concurrent refresh wins over the reaper. + /// + /// # Errors + /// XDP not loaded, a blacklist map is missing, or a BPF lookup/delete fails. + pub fn reap_expired_bans(&self) -> Result { + let now = monotonic_ns(); + let reaped = self.reap_map("blacklist_map", now)? + self.reap_map("blacklist6_map", now)?; + Ok(reaped) + } + + fn reap_map(&self, name: &str, now: u64) -> Result { + let map = self.find_map(name)?; + let keys: Vec> = map.keys().collect(); + let mut reaped = 0; + for key in keys { + let Some(value) = map.lookup(&key, MapFlags::ANY)? else { + continue; + }; + let Ok(raw) = <[u8; 8]>::try_from(value.as_slice()) else { + continue; + }; + if u64::from_le_bytes(raw) > now { + continue; + } + // A ban may have been refreshed after the first read; never delete a fresh one. + let Some(value) = map.lookup(&key, MapFlags::ANY)? else { + continue; + }; + let Ok(raw) = <[u8; 8]>::try_from(value.as_slice()) else { + continue; + }; + if u64::from_le_bytes(raw) > monotonic_ns() { + continue; + } + map.delete(&key)?; + reaped += 1; + } + Ok(reaped) + } + pub fn unban_ip(&self, ip: Ipv4Addr) -> Result<()> { let map = self.find_map("blacklist_map")?; let key = blacklist_key(32, &ip.octets()); @@ -81,25 +247,3 @@ impl XdpFilter { Ok(()) } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn blacklist_key_puts_prefix_len_first_and_octets_last() { - assert_eq!(blacklist_key(24, &[10, 0, 1, 200]), [24, 0, 0, 0, 10, 0, 1, 200]); - assert_eq!(blacklist_key(32, &[127, 0, 0, 1]), [32, 0, 0, 0, 127, 0, 0, 1]); - } - - #[test] - fn blacklist_expiry_converts_secs_to_ns() { - assert_eq!(blacklist_expiry_ns(1_000, 2), 1_000 + 2_000_000_000); - } - - #[test] - fn blacklist_expiry_saturates_instead_of_wrapping() { - assert_eq!(blacklist_expiry_ns(u64::MAX, 1), u64::MAX); - assert_eq!(blacklist_expiry_ns(0, u64::MAX), u64::MAX); - } -} diff --git a/src/xdp/filter/mod.rs b/src/xdp/filter/mod.rs index 5fe2da3..7ab1b63 100644 --- a/src/xdp/filter/mod.rs +++ b/src/xdp/filter/mod.rs @@ -1,27 +1,32 @@ -use anyhow::{Context, Result, bail}; -use libbpf_rs::{MapCore, MapFlags, Object, ObjectBuilder, RingBuffer, Xdp, XdpFlags}; +use anyhow::{Context, Result}; +use libbpf_rs::{MapCore, Object, ObjectBuilder, RingBuffer, Xdp, XdpFlags}; use std::os::unix::io::AsFd; use std::sync::Arc; -use super::XdpStats; use super::globals::XdpGlobals; use crate::config::XdpConfig; use crate::filter::blacklist::Blacklist; -use crate::traffic::prefix::{PrefixKey, PrefixStatsVal}; -use attach::{build_ringbuf, interface_cstr, patch_rodata, xdp_attach_mode_name}; -use events::AutoBanConfig; +use attach::{attach_program, patch_rodata}; +use events::{AutoBanConfig, build_ringbuf}; mod attach; pub(crate) mod events; mod maps; -pub use maps::{blacklist6_key, monotonic_ns}; +pub use attach::{interface_cstr, is_rodata_map, xdp_attach_mode_name}; +pub use maps::{blacklist_key, blacklist6_key, normalize_cidr, whitelist_key, whitelist6_key}; pub struct XdpFilter { obj: Option, ringbuf: Option>, ifindex: i32, + /// Raw `XdpFlags` bits of the slot the program was actually attached to + /// (libbpf-rs's `XdpFlags` is neither `Copy` nor `Clone`, so the bits are + /// stored and rebuilt with `from_bits_truncate`). The kernel may pick + /// generic (skb) mode after a failed native attach; detaching the native + /// slot in that case would leave an orphaned filter behind. + attach_flags: Option, interface: String, globals: XdpGlobals, blacklist: Option>, @@ -34,6 +39,7 @@ impl XdpFilter { obj: None, ringbuf: None, ifindex: 0, + attach_flags: None, interface: interface.to_string(), globals: XdpGlobals::default(), blacklist: None, @@ -56,6 +62,16 @@ impl XdpFilter { self.autoban = AutoBanConfig::from_xdp(config); } + /// Loads the object, attaches it and registers the ringbuf reader. + /// + /// Fallible setup (`build_ringbuf`) runs before the attach, so a failure + /// there cannot leave a filter behind. Everything after the attach is + /// covered by an `attach::DetachGuard`: any early return detaches instead + /// of leaving an orphaned program that drops traffic without a reader. + /// + /// # Errors + /// The environment probe, object load, map setup or attach failed. On an + /// attach failure no program is left installed. pub fn load(&mut self) -> Result<()> { // Diagnostics before loading: fail fast on an unsupported kernel or driver. super::probe::preflight(&self.interface)?; @@ -80,68 +96,65 @@ impl XdpFilter { let ifindex = unsafe { libc::if_nametoindex(c_interface.as_ptr()) }; if ifindex == 0 { let err = std::io::Error::last_os_error(); - bail!("interface '{}' not found: {err}", self.interface); + anyhow::bail!("interface '{}' not found: {err}", self.interface); } - - let prog = obj - .progs() - .find(|p| p.name() == "rampart_universal_filter") - .context("XDP program 'rampart_universal_filter' not found")?; - let xdp = Xdp::new(prog.as_fd()); - // Mode ladder: try native (drv) first; if the driver cannot handle it, fall back to - // generic (skb) with a warning (finding #1 of the competitive analysis: report the - // mode as it is, no window dressing). - let ifindex = ifindex as i32; - if let Err(e) = xdp.attach(ifindex, XdpFlags::NONE) { - tracing::warn!( - interface = %self.interface, - error = %e, - "native XDP attach failed, falling back to generic (skb) mode" - ); - xdp.attach(ifindex, XdpFlags::SKB_MODE) - .context("XDP attach failed in both native and generic modes")?; - } - let mode = xdp - .query(ifindex, XdpFlags::NONE) - .map(|q| xdp_attach_mode_name(q.attach_mode)) - .unwrap_or("unknown"); - + // The ringbuf is the only fallible post-attach step; building it first + // removes the failure mode entirely instead of relying on rollback. super::metrics::init(); let rbuf = build_ringbuf(&obj, self.blacklist.clone(), self.autoban)?; + let (attach_flags, mode, guard) = attach_program(&obj, ifindex as i32, &self.interface)?; + guard.disarm(); + self.obj = Some(obj); self.ringbuf = Some(rbuf); - self.ifindex = ifindex; + self.ifindex = ifindex as i32; + self.attach_flags = Some(attach_flags); tracing::info!("XDP filter attached to {} (mode: {mode})", self.interface); Ok(()) } + /// Detaches the program from the interface and drops the ringbuf. + /// + /// The stored attach flags select the slot the kernel actually used, and + /// the interface is queried afterwards: a detach that silently did + /// nothing (the old `XdpFlags::NONE` on a generic-mode program) must not + /// be reported as success. + /// + /// # Errors + /// The detach failed or the interface still reports an attached program. pub fn unload(&mut self) -> Result<()> { - let was_attached = self.obj.is_some(); - let mut result = Ok(()); - if let Some(obj) = self.obj.as_ref() { - result = self.detach_program(obj); - } + let Some(obj) = self.obj.as_ref() else { + self.ringbuf = None; + self.ifindex = 0; + self.attach_flags = None; + return Ok(()); + }; + let Some(flags) = self.attach_flags else { + anyhow::bail!("XDP state is inconsistent: object present without attach flags"); + }; + let result = self.detach_program(obj, flags); self.ringbuf = None; - self.obj = None; - self.ifindex = 0; - if was_attached { + if result.is_ok() { + self.obj = None; + self.ifindex = 0; + self.attach_flags = None; tracing::info!("XDP filter detached from {}", self.interface); } result } /// Detaches the XDP program using the real program fd owned by the loaded - /// `Object` (mirroring `load`'s `Xdp::new(prog.as_fd()).attach(..)`). A - /// detach failure while a filter is active is surfaced loudly and returned, - /// never swallowed — otherwise the kernel keeps dropping packets we no - /// longer intend to filter. - fn detach_program(&self, obj: &Object) -> Result<()> { + /// `Object`. A detach failure while a filter is active is surfaced loudly + /// and returned, never swallowed — otherwise the kernel keeps dropping + /// packets we no longer intend to filter. + fn detach_program(&self, obj: &Object, flags: u32) -> Result<()> { let prog = obj .progs() .find(|p| p.name() == "rampart_universal_filter") .context("XDP program 'rampart_universal_filter' not found while detaching")?; - if let Err(e) = Xdp::new(prog.as_fd()).detach(self.ifindex, XdpFlags::NONE) { + let xdp = Xdp::new(prog.as_fd()); + if let Err(e) = xdp.detach(self.ifindex, XdpFlags::from_bits_truncate(flags)) { tracing::error!( interface = %self.interface, ifindex = self.ifindex, @@ -150,6 +163,19 @@ impl XdpFilter { ); return Err(e).with_context(|| format!("failed to detach XDP from '{}'", self.interface)); } + // Query every mode (`NONE`), not just the one we detached: only an + // attach mode of NONE proves the interface is clean. + let remaining = xdp + .query(self.ifindex, XdpFlags::NONE) + .context("failed to verify the XDP detach (query)")? + .attach_mode; + if remaining != 0 { + let mode = xdp_attach_mode_name(remaining); + anyhow::bail!( + "XDP program is still attached to '{}' after detach (mode: {mode})", + self.interface + ); + } Ok(()) } @@ -159,7 +185,7 @@ impl XdpFilter { } } - fn find_map<'a>(&'a self, name: &str) -> Result { + pub(crate) fn find_map<'a>(&'a self, name: &str) -> Result { self.obj .as_ref() .context("XDP not loaded")? @@ -167,53 +193,6 @@ impl XdpFilter { .find(|m| m.name() == name) .with_context(|| format!("map '{}' not found", name)) } - - /// Reads the `prefix_stats` map (aggregates per /24 and /64 prefix). - /// - /// # Errors - /// XDP is not loaded, the map is missing from the object, or a BPF read fails. - pub fn read_prefix_stats(&self) -> Result> { - let map = self.find_map("prefix_stats")?; - let mut out = Vec::new(); - for key in map.keys() { - let Ok(kb) = <[u8; 24]>::try_from(key.as_slice()) else { - continue; - }; - let Some(val) = map.lookup(&kb, MapFlags::ANY).context("prefix_stats lookup failed")? else { - continue; - }; - let Ok(vb) = <[u8; 24]>::try_from(val.as_slice()) else { - continue; - }; - out.push((PrefixKey::from_bytes(kb), PrefixStatsVal::from_bytes(vb))); - } - Ok(out) - } - - pub fn get_stats(&self) -> Result { - let map = self.find_map("stats_map")?; - let sum = |idx: u32| -> u64 { - let key = idx.to_le_bytes(); - match map.lookup(&key, MapFlags::ANY) { - Ok(Some(v)) => v - .chunks_exact(8) - .map(|c| u64::from_le_bytes(c.try_into().expect("chunk size 8"))) - .sum(), - _ => 0, - } - }; - Ok(XdpStats { - total: sum(0), - tcp: sum(1), - whitelist: sum(2), - blacklist: sum(3), - syn_throttle: sum(4), - passed: sum(5), - dropped: sum(6), - udp: sum(7), - rate_limit: sum(8), - }) - } } // SAFETY: `XdpFilter` is only ever accessed from the single thread that owns it, diff --git a/src/xdp/mod.rs b/src/xdp/mod.rs index 9bb4b3a..0909bf2 100644 --- a/src/xdp/mod.rs +++ b/src/xdp/mod.rs @@ -6,7 +6,9 @@ mod probe; pub use probe::{ - AttachMode, EnvironmentReport, FilesystemProbe, KernelVersion, MIN_KERNEL, SystemProbe, XdpStats, + AttachMode, EnvironmentReport, FilesystemProbe, KernelVersion, MIN_KERNEL, STAT_BLACKLIST, STAT_CHALLENGE_FAILED, + STAT_CHALLENGE_SENT, STAT_CHALLENGE_VERIFIED, STAT_DROP, STAT_FRAGMENT_BYPASS, STAT_PASS, STAT_RATE_LIMIT, + STAT_SYN_THROTTLE, STAT_TCP, STAT_TOTAL, STAT_UDP, STAT_WHITELIST, SystemProbe, XdpStats, driver_supports_native_xdp, }; @@ -21,18 +23,17 @@ mod metrics; #[cfg(feature = "xdp")] mod filter; #[cfg(feature = "xdp")] -pub use filter::XdpFilter; -#[cfg(feature = "xdp")] -pub use filter::blacklist6_key; -#[cfg(feature = "xdp")] pub use filter::events::{ AutoBanConfig, BanAction, XDP_EVENT_LEN, XdpEvent, XdpEventKind, auto_ban_decision, parse_xdp_event, }; #[cfg(feature = "xdp")] -pub use filter::monotonic_ns; +pub use filter::{ + XdpFilter, blacklist_key, blacklist6_key, interface_cstr, is_rodata_map, normalize_cidr, whitelist_key, + whitelist6_key, xdp_attach_mode_name, +}; #[cfg(feature = "xdp")] -pub use probe::XdpMetrics; +pub use probe::{XdpMetrics, blacklist_expiry_ns, monotonic_ns}; #[cfg(not(feature = "xdp"))] mod noop; diff --git a/src/xdp/noop.rs b/src/xdp/noop.rs index 37c6be0..97e06f5 100644 --- a/src/xdp/noop.rs +++ b/src/xdp/noop.rs @@ -23,6 +23,12 @@ impl XdpFilter { Ok(()) } + /// Nothing to reap without the kernel filter; the maintenance loop can call + /// this unconditionally. + pub fn reap_expired_bans(&self) -> Result { + Ok(0) + } + /// Honest failure: reading `prefix_stats` is not wired up without the `xdp` feature. /// /// # Errors diff --git a/src/xdp/probe/clock.rs b/src/xdp/probe/clock.rs new file mode 100644 index 0000000..beeca51 --- /dev/null +++ b/src/xdp/probe/clock.rs @@ -0,0 +1,30 @@ +//! Monotonic clock shared by the XDP userspace path. +//! +//! The kernel writes `bpf_ktime_get_ns()` values (CLOCK_MONOTONIC nanoseconds +//! since boot) into the ban maps, and the packet path compares expiry +//! against the same clock. Userspace must therefore never use `SystemTime` +//! (epoch nanoseconds, about 50 years larger) for bans. + +/// Monotonic clock in ns since boot — the same clock as `bpf_ktime_get_ns()`, +/// which is what the XDP program compares ban expiry against. `SystemTime` +/// (epoch ns) is ~50 years larger, so bans written with it never expired. +/// +/// # Panics +/// Never: `clock_gettime(CLOCK_MONOTONIC)` cannot fail on Linux. +#[must_use] +pub fn monotonic_ns() -> u64 { + let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 }; + // SAFETY: `ts` is a valid, writable `timespec` and CLOCK_MONOTONIC is + // always available on Linux (the only target of the `xdp` feature). + unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) }; + let secs = u64::try_from(ts.tv_sec).unwrap_or(0); + secs.saturating_mul(1_000_000_000) + .saturating_add(u64::try_from(ts.tv_nsec).unwrap_or(0)) +} + +/// Absolute ban expiry on the monotonic clock, saturating so an absurd duration +/// clamps to `u64::MAX` rather than wrapping into the past. +#[must_use] +pub fn blacklist_expiry_ns(now_ns: u64, duration_secs: u64) -> u64 { + now_ns.saturating_add(duration_secs.saturating_mul(1_000_000_000)) +} diff --git a/src/xdp/probe/metrics.rs b/src/xdp/probe/metrics.rs index 99bd70f..952d0c7 100644 --- a/src/xdp/probe/metrics.rs +++ b/src/xdp/probe/metrics.rs @@ -13,6 +13,10 @@ pub struct XdpMetrics { dropped: IntGauge, udp: IntGauge, rate_limit: IntGauge, + challenge_sent: IntGauge, + challenge_verified: IntGauge, + challenge_failed: IntGauge, + fragment_bypass: IntGauge, } impl XdpMetrics { @@ -33,6 +37,13 @@ impl XdpMetrics { dropped: Self::new_gauge("rampart_xdp_dropped", "Packets dropped by filter")?, udp: Self::new_gauge("rampart_xdp_udp", "UDP packets processed")?, rate_limit: Self::new_gauge("rampart_xdp_rate_limit", "Packets rate-limited by window counter")?, + challenge_sent: Self::new_gauge("rampart_xdp_challenge_sent", "RST challenges sent")?, + challenge_verified: Self::new_gauge("rampart_xdp_challenge_verified", "RST challenges verified")?, + challenge_failed: Self::new_gauge("rampart_xdp_challenge_failed", "RST challenges failed")?, + fragment_bypass: Self::new_gauge( + "rampart_xdp_fragment_bypass", + "Packets bypassing L4 checks as fragments", + )?, }; tracing::info!("XDP Prometheus metrics registered"); Ok(m) @@ -48,6 +59,10 @@ impl XdpMetrics { self.dropped.set(stats.dropped as i64); self.udp.set(stats.udp as i64); self.rate_limit.set(stats.rate_limit as i64); + self.challenge_sent.set(stats.challenge_sent as i64); + self.challenge_verified.set(stats.challenge_verified as i64); + self.challenge_failed.set(stats.challenge_failed as i64); + self.fragment_bypass.set(stats.fragment_bypass as i64); tracing::debug!("XDP metrics updated"); } } diff --git a/src/xdp/probe/mod.rs b/src/xdp/probe/mod.rs index 4f5ad7a..bd70e2b 100644 --- a/src/xdp/probe/mod.rs +++ b/src/xdp/probe/mod.rs @@ -1,4 +1,4 @@ -//! XDP environment probing: diagnostics, statistics and metrics. +//! XDP environment probing: diagnostics, statistics, metrics and the shared clock. mod diagnostics; #[cfg(feature = "xdp")] @@ -7,8 +7,17 @@ pub use diagnostics::{ AttachMode, EnvironmentReport, FilesystemProbe, KernelVersion, MIN_KERNEL, SystemProbe, driver_supports_native_xdp, }; +#[cfg(feature = "xdp")] +mod clock; +#[cfg(feature = "xdp")] +pub use clock::{blacklist_expiry_ns, monotonic_ns}; + mod stats; -pub use stats::XdpStats; +pub use stats::{ + STAT_BLACKLIST, STAT_CHALLENGE_FAILED, STAT_CHALLENGE_SENT, STAT_CHALLENGE_VERIFIED, STAT_DROP, + STAT_FRAGMENT_BYPASS, STAT_PASS, STAT_RATE_LIMIT, STAT_SYN_THROTTLE, STAT_TCP, STAT_TOTAL, STAT_UDP, + STAT_WHITELIST, XdpStats, +}; #[cfg(feature = "xdp")] mod metrics; diff --git a/src/xdp/probe/stats.rs b/src/xdp/probe/stats.rs index 34d4845..449e047 100644 --- a/src/xdp/probe/stats.rs +++ b/src/xdp/probe/stats.rs @@ -1,3 +1,47 @@ +//! XDP program counters and the readers for the kernel statistics maps. +//! +//! `stats_map` is a `BPF_MAP_TYPE_PERCPU_ARRAY` keyed by the `STAT_*` indices +//! from `xdp/core/maps.h`; every element holds one `__u64` per CPU, so a +//! lookup returns a blob whose 8-byte chunks must be summed. +//! `prefix_stats` is a plain LRU hash read as whole key/value records. + +#[cfg(feature = "xdp")] +use anyhow::{Context, Result}; +#[cfg(feature = "xdp")] +use libbpf_rs::{MapCore, MapFlags}; + +#[cfg(feature = "xdp")] +use crate::traffic::prefix::{PrefixKey, PrefixStatsVal}; +#[cfg(feature = "xdp")] +use crate::xdp::XdpFilter; + +/// `STAT_TOTAL`: every packet that reached the filter. +pub const STAT_TOTAL: u32 = 0; +/// `STAT_TCP`: TCP packets seen. +pub const STAT_TCP: u32 = 1; +/// `STAT_WHITELIST`: packets passed by the whitelist check. +pub const STAT_WHITELIST: u32 = 2; +/// `STAT_BLACKLIST`: packets dropped by the blacklist check. +pub const STAT_BLACKLIST: u32 = 3; +/// `STAT_SYN_THROTTLE`: SYNs dropped by the per-source throttle. +pub const STAT_SYN_THROTTLE: u32 = 4; +/// `STAT_PASS`: packets passed to the kernel stack. +pub const STAT_PASS: u32 = 5; +/// `STAT_DROP`: packets dropped by any rule. +pub const STAT_DROP: u32 = 6; +/// `STAT_UDP`: UDP packets seen. +pub const STAT_UDP: u32 = 7; +/// `STAT_RATE_LIMIT`: packets dropped by a rate limiter. +pub const STAT_RATE_LIMIT: u32 = 8; +/// `STAT_CHALLENGE_SENT`: RST challenges sent to suspicious sources. +pub const STAT_CHALLENGE_SENT: u32 = 9; +/// `STAT_CHALLENGE_VERIFIED`: sources that answered a challenge correctly. +pub const STAT_CHALLENGE_VERIFIED: u32 = 10; +/// `STAT_CHALLENGE_FAILED`: sources that failed a challenge. +pub const STAT_CHALLENGE_FAILED: u32 = 11; +/// `STAT_FRAGMENT_BYPASS`: packets passed because their L4 header is unavailable (fragments). +pub const STAT_FRAGMENT_BYPASS: u32 = 12; + /// XDP program counters; the indices follow `STAT_*` from `xdp/core/maps.h`. #[derive(Debug, Clone, Copy, Default)] pub struct XdpStats { @@ -10,4 +54,67 @@ pub struct XdpStats { pub dropped: u64, pub udp: u64, pub rate_limit: u64, + pub challenge_sent: u64, + pub challenge_verified: u64, + pub challenge_failed: u64, + pub fragment_bypass: u64, +} + +#[cfg(feature = "xdp")] +impl XdpFilter { + /// Reads `stats_map` and sums the per-CPU values. + /// + /// # Errors + /// XDP is not loaded, or the map is missing from the object (individual + /// lookup failures are reported as zero rather than aborting the report). + pub fn get_stats(&self) -> Result { + let map = self.find_map("stats_map")?; + let sum = |idx: u32| -> u64 { + let key = idx.to_le_bytes(); + match map.lookup(&key, MapFlags::ANY) { + Ok(Some(v)) => v + .chunks_exact(8) + .map(|c| u64::from_le_bytes(c.try_into().expect("chunk size 8"))) + .sum(), + _ => 0, + } + }; + Ok(XdpStats { + total: sum(STAT_TOTAL), + tcp: sum(STAT_TCP), + whitelist: sum(STAT_WHITELIST), + blacklist: sum(STAT_BLACKLIST), + syn_throttle: sum(STAT_SYN_THROTTLE), + passed: sum(STAT_PASS), + dropped: sum(STAT_DROP), + udp: sum(STAT_UDP), + rate_limit: sum(STAT_RATE_LIMIT), + challenge_sent: sum(STAT_CHALLENGE_SENT), + challenge_verified: sum(STAT_CHALLENGE_VERIFIED), + challenge_failed: sum(STAT_CHALLENGE_FAILED), + fragment_bypass: sum(STAT_FRAGMENT_BYPASS), + }) + } + + /// Reads the `prefix_stats` map (aggregates per /24 and /64 prefix). + /// + /// # Errors + /// XDP is not loaded, the map is missing from the object, or a BPF read fails. + pub fn read_prefix_stats(&self) -> Result> { + let map = self.find_map("prefix_stats")?; + let mut out = Vec::new(); + for key in map.keys() { + let Ok(kb) = <[u8; 24]>::try_from(key.as_slice()) else { + continue; + }; + let Some(val) = map.lookup(&kb, MapFlags::ANY).context("prefix_stats lookup failed")? else { + continue; + }; + let Ok(vb) = <[u8; 24]>::try_from(val.as_slice()) else { + continue; + }; + out.push((PrefixKey::from_bytes(kb), PrefixStatsVal::from_bytes(vb))); + } + Ok(out) + } } diff --git a/tests/xdp_attach_smoke.rs b/tests/xdp_attach_smoke.rs new file mode 100644 index 0000000..e7cd873 --- /dev/null +++ b/tests/xdp_attach_smoke.rs @@ -0,0 +1,72 @@ +//! Opt-in live attach check for the XDP loader. +//! +//! Ignored by default: it needs root (CAP_BPF/CAP_NET_ADMIN) and a disposable +//! interface, best inside a throwaway network namespace. Run it through +//! `/tmp/opencode/loaderchk.sh` (creates a veth pair in a netns) or manually: +//! +//! ```text +//! sudo ip netns exec env RAMPART_XDP_TEST_IFACE=veth-test \ +//! target/debug/deps/xdp_attach_smoke- --ignored --nocapture +//! ``` +//! +//! It covers the regressions this suite is about: the attach mode is +//! remembered for detach (a generic-mode attach must not leave an orphaned +//! filter behind), whitelist entries load into both LPM tries, a live ban +//! survives the reaper, an expired prefix ban is reaped, and `unload` reports +//! a clean interface. +#![cfg(feature = "xdp")] + +use std::net::IpAddr; + +use rampart::xdp::XdpFilter; + +fn ip(text: &str) -> IpAddr { + text.parse().expect("test literal must be a valid address") +} + +#[test] +#[ignore = "needs root/CAP_NET_ADMIN and a disposable interface in a network namespace"] +fn attach_ban_whitelist_reap_and_detach() { + let interface = std::env::var("RAMPART_XDP_TEST_IFACE") + .expect("set RAMPART_XDP_TEST_IFACE to a disposable interface (inside a netns)"); + + let mut filter = XdpFilter::new(&interface); + filter.load().expect("XDP load and attach must succeed"); + + filter + .whitelist_add(ip("192.0.2.7")) + .expect("IPv4 whitelist entry must load into whitelist_map"); + filter + .whitelist_add(ip("2001:db8::7")) + .expect("IPv6 whitelist entry must load into whitelist6_map"); + + // A live prefix ban must not be removed by the periodic reaper. + filter + .ban_cidr(ip("10.1.2.3"), 24, 600) + .expect("CIDR ban must be accepted"); + assert_eq!( + filter.reap_expired_bans().expect("reaper must run"), + 0, + "a ban that expires in 10 minutes must survive the reaper" + ); + + // A zero-duration ban is already expired on the monotonic clock. + filter + .ban_cidr(ip("10.9.8.7"), 24, 0) + .expect("zero-duration ban must be accepted"); + assert!( + filter.reap_expired_bans().expect("reaper must run") >= 1, + "the expired prefix ban must be removed" + ); + + // unload() uses the remembered attach flags and verifies mode NONE. + filter + .unload() + .expect("detach must succeed and leave the interface clean"); + drop(filter); + + // A second attach proves no orphaned program remained on the interface. + let mut again = XdpFilter::new(&interface); + again.load().expect("re-attach on the same interface must succeed"); + again.unload().expect("second detach must succeed"); +} diff --git a/tests/xdp_ban_keys.rs b/tests/xdp_ban_keys.rs new file mode 100644 index 0000000..ef42858 --- /dev/null +++ b/tests/xdp_ban_keys.rs @@ -0,0 +1,100 @@ +//! Ban-key encoding for the IPv4 LPM trie and CIDR normalization. +//! +//! No kernel needed: these exercise the pure functions the loader uses before +//! touching `blacklist_map`. Regression class: two bans of the same /24 with +//! different host bits used to produce distinct LPM keys, and a prefix length +//! of 0 or above the address width was accepted silently. +#![cfg(feature = "xdp")] + +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + +use rampart::xdp::{blacklist_expiry_ns, blacklist_key, normalize_cidr}; + +#[test] +fn blacklist_key_puts_prefix_len_first_and_octets_last() { + assert_eq!(blacklist_key(24, &[10, 0, 1, 200]), [24, 0, 0, 0, 10, 0, 1, 200]); + assert_eq!(blacklist_key(32, &[127, 0, 0, 1]), [32, 0, 0, 0, 127, 0, 0, 1]); +} + +#[test] +fn blacklist_key_encodes_prefix_len_as_little_endian_u32() { + let key = blacklist_key(8, &[10, 1, 2, 3]); + assert_eq!(u32::from_le_bytes(key[0..4].try_into().expect("4 bytes")), 8); +} + +#[test] +fn blacklist_expiry_converts_secs_to_ns() { + assert_eq!(blacklist_expiry_ns(1_000, 2), 1_000 + 2_000_000_000); +} + +#[test] +fn blacklist_expiry_saturates_instead_of_wrapping() { + assert_eq!(blacklist_expiry_ns(u64::MAX, 1), u64::MAX); + assert_eq!(blacklist_expiry_ns(0, u64::MAX), u64::MAX); +} + +#[test] +fn normalize_cidr_masks_host_bits_for_a_slash_24() { + let net = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 24).expect("valid /24"); + assert_eq!(net, IpAddr::V4(Ipv4Addr::new(10, 1, 2, 0))); +} + +#[test] +fn without_masking_host_bits_would_produce_different_ban_keys() { + // This is the regression: these two keys denote the same /24 but differ. + assert_ne!(blacklist_key(24, &[10, 1, 2, 3]), blacklist_key(24, &[10, 1, 2, 0])); +} + +#[test] +fn a_whole_subnet_shares_one_ban_key_after_normalization() { + let first = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 24).expect("valid /24"); + let second = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 99)), 24).expect("valid /24"); + let (IpAddr::V4(first), IpAddr::V4(second)) = (first, second) else { + return; + }; + assert_eq!(blacklist_key(24, &first.octets()), blacklist_key(24, &second.octets())); +} + +#[test] +fn normalize_cidr_keeps_slash_32_unchanged() { + let addr = IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)); + assert_eq!(normalize_cidr(addr, 32).expect("valid /32"), addr); +} + +#[test] +fn normalize_cidr_masks_host_bits_for_ipv6() { + let addr = IpAddr::V6("2001:db8:1:2:3:4:5:6".parse::().expect("literal")); + let net = normalize_cidr(addr, 64).expect("valid /64"); + assert_eq!(net, IpAddr::V6("2001:db8:1:2::".parse::().expect("literal"))); +} + +#[test] +fn normalize_cidr_masks_partial_bytes() { + let net = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 255)), 20).expect("valid /20"); + assert_eq!(net, IpAddr::V4(Ipv4Addr::new(10, 1, 0, 0))); +} + +#[test] +fn normalize_cidr_keeps_slash_128_unchanged() { + let addr = IpAddr::V6("2001:db8::1".parse::().expect("literal")); + assert_eq!(normalize_cidr(addr, 128).expect("valid /128"), addr); +} + +#[test] +fn normalize_cidr_rejects_prefix_zero() { + let err = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 0).expect_err("prefix 0 is invalid"); + assert!(err.to_string().contains("invalid IPv4 prefix length 0")); +} + +#[test] +fn normalize_cidr_rejects_ipv4_prefix_above_32() { + let err = normalize_cidr(IpAddr::V4(Ipv4Addr::new(10, 1, 2, 3)), 33).expect_err("prefix 33 is invalid"); + assert!(err.to_string().contains("invalid IPv4 prefix length 33")); +} + +#[test] +fn normalize_cidr_rejects_ipv6_prefix_above_128() { + let addr = IpAddr::V6("2001:db8::1".parse::().expect("literal")); + let err = normalize_cidr(addr, 129).expect_err("prefix 129 is invalid"); + assert!(err.to_string().contains("invalid IPv6 prefix length 129")); +} diff --git a/tests/xdp_loader.rs b/tests/xdp_loader.rs new file mode 100644 index 0000000..ac22ac1 --- /dev/null +++ b/tests/xdp_loader.rs @@ -0,0 +1,41 @@ +//! 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 `
` 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"); +} diff --git a/tests/xdp_object_maps.rs b/tests/xdp_object_maps.rs index a075fc6..8abc0ad 100644 --- a/tests/xdp_object_maps.rs +++ b/tests/xdp_object_maps.rs @@ -29,6 +29,7 @@ fn every_map_the_loader_uses_exists_in_the_object() { "blacklist_map", "blacklist6_map", "whitelist_map", + "whitelist6_map", "udp_rate_limit", "udp_rate_limit6", ] { @@ -56,3 +57,34 @@ fn rodata_map_value_size_matches_loader_image() { "sizeof(struct rampart_config) in config.h must equal sizeof(XdpGlobals)" ); } + +#[test] +fn map_key_and_value_sizes_match_the_rust_encoding() { + let obj = open_object(); + // (map, key size, value size) as encoded by src/xdp/filter/maps.rs and + // src/xdp/probe/stats.rs. The C structs come from xdp/core/common.h, + // xdp/core/prefix_stats.h and xdp/core/maps.h. + let expected = [ + ("blacklist_map", 8, 8), + ("blacklist6_map", 20, 8), + ("whitelist_map", 8, 1), + ("whitelist6_map", 20, 1), + ("prefix_stats", 24, 24), + ("stats_map", 4, 8), + ]; + for (name, key_size, value_size) in expected { + let map = obj.maps().find(|m| m.name().to_string_lossy() == name); + assert!(map.is_some(), "map '{name}' is missing from the XDP object"); + let map = map.expect("map existence asserted above"); + assert_eq!( + map.key_size(), + key_size, + "map '{name}' key size drifted from the Rust encoding" + ); + assert_eq!( + map.value_size(), + value_size, + "map '{name}' value size drifted from the Rust encoding" + ); + } +} diff --git a/tests/xdp_services.rs b/tests/xdp_services.rs new file mode 100644 index 0000000..8518dfc --- /dev/null +++ b/tests/xdp_services.rs @@ -0,0 +1,79 @@ +//! Edge service wiring: whitelist parsing and the XDP startup path. +//! +//! These used to be inline `#[cfg(test)]` tests in `src/app/services.rs`; the +//! project keeps tests out of source files (TODO.md §4). Kernel-touching +//! behaviour is not exercised here: `start_xdp` is called with `[xdp]` +//! disabled, and the whitelist-map load needs a live interface. + +use std::net::IpAddr; +use std::sync::Arc; + +use rampart::app::services::{build_whitelist, start_xdp}; +use rampart::config::{Config, StoreConfig}; +use rampart::filter::blacklist::Blacklist; +use tokio::sync::watch; + +fn config_with_redis() -> Config { + Config { + store: StoreConfig { + redis_url: Some("redis://127.0.0.1:6379/0".to_string()), + ..Default::default() + }, + ..Default::default() + } +} + +#[cfg(not(feature = "store-redis"))] +#[test] +fn redis_config_without_feature_fails_fast() { + let err = build_whitelist(&config_with_redis()).expect_err("must fail without store-redis"); + assert!(err.to_string().contains("built without store-redis feature")); +} + +#[cfg(not(feature = "store-redis"))] +#[test] +fn empty_store_config_passes_without_feature() { + assert!(build_whitelist(&Config::default()).is_ok()); +} + +#[cfg(feature = "store-redis")] +#[test] +fn redis_config_passes_with_store_feature() { + assert!(build_whitelist(&config_with_redis()).is_ok()); +} + +#[test] +fn build_whitelist_parses_both_address_families() { + let config = Config { + whitelist: vec!["192.0.2.10".to_string(), "2001:db8::1".to_string()], + ..Default::default() + }; + let set = build_whitelist(&config).expect("valid whitelist entries"); + assert!(set.contains(&"192.0.2.10".parse::().expect("ipv4 literal"))); + assert!(set.contains(&"2001:db8::1".parse::().expect("ipv6 literal"))); + assert_eq!(set.len(), 2); +} + +#[test] +fn build_whitelist_rejects_a_malformed_entry() { + let config = Config { + whitelist: vec!["definitely-not-an-ip".to_string()], + ..Default::default() + }; + let err = build_whitelist(&config).expect_err("malformed entry must fail"); + assert!(err.to_string().contains("invalid whitelist entry")); +} + +/// XDP auto-ban wiring: `start_xdp` must accept the `Arc` and, with +/// `[xdp]` disabled, touch neither the kernel nor the blacklist. The +/// `set_autoban` -> `load()` order inside the enabled branch cannot be checked +/// without a kernel. +#[test] +fn start_xdp_accepts_blacklist_and_skips_kernel_when_disabled() { + let (_tx, rx) = watch::channel(false); + let blacklist = Arc::new(Blacklist::new()); + let started = + start_xdp(&Arc::new(Config::default()), &rx, &blacklist).expect("xdp disabled: no kernel work and no error"); + assert!(started.is_none()); + assert!(blacklist.is_empty()); +} diff --git a/tests/xdp_whitelist_keys.rs b/tests/xdp_whitelist_keys.rs new file mode 100644 index 0000000..cf00dfe --- /dev/null +++ b/tests/xdp_whitelist_keys.rs @@ -0,0 +1,42 @@ +//! 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]); +} diff --git a/xdp/core/common.h b/xdp/core/common.h index 7a4361d..e0ad622 100644 --- a/xdp/core/common.h +++ b/xdp/core/common.h @@ -7,11 +7,25 @@ #define ETH_P_IP 0x0800 #define ETH_P_IPV6 0x86DD +#define ETH_P_8021Q 0x8100 +#define ETH_P_8021AD 0x88A8 #define IPPROTO_TCP 6 #define IPPROTO_UDP 17 +#define IPPROTO_FRAGMENT 44 #define IP_OFFSET 0x1FFF #define IP_MF 0x2000 +/** + * One 802.1Q / 802.1ad tag. + * + * The filter only needs the encapsulated EtherType to skip up to two tags + * before L3; the TCI (PCP/DEI/VID) has no bearing on L3/L4 policy. + */ +struct rampart_vlan_hdr { + __u16 tci; // PCP/DEI/VID, unused + __u16 encap_proto; // EtherType of the next header +}; + /** * Protocol-agnostic TCP connection states. * diff --git a/xdp/core/maps.h b/xdp/core/maps.h index 9f1953b..3644d57 100644 --- a/xdp/core/maps.h +++ b/xdp/core/maps.h @@ -6,8 +6,9 @@ * * An LPM trie so operators can ban a CIDR range rather than single hosts. * The value is the ban expiry as a bpf_ktime_get_ns() timestamp; expired - * entries are removed lazily by the lookup helpers. BPF_F_NO_PREALLOC is - * required by the LPM trie type. + * entries are ignored by the packet path and reaped from userspace, which + * knows the prefix length. BPF_F_NO_PREALLOC is required by the LPM trie + * type. */ struct { __uint(type, BPF_MAP_TYPE_LPM_TRIE); @@ -34,8 +35,9 @@ struct { /** * Whitelist. * - * LPM trie of trusted CIDRs; the filter consults it before any other check - * so allowlisted traffic can never be throttled or banned away. + * LPM trie of trusted CIDRs; the filter consults it before the port gate, + * conntrack and blacklists so allowlisted traffic can never be throttled or + * banned away. */ struct { __uint(type, BPF_MAP_TYPE_LPM_TRIE); @@ -45,11 +47,28 @@ struct { __uint(map_flags, BPF_F_NO_PREALLOC); } whitelist_map SEC(".maps"); +/** + * IPv6 whitelist. + * + * IPv6 twin of whitelist_map: a 20-byte LPM key (host-order prefixlen + * followed by 16 address bytes) and the same early position in the packet + * path. Lookups use prefixlen 128 for the exact source address. + */ +struct { + __uint(type, BPF_MAP_TYPE_LPM_TRIE); + __uint(max_entries, 1000); + __type(key, struct lpm_key6); + __type(value, __u8); + __uint(map_flags, BPF_F_NO_PREALLOC); +} whitelist6_map SEC(".maps"); + /** * Connection tracking table (SYN_RECEIVED to ESTABLISHED). * * LRU so a flood of half-open flows evicts old entries instead of failing - * map updates; RST/FIN removes entries explicitly on the normal path. + * map updates; RST/FIN removes entries explicitly on the normal path. Only + * new connections are policed: a non-SYN segment whose entry was evicted + * passes, because this is a stateless filter, not a firewall. */ struct { __uint(type, BPF_MAP_TYPE_LRU_HASH); @@ -155,6 +174,7 @@ struct { #define STAT_CHALLENGE_SENT 9 #define STAT_CHALLENGE_VERIFIED 10 #define STAT_CHALLENGE_FAILED 11 +#define STAT_FRAGMENT_BYPASS 12 /** * Per-CPU counter storage. diff --git a/xdp/core/stats.h b/xdp/core/stats.h index af0f390..b40f13b 100644 --- a/xdp/core/stats.h +++ b/xdp/core/stats.h @@ -8,7 +8,9 @@ * Increment one per-CPU counter. * * A missing slot (never updated, or an index past max_entries) is ignored so - * accounting can never change a packet verdict. + * accounting can never change a packet verdict. The slot is private to the + * current CPU, so a plain read-modify-write is enough: an atomic would only + * add contention on the hottest path. * * @param idx STAT_* index into stats_map */ @@ -16,7 +18,7 @@ static __always_inline void inc_stat(__u32 idx) { __u64 *val = bpf_map_lookup_elem(&stats_map, &idx); if (val) - __sync_fetch_and_add(val, 1); + (*val)++; } /** @@ -35,6 +37,7 @@ static __always_inline void inc_rate_limit(void) { inc_stat(STAT_RATE_LIMIT); static __always_inline void inc_chal_sent(void) { inc_stat(STAT_CHALLENGE_SENT); } static __always_inline void inc_chal_verified(void) { inc_stat(STAT_CHALLENGE_VERIFIED); } static __always_inline void inc_chal_failed(void) { inc_stat(STAT_CHALLENGE_FAILED); } +static __always_inline void inc_fragment_bypass(void) { inc_stat(STAT_FRAGMENT_BYPASS); } static __always_inline void inc_pass(void) { inc_stat(STAT_PASS); } static __always_inline void inc_drop(void) { inc_stat(STAT_DROP); } diff --git a/xdp/core/syn_challenge.h b/xdp/core/syn_challenge.h index 4fb6463..1b3c53f 100644 --- a/xdp/core/syn_challenge.h +++ b/xdp/core/syn_challenge.h @@ -129,7 +129,10 @@ static __always_inline void chal_build_synack(struct xdp_md *ctx, __builtin_memcpy(tcpw, tcp, 20); if (is_ipv6) { - __builtin_memcpy(saddr, &((struct ipv6hdr *)l3)->daddr, 16); // swapped + // saddr/daddr hold the addresses of the reply: the new source is the + // original destination and the new destination is the original + // source. The checksum below is computed over exactly these values. + __builtin_memcpy(saddr, &((struct ipv6hdr *)l3)->daddr, 16); __builtin_memcpy(daddr, &((struct ipv6hdr *)l3)->saddr, 16); } else { saddr[0] = ((struct iphdr *)l3)->daddr; @@ -191,8 +194,11 @@ static __always_inline void chal_build_synack(struct xdp_md *ctx, __builtin_memcpy(eth->h_source, md, ETH_ALEN); if (is_ipv6) { struct ipv6hdr *ip6 = l3; - __builtin_memcpy(&ip6->saddr, daddr, 16); - __builtin_memcpy(&ip6->daddr, saddr, 16); + // Write the header with the same orientation the checksum was built + // from: saddr is the new source (original destination), daddr the + // new destination (original source). + __builtin_memcpy(&ip6->saddr, saddr, 16); + __builtin_memcpy(&ip6->daddr, daddr, 16); ip6->hop_limit = 64; } else { __builtin_memcpy(l3, ipw, 20); @@ -283,10 +289,13 @@ static __always_inline int chal_on_rst(struct tcphdr *tcp, __u32 ack = bpf_ntohl(tcp->ack_seq); __u8 ok = (seq == p->bad_ack) || (tcp->ack && ack == p->marker + 1); + __u64 sent_at = p->sent_at; + // Copy every field needed before deleting: an LRU element may be freed + // as soon as it is removed, so p must never be dereferenced afterwards. bpf_map_delete_elem(&challenge_pending, flow); - if (!ok || now - p->sent_at > timeout_ns) { + if (!ok || now - sent_at > timeout_ns) { inc_chal_failed(); return XDP_DROP; } diff --git a/xdp/core/universal_filter.c b/xdp/core/universal_filter.c index 9852428..7dd7f9e 100644 --- a/xdp/core/universal_filter.c +++ b/xdp/core/universal_filter.c @@ -5,13 +5,20 @@ * owns, in this order: * * 1. No pure-ACK drop (prevents TCP handshake deadlock). - * 2. LRU conntrack: SYN_RECEIVED to ESTABLISHED, with sequence tracking. + * 2. LRU conntrack, consulted only for new (SYN) connections. A non-SYN + * segment with no tracked state passes: this is a stateless filter, not + * a firewall, so spoofed SYNs cannot black-hole established flows and + * ordinary replies are never dropped for lack of state. * 3. RST/FIN removes the conntrack entry, so no stale state survives. * 4. IPv6 support alongside IPv4. - * 5. IP/CIDR blacklist and whitelist (LPM trie). + * 5. IP/CIDR blacklist and whitelist (LPM trie, IPv4 and IPv6). * 6. Per-IP SYN throttle, with invalid TCP flag combinations dropped. * 7. UDP policy from config.h: pass, drop or rate-limit. * + * L2 decapsulation peels up to two VLAN tags before L3. IP fragments carry + * no inspectable ports, so they only get the source blacklist and then pass + * (dropping first fragments would break legitimate large datagrams). + * * Deep payload inspection is delegated to an optional protocol hook (see * ../hooks/hook_api.h). Without a hook, established connections pass. */ @@ -70,7 +77,12 @@ static __always_inline __u8 detect_tcp_bypass(struct tcphdr *tcp) } /** - * Check the IPv4 blacklist and expire the entry on the way out. + * Check the IPv4 blacklist. + * + * The /32 lookup cannot address a wider /24 (or any other prefix) stored in + * the trie, so an expired entry is only ignored here; userspace reaps it + * with the prefix length it was created with. Deleting by /32 from the + * packet path used to leak every prefix ban until the trie filled up. * * @param src_ip source address, network byte order * @param now current bpf_ktime_get_ns() timestamp @@ -82,17 +94,15 @@ static __always_inline __u8 is_blacklisted(__u32 src_ip, __u64 now) __u64 *ban_until = bpf_map_lookup_elem(&blacklist_map, &key); if (!ban_until) return 0; - if (*ban_until > now) - return 1; - bpf_map_delete_elem(&blacklist_map, &key); // expired — clean up - return 0; + return *ban_until > now; } /** - * Check the IPv6 blacklist and expire the entry on the way out. + * Check the IPv6 blacklist. * * Key layout mirrors bpf_lpm_trie_key: prefixlen in host order followed by - * 16 address bytes. + * 16 address bytes. Expired entries are ignored, never deleted here, for the + * same reason as in is_blacklisted(). * * @param addr 16-byte source address, network byte order * @param now current bpf_ktime_get_ns() timestamp @@ -106,10 +116,7 @@ static __always_inline __u8 is_blacklisted6(const __u8 addr[16], __u64 now) __u64 *ban_until = bpf_map_lookup_elem(&blacklist6_map, &key); if (!ban_until) return 0; - if (*ban_until > now) - return 1; - bpf_map_delete_elem(&blacklist6_map, &key); // expired — clean up - return 0; + return *ban_until > now; } /** @@ -218,8 +225,25 @@ int rampart_universal_filter(struct xdp_md *ctx) if ((void *)(eth + 1) > data_end) return XDP_PASS; + // Strip up to two VLAN tags (802.1Q / 802.1ad). Tagged frames used to hit + // the non-IP early return, which let a banned source bypass the blacklist + // with VLAN/QinQ encapsulation. + void *l3 = (void *)(eth + 1); + __u16 eth_proto = eth->h_proto; +#pragma unroll + for (int i = 0; i < 2; i++) { + if (eth_proto != bpf_htons(ETH_P_8021Q) && + eth_proto != bpf_htons(ETH_P_8021AD)) + break; + struct rampart_vlan_hdr *vh = l3; + if ((void *)(vh + 1) > data_end) + return XDP_PASS; + eth_proto = vh->encap_proto; + l3 = (void *)(vh + 1); + } + // Non-IP traffic: pass (ARP, etc.) - if (eth->h_proto != bpf_htons(ETH_P_IP) && eth->h_proto != bpf_htons(ETH_P_IPV6)) + if (eth_proto != bpf_htons(ETH_P_IP) && eth_proto != bpf_htons(ETH_P_IPV6)) return XDP_PASS; // Parse the IP header. @@ -228,36 +252,48 @@ int rampart_universal_filter(struct xdp_md *ctx) __u8 l4_proto = 0; __u8 is_ipv6 = 0; __u8 native_v6 = 0; + __u8 is_fragment = 0; __u8 src6[16]; - if (eth->h_proto == bpf_htons(ETH_P_IP)) { - struct iphdr *ip = (void *)(eth + 1); + if (eth_proto == bpf_htons(ETH_P_IP)) { + struct iphdr *ip = l3; if ((void *)(ip + 1) > data_end) return XDP_PASS; - if (ip->ihl < 5) - return XDP_DROP; - - if (ip->protocol != IPPROTO_TCP && ip->protocol != IPPROTO_UDP) - return XDP_PASS; - - // Non-sequential fragment: pass (can't inspect ports safely) - if (ip->frag_off & bpf_htons(IP_OFFSET)) - return XDP_PASS; - - // First fragment with MF: drop (can't reassemble) - if (ip->frag_off & bpf_htons(IP_MF)) + if (ip->ihl < 5) { + inc_drop(); return XDP_DROP; + } src_ip = ip->saddr; dst_ip = ip->daddr; l4_proto = ip->protocol; + + // MF or a non-zero offset: this packet has no reliable L4 header. + // Both first and later fragments are handled by the source blacklist + // below and then passed; dropping first fragments would break + // legitimate large datagrams. + if (ip->frag_off & bpf_htons(IP_OFFSET | IP_MF)) { + is_fragment = 1; + } else if (ip->protocol != IPPROTO_TCP && ip->protocol != IPPROTO_UDP) { + return XDP_PASS; + } } else { - struct ipv6hdr *ip6 = (void *)(eth + 1); + struct ipv6hdr *ip6 = l3; if ((void *)(ip6 + 1) > data_end) return XDP_PASS; - if (ip6->nexthdr != IPPROTO_TCP && ip6->nexthdr != IPPROTO_UDP) + // Keep the full address for the IPv6 whitelist and blacklist even + // when it is IPv4-mapped; the low 32 bits are used for flow keys. + __builtin_memcpy(src6, &ip6->saddr, 16); + is_ipv6 = 1; + l4_proto = ip6->nexthdr; + + // A fragment header hides the L4 header; same policy as IPv4. + if (ip6->nexthdr == IPPROTO_FRAGMENT) { + is_fragment = 1; + } else if (ip6->nexthdr != IPPROTO_TCP && ip6->nexthdr != IPPROTO_UDP) { return XDP_PASS; // extension headers not walked + } // Use IPv4-mapped IPv6 address for flow key (::ffff:0:0/96) if (ip6->saddr.in6_u.u6_addr32[0] != 0 || @@ -270,37 +306,70 @@ int rampart_universal_filter(struct xdp_md *ctx) // separately after the generic L4/port gates and must never // touch the IPv4 maps. native_v6 = 1; - is_ipv6 = 1; - l4_proto = ip6->nexthdr; - __builtin_memcpy(src6, &ip6->saddr, 16); } else { // Source address must come from saddr, never daddr, or the // filter would track the server instead of the client. src_ip = ip6->saddr.in6_u.u6_addr32[3]; dst_ip = ip6->daddr.in6_u.u6_addr32[3]; - l4_proto = ip6->nexthdr; - is_ipv6 = 1; } } + __u64 now = bpf_ktime_get_ns(); + + // Whitelist check (LPM trie), before the port gate, conntrack and both + // blacklists: a whitelisted source must truly bypass the state machine. + if (is_ipv6) { + struct lpm_key6 wl6 = {}; + wl6.prefixlen = 128; + __builtin_memcpy(wl6.addr, src6, 16); + if (bpf_map_lookup_elem(&whitelist6_map, &wl6)) { + inc_whitelist(); + return XDP_PASS; + } + } else { + struct lpm_key wl = { .prefixlen = 32, .ip = src_ip }; + if (bpf_map_lookup_elem(&whitelist_map, &wl)) { + inc_whitelist(); + return XDP_PASS; + } + } + + // Fragments: the L4 ports are missing or incomplete, so the stateful and + // throttle logic cannot run. Apply the source blacklist and pass; the + // dedicated counter keeps this deliberate bypass observable. + if (is_fragment) { + __u8 banned = native_v6 ? is_blacklisted6(src6, now) + : is_blacklisted(src_ip, now); + if (banned) { + inc_blacklist(); + inc_drop(); + if (!native_v6 && G_FEATURE_EVENTS) + push_event(EVENT_BAN, src_ip, 0); + return XDP_DROP; + } + inc_fragment_bypass(); + inc_pass(); + return XDP_PASS; + } + // Parse the L4 header. __u8 ip_hdr_len; void *l4; if (is_ipv6) { - struct ipv6hdr *ip6 = (void *)(eth + 1); + struct ipv6hdr *ip6 = l3; l4 = (void *)(ip6 + 1); ip_hdr_len = sizeof(struct ipv6hdr); } else { - struct iphdr *ip = (void *)(eth + 1); + struct iphdr *ip = l3; l4 = (void *)ip + (ip->ihl * 4); ip_hdr_len = ip->ihl * 4; } // Minimum L4 header per protocol: a short UDP datagram used to fall through // to XDP_PASS because the check was sized for TCP, letting it bypass the - // blacklist entirely. IPv6 native fragmentation is already filtered above - // (nexthdr must be TCP/UDP), so a short UDP header here is a real datagram. + // blacklist entirely. Fragments were already handled above (source + // blacklist only), so a short UDP header here is a real datagram. __u32 min_l4 = (l4_proto == IPPROTO_TCP) ? sizeof(struct tcphdr) : sizeof(struct udphdr); if ((void *)l4 + min_l4 > data_end) @@ -321,8 +390,6 @@ int rampart_universal_filter(struct xdp_md *ctx) else inc_udp(); - __u64 now = bpf_ktime_get_ns(); - // Native IPv6: blacklist plus cheap L4 policy (no stateful v6 yet). if (native_v6) { if (is_blacklisted6(src6, now)) { @@ -363,13 +430,6 @@ int rampart_universal_filter(struct xdp_md *ctx) return XDP_PASS; } - // Whitelist check (CIDR), before any filtering. - struct lpm_key wl_key = { .prefixlen = 32, .ip = src_ip }; - if (bpf_map_lookup_elem(&whitelist_map, &wl_key)) { - inc_whitelist(); - return XDP_PASS; - } - // Blacklist check (CIDR with expiry). if (is_blacklisted(src_ip, now)) { inc_blacklist(); @@ -414,23 +474,52 @@ int rampart_universal_filter(struct xdp_md *ctx) return XDP_DROP; } - // TCP header length check - if (tcp->doff < 5) + // TCP header length check. + if (tcp->doff < 5) { + inc_drop(); return XDP_DROP; + } __u8 tcp_hdr_len = tcp->doff * 4; - if ((void *)data + sizeof(struct ethhdr) + ip_hdr_len + tcp_hdr_len > data_end) + if ((void *)l4 + tcp_hdr_len > data_end) { + inc_drop(); return XDP_DROP; + } // Per-prefix stats key (/24), built once after blacklist and bypass drops. struct prefix_key pkey; prefix_key_from_v4(&pkey, src_ip); - // Compute payload pointers + // The payload window is bounded by the IP header length field, not by + // data_end: a real NIC pads the last frame to the 60-byte Ethernet + // minimum, so counting to data_end would treat the padding as payload, + // drift expected_seq and drop the connection after a few segments. The + // frame can still be shorter than the IP header claims, so cap at + // data_end as well. + __u32 l4_len; + if (is_ipv6) { + struct ipv6hdr *ip6 = l3; + l4_len = sizeof(struct ipv6hdr) + bpf_ntohs(ip6->payload_len); + } else { + struct iphdr *ip = l3; + __u16 tot_len = bpf_ntohs(ip->tot_len); + if (tot_len < ip_hdr_len) { + inc_drop(); + return XDP_DROP; + } + l4_len = (__u32)(tot_len - ip_hdr_len); + } + + __u32 frame_l4 = (__u32)((__u8 *)data_end - (__u8 *)l4); + if (l4_len > frame_l4) + l4_len = frame_l4; + if (l4_len < tcp_hdr_len) { + inc_drop(); + return XDP_DROP; + } + + __u32 tcp_payload_len = l4_len - tcp_hdr_len; __u8 *tcp_payload = (__u8 *)tcp + tcp_hdr_len; - __u8 *tcp_payload_end = (__u8 *)data_end; - __s32 tcp_payload_len = (__s32)(tcp_payload_end - tcp_payload); - if (tcp_payload_len < 0) - tcp_payload_len = 0; + __u8 *tcp_payload_end = tcp_payload + tcp_payload_len; // Build the flow key. struct flow_key flow = { @@ -458,8 +547,14 @@ int rampart_universal_filter(struct xdp_md *ctx) // Placed after throttle and blacklist so abusive sources never get a // reply, mirroring the prefix_stats ordering. if (G_SYN_CHALLENGE_ENABLED) { - int cv = chal_on_syn(ctx, eth, (void *)(eth + 1), is_ipv6, - tcp, &flow, now); + // The challenge reply is rebuilt as a bare 20-byte IPv4 header, + // so an IPv4 SYN with options cannot be answered without shipping + // a malformed SYN-ACK. Drop it (counted) instead. + if (!is_ipv6 && ((struct iphdr *)l3)->ihl != 5) { + inc_drop(); + return XDP_DROP; + } + int cv = chal_on_syn(ctx, eth, l3, is_ipv6, tcp, &flow, now); if (cv != CHAL_CONTINUE) return cv; } @@ -467,10 +562,12 @@ int rampart_universal_filter(struct xdp_md *ctx) // Count SYN only after throttle passed (don't count throttled SYNs) update_prefix_stats(&pkey, 1, now); - // Create conntrack entry for new connection + // Create conntrack entry for new connection. A SYN may carry data + // (TCP Fast Open), which is acknowledged as part of the handshake; + // expected_seq must advance past it or the completing ACK is dropped. struct conntrack_entry ce = {}; ce.state = STATE_SYN_RECEIVED; - ce.expected_seq = bpf_ntohl(tcp->seq) + 1; // expect ACK seq + ce.expected_seq = bpf_ntohl(tcp->seq) + 1 + tcp_payload_len; ce.src_ip = src_ip; ce.src_port = tcp->source; ce.last_seen = now; @@ -499,9 +596,12 @@ int rampart_universal_filter(struct xdp_md *ctx) struct conntrack_entry *conn = bpf_map_lookup_elem(&conntrack_map, &flow); if (!conn) { - // Unknown connection — drop - inc_drop(); - return XDP_DROP; + // Stateless filter, not a firewall: only new connections (SYN) are + // policed. An untracked non-SYN segment — a flow evicted under SYN + // pressure, a connection opened before attach, or the reply to the + // daemon's own outbound connection — passes instead of being dropped. + inc_pass(); + return XDP_PASS; } conn->last_seen = now; @@ -514,19 +614,28 @@ int rampart_universal_filter(struct xdp_md *ctx) // Sequence number tracking, established connections only. __u32 seq = bpf_ntohl(tcp->seq); - if (conn->state == STATE_ESTABLISHED && tcp_payload_len > 0 && - seq != conn->expected_seq) { - conn->fails++; - if (conn->fails >= G_MAX_OUT_OF_ORDER) { - bpf_map_delete_elem(&conntrack_map, &flow); - inc_drop(); - if (G_FEATURE_EVENTS) - push_event(EVENT_CONN_DROP, src_ip, conn->fails); - return XDP_DROP; + if (conn->state == STATE_ESTABLISHED && tcp_payload_len > 0) { + if (seq != conn->expected_seq) { + conn->fails++; + if (conn->fails >= G_MAX_OUT_OF_ORDER) { + // Copy the counter before deleting: the LRU element may be + // freed by bpf_map_delete_elem, so conn must not be read + // afterwards. + __u8 fails = conn->fails; + bpf_map_delete_elem(&conntrack_map, &flow); + inc_drop(); + if (G_FEATURE_EVENTS) + push_event(EVENT_CONN_DROP, src_ip, fails); + return XDP_DROP; + } + // Allow retransmission (don't update expected_seq) + inc_pass(); + return XDP_PASS; } - // Allow retransmission (don't update expected_seq) - inc_pass(); - return XDP_PASS; + // In-order segment: reset the reordering counter, so isolated + // reordered/retransmitted segments over the connection lifetime can + // never accumulate into a false positive. + conn->fails = 0; } // State machine. diff --git a/xdp/hooks/hook_api.h b/xdp/hooks/hook_api.h index 9038e86..18db94a 100644 --- a/xdp/hooks/hook_api.h +++ b/xdp/hooks/hook_api.h @@ -28,7 +28,8 @@ * * struct cursor bounds-checked window over the TCP payload, where pos is * the first payload byte and end is one past the last byte - * of the packet (validated against data_end). + * of the payload as declared by the IP header, capped by + * data_end (Ethernet padding is never part of the window). * struct pkt_meta read-only metadata: the 4-tuple (ports in host byte * order), payload length, the full TCP flags byte and the * L3 family. @@ -98,7 +99,7 @@ */ struct cursor { __u8 *pos; // next unparsed byte - __u8 *end; // one past last valid byte (== data_end) + __u8 *end; // one past the last payload byte (IP length capped by data_end) }; /**