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