aboutsummaryrefslogtreecommitdiff
path: root/xdp_udp_handler.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2025-06-16 17:09:43 +0200
committerToni Uhlig <matzeton@googlemail.com>2025-06-18 15:16:30 +0200
commit0730e77eb4aa9841e90a17f190b9ae7d80565054 (patch)
treeb214414fb23070fcfcbfc880f78eb7b4cef987c8 /xdp_udp_handler.c
Initial commitmain
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'xdp_udp_handler.c')
-rw-r--r--xdp_udp_handler.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/xdp_udp_handler.c b/xdp_udp_handler.c
new file mode 100644
index 0000000..930667f
--- /dev/null
+++ b/xdp_udp_handler.c
@@ -0,0 +1,127 @@
+#include <linux/bpf.h>
+#include <linux/in.h>
+
+#include <bpf/bpf_helpers.h>
+
+#include "xdp_checksum.h"
+#include "xdp_manip.h"
+#include "xdp_parser.h"
+
+#ifdef XDP_DEBUG
+#define dbg_bpf_printk(fmt, args...) bpf_printk(fmt, ##args)
+#else
+#define dbg_bpf_printk(fmt, args...)
+#endif
+
+#include "common.h"
+
+SEC("xdp")
+int xdp_udp_handler(struct xdp_md *ctx) {
+ void *data_end = (void *)(long)ctx->data_end;
+ void *data = (void *)(long)ctx->data;
+ __u32 action = XDP_PASS; /* Default action */
+ struct hdr_cursor nh = {.pos = data};
+ int nh_type;
+ struct ethhdr *ethhdr;
+ struct iphdr *iphdr = NULL;
+ struct ipv6hdr *ip6hdr = NULL;
+ struct udphdr *udphdr;
+ static const __u8 payload_request[] = {PAYLOAD_REQUEST};
+ static const __u32 payload_request_size = sizeof(payload_request);
+ static const __u8 payload_response[] = {PAYLOAD_RESPONSE};
+ __u32 payload_size;
+
+ nh_type = parse_ethhdr(&nh, data_end, &ethhdr);
+
+ switch (__constant_ntohs(nh_type)) {
+ case ETH_P_IP:
+ nh_type = parse_iphdr(&nh, data_end, &iphdr);
+ break;
+ case ETH_P_IPV6:
+ nh_type = parse_ip6hdr(&nh, data_end, &ip6hdr);
+ break;
+ default:
+ goto out;
+ }
+
+ if (nh_type != IPPROTO_UDP) {
+ goto out;
+ }
+ if (parse_udphdr(&nh, data_end, &udphdr) < 0) {
+ action = XDP_ABORTED;
+ goto out;
+ }
+ if (udphdr->dest != __constant_htons(UDP_PORT)) {
+ goto out;
+ }
+
+ payload_size = (__u8 *)data_end - (__u8 *)nh.pos;
+ if (payload_size < payload_request_size) {
+ goto out;
+ }
+
+ if (nh.pos_u8 + payload_request_size + 1 > (__u8 *)data_end) {
+ goto out;
+ }
+ if (__builtin_memcmp(nh.pos_u8, payload_request, sizeof(payload_request)) !=
+ 0) {
+ goto out;
+ }
+
+ dbg_bpf_printk("Received UDP Ping Request #%u with size %u", nh.pos_u8[11],
+ payload_size);
+
+ /* Shrink UDP packet for Ping Reply */
+ if (bpf_xdp_adjust_tail(ctx, payload_request_size - payload_size) != 0) {
+ return XDP_ABORTED;
+ }
+
+ /* Set data pointers (may not be valid anymore) */
+ data = (void *)(long)ctx->data;
+ data_end = (void *)(long)ctx->data_end;
+ nh.pos = data;
+
+ if (parse_ethhdr(&nh, data_end, &ethhdr) < 0) {
+ action = XDP_ABORTED;
+ goto out;
+ }
+
+ payload_size -= 4;
+
+ if (iphdr != NULL) {
+ if (parse_iphdr(&nh, data_end, &iphdr) < 0) {
+ action = XDP_ABORTED;
+ goto out;
+ }
+ if (parse_udphdr(&nh, data_end, &udphdr) < 0) {
+ action = XDP_ABORTED;
+ goto out;
+ }
+ swap_eth_addr(ethhdr);
+ udphdr->len = __constant_htons(payload_size + sizeof(*udphdr));
+ iphdr->tot_len =
+ __constant_htons(iphdr->ihl * 4 + payload_size + sizeof(*udphdr));
+ swap_ip4_addr(iphdr);
+ iphdr->check = ip_checksum(iphdr);
+ if (nh.pos_u8 + sizeof(payload_response) > (__u8 *)data_end) {
+ action = XDP_ABORTED;
+ goto out;
+ }
+ __builtin_memcpy(nh.pos_u8, payload_response, sizeof(payload_response));
+ swap_udp_port(udphdr);
+ udphdr->check = udp_checksum(iphdr, udphdr, data_end);
+ } else if (ip6hdr != NULL) {
+ parse_ip6hdr(&nh, data_end, &ip6hdr);
+ goto out; // TODO: Implement!
+ } else {
+ action = XDP_ABORTED;
+ goto out;
+ }
+
+ action = XDP_TX;
+out:
+ return action;
+}
+
+char _license[] SEC("license") =
+ "GPL"; // Please keep this to prevent tainting kernel