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