^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* Copyright (c) 2016 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define KBUILD_MODNAME "foo"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DEFAULT_PKTGEN_UDP_PORT 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* copy of 'struct ethhdr' without __packed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct eth_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned char h_dest[ETH_ALEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned char h_source[ETH_ALEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned short h_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) SEC("simple")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int handle_ingress(struct __sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void *data = (void *)(long)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct eth_hdr *eth = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct iphdr *iph = data + sizeof(*eth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct udphdr *udp = data + sizeof(*eth) + sizeof(*iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void *data_end = (void *)(long)skb->data_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* single length check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*udp) > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (eth->h_proto != htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (iph->protocol != IPPROTO_UDP || iph->ihl != 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (ip_is_fragment(iph))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (udp->dest == htons(DEFAULT_PKTGEN_UDP_PORT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return TC_ACT_SHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) char _license[] SEC("license") = "GPL";