xref: /linux/net/core/flow_dissector.c (revision bd4af432cc71b5fbfe4833510359a6ad3ada250d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/skbuff.h>
4 #include <linux/export.h>
5 #include <linux/ip.h>
6 #include <linux/ipv6.h>
7 #include <linux/if_vlan.h>
8 #include <net/dsa.h>
9 #include <net/dst_metadata.h>
10 #include <net/ip.h>
11 #include <net/ipv6.h>
12 #include <net/gre.h>
13 #include <net/pptp.h>
14 #include <net/tipc.h>
15 #include <linux/igmp.h>
16 #include <linux/icmp.h>
17 #include <linux/sctp.h>
18 #include <linux/dccp.h>
19 #include <linux/if_tunnel.h>
20 #include <linux/if_pppox.h>
21 #include <linux/ppp_defs.h>
22 #include <linux/stddef.h>
23 #include <linux/if_ether.h>
24 #include <linux/mpls.h>
25 #include <linux/tcp.h>
26 #include <net/flow_dissector.h>
27 #include <scsi/fc/fc_fcoe.h>
28 #include <uapi/linux/batadv_packet.h>
29 #include <linux/bpf.h>
30 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
31 #include <net/netfilter/nf_conntrack_core.h>
32 #include <net/netfilter/nf_conntrack_labels.h>
33 #endif
34 
35 static DEFINE_MUTEX(flow_dissector_mutex);
36 
37 static void dissector_set_key(struct flow_dissector *flow_dissector,
38 			      enum flow_dissector_key_id key_id)
39 {
40 	flow_dissector->used_keys |= (1 << key_id);
41 }
42 
43 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
44 			     const struct flow_dissector_key *key,
45 			     unsigned int key_count)
46 {
47 	unsigned int i;
48 
49 	memset(flow_dissector, 0, sizeof(*flow_dissector));
50 
51 	for (i = 0; i < key_count; i++, key++) {
52 		/* User should make sure that every key target offset is withing
53 		 * boundaries of unsigned short.
54 		 */
55 		BUG_ON(key->offset > USHRT_MAX);
56 		BUG_ON(dissector_uses_key(flow_dissector,
57 					  key->key_id));
58 
59 		dissector_set_key(flow_dissector, key->key_id);
60 		flow_dissector->offset[key->key_id] = key->offset;
61 	}
62 
63 	/* Ensure that the dissector always includes control and basic key.
64 	 * That way we are able to avoid handling lack of these in fast path.
65 	 */
66 	BUG_ON(!dissector_uses_key(flow_dissector,
67 				   FLOW_DISSECTOR_KEY_CONTROL));
68 	BUG_ON(!dissector_uses_key(flow_dissector,
69 				   FLOW_DISSECTOR_KEY_BASIC));
70 }
71 EXPORT_SYMBOL(skb_flow_dissector_init);
72 
73 int skb_flow_dissector_prog_query(const union bpf_attr *attr,
74 				  union bpf_attr __user *uattr)
75 {
76 	__u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
77 	u32 prog_id, prog_cnt = 0, flags = 0;
78 	struct bpf_prog *attached;
79 	struct net *net;
80 
81 	if (attr->query.query_flags)
82 		return -EINVAL;
83 
84 	net = get_net_ns_by_fd(attr->query.target_fd);
85 	if (IS_ERR(net))
86 		return PTR_ERR(net);
87 
88 	rcu_read_lock();
89 	attached = rcu_dereference(net->flow_dissector_prog);
90 	if (attached) {
91 		prog_cnt = 1;
92 		prog_id = attached->aux->id;
93 	}
94 	rcu_read_unlock();
95 
96 	put_net(net);
97 
98 	if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
99 		return -EFAULT;
100 	if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
101 		return -EFAULT;
102 
103 	if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
104 		return 0;
105 
106 	if (copy_to_user(prog_ids, &prog_id, sizeof(u32)))
107 		return -EFAULT;
108 
109 	return 0;
110 }
111 
112 int skb_flow_dissector_bpf_prog_attach(const union bpf_attr *attr,
113 				       struct bpf_prog *prog)
114 {
115 	struct bpf_prog *attached;
116 	struct net *net;
117 	int ret = 0;
118 
119 	net = current->nsproxy->net_ns;
120 	mutex_lock(&flow_dissector_mutex);
121 
122 	if (net == &init_net) {
123 		/* BPF flow dissector in the root namespace overrides
124 		 * any per-net-namespace one. When attaching to root,
125 		 * make sure we don't have any BPF program attached
126 		 * to the non-root namespaces.
127 		 */
128 		struct net *ns;
129 
130 		for_each_net(ns) {
131 			if (ns == &init_net)
132 				continue;
133 			if (rcu_access_pointer(ns->flow_dissector_prog)) {
134 				ret = -EEXIST;
135 				goto out;
136 			}
137 		}
138 	} else {
139 		/* Make sure root flow dissector is not attached
140 		 * when attaching to the non-root namespace.
141 		 */
142 		if (rcu_access_pointer(init_net.flow_dissector_prog)) {
143 			ret = -EEXIST;
144 			goto out;
145 		}
146 	}
147 
148 	attached = rcu_dereference_protected(net->flow_dissector_prog,
149 					     lockdep_is_held(&flow_dissector_mutex));
150 	if (attached == prog) {
151 		/* The same program cannot be attached twice */
152 		ret = -EINVAL;
153 		goto out;
154 	}
155 	rcu_assign_pointer(net->flow_dissector_prog, prog);
156 	if (attached)
157 		bpf_prog_put(attached);
158 out:
159 	mutex_unlock(&flow_dissector_mutex);
160 	return ret;
161 }
162 
163 int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
164 {
165 	struct bpf_prog *attached;
166 	struct net *net;
167 
168 	net = current->nsproxy->net_ns;
169 	mutex_lock(&flow_dissector_mutex);
170 	attached = rcu_dereference_protected(net->flow_dissector_prog,
171 					     lockdep_is_held(&flow_dissector_mutex));
172 	if (!attached) {
173 		mutex_unlock(&flow_dissector_mutex);
174 		return -ENOENT;
175 	}
176 	RCU_INIT_POINTER(net->flow_dissector_prog, NULL);
177 	bpf_prog_put(attached);
178 	mutex_unlock(&flow_dissector_mutex);
179 	return 0;
180 }
181 
182 /**
183  * __skb_flow_get_ports - extract the upper layer ports and return them
184  * @skb: sk_buff to extract the ports from
185  * @thoff: transport header offset
186  * @ip_proto: protocol for which to get port offset
187  * @data: raw buffer pointer to the packet, if NULL use skb->data
188  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
189  *
190  * The function will try to retrieve the ports at offset thoff + poff where poff
191  * is the protocol port offset returned from proto_ports_offset
192  */
193 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
194 			    void *data, int hlen)
195 {
196 	int poff = proto_ports_offset(ip_proto);
197 
198 	if (!data) {
199 		data = skb->data;
200 		hlen = skb_headlen(skb);
201 	}
202 
203 	if (poff >= 0) {
204 		__be32 *ports, _ports;
205 
206 		ports = __skb_header_pointer(skb, thoff + poff,
207 					     sizeof(_ports), data, hlen, &_ports);
208 		if (ports)
209 			return *ports;
210 	}
211 
212 	return 0;
213 }
214 EXPORT_SYMBOL(__skb_flow_get_ports);
215 
216 static bool icmp_has_id(u8 type)
217 {
218 	switch (type) {
219 	case ICMP_ECHO:
220 	case ICMP_ECHOREPLY:
221 	case ICMP_TIMESTAMP:
222 	case ICMP_TIMESTAMPREPLY:
223 	case ICMPV6_ECHO_REQUEST:
224 	case ICMPV6_ECHO_REPLY:
225 		return true;
226 	}
227 
228 	return false;
229 }
230 
231 /**
232  * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
233  * @skb: sk_buff to extract from
234  * @key_icmp: struct flow_dissector_key_icmp to fill
235  * @data: raw buffer pointer to the packet
236  * @thoff: offset to extract at
237  * @hlen: packet header length
238  */
239 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
240 			   struct flow_dissector_key_icmp *key_icmp,
241 			   void *data, int thoff, int hlen)
242 {
243 	struct icmphdr *ih, _ih;
244 
245 	ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih);
246 	if (!ih)
247 		return;
248 
249 	key_icmp->type = ih->type;
250 	key_icmp->code = ih->code;
251 
252 	/* As we use 0 to signal that the Id field is not present,
253 	 * avoid confusion with packets without such field
254 	 */
255 	if (icmp_has_id(ih->type))
256 		key_icmp->id = ih->un.echo.id ? : 1;
257 	else
258 		key_icmp->id = 0;
259 }
260 EXPORT_SYMBOL(skb_flow_get_icmp_tci);
261 
262 /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
263  * using skb_flow_get_icmp_tci().
264  */
265 static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
266 				    struct flow_dissector *flow_dissector,
267 				    void *target_container,
268 				    void *data, int thoff, int hlen)
269 {
270 	struct flow_dissector_key_icmp *key_icmp;
271 
272 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP))
273 		return;
274 
275 	key_icmp = skb_flow_dissector_target(flow_dissector,
276 					     FLOW_DISSECTOR_KEY_ICMP,
277 					     target_container);
278 
279 	skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen);
280 }
281 
282 void skb_flow_dissect_meta(const struct sk_buff *skb,
283 			   struct flow_dissector *flow_dissector,
284 			   void *target_container)
285 {
286 	struct flow_dissector_key_meta *meta;
287 
288 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META))
289 		return;
290 
291 	meta = skb_flow_dissector_target(flow_dissector,
292 					 FLOW_DISSECTOR_KEY_META,
293 					 target_container);
294 	meta->ingress_ifindex = skb->skb_iif;
295 }
296 EXPORT_SYMBOL(skb_flow_dissect_meta);
297 
298 static void
299 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
300 				   struct flow_dissector *flow_dissector,
301 				   void *target_container)
302 {
303 	struct flow_dissector_key_control *ctrl;
304 
305 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
306 		return;
307 
308 	ctrl = skb_flow_dissector_target(flow_dissector,
309 					 FLOW_DISSECTOR_KEY_ENC_CONTROL,
310 					 target_container);
311 	ctrl->addr_type = type;
312 }
313 
314 void
315 skb_flow_dissect_ct(const struct sk_buff *skb,
316 		    struct flow_dissector *flow_dissector,
317 		    void *target_container,
318 		    u16 *ctinfo_map,
319 		    size_t mapsize)
320 {
321 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
322 	struct flow_dissector_key_ct *key;
323 	enum ip_conntrack_info ctinfo;
324 	struct nf_conn_labels *cl;
325 	struct nf_conn *ct;
326 
327 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT))
328 		return;
329 
330 	ct = nf_ct_get(skb, &ctinfo);
331 	if (!ct)
332 		return;
333 
334 	key = skb_flow_dissector_target(flow_dissector,
335 					FLOW_DISSECTOR_KEY_CT,
336 					target_container);
337 
338 	if (ctinfo < mapsize)
339 		key->ct_state = ctinfo_map[ctinfo];
340 #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)
341 	key->ct_zone = ct->zone.id;
342 #endif
343 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
344 	key->ct_mark = ct->mark;
345 #endif
346 
347 	cl = nf_ct_labels_find(ct);
348 	if (cl)
349 		memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels));
350 #endif /* CONFIG_NF_CONNTRACK */
351 }
352 EXPORT_SYMBOL(skb_flow_dissect_ct);
353 
354 void
355 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
356 			     struct flow_dissector *flow_dissector,
357 			     void *target_container)
358 {
359 	struct ip_tunnel_info *info;
360 	struct ip_tunnel_key *key;
361 
362 	/* A quick check to see if there might be something to do. */
363 	if (!dissector_uses_key(flow_dissector,
364 				FLOW_DISSECTOR_KEY_ENC_KEYID) &&
365 	    !dissector_uses_key(flow_dissector,
366 				FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
367 	    !dissector_uses_key(flow_dissector,
368 				FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
369 	    !dissector_uses_key(flow_dissector,
370 				FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
371 	    !dissector_uses_key(flow_dissector,
372 				FLOW_DISSECTOR_KEY_ENC_PORTS) &&
373 	    !dissector_uses_key(flow_dissector,
374 				FLOW_DISSECTOR_KEY_ENC_IP) &&
375 	    !dissector_uses_key(flow_dissector,
376 				FLOW_DISSECTOR_KEY_ENC_OPTS))
377 		return;
378 
379 	info = skb_tunnel_info(skb);
380 	if (!info)
381 		return;
382 
383 	key = &info->key;
384 
385 	switch (ip_tunnel_info_af(info)) {
386 	case AF_INET:
387 		skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
388 						   flow_dissector,
389 						   target_container);
390 		if (dissector_uses_key(flow_dissector,
391 				       FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
392 			struct flow_dissector_key_ipv4_addrs *ipv4;
393 
394 			ipv4 = skb_flow_dissector_target(flow_dissector,
395 							 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
396 							 target_container);
397 			ipv4->src = key->u.ipv4.src;
398 			ipv4->dst = key->u.ipv4.dst;
399 		}
400 		break;
401 	case AF_INET6:
402 		skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
403 						   flow_dissector,
404 						   target_container);
405 		if (dissector_uses_key(flow_dissector,
406 				       FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
407 			struct flow_dissector_key_ipv6_addrs *ipv6;
408 
409 			ipv6 = skb_flow_dissector_target(flow_dissector,
410 							 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
411 							 target_container);
412 			ipv6->src = key->u.ipv6.src;
413 			ipv6->dst = key->u.ipv6.dst;
414 		}
415 		break;
416 	}
417 
418 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
419 		struct flow_dissector_key_keyid *keyid;
420 
421 		keyid = skb_flow_dissector_target(flow_dissector,
422 						  FLOW_DISSECTOR_KEY_ENC_KEYID,
423 						  target_container);
424 		keyid->keyid = tunnel_id_to_key32(key->tun_id);
425 	}
426 
427 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
428 		struct flow_dissector_key_ports *tp;
429 
430 		tp = skb_flow_dissector_target(flow_dissector,
431 					       FLOW_DISSECTOR_KEY_ENC_PORTS,
432 					       target_container);
433 		tp->src = key->tp_src;
434 		tp->dst = key->tp_dst;
435 	}
436 
437 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
438 		struct flow_dissector_key_ip *ip;
439 
440 		ip = skb_flow_dissector_target(flow_dissector,
441 					       FLOW_DISSECTOR_KEY_ENC_IP,
442 					       target_container);
443 		ip->tos = key->tos;
444 		ip->ttl = key->ttl;
445 	}
446 
447 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
448 		struct flow_dissector_key_enc_opts *enc_opt;
449 
450 		enc_opt = skb_flow_dissector_target(flow_dissector,
451 						    FLOW_DISSECTOR_KEY_ENC_OPTS,
452 						    target_container);
453 
454 		if (info->options_len) {
455 			enc_opt->len = info->options_len;
456 			ip_tunnel_info_opts_get(enc_opt->data, info);
457 			enc_opt->dst_opt_type = info->key.tun_flags &
458 						TUNNEL_OPTIONS_PRESENT;
459 		}
460 	}
461 }
462 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
463 
464 static enum flow_dissect_ret
465 __skb_flow_dissect_mpls(const struct sk_buff *skb,
466 			struct flow_dissector *flow_dissector,
467 			void *target_container, void *data, int nhoff, int hlen)
468 {
469 	struct flow_dissector_key_keyid *key_keyid;
470 	struct mpls_label *hdr, _hdr[2];
471 	u32 entry, label;
472 
473 	if (!dissector_uses_key(flow_dissector,
474 				FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
475 	    !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
476 		return FLOW_DISSECT_RET_OUT_GOOD;
477 
478 	hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
479 				   hlen, &_hdr);
480 	if (!hdr)
481 		return FLOW_DISSECT_RET_OUT_BAD;
482 
483 	entry = ntohl(hdr[0].entry);
484 	label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
485 
486 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
487 		struct flow_dissector_key_mpls *key_mpls;
488 
489 		key_mpls = skb_flow_dissector_target(flow_dissector,
490 						     FLOW_DISSECTOR_KEY_MPLS,
491 						     target_container);
492 		key_mpls->mpls_label = label;
493 		key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
494 					>> MPLS_LS_TTL_SHIFT;
495 		key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
496 					>> MPLS_LS_TC_SHIFT;
497 		key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
498 					>> MPLS_LS_S_SHIFT;
499 	}
500 
501 	if (label == MPLS_LABEL_ENTROPY) {
502 		key_keyid = skb_flow_dissector_target(flow_dissector,
503 						      FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
504 						      target_container);
505 		key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
506 	}
507 	return FLOW_DISSECT_RET_OUT_GOOD;
508 }
509 
510 static enum flow_dissect_ret
511 __skb_flow_dissect_arp(const struct sk_buff *skb,
512 		       struct flow_dissector *flow_dissector,
513 		       void *target_container, void *data, int nhoff, int hlen)
514 {
515 	struct flow_dissector_key_arp *key_arp;
516 	struct {
517 		unsigned char ar_sha[ETH_ALEN];
518 		unsigned char ar_sip[4];
519 		unsigned char ar_tha[ETH_ALEN];
520 		unsigned char ar_tip[4];
521 	} *arp_eth, _arp_eth;
522 	const struct arphdr *arp;
523 	struct arphdr _arp;
524 
525 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
526 		return FLOW_DISSECT_RET_OUT_GOOD;
527 
528 	arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
529 				   hlen, &_arp);
530 	if (!arp)
531 		return FLOW_DISSECT_RET_OUT_BAD;
532 
533 	if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
534 	    arp->ar_pro != htons(ETH_P_IP) ||
535 	    arp->ar_hln != ETH_ALEN ||
536 	    arp->ar_pln != 4 ||
537 	    (arp->ar_op != htons(ARPOP_REPLY) &&
538 	     arp->ar_op != htons(ARPOP_REQUEST)))
539 		return FLOW_DISSECT_RET_OUT_BAD;
540 
541 	arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
542 				       sizeof(_arp_eth), data,
543 				       hlen, &_arp_eth);
544 	if (!arp_eth)
545 		return FLOW_DISSECT_RET_OUT_BAD;
546 
547 	key_arp = skb_flow_dissector_target(flow_dissector,
548 					    FLOW_DISSECTOR_KEY_ARP,
549 					    target_container);
550 
551 	memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
552 	memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
553 
554 	/* Only store the lower byte of the opcode;
555 	 * this covers ARPOP_REPLY and ARPOP_REQUEST.
556 	 */
557 	key_arp->op = ntohs(arp->ar_op) & 0xff;
558 
559 	ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
560 	ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
561 
562 	return FLOW_DISSECT_RET_OUT_GOOD;
563 }
564 
565 static enum flow_dissect_ret
566 __skb_flow_dissect_gre(const struct sk_buff *skb,
567 		       struct flow_dissector_key_control *key_control,
568 		       struct flow_dissector *flow_dissector,
569 		       void *target_container, void *data,
570 		       __be16 *p_proto, int *p_nhoff, int *p_hlen,
571 		       unsigned int flags)
572 {
573 	struct flow_dissector_key_keyid *key_keyid;
574 	struct gre_base_hdr *hdr, _hdr;
575 	int offset = 0;
576 	u16 gre_ver;
577 
578 	hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
579 				   data, *p_hlen, &_hdr);
580 	if (!hdr)
581 		return FLOW_DISSECT_RET_OUT_BAD;
582 
583 	/* Only look inside GRE without routing */
584 	if (hdr->flags & GRE_ROUTING)
585 		return FLOW_DISSECT_RET_OUT_GOOD;
586 
587 	/* Only look inside GRE for version 0 and 1 */
588 	gre_ver = ntohs(hdr->flags & GRE_VERSION);
589 	if (gre_ver > 1)
590 		return FLOW_DISSECT_RET_OUT_GOOD;
591 
592 	*p_proto = hdr->protocol;
593 	if (gre_ver) {
594 		/* Version1 must be PPTP, and check the flags */
595 		if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
596 			return FLOW_DISSECT_RET_OUT_GOOD;
597 	}
598 
599 	offset += sizeof(struct gre_base_hdr);
600 
601 	if (hdr->flags & GRE_CSUM)
602 		offset += sizeof_field(struct gre_full_hdr, csum) +
603 			  sizeof_field(struct gre_full_hdr, reserved1);
604 
605 	if (hdr->flags & GRE_KEY) {
606 		const __be32 *keyid;
607 		__be32 _keyid;
608 
609 		keyid = __skb_header_pointer(skb, *p_nhoff + offset,
610 					     sizeof(_keyid),
611 					     data, *p_hlen, &_keyid);
612 		if (!keyid)
613 			return FLOW_DISSECT_RET_OUT_BAD;
614 
615 		if (dissector_uses_key(flow_dissector,
616 				       FLOW_DISSECTOR_KEY_GRE_KEYID)) {
617 			key_keyid = skb_flow_dissector_target(flow_dissector,
618 							      FLOW_DISSECTOR_KEY_GRE_KEYID,
619 							      target_container);
620 			if (gre_ver == 0)
621 				key_keyid->keyid = *keyid;
622 			else
623 				key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
624 		}
625 		offset += sizeof_field(struct gre_full_hdr, key);
626 	}
627 
628 	if (hdr->flags & GRE_SEQ)
629 		offset += sizeof_field(struct pptp_gre_header, seq);
630 
631 	if (gre_ver == 0) {
632 		if (*p_proto == htons(ETH_P_TEB)) {
633 			const struct ethhdr *eth;
634 			struct ethhdr _eth;
635 
636 			eth = __skb_header_pointer(skb, *p_nhoff + offset,
637 						   sizeof(_eth),
638 						   data, *p_hlen, &_eth);
639 			if (!eth)
640 				return FLOW_DISSECT_RET_OUT_BAD;
641 			*p_proto = eth->h_proto;
642 			offset += sizeof(*eth);
643 
644 			/* Cap headers that we access via pointers at the
645 			 * end of the Ethernet header as our maximum alignment
646 			 * at that point is only 2 bytes.
647 			 */
648 			if (NET_IP_ALIGN)
649 				*p_hlen = *p_nhoff + offset;
650 		}
651 	} else { /* version 1, must be PPTP */
652 		u8 _ppp_hdr[PPP_HDRLEN];
653 		u8 *ppp_hdr;
654 
655 		if (hdr->flags & GRE_ACK)
656 			offset += sizeof_field(struct pptp_gre_header, ack);
657 
658 		ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
659 					       sizeof(_ppp_hdr),
660 					       data, *p_hlen, _ppp_hdr);
661 		if (!ppp_hdr)
662 			return FLOW_DISSECT_RET_OUT_BAD;
663 
664 		switch (PPP_PROTOCOL(ppp_hdr)) {
665 		case PPP_IP:
666 			*p_proto = htons(ETH_P_IP);
667 			break;
668 		case PPP_IPV6:
669 			*p_proto = htons(ETH_P_IPV6);
670 			break;
671 		default:
672 			/* Could probably catch some more like MPLS */
673 			break;
674 		}
675 
676 		offset += PPP_HDRLEN;
677 	}
678 
679 	*p_nhoff += offset;
680 	key_control->flags |= FLOW_DIS_ENCAPSULATION;
681 	if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
682 		return FLOW_DISSECT_RET_OUT_GOOD;
683 
684 	return FLOW_DISSECT_RET_PROTO_AGAIN;
685 }
686 
687 /**
688  * __skb_flow_dissect_batadv() - dissect batman-adv header
689  * @skb: sk_buff to with the batman-adv header
690  * @key_control: flow dissectors control key
691  * @data: raw buffer pointer to the packet, if NULL use skb->data
692  * @p_proto: pointer used to update the protocol to process next
693  * @p_nhoff: pointer used to update inner network header offset
694  * @hlen: packet header length
695  * @flags: any combination of FLOW_DISSECTOR_F_*
696  *
697  * ETH_P_BATMAN packets are tried to be dissected. Only
698  * &struct batadv_unicast packets are actually processed because they contain an
699  * inner ethernet header and are usually followed by actual network header. This
700  * allows the flow dissector to continue processing the packet.
701  *
702  * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
703  *  FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
704  *  otherwise FLOW_DISSECT_RET_OUT_BAD
705  */
706 static enum flow_dissect_ret
707 __skb_flow_dissect_batadv(const struct sk_buff *skb,
708 			  struct flow_dissector_key_control *key_control,
709 			  void *data, __be16 *p_proto, int *p_nhoff, int hlen,
710 			  unsigned int flags)
711 {
712 	struct {
713 		struct batadv_unicast_packet batadv_unicast;
714 		struct ethhdr eth;
715 	} *hdr, _hdr;
716 
717 	hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
718 				   &_hdr);
719 	if (!hdr)
720 		return FLOW_DISSECT_RET_OUT_BAD;
721 
722 	if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
723 		return FLOW_DISSECT_RET_OUT_BAD;
724 
725 	if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
726 		return FLOW_DISSECT_RET_OUT_BAD;
727 
728 	*p_proto = hdr->eth.h_proto;
729 	*p_nhoff += sizeof(*hdr);
730 
731 	key_control->flags |= FLOW_DIS_ENCAPSULATION;
732 	if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
733 		return FLOW_DISSECT_RET_OUT_GOOD;
734 
735 	return FLOW_DISSECT_RET_PROTO_AGAIN;
736 }
737 
738 static void
739 __skb_flow_dissect_tcp(const struct sk_buff *skb,
740 		       struct flow_dissector *flow_dissector,
741 		       void *target_container, void *data, int thoff, int hlen)
742 {
743 	struct flow_dissector_key_tcp *key_tcp;
744 	struct tcphdr *th, _th;
745 
746 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
747 		return;
748 
749 	th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
750 	if (!th)
751 		return;
752 
753 	if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
754 		return;
755 
756 	key_tcp = skb_flow_dissector_target(flow_dissector,
757 					    FLOW_DISSECTOR_KEY_TCP,
758 					    target_container);
759 	key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
760 }
761 
762 static void
763 __skb_flow_dissect_ports(const struct sk_buff *skb,
764 			 struct flow_dissector *flow_dissector,
765 			 void *target_container, void *data, int nhoff,
766 			 u8 ip_proto, int hlen)
767 {
768 	enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX;
769 	struct flow_dissector_key_ports *key_ports;
770 
771 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
772 		dissector_ports = FLOW_DISSECTOR_KEY_PORTS;
773 	else if (dissector_uses_key(flow_dissector,
774 				    FLOW_DISSECTOR_KEY_PORTS_RANGE))
775 		dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE;
776 
777 	if (dissector_ports == FLOW_DISSECTOR_KEY_MAX)
778 		return;
779 
780 	key_ports = skb_flow_dissector_target(flow_dissector,
781 					      dissector_ports,
782 					      target_container);
783 	key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
784 						data, hlen);
785 }
786 
787 static void
788 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
789 			struct flow_dissector *flow_dissector,
790 			void *target_container, void *data, const struct iphdr *iph)
791 {
792 	struct flow_dissector_key_ip *key_ip;
793 
794 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
795 		return;
796 
797 	key_ip = skb_flow_dissector_target(flow_dissector,
798 					   FLOW_DISSECTOR_KEY_IP,
799 					   target_container);
800 	key_ip->tos = iph->tos;
801 	key_ip->ttl = iph->ttl;
802 }
803 
804 static void
805 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
806 			struct flow_dissector *flow_dissector,
807 			void *target_container, void *data, const struct ipv6hdr *iph)
808 {
809 	struct flow_dissector_key_ip *key_ip;
810 
811 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
812 		return;
813 
814 	key_ip = skb_flow_dissector_target(flow_dissector,
815 					   FLOW_DISSECTOR_KEY_IP,
816 					   target_container);
817 	key_ip->tos = ipv6_get_dsfield(iph);
818 	key_ip->ttl = iph->hop_limit;
819 }
820 
821 /* Maximum number of protocol headers that can be parsed in
822  * __skb_flow_dissect
823  */
824 #define MAX_FLOW_DISSECT_HDRS	15
825 
826 static bool skb_flow_dissect_allowed(int *num_hdrs)
827 {
828 	++*num_hdrs;
829 
830 	return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
831 }
832 
833 static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys,
834 				     struct flow_dissector *flow_dissector,
835 				     void *target_container)
836 {
837 	struct flow_dissector_key_ports *key_ports = NULL;
838 	struct flow_dissector_key_control *key_control;
839 	struct flow_dissector_key_basic *key_basic;
840 	struct flow_dissector_key_addrs *key_addrs;
841 	struct flow_dissector_key_tags *key_tags;
842 
843 	key_control = skb_flow_dissector_target(flow_dissector,
844 						FLOW_DISSECTOR_KEY_CONTROL,
845 						target_container);
846 	key_control->thoff = flow_keys->thoff;
847 	if (flow_keys->is_frag)
848 		key_control->flags |= FLOW_DIS_IS_FRAGMENT;
849 	if (flow_keys->is_first_frag)
850 		key_control->flags |= FLOW_DIS_FIRST_FRAG;
851 	if (flow_keys->is_encap)
852 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
853 
854 	key_basic = skb_flow_dissector_target(flow_dissector,
855 					      FLOW_DISSECTOR_KEY_BASIC,
856 					      target_container);
857 	key_basic->n_proto = flow_keys->n_proto;
858 	key_basic->ip_proto = flow_keys->ip_proto;
859 
860 	if (flow_keys->addr_proto == ETH_P_IP &&
861 	    dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
862 		key_addrs = skb_flow_dissector_target(flow_dissector,
863 						      FLOW_DISSECTOR_KEY_IPV4_ADDRS,
864 						      target_container);
865 		key_addrs->v4addrs.src = flow_keys->ipv4_src;
866 		key_addrs->v4addrs.dst = flow_keys->ipv4_dst;
867 		key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
868 	} else if (flow_keys->addr_proto == ETH_P_IPV6 &&
869 		   dissector_uses_key(flow_dissector,
870 				      FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
871 		key_addrs = skb_flow_dissector_target(flow_dissector,
872 						      FLOW_DISSECTOR_KEY_IPV6_ADDRS,
873 						      target_container);
874 		memcpy(&key_addrs->v6addrs, &flow_keys->ipv6_src,
875 		       sizeof(key_addrs->v6addrs));
876 		key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
877 	}
878 
879 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
880 		key_ports = skb_flow_dissector_target(flow_dissector,
881 						      FLOW_DISSECTOR_KEY_PORTS,
882 						      target_container);
883 	else if (dissector_uses_key(flow_dissector,
884 				    FLOW_DISSECTOR_KEY_PORTS_RANGE))
885 		key_ports = skb_flow_dissector_target(flow_dissector,
886 						      FLOW_DISSECTOR_KEY_PORTS_RANGE,
887 						      target_container);
888 
889 	if (key_ports) {
890 		key_ports->src = flow_keys->sport;
891 		key_ports->dst = flow_keys->dport;
892 	}
893 
894 	if (dissector_uses_key(flow_dissector,
895 			       FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
896 		key_tags = skb_flow_dissector_target(flow_dissector,
897 						     FLOW_DISSECTOR_KEY_FLOW_LABEL,
898 						     target_container);
899 		key_tags->flow_label = ntohl(flow_keys->flow_label);
900 	}
901 }
902 
903 bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
904 		      __be16 proto, int nhoff, int hlen, unsigned int flags)
905 {
906 	struct bpf_flow_keys *flow_keys = ctx->flow_keys;
907 	u32 result;
908 
909 	/* Pass parameters to the BPF program */
910 	memset(flow_keys, 0, sizeof(*flow_keys));
911 	flow_keys->n_proto = proto;
912 	flow_keys->nhoff = nhoff;
913 	flow_keys->thoff = flow_keys->nhoff;
914 
915 	BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG !=
916 		     (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG);
917 	BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL !=
918 		     (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
919 	BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP !=
920 		     (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP);
921 	flow_keys->flags = flags;
922 
923 	result = bpf_prog_run_pin_on_cpu(prog, ctx);
924 
925 	flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
926 	flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
927 				   flow_keys->nhoff, hlen);
928 
929 	return result == BPF_OK;
930 }
931 
932 /**
933  * __skb_flow_dissect - extract the flow_keys struct and return it
934  * @net: associated network namespace, derived from @skb if NULL
935  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
936  * @flow_dissector: list of keys to dissect
937  * @target_container: target structure to put dissected values into
938  * @data: raw buffer pointer to the packet, if NULL use skb->data
939  * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
940  * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
941  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
942  * @flags: flags that control the dissection process, e.g.
943  *         FLOW_DISSECTOR_F_STOP_AT_ENCAP.
944  *
945  * The function will try to retrieve individual keys into target specified
946  * by flow_dissector from either the skbuff or a raw buffer specified by the
947  * rest parameters.
948  *
949  * Caller must take care of zeroing target container memory.
950  */
951 bool __skb_flow_dissect(const struct net *net,
952 			const struct sk_buff *skb,
953 			struct flow_dissector *flow_dissector,
954 			void *target_container,
955 			void *data, __be16 proto, int nhoff, int hlen,
956 			unsigned int flags)
957 {
958 	struct flow_dissector_key_control *key_control;
959 	struct flow_dissector_key_basic *key_basic;
960 	struct flow_dissector_key_addrs *key_addrs;
961 	struct flow_dissector_key_tags *key_tags;
962 	struct flow_dissector_key_vlan *key_vlan;
963 	struct bpf_prog *attached = NULL;
964 	enum flow_dissect_ret fdret;
965 	enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
966 	int num_hdrs = 0;
967 	u8 ip_proto = 0;
968 	bool ret;
969 
970 	if (!data) {
971 		data = skb->data;
972 		proto = skb_vlan_tag_present(skb) ?
973 			 skb->vlan_proto : skb->protocol;
974 		nhoff = skb_network_offset(skb);
975 		hlen = skb_headlen(skb);
976 #if IS_ENABLED(CONFIG_NET_DSA)
977 		if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
978 			     proto == htons(ETH_P_XDSA))) {
979 			const struct dsa_device_ops *ops;
980 			int offset = 0;
981 
982 			ops = skb->dev->dsa_ptr->tag_ops;
983 			if (ops->flow_dissect &&
984 			    !ops->flow_dissect(skb, &proto, &offset)) {
985 				hlen -= offset;
986 				nhoff += offset;
987 			}
988 		}
989 #endif
990 	}
991 
992 	/* It is ensured by skb_flow_dissector_init() that control key will
993 	 * be always present.
994 	 */
995 	key_control = skb_flow_dissector_target(flow_dissector,
996 						FLOW_DISSECTOR_KEY_CONTROL,
997 						target_container);
998 
999 	/* It is ensured by skb_flow_dissector_init() that basic key will
1000 	 * be always present.
1001 	 */
1002 	key_basic = skb_flow_dissector_target(flow_dissector,
1003 					      FLOW_DISSECTOR_KEY_BASIC,
1004 					      target_container);
1005 
1006 	if (skb) {
1007 		if (!net) {
1008 			if (skb->dev)
1009 				net = dev_net(skb->dev);
1010 			else if (skb->sk)
1011 				net = sock_net(skb->sk);
1012 		}
1013 	}
1014 
1015 	WARN_ON_ONCE(!net);
1016 	if (net) {
1017 		rcu_read_lock();
1018 		attached = rcu_dereference(init_net.flow_dissector_prog);
1019 
1020 		if (!attached)
1021 			attached = rcu_dereference(net->flow_dissector_prog);
1022 
1023 		if (attached) {
1024 			struct bpf_flow_keys flow_keys;
1025 			struct bpf_flow_dissector ctx = {
1026 				.flow_keys = &flow_keys,
1027 				.data = data,
1028 				.data_end = data + hlen,
1029 			};
1030 			__be16 n_proto = proto;
1031 
1032 			if (skb) {
1033 				ctx.skb = skb;
1034 				/* we can't use 'proto' in the skb case
1035 				 * because it might be set to skb->vlan_proto
1036 				 * which has been pulled from the data
1037 				 */
1038 				n_proto = skb->protocol;
1039 			}
1040 
1041 			ret = bpf_flow_dissect(attached, &ctx, n_proto, nhoff,
1042 					       hlen, flags);
1043 			__skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1044 						 target_container);
1045 			rcu_read_unlock();
1046 			return ret;
1047 		}
1048 		rcu_read_unlock();
1049 	}
1050 
1051 	if (dissector_uses_key(flow_dissector,
1052 			       FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1053 		struct ethhdr *eth = eth_hdr(skb);
1054 		struct flow_dissector_key_eth_addrs *key_eth_addrs;
1055 
1056 		key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1057 							  FLOW_DISSECTOR_KEY_ETH_ADDRS,
1058 							  target_container);
1059 		memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
1060 	}
1061 
1062 proto_again:
1063 	fdret = FLOW_DISSECT_RET_CONTINUE;
1064 
1065 	switch (proto) {
1066 	case htons(ETH_P_IP): {
1067 		const struct iphdr *iph;
1068 		struct iphdr _iph;
1069 
1070 		iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1071 		if (!iph || iph->ihl < 5) {
1072 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1073 			break;
1074 		}
1075 
1076 		nhoff += iph->ihl * 4;
1077 
1078 		ip_proto = iph->protocol;
1079 
1080 		if (dissector_uses_key(flow_dissector,
1081 				       FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1082 			key_addrs = skb_flow_dissector_target(flow_dissector,
1083 							      FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1084 							      target_container);
1085 
1086 			memcpy(&key_addrs->v4addrs, &iph->saddr,
1087 			       sizeof(key_addrs->v4addrs));
1088 			key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1089 		}
1090 
1091 		if (ip_is_fragment(iph)) {
1092 			key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1093 
1094 			if (iph->frag_off & htons(IP_OFFSET)) {
1095 				fdret = FLOW_DISSECT_RET_OUT_GOOD;
1096 				break;
1097 			} else {
1098 				key_control->flags |= FLOW_DIS_FIRST_FRAG;
1099 				if (!(flags &
1100 				      FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1101 					fdret = FLOW_DISSECT_RET_OUT_GOOD;
1102 					break;
1103 				}
1104 			}
1105 		}
1106 
1107 		__skb_flow_dissect_ipv4(skb, flow_dissector,
1108 					target_container, data, iph);
1109 
1110 		break;
1111 	}
1112 	case htons(ETH_P_IPV6): {
1113 		const struct ipv6hdr *iph;
1114 		struct ipv6hdr _iph;
1115 
1116 		iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1117 		if (!iph) {
1118 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1119 			break;
1120 		}
1121 
1122 		ip_proto = iph->nexthdr;
1123 		nhoff += sizeof(struct ipv6hdr);
1124 
1125 		if (dissector_uses_key(flow_dissector,
1126 				       FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1127 			key_addrs = skb_flow_dissector_target(flow_dissector,
1128 							      FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1129 							      target_container);
1130 
1131 			memcpy(&key_addrs->v6addrs, &iph->saddr,
1132 			       sizeof(key_addrs->v6addrs));
1133 			key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1134 		}
1135 
1136 		if ((dissector_uses_key(flow_dissector,
1137 					FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1138 		     (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1139 		    ip6_flowlabel(iph)) {
1140 			__be32 flow_label = ip6_flowlabel(iph);
1141 
1142 			if (dissector_uses_key(flow_dissector,
1143 					       FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1144 				key_tags = skb_flow_dissector_target(flow_dissector,
1145 								     FLOW_DISSECTOR_KEY_FLOW_LABEL,
1146 								     target_container);
1147 				key_tags->flow_label = ntohl(flow_label);
1148 			}
1149 			if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1150 				fdret = FLOW_DISSECT_RET_OUT_GOOD;
1151 				break;
1152 			}
1153 		}
1154 
1155 		__skb_flow_dissect_ipv6(skb, flow_dissector,
1156 					target_container, data, iph);
1157 
1158 		break;
1159 	}
1160 	case htons(ETH_P_8021AD):
1161 	case htons(ETH_P_8021Q): {
1162 		const struct vlan_hdr *vlan = NULL;
1163 		struct vlan_hdr _vlan;
1164 		__be16 saved_vlan_tpid = proto;
1165 
1166 		if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1167 		    skb && skb_vlan_tag_present(skb)) {
1168 			proto = skb->protocol;
1169 		} else {
1170 			vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1171 						    data, hlen, &_vlan);
1172 			if (!vlan) {
1173 				fdret = FLOW_DISSECT_RET_OUT_BAD;
1174 				break;
1175 			}
1176 
1177 			proto = vlan->h_vlan_encapsulated_proto;
1178 			nhoff += sizeof(*vlan);
1179 		}
1180 
1181 		if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1182 			dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1183 		} else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1184 			dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1185 		} else {
1186 			fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1187 			break;
1188 		}
1189 
1190 		if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1191 			key_vlan = skb_flow_dissector_target(flow_dissector,
1192 							     dissector_vlan,
1193 							     target_container);
1194 
1195 			if (!vlan) {
1196 				key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1197 				key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1198 			} else {
1199 				key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1200 					VLAN_VID_MASK;
1201 				key_vlan->vlan_priority =
1202 					(ntohs(vlan->h_vlan_TCI) &
1203 					 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1204 			}
1205 			key_vlan->vlan_tpid = saved_vlan_tpid;
1206 		}
1207 
1208 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1209 		break;
1210 	}
1211 	case htons(ETH_P_PPP_SES): {
1212 		struct {
1213 			struct pppoe_hdr hdr;
1214 			__be16 proto;
1215 		} *hdr, _hdr;
1216 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1217 		if (!hdr) {
1218 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1219 			break;
1220 		}
1221 
1222 		proto = hdr->proto;
1223 		nhoff += PPPOE_SES_HLEN;
1224 		switch (proto) {
1225 		case htons(PPP_IP):
1226 			proto = htons(ETH_P_IP);
1227 			fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1228 			break;
1229 		case htons(PPP_IPV6):
1230 			proto = htons(ETH_P_IPV6);
1231 			fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1232 			break;
1233 		default:
1234 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1235 			break;
1236 		}
1237 		break;
1238 	}
1239 	case htons(ETH_P_TIPC): {
1240 		struct tipc_basic_hdr *hdr, _hdr;
1241 
1242 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1243 					   data, hlen, &_hdr);
1244 		if (!hdr) {
1245 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1246 			break;
1247 		}
1248 
1249 		if (dissector_uses_key(flow_dissector,
1250 				       FLOW_DISSECTOR_KEY_TIPC)) {
1251 			key_addrs = skb_flow_dissector_target(flow_dissector,
1252 							      FLOW_DISSECTOR_KEY_TIPC,
1253 							      target_container);
1254 			key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1255 			key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1256 		}
1257 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
1258 		break;
1259 	}
1260 
1261 	case htons(ETH_P_MPLS_UC):
1262 	case htons(ETH_P_MPLS_MC):
1263 		fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1264 						target_container, data,
1265 						nhoff, hlen);
1266 		break;
1267 	case htons(ETH_P_FCOE):
1268 		if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1269 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1270 			break;
1271 		}
1272 
1273 		nhoff += FCOE_HEADER_LEN;
1274 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
1275 		break;
1276 
1277 	case htons(ETH_P_ARP):
1278 	case htons(ETH_P_RARP):
1279 		fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1280 					       target_container, data,
1281 					       nhoff, hlen);
1282 		break;
1283 
1284 	case htons(ETH_P_BATMAN):
1285 		fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1286 						  &proto, &nhoff, hlen, flags);
1287 		break;
1288 
1289 	default:
1290 		fdret = FLOW_DISSECT_RET_OUT_BAD;
1291 		break;
1292 	}
1293 
1294 	/* Process result of proto processing */
1295 	switch (fdret) {
1296 	case FLOW_DISSECT_RET_OUT_GOOD:
1297 		goto out_good;
1298 	case FLOW_DISSECT_RET_PROTO_AGAIN:
1299 		if (skb_flow_dissect_allowed(&num_hdrs))
1300 			goto proto_again;
1301 		goto out_good;
1302 	case FLOW_DISSECT_RET_CONTINUE:
1303 	case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1304 		break;
1305 	case FLOW_DISSECT_RET_OUT_BAD:
1306 	default:
1307 		goto out_bad;
1308 	}
1309 
1310 ip_proto_again:
1311 	fdret = FLOW_DISSECT_RET_CONTINUE;
1312 
1313 	switch (ip_proto) {
1314 	case IPPROTO_GRE:
1315 		fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1316 					       target_container, data,
1317 					       &proto, &nhoff, &hlen, flags);
1318 		break;
1319 
1320 	case NEXTHDR_HOP:
1321 	case NEXTHDR_ROUTING:
1322 	case NEXTHDR_DEST: {
1323 		u8 _opthdr[2], *opthdr;
1324 
1325 		if (proto != htons(ETH_P_IPV6))
1326 			break;
1327 
1328 		opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1329 					      data, hlen, &_opthdr);
1330 		if (!opthdr) {
1331 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1332 			break;
1333 		}
1334 
1335 		ip_proto = opthdr[0];
1336 		nhoff += (opthdr[1] + 1) << 3;
1337 
1338 		fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1339 		break;
1340 	}
1341 	case NEXTHDR_FRAGMENT: {
1342 		struct frag_hdr _fh, *fh;
1343 
1344 		if (proto != htons(ETH_P_IPV6))
1345 			break;
1346 
1347 		fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1348 					  data, hlen, &_fh);
1349 
1350 		if (!fh) {
1351 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1352 			break;
1353 		}
1354 
1355 		key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1356 
1357 		nhoff += sizeof(_fh);
1358 		ip_proto = fh->nexthdr;
1359 
1360 		if (!(fh->frag_off & htons(IP6_OFFSET))) {
1361 			key_control->flags |= FLOW_DIS_FIRST_FRAG;
1362 			if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1363 				fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1364 				break;
1365 			}
1366 		}
1367 
1368 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
1369 		break;
1370 	}
1371 	case IPPROTO_IPIP:
1372 		proto = htons(ETH_P_IP);
1373 
1374 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
1375 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1376 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
1377 			break;
1378 		}
1379 
1380 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1381 		break;
1382 
1383 	case IPPROTO_IPV6:
1384 		proto = htons(ETH_P_IPV6);
1385 
1386 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
1387 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1388 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
1389 			break;
1390 		}
1391 
1392 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1393 		break;
1394 
1395 
1396 	case IPPROTO_MPLS:
1397 		proto = htons(ETH_P_MPLS_UC);
1398 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1399 		break;
1400 
1401 	case IPPROTO_TCP:
1402 		__skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1403 				       data, nhoff, hlen);
1404 		break;
1405 
1406 	case IPPROTO_ICMP:
1407 	case IPPROTO_ICMPV6:
1408 		__skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1409 					data, nhoff, hlen);
1410 		break;
1411 
1412 	default:
1413 		break;
1414 	}
1415 
1416 	if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1417 		__skb_flow_dissect_ports(skb, flow_dissector, target_container,
1418 					 data, nhoff, ip_proto, hlen);
1419 
1420 	/* Process result of IP proto processing */
1421 	switch (fdret) {
1422 	case FLOW_DISSECT_RET_PROTO_AGAIN:
1423 		if (skb_flow_dissect_allowed(&num_hdrs))
1424 			goto proto_again;
1425 		break;
1426 	case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1427 		if (skb_flow_dissect_allowed(&num_hdrs))
1428 			goto ip_proto_again;
1429 		break;
1430 	case FLOW_DISSECT_RET_OUT_GOOD:
1431 	case FLOW_DISSECT_RET_CONTINUE:
1432 		break;
1433 	case FLOW_DISSECT_RET_OUT_BAD:
1434 	default:
1435 		goto out_bad;
1436 	}
1437 
1438 out_good:
1439 	ret = true;
1440 
1441 out:
1442 	key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1443 	key_basic->n_proto = proto;
1444 	key_basic->ip_proto = ip_proto;
1445 
1446 	return ret;
1447 
1448 out_bad:
1449 	ret = false;
1450 	goto out;
1451 }
1452 EXPORT_SYMBOL(__skb_flow_dissect);
1453 
1454 static siphash_key_t hashrnd __read_mostly;
1455 static __always_inline void __flow_hash_secret_init(void)
1456 {
1457 	net_get_random_once(&hashrnd, sizeof(hashrnd));
1458 }
1459 
1460 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1461 {
1462 	BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1463 	return &flow->FLOW_KEYS_HASH_START_FIELD;
1464 }
1465 
1466 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1467 {
1468 	size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1469 
1470 	BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1471 
1472 	switch (flow->control.addr_type) {
1473 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1474 		diff -= sizeof(flow->addrs.v4addrs);
1475 		break;
1476 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1477 		diff -= sizeof(flow->addrs.v6addrs);
1478 		break;
1479 	case FLOW_DISSECTOR_KEY_TIPC:
1480 		diff -= sizeof(flow->addrs.tipckey);
1481 		break;
1482 	}
1483 	return sizeof(*flow) - diff;
1484 }
1485 
1486 __be32 flow_get_u32_src(const struct flow_keys *flow)
1487 {
1488 	switch (flow->control.addr_type) {
1489 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1490 		return flow->addrs.v4addrs.src;
1491 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1492 		return (__force __be32)ipv6_addr_hash(
1493 			&flow->addrs.v6addrs.src);
1494 	case FLOW_DISSECTOR_KEY_TIPC:
1495 		return flow->addrs.tipckey.key;
1496 	default:
1497 		return 0;
1498 	}
1499 }
1500 EXPORT_SYMBOL(flow_get_u32_src);
1501 
1502 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1503 {
1504 	switch (flow->control.addr_type) {
1505 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1506 		return flow->addrs.v4addrs.dst;
1507 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1508 		return (__force __be32)ipv6_addr_hash(
1509 			&flow->addrs.v6addrs.dst);
1510 	default:
1511 		return 0;
1512 	}
1513 }
1514 EXPORT_SYMBOL(flow_get_u32_dst);
1515 
1516 /* Sort the source and destination IP (and the ports if the IP are the same),
1517  * to have consistent hash within the two directions
1518  */
1519 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1520 {
1521 	int addr_diff, i;
1522 
1523 	switch (keys->control.addr_type) {
1524 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1525 		addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1526 			    (__force u32)keys->addrs.v4addrs.src;
1527 		if ((addr_diff < 0) ||
1528 		    (addr_diff == 0 &&
1529 		     ((__force u16)keys->ports.dst <
1530 		      (__force u16)keys->ports.src))) {
1531 			swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1532 			swap(keys->ports.src, keys->ports.dst);
1533 		}
1534 		break;
1535 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1536 		addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1537 				   &keys->addrs.v6addrs.src,
1538 				   sizeof(keys->addrs.v6addrs.dst));
1539 		if ((addr_diff < 0) ||
1540 		    (addr_diff == 0 &&
1541 		     ((__force u16)keys->ports.dst <
1542 		      (__force u16)keys->ports.src))) {
1543 			for (i = 0; i < 4; i++)
1544 				swap(keys->addrs.v6addrs.src.s6_addr32[i],
1545 				     keys->addrs.v6addrs.dst.s6_addr32[i]);
1546 			swap(keys->ports.src, keys->ports.dst);
1547 		}
1548 		break;
1549 	}
1550 }
1551 
1552 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1553 					const siphash_key_t *keyval)
1554 {
1555 	u32 hash;
1556 
1557 	__flow_hash_consistentify(keys);
1558 
1559 	hash = siphash(flow_keys_hash_start(keys),
1560 		       flow_keys_hash_length(keys), keyval);
1561 	if (!hash)
1562 		hash = 1;
1563 
1564 	return hash;
1565 }
1566 
1567 u32 flow_hash_from_keys(struct flow_keys *keys)
1568 {
1569 	__flow_hash_secret_init();
1570 	return __flow_hash_from_keys(keys, &hashrnd);
1571 }
1572 EXPORT_SYMBOL(flow_hash_from_keys);
1573 
1574 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1575 				  struct flow_keys *keys,
1576 				  const siphash_key_t *keyval)
1577 {
1578 	skb_flow_dissect_flow_keys(skb, keys,
1579 				   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1580 
1581 	return __flow_hash_from_keys(keys, keyval);
1582 }
1583 
1584 struct _flow_keys_digest_data {
1585 	__be16	n_proto;
1586 	u8	ip_proto;
1587 	u8	padding;
1588 	__be32	ports;
1589 	__be32	src;
1590 	__be32	dst;
1591 };
1592 
1593 void make_flow_keys_digest(struct flow_keys_digest *digest,
1594 			   const struct flow_keys *flow)
1595 {
1596 	struct _flow_keys_digest_data *data =
1597 	    (struct _flow_keys_digest_data *)digest;
1598 
1599 	BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1600 
1601 	memset(digest, 0, sizeof(*digest));
1602 
1603 	data->n_proto = flow->basic.n_proto;
1604 	data->ip_proto = flow->basic.ip_proto;
1605 	data->ports = flow->ports.ports;
1606 	data->src = flow->addrs.v4addrs.src;
1607 	data->dst = flow->addrs.v4addrs.dst;
1608 }
1609 EXPORT_SYMBOL(make_flow_keys_digest);
1610 
1611 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1612 
1613 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1614 {
1615 	struct flow_keys keys;
1616 
1617 	__flow_hash_secret_init();
1618 
1619 	memset(&keys, 0, sizeof(keys));
1620 	__skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1621 			   &keys, NULL, 0, 0, 0,
1622 			   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1623 
1624 	return __flow_hash_from_keys(&keys, &hashrnd);
1625 }
1626 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1627 
1628 /**
1629  * __skb_get_hash: calculate a flow hash
1630  * @skb: sk_buff to calculate flow hash from
1631  *
1632  * This function calculates a flow hash based on src/dst addresses
1633  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
1634  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
1635  * if hash is a canonical 4-tuple hash over transport ports.
1636  */
1637 void __skb_get_hash(struct sk_buff *skb)
1638 {
1639 	struct flow_keys keys;
1640 	u32 hash;
1641 
1642 	__flow_hash_secret_init();
1643 
1644 	hash = ___skb_get_hash(skb, &keys, &hashrnd);
1645 
1646 	__skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1647 }
1648 EXPORT_SYMBOL(__skb_get_hash);
1649 
1650 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1651 			   const siphash_key_t *perturb)
1652 {
1653 	struct flow_keys keys;
1654 
1655 	return ___skb_get_hash(skb, &keys, perturb);
1656 }
1657 EXPORT_SYMBOL(skb_get_hash_perturb);
1658 
1659 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1660 		   const struct flow_keys_basic *keys, int hlen)
1661 {
1662 	u32 poff = keys->control.thoff;
1663 
1664 	/* skip L4 headers for fragments after the first */
1665 	if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1666 	    !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1667 		return poff;
1668 
1669 	switch (keys->basic.ip_proto) {
1670 	case IPPROTO_TCP: {
1671 		/* access doff as u8 to avoid unaligned access */
1672 		const u8 *doff;
1673 		u8 _doff;
1674 
1675 		doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1676 					    data, hlen, &_doff);
1677 		if (!doff)
1678 			return poff;
1679 
1680 		poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1681 		break;
1682 	}
1683 	case IPPROTO_UDP:
1684 	case IPPROTO_UDPLITE:
1685 		poff += sizeof(struct udphdr);
1686 		break;
1687 	/* For the rest, we do not really care about header
1688 	 * extensions at this point for now.
1689 	 */
1690 	case IPPROTO_ICMP:
1691 		poff += sizeof(struct icmphdr);
1692 		break;
1693 	case IPPROTO_ICMPV6:
1694 		poff += sizeof(struct icmp6hdr);
1695 		break;
1696 	case IPPROTO_IGMP:
1697 		poff += sizeof(struct igmphdr);
1698 		break;
1699 	case IPPROTO_DCCP:
1700 		poff += sizeof(struct dccp_hdr);
1701 		break;
1702 	case IPPROTO_SCTP:
1703 		poff += sizeof(struct sctphdr);
1704 		break;
1705 	}
1706 
1707 	return poff;
1708 }
1709 
1710 /**
1711  * skb_get_poff - get the offset to the payload
1712  * @skb: sk_buff to get the payload offset from
1713  *
1714  * The function will get the offset to the payload as far as it could
1715  * be dissected.  The main user is currently BPF, so that we can dynamically
1716  * truncate packets without needing to push actual payload to the user
1717  * space and can analyze headers only, instead.
1718  */
1719 u32 skb_get_poff(const struct sk_buff *skb)
1720 {
1721 	struct flow_keys_basic keys;
1722 
1723 	if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1724 					      NULL, 0, 0, 0, 0))
1725 		return 0;
1726 
1727 	return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1728 }
1729 
1730 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1731 {
1732 	memset(keys, 0, sizeof(*keys));
1733 
1734 	memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1735 	    sizeof(keys->addrs.v6addrs.src));
1736 	memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1737 	    sizeof(keys->addrs.v6addrs.dst));
1738 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1739 	keys->ports.src = fl6->fl6_sport;
1740 	keys->ports.dst = fl6->fl6_dport;
1741 	keys->keyid.keyid = fl6->fl6_gre_key;
1742 	keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1743 	keys->basic.ip_proto = fl6->flowi6_proto;
1744 
1745 	return flow_hash_from_keys(keys);
1746 }
1747 EXPORT_SYMBOL(__get_hash_from_flowi6);
1748 
1749 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1750 	{
1751 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1752 		.offset = offsetof(struct flow_keys, control),
1753 	},
1754 	{
1755 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1756 		.offset = offsetof(struct flow_keys, basic),
1757 	},
1758 	{
1759 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1760 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
1761 	},
1762 	{
1763 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1764 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
1765 	},
1766 	{
1767 		.key_id = FLOW_DISSECTOR_KEY_TIPC,
1768 		.offset = offsetof(struct flow_keys, addrs.tipckey),
1769 	},
1770 	{
1771 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
1772 		.offset = offsetof(struct flow_keys, ports),
1773 	},
1774 	{
1775 		.key_id = FLOW_DISSECTOR_KEY_VLAN,
1776 		.offset = offsetof(struct flow_keys, vlan),
1777 	},
1778 	{
1779 		.key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1780 		.offset = offsetof(struct flow_keys, tags),
1781 	},
1782 	{
1783 		.key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1784 		.offset = offsetof(struct flow_keys, keyid),
1785 	},
1786 };
1787 
1788 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1789 	{
1790 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1791 		.offset = offsetof(struct flow_keys, control),
1792 	},
1793 	{
1794 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1795 		.offset = offsetof(struct flow_keys, basic),
1796 	},
1797 	{
1798 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1799 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
1800 	},
1801 	{
1802 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1803 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
1804 	},
1805 	{
1806 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
1807 		.offset = offsetof(struct flow_keys, ports),
1808 	},
1809 };
1810 
1811 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1812 	{
1813 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1814 		.offset = offsetof(struct flow_keys, control),
1815 	},
1816 	{
1817 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1818 		.offset = offsetof(struct flow_keys, basic),
1819 	},
1820 };
1821 
1822 struct flow_dissector flow_keys_dissector __read_mostly;
1823 EXPORT_SYMBOL(flow_keys_dissector);
1824 
1825 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1826 EXPORT_SYMBOL(flow_keys_basic_dissector);
1827 
1828 static int __init init_default_flow_dissectors(void)
1829 {
1830 	skb_flow_dissector_init(&flow_keys_dissector,
1831 				flow_keys_dissector_keys,
1832 				ARRAY_SIZE(flow_keys_dissector_keys));
1833 	skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1834 				flow_keys_dissector_symmetric_keys,
1835 				ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1836 	skb_flow_dissector_init(&flow_keys_basic_dissector,
1837 				flow_keys_basic_dissector_keys,
1838 				ARRAY_SIZE(flow_keys_basic_dissector_keys));
1839 	return 0;
1840 }
1841 
1842 core_initcall(init_default_flow_dissectors);
1843