Sitemap
Rithvik

Rithvik

I write stuff.

XDP: Basic Firewall

3 min readSep 15, 2026

--

This was a project that added a few wrinkles to my brain, apparently things that seem tough are interesting. Who would’ve thought that!

Press enter or click to view image in full size
Artistic very much

What’s XDP?

XDP or eXtended Data Path is a Linux Networking subsystem which allows for bare-metal packet processing in the Linux kernel. Which allows for high performance/low latency, in-kernel packet processing.

This would also mean that it is easier to break your kernel by writing unsafe code, but they already accounted for this.

Press enter or click to view image in full size
Position of XDP relative to NIC

The program is run in a sandbox along with a verifier that checks whether or not the eBPF program follows specific rules such as accessing memory out-of-bounds, finite complexity etc.

Let’s implement it with a basic firewall!

Basic Firewall:

This is a lookup based blocking mechanism, our key idea is:

  1. Incoming packets trigger XDP hook
  2. Identify the packet’s source address through the packet header
  3. If source IP is in map → block the packet
  4. Else →let it pass

XDP has the following action flags:

xdp_actions{
XDP_ABORTED #Abort the program execution in an error
XDP_DROP #Drop the packet
XDP_PASS #Let the packet into the kernel networking stack
XDP_TX #Transmit the packet back
XDP_REDIRECT #Redirect the packet from on network interface to another (wlp2s0 → eth0, or something else)
}

I’ve only utilized XDP_PASS and XDP_DROP for the sake of simplicity and my sanity.

Code:

Kernel-space Code:

Get Rithvik’s stories in your inbox

Join Medium for free to get updates from this writer.

Basic idea of the kernel-space code i.e initialization of the XDP hook is:

  1. Initialize a map to store the blacklisted IP
  2. If source address of packet is in our blacklist map, then drop the packet
  3. Else, let it pass
SEC("xdp")
int xdp_parser_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
if ((void *)(eth+1)>data_end){
return XDP_PASS;
}
if (eth->h_proto!=bpf_htons(ETH_P_IP)){
return XDP_PASS;
}
struct iphdr *iph = (struct iphdr *)(data + ETH_HLEN);
if ((void *)(iph + 1) > data_end)
return XDP_PASS;
u8 *cnt = bpf_map_lookup_elem(&blocked_ips, &iph->saddr);
if (cnt){
__u32 ip = iph->saddr;
bpf_printk("BLOCKED: %d.%d.%d.%d",
ip & 0xff,
(ip >> 8) & 0xff,
(ip >> 16) & 0xff,
(ip >> 24) & 0xff);
return XDP_DROP;
}
return XDP_PASS;
}
char LICENSE[] SEC("license") = "GPL";

Our idea for the userspace code is:

1) Blacklist IP map initialization:

  1. Read the text file containing blacklisted IPs
  2. Create a map of blacklisted IP
  3. Send the values to the kernelspace map
map_fd = bpf_object__find_map_fd_by_name(
obj,
"blocked_ips"
);
for (auto ip: blocklist) {
err = bpf_map_update_elem(
map_fd,
&ip.first,
&ip.second,
BPF_ANY
);
if (err) {
fprintf(
stderr,
"Failed to insert %d: %s\n",
ip.first,
strerror(errno)
);
continue;
}
printf("Blocked: %d\n", ip.first);
}

2) Attach your program to the network interface you’re using (I’ve used wlp2s0)

link = bpf_program__attach_xdp(prog, ifindex);

3) Load your program

obj = bpf_object__open_file("../bpf/xdp_firewall.o", NULL);
if (libbpf_get_error(obj)) {
fprintf(stderr, "Failed to open BPF object\n");
return 1;
}
err = bpf_object__load(obj);
if (err) {
fprintf(stderr, "Failed to load BPF object: %d\n", err);
goto cleanup;
}
prog = bpf_object__find_program_by_name(
obj,
"xdp_parser_func"
);

The whole code is available on my Github! (shameless plug).

Limitations:

While XDP is a useful tool in networking and packet inspections, one of its limitations is that it can only process ingress packets, i.e. packets coming into the machine.

“How’s that a problem?” — XDP can only process the packets which are being received by your machine, when in fact the firewall is a traffic police for a 2 way street, for both incoming and outgoing traffic.

This can be overcome with using TC(Traffic Control), another Linux subsystem used for processing ingress and egress packets(requires qdisc and interface setup).

Some other limitations — the program being hard-coded to a single network interface, no dynamic change observation and no IPv6 support.

Here are the guides I use and some useful links:

Beej’s Guide (https://beej.us/guide)

Eunomia (https://eunomia.dev/)

Cilium Docs (https://docs.cilium.io)

https://www.slideshare.net/slideshow/xdp-and-ebpfmaps/78001591

--

--

Rithvik
Rithvik
0 followers

I write stuff.