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