For my master thesis I am working with eBPF, the [Extended Berkeley Packet Filter](https://www.kernel.org/doc/Documentation/networking/filter.txt).
By now it is used by several subsystems in the Linux kernel,
ranging from tracing and seccomp rules to network filtering.
As I am using it for network filtering I wanted a small useful and working example on how to parse
and resend packets with it.
Luckily, the hard part of attaching it early in the packet processing pipeline is already handled by `tc`,
Linux' traffic control utility from the iproute2 project.
However, it took me a while to get a reliably working ICMP ping-pong example to work.
Now that I have I published it to save others the trouble.
The result is online in the [ebpf-icmp-ping][git] repository.
The rest of the blog post will explain some of the steps in [`bpf.c`](https://github.com/badboy/ebpf-icmp-ping/blob/cf2c1ff5bc16049e64bf8424984d226ecaa468ea/bpf.c) and how it is used.
A subset of C can be compiled to the eBPF bytecode
and luckily the Clang compiler has a eBPF backend to make it all work.
The usable subset is a lot more restricted than plain C and requires a bit more boilerplate
to assist the compiler (and Kernel verifier) to produce safe programs.
All memory access needs to be checked up front.
Assigning from one part in the passed buffer to another might fail
(I'm not 100% sure yet whether that's due to restrictions of eBPF or the code generation).
And you can't have loops, but luckily Clang/LLVM is quite good at unrolling loops with a fixed iteration count.
Let's dive in.
First we define our function and put it in a specific section of the generated ELF file.
`tc` will know how to pull it out.
Our function gets a single pointer to a kernel-allocated buffer of the network packet.
~~~c
SEC("action")
int pingpong(struct __sk_buff *skb)
~~~
Accessing data in this buffer can be done using different methods.
Either read out bytes at specified offsets or rely on the struct definitions of the Kernel.
We do the latter, but first we need to check that there is enough data.