guard/xdp/core/prefix_stats.h
loki5512344 9baae25acb
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.
2026-11-16 12:36:19 +01:00

147 lines
4.6 KiB
C

#ifndef RAMPART_PREFIX_STATS_H
#define RAMPART_PREFIX_STATS_H
#include "common.h"
/**
* Per-prefix traffic statistics.
*
* The key layout is shared with the Rust userspace, which reads and resets
* these counters, so the fields must not be reordered. Coarse prefixes rather
* than single hosts keep the table small while still exposing a source
* subnet that floods.
*/
/**
* Prefix key.
*
* family selects the prefix width (see PREFIX_FAMILY_V4 / PREFIX_FAMILY_V6);
* addr holds network-order prefix bytes, zero-padded to 16.
*/
struct prefix_key {
__u8 family;
__u8 pad[7];
__u8 addr[16]; // network-order prefix bytes, zero-padded
};
/**
* Counters accumulated for one prefix.
*
* last_seen_ns is a bpf_ktime_get_ns() timestamp, so userspace can ignore
* prefixes that went quiet without keeping its own clock.
*/
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
};
/**
* Prefix statistics table.
*
* LRU because the number of distinct sources is attacker-controlled; the
* userspace detector reads and resets the entries on its own schedule.
*/
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 65536);
__type(key, struct prefix_key);
__type(value, struct prefix_stats_val);
} prefix_stats SEC(".maps");
#define PREFIX_FAMILY_V4 4
#define PREFIX_FAMILY_V6 6
/**
* Build a /24 IPv4 prefix key.
*
* Copies the address in network order and clears the low octet, which is
* exactly a /24 mask without an arithmetic round trip.
*
* @param key key to initialise
* @param src_ip_net_order source address, network byte order
*/
static __always_inline void prefix_key_from_v4(struct prefix_key *key,
__u32 src_ip_net_order)
{
__builtin_memset(key, 0, sizeof(*key));
key->family = PREFIX_FAMILY_V4;
__builtin_memcpy(key->addr, &src_ip_net_order, 4);
key->addr[3] = 0; // mask the low octet to get a /24
}
/**
* Build a /64 IPv6 prefix key.
*
* Only the high 64 bits are copied; the remainder stays zero from the
* memset, giving a /64 mask in network order.
*
* @param key key to initialise
* @param src_addr 16-byte source address, network byte order
*/
static __always_inline void prefix_key_from_v6(struct prefix_key *key,
const __u8 src_addr[16])
{
__builtin_memset(key, 0, sizeof(*key));
key->family = PREFIX_FAMILY_V6;
__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.
*
* Creates the entry on first sight. is_syn selects the counter to bump, so a
* SYN flood and a payload flood stay distinguishable downstream; last_seen_ns
* is refreshed on every call. The rate window itself lives in userspace.
*
* @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,
__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,
.last_seen_ns = now,
.source_bits = bit,
};
bpf_map_update_elem(&prefix_stats, key, &init, BPF_ANY);
return;
}
if (is_syn)
__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;
}
#endif /* RAMPART_PREFIX_STATS_H */