The Anatomy of a Volumetric Packet Flood
XDP_DROP, XDP_PASS, XDP_TX, or XDP_REDIRECT.
| Packet Processing Stage | Framework | CPU Overhead | Memory Allocation | Mitigation Limit |
|---|---|---|---|---|
| Driver Ring Buffer | XDP / eBPF | Minimal | None (Zero-Copy) | Line-Rate (100Gbps+) |
| Netfilter Hook | iptables / nftables | Moderate | High (SKB Created) | Moderate (2M-5M pps) |
| Socket Layer | User-Space (epoll) | Extreme | Full Application Stack | Low (Application Crash) |
Writing a Basic XDP Drop Program in C
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <bpf/bpf_helpers.h>
SEC("xdp")
int xdp_drop_reflection(struct xdp_md *ctx) {
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_PASS;
if (eth->h_proto != __constant_htons(ETH_P_IP))
return XDP_PASS;
struct iphdr *iph = data + sizeof(*eth);
if ((void *)(iph + 1) > data_end)
return XDP_PASS;
if (iph->protocol == IPPROTO_UDP) {
struct udphdr *udph = (void *)iph + (iph->ihl * 4);
if ((void *)(udph + 1) > data_end)
return XDP_PASS;
// Drop common amplification vector ports (e.g., NTP 123)
if (udph->dest == __constant_htons(123)) {
return XDP_DROP;
}
}
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";Compiling and Loading the XDP Program
# Compile the XDP C code into BPF bytecode
clang -O2 -target bpf -c xdp_drop.c -o xdp_drop.o
# Verify the bytecode loads correctly into the kernel verifier
sudo ip link set dev eth0 xdp obj xdp_drop.o sec xdpip tool:
ip link show dev eth0Integrating eBPF Maps for Dynamic Rate Limiting
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 65536);
__type(key, __u32); // Source IP address
__type(value, __u64); // Drop counter
} blocked_ips SEC(".maps");
SEC("xdp")
int xdp_dynamic_blacklist(struct xdp_md *ctx) {
// Packet parsing logic omitted for brevity
__u32 src_ip = 0; // Extracted from IP header
__u64 *drop_count = bpf_map_lookup_elem(&blocked_ips, &src_ip);
if (drop_count) {
__sync_fetch_and_add(drop_count, 1);
return XDP_DROP;
}
return XDP_PASS;
}