guard/xdp/core/common.h
loki5512344 b64c329125
feat(xdp): IPv6 parity, monotonic ban expiry, per-protocol L4 minimum
IPv6 parity:
- blacklist6_map (LPM trie, 20-byte key) and is_blacklisted6(); the
  native_v6 path now passes the port gate and the L4 checks instead of
  returning XDP_PASS early.
- ban_cidr() covers V4 and V6; gateway bans by /32 or /128; subnet monitor
  bans v6 /64 prefixes; ban_ip() removed (dead code is a bug).
- v6 TCP feeds prefix stats through prefix_key_from_v6 (subnet detector
  now sees IPv6); UDP policy=drop and detect_tcp_bypass apply to v6.
- tests/xdp_ban_v6.rs and scripts/xdp_ipv6_smoke.sh (real netns/verifier).

Bug fixes (both families):
- Ban expiry compared SystemTime epoch-ns against bpf_ktime_get_ns(), so
  bans never expired. Userspace now writes CLOCK_MONOTONIC via
  xdp::monotonic_ns(); regression in tests/xdp_ban_clock.rs.
- The L4 gate required sizeof(struct tcphdr) for every protocol, so short
  UDP datagrams bypassed the port gate and the blacklist. The minimum is
  now per-protocol (UDP 8 bytes); smoke covers it.
2026-11-15 23:08:05 +01:00

105 lines
2.8 KiB
C

#ifndef RAMPART_COMMON_H
#define RAMPART_COMMON_H
#include <linux/types.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#define ETH_P_IP 0x0800
#define ETH_P_IPV6 0x86DD
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
#define IP_OFFSET 0x1FFF
#define IP_MF 0x2000
// ── Protocol-agnostic TCP state machine ──
enum connection_state {
STATE_SYN_RECEIVED = 0, // SYN seen, waiting for ACK (handshake completing)
STATE_ESTABLISHED = 1, // handshake done; payload handed to proto_hook()
};
// ── Flow key (4-tuple for connection tracking) ──
struct flow_key {
__u32 src_ip;
__u32 dst_ip;
__u16 src_port;
__u16 dst_port;
};
// ── Connection tracking entry ──
// LRU maps evict stale entries automatically when full; RST/FIN removes
// entries explicitly. last_seen supports future timer-based idle cleanup.
struct conntrack_entry {
__u32 state;
__u32 expected_seq;
__u32 src_ip;
__u64 last_seen;
__u16 src_port;
__u8 fails;
};
// ── SYN / UDP throttle entry ──
struct throttle_entry {
__u64 window_start;
__u32 hits;
};
// ── LPM key for blacklist/whitelist ──
struct lpm_key {
__u32 prefixlen;
__u32 ip;
};
// ── LPM key for IPv6 blacklist (format of bpf_lpm_trie_key, 20 bytes) ──
struct lpm_key6 {
__u32 prefixlen;
__u8 addr[16];
};
// ── Ringbuf event (userspace receives these) ──
enum event_type {
EVENT_BAN = 0,
EVENT_RATE_LIMIT = 1,
EVENT_POLICY_DROP = 2,
EVENT_CONN_DROP = 3,
};
struct xdp_event {
__u32 type;
__u32 src_ip;
__u32 metadata;
__u64 timestamp;
};
// ── Bounds check macros (dual-bounds для verifier) ──
#define CHECK_BOUNDS_OR_RETURN(ptr, sz, pend, dend) \
do { \
if ((void *)(ptr) + (sz) > (void *)(dend)) \
goto error; \
barrier_var(ptr); \
if ((void *)(ptr) + (sz) > (void *)(pend)) \
goto error; \
} while (0)
#define barrier_var(var) asm volatile("" : "+r"(var))
// ── Ringbuf for events → userspace (declared here for push_event) ──
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24);
} events_ringbuf SEC(".maps");
// ── Event push to ringbuf ──
static __always_inline void push_event(enum event_type type, __u32 src_ip, __u32 meta)
{
struct xdp_event *e = bpf_ringbuf_reserve(&events_ringbuf, sizeof(struct xdp_event), 0);
if (!e)
return;
e->type = type;
e->src_ip = src_ip;
e->metadata = meta;
e->timestamp = bpf_ktime_get_ns();
bpf_ringbuf_submit(e, 0);
}
#endif /* RAMPART_COMMON_H */