1bd4aed0eSJiong Wang // SPDX-License-Identifier: GPL-2.0
2bd4aed0eSJiong Wang #include <limits.h>
3bd4aed0eSJiong Wang #include <stddef.h>
4bd4aed0eSJiong Wang #include <stdbool.h>
5bd4aed0eSJiong Wang #include <string.h>
6bd4aed0eSJiong Wang #include <linux/pkt_cls.h>
7bd4aed0eSJiong Wang #include <linux/bpf.h>
8bd4aed0eSJiong Wang #include <linux/in.h>
9bd4aed0eSJiong Wang #include <linux/if_ether.h>
10bd4aed0eSJiong Wang #include <linux/icmp.h>
11bd4aed0eSJiong Wang #include <linux/ip.h>
12bd4aed0eSJiong Wang #include <linux/ipv6.h>
13bd4aed0eSJiong Wang #include <linux/tcp.h>
14bd4aed0eSJiong Wang #include <linux/udp.h>
15bd4aed0eSJiong Wang #include <linux/if_packet.h>
16bd4aed0eSJiong Wang #include <sys/socket.h>
17bd4aed0eSJiong Wang #include <linux/if_tunnel.h>
18bd4aed0eSJiong Wang #include <linux/mpls.h>
193e689141SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
203e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
21bd4aed0eSJiong Wang
22b4b8a3bfSJakub Sitnicki #define PROG(F) PROG_(F, _##F)
2315669e1dSAndrii Nakryiko #define PROG_(NUM, NAME) SEC("flow_dissector") int flow_dissector_##NUM
24bd4aed0eSJiong Wang
25d6513727SShmulik Ladkani #define FLOW_CONTINUE_SADDR 0x7f00007f /* 127.0.0.127 */
26d6513727SShmulik Ladkani
27bd4aed0eSJiong Wang /* These are the identifiers of the BPF programs that will be used in tail
28bd4aed0eSJiong Wang * calls. Name is limited to 16 characters, with the terminating character and
29bd4aed0eSJiong Wang * bpf_func_ above, we have only 6 to work with, anything after will be cropped.
30bd4aed0eSJiong Wang */
31b4b8a3bfSJakub Sitnicki #define IP 0
32b4b8a3bfSJakub Sitnicki #define IPV6 1
33b4b8a3bfSJakub Sitnicki #define IPV6OP 2 /* Destination/Hop-by-Hop Options IPv6 Ext. Header */
34b4b8a3bfSJakub Sitnicki #define IPV6FR 3 /* Fragmentation IPv6 Extension Header */
35b4b8a3bfSJakub Sitnicki #define MPLS 4
36b4b8a3bfSJakub Sitnicki #define VLAN 5
37b4b8a3bfSJakub Sitnicki #define MAX_PROG 6
38bd4aed0eSJiong Wang
39bd4aed0eSJiong Wang #define IP_MF 0x2000
40bd4aed0eSJiong Wang #define IP_OFFSET 0x1FFF
41bd4aed0eSJiong Wang #define IP6_MF 0x0001
42bd4aed0eSJiong Wang #define IP6_OFFSET 0xFFF8
43bd4aed0eSJiong Wang
44bd4aed0eSJiong Wang struct vlan_hdr {
45bd4aed0eSJiong Wang __be16 h_vlan_TCI;
46bd4aed0eSJiong Wang __be16 h_vlan_encapsulated_proto;
47bd4aed0eSJiong Wang };
48bd4aed0eSJiong Wang
49bd4aed0eSJiong Wang struct gre_hdr {
50bd4aed0eSJiong Wang __be16 flags;
51bd4aed0eSJiong Wang __be16 proto;
52bd4aed0eSJiong Wang };
53bd4aed0eSJiong Wang
54bd4aed0eSJiong Wang struct frag_hdr {
55bd4aed0eSJiong Wang __u8 nexthdr;
56bd4aed0eSJiong Wang __u8 reserved;
57bd4aed0eSJiong Wang __be16 frag_off;
58bd4aed0eSJiong Wang __be32 identification;
59bd4aed0eSJiong Wang };
60bd4aed0eSJiong Wang
61df0b7792SAndrii Nakryiko struct {
62bc7430ccSAndrii Nakryiko __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
63b4b8a3bfSJakub Sitnicki __uint(max_entries, MAX_PROG);
64bc7430ccSAndrii Nakryiko __uint(key_size, sizeof(__u32));
65bc7430ccSAndrii Nakryiko __uint(value_size, sizeof(__u32));
66bc7430ccSAndrii Nakryiko } jmp_table SEC(".maps");
67bd4aed0eSJiong Wang
68df0b7792SAndrii Nakryiko struct {
699840a4ffSPetar Penkov __uint(type, BPF_MAP_TYPE_HASH);
709840a4ffSPetar Penkov __uint(max_entries, 1024);
71bc7430ccSAndrii Nakryiko __type(key, __u32);
72bc7430ccSAndrii Nakryiko __type(value, struct bpf_flow_keys);
73bc7430ccSAndrii Nakryiko } last_dissection SEC(".maps");
740905beecSStanislav Fomichev
export_flow_keys(struct bpf_flow_keys * keys,int ret)750905beecSStanislav Fomichev static __always_inline int export_flow_keys(struct bpf_flow_keys *keys,
760905beecSStanislav Fomichev int ret)
770905beecSStanislav Fomichev {
789840a4ffSPetar Penkov __u32 key = (__u32)(keys->sport) << 16 | keys->dport;
799840a4ffSPetar Penkov struct bpf_flow_keys val;
800905beecSStanislav Fomichev
819840a4ffSPetar Penkov memcpy(&val, keys, sizeof(val));
829840a4ffSPetar Penkov bpf_map_update_elem(&last_dissection, &key, &val, BPF_ANY);
830905beecSStanislav Fomichev return ret;
840905beecSStanislav Fomichev }
850905beecSStanislav Fomichev
8671c99e32SStanislav Fomichev #define IPV6_FLOWLABEL_MASK __bpf_constant_htonl(0x000FFFFF)
ip6_flowlabel(const struct ipv6hdr * hdr)8771c99e32SStanislav Fomichev static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr)
8871c99e32SStanislav Fomichev {
8971c99e32SStanislav Fomichev return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK;
9071c99e32SStanislav Fomichev }
9171c99e32SStanislav Fomichev
bpf_flow_dissect_get_header(struct __sk_buff * skb,__u16 hdr_size,void * buffer)92bd4aed0eSJiong Wang static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb,
93bd4aed0eSJiong Wang __u16 hdr_size,
94bd4aed0eSJiong Wang void *buffer)
95bd4aed0eSJiong Wang {
96bd4aed0eSJiong Wang void *data_end = (void *)(long)skb->data_end;
97bd4aed0eSJiong Wang void *data = (void *)(long)skb->data;
98bd4aed0eSJiong Wang __u16 thoff = skb->flow_keys->thoff;
99bd4aed0eSJiong Wang __u8 *hdr;
100bd4aed0eSJiong Wang
101bd4aed0eSJiong Wang /* Verifies this variable offset does not overflow */
102bd4aed0eSJiong Wang if (thoff > (USHRT_MAX - hdr_size))
103bd4aed0eSJiong Wang return NULL;
104bd4aed0eSJiong Wang
105bd4aed0eSJiong Wang hdr = data + thoff;
106bd4aed0eSJiong Wang if (hdr + hdr_size <= data_end)
107bd4aed0eSJiong Wang return hdr;
108bd4aed0eSJiong Wang
109bd4aed0eSJiong Wang if (bpf_skb_load_bytes(skb, thoff, buffer, hdr_size))
110bd4aed0eSJiong Wang return NULL;
111bd4aed0eSJiong Wang
112bd4aed0eSJiong Wang return buffer;
113bd4aed0eSJiong Wang }
114bd4aed0eSJiong Wang
115bd4aed0eSJiong Wang /* Dispatches on ETHERTYPE */
parse_eth_proto(struct __sk_buff * skb,__be16 proto)116bd4aed0eSJiong Wang static __always_inline int parse_eth_proto(struct __sk_buff *skb, __be16 proto)
117bd4aed0eSJiong Wang {
118bd4aed0eSJiong Wang struct bpf_flow_keys *keys = skb->flow_keys;
119bd4aed0eSJiong Wang
120bd4aed0eSJiong Wang switch (proto) {
121bd4aed0eSJiong Wang case bpf_htons(ETH_P_IP):
122faef26faSDaniel Borkmann bpf_tail_call_static(skb, &jmp_table, IP);
123bd4aed0eSJiong Wang break;
124bd4aed0eSJiong Wang case bpf_htons(ETH_P_IPV6):
125faef26faSDaniel Borkmann bpf_tail_call_static(skb, &jmp_table, IPV6);
126bd4aed0eSJiong Wang break;
127bd4aed0eSJiong Wang case bpf_htons(ETH_P_MPLS_MC):
128bd4aed0eSJiong Wang case bpf_htons(ETH_P_MPLS_UC):
129faef26faSDaniel Borkmann bpf_tail_call_static(skb, &jmp_table, MPLS);
130bd4aed0eSJiong Wang break;
131bd4aed0eSJiong Wang case bpf_htons(ETH_P_8021Q):
132bd4aed0eSJiong Wang case bpf_htons(ETH_P_8021AD):
133faef26faSDaniel Borkmann bpf_tail_call_static(skb, &jmp_table, VLAN);
134bd4aed0eSJiong Wang break;
135bd4aed0eSJiong Wang default:
136bd4aed0eSJiong Wang /* Protocol not supported */
1370905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
138bd4aed0eSJiong Wang }
139bd4aed0eSJiong Wang
1400905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
141bd4aed0eSJiong Wang }
142bd4aed0eSJiong Wang
143bd4aed0eSJiong Wang SEC("flow_dissector")
_dissect(struct __sk_buff * skb)144bd4aed0eSJiong Wang int _dissect(struct __sk_buff *skb)
145bd4aed0eSJiong Wang {
146822fe617SStanislav Fomichev struct bpf_flow_keys *keys = skb->flow_keys;
147822fe617SStanislav Fomichev
148d6513727SShmulik Ladkani if (keys->n_proto == bpf_htons(ETH_P_IP)) {
149d6513727SShmulik Ladkani /* IP traffic from FLOW_CONTINUE_SADDR falls-back to
150d6513727SShmulik Ladkani * standard dissector
151d6513727SShmulik Ladkani */
152d6513727SShmulik Ladkani struct iphdr *iph, _iph;
153d6513727SShmulik Ladkani
154d6513727SShmulik Ladkani iph = bpf_flow_dissect_get_header(skb, sizeof(*iph), &_iph);
155d6513727SShmulik Ladkani if (iph && iph->ihl == 5 &&
156d6513727SShmulik Ladkani iph->saddr == bpf_htonl(FLOW_CONTINUE_SADDR)) {
157d6513727SShmulik Ladkani return BPF_FLOW_DISSECTOR_CONTINUE;
158d6513727SShmulik Ladkani }
159d6513727SShmulik Ladkani }
160d6513727SShmulik Ladkani
161822fe617SStanislav Fomichev return parse_eth_proto(skb, keys->n_proto);
162bd4aed0eSJiong Wang }
163bd4aed0eSJiong Wang
164bd4aed0eSJiong Wang /* Parses on IPPROTO_* */
parse_ip_proto(struct __sk_buff * skb,__u8 proto)165bd4aed0eSJiong Wang static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)
166bd4aed0eSJiong Wang {
167bd4aed0eSJiong Wang struct bpf_flow_keys *keys = skb->flow_keys;
168bd4aed0eSJiong Wang void *data_end = (void *)(long)skb->data_end;
169bd4aed0eSJiong Wang struct icmphdr *icmp, _icmp;
170bd4aed0eSJiong Wang struct gre_hdr *gre, _gre;
171bd4aed0eSJiong Wang struct ethhdr *eth, _eth;
172bd4aed0eSJiong Wang struct tcphdr *tcp, _tcp;
173bd4aed0eSJiong Wang struct udphdr *udp, _udp;
174bd4aed0eSJiong Wang
175bd4aed0eSJiong Wang switch (proto) {
176bd4aed0eSJiong Wang case IPPROTO_ICMP:
177bd4aed0eSJiong Wang icmp = bpf_flow_dissect_get_header(skb, sizeof(*icmp), &_icmp);
178bd4aed0eSJiong Wang if (!icmp)
1790905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
1800905beecSStanislav Fomichev return export_flow_keys(keys, BPF_OK);
181bd4aed0eSJiong Wang case IPPROTO_IPIP:
182bd4aed0eSJiong Wang keys->is_encap = true;
183e853ae77SStanislav Fomichev if (keys->flags & BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP)
184e853ae77SStanislav Fomichev return export_flow_keys(keys, BPF_OK);
185e853ae77SStanislav Fomichev
186bd4aed0eSJiong Wang return parse_eth_proto(skb, bpf_htons(ETH_P_IP));
187bd4aed0eSJiong Wang case IPPROTO_IPV6:
188bd4aed0eSJiong Wang keys->is_encap = true;
189e853ae77SStanislav Fomichev if (keys->flags & BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP)
190e853ae77SStanislav Fomichev return export_flow_keys(keys, BPF_OK);
191e853ae77SStanislav Fomichev
192bd4aed0eSJiong Wang return parse_eth_proto(skb, bpf_htons(ETH_P_IPV6));
193bd4aed0eSJiong Wang case IPPROTO_GRE:
194bd4aed0eSJiong Wang gre = bpf_flow_dissect_get_header(skb, sizeof(*gre), &_gre);
195bd4aed0eSJiong Wang if (!gre)
1960905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
197bd4aed0eSJiong Wang
198bd4aed0eSJiong Wang if (bpf_htons(gre->flags & GRE_VERSION))
199bd4aed0eSJiong Wang /* Only inspect standard GRE packets with version 0 */
2000905beecSStanislav Fomichev return export_flow_keys(keys, BPF_OK);
201bd4aed0eSJiong Wang
202bd4aed0eSJiong Wang keys->thoff += sizeof(*gre); /* Step over GRE Flags and Proto */
203bd4aed0eSJiong Wang if (GRE_IS_CSUM(gre->flags))
204bd4aed0eSJiong Wang keys->thoff += 4; /* Step over chksum and Padding */
205bd4aed0eSJiong Wang if (GRE_IS_KEY(gre->flags))
206bd4aed0eSJiong Wang keys->thoff += 4; /* Step over key */
207bd4aed0eSJiong Wang if (GRE_IS_SEQ(gre->flags))
208bd4aed0eSJiong Wang keys->thoff += 4; /* Step over sequence number */
209bd4aed0eSJiong Wang
210bd4aed0eSJiong Wang keys->is_encap = true;
211e853ae77SStanislav Fomichev if (keys->flags & BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP)
212e853ae77SStanislav Fomichev return export_flow_keys(keys, BPF_OK);
213bd4aed0eSJiong Wang
214bd4aed0eSJiong Wang if (gre->proto == bpf_htons(ETH_P_TEB)) {
215bd4aed0eSJiong Wang eth = bpf_flow_dissect_get_header(skb, sizeof(*eth),
216bd4aed0eSJiong Wang &_eth);
217bd4aed0eSJiong Wang if (!eth)
2180905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
219bd4aed0eSJiong Wang
220bd4aed0eSJiong Wang keys->thoff += sizeof(*eth);
221bd4aed0eSJiong Wang
222bd4aed0eSJiong Wang return parse_eth_proto(skb, eth->h_proto);
223bd4aed0eSJiong Wang } else {
224bd4aed0eSJiong Wang return parse_eth_proto(skb, gre->proto);
225bd4aed0eSJiong Wang }
226bd4aed0eSJiong Wang case IPPROTO_TCP:
227bd4aed0eSJiong Wang tcp = bpf_flow_dissect_get_header(skb, sizeof(*tcp), &_tcp);
228bd4aed0eSJiong Wang if (!tcp)
2290905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
230bd4aed0eSJiong Wang
231bd4aed0eSJiong Wang if (tcp->doff < 5)
2320905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
233bd4aed0eSJiong Wang
234bd4aed0eSJiong Wang if ((__u8 *)tcp + (tcp->doff << 2) > data_end)
2350905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
236bd4aed0eSJiong Wang
237bd4aed0eSJiong Wang keys->sport = tcp->source;
238bd4aed0eSJiong Wang keys->dport = tcp->dest;
2390905beecSStanislav Fomichev return export_flow_keys(keys, BPF_OK);
240bd4aed0eSJiong Wang case IPPROTO_UDP:
241bd4aed0eSJiong Wang case IPPROTO_UDPLITE:
242bd4aed0eSJiong Wang udp = bpf_flow_dissect_get_header(skb, sizeof(*udp), &_udp);
243bd4aed0eSJiong Wang if (!udp)
2440905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
245bd4aed0eSJiong Wang
246bd4aed0eSJiong Wang keys->sport = udp->source;
247bd4aed0eSJiong Wang keys->dport = udp->dest;
2480905beecSStanislav Fomichev return export_flow_keys(keys, BPF_OK);
249bd4aed0eSJiong Wang default:
2500905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
251bd4aed0eSJiong Wang }
252bd4aed0eSJiong Wang
2530905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
254bd4aed0eSJiong Wang }
255bd4aed0eSJiong Wang
parse_ipv6_proto(struct __sk_buff * skb,__u8 nexthdr)256bd4aed0eSJiong Wang static __always_inline int parse_ipv6_proto(struct __sk_buff *skb, __u8 nexthdr)
257bd4aed0eSJiong Wang {
258bd4aed0eSJiong Wang struct bpf_flow_keys *keys = skb->flow_keys;
259bd4aed0eSJiong Wang
260bd4aed0eSJiong Wang switch (nexthdr) {
261bd4aed0eSJiong Wang case IPPROTO_HOPOPTS:
262bd4aed0eSJiong Wang case IPPROTO_DSTOPTS:
263faef26faSDaniel Borkmann bpf_tail_call_static(skb, &jmp_table, IPV6OP);
264bd4aed0eSJiong Wang break;
265bd4aed0eSJiong Wang case IPPROTO_FRAGMENT:
266faef26faSDaniel Borkmann bpf_tail_call_static(skb, &jmp_table, IPV6FR);
267bd4aed0eSJiong Wang break;
268bd4aed0eSJiong Wang default:
269bd4aed0eSJiong Wang return parse_ip_proto(skb, nexthdr);
270bd4aed0eSJiong Wang }
271bd4aed0eSJiong Wang
2720905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
273bd4aed0eSJiong Wang }
274bd4aed0eSJiong Wang
PROG(IP)275bd4aed0eSJiong Wang PROG(IP)(struct __sk_buff *skb)
276bd4aed0eSJiong Wang {
277bd4aed0eSJiong Wang void *data_end = (void *)(long)skb->data_end;
278bd4aed0eSJiong Wang struct bpf_flow_keys *keys = skb->flow_keys;
279bd4aed0eSJiong Wang void *data = (void *)(long)skb->data;
280bd4aed0eSJiong Wang struct iphdr *iph, _iph;
281bd4aed0eSJiong Wang bool done = false;
282bd4aed0eSJiong Wang
283bd4aed0eSJiong Wang iph = bpf_flow_dissect_get_header(skb, sizeof(*iph), &_iph);
284bd4aed0eSJiong Wang if (!iph)
2850905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
286bd4aed0eSJiong Wang
287bd4aed0eSJiong Wang /* IP header cannot be smaller than 20 bytes */
288bd4aed0eSJiong Wang if (iph->ihl < 5)
2890905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
290bd4aed0eSJiong Wang
291bd4aed0eSJiong Wang keys->addr_proto = ETH_P_IP;
292bd4aed0eSJiong Wang keys->ipv4_src = iph->saddr;
293bd4aed0eSJiong Wang keys->ipv4_dst = iph->daddr;
294ae173a91SStanislav Fomichev keys->ip_proto = iph->protocol;
295bd4aed0eSJiong Wang
296bd4aed0eSJiong Wang keys->thoff += iph->ihl << 2;
297bd4aed0eSJiong Wang if (data + keys->thoff > data_end)
2980905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
299bd4aed0eSJiong Wang
300bd4aed0eSJiong Wang if (iph->frag_off & bpf_htons(IP_MF | IP_OFFSET)) {
301bd4aed0eSJiong Wang keys->is_frag = true;
302ae173a91SStanislav Fomichev if (iph->frag_off & bpf_htons(IP_OFFSET)) {
303bd4aed0eSJiong Wang /* From second fragment on, packets do not have headers
304bd4aed0eSJiong Wang * we can parse.
305bd4aed0eSJiong Wang */
306bd4aed0eSJiong Wang done = true;
307ae173a91SStanislav Fomichev } else {
308bd4aed0eSJiong Wang keys->is_first_frag = true;
309ae173a91SStanislav Fomichev /* No need to parse fragmented packet unless
310ae173a91SStanislav Fomichev * explicitly asked for.
311ae173a91SStanislav Fomichev */
312ae173a91SStanislav Fomichev if (!(keys->flags &
313ae173a91SStanislav Fomichev BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
314ae173a91SStanislav Fomichev done = true;
315ae173a91SStanislav Fomichev }
316bd4aed0eSJiong Wang }
317bd4aed0eSJiong Wang
318bd4aed0eSJiong Wang if (done)
3190905beecSStanislav Fomichev return export_flow_keys(keys, BPF_OK);
320bd4aed0eSJiong Wang
321bd4aed0eSJiong Wang return parse_ip_proto(skb, iph->protocol);
322bd4aed0eSJiong Wang }
323bd4aed0eSJiong Wang
PROG(IPV6)324bd4aed0eSJiong Wang PROG(IPV6)(struct __sk_buff *skb)
325bd4aed0eSJiong Wang {
326bd4aed0eSJiong Wang struct bpf_flow_keys *keys = skb->flow_keys;
327bd4aed0eSJiong Wang struct ipv6hdr *ip6h, _ip6h;
328bd4aed0eSJiong Wang
329bd4aed0eSJiong Wang ip6h = bpf_flow_dissect_get_header(skb, sizeof(*ip6h), &_ip6h);
330bd4aed0eSJiong Wang if (!ip6h)
3310905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
332bd4aed0eSJiong Wang
333bd4aed0eSJiong Wang keys->addr_proto = ETH_P_IPV6;
334bd4aed0eSJiong Wang memcpy(&keys->ipv6_src, &ip6h->saddr, 2*sizeof(ip6h->saddr));
335bd4aed0eSJiong Wang
336bd4aed0eSJiong Wang keys->thoff += sizeof(struct ipv6hdr);
337ae173a91SStanislav Fomichev keys->ip_proto = ip6h->nexthdr;
33871c99e32SStanislav Fomichev keys->flow_label = ip6_flowlabel(ip6h);
33971c99e32SStanislav Fomichev
340*9fa02892SStanislav Fomichev if (keys->flow_label && keys->flags & BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
34171c99e32SStanislav Fomichev return export_flow_keys(keys, BPF_OK);
342bd4aed0eSJiong Wang
343bd4aed0eSJiong Wang return parse_ipv6_proto(skb, ip6h->nexthdr);
344bd4aed0eSJiong Wang }
345bd4aed0eSJiong Wang
PROG(IPV6OP)346bd4aed0eSJiong Wang PROG(IPV6OP)(struct __sk_buff *skb)
347bd4aed0eSJiong Wang {
3480905beecSStanislav Fomichev struct bpf_flow_keys *keys = skb->flow_keys;
349bd4aed0eSJiong Wang struct ipv6_opt_hdr *ip6h, _ip6h;
350bd4aed0eSJiong Wang
351bd4aed0eSJiong Wang ip6h = bpf_flow_dissect_get_header(skb, sizeof(*ip6h), &_ip6h);
352bd4aed0eSJiong Wang if (!ip6h)
3530905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
354bd4aed0eSJiong Wang
355bd4aed0eSJiong Wang /* hlen is in 8-octets and does not include the first 8 bytes
356bd4aed0eSJiong Wang * of the header
357bd4aed0eSJiong Wang */
358ae173a91SStanislav Fomichev keys->thoff += (1 + ip6h->hdrlen) << 3;
359ae173a91SStanislav Fomichev keys->ip_proto = ip6h->nexthdr;
360bd4aed0eSJiong Wang
361bd4aed0eSJiong Wang return parse_ipv6_proto(skb, ip6h->nexthdr);
362bd4aed0eSJiong Wang }
363bd4aed0eSJiong Wang
PROG(IPV6FR)364bd4aed0eSJiong Wang PROG(IPV6FR)(struct __sk_buff *skb)
365bd4aed0eSJiong Wang {
366bd4aed0eSJiong Wang struct bpf_flow_keys *keys = skb->flow_keys;
367bd4aed0eSJiong Wang struct frag_hdr *fragh, _fragh;
368bd4aed0eSJiong Wang
369bd4aed0eSJiong Wang fragh = bpf_flow_dissect_get_header(skb, sizeof(*fragh), &_fragh);
370bd4aed0eSJiong Wang if (!fragh)
3710905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
372bd4aed0eSJiong Wang
373bd4aed0eSJiong Wang keys->thoff += sizeof(*fragh);
374bd4aed0eSJiong Wang keys->is_frag = true;
375ae173a91SStanislav Fomichev keys->ip_proto = fragh->nexthdr;
376ae173a91SStanislav Fomichev
377ae173a91SStanislav Fomichev if (!(fragh->frag_off & bpf_htons(IP6_OFFSET))) {
378bd4aed0eSJiong Wang keys->is_first_frag = true;
379bd4aed0eSJiong Wang
380ae173a91SStanislav Fomichev /* No need to parse fragmented packet unless
381ae173a91SStanislav Fomichev * explicitly asked for.
382ae173a91SStanislav Fomichev */
383ae173a91SStanislav Fomichev if (!(keys->flags & BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
384ae173a91SStanislav Fomichev return export_flow_keys(keys, BPF_OK);
385024cd2cbSSantucci Pierpaolo } else {
386024cd2cbSSantucci Pierpaolo return export_flow_keys(keys, BPF_OK);
387ae173a91SStanislav Fomichev }
388ae173a91SStanislav Fomichev
389bd4aed0eSJiong Wang return parse_ipv6_proto(skb, fragh->nexthdr);
390bd4aed0eSJiong Wang }
391bd4aed0eSJiong Wang
PROG(MPLS)392bd4aed0eSJiong Wang PROG(MPLS)(struct __sk_buff *skb)
393bd4aed0eSJiong Wang {
3940905beecSStanislav Fomichev struct bpf_flow_keys *keys = skb->flow_keys;
395bd4aed0eSJiong Wang struct mpls_label *mpls, _mpls;
396bd4aed0eSJiong Wang
397bd4aed0eSJiong Wang mpls = bpf_flow_dissect_get_header(skb, sizeof(*mpls), &_mpls);
398bd4aed0eSJiong Wang if (!mpls)
3990905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
400bd4aed0eSJiong Wang
4010905beecSStanislav Fomichev return export_flow_keys(keys, BPF_OK);
402bd4aed0eSJiong Wang }
403bd4aed0eSJiong Wang
PROG(VLAN)404bd4aed0eSJiong Wang PROG(VLAN)(struct __sk_buff *skb)
405bd4aed0eSJiong Wang {
406bd4aed0eSJiong Wang struct bpf_flow_keys *keys = skb->flow_keys;
407bd4aed0eSJiong Wang struct vlan_hdr *vlan, _vlan;
408bd4aed0eSJiong Wang
409bd4aed0eSJiong Wang /* Account for double-tagging */
4102c3af7d9SStanislav Fomichev if (keys->n_proto == bpf_htons(ETH_P_8021AD)) {
411bd4aed0eSJiong Wang vlan = bpf_flow_dissect_get_header(skb, sizeof(*vlan), &_vlan);
412bd4aed0eSJiong Wang if (!vlan)
4130905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
414bd4aed0eSJiong Wang
415bd4aed0eSJiong Wang if (vlan->h_vlan_encapsulated_proto != bpf_htons(ETH_P_8021Q))
4160905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
417bd4aed0eSJiong Wang
4182c3af7d9SStanislav Fomichev keys->nhoff += sizeof(*vlan);
419bd4aed0eSJiong Wang keys->thoff += sizeof(*vlan);
420bd4aed0eSJiong Wang }
421bd4aed0eSJiong Wang
422bd4aed0eSJiong Wang vlan = bpf_flow_dissect_get_header(skb, sizeof(*vlan), &_vlan);
423bd4aed0eSJiong Wang if (!vlan)
4240905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
425bd4aed0eSJiong Wang
4262c3af7d9SStanislav Fomichev keys->nhoff += sizeof(*vlan);
427bd4aed0eSJiong Wang keys->thoff += sizeof(*vlan);
428bd4aed0eSJiong Wang /* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/
429bd4aed0eSJiong Wang if (vlan->h_vlan_encapsulated_proto == bpf_htons(ETH_P_8021AD) ||
430bd4aed0eSJiong Wang vlan->h_vlan_encapsulated_proto == bpf_htons(ETH_P_8021Q))
4310905beecSStanislav Fomichev return export_flow_keys(keys, BPF_DROP);
432bd4aed0eSJiong Wang
433822fe617SStanislav Fomichev keys->n_proto = vlan->h_vlan_encapsulated_proto;
434bd4aed0eSJiong Wang return parse_eth_proto(skb, vlan->h_vlan_encapsulated_proto);
435bd4aed0eSJiong Wang }
436bd4aed0eSJiong Wang
437bd4aed0eSJiong Wang char __license[] SEC("license") = "GPL";
438