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.
This commit is contained in:
parent
295ab81fb4
commit
918e561a61
104 changed files with 2129 additions and 1021 deletions
|
|
@ -1,8 +1,14 @@
|
|||
#ifndef RAMPART_MAPS_H
|
||||
#define RAMPART_MAPS_H
|
||||
|
||||
// 🔗 Blacklist (LPM_TRIE for CIDR support)
|
||||
// Cleared by Rust userspace or per-entry expiry via ringbuf
|
||||
/**
|
||||
* IPv4 blacklist.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
|
||||
__uint(max_entries, 100000);
|
||||
|
|
@ -11,8 +17,12 @@ struct {
|
|||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
} blacklist_map SEC(".maps");
|
||||
|
||||
// 🔗 IPv6 blacklist (LPM_TRIE, 128-bit prefix keys)
|
||||
// Same value/expiry contract as blacklist_map
|
||||
/**
|
||||
* IPv6 blacklist.
|
||||
*
|
||||
* LPM trie over 128-bit prefix keys, the exact analogue of blacklist_map:
|
||||
* same expiry semantics, same required map flags.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
|
||||
__uint(max_entries, 100000);
|
||||
|
|
@ -21,7 +31,12 @@ struct {
|
|||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
} blacklist6_map SEC(".maps");
|
||||
|
||||
// 🔗 Whitelist (LPM_TRIE for CIDR) — checked before any filter
|
||||
/**
|
||||
* Whitelist.
|
||||
*
|
||||
* LPM trie of trusted CIDRs; the filter consults it before any other check
|
||||
* so allowlisted traffic can never be throttled or banned away.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
|
||||
__uint(max_entries, 1000);
|
||||
|
|
@ -30,8 +45,12 @@ struct {
|
|||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
} whitelist_map SEC(".maps");
|
||||
|
||||
// 🔗 Connection tracking (SYN_RECEIVED → ESTABLISHED)
|
||||
// LRU — автоматическое вытеснение старых записей
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, 16384);
|
||||
|
|
@ -39,7 +58,12 @@ struct {
|
|||
__type(value, struct conntrack_entry);
|
||||
} conntrack_map SEC(".maps");
|
||||
|
||||
// 🔗 SYN throttle per-source-IP (LRU)
|
||||
/**
|
||||
* Per-source SYN throttle state.
|
||||
*
|
||||
* LRU keeps memory bounded under spoofed-source floods, where the number of
|
||||
* distinct keys is unbounded and an exact table would simply fill up.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, 65535);
|
||||
|
|
@ -47,7 +71,12 @@ struct {
|
|||
__type(value, struct throttle_entry);
|
||||
} connection_throttle SEC(".maps");
|
||||
|
||||
// 🔗 UDP rate limit per-source-IP (LRU) — policy from config.h
|
||||
/**
|
||||
* Per-source UDP rate-limit state.
|
||||
*
|
||||
* The window and threshold come from G_UDP_POLICY / G_UDP_HIT_COUNT, so the
|
||||
* same map serves pass, drop and rate-limit modes without reloading.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, 65535);
|
||||
|
|
@ -55,15 +84,39 @@ struct {
|
|||
__type(value, struct throttle_entry);
|
||||
} udp_rate_limit SEC(".maps");
|
||||
|
||||
// ── RST-challenge pending state (challenge reflected, awaiting proof) ──
|
||||
/**
|
||||
* IPv6 twin of udp_rate_limit.
|
||||
*
|
||||
* Native IPv6 has no 32-bit source address to key on, so the key is the full
|
||||
* 128-bit address; the value and window contract are identical.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, 65535);
|
||||
__type(key, struct in6_addr); // src6
|
||||
__type(value, struct throttle_entry);
|
||||
} udp_rate_limit6 SEC(".maps");
|
||||
|
||||
/**
|
||||
* RST-challenge pending state, one record per challenged flow.
|
||||
*
|
||||
* marker is our chosen ISN and bad_ack is the deliberately unacceptable
|
||||
* acknowledgement we sent; either one echoed back proves the source saw the
|
||||
* challenge, so both are kept until the proof arrives or the TTL expires.
|
||||
*/
|
||||
struct challenge_pending {
|
||||
__u64 sent_at; // ktime_ns of the reflected SYN-ACK
|
||||
__u32 marker; // our ISN (client may echo it as ack)
|
||||
__u32 bad_ack; // bogus ack sent (live client echoes it as RST seq)
|
||||
};
|
||||
|
||||
// 🔗 RST-challenge: verified sources (liveness proven) — sliding TTL via
|
||||
// last_seen, LRU eviction as the hard cap
|
||||
/**
|
||||
* Sources that already passed the RST challenge.
|
||||
*
|
||||
* The value is the last time the source proved liveness: the TTL is slid
|
||||
* forward on each verified SYN, and LRU eviction is the hard cap when more
|
||||
* sources are active than the table holds.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, 65536);
|
||||
|
|
@ -71,7 +124,12 @@ struct {
|
|||
__type(value, __u64); // last_seen (ktime_ns)
|
||||
} challenge_verified SEC(".maps");
|
||||
|
||||
// 🔗 RST-challenge: outstanding challenges keyed by flow 4-tuple
|
||||
/**
|
||||
* Outstanding challenges, keyed by the flow 4-tuple.
|
||||
*
|
||||
* Keyed per flow rather than per source so two concurrent connections from
|
||||
* the same host cannot overwrite each other's expected proof.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
__uint(max_entries, 16384);
|
||||
|
|
@ -79,7 +137,12 @@ struct {
|
|||
__type(value, struct challenge_pending);
|
||||
} challenge_pending SEC(".maps");
|
||||
|
||||
// 🔗 Statistics (per-CPU, атомарные инкременты)
|
||||
/**
|
||||
* Statistics counters, indexed by the STAT_* constants below.
|
||||
*
|
||||
* Per-CPU array: increments need no atomics between CPUs, userspace sums the
|
||||
* per-CPU values when it reads the map.
|
||||
*/
|
||||
#define STAT_TOTAL 0
|
||||
#define STAT_TCP 1
|
||||
#define STAT_WHITELIST 2
|
||||
|
|
@ -93,6 +156,12 @@ struct {
|
|||
#define STAT_CHALLENGE_VERIFIED 10
|
||||
#define STAT_CHALLENGE_FAILED 11
|
||||
|
||||
/**
|
||||
* Per-CPU counter storage.
|
||||
*
|
||||
* max_entries is rounded up past the highest STAT_* index so slots can be
|
||||
* added without touching the map definition.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
|
||||
__uint(max_entries, 16);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue