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