guard/xdp/core/prefix_stats.h
loki5512344 918e561a61
feat(xdp): IPv6 UDP rate limit, working loader; English JavaDoc comments
XDP behaviour:
- udp_rate_limit6 (LRU, 128-bit source key) + throttle_hit6, so
  udp_policy = "rate-limit" now applies to native IPv6 as well.
- scripts/xdp_ipv6_smoke.sh: phase 4 patches .rodata before load and
  verifies policy=2 on a real kernel (3 pass / 3 rate-limited).

XDP loader (was completely broken, never reached the verifier):
- .rodata is now one struct rampart_config with frozen offsets enforced by
  _Static_assert; XdpGlobals is #[repr(C)] and the image is built with
  offset_of!. Previously a 96-byte image was written into a 76-byte map at
  offsets clang had reordered, so load() failed with EINVAL.
- patch_rodata fails loudly when the map size and the image size diverge.
- build.rs tracks xdp/core and xdp/hooks, not just universal_filter.c.
- build_ringbuf looked for "events_map"; the map is "events_ringbuf".
- tests/xdp_object_maps.rs: verifies map names and .rodata size without root;
  the real binary was confirmed to attach in a netns.

Docs and style:
- Code comments are English, JavaDoc/Rustdoc style, without emoji or
  decorative banners (new project rule, TODO §0.8).
- README rewritten bilingual: Russian first, English below, TOC and
  copy-paste quick start.
- docs/README.md: documentation index and maintenance rules;
  configuration.md documents the whole [xdp] section and IPv4/IPv6 coverage;
  testing.md describes the real-kernel smoke; stale research links removed.
2026-11-15 23:44:07 +01:00

123 lines
3.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()
};
/**
* 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);
}
/**
* 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
*/
static __always_inline void update_prefix_stats(struct prefix_key *key,
__u8 is_syn, __u64 now)
{
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,
};
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);
val->last_seen_ns = now;
}
#endif /* RAMPART_PREFIX_STATS_H */