Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #define BPF_NO_PRESERVE_ACCESS_INDEX
0003 #include <vmlinux.h>
0004 #include <bpf/bpf_core_read.h>
0005 #include <bpf/bpf_helpers.h>
0006 
0007 #define INLINE __always_inline
0008 
0009 #define skb_shorter(skb, len) ((void *)(long)(skb)->data + (len) > (void *)(long)skb->data_end)
0010 
0011 #define ETH_IPV4_TCP_SIZE (14 + sizeof(struct iphdr) + sizeof(struct tcphdr))
0012 
0013 static INLINE struct iphdr *get_iphdr(struct __sk_buff *skb)
0014 {
0015     struct iphdr *ip = NULL;
0016     struct ethhdr *eth;
0017 
0018     if (skb_shorter(skb, ETH_IPV4_TCP_SIZE))
0019         goto out;
0020 
0021     eth = (void *)(long)skb->data;
0022     ip = (void *)(eth + 1);
0023 
0024 out:
0025     return ip;
0026 }
0027 
0028 SEC("tc")
0029 int main_prog(struct __sk_buff *skb)
0030 {
0031     struct iphdr *ip = NULL;
0032     struct tcphdr *tcp;
0033     __u8 proto = 0;
0034 
0035     if (!(ip = get_iphdr(skb)))
0036         goto out;
0037 
0038     proto = ip->protocol;
0039 
0040     if (proto != IPPROTO_TCP)
0041         goto out;
0042 
0043     tcp = (void*)(ip + 1);
0044     if (tcp->dest != 0)
0045         goto out;
0046     if (!tcp)
0047         goto out;
0048 
0049     return tcp->urg_ptr;
0050 out:
0051     return -1;
0052 }
0053 char _license[] SEC("license") = "GPL";