GNU Linux-libre 4.19.211-gnu1
[releases.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/dsa.h>
8 #include <net/dst_metadata.h>
9 #include <net/ip.h>
10 #include <net/ipv6.h>
11 #include <net/gre.h>
12 #include <net/pptp.h>
13 #include <net/tipc.h>
14 #include <linux/igmp.h>
15 #include <linux/icmp.h>
16 #include <linux/sctp.h>
17 #include <linux/dccp.h>
18 #include <linux/if_tunnel.h>
19 #include <linux/if_pppox.h>
20 #include <linux/ppp_defs.h>
21 #include <linux/stddef.h>
22 #include <linux/if_ether.h>
23 #include <linux/mpls.h>
24 #include <linux/tcp.h>
25 #include <net/flow_dissector.h>
26 #include <scsi/fc/fc_fcoe.h>
27 #include <uapi/linux/batadv_packet.h>
28
29 static void dissector_set_key(struct flow_dissector *flow_dissector,
30                               enum flow_dissector_key_id key_id)
31 {
32         flow_dissector->used_keys |= (1 << key_id);
33 }
34
35 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
36                              const struct flow_dissector_key *key,
37                              unsigned int key_count)
38 {
39         unsigned int i;
40
41         memset(flow_dissector, 0, sizeof(*flow_dissector));
42
43         for (i = 0; i < key_count; i++, key++) {
44                 /* User should make sure that every key target offset is withing
45                  * boundaries of unsigned short.
46                  */
47                 BUG_ON(key->offset > USHRT_MAX);
48                 BUG_ON(dissector_uses_key(flow_dissector,
49                                           key->key_id));
50
51                 dissector_set_key(flow_dissector, key->key_id);
52                 flow_dissector->offset[key->key_id] = key->offset;
53         }
54
55         /* Ensure that the dissector always includes control and basic key.
56          * That way we are able to avoid handling lack of these in fast path.
57          */
58         BUG_ON(!dissector_uses_key(flow_dissector,
59                                    FLOW_DISSECTOR_KEY_CONTROL));
60         BUG_ON(!dissector_uses_key(flow_dissector,
61                                    FLOW_DISSECTOR_KEY_BASIC));
62 }
63 EXPORT_SYMBOL(skb_flow_dissector_init);
64
65 /**
66  * skb_flow_get_be16 - extract be16 entity
67  * @skb: sk_buff to extract from
68  * @poff: offset to extract at
69  * @data: raw buffer pointer to the packet
70  * @hlen: packet header length
71  *
72  * The function will try to retrieve a be32 entity at
73  * offset poff
74  */
75 static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
76                                 void *data, int hlen)
77 {
78         __be16 *u, _u;
79
80         u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
81         if (u)
82                 return *u;
83
84         return 0;
85 }
86
87 /**
88  * __skb_flow_get_ports - extract the upper layer ports and return them
89  * @skb: sk_buff to extract the ports from
90  * @thoff: transport header offset
91  * @ip_proto: protocol for which to get port offset
92  * @data: raw buffer pointer to the packet, if NULL use skb->data
93  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
94  *
95  * The function will try to retrieve the ports at offset thoff + poff where poff
96  * is the protocol port offset returned from proto_ports_offset
97  */
98 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
99                             void *data, int hlen)
100 {
101         int poff = proto_ports_offset(ip_proto);
102
103         if (!data) {
104                 data = skb->data;
105                 hlen = skb_headlen(skb);
106         }
107
108         if (poff >= 0) {
109                 __be32 *ports, _ports;
110
111                 ports = __skb_header_pointer(skb, thoff + poff,
112                                              sizeof(_ports), data, hlen, &_ports);
113                 if (ports)
114                         return *ports;
115         }
116
117         return 0;
118 }
119 EXPORT_SYMBOL(__skb_flow_get_ports);
120
121 static void
122 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
123                                    struct flow_dissector *flow_dissector,
124                                    void *target_container)
125 {
126         struct flow_dissector_key_control *ctrl;
127
128         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
129                 return;
130
131         ctrl = skb_flow_dissector_target(flow_dissector,
132                                          FLOW_DISSECTOR_KEY_ENC_CONTROL,
133                                          target_container);
134         ctrl->addr_type = type;
135 }
136
137 void
138 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
139                              struct flow_dissector *flow_dissector,
140                              void *target_container)
141 {
142         struct ip_tunnel_info *info;
143         struct ip_tunnel_key *key;
144
145         /* A quick check to see if there might be something to do. */
146         if (!dissector_uses_key(flow_dissector,
147                                 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
148             !dissector_uses_key(flow_dissector,
149                                 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
150             !dissector_uses_key(flow_dissector,
151                                 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
152             !dissector_uses_key(flow_dissector,
153                                 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
154             !dissector_uses_key(flow_dissector,
155                                 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
156             !dissector_uses_key(flow_dissector,
157                                 FLOW_DISSECTOR_KEY_ENC_IP) &&
158             !dissector_uses_key(flow_dissector,
159                                 FLOW_DISSECTOR_KEY_ENC_OPTS))
160                 return;
161
162         info = skb_tunnel_info(skb);
163         if (!info)
164                 return;
165
166         key = &info->key;
167
168         switch (ip_tunnel_info_af(info)) {
169         case AF_INET:
170                 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
171                                                    flow_dissector,
172                                                    target_container);
173                 if (dissector_uses_key(flow_dissector,
174                                        FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
175                         struct flow_dissector_key_ipv4_addrs *ipv4;
176
177                         ipv4 = skb_flow_dissector_target(flow_dissector,
178                                                          FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
179                                                          target_container);
180                         ipv4->src = key->u.ipv4.src;
181                         ipv4->dst = key->u.ipv4.dst;
182                 }
183                 break;
184         case AF_INET6:
185                 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
186                                                    flow_dissector,
187                                                    target_container);
188                 if (dissector_uses_key(flow_dissector,
189                                        FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
190                         struct flow_dissector_key_ipv6_addrs *ipv6;
191
192                         ipv6 = skb_flow_dissector_target(flow_dissector,
193                                                          FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
194                                                          target_container);
195                         ipv6->src = key->u.ipv6.src;
196                         ipv6->dst = key->u.ipv6.dst;
197                 }
198                 break;
199         }
200
201         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
202                 struct flow_dissector_key_keyid *keyid;
203
204                 keyid = skb_flow_dissector_target(flow_dissector,
205                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
206                                                   target_container);
207                 keyid->keyid = tunnel_id_to_key32(key->tun_id);
208         }
209
210         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
211                 struct flow_dissector_key_ports *tp;
212
213                 tp = skb_flow_dissector_target(flow_dissector,
214                                                FLOW_DISSECTOR_KEY_ENC_PORTS,
215                                                target_container);
216                 tp->src = key->tp_src;
217                 tp->dst = key->tp_dst;
218         }
219
220         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
221                 struct flow_dissector_key_ip *ip;
222
223                 ip = skb_flow_dissector_target(flow_dissector,
224                                                FLOW_DISSECTOR_KEY_ENC_IP,
225                                                target_container);
226                 ip->tos = key->tos;
227                 ip->ttl = key->ttl;
228         }
229
230         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
231                 struct flow_dissector_key_enc_opts *enc_opt;
232
233                 enc_opt = skb_flow_dissector_target(flow_dissector,
234                                                     FLOW_DISSECTOR_KEY_ENC_OPTS,
235                                                     target_container);
236
237                 if (info->options_len) {
238                         enc_opt->len = info->options_len;
239                         ip_tunnel_info_opts_get(enc_opt->data, info);
240                         enc_opt->dst_opt_type = info->key.tun_flags &
241                                                 TUNNEL_OPTIONS_PRESENT;
242                 }
243         }
244 }
245 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
246
247 static enum flow_dissect_ret
248 __skb_flow_dissect_mpls(const struct sk_buff *skb,
249                         struct flow_dissector *flow_dissector,
250                         void *target_container, void *data, int nhoff, int hlen)
251 {
252         struct flow_dissector_key_keyid *key_keyid;
253         struct mpls_label *hdr, _hdr[2];
254         u32 entry, label;
255
256         if (!dissector_uses_key(flow_dissector,
257                                 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
258             !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
259                 return FLOW_DISSECT_RET_OUT_GOOD;
260
261         hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
262                                    hlen, &_hdr);
263         if (!hdr)
264                 return FLOW_DISSECT_RET_OUT_BAD;
265
266         entry = ntohl(hdr[0].entry);
267         label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
268
269         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
270                 struct flow_dissector_key_mpls *key_mpls;
271
272                 key_mpls = skb_flow_dissector_target(flow_dissector,
273                                                      FLOW_DISSECTOR_KEY_MPLS,
274                                                      target_container);
275                 key_mpls->mpls_label = label;
276                 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
277                                         >> MPLS_LS_TTL_SHIFT;
278                 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
279                                         >> MPLS_LS_TC_SHIFT;
280                 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
281                                         >> MPLS_LS_S_SHIFT;
282         }
283
284         if (label == MPLS_LABEL_ENTROPY) {
285                 key_keyid = skb_flow_dissector_target(flow_dissector,
286                                                       FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
287                                                       target_container);
288                 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
289         }
290         return FLOW_DISSECT_RET_OUT_GOOD;
291 }
292
293 static enum flow_dissect_ret
294 __skb_flow_dissect_arp(const struct sk_buff *skb,
295                        struct flow_dissector *flow_dissector,
296                        void *target_container, void *data, int nhoff, int hlen)
297 {
298         struct flow_dissector_key_arp *key_arp;
299         struct {
300                 unsigned char ar_sha[ETH_ALEN];
301                 unsigned char ar_sip[4];
302                 unsigned char ar_tha[ETH_ALEN];
303                 unsigned char ar_tip[4];
304         } *arp_eth, _arp_eth;
305         const struct arphdr *arp;
306         struct arphdr _arp;
307
308         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
309                 return FLOW_DISSECT_RET_OUT_GOOD;
310
311         arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
312                                    hlen, &_arp);
313         if (!arp)
314                 return FLOW_DISSECT_RET_OUT_BAD;
315
316         if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
317             arp->ar_pro != htons(ETH_P_IP) ||
318             arp->ar_hln != ETH_ALEN ||
319             arp->ar_pln != 4 ||
320             (arp->ar_op != htons(ARPOP_REPLY) &&
321              arp->ar_op != htons(ARPOP_REQUEST)))
322                 return FLOW_DISSECT_RET_OUT_BAD;
323
324         arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
325                                        sizeof(_arp_eth), data,
326                                        hlen, &_arp_eth);
327         if (!arp_eth)
328                 return FLOW_DISSECT_RET_OUT_BAD;
329
330         key_arp = skb_flow_dissector_target(flow_dissector,
331                                             FLOW_DISSECTOR_KEY_ARP,
332                                             target_container);
333
334         memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
335         memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
336
337         /* Only store the lower byte of the opcode;
338          * this covers ARPOP_REPLY and ARPOP_REQUEST.
339          */
340         key_arp->op = ntohs(arp->ar_op) & 0xff;
341
342         ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
343         ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
344
345         return FLOW_DISSECT_RET_OUT_GOOD;
346 }
347
348 static enum flow_dissect_ret
349 __skb_flow_dissect_gre(const struct sk_buff *skb,
350                        struct flow_dissector_key_control *key_control,
351                        struct flow_dissector *flow_dissector,
352                        void *target_container, void *data,
353                        __be16 *p_proto, int *p_nhoff, int *p_hlen,
354                        unsigned int flags)
355 {
356         struct flow_dissector_key_keyid *key_keyid;
357         struct gre_base_hdr *hdr, _hdr;
358         int offset = 0;
359         u16 gre_ver;
360
361         hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
362                                    data, *p_hlen, &_hdr);
363         if (!hdr)
364                 return FLOW_DISSECT_RET_OUT_BAD;
365
366         /* Only look inside GRE without routing */
367         if (hdr->flags & GRE_ROUTING)
368                 return FLOW_DISSECT_RET_OUT_GOOD;
369
370         /* Only look inside GRE for version 0 and 1 */
371         gre_ver = ntohs(hdr->flags & GRE_VERSION);
372         if (gre_ver > 1)
373                 return FLOW_DISSECT_RET_OUT_GOOD;
374
375         *p_proto = hdr->protocol;
376         if (gre_ver) {
377                 /* Version1 must be PPTP, and check the flags */
378                 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
379                         return FLOW_DISSECT_RET_OUT_GOOD;
380         }
381
382         offset += sizeof(struct gre_base_hdr);
383
384         if (hdr->flags & GRE_CSUM)
385                 offset += sizeof(((struct gre_full_hdr *) 0)->csum) +
386                           sizeof(((struct gre_full_hdr *) 0)->reserved1);
387
388         if (hdr->flags & GRE_KEY) {
389                 const __be32 *keyid;
390                 __be32 _keyid;
391
392                 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
393                                              sizeof(_keyid),
394                                              data, *p_hlen, &_keyid);
395                 if (!keyid)
396                         return FLOW_DISSECT_RET_OUT_BAD;
397
398                 if (dissector_uses_key(flow_dissector,
399                                        FLOW_DISSECTOR_KEY_GRE_KEYID)) {
400                         key_keyid = skb_flow_dissector_target(flow_dissector,
401                                                               FLOW_DISSECTOR_KEY_GRE_KEYID,
402                                                               target_container);
403                         if (gre_ver == 0)
404                                 key_keyid->keyid = *keyid;
405                         else
406                                 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
407                 }
408                 offset += sizeof(((struct gre_full_hdr *) 0)->key);
409         }
410
411         if (hdr->flags & GRE_SEQ)
412                 offset += sizeof(((struct pptp_gre_header *) 0)->seq);
413
414         if (gre_ver == 0) {
415                 if (*p_proto == htons(ETH_P_TEB)) {
416                         const struct ethhdr *eth;
417                         struct ethhdr _eth;
418
419                         eth = __skb_header_pointer(skb, *p_nhoff + offset,
420                                                    sizeof(_eth),
421                                                    data, *p_hlen, &_eth);
422                         if (!eth)
423                                 return FLOW_DISSECT_RET_OUT_BAD;
424                         *p_proto = eth->h_proto;
425                         offset += sizeof(*eth);
426
427                         /* Cap headers that we access via pointers at the
428                          * end of the Ethernet header as our maximum alignment
429                          * at that point is only 2 bytes.
430                          */
431                         if (NET_IP_ALIGN)
432                                 *p_hlen = *p_nhoff + offset;
433                 }
434         } else { /* version 1, must be PPTP */
435                 u8 _ppp_hdr[PPP_HDRLEN];
436                 u8 *ppp_hdr;
437
438                 if (hdr->flags & GRE_ACK)
439                         offset += sizeof(((struct pptp_gre_header *) 0)->ack);
440
441                 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
442                                                sizeof(_ppp_hdr),
443                                                data, *p_hlen, _ppp_hdr);
444                 if (!ppp_hdr)
445                         return FLOW_DISSECT_RET_OUT_BAD;
446
447                 switch (PPP_PROTOCOL(ppp_hdr)) {
448                 case PPP_IP:
449                         *p_proto = htons(ETH_P_IP);
450                         break;
451                 case PPP_IPV6:
452                         *p_proto = htons(ETH_P_IPV6);
453                         break;
454                 default:
455                         /* Could probably catch some more like MPLS */
456                         break;
457                 }
458
459                 offset += PPP_HDRLEN;
460         }
461
462         *p_nhoff += offset;
463         key_control->flags |= FLOW_DIS_ENCAPSULATION;
464         if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
465                 return FLOW_DISSECT_RET_OUT_GOOD;
466
467         return FLOW_DISSECT_RET_PROTO_AGAIN;
468 }
469
470 /**
471  * __skb_flow_dissect_batadv() - dissect batman-adv header
472  * @skb: sk_buff to with the batman-adv header
473  * @key_control: flow dissectors control key
474  * @data: raw buffer pointer to the packet, if NULL use skb->data
475  * @p_proto: pointer used to update the protocol to process next
476  * @p_nhoff: pointer used to update inner network header offset
477  * @hlen: packet header length
478  * @flags: any combination of FLOW_DISSECTOR_F_*
479  *
480  * ETH_P_BATMAN packets are tried to be dissected. Only
481  * &struct batadv_unicast packets are actually processed because they contain an
482  * inner ethernet header and are usually followed by actual network header. This
483  * allows the flow dissector to continue processing the packet.
484  *
485  * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
486  *  FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
487  *  otherwise FLOW_DISSECT_RET_OUT_BAD
488  */
489 static enum flow_dissect_ret
490 __skb_flow_dissect_batadv(const struct sk_buff *skb,
491                           struct flow_dissector_key_control *key_control,
492                           void *data, __be16 *p_proto, int *p_nhoff, int hlen,
493                           unsigned int flags)
494 {
495         struct {
496                 struct batadv_unicast_packet batadv_unicast;
497                 struct ethhdr eth;
498         } *hdr, _hdr;
499
500         hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
501                                    &_hdr);
502         if (!hdr)
503                 return FLOW_DISSECT_RET_OUT_BAD;
504
505         if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
506                 return FLOW_DISSECT_RET_OUT_BAD;
507
508         if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
509                 return FLOW_DISSECT_RET_OUT_BAD;
510
511         *p_proto = hdr->eth.h_proto;
512         *p_nhoff += sizeof(*hdr);
513
514         key_control->flags |= FLOW_DIS_ENCAPSULATION;
515         if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
516                 return FLOW_DISSECT_RET_OUT_GOOD;
517
518         return FLOW_DISSECT_RET_PROTO_AGAIN;
519 }
520
521 static void
522 __skb_flow_dissect_tcp(const struct sk_buff *skb,
523                        struct flow_dissector *flow_dissector,
524                        void *target_container, void *data, int thoff, int hlen)
525 {
526         struct flow_dissector_key_tcp *key_tcp;
527         struct tcphdr *th, _th;
528
529         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
530                 return;
531
532         th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
533         if (!th)
534                 return;
535
536         if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
537                 return;
538
539         key_tcp = skb_flow_dissector_target(flow_dissector,
540                                             FLOW_DISSECTOR_KEY_TCP,
541                                             target_container);
542         key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
543 }
544
545 static void
546 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
547                         struct flow_dissector *flow_dissector,
548                         void *target_container, void *data, const struct iphdr *iph)
549 {
550         struct flow_dissector_key_ip *key_ip;
551
552         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
553                 return;
554
555         key_ip = skb_flow_dissector_target(flow_dissector,
556                                            FLOW_DISSECTOR_KEY_IP,
557                                            target_container);
558         key_ip->tos = iph->tos;
559         key_ip->ttl = iph->ttl;
560 }
561
562 static void
563 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
564                         struct flow_dissector *flow_dissector,
565                         void *target_container, void *data, const struct ipv6hdr *iph)
566 {
567         struct flow_dissector_key_ip *key_ip;
568
569         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
570                 return;
571
572         key_ip = skb_flow_dissector_target(flow_dissector,
573                                            FLOW_DISSECTOR_KEY_IP,
574                                            target_container);
575         key_ip->tos = ipv6_get_dsfield(iph);
576         key_ip->ttl = iph->hop_limit;
577 }
578
579 /* Maximum number of protocol headers that can be parsed in
580  * __skb_flow_dissect
581  */
582 #define MAX_FLOW_DISSECT_HDRS   15
583
584 static bool skb_flow_dissect_allowed(int *num_hdrs)
585 {
586         ++*num_hdrs;
587
588         return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
589 }
590
591 /**
592  * __skb_flow_dissect - extract the flow_keys struct and return it
593  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
594  * @flow_dissector: list of keys to dissect
595  * @target_container: target structure to put dissected values into
596  * @data: raw buffer pointer to the packet, if NULL use skb->data
597  * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
598  * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
599  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
600  *
601  * The function will try to retrieve individual keys into target specified
602  * by flow_dissector from either the skbuff or a raw buffer specified by the
603  * rest parameters.
604  *
605  * Caller must take care of zeroing target container memory.
606  */
607 bool __skb_flow_dissect(const struct sk_buff *skb,
608                         struct flow_dissector *flow_dissector,
609                         void *target_container,
610                         void *data, __be16 proto, int nhoff, int hlen,
611                         unsigned int flags)
612 {
613         struct flow_dissector_key_control *key_control;
614         struct flow_dissector_key_basic *key_basic;
615         struct flow_dissector_key_addrs *key_addrs;
616         struct flow_dissector_key_ports *key_ports;
617         struct flow_dissector_key_icmp *key_icmp;
618         struct flow_dissector_key_tags *key_tags;
619         struct flow_dissector_key_vlan *key_vlan;
620         enum flow_dissect_ret fdret;
621         enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
622         int num_hdrs = 0;
623         u8 ip_proto = 0;
624         bool ret;
625
626         if (!data) {
627                 data = skb->data;
628                 proto = skb_vlan_tag_present(skb) ?
629                          skb->vlan_proto : skb->protocol;
630                 nhoff = skb_network_offset(skb);
631                 hlen = skb_headlen(skb);
632 #if IS_ENABLED(CONFIG_NET_DSA)
633                 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
634                              proto == htons(ETH_P_XDSA))) {
635                         const struct dsa_device_ops *ops;
636                         int offset = 0;
637
638                         ops = skb->dev->dsa_ptr->tag_ops;
639                         if (ops->flow_dissect &&
640                             !ops->flow_dissect(skb, &proto, &offset)) {
641                                 hlen -= offset;
642                                 nhoff += offset;
643                         }
644                 }
645 #endif
646         }
647
648         /* It is ensured by skb_flow_dissector_init() that control key will
649          * be always present.
650          */
651         key_control = skb_flow_dissector_target(flow_dissector,
652                                                 FLOW_DISSECTOR_KEY_CONTROL,
653                                                 target_container);
654
655         /* It is ensured by skb_flow_dissector_init() that basic key will
656          * be always present.
657          */
658         key_basic = skb_flow_dissector_target(flow_dissector,
659                                               FLOW_DISSECTOR_KEY_BASIC,
660                                               target_container);
661
662         if (dissector_uses_key(flow_dissector,
663                                FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
664                 struct ethhdr *eth = eth_hdr(skb);
665                 struct flow_dissector_key_eth_addrs *key_eth_addrs;
666
667                 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
668                                                           FLOW_DISSECTOR_KEY_ETH_ADDRS,
669                                                           target_container);
670                 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
671         }
672
673 proto_again:
674         fdret = FLOW_DISSECT_RET_CONTINUE;
675
676         switch (proto) {
677         case htons(ETH_P_IP): {
678                 const struct iphdr *iph;
679                 struct iphdr _iph;
680
681                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
682                 if (!iph || iph->ihl < 5) {
683                         fdret = FLOW_DISSECT_RET_OUT_BAD;
684                         break;
685                 }
686
687                 nhoff += iph->ihl * 4;
688
689                 ip_proto = iph->protocol;
690
691                 if (dissector_uses_key(flow_dissector,
692                                        FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
693                         key_addrs = skb_flow_dissector_target(flow_dissector,
694                                                               FLOW_DISSECTOR_KEY_IPV4_ADDRS,
695                                                               target_container);
696
697                         memcpy(&key_addrs->v4addrs.src, &iph->saddr,
698                                sizeof(key_addrs->v4addrs.src));
699                         memcpy(&key_addrs->v4addrs.dst, &iph->daddr,
700                                sizeof(key_addrs->v4addrs.dst));
701                         key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
702                 }
703
704                 if (ip_is_fragment(iph)) {
705                         key_control->flags |= FLOW_DIS_IS_FRAGMENT;
706
707                         if (iph->frag_off & htons(IP_OFFSET)) {
708                                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
709                                 break;
710                         } else {
711                                 key_control->flags |= FLOW_DIS_FIRST_FRAG;
712                                 if (!(flags &
713                                       FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
714                                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
715                                         break;
716                                 }
717                         }
718                 }
719
720                 __skb_flow_dissect_ipv4(skb, flow_dissector,
721                                         target_container, data, iph);
722
723                 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) {
724                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
725                         break;
726                 }
727
728                 break;
729         }
730         case htons(ETH_P_IPV6): {
731                 const struct ipv6hdr *iph;
732                 struct ipv6hdr _iph;
733
734                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
735                 if (!iph) {
736                         fdret = FLOW_DISSECT_RET_OUT_BAD;
737                         break;
738                 }
739
740                 ip_proto = iph->nexthdr;
741                 nhoff += sizeof(struct ipv6hdr);
742
743                 if (dissector_uses_key(flow_dissector,
744                                        FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
745                         key_addrs = skb_flow_dissector_target(flow_dissector,
746                                                               FLOW_DISSECTOR_KEY_IPV6_ADDRS,
747                                                               target_container);
748
749                         memcpy(&key_addrs->v6addrs.src, &iph->saddr,
750                                sizeof(key_addrs->v6addrs.src));
751                         memcpy(&key_addrs->v6addrs.dst, &iph->daddr,
752                                sizeof(key_addrs->v6addrs.dst));
753                         key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
754                 }
755
756                 if ((dissector_uses_key(flow_dissector,
757                                         FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
758                      (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
759                     ip6_flowlabel(iph)) {
760                         __be32 flow_label = ip6_flowlabel(iph);
761
762                         if (dissector_uses_key(flow_dissector,
763                                                FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
764                                 key_tags = skb_flow_dissector_target(flow_dissector,
765                                                                      FLOW_DISSECTOR_KEY_FLOW_LABEL,
766                                                                      target_container);
767                                 key_tags->flow_label = ntohl(flow_label);
768                         }
769                         if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
770                                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
771                                 break;
772                         }
773                 }
774
775                 __skb_flow_dissect_ipv6(skb, flow_dissector,
776                                         target_container, data, iph);
777
778                 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
779                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
780
781                 break;
782         }
783         case htons(ETH_P_8021AD):
784         case htons(ETH_P_8021Q): {
785                 const struct vlan_hdr *vlan = NULL;
786                 struct vlan_hdr _vlan;
787                 __be16 saved_vlan_tpid = proto;
788
789                 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
790                     skb && skb_vlan_tag_present(skb)) {
791                         proto = skb->protocol;
792                 } else {
793                         vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
794                                                     data, hlen, &_vlan);
795                         if (!vlan) {
796                                 fdret = FLOW_DISSECT_RET_OUT_BAD;
797                                 break;
798                         }
799
800                         proto = vlan->h_vlan_encapsulated_proto;
801                         nhoff += sizeof(*vlan);
802                 }
803
804                 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
805                         dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
806                 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
807                         dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
808                 } else {
809                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
810                         break;
811                 }
812
813                 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
814                         key_vlan = skb_flow_dissector_target(flow_dissector,
815                                                              dissector_vlan,
816                                                              target_container);
817
818                         if (!vlan) {
819                                 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
820                                 key_vlan->vlan_priority =
821                                         (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
822                         } else {
823                                 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
824                                         VLAN_VID_MASK;
825                                 key_vlan->vlan_priority =
826                                         (ntohs(vlan->h_vlan_TCI) &
827                                          VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
828                         }
829                         key_vlan->vlan_tpid = saved_vlan_tpid;
830                 }
831
832                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
833                 break;
834         }
835         case htons(ETH_P_PPP_SES): {
836                 struct {
837                         struct pppoe_hdr hdr;
838                         __be16 proto;
839                 } *hdr, _hdr;
840                 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
841                 if (!hdr) {
842                         fdret = FLOW_DISSECT_RET_OUT_BAD;
843                         break;
844                 }
845
846                 proto = hdr->proto;
847                 nhoff += PPPOE_SES_HLEN;
848                 switch (proto) {
849                 case htons(PPP_IP):
850                         proto = htons(ETH_P_IP);
851                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
852                         break;
853                 case htons(PPP_IPV6):
854                         proto = htons(ETH_P_IPV6);
855                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
856                         break;
857                 default:
858                         fdret = FLOW_DISSECT_RET_OUT_BAD;
859                         break;
860                 }
861                 break;
862         }
863         case htons(ETH_P_TIPC): {
864                 struct tipc_basic_hdr *hdr, _hdr;
865
866                 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
867                                            data, hlen, &_hdr);
868                 if (!hdr) {
869                         fdret = FLOW_DISSECT_RET_OUT_BAD;
870                         break;
871                 }
872
873                 if (dissector_uses_key(flow_dissector,
874                                        FLOW_DISSECTOR_KEY_TIPC)) {
875                         key_addrs = skb_flow_dissector_target(flow_dissector,
876                                                               FLOW_DISSECTOR_KEY_TIPC,
877                                                               target_container);
878                         key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
879                         key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
880                 }
881                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
882                 break;
883         }
884
885         case htons(ETH_P_MPLS_UC):
886         case htons(ETH_P_MPLS_MC):
887                 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
888                                                 target_container, data,
889                                                 nhoff, hlen);
890                 break;
891         case htons(ETH_P_FCOE):
892                 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
893                         fdret = FLOW_DISSECT_RET_OUT_BAD;
894                         break;
895                 }
896
897                 nhoff += FCOE_HEADER_LEN;
898                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
899                 break;
900
901         case htons(ETH_P_ARP):
902         case htons(ETH_P_RARP):
903                 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
904                                                target_container, data,
905                                                nhoff, hlen);
906                 break;
907
908         case htons(ETH_P_BATMAN):
909                 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
910                                                   &proto, &nhoff, hlen, flags);
911                 break;
912
913         default:
914                 fdret = FLOW_DISSECT_RET_OUT_BAD;
915                 break;
916         }
917
918         /* Process result of proto processing */
919         switch (fdret) {
920         case FLOW_DISSECT_RET_OUT_GOOD:
921                 goto out_good;
922         case FLOW_DISSECT_RET_PROTO_AGAIN:
923                 if (skb_flow_dissect_allowed(&num_hdrs))
924                         goto proto_again;
925                 goto out_good;
926         case FLOW_DISSECT_RET_CONTINUE:
927         case FLOW_DISSECT_RET_IPPROTO_AGAIN:
928                 break;
929         case FLOW_DISSECT_RET_OUT_BAD:
930         default:
931                 goto out_bad;
932         }
933
934 ip_proto_again:
935         fdret = FLOW_DISSECT_RET_CONTINUE;
936
937         switch (ip_proto) {
938         case IPPROTO_GRE:
939                 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
940                                                target_container, data,
941                                                &proto, &nhoff, &hlen, flags);
942                 break;
943
944         case NEXTHDR_HOP:
945         case NEXTHDR_ROUTING:
946         case NEXTHDR_DEST: {
947                 u8 _opthdr[2], *opthdr;
948
949                 if (proto != htons(ETH_P_IPV6))
950                         break;
951
952                 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
953                                               data, hlen, &_opthdr);
954                 if (!opthdr) {
955                         fdret = FLOW_DISSECT_RET_OUT_BAD;
956                         break;
957                 }
958
959                 ip_proto = opthdr[0];
960                 nhoff += (opthdr[1] + 1) << 3;
961
962                 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
963                 break;
964         }
965         case NEXTHDR_FRAGMENT: {
966                 struct frag_hdr _fh, *fh;
967
968                 if (proto != htons(ETH_P_IPV6))
969                         break;
970
971                 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
972                                           data, hlen, &_fh);
973
974                 if (!fh) {
975                         fdret = FLOW_DISSECT_RET_OUT_BAD;
976                         break;
977                 }
978
979                 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
980
981                 nhoff += sizeof(_fh);
982                 ip_proto = fh->nexthdr;
983
984                 if (!(fh->frag_off & htons(IP6_OFFSET))) {
985                         key_control->flags |= FLOW_DIS_FIRST_FRAG;
986                         if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
987                                 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
988                                 break;
989                         }
990                 }
991
992                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
993                 break;
994         }
995         case IPPROTO_IPIP:
996                 proto = htons(ETH_P_IP);
997
998                 key_control->flags |= FLOW_DIS_ENCAPSULATION;
999                 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1000                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1001                         break;
1002                 }
1003
1004                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1005                 break;
1006
1007         case IPPROTO_IPV6:
1008                 proto = htons(ETH_P_IPV6);
1009
1010                 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1011                 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1012                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1013                         break;
1014                 }
1015
1016                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1017                 break;
1018
1019
1020         case IPPROTO_MPLS:
1021                 proto = htons(ETH_P_MPLS_UC);
1022                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1023                 break;
1024
1025         case IPPROTO_TCP:
1026                 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1027                                        data, nhoff, hlen);
1028                 break;
1029
1030         default:
1031                 break;
1032         }
1033
1034         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS) &&
1035             !(key_control->flags & FLOW_DIS_IS_FRAGMENT)) {
1036                 key_ports = skb_flow_dissector_target(flow_dissector,
1037                                                       FLOW_DISSECTOR_KEY_PORTS,
1038                                                       target_container);
1039                 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
1040                                                         data, hlen);
1041         }
1042
1043         if (dissector_uses_key(flow_dissector,
1044                                FLOW_DISSECTOR_KEY_ICMP)) {
1045                 key_icmp = skb_flow_dissector_target(flow_dissector,
1046                                                      FLOW_DISSECTOR_KEY_ICMP,
1047                                                      target_container);
1048                 key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen);
1049         }
1050
1051         /* Process result of IP proto processing */
1052         switch (fdret) {
1053         case FLOW_DISSECT_RET_PROTO_AGAIN:
1054                 if (skb_flow_dissect_allowed(&num_hdrs))
1055                         goto proto_again;
1056                 break;
1057         case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1058                 if (skb_flow_dissect_allowed(&num_hdrs))
1059                         goto ip_proto_again;
1060                 break;
1061         case FLOW_DISSECT_RET_OUT_GOOD:
1062         case FLOW_DISSECT_RET_CONTINUE:
1063                 break;
1064         case FLOW_DISSECT_RET_OUT_BAD:
1065         default:
1066                 goto out_bad;
1067         }
1068
1069 out_good:
1070         ret = true;
1071
1072 out:
1073         key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1074         key_basic->n_proto = proto;
1075         key_basic->ip_proto = ip_proto;
1076
1077         return ret;
1078
1079 out_bad:
1080         ret = false;
1081         goto out;
1082 }
1083 EXPORT_SYMBOL(__skb_flow_dissect);
1084
1085 static siphash_key_t hashrnd __read_mostly;
1086 static __always_inline void __flow_hash_secret_init(void)
1087 {
1088         net_get_random_once(&hashrnd, sizeof(hashrnd));
1089 }
1090
1091 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1092 {
1093         BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1094         return &flow->FLOW_KEYS_HASH_START_FIELD;
1095 }
1096
1097 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1098 {
1099         size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1100         BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
1101                      sizeof(*flow) - sizeof(flow->addrs));
1102
1103         switch (flow->control.addr_type) {
1104         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1105                 diff -= sizeof(flow->addrs.v4addrs);
1106                 break;
1107         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1108                 diff -= sizeof(flow->addrs.v6addrs);
1109                 break;
1110         case FLOW_DISSECTOR_KEY_TIPC:
1111                 diff -= sizeof(flow->addrs.tipckey);
1112                 break;
1113         }
1114         return sizeof(*flow) - diff;
1115 }
1116
1117 __be32 flow_get_u32_src(const struct flow_keys *flow)
1118 {
1119         switch (flow->control.addr_type) {
1120         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1121                 return flow->addrs.v4addrs.src;
1122         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1123                 return (__force __be32)ipv6_addr_hash(
1124                         &flow->addrs.v6addrs.src);
1125         case FLOW_DISSECTOR_KEY_TIPC:
1126                 return flow->addrs.tipckey.key;
1127         default:
1128                 return 0;
1129         }
1130 }
1131 EXPORT_SYMBOL(flow_get_u32_src);
1132
1133 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1134 {
1135         switch (flow->control.addr_type) {
1136         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1137                 return flow->addrs.v4addrs.dst;
1138         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1139                 return (__force __be32)ipv6_addr_hash(
1140                         &flow->addrs.v6addrs.dst);
1141         default:
1142                 return 0;
1143         }
1144 }
1145 EXPORT_SYMBOL(flow_get_u32_dst);
1146
1147 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1148 {
1149         int addr_diff, i;
1150
1151         switch (keys->control.addr_type) {
1152         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1153                 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1154                             (__force u32)keys->addrs.v4addrs.src;
1155                 if ((addr_diff < 0) ||
1156                     (addr_diff == 0 &&
1157                      ((__force u16)keys->ports.dst <
1158                       (__force u16)keys->ports.src))) {
1159                         swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1160                         swap(keys->ports.src, keys->ports.dst);
1161                 }
1162                 break;
1163         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1164                 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1165                                    &keys->addrs.v6addrs.src,
1166                                    sizeof(keys->addrs.v6addrs.dst));
1167                 if ((addr_diff < 0) ||
1168                     (addr_diff == 0 &&
1169                      ((__force u16)keys->ports.dst <
1170                       (__force u16)keys->ports.src))) {
1171                         for (i = 0; i < 4; i++)
1172                                 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1173                                      keys->addrs.v6addrs.dst.s6_addr32[i]);
1174                         swap(keys->ports.src, keys->ports.dst);
1175                 }
1176                 break;
1177         }
1178 }
1179
1180 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1181                                         const siphash_key_t *keyval)
1182 {
1183         u32 hash;
1184
1185         __flow_hash_consistentify(keys);
1186
1187         hash = siphash(flow_keys_hash_start(keys),
1188                        flow_keys_hash_length(keys), keyval);
1189         if (!hash)
1190                 hash = 1;
1191
1192         return hash;
1193 }
1194
1195 u32 flow_hash_from_keys(struct flow_keys *keys)
1196 {
1197         __flow_hash_secret_init();
1198         return __flow_hash_from_keys(keys, &hashrnd);
1199 }
1200 EXPORT_SYMBOL(flow_hash_from_keys);
1201
1202 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1203                                   struct flow_keys *keys,
1204                                   const siphash_key_t *keyval)
1205 {
1206         skb_flow_dissect_flow_keys(skb, keys,
1207                                    FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1208
1209         return __flow_hash_from_keys(keys, keyval);
1210 }
1211
1212 struct _flow_keys_digest_data {
1213         __be16  n_proto;
1214         u8      ip_proto;
1215         u8      padding;
1216         __be32  ports;
1217         __be32  src;
1218         __be32  dst;
1219 };
1220
1221 void make_flow_keys_digest(struct flow_keys_digest *digest,
1222                            const struct flow_keys *flow)
1223 {
1224         struct _flow_keys_digest_data *data =
1225             (struct _flow_keys_digest_data *)digest;
1226
1227         BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1228
1229         memset(digest, 0, sizeof(*digest));
1230
1231         data->n_proto = flow->basic.n_proto;
1232         data->ip_proto = flow->basic.ip_proto;
1233         data->ports = flow->ports.ports;
1234         data->src = flow->addrs.v4addrs.src;
1235         data->dst = flow->addrs.v4addrs.dst;
1236 }
1237 EXPORT_SYMBOL(make_flow_keys_digest);
1238
1239 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1240
1241 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1242 {
1243         struct flow_keys keys;
1244
1245         __flow_hash_secret_init();
1246
1247         memset(&keys, 0, sizeof(keys));
1248         __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
1249                            NULL, 0, 0, 0,
1250                            FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1251
1252         return __flow_hash_from_keys(&keys, &hashrnd);
1253 }
1254 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1255
1256 /**
1257  * __skb_get_hash: calculate a flow hash
1258  * @skb: sk_buff to calculate flow hash from
1259  *
1260  * This function calculates a flow hash based on src/dst addresses
1261  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
1262  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
1263  * if hash is a canonical 4-tuple hash over transport ports.
1264  */
1265 void __skb_get_hash(struct sk_buff *skb)
1266 {
1267         struct flow_keys keys;
1268         u32 hash;
1269
1270         __flow_hash_secret_init();
1271
1272         hash = ___skb_get_hash(skb, &keys, &hashrnd);
1273
1274         __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1275 }
1276 EXPORT_SYMBOL(__skb_get_hash);
1277
1278 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1279                            const siphash_key_t *perturb)
1280 {
1281         struct flow_keys keys;
1282
1283         return ___skb_get_hash(skb, &keys, perturb);
1284 }
1285 EXPORT_SYMBOL(skb_get_hash_perturb);
1286
1287 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1288                    const struct flow_keys_basic *keys, int hlen)
1289 {
1290         u32 poff = keys->control.thoff;
1291
1292         /* skip L4 headers for fragments after the first */
1293         if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1294             !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1295                 return poff;
1296
1297         switch (keys->basic.ip_proto) {
1298         case IPPROTO_TCP: {
1299                 /* access doff as u8 to avoid unaligned access */
1300                 const u8 *doff;
1301                 u8 _doff;
1302
1303                 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1304                                             data, hlen, &_doff);
1305                 if (!doff)
1306                         return poff;
1307
1308                 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1309                 break;
1310         }
1311         case IPPROTO_UDP:
1312         case IPPROTO_UDPLITE:
1313                 poff += sizeof(struct udphdr);
1314                 break;
1315         /* For the rest, we do not really care about header
1316          * extensions at this point for now.
1317          */
1318         case IPPROTO_ICMP:
1319                 poff += sizeof(struct icmphdr);
1320                 break;
1321         case IPPROTO_ICMPV6:
1322                 poff += sizeof(struct icmp6hdr);
1323                 break;
1324         case IPPROTO_IGMP:
1325                 poff += sizeof(struct igmphdr);
1326                 break;
1327         case IPPROTO_DCCP:
1328                 poff += sizeof(struct dccp_hdr);
1329                 break;
1330         case IPPROTO_SCTP:
1331                 poff += sizeof(struct sctphdr);
1332                 break;
1333         }
1334
1335         return poff;
1336 }
1337
1338 /**
1339  * skb_get_poff - get the offset to the payload
1340  * @skb: sk_buff to get the payload offset from
1341  *
1342  * The function will get the offset to the payload as far as it could
1343  * be dissected.  The main user is currently BPF, so that we can dynamically
1344  * truncate packets without needing to push actual payload to the user
1345  * space and can analyze headers only, instead.
1346  */
1347 u32 skb_get_poff(const struct sk_buff *skb)
1348 {
1349         struct flow_keys_basic keys;
1350
1351         if (!skb_flow_dissect_flow_keys_basic(skb, &keys, NULL, 0, 0, 0, 0))
1352                 return 0;
1353
1354         return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1355 }
1356
1357 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1358 {
1359         memset(keys, 0, sizeof(*keys));
1360
1361         memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1362             sizeof(keys->addrs.v6addrs.src));
1363         memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1364             sizeof(keys->addrs.v6addrs.dst));
1365         keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1366         keys->ports.src = fl6->fl6_sport;
1367         keys->ports.dst = fl6->fl6_dport;
1368         keys->keyid.keyid = fl6->fl6_gre_key;
1369         keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1370         keys->basic.ip_proto = fl6->flowi6_proto;
1371
1372         return flow_hash_from_keys(keys);
1373 }
1374 EXPORT_SYMBOL(__get_hash_from_flowi6);
1375
1376 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1377         {
1378                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1379                 .offset = offsetof(struct flow_keys, control),
1380         },
1381         {
1382                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1383                 .offset = offsetof(struct flow_keys, basic),
1384         },
1385         {
1386                 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1387                 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1388         },
1389         {
1390                 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1391                 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1392         },
1393         {
1394                 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1395                 .offset = offsetof(struct flow_keys, addrs.tipckey),
1396         },
1397         {
1398                 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1399                 .offset = offsetof(struct flow_keys, ports),
1400         },
1401         {
1402                 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1403                 .offset = offsetof(struct flow_keys, vlan),
1404         },
1405         {
1406                 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1407                 .offset = offsetof(struct flow_keys, tags),
1408         },
1409         {
1410                 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1411                 .offset = offsetof(struct flow_keys, keyid),
1412         },
1413 };
1414
1415 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1416         {
1417                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1418                 .offset = offsetof(struct flow_keys, control),
1419         },
1420         {
1421                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1422                 .offset = offsetof(struct flow_keys, basic),
1423         },
1424         {
1425                 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1426                 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1427         },
1428         {
1429                 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1430                 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1431         },
1432         {
1433                 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1434                 .offset = offsetof(struct flow_keys, ports),
1435         },
1436 };
1437
1438 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1439         {
1440                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1441                 .offset = offsetof(struct flow_keys, control),
1442         },
1443         {
1444                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1445                 .offset = offsetof(struct flow_keys, basic),
1446         },
1447 };
1448
1449 struct flow_dissector flow_keys_dissector __read_mostly;
1450 EXPORT_SYMBOL(flow_keys_dissector);
1451
1452 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1453 EXPORT_SYMBOL(flow_keys_basic_dissector);
1454
1455 static int __init init_default_flow_dissectors(void)
1456 {
1457         skb_flow_dissector_init(&flow_keys_dissector,
1458                                 flow_keys_dissector_keys,
1459                                 ARRAY_SIZE(flow_keys_dissector_keys));
1460         skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1461                                 flow_keys_dissector_symmetric_keys,
1462                                 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1463         skb_flow_dissector_init(&flow_keys_basic_dissector,
1464                                 flow_keys_basic_dissector_keys,
1465                                 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1466         return 0;
1467 }
1468
1469 core_initcall(init_default_flow_dissectors);