root/net/core/flow_dissector.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dissector_set_key
  2. skb_flow_dissector_init
  3. skb_flow_dissector_prog_query
  4. skb_flow_dissector_bpf_prog_attach
  5. flow_dissector_bpf_prog_detach
  6. skb_flow_get_be16
  7. skb_flow_dissector_bpf_prog_detach
  8. flow_dissector_pernet_pre_exit
  9. __skb_flow_get_ports
  10. skb_flow_dissect_meta
  11. skb_flow_dissect_set_enc_addr_type
  12. skb_flow_dissect_ct
  13. skb_flow_dissect_tunnel_info
  14. __skb_flow_dissect_mpls
  15. __skb_flow_dissect_arp
  16. __skb_flow_dissect_gre
  17. __skb_flow_dissect_batadv
  18. __skb_flow_dissect_tcp
  19. __skb_flow_dissect_ports
  20. __skb_flow_dissect_ipv4
  21. __skb_flow_dissect_ipv6
  22. skb_flow_dissect_allowed
  23. __skb_flow_bpf_to_target
  24. bpf_flow_dissect
  25. __skb_flow_dissect
  26. __flow_hash_secret_init
  27. flow_keys_hash_start
  28. flow_keys_hash_length
  29. flow_get_u32_src
  30. flow_get_u32_dst
  31. __flow_hash_consistentify
  32. __flow_hash_from_keys
  33. flow_hash_from_keys
  34. ___skb_get_hash
  35. make_flow_keys_digest
  36. __skb_get_hash_symmetric
  37. __skb_get_hash
  38. skb_get_hash_perturb
  39. __skb_get_poff
  40. skb_get_poff
  41. __get_hash_from_flowi6
  42. init_default_flow_dissectors

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

/* [<][>][^][v][top][bottom][index][help] */