feat(xdp): estimate distinct sources in prefix stats, fix the detection window

The subnet detector's spoof gate needs the number of distinct sources in a
prefix, but the XDP path reported 0, so a /24 flooded from a botnet could never
reach the Block verdict and the whole subnet detector was inert with XDP on.

- prefix_stats carries a 64-bit bitmap of hashed source addresses (interface
  ID for IPv6, address for IPv4); userspace counts the set bits.
- read_prefix_stats now resets each entry after reading: the counters are
  documented as per-window and compared against fixed thresholds, but were
  cumulative, so with enough traffic every active prefix eventually looked
  like an attack.
- The monitor no longer suppresses Block on the XDP path and no longer carries
  the now-dead "no source diversity" branch.

ABI: the prefix_stats value grows 24 -> 32 bytes; the xdp_object_maps test
pins the new size.
This commit is contained in:
Loki 2026-11-16 12:36:19 +01:00
parent c1a076a4a3
commit 9baae25acb
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
8 changed files with 88 additions and 56 deletions

View file

@ -34,6 +34,7 @@ struct prefix_stats_val {
__u64 syn_count;
__u64 pkt_count;
__u64 last_seen_ns; // bpf_ktime_get_ns()
__u64 source_bits; // 64-bit bitmap of hashed source addresses
};
/**
@ -87,6 +88,24 @@ static __always_inline void prefix_key_from_v6(struct prefix_key *key,
__builtin_memcpy(key->addr, src_addr, 8);
}
/**
* Maps a 32-bit source identifier to one bit of a 64-bit bitmap.
*
* The subnet detector needs an estimate of distinct sources ("spoof gate":
* a /24 flooded from one IP is not a botnet). Exact counting in XDP would
* need an unbounded map, so we set a bit of a multiplicative hash. Collisions
* can only over-count, and with min_unique_sources=16 the 64-bit space makes
* that unlikely for a real botnet while a single source still sets one bit.
*
* @param src_id identifier that differs per source address
* @return a single-bit mask
*/
static __always_inline __u64 source_bit(__u32 src_id)
{
__u32 hash = src_id * 2654435761u;
return 1ULL << (hash >> 26);
}
/**
* Increment the counters for one prefix.
*
@ -97,17 +116,21 @@ static __always_inline void prefix_key_from_v6(struct prefix_key *key,
* @param key prefix key, already built
* @param is_syn 1 counts a SYN, 0 counts any other packet
* @param now current bpf_ktime_get_ns() timestamp
* @param src_id 32-bit identifier of the full source address
*/
static __always_inline void update_prefix_stats(struct prefix_key *key,
__u8 is_syn, __u64 now)
__u8 is_syn, __u64 now,
__u32 src_id)
{
__u64 bit = source_bit(src_id);
struct prefix_stats_val *val = bpf_map_lookup_elem(&prefix_stats, key);
if (!val) {
struct prefix_stats_val init = {
.syn_count = is_syn ? 1 : 0,
.pkt_count = is_syn ? 0 : 1,
.syn_count = is_syn ? 1 : 0,
.pkt_count = is_syn ? 0 : 1,
.last_seen_ns = now,
.source_bits = bit,
};
bpf_map_update_elem(&prefix_stats, key, &init, BPF_ANY);
return;
@ -117,6 +140,7 @@ static __always_inline void update_prefix_stats(struct prefix_key *key,
__sync_fetch_and_add(&val->syn_count, 1);
else
__sync_fetch_and_add(&val->pkt_count, 1);
__sync_fetch_and_or(&val->source_bits, bit);
val->last_seen_ns = now;
}

View file

@ -425,7 +425,9 @@ int rampart_universal_filter(struct xdp_md *ctx)
struct prefix_key pkey6;
prefix_key_from_v6(&pkey6, src6);
struct tcphdr *tcp6 = (struct tcphdr *)l4;
update_prefix_stats(&pkey6, (tcp6->syn && !tcp6->ack) ? 1 : 0, now);
__u32 v6_id;
__builtin_memcpy(&v6_id, src6 + 12, 4); // interface ID differs per host
update_prefix_stats(&pkey6, (tcp6->syn && !tcp6->ack) ? 1 : 0, now, v6_id);
inc_pass();
return XDP_PASS;
}
@ -560,7 +562,7 @@ 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);
update_prefix_stats(&pkey, 1, now, src_ip);
// Create conntrack entry for new connection. A SYN may carry data
// (TCP Fast Open), which is acknowledged as part of the handshake;
@ -584,7 +586,7 @@ int rampart_universal_filter(struct xdp_md *ctx)
// Connection tracking lookup.
// pkt_count: all non-SYN TCP that survived blacklist/bypass/throttle
update_prefix_stats(&pkey, 0, now);
update_prefix_stats(&pkey, 0, now, src_ip);
// RST challenge proof: match a pending challenge before conntrack,
// because the RST answers a flow that was never tracked.