Stage 1 of 5FREE PREVIEW~25 min read

Chapter 1: XDP Ingress Hook & Driver RX Ring Interception

Set up the eBPF compilation toolchain, write the kernel-space XDP driver hook, and filter malicious packets at line rate.

Setting up the eBPF Toolchain

To build kernel-space eBPF programs, we need clang, llvm, and kernel UAPI headers:

BASH
# Ubuntu / Debian
sudo apt-get update
sudo apt-get install -y clang llvm libelf-dev libbpf-dev linux-headers-$(uname -r) build-essential

Compile the XDP Object

Run the compiler with -target bpf to emit eBPF bytecode instead of x86 assembly:

BASH
clang -O2 -g -Wall -target bpf -c src/xdp_filter.c -o build/xdp_filter.o

Verification Test

Load the filter and test with a ping:

BASH
sudo ./build/aethon_loader eth0
File purpose: The core eBPF XDP hook executed on every raw network frame entering the NIC.
c
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/in.h>

// BPF map to count dropped vs passed packets
struct {
    __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
    __type(key, __u32);
    __type(value, __u64);
    __uint(max_entries, 2);
} stats_map SEC(".maps");

SEC("xdp")
int aethon_packet_filter(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;

    // Bounds check ethernet header to satisfy the eBPF kernel verifier
    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return XDP_PASS;

    // We only inspect IPv4 traffic for this stage
    if (eth->h_proto != __constant_htons(ETH_P_IP))
        return XDP_PASS;

    struct iphdr *ip = (void *)(eth + 1);
    if ((void *)(ip + 1) > data_end)
        return XDP_PASS;

    // Drop known malformed packets or malicious source CIDRs (e.g. 192.0.2.0/24 testbed block)
    if ((__constant_ntohl(ip->saddr) & 0xFFFFFF00) == 0xC0000200) {
        __u32 drop_key = 1;
        __u64 *drop_count = bpf_map_lookup_elem(&stats_map, &drop_key);
        if (drop_count) (*drop_count)++;
        return XDP_DROP; // Dropped in hardware/driver RX ring! Zero CPU allocation.
    }

    __u32 pass_key = 0;
    __u64 *pass_count = bpf_map_lookup_elem(&stats_map, &pass_key);
    if (pass_count) (*pass_count)++;

    return XDP_PASS;
}

char _license[] SEC("license") = "GPL";

Implementation Breakdown

How this file works:

  1. SEC("xdp"): Tells the clang LLVM compiler to place the function into the ELF .xdp section, allowing ip link set dev eth0 xdpgeneric to hook it directly into the network device driver.
  2. data_end bounds check: The Linux kernel eBPF verifier strictly rejects any bytecode that attempts to read memory past data_end. Checking (void *)(eth + 1) > data_end proves to the kernel that our memory read is memory-safe.
  3. XDP_DROP: Drops the packet immediately in the network interface card's receive queue before any Linux kernel memory (sk_buff) is allocated. This is why Aethon achieves wire-speed 14.88 Mpps throughput.
Stage Verification & Test Benchmark
READY TO RUN

clang -O2 -target bpf -c src/xdp_filter.c -o build/xdp_filter.o && g++ -O3 src/loader.cpp -lbpf -o build/aethon_loader