flow_dissector: Correctly handle parsing FCoE
[deliverable/linux.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 = 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 struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs;
224
225 key_ipv6_addrs = skb_flow_dissector_target(flow_dissector,
226 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
227 target_container);
228
229 memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs));
230 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
231 }
232
233 if ((dissector_uses_key(flow_dissector,
234 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
235 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
236 ip6_flowlabel(iph)) {
237 __be32 flow_label = ip6_flowlabel(iph);
238
239 if (dissector_uses_key(flow_dissector,
240 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
241 key_tags = skb_flow_dissector_target(flow_dissector,
242 FLOW_DISSECTOR_KEY_FLOW_LABEL,
243 target_container);
244 key_tags->flow_label = ntohl(flow_label);
245 }
246 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
247 goto out_good;
248 }
249
250 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
251 goto out_good;
252
253 break;
254 }
255 case htons(ETH_P_8021AD):
256 case htons(ETH_P_8021Q): {
257 const struct vlan_hdr *vlan;
258 struct vlan_hdr _vlan;
259
260 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
261 if (!vlan)
262 goto out_bad;
263
264 if (dissector_uses_key(flow_dissector,
265 FLOW_DISSECTOR_KEY_VLANID)) {
266 key_tags = skb_flow_dissector_target(flow_dissector,
267 FLOW_DISSECTOR_KEY_VLANID,
268 target_container);
269
270 key_tags->vlan_id = skb_vlan_tag_get_id(skb);
271 }
272
273 proto = vlan->h_vlan_encapsulated_proto;
274 nhoff += sizeof(*vlan);
275 goto again;
276 }
277 case htons(ETH_P_PPP_SES): {
278 struct {
279 struct pppoe_hdr hdr;
280 __be16 proto;
281 } *hdr, _hdr;
282 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
283 if (!hdr)
284 goto out_bad;
285 proto = hdr->proto;
286 nhoff += PPPOE_SES_HLEN;
287 switch (proto) {
288 case htons(PPP_IP):
289 goto ip;
290 case htons(PPP_IPV6):
291 goto ipv6;
292 default:
293 goto out_bad;
294 }
295 }
296 case htons(ETH_P_TIPC): {
297 struct {
298 __be32 pre[3];
299 __be32 srcnode;
300 } *hdr, _hdr;
301 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
302 if (!hdr)
303 goto out_bad;
304
305 if (dissector_uses_key(flow_dissector,
306 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
307 key_addrs = skb_flow_dissector_target(flow_dissector,
308 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
309 target_container);
310 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
311 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
312 }
313 goto out_good;
314 }
315
316 case htons(ETH_P_MPLS_UC):
317 case htons(ETH_P_MPLS_MC): {
318 struct mpls_label *hdr, _hdr[2];
319 mpls:
320 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
321 hlen, &_hdr);
322 if (!hdr)
323 goto out_bad;
324
325 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
326 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
327 if (dissector_uses_key(flow_dissector,
328 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
329 key_keyid = skb_flow_dissector_target(flow_dissector,
330 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
331 target_container);
332 key_keyid->keyid = hdr[1].entry &
333 htonl(MPLS_LS_LABEL_MASK);
334 }
335
336 goto out_good;
337 }
338
339 goto out_good;
340 }
341
342 case htons(ETH_P_FCOE):
343 if ((hlen - nhoff) < FCOE_HEADER_LEN)
344 goto out_bad;
345
346 nhoff += FCOE_HEADER_LEN;
347 goto out_good;
348 default:
349 goto out_bad;
350 }
351
352 ip_proto_again:
353 switch (ip_proto) {
354 case IPPROTO_GRE: {
355 struct gre_hdr {
356 __be16 flags;
357 __be16 proto;
358 } *hdr, _hdr;
359
360 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
361 if (!hdr)
362 goto out_bad;
363 /*
364 * Only look inside GRE if version zero and no
365 * routing
366 */
367 if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
368 break;
369
370 proto = hdr->proto;
371 nhoff += 4;
372 if (hdr->flags & GRE_CSUM)
373 nhoff += 4;
374 if (hdr->flags & GRE_KEY) {
375 const __be32 *keyid;
376 __be32 _keyid;
377
378 keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
379 data, hlen, &_keyid);
380
381 if (!keyid)
382 goto out_bad;
383
384 if (dissector_uses_key(flow_dissector,
385 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
386 key_keyid = skb_flow_dissector_target(flow_dissector,
387 FLOW_DISSECTOR_KEY_GRE_KEYID,
388 target_container);
389 key_keyid->keyid = *keyid;
390 }
391 nhoff += 4;
392 }
393 if (hdr->flags & GRE_SEQ)
394 nhoff += 4;
395 if (proto == htons(ETH_P_TEB)) {
396 const struct ethhdr *eth;
397 struct ethhdr _eth;
398
399 eth = __skb_header_pointer(skb, nhoff,
400 sizeof(_eth),
401 data, hlen, &_eth);
402 if (!eth)
403 goto out_bad;
404 proto = eth->h_proto;
405 nhoff += sizeof(*eth);
406
407 /* Cap headers that we access via pointers at the
408 * end of the Ethernet header as our maximum alignment
409 * at that point is only 2 bytes.
410 */
411 if (NET_IP_ALIGN)
412 hlen = nhoff;
413 }
414
415 key_control->flags |= FLOW_DIS_ENCAPSULATION;
416 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
417 goto out_good;
418
419 goto again;
420 }
421 case NEXTHDR_HOP:
422 case NEXTHDR_ROUTING:
423 case NEXTHDR_DEST: {
424 u8 _opthdr[2], *opthdr;
425
426 if (proto != htons(ETH_P_IPV6))
427 break;
428
429 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
430 data, hlen, &_opthdr);
431 if (!opthdr)
432 goto out_bad;
433
434 ip_proto = opthdr[0];
435 nhoff += (opthdr[1] + 1) << 3;
436
437 goto ip_proto_again;
438 }
439 case NEXTHDR_FRAGMENT: {
440 struct frag_hdr _fh, *fh;
441
442 if (proto != htons(ETH_P_IPV6))
443 break;
444
445 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
446 data, hlen, &_fh);
447
448 if (!fh)
449 goto out_bad;
450
451 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
452
453 nhoff += sizeof(_fh);
454 ip_proto = fh->nexthdr;
455
456 if (!(fh->frag_off & htons(IP6_OFFSET))) {
457 key_control->flags |= FLOW_DIS_FIRST_FRAG;
458 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
459 goto ip_proto_again;
460 }
461 goto out_good;
462 }
463 case IPPROTO_IPIP:
464 proto = htons(ETH_P_IP);
465
466 key_control->flags |= FLOW_DIS_ENCAPSULATION;
467 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
468 goto out_good;
469
470 goto ip;
471 case IPPROTO_IPV6:
472 proto = htons(ETH_P_IPV6);
473
474 key_control->flags |= FLOW_DIS_ENCAPSULATION;
475 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
476 goto out_good;
477
478 goto ipv6;
479 case IPPROTO_MPLS:
480 proto = htons(ETH_P_MPLS_UC);
481 goto mpls;
482 default:
483 break;
484 }
485
486 if (dissector_uses_key(flow_dissector,
487 FLOW_DISSECTOR_KEY_PORTS)) {
488 key_ports = skb_flow_dissector_target(flow_dissector,
489 FLOW_DISSECTOR_KEY_PORTS,
490 target_container);
491 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
492 data, hlen);
493 }
494
495 out_good:
496 ret = true;
497
498 out_bad:
499 key_basic->n_proto = proto;
500 key_basic->ip_proto = ip_proto;
501 key_control->thoff = (u16)nhoff;
502
503 return ret;
504 }
505 EXPORT_SYMBOL(__skb_flow_dissect);
506
507 static u32 hashrnd __read_mostly;
508 static __always_inline void __flow_hash_secret_init(void)
509 {
510 net_get_random_once(&hashrnd, sizeof(hashrnd));
511 }
512
513 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
514 u32 keyval)
515 {
516 return jhash2(words, length, keyval);
517 }
518
519 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
520 {
521 const void *p = flow;
522
523 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
524 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
525 }
526
527 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
528 {
529 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
530 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
531 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
532 sizeof(*flow) - sizeof(flow->addrs));
533
534 switch (flow->control.addr_type) {
535 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
536 diff -= sizeof(flow->addrs.v4addrs);
537 break;
538 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
539 diff -= sizeof(flow->addrs.v6addrs);
540 break;
541 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
542 diff -= sizeof(flow->addrs.tipcaddrs);
543 break;
544 }
545 return (sizeof(*flow) - diff) / sizeof(u32);
546 }
547
548 __be32 flow_get_u32_src(const struct flow_keys *flow)
549 {
550 switch (flow->control.addr_type) {
551 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
552 return flow->addrs.v4addrs.src;
553 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
554 return (__force __be32)ipv6_addr_hash(
555 &flow->addrs.v6addrs.src);
556 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
557 return flow->addrs.tipcaddrs.srcnode;
558 default:
559 return 0;
560 }
561 }
562 EXPORT_SYMBOL(flow_get_u32_src);
563
564 __be32 flow_get_u32_dst(const struct flow_keys *flow)
565 {
566 switch (flow->control.addr_type) {
567 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
568 return flow->addrs.v4addrs.dst;
569 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
570 return (__force __be32)ipv6_addr_hash(
571 &flow->addrs.v6addrs.dst);
572 default:
573 return 0;
574 }
575 }
576 EXPORT_SYMBOL(flow_get_u32_dst);
577
578 static inline void __flow_hash_consistentify(struct flow_keys *keys)
579 {
580 int addr_diff, i;
581
582 switch (keys->control.addr_type) {
583 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
584 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
585 (__force u32)keys->addrs.v4addrs.src;
586 if ((addr_diff < 0) ||
587 (addr_diff == 0 &&
588 ((__force u16)keys->ports.dst <
589 (__force u16)keys->ports.src))) {
590 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
591 swap(keys->ports.src, keys->ports.dst);
592 }
593 break;
594 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
595 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
596 &keys->addrs.v6addrs.src,
597 sizeof(keys->addrs.v6addrs.dst));
598 if ((addr_diff < 0) ||
599 (addr_diff == 0 &&
600 ((__force u16)keys->ports.dst <
601 (__force u16)keys->ports.src))) {
602 for (i = 0; i < 4; i++)
603 swap(keys->addrs.v6addrs.src.s6_addr32[i],
604 keys->addrs.v6addrs.dst.s6_addr32[i]);
605 swap(keys->ports.src, keys->ports.dst);
606 }
607 break;
608 }
609 }
610
611 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
612 {
613 u32 hash;
614
615 __flow_hash_consistentify(keys);
616
617 hash = __flow_hash_words(flow_keys_hash_start(keys),
618 flow_keys_hash_length(keys), keyval);
619 if (!hash)
620 hash = 1;
621
622 return hash;
623 }
624
625 u32 flow_hash_from_keys(struct flow_keys *keys)
626 {
627 __flow_hash_secret_init();
628 return __flow_hash_from_keys(keys, hashrnd);
629 }
630 EXPORT_SYMBOL(flow_hash_from_keys);
631
632 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
633 struct flow_keys *keys, u32 keyval)
634 {
635 skb_flow_dissect_flow_keys(skb, keys,
636 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
637
638 return __flow_hash_from_keys(keys, keyval);
639 }
640
641 struct _flow_keys_digest_data {
642 __be16 n_proto;
643 u8 ip_proto;
644 u8 padding;
645 __be32 ports;
646 __be32 src;
647 __be32 dst;
648 };
649
650 void make_flow_keys_digest(struct flow_keys_digest *digest,
651 const struct flow_keys *flow)
652 {
653 struct _flow_keys_digest_data *data =
654 (struct _flow_keys_digest_data *)digest;
655
656 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
657
658 memset(digest, 0, sizeof(*digest));
659
660 data->n_proto = flow->basic.n_proto;
661 data->ip_proto = flow->basic.ip_proto;
662 data->ports = flow->ports.ports;
663 data->src = flow->addrs.v4addrs.src;
664 data->dst = flow->addrs.v4addrs.dst;
665 }
666 EXPORT_SYMBOL(make_flow_keys_digest);
667
668 /**
669 * __skb_get_hash: calculate a flow hash
670 * @skb: sk_buff to calculate flow hash from
671 *
672 * This function calculates a flow hash based on src/dst addresses
673 * and src/dst port numbers. Sets hash in skb to non-zero hash value
674 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
675 * if hash is a canonical 4-tuple hash over transport ports.
676 */
677 void __skb_get_hash(struct sk_buff *skb)
678 {
679 struct flow_keys keys;
680
681 __flow_hash_secret_init();
682
683 __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
684 flow_keys_have_l4(&keys));
685 }
686 EXPORT_SYMBOL(__skb_get_hash);
687
688 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
689 {
690 struct flow_keys keys;
691
692 return ___skb_get_hash(skb, &keys, perturb);
693 }
694 EXPORT_SYMBOL(skb_get_hash_perturb);
695
696 __u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
697 {
698 struct flow_keys keys;
699
700 memset(&keys, 0, sizeof(keys));
701
702 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
703 sizeof(keys.addrs.v6addrs.src));
704 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
705 sizeof(keys.addrs.v6addrs.dst));
706 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
707 keys.ports.src = fl6->fl6_sport;
708 keys.ports.dst = fl6->fl6_dport;
709 keys.keyid.keyid = fl6->fl6_gre_key;
710 keys.tags.flow_label = (__force u32)fl6->flowlabel;
711 keys.basic.ip_proto = fl6->flowi6_proto;
712
713 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
714 flow_keys_have_l4(&keys));
715
716 return skb->hash;
717 }
718 EXPORT_SYMBOL(__skb_get_hash_flowi6);
719
720 __u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
721 {
722 struct flow_keys keys;
723
724 memset(&keys, 0, sizeof(keys));
725
726 keys.addrs.v4addrs.src = fl4->saddr;
727 keys.addrs.v4addrs.dst = fl4->daddr;
728 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
729 keys.ports.src = fl4->fl4_sport;
730 keys.ports.dst = fl4->fl4_dport;
731 keys.keyid.keyid = fl4->fl4_gre_key;
732 keys.basic.ip_proto = fl4->flowi4_proto;
733
734 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
735 flow_keys_have_l4(&keys));
736
737 return skb->hash;
738 }
739 EXPORT_SYMBOL(__skb_get_hash_flowi4);
740
741 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
742 const struct flow_keys *keys, int hlen)
743 {
744 u32 poff = keys->control.thoff;
745
746 /* skip L4 headers for fragments after the first */
747 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
748 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
749 return poff;
750
751 switch (keys->basic.ip_proto) {
752 case IPPROTO_TCP: {
753 /* access doff as u8 to avoid unaligned access */
754 const u8 *doff;
755 u8 _doff;
756
757 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
758 data, hlen, &_doff);
759 if (!doff)
760 return poff;
761
762 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
763 break;
764 }
765 case IPPROTO_UDP:
766 case IPPROTO_UDPLITE:
767 poff += sizeof(struct udphdr);
768 break;
769 /* For the rest, we do not really care about header
770 * extensions at this point for now.
771 */
772 case IPPROTO_ICMP:
773 poff += sizeof(struct icmphdr);
774 break;
775 case IPPROTO_ICMPV6:
776 poff += sizeof(struct icmp6hdr);
777 break;
778 case IPPROTO_IGMP:
779 poff += sizeof(struct igmphdr);
780 break;
781 case IPPROTO_DCCP:
782 poff += sizeof(struct dccp_hdr);
783 break;
784 case IPPROTO_SCTP:
785 poff += sizeof(struct sctphdr);
786 break;
787 }
788
789 return poff;
790 }
791
792 /**
793 * skb_get_poff - get the offset to the payload
794 * @skb: sk_buff to get the payload offset from
795 *
796 * The function will get the offset to the payload as far as it could
797 * be dissected. The main user is currently BPF, so that we can dynamically
798 * truncate packets without needing to push actual payload to the user
799 * space and can analyze headers only, instead.
800 */
801 u32 skb_get_poff(const struct sk_buff *skb)
802 {
803 struct flow_keys keys;
804
805 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
806 return 0;
807
808 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
809 }
810
811 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
812 {
813 memset(keys, 0, sizeof(*keys));
814
815 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
816 sizeof(keys->addrs.v6addrs.src));
817 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
818 sizeof(keys->addrs.v6addrs.dst));
819 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
820 keys->ports.src = fl6->fl6_sport;
821 keys->ports.dst = fl6->fl6_dport;
822 keys->keyid.keyid = fl6->fl6_gre_key;
823 keys->tags.flow_label = (__force u32)fl6->flowlabel;
824 keys->basic.ip_proto = fl6->flowi6_proto;
825
826 return flow_hash_from_keys(keys);
827 }
828 EXPORT_SYMBOL(__get_hash_from_flowi6);
829
830 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
831 {
832 memset(keys, 0, sizeof(*keys));
833
834 keys->addrs.v4addrs.src = fl4->saddr;
835 keys->addrs.v4addrs.dst = fl4->daddr;
836 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
837 keys->ports.src = fl4->fl4_sport;
838 keys->ports.dst = fl4->fl4_dport;
839 keys->keyid.keyid = fl4->fl4_gre_key;
840 keys->basic.ip_proto = fl4->flowi4_proto;
841
842 return flow_hash_from_keys(keys);
843 }
844 EXPORT_SYMBOL(__get_hash_from_flowi4);
845
846 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
847 {
848 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
849 .offset = offsetof(struct flow_keys, control),
850 },
851 {
852 .key_id = FLOW_DISSECTOR_KEY_BASIC,
853 .offset = offsetof(struct flow_keys, basic),
854 },
855 {
856 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
857 .offset = offsetof(struct flow_keys, addrs.v4addrs),
858 },
859 {
860 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
861 .offset = offsetof(struct flow_keys, addrs.v6addrs),
862 },
863 {
864 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
865 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
866 },
867 {
868 .key_id = FLOW_DISSECTOR_KEY_PORTS,
869 .offset = offsetof(struct flow_keys, ports),
870 },
871 {
872 .key_id = FLOW_DISSECTOR_KEY_VLANID,
873 .offset = offsetof(struct flow_keys, tags),
874 },
875 {
876 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
877 .offset = offsetof(struct flow_keys, tags),
878 },
879 {
880 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
881 .offset = offsetof(struct flow_keys, keyid),
882 },
883 };
884
885 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
886 {
887 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
888 .offset = offsetof(struct flow_keys, control),
889 },
890 {
891 .key_id = FLOW_DISSECTOR_KEY_BASIC,
892 .offset = offsetof(struct flow_keys, basic),
893 },
894 };
895
896 struct flow_dissector flow_keys_dissector __read_mostly;
897 EXPORT_SYMBOL(flow_keys_dissector);
898
899 struct flow_dissector flow_keys_buf_dissector __read_mostly;
900
901 static int __init init_default_flow_dissectors(void)
902 {
903 skb_flow_dissector_init(&flow_keys_dissector,
904 flow_keys_dissector_keys,
905 ARRAY_SIZE(flow_keys_dissector_keys));
906 skb_flow_dissector_init(&flow_keys_buf_dissector,
907 flow_keys_buf_dissector_keys,
908 ARRAY_SIZE(flow_keys_buf_dissector_keys));
909 return 0;
910 }
911
912 late_initcall_sync(init_default_flow_dissectors);
This page took 0.084644 seconds and 5 git commands to generate.