xref: /linux/net/core/flow_dissector.c (revision 3f2fb9a834cb1fcddbae22deca7fde136944dc89)
1 #include <linux/kernel.h>
2 #include <linux/skbuff.h>
3 #include <linux/export.h>
4 #include <linux/ip.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
7 #include <net/ip.h>
8 #include <net/ipv6.h>
9 #include <linux/igmp.h>
10 #include <linux/icmp.h>
11 #include <linux/sctp.h>
12 #include <linux/dccp.h>
13 #include <linux/if_tunnel.h>
14 #include <linux/if_pppox.h>
15 #include <linux/ppp_defs.h>
16 #include <linux/stddef.h>
17 #include <linux/if_ether.h>
18 #include <linux/mpls.h>
19 #include <net/flow_dissector.h>
20 #include <scsi/fc/fc_fcoe.h>
21 
22 static bool dissector_uses_key(const struct flow_dissector *flow_dissector,
23 			       enum flow_dissector_key_id key_id)
24 {
25 	return flow_dissector->used_keys & (1 << key_id);
26 }
27 
28 static void dissector_set_key(struct flow_dissector *flow_dissector,
29 			      enum flow_dissector_key_id key_id)
30 {
31 	flow_dissector->used_keys |= (1 << key_id);
32 }
33 
34 static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
35 				       enum flow_dissector_key_id key_id,
36 				       void *target_container)
37 {
38 	return ((char *) target_container) + flow_dissector->offset[key_id];
39 }
40 
41 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
42 			     const struct flow_dissector_key *key,
43 			     unsigned int key_count)
44 {
45 	unsigned int i;
46 
47 	memset(flow_dissector, 0, sizeof(*flow_dissector));
48 
49 	for (i = 0; i < key_count; i++, key++) {
50 		/* User should make sure that every key target offset is withing
51 		 * boundaries of unsigned short.
52 		 */
53 		BUG_ON(key->offset > USHRT_MAX);
54 		BUG_ON(dissector_uses_key(flow_dissector,
55 					  key->key_id));
56 
57 		dissector_set_key(flow_dissector, key->key_id);
58 		flow_dissector->offset[key->key_id] = key->offset;
59 	}
60 
61 	/* Ensure that the dissector always includes control and basic key.
62 	 * That way we are able to avoid handling lack of these in fast path.
63 	 */
64 	BUG_ON(!dissector_uses_key(flow_dissector,
65 				   FLOW_DISSECTOR_KEY_CONTROL));
66 	BUG_ON(!dissector_uses_key(flow_dissector,
67 				   FLOW_DISSECTOR_KEY_BASIC));
68 }
69 EXPORT_SYMBOL(skb_flow_dissector_init);
70 
71 /**
72  * __skb_flow_get_ports - extract the upper layer ports and return them
73  * @skb: sk_buff to extract the ports from
74  * @thoff: transport header offset
75  * @ip_proto: protocol for which to get port offset
76  * @data: raw buffer pointer to the packet, if NULL use skb->data
77  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
78  *
79  * The function will try to retrieve the ports at offset thoff + poff where poff
80  * is the protocol port offset returned from proto_ports_offset
81  */
82 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
83 			    void *data, int hlen)
84 {
85 	int poff = proto_ports_offset(ip_proto);
86 
87 	if (!data) {
88 		data = skb->data;
89 		hlen = skb_headlen(skb);
90 	}
91 
92 	if (poff >= 0) {
93 		__be32 *ports, _ports;
94 
95 		ports = __skb_header_pointer(skb, thoff + poff,
96 					     sizeof(_ports), data, hlen, &_ports);
97 		if (ports)
98 			return *ports;
99 	}
100 
101 	return 0;
102 }
103 EXPORT_SYMBOL(__skb_flow_get_ports);
104 
105 /**
106  * __skb_flow_dissect - extract the flow_keys struct and return it
107  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
108  * @flow_dissector: list of keys to dissect
109  * @target_container: target structure to put dissected values into
110  * @data: raw buffer pointer to the packet, if NULL use skb->data
111  * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
112  * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
113  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
114  *
115  * The function will try to retrieve individual keys into target specified
116  * by flow_dissector from either the skbuff or a raw buffer specified by the
117  * rest parameters.
118  *
119  * Caller must take care of zeroing target container memory.
120  */
121 bool __skb_flow_dissect(const struct sk_buff *skb,
122 			struct flow_dissector *flow_dissector,
123 			void *target_container,
124 			void *data, __be16 proto, int nhoff, int hlen,
125 			unsigned int flags)
126 {
127 	struct flow_dissector_key_control *key_control;
128 	struct flow_dissector_key_basic *key_basic;
129 	struct flow_dissector_key_addrs *key_addrs;
130 	struct flow_dissector_key_ports *key_ports;
131 	struct flow_dissector_key_tags *key_tags;
132 	struct flow_dissector_key_keyid *key_keyid;
133 	u8 ip_proto = 0;
134 	bool ret = false;
135 
136 	if (!data) {
137 		data = skb->data;
138 		proto = skb->protocol;
139 		nhoff = skb_network_offset(skb);
140 		hlen = skb_headlen(skb);
141 	}
142 
143 	/* It is ensured by skb_flow_dissector_init() that control key will
144 	 * be always present.
145 	 */
146 	key_control = skb_flow_dissector_target(flow_dissector,
147 						FLOW_DISSECTOR_KEY_CONTROL,
148 						target_container);
149 
150 	/* It is ensured by skb_flow_dissector_init() that basic key will
151 	 * be always present.
152 	 */
153 	key_basic = skb_flow_dissector_target(flow_dissector,
154 					      FLOW_DISSECTOR_KEY_BASIC,
155 					      target_container);
156 
157 	if (dissector_uses_key(flow_dissector,
158 			       FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
159 		struct ethhdr *eth = eth_hdr(skb);
160 		struct flow_dissector_key_eth_addrs *key_eth_addrs;
161 
162 		key_eth_addrs = skb_flow_dissector_target(flow_dissector,
163 							  FLOW_DISSECTOR_KEY_ETH_ADDRS,
164 							  target_container);
165 		memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
166 	}
167 
168 again:
169 	switch (proto) {
170 	case htons(ETH_P_IP): {
171 		const struct iphdr *iph;
172 		struct iphdr _iph;
173 ip:
174 		iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
175 		if (!iph || iph->ihl < 5)
176 			goto out_bad;
177 		nhoff += iph->ihl * 4;
178 
179 		ip_proto = iph->protocol;
180 
181 		if (dissector_uses_key(flow_dissector,
182 				       FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
183 			key_addrs = skb_flow_dissector_target(flow_dissector,
184 							      FLOW_DISSECTOR_KEY_IPV4_ADDRS,
185 							      target_container);
186 
187 			memcpy(&key_addrs->v4addrs, &iph->saddr,
188 			       sizeof(key_addrs->v4addrs));
189 			key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
190 		}
191 
192 		if (ip_is_fragment(iph)) {
193 			key_control->flags |= FLOW_DIS_IS_FRAGMENT;
194 
195 			if (iph->frag_off & htons(IP_OFFSET)) {
196 				goto out_good;
197 			} else {
198 				key_control->flags |= FLOW_DIS_FIRST_FRAG;
199 				if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
200 					goto out_good;
201 			}
202 		}
203 
204 		if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
205 			goto out_good;
206 
207 		break;
208 	}
209 	case htons(ETH_P_IPV6): {
210 		const struct ipv6hdr *iph;
211 		struct ipv6hdr _iph;
212 
213 ipv6:
214 		iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
215 		if (!iph)
216 			goto out_bad;
217 
218 		ip_proto = iph->nexthdr;
219 		nhoff += sizeof(struct ipv6hdr);
220 
221 		if (dissector_uses_key(flow_dissector,
222 				       FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
223 			key_addrs = skb_flow_dissector_target(flow_dissector,
224 							      FLOW_DISSECTOR_KEY_IPV6_ADDRS,
225 							      target_container);
226 
227 			memcpy(&key_addrs->v6addrs, &iph->saddr,
228 			       sizeof(key_addrs->v6addrs));
229 			key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
230 		}
231 
232 		if ((dissector_uses_key(flow_dissector,
233 					FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
234 		     (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
235 		    ip6_flowlabel(iph)) {
236 			__be32 flow_label = ip6_flowlabel(iph);
237 
238 			if (dissector_uses_key(flow_dissector,
239 					       FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
240 				key_tags = skb_flow_dissector_target(flow_dissector,
241 								     FLOW_DISSECTOR_KEY_FLOW_LABEL,
242 								     target_container);
243 				key_tags->flow_label = ntohl(flow_label);
244 			}
245 			if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
246 				goto out_good;
247 		}
248 
249 		if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
250 			goto out_good;
251 
252 		break;
253 	}
254 	case htons(ETH_P_8021AD):
255 	case htons(ETH_P_8021Q): {
256 		const struct vlan_hdr *vlan;
257 		struct vlan_hdr _vlan;
258 
259 		vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
260 		if (!vlan)
261 			goto out_bad;
262 
263 		if (dissector_uses_key(flow_dissector,
264 				       FLOW_DISSECTOR_KEY_VLANID)) {
265 			key_tags = skb_flow_dissector_target(flow_dissector,
266 							     FLOW_DISSECTOR_KEY_VLANID,
267 							     target_container);
268 
269 			key_tags->vlan_id = skb_vlan_tag_get_id(skb);
270 		}
271 
272 		proto = vlan->h_vlan_encapsulated_proto;
273 		nhoff += sizeof(*vlan);
274 		goto again;
275 	}
276 	case htons(ETH_P_PPP_SES): {
277 		struct {
278 			struct pppoe_hdr hdr;
279 			__be16 proto;
280 		} *hdr, _hdr;
281 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
282 		if (!hdr)
283 			goto out_bad;
284 		proto = hdr->proto;
285 		nhoff += PPPOE_SES_HLEN;
286 		switch (proto) {
287 		case htons(PPP_IP):
288 			goto ip;
289 		case htons(PPP_IPV6):
290 			goto ipv6;
291 		default:
292 			goto out_bad;
293 		}
294 	}
295 	case htons(ETH_P_TIPC): {
296 		struct {
297 			__be32 pre[3];
298 			__be32 srcnode;
299 		} *hdr, _hdr;
300 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
301 		if (!hdr)
302 			goto out_bad;
303 
304 		if (dissector_uses_key(flow_dissector,
305 				       FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
306 			key_addrs = skb_flow_dissector_target(flow_dissector,
307 							      FLOW_DISSECTOR_KEY_TIPC_ADDRS,
308 							      target_container);
309 			key_addrs->tipcaddrs.srcnode = hdr->srcnode;
310 			key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
311 		}
312 		goto out_good;
313 	}
314 
315 	case htons(ETH_P_MPLS_UC):
316 	case htons(ETH_P_MPLS_MC): {
317 		struct mpls_label *hdr, _hdr[2];
318 mpls:
319 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
320 					   hlen, &_hdr);
321 		if (!hdr)
322 			goto out_bad;
323 
324 		if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
325 		     MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
326 			if (dissector_uses_key(flow_dissector,
327 					       FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
328 				key_keyid = skb_flow_dissector_target(flow_dissector,
329 								      FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
330 								      target_container);
331 				key_keyid->keyid = hdr[1].entry &
332 					htonl(MPLS_LS_LABEL_MASK);
333 			}
334 
335 			goto out_good;
336 		}
337 
338 		goto out_good;
339 	}
340 
341 	case htons(ETH_P_FCOE):
342 		if ((hlen - nhoff) < FCOE_HEADER_LEN)
343 			goto out_bad;
344 
345 		nhoff += FCOE_HEADER_LEN;
346 		goto out_good;
347 	default:
348 		goto out_bad;
349 	}
350 
351 ip_proto_again:
352 	switch (ip_proto) {
353 	case IPPROTO_GRE: {
354 		struct gre_hdr {
355 			__be16 flags;
356 			__be16 proto;
357 		} *hdr, _hdr;
358 
359 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
360 		if (!hdr)
361 			goto out_bad;
362 		/*
363 		 * Only look inside GRE if version zero and no
364 		 * routing
365 		 */
366 		if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
367 			break;
368 
369 		proto = hdr->proto;
370 		nhoff += 4;
371 		if (hdr->flags & GRE_CSUM)
372 			nhoff += 4;
373 		if (hdr->flags & GRE_KEY) {
374 			const __be32 *keyid;
375 			__be32 _keyid;
376 
377 			keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
378 						     data, hlen, &_keyid);
379 
380 			if (!keyid)
381 				goto out_bad;
382 
383 			if (dissector_uses_key(flow_dissector,
384 					       FLOW_DISSECTOR_KEY_GRE_KEYID)) {
385 				key_keyid = skb_flow_dissector_target(flow_dissector,
386 								      FLOW_DISSECTOR_KEY_GRE_KEYID,
387 								      target_container);
388 				key_keyid->keyid = *keyid;
389 			}
390 			nhoff += 4;
391 		}
392 		if (hdr->flags & GRE_SEQ)
393 			nhoff += 4;
394 		if (proto == htons(ETH_P_TEB)) {
395 			const struct ethhdr *eth;
396 			struct ethhdr _eth;
397 
398 			eth = __skb_header_pointer(skb, nhoff,
399 						   sizeof(_eth),
400 						   data, hlen, &_eth);
401 			if (!eth)
402 				goto out_bad;
403 			proto = eth->h_proto;
404 			nhoff += sizeof(*eth);
405 
406 			/* Cap headers that we access via pointers at the
407 			 * end of the Ethernet header as our maximum alignment
408 			 * at that point is only 2 bytes.
409 			 */
410 			if (NET_IP_ALIGN)
411 				hlen = nhoff;
412 		}
413 
414 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
415 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
416 			goto out_good;
417 
418 		goto again;
419 	}
420 	case NEXTHDR_HOP:
421 	case NEXTHDR_ROUTING:
422 	case NEXTHDR_DEST: {
423 		u8 _opthdr[2], *opthdr;
424 
425 		if (proto != htons(ETH_P_IPV6))
426 			break;
427 
428 		opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
429 					      data, hlen, &_opthdr);
430 		if (!opthdr)
431 			goto out_bad;
432 
433 		ip_proto = opthdr[0];
434 		nhoff += (opthdr[1] + 1) << 3;
435 
436 		goto ip_proto_again;
437 	}
438 	case NEXTHDR_FRAGMENT: {
439 		struct frag_hdr _fh, *fh;
440 
441 		if (proto != htons(ETH_P_IPV6))
442 			break;
443 
444 		fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
445 					  data, hlen, &_fh);
446 
447 		if (!fh)
448 			goto out_bad;
449 
450 		key_control->flags |= FLOW_DIS_IS_FRAGMENT;
451 
452 		nhoff += sizeof(_fh);
453 		ip_proto = fh->nexthdr;
454 
455 		if (!(fh->frag_off & htons(IP6_OFFSET))) {
456 			key_control->flags |= FLOW_DIS_FIRST_FRAG;
457 			if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
458 				goto ip_proto_again;
459 		}
460 		goto out_good;
461 	}
462 	case IPPROTO_IPIP:
463 		proto = htons(ETH_P_IP);
464 
465 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
466 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
467 			goto out_good;
468 
469 		goto ip;
470 	case IPPROTO_IPV6:
471 		proto = htons(ETH_P_IPV6);
472 
473 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
474 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
475 			goto out_good;
476 
477 		goto ipv6;
478 	case IPPROTO_MPLS:
479 		proto = htons(ETH_P_MPLS_UC);
480 		goto mpls;
481 	default:
482 		break;
483 	}
484 
485 	if (dissector_uses_key(flow_dissector,
486 			       FLOW_DISSECTOR_KEY_PORTS)) {
487 		key_ports = skb_flow_dissector_target(flow_dissector,
488 						      FLOW_DISSECTOR_KEY_PORTS,
489 						      target_container);
490 		key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
491 							data, hlen);
492 	}
493 
494 out_good:
495 	ret = true;
496 
497 out_bad:
498 	key_basic->n_proto = proto;
499 	key_basic->ip_proto = ip_proto;
500 	key_control->thoff = (u16)nhoff;
501 
502 	return ret;
503 }
504 EXPORT_SYMBOL(__skb_flow_dissect);
505 
506 static u32 hashrnd __read_mostly;
507 static __always_inline void __flow_hash_secret_init(void)
508 {
509 	net_get_random_once(&hashrnd, sizeof(hashrnd));
510 }
511 
512 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
513 					     u32 keyval)
514 {
515 	return jhash2(words, length, keyval);
516 }
517 
518 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
519 {
520 	const void *p = flow;
521 
522 	BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
523 	return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
524 }
525 
526 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
527 {
528 	size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
529 	BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
530 	BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
531 		     sizeof(*flow) - sizeof(flow->addrs));
532 
533 	switch (flow->control.addr_type) {
534 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
535 		diff -= sizeof(flow->addrs.v4addrs);
536 		break;
537 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
538 		diff -= sizeof(flow->addrs.v6addrs);
539 		break;
540 	case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
541 		diff -= sizeof(flow->addrs.tipcaddrs);
542 		break;
543 	}
544 	return (sizeof(*flow) - diff) / sizeof(u32);
545 }
546 
547 __be32 flow_get_u32_src(const struct flow_keys *flow)
548 {
549 	switch (flow->control.addr_type) {
550 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
551 		return flow->addrs.v4addrs.src;
552 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
553 		return (__force __be32)ipv6_addr_hash(
554 			&flow->addrs.v6addrs.src);
555 	case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
556 		return flow->addrs.tipcaddrs.srcnode;
557 	default:
558 		return 0;
559 	}
560 }
561 EXPORT_SYMBOL(flow_get_u32_src);
562 
563 __be32 flow_get_u32_dst(const struct flow_keys *flow)
564 {
565 	switch (flow->control.addr_type) {
566 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
567 		return flow->addrs.v4addrs.dst;
568 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
569 		return (__force __be32)ipv6_addr_hash(
570 			&flow->addrs.v6addrs.dst);
571 	default:
572 		return 0;
573 	}
574 }
575 EXPORT_SYMBOL(flow_get_u32_dst);
576 
577 static inline void __flow_hash_consistentify(struct flow_keys *keys)
578 {
579 	int addr_diff, i;
580 
581 	switch (keys->control.addr_type) {
582 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
583 		addr_diff = (__force u32)keys->addrs.v4addrs.dst -
584 			    (__force u32)keys->addrs.v4addrs.src;
585 		if ((addr_diff < 0) ||
586 		    (addr_diff == 0 &&
587 		     ((__force u16)keys->ports.dst <
588 		      (__force u16)keys->ports.src))) {
589 			swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
590 			swap(keys->ports.src, keys->ports.dst);
591 		}
592 		break;
593 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
594 		addr_diff = memcmp(&keys->addrs.v6addrs.dst,
595 				   &keys->addrs.v6addrs.src,
596 				   sizeof(keys->addrs.v6addrs.dst));
597 		if ((addr_diff < 0) ||
598 		    (addr_diff == 0 &&
599 		     ((__force u16)keys->ports.dst <
600 		      (__force u16)keys->ports.src))) {
601 			for (i = 0; i < 4; i++)
602 				swap(keys->addrs.v6addrs.src.s6_addr32[i],
603 				     keys->addrs.v6addrs.dst.s6_addr32[i]);
604 			swap(keys->ports.src, keys->ports.dst);
605 		}
606 		break;
607 	}
608 }
609 
610 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
611 {
612 	u32 hash;
613 
614 	__flow_hash_consistentify(keys);
615 
616 	hash = __flow_hash_words(flow_keys_hash_start(keys),
617 				 flow_keys_hash_length(keys), keyval);
618 	if (!hash)
619 		hash = 1;
620 
621 	return hash;
622 }
623 
624 u32 flow_hash_from_keys(struct flow_keys *keys)
625 {
626 	__flow_hash_secret_init();
627 	return __flow_hash_from_keys(keys, hashrnd);
628 }
629 EXPORT_SYMBOL(flow_hash_from_keys);
630 
631 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
632 				  struct flow_keys *keys, u32 keyval)
633 {
634 	skb_flow_dissect_flow_keys(skb, keys,
635 				   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
636 
637 	return __flow_hash_from_keys(keys, keyval);
638 }
639 
640 struct _flow_keys_digest_data {
641 	__be16	n_proto;
642 	u8	ip_proto;
643 	u8	padding;
644 	__be32	ports;
645 	__be32	src;
646 	__be32	dst;
647 };
648 
649 void make_flow_keys_digest(struct flow_keys_digest *digest,
650 			   const struct flow_keys *flow)
651 {
652 	struct _flow_keys_digest_data *data =
653 	    (struct _flow_keys_digest_data *)digest;
654 
655 	BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
656 
657 	memset(digest, 0, sizeof(*digest));
658 
659 	data->n_proto = flow->basic.n_proto;
660 	data->ip_proto = flow->basic.ip_proto;
661 	data->ports = flow->ports.ports;
662 	data->src = flow->addrs.v4addrs.src;
663 	data->dst = flow->addrs.v4addrs.dst;
664 }
665 EXPORT_SYMBOL(make_flow_keys_digest);
666 
667 /**
668  * __skb_get_hash: calculate a flow hash
669  * @skb: sk_buff to calculate flow hash from
670  *
671  * This function calculates a flow hash based on src/dst addresses
672  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
673  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
674  * if hash is a canonical 4-tuple hash over transport ports.
675  */
676 void __skb_get_hash(struct sk_buff *skb)
677 {
678 	struct flow_keys keys;
679 
680 	__flow_hash_secret_init();
681 
682 	__skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
683 			  flow_keys_have_l4(&keys));
684 }
685 EXPORT_SYMBOL(__skb_get_hash);
686 
687 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
688 {
689 	struct flow_keys keys;
690 
691 	return ___skb_get_hash(skb, &keys, perturb);
692 }
693 EXPORT_SYMBOL(skb_get_hash_perturb);
694 
695 __u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
696 {
697 	struct flow_keys keys;
698 
699 	memset(&keys, 0, sizeof(keys));
700 
701 	memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
702 	       sizeof(keys.addrs.v6addrs.src));
703 	memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
704 	       sizeof(keys.addrs.v6addrs.dst));
705 	keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
706 	keys.ports.src = fl6->fl6_sport;
707 	keys.ports.dst = fl6->fl6_dport;
708 	keys.keyid.keyid = fl6->fl6_gre_key;
709 	keys.tags.flow_label = (__force u32)fl6->flowlabel;
710 	keys.basic.ip_proto = fl6->flowi6_proto;
711 
712 	__skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
713 			  flow_keys_have_l4(&keys));
714 
715 	return skb->hash;
716 }
717 EXPORT_SYMBOL(__skb_get_hash_flowi6);
718 
719 __u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
720 {
721 	struct flow_keys keys;
722 
723 	memset(&keys, 0, sizeof(keys));
724 
725 	keys.addrs.v4addrs.src = fl4->saddr;
726 	keys.addrs.v4addrs.dst = fl4->daddr;
727 	keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
728 	keys.ports.src = fl4->fl4_sport;
729 	keys.ports.dst = fl4->fl4_dport;
730 	keys.keyid.keyid = fl4->fl4_gre_key;
731 	keys.basic.ip_proto = fl4->flowi4_proto;
732 
733 	__skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
734 			  flow_keys_have_l4(&keys));
735 
736 	return skb->hash;
737 }
738 EXPORT_SYMBOL(__skb_get_hash_flowi4);
739 
740 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
741 		   const struct flow_keys *keys, int hlen)
742 {
743 	u32 poff = keys->control.thoff;
744 
745 	/* skip L4 headers for fragments after the first */
746 	if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
747 	    !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
748 		return poff;
749 
750 	switch (keys->basic.ip_proto) {
751 	case IPPROTO_TCP: {
752 		/* access doff as u8 to avoid unaligned access */
753 		const u8 *doff;
754 		u8 _doff;
755 
756 		doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
757 					    data, hlen, &_doff);
758 		if (!doff)
759 			return poff;
760 
761 		poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
762 		break;
763 	}
764 	case IPPROTO_UDP:
765 	case IPPROTO_UDPLITE:
766 		poff += sizeof(struct udphdr);
767 		break;
768 	/* For the rest, we do not really care about header
769 	 * extensions at this point for now.
770 	 */
771 	case IPPROTO_ICMP:
772 		poff += sizeof(struct icmphdr);
773 		break;
774 	case IPPROTO_ICMPV6:
775 		poff += sizeof(struct icmp6hdr);
776 		break;
777 	case IPPROTO_IGMP:
778 		poff += sizeof(struct igmphdr);
779 		break;
780 	case IPPROTO_DCCP:
781 		poff += sizeof(struct dccp_hdr);
782 		break;
783 	case IPPROTO_SCTP:
784 		poff += sizeof(struct sctphdr);
785 		break;
786 	}
787 
788 	return poff;
789 }
790 
791 /**
792  * skb_get_poff - get the offset to the payload
793  * @skb: sk_buff to get the payload offset from
794  *
795  * The function will get the offset to the payload as far as it could
796  * be dissected.  The main user is currently BPF, so that we can dynamically
797  * truncate packets without needing to push actual payload to the user
798  * space and can analyze headers only, instead.
799  */
800 u32 skb_get_poff(const struct sk_buff *skb)
801 {
802 	struct flow_keys keys;
803 
804 	if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
805 		return 0;
806 
807 	return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
808 }
809 
810 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
811 {
812 	memset(keys, 0, sizeof(*keys));
813 
814 	memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
815 	    sizeof(keys->addrs.v6addrs.src));
816 	memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
817 	    sizeof(keys->addrs.v6addrs.dst));
818 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
819 	keys->ports.src = fl6->fl6_sport;
820 	keys->ports.dst = fl6->fl6_dport;
821 	keys->keyid.keyid = fl6->fl6_gre_key;
822 	keys->tags.flow_label = (__force u32)fl6->flowlabel;
823 	keys->basic.ip_proto = fl6->flowi6_proto;
824 
825 	return flow_hash_from_keys(keys);
826 }
827 EXPORT_SYMBOL(__get_hash_from_flowi6);
828 
829 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
830 {
831 	memset(keys, 0, sizeof(*keys));
832 
833 	keys->addrs.v4addrs.src = fl4->saddr;
834 	keys->addrs.v4addrs.dst = fl4->daddr;
835 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
836 	keys->ports.src = fl4->fl4_sport;
837 	keys->ports.dst = fl4->fl4_dport;
838 	keys->keyid.keyid = fl4->fl4_gre_key;
839 	keys->basic.ip_proto = fl4->flowi4_proto;
840 
841 	return flow_hash_from_keys(keys);
842 }
843 EXPORT_SYMBOL(__get_hash_from_flowi4);
844 
845 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
846 	{
847 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
848 		.offset = offsetof(struct flow_keys, control),
849 	},
850 	{
851 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
852 		.offset = offsetof(struct flow_keys, basic),
853 	},
854 	{
855 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
856 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
857 	},
858 	{
859 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
860 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
861 	},
862 	{
863 		.key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
864 		.offset = offsetof(struct flow_keys, addrs.tipcaddrs),
865 	},
866 	{
867 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
868 		.offset = offsetof(struct flow_keys, ports),
869 	},
870 	{
871 		.key_id = FLOW_DISSECTOR_KEY_VLANID,
872 		.offset = offsetof(struct flow_keys, tags),
873 	},
874 	{
875 		.key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
876 		.offset = offsetof(struct flow_keys, tags),
877 	},
878 	{
879 		.key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
880 		.offset = offsetof(struct flow_keys, keyid),
881 	},
882 };
883 
884 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
885 	{
886 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
887 		.offset = offsetof(struct flow_keys, control),
888 	},
889 	{
890 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
891 		.offset = offsetof(struct flow_keys, basic),
892 	},
893 };
894 
895 struct flow_dissector flow_keys_dissector __read_mostly;
896 EXPORT_SYMBOL(flow_keys_dissector);
897 
898 struct flow_dissector flow_keys_buf_dissector __read_mostly;
899 
900 static int __init init_default_flow_dissectors(void)
901 {
902 	skb_flow_dissector_init(&flow_keys_dissector,
903 				flow_keys_dissector_keys,
904 				ARRAY_SIZE(flow_keys_dissector_keys));
905 	skb_flow_dissector_init(&flow_keys_buf_dissector,
906 				flow_keys_buf_dissector_keys,
907 				ARRAY_SIZE(flow_keys_buf_dissector_keys));
908 	return 0;
909 }
910 
911 late_initcall_sync(init_default_flow_dissectors);
912