7536a4669029fb4fcf7de8e25ce32b0110ca10e5
[cascardo/linux.git] / net / core / flow_dissector.c
1 #include <linux/kernel.h>
2 #include <linux/skbuff.h>
3 #include <linux/export.h>
4 #include <linux/ip.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
7 #include <net/ip.h>
8 #include <net/ipv6.h>
9 #include <linux/igmp.h>
10 #include <linux/icmp.h>
11 #include <linux/sctp.h>
12 #include <linux/dccp.h>
13 #include <linux/if_tunnel.h>
14 #include <linux/if_pppox.h>
15 #include <linux/ppp_defs.h>
16 #include <linux/stddef.h>
17 #include <linux/if_ether.h>
18 #include <linux/mpls.h>
19 #include <net/flow_dissector.h>
20 #include <scsi/fc/fc_fcoe.h>
21
22 static bool 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
28 static 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
34 static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
35                                        enum flow_dissector_key_id key_id,
36                                        void *target_container)
37 {
38         return ((char *) target_container) + flow_dissector->offset[key_id];
39 }
40
41 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
42                              const struct flow_dissector_key *key,
43                              unsigned int key_count)
44 {
45         unsigned int i;
46
47         memset(flow_dissector, 0, sizeof(*flow_dissector));
48
49         for (i = 0; i < key_count; i++, key++) {
50                 /* User should make sure that every key target offset is withing
51                  * boundaries of unsigned short.
52                  */
53                 BUG_ON(key->offset > USHRT_MAX);
54                 BUG_ON(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 }
69 EXPORT_SYMBOL(skb_flow_dissector_init);
70
71 /**
72  * __skb_flow_get_ports - extract the upper layer ports and return them
73  * @skb: sk_buff to extract the ports from
74  * @thoff: transport header offset
75  * @ip_proto: protocol for which to get port offset
76  * @data: raw buffer pointer to the packet, if NULL use skb->data
77  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
78  *
79  * The function will try to retrieve the ports at offset thoff + poff where poff
80  * is the protocol port offset returned from proto_ports_offset
81  */
82 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
83                             void *data, int hlen)
84 {
85         int poff = proto_ports_offset(ip_proto);
86
87         if (!data) {
88                 data = skb->data;
89                 hlen = skb_headlen(skb);
90         }
91
92         if (poff >= 0) {
93                 __be32 *ports, _ports;
94
95                 ports = __skb_header_pointer(skb, thoff + poff,
96                                              sizeof(_ports), data, hlen, &_ports);
97                 if (ports)
98                         return *ports;
99         }
100
101         return 0;
102 }
103 EXPORT_SYMBOL(__skb_flow_get_ports);
104
105 /**
106  * __skb_flow_dissect - extract the flow_keys struct and return it
107  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
108  * @flow_dissector: list of keys to dissect
109  * @target_container: target structure to put dissected values into
110  * @data: raw buffer pointer to the packet, if NULL use skb->data
111  * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
112  * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
113  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
114  *
115  * The function will try to retrieve individual keys into target specified
116  * by flow_dissector from either the skbuff or a raw buffer specified by the
117  * rest parameters.
118  *
119  * Caller must take care of zeroing target container memory.
120  */
121 bool __skb_flow_dissect(const struct sk_buff *skb,
122                         struct flow_dissector *flow_dissector,
123                         void *target_container,
124                         void *data, __be16 proto, int nhoff, int hlen,
125                         unsigned int flags)
126 {
127         struct flow_dissector_key_control *key_control;
128         struct flow_dissector_key_basic *key_basic;
129         struct flow_dissector_key_addrs *key_addrs;
130         struct flow_dissector_key_ports *key_ports;
131         struct flow_dissector_key_tags *key_tags;
132         struct flow_dissector_key_keyid *key_keyid;
133         u8 ip_proto = 0;
134         bool ret = false;
135
136         if (!data) {
137                 data = skb->data;
138                 proto = skb->protocol;
139                 nhoff = skb_network_offset(skb);
140                 hlen = skb_headlen(skb);
141         }
142
143         /* It is ensured by skb_flow_dissector_init() that control key will
144          * be always present.
145          */
146         key_control = skb_flow_dissector_target(flow_dissector,
147                                                 FLOW_DISSECTOR_KEY_CONTROL,
148                                                 target_container);
149
150         /* It is ensured by skb_flow_dissector_init() that basic key will
151          * be always present.
152          */
153         key_basic = skb_flow_dissector_target(flow_dissector,
154                                               FLOW_DISSECTOR_KEY_BASIC,
155                                               target_container);
156
157         if (skb_flow_dissector_uses_key(flow_dissector,
158                                         FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
159                 struct ethhdr *eth = eth_hdr(skb);
160                 struct flow_dissector_key_eth_addrs *key_eth_addrs;
161
162                 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
163                                                           FLOW_DISSECTOR_KEY_ETH_ADDRS,
164                                                           target_container);
165                 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
166         }
167
168 again:
169         switch (proto) {
170         case htons(ETH_P_IP): {
171                 const struct iphdr *iph;
172                 struct iphdr _iph;
173 ip:
174                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
175                 if (!iph || iph->ihl < 5)
176                         goto out_bad;
177                 nhoff += iph->ihl * 4;
178
179                 ip_proto = iph->protocol;
180
181                 if (!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
191                 if (ip_is_fragment(iph)) {
192                         key_control->is_fragment = 1;
193
194                         if (iph->frag_off & htons(IP_OFFSET)) {
195                                 goto out_good;
196                         } else {
197                                 key_control->first_frag = 1;
198                                 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
199                                         goto out_good;
200                         }
201                 }
202
203                 break;
204         }
205         case htons(ETH_P_IPV6): {
206                 const struct ipv6hdr *iph;
207                 struct ipv6hdr _iph;
208                 __be32 flow_label;
209
210 ipv6:
211                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
212                 if (!iph)
213                         goto out_bad;
214
215                 ip_proto = iph->nexthdr;
216                 nhoff += sizeof(struct ipv6hdr);
217
218                 if (skb_flow_dissector_uses_key(flow_dissector,
219                                                 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
220                         struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs;
221
222                         key_ipv6_addrs = skb_flow_dissector_target(flow_dissector,
223                                                                    FLOW_DISSECTOR_KEY_IPV6_ADDRS,
224                                                                    target_container);
225
226                         memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs));
227                         key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
228                 }
229
230                 flow_label = ip6_flowlabel(iph);
231                 if (flow_label) {
232                         if (skb_flow_dissector_uses_key(flow_dissector,
233                                 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
234                                 key_tags = skb_flow_dissector_target(flow_dissector,
235                                                                      FLOW_DISSECTOR_KEY_FLOW_LABEL,
236                                                                      target_container);
237                                 key_tags->flow_label = ntohl(flow_label);
238                         }
239                 }
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 (skb_flow_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 (skb_flow_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 (skb_flow_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                 key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
332                 /* fall through */
333         default:
334                 goto out_bad;
335         }
336
337 ip_proto_again:
338         switch (ip_proto) {
339         case IPPROTO_GRE: {
340                 struct gre_hdr {
341                         __be16 flags;
342                         __be16 proto;
343                 } *hdr, _hdr;
344
345                 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
346                 if (!hdr)
347                         goto out_bad;
348                 /*
349                  * Only look inside GRE if version zero and no
350                  * routing
351                  */
352                 if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
353                         break;
354
355                 proto = hdr->proto;
356                 nhoff += 4;
357                 if (hdr->flags & GRE_CSUM)
358                         nhoff += 4;
359                 if (hdr->flags & GRE_KEY) {
360                         const __be32 *keyid;
361                         __be32 _keyid;
362
363                         keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
364                                                      data, hlen, &_keyid);
365
366                         if (!keyid)
367                                 goto out_bad;
368
369                         if (skb_flow_dissector_uses_key(flow_dissector,
370                                                         FLOW_DISSECTOR_KEY_GRE_KEYID)) {
371                                 key_keyid = skb_flow_dissector_target(flow_dissector,
372                                                                       FLOW_DISSECTOR_KEY_GRE_KEYID,
373                                                                       target_container);
374                                 key_keyid->keyid = *keyid;
375                         }
376                         nhoff += 4;
377                 }
378                 if (hdr->flags & GRE_SEQ)
379                         nhoff += 4;
380                 if (proto == htons(ETH_P_TEB)) {
381                         const struct ethhdr *eth;
382                         struct ethhdr _eth;
383
384                         eth = __skb_header_pointer(skb, nhoff,
385                                                    sizeof(_eth),
386                                                    data, hlen, &_eth);
387                         if (!eth)
388                                 goto out_bad;
389                         proto = eth->h_proto;
390                         nhoff += sizeof(*eth);
391                 }
392                 goto again;
393         }
394         case NEXTHDR_HOP:
395         case NEXTHDR_ROUTING:
396         case NEXTHDR_DEST: {
397                 u8 _opthdr[2], *opthdr;
398
399                 if (proto != htons(ETH_P_IPV6))
400                         break;
401
402                 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
403                                               data, hlen, &_opthdr);
404                 if (!opthdr)
405                         goto out_bad;
406
407                 ip_proto = opthdr[0];
408                 nhoff += (opthdr[1] + 1) << 3;
409
410                 goto ip_proto_again;
411         }
412         case IPPROTO_IPIP:
413                 proto = htons(ETH_P_IP);
414                 goto ip;
415         case IPPROTO_IPV6:
416                 proto = htons(ETH_P_IPV6);
417                 goto ipv6;
418         case IPPROTO_MPLS:
419                 proto = htons(ETH_P_MPLS_UC);
420                 goto mpls;
421         default:
422                 break;
423         }
424
425         if (skb_flow_dissector_uses_key(flow_dissector,
426                                         FLOW_DISSECTOR_KEY_PORTS)) {
427                 key_ports = skb_flow_dissector_target(flow_dissector,
428                                                       FLOW_DISSECTOR_KEY_PORTS,
429                                                       target_container);
430                 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
431                                                         data, hlen);
432         }
433
434 out_good:
435         ret = true;
436
437 out_bad:
438         key_basic->n_proto = proto;
439         key_basic->ip_proto = ip_proto;
440         key_control->thoff = (u16)nhoff;
441
442         return ret;
443 }
444 EXPORT_SYMBOL(__skb_flow_dissect);
445
446 static u32 hashrnd __read_mostly;
447 static __always_inline void __flow_hash_secret_init(void)
448 {
449         net_get_random_once(&hashrnd, sizeof(hashrnd));
450 }
451
452 static __always_inline u32 __flow_hash_words(u32 *words, u32 length, u32 keyval)
453 {
454         return jhash2(words, length, keyval);
455 }
456
457 static inline void *flow_keys_hash_start(struct flow_keys *flow)
458 {
459         BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
460         return (void *)flow + FLOW_KEYS_HASH_OFFSET;
461 }
462
463 static inline size_t flow_keys_hash_length(struct flow_keys *flow)
464 {
465         size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
466         BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
467         BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
468                      sizeof(*flow) - sizeof(flow->addrs));
469
470         switch (flow->control.addr_type) {
471         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
472                 diff -= sizeof(flow->addrs.v4addrs);
473                 break;
474         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
475                 diff -= sizeof(flow->addrs.v6addrs);
476                 break;
477         case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
478                 diff -= sizeof(flow->addrs.tipcaddrs);
479                 break;
480         }
481         return (sizeof(*flow) - diff) / sizeof(u32);
482 }
483
484 __be32 flow_get_u32_src(const struct flow_keys *flow)
485 {
486         switch (flow->control.addr_type) {
487         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
488                 return flow->addrs.v4addrs.src;
489         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
490                 return (__force __be32)ipv6_addr_hash(
491                         &flow->addrs.v6addrs.src);
492         case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
493                 return flow->addrs.tipcaddrs.srcnode;
494         default:
495                 return 0;
496         }
497 }
498 EXPORT_SYMBOL(flow_get_u32_src);
499
500 __be32 flow_get_u32_dst(const struct flow_keys *flow)
501 {
502         switch (flow->control.addr_type) {
503         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
504                 return flow->addrs.v4addrs.dst;
505         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
506                 return (__force __be32)ipv6_addr_hash(
507                         &flow->addrs.v6addrs.dst);
508         default:
509                 return 0;
510         }
511 }
512 EXPORT_SYMBOL(flow_get_u32_dst);
513
514 static inline void __flow_hash_consistentify(struct flow_keys *keys)
515 {
516         int addr_diff, i;
517
518         switch (keys->control.addr_type) {
519         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
520                 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
521                             (__force u32)keys->addrs.v4addrs.src;
522                 if ((addr_diff < 0) ||
523                     (addr_diff == 0 &&
524                      ((__force u16)keys->ports.dst <
525                       (__force u16)keys->ports.src))) {
526                         swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
527                         swap(keys->ports.src, keys->ports.dst);
528                 }
529                 break;
530         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
531                 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
532                                    &keys->addrs.v6addrs.src,
533                                    sizeof(keys->addrs.v6addrs.dst));
534                 if ((addr_diff < 0) ||
535                     (addr_diff == 0 &&
536                      ((__force u16)keys->ports.dst <
537                       (__force u16)keys->ports.src))) {
538                         for (i = 0; i < 4; i++)
539                                 swap(keys->addrs.v6addrs.src.s6_addr32[i],
540                                      keys->addrs.v6addrs.dst.s6_addr32[i]);
541                         swap(keys->ports.src, keys->ports.dst);
542                 }
543                 break;
544         }
545 }
546
547 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
548 {
549         u32 hash;
550
551         __flow_hash_consistentify(keys);
552
553         hash = __flow_hash_words((u32 *)flow_keys_hash_start(keys),
554                                  flow_keys_hash_length(keys), keyval);
555         if (!hash)
556                 hash = 1;
557
558         return hash;
559 }
560
561 u32 flow_hash_from_keys(struct flow_keys *keys)
562 {
563         __flow_hash_secret_init();
564         return __flow_hash_from_keys(keys, hashrnd);
565 }
566 EXPORT_SYMBOL(flow_hash_from_keys);
567
568 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
569                                   struct flow_keys *keys, u32 keyval)
570 {
571         if (!skb_flow_dissect_flow_keys(skb, keys, 0))
572                 return 0;
573
574         return __flow_hash_from_keys(keys, keyval);
575 }
576
577 struct _flow_keys_digest_data {
578         __be16  n_proto;
579         u8      ip_proto;
580         u8      padding;
581         __be32  ports;
582         __be32  src;
583         __be32  dst;
584 };
585
586 void make_flow_keys_digest(struct flow_keys_digest *digest,
587                            const struct flow_keys *flow)
588 {
589         struct _flow_keys_digest_data *data =
590             (struct _flow_keys_digest_data *)digest;
591
592         BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
593
594         memset(digest, 0, sizeof(*digest));
595
596         data->n_proto = flow->basic.n_proto;
597         data->ip_proto = flow->basic.ip_proto;
598         data->ports = flow->ports.ports;
599         data->src = flow->addrs.v4addrs.src;
600         data->dst = flow->addrs.v4addrs.dst;
601 }
602 EXPORT_SYMBOL(make_flow_keys_digest);
603
604 /**
605  * __skb_get_hash: calculate a flow hash
606  * @skb: sk_buff to calculate flow hash from
607  *
608  * This function calculates a flow hash based on src/dst addresses
609  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
610  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
611  * if hash is a canonical 4-tuple hash over transport ports.
612  */
613 void __skb_get_hash(struct sk_buff *skb)
614 {
615         struct flow_keys keys;
616         u32 hash;
617
618         __flow_hash_secret_init();
619
620         hash = ___skb_get_hash(skb, &keys, hashrnd);
621         if (!hash)
622                 return;
623
624         __skb_set_sw_hash(skb, hash,
625                           flow_keys_have_l4(&keys));
626 }
627 EXPORT_SYMBOL(__skb_get_hash);
628
629 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
630 {
631         struct flow_keys keys;
632
633         return ___skb_get_hash(skb, &keys, perturb);
634 }
635 EXPORT_SYMBOL(skb_get_hash_perturb);
636
637 __u32 __skb_get_hash_flowi6(struct sk_buff *skb, struct flowi6 *fl6)
638 {
639         struct flow_keys keys;
640
641         memset(&keys, 0, sizeof(keys));
642
643         memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
644                sizeof(keys.addrs.v6addrs.src));
645         memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
646                sizeof(keys.addrs.v6addrs.dst));
647         keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
648         keys.ports.src = fl6->fl6_sport;
649         keys.ports.dst = fl6->fl6_dport;
650         keys.keyid.keyid = fl6->fl6_gre_key;
651         keys.tags.flow_label = (__force u32)fl6->flowlabel;
652         keys.basic.ip_proto = fl6->flowi6_proto;
653
654         __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
655                           flow_keys_have_l4(&keys));
656
657         return skb->hash;
658 }
659 EXPORT_SYMBOL(__skb_get_hash_flowi6);
660
661 __u32 __skb_get_hash_flowi4(struct sk_buff *skb, struct flowi4 *fl4)
662 {
663         struct flow_keys keys;
664
665         memset(&keys, 0, sizeof(keys));
666
667         keys.addrs.v4addrs.src = fl4->saddr;
668         keys.addrs.v4addrs.dst = fl4->daddr;
669         keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
670         keys.ports.src = fl4->fl4_sport;
671         keys.ports.dst = fl4->fl4_dport;
672         keys.keyid.keyid = fl4->fl4_gre_key;
673         keys.basic.ip_proto = fl4->flowi4_proto;
674
675         __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
676                           flow_keys_have_l4(&keys));
677
678         return skb->hash;
679 }
680 EXPORT_SYMBOL(__skb_get_hash_flowi4);
681
682 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
683                    const struct flow_keys *keys, int hlen)
684 {
685         u32 poff = keys->control.thoff;
686
687         switch (keys->basic.ip_proto) {
688         case IPPROTO_TCP: {
689                 /* access doff as u8 to avoid unaligned access */
690                 const u8 *doff;
691                 u8 _doff;
692
693                 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
694                                             data, hlen, &_doff);
695                 if (!doff)
696                         return poff;
697
698                 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
699                 break;
700         }
701         case IPPROTO_UDP:
702         case IPPROTO_UDPLITE:
703                 poff += sizeof(struct udphdr);
704                 break;
705         /* For the rest, we do not really care about header
706          * extensions at this point for now.
707          */
708         case IPPROTO_ICMP:
709                 poff += sizeof(struct icmphdr);
710                 break;
711         case IPPROTO_ICMPV6:
712                 poff += sizeof(struct icmp6hdr);
713                 break;
714         case IPPROTO_IGMP:
715                 poff += sizeof(struct igmphdr);
716                 break;
717         case IPPROTO_DCCP:
718                 poff += sizeof(struct dccp_hdr);
719                 break;
720         case IPPROTO_SCTP:
721                 poff += sizeof(struct sctphdr);
722                 break;
723         }
724
725         return poff;
726 }
727
728 /**
729  * skb_get_poff - get the offset to the payload
730  * @skb: sk_buff to get the payload offset from
731  *
732  * The function will get the offset to the payload as far as it could
733  * be dissected.  The main user is currently BPF, so that we can dynamically
734  * truncate packets without needing to push actual payload to the user
735  * space and can analyze headers only, instead.
736  */
737 u32 skb_get_poff(const struct sk_buff *skb)
738 {
739         struct flow_keys keys;
740
741         if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
742                 return 0;
743
744         return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
745 }
746
747 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
748         {
749                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
750                 .offset = offsetof(struct flow_keys, control),
751         },
752         {
753                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
754                 .offset = offsetof(struct flow_keys, basic),
755         },
756         {
757                 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
758                 .offset = offsetof(struct flow_keys, addrs.v4addrs),
759         },
760         {
761                 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
762                 .offset = offsetof(struct flow_keys, addrs.v6addrs),
763         },
764         {
765                 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
766                 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
767         },
768         {
769                 .key_id = FLOW_DISSECTOR_KEY_PORTS,
770                 .offset = offsetof(struct flow_keys, ports),
771         },
772         {
773                 .key_id = FLOW_DISSECTOR_KEY_VLANID,
774                 .offset = offsetof(struct flow_keys, tags),
775         },
776         {
777                 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
778                 .offset = offsetof(struct flow_keys, tags),
779         },
780         {
781                 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
782                 .offset = offsetof(struct flow_keys, keyid),
783         },
784 };
785
786 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
787         {
788                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
789                 .offset = offsetof(struct flow_keys, control),
790         },
791         {
792                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
793                 .offset = offsetof(struct flow_keys, basic),
794         },
795 };
796
797 struct flow_dissector flow_keys_dissector __read_mostly;
798 EXPORT_SYMBOL(flow_keys_dissector);
799
800 struct flow_dissector flow_keys_buf_dissector __read_mostly;
801
802 static int __init init_default_flow_dissectors(void)
803 {
804         skb_flow_dissector_init(&flow_keys_dissector,
805                                 flow_keys_dissector_keys,
806                                 ARRAY_SIZE(flow_keys_dissector_keys));
807         skb_flow_dissector_init(&flow_keys_buf_dissector,
808                                 flow_keys_buf_dissector_keys,
809                                 ARRAY_SIZE(flow_keys_buf_dissector_keys));
810         return 0;
811 }
812
813 late_initcall_sync(init_default_flow_dissectors);