linux/drivers/net/vxlan.c
<<
>>
Prefs
   1/*
   2 * VXLAN: Virtual eXtensible Local Area Network
   3 *
   4 * Copyright (c) 2012-2013 Vyatta Inc.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 */
  10
  11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12
  13#include <linux/kernel.h>
  14#include <linux/module.h>
  15#include <linux/errno.h>
  16#include <linux/slab.h>
  17#include <linux/udp.h>
  18#include <linux/igmp.h>
  19#include <linux/if_ether.h>
  20#include <linux/ethtool.h>
  21#include <net/arp.h>
  22#include <net/ndisc.h>
  23#include <net/ip.h>
  24#include <net/icmp.h>
  25#include <net/rtnetlink.h>
  26#include <net/inet_ecn.h>
  27#include <net/net_namespace.h>
  28#include <net/netns/generic.h>
  29#include <net/tun_proto.h>
  30#include <net/vxlan.h>
  31
  32#if IS_ENABLED(CONFIG_IPV6)
  33#include <net/ip6_tunnel.h>
  34#include <net/ip6_checksum.h>
  35#endif
  36
  37#define VXLAN_VERSION   "0.1"
  38
  39#define PORT_HASH_BITS  8
  40#define PORT_HASH_SIZE  (1<<PORT_HASH_BITS)
  41#define FDB_AGE_DEFAULT 300 /* 5 min */
  42#define FDB_AGE_INTERVAL (10 * HZ)      /* rescan interval */
  43
  44/* UDP port for VXLAN traffic.
  45 * The IANA assigned port is 4789, but the Linux default is 8472
  46 * for compatibility with early adopters.
  47 */
  48static unsigned short vxlan_port __read_mostly = 8472;
  49module_param_named(udp_port, vxlan_port, ushort, 0444);
  50MODULE_PARM_DESC(udp_port, "Destination UDP port");
  51
  52static bool log_ecn_error = true;
  53module_param(log_ecn_error, bool, 0644);
  54MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  55
  56static int vxlan_net_id;
  57static struct rtnl_link_ops vxlan_link_ops;
  58
  59static const u8 all_zeros_mac[ETH_ALEN + 2];
  60
  61static int vxlan_sock_add(struct vxlan_dev *vxlan);
  62
  63static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
  64
  65/* per-network namespace private data for this module */
  66struct vxlan_net {
  67        struct list_head  vxlan_list;
  68        struct hlist_head sock_list[PORT_HASH_SIZE];
  69        spinlock_t        sock_lock;
  70};
  71
  72/* Forwarding table entry */
  73struct vxlan_fdb {
  74        struct hlist_node hlist;        /* linked list of entries */
  75        struct rcu_head   rcu;
  76        unsigned long     updated;      /* jiffies */
  77        unsigned long     used;
  78        struct list_head  remotes;
  79        u8                eth_addr[ETH_ALEN];
  80        u16               state;        /* see ndm_state */
  81        __be32            vni;
  82        u8                flags;        /* see ndm_flags */
  83};
  84
  85/* salt for hash table */
  86static u32 vxlan_salt __read_mostly;
  87
  88static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
  89{
  90        return vs->flags & VXLAN_F_COLLECT_METADATA ||
  91               ip_tunnel_collect_metadata();
  92}
  93
  94#if IS_ENABLED(CONFIG_IPV6)
  95static inline
  96bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  97{
  98        if (a->sa.sa_family != b->sa.sa_family)
  99                return false;
 100        if (a->sa.sa_family == AF_INET6)
 101                return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
 102        else
 103                return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
 104}
 105
 106static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
 107{
 108        if (nla_len(nla) >= sizeof(struct in6_addr)) {
 109                ip->sin6.sin6_addr = nla_get_in6_addr(nla);
 110                ip->sa.sa_family = AF_INET6;
 111                return 0;
 112        } else if (nla_len(nla) >= sizeof(__be32)) {
 113                ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
 114                ip->sa.sa_family = AF_INET;
 115                return 0;
 116        } else {
 117                return -EAFNOSUPPORT;
 118        }
 119}
 120
 121static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
 122                              const union vxlan_addr *ip)
 123{
 124        if (ip->sa.sa_family == AF_INET6)
 125                return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
 126        else
 127                return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
 128}
 129
 130#else /* !CONFIG_IPV6 */
 131
 132static inline
 133bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
 134{
 135        return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
 136}
 137
 138static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
 139{
 140        if (nla_len(nla) >= sizeof(struct in6_addr)) {
 141                return -EAFNOSUPPORT;
 142        } else if (nla_len(nla) >= sizeof(__be32)) {
 143                ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
 144                ip->sa.sa_family = AF_INET;
 145                return 0;
 146        } else {
 147                return -EAFNOSUPPORT;
 148        }
 149}
 150
 151static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
 152                              const union vxlan_addr *ip)
 153{
 154        return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
 155}
 156#endif
 157
 158/* Virtual Network hash table head */
 159static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
 160{
 161        return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
 162}
 163
 164/* Socket hash table head */
 165static inline struct hlist_head *vs_head(struct net *net, __be16 port)
 166{
 167        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
 168
 169        return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
 170}
 171
 172/* First remote destination for a forwarding entry.
 173 * Guaranteed to be non-NULL because remotes are never deleted.
 174 */
 175static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
 176{
 177        return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
 178}
 179
 180static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
 181{
 182        return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
 183}
 184
 185/* Find VXLAN socket based on network namespace, address family and UDP port
 186 * and enabled unshareable flags.
 187 */
 188static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
 189                                          __be16 port, u32 flags)
 190{
 191        struct vxlan_sock *vs;
 192
 193        flags &= VXLAN_F_RCV_FLAGS;
 194
 195        hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
 196                if (inet_sk(vs->sock->sk)->inet_sport == port &&
 197                    vxlan_get_sk_family(vs) == family &&
 198                    vs->flags == flags)
 199                        return vs;
 200        }
 201        return NULL;
 202}
 203
 204static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
 205                                           __be32 vni)
 206{
 207        struct vxlan_dev_node *node;
 208
 209        /* For flow based devices, map all packets to VNI 0 */
 210        if (vs->flags & VXLAN_F_COLLECT_METADATA)
 211                vni = 0;
 212
 213        hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
 214                if (node->vxlan->default_dst.remote_vni != vni)
 215                        continue;
 216
 217                if (IS_ENABLED(CONFIG_IPV6)) {
 218                        const struct vxlan_config *cfg = &node->vxlan->cfg;
 219
 220                        if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
 221                            cfg->remote_ifindex != ifindex)
 222                                continue;
 223                }
 224
 225                return node->vxlan;
 226        }
 227
 228        return NULL;
 229}
 230
 231/* Look up VNI in a per net namespace table */
 232static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
 233                                        __be32 vni, sa_family_t family,
 234                                        __be16 port, u32 flags)
 235{
 236        struct vxlan_sock *vs;
 237
 238        vs = vxlan_find_sock(net, family, port, flags);
 239        if (!vs)
 240                return NULL;
 241
 242        return vxlan_vs_find_vni(vs, ifindex, vni);
 243}
 244
 245/* Fill in neighbour message in skbuff. */
 246static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
 247                          const struct vxlan_fdb *fdb,
 248                          u32 portid, u32 seq, int type, unsigned int flags,
 249                          const struct vxlan_rdst *rdst)
 250{
 251        unsigned long now = jiffies;
 252        struct nda_cacheinfo ci;
 253        struct nlmsghdr *nlh;
 254        struct ndmsg *ndm;
 255        bool send_ip, send_eth;
 256
 257        nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
 258        if (nlh == NULL)
 259                return -EMSGSIZE;
 260
 261        ndm = nlmsg_data(nlh);
 262        memset(ndm, 0, sizeof(*ndm));
 263
 264        send_eth = send_ip = true;
 265
 266        if (type == RTM_GETNEIGH) {
 267                send_ip = !vxlan_addr_any(&rdst->remote_ip);
 268                send_eth = !is_zero_ether_addr(fdb->eth_addr);
 269                ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
 270        } else
 271                ndm->ndm_family = AF_BRIDGE;
 272        ndm->ndm_state = fdb->state;
 273        ndm->ndm_ifindex = vxlan->dev->ifindex;
 274        ndm->ndm_flags = fdb->flags;
 275        ndm->ndm_type = RTN_UNICAST;
 276
 277        if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
 278            nla_put_s32(skb, NDA_LINK_NETNSID,
 279                        peernet2id(dev_net(vxlan->dev), vxlan->net)))
 280                goto nla_put_failure;
 281
 282        if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
 283                goto nla_put_failure;
 284
 285        if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
 286                goto nla_put_failure;
 287
 288        if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
 289            nla_put_be16(skb, NDA_PORT, rdst->remote_port))
 290                goto nla_put_failure;
 291        if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
 292            nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
 293                goto nla_put_failure;
 294        if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
 295            nla_put_u32(skb, NDA_SRC_VNI,
 296                        be32_to_cpu(fdb->vni)))
 297                goto nla_put_failure;
 298        if (rdst->remote_ifindex &&
 299            nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
 300                goto nla_put_failure;
 301
 302        ci.ndm_used      = jiffies_to_clock_t(now - fdb->used);
 303        ci.ndm_confirmed = 0;
 304        ci.ndm_updated   = jiffies_to_clock_t(now - fdb->updated);
 305        ci.ndm_refcnt    = 0;
 306
 307        if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
 308                goto nla_put_failure;
 309
 310        nlmsg_end(skb, nlh);
 311        return 0;
 312
 313nla_put_failure:
 314        nlmsg_cancel(skb, nlh);
 315        return -EMSGSIZE;
 316}
 317
 318static inline size_t vxlan_nlmsg_size(void)
 319{
 320        return NLMSG_ALIGN(sizeof(struct ndmsg))
 321                + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
 322                + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
 323                + nla_total_size(sizeof(__be16)) /* NDA_PORT */
 324                + nla_total_size(sizeof(__be32)) /* NDA_VNI */
 325                + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
 326                + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
 327                + nla_total_size(sizeof(struct nda_cacheinfo));
 328}
 329
 330static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
 331                             struct vxlan_rdst *rd, int type)
 332{
 333        struct net *net = dev_net(vxlan->dev);
 334        struct sk_buff *skb;
 335        int err = -ENOBUFS;
 336
 337        skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
 338        if (skb == NULL)
 339                goto errout;
 340
 341        err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
 342        if (err < 0) {
 343                /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
 344                WARN_ON(err == -EMSGSIZE);
 345                kfree_skb(skb);
 346                goto errout;
 347        }
 348
 349        rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
 350        return;
 351errout:
 352        if (err < 0)
 353                rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
 354}
 355
 356static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
 357{
 358        struct vxlan_dev *vxlan = netdev_priv(dev);
 359        struct vxlan_fdb f = {
 360                .state = NUD_STALE,
 361        };
 362        struct vxlan_rdst remote = {
 363                .remote_ip = *ipa, /* goes to NDA_DST */
 364                .remote_vni = cpu_to_be32(VXLAN_N_VID),
 365        };
 366
 367        vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
 368}
 369
 370static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
 371{
 372        struct vxlan_fdb f = {
 373                .state = NUD_STALE,
 374        };
 375        struct vxlan_rdst remote = { };
 376
 377        memcpy(f.eth_addr, eth_addr, ETH_ALEN);
 378
 379        vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
 380}
 381
 382/* Hash Ethernet address */
 383static u32 eth_hash(const unsigned char *addr)
 384{
 385        u64 value = get_unaligned((u64 *)addr);
 386
 387        /* only want 6 bytes */
 388#ifdef __BIG_ENDIAN
 389        value >>= 16;
 390#else
 391        value <<= 16;
 392#endif
 393        return hash_64(value, FDB_HASH_BITS);
 394}
 395
 396static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
 397{
 398        /* use 1 byte of OUI and 3 bytes of NIC */
 399        u32 key = get_unaligned((u32 *)(addr + 2));
 400
 401        return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
 402}
 403
 404/* Hash chain to use given mac address */
 405static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
 406                                                const u8 *mac, __be32 vni)
 407{
 408        if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
 409                return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
 410        else
 411                return &vxlan->fdb_head[eth_hash(mac)];
 412}
 413
 414/* Look up Ethernet address in forwarding table */
 415static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
 416                                          const u8 *mac, __be32 vni)
 417{
 418        struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
 419        struct vxlan_fdb *f;
 420
 421        hlist_for_each_entry_rcu(f, head, hlist) {
 422                if (ether_addr_equal(mac, f->eth_addr)) {
 423                        if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
 424                                if (vni == f->vni)
 425                                        return f;
 426                        } else {
 427                                return f;
 428                        }
 429                }
 430        }
 431
 432        return NULL;
 433}
 434
 435static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
 436                                        const u8 *mac, __be32 vni)
 437{
 438        struct vxlan_fdb *f;
 439
 440        f = __vxlan_find_mac(vxlan, mac, vni);
 441        if (f)
 442                f->used = jiffies;
 443
 444        return f;
 445}
 446
 447/* caller should hold vxlan->hash_lock */
 448static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
 449                                              union vxlan_addr *ip, __be16 port,
 450                                              __be32 vni, __u32 ifindex)
 451{
 452        struct vxlan_rdst *rd;
 453
 454        list_for_each_entry(rd, &f->remotes, list) {
 455                if (vxlan_addr_equal(&rd->remote_ip, ip) &&
 456                    rd->remote_port == port &&
 457                    rd->remote_vni == vni &&
 458                    rd->remote_ifindex == ifindex)
 459                        return rd;
 460        }
 461
 462        return NULL;
 463}
 464
 465/* Replace destination of unicast mac */
 466static int vxlan_fdb_replace(struct vxlan_fdb *f,
 467                             union vxlan_addr *ip, __be16 port, __be32 vni,
 468                             __u32 ifindex)
 469{
 470        struct vxlan_rdst *rd;
 471
 472        rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
 473        if (rd)
 474                return 0;
 475
 476        rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
 477        if (!rd)
 478                return 0;
 479
 480        dst_cache_reset(&rd->dst_cache);
 481        rd->remote_ip = *ip;
 482        rd->remote_port = port;
 483        rd->remote_vni = vni;
 484        rd->remote_ifindex = ifindex;
 485        return 1;
 486}
 487
 488/* Add/update destinations for multicast */
 489static int vxlan_fdb_append(struct vxlan_fdb *f,
 490                            union vxlan_addr *ip, __be16 port, __be32 vni,
 491                            __u32 ifindex, struct vxlan_rdst **rdp)
 492{
 493        struct vxlan_rdst *rd;
 494
 495        rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
 496        if (rd)
 497                return 0;
 498
 499        rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
 500        if (rd == NULL)
 501                return -ENOBUFS;
 502
 503        if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
 504                kfree(rd);
 505                return -ENOBUFS;
 506        }
 507
 508        rd->remote_ip = *ip;
 509        rd->remote_port = port;
 510        rd->remote_vni = vni;
 511        rd->remote_ifindex = ifindex;
 512
 513        list_add_tail_rcu(&rd->list, &f->remotes);
 514
 515        *rdp = rd;
 516        return 1;
 517}
 518
 519static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
 520                                          unsigned int off,
 521                                          struct vxlanhdr *vh, size_t hdrlen,
 522                                          __be32 vni_field,
 523                                          struct gro_remcsum *grc,
 524                                          bool nopartial)
 525{
 526        size_t start, offset;
 527
 528        if (skb->remcsum_offload)
 529                return vh;
 530
 531        if (!NAPI_GRO_CB(skb)->csum_valid)
 532                return NULL;
 533
 534        start = vxlan_rco_start(vni_field);
 535        offset = start + vxlan_rco_offset(vni_field);
 536
 537        vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
 538                                     start, offset, grc, nopartial);
 539
 540        skb->remcsum_offload = 1;
 541
 542        return vh;
 543}
 544
 545static struct sk_buff **vxlan_gro_receive(struct sock *sk,
 546                                          struct sk_buff **head,
 547                                          struct sk_buff *skb)
 548{
 549        struct sk_buff *p, **pp = NULL;
 550        struct vxlanhdr *vh, *vh2;
 551        unsigned int hlen, off_vx;
 552        int flush = 1;
 553        struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
 554        __be32 flags;
 555        struct gro_remcsum grc;
 556
 557        skb_gro_remcsum_init(&grc);
 558
 559        off_vx = skb_gro_offset(skb);
 560        hlen = off_vx + sizeof(*vh);
 561        vh   = skb_gro_header_fast(skb, off_vx);
 562        if (skb_gro_header_hard(skb, hlen)) {
 563                vh = skb_gro_header_slow(skb, hlen, off_vx);
 564                if (unlikely(!vh))
 565                        goto out;
 566        }
 567
 568        skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
 569
 570        flags = vh->vx_flags;
 571
 572        if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
 573                vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
 574                                       vh->vx_vni, &grc,
 575                                       !!(vs->flags &
 576                                          VXLAN_F_REMCSUM_NOPARTIAL));
 577
 578                if (!vh)
 579                        goto out;
 580        }
 581
 582        skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
 583
 584        for (p = *head; p; p = p->next) {
 585                if (!NAPI_GRO_CB(p)->same_flow)
 586                        continue;
 587
 588                vh2 = (struct vxlanhdr *)(p->data + off_vx);
 589                if (vh->vx_flags != vh2->vx_flags ||
 590                    vh->vx_vni != vh2->vx_vni) {
 591                        NAPI_GRO_CB(p)->same_flow = 0;
 592                        continue;
 593                }
 594        }
 595
 596        pp = call_gro_receive(eth_gro_receive, head, skb);
 597        flush = 0;
 598
 599out:
 600        skb_gro_remcsum_cleanup(skb, &grc);
 601        skb->remcsum_offload = 0;
 602        NAPI_GRO_CB(skb)->flush |= flush;
 603
 604        return pp;
 605}
 606
 607static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
 608{
 609        /* Sets 'skb->inner_mac_header' since we are always called with
 610         * 'skb->encapsulation' set.
 611         */
 612        return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
 613}
 614
 615/* Add new entry to forwarding table -- assumes lock held */
 616static int vxlan_fdb_create(struct vxlan_dev *vxlan,
 617                            const u8 *mac, union vxlan_addr *ip,
 618                            __u16 state, __u16 flags,
 619                            __be16 port, __be32 src_vni, __be32 vni,
 620                            __u32 ifindex, __u8 ndm_flags)
 621{
 622        struct vxlan_rdst *rd = NULL;
 623        struct vxlan_fdb *f;
 624        int notify = 0;
 625        int rc;
 626
 627        f = __vxlan_find_mac(vxlan, mac, src_vni);
 628        if (f) {
 629                if (flags & NLM_F_EXCL) {
 630                        netdev_dbg(vxlan->dev,
 631                                   "lost race to create %pM\n", mac);
 632                        return -EEXIST;
 633                }
 634                if (f->state != state) {
 635                        f->state = state;
 636                        f->updated = jiffies;
 637                        notify = 1;
 638                }
 639                if (f->flags != ndm_flags) {
 640                        f->flags = ndm_flags;
 641                        f->updated = jiffies;
 642                        notify = 1;
 643                }
 644                if ((flags & NLM_F_REPLACE)) {
 645                        /* Only change unicasts */
 646                        if (!(is_multicast_ether_addr(f->eth_addr) ||
 647                             is_zero_ether_addr(f->eth_addr))) {
 648                                notify |= vxlan_fdb_replace(f, ip, port, vni,
 649                                                           ifindex);
 650                        } else
 651                                return -EOPNOTSUPP;
 652                }
 653                if ((flags & NLM_F_APPEND) &&
 654                    (is_multicast_ether_addr(f->eth_addr) ||
 655                     is_zero_ether_addr(f->eth_addr))) {
 656                        rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
 657
 658                        if (rc < 0)
 659                                return rc;
 660                        notify |= rc;
 661                }
 662        } else {
 663                if (!(flags & NLM_F_CREATE))
 664                        return -ENOENT;
 665
 666                if (vxlan->cfg.addrmax &&
 667                    vxlan->addrcnt >= vxlan->cfg.addrmax)
 668                        return -ENOSPC;
 669
 670                /* Disallow replace to add a multicast entry */
 671                if ((flags & NLM_F_REPLACE) &&
 672                    (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
 673                        return -EOPNOTSUPP;
 674
 675                netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
 676                f = kmalloc(sizeof(*f), GFP_ATOMIC);
 677                if (!f)
 678                        return -ENOMEM;
 679
 680                notify = 1;
 681                f->state = state;
 682                f->flags = ndm_flags;
 683                f->updated = f->used = jiffies;
 684                f->vni = src_vni;
 685                INIT_LIST_HEAD(&f->remotes);
 686                memcpy(f->eth_addr, mac, ETH_ALEN);
 687
 688                rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
 689                if (rc < 0) {
 690                        kfree(f);
 691                        return rc;
 692                }
 693
 694                ++vxlan->addrcnt;
 695                hlist_add_head_rcu(&f->hlist,
 696                                   vxlan_fdb_head(vxlan, mac, src_vni));
 697        }
 698
 699        if (notify) {
 700                if (rd == NULL)
 701                        rd = first_remote_rtnl(f);
 702                vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
 703        }
 704
 705        return 0;
 706}
 707
 708static void vxlan_fdb_free(struct rcu_head *head)
 709{
 710        struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
 711        struct vxlan_rdst *rd, *nd;
 712
 713        list_for_each_entry_safe(rd, nd, &f->remotes, list) {
 714                dst_cache_destroy(&rd->dst_cache);
 715                kfree(rd);
 716        }
 717        kfree(f);
 718}
 719
 720static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
 721{
 722        netdev_dbg(vxlan->dev,
 723                    "delete %pM\n", f->eth_addr);
 724
 725        --vxlan->addrcnt;
 726        vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
 727
 728        hlist_del_rcu(&f->hlist);
 729        call_rcu(&f->rcu, vxlan_fdb_free);
 730}
 731
 732static void vxlan_dst_free(struct rcu_head *head)
 733{
 734        struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
 735
 736        dst_cache_destroy(&rd->dst_cache);
 737        kfree(rd);
 738}
 739
 740static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
 741                                  struct vxlan_rdst *rd)
 742{
 743        list_del_rcu(&rd->list);
 744        vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
 745        call_rcu(&rd->rcu, vxlan_dst_free);
 746}
 747
 748static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
 749                           union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
 750                           __be32 *vni, u32 *ifindex)
 751{
 752        struct net *net = dev_net(vxlan->dev);
 753        int err;
 754
 755        if (tb[NDA_DST]) {
 756                err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
 757                if (err)
 758                        return err;
 759        } else {
 760                union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
 761                if (remote->sa.sa_family == AF_INET) {
 762                        ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
 763                        ip->sa.sa_family = AF_INET;
 764#if IS_ENABLED(CONFIG_IPV6)
 765                } else {
 766                        ip->sin6.sin6_addr = in6addr_any;
 767                        ip->sa.sa_family = AF_INET6;
 768#endif
 769                }
 770        }
 771
 772        if (tb[NDA_PORT]) {
 773                if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
 774                        return -EINVAL;
 775                *port = nla_get_be16(tb[NDA_PORT]);
 776        } else {
 777                *port = vxlan->cfg.dst_port;
 778        }
 779
 780        if (tb[NDA_VNI]) {
 781                if (nla_len(tb[NDA_VNI]) != sizeof(u32))
 782                        return -EINVAL;
 783                *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
 784        } else {
 785                *vni = vxlan->default_dst.remote_vni;
 786        }
 787
 788        if (tb[NDA_SRC_VNI]) {
 789                if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
 790                        return -EINVAL;
 791                *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
 792        } else {
 793                *src_vni = vxlan->default_dst.remote_vni;
 794        }
 795
 796        if (tb[NDA_IFINDEX]) {
 797                struct net_device *tdev;
 798
 799                if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
 800                        return -EINVAL;
 801                *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
 802                tdev = __dev_get_by_index(net, *ifindex);
 803                if (!tdev)
 804                        return -EADDRNOTAVAIL;
 805        } else {
 806                *ifindex = 0;
 807        }
 808
 809        return 0;
 810}
 811
 812/* Add static entry (via netlink) */
 813static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
 814                         struct net_device *dev,
 815                         const unsigned char *addr, u16 vid, u16 flags)
 816{
 817        struct vxlan_dev *vxlan = netdev_priv(dev);
 818        /* struct net *net = dev_net(vxlan->dev); */
 819        union vxlan_addr ip;
 820        __be16 port;
 821        __be32 src_vni, vni;
 822        u32 ifindex;
 823        int err;
 824
 825        if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
 826                pr_info("RTM_NEWNEIGH with invalid state %#x\n",
 827                        ndm->ndm_state);
 828                return -EINVAL;
 829        }
 830
 831        if (tb[NDA_DST] == NULL)
 832                return -EINVAL;
 833
 834        err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
 835        if (err)
 836                return err;
 837
 838        if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
 839                return -EAFNOSUPPORT;
 840
 841        spin_lock_bh(&vxlan->hash_lock);
 842        err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
 843                               port, src_vni, vni, ifindex, ndm->ndm_flags);
 844        spin_unlock_bh(&vxlan->hash_lock);
 845
 846        return err;
 847}
 848
 849static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
 850                              const unsigned char *addr, union vxlan_addr ip,
 851                              __be16 port, __be32 src_vni, u32 vni, u32 ifindex,
 852                              u16 vid)
 853{
 854        struct vxlan_fdb *f;
 855        struct vxlan_rdst *rd = NULL;
 856        int err = -ENOENT;
 857
 858        f = vxlan_find_mac(vxlan, addr, src_vni);
 859        if (!f)
 860                return err;
 861
 862        if (!vxlan_addr_any(&ip)) {
 863                rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
 864                if (!rd)
 865                        goto out;
 866        }
 867
 868        /* remove a destination if it's not the only one on the list,
 869         * otherwise destroy the fdb entry
 870         */
 871        if (rd && !list_is_singular(&f->remotes)) {
 872                vxlan_fdb_dst_destroy(vxlan, f, rd);
 873                goto out;
 874        }
 875
 876        vxlan_fdb_destroy(vxlan, f);
 877
 878out:
 879        return 0;
 880}
 881
 882/* Delete entry (via netlink) */
 883static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
 884                            struct net_device *dev,
 885                            const unsigned char *addr, u16 vid)
 886{
 887        struct vxlan_dev *vxlan = netdev_priv(dev);
 888        union vxlan_addr ip;
 889        __be32 src_vni, vni;
 890        __be16 port;
 891        u32 ifindex;
 892        int err;
 893
 894        err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
 895        if (err)
 896                return err;
 897
 898        spin_lock_bh(&vxlan->hash_lock);
 899        err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
 900                                 vid);
 901        spin_unlock_bh(&vxlan->hash_lock);
 902
 903        return err;
 904}
 905
 906/* Dump forwarding table */
 907static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
 908                          struct net_device *dev,
 909                          struct net_device *filter_dev, int *idx)
 910{
 911        struct vxlan_dev *vxlan = netdev_priv(dev);
 912        unsigned int h;
 913        int err = 0;
 914
 915        for (h = 0; h < FDB_HASH_SIZE; ++h) {
 916                struct vxlan_fdb *f;
 917
 918                hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
 919                        struct vxlan_rdst *rd;
 920
 921                        list_for_each_entry_rcu(rd, &f->remotes, list) {
 922                                if (*idx < cb->args[2])
 923                                        goto skip;
 924
 925                                err = vxlan_fdb_info(skb, vxlan, f,
 926                                                     NETLINK_CB(cb->skb).portid,
 927                                                     cb->nlh->nlmsg_seq,
 928                                                     RTM_NEWNEIGH,
 929                                                     NLM_F_MULTI, rd);
 930                                if (err < 0)
 931                                        goto out;
 932skip:
 933                                *idx += 1;
 934                        }
 935                }
 936        }
 937out:
 938        return err;
 939}
 940
 941/* Watch incoming packets to learn mapping between Ethernet address
 942 * and Tunnel endpoint.
 943 * Return true if packet is bogus and should be dropped.
 944 */
 945static bool vxlan_snoop(struct net_device *dev,
 946                        union vxlan_addr *src_ip, const u8 *src_mac,
 947                        u32 src_ifindex, __be32 vni)
 948{
 949        struct vxlan_dev *vxlan = netdev_priv(dev);
 950        struct vxlan_fdb *f;
 951        u32 ifindex = 0;
 952
 953#if IS_ENABLED(CONFIG_IPV6)
 954        if (src_ip->sa.sa_family == AF_INET6 &&
 955            (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
 956                ifindex = src_ifindex;
 957#endif
 958
 959        f = vxlan_find_mac(vxlan, src_mac, vni);
 960        if (likely(f)) {
 961                struct vxlan_rdst *rdst = first_remote_rcu(f);
 962
 963                if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
 964                           rdst->remote_ifindex == ifindex))
 965                        return false;
 966
 967                /* Don't migrate static entries, drop packets */
 968                if (f->state & (NUD_PERMANENT | NUD_NOARP))
 969                        return true;
 970
 971                if (net_ratelimit())
 972                        netdev_info(dev,
 973                                    "%pM migrated from %pIS to %pIS\n",
 974                                    src_mac, &rdst->remote_ip.sa, &src_ip->sa);
 975
 976                rdst->remote_ip = *src_ip;
 977                f->updated = jiffies;
 978                vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
 979        } else {
 980                /* learned new entry */
 981                spin_lock(&vxlan->hash_lock);
 982
 983                /* close off race between vxlan_flush and incoming packets */
 984                if (netif_running(dev))
 985                        vxlan_fdb_create(vxlan, src_mac, src_ip,
 986                                         NUD_REACHABLE,
 987                                         NLM_F_EXCL|NLM_F_CREATE,
 988                                         vxlan->cfg.dst_port,
 989                                         vni,
 990                                         vxlan->default_dst.remote_vni,
 991                                         ifindex, NTF_SELF);
 992                spin_unlock(&vxlan->hash_lock);
 993        }
 994
 995        return false;
 996}
 997
 998/* See if multicast group is already in use by other ID */
 999static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1000{
1001        struct vxlan_dev *vxlan;
1002        struct vxlan_sock *sock4;
1003#if IS_ENABLED(CONFIG_IPV6)
1004        struct vxlan_sock *sock6;
1005#endif
1006        unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1007
1008        sock4 = rtnl_dereference(dev->vn4_sock);
1009
1010        /* The vxlan_sock is only used by dev, leaving group has
1011         * no effect on other vxlan devices.
1012         */
1013        if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
1014                return false;
1015#if IS_ENABLED(CONFIG_IPV6)
1016        sock6 = rtnl_dereference(dev->vn6_sock);
1017        if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
1018                return false;
1019#endif
1020
1021        list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1022                if (!netif_running(vxlan->dev) || vxlan == dev)
1023                        continue;
1024
1025                if (family == AF_INET &&
1026                    rtnl_dereference(vxlan->vn4_sock) != sock4)
1027                        continue;
1028#if IS_ENABLED(CONFIG_IPV6)
1029                if (family == AF_INET6 &&
1030                    rtnl_dereference(vxlan->vn6_sock) != sock6)
1031                        continue;
1032#endif
1033
1034                if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1035                                      &dev->default_dst.remote_ip))
1036                        continue;
1037
1038                if (vxlan->default_dst.remote_ifindex !=
1039                    dev->default_dst.remote_ifindex)
1040                        continue;
1041
1042                return true;
1043        }
1044
1045        return false;
1046}
1047
1048static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1049{
1050        struct vxlan_net *vn;
1051
1052        if (!vs)
1053                return false;
1054        if (!atomic_dec_and_test(&vs->refcnt))
1055                return false;
1056
1057        vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1058        spin_lock(&vn->sock_lock);
1059        hlist_del_rcu(&vs->hlist);
1060        udp_tunnel_notify_del_rx_port(vs->sock,
1061                                      (vs->flags & VXLAN_F_GPE) ?
1062                                      UDP_TUNNEL_TYPE_VXLAN_GPE :
1063                                      UDP_TUNNEL_TYPE_VXLAN);
1064        spin_unlock(&vn->sock_lock);
1065
1066        return true;
1067}
1068
1069static void vxlan_sock_release(struct vxlan_dev *vxlan)
1070{
1071        struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1072#if IS_ENABLED(CONFIG_IPV6)
1073        struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1074
1075        RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1076#endif
1077
1078        RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1079        synchronize_net();
1080
1081        vxlan_vs_del_dev(vxlan);
1082
1083        if (__vxlan_sock_release_prep(sock4)) {
1084                udp_tunnel_sock_release(sock4->sock);
1085                kfree(sock4);
1086        }
1087
1088#if IS_ENABLED(CONFIG_IPV6)
1089        if (__vxlan_sock_release_prep(sock6)) {
1090                udp_tunnel_sock_release(sock6->sock);
1091                kfree(sock6);
1092        }
1093#endif
1094}
1095
1096/* Update multicast group membership when first VNI on
1097 * multicast address is brought up
1098 */
1099static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1100{
1101        struct sock *sk;
1102        union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1103        int ifindex = vxlan->default_dst.remote_ifindex;
1104        int ret = -EINVAL;
1105
1106        if (ip->sa.sa_family == AF_INET) {
1107                struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1108                struct ip_mreqn mreq = {
1109                        .imr_multiaddr.s_addr   = ip->sin.sin_addr.s_addr,
1110                        .imr_ifindex            = ifindex,
1111                };
1112
1113                sk = sock4->sock->sk;
1114                lock_sock(sk);
1115                ret = ip_mc_join_group(sk, &mreq);
1116                release_sock(sk);
1117#if IS_ENABLED(CONFIG_IPV6)
1118        } else {
1119                struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1120
1121                sk = sock6->sock->sk;
1122                lock_sock(sk);
1123                ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1124                                                   &ip->sin6.sin6_addr);
1125                release_sock(sk);
1126#endif
1127        }
1128
1129        return ret;
1130}
1131
1132/* Inverse of vxlan_igmp_join when last VNI is brought down */
1133static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1134{
1135        struct sock *sk;
1136        union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1137        int ifindex = vxlan->default_dst.remote_ifindex;
1138        int ret = -EINVAL;
1139
1140        if (ip->sa.sa_family == AF_INET) {
1141                struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1142                struct ip_mreqn mreq = {
1143                        .imr_multiaddr.s_addr   = ip->sin.sin_addr.s_addr,
1144                        .imr_ifindex            = ifindex,
1145                };
1146
1147                sk = sock4->sock->sk;
1148                lock_sock(sk);
1149                ret = ip_mc_leave_group(sk, &mreq);
1150                release_sock(sk);
1151#if IS_ENABLED(CONFIG_IPV6)
1152        } else {
1153                struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1154
1155                sk = sock6->sock->sk;
1156                lock_sock(sk);
1157                ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1158                                                   &ip->sin6.sin6_addr);
1159                release_sock(sk);
1160#endif
1161        }
1162
1163        return ret;
1164}
1165
1166static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1167                          struct sk_buff *skb, u32 vxflags)
1168{
1169        size_t start, offset;
1170
1171        if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1172                goto out;
1173
1174        start = vxlan_rco_start(unparsed->vx_vni);
1175        offset = start + vxlan_rco_offset(unparsed->vx_vni);
1176
1177        if (!pskb_may_pull(skb, offset + sizeof(u16)))
1178                return false;
1179
1180        skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1181                            !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1182out:
1183        unparsed->vx_flags &= ~VXLAN_HF_RCO;
1184        unparsed->vx_vni &= VXLAN_VNI_MASK;
1185        return true;
1186}
1187
1188static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1189                                struct sk_buff *skb, u32 vxflags,
1190                                struct vxlan_metadata *md)
1191{
1192        struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1193        struct metadata_dst *tun_dst;
1194
1195        if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1196                goto out;
1197
1198        md->gbp = ntohs(gbp->policy_id);
1199
1200        tun_dst = (struct metadata_dst *)skb_dst(skb);
1201        if (tun_dst) {
1202                tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1203                tun_dst->u.tun_info.options_len = sizeof(*md);
1204        }
1205
1206        if (gbp->dont_learn)
1207                md->gbp |= VXLAN_GBP_DONT_LEARN;
1208
1209        if (gbp->policy_applied)
1210                md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1211
1212        /* In flow-based mode, GBP is carried in dst_metadata */
1213        if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1214                skb->mark = md->gbp;
1215out:
1216        unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1217}
1218
1219static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1220                                __be16 *protocol,
1221                                struct sk_buff *skb, u32 vxflags)
1222{
1223        struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1224
1225        /* Need to have Next Protocol set for interfaces in GPE mode. */
1226        if (!gpe->np_applied)
1227                return false;
1228        /* "The initial version is 0. If a receiver does not support the
1229         * version indicated it MUST drop the packet.
1230         */
1231        if (gpe->version != 0)
1232                return false;
1233        /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1234         * processing MUST occur." However, we don't implement OAM
1235         * processing, thus drop the packet.
1236         */
1237        if (gpe->oam_flag)
1238                return false;
1239
1240        *protocol = tun_p_to_eth_p(gpe->next_protocol);
1241        if (!*protocol)
1242                return false;
1243
1244        unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1245        return true;
1246}
1247
1248static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1249                          struct vxlan_sock *vs,
1250                          struct sk_buff *skb, __be32 vni)
1251{
1252        union vxlan_addr saddr;
1253        u32 ifindex = skb->dev->ifindex;
1254
1255        skb_reset_mac_header(skb);
1256        skb->protocol = eth_type_trans(skb, vxlan->dev);
1257        skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1258
1259        /* Ignore packet loops (and multicast echo) */
1260        if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1261                return false;
1262
1263        /* Get address from the outer IP header */
1264        if (vxlan_get_sk_family(vs) == AF_INET) {
1265                saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1266                saddr.sa.sa_family = AF_INET;
1267#if IS_ENABLED(CONFIG_IPV6)
1268        } else {
1269                saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1270                saddr.sa.sa_family = AF_INET6;
1271#endif
1272        }
1273
1274        if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1275            vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1276                return false;
1277
1278        return true;
1279}
1280
1281static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1282                                  struct sk_buff *skb)
1283{
1284        int err = 0;
1285
1286        if (vxlan_get_sk_family(vs) == AF_INET)
1287                err = IP_ECN_decapsulate(oiph, skb);
1288#if IS_ENABLED(CONFIG_IPV6)
1289        else
1290                err = IP6_ECN_decapsulate(oiph, skb);
1291#endif
1292
1293        if (unlikely(err) && log_ecn_error) {
1294                if (vxlan_get_sk_family(vs) == AF_INET)
1295                        net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1296                                             &((struct iphdr *)oiph)->saddr,
1297                                             ((struct iphdr *)oiph)->tos);
1298                else
1299                        net_info_ratelimited("non-ECT from %pI6\n",
1300                                             &((struct ipv6hdr *)oiph)->saddr);
1301        }
1302        return err <= 1;
1303}
1304
1305/* Callback from net/ipv4/udp.c to receive packets */
1306static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1307{
1308        struct pcpu_sw_netstats *stats;
1309        struct vxlan_dev *vxlan;
1310        struct vxlan_sock *vs;
1311        struct vxlanhdr unparsed;
1312        struct vxlan_metadata _md;
1313        struct vxlan_metadata *md = &_md;
1314        __be16 protocol = htons(ETH_P_TEB);
1315        bool raw_proto = false;
1316        void *oiph;
1317        __be32 vni = 0;
1318
1319        /* Need UDP and VXLAN header to be present */
1320        if (!pskb_may_pull(skb, VXLAN_HLEN))
1321                goto drop;
1322
1323        unparsed = *vxlan_hdr(skb);
1324        /* VNI flag always required to be set */
1325        if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1326                netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1327                           ntohl(vxlan_hdr(skb)->vx_flags),
1328                           ntohl(vxlan_hdr(skb)->vx_vni));
1329                /* Return non vxlan pkt */
1330                goto drop;
1331        }
1332        unparsed.vx_flags &= ~VXLAN_HF_VNI;
1333        unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1334
1335        vs = rcu_dereference_sk_user_data(sk);
1336        if (!vs)
1337                goto drop;
1338
1339        vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1340
1341        vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1342        if (!vxlan)
1343                goto drop;
1344
1345        /* For backwards compatibility, only allow reserved fields to be
1346         * used by VXLAN extensions if explicitly requested.
1347         */
1348        if (vs->flags & VXLAN_F_GPE) {
1349                if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1350                        goto drop;
1351                raw_proto = true;
1352        }
1353
1354        if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1355                                   !net_eq(vxlan->net, dev_net(vxlan->dev))))
1356                        goto drop;
1357
1358        if (vxlan_collect_metadata(vs)) {
1359                struct metadata_dst *tun_dst;
1360
1361                tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1362                                         key32_to_tunnel_id(vni), sizeof(*md));
1363
1364                if (!tun_dst)
1365                        goto drop;
1366
1367                md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1368
1369                skb_dst_set(skb, (struct dst_entry *)tun_dst);
1370        } else {
1371                memset(md, 0, sizeof(*md));
1372        }
1373
1374        if (vs->flags & VXLAN_F_REMCSUM_RX)
1375                if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1376                        goto drop;
1377        if (vs->flags & VXLAN_F_GBP)
1378                vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1379        /* Note that GBP and GPE can never be active together. This is
1380         * ensured in vxlan_dev_configure.
1381         */
1382
1383        if (unparsed.vx_flags || unparsed.vx_vni) {
1384                /* If there are any unprocessed flags remaining treat
1385                 * this as a malformed packet. This behavior diverges from
1386                 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1387                 * in reserved fields are to be ignored. The approach here
1388                 * maintains compatibility with previous stack code, and also
1389                 * is more robust and provides a little more security in
1390                 * adding extensions to VXLAN.
1391                 */
1392                goto drop;
1393        }
1394
1395        if (!raw_proto) {
1396                if (!vxlan_set_mac(vxlan, vs, skb, vni))
1397                        goto drop;
1398        } else {
1399                skb_reset_mac_header(skb);
1400                skb->dev = vxlan->dev;
1401                skb->pkt_type = PACKET_HOST;
1402        }
1403
1404        oiph = skb_network_header(skb);
1405        skb_reset_network_header(skb);
1406
1407        if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1408                ++vxlan->dev->stats.rx_frame_errors;
1409                ++vxlan->dev->stats.rx_errors;
1410                goto drop;
1411        }
1412
1413        rcu_read_lock();
1414
1415        if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1416                rcu_read_unlock();
1417                atomic_long_inc(&vxlan->dev->rx_dropped);
1418                goto drop;
1419        }
1420
1421        stats = this_cpu_ptr(vxlan->dev->tstats);
1422        u64_stats_update_begin(&stats->syncp);
1423        stats->rx_packets++;
1424        stats->rx_bytes += skb->len;
1425        u64_stats_update_end(&stats->syncp);
1426
1427        gro_cells_receive(&vxlan->gro_cells, skb);
1428
1429        rcu_read_unlock();
1430
1431        return 0;
1432
1433drop:
1434        /* Consume bad packet */
1435        kfree_skb(skb);
1436        return 0;
1437}
1438
1439/* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
1440static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1441{
1442        struct vxlan_dev *vxlan;
1443        struct vxlan_sock *vs;
1444        struct vxlanhdr *hdr;
1445        __be32 vni;
1446
1447        if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1448                return -EINVAL;
1449
1450        hdr = vxlan_hdr(skb);
1451
1452        if (!(hdr->vx_flags & VXLAN_HF_VNI))
1453                return -EINVAL;
1454
1455        vs = rcu_dereference_sk_user_data(sk);
1456        if (!vs)
1457                return -ENOENT;
1458
1459        vni = vxlan_vni(hdr->vx_vni);
1460        vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1461        if (!vxlan)
1462                return -ENOENT;
1463
1464        return 0;
1465}
1466
1467static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1468{
1469        struct vxlan_dev *vxlan = netdev_priv(dev);
1470        struct arphdr *parp;
1471        u8 *arpptr, *sha;
1472        __be32 sip, tip;
1473        struct neighbour *n;
1474
1475        if (dev->flags & IFF_NOARP)
1476                goto out;
1477
1478        if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1479                dev->stats.tx_dropped++;
1480                goto out;
1481        }
1482        parp = arp_hdr(skb);
1483
1484        if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1485             parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1486            parp->ar_pro != htons(ETH_P_IP) ||
1487            parp->ar_op != htons(ARPOP_REQUEST) ||
1488            parp->ar_hln != dev->addr_len ||
1489            parp->ar_pln != 4)
1490                goto out;
1491        arpptr = (u8 *)parp + sizeof(struct arphdr);
1492        sha = arpptr;
1493        arpptr += dev->addr_len;        /* sha */
1494        memcpy(&sip, arpptr, sizeof(sip));
1495        arpptr += sizeof(sip);
1496        arpptr += dev->addr_len;        /* tha */
1497        memcpy(&tip, arpptr, sizeof(tip));
1498
1499        if (ipv4_is_loopback(tip) ||
1500            ipv4_is_multicast(tip))
1501                goto out;
1502
1503        n = neigh_lookup(&arp_tbl, &tip, dev);
1504
1505        if (n) {
1506                struct vxlan_fdb *f;
1507                struct sk_buff  *reply;
1508
1509                if (!(n->nud_state & NUD_CONNECTED)) {
1510                        neigh_release(n);
1511                        goto out;
1512                }
1513
1514                f = vxlan_find_mac(vxlan, n->ha, vni);
1515                if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1516                        /* bridge-local neighbor */
1517                        neigh_release(n);
1518                        goto out;
1519                }
1520
1521                reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1522                                n->ha, sha);
1523
1524                neigh_release(n);
1525
1526                if (reply == NULL)
1527                        goto out;
1528
1529                skb_reset_mac_header(reply);
1530                __skb_pull(reply, skb_network_offset(reply));
1531                reply->ip_summed = CHECKSUM_UNNECESSARY;
1532                reply->pkt_type = PACKET_HOST;
1533
1534                if (netif_rx_ni(reply) == NET_RX_DROP)
1535                        dev->stats.rx_dropped++;
1536        } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
1537                union vxlan_addr ipa = {
1538                        .sin.sin_addr.s_addr = tip,
1539                        .sin.sin_family = AF_INET,
1540                };
1541
1542                vxlan_ip_miss(dev, &ipa);
1543        }
1544out:
1545        consume_skb(skb);
1546        return NETDEV_TX_OK;
1547}
1548
1549#if IS_ENABLED(CONFIG_IPV6)
1550static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1551        struct neighbour *n, bool isrouter)
1552{
1553        struct net_device *dev = request->dev;
1554        struct sk_buff *reply;
1555        struct nd_msg *ns, *na;
1556        struct ipv6hdr *pip6;
1557        u8 *daddr;
1558        int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1559        int ns_olen;
1560        int i, len;
1561
1562        if (dev == NULL || !pskb_may_pull(request, request->len))
1563                return NULL;
1564
1565        len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1566                sizeof(*na) + na_olen + dev->needed_tailroom;
1567        reply = alloc_skb(len, GFP_ATOMIC);
1568        if (reply == NULL)
1569                return NULL;
1570
1571        reply->protocol = htons(ETH_P_IPV6);
1572        reply->dev = dev;
1573        skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1574        skb_push(reply, sizeof(struct ethhdr));
1575        skb_reset_mac_header(reply);
1576
1577        ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
1578
1579        daddr = eth_hdr(request)->h_source;
1580        ns_olen = request->len - skb_network_offset(request) -
1581                sizeof(struct ipv6hdr) - sizeof(*ns);
1582        for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1583                if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1584                        daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1585                        break;
1586                }
1587        }
1588
1589        /* Ethernet header */
1590        ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1591        ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1592        eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1593        reply->protocol = htons(ETH_P_IPV6);
1594
1595        skb_pull(reply, sizeof(struct ethhdr));
1596        skb_reset_network_header(reply);
1597        skb_put(reply, sizeof(struct ipv6hdr));
1598
1599        /* IPv6 header */
1600
1601        pip6 = ipv6_hdr(reply);
1602        memset(pip6, 0, sizeof(struct ipv6hdr));
1603        pip6->version = 6;
1604        pip6->priority = ipv6_hdr(request)->priority;
1605        pip6->nexthdr = IPPROTO_ICMPV6;
1606        pip6->hop_limit = 255;
1607        pip6->daddr = ipv6_hdr(request)->saddr;
1608        pip6->saddr = *(struct in6_addr *)n->primary_key;
1609
1610        skb_pull(reply, sizeof(struct ipv6hdr));
1611        skb_reset_transport_header(reply);
1612
1613        /* Neighbor Advertisement */
1614        na = skb_put_zero(reply, sizeof(*na) + na_olen);
1615        na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1616        na->icmph.icmp6_router = isrouter;
1617        na->icmph.icmp6_override = 1;
1618        na->icmph.icmp6_solicited = 1;
1619        na->target = ns->target;
1620        ether_addr_copy(&na->opt[2], n->ha);
1621        na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1622        na->opt[1] = na_olen >> 3;
1623
1624        na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1625                &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1626                csum_partial(na, sizeof(*na)+na_olen, 0));
1627
1628        pip6->payload_len = htons(sizeof(*na)+na_olen);
1629
1630        skb_push(reply, sizeof(struct ipv6hdr));
1631
1632        reply->ip_summed = CHECKSUM_UNNECESSARY;
1633
1634        return reply;
1635}
1636
1637static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1638{
1639        struct vxlan_dev *vxlan = netdev_priv(dev);
1640        const struct in6_addr *daddr;
1641        const struct ipv6hdr *iphdr;
1642        struct inet6_dev *in6_dev;
1643        struct neighbour *n;
1644        struct nd_msg *msg;
1645
1646        in6_dev = __in6_dev_get(dev);
1647        if (!in6_dev)
1648                goto out;
1649
1650        iphdr = ipv6_hdr(skb);
1651        daddr = &iphdr->daddr;
1652        msg = (struct nd_msg *)(iphdr + 1);
1653
1654        if (ipv6_addr_loopback(daddr) ||
1655            ipv6_addr_is_multicast(&msg->target))
1656                goto out;
1657
1658        n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1659
1660        if (n) {
1661                struct vxlan_fdb *f;
1662                struct sk_buff *reply;
1663
1664                if (!(n->nud_state & NUD_CONNECTED)) {
1665                        neigh_release(n);
1666                        goto out;
1667                }
1668
1669                f = vxlan_find_mac(vxlan, n->ha, vni);
1670                if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1671                        /* bridge-local neighbor */
1672                        neigh_release(n);
1673                        goto out;
1674                }
1675
1676                reply = vxlan_na_create(skb, n,
1677                                        !!(f ? f->flags & NTF_ROUTER : 0));
1678
1679                neigh_release(n);
1680
1681                if (reply == NULL)
1682                        goto out;
1683
1684                if (netif_rx_ni(reply) == NET_RX_DROP)
1685                        dev->stats.rx_dropped++;
1686
1687        } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
1688                union vxlan_addr ipa = {
1689                        .sin6.sin6_addr = msg->target,
1690                        .sin6.sin6_family = AF_INET6,
1691                };
1692
1693                vxlan_ip_miss(dev, &ipa);
1694        }
1695
1696out:
1697        consume_skb(skb);
1698        return NETDEV_TX_OK;
1699}
1700#endif
1701
1702static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1703{
1704        struct vxlan_dev *vxlan = netdev_priv(dev);
1705        struct neighbour *n;
1706
1707        if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1708                return false;
1709
1710        n = NULL;
1711        switch (ntohs(eth_hdr(skb)->h_proto)) {
1712        case ETH_P_IP:
1713        {
1714                struct iphdr *pip;
1715
1716                if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1717                        return false;
1718                pip = ip_hdr(skb);
1719                n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1720                if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
1721                        union vxlan_addr ipa = {
1722                                .sin.sin_addr.s_addr = pip->daddr,
1723                                .sin.sin_family = AF_INET,
1724                        };
1725
1726                        vxlan_ip_miss(dev, &ipa);
1727                        return false;
1728                }
1729
1730                break;
1731        }
1732#if IS_ENABLED(CONFIG_IPV6)
1733        case ETH_P_IPV6:
1734        {
1735                struct ipv6hdr *pip6;
1736
1737                if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1738                        return false;
1739                pip6 = ipv6_hdr(skb);
1740                n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1741                if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
1742                        union vxlan_addr ipa = {
1743                                .sin6.sin6_addr = pip6->daddr,
1744                                .sin6.sin6_family = AF_INET6,
1745                        };
1746
1747                        vxlan_ip_miss(dev, &ipa);
1748                        return false;
1749                }
1750
1751                break;
1752        }
1753#endif
1754        default:
1755                return false;
1756        }
1757
1758        if (n) {
1759                bool diff;
1760
1761                diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1762                if (diff) {
1763                        memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1764                                dev->addr_len);
1765                        memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1766                }
1767                neigh_release(n);
1768                return diff;
1769        }
1770
1771        return false;
1772}
1773
1774static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1775                                struct vxlan_metadata *md)
1776{
1777        struct vxlanhdr_gbp *gbp;
1778
1779        if (!md->gbp)
1780                return;
1781
1782        gbp = (struct vxlanhdr_gbp *)vxh;
1783        vxh->vx_flags |= VXLAN_HF_GBP;
1784
1785        if (md->gbp & VXLAN_GBP_DONT_LEARN)
1786                gbp->dont_learn = 1;
1787
1788        if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1789                gbp->policy_applied = 1;
1790
1791        gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1792}
1793
1794static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1795                               __be16 protocol)
1796{
1797        struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1798
1799        gpe->np_applied = 1;
1800        gpe->next_protocol = tun_p_from_eth_p(protocol);
1801        if (!gpe->next_protocol)
1802                return -EPFNOSUPPORT;
1803        return 0;
1804}
1805
1806static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1807                           int iphdr_len, __be32 vni,
1808                           struct vxlan_metadata *md, u32 vxflags,
1809                           bool udp_sum)
1810{
1811        struct vxlanhdr *vxh;
1812        int min_headroom;
1813        int err;
1814        int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1815        __be16 inner_protocol = htons(ETH_P_TEB);
1816
1817        min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1818                        + VXLAN_HLEN + iphdr_len;
1819
1820        /* Need space for new headers (invalidates iph ptr) */
1821        err = skb_cow_head(skb, min_headroom);
1822        if (unlikely(err))
1823                return err;
1824
1825        err = iptunnel_handle_offloads(skb, type);
1826        if (err)
1827                return err;
1828
1829        vxh = __skb_push(skb, sizeof(*vxh));
1830        vxh->vx_flags = VXLAN_HF_VNI;
1831        vxh->vx_vni = vxlan_vni_field(vni);
1832
1833        if (vxflags & VXLAN_F_GBP)
1834                vxlan_build_gbp_hdr(vxh, vxflags, md);
1835        if (vxflags & VXLAN_F_GPE) {
1836                err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1837                if (err < 0)
1838                        return err;
1839                inner_protocol = skb->protocol;
1840        }
1841
1842        skb_set_inner_protocol(skb, inner_protocol);
1843        return 0;
1844}
1845
1846static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1847                                      struct vxlan_sock *sock4,
1848                                      struct sk_buff *skb, int oif, u8 tos,
1849                                      __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
1850                                      struct dst_cache *dst_cache,
1851                                      const struct ip_tunnel_info *info)
1852{
1853        bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1854        struct rtable *rt = NULL;
1855        struct flowi4 fl4;
1856
1857        if (!sock4)
1858                return ERR_PTR(-EIO);
1859
1860        if (tos && !info)
1861                use_cache = false;
1862        if (use_cache) {
1863                rt = dst_cache_get_ip4(dst_cache, saddr);
1864                if (rt)
1865                        return rt;
1866        }
1867
1868        memset(&fl4, 0, sizeof(fl4));
1869        fl4.flowi4_oif = oif;
1870        fl4.flowi4_tos = RT_TOS(tos);
1871        fl4.flowi4_mark = skb->mark;
1872        fl4.flowi4_proto = IPPROTO_UDP;
1873        fl4.daddr = daddr;
1874        fl4.saddr = *saddr;
1875        fl4.fl4_dport = dport;
1876        fl4.fl4_sport = sport;
1877
1878        rt = ip_route_output_key(vxlan->net, &fl4);
1879        if (likely(!IS_ERR(rt))) {
1880                if (rt->dst.dev == dev) {
1881                        netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1882                        ip_rt_put(rt);
1883                        return ERR_PTR(-ELOOP);
1884                }
1885
1886                *saddr = fl4.saddr;
1887                if (use_cache)
1888                        dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
1889        } else {
1890                netdev_dbg(dev, "no route to %pI4\n", &daddr);
1891                return ERR_PTR(-ENETUNREACH);
1892        }
1893        return rt;
1894}
1895
1896#if IS_ENABLED(CONFIG_IPV6)
1897static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
1898                                          struct net_device *dev,
1899                                          struct vxlan_sock *sock6,
1900                                          struct sk_buff *skb, int oif, u8 tos,
1901                                          __be32 label,
1902                                          const struct in6_addr *daddr,
1903                                          struct in6_addr *saddr,
1904                                          __be16 dport, __be16 sport,
1905                                          struct dst_cache *dst_cache,
1906                                          const struct ip_tunnel_info *info)
1907{
1908        bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1909        struct dst_entry *ndst;
1910        struct flowi6 fl6;
1911        int err;
1912
1913        if (!sock6)
1914                return ERR_PTR(-EIO);
1915
1916        if (tos && !info)
1917                use_cache = false;
1918        if (use_cache) {
1919                ndst = dst_cache_get_ip6(dst_cache, saddr);
1920                if (ndst)
1921                        return ndst;
1922        }
1923
1924        memset(&fl6, 0, sizeof(fl6));
1925        fl6.flowi6_oif = oif;
1926        fl6.daddr = *daddr;
1927        fl6.saddr = *saddr;
1928        fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
1929        fl6.flowi6_mark = skb->mark;
1930        fl6.flowi6_proto = IPPROTO_UDP;
1931        fl6.fl6_dport = dport;
1932        fl6.fl6_sport = sport;
1933
1934        err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
1935                                         sock6->sock->sk,
1936                                         &ndst, &fl6);
1937        if (unlikely(err < 0)) {
1938                netdev_dbg(dev, "no route to %pI6\n", daddr);
1939                return ERR_PTR(-ENETUNREACH);
1940        }
1941
1942        if (unlikely(ndst->dev == dev)) {
1943                netdev_dbg(dev, "circular route to %pI6\n", daddr);
1944                dst_release(ndst);
1945                return ERR_PTR(-ELOOP);
1946        }
1947
1948        *saddr = fl6.saddr;
1949        if (use_cache)
1950                dst_cache_set_ip6(dst_cache, ndst, saddr);
1951        return ndst;
1952}
1953#endif
1954
1955/* Bypass encapsulation if the destination is local */
1956static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1957                               struct vxlan_dev *dst_vxlan, __be32 vni)
1958{
1959        struct pcpu_sw_netstats *tx_stats, *rx_stats;
1960        union vxlan_addr loopback;
1961        union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1962        struct net_device *dev;
1963        int len = skb->len;
1964
1965        tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1966        rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1967        skb->pkt_type = PACKET_HOST;
1968        skb->encapsulation = 0;
1969        skb->dev = dst_vxlan->dev;
1970        __skb_pull(skb, skb_network_offset(skb));
1971
1972        if (remote_ip->sa.sa_family == AF_INET) {
1973                loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1974                loopback.sa.sa_family =  AF_INET;
1975#if IS_ENABLED(CONFIG_IPV6)
1976        } else {
1977                loopback.sin6.sin6_addr = in6addr_loopback;
1978                loopback.sa.sa_family =  AF_INET6;
1979#endif
1980        }
1981
1982        rcu_read_lock();
1983        dev = skb->dev;
1984        if (unlikely(!(dev->flags & IFF_UP))) {
1985                kfree_skb(skb);
1986                goto drop;
1987        }
1988
1989        if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
1990                vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
1991
1992        u64_stats_update_begin(&tx_stats->syncp);
1993        tx_stats->tx_packets++;
1994        tx_stats->tx_bytes += len;
1995        u64_stats_update_end(&tx_stats->syncp);
1996
1997        if (netif_rx(skb) == NET_RX_SUCCESS) {
1998                u64_stats_update_begin(&rx_stats->syncp);
1999                rx_stats->rx_packets++;
2000                rx_stats->rx_bytes += len;
2001                u64_stats_update_end(&rx_stats->syncp);
2002        } else {
2003drop:
2004                dev->stats.rx_dropped++;
2005        }
2006        rcu_read_unlock();
2007}
2008
2009static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2010                                 struct vxlan_dev *vxlan,
2011                                 union vxlan_addr *daddr,
2012                                 __be16 dst_port, int dst_ifindex, __be32 vni,
2013                                 struct dst_entry *dst,
2014                                 u32 rt_flags)
2015{
2016#if IS_ENABLED(CONFIG_IPV6)
2017        /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2018         * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2019         * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2020         */
2021        BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2022#endif
2023        /* Bypass encapsulation if the destination is local */
2024        if (rt_flags & RTCF_LOCAL &&
2025            !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2026                struct vxlan_dev *dst_vxlan;
2027
2028                dst_release(dst);
2029                dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2030                                           daddr->sa.sa_family, dst_port,
2031                                           vxlan->cfg.flags);
2032                if (!dst_vxlan) {
2033                        dev->stats.tx_errors++;
2034                        kfree_skb(skb);
2035
2036                        return -ENOENT;
2037                }
2038                vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
2039                return 1;
2040        }
2041
2042        return 0;
2043}
2044
2045static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2046                           __be32 default_vni, struct vxlan_rdst *rdst,
2047                           bool did_rsc)
2048{
2049        struct dst_cache *dst_cache;
2050        struct ip_tunnel_info *info;
2051        struct vxlan_dev *vxlan = netdev_priv(dev);
2052        const struct iphdr *old_iph = ip_hdr(skb);
2053        union vxlan_addr *dst;
2054        union vxlan_addr remote_ip, local_ip;
2055        struct vxlan_metadata _md;
2056        struct vxlan_metadata *md = &_md;
2057        __be16 src_port = 0, dst_port;
2058        struct dst_entry *ndst = NULL;
2059        __be32 vni, label;
2060        __u8 tos, ttl;
2061        int ifindex;
2062        int err;
2063        u32 flags = vxlan->cfg.flags;
2064        bool udp_sum = false;
2065        bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2066
2067        info = skb_tunnel_info(skb);
2068
2069        if (rdst) {
2070                dst = &rdst->remote_ip;
2071                if (vxlan_addr_any(dst)) {
2072                        if (did_rsc) {
2073                                /* short-circuited back to local bridge */
2074                                vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
2075                                return;
2076                        }
2077                        goto drop;
2078                }
2079
2080                dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2081                vni = (rdst->remote_vni) ? : default_vni;
2082                ifindex = rdst->remote_ifindex;
2083                local_ip = vxlan->cfg.saddr;
2084                dst_cache = &rdst->dst_cache;
2085                md->gbp = skb->mark;
2086                if (flags & VXLAN_F_TTL_INHERIT) {
2087                        ttl = ip_tunnel_get_ttl(old_iph, skb);
2088                } else {
2089                        ttl = vxlan->cfg.ttl;
2090                        if (!ttl && vxlan_addr_multicast(dst))
2091                                ttl = 1;
2092                }
2093
2094                tos = vxlan->cfg.tos;
2095                if (tos == 1)
2096                        tos = ip_tunnel_get_dsfield(old_iph, skb);
2097
2098                if (dst->sa.sa_family == AF_INET)
2099                        udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2100                else
2101                        udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2102                label = vxlan->cfg.label;
2103        } else {
2104                if (!info) {
2105                        WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2106                                  dev->name);
2107                        goto drop;
2108                }
2109                remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2110                if (remote_ip.sa.sa_family == AF_INET) {
2111                        remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2112                        local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2113                } else {
2114                        remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2115                        local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2116                }
2117                dst = &remote_ip;
2118                dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2119                vni = tunnel_id_to_key32(info->key.tun_id);
2120                ifindex = 0;
2121                dst_cache = &info->dst_cache;
2122                if (info->options_len &&
2123                    info->key.tun_flags & TUNNEL_VXLAN_OPT)
2124                        md = ip_tunnel_info_opts(info);
2125                ttl = info->key.ttl;
2126                tos = info->key.tos;
2127                label = info->key.label;
2128                udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2129        }
2130        src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2131                                     vxlan->cfg.port_max, true);
2132
2133        rcu_read_lock();
2134        if (dst->sa.sa_family == AF_INET) {
2135                struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2136                struct rtable *rt;
2137                __be16 df = 0;
2138
2139                rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2140                                     dst->sin.sin_addr.s_addr,
2141                                     &local_ip.sin.sin_addr.s_addr,
2142                                     dst_port, src_port,
2143                                     dst_cache, info);
2144                if (IS_ERR(rt)) {
2145                        err = PTR_ERR(rt);
2146                        goto tx_error;
2147                }
2148
2149                /* Bypass encapsulation if the destination is local */
2150                if (!info) {
2151                        err = encap_bypass_if_local(skb, dev, vxlan, dst,
2152                                                    dst_port, ifindex, vni,
2153                                                    &rt->dst, rt->rt_flags);
2154                        if (err)
2155                                goto out_unlock;
2156                } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2157                        df = htons(IP_DF);
2158                }
2159
2160                ndst = &rt->dst;
2161                skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM);
2162
2163                tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2164                ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2165                err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2166                                      vni, md, flags, udp_sum);
2167                if (err < 0)
2168                        goto tx_error;
2169
2170                udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2171                                    dst->sin.sin_addr.s_addr, tos, ttl, df,
2172                                    src_port, dst_port, xnet, !udp_sum);
2173#if IS_ENABLED(CONFIG_IPV6)
2174        } else {
2175                struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2176
2177                ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2178                                        label, &dst->sin6.sin6_addr,
2179                                        &local_ip.sin6.sin6_addr,
2180                                        dst_port, src_port,
2181                                        dst_cache, info);
2182                if (IS_ERR(ndst)) {
2183                        err = PTR_ERR(ndst);
2184                        ndst = NULL;
2185                        goto tx_error;
2186                }
2187
2188                if (!info) {
2189                        u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2190
2191                        err = encap_bypass_if_local(skb, dev, vxlan, dst,
2192                                                    dst_port, ifindex, vni,
2193                                                    ndst, rt6i_flags);
2194                        if (err)
2195                                goto out_unlock;
2196                }
2197
2198                skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM);
2199
2200                tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2201                ttl = ttl ? : ip6_dst_hoplimit(ndst);
2202                skb_scrub_packet(skb, xnet);
2203                err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2204                                      vni, md, flags, udp_sum);
2205                if (err < 0)
2206                        goto tx_error;
2207
2208                udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2209                                     &local_ip.sin6.sin6_addr,
2210                                     &dst->sin6.sin6_addr, tos, ttl,
2211                                     label, src_port, dst_port, !udp_sum);
2212#endif
2213        }
2214out_unlock:
2215        rcu_read_unlock();
2216        return;
2217
2218drop:
2219        dev->stats.tx_dropped++;
2220        dev_kfree_skb(skb);
2221        return;
2222
2223tx_error:
2224        rcu_read_unlock();
2225        if (err == -ELOOP)
2226                dev->stats.collisions++;
2227        else if (err == -ENETUNREACH)
2228                dev->stats.tx_carrier_errors++;
2229        dst_release(ndst);
2230        dev->stats.tx_errors++;
2231        kfree_skb(skb);
2232}
2233
2234/* Transmit local packets over Vxlan
2235 *
2236 * Outer IP header inherits ECN and DF from inner header.
2237 * Outer UDP destination is the VXLAN assigned port.
2238 *           source port is based on hash of flow
2239 */
2240static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2241{
2242        struct vxlan_dev *vxlan = netdev_priv(dev);
2243        struct vxlan_rdst *rdst, *fdst = NULL;
2244        const struct ip_tunnel_info *info;
2245        bool did_rsc = false;
2246        struct vxlan_fdb *f;
2247        struct ethhdr *eth;
2248        __be32 vni = 0;
2249
2250        info = skb_tunnel_info(skb);
2251
2252        skb_reset_mac_header(skb);
2253
2254        if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2255                if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2256                    info->mode & IP_TUNNEL_INFO_TX) {
2257                        vni = tunnel_id_to_key32(info->key.tun_id);
2258                } else {
2259                        if (info && info->mode & IP_TUNNEL_INFO_TX)
2260                                vxlan_xmit_one(skb, dev, vni, NULL, false);
2261                        else
2262                                kfree_skb(skb);
2263                        return NETDEV_TX_OK;
2264                }
2265        }
2266
2267        if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2268                eth = eth_hdr(skb);
2269                if (ntohs(eth->h_proto) == ETH_P_ARP)
2270                        return arp_reduce(dev, skb, vni);
2271#if IS_ENABLED(CONFIG_IPV6)
2272                else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2273                         pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2274                                            sizeof(struct nd_msg)) &&
2275                         ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2276                        struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2277
2278                        if (m->icmph.icmp6_code == 0 &&
2279                            m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2280                                return neigh_reduce(dev, skb, vni);
2281                }
2282#endif
2283        }
2284
2285        eth = eth_hdr(skb);
2286        f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2287        did_rsc = false;
2288
2289        if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2290            (ntohs(eth->h_proto) == ETH_P_IP ||
2291             ntohs(eth->h_proto) == ETH_P_IPV6)) {
2292                did_rsc = route_shortcircuit(dev, skb);
2293                if (did_rsc)
2294                        f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2295        }
2296
2297        if (f == NULL) {
2298                f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2299                if (f == NULL) {
2300                        if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2301                            !is_multicast_ether_addr(eth->h_dest))
2302                                vxlan_fdb_miss(vxlan, eth->h_dest);
2303
2304                        dev->stats.tx_dropped++;
2305                        kfree_skb(skb);
2306                        return NETDEV_TX_OK;
2307                }
2308        }
2309
2310        list_for_each_entry_rcu(rdst, &f->remotes, list) {
2311                struct sk_buff *skb1;
2312
2313                if (!fdst) {
2314                        fdst = rdst;
2315                        continue;
2316                }
2317                skb1 = skb_clone(skb, GFP_ATOMIC);
2318                if (skb1)
2319                        vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2320        }
2321
2322        if (fdst)
2323                vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2324        else
2325                kfree_skb(skb);
2326        return NETDEV_TX_OK;
2327}
2328
2329/* Walk the forwarding table and purge stale entries */
2330static void vxlan_cleanup(unsigned long arg)
2331{
2332        struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2333        unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2334        unsigned int h;
2335
2336        if (!netif_running(vxlan->dev))
2337                return;
2338
2339        for (h = 0; h < FDB_HASH_SIZE; ++h) {
2340                struct hlist_node *p, *n;
2341
2342                spin_lock_bh(&vxlan->hash_lock);
2343                hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2344                        struct vxlan_fdb *f
2345                                = container_of(p, struct vxlan_fdb, hlist);
2346                        unsigned long timeout;
2347
2348                        if (f->state & (NUD_PERMANENT | NUD_NOARP))
2349                                continue;
2350
2351                        if (f->flags & NTF_EXT_LEARNED)
2352                                continue;
2353
2354                        timeout = f->used + vxlan->cfg.age_interval * HZ;
2355                        if (time_before_eq(timeout, jiffies)) {
2356                                netdev_dbg(vxlan->dev,
2357                                           "garbage collect %pM\n",
2358                                           f->eth_addr);
2359                                f->state = NUD_STALE;
2360                                vxlan_fdb_destroy(vxlan, f);
2361                        } else if (time_before(timeout, next_timer))
2362                                next_timer = timeout;
2363                }
2364                spin_unlock_bh(&vxlan->hash_lock);
2365        }
2366
2367        mod_timer(&vxlan->age_timer, next_timer);
2368}
2369
2370static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2371{
2372        struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2373
2374        spin_lock(&vn->sock_lock);
2375        hlist_del_init_rcu(&vxlan->hlist4.hlist);
2376#if IS_ENABLED(CONFIG_IPV6)
2377        hlist_del_init_rcu(&vxlan->hlist6.hlist);
2378#endif
2379        spin_unlock(&vn->sock_lock);
2380}
2381
2382static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2383                             struct vxlan_dev_node *node)
2384{
2385        struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2386        __be32 vni = vxlan->default_dst.remote_vni;
2387
2388        node->vxlan = vxlan;
2389        spin_lock(&vn->sock_lock);
2390        hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2391        spin_unlock(&vn->sock_lock);
2392}
2393
2394/* Setup stats when device is created */
2395static int vxlan_init(struct net_device *dev)
2396{
2397        dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2398        if (!dev->tstats)
2399                return -ENOMEM;
2400
2401        return 0;
2402}
2403
2404static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
2405{
2406        struct vxlan_fdb *f;
2407
2408        spin_lock_bh(&vxlan->hash_lock);
2409        f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
2410        if (f)
2411                vxlan_fdb_destroy(vxlan, f);
2412        spin_unlock_bh(&vxlan->hash_lock);
2413}
2414
2415static void vxlan_uninit(struct net_device *dev)
2416{
2417        struct vxlan_dev *vxlan = netdev_priv(dev);
2418
2419        gro_cells_destroy(&vxlan->gro_cells);
2420
2421        vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
2422
2423        free_percpu(dev->tstats);
2424}
2425
2426/* Start ageing timer and join group when device is brought up */
2427static int vxlan_open(struct net_device *dev)
2428{
2429        struct vxlan_dev *vxlan = netdev_priv(dev);
2430        int ret;
2431
2432        ret = vxlan_sock_add(vxlan);
2433        if (ret < 0)
2434                return ret;
2435
2436        if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2437                ret = vxlan_igmp_join(vxlan);
2438                if (ret == -EADDRINUSE)
2439                        ret = 0;
2440                if (ret) {
2441                        vxlan_sock_release(vxlan);
2442                        return ret;
2443                }
2444        }
2445
2446        if (vxlan->cfg.age_interval)
2447                mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2448
2449        return ret;
2450}
2451
2452/* Purge the forwarding table */
2453static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
2454{
2455        unsigned int h;
2456
2457        spin_lock_bh(&vxlan->hash_lock);
2458        for (h = 0; h < FDB_HASH_SIZE; ++h) {
2459                struct hlist_node *p, *n;
2460                hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2461                        struct vxlan_fdb *f
2462                                = container_of(p, struct vxlan_fdb, hlist);
2463                        if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2464                                continue;
2465                        /* the all_zeros_mac entry is deleted at vxlan_uninit */
2466                        if (!is_zero_ether_addr(f->eth_addr))
2467                                vxlan_fdb_destroy(vxlan, f);
2468                }
2469        }
2470        spin_unlock_bh(&vxlan->hash_lock);
2471}
2472
2473/* Cleanup timer and forwarding table on shutdown */
2474static int vxlan_stop(struct net_device *dev)
2475{
2476        struct vxlan_dev *vxlan = netdev_priv(dev);
2477        struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2478        int ret = 0;
2479
2480        if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2481            !vxlan_group_used(vn, vxlan))
2482                ret = vxlan_igmp_leave(vxlan);
2483
2484        del_timer_sync(&vxlan->age_timer);
2485
2486        vxlan_flush(vxlan, false);
2487        vxlan_sock_release(vxlan);
2488
2489        return ret;
2490}
2491
2492/* Stub, nothing needs to be done. */
2493static void vxlan_set_multicast_list(struct net_device *dev)
2494{
2495}
2496
2497static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2498{
2499        struct vxlan_dev *vxlan = netdev_priv(dev);
2500        struct vxlan_rdst *dst = &vxlan->default_dst;
2501        struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2502                                                         dst->remote_ifindex);
2503        bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
2504
2505        /* This check is different than dev->max_mtu, because it looks at
2506         * the lowerdev->mtu, rather than the static dev->max_mtu
2507         */
2508        if (lowerdev) {
2509                int max_mtu = lowerdev->mtu -
2510                              (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2511                if (new_mtu > max_mtu)
2512                        return -EINVAL;
2513        }
2514
2515        dev->mtu = new_mtu;
2516        return 0;
2517}
2518
2519static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2520{
2521        struct vxlan_dev *vxlan = netdev_priv(dev);
2522        struct ip_tunnel_info *info = skb_tunnel_info(skb);
2523        __be16 sport, dport;
2524
2525        sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2526                                  vxlan->cfg.port_max, true);
2527        dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2528
2529        if (ip_tunnel_info_af(info) == AF_INET) {
2530                struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2531                struct rtable *rt;
2532
2533                rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
2534                                     info->key.u.ipv4.dst,
2535                                     &info->key.u.ipv4.src, dport, sport,
2536                                     &info->dst_cache, info);
2537                if (IS_ERR(rt))
2538                        return PTR_ERR(rt);
2539                ip_rt_put(rt);
2540        } else {
2541#if IS_ENABLED(CONFIG_IPV6)
2542                struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2543                struct dst_entry *ndst;
2544
2545                ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
2546                                        info->key.label, &info->key.u.ipv6.dst,
2547                                        &info->key.u.ipv6.src, dport, sport,
2548                                        &info->dst_cache, info);
2549                if (IS_ERR(ndst))
2550                        return PTR_ERR(ndst);
2551                dst_release(ndst);
2552#else /* !CONFIG_IPV6 */
2553                return -EPFNOSUPPORT;
2554#endif
2555        }
2556        info->key.tp_src = sport;
2557        info->key.tp_dst = dport;
2558        return 0;
2559}
2560
2561static const struct net_device_ops vxlan_netdev_ether_ops = {
2562        .ndo_size               = sizeof(struct net_device_ops),
2563        .ndo_init               = vxlan_init,
2564        .ndo_uninit             = vxlan_uninit,
2565        .ndo_open               = vxlan_open,
2566        .ndo_stop               = vxlan_stop,
2567        .ndo_start_xmit         = vxlan_xmit,
2568        .ndo_get_stats64        = ip_tunnel_get_stats64,
2569        .ndo_set_rx_mode        = vxlan_set_multicast_list,
2570        .extended.ndo_change_mtu        = vxlan_change_mtu,
2571        .ndo_validate_addr      = eth_validate_addr,
2572        .ndo_set_mac_address    = eth_mac_addr,
2573        .ndo_fdb_add            = vxlan_fdb_add,
2574        .ndo_fdb_del            = vxlan_fdb_delete,
2575        .extended.ndo_fdb_dump  = vxlan_fdb_dump,
2576        .ndo_fill_metadata_dst  = vxlan_fill_metadata_dst,
2577};
2578
2579static const struct net_device_ops vxlan_netdev_raw_ops = {
2580        .ndo_size               = sizeof(struct net_device_ops),
2581        .ndo_init               = vxlan_init,
2582        .ndo_uninit             = vxlan_uninit,
2583        .ndo_open               = vxlan_open,
2584        .ndo_stop               = vxlan_stop,
2585        .ndo_start_xmit         = vxlan_xmit,
2586        .ndo_get_stats64        = ip_tunnel_get_stats64,
2587        .extended.ndo_change_mtu        = vxlan_change_mtu,
2588        .ndo_fill_metadata_dst  = vxlan_fill_metadata_dst,
2589};
2590
2591/* Info for udev, that this is a virtual tunnel endpoint */
2592static struct device_type vxlan_type = {
2593        .name = "vxlan",
2594};
2595
2596/* Calls the ndo_udp_tunnel_add of the caller in order to
2597 * supply the listening VXLAN udp ports. Callers are expected
2598 * to implement the ndo_udp_tunnel_add.
2599 */
2600static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
2601{
2602        struct vxlan_sock *vs;
2603        struct net *net = dev_net(dev);
2604        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2605        unsigned int i;
2606
2607        spin_lock(&vn->sock_lock);
2608        for (i = 0; i < PORT_HASH_SIZE; ++i) {
2609                hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2610                        unsigned short type;
2611
2612                        if (vs->flags & VXLAN_F_GPE)
2613                                type = UDP_TUNNEL_TYPE_VXLAN_GPE;
2614                        else
2615                                type = UDP_TUNNEL_TYPE_VXLAN;
2616
2617                        if (push)
2618                                udp_tunnel_push_rx_port(dev, vs->sock, type);
2619                        else
2620                                udp_tunnel_drop_rx_port(dev, vs->sock, type);
2621                }
2622        }
2623        spin_unlock(&vn->sock_lock);
2624}
2625
2626/* Initialize the device structure. */
2627static void vxlan_setup(struct net_device *dev)
2628{
2629        struct vxlan_dev *vxlan = netdev_priv(dev);
2630        unsigned int h;
2631
2632        eth_hw_addr_random(dev);
2633        ether_setup(dev);
2634
2635        dev->extended->needs_free_netdev = true;
2636        SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2637
2638        dev->features   |= NETIF_F_LLTX;
2639        dev->features   |= NETIF_F_SG | NETIF_F_HW_CSUM;
2640        dev->features   |= NETIF_F_RXCSUM;
2641        dev->features   |= NETIF_F_GSO_SOFTWARE;
2642
2643        dev->vlan_features = dev->features;
2644        dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2645        dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2646        netif_keep_dst(dev);
2647        dev->priv_flags |= IFF_NO_QUEUE;
2648
2649        /* MTU range: 68 - 65535 */
2650        dev->extended->min_mtu = ETH_MIN_MTU;
2651        dev->extended->max_mtu = ETH_MAX_MTU;
2652
2653        INIT_LIST_HEAD(&vxlan->next);
2654        spin_lock_init(&vxlan->hash_lock);
2655
2656        init_timer_deferrable(&vxlan->age_timer);
2657        vxlan->age_timer.function = vxlan_cleanup;
2658        vxlan->age_timer.data = (unsigned long) vxlan;
2659
2660        vxlan->dev = dev;
2661
2662        gro_cells_init(&vxlan->gro_cells, dev);
2663
2664        for (h = 0; h < FDB_HASH_SIZE; ++h)
2665                INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2666}
2667
2668static void vxlan_ether_setup(struct net_device *dev)
2669{
2670        dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2671        dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2672        dev->netdev_ops = &vxlan_netdev_ether_ops;
2673}
2674
2675static void vxlan_raw_setup(struct net_device *dev)
2676{
2677        dev->header_ops = NULL;
2678        dev->type = ARPHRD_NONE;
2679        dev->hard_header_len = 0;
2680        dev->addr_len = 0;
2681        dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2682        dev->netdev_ops = &vxlan_netdev_raw_ops;
2683}
2684
2685static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2686        [IFLA_VXLAN_ID]         = { .type = NLA_U32 },
2687        [IFLA_VXLAN_GROUP]      = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2688        [IFLA_VXLAN_GROUP6]     = { .len = sizeof(struct in6_addr) },
2689        [IFLA_VXLAN_LINK]       = { .type = NLA_U32 },
2690        [IFLA_VXLAN_LOCAL]      = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2691        [IFLA_VXLAN_LOCAL6]     = { .len = sizeof(struct in6_addr) },
2692        [IFLA_VXLAN_TOS]        = { .type = NLA_U8 },
2693        [IFLA_VXLAN_TTL]        = { .type = NLA_U8 },
2694        [IFLA_VXLAN_LABEL]      = { .type = NLA_U32 },
2695        [IFLA_VXLAN_LEARNING]   = { .type = NLA_U8 },
2696        [IFLA_VXLAN_AGEING]     = { .type = NLA_U32 },
2697        [IFLA_VXLAN_LIMIT]      = { .type = NLA_U32 },
2698        [IFLA_VXLAN_PORT_RANGE] = { .len  = sizeof(struct ifla_vxlan_port_range) },
2699        [IFLA_VXLAN_PROXY]      = { .type = NLA_U8 },
2700        [IFLA_VXLAN_RSC]        = { .type = NLA_U8 },
2701        [IFLA_VXLAN_L2MISS]     = { .type = NLA_U8 },
2702        [IFLA_VXLAN_L3MISS]     = { .type = NLA_U8 },
2703        [IFLA_VXLAN_COLLECT_METADATA]   = { .type = NLA_U8 },
2704        [IFLA_VXLAN_PORT]       = { .type = NLA_U16 },
2705        [IFLA_VXLAN_UDP_CSUM]   = { .type = NLA_U8 },
2706        [IFLA_VXLAN_UDP_ZERO_CSUM6_TX]  = { .type = NLA_U8 },
2707        [IFLA_VXLAN_UDP_ZERO_CSUM6_RX]  = { .type = NLA_U8 },
2708        [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2709        [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2710        [IFLA_VXLAN_GBP]        = { .type = NLA_FLAG, },
2711        [IFLA_VXLAN_GPE]        = { .type = NLA_FLAG, },
2712        [IFLA_VXLAN_REMCSUM_NOPARTIAL]  = { .type = NLA_FLAG },
2713        [IFLA_VXLAN_TTL_INHERIT]        = { .type = NLA_FLAG },
2714};
2715
2716static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2717{
2718        if (tb[IFLA_ADDRESS]) {
2719                if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2720                        pr_debug("invalid link address (not ethernet)\n");
2721                        return -EINVAL;
2722                }
2723
2724                if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2725                        pr_debug("invalid all zero ethernet address\n");
2726                        return -EADDRNOTAVAIL;
2727                }
2728        }
2729
2730        if (tb[IFLA_MTU]) {
2731                u32 mtu = nla_get_u32(tb[IFLA_MTU]);
2732
2733                if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU)
2734                        return -EINVAL;
2735        }
2736
2737        if (!data)
2738                return -EINVAL;
2739
2740        if (data[IFLA_VXLAN_ID]) {
2741                u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2742
2743                if (id >= VXLAN_N_VID)
2744                        return -ERANGE;
2745        }
2746
2747        if (data[IFLA_VXLAN_PORT_RANGE]) {
2748                const struct ifla_vxlan_port_range *p
2749                        = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2750
2751                if (ntohs(p->high) < ntohs(p->low)) {
2752                        pr_debug("port range %u .. %u not valid\n",
2753                                 ntohs(p->low), ntohs(p->high));
2754                        return -EINVAL;
2755                }
2756        }
2757
2758        return 0;
2759}
2760
2761static void vxlan_get_drvinfo(struct net_device *netdev,
2762                              struct ethtool_drvinfo *drvinfo)
2763{
2764        strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2765        strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2766}
2767
2768static const struct ethtool_ops vxlan_ethtool_ops = {
2769        .get_drvinfo    = vxlan_get_drvinfo,
2770        .get_link       = ethtool_op_get_link,
2771};
2772
2773static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2774                                        __be16 port, u32 flags)
2775{
2776        struct socket *sock;
2777        struct udp_port_cfg udp_conf;
2778        int err;
2779
2780        memset(&udp_conf, 0, sizeof(udp_conf));
2781
2782        if (ipv6) {
2783                udp_conf.family = AF_INET6;
2784                udp_conf.use_udp6_rx_checksums =
2785                    !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2786                udp_conf.ipv6_v6only = 1;
2787        } else {
2788                udp_conf.family = AF_INET;
2789        }
2790
2791        udp_conf.local_udp_port = port;
2792
2793        /* Open UDP socket */
2794        err = udp_sock_create(net, &udp_conf, &sock);
2795        if (err < 0)
2796                return ERR_PTR(err);
2797
2798        return sock;
2799}
2800
2801/* Create new listen socket if needed */
2802static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2803                                              __be16 port, u32 flags)
2804{
2805        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2806        struct vxlan_sock *vs;
2807        struct socket *sock;
2808        unsigned int h;
2809        struct udp_tunnel_sock_cfg tunnel_cfg;
2810
2811        vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2812        if (!vs)
2813                return ERR_PTR(-ENOMEM);
2814
2815        for (h = 0; h < VNI_HASH_SIZE; ++h)
2816                INIT_HLIST_HEAD(&vs->vni_list[h]);
2817
2818        sock = vxlan_create_sock(net, ipv6, port, flags);
2819        if (IS_ERR(sock)) {
2820                kfree(vs);
2821                return ERR_CAST(sock);
2822        }
2823
2824        vs->sock = sock;
2825        atomic_set(&vs->refcnt, 1);
2826        vs->flags = (flags & VXLAN_F_RCV_FLAGS);
2827
2828        spin_lock(&vn->sock_lock);
2829        hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2830        udp_tunnel_notify_add_rx_port(sock,
2831                                      (vs->flags & VXLAN_F_GPE) ?
2832                                      UDP_TUNNEL_TYPE_VXLAN_GPE :
2833                                      UDP_TUNNEL_TYPE_VXLAN);
2834        spin_unlock(&vn->sock_lock);
2835
2836        /* Mark socket as an encapsulation socket. */
2837        memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
2838        tunnel_cfg.sk_user_data = vs;
2839        tunnel_cfg.encap_type = 1;
2840        tunnel_cfg.encap_rcv = vxlan_rcv;
2841        tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
2842        tunnel_cfg.encap_destroy = NULL;
2843        tunnel_cfg.gro_receive = vxlan_gro_receive;
2844        tunnel_cfg.gro_complete = vxlan_gro_complete;
2845
2846        setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2847
2848        return vs;
2849}
2850
2851static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
2852{
2853        struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2854        struct vxlan_sock *vs = NULL;
2855        struct vxlan_dev_node *node;
2856
2857        if (!vxlan->cfg.no_share) {
2858                spin_lock(&vn->sock_lock);
2859                vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2860                                     vxlan->cfg.dst_port, vxlan->cfg.flags);
2861                if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
2862                        spin_unlock(&vn->sock_lock);
2863                        return -EBUSY;
2864                }
2865                spin_unlock(&vn->sock_lock);
2866        }
2867        if (!vs)
2868                vs = vxlan_socket_create(vxlan->net, ipv6,
2869                                         vxlan->cfg.dst_port, vxlan->cfg.flags);
2870        if (IS_ERR(vs))
2871                return PTR_ERR(vs);
2872#if IS_ENABLED(CONFIG_IPV6)
2873        if (ipv6) {
2874                rcu_assign_pointer(vxlan->vn6_sock, vs);
2875                node = &vxlan->hlist6;
2876        } else
2877#endif
2878        {
2879                rcu_assign_pointer(vxlan->vn4_sock, vs);
2880                node = &vxlan->hlist4;
2881        }
2882        vxlan_vs_add_dev(vs, vxlan, node);
2883        return 0;
2884}
2885
2886static int vxlan_sock_add(struct vxlan_dev *vxlan)
2887{
2888        bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
2889        bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
2890        bool ipv4 = !ipv6 || metadata;
2891        int ret = 0;
2892
2893        RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
2894#if IS_ENABLED(CONFIG_IPV6)
2895        RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
2896        if (ipv6) {
2897                ret = __vxlan_sock_add(vxlan, true);
2898                if (ret < 0 && ret != -EAFNOSUPPORT)
2899                        ipv4 = false;
2900        }
2901#endif
2902        if (ipv4)
2903                ret = __vxlan_sock_add(vxlan, false);
2904        if (ret < 0)
2905                vxlan_sock_release(vxlan);
2906        return ret;
2907}
2908
2909static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
2910                                 struct net_device **lower,
2911                                 struct vxlan_dev *old)
2912{
2913        struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
2914        struct vxlan_dev *tmp;
2915        bool use_ipv6 = false;
2916
2917        if (conf->flags & VXLAN_F_GPE) {
2918                /* For now, allow GPE only together with
2919                 * COLLECT_METADATA. This can be relaxed later; in such
2920                 * case, the other side of the PtP link will have to be
2921                 * provided.
2922                 */
2923                if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2924                    !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2925                        return -EINVAL;
2926                }
2927        }
2928
2929        if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
2930                /* Unless IPv6 is explicitly requested, assume IPv4 */
2931                conf->remote_ip.sa.sa_family = AF_INET;
2932                conf->saddr.sa.sa_family = AF_INET;
2933        } else if (!conf->remote_ip.sa.sa_family) {
2934                conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
2935        } else if (!conf->saddr.sa.sa_family) {
2936                conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
2937        }
2938
2939        if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family)
2940                return -EINVAL;
2941
2942        if (vxlan_addr_multicast(&conf->saddr))
2943                return -EINVAL;
2944
2945        if (conf->saddr.sa.sa_family == AF_INET6) {
2946                if (!IS_ENABLED(CONFIG_IPV6))
2947                        return -EPFNOSUPPORT;
2948                use_ipv6 = true;
2949                conf->flags |= VXLAN_F_IPV6;
2950
2951                if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2952                        int local_type =
2953                                ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
2954                        int remote_type =
2955                                ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
2956
2957                        if (local_type & IPV6_ADDR_LINKLOCAL) {
2958                                if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
2959                                    (remote_type != IPV6_ADDR_ANY))
2960                                        return -EINVAL;
2961
2962                                conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
2963                        } else {
2964                                if (remote_type ==
2965                                    (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL))
2966                                        return -EINVAL;
2967
2968                                conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
2969                        }
2970                }
2971        }
2972
2973        if (conf->label && !use_ipv6)
2974                return -EINVAL;
2975
2976        if (conf->remote_ifindex) {
2977                struct net_device *lowerdev;
2978
2979                lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
2980                if (!lowerdev)
2981                        return -ENODEV;
2982
2983#if IS_ENABLED(CONFIG_IPV6)
2984                if (use_ipv6) {
2985                        struct inet6_dev *idev = __in6_dev_get(lowerdev);
2986                        if (idev && idev->cnf.disable_ipv6)
2987                                return -EPERM;
2988                }
2989#endif
2990
2991                *lower = lowerdev;
2992        } else {
2993                if (vxlan_addr_multicast(&conf->remote_ip))
2994                        return -EINVAL;
2995
2996#if IS_ENABLED(CONFIG_IPV6)
2997                if (conf->flags & VXLAN_F_IPV6_LINKLOCAL)
2998                        return -EINVAL;
2999#endif
3000
3001                *lower = NULL;
3002        }
3003
3004        if (!conf->dst_port) {
3005                if (conf->flags & VXLAN_F_GPE)
3006                        conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3007                else
3008                        conf->dst_port = htons(vxlan_port);
3009        }
3010
3011        if (!conf->age_interval)
3012                conf->age_interval = FDB_AGE_DEFAULT;
3013
3014        list_for_each_entry(tmp, &vn->vxlan_list, next) {
3015                if (tmp == old)
3016                        continue;
3017
3018                if (tmp->cfg.vni != conf->vni)
3019                        continue;
3020                if (tmp->cfg.dst_port != conf->dst_port)
3021                        continue;
3022                if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3023                    (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3024                        continue;
3025
3026                if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3027                    tmp->cfg.remote_ifindex != conf->remote_ifindex)
3028                        continue;
3029
3030                return -EEXIST;
3031        }
3032
3033        return 0;
3034}
3035
3036static void vxlan_config_apply(struct net_device *dev,
3037                               struct vxlan_config *conf,
3038                               struct net_device *lowerdev,
3039                               struct net *src_net,
3040                               bool changelink)
3041{
3042        struct vxlan_dev *vxlan = netdev_priv(dev);
3043        struct vxlan_rdst *dst = &vxlan->default_dst;
3044        unsigned short needed_headroom = ETH_HLEN;
3045        bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3046        int max_mtu = ETH_MAX_MTU;
3047
3048        if (!changelink) {
3049                if (conf->flags & VXLAN_F_GPE)
3050                        vxlan_raw_setup(dev);
3051                else
3052                        vxlan_ether_setup(dev);
3053
3054                if (conf->mtu)
3055                        dev->mtu = conf->mtu;
3056
3057                vxlan->net = src_net;
3058        }
3059
3060        dst->remote_vni = conf->vni;
3061
3062        memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3063
3064        if (lowerdev) {
3065                dst->remote_ifindex = conf->remote_ifindex;
3066
3067                dev->gso_max_size = lowerdev->gso_max_size;
3068                dev->gso_max_segs = lowerdev->gso_max_segs;
3069
3070                needed_headroom = lowerdev->hard_header_len;
3071
3072                max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3073                                           VXLAN_HEADROOM);
3074                if (max_mtu < ETH_MIN_MTU)
3075                        max_mtu = ETH_MIN_MTU;
3076
3077                if (!changelink && !conf->mtu)
3078                        dev->mtu = max_mtu;
3079        }
3080
3081        if (dev->mtu > max_mtu)
3082                dev->mtu = max_mtu;
3083
3084        if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3085                needed_headroom += VXLAN6_HEADROOM;
3086        else
3087                needed_headroom += VXLAN_HEADROOM;
3088        dev->needed_headroom = needed_headroom;
3089
3090        memcpy(&vxlan->cfg, conf, sizeof(*conf));
3091}
3092
3093static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3094                               struct vxlan_config *conf,
3095                               bool changelink)
3096{
3097        struct vxlan_dev *vxlan = netdev_priv(dev);
3098        struct net_device *lowerdev;
3099        int ret;
3100
3101        ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan);
3102        if (ret)
3103                return ret;
3104
3105        vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3106
3107        return 0;
3108}
3109
3110static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3111                              struct vxlan_config *conf)
3112{
3113        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3114        struct vxlan_dev *vxlan = netdev_priv(dev);
3115        int err;
3116
3117        err = vxlan_dev_configure(net, dev, conf, false);
3118        if (err)
3119                return err;
3120
3121        dev->ethtool_ops = &vxlan_ethtool_ops;
3122
3123        /* create an fdb entry for a valid default destination */
3124        if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3125                err = vxlan_fdb_create(vxlan, all_zeros_mac,
3126                                       &vxlan->default_dst.remote_ip,
3127                                       NUD_REACHABLE | NUD_PERMANENT,
3128                                       NLM_F_EXCL | NLM_F_CREATE,
3129                                       vxlan->cfg.dst_port,
3130                                       vxlan->default_dst.remote_vni,
3131                                       vxlan->default_dst.remote_vni,
3132                                       vxlan->default_dst.remote_ifindex,
3133                                       NTF_SELF);
3134                if (err)
3135                        return err;
3136        }
3137
3138        err = register_netdevice(dev);
3139        if (err) {
3140                vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
3141                return err;
3142        }
3143
3144        list_add(&vxlan->next, &vn->vxlan_list);
3145        return 0;
3146}
3147
3148static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3149                         struct net_device *dev, struct vxlan_config *conf,
3150                         bool changelink)
3151{
3152        struct vxlan_dev *vxlan = netdev_priv(dev);
3153
3154        memset(conf, 0, sizeof(*conf));
3155
3156        /* if changelink operation, start with old existing cfg */
3157        if (changelink)
3158                memcpy(conf, &vxlan->cfg, sizeof(*conf));
3159
3160        if (data[IFLA_VXLAN_ID]) {
3161                __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3162
3163                if (changelink && (vni != conf->vni))
3164                        return -EOPNOTSUPP;
3165                conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3166        }
3167
3168        if (data[IFLA_VXLAN_GROUP]) {
3169                if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
3170                        return -EOPNOTSUPP;
3171
3172                conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
3173                conf->remote_ip.sa.sa_family = AF_INET;
3174        } else if (data[IFLA_VXLAN_GROUP6]) {
3175                if (!IS_ENABLED(CONFIG_IPV6))
3176                        return -EPFNOSUPPORT;
3177
3178                if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
3179                        return -EOPNOTSUPP;
3180
3181                conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3182                conf->remote_ip.sa.sa_family = AF_INET6;
3183        }
3184
3185        if (data[IFLA_VXLAN_LOCAL]) {
3186                if (changelink && (conf->saddr.sa.sa_family != AF_INET))
3187                        return -EOPNOTSUPP;
3188
3189                conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3190                conf->saddr.sa.sa_family = AF_INET;
3191        } else if (data[IFLA_VXLAN_LOCAL6]) {
3192                if (!IS_ENABLED(CONFIG_IPV6))
3193                        return -EPFNOSUPPORT;
3194
3195                if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
3196                        return -EOPNOTSUPP;
3197
3198                /* TODO: respect scope id */
3199                conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3200                conf->saddr.sa.sa_family = AF_INET6;
3201        }
3202
3203        if (data[IFLA_VXLAN_LINK])
3204                conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
3205
3206        if (data[IFLA_VXLAN_TOS])
3207                conf->tos  = nla_get_u8(data[IFLA_VXLAN_TOS]);
3208
3209        if (data[IFLA_VXLAN_TTL])
3210                conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
3211
3212        if (data[IFLA_VXLAN_TTL_INHERIT]) {
3213                if (changelink)
3214                        return -EOPNOTSUPP;
3215                conf->flags |= VXLAN_F_TTL_INHERIT;
3216        }
3217
3218        if (data[IFLA_VXLAN_LABEL])
3219                conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
3220                             IPV6_FLOWLABEL_MASK;
3221
3222        if (data[IFLA_VXLAN_LEARNING]) {
3223                if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
3224                        conf->flags |= VXLAN_F_LEARN;
3225                else
3226                        conf->flags &= ~VXLAN_F_LEARN;
3227        } else if (!changelink) {
3228                /* default to learn on a new device */
3229                conf->flags |= VXLAN_F_LEARN;
3230        }
3231
3232        if (data[IFLA_VXLAN_AGEING]) {
3233                if (changelink)
3234                        return -EOPNOTSUPP;
3235                conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3236        }
3237
3238        if (data[IFLA_VXLAN_PROXY]) {
3239                if (changelink)
3240                        return -EOPNOTSUPP;
3241                if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3242                        conf->flags |= VXLAN_F_PROXY;
3243        }
3244
3245        if (data[IFLA_VXLAN_RSC]) {
3246                if (changelink)
3247                        return -EOPNOTSUPP;
3248                if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3249                        conf->flags |= VXLAN_F_RSC;
3250        }
3251
3252        if (data[IFLA_VXLAN_L2MISS]) {
3253                if (changelink)
3254                        return -EOPNOTSUPP;
3255                if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3256                        conf->flags |= VXLAN_F_L2MISS;
3257        }
3258
3259        if (data[IFLA_VXLAN_L3MISS]) {
3260                if (changelink)
3261                        return -EOPNOTSUPP;
3262                if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3263                        conf->flags |= VXLAN_F_L3MISS;
3264        }
3265
3266        if (data[IFLA_VXLAN_LIMIT]) {
3267                if (changelink)
3268                        return -EOPNOTSUPP;
3269                conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3270        }
3271
3272        if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3273                if (changelink)
3274                        return -EOPNOTSUPP;
3275                if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3276                        conf->flags |= VXLAN_F_COLLECT_METADATA;
3277        }
3278
3279        if (data[IFLA_VXLAN_PORT_RANGE]) {
3280                if (!changelink) {
3281                        const struct ifla_vxlan_port_range *p
3282                                = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3283                        conf->port_min = ntohs(p->low);
3284                        conf->port_max = ntohs(p->high);
3285                } else {
3286                        return -EOPNOTSUPP;
3287                }
3288        }
3289
3290        if (data[IFLA_VXLAN_PORT]) {
3291                if (changelink)
3292                        return -EOPNOTSUPP;
3293                conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3294        }
3295
3296        /* RHEL: preserve the previous default */
3297        if (!changelink)
3298                conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3299        if (data[IFLA_VXLAN_UDP_CSUM]) {
3300                if (changelink)
3301                        return -EOPNOTSUPP;
3302                if (nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3303                        conf->flags &= ~VXLAN_F_UDP_ZERO_CSUM_TX;
3304        }
3305
3306        if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3307                if (changelink)
3308                        return -EOPNOTSUPP;
3309                if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3310                        conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3311        }
3312
3313        if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3314                if (changelink)
3315                        return -EOPNOTSUPP;
3316                if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3317                        conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3318        }
3319
3320        if (data[IFLA_VXLAN_REMCSUM_TX]) {
3321                if (changelink)
3322                        return -EOPNOTSUPP;
3323                if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3324                        conf->flags |= VXLAN_F_REMCSUM_TX;
3325        }
3326
3327        if (data[IFLA_VXLAN_REMCSUM_RX]) {
3328                if (changelink)
3329                        return -EOPNOTSUPP;
3330                if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3331                        conf->flags |= VXLAN_F_REMCSUM_RX;
3332        }
3333
3334        if (data[IFLA_VXLAN_GBP]) {
3335                if (changelink)
3336                        return -EOPNOTSUPP;
3337                conf->flags |= VXLAN_F_GBP;
3338        }
3339
3340        if (data[IFLA_VXLAN_GPE]) {
3341                if (changelink)
3342                        return -EOPNOTSUPP;
3343                conf->flags |= VXLAN_F_GPE;
3344        }
3345
3346        if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3347                if (changelink)
3348                        return -EOPNOTSUPP;
3349                conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3350        }
3351
3352        if (tb[IFLA_MTU]) {
3353                if (changelink)
3354                        return -EOPNOTSUPP;
3355                conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3356        }
3357
3358        return 0;
3359}
3360
3361static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3362                         struct nlattr *tb[], struct nlattr *data[])
3363{
3364        struct vxlan_config conf;
3365        int err;
3366
3367        err = vxlan_nl2conf(tb, data, dev, &conf, false);
3368        if (err)
3369                return err;
3370
3371        return __vxlan_dev_create(src_net, dev, &conf);
3372}
3373
3374static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3375                            struct nlattr *data[])
3376{
3377        struct vxlan_dev *vxlan = netdev_priv(dev);
3378        struct vxlan_rdst *dst = &vxlan->default_dst;
3379        struct vxlan_rdst old_dst;
3380        struct vxlan_config conf;
3381        int err;
3382
3383        err = vxlan_nl2conf(tb, data,
3384                            dev, &conf, true);
3385        if (err)
3386                return err;
3387
3388        memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
3389
3390        err = vxlan_dev_configure(vxlan->net, dev, &conf, true);
3391        if (err)
3392                return err;
3393
3394        /* handle default dst entry */
3395        if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3396                spin_lock_bh(&vxlan->hash_lock);
3397                if (!vxlan_addr_any(&old_dst.remote_ip))
3398                        __vxlan_fdb_delete(vxlan, all_zeros_mac,
3399                                           old_dst.remote_ip,
3400                                           vxlan->cfg.dst_port,
3401                                           old_dst.remote_vni,
3402                                           old_dst.remote_vni,
3403                                           old_dst.remote_ifindex, 0);
3404
3405                if (!vxlan_addr_any(&dst->remote_ip)) {
3406                        err = vxlan_fdb_create(vxlan, all_zeros_mac,
3407                                               &dst->remote_ip,
3408                                               NUD_REACHABLE | NUD_PERMANENT,
3409                                               NLM_F_CREATE | NLM_F_APPEND,
3410                                               vxlan->cfg.dst_port,
3411                                               dst->remote_vni,
3412                                               dst->remote_vni,
3413                                               dst->remote_ifindex,
3414                                               NTF_SELF);
3415                        if (err) {
3416                                spin_unlock_bh(&vxlan->hash_lock);
3417                                return err;
3418                        }
3419                }
3420                spin_unlock_bh(&vxlan->hash_lock);
3421        }
3422
3423        return 0;
3424}
3425
3426static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3427{
3428        struct vxlan_dev *vxlan = netdev_priv(dev);
3429
3430        vxlan_flush(vxlan, true);
3431
3432        list_del(&vxlan->next);
3433        unregister_netdevice_queue(dev, head);
3434}
3435
3436static size_t vxlan_get_size(const struct net_device *dev)
3437{
3438
3439        return nla_total_size(sizeof(__u32)) +  /* IFLA_VXLAN_ID */
3440                nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
3441                nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
3442                nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
3443                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TTL */
3444                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TOS */
3445                nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
3446                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_LEARNING */
3447                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_PROXY */
3448                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_RSC */
3449                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L2MISS */
3450                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L3MISS */
3451                nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_COLLECT_METADATA */
3452                nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3453                nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
3454                nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
3455                nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3456                nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3457                nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3458                nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
3459                nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3460                nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
3461                0;
3462}
3463
3464static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3465{
3466        const struct vxlan_dev *vxlan = netdev_priv(dev);
3467        const struct vxlan_rdst *dst = &vxlan->default_dst;
3468        struct ifla_vxlan_port_range ports = {
3469                .low =  htons(vxlan->cfg.port_min),
3470                .high = htons(vxlan->cfg.port_max),
3471        };
3472
3473        if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
3474                goto nla_put_failure;
3475
3476        if (!vxlan_addr_any(&dst->remote_ip)) {
3477                if (dst->remote_ip.sa.sa_family == AF_INET) {
3478                        if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3479                                            dst->remote_ip.sin.sin_addr.s_addr))
3480                                goto nla_put_failure;
3481#if IS_ENABLED(CONFIG_IPV6)
3482                } else {
3483                        if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3484                                             &dst->remote_ip.sin6.sin6_addr))
3485                                goto nla_put_failure;
3486#endif
3487                }
3488        }
3489
3490        if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
3491                goto nla_put_failure;
3492
3493        if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3494                if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
3495                        if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
3496                                            vxlan->cfg.saddr.sin.sin_addr.s_addr))
3497                                goto nla_put_failure;
3498#if IS_ENABLED(CONFIG_IPV6)
3499                } else {
3500                        if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
3501                                             &vxlan->cfg.saddr.sin6.sin6_addr))
3502                                goto nla_put_failure;
3503#endif
3504                }
3505        }
3506
3507        if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3508            nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
3509            nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
3510            nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3511                        !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
3512            nla_put_u8(skb, IFLA_VXLAN_PROXY,
3513                        !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
3514            nla_put_u8(skb, IFLA_VXLAN_RSC,
3515                       !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
3516            nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3517                        !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
3518            nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3519                        !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
3520            nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3521                       !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
3522            nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3523            nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3524            nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
3525            nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
3526                        !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
3527            nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3528                        !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3529            nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
3530                        !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3531            nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3532                        !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
3533            nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3534                        !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
3535                goto nla_put_failure;
3536
3537        if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3538                goto nla_put_failure;
3539
3540        if (vxlan->cfg.flags & VXLAN_F_GBP &&
3541            nla_put_flag(skb, IFLA_VXLAN_GBP))
3542                goto nla_put_failure;
3543
3544        if (vxlan->cfg.flags & VXLAN_F_GPE &&
3545            nla_put_flag(skb, IFLA_VXLAN_GPE))
3546                goto nla_put_failure;
3547
3548        if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3549            nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3550                goto nla_put_failure;
3551
3552        return 0;
3553
3554nla_put_failure:
3555        return -EMSGSIZE;
3556}
3557
3558static struct net *vxlan_get_link_net(const struct net_device *dev)
3559{
3560        struct vxlan_dev *vxlan = netdev_priv(dev);
3561
3562        return vxlan->net;
3563}
3564
3565static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3566        .kind           = "vxlan",
3567        .maxtype        = IFLA_VXLAN_MAX,
3568        .policy         = vxlan_policy,
3569        .priv_size      = sizeof(struct vxlan_dev),
3570        .setup          = vxlan_setup,
3571        .validate       = vxlan_validate,
3572        .newlink        = vxlan_newlink,
3573        .changelink     = vxlan_changelink,
3574        .dellink        = vxlan_dellink,
3575        .get_size       = vxlan_get_size,
3576        .fill_info      = vxlan_fill_info,
3577        .get_link_net   = vxlan_get_link_net,
3578};
3579
3580struct net_device *vxlan_dev_create(struct net *net, const char *name,
3581                                    struct vxlan_config *conf)
3582{
3583        struct nlattr *tb[IFLA_MAX + 1];
3584        struct net_device *dev;
3585        int err;
3586
3587        memset(&tb, 0, sizeof(tb));
3588
3589        dev = rtnl_create_link(net, name,
3590                               &vxlan_link_ops, tb);
3591        if (IS_ERR(dev))
3592                return dev;
3593
3594        err = __vxlan_dev_create(net, dev, conf);
3595        if (err < 0) {
3596                free_netdev(dev);
3597                return ERR_PTR(err);
3598        }
3599
3600        err = rtnl_configure_link(dev, NULL);
3601        if (err < 0) {
3602                LIST_HEAD(list_kill);
3603
3604                vxlan_dellink(dev, &list_kill);
3605                unregister_netdevice_many(&list_kill);
3606                return ERR_PTR(err);
3607        }
3608
3609        return dev;
3610}
3611EXPORT_SYMBOL_GPL(vxlan_dev_create);
3612
3613static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3614                                             struct net_device *dev)
3615{
3616        struct vxlan_dev *vxlan, *next;
3617        LIST_HEAD(list_kill);
3618
3619        list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3620                struct vxlan_rdst *dst = &vxlan->default_dst;
3621
3622                /* In case we created vxlan device with carrier
3623                 * and we loose the carrier due to module unload
3624                 * we also need to remove vxlan device. In other
3625                 * cases, it's not necessary and remote_ifindex
3626                 * is 0 here, so no matches.
3627                 */
3628                if (dst->remote_ifindex == dev->ifindex)
3629                        vxlan_dellink(vxlan->dev, &list_kill);
3630        }
3631
3632        unregister_netdevice_many(&list_kill);
3633}
3634
3635static int vxlan_netdevice_event(struct notifier_block *unused,
3636                                 unsigned long event, void *ptr)
3637{
3638        struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3639        struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
3640
3641        if (event == NETDEV_UNREGISTER) {
3642                vxlan_offload_rx_ports(dev, false);
3643                vxlan_handle_lowerdev_unregister(vn, dev);
3644        } else if (event == NETDEV_REGISTER) {
3645                vxlan_offload_rx_ports(dev, true);
3646        } else if (event == NETDEV_OFFLOAD_PUSH_VXLAN ||
3647                 event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
3648                 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
3649                vxlan_offload_rx_ports(dev, event != NETDEV_UDP_TUNNEL_DROP_INFO);
3650        }
3651
3652        return NOTIFY_DONE;
3653}
3654
3655static struct notifier_block vxlan_notifier_block __read_mostly = {
3656        .notifier_call = vxlan_netdevice_event,
3657};
3658
3659static __net_init int vxlan_init_net(struct net *net)
3660{
3661        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3662        unsigned int h;
3663
3664        INIT_LIST_HEAD(&vn->vxlan_list);
3665        spin_lock_init(&vn->sock_lock);
3666
3667        for (h = 0; h < PORT_HASH_SIZE; ++h)
3668                INIT_HLIST_HEAD(&vn->sock_list[h]);
3669
3670        return 0;
3671}
3672
3673static void __net_exit vxlan_exit_net(struct net *net)
3674{
3675        struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3676        struct vxlan_dev *vxlan, *next;
3677        struct net_device *dev, *aux;
3678        LIST_HEAD(list);
3679
3680        rtnl_lock();
3681        for_each_netdev_safe(net, dev, aux)
3682                if (dev->rtnl_link_ops == &vxlan_link_ops)
3683                        unregister_netdevice_queue(dev, &list);
3684
3685        list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3686                /* If vxlan->dev is in the same netns, it has already been added
3687                 * to the list by the previous loop.
3688                 */
3689                if (!net_eq(dev_net(vxlan->dev), net))
3690                        unregister_netdevice_queue(vxlan->dev, &list);
3691        }
3692
3693        unregister_netdevice_many(&list);
3694        rtnl_unlock();
3695}
3696
3697static struct pernet_operations vxlan_net_ops = {
3698        .init = vxlan_init_net,
3699        .exit = vxlan_exit_net,
3700        .id   = &vxlan_net_id,
3701        .size = sizeof(struct vxlan_net),
3702};
3703
3704static int __init vxlan_init_module(void)
3705{
3706        int rc;
3707
3708        get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3709
3710        rc = register_pernet_subsys(&vxlan_net_ops);
3711        if (rc)
3712                goto out1;
3713
3714        rc = register_netdevice_notifier_rh(&vxlan_notifier_block);
3715        if (rc)
3716                goto out2;
3717
3718        rc = rtnl_link_register(&vxlan_link_ops);
3719        if (rc)
3720                goto out3;
3721
3722        return 0;
3723out3:
3724        unregister_netdevice_notifier_rh(&vxlan_notifier_block);
3725out2:
3726        unregister_pernet_subsys(&vxlan_net_ops);
3727out1:
3728        return rc;
3729}
3730late_initcall(vxlan_init_module);
3731
3732static void __exit vxlan_cleanup_module(void)
3733{
3734        rtnl_link_unregister(&vxlan_link_ops);
3735        unregister_netdevice_notifier_rh(&vxlan_notifier_block);
3736        unregister_pernet_subsys(&vxlan_net_ops);
3737        /* rcu_barrier() is called by netns */
3738}
3739module_exit(vxlan_cleanup_module);
3740
3741MODULE_LICENSE("GPL");
3742MODULE_VERSION(VXLAN_VERSION);
3743MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3744MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3745MODULE_ALIAS_RTNL_LINK("vxlan");
3746