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