How to Configure Linux kernel eBPF to Drop DDoS Packets at the XDP Layer

Historically, when a Linux server falls victim to a volumetric Distributed Denial of Service (DDoS) attack (such as a massive SYN flood or UDP amplification attack), administrators rely on iptables or nftables to drop the malicious packets. However, these traditional firewalls operate too high up in the Linux networking stack. By the time a packet traverses the Network Interface Card (NIC) driver, allocates a socket buffer (sk_buff) in kernel memory, and reaches the netfilter hooks where iptables resides, a significant amount of CPU cycles have already been consumed. Under a severe DDoS load, the sheer act of dropping packets can cause the kernel to exhaust all CPU resources, resulting in a total server outage. To mitigate this, modern Linux kernels utilize eBPF and the eXpress Data Path (XDP), allowing you to mathematically drop malicious packets at the absolute lowest level of the hardware driver, before the kernel even knows they exist.

Understanding eBPF and XDP

Extended Berkeley Packet Filter (eBPF) is a revolutionary kernel technology that allows you to safely inject and execute sandboxed bytecode directly within the Linux kernel. XDP is a specific networking hook for eBPF programs.

When you attach an eBPF program to the XDP hook, the bytecode is executed directly inside the NIC driver, the very millisecond a packet arrives off the physical wire. The XDP program inspects the raw packet headers (Source IP, Destination Port, TCP Flags). Based on the logic you define, the program can issue an XDP_DROP command.

When XDP_DROP is issued, the packet is instantly discarded. No memory is allocated, no interrupts are generated for the upper networking stack, and the CPU overhead is practically zero. An XDP-enabled Linux server can silently drop millions of packets per second on a single CPU core, a feat mathematically impossible with legacy iptables.

Writing the XDP Packet Filter

While you can write eBPF programs in raw C and compile them with clang, the most efficient way to deploy XDP filters is utilizing the xdp-tools package and the xdp-filter utility, which abstracts the complex C compilation.

First, install the required user-space utilities on your Linux distribution (e.g., Ubuntu/Debian):

sudo apt update
sudo apt install xdp-tools bpfcc-tools

Assume your web server is under a severe volumetric attack originating from a specific malicious subnet (e.g., 198.51.100.0/24) hitting your primary network interface, eth0.

Deploying the XDP Drop Policy

To deploy the filter, you must first load the core XDP program onto the network interface. The xdp-filter utility handles the eBPF bytecode injection automatically.

sudo xdp-filter load eth0 -f ipv4

This command attaches the XDP hook to eth0 and configures it to inspect IPv4 traffic. At this point, the filter is active but empty; no packets are being dropped.

Next, you inject the specific blocking rules directly into the eBPF map (the shared memory space between the kernel program and user-space). To drop all traffic from the malicious subnet, execute:

sudo xdp-filter ip 198.51.100.0/24 -m drop

The moment this command executes, the NIC driver begins mathematically annihilating any packet originating from that subnet before it reaches the Linux network stack. The mitigation is instantaneous.

Verifying the Mitigation

To observe the effectiveness of the XDP drop program, you cannot rely on traditional tools like tcpdump, because tcpdump hooks into the kernel stack after the XDP layer. The packets never reach tcpdump.

Instead, utilize the built-in statistics engine provided by xdp-filter:

sudo xdp-filter stats eth0

This command polls the eBPF maps and displays a real-time output of exactly how many packets are being discarded by the XDP_DROP instruction, proving the mitigation is active. When the DDoS attack subsides, you can cleanly unload the eBPF program and return the NIC to its standard operational state without requiring a reboot or interrupting legitimate traffic:

sudo xdp-filter unload eth0

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.