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
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue