0001
0002
0003
0004
0005
0006
0007 #define KBUILD_MODNAME "foo"
0008 #include <linux/ip.h>
0009 #include <linux/ipv6.h>
0010 #include <linux/in.h>
0011 #include <linux/tcp.h>
0012 #include <linux/udp.h>
0013 #include <uapi/linux/bpf.h>
0014 #include <bpf/bpf_helpers.h>
0015 #include "bpf_legacy.h"
0016
0017 #define DEFAULT_PKTGEN_UDP_PORT 9
0018 #define IP_MF 0x2000
0019 #define IP_OFFSET 0x1FFF
0020
0021 static inline int ip_is_fragment(struct __sk_buff *ctx, __u64 nhoff)
0022 {
0023 return load_half(ctx, nhoff + offsetof(struct iphdr, frag_off))
0024 & (IP_MF | IP_OFFSET);
0025 }
0026
0027 SEC("ldabs")
0028 int handle_ingress(struct __sk_buff *skb)
0029 {
0030 __u64 troff = ETH_HLEN + sizeof(struct iphdr);
0031
0032 if (load_half(skb, offsetof(struct ethhdr, h_proto)) != ETH_P_IP)
0033 return 0;
0034 if (load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol)) != IPPROTO_UDP ||
0035 load_byte(skb, ETH_HLEN) != 0x45)
0036 return 0;
0037 if (ip_is_fragment(skb, ETH_HLEN))
0038 return 0;
0039 if (load_half(skb, troff + offsetof(struct udphdr, dest)) == DEFAULT_PKTGEN_UDP_PORT)
0040 return TC_ACT_SHOT;
0041 return 0;
0042 }
0043 char _license[] SEC("license") = "GPL";