flowi: Abstract out functions to get flow hash based on flowi
[deliverable/linux.git] / net / core / flow_dissector.c
... / ...
CommitLineData
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
22static bool skb_flow_dissector_uses_key(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
28static void skb_flow_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
34static 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
41void 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(skb_flow_dissector_uses_key(flow_dissector,
55 key->key_id));
56
57 skb_flow_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(!skb_flow_dissector_uses_key(flow_dissector,
65 FLOW_DISSECTOR_KEY_CONTROL));
66 BUG_ON(!skb_flow_dissector_uses_key(flow_dissector,
67 FLOW_DISSECTOR_KEY_BASIC));
68}
69EXPORT_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}
103EXPORT_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 */
121bool __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{
126 struct flow_dissector_key_control *key_control;
127 struct flow_dissector_key_basic *key_basic;
128 struct flow_dissector_key_addrs *key_addrs;
129 struct flow_dissector_key_ports *key_ports;
130 struct flow_dissector_key_tags *key_tags;
131 struct flow_dissector_key_keyid *key_keyid;
132 u8 ip_proto = 0;
133
134 if (!data) {
135 data = skb->data;
136 proto = skb->protocol;
137 nhoff = skb_network_offset(skb);
138 hlen = skb_headlen(skb);
139 }
140
141 /* It is ensured by skb_flow_dissector_init() that control key will
142 * be always present.
143 */
144 key_control = skb_flow_dissector_target(flow_dissector,
145 FLOW_DISSECTOR_KEY_CONTROL,
146 target_container);
147
148 /* It is ensured by skb_flow_dissector_init() that basic key will
149 * be always present.
150 */
151 key_basic = skb_flow_dissector_target(flow_dissector,
152 FLOW_DISSECTOR_KEY_BASIC,
153 target_container);
154
155 if (skb_flow_dissector_uses_key(flow_dissector,
156 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
157 struct ethhdr *eth = eth_hdr(skb);
158 struct flow_dissector_key_eth_addrs *key_eth_addrs;
159
160 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
161 FLOW_DISSECTOR_KEY_ETH_ADDRS,
162 target_container);
163 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
164 }
165
166again:
167 switch (proto) {
168 case htons(ETH_P_IP): {
169 const struct iphdr *iph;
170 struct iphdr _iph;
171ip:
172 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
173 if (!iph || iph->ihl < 5)
174 return false;
175 nhoff += iph->ihl * 4;
176
177 ip_proto = iph->protocol;
178 if (ip_is_fragment(iph))
179 ip_proto = 0;
180
181 if (!skb_flow_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 break;
191 }
192 case htons(ETH_P_IPV6): {
193 const struct ipv6hdr *iph;
194 struct ipv6hdr _iph;
195 __be32 flow_label;
196
197ipv6:
198 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
199 if (!iph)
200 return false;
201
202 ip_proto = iph->nexthdr;
203 nhoff += sizeof(struct ipv6hdr);
204
205 if (skb_flow_dissector_uses_key(flow_dissector,
206 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
207 struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs;
208
209 key_ipv6_addrs = skb_flow_dissector_target(flow_dissector,
210 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
211 target_container);
212
213 memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs));
214 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
215 }
216
217 flow_label = ip6_flowlabel(iph);
218 if (flow_label) {
219 if (skb_flow_dissector_uses_key(flow_dissector,
220 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
221 key_tags = skb_flow_dissector_target(flow_dissector,
222 FLOW_DISSECTOR_KEY_FLOW_LABEL,
223 target_container);
224 key_tags->flow_label = ntohl(flow_label);
225 }
226 }
227
228 break;
229 }
230 case htons(ETH_P_8021AD):
231 case htons(ETH_P_8021Q): {
232 const struct vlan_hdr *vlan;
233 struct vlan_hdr _vlan;
234
235 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
236 if (!vlan)
237 return false;
238
239 if (skb_flow_dissector_uses_key(flow_dissector,
240 FLOW_DISSECTOR_KEY_VLANID)) {
241 key_tags = skb_flow_dissector_target(flow_dissector,
242 FLOW_DISSECTOR_KEY_VLANID,
243 target_container);
244
245 key_tags->vlan_id = skb_vlan_tag_get_id(skb);
246 }
247
248 proto = vlan->h_vlan_encapsulated_proto;
249 nhoff += sizeof(*vlan);
250 goto again;
251 }
252 case htons(ETH_P_PPP_SES): {
253 struct {
254 struct pppoe_hdr hdr;
255 __be16 proto;
256 } *hdr, _hdr;
257 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
258 if (!hdr)
259 return false;
260 proto = hdr->proto;
261 nhoff += PPPOE_SES_HLEN;
262 switch (proto) {
263 case htons(PPP_IP):
264 goto ip;
265 case htons(PPP_IPV6):
266 goto ipv6;
267 default:
268 return false;
269 }
270 }
271 case htons(ETH_P_TIPC): {
272 struct {
273 __be32 pre[3];
274 __be32 srcnode;
275 } *hdr, _hdr;
276 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
277 if (!hdr)
278 return false;
279 key_basic->n_proto = proto;
280 key_control->thoff = (u16)nhoff;
281
282 if (skb_flow_dissector_uses_key(flow_dissector,
283 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
284 key_addrs = skb_flow_dissector_target(flow_dissector,
285 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
286 target_container);
287 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
288 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
289 }
290 return true;
291 }
292
293 case htons(ETH_P_MPLS_UC):
294 case htons(ETH_P_MPLS_MC): {
295 struct mpls_label *hdr, _hdr[2];
296mpls:
297 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
298 hlen, &_hdr);
299 if (!hdr)
300 return false;
301
302 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
303 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
304 if (skb_flow_dissector_uses_key(flow_dissector,
305 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
306 key_keyid = skb_flow_dissector_target(flow_dissector,
307 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
308 target_container);
309 key_keyid->keyid = hdr[1].entry &
310 htonl(MPLS_LS_LABEL_MASK);
311 }
312
313 key_basic->n_proto = proto;
314 key_basic->ip_proto = ip_proto;
315 key_control->thoff = (u16)nhoff;
316
317 return true;
318 }
319
320 return true;
321 }
322
323 case htons(ETH_P_FCOE):
324 key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
325 /* fall through */
326 default:
327 return false;
328 }
329
330ip_proto_again:
331 switch (ip_proto) {
332 case IPPROTO_GRE: {
333 struct gre_hdr {
334 __be16 flags;
335 __be16 proto;
336 } *hdr, _hdr;
337
338 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
339 if (!hdr)
340 return false;
341 /*
342 * Only look inside GRE if version zero and no
343 * routing
344 */
345 if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
346 break;
347
348 proto = hdr->proto;
349 nhoff += 4;
350 if (hdr->flags & GRE_CSUM)
351 nhoff += 4;
352 if (hdr->flags & GRE_KEY) {
353 const __be32 *keyid;
354 __be32 _keyid;
355
356 keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
357 data, hlen, &_keyid);
358
359 if (!keyid)
360 return false;
361
362 if (skb_flow_dissector_uses_key(flow_dissector,
363 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
364 key_keyid = skb_flow_dissector_target(flow_dissector,
365 FLOW_DISSECTOR_KEY_GRE_KEYID,
366 target_container);
367 key_keyid->keyid = *keyid;
368 }
369 nhoff += 4;
370 }
371 if (hdr->flags & GRE_SEQ)
372 nhoff += 4;
373 if (proto == htons(ETH_P_TEB)) {
374 const struct ethhdr *eth;
375 struct ethhdr _eth;
376
377 eth = __skb_header_pointer(skb, nhoff,
378 sizeof(_eth),
379 data, hlen, &_eth);
380 if (!eth)
381 return false;
382 proto = eth->h_proto;
383 nhoff += sizeof(*eth);
384 }
385 goto again;
386 }
387 case NEXTHDR_HOP:
388 case NEXTHDR_ROUTING:
389 case NEXTHDR_DEST: {
390 u8 _opthdr[2], *opthdr;
391
392 if (proto != htons(ETH_P_IPV6))
393 break;
394
395 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
396 data, hlen, &_opthdr);
397 if (!opthdr)
398 return false;
399
400 ip_proto = opthdr[0];
401 nhoff += (opthdr[1] + 1) << 3;
402
403 goto ip_proto_again;
404 }
405 case IPPROTO_IPIP:
406 proto = htons(ETH_P_IP);
407 goto ip;
408 case IPPROTO_IPV6:
409 proto = htons(ETH_P_IPV6);
410 goto ipv6;
411 case IPPROTO_MPLS:
412 proto = htons(ETH_P_MPLS_UC);
413 goto mpls;
414 default:
415 break;
416 }
417
418 key_basic->n_proto = proto;
419 key_basic->ip_proto = ip_proto;
420 key_control->thoff = (u16)nhoff;
421
422 if (skb_flow_dissector_uses_key(flow_dissector,
423 FLOW_DISSECTOR_KEY_PORTS)) {
424 key_ports = skb_flow_dissector_target(flow_dissector,
425 FLOW_DISSECTOR_KEY_PORTS,
426 target_container);
427 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
428 data, hlen);
429 }
430
431 return true;
432}
433EXPORT_SYMBOL(__skb_flow_dissect);
434
435static u32 hashrnd __read_mostly;
436static __always_inline void __flow_hash_secret_init(void)
437{
438 net_get_random_once(&hashrnd, sizeof(hashrnd));
439}
440
441static __always_inline u32 __flow_hash_words(u32 *words, u32 length, u32 keyval)
442{
443 return jhash2(words, length, keyval);
444}
445
446static inline void *flow_keys_hash_start(struct flow_keys *flow)
447{
448 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
449 return (void *)flow + FLOW_KEYS_HASH_OFFSET;
450}
451
452static inline size_t flow_keys_hash_length(struct flow_keys *flow)
453{
454 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
455 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
456 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
457 sizeof(*flow) - sizeof(flow->addrs));
458
459 switch (flow->control.addr_type) {
460 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
461 diff -= sizeof(flow->addrs.v4addrs);
462 break;
463 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
464 diff -= sizeof(flow->addrs.v6addrs);
465 break;
466 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
467 diff -= sizeof(flow->addrs.tipcaddrs);
468 break;
469 }
470 return (sizeof(*flow) - diff) / sizeof(u32);
471}
472
473__be32 flow_get_u32_src(const struct flow_keys *flow)
474{
475 switch (flow->control.addr_type) {
476 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
477 return flow->addrs.v4addrs.src;
478 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
479 return (__force __be32)ipv6_addr_hash(
480 &flow->addrs.v6addrs.src);
481 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
482 return flow->addrs.tipcaddrs.srcnode;
483 default:
484 return 0;
485 }
486}
487EXPORT_SYMBOL(flow_get_u32_src);
488
489__be32 flow_get_u32_dst(const struct flow_keys *flow)
490{
491 switch (flow->control.addr_type) {
492 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
493 return flow->addrs.v4addrs.dst;
494 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
495 return (__force __be32)ipv6_addr_hash(
496 &flow->addrs.v6addrs.dst);
497 default:
498 return 0;
499 }
500}
501EXPORT_SYMBOL(flow_get_u32_dst);
502
503static inline void __flow_hash_consistentify(struct flow_keys *keys)
504{
505 int addr_diff, i;
506
507 switch (keys->control.addr_type) {
508 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
509 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
510 (__force u32)keys->addrs.v4addrs.src;
511 if ((addr_diff < 0) ||
512 (addr_diff == 0 &&
513 ((__force u16)keys->ports.dst <
514 (__force u16)keys->ports.src))) {
515 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
516 swap(keys->ports.src, keys->ports.dst);
517 }
518 break;
519 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
520 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
521 &keys->addrs.v6addrs.src,
522 sizeof(keys->addrs.v6addrs.dst));
523 if ((addr_diff < 0) ||
524 (addr_diff == 0 &&
525 ((__force u16)keys->ports.dst <
526 (__force u16)keys->ports.src))) {
527 for (i = 0; i < 4; i++)
528 swap(keys->addrs.v6addrs.src.s6_addr32[i],
529 keys->addrs.v6addrs.dst.s6_addr32[i]);
530 swap(keys->ports.src, keys->ports.dst);
531 }
532 break;
533 }
534}
535
536static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
537{
538 u32 hash;
539
540 __flow_hash_consistentify(keys);
541
542 hash = __flow_hash_words((u32 *)flow_keys_hash_start(keys),
543 flow_keys_hash_length(keys), keyval);
544 if (!hash)
545 hash = 1;
546
547 return hash;
548}
549
550u32 flow_hash_from_keys(struct flow_keys *keys)
551{
552 __flow_hash_secret_init();
553 return __flow_hash_from_keys(keys, hashrnd);
554}
555EXPORT_SYMBOL(flow_hash_from_keys);
556
557static inline u32 ___skb_get_hash(const struct sk_buff *skb,
558 struct flow_keys *keys, u32 keyval)
559{
560 if (!skb_flow_dissect_flow_keys(skb, keys))
561 return 0;
562
563 return __flow_hash_from_keys(keys, keyval);
564}
565
566struct _flow_keys_digest_data {
567 __be16 n_proto;
568 u8 ip_proto;
569 u8 padding;
570 __be32 ports;
571 __be32 src;
572 __be32 dst;
573};
574
575void make_flow_keys_digest(struct flow_keys_digest *digest,
576 const struct flow_keys *flow)
577{
578 struct _flow_keys_digest_data *data =
579 (struct _flow_keys_digest_data *)digest;
580
581 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
582
583 memset(digest, 0, sizeof(*digest));
584
585 data->n_proto = flow->basic.n_proto;
586 data->ip_proto = flow->basic.ip_proto;
587 data->ports = flow->ports.ports;
588 data->src = flow->addrs.v4addrs.src;
589 data->dst = flow->addrs.v4addrs.dst;
590}
591EXPORT_SYMBOL(make_flow_keys_digest);
592
593/**
594 * __skb_get_hash: calculate a flow hash
595 * @skb: sk_buff to calculate flow hash from
596 *
597 * This function calculates a flow hash based on src/dst addresses
598 * and src/dst port numbers. Sets hash in skb to non-zero hash value
599 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
600 * if hash is a canonical 4-tuple hash over transport ports.
601 */
602void __skb_get_hash(struct sk_buff *skb)
603{
604 struct flow_keys keys;
605 u32 hash;
606
607 __flow_hash_secret_init();
608
609 hash = ___skb_get_hash(skb, &keys, hashrnd);
610 if (!hash)
611 return;
612
613 __skb_set_sw_hash(skb, hash,
614 flow_keys_have_l4(&keys));
615}
616EXPORT_SYMBOL(__skb_get_hash);
617
618__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
619{
620 struct flow_keys keys;
621
622 return ___skb_get_hash(skb, &keys, perturb);
623}
624EXPORT_SYMBOL(skb_get_hash_perturb);
625
626__u32 __skb_get_hash_flowi6(struct sk_buff *skb, struct flowi6 *fl6)
627{
628 struct flow_keys keys;
629
630 memset(&keys, 0, sizeof(keys));
631
632 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
633 sizeof(keys.addrs.v6addrs.src));
634 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
635 sizeof(keys.addrs.v6addrs.dst));
636 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
637 keys.ports.src = fl6->fl6_sport;
638 keys.ports.dst = fl6->fl6_dport;
639 keys.keyid.keyid = fl6->fl6_gre_key;
640 keys.tags.flow_label = (__force u32)fl6->flowlabel;
641 keys.basic.ip_proto = fl6->flowi6_proto;
642
643 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
644 flow_keys_have_l4(&keys));
645
646 return skb->hash;
647}
648EXPORT_SYMBOL(__skb_get_hash_flowi6);
649
650__u32 __skb_get_hash_flowi4(struct sk_buff *skb, struct flowi4 *fl4)
651{
652 struct flow_keys keys;
653
654 memset(&keys, 0, sizeof(keys));
655
656 keys.addrs.v4addrs.src = fl4->saddr;
657 keys.addrs.v4addrs.dst = fl4->daddr;
658 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
659 keys.ports.src = fl4->fl4_sport;
660 keys.ports.dst = fl4->fl4_dport;
661 keys.keyid.keyid = fl4->fl4_gre_key;
662 keys.basic.ip_proto = fl4->flowi4_proto;
663
664 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
665 flow_keys_have_l4(&keys));
666
667 return skb->hash;
668}
669EXPORT_SYMBOL(__skb_get_hash_flowi4);
670
671u32 __skb_get_poff(const struct sk_buff *skb, void *data,
672 const struct flow_keys *keys, int hlen)
673{
674 u32 poff = keys->control.thoff;
675
676 switch (keys->basic.ip_proto) {
677 case IPPROTO_TCP: {
678 /* access doff as u8 to avoid unaligned access */
679 const u8 *doff;
680 u8 _doff;
681
682 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
683 data, hlen, &_doff);
684 if (!doff)
685 return poff;
686
687 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
688 break;
689 }
690 case IPPROTO_UDP:
691 case IPPROTO_UDPLITE:
692 poff += sizeof(struct udphdr);
693 break;
694 /* For the rest, we do not really care about header
695 * extensions at this point for now.
696 */
697 case IPPROTO_ICMP:
698 poff += sizeof(struct icmphdr);
699 break;
700 case IPPROTO_ICMPV6:
701 poff += sizeof(struct icmp6hdr);
702 break;
703 case IPPROTO_IGMP:
704 poff += sizeof(struct igmphdr);
705 break;
706 case IPPROTO_DCCP:
707 poff += sizeof(struct dccp_hdr);
708 break;
709 case IPPROTO_SCTP:
710 poff += sizeof(struct sctphdr);
711 break;
712 }
713
714 return poff;
715}
716
717/**
718 * skb_get_poff - get the offset to the payload
719 * @skb: sk_buff to get the payload offset from
720 *
721 * The function will get the offset to the payload as far as it could
722 * be dissected. The main user is currently BPF, so that we can dynamically
723 * truncate packets without needing to push actual payload to the user
724 * space and can analyze headers only, instead.
725 */
726u32 skb_get_poff(const struct sk_buff *skb)
727{
728 struct flow_keys keys;
729
730 if (!skb_flow_dissect_flow_keys(skb, &keys))
731 return 0;
732
733 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
734}
735
736static const struct flow_dissector_key flow_keys_dissector_keys[] = {
737 {
738 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
739 .offset = offsetof(struct flow_keys, control),
740 },
741 {
742 .key_id = FLOW_DISSECTOR_KEY_BASIC,
743 .offset = offsetof(struct flow_keys, basic),
744 },
745 {
746 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
747 .offset = offsetof(struct flow_keys, addrs.v4addrs),
748 },
749 {
750 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
751 .offset = offsetof(struct flow_keys, addrs.v6addrs),
752 },
753 {
754 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
755 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
756 },
757 {
758 .key_id = FLOW_DISSECTOR_KEY_PORTS,
759 .offset = offsetof(struct flow_keys, ports),
760 },
761 {
762 .key_id = FLOW_DISSECTOR_KEY_VLANID,
763 .offset = offsetof(struct flow_keys, tags),
764 },
765 {
766 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
767 .offset = offsetof(struct flow_keys, tags),
768 },
769 {
770 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
771 .offset = offsetof(struct flow_keys, keyid),
772 },
773};
774
775static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
776 {
777 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
778 .offset = offsetof(struct flow_keys, control),
779 },
780 {
781 .key_id = FLOW_DISSECTOR_KEY_BASIC,
782 .offset = offsetof(struct flow_keys, basic),
783 },
784};
785
786struct flow_dissector flow_keys_dissector __read_mostly;
787EXPORT_SYMBOL(flow_keys_dissector);
788
789struct flow_dissector flow_keys_buf_dissector __read_mostly;
790
791static int __init init_default_flow_dissectors(void)
792{
793 skb_flow_dissector_init(&flow_keys_dissector,
794 flow_keys_dissector_keys,
795 ARRAY_SIZE(flow_keys_dissector_keys));
796 skb_flow_dissector_init(&flow_keys_buf_dissector,
797 flow_keys_buf_dissector_keys,
798 ARRAY_SIZE(flow_keys_buf_dissector_keys));
799 return 0;
800}
801
802late_initcall_sync(init_default_flow_dissectors);
This page took 0.030558 seconds and 5 git commands to generate.