• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/dsa.h>
8 #include <net/ip.h>
9 #include <net/ipv6.h>
10 #include <net/gre.h>
11 #include <net/pptp.h>
12 #include <linux/igmp.h>
13 #include <linux/icmp.h>
14 #include <linux/sctp.h>
15 #include <linux/dccp.h>
16 #include <linux/if_tunnel.h>
17 #include <linux/if_pppox.h>
18 #include <linux/ppp_defs.h>
19 #include <linux/stddef.h>
20 #include <linux/if_ether.h>
21 #include <linux/mpls.h>
22 #include <linux/tcp.h>
23 #include <net/flow_dissector.h>
24 #include <scsi/fc/fc_fcoe.h>
25 
dissector_set_key(struct flow_dissector * flow_dissector,enum flow_dissector_key_id key_id)26 static void dissector_set_key(struct flow_dissector *flow_dissector,
27 			      enum flow_dissector_key_id key_id)
28 {
29 	flow_dissector->used_keys |= (1 << key_id);
30 }
31 
skb_flow_dissector_init(struct flow_dissector * flow_dissector,const struct flow_dissector_key * key,unsigned int key_count)32 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
33 			     const struct flow_dissector_key *key,
34 			     unsigned int key_count)
35 {
36 	unsigned int i;
37 
38 	memset(flow_dissector, 0, sizeof(*flow_dissector));
39 
40 	for (i = 0; i < key_count; i++, key++) {
41 		/* User should make sure that every key target offset is withing
42 		 * boundaries of unsigned short.
43 		 */
44 		BUG_ON(key->offset > USHRT_MAX);
45 		BUG_ON(dissector_uses_key(flow_dissector,
46 					  key->key_id));
47 
48 		dissector_set_key(flow_dissector, key->key_id);
49 		flow_dissector->offset[key->key_id] = key->offset;
50 	}
51 
52 	/* Ensure that the dissector always includes control and basic key.
53 	 * That way we are able to avoid handling lack of these in fast path.
54 	 */
55 	BUG_ON(!dissector_uses_key(flow_dissector,
56 				   FLOW_DISSECTOR_KEY_CONTROL));
57 	BUG_ON(!dissector_uses_key(flow_dissector,
58 				   FLOW_DISSECTOR_KEY_BASIC));
59 }
60 EXPORT_SYMBOL(skb_flow_dissector_init);
61 
62 /**
63  * skb_flow_get_be16 - extract be16 entity
64  * @skb: sk_buff to extract from
65  * @poff: offset to extract at
66  * @data: raw buffer pointer to the packet
67  * @hlen: packet header length
68  *
69  * The function will try to retrieve a be32 entity at
70  * offset poff
71  */
skb_flow_get_be16(const struct sk_buff * skb,int poff,void * data,int hlen)72 static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
73 				void *data, int hlen)
74 {
75 	__be16 *u, _u;
76 
77 	u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
78 	if (u)
79 		return *u;
80 
81 	return 0;
82 }
83 
84 /**
85  * __skb_flow_get_ports - extract the upper layer ports and return them
86  * @skb: sk_buff to extract the ports from
87  * @thoff: transport header offset
88  * @ip_proto: protocol for which to get port offset
89  * @data: raw buffer pointer to the packet, if NULL use skb->data
90  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
91  *
92  * The function will try to retrieve the ports at offset thoff + poff where poff
93  * is the protocol port offset returned from proto_ports_offset
94  */
__skb_flow_get_ports(const struct sk_buff * skb,int thoff,u8 ip_proto,void * data,int hlen)95 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
96 			    void *data, int hlen)
97 {
98 	int poff = proto_ports_offset(ip_proto);
99 
100 	if (!data) {
101 		data = skb->data;
102 		hlen = skb_headlen(skb);
103 	}
104 
105 	if (poff >= 0) {
106 		__be32 *ports, _ports;
107 
108 		ports = __skb_header_pointer(skb, thoff + poff,
109 					     sizeof(_ports), data, hlen, &_ports);
110 		if (ports)
111 			return *ports;
112 	}
113 
114 	return 0;
115 }
116 EXPORT_SYMBOL(__skb_flow_get_ports);
117 
118 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)119 __skb_flow_dissect_mpls(const struct sk_buff *skb,
120 			struct flow_dissector *flow_dissector,
121 			void *target_container, void *data, int nhoff, int hlen)
122 {
123 	struct flow_dissector_key_keyid *key_keyid;
124 	struct mpls_label *hdr, _hdr[2];
125 	u32 entry, label;
126 
127 	if (!dissector_uses_key(flow_dissector,
128 				FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
129 	    !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
130 		return FLOW_DISSECT_RET_OUT_GOOD;
131 
132 	hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
133 				   hlen, &_hdr);
134 	if (!hdr)
135 		return FLOW_DISSECT_RET_OUT_BAD;
136 
137 	entry = ntohl(hdr[0].entry);
138 	label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
139 
140 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
141 		struct flow_dissector_key_mpls *key_mpls;
142 
143 		key_mpls = skb_flow_dissector_target(flow_dissector,
144 						     FLOW_DISSECTOR_KEY_MPLS,
145 						     target_container);
146 		key_mpls->mpls_label = label;
147 		key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
148 					>> MPLS_LS_TTL_SHIFT;
149 		key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
150 					>> MPLS_LS_TC_SHIFT;
151 		key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
152 					>> MPLS_LS_S_SHIFT;
153 	}
154 
155 	if (label == MPLS_LABEL_ENTROPY) {
156 		key_keyid = skb_flow_dissector_target(flow_dissector,
157 						      FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
158 						      target_container);
159 		key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
160 	}
161 	return FLOW_DISSECT_RET_OUT_GOOD;
162 }
163 
164 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)165 __skb_flow_dissect_arp(const struct sk_buff *skb,
166 		       struct flow_dissector *flow_dissector,
167 		       void *target_container, void *data, int nhoff, int hlen)
168 {
169 	struct flow_dissector_key_arp *key_arp;
170 	struct {
171 		unsigned char ar_sha[ETH_ALEN];
172 		unsigned char ar_sip[4];
173 		unsigned char ar_tha[ETH_ALEN];
174 		unsigned char ar_tip[4];
175 	} *arp_eth, _arp_eth;
176 	const struct arphdr *arp;
177 	struct arphdr _arp;
178 
179 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
180 		return FLOW_DISSECT_RET_OUT_GOOD;
181 
182 	arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
183 				   hlen, &_arp);
184 	if (!arp)
185 		return FLOW_DISSECT_RET_OUT_BAD;
186 
187 	if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
188 	    arp->ar_pro != htons(ETH_P_IP) ||
189 	    arp->ar_hln != ETH_ALEN ||
190 	    arp->ar_pln != 4 ||
191 	    (arp->ar_op != htons(ARPOP_REPLY) &&
192 	     arp->ar_op != htons(ARPOP_REQUEST)))
193 		return FLOW_DISSECT_RET_OUT_BAD;
194 
195 	arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
196 				       sizeof(_arp_eth), data,
197 				       hlen, &_arp_eth);
198 	if (!arp_eth)
199 		return FLOW_DISSECT_RET_OUT_BAD;
200 
201 	key_arp = skb_flow_dissector_target(flow_dissector,
202 					    FLOW_DISSECTOR_KEY_ARP,
203 					    target_container);
204 
205 	memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
206 	memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
207 
208 	/* Only store the lower byte of the opcode;
209 	 * this covers ARPOP_REPLY and ARPOP_REQUEST.
210 	 */
211 	key_arp->op = ntohs(arp->ar_op) & 0xff;
212 
213 	ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
214 	ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
215 
216 	return FLOW_DISSECT_RET_OUT_GOOD;
217 }
218 
219 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)220 __skb_flow_dissect_gre(const struct sk_buff *skb,
221 		       struct flow_dissector_key_control *key_control,
222 		       struct flow_dissector *flow_dissector,
223 		       void *target_container, void *data,
224 		       __be16 *p_proto, int *p_nhoff, int *p_hlen,
225 		       unsigned int flags)
226 {
227 	struct flow_dissector_key_keyid *key_keyid;
228 	struct gre_base_hdr *hdr, _hdr;
229 	int offset = 0;
230 	u16 gre_ver;
231 
232 	hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
233 				   data, *p_hlen, &_hdr);
234 	if (!hdr)
235 		return FLOW_DISSECT_RET_OUT_BAD;
236 
237 	/* Only look inside GRE without routing */
238 	if (hdr->flags & GRE_ROUTING)
239 		return FLOW_DISSECT_RET_OUT_GOOD;
240 
241 	/* Only look inside GRE for version 0 and 1 */
242 	gre_ver = ntohs(hdr->flags & GRE_VERSION);
243 	if (gre_ver > 1)
244 		return FLOW_DISSECT_RET_OUT_GOOD;
245 
246 	*p_proto = hdr->protocol;
247 	if (gre_ver) {
248 		/* Version1 must be PPTP, and check the flags */
249 		if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
250 			return FLOW_DISSECT_RET_OUT_GOOD;
251 	}
252 
253 	offset += sizeof(struct gre_base_hdr);
254 
255 	if (hdr->flags & GRE_CSUM)
256 		offset += sizeof(((struct gre_full_hdr *) 0)->csum) +
257 			  sizeof(((struct gre_full_hdr *) 0)->reserved1);
258 
259 	if (hdr->flags & GRE_KEY) {
260 		const __be32 *keyid;
261 		__be32 _keyid;
262 
263 		keyid = __skb_header_pointer(skb, *p_nhoff + offset,
264 					     sizeof(_keyid),
265 					     data, *p_hlen, &_keyid);
266 		if (!keyid)
267 			return FLOW_DISSECT_RET_OUT_BAD;
268 
269 		if (dissector_uses_key(flow_dissector,
270 				       FLOW_DISSECTOR_KEY_GRE_KEYID)) {
271 			key_keyid = skb_flow_dissector_target(flow_dissector,
272 							      FLOW_DISSECTOR_KEY_GRE_KEYID,
273 							      target_container);
274 			if (gre_ver == 0)
275 				key_keyid->keyid = *keyid;
276 			else
277 				key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
278 		}
279 		offset += sizeof(((struct gre_full_hdr *) 0)->key);
280 	}
281 
282 	if (hdr->flags & GRE_SEQ)
283 		offset += sizeof(((struct pptp_gre_header *) 0)->seq);
284 
285 	if (gre_ver == 0) {
286 		if (*p_proto == htons(ETH_P_TEB)) {
287 			const struct ethhdr *eth;
288 			struct ethhdr _eth;
289 
290 			eth = __skb_header_pointer(skb, *p_nhoff + offset,
291 						   sizeof(_eth),
292 						   data, *p_hlen, &_eth);
293 			if (!eth)
294 				return FLOW_DISSECT_RET_OUT_BAD;
295 			*p_proto = eth->h_proto;
296 			offset += sizeof(*eth);
297 
298 			/* Cap headers that we access via pointers at the
299 			 * end of the Ethernet header as our maximum alignment
300 			 * at that point is only 2 bytes.
301 			 */
302 			if (NET_IP_ALIGN)
303 				*p_hlen = *p_nhoff + offset;
304 		}
305 	} else { /* version 1, must be PPTP */
306 		u8 _ppp_hdr[PPP_HDRLEN];
307 		u8 *ppp_hdr;
308 
309 		if (hdr->flags & GRE_ACK)
310 			offset += sizeof(((struct pptp_gre_header *) 0)->ack);
311 
312 		ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
313 					       sizeof(_ppp_hdr),
314 					       data, *p_hlen, _ppp_hdr);
315 		if (!ppp_hdr)
316 			return FLOW_DISSECT_RET_OUT_BAD;
317 
318 		switch (PPP_PROTOCOL(ppp_hdr)) {
319 		case PPP_IP:
320 			*p_proto = htons(ETH_P_IP);
321 			break;
322 		case PPP_IPV6:
323 			*p_proto = htons(ETH_P_IPV6);
324 			break;
325 		default:
326 			/* Could probably catch some more like MPLS */
327 			break;
328 		}
329 
330 		offset += PPP_HDRLEN;
331 	}
332 
333 	*p_nhoff += offset;
334 	key_control->flags |= FLOW_DIS_ENCAPSULATION;
335 	if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
336 		return FLOW_DISSECT_RET_OUT_GOOD;
337 
338 	return FLOW_DISSECT_RET_PROTO_AGAIN;
339 }
340 
341 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)342 __skb_flow_dissect_tcp(const struct sk_buff *skb,
343 		       struct flow_dissector *flow_dissector,
344 		       void *target_container, void *data, int thoff, int hlen)
345 {
346 	struct flow_dissector_key_tcp *key_tcp;
347 	struct tcphdr *th, _th;
348 
349 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
350 		return;
351 
352 	th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
353 	if (!th)
354 		return;
355 
356 	if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
357 		return;
358 
359 	key_tcp = skb_flow_dissector_target(flow_dissector,
360 					    FLOW_DISSECTOR_KEY_TCP,
361 					    target_container);
362 	key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
363 }
364 
365 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)366 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
367 			struct flow_dissector *flow_dissector,
368 			void *target_container, void *data, const struct iphdr *iph)
369 {
370 	struct flow_dissector_key_ip *key_ip;
371 
372 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
373 		return;
374 
375 	key_ip = skb_flow_dissector_target(flow_dissector,
376 					   FLOW_DISSECTOR_KEY_IP,
377 					   target_container);
378 	key_ip->tos = iph->tos;
379 	key_ip->ttl = iph->ttl;
380 }
381 
382 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)383 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
384 			struct flow_dissector *flow_dissector,
385 			void *target_container, void *data, const struct ipv6hdr *iph)
386 {
387 	struct flow_dissector_key_ip *key_ip;
388 
389 	if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
390 		return;
391 
392 	key_ip = skb_flow_dissector_target(flow_dissector,
393 					   FLOW_DISSECTOR_KEY_IP,
394 					   target_container);
395 	key_ip->tos = ipv6_get_dsfield(iph);
396 	key_ip->ttl = iph->hop_limit;
397 }
398 
399 /* Maximum number of protocol headers that can be parsed in
400  * __skb_flow_dissect
401  */
402 #define MAX_FLOW_DISSECT_HDRS	15
403 
skb_flow_dissect_allowed(int * num_hdrs)404 static bool skb_flow_dissect_allowed(int *num_hdrs)
405 {
406 	++*num_hdrs;
407 
408 	return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
409 }
410 
411 /**
412  * __skb_flow_dissect - extract the flow_keys struct and return it
413  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
414  * @flow_dissector: list of keys to dissect
415  * @target_container: target structure to put dissected values into
416  * @data: raw buffer pointer to the packet, if NULL use skb->data
417  * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
418  * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
419  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
420  *
421  * The function will try to retrieve individual keys into target specified
422  * by flow_dissector from either the skbuff or a raw buffer specified by the
423  * rest parameters.
424  *
425  * Caller must take care of zeroing target container memory.
426  */
__skb_flow_dissect(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,void * data,__be16 proto,int nhoff,int hlen,unsigned int flags)427 bool __skb_flow_dissect(const struct sk_buff *skb,
428 			struct flow_dissector *flow_dissector,
429 			void *target_container,
430 			void *data, __be16 proto, int nhoff, int hlen,
431 			unsigned int flags)
432 {
433 	struct flow_dissector_key_control *key_control;
434 	struct flow_dissector_key_basic *key_basic;
435 	struct flow_dissector_key_addrs *key_addrs;
436 	struct flow_dissector_key_ports *key_ports;
437 	struct flow_dissector_key_icmp *key_icmp;
438 	struct flow_dissector_key_tags *key_tags;
439 	struct flow_dissector_key_vlan *key_vlan;
440 	enum flow_dissect_ret fdret;
441 	bool skip_vlan = false;
442 	int num_hdrs = 0;
443 	u8 ip_proto = 0;
444 	bool ret;
445 
446 	if (!data) {
447 		data = skb->data;
448 		proto = skb_vlan_tag_present(skb) ?
449 			 skb->vlan_proto : skb->protocol;
450 		nhoff = skb_network_offset(skb);
451 		hlen = skb_headlen(skb);
452 #if IS_ENABLED(CONFIG_NET_DSA)
453 		if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
454 			     proto == htons(ETH_P_XDSA))) {
455 			const struct dsa_device_ops *ops;
456 			int offset = 0;
457 
458 			ops = skb->dev->dsa_ptr->tag_ops;
459 			if (ops->flow_dissect &&
460 			    !ops->flow_dissect(skb, &proto, &offset)) {
461 				hlen -= offset;
462 				nhoff += offset;
463 			}
464 		}
465 #endif
466 	}
467 
468 	/* It is ensured by skb_flow_dissector_init() that control key will
469 	 * be always present.
470 	 */
471 	key_control = skb_flow_dissector_target(flow_dissector,
472 						FLOW_DISSECTOR_KEY_CONTROL,
473 						target_container);
474 
475 	/* It is ensured by skb_flow_dissector_init() that basic key will
476 	 * be always present.
477 	 */
478 	key_basic = skb_flow_dissector_target(flow_dissector,
479 					      FLOW_DISSECTOR_KEY_BASIC,
480 					      target_container);
481 
482 	if (dissector_uses_key(flow_dissector,
483 			       FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
484 		struct ethhdr *eth = eth_hdr(skb);
485 		struct flow_dissector_key_eth_addrs *key_eth_addrs;
486 
487 		key_eth_addrs = skb_flow_dissector_target(flow_dissector,
488 							  FLOW_DISSECTOR_KEY_ETH_ADDRS,
489 							  target_container);
490 		memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
491 	}
492 
493 proto_again:
494 	fdret = FLOW_DISSECT_RET_CONTINUE;
495 
496 	switch (proto) {
497 	case htons(ETH_P_IP): {
498 		const struct iphdr *iph;
499 		struct iphdr _iph;
500 
501 		iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
502 		if (!iph || iph->ihl < 5) {
503 			fdret = FLOW_DISSECT_RET_OUT_BAD;
504 			break;
505 		}
506 
507 		nhoff += iph->ihl * 4;
508 
509 		ip_proto = iph->protocol;
510 
511 		if (dissector_uses_key(flow_dissector,
512 				       FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
513 			key_addrs = skb_flow_dissector_target(flow_dissector,
514 							      FLOW_DISSECTOR_KEY_IPV4_ADDRS,
515 							      target_container);
516 
517 			memcpy(&key_addrs->v4addrs, &iph->saddr,
518 			       sizeof(key_addrs->v4addrs));
519 			key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
520 		}
521 
522 		if (ip_is_fragment(iph)) {
523 			key_control->flags |= FLOW_DIS_IS_FRAGMENT;
524 
525 			if (iph->frag_off & htons(IP_OFFSET)) {
526 				fdret = FLOW_DISSECT_RET_OUT_GOOD;
527 				break;
528 			} else {
529 				key_control->flags |= FLOW_DIS_FIRST_FRAG;
530 				if (!(flags &
531 				      FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
532 					fdret = FLOW_DISSECT_RET_OUT_GOOD;
533 					break;
534 				}
535 			}
536 		}
537 
538 		__skb_flow_dissect_ipv4(skb, flow_dissector,
539 					target_container, data, iph);
540 
541 		if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) {
542 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
543 			break;
544 		}
545 
546 		break;
547 	}
548 	case htons(ETH_P_IPV6): {
549 		const struct ipv6hdr *iph;
550 		struct ipv6hdr _iph;
551 
552 		iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
553 		if (!iph) {
554 			fdret = FLOW_DISSECT_RET_OUT_BAD;
555 			break;
556 		}
557 
558 		ip_proto = iph->nexthdr;
559 		nhoff += sizeof(struct ipv6hdr);
560 
561 		if (dissector_uses_key(flow_dissector,
562 				       FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
563 			key_addrs = skb_flow_dissector_target(flow_dissector,
564 							      FLOW_DISSECTOR_KEY_IPV6_ADDRS,
565 							      target_container);
566 
567 			memcpy(&key_addrs->v6addrs, &iph->saddr,
568 			       sizeof(key_addrs->v6addrs));
569 			key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
570 		}
571 
572 		if ((dissector_uses_key(flow_dissector,
573 					FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
574 		     (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
575 		    ip6_flowlabel(iph)) {
576 			__be32 flow_label = ip6_flowlabel(iph);
577 
578 			if (dissector_uses_key(flow_dissector,
579 					       FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
580 				key_tags = skb_flow_dissector_target(flow_dissector,
581 								     FLOW_DISSECTOR_KEY_FLOW_LABEL,
582 								     target_container);
583 				key_tags->flow_label = ntohl(flow_label);
584 			}
585 			if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
586 				fdret = FLOW_DISSECT_RET_OUT_GOOD;
587 				break;
588 			}
589 		}
590 
591 		__skb_flow_dissect_ipv6(skb, flow_dissector,
592 					target_container, data, iph);
593 
594 		if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
595 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
596 
597 		break;
598 	}
599 	case htons(ETH_P_8021AD):
600 	case htons(ETH_P_8021Q): {
601 		const struct vlan_hdr *vlan;
602 		struct vlan_hdr _vlan;
603 		bool vlan_tag_present = skb && skb_vlan_tag_present(skb);
604 
605 		if (vlan_tag_present)
606 			proto = skb->protocol;
607 
608 		if (!vlan_tag_present || eth_type_vlan(skb->protocol)) {
609 			vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
610 						    data, hlen, &_vlan);
611 			if (!vlan) {
612 				fdret = FLOW_DISSECT_RET_OUT_BAD;
613 				break;
614 			}
615 
616 			proto = vlan->h_vlan_encapsulated_proto;
617 			nhoff += sizeof(*vlan);
618 			if (skip_vlan) {
619 				fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
620 				break;
621 			}
622 		}
623 
624 		skip_vlan = true;
625 		if (dissector_uses_key(flow_dissector,
626 				       FLOW_DISSECTOR_KEY_VLAN)) {
627 			key_vlan = skb_flow_dissector_target(flow_dissector,
628 							     FLOW_DISSECTOR_KEY_VLAN,
629 							     target_container);
630 
631 			if (vlan_tag_present) {
632 				key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
633 				key_vlan->vlan_priority =
634 					(skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
635 			} else {
636 				key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
637 					VLAN_VID_MASK;
638 				key_vlan->vlan_priority =
639 					(ntohs(vlan->h_vlan_TCI) &
640 					 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
641 			}
642 		}
643 
644 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
645 		break;
646 	}
647 	case htons(ETH_P_PPP_SES): {
648 		struct {
649 			struct pppoe_hdr hdr;
650 			__be16 proto;
651 		} *hdr, _hdr;
652 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
653 		if (!hdr) {
654 			fdret = FLOW_DISSECT_RET_OUT_BAD;
655 			break;
656 		}
657 
658 		proto = hdr->proto;
659 		nhoff += PPPOE_SES_HLEN;
660 		switch (proto) {
661 		case htons(PPP_IP):
662 			proto = htons(ETH_P_IP);
663 			fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
664 			break;
665 		case htons(PPP_IPV6):
666 			proto = htons(ETH_P_IPV6);
667 			fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
668 			break;
669 		default:
670 			fdret = FLOW_DISSECT_RET_OUT_BAD;
671 			break;
672 		}
673 		break;
674 	}
675 	case htons(ETH_P_TIPC): {
676 		struct {
677 			__be32 pre[3];
678 			__be32 srcnode;
679 		} *hdr, _hdr;
680 		hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
681 		if (!hdr) {
682 			fdret = FLOW_DISSECT_RET_OUT_BAD;
683 			break;
684 		}
685 
686 		if (dissector_uses_key(flow_dissector,
687 				       FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
688 			key_addrs = skb_flow_dissector_target(flow_dissector,
689 							      FLOW_DISSECTOR_KEY_TIPC_ADDRS,
690 							      target_container);
691 			key_addrs->tipcaddrs.srcnode = hdr->srcnode;
692 			key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
693 		}
694 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
695 		break;
696 	}
697 
698 	case htons(ETH_P_MPLS_UC):
699 	case htons(ETH_P_MPLS_MC):
700 		fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
701 						target_container, data,
702 						nhoff, hlen);
703 		break;
704 	case htons(ETH_P_FCOE):
705 		if ((hlen - nhoff) < FCOE_HEADER_LEN) {
706 			fdret = FLOW_DISSECT_RET_OUT_BAD;
707 			break;
708 		}
709 
710 		nhoff += FCOE_HEADER_LEN;
711 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
712 		break;
713 
714 	case htons(ETH_P_ARP):
715 	case htons(ETH_P_RARP):
716 		fdret = __skb_flow_dissect_arp(skb, flow_dissector,
717 					       target_container, data,
718 					       nhoff, hlen);
719 		break;
720 
721 	default:
722 		fdret = FLOW_DISSECT_RET_OUT_BAD;
723 		break;
724 	}
725 
726 	/* Process result of proto processing */
727 	switch (fdret) {
728 	case FLOW_DISSECT_RET_OUT_GOOD:
729 		goto out_good;
730 	case FLOW_DISSECT_RET_PROTO_AGAIN:
731 		if (skb_flow_dissect_allowed(&num_hdrs))
732 			goto proto_again;
733 		goto out_good;
734 	case FLOW_DISSECT_RET_CONTINUE:
735 	case FLOW_DISSECT_RET_IPPROTO_AGAIN:
736 		break;
737 	case FLOW_DISSECT_RET_OUT_BAD:
738 	default:
739 		goto out_bad;
740 	}
741 
742 ip_proto_again:
743 	fdret = FLOW_DISSECT_RET_CONTINUE;
744 
745 	switch (ip_proto) {
746 	case IPPROTO_GRE:
747 		fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
748 					       target_container, data,
749 					       &proto, &nhoff, &hlen, flags);
750 		break;
751 
752 	case NEXTHDR_HOP:
753 	case NEXTHDR_ROUTING:
754 	case NEXTHDR_DEST: {
755 		u8 _opthdr[2], *opthdr;
756 
757 		if (proto != htons(ETH_P_IPV6))
758 			break;
759 
760 		opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
761 					      data, hlen, &_opthdr);
762 		if (!opthdr) {
763 			fdret = FLOW_DISSECT_RET_OUT_BAD;
764 			break;
765 		}
766 
767 		ip_proto = opthdr[0];
768 		nhoff += (opthdr[1] + 1) << 3;
769 
770 		fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
771 		break;
772 	}
773 	case NEXTHDR_FRAGMENT: {
774 		struct frag_hdr _fh, *fh;
775 
776 		if (proto != htons(ETH_P_IPV6))
777 			break;
778 
779 		fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
780 					  data, hlen, &_fh);
781 
782 		if (!fh) {
783 			fdret = FLOW_DISSECT_RET_OUT_BAD;
784 			break;
785 		}
786 
787 		key_control->flags |= FLOW_DIS_IS_FRAGMENT;
788 
789 		nhoff += sizeof(_fh);
790 		ip_proto = fh->nexthdr;
791 
792 		if (!(fh->frag_off & htons(IP6_OFFSET))) {
793 			key_control->flags |= FLOW_DIS_FIRST_FRAG;
794 			if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
795 				fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
796 				break;
797 			}
798 		}
799 
800 		fdret = FLOW_DISSECT_RET_OUT_GOOD;
801 		break;
802 	}
803 	case IPPROTO_IPIP:
804 		proto = htons(ETH_P_IP);
805 
806 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
807 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
808 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
809 			break;
810 		}
811 
812 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
813 		break;
814 
815 	case IPPROTO_IPV6:
816 		proto = htons(ETH_P_IPV6);
817 
818 		key_control->flags |= FLOW_DIS_ENCAPSULATION;
819 		if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
820 			fdret = FLOW_DISSECT_RET_OUT_GOOD;
821 			break;
822 		}
823 
824 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
825 		break;
826 
827 
828 	case IPPROTO_MPLS:
829 		proto = htons(ETH_P_MPLS_UC);
830 		fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
831 		break;
832 
833 	case IPPROTO_TCP:
834 		__skb_flow_dissect_tcp(skb, flow_dissector, target_container,
835 				       data, nhoff, hlen);
836 		break;
837 
838 	default:
839 		break;
840 	}
841 
842 	if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS) &&
843 	    !(key_control->flags & FLOW_DIS_IS_FRAGMENT)) {
844 		key_ports = skb_flow_dissector_target(flow_dissector,
845 						      FLOW_DISSECTOR_KEY_PORTS,
846 						      target_container);
847 		key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
848 							data, hlen);
849 	}
850 
851 	if (dissector_uses_key(flow_dissector,
852 			       FLOW_DISSECTOR_KEY_ICMP)) {
853 		key_icmp = skb_flow_dissector_target(flow_dissector,
854 						     FLOW_DISSECTOR_KEY_ICMP,
855 						     target_container);
856 		key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen);
857 	}
858 
859 	/* Process result of IP proto processing */
860 	switch (fdret) {
861 	case FLOW_DISSECT_RET_PROTO_AGAIN:
862 		if (skb_flow_dissect_allowed(&num_hdrs))
863 			goto proto_again;
864 		break;
865 	case FLOW_DISSECT_RET_IPPROTO_AGAIN:
866 		if (skb_flow_dissect_allowed(&num_hdrs))
867 			goto ip_proto_again;
868 		break;
869 	case FLOW_DISSECT_RET_OUT_GOOD:
870 	case FLOW_DISSECT_RET_CONTINUE:
871 		break;
872 	case FLOW_DISSECT_RET_OUT_BAD:
873 	default:
874 		goto out_bad;
875 	}
876 
877 out_good:
878 	ret = true;
879 
880 out:
881 	key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
882 	key_basic->n_proto = proto;
883 	key_basic->ip_proto = ip_proto;
884 
885 	return ret;
886 
887 out_bad:
888 	ret = false;
889 	goto out;
890 }
891 EXPORT_SYMBOL(__skb_flow_dissect);
892 
893 static siphash_key_t hashrnd __read_mostly;
__flow_hash_secret_init(void)894 static __always_inline void __flow_hash_secret_init(void)
895 {
896 	net_get_random_once(&hashrnd, sizeof(hashrnd));
897 }
898 
flow_keys_hash_start(const struct flow_keys * flow)899 static const void *flow_keys_hash_start(const struct flow_keys *flow)
900 {
901 	BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
902 	return &flow->FLOW_KEYS_HASH_START_FIELD;
903 }
904 
flow_keys_hash_length(const struct flow_keys * flow)905 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
906 {
907 	size_t len = offsetof(typeof(*flow), addrs) - FLOW_KEYS_HASH_OFFSET;
908 
909 	switch (flow->control.addr_type) {
910 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
911 		len += sizeof(flow->addrs.v4addrs);
912 		break;
913 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
914 		len += sizeof(flow->addrs.v6addrs);
915 		break;
916 	case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
917 		len += sizeof(flow->addrs.tipcaddrs);
918 		break;
919 	}
920 	return len;
921 }
922 
flow_get_u32_src(const struct flow_keys * flow)923 __be32 flow_get_u32_src(const struct flow_keys *flow)
924 {
925 	switch (flow->control.addr_type) {
926 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
927 		return flow->addrs.v4addrs.src;
928 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
929 		return (__force __be32)ipv6_addr_hash(
930 			&flow->addrs.v6addrs.src);
931 	case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
932 		return flow->addrs.tipcaddrs.srcnode;
933 	default:
934 		return 0;
935 	}
936 }
937 EXPORT_SYMBOL(flow_get_u32_src);
938 
flow_get_u32_dst(const struct flow_keys * flow)939 __be32 flow_get_u32_dst(const struct flow_keys *flow)
940 {
941 	switch (flow->control.addr_type) {
942 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
943 		return flow->addrs.v4addrs.dst;
944 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
945 		return (__force __be32)ipv6_addr_hash(
946 			&flow->addrs.v6addrs.dst);
947 	default:
948 		return 0;
949 	}
950 }
951 EXPORT_SYMBOL(flow_get_u32_dst);
952 
__flow_hash_consistentify(struct flow_keys * keys)953 static inline void __flow_hash_consistentify(struct flow_keys *keys)
954 {
955 	int addr_diff, i;
956 
957 	switch (keys->control.addr_type) {
958 	case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
959 		addr_diff = (__force u32)keys->addrs.v4addrs.dst -
960 			    (__force u32)keys->addrs.v4addrs.src;
961 		if ((addr_diff < 0) ||
962 		    (addr_diff == 0 &&
963 		     ((__force u16)keys->ports.dst <
964 		      (__force u16)keys->ports.src))) {
965 			swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
966 			swap(keys->ports.src, keys->ports.dst);
967 		}
968 		break;
969 	case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
970 		addr_diff = memcmp(&keys->addrs.v6addrs.dst,
971 				   &keys->addrs.v6addrs.src,
972 				   sizeof(keys->addrs.v6addrs.dst));
973 		if ((addr_diff < 0) ||
974 		    (addr_diff == 0 &&
975 		     ((__force u16)keys->ports.dst <
976 		      (__force u16)keys->ports.src))) {
977 			for (i = 0; i < 4; i++)
978 				swap(keys->addrs.v6addrs.src.s6_addr32[i],
979 				     keys->addrs.v6addrs.dst.s6_addr32[i]);
980 			swap(keys->ports.src, keys->ports.dst);
981 		}
982 		break;
983 	}
984 }
985 
__flow_hash_from_keys(struct flow_keys * keys,const siphash_key_t * keyval)986 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
987 					const siphash_key_t *keyval)
988 {
989 	u32 hash;
990 
991 	__flow_hash_consistentify(keys);
992 
993 	hash = siphash(flow_keys_hash_start(keys),
994 		       flow_keys_hash_length(keys), keyval);
995 	if (!hash)
996 		hash = 1;
997 
998 	return hash;
999 }
1000 
flow_hash_from_keys(struct flow_keys * keys)1001 u32 flow_hash_from_keys(struct flow_keys *keys)
1002 {
1003 	__flow_hash_secret_init();
1004 	return __flow_hash_from_keys(keys, &hashrnd);
1005 }
1006 EXPORT_SYMBOL(flow_hash_from_keys);
1007 
___skb_get_hash(const struct sk_buff * skb,struct flow_keys * keys,const siphash_key_t * keyval)1008 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1009 				  struct flow_keys *keys,
1010 				  const siphash_key_t *keyval)
1011 {
1012 	skb_flow_dissect_flow_keys(skb, keys,
1013 				   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1014 
1015 	return __flow_hash_from_keys(keys, keyval);
1016 }
1017 
1018 struct _flow_keys_digest_data {
1019 	__be16	n_proto;
1020 	u8	ip_proto;
1021 	u8	padding;
1022 	__be32	ports;
1023 	__be32	src;
1024 	__be32	dst;
1025 };
1026 
make_flow_keys_digest(struct flow_keys_digest * digest,const struct flow_keys * flow)1027 void make_flow_keys_digest(struct flow_keys_digest *digest,
1028 			   const struct flow_keys *flow)
1029 {
1030 	struct _flow_keys_digest_data *data =
1031 	    (struct _flow_keys_digest_data *)digest;
1032 
1033 	BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1034 
1035 	memset(digest, 0, sizeof(*digest));
1036 
1037 	data->n_proto = flow->basic.n_proto;
1038 	data->ip_proto = flow->basic.ip_proto;
1039 	data->ports = flow->ports.ports;
1040 	data->src = flow->addrs.v4addrs.src;
1041 	data->dst = flow->addrs.v4addrs.dst;
1042 }
1043 EXPORT_SYMBOL(make_flow_keys_digest);
1044 
1045 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1046 
__skb_get_hash_symmetric(const struct sk_buff * skb)1047 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1048 {
1049 	struct flow_keys keys;
1050 
1051 	__flow_hash_secret_init();
1052 
1053 	memset(&keys, 0, sizeof(keys));
1054 	__skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
1055 			   NULL, 0, 0, 0,
1056 			   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1057 
1058 	return __flow_hash_from_keys(&keys, &hashrnd);
1059 }
1060 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1061 
1062 /**
1063  * __skb_get_hash: calculate a flow hash
1064  * @skb: sk_buff to calculate flow hash from
1065  *
1066  * This function calculates a flow hash based on src/dst addresses
1067  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
1068  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
1069  * if hash is a canonical 4-tuple hash over transport ports.
1070  */
__skb_get_hash(struct sk_buff * skb)1071 void __skb_get_hash(struct sk_buff *skb)
1072 {
1073 	struct flow_keys keys;
1074 	u32 hash;
1075 
1076 	__flow_hash_secret_init();
1077 
1078 	hash = ___skb_get_hash(skb, &keys, &hashrnd);
1079 
1080 	__skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1081 }
1082 EXPORT_SYMBOL(__skb_get_hash);
1083 
skb_get_hash_perturb(const struct sk_buff * skb,const siphash_key_t * perturb)1084 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1085 			   const siphash_key_t *perturb)
1086 {
1087 	struct flow_keys keys;
1088 
1089 	return ___skb_get_hash(skb, &keys, perturb);
1090 }
1091 EXPORT_SYMBOL(skb_get_hash_perturb);
1092 
__skb_get_poff(const struct sk_buff * skb,void * data,const struct flow_keys * keys,int hlen)1093 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1094 		   const struct flow_keys *keys, int hlen)
1095 {
1096 	u32 poff = keys->control.thoff;
1097 
1098 	/* skip L4 headers for fragments after the first */
1099 	if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1100 	    !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1101 		return poff;
1102 
1103 	switch (keys->basic.ip_proto) {
1104 	case IPPROTO_TCP: {
1105 		/* access doff as u8 to avoid unaligned access */
1106 		const u8 *doff;
1107 		u8 _doff;
1108 
1109 		doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1110 					    data, hlen, &_doff);
1111 		if (!doff)
1112 			return poff;
1113 
1114 		poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1115 		break;
1116 	}
1117 	case IPPROTO_UDP:
1118 	case IPPROTO_UDPLITE:
1119 		poff += sizeof(struct udphdr);
1120 		break;
1121 	/* For the rest, we do not really care about header
1122 	 * extensions at this point for now.
1123 	 */
1124 	case IPPROTO_ICMP:
1125 		poff += sizeof(struct icmphdr);
1126 		break;
1127 	case IPPROTO_ICMPV6:
1128 		poff += sizeof(struct icmp6hdr);
1129 		break;
1130 	case IPPROTO_IGMP:
1131 		poff += sizeof(struct igmphdr);
1132 		break;
1133 	case IPPROTO_DCCP:
1134 		poff += sizeof(struct dccp_hdr);
1135 		break;
1136 	case IPPROTO_SCTP:
1137 		poff += sizeof(struct sctphdr);
1138 		break;
1139 	}
1140 
1141 	return poff;
1142 }
1143 
1144 /**
1145  * skb_get_poff - get the offset to the payload
1146  * @skb: sk_buff to get the payload offset from
1147  *
1148  * The function will get the offset to the payload as far as it could
1149  * be dissected.  The main user is currently BPF, so that we can dynamically
1150  * truncate packets without needing to push actual payload to the user
1151  * space and can analyze headers only, instead.
1152  */
skb_get_poff(const struct sk_buff * skb)1153 u32 skb_get_poff(const struct sk_buff *skb)
1154 {
1155 	struct flow_keys keys;
1156 
1157 	if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
1158 		return 0;
1159 
1160 	return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1161 }
1162 
__get_hash_from_flowi6(const struct flowi6 * fl6,struct flow_keys * keys)1163 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1164 {
1165 	memset(keys, 0, sizeof(*keys));
1166 
1167 	memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1168 	    sizeof(keys->addrs.v6addrs.src));
1169 	memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1170 	    sizeof(keys->addrs.v6addrs.dst));
1171 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1172 	keys->ports.src = fl6->fl6_sport;
1173 	keys->ports.dst = fl6->fl6_dport;
1174 	keys->keyid.keyid = fl6->fl6_gre_key;
1175 	keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1176 	keys->basic.ip_proto = fl6->flowi6_proto;
1177 
1178 	return flow_hash_from_keys(keys);
1179 }
1180 EXPORT_SYMBOL(__get_hash_from_flowi6);
1181 
__get_hash_from_flowi4(const struct flowi4 * fl4,struct flow_keys * keys)1182 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
1183 {
1184 	memset(keys, 0, sizeof(*keys));
1185 
1186 	keys->addrs.v4addrs.src = fl4->saddr;
1187 	keys->addrs.v4addrs.dst = fl4->daddr;
1188 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1189 	keys->ports.src = fl4->fl4_sport;
1190 	keys->ports.dst = fl4->fl4_dport;
1191 	keys->keyid.keyid = fl4->fl4_gre_key;
1192 	keys->basic.ip_proto = fl4->flowi4_proto;
1193 
1194 	return flow_hash_from_keys(keys);
1195 }
1196 EXPORT_SYMBOL(__get_hash_from_flowi4);
1197 
1198 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1199 	{
1200 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1201 		.offset = offsetof(struct flow_keys, control),
1202 	},
1203 	{
1204 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1205 		.offset = offsetof(struct flow_keys, basic),
1206 	},
1207 	{
1208 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1209 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
1210 	},
1211 	{
1212 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1213 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
1214 	},
1215 	{
1216 		.key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
1217 		.offset = offsetof(struct flow_keys, addrs.tipcaddrs),
1218 	},
1219 	{
1220 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
1221 		.offset = offsetof(struct flow_keys, ports),
1222 	},
1223 	{
1224 		.key_id = FLOW_DISSECTOR_KEY_VLAN,
1225 		.offset = offsetof(struct flow_keys, vlan),
1226 	},
1227 	{
1228 		.key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1229 		.offset = offsetof(struct flow_keys, tags),
1230 	},
1231 	{
1232 		.key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1233 		.offset = offsetof(struct flow_keys, keyid),
1234 	},
1235 };
1236 
1237 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1238 	{
1239 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1240 		.offset = offsetof(struct flow_keys, control),
1241 	},
1242 	{
1243 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1244 		.offset = offsetof(struct flow_keys, basic),
1245 	},
1246 	{
1247 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1248 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
1249 	},
1250 	{
1251 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1252 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
1253 	},
1254 	{
1255 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
1256 		.offset = offsetof(struct flow_keys, ports),
1257 	},
1258 };
1259 
1260 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
1261 	{
1262 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
1263 		.offset = offsetof(struct flow_keys, control),
1264 	},
1265 	{
1266 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
1267 		.offset = offsetof(struct flow_keys, basic),
1268 	},
1269 };
1270 
1271 struct flow_dissector flow_keys_dissector __read_mostly;
1272 EXPORT_SYMBOL(flow_keys_dissector);
1273 
1274 struct flow_dissector flow_keys_buf_dissector __read_mostly;
1275 
init_default_flow_dissectors(void)1276 static int __init init_default_flow_dissectors(void)
1277 {
1278 	skb_flow_dissector_init(&flow_keys_dissector,
1279 				flow_keys_dissector_keys,
1280 				ARRAY_SIZE(flow_keys_dissector_keys));
1281 	skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1282 				flow_keys_dissector_symmetric_keys,
1283 				ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1284 	skb_flow_dissector_init(&flow_keys_buf_dissector,
1285 				flow_keys_buf_dissector_keys,
1286 				ARRAY_SIZE(flow_keys_buf_dissector_keys));
1287 	return 0;
1288 }
1289 
1290 core_initcall(init_default_flow_dissectors);
1291