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