arc: dts: use 'atmel' as manufacturer for at24 in axs10x_mb
[linux/fpc-iii.git] / samples / bpf / parse_varlen.c
blob95c16324760c0be1af8be927e1adffae0b582525
1 /* Copyright (c) 2016 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7 #define KBUILD_MODNAME "foo"
8 #include <linux/if_ether.h>
9 #include <linux/ip.h>
10 #include <linux/ipv6.h>
11 #include <linux/in.h>
12 #include <linux/tcp.h>
13 #include <linux/udp.h>
14 #include <uapi/linux/bpf.h>
15 #include <net/ip.h>
16 #include "bpf_helpers.h"
18 #define DEFAULT_PKTGEN_UDP_PORT 9
19 #define DEBUG 0
21 static int tcp(void *data, uint64_t tp_off, void *data_end)
23 struct tcphdr *tcp = data + tp_off;
25 if (tcp + 1 > data_end)
26 return 0;
27 if (tcp->dest == htons(80) || tcp->source == htons(80))
28 return TC_ACT_SHOT;
29 return 0;
32 static int udp(void *data, uint64_t tp_off, void *data_end)
34 struct udphdr *udp = data + tp_off;
36 if (udp + 1 > data_end)
37 return 0;
38 if (udp->dest == htons(DEFAULT_PKTGEN_UDP_PORT) ||
39 udp->source == htons(DEFAULT_PKTGEN_UDP_PORT)) {
40 if (DEBUG) {
41 char fmt[] = "udp port 9 indeed\n";
43 bpf_trace_printk(fmt, sizeof(fmt));
45 return TC_ACT_SHOT;
47 return 0;
50 static int parse_ipv4(void *data, uint64_t nh_off, void *data_end)
52 struct iphdr *iph;
53 uint64_t ihl_len;
55 iph = data + nh_off;
56 if (iph + 1 > data_end)
57 return 0;
59 if (ip_is_fragment(iph))
60 return 0;
61 ihl_len = iph->ihl * 4;
63 if (iph->protocol == IPPROTO_IPIP) {
64 iph = data + nh_off + ihl_len;
65 if (iph + 1 > data_end)
66 return 0;
67 ihl_len += iph->ihl * 4;
70 if (iph->protocol == IPPROTO_TCP)
71 return tcp(data, nh_off + ihl_len, data_end);
72 else if (iph->protocol == IPPROTO_UDP)
73 return udp(data, nh_off + ihl_len, data_end);
74 return 0;
77 static int parse_ipv6(void *data, uint64_t nh_off, void *data_end)
79 struct ipv6hdr *ip6h;
80 struct iphdr *iph;
81 uint64_t ihl_len = sizeof(struct ipv6hdr);
82 uint64_t nexthdr;
84 ip6h = data + nh_off;
85 if (ip6h + 1 > data_end)
86 return 0;
88 nexthdr = ip6h->nexthdr;
90 if (nexthdr == IPPROTO_IPIP) {
91 iph = data + nh_off + ihl_len;
92 if (iph + 1 > data_end)
93 return 0;
94 ihl_len += iph->ihl * 4;
95 nexthdr = iph->protocol;
96 } else if (nexthdr == IPPROTO_IPV6) {
97 ip6h = data + nh_off + ihl_len;
98 if (ip6h + 1 > data_end)
99 return 0;
100 ihl_len += sizeof(struct ipv6hdr);
101 nexthdr = ip6h->nexthdr;
104 if (nexthdr == IPPROTO_TCP)
105 return tcp(data, nh_off + ihl_len, data_end);
106 else if (nexthdr == IPPROTO_UDP)
107 return udp(data, nh_off + ihl_len, data_end);
108 return 0;
111 struct vlan_hdr {
112 uint16_t h_vlan_TCI;
113 uint16_t h_vlan_encapsulated_proto;
116 SEC("varlen")
117 int handle_ingress(struct __sk_buff *skb)
119 void *data = (void *)(long)skb->data;
120 struct ethhdr *eth = data;
121 void *data_end = (void *)(long)skb->data_end;
122 uint64_t h_proto, nh_off;
124 nh_off = sizeof(*eth);
125 if (data + nh_off > data_end)
126 return 0;
128 h_proto = eth->h_proto;
130 if (h_proto == ETH_P_8021Q || h_proto == ETH_P_8021AD) {
131 struct vlan_hdr *vhdr;
133 vhdr = data + nh_off;
134 nh_off += sizeof(struct vlan_hdr);
135 if (data + nh_off > data_end)
136 return 0;
137 h_proto = vhdr->h_vlan_encapsulated_proto;
139 if (h_proto == ETH_P_8021Q || h_proto == ETH_P_8021AD) {
140 struct vlan_hdr *vhdr;
142 vhdr = data + nh_off;
143 nh_off += sizeof(struct vlan_hdr);
144 if (data + nh_off > data_end)
145 return 0;
146 h_proto = vhdr->h_vlan_encapsulated_proto;
148 if (h_proto == htons(ETH_P_IP))
149 return parse_ipv4(data, nh_off, data_end);
150 else if (h_proto == htons(ETH_P_IPV6))
151 return parse_ipv6(data, nh_off, data_end);
152 return 0;
154 char _license[] SEC("license") = "GPL";