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