The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/if_vxlan.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice unmodified, this list of conditions, and the following
   10  *    disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include "opt_inet.h"
   28 #include "opt_inet6.h"
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #include <sys/param.h>
   34 #include <sys/eventhandler.h>
   35 #include <sys/kernel.h>
   36 #include <sys/lock.h>
   37 #include <sys/hash.h>
   38 #include <sys/malloc.h>
   39 #include <sys/mbuf.h>
   40 #include <sys/module.h>
   41 #include <sys/refcount.h>
   42 #include <sys/rmlock.h>
   43 #include <sys/priv.h>
   44 #include <sys/proc.h>
   45 #include <sys/queue.h>
   46 #include <sys/sbuf.h>
   47 #include <sys/socket.h>
   48 #include <sys/socketvar.h>
   49 #include <sys/sockio.h>
   50 #include <sys/sysctl.h>
   51 #include <sys/systm.h>
   52 
   53 #include <net/bpf.h>
   54 #include <net/ethernet.h>
   55 #include <net/if.h>
   56 #include <net/if_var.h>
   57 #include <net/if_clone.h>
   58 #include <net/if_dl.h>
   59 #include <net/if_media.h>
   60 #include <net/if_types.h>
   61 #include <net/if_vxlan.h>
   62 #include <net/netisr.h>
   63 
   64 #include <netinet/in.h>
   65 #include <netinet/in_systm.h>
   66 #include <netinet/in_var.h>
   67 #include <netinet/in_pcb.h>
   68 #include <netinet/ip.h>
   69 #include <netinet/ip6.h>
   70 #include <netinet/ip_var.h>
   71 #include <netinet/udp.h>
   72 #include <netinet/udp_var.h>
   73 
   74 #include <netinet6/ip6_var.h>
   75 #include <netinet6/scope6_var.h>
   76 
   77 struct vxlan_softc;
   78 LIST_HEAD(vxlan_softc_head, vxlan_softc);
   79 
   80 struct vxlan_socket_mc_info {
   81         union vxlan_sockaddr             vxlsomc_saddr;
   82         union vxlan_sockaddr             vxlsomc_gaddr;
   83         int                              vxlsomc_ifidx;
   84         int                              vxlsomc_users;
   85 };
   86 
   87 #define VXLAN_SO_MC_MAX_GROUPS          32
   88 
   89 #define VXLAN_SO_VNI_HASH_SHIFT         6
   90 #define VXLAN_SO_VNI_HASH_SIZE          (1 << VXLAN_SO_VNI_HASH_SHIFT)
   91 #define VXLAN_SO_VNI_HASH(_vni)         ((_vni) % VXLAN_SO_VNI_HASH_SIZE)
   92 
   93 struct vxlan_socket {
   94         struct socket                   *vxlso_sock;
   95         struct rmlock                    vxlso_lock;
   96         u_int                            vxlso_refcnt;
   97         union vxlan_sockaddr             vxlso_laddr;
   98         LIST_ENTRY(vxlan_socket)         vxlso_entry;
   99         struct vxlan_softc_head          vxlso_vni_hash[VXLAN_SO_VNI_HASH_SIZE];
  100         struct vxlan_socket_mc_info      vxlso_mc[VXLAN_SO_MC_MAX_GROUPS];
  101 };
  102 
  103 #define VXLAN_SO_RLOCK(_vso, _p)        rm_rlock(&(_vso)->vxlso_lock, (_p))
  104 #define VXLAN_SO_RUNLOCK(_vso, _p)      rm_runlock(&(_vso)->vxlso_lock, (_p))
  105 #define VXLAN_SO_WLOCK(_vso)            rm_wlock(&(_vso)->vxlso_lock)
  106 #define VXLAN_SO_WUNLOCK(_vso)          rm_wunlock(&(_vso)->vxlso_lock)
  107 #define VXLAN_SO_LOCK_ASSERT(_vso) \
  108     rm_assert(&(_vso)->vxlso_lock, RA_LOCKED)
  109 #define VXLAN_SO_LOCK_WASSERT(_vso) \
  110     rm_assert(&(_vso)->vxlso_lock, RA_WLOCKED)
  111 
  112 #define VXLAN_SO_ACQUIRE(_vso)          refcount_acquire(&(_vso)->vxlso_refcnt)
  113 #define VXLAN_SO_RELEASE(_vso)          refcount_release(&(_vso)->vxlso_refcnt)
  114 
  115 struct vxlan_ftable_entry {
  116         LIST_ENTRY(vxlan_ftable_entry)   vxlfe_hash;
  117         uint16_t                         vxlfe_flags;
  118         uint8_t                          vxlfe_mac[ETHER_ADDR_LEN];
  119         union vxlan_sockaddr             vxlfe_raddr;
  120         time_t                           vxlfe_expire;
  121 };
  122 
  123 #define VXLAN_FE_FLAG_DYNAMIC           0x01
  124 #define VXLAN_FE_FLAG_STATIC            0x02
  125 
  126 #define VXLAN_FE_IS_DYNAMIC(_fe) \
  127     ((_fe)->vxlfe_flags & VXLAN_FE_FLAG_DYNAMIC)
  128 
  129 #define VXLAN_SC_FTABLE_SHIFT           9
  130 #define VXLAN_SC_FTABLE_SIZE            (1 << VXLAN_SC_FTABLE_SHIFT)
  131 #define VXLAN_SC_FTABLE_MASK            (VXLAN_SC_FTABLE_SIZE - 1)
  132 #define VXLAN_SC_FTABLE_HASH(_sc, _mac) \
  133     (vxlan_mac_hash(_sc, _mac) % VXLAN_SC_FTABLE_SIZE)
  134 
  135 LIST_HEAD(vxlan_ftable_head, vxlan_ftable_entry);
  136 
  137 struct vxlan_statistics {
  138         uint32_t        ftable_nospace;
  139         uint32_t        ftable_lock_upgrade_failed;
  140 };
  141 
  142 struct vxlan_softc {
  143         struct ifnet                    *vxl_ifp;
  144         u_int                            vxl_fibnum;
  145         struct vxlan_socket             *vxl_sock;
  146         uint32_t                         vxl_vni;
  147         union vxlan_sockaddr             vxl_src_addr;
  148         union vxlan_sockaddr             vxl_dst_addr;
  149         uint32_t                         vxl_flags;
  150 #define VXLAN_FLAG_INIT         0x0001
  151 #define VXLAN_FLAG_TEARDOWN     0x0002
  152 #define VXLAN_FLAG_LEARN        0x0004
  153 
  154         uint32_t                         vxl_port_hash_key;
  155         uint16_t                         vxl_min_port;
  156         uint16_t                         vxl_max_port;
  157         uint8_t                          vxl_ttl;
  158 
  159         /* Lookup table from MAC address to forwarding entry. */
  160         uint32_t                         vxl_ftable_cnt;
  161         uint32_t                         vxl_ftable_max;
  162         uint32_t                         vxl_ftable_timeout;
  163         uint32_t                         vxl_ftable_hash_key;
  164         struct vxlan_ftable_head        *vxl_ftable;
  165 
  166         /* Derived from vxl_dst_addr. */
  167         struct vxlan_ftable_entry        vxl_default_fe;
  168 
  169         struct ip_moptions              *vxl_im4o;
  170         struct ip6_moptions             *vxl_im6o;
  171 
  172         struct rmlock                    vxl_lock;
  173         volatile u_int                   vxl_refcnt;
  174 
  175         int                              vxl_unit;
  176         int                              vxl_vso_mc_index;
  177         struct vxlan_statistics          vxl_stats;
  178         struct sysctl_oid               *vxl_sysctl_node;
  179         struct sysctl_ctx_list           vxl_sysctl_ctx;
  180         struct callout                   vxl_callout;
  181         struct ether_addr                vxl_hwaddr;
  182         int                              vxl_mc_ifindex;
  183         struct ifnet                    *vxl_mc_ifp;
  184         struct ifmedia                   vxl_media;
  185         char                             vxl_mc_ifname[IFNAMSIZ];
  186         LIST_ENTRY(vxlan_softc)          vxl_entry;
  187         LIST_ENTRY(vxlan_softc)          vxl_ifdetach_list;
  188 };
  189 
  190 #define VXLAN_RLOCK(_sc, _p)    rm_rlock(&(_sc)->vxl_lock, (_p))
  191 #define VXLAN_RUNLOCK(_sc, _p)  rm_runlock(&(_sc)->vxl_lock, (_p))
  192 #define VXLAN_WLOCK(_sc)        rm_wlock(&(_sc)->vxl_lock)
  193 #define VXLAN_WUNLOCK(_sc)      rm_wunlock(&(_sc)->vxl_lock)
  194 #define VXLAN_LOCK_WOWNED(_sc)  rm_wowned(&(_sc)->vxl_lock)
  195 #define VXLAN_LOCK_ASSERT(_sc)  rm_assert(&(_sc)->vxl_lock, RA_LOCKED)
  196 #define VXLAN_LOCK_WASSERT(_sc) rm_assert(&(_sc)->vxl_lock, RA_WLOCKED)
  197 #define VXLAN_UNLOCK(_sc, _p) do {              \
  198     if (VXLAN_LOCK_WOWNED(_sc))                 \
  199         VXLAN_WUNLOCK(_sc);                     \
  200     else                                        \
  201         VXLAN_RUNLOCK(_sc, _p);                 \
  202 } while (0)
  203 
  204 #define VXLAN_ACQUIRE(_sc)      refcount_acquire(&(_sc)->vxl_refcnt)
  205 #define VXLAN_RELEASE(_sc)      refcount_release(&(_sc)->vxl_refcnt)
  206 
  207 #define satoconstsin(sa)        ((const struct sockaddr_in *)(sa))
  208 #define satoconstsin6(sa)       ((const struct sockaddr_in6 *)(sa))
  209 
  210 struct vxlanudphdr {
  211         struct udphdr           vxlh_udp;
  212         struct vxlan_header     vxlh_hdr;
  213 } __packed;
  214 
  215 static int      vxlan_ftable_addr_cmp(const uint8_t *, const uint8_t *);
  216 static void     vxlan_ftable_init(struct vxlan_softc *);
  217 static void     vxlan_ftable_fini(struct vxlan_softc *);
  218 static void     vxlan_ftable_flush(struct vxlan_softc *, int);
  219 static void     vxlan_ftable_expire(struct vxlan_softc *);
  220 static int      vxlan_ftable_update_locked(struct vxlan_softc *,
  221                     const union vxlan_sockaddr *, const uint8_t *,
  222                     struct rm_priotracker *);
  223 static int      vxlan_ftable_learn(struct vxlan_softc *,
  224                     const struct sockaddr *, const uint8_t *);
  225 static int      vxlan_ftable_sysctl_dump(SYSCTL_HANDLER_ARGS);
  226 
  227 static struct vxlan_ftable_entry *
  228                 vxlan_ftable_entry_alloc(void);
  229 static void     vxlan_ftable_entry_free(struct vxlan_ftable_entry *);
  230 static void     vxlan_ftable_entry_init(struct vxlan_softc *,
  231                     struct vxlan_ftable_entry *, const uint8_t *,
  232                     const struct sockaddr *, uint32_t);
  233 static void     vxlan_ftable_entry_destroy(struct vxlan_softc *,
  234                     struct vxlan_ftable_entry *);
  235 static int      vxlan_ftable_entry_insert(struct vxlan_softc *,
  236                     struct vxlan_ftable_entry *);
  237 static struct vxlan_ftable_entry *
  238                 vxlan_ftable_entry_lookup(struct vxlan_softc *,
  239                     const uint8_t *);
  240 static void     vxlan_ftable_entry_dump(struct vxlan_ftable_entry *,
  241                     struct sbuf *);
  242 
  243 static struct vxlan_socket *
  244                 vxlan_socket_alloc(const union vxlan_sockaddr *);
  245 static void     vxlan_socket_destroy(struct vxlan_socket *);
  246 static void     vxlan_socket_release(struct vxlan_socket *);
  247 static struct vxlan_socket *
  248                 vxlan_socket_lookup(union vxlan_sockaddr *vxlsa);
  249 static void     vxlan_socket_insert(struct vxlan_socket *);
  250 static int      vxlan_socket_init(struct vxlan_socket *, struct ifnet *);
  251 static int      vxlan_socket_bind(struct vxlan_socket *, struct ifnet *);
  252 static int      vxlan_socket_create(struct ifnet *, int,
  253                     const union vxlan_sockaddr *, struct vxlan_socket **);
  254 static void     vxlan_socket_ifdetach(struct vxlan_socket *,
  255                     struct ifnet *, struct vxlan_softc_head *);
  256 
  257 static struct vxlan_socket *
  258                 vxlan_socket_mc_lookup(const union vxlan_sockaddr *);
  259 static int      vxlan_sockaddr_mc_info_match(
  260                     const struct vxlan_socket_mc_info *,
  261                     const union vxlan_sockaddr *,
  262                     const union vxlan_sockaddr *, int);
  263 static int      vxlan_socket_mc_join_group(struct vxlan_socket *,
  264                     const union vxlan_sockaddr *, const union vxlan_sockaddr *,
  265                     int *, union vxlan_sockaddr *);
  266 static int      vxlan_socket_mc_leave_group(struct vxlan_socket *,
  267                     const union vxlan_sockaddr *,
  268                     const union vxlan_sockaddr *, int);
  269 static int      vxlan_socket_mc_add_group(struct vxlan_socket *,
  270                     const union vxlan_sockaddr *, const union vxlan_sockaddr *,
  271                     int, int *);
  272 static void     vxlan_socket_mc_release_group_by_idx(struct vxlan_socket *,
  273                     int);
  274 
  275 static struct vxlan_softc *
  276                 vxlan_socket_lookup_softc_locked(struct vxlan_socket *,
  277                     uint32_t);
  278 static struct vxlan_softc *
  279                 vxlan_socket_lookup_softc(struct vxlan_socket *, uint32_t);
  280 static int      vxlan_socket_insert_softc(struct vxlan_socket *,
  281                     struct vxlan_softc *);
  282 static void     vxlan_socket_remove_softc(struct vxlan_socket *,
  283                     struct vxlan_softc *);
  284 
  285 static struct ifnet *
  286                 vxlan_multicast_if_ref(struct vxlan_softc *, int);
  287 static void     vxlan_free_multicast(struct vxlan_softc *);
  288 static int      vxlan_setup_multicast_interface(struct vxlan_softc *);
  289 
  290 static int      vxlan_setup_multicast(struct vxlan_softc *);
  291 static int      vxlan_setup_socket(struct vxlan_softc *);
  292 static void     vxlan_setup_interface(struct vxlan_softc *);
  293 static int      vxlan_valid_init_config(struct vxlan_softc *);
  294 static void     vxlan_init_wait(struct vxlan_softc *);
  295 static void     vxlan_init_complete(struct vxlan_softc *);
  296 static void     vxlan_init(void *);
  297 static void     vxlan_release(struct vxlan_softc *);
  298 static void     vxlan_teardown_wait(struct vxlan_softc *);
  299 static void     vxlan_teardown_complete(struct vxlan_softc *);
  300 static void     vxlan_teardown_locked(struct vxlan_softc *);
  301 static void     vxlan_teardown(struct vxlan_softc *);
  302 static void     vxlan_ifdetach(struct vxlan_softc *, struct ifnet *,
  303                     struct vxlan_softc_head *);
  304 static void     vxlan_timer(void *);
  305 
  306 static int      vxlan_ctrl_get_config(struct vxlan_softc *, void *);
  307 static int      vxlan_ctrl_set_vni(struct vxlan_softc *, void *);
  308 static int      vxlan_ctrl_set_local_addr(struct vxlan_softc *, void *);
  309 static int      vxlan_ctrl_set_remote_addr(struct vxlan_softc *, void *);
  310 static int      vxlan_ctrl_set_local_port(struct vxlan_softc *, void *);
  311 static int      vxlan_ctrl_set_remote_port(struct vxlan_softc *, void *);
  312 static int      vxlan_ctrl_set_port_range(struct vxlan_softc *, void *);
  313 static int      vxlan_ctrl_set_ftable_timeout(struct vxlan_softc *, void *);
  314 static int      vxlan_ctrl_set_ftable_max(struct vxlan_softc *, void *);
  315 static int      vxlan_ctrl_set_multicast_if(struct vxlan_softc * , void *);
  316 static int      vxlan_ctrl_set_ttl(struct vxlan_softc *, void *);
  317 static int      vxlan_ctrl_set_learn(struct vxlan_softc *, void *);
  318 static int      vxlan_ctrl_ftable_entry_add(struct vxlan_softc *, void *);
  319 static int      vxlan_ctrl_ftable_entry_rem(struct vxlan_softc *, void *);
  320 static int      vxlan_ctrl_flush(struct vxlan_softc *, void *);
  321 static int      vxlan_ioctl_drvspec(struct vxlan_softc *,
  322                     struct ifdrv *, int);
  323 static int      vxlan_ioctl_ifflags(struct vxlan_softc *);
  324 static int      vxlan_ioctl(struct ifnet *, u_long, caddr_t);
  325 
  326 #if defined(INET) || defined(INET6)
  327 static uint16_t vxlan_pick_source_port(struct vxlan_softc *, struct mbuf *);
  328 static void     vxlan_encap_header(struct vxlan_softc *, struct mbuf *,
  329                     int, uint16_t, uint16_t);
  330 #endif
  331 static int      vxlan_encap4(struct vxlan_softc *,
  332                     const union vxlan_sockaddr *, struct mbuf *);
  333 static int      vxlan_encap6(struct vxlan_softc *,
  334                     const union vxlan_sockaddr *, struct mbuf *);
  335 static int      vxlan_transmit(struct ifnet *, struct mbuf *);
  336 static void     vxlan_qflush(struct ifnet *);
  337 static void     vxlan_rcv_udp_packet(struct mbuf *, int, struct inpcb *,
  338                     const struct sockaddr *, void *);
  339 static int      vxlan_input(struct vxlan_socket *, uint32_t, struct mbuf **,
  340                     const struct sockaddr *);
  341 
  342 static void     vxlan_set_default_config(struct vxlan_softc *);
  343 static int      vxlan_set_user_config(struct vxlan_softc *,
  344                      struct ifvxlanparam *);
  345 static int      vxlan_clone_create(struct if_clone *, int, caddr_t);
  346 static void     vxlan_clone_destroy(struct ifnet *);
  347 
  348 static uint32_t vxlan_mac_hash(struct vxlan_softc *, const uint8_t *);
  349 static int      vxlan_media_change(struct ifnet *);
  350 static void     vxlan_media_status(struct ifnet *, struct ifmediareq *);
  351 
  352 static int      vxlan_sockaddr_cmp(const union vxlan_sockaddr *,
  353                     const struct sockaddr *);
  354 static void     vxlan_sockaddr_copy(union vxlan_sockaddr *,
  355                     const struct sockaddr *);
  356 static int      vxlan_sockaddr_in_equal(const union vxlan_sockaddr *,
  357                     const struct sockaddr *);
  358 static void     vxlan_sockaddr_in_copy(union vxlan_sockaddr *,
  359                     const struct sockaddr *);
  360 static int      vxlan_sockaddr_supported(const union vxlan_sockaddr *, int);
  361 static int      vxlan_sockaddr_in_any(const union vxlan_sockaddr *);
  362 static int      vxlan_sockaddr_in_multicast(const union vxlan_sockaddr *);
  363 static int      vxlan_sockaddr_in6_embedscope(union vxlan_sockaddr *);
  364 
  365 static int      vxlan_can_change_config(struct vxlan_softc *);
  366 static int      vxlan_check_vni(uint32_t);
  367 static int      vxlan_check_ttl(int);
  368 static int      vxlan_check_ftable_timeout(uint32_t);
  369 static int      vxlan_check_ftable_max(uint32_t);
  370 
  371 static void     vxlan_sysctl_setup(struct vxlan_softc *);
  372 static void     vxlan_sysctl_destroy(struct vxlan_softc *);
  373 static int      vxlan_tunable_int(struct vxlan_softc *, const char *, int);
  374 
  375 static void     vxlan_ifdetach_event(void *, struct ifnet *);
  376 static void     vxlan_load(void);
  377 static void     vxlan_unload(void);
  378 static int      vxlan_modevent(module_t, int, void *);
  379 
  380 static const char vxlan_name[] = "vxlan";
  381 static MALLOC_DEFINE(M_VXLAN, vxlan_name,
  382     "Virtual eXtensible LAN Interface");
  383 static struct if_clone *vxlan_cloner;
  384 
  385 static struct mtx vxlan_list_mtx;
  386 #define VXLAN_LIST_LOCK()       mtx_lock(&vxlan_list_mtx)
  387 #define VXLAN_LIST_UNLOCK()     mtx_unlock(&vxlan_list_mtx)
  388 
  389 static LIST_HEAD(, vxlan_socket) vxlan_socket_list;
  390 
  391 static eventhandler_tag vxlan_ifdetach_event_tag;
  392 
  393 SYSCTL_DECL(_net_link);
  394 SYSCTL_NODE(_net_link, OID_AUTO, vxlan, CTLFLAG_RW, 0,
  395     "Virtual eXtensible Local Area Network");
  396 
  397 static int vxlan_legacy_port = 0;
  398 TUNABLE_INT("net.link.vxlan.legacy_port", &vxlan_legacy_port);
  399 static int vxlan_reuse_port = 0;
  400 TUNABLE_INT("net.link.vxlan.reuse_port", &vxlan_reuse_port);
  401 
  402 /* Default maximum number of addresses in the forwarding table. */
  403 #ifndef VXLAN_FTABLE_MAX
  404 #define VXLAN_FTABLE_MAX        2000
  405 #endif
  406 
  407 /* Timeout (in seconds) of addresses learned in the forwarding table. */
  408 #ifndef VXLAN_FTABLE_TIMEOUT
  409 #define VXLAN_FTABLE_TIMEOUT    (20 * 60)
  410 #endif
  411 
  412 /*
  413  * Maximum timeout (in seconds) of addresses learned in the forwarding
  414  * table.
  415  */
  416 #ifndef VXLAN_FTABLE_MAX_TIMEOUT
  417 #define VXLAN_FTABLE_MAX_TIMEOUT        (60 * 60 * 24)
  418 #endif
  419 
  420 /* Number of seconds between pruning attempts of the forwarding table. */
  421 #ifndef VXLAN_FTABLE_PRUNE
  422 #define VXLAN_FTABLE_PRUNE      (5 * 60)
  423 #endif
  424 
  425 static int vxlan_ftable_prune_period = VXLAN_FTABLE_PRUNE;
  426 
  427 struct vxlan_control {
  428         int     (*vxlc_func)(struct vxlan_softc *, void *);
  429         int     vxlc_argsize;
  430         int     vxlc_flags;
  431 #define VXLAN_CTRL_FLAG_COPYIN  0x01
  432 #define VXLAN_CTRL_FLAG_COPYOUT 0x02
  433 #define VXLAN_CTRL_FLAG_SUSER   0x04
  434 };
  435 
  436 static const struct vxlan_control vxlan_control_table[] = {
  437         [VXLAN_CMD_GET_CONFIG] =
  438             {   vxlan_ctrl_get_config, sizeof(struct ifvxlancfg),
  439                 VXLAN_CTRL_FLAG_COPYOUT
  440             },
  441 
  442         [VXLAN_CMD_SET_VNI] =
  443             {   vxlan_ctrl_set_vni, sizeof(struct ifvxlancmd),
  444                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  445             },
  446 
  447         [VXLAN_CMD_SET_LOCAL_ADDR] =
  448             {   vxlan_ctrl_set_local_addr, sizeof(struct ifvxlancmd),
  449                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  450             },
  451 
  452         [VXLAN_CMD_SET_REMOTE_ADDR] =
  453             {   vxlan_ctrl_set_remote_addr, sizeof(struct ifvxlancmd),
  454                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  455             },
  456 
  457         [VXLAN_CMD_SET_LOCAL_PORT] =
  458             {   vxlan_ctrl_set_local_port, sizeof(struct ifvxlancmd),
  459                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  460             },
  461 
  462         [VXLAN_CMD_SET_REMOTE_PORT] =
  463             {   vxlan_ctrl_set_remote_port, sizeof(struct ifvxlancmd),
  464                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  465             },
  466 
  467         [VXLAN_CMD_SET_PORT_RANGE] =
  468             {   vxlan_ctrl_set_port_range, sizeof(struct ifvxlancmd),
  469                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  470             },
  471 
  472         [VXLAN_CMD_SET_FTABLE_TIMEOUT] =
  473             {   vxlan_ctrl_set_ftable_timeout, sizeof(struct ifvxlancmd),
  474                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  475             },
  476 
  477         [VXLAN_CMD_SET_FTABLE_MAX] =
  478             {   vxlan_ctrl_set_ftable_max, sizeof(struct ifvxlancmd),
  479                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  480             },
  481 
  482         [VXLAN_CMD_SET_MULTICAST_IF] =
  483             {   vxlan_ctrl_set_multicast_if, sizeof(struct ifvxlancmd),
  484                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  485             },
  486 
  487         [VXLAN_CMD_SET_TTL] =
  488             {   vxlan_ctrl_set_ttl, sizeof(struct ifvxlancmd),
  489                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  490             },
  491 
  492         [VXLAN_CMD_SET_LEARN] =
  493             {   vxlan_ctrl_set_learn, sizeof(struct ifvxlancmd),
  494                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  495             },
  496 
  497         [VXLAN_CMD_FTABLE_ENTRY_ADD] =
  498             {   vxlan_ctrl_ftable_entry_add, sizeof(struct ifvxlancmd),
  499                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  500             },
  501 
  502         [VXLAN_CMD_FTABLE_ENTRY_REM] =
  503             {   vxlan_ctrl_ftable_entry_rem, sizeof(struct ifvxlancmd),
  504                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  505             },
  506 
  507         [VXLAN_CMD_FLUSH] =
  508             {   vxlan_ctrl_flush, sizeof(struct ifvxlancmd),
  509                 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
  510             },
  511 };
  512 
  513 static const int vxlan_control_table_size = nitems(vxlan_control_table);
  514 
  515 static int
  516 vxlan_ftable_addr_cmp(const uint8_t *a, const uint8_t *b)
  517 {
  518         int i, d;
  519 
  520         for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++)
  521                 d = ((int)a[i]) - ((int)b[i]);
  522 
  523         return (d);
  524 }
  525 
  526 static void
  527 vxlan_ftable_init(struct vxlan_softc *sc)
  528 {
  529         int i;
  530 
  531         sc->vxl_ftable = malloc(sizeof(struct vxlan_ftable_head) *
  532             VXLAN_SC_FTABLE_SIZE, M_VXLAN, M_ZERO | M_WAITOK);
  533 
  534         for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++)
  535                 LIST_INIT(&sc->vxl_ftable[i]);
  536         sc->vxl_ftable_hash_key = arc4random();
  537 }
  538 
  539 static void
  540 vxlan_ftable_fini(struct vxlan_softc *sc)
  541 {
  542         int i;
  543 
  544         for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
  545                 KASSERT(LIST_EMPTY(&sc->vxl_ftable[i]),
  546                     ("%s: vxlan %p ftable[%d] not empty", __func__, sc, i));
  547         }
  548         MPASS(sc->vxl_ftable_cnt == 0);
  549 
  550         free(sc->vxl_ftable, M_VXLAN);
  551         sc->vxl_ftable = NULL;
  552 }
  553 
  554 static void
  555 vxlan_ftable_flush(struct vxlan_softc *sc, int all)
  556 {
  557         struct vxlan_ftable_entry *fe, *tfe;
  558         int i;
  559 
  560         for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
  561                 LIST_FOREACH_SAFE(fe, &sc->vxl_ftable[i], vxlfe_hash, tfe) {
  562                         if (all || VXLAN_FE_IS_DYNAMIC(fe))
  563                                 vxlan_ftable_entry_destroy(sc, fe);
  564                 }
  565         }
  566 }
  567 
  568 static void
  569 vxlan_ftable_expire(struct vxlan_softc *sc)
  570 {
  571         struct vxlan_ftable_entry *fe, *tfe;
  572         int i;
  573 
  574         VXLAN_LOCK_WASSERT(sc);
  575 
  576         for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
  577                 LIST_FOREACH_SAFE(fe, &sc->vxl_ftable[i], vxlfe_hash, tfe) {
  578                         if (VXLAN_FE_IS_DYNAMIC(fe) &&
  579                             time_uptime >= fe->vxlfe_expire)
  580                                 vxlan_ftable_entry_destroy(sc, fe);
  581                 }
  582         }
  583 }
  584 
  585 static int
  586 vxlan_ftable_update_locked(struct vxlan_softc *sc,
  587     const union vxlan_sockaddr *vxlsa, const uint8_t *mac,
  588     struct rm_priotracker *tracker)
  589 {
  590         struct vxlan_ftable_entry *fe;
  591         int error __unused;
  592 
  593         VXLAN_LOCK_ASSERT(sc);
  594 
  595 again:
  596         /*
  597          * A forwarding entry for this MAC address might already exist. If
  598          * so, update it, otherwise create a new one. We may have to upgrade
  599          * the lock if we have to change or create an entry.
  600          */
  601         fe = vxlan_ftable_entry_lookup(sc, mac);
  602         if (fe != NULL) {
  603                 fe->vxlfe_expire = time_uptime + sc->vxl_ftable_timeout;
  604 
  605                 if (!VXLAN_FE_IS_DYNAMIC(fe) ||
  606                     vxlan_sockaddr_in_equal(&fe->vxlfe_raddr, &vxlsa->sa))
  607                         return (0);
  608                 if (!VXLAN_LOCK_WOWNED(sc)) {
  609                         VXLAN_RUNLOCK(sc, tracker);
  610                         VXLAN_WLOCK(sc);
  611                         sc->vxl_stats.ftable_lock_upgrade_failed++;
  612                         goto again;
  613                 }
  614                 vxlan_sockaddr_in_copy(&fe->vxlfe_raddr, &vxlsa->sa);
  615                 return (0);
  616         }
  617 
  618         if (!VXLAN_LOCK_WOWNED(sc)) {
  619                 VXLAN_RUNLOCK(sc, tracker);
  620                 VXLAN_WLOCK(sc);
  621                 sc->vxl_stats.ftable_lock_upgrade_failed++;
  622                 goto again;
  623         }
  624 
  625         if (sc->vxl_ftable_cnt >= sc->vxl_ftable_max) {
  626                 sc->vxl_stats.ftable_nospace++;
  627                 return (ENOSPC);
  628         }
  629 
  630         fe = vxlan_ftable_entry_alloc();
  631         if (fe == NULL)
  632                 return (ENOMEM);
  633 
  634         vxlan_ftable_entry_init(sc, fe, mac, &vxlsa->sa, VXLAN_FE_FLAG_DYNAMIC);
  635 
  636         /* The prior lookup failed, so the insert should not. */
  637         error = vxlan_ftable_entry_insert(sc, fe);
  638         MPASS(error == 0);
  639 
  640         return (0);
  641 }
  642 
  643 static int
  644 vxlan_ftable_learn(struct vxlan_softc *sc, const struct sockaddr *sa,
  645     const uint8_t *mac)
  646 {
  647         struct rm_priotracker tracker;
  648         union vxlan_sockaddr vxlsa;
  649         int error;
  650 
  651         /*
  652          * The source port may be randomly selected by the remote host, so
  653          * use the port of the default destination address.
  654          */
  655         vxlan_sockaddr_copy(&vxlsa, sa);
  656         vxlsa.in4.sin_port = sc->vxl_dst_addr.in4.sin_port;
  657 
  658         if (VXLAN_SOCKADDR_IS_IPV6(&vxlsa)) {
  659                 error = vxlan_sockaddr_in6_embedscope(&vxlsa);
  660                 if (error)
  661                         return (error);
  662         }
  663 
  664         VXLAN_RLOCK(sc, &tracker);
  665         error = vxlan_ftable_update_locked(sc, &vxlsa, mac, &tracker);
  666         VXLAN_UNLOCK(sc, &tracker);
  667 
  668         return (error);
  669 }
  670 
  671 static int
  672 vxlan_ftable_sysctl_dump(SYSCTL_HANDLER_ARGS)
  673 {
  674         struct rm_priotracker tracker;
  675         struct sbuf sb;
  676         struct vxlan_softc *sc;
  677         struct vxlan_ftable_entry *fe;
  678         size_t size;
  679         int i, error;
  680 
  681         /*
  682          * This is mostly intended for debugging during development. It is
  683          * not practical to dump an entire large table this way.
  684          */
  685 
  686         sc = arg1;
  687         size = PAGE_SIZE;       /* Calculate later. */
  688 
  689         sbuf_new(&sb, NULL, size, SBUF_FIXEDLEN);
  690         sbuf_putc(&sb, '\n');
  691 
  692         VXLAN_RLOCK(sc, &tracker);
  693         for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
  694                 LIST_FOREACH(fe, &sc->vxl_ftable[i], vxlfe_hash) {
  695                         if (sbuf_error(&sb) != 0)
  696                                 break;
  697                         vxlan_ftable_entry_dump(fe, &sb);
  698                 }
  699         }
  700         VXLAN_RUNLOCK(sc, &tracker);
  701 
  702         if (sbuf_len(&sb) == 1)
  703                 sbuf_setpos(&sb, 0);
  704 
  705         sbuf_finish(&sb);
  706         error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
  707         sbuf_delete(&sb);
  708 
  709         return (error);
  710 }
  711 
  712 static struct vxlan_ftable_entry *
  713 vxlan_ftable_entry_alloc(void)
  714 {
  715         struct vxlan_ftable_entry *fe;
  716 
  717         fe = malloc(sizeof(*fe), M_VXLAN, M_ZERO | M_NOWAIT);
  718 
  719         return (fe);
  720 }
  721 
  722 static void
  723 vxlan_ftable_entry_free(struct vxlan_ftable_entry *fe)
  724 {
  725 
  726         free(fe, M_VXLAN);
  727 }
  728 
  729 static void
  730 vxlan_ftable_entry_init(struct vxlan_softc *sc, struct vxlan_ftable_entry *fe,
  731     const uint8_t *mac, const struct sockaddr *sa, uint32_t flags)
  732 {
  733 
  734         fe->vxlfe_flags = flags;
  735         fe->vxlfe_expire = time_uptime + sc->vxl_ftable_timeout;
  736         memcpy(fe->vxlfe_mac, mac, ETHER_ADDR_LEN);
  737         vxlan_sockaddr_copy(&fe->vxlfe_raddr, sa);
  738 }
  739 
  740 static void
  741 vxlan_ftable_entry_destroy(struct vxlan_softc *sc,
  742     struct vxlan_ftable_entry *fe)
  743 {
  744 
  745         sc->vxl_ftable_cnt--;
  746         LIST_REMOVE(fe, vxlfe_hash);
  747         vxlan_ftable_entry_free(fe);
  748 }
  749 
  750 static int
  751 vxlan_ftable_entry_insert(struct vxlan_softc *sc,
  752     struct vxlan_ftable_entry *fe)
  753 {
  754         struct vxlan_ftable_entry *lfe;
  755         uint32_t hash;
  756         int dir;
  757 
  758         VXLAN_LOCK_WASSERT(sc);
  759         hash = VXLAN_SC_FTABLE_HASH(sc, fe->vxlfe_mac);
  760 
  761         lfe = LIST_FIRST(&sc->vxl_ftable[hash]);
  762         if (lfe == NULL) {
  763                 LIST_INSERT_HEAD(&sc->vxl_ftable[hash], fe, vxlfe_hash);
  764                 goto out;
  765         }
  766 
  767         do {
  768                 dir = vxlan_ftable_addr_cmp(fe->vxlfe_mac, lfe->vxlfe_mac);
  769                 if (dir == 0)
  770                         return (EEXIST);
  771                 if (dir > 0) {
  772                         LIST_INSERT_BEFORE(lfe, fe, vxlfe_hash);
  773                         goto out;
  774                 } else if (LIST_NEXT(lfe, vxlfe_hash) == NULL) {
  775                         LIST_INSERT_AFTER(lfe, fe, vxlfe_hash);
  776                         goto out;
  777                 } else
  778                         lfe = LIST_NEXT(lfe, vxlfe_hash);
  779         } while (lfe != NULL);
  780 
  781 out:
  782         sc->vxl_ftable_cnt++;
  783 
  784         return (0);
  785 }
  786 
  787 static struct vxlan_ftable_entry *
  788 vxlan_ftable_entry_lookup(struct vxlan_softc *sc, const uint8_t *mac)
  789 {
  790         struct vxlan_ftable_entry *fe;
  791         uint32_t hash;
  792         int dir;
  793 
  794         VXLAN_LOCK_ASSERT(sc);
  795         hash = VXLAN_SC_FTABLE_HASH(sc, mac);
  796 
  797         LIST_FOREACH(fe, &sc->vxl_ftable[hash], vxlfe_hash) {
  798                 dir = vxlan_ftable_addr_cmp(mac, fe->vxlfe_mac);
  799                 if (dir == 0)
  800                         return (fe);
  801                 if (dir > 0)
  802                         break;
  803         }
  804 
  805         return (NULL);
  806 }
  807 
  808 static void
  809 vxlan_ftable_entry_dump(struct vxlan_ftable_entry *fe, struct sbuf *sb)
  810 {
  811         char buf[64];
  812         const union vxlan_sockaddr *sa;
  813         const void *addr;
  814         int i, len, af, width;
  815 
  816         sa = &fe->vxlfe_raddr;
  817         af = sa->sa.sa_family;
  818         len = sbuf_len(sb);
  819 
  820         sbuf_printf(sb, "%c 0x%02X ", VXLAN_FE_IS_DYNAMIC(fe) ? 'D' : 'S',
  821             fe->vxlfe_flags);
  822 
  823         for (i = 0; i < ETHER_ADDR_LEN - 1; i++)
  824                 sbuf_printf(sb, "%02X:", fe->vxlfe_mac[i]);
  825         sbuf_printf(sb, "%02X ", fe->vxlfe_mac[i]);
  826 
  827         if (af == AF_INET) {
  828                 addr = &sa->in4.sin_addr;
  829                 width = INET_ADDRSTRLEN - 1;
  830         } else {
  831                 addr = &sa->in6.sin6_addr;
  832                 width = INET6_ADDRSTRLEN - 1;
  833         }
  834         inet_ntop(af, addr, buf, sizeof(buf));
  835         sbuf_printf(sb, "%*s ", width, buf);
  836 
  837         sbuf_printf(sb, "%08jd", (intmax_t)fe->vxlfe_expire);
  838 
  839         sbuf_putc(sb, '\n');
  840 
  841         /* Truncate a partial line. */
  842         if (sbuf_error(sb) != 0)
  843                 sbuf_setpos(sb, len);
  844 }
  845 
  846 static struct vxlan_socket *
  847 vxlan_socket_alloc(const union vxlan_sockaddr *sa)
  848 {
  849         struct vxlan_socket *vso;
  850         int i;
  851 
  852         vso = malloc(sizeof(*vso), M_VXLAN, M_WAITOK | M_ZERO);
  853         rm_init(&vso->vxlso_lock, "vxlansorm");
  854         refcount_init(&vso->vxlso_refcnt, 0);
  855         for (i = 0; i < VXLAN_SO_VNI_HASH_SIZE; i++)
  856                 LIST_INIT(&vso->vxlso_vni_hash[i]);
  857         vso->vxlso_laddr = *sa;
  858 
  859         return (vso);
  860 }
  861 
  862 static void
  863 vxlan_socket_destroy(struct vxlan_socket *vso)
  864 {
  865         struct socket *so;
  866 #ifdef INVARIANTS
  867         int i;
  868         struct vxlan_socket_mc_info *mc;
  869 
  870         for (i = 0; i < VXLAN_SO_MC_MAX_GROUPS; i++) {
  871                 mc = &vso->vxlso_mc[i];
  872                 KASSERT(mc->vxlsomc_gaddr.sa.sa_family == AF_UNSPEC,
  873                     ("%s: socket %p mc[%d] still has address",
  874                      __func__, vso, i));
  875         }
  876 
  877         for (i = 0; i < VXLAN_SO_VNI_HASH_SIZE; i++) {
  878                 KASSERT(LIST_EMPTY(&vso->vxlso_vni_hash[i]),
  879                     ("%s: socket %p vni_hash[%d] not empty",
  880                      __func__, vso, i));
  881         }
  882 #endif
  883         so = vso->vxlso_sock;
  884         if (so != NULL) {
  885                 vso->vxlso_sock = NULL;
  886                 soclose(so);
  887         }
  888 
  889         rm_destroy(&vso->vxlso_lock);
  890         free(vso, M_VXLAN);
  891 }
  892 
  893 static void
  894 vxlan_socket_release(struct vxlan_socket *vso)
  895 {
  896         int destroy;
  897 
  898         VXLAN_LIST_LOCK();
  899         destroy = VXLAN_SO_RELEASE(vso);
  900         if (destroy != 0)
  901                 LIST_REMOVE(vso, vxlso_entry);
  902         VXLAN_LIST_UNLOCK();
  903 
  904         if (destroy != 0)
  905                 vxlan_socket_destroy(vso);
  906 }
  907 
  908 static struct vxlan_socket *
  909 vxlan_socket_lookup(union vxlan_sockaddr *vxlsa)
  910 {
  911         struct vxlan_socket *vso;
  912 
  913         VXLAN_LIST_LOCK();
  914         LIST_FOREACH(vso, &vxlan_socket_list, vxlso_entry) {
  915                 if (vxlan_sockaddr_cmp(&vso->vxlso_laddr, &vxlsa->sa) == 0) {
  916                         VXLAN_SO_ACQUIRE(vso);
  917                         break;
  918                 }
  919         }
  920         VXLAN_LIST_UNLOCK();
  921 
  922         return (vso);
  923 }
  924 
  925 static void
  926 vxlan_socket_insert(struct vxlan_socket *vso)
  927 {
  928 
  929         VXLAN_LIST_LOCK();
  930         VXLAN_SO_ACQUIRE(vso);
  931         LIST_INSERT_HEAD(&vxlan_socket_list, vso, vxlso_entry);
  932         VXLAN_LIST_UNLOCK();
  933 }
  934 
  935 static int
  936 vxlan_socket_init(struct vxlan_socket *vso, struct ifnet *ifp)
  937 {
  938         struct thread *td;
  939         int error;
  940 
  941         td = curthread;
  942 
  943         error = socreate(vso->vxlso_laddr.sa.sa_family, &vso->vxlso_sock,
  944             SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td);
  945         if (error) {
  946                 if_printf(ifp, "cannot create socket: %d\n", error);
  947                 return (error);
  948         }
  949 
  950         error = udp_set_kernel_tunneling(vso->vxlso_sock,
  951             vxlan_rcv_udp_packet, NULL, vso);
  952         if (error) {
  953                 if_printf(ifp, "cannot set tunneling function: %d\n", error);
  954                 return (error);
  955         }
  956 
  957         if (vxlan_reuse_port != 0) {
  958                 struct sockopt sopt;
  959                 int val = 1;
  960 
  961                 bzero(&sopt, sizeof(sopt));
  962                 sopt.sopt_dir = SOPT_SET;
  963                 sopt.sopt_level = IPPROTO_IP;
  964                 sopt.sopt_name = SO_REUSEPORT;
  965                 sopt.sopt_val = &val;
  966                 sopt.sopt_valsize = sizeof(val);
  967                 error = sosetopt(vso->vxlso_sock, &sopt);
  968                 if (error) {
  969                         if_printf(ifp,
  970                             "cannot set REUSEADDR socket opt: %d\n", error);
  971                         return (error);
  972                 }
  973         }
  974 
  975         return (0);
  976 }
  977 
  978 static int
  979 vxlan_socket_bind(struct vxlan_socket *vso, struct ifnet *ifp)
  980 {
  981         union vxlan_sockaddr laddr;
  982         struct thread *td;
  983         int error;
  984 
  985         td = curthread;
  986         laddr = vso->vxlso_laddr;
  987 
  988         error = sobind(vso->vxlso_sock, &laddr.sa, td);
  989         if (error) {
  990                 if (error != EADDRINUSE)
  991                         if_printf(ifp, "cannot bind socket: %d\n", error);
  992                 return (error);
  993         }
  994 
  995         return (0);
  996 }
  997 
  998 static int
  999 vxlan_socket_create(struct ifnet *ifp, int multicast,
 1000     const union vxlan_sockaddr *saddr, struct vxlan_socket **vsop)
 1001 {
 1002         union vxlan_sockaddr laddr;
 1003         struct vxlan_socket *vso;
 1004         int error;
 1005 
 1006         laddr = *saddr;
 1007 
 1008         /*
 1009          * If this socket will be multicast, then only the local port
 1010          * must be specified when binding.
 1011          */
 1012         if (multicast != 0) {
 1013                 if (VXLAN_SOCKADDR_IS_IPV4(&laddr))
 1014                         laddr.in4.sin_addr.s_addr = INADDR_ANY;
 1015 #ifdef INET6
 1016                 else
 1017                         laddr.in6.sin6_addr = in6addr_any;
 1018 #endif
 1019         }
 1020 
 1021         vso = vxlan_socket_alloc(&laddr);
 1022         if (vso == NULL)
 1023                 return (ENOMEM);
 1024 
 1025         error = vxlan_socket_init(vso, ifp);
 1026         if (error)
 1027                 goto fail;
 1028 
 1029         error = vxlan_socket_bind(vso, ifp);
 1030         if (error)
 1031                 goto fail;
 1032 
 1033         /*
 1034          * There is a small window between the bind completing and
 1035          * inserting the socket, so that a concurrent create may fail.
 1036          * Let's not worry about that for now.
 1037          */
 1038         vxlan_socket_insert(vso);
 1039         *vsop = vso;
 1040 
 1041         return (0);
 1042 
 1043 fail:
 1044         vxlan_socket_destroy(vso);
 1045 
 1046         return (error);
 1047 }
 1048 
 1049 static void
 1050 vxlan_socket_ifdetach(struct vxlan_socket *vso, struct ifnet *ifp,
 1051     struct vxlan_softc_head *list)
 1052 {
 1053         struct rm_priotracker tracker;
 1054         struct vxlan_softc *sc;
 1055         int i;
 1056 
 1057         VXLAN_SO_RLOCK(vso, &tracker);
 1058         for (i = 0; i < VXLAN_SO_VNI_HASH_SIZE; i++) {
 1059                 LIST_FOREACH(sc, &vso->vxlso_vni_hash[i], vxl_entry)
 1060                         vxlan_ifdetach(sc, ifp, list);
 1061         }
 1062         VXLAN_SO_RUNLOCK(vso, &tracker);
 1063 }
 1064 
 1065 static struct vxlan_socket *
 1066 vxlan_socket_mc_lookup(const union vxlan_sockaddr *vxlsa)
 1067 {
 1068         union vxlan_sockaddr laddr;
 1069         struct vxlan_socket *vso;
 1070 
 1071         laddr = *vxlsa;
 1072 
 1073         if (VXLAN_SOCKADDR_IS_IPV4(&laddr))
 1074                 laddr.in4.sin_addr.s_addr = INADDR_ANY;
 1075 #ifdef INET6
 1076         else
 1077                 laddr.in6.sin6_addr = in6addr_any;
 1078 #endif
 1079 
 1080         vso = vxlan_socket_lookup(&laddr);
 1081 
 1082         return (vso);
 1083 }
 1084 
 1085 static int
 1086 vxlan_sockaddr_mc_info_match(const struct vxlan_socket_mc_info *mc,
 1087     const union vxlan_sockaddr *group, const union vxlan_sockaddr *local,
 1088     int ifidx)
 1089 {
 1090 
 1091         if (!vxlan_sockaddr_in_any(local) &&
 1092             !vxlan_sockaddr_in_equal(&mc->vxlsomc_saddr, &local->sa))
 1093                 return (0);
 1094         if (!vxlan_sockaddr_in_equal(&mc->vxlsomc_gaddr, &group->sa))
 1095                 return (0);
 1096         if (ifidx != 0 && ifidx != mc->vxlsomc_ifidx)
 1097                 return (0);
 1098 
 1099         return (1);
 1100 }
 1101 
 1102 static int
 1103 vxlan_socket_mc_join_group(struct vxlan_socket *vso,
 1104     const union vxlan_sockaddr *group, const union vxlan_sockaddr *local,
 1105     int *ifidx, union vxlan_sockaddr *source)
 1106 {
 1107         struct sockopt sopt;
 1108         int error;
 1109 
 1110         *source = *local;
 1111 
 1112         if (VXLAN_SOCKADDR_IS_IPV4(group)) {
 1113                 struct ip_mreq mreq;
 1114 
 1115                 mreq.imr_multiaddr = group->in4.sin_addr;
 1116                 mreq.imr_interface = local->in4.sin_addr;
 1117 
 1118                 bzero(&sopt, sizeof(sopt));
 1119                 sopt.sopt_dir = SOPT_SET;
 1120                 sopt.sopt_level = IPPROTO_IP;
 1121                 sopt.sopt_name = IP_ADD_MEMBERSHIP;
 1122                 sopt.sopt_val = &mreq;
 1123                 sopt.sopt_valsize = sizeof(mreq);
 1124                 error = sosetopt(vso->vxlso_sock, &sopt);
 1125                 if (error)
 1126                         return (error);
 1127 
 1128                 /*
 1129                  * BMV: Ideally, there would be a formal way for us to get
 1130                  * the local interface that was selected based on the
 1131                  * imr_interface address. We could then update *ifidx so
 1132                  * vxlan_sockaddr_mc_info_match() would return a match for
 1133                  * later creates that explicitly set the multicast interface.
 1134                  *
 1135                  * If we really need to, we can of course look in the INP's
 1136                  * membership list:
 1137                  *     sotoinpcb(vso->vxlso_sock)->inp_moptions->
 1138                  *         imo_head[]->imf_inm->inm_ifp
 1139                  * similarly to imo_match_group().
 1140                  */
 1141                 source->in4.sin_addr = local->in4.sin_addr;
 1142 
 1143         } else if (VXLAN_SOCKADDR_IS_IPV6(group)) {
 1144                 struct ipv6_mreq mreq;
 1145 
 1146                 mreq.ipv6mr_multiaddr = group->in6.sin6_addr;
 1147                 mreq.ipv6mr_interface = *ifidx;
 1148 
 1149                 bzero(&sopt, sizeof(sopt));
 1150                 sopt.sopt_dir = SOPT_SET;
 1151                 sopt.sopt_level = IPPROTO_IPV6;
 1152                 sopt.sopt_name = IPV6_JOIN_GROUP;
 1153                 sopt.sopt_val = &mreq;
 1154                 sopt.sopt_valsize = sizeof(mreq);
 1155                 error = sosetopt(vso->vxlso_sock, &sopt);
 1156                 if (error)
 1157                         return (error);
 1158 
 1159                 /*
 1160                  * BMV: As with IPv4, we would really like to know what
 1161                  * interface in6p_lookup_mcast_ifp() selected.
 1162                  */
 1163         } else
 1164                 error = EAFNOSUPPORT;
 1165 
 1166         return (error);
 1167 }
 1168 
 1169 static int
 1170 vxlan_socket_mc_leave_group(struct vxlan_socket *vso,
 1171     const union vxlan_sockaddr *group, const union vxlan_sockaddr *source,
 1172     int ifidx)
 1173 {
 1174         struct sockopt sopt;
 1175         int error;
 1176 
 1177         bzero(&sopt, sizeof(sopt));
 1178         sopt.sopt_dir = SOPT_SET;
 1179 
 1180         if (VXLAN_SOCKADDR_IS_IPV4(group)) {
 1181                 struct ip_mreq mreq;
 1182 
 1183                 mreq.imr_multiaddr = group->in4.sin_addr;
 1184                 mreq.imr_interface = source->in4.sin_addr;
 1185 
 1186                 sopt.sopt_level = IPPROTO_IP;
 1187                 sopt.sopt_name = IP_DROP_MEMBERSHIP;
 1188                 sopt.sopt_val = &mreq;
 1189                 sopt.sopt_valsize = sizeof(mreq);
 1190                 error = sosetopt(vso->vxlso_sock, &sopt);
 1191 
 1192         } else if (VXLAN_SOCKADDR_IS_IPV6(group)) {
 1193                 struct ipv6_mreq mreq;
 1194 
 1195                 mreq.ipv6mr_multiaddr = group->in6.sin6_addr;
 1196                 mreq.ipv6mr_interface = ifidx;
 1197 
 1198                 sopt.sopt_level = IPPROTO_IPV6;
 1199                 sopt.sopt_name = IPV6_LEAVE_GROUP;
 1200                 sopt.sopt_val = &mreq;
 1201                 sopt.sopt_valsize = sizeof(mreq);
 1202                 error = sosetopt(vso->vxlso_sock, &sopt);
 1203 
 1204         } else
 1205                 error = EAFNOSUPPORT;
 1206 
 1207         return (error);
 1208 }
 1209 
 1210 static int
 1211 vxlan_socket_mc_add_group(struct vxlan_socket *vso,
 1212     const union vxlan_sockaddr *group, const union vxlan_sockaddr *local,
 1213     int ifidx, int *idx)
 1214 {
 1215         union vxlan_sockaddr source;
 1216         struct vxlan_socket_mc_info *mc;
 1217         int i, empty, error;
 1218 
 1219         /*
 1220          * Within a socket, the same multicast group may be used by multiple
 1221          * interfaces, each with a different network identifier. But a socket
 1222          * may only join a multicast group once, so keep track of the users
 1223          * here.
 1224          */
 1225 
 1226         VXLAN_SO_WLOCK(vso);
 1227         for (empty = 0, i = 0; i < VXLAN_SO_MC_MAX_GROUPS; i++) {
 1228                 mc = &vso->vxlso_mc[i];
 1229 
 1230                 if (mc->vxlsomc_gaddr.sa.sa_family == AF_UNSPEC) {
 1231                         empty++;
 1232                         continue;
 1233                 }
 1234 
 1235                 if (vxlan_sockaddr_mc_info_match(mc, group, local, ifidx))
 1236                         goto out;
 1237         }
 1238         VXLAN_SO_WUNLOCK(vso);
 1239 
 1240         if (empty == 0)
 1241                 return (ENOSPC);
 1242 
 1243         error = vxlan_socket_mc_join_group(vso, group, local, &ifidx, &source);
 1244         if (error)
 1245                 return (error);
 1246 
 1247         VXLAN_SO_WLOCK(vso);
 1248         for (i = 0; i < VXLAN_SO_MC_MAX_GROUPS; i++) {
 1249                 mc = &vso->vxlso_mc[i];
 1250 
 1251                 if (mc->vxlsomc_gaddr.sa.sa_family == AF_UNSPEC) {
 1252                         vxlan_sockaddr_copy(&mc->vxlsomc_gaddr, &group->sa);
 1253                         vxlan_sockaddr_copy(&mc->vxlsomc_saddr, &source.sa);
 1254                         mc->vxlsomc_ifidx = ifidx;
 1255                         goto out;
 1256                 }
 1257         }
 1258         VXLAN_SO_WUNLOCK(vso);
 1259 
 1260         error = vxlan_socket_mc_leave_group(vso, group, &source, ifidx);
 1261         MPASS(error == 0);
 1262 
 1263         return (ENOSPC);
 1264 
 1265 out:
 1266         mc->vxlsomc_users++;
 1267         VXLAN_SO_WUNLOCK(vso);
 1268 
 1269         *idx = i;
 1270 
 1271         return (0);
 1272 }
 1273 
 1274 static void
 1275 vxlan_socket_mc_release_group_by_idx(struct vxlan_socket *vso, int idx)
 1276 {
 1277         union vxlan_sockaddr group, source;
 1278         struct vxlan_socket_mc_info *mc;
 1279         int ifidx, leave;
 1280 
 1281         KASSERT(idx >= 0 && idx < VXLAN_SO_MC_MAX_GROUPS,
 1282             ("%s: vso %p idx %d out of bounds", __func__, vso, idx));
 1283 
 1284         leave = 0;
 1285         mc = &vso->vxlso_mc[idx];
 1286 
 1287         VXLAN_SO_WLOCK(vso);
 1288         mc->vxlsomc_users--;
 1289         if (mc->vxlsomc_users == 0) {
 1290                 group = mc->vxlsomc_gaddr;
 1291                 source = mc->vxlsomc_saddr;
 1292                 ifidx = mc->vxlsomc_ifidx;
 1293                 bzero(mc, sizeof(*mc));
 1294                 leave = 1;
 1295         }
 1296         VXLAN_SO_WUNLOCK(vso);
 1297 
 1298         if (leave != 0) {
 1299                 /*
 1300                  * Our socket's membership in this group may have already
 1301                  * been removed if we joined through an interface that's
 1302                  * been detached.
 1303                  */
 1304                 vxlan_socket_mc_leave_group(vso, &group, &source, ifidx);
 1305         }
 1306 }
 1307 
 1308 static struct vxlan_softc *
 1309 vxlan_socket_lookup_softc_locked(struct vxlan_socket *vso, uint32_t vni)
 1310 {
 1311         struct vxlan_softc *sc;
 1312         uint32_t hash;
 1313 
 1314         VXLAN_SO_LOCK_ASSERT(vso);
 1315         hash = VXLAN_SO_VNI_HASH(vni);
 1316 
 1317         LIST_FOREACH(sc, &vso->vxlso_vni_hash[hash], vxl_entry) {
 1318                 if (sc->vxl_vni == vni) {
 1319                         VXLAN_ACQUIRE(sc);
 1320                         break;
 1321                 }
 1322         }
 1323 
 1324         return (sc);
 1325 }
 1326 
 1327 static struct vxlan_softc *
 1328 vxlan_socket_lookup_softc(struct vxlan_socket *vso, uint32_t vni)
 1329 {
 1330         struct rm_priotracker tracker;
 1331         struct vxlan_softc *sc;
 1332 
 1333         VXLAN_SO_RLOCK(vso, &tracker);
 1334         sc = vxlan_socket_lookup_softc_locked(vso, vni);
 1335         VXLAN_SO_RUNLOCK(vso, &tracker);
 1336 
 1337         return (sc);
 1338 }
 1339 
 1340 static int
 1341 vxlan_socket_insert_softc(struct vxlan_socket *vso, struct vxlan_softc *sc)
 1342 {
 1343         struct vxlan_softc *tsc;
 1344         uint32_t vni, hash;
 1345 
 1346         vni = sc->vxl_vni;
 1347         hash = VXLAN_SO_VNI_HASH(vni);
 1348 
 1349         VXLAN_SO_WLOCK(vso);
 1350         tsc = vxlan_socket_lookup_softc_locked(vso, vni);
 1351         if (tsc != NULL) {
 1352                 VXLAN_SO_WUNLOCK(vso);
 1353                 vxlan_release(tsc);
 1354                 return (EEXIST);
 1355         }
 1356 
 1357         VXLAN_ACQUIRE(sc);
 1358         LIST_INSERT_HEAD(&vso->vxlso_vni_hash[hash], sc, vxl_entry);
 1359         VXLAN_SO_WUNLOCK(vso);
 1360 
 1361         return (0);
 1362 }
 1363 
 1364 static void
 1365 vxlan_socket_remove_softc(struct vxlan_socket *vso, struct vxlan_softc *sc)
 1366 {
 1367 
 1368         VXLAN_SO_WLOCK(vso);
 1369         LIST_REMOVE(sc, vxl_entry);
 1370         VXLAN_SO_WUNLOCK(vso);
 1371 
 1372         vxlan_release(sc);
 1373 }
 1374 
 1375 static struct ifnet *
 1376 vxlan_multicast_if_ref(struct vxlan_softc *sc, int ipv4)
 1377 {
 1378         struct ifnet *ifp;
 1379 
 1380         VXLAN_LOCK_ASSERT(sc);
 1381 
 1382         if (ipv4 && sc->vxl_im4o != NULL)
 1383                 ifp = sc->vxl_im4o->imo_multicast_ifp;
 1384         else if (!ipv4 && sc->vxl_im6o != NULL)
 1385                 ifp = sc->vxl_im6o->im6o_multicast_ifp;
 1386         else
 1387                 ifp = NULL;
 1388 
 1389         if (ifp != NULL)
 1390                 if_ref(ifp);
 1391 
 1392         return (ifp);
 1393 }
 1394 
 1395 static void
 1396 vxlan_free_multicast(struct vxlan_softc *sc)
 1397 {
 1398 
 1399         if (sc->vxl_mc_ifp != NULL) {
 1400                 if_rele(sc->vxl_mc_ifp);
 1401                 sc->vxl_mc_ifp = NULL;
 1402                 sc->vxl_mc_ifindex = 0;
 1403         }
 1404 
 1405         if (sc->vxl_im4o != NULL) {
 1406                 free(sc->vxl_im4o, M_VXLAN);
 1407                 sc->vxl_im4o = NULL;
 1408         }
 1409 
 1410         if (sc->vxl_im6o != NULL) {
 1411                 free(sc->vxl_im6o, M_VXLAN);
 1412                 sc->vxl_im6o = NULL;
 1413         }
 1414 }
 1415 
 1416 static int
 1417 vxlan_setup_multicast_interface(struct vxlan_softc *sc)
 1418 {
 1419         struct ifnet *ifp;
 1420 
 1421         ifp = ifunit_ref(sc->vxl_mc_ifname);
 1422         if (ifp == NULL) {
 1423                 if_printf(sc->vxl_ifp, "multicast interface %s does "
 1424                     "not exist\n", sc->vxl_mc_ifname);
 1425                 return (ENOENT);
 1426         }
 1427 
 1428         if ((ifp->if_flags & IFF_MULTICAST) == 0) {
 1429                 if_printf(sc->vxl_ifp, "interface %s does not support "
 1430                      "multicast\n", sc->vxl_mc_ifname);
 1431                 if_rele(ifp);
 1432                 return (ENOTSUP);
 1433         }
 1434 
 1435         sc->vxl_mc_ifp = ifp;
 1436         sc->vxl_mc_ifindex = ifp->if_index;
 1437 
 1438         return (0);
 1439 }
 1440 
 1441 static int
 1442 vxlan_setup_multicast(struct vxlan_softc *sc)
 1443 {
 1444         const union vxlan_sockaddr *group;
 1445         int error;
 1446 
 1447         group = &sc->vxl_dst_addr;
 1448         error = 0;
 1449 
 1450         if (sc->vxl_mc_ifname[0] != '\0') {
 1451                 error = vxlan_setup_multicast_interface(sc);
 1452                 if (error)
 1453                         return (error);
 1454         }
 1455 
 1456         /*
 1457          * Initialize an multicast options structure that is sufficiently
 1458          * populated for use in the respective IP output routine. This
 1459          * structure is typically stored in the socket, but our sockets
 1460          * may be shared among multiple interfaces.
 1461          */
 1462         if (VXLAN_SOCKADDR_IS_IPV4(group)) {
 1463                 sc->vxl_im4o = malloc(sizeof(struct ip_moptions), M_VXLAN,
 1464                     M_ZERO | M_WAITOK);
 1465                 sc->vxl_im4o->imo_multicast_ifp = sc->vxl_mc_ifp;
 1466                 sc->vxl_im4o->imo_multicast_ttl = sc->vxl_ttl;
 1467                 sc->vxl_im4o->imo_multicast_vif = -1;
 1468         } else if (VXLAN_SOCKADDR_IS_IPV6(group)) {
 1469                 sc->vxl_im6o = malloc(sizeof(struct ip6_moptions), M_VXLAN,
 1470                     M_ZERO | M_WAITOK);
 1471                 sc->vxl_im6o->im6o_multicast_ifp = sc->vxl_mc_ifp;
 1472                 sc->vxl_im6o->im6o_multicast_hlim = sc->vxl_ttl;
 1473         }
 1474 
 1475         return (error);
 1476 }
 1477 
 1478 static int
 1479 vxlan_setup_socket(struct vxlan_softc *sc)
 1480 {
 1481         struct vxlan_socket *vso;
 1482         struct ifnet *ifp;
 1483         union vxlan_sockaddr *saddr, *daddr;
 1484         int multicast, error;
 1485 
 1486         vso = NULL;
 1487         ifp = sc->vxl_ifp;
 1488         saddr = &sc->vxl_src_addr;
 1489         daddr = &sc->vxl_dst_addr;
 1490 
 1491         multicast = vxlan_sockaddr_in_multicast(daddr);
 1492         MPASS(multicast != -1);
 1493         sc->vxl_vso_mc_index = -1;
 1494 
 1495         /*
 1496          * Try to create the socket. If that fails, attempt to use an
 1497          * existing socket.
 1498          */
 1499         error = vxlan_socket_create(ifp, multicast, saddr, &vso);
 1500         if (error) {
 1501                 if (multicast != 0)
 1502                         vso = vxlan_socket_mc_lookup(saddr);
 1503                 else
 1504                         vso = vxlan_socket_lookup(saddr);
 1505 
 1506                 if (vso == NULL) {
 1507                         if_printf(ifp, "cannot create socket (error: %d), "
 1508                             "and no existing socket found\n", error);
 1509                         goto out;
 1510                 }
 1511         }
 1512 
 1513         if (multicast != 0) {
 1514                 error = vxlan_setup_multicast(sc);
 1515                 if (error)
 1516                         goto out;
 1517 
 1518                 error = vxlan_socket_mc_add_group(vso, daddr, saddr,
 1519                     sc->vxl_mc_ifindex, &sc->vxl_vso_mc_index);
 1520                 if (error)
 1521                         goto out;
 1522         }
 1523 
 1524         sc->vxl_sock = vso;
 1525         error = vxlan_socket_insert_softc(vso, sc);
 1526         if (error) {
 1527                 sc->vxl_sock = NULL;
 1528                 if_printf(ifp, "network identifier %d already exists in "
 1529                     "this socket\n", sc->vxl_vni);
 1530                 goto out;
 1531         }
 1532 
 1533         return (0);
 1534 
 1535 out:
 1536         if (vso != NULL) {
 1537                 if (sc->vxl_vso_mc_index != -1) {
 1538                         vxlan_socket_mc_release_group_by_idx(vso,
 1539                             sc->vxl_vso_mc_index);
 1540                         sc->vxl_vso_mc_index = -1;
 1541                 }
 1542                 if (multicast != 0)
 1543                         vxlan_free_multicast(sc);
 1544                 vxlan_socket_release(vso);
 1545         }
 1546 
 1547         return (error);
 1548 }
 1549 
 1550 static void
 1551 vxlan_setup_interface(struct vxlan_softc *sc)
 1552 {
 1553         struct ifnet *ifp;
 1554 
 1555         ifp = sc->vxl_ifp;
 1556         ifp->if_hdrlen = ETHER_HDR_LEN + sizeof(struct vxlanudphdr);
 1557 
 1558         if (VXLAN_SOCKADDR_IS_IPV4(&sc->vxl_dst_addr) != 0)
 1559                 ifp->if_hdrlen += sizeof(struct ip);
 1560         else if (VXLAN_SOCKADDR_IS_IPV6(&sc->vxl_dst_addr) != 0)
 1561                 ifp->if_hdrlen += sizeof(struct ip6_hdr);
 1562 }
 1563 
 1564 static int
 1565 vxlan_valid_init_config(struct vxlan_softc *sc)
 1566 {
 1567         const char *reason;
 1568 
 1569         if (vxlan_check_vni(sc->vxl_vni) != 0) {
 1570                 reason = "invalid virtual network identifier specified";
 1571                 goto fail;
 1572         }
 1573 
 1574         if (vxlan_sockaddr_supported(&sc->vxl_src_addr, 1) == 0) {
 1575                 reason = "source address type is not supported";
 1576                 goto fail;
 1577         }
 1578 
 1579         if (vxlan_sockaddr_supported(&sc->vxl_dst_addr, 0) == 0) {
 1580                 reason = "destination address type is not supported";
 1581                 goto fail;
 1582         }
 1583 
 1584         if (vxlan_sockaddr_in_any(&sc->vxl_dst_addr) != 0) {
 1585                 reason = "no valid destination address specified";
 1586                 goto fail;
 1587         }
 1588 
 1589         if (vxlan_sockaddr_in_multicast(&sc->vxl_dst_addr) == 0 &&
 1590             sc->vxl_mc_ifname[0] != '\0') {
 1591                 reason = "can only specify interface with a group address";
 1592                 goto fail;
 1593         }
 1594 
 1595         if (vxlan_sockaddr_in_any(&sc->vxl_src_addr) == 0) {
 1596                 if (VXLAN_SOCKADDR_IS_IPV4(&sc->vxl_src_addr) ^
 1597                     VXLAN_SOCKADDR_IS_IPV4(&sc->vxl_dst_addr)) {
 1598                         reason = "source and destination address must both "
 1599                             "be either IPv4 or IPv6";
 1600                         goto fail;
 1601                 }
 1602         }
 1603 
 1604         if (sc->vxl_src_addr.in4.sin_port == 0) {
 1605                 reason = "local port not specified";
 1606                 goto fail;
 1607         }
 1608 
 1609         if (sc->vxl_dst_addr.in4.sin_port == 0) {
 1610                 reason = "remote port not specified";
 1611                 goto fail;
 1612         }
 1613 
 1614         return (0);
 1615 
 1616 fail:
 1617         if_printf(sc->vxl_ifp, "cannot initialize interface: %s\n", reason);
 1618         return (EINVAL);
 1619 }
 1620 
 1621 static void
 1622 vxlan_init_wait(struct vxlan_softc *sc)
 1623 {
 1624 
 1625         VXLAN_LOCK_WASSERT(sc);
 1626         while (sc->vxl_flags & VXLAN_FLAG_INIT)
 1627                 rm_sleep(sc, &sc->vxl_lock, 0, "vxlint", hz);
 1628 }
 1629 
 1630 static void
 1631 vxlan_init_complete(struct vxlan_softc *sc)
 1632 {
 1633 
 1634         VXLAN_WLOCK(sc);
 1635         sc->vxl_flags &= ~VXLAN_FLAG_INIT;
 1636         wakeup(sc);
 1637         VXLAN_WUNLOCK(sc);
 1638 }
 1639 
 1640 static void
 1641 vxlan_init(void *xsc)
 1642 {
 1643         static const uint8_t empty_mac[ETHER_ADDR_LEN];
 1644         struct vxlan_softc *sc;
 1645         struct ifnet *ifp;
 1646 
 1647         sc = xsc;
 1648         ifp = sc->vxl_ifp;
 1649 
 1650         VXLAN_WLOCK(sc);
 1651         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
 1652                 VXLAN_WUNLOCK(sc);
 1653                 return;
 1654         }
 1655         sc->vxl_flags |= VXLAN_FLAG_INIT;
 1656         VXLAN_WUNLOCK(sc);
 1657 
 1658         if (vxlan_valid_init_config(sc) != 0)
 1659                 goto out;
 1660 
 1661         vxlan_setup_interface(sc);
 1662 
 1663         if (vxlan_setup_socket(sc) != 0)
 1664                 goto out;
 1665 
 1666         /* Initialize the default forwarding entry. */
 1667         vxlan_ftable_entry_init(sc, &sc->vxl_default_fe, empty_mac,
 1668             &sc->vxl_dst_addr.sa, VXLAN_FE_FLAG_STATIC);
 1669 
 1670         VXLAN_WLOCK(sc);
 1671         ifp->if_drv_flags |= IFF_DRV_RUNNING;
 1672         callout_reset(&sc->vxl_callout, vxlan_ftable_prune_period * hz,
 1673             vxlan_timer, sc);
 1674         VXLAN_WUNLOCK(sc);
 1675 
 1676         if_link_state_change(ifp, LINK_STATE_UP);
 1677 out:
 1678         vxlan_init_complete(sc);
 1679 }
 1680 
 1681 static void
 1682 vxlan_release(struct vxlan_softc *sc)
 1683 {
 1684 
 1685         /*
 1686          * The softc may be destroyed as soon as we release our reference,
 1687          * so we cannot serialize the wakeup with the softc lock. We use a
 1688          * timeout in our sleeps so a missed wakeup is unfortunate but not
 1689          * fatal.
 1690          */
 1691         if (VXLAN_RELEASE(sc) != 0)
 1692                 wakeup(sc);
 1693 }
 1694 
 1695 static void
 1696 vxlan_teardown_wait(struct vxlan_softc *sc)
 1697 {
 1698 
 1699         VXLAN_LOCK_WASSERT(sc);
 1700         while (sc->vxl_flags & VXLAN_FLAG_TEARDOWN)
 1701                 rm_sleep(sc, &sc->vxl_lock, 0, "vxltrn", hz);
 1702 }
 1703 
 1704 static void
 1705 vxlan_teardown_complete(struct vxlan_softc *sc)
 1706 {
 1707 
 1708         VXLAN_WLOCK(sc);
 1709         sc->vxl_flags &= ~VXLAN_FLAG_TEARDOWN;
 1710         wakeup(sc);
 1711         VXLAN_WUNLOCK(sc);
 1712 }
 1713 
 1714 static void
 1715 vxlan_teardown_locked(struct vxlan_softc *sc)
 1716 {
 1717         struct ifnet *ifp;
 1718         struct vxlan_socket *vso;
 1719 
 1720         ifp = sc->vxl_ifp;
 1721 
 1722         VXLAN_LOCK_WASSERT(sc);
 1723         MPASS(sc->vxl_flags & VXLAN_FLAG_TEARDOWN);
 1724 
 1725         ifp->if_flags &= ~IFF_UP;
 1726         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1727         callout_stop(&sc->vxl_callout);
 1728         vso = sc->vxl_sock;
 1729         sc->vxl_sock = NULL;
 1730 
 1731         VXLAN_WUNLOCK(sc);
 1732         if_link_state_change(ifp, LINK_STATE_DOWN);
 1733 
 1734         if (vso != NULL) {
 1735                 vxlan_socket_remove_softc(vso, sc);
 1736 
 1737                 if (sc->vxl_vso_mc_index != -1) {
 1738                         vxlan_socket_mc_release_group_by_idx(vso,
 1739                             sc->vxl_vso_mc_index);
 1740                         sc->vxl_vso_mc_index = -1;
 1741                 }
 1742         }
 1743 
 1744         VXLAN_WLOCK(sc);
 1745         while (sc->vxl_refcnt != 0)
 1746                 rm_sleep(sc, &sc->vxl_lock, 0, "vxldrn", hz);
 1747         VXLAN_WUNLOCK(sc);
 1748 
 1749         callout_drain(&sc->vxl_callout);
 1750 
 1751         vxlan_free_multicast(sc);
 1752         if (vso != NULL)
 1753                 vxlan_socket_release(vso);
 1754 
 1755         vxlan_teardown_complete(sc);
 1756 }
 1757 
 1758 static void
 1759 vxlan_teardown(struct vxlan_softc *sc)
 1760 {
 1761 
 1762         VXLAN_WLOCK(sc);
 1763         if (sc->vxl_flags & VXLAN_FLAG_TEARDOWN) {
 1764                 vxlan_teardown_wait(sc);
 1765                 VXLAN_WUNLOCK(sc);
 1766                 return;
 1767         }
 1768 
 1769         sc->vxl_flags |= VXLAN_FLAG_TEARDOWN;
 1770         vxlan_teardown_locked(sc);
 1771 }
 1772 
 1773 static void
 1774 vxlan_ifdetach(struct vxlan_softc *sc, struct ifnet *ifp,
 1775     struct vxlan_softc_head *list)
 1776 {
 1777 
 1778         VXLAN_WLOCK(sc);
 1779 
 1780         if (sc->vxl_mc_ifp != ifp)
 1781                 goto out;
 1782         if (sc->vxl_flags & VXLAN_FLAG_TEARDOWN)
 1783                 goto out;
 1784 
 1785         sc->vxl_flags |= VXLAN_FLAG_TEARDOWN;
 1786         LIST_INSERT_HEAD(list, sc, vxl_ifdetach_list);
 1787 
 1788 out:
 1789         VXLAN_WUNLOCK(sc);
 1790 }
 1791 
 1792 static void
 1793 vxlan_timer(void *xsc)
 1794 {
 1795         struct vxlan_softc *sc;
 1796 
 1797         sc = xsc;
 1798         VXLAN_LOCK_WASSERT(sc);
 1799 
 1800         vxlan_ftable_expire(sc);
 1801         callout_schedule(&sc->vxl_callout, vxlan_ftable_prune_period * hz);
 1802 }
 1803 
 1804 static int
 1805 vxlan_ioctl_ifflags(struct vxlan_softc *sc)
 1806 {
 1807         struct ifnet *ifp;
 1808 
 1809         ifp = sc->vxl_ifp;
 1810 
 1811         if (ifp->if_flags & IFF_UP) {
 1812                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
 1813                         vxlan_init(sc);
 1814         } else {
 1815                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1816                         vxlan_teardown(sc);
 1817         }
 1818 
 1819         return (0);
 1820 }
 1821 
 1822 static int
 1823 vxlan_ctrl_get_config(struct vxlan_softc *sc, void *arg)
 1824 {
 1825         struct rm_priotracker tracker;
 1826         struct ifvxlancfg *cfg;
 1827 
 1828         cfg = arg;
 1829         bzero(cfg, sizeof(*cfg));
 1830 
 1831         VXLAN_RLOCK(sc, &tracker);
 1832         cfg->vxlc_vni = sc->vxl_vni;
 1833         memcpy(&cfg->vxlc_local_sa, &sc->vxl_src_addr,
 1834             sizeof(union vxlan_sockaddr));
 1835         memcpy(&cfg->vxlc_remote_sa, &sc->vxl_dst_addr,
 1836             sizeof(union vxlan_sockaddr));
 1837         cfg->vxlc_mc_ifindex = sc->vxl_mc_ifindex;
 1838         cfg->vxlc_ftable_cnt = sc->vxl_ftable_cnt;
 1839         cfg->vxlc_ftable_max = sc->vxl_ftable_max;
 1840         cfg->vxlc_ftable_timeout = sc->vxl_ftable_timeout;
 1841         cfg->vxlc_port_min = sc->vxl_min_port;
 1842         cfg->vxlc_port_max = sc->vxl_max_port;
 1843         cfg->vxlc_learn = (sc->vxl_flags & VXLAN_FLAG_LEARN) != 0;
 1844         cfg->vxlc_ttl = sc->vxl_ttl;
 1845         VXLAN_RUNLOCK(sc, &tracker);
 1846 
 1847 #ifdef INET6
 1848         if (VXLAN_SOCKADDR_IS_IPV6(&cfg->vxlc_local_sa))
 1849                 sa6_recoverscope(&cfg->vxlc_local_sa.in6);
 1850         if (VXLAN_SOCKADDR_IS_IPV6(&cfg->vxlc_remote_sa))
 1851                 sa6_recoverscope(&cfg->vxlc_remote_sa.in6);
 1852 #endif
 1853 
 1854         return (0);
 1855 }
 1856 
 1857 static int
 1858 vxlan_ctrl_set_vni(struct vxlan_softc *sc, void *arg)
 1859 {
 1860         struct ifvxlancmd *cmd;
 1861         int error;
 1862 
 1863         cmd = arg;
 1864 
 1865         if (vxlan_check_vni(cmd->vxlcmd_vni) != 0)
 1866                 return (EINVAL);
 1867 
 1868         VXLAN_WLOCK(sc);
 1869         if (vxlan_can_change_config(sc)) {
 1870                 sc->vxl_vni = cmd->vxlcmd_vni;
 1871                 error = 0;
 1872         } else
 1873                 error = EBUSY;
 1874         VXLAN_WUNLOCK(sc);
 1875 
 1876         return (error);
 1877 }
 1878 
 1879 static int
 1880 vxlan_ctrl_set_local_addr(struct vxlan_softc *sc, void *arg)
 1881 {
 1882         struct ifvxlancmd *cmd;
 1883         union vxlan_sockaddr *vxlsa;
 1884         int error;
 1885 
 1886         cmd = arg;
 1887         vxlsa = &cmd->vxlcmd_sa;
 1888 
 1889         if (!VXLAN_SOCKADDR_IS_IPV46(vxlsa))
 1890                 return (EINVAL);
 1891         if (vxlan_sockaddr_in_multicast(vxlsa) != 0)
 1892                 return (EINVAL);
 1893         if (VXLAN_SOCKADDR_IS_IPV6(vxlsa)) {
 1894                 error = vxlan_sockaddr_in6_embedscope(vxlsa);
 1895                 if (error)
 1896                         return (error);
 1897         }
 1898 
 1899         VXLAN_WLOCK(sc);
 1900         if (vxlan_can_change_config(sc)) {
 1901                 vxlan_sockaddr_in_copy(&sc->vxl_src_addr, &vxlsa->sa);
 1902                 error = 0;
 1903         } else
 1904                 error = EBUSY;
 1905         VXLAN_WUNLOCK(sc);
 1906 
 1907         return (error);
 1908 }
 1909 
 1910 static int
 1911 vxlan_ctrl_set_remote_addr(struct vxlan_softc *sc, void *arg)
 1912 {
 1913         struct ifvxlancmd *cmd;
 1914         union vxlan_sockaddr *vxlsa;
 1915         int error;
 1916 
 1917         cmd = arg;
 1918         vxlsa = &cmd->vxlcmd_sa;
 1919 
 1920         if (!VXLAN_SOCKADDR_IS_IPV46(vxlsa))
 1921                 return (EINVAL);
 1922         if (VXLAN_SOCKADDR_IS_IPV6(vxlsa)) {
 1923                 error = vxlan_sockaddr_in6_embedscope(vxlsa);
 1924                 if (error)
 1925                         return (error);
 1926         }
 1927 
 1928         VXLAN_WLOCK(sc);
 1929         if (vxlan_can_change_config(sc)) {
 1930                 vxlan_sockaddr_in_copy(&sc->vxl_dst_addr, &vxlsa->sa);
 1931                 error = 0;
 1932         } else
 1933                 error = EBUSY;
 1934         VXLAN_WUNLOCK(sc);
 1935 
 1936         return (error);
 1937 }
 1938 
 1939 static int
 1940 vxlan_ctrl_set_local_port(struct vxlan_softc *sc, void *arg)
 1941 {
 1942         struct ifvxlancmd *cmd;
 1943         int error;
 1944 
 1945         cmd = arg;
 1946 
 1947         if (cmd->vxlcmd_port == 0)
 1948                 return (EINVAL);
 1949 
 1950         VXLAN_WLOCK(sc);
 1951         if (vxlan_can_change_config(sc)) {
 1952                 sc->vxl_src_addr.in4.sin_port = htons(cmd->vxlcmd_port);
 1953                 error = 0;
 1954         } else
 1955                 error = EBUSY;
 1956         VXLAN_WUNLOCK(sc);
 1957 
 1958         return (error);
 1959 }
 1960 
 1961 static int
 1962 vxlan_ctrl_set_remote_port(struct vxlan_softc *sc, void *arg)
 1963 {
 1964         struct ifvxlancmd *cmd;
 1965         int error;
 1966 
 1967         cmd = arg;
 1968 
 1969         if (cmd->vxlcmd_port == 0)
 1970                 return (EINVAL);
 1971 
 1972         VXLAN_WLOCK(sc);
 1973         if (vxlan_can_change_config(sc)) {
 1974                 sc->vxl_dst_addr.in4.sin_port = htons(cmd->vxlcmd_port);
 1975                 error = 0;
 1976         } else
 1977                 error = EBUSY;
 1978         VXLAN_WUNLOCK(sc);
 1979 
 1980         return (error);
 1981 }
 1982 
 1983 static int
 1984 vxlan_ctrl_set_port_range(struct vxlan_softc *sc, void *arg)
 1985 {
 1986         struct ifvxlancmd *cmd;
 1987         uint16_t min, max;
 1988         int error;
 1989 
 1990         cmd = arg;
 1991         min = cmd->vxlcmd_port_min;
 1992         max = cmd->vxlcmd_port_max;
 1993 
 1994         if (max < min)
 1995                 return (EINVAL);
 1996 
 1997         VXLAN_WLOCK(sc);
 1998         if (vxlan_can_change_config(sc)) {
 1999                 sc->vxl_min_port = min;
 2000                 sc->vxl_max_port = max;
 2001                 error = 0;
 2002         } else
 2003                 error = EBUSY;
 2004         VXLAN_WUNLOCK(sc);
 2005 
 2006         return (error);
 2007 }
 2008 
 2009 static int
 2010 vxlan_ctrl_set_ftable_timeout(struct vxlan_softc *sc, void *arg)
 2011 {
 2012         struct ifvxlancmd *cmd;
 2013         int error;
 2014 
 2015         cmd = arg;
 2016 
 2017         VXLAN_WLOCK(sc);
 2018         if (vxlan_check_ftable_timeout(cmd->vxlcmd_ftable_timeout) == 0) {
 2019                 sc->vxl_ftable_timeout = cmd->vxlcmd_ftable_timeout;
 2020                 error = 0;
 2021         } else
 2022                 error = EINVAL;
 2023         VXLAN_WUNLOCK(sc);
 2024 
 2025         return (error);
 2026 }
 2027 
 2028 static int
 2029 vxlan_ctrl_set_ftable_max(struct vxlan_softc *sc, void *arg)
 2030 {
 2031         struct ifvxlancmd *cmd;
 2032         int error;
 2033 
 2034         cmd = arg;
 2035 
 2036         VXLAN_WLOCK(sc);
 2037         if (vxlan_check_ftable_max(cmd->vxlcmd_ftable_max) == 0) {
 2038                 sc->vxl_ftable_max = cmd->vxlcmd_ftable_max;
 2039                 error = 0;
 2040         } else
 2041                 error = EINVAL;
 2042         VXLAN_WUNLOCK(sc);
 2043 
 2044         return (error);
 2045 }
 2046 
 2047 static int
 2048 vxlan_ctrl_set_multicast_if(struct vxlan_softc * sc, void *arg)
 2049 {
 2050         struct ifvxlancmd *cmd;
 2051         int error;
 2052 
 2053         cmd = arg;
 2054 
 2055         VXLAN_WLOCK(sc);
 2056         if (vxlan_can_change_config(sc)) {
 2057                 strlcpy(sc->vxl_mc_ifname, cmd->vxlcmd_ifname, IFNAMSIZ);
 2058                 error = 0;
 2059         } else
 2060                 error = EBUSY;
 2061         VXLAN_WUNLOCK(sc);
 2062 
 2063         return (error);
 2064 }
 2065 
 2066 static int
 2067 vxlan_ctrl_set_ttl(struct vxlan_softc *sc, void *arg)
 2068 {
 2069         struct ifvxlancmd *cmd;
 2070         int error;
 2071 
 2072         cmd = arg;
 2073 
 2074         VXLAN_WLOCK(sc);
 2075         if (vxlan_check_ttl(cmd->vxlcmd_ttl) == 0) {
 2076                 sc->vxl_ttl = cmd->vxlcmd_ttl;
 2077                 if (sc->vxl_im4o != NULL)
 2078                         sc->vxl_im4o->imo_multicast_ttl = sc->vxl_ttl;
 2079                 if (sc->vxl_im6o != NULL)
 2080                         sc->vxl_im6o->im6o_multicast_hlim = sc->vxl_ttl;
 2081                 error = 0;
 2082         } else
 2083                 error = EINVAL;
 2084         VXLAN_WUNLOCK(sc);
 2085 
 2086         return (error);
 2087 }
 2088 
 2089 static int
 2090 vxlan_ctrl_set_learn(struct vxlan_softc *sc, void *arg)
 2091 {
 2092         struct ifvxlancmd *cmd;
 2093 
 2094         cmd = arg;
 2095 
 2096         VXLAN_WLOCK(sc);
 2097         if (cmd->vxlcmd_flags & VXLAN_CMD_FLAG_LEARN)
 2098                 sc->vxl_flags |= VXLAN_FLAG_LEARN;
 2099         else
 2100                 sc->vxl_flags &= ~VXLAN_FLAG_LEARN;
 2101         VXLAN_WUNLOCK(sc);
 2102 
 2103         return (0);
 2104 }
 2105 
 2106 static int
 2107 vxlan_ctrl_ftable_entry_add(struct vxlan_softc *sc, void *arg)
 2108 {
 2109         union vxlan_sockaddr vxlsa;
 2110         struct ifvxlancmd *cmd;
 2111         struct vxlan_ftable_entry *fe;
 2112         int error;
 2113 
 2114         cmd = arg;
 2115         vxlsa = cmd->vxlcmd_sa;
 2116 
 2117         if (!VXLAN_SOCKADDR_IS_IPV46(&vxlsa))
 2118                 return (EINVAL);
 2119         if (vxlan_sockaddr_in_any(&vxlsa) != 0)
 2120                 return (EINVAL);
 2121         if (vxlan_sockaddr_in_multicast(&vxlsa) != 0)
 2122                 return (EINVAL);
 2123         /* BMV: We could support both IPv4 and IPv6 later. */
 2124         if (vxlsa.sa.sa_family != sc->vxl_dst_addr.sa.sa_family)
 2125                 return (EAFNOSUPPORT);
 2126 
 2127         if (VXLAN_SOCKADDR_IS_IPV6(&vxlsa)) {
 2128                 error = vxlan_sockaddr_in6_embedscope(&vxlsa);
 2129                 if (error)
 2130                         return (error);
 2131         }
 2132 
 2133         fe = vxlan_ftable_entry_alloc();
 2134         if (fe == NULL)
 2135                 return (ENOMEM);
 2136 
 2137         if (vxlsa.in4.sin_port == 0)
 2138                 vxlsa.in4.sin_port = sc->vxl_dst_addr.in4.sin_port;
 2139 
 2140         vxlan_ftable_entry_init(sc, fe, cmd->vxlcmd_mac, &vxlsa.sa,
 2141             VXLAN_FE_FLAG_STATIC);
 2142 
 2143         VXLAN_WLOCK(sc);
 2144         error = vxlan_ftable_entry_insert(sc, fe);
 2145         VXLAN_WUNLOCK(sc);
 2146 
 2147         if (error)
 2148                 vxlan_ftable_entry_free(fe);
 2149 
 2150         return (error);
 2151 }
 2152 
 2153 static int
 2154 vxlan_ctrl_ftable_entry_rem(struct vxlan_softc *sc, void *arg)
 2155 {
 2156         struct ifvxlancmd *cmd;
 2157         struct vxlan_ftable_entry *fe;
 2158         int error;
 2159 
 2160         cmd = arg;
 2161 
 2162         VXLAN_WLOCK(sc);
 2163         fe = vxlan_ftable_entry_lookup(sc, cmd->vxlcmd_mac);
 2164         if (fe != NULL) {
 2165                 vxlan_ftable_entry_destroy(sc, fe);
 2166                 error = 0;
 2167         } else
 2168                 error = ENOENT;
 2169         VXLAN_WUNLOCK(sc);
 2170 
 2171         return (error);
 2172 }
 2173 
 2174 static int
 2175 vxlan_ctrl_flush(struct vxlan_softc *sc, void *arg)
 2176 {
 2177         struct ifvxlancmd *cmd;
 2178         int all;
 2179 
 2180         cmd = arg;
 2181         all = cmd->vxlcmd_flags & VXLAN_CMD_FLAG_FLUSH_ALL;
 2182 
 2183         VXLAN_WLOCK(sc);
 2184         vxlan_ftable_flush(sc, all);
 2185         VXLAN_WUNLOCK(sc);
 2186 
 2187         return (0);
 2188 }
 2189 
 2190 static int
 2191 vxlan_ioctl_drvspec(struct vxlan_softc *sc, struct ifdrv *ifd, int get)
 2192 {
 2193         const struct vxlan_control *vc;
 2194         union {
 2195                 struct ifvxlancfg       cfg;
 2196                 struct ifvxlancmd       cmd;
 2197         } args;
 2198         int out, error;
 2199 
 2200         if (ifd->ifd_cmd >= vxlan_control_table_size)
 2201                 return (EINVAL);
 2202 
 2203         bzero(&args, sizeof(args));
 2204         vc = &vxlan_control_table[ifd->ifd_cmd];
 2205         out = (vc->vxlc_flags & VXLAN_CTRL_FLAG_COPYOUT) != 0;
 2206 
 2207         if ((get != 0 && out == 0) || (get == 0 && out != 0))
 2208                 return (EINVAL);
 2209 
 2210         if (vc->vxlc_flags & VXLAN_CTRL_FLAG_SUSER) {
 2211                 error = priv_check(curthread, PRIV_NET_VXLAN);
 2212                 if (error)
 2213                         return (error);
 2214         }
 2215 
 2216         if (ifd->ifd_len != vc->vxlc_argsize ||
 2217             ifd->ifd_len > sizeof(args))
 2218                 return (EINVAL);
 2219 
 2220         if (vc->vxlc_flags & VXLAN_CTRL_FLAG_COPYIN) {
 2221                 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
 2222                 if (error)
 2223                         return (error);
 2224         }
 2225 
 2226         error = vc->vxlc_func(sc, &args);
 2227         if (error)
 2228                 return (error);
 2229 
 2230         if (vc->vxlc_flags & VXLAN_CTRL_FLAG_COPYOUT) {
 2231                 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
 2232                 if (error)
 2233                         return (error);
 2234         }
 2235 
 2236         return (0);
 2237 }
 2238 
 2239 static int
 2240 vxlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 2241 {
 2242         struct rm_priotracker tracker;
 2243         struct vxlan_softc *sc;
 2244         struct ifreq *ifr;
 2245         struct ifdrv *ifd;
 2246         int error;
 2247 
 2248         sc = ifp->if_softc;
 2249         ifr = (struct ifreq *) data;
 2250         ifd = (struct ifdrv *) data;
 2251 
 2252         error = 0;
 2253 
 2254         switch (cmd) {
 2255         case SIOCADDMULTI:
 2256         case SIOCDELMULTI:
 2257                 break;
 2258 
 2259         case SIOCGDRVSPEC:
 2260         case SIOCSDRVSPEC:
 2261                 error = vxlan_ioctl_drvspec(sc, ifd, cmd == SIOCGDRVSPEC);
 2262                 break;
 2263 
 2264         case SIOCSIFFLAGS:
 2265                 error = vxlan_ioctl_ifflags(sc);
 2266                 break;
 2267 
 2268         case SIOCSIFMEDIA:
 2269         case SIOCGIFMEDIA:
 2270                 error = ifmedia_ioctl(ifp, ifr, &sc->vxl_media, cmd);
 2271                 break;
 2272 
 2273         case SIOCGTUNFIB:
 2274                 VXLAN_RLOCK(sc, &tracker);
 2275                 ifr->ifr_fib = sc->vxl_fibnum;
 2276                 VXLAN_RUNLOCK(sc, &tracker);
 2277                 break;
 2278 
 2279         case SIOCSTUNFIB:
 2280                 if ((error = priv_check(curthread, PRIV_NET_VXLAN)) != 0)
 2281                         break;
 2282 
 2283                 if (ifr->ifr_fib >= rt_numfibs)
 2284                         error = EINVAL;
 2285                 else {
 2286                         VXLAN_WLOCK(sc);
 2287                         sc->vxl_fibnum = ifr->ifr_fib;
 2288                         VXLAN_WUNLOCK(sc);
 2289                 }
 2290                 break;
 2291 
 2292         default:
 2293                 error = ether_ioctl(ifp, cmd, data);
 2294                 break;
 2295         }
 2296 
 2297         return (error);
 2298 }
 2299 
 2300 #if defined(INET) || defined(INET6)
 2301 static uint16_t
 2302 vxlan_pick_source_port(struct vxlan_softc *sc, struct mbuf *m)
 2303 {
 2304         int range;
 2305         uint32_t hash;
 2306 
 2307         range = sc->vxl_max_port - sc->vxl_min_port + 1;
 2308 
 2309         if (M_HASHTYPE_ISHASH(m))
 2310                 hash = m->m_pkthdr.flowid;
 2311         else
 2312                 hash = jenkins_hash(m->m_data, ETHER_HDR_LEN,
 2313                     sc->vxl_port_hash_key);
 2314 
 2315         return (sc->vxl_min_port + (hash % range));
 2316 }
 2317 
 2318 static void
 2319 vxlan_encap_header(struct vxlan_softc *sc, struct mbuf *m, int ipoff,
 2320     uint16_t srcport, uint16_t dstport)
 2321 {
 2322         struct vxlanudphdr *hdr;
 2323         struct udphdr *udph;
 2324         struct vxlan_header *vxh;
 2325         int len;
 2326 
 2327         len = m->m_pkthdr.len - ipoff;
 2328         MPASS(len >= sizeof(struct vxlanudphdr));
 2329         hdr = mtodo(m, ipoff);
 2330 
 2331         udph = &hdr->vxlh_udp;
 2332         udph->uh_sport = srcport;
 2333         udph->uh_dport = dstport;
 2334         udph->uh_ulen = htons(len);
 2335         udph->uh_sum = 0;
 2336 
 2337         vxh = &hdr->vxlh_hdr;
 2338         vxh->vxlh_flags = htonl(VXLAN_HDR_FLAGS_VALID_VNI);
 2339         vxh->vxlh_vni = htonl(sc->vxl_vni << VXLAN_HDR_VNI_SHIFT);
 2340 }
 2341 #endif
 2342 
 2343 static int
 2344 vxlan_encap4(struct vxlan_softc *sc, const union vxlan_sockaddr *fvxlsa,
 2345     struct mbuf *m)
 2346 {
 2347 #ifdef INET
 2348         struct ifnet *ifp;
 2349         struct ip *ip;
 2350         struct in_addr srcaddr, dstaddr;
 2351         uint16_t srcport, dstport;
 2352         int plen, mcast, error;
 2353 
 2354         ifp = sc->vxl_ifp;
 2355         srcaddr = sc->vxl_src_addr.in4.sin_addr;
 2356         srcport = vxlan_pick_source_port(sc, m);
 2357         dstaddr = fvxlsa->in4.sin_addr;
 2358         dstport = fvxlsa->in4.sin_port;
 2359 
 2360         plen = m->m_pkthdr.len;
 2361         M_PREPEND(m, sizeof(struct ip) + sizeof(struct vxlanudphdr),
 2362             M_NOWAIT);
 2363         if (m == NULL) {
 2364                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 2365                 return (ENOBUFS);
 2366         }
 2367 
 2368         ip = mtod(m, struct ip *);
 2369         ip->ip_tos = 0;
 2370         ip->ip_len = htons(m->m_pkthdr.len);
 2371         ip->ip_off = 0;
 2372         ip->ip_ttl = sc->vxl_ttl;
 2373         ip->ip_p = IPPROTO_UDP;
 2374         ip->ip_sum = 0;
 2375         ip->ip_src = srcaddr;
 2376         ip->ip_dst = dstaddr;
 2377 
 2378         vxlan_encap_header(sc, m, sizeof(struct ip), srcport, dstport);
 2379 
 2380         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
 2381         m->m_flags &= ~(M_MCAST | M_BCAST);
 2382 
 2383         error = ip_output(m, NULL, NULL, 0, sc->vxl_im4o, NULL);
 2384         if (error == 0) {
 2385                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
 2386                 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
 2387                 if (mcast != 0)
 2388                         if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
 2389         } else
 2390                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 2391 
 2392         return (error);
 2393 #else
 2394         m_freem(m);
 2395         return (ENOTSUP);
 2396 #endif
 2397 }
 2398 
 2399 static int
 2400 vxlan_encap6(struct vxlan_softc *sc, const union vxlan_sockaddr *fvxlsa,
 2401     struct mbuf *m)
 2402 {
 2403 #ifdef INET6
 2404         struct ifnet *ifp;
 2405         struct ip6_hdr *ip6;
 2406         const struct in6_addr *srcaddr, *dstaddr;
 2407         uint16_t srcport, dstport;
 2408         int plen, mcast, error;
 2409 
 2410         ifp = sc->vxl_ifp;
 2411         srcaddr = &sc->vxl_src_addr.in6.sin6_addr;
 2412         srcport = vxlan_pick_source_port(sc, m);
 2413         dstaddr = &fvxlsa->in6.sin6_addr;
 2414         dstport = fvxlsa->in6.sin6_port;
 2415 
 2416         plen = m->m_pkthdr.len;
 2417         M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct vxlanudphdr),
 2418             M_NOWAIT);
 2419         if (m == NULL) {
 2420                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 2421                 return (ENOBUFS);
 2422         }
 2423 
 2424         ip6 = mtod(m, struct ip6_hdr *);
 2425         ip6->ip6_flow = 0;              /* BMV: Keep in forwarding entry? */
 2426         ip6->ip6_vfc = IPV6_VERSION;
 2427         ip6->ip6_plen = 0;
 2428         ip6->ip6_nxt = IPPROTO_UDP;
 2429         ip6->ip6_hlim = sc->vxl_ttl;
 2430         ip6->ip6_src = *srcaddr;
 2431         ip6->ip6_dst = *dstaddr;
 2432 
 2433         vxlan_encap_header(sc, m, sizeof(struct ip6_hdr), srcport, dstport);
 2434 
 2435         /*
 2436          * XXX BMV We need support for RFC6935 before we can send and
 2437          * receive IPv6 UDP packets with a zero checksum.
 2438          */
 2439         {
 2440                 struct udphdr *hdr = mtodo(m, sizeof(struct ip6_hdr));
 2441                 hdr->uh_sum = in6_cksum_pseudo(ip6,
 2442                     m->m_pkthdr.len - sizeof(struct ip6_hdr), IPPROTO_UDP, 0);
 2443                 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
 2444                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
 2445         }
 2446 
 2447         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
 2448         m->m_flags &= ~(M_MCAST | M_BCAST);
 2449 
 2450         error = ip6_output(m, NULL, NULL, 0, sc->vxl_im6o, NULL, NULL);
 2451         if (error == 0) {
 2452                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
 2453                 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
 2454                 if (mcast != 0)
 2455                         if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
 2456         } else
 2457                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 2458 
 2459         return (error);
 2460 #else
 2461         m_freem(m);
 2462         return (ENOTSUP);
 2463 #endif
 2464 }
 2465 
 2466 static int
 2467 vxlan_transmit(struct ifnet *ifp, struct mbuf *m)
 2468 {
 2469         struct rm_priotracker tracker;
 2470         union vxlan_sockaddr vxlsa;
 2471         struct vxlan_softc *sc;
 2472         struct vxlan_ftable_entry *fe;
 2473         struct ifnet *mcifp;
 2474         struct ether_header *eh;
 2475         int ipv4, error;
 2476 
 2477         sc = ifp->if_softc;
 2478         eh = mtod(m, struct ether_header *);
 2479         fe = NULL;
 2480         mcifp = NULL;
 2481 
 2482         ETHER_BPF_MTAP(ifp, m);
 2483 
 2484         VXLAN_RLOCK(sc, &tracker);
 2485         M_SETFIB(m, sc->vxl_fibnum);
 2486         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 2487                 VXLAN_RUNLOCK(sc, &tracker);
 2488                 m_freem(m);
 2489                 return (ENETDOWN);
 2490         }
 2491 
 2492         if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
 2493                 fe = vxlan_ftable_entry_lookup(sc, eh->ether_dhost);
 2494         if (fe == NULL)
 2495                 fe = &sc->vxl_default_fe;
 2496         vxlan_sockaddr_copy(&vxlsa, &fe->vxlfe_raddr.sa);
 2497 
 2498         ipv4 = VXLAN_SOCKADDR_IS_IPV4(&vxlsa) != 0;
 2499         if (vxlan_sockaddr_in_multicast(&vxlsa) != 0)
 2500                 mcifp = vxlan_multicast_if_ref(sc, ipv4);
 2501 
 2502         VXLAN_ACQUIRE(sc);
 2503         VXLAN_RUNLOCK(sc, &tracker);
 2504 
 2505         if (ipv4 != 0)
 2506                 error = vxlan_encap4(sc, &vxlsa, m);
 2507         else
 2508                 error = vxlan_encap6(sc, &vxlsa, m);
 2509 
 2510         vxlan_release(sc);
 2511         if (mcifp != NULL)
 2512                 if_rele(mcifp);
 2513 
 2514         return (error);
 2515 }
 2516 
 2517 static void
 2518 vxlan_qflush(struct ifnet *ifp __unused)
 2519 {
 2520 }
 2521 
 2522 static void
 2523 vxlan_rcv_udp_packet(struct mbuf *m, int offset, struct inpcb *inpcb,
 2524     const struct sockaddr *srcsa, void *xvso)
 2525 {
 2526         struct vxlan_socket *vso;
 2527         struct vxlan_header *vxh, vxlanhdr;
 2528         uint32_t vni;
 2529         int error __unused;
 2530 
 2531         M_ASSERTPKTHDR(m);
 2532         vso = xvso;
 2533         offset += sizeof(struct udphdr);
 2534 
 2535         if (m->m_pkthdr.len < offset + sizeof(struct vxlan_header))
 2536                 goto out;
 2537 
 2538         if (__predict_false(m->m_len < offset + sizeof(struct vxlan_header))) {
 2539                 m_copydata(m, offset, sizeof(struct vxlan_header),
 2540                     (caddr_t) &vxlanhdr);
 2541                 vxh = &vxlanhdr;
 2542         } else
 2543                 vxh = mtodo(m, offset);
 2544 
 2545         /*
 2546          * Drop if there is a reserved bit set in either the flags or VNI
 2547          * fields of the header. This goes against the specification, but
 2548          * a bit set may indicate an unsupported new feature. This matches
 2549          * the behavior of the Linux implementation.
 2550          */
 2551         if (vxh->vxlh_flags != htonl(VXLAN_HDR_FLAGS_VALID_VNI) ||
 2552             vxh->vxlh_vni & ~htonl(VXLAN_VNI_MASK))
 2553                 goto out;
 2554 
 2555         vni = ntohl(vxh->vxlh_vni) >> VXLAN_HDR_VNI_SHIFT;
 2556 
 2557         /* Adjust to the start of the inner Ethernet frame. */
 2558         m_adj_decap(m, offset + sizeof(struct vxlan_header));
 2559 
 2560         error = vxlan_input(vso, vni, &m, srcsa);
 2561         MPASS(error != 0 || m == NULL);
 2562 
 2563 out:
 2564         if (m != NULL)
 2565                 m_freem(m);
 2566 }
 2567 
 2568 static int
 2569 vxlan_input(struct vxlan_socket *vso, uint32_t vni, struct mbuf **m0,
 2570     const struct sockaddr *sa)
 2571 {
 2572         struct vxlan_softc *sc;
 2573         struct ifnet *ifp;
 2574         struct mbuf *m;
 2575         struct ether_header *eh;
 2576         int error;
 2577 
 2578         sc = vxlan_socket_lookup_softc(vso, vni);
 2579         if (sc == NULL)
 2580                 return (ENOENT);
 2581 
 2582         ifp = sc->vxl_ifp;
 2583         m = *m0;
 2584         if (m->m_len < ETHER_HDR_LEN &&
 2585             (m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
 2586                 *m0 = NULL;
 2587                 error = ENOBUFS;
 2588                 goto out;
 2589         }
 2590         eh = mtod(m, struct ether_header *);
 2591 
 2592         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 2593                 error = ENETDOWN;
 2594                 goto out;
 2595         } else if (ifp == m->m_pkthdr.rcvif) {
 2596                 /* XXX Does not catch more complex loops. */
 2597                 error = EDEADLK;
 2598                 goto out;
 2599         }
 2600 
 2601         if (sc->vxl_flags & VXLAN_FLAG_LEARN)
 2602                 vxlan_ftable_learn(sc, sa, eh->ether_shost);
 2603 
 2604         m_clrprotoflags(m);
 2605         m->m_pkthdr.rcvif = ifp;
 2606         M_SETFIB(m, ifp->if_fib);
 2607 
 2608         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
 2609         error = netisr_queue_src(NETISR_ETHER, 0, m);
 2610         *m0 = NULL;
 2611 
 2612 out:
 2613         vxlan_release(sc);
 2614         return (error);
 2615 }
 2616 
 2617 static void
 2618 vxlan_set_default_config(struct vxlan_softc *sc)
 2619 {
 2620 
 2621         sc->vxl_flags |= VXLAN_FLAG_LEARN;
 2622 
 2623         sc->vxl_vni = VXLAN_VNI_MAX;
 2624         sc->vxl_ttl = IPDEFTTL;
 2625 
 2626         if (!vxlan_tunable_int(sc, "legacy_port", vxlan_legacy_port)) {
 2627                 sc->vxl_src_addr.in4.sin_port = htons(VXLAN_PORT);
 2628                 sc->vxl_dst_addr.in4.sin_port = htons(VXLAN_PORT);
 2629         } else {
 2630                 sc->vxl_src_addr.in4.sin_port = htons(VXLAN_LEGACY_PORT);
 2631                 sc->vxl_dst_addr.in4.sin_port = htons(VXLAN_LEGACY_PORT);
 2632         }
 2633 
 2634         sc->vxl_min_port = V_ipport_firstauto;
 2635         sc->vxl_max_port = V_ipport_lastauto;
 2636 
 2637         sc->vxl_ftable_max = VXLAN_FTABLE_MAX;
 2638         sc->vxl_ftable_timeout = VXLAN_FTABLE_TIMEOUT;
 2639 }
 2640 
 2641 static int
 2642 vxlan_set_user_config(struct vxlan_softc *sc, struct ifvxlanparam *vxlp)
 2643 {
 2644 
 2645 #ifndef INET
 2646         if (vxlp->vxlp_with & (VXLAN_PARAM_WITH_LOCAL_ADDR4 |
 2647             VXLAN_PARAM_WITH_REMOTE_ADDR4))
 2648                 return (EAFNOSUPPORT);
 2649 #endif
 2650 
 2651 #ifndef INET6
 2652         if (vxlp->vxlp_with & (VXLAN_PARAM_WITH_LOCAL_ADDR6 |
 2653             VXLAN_PARAM_WITH_REMOTE_ADDR6))
 2654                 return (EAFNOSUPPORT);
 2655 #else
 2656         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LOCAL_ADDR6) {
 2657                 int error = vxlan_sockaddr_in6_embedscope(&vxlp->vxlp_local_sa);
 2658                 if (error)
 2659                         return (error);
 2660         }
 2661         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_REMOTE_ADDR6) {
 2662                 int error = vxlan_sockaddr_in6_embedscope(
 2663                    &vxlp->vxlp_remote_sa);
 2664                 if (error)
 2665                         return (error);
 2666         }
 2667 #endif
 2668 
 2669         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_VNI) {
 2670                 if (vxlan_check_vni(vxlp->vxlp_vni) == 0)
 2671                         sc->vxl_vni = vxlp->vxlp_vni;
 2672         }
 2673 
 2674         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LOCAL_ADDR4) {
 2675                 sc->vxl_src_addr.in4.sin_len = sizeof(struct sockaddr_in);
 2676                 sc->vxl_src_addr.in4.sin_family = AF_INET;
 2677                 sc->vxl_src_addr.in4.sin_addr =
 2678                     vxlp->vxlp_local_sa.in4.sin_addr;
 2679         } else if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LOCAL_ADDR6) {
 2680                 sc->vxl_src_addr.in6.sin6_len = sizeof(struct sockaddr_in6);
 2681                 sc->vxl_src_addr.in6.sin6_family = AF_INET6;
 2682                 sc->vxl_src_addr.in6.sin6_addr =
 2683                     vxlp->vxlp_local_sa.in6.sin6_addr;
 2684         }
 2685 
 2686         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_REMOTE_ADDR4) {
 2687                 sc->vxl_dst_addr.in4.sin_len = sizeof(struct sockaddr_in);
 2688                 sc->vxl_dst_addr.in4.sin_family = AF_INET;
 2689                 sc->vxl_dst_addr.in4.sin_addr =
 2690                     vxlp->vxlp_remote_sa.in4.sin_addr;
 2691         } else if (vxlp->vxlp_with & VXLAN_PARAM_WITH_REMOTE_ADDR6) {
 2692                 sc->vxl_dst_addr.in6.sin6_len = sizeof(struct sockaddr_in6);
 2693                 sc->vxl_dst_addr.in6.sin6_family = AF_INET6;
 2694                 sc->vxl_dst_addr.in6.sin6_addr =
 2695                     vxlp->vxlp_remote_sa.in6.sin6_addr;
 2696         }
 2697 
 2698         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LOCAL_PORT)
 2699                 sc->vxl_src_addr.in4.sin_port = htons(vxlp->vxlp_local_port);
 2700         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_REMOTE_PORT)
 2701                 sc->vxl_dst_addr.in4.sin_port = htons(vxlp->vxlp_remote_port);
 2702 
 2703         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_PORT_RANGE) {
 2704                 if (vxlp->vxlp_min_port <= vxlp->vxlp_max_port) {
 2705                         sc->vxl_min_port = vxlp->vxlp_min_port;
 2706                         sc->vxl_max_port = vxlp->vxlp_max_port;
 2707                 }
 2708         }
 2709 
 2710         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_MULTICAST_IF)
 2711                 strlcpy(sc->vxl_mc_ifname, vxlp->vxlp_mc_ifname, IFNAMSIZ);
 2712 
 2713         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_FTABLE_TIMEOUT) {
 2714                 if (vxlan_check_ftable_timeout(vxlp->vxlp_ftable_timeout) == 0)
 2715                         sc->vxl_ftable_timeout = vxlp->vxlp_ftable_timeout;
 2716         }
 2717 
 2718         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_FTABLE_MAX) {
 2719                 if (vxlan_check_ftable_max(vxlp->vxlp_ftable_max) == 0)
 2720                         sc->vxl_ftable_max = vxlp->vxlp_ftable_max;
 2721         }
 2722 
 2723         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_TTL) {
 2724                 if (vxlan_check_ttl(vxlp->vxlp_ttl) == 0)
 2725                         sc->vxl_ttl = vxlp->vxlp_ttl;
 2726         }
 2727 
 2728         if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LEARN) {
 2729                 if (vxlp->vxlp_learn == 0)
 2730                         sc->vxl_flags &= ~VXLAN_FLAG_LEARN;
 2731         }
 2732 
 2733         return (0);
 2734 }
 2735 
 2736 static int
 2737 vxlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
 2738 {
 2739         struct vxlan_softc *sc;
 2740         struct ifnet *ifp;
 2741         struct ifvxlanparam vxlp;
 2742         int error;
 2743 
 2744         sc = malloc(sizeof(struct vxlan_softc), M_VXLAN, M_WAITOK | M_ZERO);
 2745         sc->vxl_unit = unit;
 2746         sc->vxl_fibnum = curthread->td_proc->p_fibnum;
 2747         vxlan_set_default_config(sc);
 2748 
 2749         if (params != 0) {
 2750                 error = copyin(params, &vxlp, sizeof(vxlp));
 2751                 if (error)
 2752                         goto fail;
 2753 
 2754                 error = vxlan_set_user_config(sc, &vxlp);
 2755                 if (error)
 2756                         goto fail;
 2757         }
 2758 
 2759         ifp = if_alloc(IFT_ETHER);
 2760         if (ifp == NULL) {
 2761                 error = ENOSPC;
 2762                 goto fail;
 2763         }
 2764 
 2765         sc->vxl_ifp = ifp;
 2766         rm_init(&sc->vxl_lock, "vxlanrm");
 2767         callout_init_rw(&sc->vxl_callout, &sc->vxl_lock, 0);
 2768         sc->vxl_port_hash_key = arc4random();
 2769         vxlan_ftable_init(sc);
 2770 
 2771         vxlan_sysctl_setup(sc);
 2772 
 2773         ifp->if_softc = sc;
 2774         if_initname(ifp, vxlan_name, unit);
 2775         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 2776         ifp->if_init = vxlan_init;
 2777         ifp->if_ioctl = vxlan_ioctl;
 2778         ifp->if_transmit = vxlan_transmit;
 2779         ifp->if_qflush = vxlan_qflush;
 2780         ifp->if_capabilities |= IFCAP_LINKSTATE;
 2781         ifp->if_capenable |= IFCAP_LINKSTATE;
 2782 
 2783         ifmedia_init(&sc->vxl_media, 0, vxlan_media_change, vxlan_media_status);
 2784         ifmedia_add(&sc->vxl_media, IFM_ETHER | IFM_AUTO, 0, NULL);
 2785         ifmedia_set(&sc->vxl_media, IFM_ETHER | IFM_AUTO);
 2786 
 2787         ether_gen_addr(ifp, &sc->vxl_hwaddr);
 2788         ether_ifattach(ifp, sc->vxl_hwaddr.octet);
 2789 
 2790         ifp->if_baudrate = 0;
 2791         ifp->if_hdrlen = 0;
 2792 
 2793         return (0);
 2794 
 2795 fail:
 2796         free(sc, M_VXLAN);
 2797         return (error);
 2798 }
 2799 
 2800 static void
 2801 vxlan_clone_destroy(struct ifnet *ifp)
 2802 {
 2803         struct vxlan_softc *sc;
 2804 
 2805         sc = ifp->if_softc;
 2806 
 2807         vxlan_teardown(sc);
 2808 
 2809         vxlan_ftable_flush(sc, 1);
 2810 
 2811         ether_ifdetach(ifp);
 2812         if_free(ifp);
 2813         ifmedia_removeall(&sc->vxl_media);
 2814 
 2815         vxlan_ftable_fini(sc);
 2816 
 2817         vxlan_sysctl_destroy(sc);
 2818         rm_destroy(&sc->vxl_lock);
 2819         free(sc, M_VXLAN);
 2820 }
 2821 
 2822 /* BMV: Taken from if_bridge. */
 2823 static uint32_t
 2824 vxlan_mac_hash(struct vxlan_softc *sc, const uint8_t *addr)
 2825 {
 2826         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->vxl_ftable_hash_key;
 2827 
 2828         b += addr[5] << 8;
 2829         b += addr[4];
 2830         a += addr[3] << 24;
 2831         a += addr[2] << 16;
 2832         a += addr[1] << 8;
 2833         a += addr[0];
 2834 
 2835 /*
 2836  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
 2837  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
 2838  */
 2839 #define mix(a, b, c)                                                    \
 2840 do {                                                                    \
 2841         a -= b; a -= c; a ^= (c >> 13);                                 \
 2842         b -= c; b -= a; b ^= (a << 8);                                  \
 2843         c -= a; c -= b; c ^= (b >> 13);                                 \
 2844         a -= b; a -= c; a ^= (c >> 12);                                 \
 2845         b -= c; b -= a; b ^= (a << 16);                                 \
 2846         c -= a; c -= b; c ^= (b >> 5);                                  \
 2847         a -= b; a -= c; a ^= (c >> 3);                                  \
 2848         b -= c; b -= a; b ^= (a << 10);                                 \
 2849         c -= a; c -= b; c ^= (b >> 15);                                 \
 2850 } while (0)
 2851 
 2852         mix(a, b, c);
 2853 
 2854 #undef mix
 2855 
 2856         return (c);
 2857 }
 2858 
 2859 static int
 2860 vxlan_media_change(struct ifnet *ifp)
 2861 {
 2862 
 2863         /* Ignore. */
 2864         return (0);
 2865 }
 2866 
 2867 static void
 2868 vxlan_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
 2869 {
 2870 
 2871         ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
 2872         ifmr->ifm_active = IFM_ETHER | IFM_FDX;
 2873 }
 2874 
 2875 static int
 2876 vxlan_sockaddr_cmp(const union vxlan_sockaddr *vxladdr,
 2877     const struct sockaddr *sa)
 2878 {
 2879 
 2880         return (bcmp(&vxladdr->sa, sa, vxladdr->sa.sa_len));
 2881 }
 2882 
 2883 static void
 2884 vxlan_sockaddr_copy(union vxlan_sockaddr *vxladdr,
 2885     const struct sockaddr *sa)
 2886 {
 2887 
 2888         MPASS(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
 2889         bzero(vxladdr, sizeof(*vxladdr));
 2890 
 2891         if (sa->sa_family == AF_INET) {
 2892                 vxladdr->in4 = *satoconstsin(sa);
 2893                 vxladdr->in4.sin_len = sizeof(struct sockaddr_in);
 2894         } else if (sa->sa_family == AF_INET6) {
 2895                 vxladdr->in6 = *satoconstsin6(sa);
 2896                 vxladdr->in6.sin6_len = sizeof(struct sockaddr_in6);
 2897         }
 2898 }
 2899 
 2900 static int
 2901 vxlan_sockaddr_in_equal(const union vxlan_sockaddr *vxladdr,
 2902     const struct sockaddr *sa)
 2903 {
 2904         int equal;
 2905 
 2906         if (sa->sa_family == AF_INET) {
 2907                 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
 2908                 equal = in4->s_addr == vxladdr->in4.sin_addr.s_addr;
 2909         } else if (sa->sa_family == AF_INET6) {
 2910                 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
 2911                 equal = IN6_ARE_ADDR_EQUAL(in6, &vxladdr->in6.sin6_addr);
 2912         } else
 2913                 equal = 0;
 2914 
 2915         return (equal);
 2916 }
 2917 
 2918 static void
 2919 vxlan_sockaddr_in_copy(union vxlan_sockaddr *vxladdr,
 2920     const struct sockaddr *sa)
 2921 {
 2922 
 2923         MPASS(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
 2924 
 2925         if (sa->sa_family == AF_INET) {
 2926                 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
 2927                 vxladdr->in4.sin_family = AF_INET;
 2928                 vxladdr->in4.sin_len = sizeof(struct sockaddr_in);
 2929                 vxladdr->in4.sin_addr = *in4;
 2930         } else if (sa->sa_family == AF_INET6) {
 2931                 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
 2932                 vxladdr->in6.sin6_family = AF_INET6;
 2933                 vxladdr->in6.sin6_len = sizeof(struct sockaddr_in6);
 2934                 vxladdr->in6.sin6_addr = *in6;
 2935         }
 2936 }
 2937 
 2938 static int
 2939 vxlan_sockaddr_supported(const union vxlan_sockaddr *vxladdr, int unspec)
 2940 {
 2941         const struct sockaddr *sa;
 2942         int supported;
 2943 
 2944         sa = &vxladdr->sa;
 2945         supported = 0;
 2946 
 2947         if (sa->sa_family == AF_UNSPEC && unspec != 0) {
 2948                 supported = 1;
 2949         } else if (sa->sa_family == AF_INET) {
 2950 #ifdef INET
 2951                 supported = 1;
 2952 #endif
 2953         } else if (sa->sa_family == AF_INET6) {
 2954 #ifdef INET6
 2955                 supported = 1;
 2956 #endif
 2957         }
 2958 
 2959         return (supported);
 2960 }
 2961 
 2962 static int
 2963 vxlan_sockaddr_in_any(const union vxlan_sockaddr *vxladdr)
 2964 {
 2965         const struct sockaddr *sa;
 2966         int any;
 2967 
 2968         sa = &vxladdr->sa;
 2969 
 2970         if (sa->sa_family == AF_INET) {
 2971                 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
 2972                 any = in4->s_addr == INADDR_ANY;
 2973         } else if (sa->sa_family == AF_INET6) {
 2974                 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
 2975                 any = IN6_IS_ADDR_UNSPECIFIED(in6);
 2976         } else
 2977                 any = -1;
 2978 
 2979         return (any);
 2980 }
 2981 
 2982 static int
 2983 vxlan_sockaddr_in_multicast(const union vxlan_sockaddr *vxladdr)
 2984 {
 2985         const struct sockaddr *sa;
 2986         int mc;
 2987 
 2988         sa = &vxladdr->sa;
 2989 
 2990         if (sa->sa_family == AF_INET) {
 2991                 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
 2992                 mc = IN_MULTICAST(ntohl(in4->s_addr));
 2993         } else if (sa->sa_family == AF_INET6) {
 2994                 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
 2995                 mc = IN6_IS_ADDR_MULTICAST(in6);
 2996         } else
 2997                 mc = -1;
 2998 
 2999         return (mc);
 3000 }
 3001 
 3002 static int
 3003 vxlan_sockaddr_in6_embedscope(union vxlan_sockaddr *vxladdr)
 3004 {
 3005         int error;
 3006 
 3007         MPASS(VXLAN_SOCKADDR_IS_IPV6(vxladdr));
 3008 #ifdef INET6
 3009         error = sa6_embedscope(&vxladdr->in6, V_ip6_use_defzone);
 3010 #else
 3011         error = EAFNOSUPPORT;
 3012 #endif
 3013 
 3014         return (error);
 3015 }
 3016 
 3017 static int
 3018 vxlan_can_change_config(struct vxlan_softc *sc)
 3019 {
 3020         struct ifnet *ifp;
 3021 
 3022         ifp = sc->vxl_ifp;
 3023         VXLAN_LOCK_ASSERT(sc);
 3024 
 3025         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 3026                 return (0);
 3027         if (sc->vxl_flags & (VXLAN_FLAG_INIT | VXLAN_FLAG_TEARDOWN))
 3028                 return (0);
 3029 
 3030         return (1);
 3031 }
 3032 
 3033 static int
 3034 vxlan_check_vni(uint32_t vni)
 3035 {
 3036 
 3037         return (vni >= VXLAN_VNI_MAX);
 3038 }
 3039 
 3040 static int
 3041 vxlan_check_ttl(int ttl)
 3042 {
 3043 
 3044         return (ttl > MAXTTL);
 3045 }
 3046 
 3047 static int
 3048 vxlan_check_ftable_timeout(uint32_t timeout)
 3049 {
 3050 
 3051         return (timeout > VXLAN_FTABLE_MAX_TIMEOUT);
 3052 }
 3053 
 3054 static int
 3055 vxlan_check_ftable_max(uint32_t max)
 3056 {
 3057 
 3058         return (max > VXLAN_FTABLE_MAX);
 3059 }
 3060 
 3061 static void
 3062 vxlan_sysctl_setup(struct vxlan_softc *sc)
 3063 {
 3064         struct sysctl_ctx_list *ctx;
 3065         struct sysctl_oid *node;
 3066         struct vxlan_statistics *stats;
 3067         char namebuf[8];
 3068 
 3069         ctx = &sc->vxl_sysctl_ctx;
 3070         stats = &sc->vxl_stats;
 3071         snprintf(namebuf, sizeof(namebuf), "%d", sc->vxl_unit);
 3072 
 3073         sysctl_ctx_init(ctx);
 3074         sc->vxl_sysctl_node = SYSCTL_ADD_NODE(ctx,
 3075             SYSCTL_STATIC_CHILDREN(_net_link_vxlan), OID_AUTO, namebuf,
 3076             CTLFLAG_RD, NULL, "");
 3077 
 3078         node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->vxl_sysctl_node),
 3079             OID_AUTO, "ftable", CTLFLAG_RD, NULL, "");
 3080         SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "count",
 3081             CTLFLAG_RD, &sc->vxl_ftable_cnt, 0,
 3082             "Number of entries in forwarding table");
 3083         SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "max",
 3084              CTLFLAG_RD, &sc->vxl_ftable_max, 0,
 3085             "Maximum number of entries allowed in forwarding table");
 3086         SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "timeout",
 3087             CTLFLAG_RD, &sc->vxl_ftable_timeout, 0,
 3088             "Number of seconds between prunes of the forwarding table");
 3089         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "dump",
 3090             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP,
 3091             sc, 0, vxlan_ftable_sysctl_dump, "A",
 3092             "Dump the forwarding table entries");
 3093 
 3094         node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->vxl_sysctl_node),
 3095             OID_AUTO, "stats", CTLFLAG_RD, NULL, "");
 3096         SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
 3097             "ftable_nospace", CTLFLAG_RD, &stats->ftable_nospace, 0,
 3098             "Fowarding table reached maximum entries");
 3099         SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
 3100             "ftable_lock_upgrade_failed", CTLFLAG_RD,
 3101             &stats->ftable_lock_upgrade_failed, 0,
 3102             "Forwarding table update required lock upgrade");
 3103 }
 3104 
 3105 static void
 3106 vxlan_sysctl_destroy(struct vxlan_softc *sc)
 3107 {
 3108 
 3109         sysctl_ctx_free(&sc->vxl_sysctl_ctx);
 3110         sc->vxl_sysctl_node = NULL;
 3111 }
 3112 
 3113 static int
 3114 vxlan_tunable_int(struct vxlan_softc *sc, const char *knob, int def)
 3115 {
 3116         char path[64];
 3117 
 3118         snprintf(path, sizeof(path), "net.link.vxlan.%d.%s",
 3119             sc->vxl_unit, knob);
 3120         TUNABLE_INT_FETCH(path, &def);
 3121 
 3122         return (def);
 3123 }
 3124 
 3125 static void
 3126 vxlan_ifdetach_event(void *arg __unused, struct ifnet *ifp)
 3127 {
 3128         struct vxlan_softc_head list;
 3129         struct vxlan_socket *vso;
 3130         struct vxlan_softc *sc, *tsc;
 3131 
 3132         LIST_INIT(&list);
 3133 
 3134         if (ifp->if_flags & IFF_RENAMING)
 3135                 return;
 3136         if ((ifp->if_flags & IFF_MULTICAST) == 0)
 3137                 return;
 3138 
 3139         VXLAN_LIST_LOCK();
 3140         LIST_FOREACH(vso, &vxlan_socket_list, vxlso_entry)
 3141                 vxlan_socket_ifdetach(vso, ifp, &list);
 3142         VXLAN_LIST_UNLOCK();
 3143 
 3144         LIST_FOREACH_SAFE(sc, &list, vxl_ifdetach_list, tsc) {
 3145                 LIST_REMOVE(sc, vxl_ifdetach_list);
 3146 
 3147                 VXLAN_WLOCK(sc);
 3148                 if (sc->vxl_flags & VXLAN_FLAG_INIT)
 3149                         vxlan_init_wait(sc);
 3150                 vxlan_teardown_locked(sc);
 3151         }
 3152 }
 3153 
 3154 static void
 3155 vxlan_load(void)
 3156 {
 3157 
 3158         mtx_init(&vxlan_list_mtx, "vxlan list", NULL, MTX_DEF);
 3159         LIST_INIT(&vxlan_socket_list);
 3160         vxlan_ifdetach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
 3161             vxlan_ifdetach_event, NULL, EVENTHANDLER_PRI_ANY);
 3162         vxlan_cloner = if_clone_simple(vxlan_name, vxlan_clone_create,
 3163             vxlan_clone_destroy, 0);
 3164 }
 3165 
 3166 static void
 3167 vxlan_unload(void)
 3168 {
 3169 
 3170         EVENTHANDLER_DEREGISTER(ifnet_departure_event,
 3171             vxlan_ifdetach_event_tag);
 3172         if_clone_detach(vxlan_cloner);
 3173         mtx_destroy(&vxlan_list_mtx);
 3174         MPASS(LIST_EMPTY(&vxlan_socket_list));
 3175 }
 3176 
 3177 static int
 3178 vxlan_modevent(module_t mod, int type, void *unused)
 3179 {
 3180         int error;
 3181 
 3182         error = 0;
 3183 
 3184         switch (type) {
 3185         case MOD_LOAD:
 3186                 vxlan_load();
 3187                 break;
 3188         case MOD_UNLOAD:
 3189                 vxlan_unload();
 3190                 break;
 3191         default:
 3192                 error = ENOTSUP;
 3193                 break;
 3194         }
 3195 
 3196         return (error);
 3197 }
 3198 
 3199 static moduledata_t vxlan_mod = {
 3200         "if_vxlan",
 3201         vxlan_modevent,
 3202         0
 3203 };
 3204 
 3205 DECLARE_MODULE(if_vxlan, vxlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
 3206 MODULE_VERSION(if_vxlan, 1);

Cache object: 0ab5e2870c86a8b058b78dbf5f8628f9


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.