Merge 4.4.85 into android-4.4
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / net / core / flow_dissector.c
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;
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 break;
184
185 key_addrs = skb_flow_dissector_target(flow_dissector,
186 FLOW_DISSECTOR_KEY_IPV4_ADDRS, target_container);
187 memcpy(&key_addrs->v4addrs, &iph->saddr,
188 sizeof(key_addrs->v4addrs));
189 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
190
191 if (ip_is_fragment(iph)) {
192 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
193
194 if (iph->frag_off & htons(IP_OFFSET)) {
195 goto out_good;
196 } else {
197 key_control->flags |= FLOW_DIS_FIRST_FRAG;
198 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
199 goto out_good;
200 }
201 }
202
203 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
204 goto out_good;
205
206 break;
207 }
208 case htons(ETH_P_IPV6): {
209 const struct ipv6hdr *iph;
210 struct ipv6hdr _iph;
211
212 ipv6:
213 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
214 if (!iph)
215 goto out_bad;
216
217 ip_proto = iph->nexthdr;
218 nhoff += sizeof(struct ipv6hdr);
219
220 if (dissector_uses_key(flow_dissector,
221 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
222 struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs;
223
224 key_ipv6_addrs = skb_flow_dissector_target(flow_dissector,
225 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
226 target_container);
227
228 memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs));
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 key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
343 /* fall through */
344 default:
345 goto out_bad;
346 }
347
348 ip_proto_again:
349 switch (ip_proto) {
350 case IPPROTO_GRE: {
351 struct gre_hdr {
352 __be16 flags;
353 __be16 proto;
354 } *hdr, _hdr;
355
356 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
357 if (!hdr)
358 goto out_bad;
359 /*
360 * Only look inside GRE if version zero and no
361 * routing
362 */
363 if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
364 break;
365
366 proto = hdr->proto;
367 nhoff += 4;
368 if (hdr->flags & GRE_CSUM)
369 nhoff += 4;
370 if (hdr->flags & GRE_KEY) {
371 const __be32 *keyid;
372 __be32 _keyid;
373
374 keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
375 data, hlen, &_keyid);
376
377 if (!keyid)
378 goto out_bad;
379
380 if (dissector_uses_key(flow_dissector,
381 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
382 key_keyid = skb_flow_dissector_target(flow_dissector,
383 FLOW_DISSECTOR_KEY_GRE_KEYID,
384 target_container);
385 key_keyid->keyid = *keyid;
386 }
387 nhoff += 4;
388 }
389 if (hdr->flags & GRE_SEQ)
390 nhoff += 4;
391 if (proto == htons(ETH_P_TEB)) {
392 const struct ethhdr *eth;
393 struct ethhdr _eth;
394
395 eth = __skb_header_pointer(skb, nhoff,
396 sizeof(_eth),
397 data, hlen, &_eth);
398 if (!eth)
399 goto out_bad;
400 proto = eth->h_proto;
401 nhoff += sizeof(*eth);
402
403 /* Cap headers that we access via pointers at the
404 * end of the Ethernet header as our maximum alignment
405 * at that point is only 2 bytes.
406 */
407 if (NET_IP_ALIGN)
408 hlen = nhoff;
409 }
410
411 key_control->flags |= FLOW_DIS_ENCAPSULATION;
412 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
413 goto out_good;
414
415 goto again;
416 }
417 case NEXTHDR_HOP:
418 case NEXTHDR_ROUTING:
419 case NEXTHDR_DEST: {
420 u8 _opthdr[2], *opthdr;
421
422 if (proto != htons(ETH_P_IPV6))
423 break;
424
425 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
426 data, hlen, &_opthdr);
427 if (!opthdr)
428 goto out_bad;
429
430 ip_proto = opthdr[0];
431 nhoff += (opthdr[1] + 1) << 3;
432
433 goto ip_proto_again;
434 }
435 case NEXTHDR_FRAGMENT: {
436 struct frag_hdr _fh, *fh;
437
438 if (proto != htons(ETH_P_IPV6))
439 break;
440
441 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
442 data, hlen, &_fh);
443
444 if (!fh)
445 goto out_bad;
446
447 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
448
449 nhoff += sizeof(_fh);
450
451 if (!(fh->frag_off & htons(IP6_OFFSET))) {
452 key_control->flags |= FLOW_DIS_FIRST_FRAG;
453 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
454 ip_proto = fh->nexthdr;
455 goto ip_proto_again;
456 }
457 }
458 goto out_good;
459 }
460 case IPPROTO_IPIP:
461 proto = htons(ETH_P_IP);
462
463 key_control->flags |= FLOW_DIS_ENCAPSULATION;
464 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
465 goto out_good;
466
467 goto ip;
468 case IPPROTO_IPV6:
469 proto = htons(ETH_P_IPV6);
470
471 key_control->flags |= FLOW_DIS_ENCAPSULATION;
472 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
473 goto out_good;
474
475 goto ipv6;
476 case IPPROTO_MPLS:
477 proto = htons(ETH_P_MPLS_UC);
478 goto mpls;
479 default:
480 break;
481 }
482
483 if (dissector_uses_key(flow_dissector,
484 FLOW_DISSECTOR_KEY_PORTS)) {
485 key_ports = skb_flow_dissector_target(flow_dissector,
486 FLOW_DISSECTOR_KEY_PORTS,
487 target_container);
488 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
489 data, hlen);
490 }
491
492 out_good:
493 ret = true;
494
495 key_control->thoff = (u16)nhoff;
496 out:
497 key_basic->n_proto = proto;
498 key_basic->ip_proto = ip_proto;
499
500 return ret;
501
502 out_bad:
503 ret = false;
504 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
505 goto out;
506 }
507 EXPORT_SYMBOL(__skb_flow_dissect);
508
509 static u32 hashrnd __read_mostly;
510 static __always_inline void __flow_hash_secret_init(void)
511 {
512 net_get_random_once(&hashrnd, sizeof(hashrnd));
513 }
514
515 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
516 u32 keyval)
517 {
518 return jhash2(words, length, keyval);
519 }
520
521 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
522 {
523 const void *p = flow;
524
525 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
526 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
527 }
528
529 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
530 {
531 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
532 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
533 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
534 sizeof(*flow) - sizeof(flow->addrs));
535
536 switch (flow->control.addr_type) {
537 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
538 diff -= sizeof(flow->addrs.v4addrs);
539 break;
540 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
541 diff -= sizeof(flow->addrs.v6addrs);
542 break;
543 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
544 diff -= sizeof(flow->addrs.tipcaddrs);
545 break;
546 }
547 return (sizeof(*flow) - diff) / sizeof(u32);
548 }
549
550 __be32 flow_get_u32_src(const struct flow_keys *flow)
551 {
552 switch (flow->control.addr_type) {
553 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
554 return flow->addrs.v4addrs.src;
555 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
556 return (__force __be32)ipv6_addr_hash(
557 &flow->addrs.v6addrs.src);
558 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
559 return flow->addrs.tipcaddrs.srcnode;
560 default:
561 return 0;
562 }
563 }
564 EXPORT_SYMBOL(flow_get_u32_src);
565
566 __be32 flow_get_u32_dst(const struct flow_keys *flow)
567 {
568 switch (flow->control.addr_type) {
569 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
570 return flow->addrs.v4addrs.dst;
571 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
572 return (__force __be32)ipv6_addr_hash(
573 &flow->addrs.v6addrs.dst);
574 default:
575 return 0;
576 }
577 }
578 EXPORT_SYMBOL(flow_get_u32_dst);
579
580 static inline void __flow_hash_consistentify(struct flow_keys *keys)
581 {
582 int addr_diff, i;
583
584 switch (keys->control.addr_type) {
585 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
586 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
587 (__force u32)keys->addrs.v4addrs.src;
588 if ((addr_diff < 0) ||
589 (addr_diff == 0 &&
590 ((__force u16)keys->ports.dst <
591 (__force u16)keys->ports.src))) {
592 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
593 swap(keys->ports.src, keys->ports.dst);
594 }
595 break;
596 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
597 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
598 &keys->addrs.v6addrs.src,
599 sizeof(keys->addrs.v6addrs.dst));
600 if ((addr_diff < 0) ||
601 (addr_diff == 0 &&
602 ((__force u16)keys->ports.dst <
603 (__force u16)keys->ports.src))) {
604 for (i = 0; i < 4; i++)
605 swap(keys->addrs.v6addrs.src.s6_addr32[i],
606 keys->addrs.v6addrs.dst.s6_addr32[i]);
607 swap(keys->ports.src, keys->ports.dst);
608 }
609 break;
610 }
611 }
612
613 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
614 {
615 u32 hash;
616
617 __flow_hash_consistentify(keys);
618
619 hash = __flow_hash_words(flow_keys_hash_start(keys),
620 flow_keys_hash_length(keys), keyval);
621 if (!hash)
622 hash = 1;
623
624 return hash;
625 }
626
627 u32 flow_hash_from_keys(struct flow_keys *keys)
628 {
629 __flow_hash_secret_init();
630 return __flow_hash_from_keys(keys, hashrnd);
631 }
632 EXPORT_SYMBOL(flow_hash_from_keys);
633
634 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
635 struct flow_keys *keys, u32 keyval)
636 {
637 skb_flow_dissect_flow_keys(skb, keys,
638 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
639
640 return __flow_hash_from_keys(keys, keyval);
641 }
642
643 struct _flow_keys_digest_data {
644 __be16 n_proto;
645 u8 ip_proto;
646 u8 padding;
647 __be32 ports;
648 __be32 src;
649 __be32 dst;
650 };
651
652 void make_flow_keys_digest(struct flow_keys_digest *digest,
653 const struct flow_keys *flow)
654 {
655 struct _flow_keys_digest_data *data =
656 (struct _flow_keys_digest_data *)digest;
657
658 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
659
660 memset(digest, 0, sizeof(*digest));
661
662 data->n_proto = flow->basic.n_proto;
663 data->ip_proto = flow->basic.ip_proto;
664 data->ports = flow->ports.ports;
665 data->src = flow->addrs.v4addrs.src;
666 data->dst = flow->addrs.v4addrs.dst;
667 }
668 EXPORT_SYMBOL(make_flow_keys_digest);
669
670 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
671
672 u32 __skb_get_hash_symmetric(struct sk_buff *skb)
673 {
674 struct flow_keys keys;
675
676 __flow_hash_secret_init();
677
678 memset(&keys, 0, sizeof(keys));
679 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
680 NULL, 0, 0, 0,
681 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
682
683 return __flow_hash_from_keys(&keys, hashrnd);
684 }
685 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
686
687 /**
688 * __skb_get_hash: calculate a flow hash
689 * @skb: sk_buff to calculate flow hash from
690 *
691 * This function calculates a flow hash based on src/dst addresses
692 * and src/dst port numbers. Sets hash in skb to non-zero hash value
693 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
694 * if hash is a canonical 4-tuple hash over transport ports.
695 */
696 void __skb_get_hash(struct sk_buff *skb)
697 {
698 struct flow_keys keys;
699
700 __flow_hash_secret_init();
701
702 __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
703 flow_keys_have_l4(&keys));
704 }
705 EXPORT_SYMBOL(__skb_get_hash);
706
707 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
708 {
709 struct flow_keys keys;
710
711 return ___skb_get_hash(skb, &keys, perturb);
712 }
713 EXPORT_SYMBOL(skb_get_hash_perturb);
714
715 __u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
716 {
717 struct flow_keys keys;
718
719 memset(&keys, 0, sizeof(keys));
720
721 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
722 sizeof(keys.addrs.v6addrs.src));
723 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
724 sizeof(keys.addrs.v6addrs.dst));
725 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
726 keys.ports.src = fl6->fl6_sport;
727 keys.ports.dst = fl6->fl6_dport;
728 keys.keyid.keyid = fl6->fl6_gre_key;
729 keys.tags.flow_label = (__force u32)fl6->flowlabel;
730 keys.basic.ip_proto = fl6->flowi6_proto;
731
732 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
733 flow_keys_have_l4(&keys));
734
735 return skb->hash;
736 }
737 EXPORT_SYMBOL(__skb_get_hash_flowi6);
738
739 __u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
740 {
741 struct flow_keys keys;
742
743 memset(&keys, 0, sizeof(keys));
744
745 keys.addrs.v4addrs.src = fl4->saddr;
746 keys.addrs.v4addrs.dst = fl4->daddr;
747 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
748 keys.ports.src = fl4->fl4_sport;
749 keys.ports.dst = fl4->fl4_dport;
750 keys.keyid.keyid = fl4->fl4_gre_key;
751 keys.basic.ip_proto = fl4->flowi4_proto;
752
753 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
754 flow_keys_have_l4(&keys));
755
756 return skb->hash;
757 }
758 EXPORT_SYMBOL(__skb_get_hash_flowi4);
759
760 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
761 const struct flow_keys *keys, int hlen)
762 {
763 u32 poff = keys->control.thoff;
764
765 switch (keys->basic.ip_proto) {
766 case IPPROTO_TCP: {
767 /* access doff as u8 to avoid unaligned access */
768 const u8 *doff;
769 u8 _doff;
770
771 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
772 data, hlen, &_doff);
773 if (!doff)
774 return poff;
775
776 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
777 break;
778 }
779 case IPPROTO_UDP:
780 case IPPROTO_UDPLITE:
781 poff += sizeof(struct udphdr);
782 break;
783 /* For the rest, we do not really care about header
784 * extensions at this point for now.
785 */
786 case IPPROTO_ICMP:
787 poff += sizeof(struct icmphdr);
788 break;
789 case IPPROTO_ICMPV6:
790 poff += sizeof(struct icmp6hdr);
791 break;
792 case IPPROTO_IGMP:
793 poff += sizeof(struct igmphdr);
794 break;
795 case IPPROTO_DCCP:
796 poff += sizeof(struct dccp_hdr);
797 break;
798 case IPPROTO_SCTP:
799 poff += sizeof(struct sctphdr);
800 break;
801 }
802
803 return poff;
804 }
805
806 /**
807 * skb_get_poff - get the offset to the payload
808 * @skb: sk_buff to get the payload offset from
809 *
810 * The function will get the offset to the payload as far as it could
811 * be dissected. The main user is currently BPF, so that we can dynamically
812 * truncate packets without needing to push actual payload to the user
813 * space and can analyze headers only, instead.
814 */
815 u32 skb_get_poff(const struct sk_buff *skb)
816 {
817 struct flow_keys keys;
818
819 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
820 return 0;
821
822 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
823 }
824
825 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
826 {
827 memset(keys, 0, sizeof(*keys));
828
829 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
830 sizeof(keys->addrs.v6addrs.src));
831 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
832 sizeof(keys->addrs.v6addrs.dst));
833 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
834 keys->ports.src = fl6->fl6_sport;
835 keys->ports.dst = fl6->fl6_dport;
836 keys->keyid.keyid = fl6->fl6_gre_key;
837 keys->tags.flow_label = (__force u32)fl6->flowlabel;
838 keys->basic.ip_proto = fl6->flowi6_proto;
839
840 return flow_hash_from_keys(keys);
841 }
842 EXPORT_SYMBOL(__get_hash_from_flowi6);
843
844 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
845 {
846 memset(keys, 0, sizeof(*keys));
847
848 keys->addrs.v4addrs.src = fl4->saddr;
849 keys->addrs.v4addrs.dst = fl4->daddr;
850 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
851 keys->ports.src = fl4->fl4_sport;
852 keys->ports.dst = fl4->fl4_dport;
853 keys->keyid.keyid = fl4->fl4_gre_key;
854 keys->basic.ip_proto = fl4->flowi4_proto;
855
856 return flow_hash_from_keys(keys);
857 }
858 EXPORT_SYMBOL(__get_hash_from_flowi4);
859
860 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
861 {
862 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
863 .offset = offsetof(struct flow_keys, control),
864 },
865 {
866 .key_id = FLOW_DISSECTOR_KEY_BASIC,
867 .offset = offsetof(struct flow_keys, basic),
868 },
869 {
870 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
871 .offset = offsetof(struct flow_keys, addrs.v4addrs),
872 },
873 {
874 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
875 .offset = offsetof(struct flow_keys, addrs.v6addrs),
876 },
877 {
878 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
879 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
880 },
881 {
882 .key_id = FLOW_DISSECTOR_KEY_PORTS,
883 .offset = offsetof(struct flow_keys, ports),
884 },
885 {
886 .key_id = FLOW_DISSECTOR_KEY_VLANID,
887 .offset = offsetof(struct flow_keys, tags),
888 },
889 {
890 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
891 .offset = offsetof(struct flow_keys, tags),
892 },
893 {
894 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
895 .offset = offsetof(struct flow_keys, keyid),
896 },
897 };
898
899 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
900 {
901 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
902 .offset = offsetof(struct flow_keys, control),
903 },
904 {
905 .key_id = FLOW_DISSECTOR_KEY_BASIC,
906 .offset = offsetof(struct flow_keys, basic),
907 },
908 {
909 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
910 .offset = offsetof(struct flow_keys, addrs.v4addrs),
911 },
912 {
913 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
914 .offset = offsetof(struct flow_keys, addrs.v6addrs),
915 },
916 {
917 .key_id = FLOW_DISSECTOR_KEY_PORTS,
918 .offset = offsetof(struct flow_keys, ports),
919 },
920 };
921
922 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
923 {
924 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
925 .offset = offsetof(struct flow_keys, control),
926 },
927 {
928 .key_id = FLOW_DISSECTOR_KEY_BASIC,
929 .offset = offsetof(struct flow_keys, basic),
930 },
931 };
932
933 struct flow_dissector flow_keys_dissector __read_mostly;
934 EXPORT_SYMBOL(flow_keys_dissector);
935
936 struct flow_dissector flow_keys_buf_dissector __read_mostly;
937
938 static int __init init_default_flow_dissectors(void)
939 {
940 skb_flow_dissector_init(&flow_keys_dissector,
941 flow_keys_dissector_keys,
942 ARRAY_SIZE(flow_keys_dissector_keys));
943 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
944 flow_keys_dissector_symmetric_keys,
945 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
946 skb_flow_dissector_init(&flow_keys_buf_dissector,
947 flow_keys_buf_dissector_keys,
948 ARRAY_SIZE(flow_keys_buf_dissector_keys));
949 return 0;
950 }
951
952 core_initcall(init_default_flow_dissectors);