Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2016 Facebook
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
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 <net/ip.h>
0015 #include <bpf/bpf_helpers.h>
0016 
0017 #define DEFAULT_PKTGEN_UDP_PORT 9
0018 
0019 /* copy of 'struct ethhdr' without __packed */
0020 struct eth_hdr {
0021     unsigned char   h_dest[ETH_ALEN];
0022     unsigned char   h_source[ETH_ALEN];
0023     unsigned short  h_proto;
0024 };
0025 
0026 SEC("simple")
0027 int handle_ingress(struct __sk_buff *skb)
0028 {
0029     void *data = (void *)(long)skb->data;
0030     struct eth_hdr *eth = data;
0031     struct iphdr *iph = data + sizeof(*eth);
0032     struct udphdr *udp = data + sizeof(*eth) + sizeof(*iph);
0033     void *data_end = (void *)(long)skb->data_end;
0034 
0035     /* single length check */
0036     if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*udp) > data_end)
0037         return 0;
0038 
0039     if (eth->h_proto != htons(ETH_P_IP))
0040         return 0;
0041     if (iph->protocol != IPPROTO_UDP || iph->ihl != 5)
0042         return 0;
0043     if (ip_is_fragment(iph))
0044         return 0;
0045     if (udp->dest == htons(DEFAULT_PKTGEN_UDP_PORT))
0046         return TC_ACT_SHOT;
0047     return 0;
0048 }
0049 char _license[] SEC("license") = "GPL";