fix(xdp): correct payload length, attach/detach, whitelist and ban upkeep
BPF program (the first two made the filter wrong on real hardware): - Compute the TCP payload length from ip->tot_len (IPv4) / payload_len (IPv6) instead of data_end. Ethernet padding on a real NIC added 6 bogus payload bytes, broke expected_seq and dropped every connection after the fourth segment. scripts/xdp_pkt_test.sh reproduces it on synthetic frames and fails against the old code. - Read map values (conn->fails, sent_at) before bpf_map_delete_elem; the LRU element is freed by the delete. - Parse up to two VLAN/QinQ tags before L3; fragments are checked against the blacklist and passed instead of bypassing all filtering; first fragments are no longer dropped. - Unknown non-SYN flows PASS: the program is a stateless filter fed by the conntrack state machine, not a firewall, so spoofed SYNs cannot evict established flows into a drop and replies to the daemon's own outbound TCP keep working. - Reset conn->fails on in-order segments, honour SYN payload (TFO), fix the IPv6 SYN-ACK address swap, count every DROP path, drop SYN with IP options. - Expired prefix bans are no longer deleted with the wrong LPM key; userspace reaps them with the exact key. Loader: - Remember the effective attach flags (native vs generic) and use them for detach/query, verifying the interface is clean afterwards. The old code always detached the native slot, leaving a generic program attached after shutdown. - DetachGuard rolls back the attach if anything after it fails. - ban_cidr validates the prefix length and masks host bits. - Whitelist maps (v4 and the new v6 LPM trie) are populated from the config at startup and fail fast on error. - Stats read all 13 counters, and a test asserts every map's key/value size against the Rust encodings. Tests: xdp_pkt_test (padding, VLAN/QinQ, fragments), xdp_loader (attach flags, rollback, detach), xdp_ban_keys, xdp_whitelist_keys, xdp_services, live xdp_attach_smoke (ignored, needs root).
This commit is contained in:
parent
a8a3057719
commit
14dcaccdbf
27 changed files with 1578 additions and 408 deletions
|
|
@ -7,11 +7,25 @@
|
|||
|
||||
#define ETH_P_IP 0x0800
|
||||
#define ETH_P_IPV6 0x86DD
|
||||
#define ETH_P_8021Q 0x8100
|
||||
#define ETH_P_8021AD 0x88A8
|
||||
#define IPPROTO_TCP 6
|
||||
#define IPPROTO_UDP 17
|
||||
#define IPPROTO_FRAGMENT 44
|
||||
#define IP_OFFSET 0x1FFF
|
||||
#define IP_MF 0x2000
|
||||
|
||||
/**
|
||||
* One 802.1Q / 802.1ad tag.
|
||||
*
|
||||
* The filter only needs the encapsulated EtherType to skip up to two tags
|
||||
* before L3; the TCI (PCP/DEI/VID) has no bearing on L3/L4 policy.
|
||||
*/
|
||||
struct rampart_vlan_hdr {
|
||||
__u16 tci; // PCP/DEI/VID, unused
|
||||
__u16 encap_proto; // EtherType of the next header
|
||||
};
|
||||
|
||||
/**
|
||||
* Protocol-agnostic TCP connection states.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -6,8 +6,9 @@
|
|||
*
|
||||
* 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.
|
||||
* entries are ignored by the packet path and reaped from userspace, which
|
||||
* knows the prefix length. BPF_F_NO_PREALLOC is required by the LPM trie
|
||||
* type.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
|
||||
|
|
@ -34,8 +35,9 @@ struct {
|
|||
/**
|
||||
* Whitelist.
|
||||
*
|
||||
* LPM trie of trusted CIDRs; the filter consults it before any other check
|
||||
* so allowlisted traffic can never be throttled or banned away.
|
||||
* LPM trie of trusted CIDRs; the filter consults it before the port gate,
|
||||
* conntrack and blacklists so allowlisted traffic can never be throttled or
|
||||
* banned away.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
|
||||
|
|
@ -45,11 +47,28 @@ struct {
|
|||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
} whitelist_map SEC(".maps");
|
||||
|
||||
/**
|
||||
* IPv6 whitelist.
|
||||
*
|
||||
* IPv6 twin of whitelist_map: a 20-byte LPM key (host-order prefixlen
|
||||
* followed by 16 address bytes) and the same early position in the packet
|
||||
* path. Lookups use prefixlen 128 for the exact source address.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
|
||||
__uint(max_entries, 1000);
|
||||
__type(key, struct lpm_key6);
|
||||
__type(value, __u8);
|
||||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
} whitelist6_map SEC(".maps");
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* map updates; RST/FIN removes entries explicitly on the normal path. Only
|
||||
* new connections are policed: a non-SYN segment whose entry was evicted
|
||||
* passes, because this is a stateless filter, not a firewall.
|
||||
*/
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
||||
|
|
@ -155,6 +174,7 @@ struct {
|
|||
#define STAT_CHALLENGE_SENT 9
|
||||
#define STAT_CHALLENGE_VERIFIED 10
|
||||
#define STAT_CHALLENGE_FAILED 11
|
||||
#define STAT_FRAGMENT_BYPASS 12
|
||||
|
||||
/**
|
||||
* Per-CPU counter storage.
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@
|
|||
* Increment one per-CPU counter.
|
||||
*
|
||||
* A missing slot (never updated, or an index past max_entries) is ignored so
|
||||
* accounting can never change a packet verdict.
|
||||
* accounting can never change a packet verdict. The slot is private to the
|
||||
* current CPU, so a plain read-modify-write is enough: an atomic would only
|
||||
* add contention on the hottest path.
|
||||
*
|
||||
* @param idx STAT_* index into stats_map
|
||||
*/
|
||||
|
|
@ -16,7 +18,7 @@ static __always_inline void inc_stat(__u32 idx)
|
|||
{
|
||||
__u64 *val = bpf_map_lookup_elem(&stats_map, &idx);
|
||||
if (val)
|
||||
__sync_fetch_and_add(val, 1);
|
||||
(*val)++;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -35,6 +37,7 @@ static __always_inline void inc_rate_limit(void) { inc_stat(STAT_RATE_LIMIT);
|
|||
static __always_inline void inc_chal_sent(void) { inc_stat(STAT_CHALLENGE_SENT); }
|
||||
static __always_inline void inc_chal_verified(void) { inc_stat(STAT_CHALLENGE_VERIFIED); }
|
||||
static __always_inline void inc_chal_failed(void) { inc_stat(STAT_CHALLENGE_FAILED); }
|
||||
static __always_inline void inc_fragment_bypass(void) { inc_stat(STAT_FRAGMENT_BYPASS); }
|
||||
static __always_inline void inc_pass(void) { inc_stat(STAT_PASS); }
|
||||
static __always_inline void inc_drop(void) { inc_stat(STAT_DROP); }
|
||||
|
||||
|
|
|
|||
|
|
@ -129,7 +129,10 @@ static __always_inline void chal_build_synack(struct xdp_md *ctx,
|
|||
__builtin_memcpy(tcpw, tcp, 20);
|
||||
|
||||
if (is_ipv6) {
|
||||
__builtin_memcpy(saddr, &((struct ipv6hdr *)l3)->daddr, 16); // swapped
|
||||
// saddr/daddr hold the addresses of the reply: the new source is the
|
||||
// original destination and the new destination is the original
|
||||
// source. The checksum below is computed over exactly these values.
|
||||
__builtin_memcpy(saddr, &((struct ipv6hdr *)l3)->daddr, 16);
|
||||
__builtin_memcpy(daddr, &((struct ipv6hdr *)l3)->saddr, 16);
|
||||
} else {
|
||||
saddr[0] = ((struct iphdr *)l3)->daddr;
|
||||
|
|
@ -191,8 +194,11 @@ static __always_inline void chal_build_synack(struct xdp_md *ctx,
|
|||
__builtin_memcpy(eth->h_source, md, ETH_ALEN);
|
||||
if (is_ipv6) {
|
||||
struct ipv6hdr *ip6 = l3;
|
||||
__builtin_memcpy(&ip6->saddr, daddr, 16);
|
||||
__builtin_memcpy(&ip6->daddr, saddr, 16);
|
||||
// Write the header with the same orientation the checksum was built
|
||||
// from: saddr is the new source (original destination), daddr the
|
||||
// new destination (original source).
|
||||
__builtin_memcpy(&ip6->saddr, saddr, 16);
|
||||
__builtin_memcpy(&ip6->daddr, daddr, 16);
|
||||
ip6->hop_limit = 64;
|
||||
} else {
|
||||
__builtin_memcpy(l3, ipw, 20);
|
||||
|
|
@ -283,10 +289,13 @@ static __always_inline int chal_on_rst(struct tcphdr *tcp,
|
|||
__u32 ack = bpf_ntohl(tcp->ack_seq);
|
||||
__u8 ok = (seq == p->bad_ack) ||
|
||||
(tcp->ack && ack == p->marker + 1);
|
||||
__u64 sent_at = p->sent_at;
|
||||
|
||||
// Copy every field needed before deleting: an LRU element may be freed
|
||||
// as soon as it is removed, so p must never be dereferenced afterwards.
|
||||
bpf_map_delete_elem(&challenge_pending, flow);
|
||||
|
||||
if (!ok || now - p->sent_at > timeout_ns) {
|
||||
if (!ok || now - sent_at > timeout_ns) {
|
||||
inc_chal_failed();
|
||||
return XDP_DROP;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,20 @@
|
|||
* owns, in this order:
|
||||
*
|
||||
* 1. No pure-ACK drop (prevents TCP handshake deadlock).
|
||||
* 2. LRU conntrack: SYN_RECEIVED to ESTABLISHED, with sequence tracking.
|
||||
* 2. LRU conntrack, consulted only for new (SYN) connections. A non-SYN
|
||||
* segment with no tracked state passes: this is a stateless filter, not
|
||||
* a firewall, so spoofed SYNs cannot black-hole established flows and
|
||||
* ordinary replies are never dropped for lack of state.
|
||||
* 3. RST/FIN removes the conntrack entry, so no stale state survives.
|
||||
* 4. IPv6 support alongside IPv4.
|
||||
* 5. IP/CIDR blacklist and whitelist (LPM trie).
|
||||
* 5. IP/CIDR blacklist and whitelist (LPM trie, IPv4 and IPv6).
|
||||
* 6. Per-IP SYN throttle, with invalid TCP flag combinations dropped.
|
||||
* 7. UDP policy from config.h: pass, drop or rate-limit.
|
||||
*
|
||||
* L2 decapsulation peels up to two VLAN tags before L3. IP fragments carry
|
||||
* no inspectable ports, so they only get the source blacklist and then pass
|
||||
* (dropping first fragments would break legitimate large datagrams).
|
||||
*
|
||||
* Deep payload inspection is delegated to an optional protocol hook (see
|
||||
* ../hooks/hook_api.h). Without a hook, established connections pass.
|
||||
*/
|
||||
|
|
@ -70,7 +77,12 @@ static __always_inline __u8 detect_tcp_bypass(struct tcphdr *tcp)
|
|||
}
|
||||
|
||||
/**
|
||||
* Check the IPv4 blacklist and expire the entry on the way out.
|
||||
* Check the IPv4 blacklist.
|
||||
*
|
||||
* The /32 lookup cannot address a wider /24 (or any other prefix) stored in
|
||||
* the trie, so an expired entry is only ignored here; userspace reaps it
|
||||
* with the prefix length it was created with. Deleting by /32 from the
|
||||
* packet path used to leak every prefix ban until the trie filled up.
|
||||
*
|
||||
* @param src_ip source address, network byte order
|
||||
* @param now current bpf_ktime_get_ns() timestamp
|
||||
|
|
@ -82,17 +94,15 @@ static __always_inline __u8 is_blacklisted(__u32 src_ip, __u64 now)
|
|||
__u64 *ban_until = bpf_map_lookup_elem(&blacklist_map, &key);
|
||||
if (!ban_until)
|
||||
return 0;
|
||||
if (*ban_until > now)
|
||||
return 1;
|
||||
bpf_map_delete_elem(&blacklist_map, &key); // expired — clean up
|
||||
return 0;
|
||||
return *ban_until > now;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the IPv6 blacklist and expire the entry on the way out.
|
||||
* Check the IPv6 blacklist.
|
||||
*
|
||||
* Key layout mirrors bpf_lpm_trie_key: prefixlen in host order followed by
|
||||
* 16 address bytes.
|
||||
* 16 address bytes. Expired entries are ignored, never deleted here, for the
|
||||
* same reason as in is_blacklisted().
|
||||
*
|
||||
* @param addr 16-byte source address, network byte order
|
||||
* @param now current bpf_ktime_get_ns() timestamp
|
||||
|
|
@ -106,10 +116,7 @@ static __always_inline __u8 is_blacklisted6(const __u8 addr[16], __u64 now)
|
|||
__u64 *ban_until = bpf_map_lookup_elem(&blacklist6_map, &key);
|
||||
if (!ban_until)
|
||||
return 0;
|
||||
if (*ban_until > now)
|
||||
return 1;
|
||||
bpf_map_delete_elem(&blacklist6_map, &key); // expired — clean up
|
||||
return 0;
|
||||
return *ban_until > now;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -218,8 +225,25 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
if ((void *)(eth + 1) > data_end)
|
||||
return XDP_PASS;
|
||||
|
||||
// Strip up to two VLAN tags (802.1Q / 802.1ad). Tagged frames used to hit
|
||||
// the non-IP early return, which let a banned source bypass the blacklist
|
||||
// with VLAN/QinQ encapsulation.
|
||||
void *l3 = (void *)(eth + 1);
|
||||
__u16 eth_proto = eth->h_proto;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (eth_proto != bpf_htons(ETH_P_8021Q) &&
|
||||
eth_proto != bpf_htons(ETH_P_8021AD))
|
||||
break;
|
||||
struct rampart_vlan_hdr *vh = l3;
|
||||
if ((void *)(vh + 1) > data_end)
|
||||
return XDP_PASS;
|
||||
eth_proto = vh->encap_proto;
|
||||
l3 = (void *)(vh + 1);
|
||||
}
|
||||
|
||||
// Non-IP traffic: pass (ARP, etc.)
|
||||
if (eth->h_proto != bpf_htons(ETH_P_IP) && eth->h_proto != bpf_htons(ETH_P_IPV6))
|
||||
if (eth_proto != bpf_htons(ETH_P_IP) && eth_proto != bpf_htons(ETH_P_IPV6))
|
||||
return XDP_PASS;
|
||||
|
||||
// Parse the IP header.
|
||||
|
|
@ -228,36 +252,48 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
__u8 l4_proto = 0;
|
||||
__u8 is_ipv6 = 0;
|
||||
__u8 native_v6 = 0;
|
||||
__u8 is_fragment = 0;
|
||||
__u8 src6[16];
|
||||
|
||||
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
|
||||
struct iphdr *ip = (void *)(eth + 1);
|
||||
if (eth_proto == bpf_htons(ETH_P_IP)) {
|
||||
struct iphdr *ip = l3;
|
||||
if ((void *)(ip + 1) > data_end)
|
||||
return XDP_PASS;
|
||||
if (ip->ihl < 5)
|
||||
return XDP_DROP;
|
||||
|
||||
if (ip->protocol != IPPROTO_TCP && ip->protocol != IPPROTO_UDP)
|
||||
return XDP_PASS;
|
||||
|
||||
// Non-sequential fragment: pass (can't inspect ports safely)
|
||||
if (ip->frag_off & bpf_htons(IP_OFFSET))
|
||||
return XDP_PASS;
|
||||
|
||||
// First fragment with MF: drop (can't reassemble)
|
||||
if (ip->frag_off & bpf_htons(IP_MF))
|
||||
if (ip->ihl < 5) {
|
||||
inc_drop();
|
||||
return XDP_DROP;
|
||||
}
|
||||
|
||||
src_ip = ip->saddr;
|
||||
dst_ip = ip->daddr;
|
||||
l4_proto = ip->protocol;
|
||||
|
||||
// MF or a non-zero offset: this packet has no reliable L4 header.
|
||||
// Both first and later fragments are handled by the source blacklist
|
||||
// below and then passed; dropping first fragments would break
|
||||
// legitimate large datagrams.
|
||||
if (ip->frag_off & bpf_htons(IP_OFFSET | IP_MF)) {
|
||||
is_fragment = 1;
|
||||
} else if (ip->protocol != IPPROTO_TCP && ip->protocol != IPPROTO_UDP) {
|
||||
return XDP_PASS;
|
||||
}
|
||||
} else {
|
||||
struct ipv6hdr *ip6 = (void *)(eth + 1);
|
||||
struct ipv6hdr *ip6 = l3;
|
||||
if ((void *)(ip6 + 1) > data_end)
|
||||
return XDP_PASS;
|
||||
|
||||
if (ip6->nexthdr != IPPROTO_TCP && ip6->nexthdr != IPPROTO_UDP)
|
||||
// Keep the full address for the IPv6 whitelist and blacklist even
|
||||
// when it is IPv4-mapped; the low 32 bits are used for flow keys.
|
||||
__builtin_memcpy(src6, &ip6->saddr, 16);
|
||||
is_ipv6 = 1;
|
||||
l4_proto = ip6->nexthdr;
|
||||
|
||||
// A fragment header hides the L4 header; same policy as IPv4.
|
||||
if (ip6->nexthdr == IPPROTO_FRAGMENT) {
|
||||
is_fragment = 1;
|
||||
} else if (ip6->nexthdr != IPPROTO_TCP && ip6->nexthdr != IPPROTO_UDP) {
|
||||
return XDP_PASS; // extension headers not walked
|
||||
}
|
||||
|
||||
// Use IPv4-mapped IPv6 address for flow key (::ffff:0:0/96)
|
||||
if (ip6->saddr.in6_u.u6_addr32[0] != 0 ||
|
||||
|
|
@ -270,37 +306,70 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
// separately after the generic L4/port gates and must never
|
||||
// touch the IPv4 maps.
|
||||
native_v6 = 1;
|
||||
is_ipv6 = 1;
|
||||
l4_proto = ip6->nexthdr;
|
||||
__builtin_memcpy(src6, &ip6->saddr, 16);
|
||||
} else {
|
||||
// Source address must come from saddr, never daddr, or the
|
||||
// filter would track the server instead of the client.
|
||||
src_ip = ip6->saddr.in6_u.u6_addr32[3];
|
||||
dst_ip = ip6->daddr.in6_u.u6_addr32[3];
|
||||
l4_proto = ip6->nexthdr;
|
||||
is_ipv6 = 1;
|
||||
}
|
||||
}
|
||||
|
||||
__u64 now = bpf_ktime_get_ns();
|
||||
|
||||
// Whitelist check (LPM trie), before the port gate, conntrack and both
|
||||
// blacklists: a whitelisted source must truly bypass the state machine.
|
||||
if (is_ipv6) {
|
||||
struct lpm_key6 wl6 = {};
|
||||
wl6.prefixlen = 128;
|
||||
__builtin_memcpy(wl6.addr, src6, 16);
|
||||
if (bpf_map_lookup_elem(&whitelist6_map, &wl6)) {
|
||||
inc_whitelist();
|
||||
return XDP_PASS;
|
||||
}
|
||||
} else {
|
||||
struct lpm_key wl = { .prefixlen = 32, .ip = src_ip };
|
||||
if (bpf_map_lookup_elem(&whitelist_map, &wl)) {
|
||||
inc_whitelist();
|
||||
return XDP_PASS;
|
||||
}
|
||||
}
|
||||
|
||||
// Fragments: the L4 ports are missing or incomplete, so the stateful and
|
||||
// throttle logic cannot run. Apply the source blacklist and pass; the
|
||||
// dedicated counter keeps this deliberate bypass observable.
|
||||
if (is_fragment) {
|
||||
__u8 banned = native_v6 ? is_blacklisted6(src6, now)
|
||||
: is_blacklisted(src_ip, now);
|
||||
if (banned) {
|
||||
inc_blacklist();
|
||||
inc_drop();
|
||||
if (!native_v6 && G_FEATURE_EVENTS)
|
||||
push_event(EVENT_BAN, src_ip, 0);
|
||||
return XDP_DROP;
|
||||
}
|
||||
inc_fragment_bypass();
|
||||
inc_pass();
|
||||
return XDP_PASS;
|
||||
}
|
||||
|
||||
// Parse the L4 header.
|
||||
__u8 ip_hdr_len;
|
||||
void *l4;
|
||||
|
||||
if (is_ipv6) {
|
||||
struct ipv6hdr *ip6 = (void *)(eth + 1);
|
||||
struct ipv6hdr *ip6 = l3;
|
||||
l4 = (void *)(ip6 + 1);
|
||||
ip_hdr_len = sizeof(struct ipv6hdr);
|
||||
} else {
|
||||
struct iphdr *ip = (void *)(eth + 1);
|
||||
struct iphdr *ip = l3;
|
||||
l4 = (void *)ip + (ip->ihl * 4);
|
||||
ip_hdr_len = ip->ihl * 4;
|
||||
}
|
||||
|
||||
// Minimum L4 header per protocol: a short UDP datagram used to fall through
|
||||
// to XDP_PASS because the check was sized for TCP, letting it bypass the
|
||||
// blacklist entirely. IPv6 native fragmentation is already filtered above
|
||||
// (nexthdr must be TCP/UDP), so a short UDP header here is a real datagram.
|
||||
// blacklist entirely. Fragments were already handled above (source
|
||||
// blacklist only), so a short UDP header here is a real datagram.
|
||||
__u32 min_l4 = (l4_proto == IPPROTO_TCP) ? sizeof(struct tcphdr)
|
||||
: sizeof(struct udphdr);
|
||||
if ((void *)l4 + min_l4 > data_end)
|
||||
|
|
@ -321,8 +390,6 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
else
|
||||
inc_udp();
|
||||
|
||||
__u64 now = bpf_ktime_get_ns();
|
||||
|
||||
// Native IPv6: blacklist plus cheap L4 policy (no stateful v6 yet).
|
||||
if (native_v6) {
|
||||
if (is_blacklisted6(src6, now)) {
|
||||
|
|
@ -363,13 +430,6 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
return XDP_PASS;
|
||||
}
|
||||
|
||||
// Whitelist check (CIDR), before any filtering.
|
||||
struct lpm_key wl_key = { .prefixlen = 32, .ip = src_ip };
|
||||
if (bpf_map_lookup_elem(&whitelist_map, &wl_key)) {
|
||||
inc_whitelist();
|
||||
return XDP_PASS;
|
||||
}
|
||||
|
||||
// Blacklist check (CIDR with expiry).
|
||||
if (is_blacklisted(src_ip, now)) {
|
||||
inc_blacklist();
|
||||
|
|
@ -414,23 +474,52 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
return XDP_DROP;
|
||||
}
|
||||
|
||||
// TCP header length check
|
||||
if (tcp->doff < 5)
|
||||
// TCP header length check.
|
||||
if (tcp->doff < 5) {
|
||||
inc_drop();
|
||||
return XDP_DROP;
|
||||
}
|
||||
__u8 tcp_hdr_len = tcp->doff * 4;
|
||||
if ((void *)data + sizeof(struct ethhdr) + ip_hdr_len + tcp_hdr_len > data_end)
|
||||
if ((void *)l4 + tcp_hdr_len > data_end) {
|
||||
inc_drop();
|
||||
return XDP_DROP;
|
||||
}
|
||||
|
||||
// Per-prefix stats key (/24), built once after blacklist and bypass drops.
|
||||
struct prefix_key pkey;
|
||||
prefix_key_from_v4(&pkey, src_ip);
|
||||
|
||||
// Compute payload pointers
|
||||
// The payload window is bounded by the IP header length field, not by
|
||||
// data_end: a real NIC pads the last frame to the 60-byte Ethernet
|
||||
// minimum, so counting to data_end would treat the padding as payload,
|
||||
// drift expected_seq and drop the connection after a few segments. The
|
||||
// frame can still be shorter than the IP header claims, so cap at
|
||||
// data_end as well.
|
||||
__u32 l4_len;
|
||||
if (is_ipv6) {
|
||||
struct ipv6hdr *ip6 = l3;
|
||||
l4_len = sizeof(struct ipv6hdr) + bpf_ntohs(ip6->payload_len);
|
||||
} else {
|
||||
struct iphdr *ip = l3;
|
||||
__u16 tot_len = bpf_ntohs(ip->tot_len);
|
||||
if (tot_len < ip_hdr_len) {
|
||||
inc_drop();
|
||||
return XDP_DROP;
|
||||
}
|
||||
l4_len = (__u32)(tot_len - ip_hdr_len);
|
||||
}
|
||||
|
||||
__u32 frame_l4 = (__u32)((__u8 *)data_end - (__u8 *)l4);
|
||||
if (l4_len > frame_l4)
|
||||
l4_len = frame_l4;
|
||||
if (l4_len < tcp_hdr_len) {
|
||||
inc_drop();
|
||||
return XDP_DROP;
|
||||
}
|
||||
|
||||
__u32 tcp_payload_len = l4_len - tcp_hdr_len;
|
||||
__u8 *tcp_payload = (__u8 *)tcp + tcp_hdr_len;
|
||||
__u8 *tcp_payload_end = (__u8 *)data_end;
|
||||
__s32 tcp_payload_len = (__s32)(tcp_payload_end - tcp_payload);
|
||||
if (tcp_payload_len < 0)
|
||||
tcp_payload_len = 0;
|
||||
__u8 *tcp_payload_end = tcp_payload + tcp_payload_len;
|
||||
|
||||
// Build the flow key.
|
||||
struct flow_key flow = {
|
||||
|
|
@ -458,8 +547,14 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
// Placed after throttle and blacklist so abusive sources never get a
|
||||
// reply, mirroring the prefix_stats ordering.
|
||||
if (G_SYN_CHALLENGE_ENABLED) {
|
||||
int cv = chal_on_syn(ctx, eth, (void *)(eth + 1), is_ipv6,
|
||||
tcp, &flow, now);
|
||||
// The challenge reply is rebuilt as a bare 20-byte IPv4 header,
|
||||
// so an IPv4 SYN with options cannot be answered without shipping
|
||||
// a malformed SYN-ACK. Drop it (counted) instead.
|
||||
if (!is_ipv6 && ((struct iphdr *)l3)->ihl != 5) {
|
||||
inc_drop();
|
||||
return XDP_DROP;
|
||||
}
|
||||
int cv = chal_on_syn(ctx, eth, l3, is_ipv6, tcp, &flow, now);
|
||||
if (cv != CHAL_CONTINUE)
|
||||
return cv;
|
||||
}
|
||||
|
|
@ -467,10 +562,12 @@ 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);
|
||||
|
||||
// Create conntrack entry for new connection
|
||||
// Create conntrack entry for new connection. A SYN may carry data
|
||||
// (TCP Fast Open), which is acknowledged as part of the handshake;
|
||||
// expected_seq must advance past it or the completing ACK is dropped.
|
||||
struct conntrack_entry ce = {};
|
||||
ce.state = STATE_SYN_RECEIVED;
|
||||
ce.expected_seq = bpf_ntohl(tcp->seq) + 1; // expect ACK seq
|
||||
ce.expected_seq = bpf_ntohl(tcp->seq) + 1 + tcp_payload_len;
|
||||
ce.src_ip = src_ip;
|
||||
ce.src_port = tcp->source;
|
||||
ce.last_seen = now;
|
||||
|
|
@ -499,9 +596,12 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
|
||||
struct conntrack_entry *conn = bpf_map_lookup_elem(&conntrack_map, &flow);
|
||||
if (!conn) {
|
||||
// Unknown connection — drop
|
||||
inc_drop();
|
||||
return XDP_DROP;
|
||||
// Stateless filter, not a firewall: only new connections (SYN) are
|
||||
// policed. An untracked non-SYN segment — a flow evicted under SYN
|
||||
// pressure, a connection opened before attach, or the reply to the
|
||||
// daemon's own outbound connection — passes instead of being dropped.
|
||||
inc_pass();
|
||||
return XDP_PASS;
|
||||
}
|
||||
conn->last_seen = now;
|
||||
|
||||
|
|
@ -514,19 +614,28 @@ int rampart_universal_filter(struct xdp_md *ctx)
|
|||
|
||||
// Sequence number tracking, established connections only.
|
||||
__u32 seq = bpf_ntohl(tcp->seq);
|
||||
if (conn->state == STATE_ESTABLISHED && tcp_payload_len > 0 &&
|
||||
seq != conn->expected_seq) {
|
||||
conn->fails++;
|
||||
if (conn->fails >= G_MAX_OUT_OF_ORDER) {
|
||||
bpf_map_delete_elem(&conntrack_map, &flow);
|
||||
inc_drop();
|
||||
if (G_FEATURE_EVENTS)
|
||||
push_event(EVENT_CONN_DROP, src_ip, conn->fails);
|
||||
return XDP_DROP;
|
||||
if (conn->state == STATE_ESTABLISHED && tcp_payload_len > 0) {
|
||||
if (seq != conn->expected_seq) {
|
||||
conn->fails++;
|
||||
if (conn->fails >= G_MAX_OUT_OF_ORDER) {
|
||||
// Copy the counter before deleting: the LRU element may be
|
||||
// freed by bpf_map_delete_elem, so conn must not be read
|
||||
// afterwards.
|
||||
__u8 fails = conn->fails;
|
||||
bpf_map_delete_elem(&conntrack_map, &flow);
|
||||
inc_drop();
|
||||
if (G_FEATURE_EVENTS)
|
||||
push_event(EVENT_CONN_DROP, src_ip, fails);
|
||||
return XDP_DROP;
|
||||
}
|
||||
// Allow retransmission (don't update expected_seq)
|
||||
inc_pass();
|
||||
return XDP_PASS;
|
||||
}
|
||||
// Allow retransmission (don't update expected_seq)
|
||||
inc_pass();
|
||||
return XDP_PASS;
|
||||
// In-order segment: reset the reordering counter, so isolated
|
||||
// reordered/retransmitted segments over the connection lifetime can
|
||||
// never accumulate into a false positive.
|
||||
conn->fails = 0;
|
||||
}
|
||||
|
||||
// State machine.
|
||||
|
|
|
|||
|
|
@ -28,7 +28,8 @@
|
|||
*
|
||||
* struct cursor bounds-checked window over the TCP payload, where pos is
|
||||
* the first payload byte and end is one past the last byte
|
||||
* of the packet (validated against data_end).
|
||||
* of the payload as declared by the IP header, capped by
|
||||
* data_end (Ethernet padding is never part of the window).
|
||||
* struct pkt_meta read-only metadata: the 4-tuple (ports in host byte
|
||||
* order), payload length, the full TCP flags byte and the
|
||||
* L3 family.
|
||||
|
|
@ -98,7 +99,7 @@
|
|||
*/
|
||||
struct cursor {
|
||||
__u8 *pos; // next unparsed byte
|
||||
__u8 *end; // one past last valid byte (== data_end)
|
||||
__u8 *end; // one past the last payload byte (IP length capped by data_end)
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue