aboutsummaryrefslogtreecommitdiff
path: root/pa4_ipsec/ingress.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-12-15 22:37:30 -0700
committerJack Sangdahl <[email protected]>2024-12-15 22:37:30 -0700
commit9c5b2396436398a3e205831c007ecc4e2b2c604e (patch)
tree763566660faf574ab04fba71b82e3c32c0daac0d /pa4_ipsec/ingress.c
parentd3b273cbebd8a4d6b890d11bd4ae505b45abf739 (diff)
downloadecen5273-9c5b2396436398a3e205831c007ecc4e2b2c604e.tar.gz
ecen5273-9c5b2396436398a3e205831c007ecc4e2b2c604e.zip
Programming Assignment 4: eBPF IPsecHEADmaster
Signed-off-by: Jack Sangdahl <[email protected]>
Diffstat (limited to 'pa4_ipsec/ingress.c')
-rw-r--r--pa4_ipsec/ingress.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/pa4_ipsec/ingress.c b/pa4_ipsec/ingress.c
new file mode 100644
index 0000000..1021687
--- /dev/null
+++ b/pa4_ipsec/ingress.c
@@ -0,0 +1,36 @@
+// ingress.c
+#include <vmlinux.h>
+
+#include <bpf/bpf_endian.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#define TC_ACT_OK 0
+#define ETH_P_IP 0x0800 /* Internet Protocol packet */
+
+/// @tchook {"ifindex":2, "attach_point":"BPF_TC_INGRESS"}
+/// @tcopts {"handle":1, "priority":1}
+SEC("tc")
+int tc_ingress(struct __sk_buff *ctx) {
+ void *data_end = (void *)(__u64)ctx->data_end;
+ void *data = (void *)(__u64)ctx->data;
+ struct ethhdr *l2;
+ struct iphdr *l3;
+
+ if (ctx->protocol != bpf_htons(ETH_P_IP))
+ return TC_ACT_OK;
+
+ l2 = data;
+ if ((void *)(l2 + 1) > data_end)
+ return TC_ACT_OK;
+
+ l3 = (struct iphdr *)(l2 + 1);
+ if ((void *)(l3 + 1) > data_end)
+ return TC_ACT_OK;
+
+ bpf_printk("Got IP packet: tot_len: %d, ttl: %d", bpf_ntohs(l3->tot_len),
+ l3->ttl);
+ return TC_ACT_OK;
+}
+
+char __license[] SEC("license") = "GPL";