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