65 lines · c
1// SPDX-License-Identifier: GPL-2.02#ifndef BPF_NO_PRESERVE_ACCESS_INDEX3#define BPF_NO_PRESERVE_ACCESS_INDEX4#endif5#include <vmlinux.h>6#include <bpf/bpf_core_read.h>7#include <bpf/bpf_helpers.h>8 9#define INLINE __always_inline10 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 15static 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 26out:27 return ip;28}29 30SEC("tc")31int main_prog(struct __sk_buff *skb)32{33 struct iphdr *ip = NULL;34 struct tcphdr *tcp;35 __u8 proto = 0;36 int urg_ptr;37 u32 offset;38 39 if (!(ip = get_iphdr(skb)))40 goto out;41 42 proto = ip->protocol;43 44 if (proto != IPPROTO_TCP)45 goto out;46 47 tcp = (void*)(ip + 1);48 if (tcp->dest != 0)49 goto out;50 if (!tcp)51 goto out;52 53 urg_ptr = tcp->urg_ptr;54 55 /* Checksum validation part */56 proto++;57 offset = sizeof(struct ethhdr) + offsetof(struct iphdr, protocol);58 bpf_skb_store_bytes(skb, offset, &proto, sizeof(proto), BPF_F_RECOMPUTE_CSUM);59 60 return urg_ptr;61out:62 return -1;63}64char _license[] SEC("license") = "GPL";65