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