this repo has no description
1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2/* Copyright (c) 2022 Hengqi Chen */
3#include "vmlinux.h"
4#include <bpf/bpf_endian.h>
5#include <bpf/bpf_helpers.h>
6#include <bpf/bpf_tracing.h>
7
8#define TC_ACT_OK 0
9#define ETH_P_IP 0x0800 /* Internet Protocol packet */
10
11SEC("tc")
12int tc_ingress(struct __sk_buff *ctx)
13{
14 void *data_end = (void *)(__u64)ctx->data_end;
15 void *data = (void *)(__u64)ctx->data;
16 struct ethhdr *l2;
17 struct iphdr *l3;
18
19 if (ctx->protocol != bpf_htons(ETH_P_IP))
20 return TC_ACT_OK;
21
22 l2 = data;
23 if ((void *)(l2 + 1) > data_end)
24 return TC_ACT_OK;
25
26 l3 = (struct iphdr *)(l2 + 1);
27 if ((void *)(l3 + 1) > data_end)
28 return TC_ACT_OK;
29
30 bpf_printk("Got IP packet: tot_len: %d, ttl: %d", bpf_ntohs(l3->tot_len), l3->ttl);
31 return TC_ACT_OK;
32}
33
34char __license[] SEC("license") = "GPL";