1 // SPDX-License-Identifier: GPL-2.0
2 #define BPF_NO_PRESERVE_ACCESS_INDEX
3 #include <vmlinux.h>
4 #include <bpf/bpf_core_read.h>
5 #include <bpf/bpf_helpers.h>
6 
7 #define NULL 0
8 #define INLINE __always_inline
9 
10 #define skb_shorter(skb, len) ((void *)(long)(skb)->data + (len) > (void *)(long)skb->data_end)
11 
12 #define ETH_IPV4_TCP_SIZE (14 + sizeof(struct iphdr) + sizeof(struct tcphdr))
13 
14 static INLINE struct iphdr *get_iphdr(struct __sk_buff *skb)
15 {
16 	struct iphdr *ip = NULL;
17 	struct ethhdr *eth;
18 
19 	if (skb_shorter(skb, ETH_IPV4_TCP_SIZE))
20 		goto out;
21 
22 	eth = (void *)(long)skb->data;
23 	ip = (void *)(eth + 1);
24 
25 out:
26 	return ip;
27 }
28 
29 SEC("classifier/cls")
30 int main_prog(struct __sk_buff *skb)
31 {
32 	struct iphdr *ip = NULL;
33 	struct tcphdr *tcp;
34 	__u8 proto = 0;
35 
36 	if (!(ip = get_iphdr(skb)))
37 		goto out;
38 
39 	proto = ip->protocol;
40 
41 	if (proto != IPPROTO_TCP)
42 		goto out;
43 
44 	tcp = (void*)(ip + 1);
45 	if (tcp->dest != 0)
46 		goto out;
47 	if (!tcp)
48 		goto out;
49 
50 	return tcp->urg_ptr;
51 out:
52 	return -1;
53 }
54 char _license[] SEC("license") = "GPL";
55