xref: /openbmc/linux/net/core/flow_dissector.c (revision 106e0121)
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, 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 			key_vlan->vlan_eth_type = proto;
1187 		}
1188 
1189 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1190 		break;
1191 	}
1192 	case htons(ETH_P_PPP_SES): {
1193 		struct {
1194 			struct pppoe_hdr hdr;
1195 			__be16 proto;
1196 		} *hdr, _hdr;
1197 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1198 		if (!hdr) {
1199 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1200 			break;
1201 		}
1202 
1203 		nhoff += PPPOE_SES_HLEN;
1204 		switch (hdr->proto) {
1205 		case htons(PPP_IP):
1206 			proto = htons(ETH_P_IP);
1207 			fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1208 			break;
1209 		case htons(PPP_IPV6):
1210 			proto = htons(ETH_P_IPV6);
1211 			fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1212 			break;
1213 		default:
1214 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1215 			break;
1216 		}
1217 		break;
1218 	}
1219 	case htons(ETH_P_TIPC): {
1220 		struct tipc_basic_hdr *hdr, _hdr;
1221 
1222 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1223 					   data, hlen, &_hdr);
1224 		if (!hdr) {
1225 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1226 			break;
1227 		}
1228 
1229 		if (dissector_uses_key(flow_dissector,
1230 				       FLOW_DISSECTOR_KEY_TIPC)) {
1231 			key_addrs = skb_flow_dissector_target(flow_dissector,
1232 							      FLOW_DISSECTOR_KEY_TIPC,
1233 							      target_container);
1234 			key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1235 			key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1236 		}
1237 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
1238 		break;
1239 	}
1240 
1241 	case htons(ETH_P_MPLS_UC):
1242 	case htons(ETH_P_MPLS_MC):
1243 		fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1244 						target_container, data,
1245 						nhoff, hlen, mpls_lse,
1246 						&mpls_el);
1247 		nhoff += sizeof(struct mpls_label);
1248 		mpls_lse++;
1249 		break;
1250 	case htons(ETH_P_FCOE):
1251 		if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1252 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1253 			break;
1254 		}
1255 
1256 		nhoff += FCOE_HEADER_LEN;
1257 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
1258 		break;
1259 
1260 	case htons(ETH_P_ARP):
1261 	case htons(ETH_P_RARP):
1262 		fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1263 					       target_container, data,
1264 					       nhoff, hlen);
1265 		break;
1266 
1267 	case htons(ETH_P_BATMAN):
1268 		fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1269 						  &proto, &nhoff, hlen, flags);
1270 		break;
1271 
1272 	case htons(ETH_P_1588): {
1273 		struct ptp_header *hdr, _hdr;
1274 
1275 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
1276 					   hlen, &_hdr);
1277 		if (!hdr) {
1278 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1279 			break;
1280 		}
1281 
1282 		nhoff += ntohs(hdr->message_length);
1283 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
1284 		break;
1285 	}
1286 
1287 	case htons(ETH_P_PRP):
1288 	case htons(ETH_P_HSR): {
1289 		struct hsr_tag *hdr, _hdr;
1290 
1291 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen,
1292 					   &_hdr);
1293 		if (!hdr) {
1294 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1295 			break;
1296 		}
1297 
1298 		proto = hdr->encap_proto;
1299 		nhoff += HSR_HLEN;
1300 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1301 		break;
1302 	}
1303 
1304 	default:
1305 		fdret = FLOW_DISSECT_RET_OUT_BAD;
1306 		break;
1307 	}
1308 
1309 	/* Process result of proto processing */
1310 	switch (fdret) {
1311 	case FLOW_DISSECT_RET_OUT_GOOD:
1312 		goto out_good;
1313 	case FLOW_DISSECT_RET_PROTO_AGAIN:
1314 		if (skb_flow_dissect_allowed(&num_hdrs))
1315 			goto proto_again;
1316 		goto out_good;
1317 	case FLOW_DISSECT_RET_CONTINUE:
1318 	case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1319 		break;
1320 	case FLOW_DISSECT_RET_OUT_BAD:
1321 	default:
1322 		goto out_bad;
1323 	}
1324 
1325 ip_proto_again:
1326 	fdret = FLOW_DISSECT_RET_CONTINUE;
1327 
1328 	switch (ip_proto) {
1329 	case IPPROTO_GRE:
1330 		if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1331 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
1332 			break;
1333 		}
1334 
1335 		fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1336 					       target_container, data,
1337 					       &proto, &nhoff, &hlen, flags);
1338 		break;
1339 
1340 	case NEXTHDR_HOP:
1341 	case NEXTHDR_ROUTING:
1342 	case NEXTHDR_DEST: {
1343 		u8 _opthdr[2], *opthdr;
1344 
1345 		if (proto != htons(ETH_P_IPV6))
1346 			break;
1347 
1348 		opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1349 					      data, hlen, &_opthdr);
1350 		if (!opthdr) {
1351 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1352 			break;
1353 		}
1354 
1355 		ip_proto = opthdr[0];
1356 		nhoff += (opthdr[1] + 1) << 3;
1357 
1358 		fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1359 		break;
1360 	}
1361 	case NEXTHDR_FRAGMENT: {
1362 		struct frag_hdr _fh, *fh;
1363 
1364 		if (proto != htons(ETH_P_IPV6))
1365 			break;
1366 
1367 		fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1368 					  data, hlen, &_fh);
1369 
1370 		if (!fh) {
1371 			fdret = FLOW_DISSECT_RET_OUT_BAD;
1372 			break;
1373 		}
1374 
1375 		key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1376 
1377 		nhoff += sizeof(_fh);
1378 		ip_proto = fh->nexthdr;
1379 
1380 		if (!(fh->frag_off & htons(IP6_OFFSET))) {
1381 			key_control->flags |= FLOW_DIS_FIRST_FRAG;
1382 			if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1383 				fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1384 				break;
1385 			}
1386 		}
1387 
1388 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
1389 		break;
1390 	}
1391 	case IPPROTO_IPIP:
1392 		if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1393 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
1394 			break;
1395 		}
1396 
1397 		proto = htons(ETH_P_IP);
1398 
1399 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
1400 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1401 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
1402 			break;
1403 		}
1404 
1405 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1406 		break;
1407 
1408 	case IPPROTO_IPV6:
1409 		if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1410 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
1411 			break;
1412 		}
1413 
1414 		proto = htons(ETH_P_IPV6);
1415 
1416 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
1417 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1418 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
1419 			break;
1420 		}
1421 
1422 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1423 		break;
1424 
1425 
1426 	case IPPROTO_MPLS:
1427 		proto = htons(ETH_P_MPLS_UC);
1428 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1429 		break;
1430 
1431 	case IPPROTO_TCP:
1432 		__skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1433 				       data, nhoff, hlen);
1434 		break;
1435 
1436 	case IPPROTO_ICMP:
1437 	case IPPROTO_ICMPV6:
1438 		__skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1439 					data, nhoff, hlen);
1440 		break;
1441 
1442 	default:
1443 		break;
1444 	}
1445 
1446 	if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1447 		__skb_flow_dissect_ports(skb, flow_dissector, target_container,
1448 					 data, nhoff, ip_proto, hlen);
1449 
1450 	/* Process result of IP proto processing */
1451 	switch (fdret) {
1452 	case FLOW_DISSECT_RET_PROTO_AGAIN:
1453 		if (skb_flow_dissect_allowed(&num_hdrs))
1454 			goto proto_again;
1455 		break;
1456 	case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1457 		if (skb_flow_dissect_allowed(&num_hdrs))
1458 			goto ip_proto_again;
1459 		break;
1460 	case FLOW_DISSECT_RET_OUT_GOOD:
1461 	case FLOW_DISSECT_RET_CONTINUE:
1462 		break;
1463 	case FLOW_DISSECT_RET_OUT_BAD:
1464 	default:
1465 		goto out_bad;
1466 	}
1467 
1468 out_good:
1469 	ret = true;
1470 
1471 out:
1472 	key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1473 	key_basic->n_proto = proto;
1474 	key_basic->ip_proto = ip_proto;
1475 
1476 	return ret;
1477 
1478 out_bad:
1479 	ret = false;
1480 	goto out;
1481 }
1482 EXPORT_SYMBOL(__skb_flow_dissect);
1483 
1484 static siphash_aligned_key_t hashrnd;
1485 static __always_inline void __flow_hash_secret_init(void)
1486 {
1487 	net_get_random_once(&hashrnd, sizeof(hashrnd));
1488 }
1489 
1490 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1491 {
1492 	BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1493 	return &flow->FLOW_KEYS_HASH_START_FIELD;
1494 }
1495 
1496 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1497 {
1498 	size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1499 
1500 	BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1501 
1502 	switch (flow->control.addr_type) {
1503 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1504 		diff -= sizeof(flow->addrs.v4addrs);
1505 		break;
1506 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1507 		diff -= sizeof(flow->addrs.v6addrs);
1508 		break;
1509 	case FLOW_DISSECTOR_KEY_TIPC:
1510 		diff -= sizeof(flow->addrs.tipckey);
1511 		break;
1512 	}
1513 	return sizeof(*flow) - diff;
1514 }
1515 
1516 __be32 flow_get_u32_src(const struct flow_keys *flow)
1517 {
1518 	switch (flow->control.addr_type) {
1519 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1520 		return flow->addrs.v4addrs.src;
1521 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1522 		return (__force __be32)ipv6_addr_hash(
1523 			&flow->addrs.v6addrs.src);
1524 	case FLOW_DISSECTOR_KEY_TIPC:
1525 		return flow->addrs.tipckey.key;
1526 	default:
1527 		return 0;
1528 	}
1529 }
1530 EXPORT_SYMBOL(flow_get_u32_src);
1531 
1532 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1533 {
1534 	switch (flow->control.addr_type) {
1535 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1536 		return flow->addrs.v4addrs.dst;
1537 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1538 		return (__force __be32)ipv6_addr_hash(
1539 			&flow->addrs.v6addrs.dst);
1540 	default:
1541 		return 0;
1542 	}
1543 }
1544 EXPORT_SYMBOL(flow_get_u32_dst);
1545 
1546 /* Sort the source and destination IP and the ports,
1547  * to have consistent hash within the two directions
1548  */
1549 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1550 {
1551 	int addr_diff, i;
1552 
1553 	switch (keys->control.addr_type) {
1554 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1555 		addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1556 			    (__force u32)keys->addrs.v4addrs.src;
1557 		if (addr_diff < 0)
1558 			swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1559 
1560 		if ((__force u16)keys->ports.dst <
1561 		    (__force u16)keys->ports.src) {
1562 			swap(keys->ports.src, keys->ports.dst);
1563 		}
1564 		break;
1565 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1566 		addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1567 				   &keys->addrs.v6addrs.src,
1568 				   sizeof(keys->addrs.v6addrs.dst));
1569 		if (addr_diff < 0) {
1570 			for (i = 0; i < 4; i++)
1571 				swap(keys->addrs.v6addrs.src.s6_addr32[i],
1572 				     keys->addrs.v6addrs.dst.s6_addr32[i]);
1573 		}
1574 		if ((__force u16)keys->ports.dst <
1575 		    (__force u16)keys->ports.src) {
1576 			swap(keys->ports.src, keys->ports.dst);
1577 		}
1578 		break;
1579 	}
1580 }
1581 
1582 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1583 					const siphash_key_t *keyval)
1584 {
1585 	u32 hash;
1586 
1587 	__flow_hash_consistentify(keys);
1588 
1589 	hash = siphash(flow_keys_hash_start(keys),
1590 		       flow_keys_hash_length(keys), keyval);
1591 	if (!hash)
1592 		hash = 1;
1593 
1594 	return hash;
1595 }
1596 
1597 u32 flow_hash_from_keys(struct flow_keys *keys)
1598 {
1599 	__flow_hash_secret_init();
1600 	return __flow_hash_from_keys(keys, &hashrnd);
1601 }
1602 EXPORT_SYMBOL(flow_hash_from_keys);
1603 
1604 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1605 				  struct flow_keys *keys,
1606 				  const siphash_key_t *keyval)
1607 {
1608 	skb_flow_dissect_flow_keys(skb, keys,
1609 				   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1610 
1611 	return __flow_hash_from_keys(keys, keyval);
1612 }
1613 
1614 struct _flow_keys_digest_data {
1615 	__be16	n_proto;
1616 	u8	ip_proto;
1617 	u8	padding;
1618 	__be32	ports;
1619 	__be32	src;
1620 	__be32	dst;
1621 };
1622 
1623 void make_flow_keys_digest(struct flow_keys_digest *digest,
1624 			   const struct flow_keys *flow)
1625 {
1626 	struct _flow_keys_digest_data *data =
1627 	    (struct _flow_keys_digest_data *)digest;
1628 
1629 	BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1630 
1631 	memset(digest, 0, sizeof(*digest));
1632 
1633 	data->n_proto = flow->basic.n_proto;
1634 	data->ip_proto = flow->basic.ip_proto;
1635 	data->ports = flow->ports.ports;
1636 	data->src = flow->addrs.v4addrs.src;
1637 	data->dst = flow->addrs.v4addrs.dst;
1638 }
1639 EXPORT_SYMBOL(make_flow_keys_digest);
1640 
1641 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1642 
1643 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1644 {
1645 	struct flow_keys keys;
1646 
1647 	__flow_hash_secret_init();
1648 
1649 	memset(&keys, 0, sizeof(keys));
1650 	__skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1651 			   &keys, NULL, 0, 0, 0,
1652 			   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1653 
1654 	return __flow_hash_from_keys(&keys, &hashrnd);
1655 }
1656 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1657 
1658 /**
1659  * __skb_get_hash: calculate a flow hash
1660  * @skb: sk_buff to calculate flow hash from
1661  *
1662  * This function calculates a flow hash based on src/dst addresses
1663  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
1664  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
1665  * if hash is a canonical 4-tuple hash over transport ports.
1666  */
1667 void __skb_get_hash(struct sk_buff *skb)
1668 {
1669 	struct flow_keys keys;
1670 	u32 hash;
1671 
1672 	__flow_hash_secret_init();
1673 
1674 	hash = ___skb_get_hash(skb, &keys, &hashrnd);
1675 
1676 	__skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1677 }
1678 EXPORT_SYMBOL(__skb_get_hash);
1679 
1680 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1681 			   const siphash_key_t *perturb)
1682 {
1683 	struct flow_keys keys;
1684 
1685 	return ___skb_get_hash(skb, &keys, perturb);
1686 }
1687 EXPORT_SYMBOL(skb_get_hash_perturb);
1688 
1689 u32 __skb_get_poff(const struct sk_buff *skb, const void *data,
1690 		   const struct flow_keys_basic *keys, int hlen)
1691 {
1692 	u32 poff = keys->control.thoff;
1693 
1694 	/* skip L4 headers for fragments after the first */
1695 	if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1696 	    !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1697 		return poff;
1698 
1699 	switch (keys->basic.ip_proto) {
1700 	case IPPROTO_TCP: {
1701 		/* access doff as u8 to avoid unaligned access */
1702 		const u8 *doff;
1703 		u8 _doff;
1704 
1705 		doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1706 					    data, hlen, &_doff);
1707 		if (!doff)
1708 			return poff;
1709 
1710 		poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1711 		break;
1712 	}
1713 	case IPPROTO_UDP:
1714 	case IPPROTO_UDPLITE:
1715 		poff += sizeof(struct udphdr);
1716 		break;
1717 	/* For the rest, we do not really care about header
1718 	 * extensions at this point for now.
1719 	 */
1720 	case IPPROTO_ICMP:
1721 		poff += sizeof(struct icmphdr);
1722 		break;
1723 	case IPPROTO_ICMPV6:
1724 		poff += sizeof(struct icmp6hdr);
1725 		break;
1726 	case IPPROTO_IGMP:
1727 		poff += sizeof(struct igmphdr);
1728 		break;
1729 	case IPPROTO_DCCP:
1730 		poff += sizeof(struct dccp_hdr);
1731 		break;
1732 	case IPPROTO_SCTP:
1733 		poff += sizeof(struct sctphdr);
1734 		break;
1735 	}
1736 
1737 	return poff;
1738 }
1739 
1740 /**
1741  * skb_get_poff - get the offset to the payload
1742  * @skb: sk_buff to get the payload offset from
1743  *
1744  * The function will get the offset to the payload as far as it could
1745  * be dissected.  The main user is currently BPF, so that we can dynamically
1746  * truncate packets without needing to push actual payload to the user
1747  * space and can analyze headers only, instead.
1748  */
1749 u32 skb_get_poff(const struct sk_buff *skb)
1750 {
1751 	struct flow_keys_basic keys;
1752 
1753 	if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1754 					      NULL, 0, 0, 0, 0))
1755 		return 0;
1756 
1757 	return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1758 }
1759 
1760 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1761 {
1762 	memset(keys, 0, sizeof(*keys));
1763 
1764 	memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1765 	    sizeof(keys->addrs.v6addrs.src));
1766 	memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1767 	    sizeof(keys->addrs.v6addrs.dst));
1768 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1769 	keys->ports.src = fl6->fl6_sport;
1770 	keys->ports.dst = fl6->fl6_dport;
1771 	keys->keyid.keyid = fl6->fl6_gre_key;
1772 	keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1773 	keys->basic.ip_proto = fl6->flowi6_proto;
1774 
1775 	return flow_hash_from_keys(keys);
1776 }
1777 EXPORT_SYMBOL(__get_hash_from_flowi6);
1778 
1779 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1780 	{
1781 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1782 		.offset = offsetof(struct flow_keys, control),
1783 	},
1784 	{
1785 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1786 		.offset = offsetof(struct flow_keys, basic),
1787 	},
1788 	{
1789 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1790 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
1791 	},
1792 	{
1793 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1794 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
1795 	},
1796 	{
1797 		.key_id = FLOW_DISSECTOR_KEY_TIPC,
1798 		.offset = offsetof(struct flow_keys, addrs.tipckey),
1799 	},
1800 	{
1801 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
1802 		.offset = offsetof(struct flow_keys, ports),
1803 	},
1804 	{
1805 		.key_id = FLOW_DISSECTOR_KEY_VLAN,
1806 		.offset = offsetof(struct flow_keys, vlan),
1807 	},
1808 	{
1809 		.key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1810 		.offset = offsetof(struct flow_keys, tags),
1811 	},
1812 	{
1813 		.key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1814 		.offset = offsetof(struct flow_keys, keyid),
1815 	},
1816 };
1817 
1818 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1819 	{
1820 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1821 		.offset = offsetof(struct flow_keys, control),
1822 	},
1823 	{
1824 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1825 		.offset = offsetof(struct flow_keys, basic),
1826 	},
1827 	{
1828 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1829 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
1830 	},
1831 	{
1832 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1833 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
1834 	},
1835 	{
1836 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
1837 		.offset = offsetof(struct flow_keys, ports),
1838 	},
1839 };
1840 
1841 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1842 	{
1843 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1844 		.offset = offsetof(struct flow_keys, control),
1845 	},
1846 	{
1847 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1848 		.offset = offsetof(struct flow_keys, basic),
1849 	},
1850 };
1851 
1852 struct flow_dissector flow_keys_dissector __read_mostly;
1853 EXPORT_SYMBOL(flow_keys_dissector);
1854 
1855 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1856 EXPORT_SYMBOL(flow_keys_basic_dissector);
1857 
1858 static int __init init_default_flow_dissectors(void)
1859 {
1860 	skb_flow_dissector_init(&flow_keys_dissector,
1861 				flow_keys_dissector_keys,
1862 				ARRAY_SIZE(flow_keys_dissector_keys));
1863 	skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1864 				flow_keys_dissector_symmetric_keys,
1865 				ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1866 	skb_flow_dissector_init(&flow_keys_basic_dissector,
1867 				flow_keys_basic_dissector_keys,
1868 				ARRAY_SIZE(flow_keys_basic_dissector_keys));
1869 	return 0;
1870 }
1871 core_initcall(init_default_flow_dissectors);
1872