-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast_two.c
57 lines (39 loc) · 1.41 KB
/
last_two.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <netinet/in.h>
#include "common.h"
SEC("xdp_prog")
int prog(struct xdp_md* ctx) {
// Initialize data and data end.
void* data = (void*)(long)ctx->data;
void* data_end = (void*)(long)ctx->data_end;
// Initialite IP header and check.
struct iphdr* iph = data + sizeof(struct ethhdr);
if (iph + 1 > (struct iphdr *)data_end)
return XDP_DROP;
// We only want to deal with TCP packets.
if (iph->protocol != IPPROTO_TCP)
return XDP_PASS;
// Initialize TCP header and check.
struct tcphdr* tcph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
if (tcph + 1 > (struct tcphdr *)data_end)
return XDP_DROP;
// Retrieve IP header's length.
__u16 ipLen = ntohs(iph->tot_len);
// Initialize payload.
__u8* pl = data + sizeof(struct ethhdr) + (iph->ihl * 4) + (tcph->doff * 4);
// Retrieve offset to last packet.
__u16 off = ipLen - (iph->ihl * 4) - (tcph->doff * 4);
// Initialize last byte of data and check.
__u8* last = pl + off;
if (last + 1 > (__u8*)data_end)
return XDP_PASS;
if (last < (__u8*)data)
return XDP_PASS;
// Print the last byte of data to /sys/kernel/tracing/trace_pipe.
bpf_printk("Last byte of packet is %d.\n", *last);
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";