Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved IP Handling, Error Management, and GitHub Actions Integration #1

Merged
merged 7 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Test XDP program compile/load/attach on Ubuntu

on: [push, pull_request]

jobs:
xdp_test:
runs-on: ubuntu-latest

env:
C_INCLUDE_PATH: /usr/include/x86_64-linux-gnu
IFACE: lo

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up BPF toolchain
run: |
sudo apt-get update
sudo apt-get install clang llvm libbpf-dev make linux-headers-$(uname -r) -y
find /usr/include -name 'types.h'

- name: Test compile XDP program
run: make compile

- name: Test attach XDP program
run: make attach

- name: Check if XDP program is attached
run: make iface-inspect

- name: Dump BPF map
run: make dump

- name: Test detach XDP program
run: make detach
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ attach:
detach:
sudo ip link set dev $(IFACE) xdp off

iface-inspect:
sudo ip link show $(IFACE)

dump:
sudo bpftool map dump name $(MAP) &> /dev/null
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Test XDP program compile/load/attach on Ubuntu](https://github.com/SRodi/xdp-ddos-protect/actions/workflows/test.yml/badge.svg)](https://github.com/SRodi/xdp-ddos-protect/actions/workflows/test.yml)

# Protect from DDoS attacks with XDP

The [xdp_ddos_protection.c](./xdp_ddos_protection.c) file contains an eBPF program designed for DDoS protection using XDP (eXpress Data Path).
Expand Down
14 changes: 10 additions & 4 deletions xdp_ddos_protection.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ struct {

SEC("xdp") int ddos_protection(struct xdp_md *ctx) {
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data; // Parse Ethernet header
void *data = (void *)(long)ctx->data;

// Parse Ethernet header
struct ethhdr *eth = data;

// Check if packet is large enough to contain Ethernet header
if ((void *)(eth + 1) > data_end)
return XDP_PASS;

Expand All @@ -33,10 +35,12 @@ SEC("xdp") int ddos_protection(struct xdp_md *ctx) {

// Parse IP header
struct iphdr *iph = (void *)(eth + 1);
// Check if ethernet frame is large enough to contain IP header
if ((void *)(iph + 1) > data_end)
return XDP_PASS;

__u32 src_ip = iph->saddr; // Source IP address
// Convert source IP from network to host byte order
__u32 src_ip = __builtin_bswap32(iph->saddr);

// Lookup rate limit entry for this IP
struct rate_limit_entry *entry = bpf_map_lookup_elem(&rate_limit_map, &src_ip);
Expand All @@ -63,7 +67,9 @@ SEC("xdp") int ddos_protection(struct xdp_md *ctx) {
__builtin_memset(&new_entry, 0, sizeof(new_entry));
new_entry.last_update = current_time;
new_entry.packet_count = 1;
bpf_map_update_elem(&rate_limit_map, &src_ip, &new_entry, BPF_ANY);
if (bpf_map_update_elem(&rate_limit_map, &src_ip, &new_entry, BPF_ANY) != 0) {
return XDP_ABORTED; // Handle error if update fails
}
}
return XDP_PASS; // Allow packet if under threshold
}
Expand Down
Loading