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.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) 1980, 1986, 1993
    3  *      The Regents of the University of California.  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, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)if.c        8.5 (Berkeley) 1/9/95
   30  * $FreeBSD: releng/11.0/sys/net/if.c 303658 2016-08-02 13:57:20Z glebius $
   31  */
   32 
   33 #include "opt_compat.h"
   34 #include "opt_inet6.h"
   35 #include "opt_inet.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/types.h>
   39 #include <sys/conf.h>
   40 #include <sys/malloc.h>
   41 #include <sys/sbuf.h>
   42 #include <sys/bus.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/systm.h>
   45 #include <sys/priv.h>
   46 #include <sys/proc.h>
   47 #include <sys/socket.h>
   48 #include <sys/socketvar.h>
   49 #include <sys/protosw.h>
   50 #include <sys/kernel.h>
   51 #include <sys/lock.h>
   52 #include <sys/refcount.h>
   53 #include <sys/module.h>
   54 #include <sys/rwlock.h>
   55 #include <sys/sockio.h>
   56 #include <sys/syslog.h>
   57 #include <sys/sysctl.h>
   58 #include <sys/taskqueue.h>
   59 #include <sys/domain.h>
   60 #include <sys/jail.h>
   61 #include <sys/priv.h>
   62 
   63 #include <machine/stdarg.h>
   64 #include <vm/uma.h>
   65 
   66 #include <net/bpf.h>
   67 #include <net/ethernet.h>
   68 #include <net/if.h>
   69 #include <net/if_arp.h>
   70 #include <net/if_clone.h>
   71 #include <net/if_dl.h>
   72 #include <net/if_types.h>
   73 #include <net/if_var.h>
   74 #include <net/if_media.h>
   75 #include <net/if_vlan_var.h>
   76 #include <net/radix.h>
   77 #include <net/route.h>
   78 #include <net/vnet.h>
   79 
   80 #if defined(INET) || defined(INET6)
   81 #include <net/ethernet.h>
   82 #include <netinet/in.h>
   83 #include <netinet/in_var.h>
   84 #include <netinet/ip.h>
   85 #include <netinet/ip_carp.h>
   86 #ifdef INET
   87 #include <netinet/if_ether.h>
   88 #endif /* INET */
   89 #ifdef INET6
   90 #include <netinet6/in6_var.h>
   91 #include <netinet6/in6_ifattach.h>
   92 #endif /* INET6 */
   93 #endif /* INET || INET6 */
   94 
   95 #include <security/mac/mac_framework.h>
   96 
   97 #ifdef COMPAT_FREEBSD32
   98 #include <sys/mount.h>
   99 #include <compat/freebsd32/freebsd32.h>
  100 #endif
  101 
  102 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
  103 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
  104 
  105 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
  106     &ifqmaxlen, 0, "max send queue size");
  107 
  108 /* Log link state change events */
  109 static int log_link_state_change = 1;
  110 
  111 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
  112         &log_link_state_change, 0,
  113         "log interface link state change events");
  114 
  115 /* Log promiscuous mode change events */
  116 static int log_promisc_mode_change = 1;
  117 
  118 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
  119         &log_promisc_mode_change, 1,
  120         "log promiscuous mode change events");
  121 
  122 /* Interface description */
  123 static unsigned int ifdescr_maxlen = 1024;
  124 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
  125         &ifdescr_maxlen, 0,
  126         "administrative maximum length for interface description");
  127 
  128 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
  129 
  130 /* global sx for non-critical path ifdescr */
  131 static struct sx ifdescr_sx;
  132 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
  133 
  134 void    (*bridge_linkstate_p)(struct ifnet *ifp);
  135 void    (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
  136 void    (*lagg_linkstate_p)(struct ifnet *ifp, int state);
  137 /* These are external hooks for CARP. */
  138 void    (*carp_linkstate_p)(struct ifnet *ifp);
  139 void    (*carp_demote_adj_p)(int, char *);
  140 int     (*carp_master_p)(struct ifaddr *);
  141 #if defined(INET) || defined(INET6)
  142 int     (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
  143 int     (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
  144     const struct sockaddr *sa);
  145 int     (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);   
  146 int     (*carp_attach_p)(struct ifaddr *, int);
  147 void    (*carp_detach_p)(struct ifaddr *);
  148 #endif
  149 #ifdef INET
  150 int     (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
  151 #endif
  152 #ifdef INET6
  153 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
  154 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
  155     const struct in6_addr *taddr);
  156 #endif
  157 
  158 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
  159 
  160 /*
  161  * XXX: Style; these should be sorted alphabetically, and unprototyped
  162  * static functions should be prototyped. Currently they are sorted by
  163  * declaration order.
  164  */
  165 static void     if_attachdomain(void *);
  166 static void     if_attachdomain1(struct ifnet *);
  167 static int      ifconf(u_long, caddr_t);
  168 static void     if_freemulti(struct ifmultiaddr *);
  169 static void     if_grow(void);
  170 static void     if_input_default(struct ifnet *, struct mbuf *);
  171 static int      if_requestencap_default(struct ifnet *, struct if_encap_req *);
  172 static void     if_route(struct ifnet *, int flag, int fam);
  173 static int      if_setflag(struct ifnet *, int, int, int *, int);
  174 static int      if_transmit(struct ifnet *ifp, struct mbuf *m);
  175 static void     if_unroute(struct ifnet *, int flag, int fam);
  176 static void     link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
  177 static int      ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
  178 static int      if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
  179 static void     do_link_state_change(void *, int);
  180 static int      if_getgroup(struct ifgroupreq *, struct ifnet *);
  181 static int      if_getgroupmembers(struct ifgroupreq *);
  182 static void     if_delgroups(struct ifnet *);
  183 static void     if_attach_internal(struct ifnet *, int, struct if_clone *);
  184 static int      if_detach_internal(struct ifnet *, int, struct if_clone **);
  185 #ifdef VIMAGE
  186 static void     if_vmove(struct ifnet *, struct vnet *);
  187 #endif
  188 
  189 #ifdef INET6
  190 /*
  191  * XXX: declare here to avoid to include many inet6 related files..
  192  * should be more generalized?
  193  */
  194 extern void     nd6_setmtu(struct ifnet *);
  195 #endif
  196 
  197 /* ipsec helper hooks */
  198 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
  199 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
  200 
  201 VNET_DEFINE(int, if_index);
  202 int     ifqmaxlen = IFQ_MAXLEN;
  203 VNET_DEFINE(struct ifnethead, ifnet);   /* depend on static init XXX */
  204 VNET_DEFINE(struct ifgrouphead, ifg_head);
  205 
  206 static VNET_DEFINE(int, if_indexlim) = 8;
  207 
  208 /* Table of ifnet by index. */
  209 VNET_DEFINE(struct ifnet **, ifindex_table);
  210 
  211 #define V_if_indexlim           VNET(if_indexlim)
  212 #define V_ifindex_table         VNET(ifindex_table)
  213 
  214 /*
  215  * The global network interface list (V_ifnet) and related state (such as
  216  * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
  217  * an rwlock.  Either may be acquired shared to stablize the list, but both
  218  * must be acquired writable to modify the list.  This model allows us to
  219  * both stablize the interface list during interrupt thread processing, but
  220  * also to stablize it over long-running ioctls, without introducing priority
  221  * inversions and deadlocks.
  222  */
  223 struct rwlock ifnet_rwlock;
  224 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
  225 struct sx ifnet_sxlock;
  226 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
  227 
  228 /*
  229  * The allocation of network interfaces is a rather non-atomic affair; we
  230  * need to select an index before we are ready to expose the interface for
  231  * use, so will use this pointer value to indicate reservation.
  232  */
  233 #define IFNET_HOLD      (void *)(uintptr_t)(-1)
  234 
  235 static  if_com_alloc_t *if_com_alloc[256];
  236 static  if_com_free_t *if_com_free[256];
  237 
  238 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
  239 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
  240 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
  241 
  242 struct ifnet *
  243 ifnet_byindex_locked(u_short idx)
  244 {
  245 
  246         if (idx > V_if_index)
  247                 return (NULL);
  248         if (V_ifindex_table[idx] == IFNET_HOLD)
  249                 return (NULL);
  250         return (V_ifindex_table[idx]);
  251 }
  252 
  253 struct ifnet *
  254 ifnet_byindex(u_short idx)
  255 {
  256         struct ifnet *ifp;
  257 
  258         IFNET_RLOCK_NOSLEEP();
  259         ifp = ifnet_byindex_locked(idx);
  260         IFNET_RUNLOCK_NOSLEEP();
  261         return (ifp);
  262 }
  263 
  264 struct ifnet *
  265 ifnet_byindex_ref(u_short idx)
  266 {
  267         struct ifnet *ifp;
  268 
  269         IFNET_RLOCK_NOSLEEP();
  270         ifp = ifnet_byindex_locked(idx);
  271         if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
  272                 IFNET_RUNLOCK_NOSLEEP();
  273                 return (NULL);
  274         }
  275         if_ref(ifp);
  276         IFNET_RUNLOCK_NOSLEEP();
  277         return (ifp);
  278 }
  279 
  280 /*
  281  * Allocate an ifindex array entry; return 0 on success or an error on
  282  * failure.
  283  */
  284 static u_short
  285 ifindex_alloc(void)
  286 {
  287         u_short idx;
  288 
  289         IFNET_WLOCK_ASSERT();
  290 retry:
  291         /*
  292          * Try to find an empty slot below V_if_index.  If we fail, take the
  293          * next slot.
  294          */
  295         for (idx = 1; idx <= V_if_index; idx++) {
  296                 if (V_ifindex_table[idx] == NULL)
  297                         break;
  298         }
  299 
  300         /* Catch if_index overflow. */
  301         if (idx >= V_if_indexlim) {
  302                 if_grow();
  303                 goto retry;
  304         }
  305         if (idx > V_if_index)
  306                 V_if_index = idx;
  307         return (idx);
  308 }
  309 
  310 static void
  311 ifindex_free_locked(u_short idx)
  312 {
  313 
  314         IFNET_WLOCK_ASSERT();
  315 
  316         V_ifindex_table[idx] = NULL;
  317         while (V_if_index > 0 &&
  318             V_ifindex_table[V_if_index] == NULL)
  319                 V_if_index--;
  320 }
  321 
  322 static void
  323 ifindex_free(u_short idx)
  324 {
  325 
  326         IFNET_WLOCK();
  327         ifindex_free_locked(idx);
  328         IFNET_WUNLOCK();
  329 }
  330 
  331 static void
  332 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
  333 {
  334 
  335         IFNET_WLOCK_ASSERT();
  336 
  337         V_ifindex_table[idx] = ifp;
  338 }
  339 
  340 static void
  341 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
  342 {
  343 
  344         IFNET_WLOCK();
  345         ifnet_setbyindex_locked(idx, ifp);
  346         IFNET_WUNLOCK();
  347 }
  348 
  349 struct ifaddr *
  350 ifaddr_byindex(u_short idx)
  351 {
  352         struct ifnet *ifp;
  353         struct ifaddr *ifa = NULL;
  354 
  355         IFNET_RLOCK_NOSLEEP();
  356         ifp = ifnet_byindex_locked(idx);
  357         if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
  358                 ifa_ref(ifa);
  359         IFNET_RUNLOCK_NOSLEEP();
  360         return (ifa);
  361 }
  362 
  363 /*
  364  * Network interface utility routines.
  365  *
  366  * Routines with ifa_ifwith* names take sockaddr *'s as
  367  * parameters.
  368  */
  369 
  370 static void
  371 vnet_if_init(const void *unused __unused)
  372 {
  373 
  374         TAILQ_INIT(&V_ifnet);
  375         TAILQ_INIT(&V_ifg_head);
  376         IFNET_WLOCK();
  377         if_grow();                              /* create initial table */
  378         IFNET_WUNLOCK();
  379         vnet_if_clone_init();
  380 }
  381 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
  382     NULL);
  383 
  384 #ifdef VIMAGE
  385 static void
  386 vnet_if_uninit(const void *unused __unused)
  387 {
  388 
  389         VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
  390             "not empty", __func__, __LINE__, &V_ifnet));
  391         VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
  392             "not empty", __func__, __LINE__, &V_ifg_head));
  393 
  394         free((caddr_t)V_ifindex_table, M_IFNET);
  395 }
  396 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
  397     vnet_if_uninit, NULL);
  398 
  399 static void
  400 vnet_if_return(const void *unused __unused)
  401 {
  402         struct ifnet *ifp, *nifp;
  403 
  404         /* Return all inherited interfaces to their parent vnets. */
  405         TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
  406                 if (ifp->if_home_vnet != ifp->if_vnet)
  407                         if_vmove(ifp, ifp->if_home_vnet);
  408         }
  409 }
  410 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
  411     vnet_if_return, NULL);
  412 #endif
  413 
  414 static void
  415 if_grow(void)
  416 {
  417         int oldlim;
  418         u_int n;
  419         struct ifnet **e;
  420 
  421         IFNET_WLOCK_ASSERT();
  422         oldlim = V_if_indexlim;
  423         IFNET_WUNLOCK();
  424         n = (oldlim << 1) * sizeof(*e);
  425         e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
  426         IFNET_WLOCK();
  427         if (V_if_indexlim != oldlim) {
  428                 free(e, M_IFNET);
  429                 return;
  430         }
  431         if (V_ifindex_table != NULL) {
  432                 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
  433                 free((caddr_t)V_ifindex_table, M_IFNET);
  434         }
  435         V_if_indexlim <<= 1;
  436         V_ifindex_table = e;
  437 }
  438 
  439 /*
  440  * Allocate a struct ifnet and an index for an interface.  A layer 2
  441  * common structure will also be allocated if an allocation routine is
  442  * registered for the passed type.
  443  */
  444 struct ifnet *
  445 if_alloc(u_char type)
  446 {
  447         struct ifnet *ifp;
  448         u_short idx;
  449 
  450         ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
  451         IFNET_WLOCK();
  452         idx = ifindex_alloc();
  453         ifnet_setbyindex_locked(idx, IFNET_HOLD);
  454         IFNET_WUNLOCK();
  455         ifp->if_index = idx;
  456         ifp->if_type = type;
  457         ifp->if_alloctype = type;
  458 #ifdef VIMAGE
  459         ifp->if_vnet = curvnet;
  460 #endif
  461         if (if_com_alloc[type] != NULL) {
  462                 ifp->if_l2com = if_com_alloc[type](type, ifp);
  463                 if (ifp->if_l2com == NULL) {
  464                         free(ifp, M_IFNET);
  465                         ifindex_free(idx);
  466                         return (NULL);
  467                 }
  468         }
  469 
  470         IF_ADDR_LOCK_INIT(ifp);
  471         TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
  472         ifp->if_afdata_initialized = 0;
  473         IF_AFDATA_LOCK_INIT(ifp);
  474         TAILQ_INIT(&ifp->if_addrhead);
  475         TAILQ_INIT(&ifp->if_multiaddrs);
  476         TAILQ_INIT(&ifp->if_groups);
  477 #ifdef MAC
  478         mac_ifnet_init(ifp);
  479 #endif
  480         ifq_init(&ifp->if_snd, ifp);
  481 
  482         refcount_init(&ifp->if_refcount, 1);    /* Index reference. */
  483         for (int i = 0; i < IFCOUNTERS; i++)
  484                 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
  485         ifp->if_get_counter = if_get_counter_default;
  486         ifnet_setbyindex(ifp->if_index, ifp);
  487         return (ifp);
  488 }
  489 
  490 /*
  491  * Do the actual work of freeing a struct ifnet, and layer 2 common
  492  * structure.  This call is made when the last reference to an
  493  * interface is released.
  494  */
  495 static void
  496 if_free_internal(struct ifnet *ifp)
  497 {
  498 
  499         KASSERT((ifp->if_flags & IFF_DYING),
  500             ("if_free_internal: interface not dying"));
  501 
  502         if (if_com_free[ifp->if_alloctype] != NULL)
  503                 if_com_free[ifp->if_alloctype](ifp->if_l2com,
  504                     ifp->if_alloctype);
  505 
  506 #ifdef MAC
  507         mac_ifnet_destroy(ifp);
  508 #endif /* MAC */
  509         if (ifp->if_description != NULL)
  510                 free(ifp->if_description, M_IFDESCR);
  511         IF_AFDATA_DESTROY(ifp);
  512         IF_ADDR_LOCK_DESTROY(ifp);
  513         ifq_delete(&ifp->if_snd);
  514 
  515         for (int i = 0; i < IFCOUNTERS; i++)
  516                 counter_u64_free(ifp->if_counters[i]);
  517 
  518         free(ifp, M_IFNET);
  519 }
  520 
  521 /*
  522  * Deregister an interface and free the associated storage.
  523  */
  524 void
  525 if_free(struct ifnet *ifp)
  526 {
  527 
  528         ifp->if_flags |= IFF_DYING;                     /* XXX: Locking */
  529 
  530         CURVNET_SET_QUIET(ifp->if_vnet);
  531         IFNET_WLOCK();
  532         KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
  533             ("%s: freeing unallocated ifnet", ifp->if_xname));
  534 
  535         ifindex_free_locked(ifp->if_index);
  536         IFNET_WUNLOCK();
  537 
  538         if (refcount_release(&ifp->if_refcount))
  539                 if_free_internal(ifp);
  540         CURVNET_RESTORE();
  541 }
  542 
  543 /*
  544  * Interfaces to keep an ifnet type-stable despite the possibility of the
  545  * driver calling if_free().  If there are additional references, we defer
  546  * freeing the underlying data structure.
  547  */
  548 void
  549 if_ref(struct ifnet *ifp)
  550 {
  551 
  552         /* We don't assert the ifnet list lock here, but arguably should. */
  553         refcount_acquire(&ifp->if_refcount);
  554 }
  555 
  556 void
  557 if_rele(struct ifnet *ifp)
  558 {
  559 
  560         if (!refcount_release(&ifp->if_refcount))
  561                 return;
  562         if_free_internal(ifp);
  563 }
  564 
  565 void
  566 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
  567 {
  568         
  569         mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
  570 
  571         if (ifq->ifq_maxlen == 0) 
  572                 ifq->ifq_maxlen = ifqmaxlen;
  573 
  574         ifq->altq_type = 0;
  575         ifq->altq_disc = NULL;
  576         ifq->altq_flags &= ALTQF_CANTCHANGE;
  577         ifq->altq_tbr  = NULL;
  578         ifq->altq_ifp  = ifp;
  579 }
  580 
  581 void
  582 ifq_delete(struct ifaltq *ifq)
  583 {
  584         mtx_destroy(&ifq->ifq_mtx);
  585 }
  586 
  587 /*
  588  * Perform generic interface initialization tasks and attach the interface
  589  * to the list of "active" interfaces.  If vmove flag is set on entry
  590  * to if_attach_internal(), perform only a limited subset of initialization
  591  * tasks, given that we are moving from one vnet to another an ifnet which
  592  * has already been fully initialized.
  593  *
  594  * Note that if_detach_internal() removes group membership unconditionally
  595  * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
  596  * Thus, when if_vmove() is applied to a cloned interface, group membership
  597  * is lost while a cloned one always joins a group whose name is
  598  * ifc->ifc_name.  To recover this after if_detach_internal() and
  599  * if_attach_internal(), the cloner should be specified to
  600  * if_attach_internal() via ifc.  If it is non-NULL, if_attach_internal()
  601  * attempts to join a group whose name is ifc->ifc_name.
  602  *
  603  * XXX:
  604  *  - The decision to return void and thus require this function to
  605  *    succeed is questionable.
  606  *  - We should probably do more sanity checking.  For instance we don't
  607  *    do anything to insure if_xname is unique or non-empty.
  608  */
  609 void
  610 if_attach(struct ifnet *ifp)
  611 {
  612 
  613         if_attach_internal(ifp, 0, NULL);
  614 }
  615 
  616 /*
  617  * Compute the least common TSO limit.
  618  */
  619 void
  620 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
  621 {
  622         /*
  623          * 1) If there is no limit currently, take the limit from
  624          * the network adapter.
  625          *
  626          * 2) If the network adapter has a limit below the current
  627          * limit, apply it.
  628          */
  629         if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
  630             ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
  631                 pmax->tsomaxbytes = ifp->if_hw_tsomax;
  632         }
  633         if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
  634             ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
  635                 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
  636         }
  637         if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
  638             ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
  639                 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
  640         }
  641 }
  642 
  643 /*
  644  * Update TSO limit of a network adapter.
  645  *
  646  * Returns zero if no change. Else non-zero.
  647  */
  648 int
  649 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
  650 {
  651         int retval = 0;
  652         if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
  653                 ifp->if_hw_tsomax = pmax->tsomaxbytes;
  654                 retval++;
  655         }
  656         if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
  657                 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
  658                 retval++;
  659         }
  660         if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
  661                 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
  662                 retval++;
  663         }
  664         return (retval);
  665 }
  666 
  667 static void
  668 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
  669 {
  670         unsigned socksize, ifasize;
  671         int namelen, masklen;
  672         struct sockaddr_dl *sdl;
  673         struct ifaddr *ifa;
  674 
  675         if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
  676                 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
  677                     ifp->if_xname);
  678 
  679 #ifdef VIMAGE
  680         ifp->if_vnet = curvnet;
  681         if (ifp->if_home_vnet == NULL)
  682                 ifp->if_home_vnet = curvnet;
  683 #endif
  684 
  685         if_addgroup(ifp, IFG_ALL);
  686 
  687         /* Restore group membership for cloned interfaces. */
  688         if (vmove && ifc != NULL)
  689                 if_clone_addgroup(ifp, ifc);
  690 
  691         getmicrotime(&ifp->if_lastchange);
  692         ifp->if_epoch = time_uptime;
  693 
  694         KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
  695             (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
  696             ("transmit and qflush must both either be set or both be NULL"));
  697         if (ifp->if_transmit == NULL) {
  698                 ifp->if_transmit = if_transmit;
  699                 ifp->if_qflush = if_qflush;
  700         }
  701         if (ifp->if_input == NULL)
  702                 ifp->if_input = if_input_default;
  703 
  704         if (ifp->if_requestencap == NULL)
  705                 ifp->if_requestencap = if_requestencap_default;
  706 
  707         if (!vmove) {
  708 #ifdef MAC
  709                 mac_ifnet_create(ifp);
  710 #endif
  711 
  712                 /*
  713                  * Create a Link Level name for this device.
  714                  */
  715                 namelen = strlen(ifp->if_xname);
  716                 /*
  717                  * Always save enough space for any possiable name so we
  718                  * can do a rename in place later.
  719                  */
  720                 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
  721                 socksize = masklen + ifp->if_addrlen;
  722                 if (socksize < sizeof(*sdl))
  723                         socksize = sizeof(*sdl);
  724                 socksize = roundup2(socksize, sizeof(long));
  725                 ifasize = sizeof(*ifa) + 2 * socksize;
  726                 ifa = ifa_alloc(ifasize, M_WAITOK);
  727                 sdl = (struct sockaddr_dl *)(ifa + 1);
  728                 sdl->sdl_len = socksize;
  729                 sdl->sdl_family = AF_LINK;
  730                 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
  731                 sdl->sdl_nlen = namelen;
  732                 sdl->sdl_index = ifp->if_index;
  733                 sdl->sdl_type = ifp->if_type;
  734                 ifp->if_addr = ifa;
  735                 ifa->ifa_ifp = ifp;
  736                 ifa->ifa_rtrequest = link_rtrequest;
  737                 ifa->ifa_addr = (struct sockaddr *)sdl;
  738                 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
  739                 ifa->ifa_netmask = (struct sockaddr *)sdl;
  740                 sdl->sdl_len = masklen;
  741                 while (namelen != 0)
  742                         sdl->sdl_data[--namelen] = 0xff;
  743                 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
  744                 /* Reliably crash if used uninitialized. */
  745                 ifp->if_broadcastaddr = NULL;
  746 
  747 #if defined(INET) || defined(INET6)
  748                 /* Use defaults for TSO, if nothing is set */
  749                 if (ifp->if_hw_tsomax == 0 &&
  750                     ifp->if_hw_tsomaxsegcount == 0 &&
  751                     ifp->if_hw_tsomaxsegsize == 0) {
  752                         /*
  753                          * The TSO defaults needs to be such that an
  754                          * NFS mbuf list of 35 mbufs totalling just
  755                          * below 64K works and that a chain of mbufs
  756                          * can be defragged into at most 32 segments:
  757                          */
  758                         ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
  759                             (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
  760                         ifp->if_hw_tsomaxsegcount = 35;
  761                         ifp->if_hw_tsomaxsegsize = 2048;        /* 2K */
  762 
  763                         /* XXX some drivers set IFCAP_TSO after ethernet attach */
  764                         if (ifp->if_capabilities & IFCAP_TSO) {
  765                                 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
  766                                     ifp->if_hw_tsomax,
  767                                     ifp->if_hw_tsomaxsegcount,
  768                                     ifp->if_hw_tsomaxsegsize);
  769                         }
  770                 }
  771 #endif
  772         }
  773 #ifdef VIMAGE
  774         else {
  775                 /*
  776                  * Update the interface index in the link layer address
  777                  * of the interface.
  778                  */
  779                 for (ifa = ifp->if_addr; ifa != NULL;
  780                     ifa = TAILQ_NEXT(ifa, ifa_link)) {
  781                         if (ifa->ifa_addr->sa_family == AF_LINK) {
  782                                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
  783                                 sdl->sdl_index = ifp->if_index;
  784                         }
  785                 }
  786         }
  787 #endif
  788 
  789         IFNET_WLOCK();
  790         TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
  791 #ifdef VIMAGE
  792         curvnet->vnet_ifcnt++;
  793 #endif
  794         IFNET_WUNLOCK();
  795 
  796         if (domain_init_status >= 2)
  797                 if_attachdomain1(ifp);
  798 
  799         EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
  800         if (IS_DEFAULT_VNET(curvnet))
  801                 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
  802 
  803         /* Announce the interface. */
  804         rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
  805 }
  806 
  807 static void
  808 if_attachdomain(void *dummy)
  809 {
  810         struct ifnet *ifp;
  811 
  812         TAILQ_FOREACH(ifp, &V_ifnet, if_link)
  813                 if_attachdomain1(ifp);
  814 }
  815 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
  816     if_attachdomain, NULL);
  817 
  818 static void
  819 if_attachdomain1(struct ifnet *ifp)
  820 {
  821         struct domain *dp;
  822 
  823         /*
  824          * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
  825          * cannot lock ifp->if_afdata initialization, entirely.
  826          */
  827         IF_AFDATA_LOCK(ifp);
  828         if (ifp->if_afdata_initialized >= domain_init_status) {
  829                 IF_AFDATA_UNLOCK(ifp);
  830                 log(LOG_WARNING, "%s called more than once on %s\n",
  831                     __func__, ifp->if_xname);
  832                 return;
  833         }
  834         ifp->if_afdata_initialized = domain_init_status;
  835         IF_AFDATA_UNLOCK(ifp);
  836 
  837         /* address family dependent data region */
  838         bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
  839         for (dp = domains; dp; dp = dp->dom_next) {
  840                 if (dp->dom_ifattach)
  841                         ifp->if_afdata[dp->dom_family] =
  842                             (*dp->dom_ifattach)(ifp);
  843         }
  844 }
  845 
  846 /*
  847  * Remove any unicast or broadcast network addresses from an interface.
  848  */
  849 void
  850 if_purgeaddrs(struct ifnet *ifp)
  851 {
  852         struct ifaddr *ifa, *next;
  853 
  854         /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
  855         TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
  856                 if (ifa->ifa_addr->sa_family == AF_LINK)
  857                         continue;
  858 #ifdef INET
  859                 /* XXX: Ugly!! ad hoc just for INET */
  860                 if (ifa->ifa_addr->sa_family == AF_INET) {
  861                         struct ifaliasreq ifr;
  862 
  863                         bzero(&ifr, sizeof(ifr));
  864                         ifr.ifra_addr = *ifa->ifa_addr;
  865                         if (ifa->ifa_dstaddr)
  866                                 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
  867                         if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
  868                             NULL) == 0)
  869                                 continue;
  870                 }
  871 #endif /* INET */
  872 #ifdef INET6
  873                 if (ifa->ifa_addr->sa_family == AF_INET6) {
  874                         in6_purgeaddr(ifa);
  875                         /* ifp_addrhead is already updated */
  876                         continue;
  877                 }
  878 #endif /* INET6 */
  879                 IF_ADDR_WLOCK(ifp);
  880                 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
  881                 IF_ADDR_WUNLOCK(ifp);
  882                 ifa_free(ifa);
  883         }
  884 }
  885 
  886 /*
  887  * Remove any multicast network addresses from an interface when an ifnet
  888  * is going away.
  889  */
  890 static void
  891 if_purgemaddrs(struct ifnet *ifp)
  892 {
  893         struct ifmultiaddr *ifma;
  894         struct ifmultiaddr *next;
  895 
  896         IF_ADDR_WLOCK(ifp);
  897         TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
  898                 if_delmulti_locked(ifp, ifma, 1);
  899         IF_ADDR_WUNLOCK(ifp);
  900 }
  901 
  902 /*
  903  * Detach an interface, removing it from the list of "active" interfaces.
  904  * If vmove flag is set on entry to if_detach_internal(), perform only a
  905  * limited subset of cleanup tasks, given that we are moving an ifnet from
  906  * one vnet to another, where it must be fully operational.
  907  *
  908  * XXXRW: There are some significant questions about event ordering, and
  909  * how to prevent things from starting to use the interface during detach.
  910  */
  911 void
  912 if_detach(struct ifnet *ifp)
  913 {
  914 
  915         CURVNET_SET_QUIET(ifp->if_vnet);
  916         if_detach_internal(ifp, 0, NULL);
  917         CURVNET_RESTORE();
  918 }
  919 
  920 /*
  921  * The vmove flag, if set, indicates that we are called from a callpath
  922  * that is moving an interface to a different vnet instance.
  923  *
  924  * The shutdown flag, if set, indicates that we are called in the
  925  * process of shutting down a vnet instance.  Currently only the
  926  * vnet_if_return SYSUNINIT function sets it.  Note: we can be called
  927  * on a vnet instance shutdown without this flag being set, e.g., when
  928  * the cloned interfaces are destoyed as first thing of teardown.
  929  */
  930 static int
  931 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
  932 {
  933         struct ifaddr *ifa;
  934         int i;
  935         struct domain *dp;
  936         struct ifnet *iter;
  937         int found = 0;
  938 #ifdef VIMAGE
  939         int shutdown;
  940 
  941         shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
  942                  ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
  943 #endif
  944         IFNET_WLOCK();
  945         TAILQ_FOREACH(iter, &V_ifnet, if_link)
  946                 if (iter == ifp) {
  947                         TAILQ_REMOVE(&V_ifnet, ifp, if_link);
  948                         found = 1;
  949                         break;
  950                 }
  951         IFNET_WUNLOCK();
  952         if (!found) {
  953                 /*
  954                  * While we would want to panic here, we cannot
  955                  * guarantee that the interface is indeed still on
  956                  * the list given we don't hold locks all the way.
  957                  */
  958                 return (ENOENT);
  959 #if 0
  960                 if (vmove)
  961                         panic("%s: ifp=%p not on the ifnet tailq %p",
  962                             __func__, ifp, &V_ifnet);
  963                 else
  964                         return; /* XXX this should panic as well? */
  965 #endif
  966         }
  967 
  968         /*
  969          * At this point we know the interface still was on the ifnet list
  970          * and we removed it so we are in a stable state.
  971          */
  972 #ifdef VIMAGE
  973         curvnet->vnet_ifcnt--;
  974 #endif
  975 
  976         /*
  977          * In any case (destroy or vmove) detach us from the groups
  978          * and remove/wait for pending events on the taskq.
  979          * XXX-BZ in theory an interface could still enqueue a taskq change?
  980          */
  981         if_delgroups(ifp);
  982 
  983         taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
  984 
  985         /*
  986          * Check if this is a cloned interface or not. Must do even if
  987          * shutting down as a if_vmove_reclaim() would move the ifp and
  988          * the if_clone_addgroup() will have a corrupted string overwise
  989          * from a gibberish pointer.
  990          */
  991         if (vmove && ifcp != NULL)
  992                 *ifcp = if_clone_findifc(ifp);
  993 
  994         if_down(ifp);
  995 
  996 #ifdef VIMAGE
  997         /*
  998          * On VNET shutdown abort here as the stack teardown will do all
  999          * the work top-down for us.
 1000          */
 1001         if (shutdown) {
 1002                 /*
 1003                  * In case of a vmove we are done here without error.
 1004                  * If we would signal an error it would lead to the same
 1005                  * abort as if we did not find the ifnet anymore.
 1006                  * if_detach() calls us in void context and does not care
 1007                  * about an early abort notification, so life is splendid :)
 1008                  */
 1009                 goto finish_vnet_shutdown;
 1010         }
 1011 #endif
 1012 
 1013         /*
 1014          * At this point we are not tearing down a VNET and are either
 1015          * going to destroy or vmove the interface and have to cleanup
 1016          * accordingly.
 1017          */
 1018 
 1019         /*
 1020          * Remove routes and flush queues.
 1021          */
 1022 #ifdef ALTQ
 1023         if (ALTQ_IS_ENABLED(&ifp->if_snd))
 1024                 altq_disable(&ifp->if_snd);
 1025         if (ALTQ_IS_ATTACHED(&ifp->if_snd))
 1026                 altq_detach(&ifp->if_snd);
 1027 #endif
 1028 
 1029         if_purgeaddrs(ifp);
 1030 
 1031 #ifdef INET
 1032         in_ifdetach(ifp);
 1033 #endif
 1034 
 1035 #ifdef INET6
 1036         /*
 1037          * Remove all IPv6 kernel structs related to ifp.  This should be done
 1038          * before removing routing entries below, since IPv6 interface direct
 1039          * routes are expected to be removed by the IPv6-specific kernel API.
 1040          * Otherwise, the kernel will detect some inconsistency and bark it.
 1041          */
 1042         in6_ifdetach(ifp);
 1043 #endif
 1044         if_purgemaddrs(ifp);
 1045 
 1046         /* Announce that the interface is gone. */
 1047         rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
 1048         EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
 1049         if (IS_DEFAULT_VNET(curvnet))
 1050                 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
 1051 
 1052         if (!vmove) {
 1053                 /*
 1054                  * Prevent further calls into the device driver via ifnet.
 1055                  */
 1056                 if_dead(ifp);
 1057 
 1058                 /*
 1059                  * Remove link ifaddr pointer and maybe decrement if_index.
 1060                  * Clean up all addresses.
 1061                  */
 1062                 ifp->if_addr = NULL;
 1063 
 1064                 /* We can now free link ifaddr. */
 1065                 IF_ADDR_WLOCK(ifp);
 1066                 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
 1067                         ifa = TAILQ_FIRST(&ifp->if_addrhead);
 1068                         TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
 1069                         IF_ADDR_WUNLOCK(ifp);
 1070                         ifa_free(ifa);
 1071                 } else
 1072                         IF_ADDR_WUNLOCK(ifp);
 1073         }
 1074 
 1075         rt_flushifroutes(ifp);
 1076 
 1077 #ifdef VIMAGE
 1078 finish_vnet_shutdown:
 1079 #endif
 1080         /*
 1081          * We cannot hold the lock over dom_ifdetach calls as they might
 1082          * sleep, for example trying to drain a callout, thus open up the
 1083          * theoretical race with re-attaching.
 1084          */
 1085         IF_AFDATA_LOCK(ifp);
 1086         i = ifp->if_afdata_initialized;
 1087         ifp->if_afdata_initialized = 0;
 1088         IF_AFDATA_UNLOCK(ifp);
 1089         for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
 1090                 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
 1091                         (*dp->dom_ifdetach)(ifp,
 1092                             ifp->if_afdata[dp->dom_family]);
 1093                         ifp->if_afdata[dp->dom_family] = NULL;
 1094                 }
 1095         }
 1096 
 1097         return (0);
 1098 }
 1099 
 1100 #ifdef VIMAGE
 1101 /*
 1102  * if_vmove() performs a limited version of if_detach() in current
 1103  * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
 1104  * An attempt is made to shrink if_index in current vnet, find an
 1105  * unused if_index in target vnet and calls if_grow() if necessary,
 1106  * and finally find an unused if_xname for the target vnet.
 1107  */
 1108 static void
 1109 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
 1110 {
 1111         struct if_clone *ifc;
 1112         u_int bif_dlt, bif_hdrlen;
 1113         int rc;
 1114 
 1115         /*
 1116          * if_detach_internal() will call the eventhandler to notify
 1117          * interface departure.  That will detach if_bpf.  We need to
 1118          * safe the dlt and hdrlen so we can re-attach it later.
 1119          */
 1120         bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
 1121 
 1122         /*
 1123          * Detach from current vnet, but preserve LLADDR info, do not
 1124          * mark as dead etc. so that the ifnet can be reattached later.
 1125          * If we cannot find it, we lost the race to someone else.
 1126          */
 1127         rc = if_detach_internal(ifp, 1, &ifc);
 1128         if (rc != 0)
 1129                 return;
 1130 
 1131         /*
 1132          * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
 1133          * the if_index for that vnet if possible.
 1134          *
 1135          * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
 1136          * or we'd lock on one vnet and unlock on another.
 1137          */
 1138         IFNET_WLOCK();
 1139         ifindex_free_locked(ifp->if_index);
 1140         IFNET_WUNLOCK();
 1141 
 1142         /*
 1143          * Perform interface-specific reassignment tasks, if provided by
 1144          * the driver.
 1145          */
 1146         if (ifp->if_reassign != NULL)
 1147                 ifp->if_reassign(ifp, new_vnet, NULL);
 1148 
 1149         /*
 1150          * Switch to the context of the target vnet.
 1151          */
 1152         CURVNET_SET_QUIET(new_vnet);
 1153 
 1154         IFNET_WLOCK();
 1155         ifp->if_index = ifindex_alloc();
 1156         ifnet_setbyindex_locked(ifp->if_index, ifp);
 1157         IFNET_WUNLOCK();
 1158 
 1159         if_attach_internal(ifp, 1, ifc);
 1160 
 1161         if (ifp->if_bpf == NULL)
 1162                 bpfattach(ifp, bif_dlt, bif_hdrlen);
 1163 
 1164         CURVNET_RESTORE();
 1165 }
 1166 
 1167 /*
 1168  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
 1169  */
 1170 static int
 1171 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
 1172 {
 1173         struct prison *pr;
 1174         struct ifnet *difp;
 1175         int shutdown;
 1176 
 1177         /* Try to find the prison within our visibility. */
 1178         sx_slock(&allprison_lock);
 1179         pr = prison_find_child(td->td_ucred->cr_prison, jid);
 1180         sx_sunlock(&allprison_lock);
 1181         if (pr == NULL)
 1182                 return (ENXIO);
 1183         prison_hold_locked(pr);
 1184         mtx_unlock(&pr->pr_mtx);
 1185 
 1186         /* Do not try to move the iface from and to the same prison. */
 1187         if (pr->pr_vnet == ifp->if_vnet) {
 1188                 prison_free(pr);
 1189                 return (EEXIST);
 1190         }
 1191 
 1192         /* Make sure the named iface does not exists in the dst. prison/vnet. */
 1193         /* XXX Lock interfaces to avoid races. */
 1194         CURVNET_SET_QUIET(pr->pr_vnet);
 1195         difp = ifunit(ifname);
 1196         if (difp != NULL) {
 1197                 CURVNET_RESTORE();
 1198                 prison_free(pr);
 1199                 return (EEXIST);
 1200         }
 1201 
 1202         /* Make sure the VNET is stable. */
 1203         shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
 1204                  ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
 1205         if (shutdown) {
 1206                 CURVNET_RESTORE();
 1207                 prison_free(pr);
 1208                 return (EBUSY);
 1209         }
 1210         CURVNET_RESTORE();
 1211 
 1212         /* Move the interface into the child jail/vnet. */
 1213         if_vmove(ifp, pr->pr_vnet);
 1214 
 1215         /* Report the new if_xname back to the userland. */
 1216         sprintf(ifname, "%s", ifp->if_xname);
 1217 
 1218         prison_free(pr);
 1219         return (0);
 1220 }
 1221 
 1222 static int
 1223 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
 1224 {
 1225         struct prison *pr;
 1226         struct vnet *vnet_dst;
 1227         struct ifnet *ifp;
 1228         int shutdown;
 1229 
 1230         /* Try to find the prison within our visibility. */
 1231         sx_slock(&allprison_lock);
 1232         pr = prison_find_child(td->td_ucred->cr_prison, jid);
 1233         sx_sunlock(&allprison_lock);
 1234         if (pr == NULL)
 1235                 return (ENXIO);
 1236         prison_hold_locked(pr);
 1237         mtx_unlock(&pr->pr_mtx);
 1238 
 1239         /* Make sure the named iface exists in the source prison/vnet. */
 1240         CURVNET_SET(pr->pr_vnet);
 1241         ifp = ifunit(ifname);           /* XXX Lock to avoid races. */
 1242         if (ifp == NULL) {
 1243                 CURVNET_RESTORE();
 1244                 prison_free(pr);
 1245                 return (ENXIO);
 1246         }
 1247 
 1248         /* Do not try to move the iface from and to the same prison. */
 1249         vnet_dst = TD_TO_VNET(td);
 1250         if (vnet_dst == ifp->if_vnet) {
 1251                 CURVNET_RESTORE();
 1252                 prison_free(pr);
 1253                 return (EEXIST);
 1254         }
 1255 
 1256         /* Make sure the VNET is stable. */
 1257         shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
 1258                  ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
 1259         if (shutdown) {
 1260                 CURVNET_RESTORE();
 1261                 prison_free(pr);
 1262                 return (EBUSY);
 1263         }
 1264 
 1265         /* Get interface back from child jail/vnet. */
 1266         if_vmove(ifp, vnet_dst);
 1267         CURVNET_RESTORE();
 1268 
 1269         /* Report the new if_xname back to the userland. */
 1270         sprintf(ifname, "%s", ifp->if_xname);
 1271 
 1272         prison_free(pr);
 1273         return (0);
 1274 }
 1275 #endif /* VIMAGE */
 1276 
 1277 /*
 1278  * Add a group to an interface
 1279  */
 1280 int
 1281 if_addgroup(struct ifnet *ifp, const char *groupname)
 1282 {
 1283         struct ifg_list         *ifgl;
 1284         struct ifg_group        *ifg = NULL;
 1285         struct ifg_member       *ifgm;
 1286         int                      new = 0;
 1287 
 1288         if (groupname[0] && groupname[strlen(groupname) - 1] >= '' &&
 1289             groupname[strlen(groupname) - 1] <= '9')
 1290                 return (EINVAL);
 1291 
 1292         IFNET_WLOCK();
 1293         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
 1294                 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
 1295                         IFNET_WUNLOCK();
 1296                         return (EEXIST);
 1297                 }
 1298 
 1299         if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
 1300             M_NOWAIT)) == NULL) {
 1301                 IFNET_WUNLOCK();
 1302                 return (ENOMEM);
 1303         }
 1304 
 1305         if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
 1306             M_TEMP, M_NOWAIT)) == NULL) {
 1307                 free(ifgl, M_TEMP);
 1308                 IFNET_WUNLOCK();
 1309                 return (ENOMEM);
 1310         }
 1311 
 1312         TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
 1313                 if (!strcmp(ifg->ifg_group, groupname))
 1314                         break;
 1315 
 1316         if (ifg == NULL) {
 1317                 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
 1318                     M_TEMP, M_NOWAIT)) == NULL) {
 1319                         free(ifgl, M_TEMP);
 1320                         free(ifgm, M_TEMP);
 1321                         IFNET_WUNLOCK();
 1322                         return (ENOMEM);
 1323                 }
 1324                 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
 1325                 ifg->ifg_refcnt = 0;
 1326                 TAILQ_INIT(&ifg->ifg_members);
 1327                 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
 1328                 new = 1;
 1329         }
 1330 
 1331         ifg->ifg_refcnt++;
 1332         ifgl->ifgl_group = ifg;
 1333         ifgm->ifgm_ifp = ifp;
 1334 
 1335         IF_ADDR_WLOCK(ifp);
 1336         TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
 1337         TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
 1338         IF_ADDR_WUNLOCK(ifp);
 1339 
 1340         IFNET_WUNLOCK();
 1341 
 1342         if (new)
 1343                 EVENTHANDLER_INVOKE(group_attach_event, ifg);
 1344         EVENTHANDLER_INVOKE(group_change_event, groupname);
 1345 
 1346         return (0);
 1347 }
 1348 
 1349 /*
 1350  * Remove a group from an interface
 1351  */
 1352 int
 1353 if_delgroup(struct ifnet *ifp, const char *groupname)
 1354 {
 1355         struct ifg_list         *ifgl;
 1356         struct ifg_member       *ifgm;
 1357 
 1358         IFNET_WLOCK();
 1359         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
 1360                 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
 1361                         break;
 1362         if (ifgl == NULL) {
 1363                 IFNET_WUNLOCK();
 1364                 return (ENOENT);
 1365         }
 1366 
 1367         IF_ADDR_WLOCK(ifp);
 1368         TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
 1369         IF_ADDR_WUNLOCK(ifp);
 1370 
 1371         TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
 1372                 if (ifgm->ifgm_ifp == ifp)
 1373                         break;
 1374 
 1375         if (ifgm != NULL) {
 1376                 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
 1377                 free(ifgm, M_TEMP);
 1378         }
 1379 
 1380         if (--ifgl->ifgl_group->ifg_refcnt == 0) {
 1381                 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
 1382                 IFNET_WUNLOCK();
 1383                 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
 1384                 free(ifgl->ifgl_group, M_TEMP);
 1385         } else
 1386                 IFNET_WUNLOCK();
 1387 
 1388         free(ifgl, M_TEMP);
 1389 
 1390         EVENTHANDLER_INVOKE(group_change_event, groupname);
 1391 
 1392         return (0);
 1393 }
 1394 
 1395 /*
 1396  * Remove an interface from all groups
 1397  */
 1398 static void
 1399 if_delgroups(struct ifnet *ifp)
 1400 {
 1401         struct ifg_list         *ifgl;
 1402         struct ifg_member       *ifgm;
 1403         char groupname[IFNAMSIZ];
 1404 
 1405         IFNET_WLOCK();
 1406         while (!TAILQ_EMPTY(&ifp->if_groups)) {
 1407                 ifgl = TAILQ_FIRST(&ifp->if_groups);
 1408 
 1409                 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
 1410 
 1411                 IF_ADDR_WLOCK(ifp);
 1412                 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
 1413                 IF_ADDR_WUNLOCK(ifp);
 1414 
 1415                 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
 1416                         if (ifgm->ifgm_ifp == ifp)
 1417                                 break;
 1418 
 1419                 if (ifgm != NULL) {
 1420                         TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
 1421                             ifgm_next);
 1422                         free(ifgm, M_TEMP);
 1423                 }
 1424 
 1425                 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
 1426                         TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
 1427                         IFNET_WUNLOCK();
 1428                         EVENTHANDLER_INVOKE(group_detach_event,
 1429                             ifgl->ifgl_group);
 1430                         free(ifgl->ifgl_group, M_TEMP);
 1431                 } else
 1432                         IFNET_WUNLOCK();
 1433 
 1434                 free(ifgl, M_TEMP);
 1435 
 1436                 EVENTHANDLER_INVOKE(group_change_event, groupname);
 1437 
 1438                 IFNET_WLOCK();
 1439         }
 1440         IFNET_WUNLOCK();
 1441 }
 1442 
 1443 /*
 1444  * Stores all groups from an interface in memory pointed
 1445  * to by data
 1446  */
 1447 static int
 1448 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
 1449 {
 1450         int                      len, error;
 1451         struct ifg_list         *ifgl;
 1452         struct ifg_req           ifgrq, *ifgp;
 1453         struct ifgroupreq       *ifgr = data;
 1454 
 1455         if (ifgr->ifgr_len == 0) {
 1456                 IF_ADDR_RLOCK(ifp);
 1457                 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
 1458                         ifgr->ifgr_len += sizeof(struct ifg_req);
 1459                 IF_ADDR_RUNLOCK(ifp);
 1460                 return (0);
 1461         }
 1462 
 1463         len = ifgr->ifgr_len;
 1464         ifgp = ifgr->ifgr_groups;
 1465         /* XXX: wire */
 1466         IF_ADDR_RLOCK(ifp);
 1467         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
 1468                 if (len < sizeof(ifgrq)) {
 1469                         IF_ADDR_RUNLOCK(ifp);
 1470                         return (EINVAL);
 1471                 }
 1472                 bzero(&ifgrq, sizeof ifgrq);
 1473                 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
 1474                     sizeof(ifgrq.ifgrq_group));
 1475                 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
 1476                         IF_ADDR_RUNLOCK(ifp);
 1477                         return (error);
 1478                 }
 1479                 len -= sizeof(ifgrq);
 1480                 ifgp++;
 1481         }
 1482         IF_ADDR_RUNLOCK(ifp);
 1483 
 1484         return (0);
 1485 }
 1486 
 1487 /*
 1488  * Stores all members of a group in memory pointed to by data
 1489  */
 1490 static int
 1491 if_getgroupmembers(struct ifgroupreq *data)
 1492 {
 1493         struct ifgroupreq       *ifgr = data;
 1494         struct ifg_group        *ifg;
 1495         struct ifg_member       *ifgm;
 1496         struct ifg_req           ifgrq, *ifgp;
 1497         int                      len, error;
 1498 
 1499         IFNET_RLOCK();
 1500         TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
 1501                 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
 1502                         break;
 1503         if (ifg == NULL) {
 1504                 IFNET_RUNLOCK();
 1505                 return (ENOENT);
 1506         }
 1507 
 1508         if (ifgr->ifgr_len == 0) {
 1509                 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
 1510                         ifgr->ifgr_len += sizeof(ifgrq);
 1511                 IFNET_RUNLOCK();
 1512                 return (0);
 1513         }
 1514 
 1515         len = ifgr->ifgr_len;
 1516         ifgp = ifgr->ifgr_groups;
 1517         TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
 1518                 if (len < sizeof(ifgrq)) {
 1519                         IFNET_RUNLOCK();
 1520                         return (EINVAL);
 1521                 }
 1522                 bzero(&ifgrq, sizeof ifgrq);
 1523                 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
 1524                     sizeof(ifgrq.ifgrq_member));
 1525                 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
 1526                         IFNET_RUNLOCK();
 1527                         return (error);
 1528                 }
 1529                 len -= sizeof(ifgrq);
 1530                 ifgp++;
 1531         }
 1532         IFNET_RUNLOCK();
 1533 
 1534         return (0);
 1535 }
 1536 
 1537 /*
 1538  * Return counter values from counter(9)s stored in ifnet.
 1539  */
 1540 uint64_t
 1541 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
 1542 {
 1543 
 1544         KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
 1545 
 1546         return (counter_u64_fetch(ifp->if_counters[cnt]));
 1547 }
 1548 
 1549 /*
 1550  * Increase an ifnet counter. Usually used for counters shared
 1551  * between the stack and a driver, but function supports them all.
 1552  */
 1553 void
 1554 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
 1555 {
 1556 
 1557         KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
 1558 
 1559         counter_u64_add(ifp->if_counters[cnt], inc);
 1560 }
 1561 
 1562 /*
 1563  * Copy data from ifnet to userland API structure if_data.
 1564  */
 1565 void
 1566 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
 1567 {
 1568 
 1569         ifd->ifi_type = ifp->if_type;
 1570         ifd->ifi_physical = 0;
 1571         ifd->ifi_addrlen = ifp->if_addrlen;
 1572         ifd->ifi_hdrlen = ifp->if_hdrlen;
 1573         ifd->ifi_link_state = ifp->if_link_state;
 1574         ifd->ifi_vhid = 0;
 1575         ifd->ifi_datalen = sizeof(struct if_data);
 1576         ifd->ifi_mtu = ifp->if_mtu;
 1577         ifd->ifi_metric = ifp->if_metric;
 1578         ifd->ifi_baudrate = ifp->if_baudrate;
 1579         ifd->ifi_hwassist = ifp->if_hwassist;
 1580         ifd->ifi_epoch = ifp->if_epoch;
 1581         ifd->ifi_lastchange = ifp->if_lastchange;
 1582 
 1583         ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
 1584         ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
 1585         ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
 1586         ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
 1587         ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
 1588         ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
 1589         ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
 1590         ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
 1591         ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
 1592         ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
 1593         ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
 1594         ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
 1595 }
 1596 
 1597 /*
 1598  * Wrapper functions for struct ifnet address list locking macros.  These are
 1599  * used by kernel modules to avoid encoding programming interface or binary
 1600  * interface assumptions that may be violated when kernel-internal locking
 1601  * approaches change.
 1602  */
 1603 void
 1604 if_addr_rlock(struct ifnet *ifp)
 1605 {
 1606 
 1607         IF_ADDR_RLOCK(ifp);
 1608 }
 1609 
 1610 void
 1611 if_addr_runlock(struct ifnet *ifp)
 1612 {
 1613 
 1614         IF_ADDR_RUNLOCK(ifp);
 1615 }
 1616 
 1617 void
 1618 if_maddr_rlock(if_t ifp)
 1619 {
 1620 
 1621         IF_ADDR_RLOCK((struct ifnet *)ifp);
 1622 }
 1623 
 1624 void
 1625 if_maddr_runlock(if_t ifp)
 1626 {
 1627 
 1628         IF_ADDR_RUNLOCK((struct ifnet *)ifp);
 1629 }
 1630 
 1631 /*
 1632  * Initialization, destruction and refcounting functions for ifaddrs.
 1633  */
 1634 struct ifaddr *
 1635 ifa_alloc(size_t size, int flags)
 1636 {
 1637         struct ifaddr *ifa;
 1638 
 1639         KASSERT(size >= sizeof(struct ifaddr),
 1640             ("%s: invalid size %zu", __func__, size));
 1641 
 1642         ifa = malloc(size, M_IFADDR, M_ZERO | flags);
 1643         if (ifa == NULL)
 1644                 return (NULL);
 1645 
 1646         if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
 1647                 goto fail;
 1648         if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
 1649                 goto fail;
 1650         if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
 1651                 goto fail;
 1652         if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
 1653                 goto fail;
 1654 
 1655         refcount_init(&ifa->ifa_refcnt, 1);
 1656 
 1657         return (ifa);
 1658 
 1659 fail:
 1660         /* free(NULL) is okay */
 1661         counter_u64_free(ifa->ifa_opackets);
 1662         counter_u64_free(ifa->ifa_ipackets);
 1663         counter_u64_free(ifa->ifa_obytes);
 1664         counter_u64_free(ifa->ifa_ibytes);
 1665         free(ifa, M_IFADDR);
 1666 
 1667         return (NULL);
 1668 }
 1669 
 1670 void
 1671 ifa_ref(struct ifaddr *ifa)
 1672 {
 1673 
 1674         refcount_acquire(&ifa->ifa_refcnt);
 1675 }
 1676 
 1677 void
 1678 ifa_free(struct ifaddr *ifa)
 1679 {
 1680 
 1681         if (refcount_release(&ifa->ifa_refcnt)) {
 1682                 counter_u64_free(ifa->ifa_opackets);
 1683                 counter_u64_free(ifa->ifa_ipackets);
 1684                 counter_u64_free(ifa->ifa_obytes);
 1685                 counter_u64_free(ifa->ifa_ibytes);
 1686                 free(ifa, M_IFADDR);
 1687         }
 1688 }
 1689 
 1690 static int
 1691 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
 1692     struct sockaddr *ia)
 1693 {
 1694         int error;
 1695         struct rt_addrinfo info;
 1696         struct sockaddr_dl null_sdl;
 1697         struct ifnet *ifp;
 1698 
 1699         ifp = ifa->ifa_ifp;
 1700 
 1701         bzero(&info, sizeof(info));
 1702         if (cmd != RTM_DELETE)
 1703                 info.rti_ifp = V_loif;
 1704         info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
 1705         info.rti_info[RTAX_DST] = ia;
 1706         info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
 1707         link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
 1708 
 1709         error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
 1710 
 1711         if (error != 0)
 1712                 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
 1713                     __func__, otype, if_name(ifp), error);
 1714 
 1715         return (error);
 1716 }
 1717 
 1718 int
 1719 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
 1720 {
 1721 
 1722         return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
 1723 }
 1724 
 1725 int
 1726 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
 1727 {
 1728 
 1729         return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
 1730 }
 1731 
 1732 int
 1733 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
 1734 {
 1735 
 1736         return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
 1737 }
 1738 
 1739 /*
 1740  * XXX: Because sockaddr_dl has deeper structure than the sockaddr
 1741  * structs used to represent other address families, it is necessary
 1742  * to perform a different comparison.
 1743  */
 1744 
 1745 #define sa_dl_equal(a1, a2)     \
 1746         ((((const struct sockaddr_dl *)(a1))->sdl_len ==                \
 1747          ((const struct sockaddr_dl *)(a2))->sdl_len) &&                \
 1748          (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)),               \
 1749                CLLADDR((const struct sockaddr_dl *)(a2)),               \
 1750                ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
 1751 
 1752 /*
 1753  * Locate an interface based on a complete address.
 1754  */
 1755 /*ARGSUSED*/
 1756 static struct ifaddr *
 1757 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
 1758 {
 1759         struct ifnet *ifp;
 1760         struct ifaddr *ifa;
 1761 
 1762         IFNET_RLOCK_NOSLEEP();
 1763         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 1764                 IF_ADDR_RLOCK(ifp);
 1765                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1766                         if (ifa->ifa_addr->sa_family != addr->sa_family)
 1767                                 continue;
 1768                         if (sa_equal(addr, ifa->ifa_addr)) {
 1769                                 if (getref)
 1770                                         ifa_ref(ifa);
 1771                                 IF_ADDR_RUNLOCK(ifp);
 1772                                 goto done;
 1773                         }
 1774                         /* IP6 doesn't have broadcast */
 1775                         if ((ifp->if_flags & IFF_BROADCAST) &&
 1776                             ifa->ifa_broadaddr &&
 1777                             ifa->ifa_broadaddr->sa_len != 0 &&
 1778                             sa_equal(ifa->ifa_broadaddr, addr)) {
 1779                                 if (getref)
 1780                                         ifa_ref(ifa);
 1781                                 IF_ADDR_RUNLOCK(ifp);
 1782                                 goto done;
 1783                         }
 1784                 }
 1785                 IF_ADDR_RUNLOCK(ifp);
 1786         }
 1787         ifa = NULL;
 1788 done:
 1789         IFNET_RUNLOCK_NOSLEEP();
 1790         return (ifa);
 1791 }
 1792 
 1793 struct ifaddr *
 1794 ifa_ifwithaddr(const struct sockaddr *addr)
 1795 {
 1796 
 1797         return (ifa_ifwithaddr_internal(addr, 1));
 1798 }
 1799 
 1800 int
 1801 ifa_ifwithaddr_check(const struct sockaddr *addr)
 1802 {
 1803 
 1804         return (ifa_ifwithaddr_internal(addr, 0) != NULL);
 1805 }
 1806 
 1807 /*
 1808  * Locate an interface based on the broadcast address.
 1809  */
 1810 /* ARGSUSED */
 1811 struct ifaddr *
 1812 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
 1813 {
 1814         struct ifnet *ifp;
 1815         struct ifaddr *ifa;
 1816 
 1817         IFNET_RLOCK_NOSLEEP();
 1818         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 1819                 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
 1820                         continue;
 1821                 IF_ADDR_RLOCK(ifp);
 1822                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1823                         if (ifa->ifa_addr->sa_family != addr->sa_family)
 1824                                 continue;
 1825                         if ((ifp->if_flags & IFF_BROADCAST) &&
 1826                             ifa->ifa_broadaddr &&
 1827                             ifa->ifa_broadaddr->sa_len != 0 &&
 1828                             sa_equal(ifa->ifa_broadaddr, addr)) {
 1829                                 ifa_ref(ifa);
 1830                                 IF_ADDR_RUNLOCK(ifp);
 1831                                 goto done;
 1832                         }
 1833                 }
 1834                 IF_ADDR_RUNLOCK(ifp);
 1835         }
 1836         ifa = NULL;
 1837 done:
 1838         IFNET_RUNLOCK_NOSLEEP();
 1839         return (ifa);
 1840 }
 1841 
 1842 /*
 1843  * Locate the point to point interface with a given destination address.
 1844  */
 1845 /*ARGSUSED*/
 1846 struct ifaddr *
 1847 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
 1848 {
 1849         struct ifnet *ifp;
 1850         struct ifaddr *ifa;
 1851 
 1852         IFNET_RLOCK_NOSLEEP();
 1853         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 1854                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
 1855                         continue;
 1856                 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
 1857                         continue;
 1858                 IF_ADDR_RLOCK(ifp);
 1859                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1860                         if (ifa->ifa_addr->sa_family != addr->sa_family)
 1861                                 continue;
 1862                         if (ifa->ifa_dstaddr != NULL &&
 1863                             sa_equal(addr, ifa->ifa_dstaddr)) {
 1864                                 ifa_ref(ifa);
 1865                                 IF_ADDR_RUNLOCK(ifp);
 1866                                 goto done;
 1867                         }
 1868                 }
 1869                 IF_ADDR_RUNLOCK(ifp);
 1870         }
 1871         ifa = NULL;
 1872 done:
 1873         IFNET_RUNLOCK_NOSLEEP();
 1874         return (ifa);
 1875 }
 1876 
 1877 /*
 1878  * Find an interface on a specific network.  If many, choice
 1879  * is most specific found.
 1880  */
 1881 struct ifaddr *
 1882 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
 1883 {
 1884         struct ifnet *ifp;
 1885         struct ifaddr *ifa;
 1886         struct ifaddr *ifa_maybe = NULL;
 1887         u_int af = addr->sa_family;
 1888         const char *addr_data = addr->sa_data, *cplim;
 1889 
 1890         /*
 1891          * AF_LINK addresses can be looked up directly by their index number,
 1892          * so do that if we can.
 1893          */
 1894         if (af == AF_LINK) {
 1895             const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
 1896             if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
 1897                 return (ifaddr_byindex(sdl->sdl_index));
 1898         }
 1899 
 1900         /*
 1901          * Scan though each interface, looking for ones that have addresses
 1902          * in this address family and the requested fib.  Maintain a reference
 1903          * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
 1904          * kept it stable when we move onto the next interface.
 1905          */
 1906         IFNET_RLOCK_NOSLEEP();
 1907         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 1908                 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
 1909                         continue;
 1910                 IF_ADDR_RLOCK(ifp);
 1911                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1912                         const char *cp, *cp2, *cp3;
 1913 
 1914                         if (ifa->ifa_addr->sa_family != af)
 1915 next:                           continue;
 1916                         if (af == AF_INET && 
 1917                             ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
 1918                                 /*
 1919                                  * This is a bit broken as it doesn't
 1920                                  * take into account that the remote end may
 1921                                  * be a single node in the network we are
 1922                                  * looking for.
 1923                                  * The trouble is that we don't know the
 1924                                  * netmask for the remote end.
 1925                                  */
 1926                                 if (ifa->ifa_dstaddr != NULL &&
 1927                                     sa_equal(addr, ifa->ifa_dstaddr)) {
 1928                                         ifa_ref(ifa);
 1929                                         IF_ADDR_RUNLOCK(ifp);
 1930                                         goto done;
 1931                                 }
 1932                         } else {
 1933                                 /*
 1934                                  * Scan all the bits in the ifa's address.
 1935                                  * If a bit dissagrees with what we are
 1936                                  * looking for, mask it with the netmask
 1937                                  * to see if it really matters.
 1938                                  * (A byte at a time)
 1939                                  */
 1940                                 if (ifa->ifa_netmask == 0)
 1941                                         continue;
 1942                                 cp = addr_data;
 1943                                 cp2 = ifa->ifa_addr->sa_data;
 1944                                 cp3 = ifa->ifa_netmask->sa_data;
 1945                                 cplim = ifa->ifa_netmask->sa_len
 1946                                         + (char *)ifa->ifa_netmask;
 1947                                 while (cp3 < cplim)
 1948                                         if ((*cp++ ^ *cp2++) & *cp3++)
 1949                                                 goto next; /* next address! */
 1950                                 /*
 1951                                  * If the netmask of what we just found
 1952                                  * is more specific than what we had before
 1953                                  * (if we had one), or if the virtual status
 1954                                  * of new prefix is better than of the old one,
 1955                                  * then remember the new one before continuing
 1956                                  * to search for an even better one.
 1957                                  */
 1958                                 if (ifa_maybe == NULL ||
 1959                                     ifa_preferred(ifa_maybe, ifa) ||
 1960                                     rn_refines((caddr_t)ifa->ifa_netmask,
 1961                                     (caddr_t)ifa_maybe->ifa_netmask)) {
 1962                                         if (ifa_maybe != NULL)
 1963                                                 ifa_free(ifa_maybe);
 1964                                         ifa_maybe = ifa;
 1965                                         ifa_ref(ifa_maybe);
 1966                                 }
 1967                         }
 1968                 }
 1969                 IF_ADDR_RUNLOCK(ifp);
 1970         }
 1971         ifa = ifa_maybe;
 1972         ifa_maybe = NULL;
 1973 done:
 1974         IFNET_RUNLOCK_NOSLEEP();
 1975         if (ifa_maybe != NULL)
 1976                 ifa_free(ifa_maybe);
 1977         return (ifa);
 1978 }
 1979 
 1980 /*
 1981  * Find an interface address specific to an interface best matching
 1982  * a given address.
 1983  */
 1984 struct ifaddr *
 1985 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
 1986 {
 1987         struct ifaddr *ifa;
 1988         const char *cp, *cp2, *cp3;
 1989         char *cplim;
 1990         struct ifaddr *ifa_maybe = NULL;
 1991         u_int af = addr->sa_family;
 1992 
 1993         if (af >= AF_MAX)
 1994                 return (NULL);
 1995         IF_ADDR_RLOCK(ifp);
 1996         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1997                 if (ifa->ifa_addr->sa_family != af)
 1998                         continue;
 1999                 if (ifa_maybe == NULL)
 2000                         ifa_maybe = ifa;
 2001                 if (ifa->ifa_netmask == 0) {
 2002                         if (sa_equal(addr, ifa->ifa_addr) ||
 2003                             (ifa->ifa_dstaddr &&
 2004                             sa_equal(addr, ifa->ifa_dstaddr)))
 2005                                 goto done;
 2006                         continue;
 2007                 }
 2008                 if (ifp->if_flags & IFF_POINTOPOINT) {
 2009                         if (sa_equal(addr, ifa->ifa_dstaddr))
 2010                                 goto done;
 2011                 } else {
 2012                         cp = addr->sa_data;
 2013                         cp2 = ifa->ifa_addr->sa_data;
 2014                         cp3 = ifa->ifa_netmask->sa_data;
 2015                         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
 2016                         for (; cp3 < cplim; cp3++)
 2017                                 if ((*cp++ ^ *cp2++) & *cp3)
 2018                                         break;
 2019                         if (cp3 == cplim)
 2020                                 goto done;
 2021                 }
 2022         }
 2023         ifa = ifa_maybe;
 2024 done:
 2025         if (ifa != NULL)
 2026                 ifa_ref(ifa);
 2027         IF_ADDR_RUNLOCK(ifp);
 2028         return (ifa);
 2029 }
 2030 
 2031 /*
 2032  * See whether new ifa is better than current one:
 2033  * 1) A non-virtual one is preferred over virtual.
 2034  * 2) A virtual in master state preferred over any other state.
 2035  *
 2036  * Used in several address selecting functions.
 2037  */
 2038 int
 2039 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
 2040 {
 2041 
 2042         return (cur->ifa_carp && (!next->ifa_carp ||
 2043             ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
 2044 }
 2045 
 2046 #include <net/if_llatbl.h>
 2047 
 2048 /*
 2049  * Default action when installing a route with a Link Level gateway.
 2050  * Lookup an appropriate real ifa to point to.
 2051  * This should be moved to /sys/net/link.c eventually.
 2052  */
 2053 static void
 2054 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
 2055 {
 2056         struct ifaddr *ifa, *oifa;
 2057         struct sockaddr *dst;
 2058         struct ifnet *ifp;
 2059 
 2060         if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
 2061             ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
 2062                 return;
 2063         ifa = ifaof_ifpforaddr(dst, ifp);
 2064         if (ifa) {
 2065                 oifa = rt->rt_ifa;
 2066                 rt->rt_ifa = ifa;
 2067                 ifa_free(oifa);
 2068                 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
 2069                         ifa->ifa_rtrequest(cmd, rt, info);
 2070         }
 2071 }
 2072 
 2073 struct sockaddr_dl *
 2074 link_alloc_sdl(size_t size, int flags)
 2075 {
 2076 
 2077         return (malloc(size, M_TEMP, flags));
 2078 }
 2079 
 2080 void
 2081 link_free_sdl(struct sockaddr *sa)
 2082 {
 2083         free(sa, M_TEMP);
 2084 }
 2085 
 2086 /*
 2087  * Fills in given sdl with interface basic info.
 2088  * Returns pointer to filled sdl.
 2089  */
 2090 struct sockaddr_dl *
 2091 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
 2092 {
 2093         struct sockaddr_dl *sdl;
 2094 
 2095         sdl = (struct sockaddr_dl *)paddr;
 2096         memset(sdl, 0, sizeof(struct sockaddr_dl));
 2097         sdl->sdl_len = sizeof(struct sockaddr_dl);
 2098         sdl->sdl_family = AF_LINK;
 2099         sdl->sdl_index = ifp->if_index;
 2100         sdl->sdl_type = iftype;
 2101 
 2102         return (sdl);
 2103 }
 2104 
 2105 /*
 2106  * Mark an interface down and notify protocols of
 2107  * the transition.
 2108  */
 2109 static void
 2110 if_unroute(struct ifnet *ifp, int flag, int fam)
 2111 {
 2112         struct ifaddr *ifa;
 2113 
 2114         KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
 2115 
 2116         ifp->if_flags &= ~flag;
 2117         getmicrotime(&ifp->if_lastchange);
 2118         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
 2119                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
 2120                         pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
 2121         ifp->if_qflush(ifp);
 2122 
 2123         if (ifp->if_carp)
 2124                 (*carp_linkstate_p)(ifp);
 2125         rt_ifmsg(ifp);
 2126 }
 2127 
 2128 /*
 2129  * Mark an interface up and notify protocols of
 2130  * the transition.
 2131  */
 2132 static void
 2133 if_route(struct ifnet *ifp, int flag, int fam)
 2134 {
 2135         struct ifaddr *ifa;
 2136 
 2137         KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
 2138 
 2139         ifp->if_flags |= flag;
 2140         getmicrotime(&ifp->if_lastchange);
 2141         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
 2142                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
 2143                         pfctlinput(PRC_IFUP, ifa->ifa_addr);
 2144         if (ifp->if_carp)
 2145                 (*carp_linkstate_p)(ifp);
 2146         rt_ifmsg(ifp);
 2147 #ifdef INET6
 2148         in6_if_up(ifp);
 2149 #endif
 2150 }
 2151 
 2152 void    (*vlan_link_state_p)(struct ifnet *);   /* XXX: private from if_vlan */
 2153 void    (*vlan_trunk_cap_p)(struct ifnet *);            /* XXX: private from if_vlan */
 2154 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
 2155 struct  ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
 2156 int     (*vlan_tag_p)(struct ifnet *, uint16_t *);
 2157 int     (*vlan_setcookie_p)(struct ifnet *, void *);
 2158 void    *(*vlan_cookie_p)(struct ifnet *);
 2159 
 2160 /*
 2161  * Handle a change in the interface link state. To avoid LORs
 2162  * between driver lock and upper layer locks, as well as possible
 2163  * recursions, we post event to taskqueue, and all job
 2164  * is done in static do_link_state_change().
 2165  */
 2166 void
 2167 if_link_state_change(struct ifnet *ifp, int link_state)
 2168 {
 2169         /* Return if state hasn't changed. */
 2170         if (ifp->if_link_state == link_state)
 2171                 return;
 2172 
 2173         ifp->if_link_state = link_state;
 2174 
 2175         taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
 2176 }
 2177 
 2178 static void
 2179 do_link_state_change(void *arg, int pending)
 2180 {
 2181         struct ifnet *ifp = (struct ifnet *)arg;
 2182         int link_state = ifp->if_link_state;
 2183         CURVNET_SET(ifp->if_vnet);
 2184 
 2185         /* Notify that the link state has changed. */
 2186         rt_ifmsg(ifp);
 2187         if (ifp->if_vlantrunk != NULL)
 2188                 (*vlan_link_state_p)(ifp);
 2189 
 2190         if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
 2191             ifp->if_l2com != NULL)
 2192                 (*ng_ether_link_state_p)(ifp, link_state);
 2193         if (ifp->if_carp)
 2194                 (*carp_linkstate_p)(ifp);
 2195         if (ifp->if_bridge)
 2196                 (*bridge_linkstate_p)(ifp);
 2197         if (ifp->if_lagg)
 2198                 (*lagg_linkstate_p)(ifp, link_state);
 2199 
 2200         if (IS_DEFAULT_VNET(curvnet))
 2201                 devctl_notify("IFNET", ifp->if_xname,
 2202                     (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
 2203                     NULL);
 2204         if (pending > 1)
 2205                 if_printf(ifp, "%d link states coalesced\n", pending);
 2206         if (log_link_state_change)
 2207                 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
 2208                     (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
 2209         EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
 2210         CURVNET_RESTORE();
 2211 }
 2212 
 2213 /*
 2214  * Mark an interface down and notify protocols of
 2215  * the transition.
 2216  */
 2217 void
 2218 if_down(struct ifnet *ifp)
 2219 {
 2220 
 2221         if_unroute(ifp, IFF_UP, AF_UNSPEC);
 2222 }
 2223 
 2224 /*
 2225  * Mark an interface up and notify protocols of
 2226  * the transition.
 2227  */
 2228 void
 2229 if_up(struct ifnet *ifp)
 2230 {
 2231 
 2232         if_route(ifp, IFF_UP, AF_UNSPEC);
 2233 }
 2234 
 2235 /*
 2236  * Flush an interface queue.
 2237  */
 2238 void
 2239 if_qflush(struct ifnet *ifp)
 2240 {
 2241         struct mbuf *m, *n;
 2242         struct ifaltq *ifq;
 2243         
 2244         ifq = &ifp->if_snd;
 2245         IFQ_LOCK(ifq);
 2246 #ifdef ALTQ
 2247         if (ALTQ_IS_ENABLED(ifq))
 2248                 ALTQ_PURGE(ifq);
 2249 #endif
 2250         n = ifq->ifq_head;
 2251         while ((m = n) != NULL) {
 2252                 n = m->m_nextpkt;
 2253                 m_freem(m);
 2254         }
 2255         ifq->ifq_head = 0;
 2256         ifq->ifq_tail = 0;
 2257         ifq->ifq_len = 0;
 2258         IFQ_UNLOCK(ifq);
 2259 }
 2260 
 2261 /*
 2262  * Map interface name to interface structure pointer, with or without
 2263  * returning a reference.
 2264  */
 2265 struct ifnet *
 2266 ifunit_ref(const char *name)
 2267 {
 2268         struct ifnet *ifp;
 2269 
 2270         IFNET_RLOCK_NOSLEEP();
 2271         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 2272                 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
 2273                     !(ifp->if_flags & IFF_DYING))
 2274                         break;
 2275         }
 2276         if (ifp != NULL)
 2277                 if_ref(ifp);
 2278         IFNET_RUNLOCK_NOSLEEP();
 2279         return (ifp);
 2280 }
 2281 
 2282 struct ifnet *
 2283 ifunit(const char *name)
 2284 {
 2285         struct ifnet *ifp;
 2286 
 2287         IFNET_RLOCK_NOSLEEP();
 2288         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 2289                 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
 2290                         break;
 2291         }
 2292         IFNET_RUNLOCK_NOSLEEP();
 2293         return (ifp);
 2294 }
 2295 
 2296 /*
 2297  * Hardware specific interface ioctls.
 2298  */
 2299 static int
 2300 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
 2301 {
 2302         struct ifreq *ifr;
 2303         int error = 0;
 2304         int new_flags, temp_flags;
 2305         size_t namelen, onamelen;
 2306         size_t descrlen;
 2307         char *descrbuf, *odescrbuf;
 2308         char new_name[IFNAMSIZ];
 2309         struct ifaddr *ifa;
 2310         struct sockaddr_dl *sdl;
 2311 
 2312         ifr = (struct ifreq *)data;
 2313         switch (cmd) {
 2314         case SIOCGIFINDEX:
 2315                 ifr->ifr_index = ifp->if_index;
 2316                 break;
 2317 
 2318         case SIOCGIFFLAGS:
 2319                 temp_flags = ifp->if_flags | ifp->if_drv_flags;
 2320                 ifr->ifr_flags = temp_flags & 0xffff;
 2321                 ifr->ifr_flagshigh = temp_flags >> 16;
 2322                 break;
 2323 
 2324         case SIOCGIFCAP:
 2325                 ifr->ifr_reqcap = ifp->if_capabilities;
 2326                 ifr->ifr_curcap = ifp->if_capenable;
 2327                 break;
 2328 
 2329 #ifdef MAC
 2330         case SIOCGIFMAC:
 2331                 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
 2332                 break;
 2333 #endif
 2334 
 2335         case SIOCGIFMETRIC:
 2336                 ifr->ifr_metric = ifp->if_metric;
 2337                 break;
 2338 
 2339         case SIOCGIFMTU:
 2340                 ifr->ifr_mtu = ifp->if_mtu;
 2341                 break;
 2342 
 2343         case SIOCGIFPHYS:
 2344                 /* XXXGL: did this ever worked? */
 2345                 ifr->ifr_phys = 0;
 2346                 break;
 2347 
 2348         case SIOCGIFDESCR:
 2349                 error = 0;
 2350                 sx_slock(&ifdescr_sx);
 2351                 if (ifp->if_description == NULL)
 2352                         error = ENOMSG;
 2353                 else {
 2354                         /* space for terminating nul */
 2355                         descrlen = strlen(ifp->if_description) + 1;
 2356                         if (ifr->ifr_buffer.length < descrlen)
 2357                                 ifr->ifr_buffer.buffer = NULL;
 2358                         else
 2359                                 error = copyout(ifp->if_description,
 2360                                     ifr->ifr_buffer.buffer, descrlen);
 2361                         ifr->ifr_buffer.length = descrlen;
 2362                 }
 2363                 sx_sunlock(&ifdescr_sx);
 2364                 break;
 2365 
 2366         case SIOCSIFDESCR:
 2367                 error = priv_check(td, PRIV_NET_SETIFDESCR);
 2368                 if (error)
 2369                         return (error);
 2370 
 2371                 /*
 2372                  * Copy only (length-1) bytes to make sure that
 2373                  * if_description is always nul terminated.  The
 2374                  * length parameter is supposed to count the
 2375                  * terminating nul in.
 2376                  */
 2377                 if (ifr->ifr_buffer.length > ifdescr_maxlen)
 2378                         return (ENAMETOOLONG);
 2379                 else if (ifr->ifr_buffer.length == 0)
 2380                         descrbuf = NULL;
 2381                 else {
 2382                         descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
 2383                             M_WAITOK | M_ZERO);
 2384                         error = copyin(ifr->ifr_buffer.buffer, descrbuf,
 2385                             ifr->ifr_buffer.length - 1);
 2386                         if (error) {
 2387                                 free(descrbuf, M_IFDESCR);
 2388                                 break;
 2389                         }
 2390                 }
 2391 
 2392                 sx_xlock(&ifdescr_sx);
 2393                 odescrbuf = ifp->if_description;
 2394                 ifp->if_description = descrbuf;
 2395                 sx_xunlock(&ifdescr_sx);
 2396 
 2397                 getmicrotime(&ifp->if_lastchange);
 2398                 free(odescrbuf, M_IFDESCR);
 2399                 break;
 2400 
 2401         case SIOCGIFFIB:
 2402                 ifr->ifr_fib = ifp->if_fib;
 2403                 break;
 2404 
 2405         case SIOCSIFFIB:
 2406                 error = priv_check(td, PRIV_NET_SETIFFIB);
 2407                 if (error)
 2408                         return (error);
 2409                 if (ifr->ifr_fib >= rt_numfibs)
 2410                         return (EINVAL);
 2411 
 2412                 ifp->if_fib = ifr->ifr_fib;
 2413                 break;
 2414 
 2415         case SIOCSIFFLAGS:
 2416                 error = priv_check(td, PRIV_NET_SETIFFLAGS);
 2417                 if (error)
 2418                         return (error);
 2419                 /*
 2420                  * Currently, no driver owned flags pass the IFF_CANTCHANGE
 2421                  * check, so we don't need special handling here yet.
 2422                  */
 2423                 new_flags = (ifr->ifr_flags & 0xffff) |
 2424                     (ifr->ifr_flagshigh << 16);
 2425                 if (ifp->if_flags & IFF_UP &&
 2426                     (new_flags & IFF_UP) == 0) {
 2427                         if_down(ifp);
 2428                 } else if (new_flags & IFF_UP &&
 2429                     (ifp->if_flags & IFF_UP) == 0) {
 2430                         if_up(ifp);
 2431                 }
 2432                 /* See if permanently promiscuous mode bit is about to flip */
 2433                 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
 2434                         if (new_flags & IFF_PPROMISC)
 2435                                 ifp->if_flags |= IFF_PROMISC;
 2436                         else if (ifp->if_pcount == 0)
 2437                                 ifp->if_flags &= ~IFF_PROMISC;
 2438                         if (log_promisc_mode_change)
 2439                                 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
 2440                                     ifp->if_xname,
 2441                                     ((new_flags & IFF_PPROMISC) ?
 2442                                      "enabled" : "disabled"));
 2443                 }
 2444                 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
 2445                         (new_flags &~ IFF_CANTCHANGE);
 2446                 if (ifp->if_ioctl) {
 2447                         (void) (*ifp->if_ioctl)(ifp, cmd, data);
 2448                 }
 2449                 getmicrotime(&ifp->if_lastchange);
 2450                 break;
 2451 
 2452         case SIOCSIFCAP:
 2453                 error = priv_check(td, PRIV_NET_SETIFCAP);
 2454                 if (error)
 2455                         return (error);
 2456                 if (ifp->if_ioctl == NULL)
 2457                         return (EOPNOTSUPP);
 2458                 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
 2459                         return (EINVAL);
 2460                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 2461                 if (error == 0)
 2462                         getmicrotime(&ifp->if_lastchange);
 2463                 break;
 2464 
 2465 #ifdef MAC
 2466         case SIOCSIFMAC:
 2467                 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
 2468                 break;
 2469 #endif
 2470 
 2471         case SIOCSIFNAME:
 2472                 error = priv_check(td, PRIV_NET_SETIFNAME);
 2473                 if (error)
 2474                         return (error);
 2475                 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
 2476                 if (error != 0)
 2477                         return (error);
 2478                 if (new_name[0] == '\0')
 2479                         return (EINVAL);
 2480                 if (new_name[IFNAMSIZ-1] != '\0') {
 2481                         new_name[IFNAMSIZ-1] = '\0';
 2482                         if (strlen(new_name) == IFNAMSIZ-1)
 2483                                 return (EINVAL);
 2484                 }
 2485                 if (ifunit(new_name) != NULL)
 2486                         return (EEXIST);
 2487 
 2488                 /*
 2489                  * XXX: Locking.  Nothing else seems to lock if_flags,
 2490                  * and there are numerous other races with the
 2491                  * ifunit() checks not being atomic with namespace
 2492                  * changes (renames, vmoves, if_attach, etc).
 2493                  */
 2494                 ifp->if_flags |= IFF_RENAMING;
 2495                 
 2496                 /* Announce the departure of the interface. */
 2497                 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
 2498                 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
 2499 
 2500                 log(LOG_INFO, "%s: changing name to '%s'\n",
 2501                     ifp->if_xname, new_name);
 2502 
 2503                 IF_ADDR_WLOCK(ifp);
 2504                 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
 2505                 ifa = ifp->if_addr;
 2506                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
 2507                 namelen = strlen(new_name);
 2508                 onamelen = sdl->sdl_nlen;
 2509                 /*
 2510                  * Move the address if needed.  This is safe because we
 2511                  * allocate space for a name of length IFNAMSIZ when we
 2512                  * create this in if_attach().
 2513                  */
 2514                 if (namelen != onamelen) {
 2515                         bcopy(sdl->sdl_data + onamelen,
 2516                             sdl->sdl_data + namelen, sdl->sdl_alen);
 2517                 }
 2518                 bcopy(new_name, sdl->sdl_data, namelen);
 2519                 sdl->sdl_nlen = namelen;
 2520                 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
 2521                 bzero(sdl->sdl_data, onamelen);
 2522                 while (namelen != 0)
 2523                         sdl->sdl_data[--namelen] = 0xff;
 2524                 IF_ADDR_WUNLOCK(ifp);
 2525 
 2526                 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
 2527                 /* Announce the return of the interface. */
 2528                 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
 2529 
 2530                 ifp->if_flags &= ~IFF_RENAMING;
 2531                 break;
 2532 
 2533 #ifdef VIMAGE
 2534         case SIOCSIFVNET:
 2535                 error = priv_check(td, PRIV_NET_SETIFVNET);
 2536                 if (error)
 2537                         return (error);
 2538                 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
 2539                 break;
 2540 #endif
 2541 
 2542         case SIOCSIFMETRIC:
 2543                 error = priv_check(td, PRIV_NET_SETIFMETRIC);
 2544                 if (error)
 2545                         return (error);
 2546                 ifp->if_metric = ifr->ifr_metric;
 2547                 getmicrotime(&ifp->if_lastchange);
 2548                 break;
 2549 
 2550         case SIOCSIFPHYS:
 2551                 error = priv_check(td, PRIV_NET_SETIFPHYS);
 2552                 if (error)
 2553                         return (error);
 2554                 if (ifp->if_ioctl == NULL)
 2555                         return (EOPNOTSUPP);
 2556                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 2557                 if (error == 0)
 2558                         getmicrotime(&ifp->if_lastchange);
 2559                 break;
 2560 
 2561         case SIOCSIFMTU:
 2562         {
 2563                 u_long oldmtu = ifp->if_mtu;
 2564 
 2565                 error = priv_check(td, PRIV_NET_SETIFMTU);
 2566                 if (error)
 2567                         return (error);
 2568                 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
 2569                         return (EINVAL);
 2570                 if (ifp->if_ioctl == NULL)
 2571                         return (EOPNOTSUPP);
 2572                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 2573                 if (error == 0) {
 2574                         getmicrotime(&ifp->if_lastchange);
 2575                         rt_ifmsg(ifp);
 2576                 }
 2577                 /*
 2578                  * If the link MTU changed, do network layer specific procedure.
 2579                  */
 2580                 if (ifp->if_mtu != oldmtu) {
 2581 #ifdef INET6
 2582                         nd6_setmtu(ifp);
 2583 #endif
 2584                         rt_updatemtu(ifp);
 2585                 }
 2586                 break;
 2587         }
 2588 
 2589         case SIOCADDMULTI:
 2590         case SIOCDELMULTI:
 2591                 if (cmd == SIOCADDMULTI)
 2592                         error = priv_check(td, PRIV_NET_ADDMULTI);
 2593                 else
 2594                         error = priv_check(td, PRIV_NET_DELMULTI);
 2595                 if (error)
 2596                         return (error);
 2597 
 2598                 /* Don't allow group membership on non-multicast interfaces. */
 2599                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
 2600                         return (EOPNOTSUPP);
 2601 
 2602                 /* Don't let users screw up protocols' entries. */
 2603                 if (ifr->ifr_addr.sa_family != AF_LINK)
 2604                         return (EINVAL);
 2605 
 2606                 if (cmd == SIOCADDMULTI) {
 2607                         struct ifmultiaddr *ifma;
 2608 
 2609                         /*
 2610                          * Userland is only permitted to join groups once
 2611                          * via the if_addmulti() KPI, because it cannot hold
 2612                          * struct ifmultiaddr * between calls. It may also
 2613                          * lose a race while we check if the membership
 2614                          * already exists.
 2615                          */
 2616                         IF_ADDR_RLOCK(ifp);
 2617                         ifma = if_findmulti(ifp, &ifr->ifr_addr);
 2618                         IF_ADDR_RUNLOCK(ifp);
 2619                         if (ifma != NULL)
 2620                                 error = EADDRINUSE;
 2621                         else
 2622                                 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
 2623                 } else {
 2624                         error = if_delmulti(ifp, &ifr->ifr_addr);
 2625                 }
 2626                 if (error == 0)
 2627                         getmicrotime(&ifp->if_lastchange);
 2628                 break;
 2629 
 2630         case SIOCSIFPHYADDR:
 2631         case SIOCDIFPHYADDR:
 2632 #ifdef INET6
 2633         case SIOCSIFPHYADDR_IN6:
 2634 #endif
 2635         case SIOCSIFMEDIA:
 2636         case SIOCSIFGENERIC:
 2637                 error = priv_check(td, PRIV_NET_HWIOCTL);
 2638                 if (error)
 2639                         return (error);
 2640                 if (ifp->if_ioctl == NULL)
 2641                         return (EOPNOTSUPP);
 2642                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 2643                 if (error == 0)
 2644                         getmicrotime(&ifp->if_lastchange);
 2645                 break;
 2646 
 2647         case SIOCGIFSTATUS:
 2648         case SIOCGIFPSRCADDR:
 2649         case SIOCGIFPDSTADDR:
 2650         case SIOCGIFMEDIA:
 2651         case SIOCGIFXMEDIA:
 2652         case SIOCGIFGENERIC:
 2653                 if (ifp->if_ioctl == NULL)
 2654                         return (EOPNOTSUPP);
 2655                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 2656                 break;
 2657 
 2658         case SIOCSIFLLADDR:
 2659                 error = priv_check(td, PRIV_NET_SETLLADDR);
 2660                 if (error)
 2661                         return (error);
 2662                 error = if_setlladdr(ifp,
 2663                     ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
 2664                 break;
 2665 
 2666         case SIOCAIFGROUP:
 2667         {
 2668                 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
 2669 
 2670                 error = priv_check(td, PRIV_NET_ADDIFGROUP);
 2671                 if (error)
 2672                         return (error);
 2673                 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
 2674                         return (error);
 2675                 break;
 2676         }
 2677 
 2678         case SIOCGIFGROUP:
 2679                 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
 2680                         return (error);
 2681                 break;
 2682 
 2683         case SIOCDIFGROUP:
 2684         {
 2685                 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
 2686 
 2687                 error = priv_check(td, PRIV_NET_DELIFGROUP);
 2688                 if (error)
 2689                         return (error);
 2690                 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
 2691                         return (error);
 2692                 break;
 2693         }
 2694 
 2695         default:
 2696                 error = ENOIOCTL;
 2697                 break;
 2698         }
 2699         return (error);
 2700 }
 2701 
 2702 /* COMPAT_SVR4 */
 2703 #define OSIOCGIFCONF    _IOWR('i', 20, struct ifconf)
 2704 
 2705 #ifdef COMPAT_FREEBSD32
 2706 struct ifconf32 {
 2707         int32_t ifc_len;
 2708         union {
 2709                 uint32_t        ifcu_buf;
 2710                 uint32_t        ifcu_req;
 2711         } ifc_ifcu;
 2712 };
 2713 #define SIOCGIFCONF32   _IOWR('i', 36, struct ifconf32)
 2714 #endif
 2715 
 2716 /*
 2717  * Interface ioctls.
 2718  */
 2719 int
 2720 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
 2721 {
 2722         struct ifnet *ifp;
 2723         struct ifreq *ifr;
 2724         int error;
 2725         int oif_flags;
 2726 #ifdef VIMAGE
 2727         int shutdown;
 2728 #endif
 2729 
 2730         CURVNET_SET(so->so_vnet);
 2731 #ifdef VIMAGE
 2732         /* Make sure the VNET is stable. */
 2733         shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
 2734                  so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
 2735         if (shutdown) {
 2736                 CURVNET_RESTORE();
 2737                 return (EBUSY);
 2738         }
 2739 #endif
 2740 
 2741 
 2742         switch (cmd) {
 2743         case SIOCGIFCONF:
 2744         case OSIOCGIFCONF:      /* COMPAT_SVR4 */
 2745                 error = ifconf(cmd, data);
 2746                 CURVNET_RESTORE();
 2747                 return (error);
 2748 
 2749 #ifdef COMPAT_FREEBSD32
 2750         case SIOCGIFCONF32:
 2751                 {
 2752                         struct ifconf32 *ifc32;
 2753                         struct ifconf ifc;
 2754 
 2755                         ifc32 = (struct ifconf32 *)data;
 2756                         ifc.ifc_len = ifc32->ifc_len;
 2757                         ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
 2758 
 2759                         error = ifconf(SIOCGIFCONF, (void *)&ifc);
 2760                         CURVNET_RESTORE();
 2761                         if (error == 0)
 2762                                 ifc32->ifc_len = ifc.ifc_len;
 2763                         return (error);
 2764                 }
 2765 #endif
 2766         }
 2767         ifr = (struct ifreq *)data;
 2768 
 2769         switch (cmd) {
 2770 #ifdef VIMAGE
 2771         case SIOCSIFRVNET:
 2772                 error = priv_check(td, PRIV_NET_SETIFVNET);
 2773                 if (error == 0)
 2774                         error = if_vmove_reclaim(td, ifr->ifr_name,
 2775                             ifr->ifr_jid);
 2776                 CURVNET_RESTORE();
 2777                 return (error);
 2778 #endif
 2779         case SIOCIFCREATE:
 2780         case SIOCIFCREATE2:
 2781                 error = priv_check(td, PRIV_NET_IFCREATE);
 2782                 if (error == 0)
 2783                         error = if_clone_create(ifr->ifr_name,
 2784                             sizeof(ifr->ifr_name),
 2785                             cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
 2786                 CURVNET_RESTORE();
 2787                 return (error);
 2788         case SIOCIFDESTROY:
 2789                 error = priv_check(td, PRIV_NET_IFDESTROY);
 2790                 if (error == 0)
 2791                         error = if_clone_destroy(ifr->ifr_name);
 2792                 CURVNET_RESTORE();
 2793                 return (error);
 2794 
 2795         case SIOCIFGCLONERS:
 2796                 error = if_clone_list((struct if_clonereq *)data);
 2797                 CURVNET_RESTORE();
 2798                 return (error);
 2799         case SIOCGIFGMEMB:
 2800                 error = if_getgroupmembers((struct ifgroupreq *)data);
 2801                 CURVNET_RESTORE();
 2802                 return (error);
 2803 #if defined(INET) || defined(INET6)
 2804         case SIOCSVH:
 2805         case SIOCGVH:
 2806                 if (carp_ioctl_p == NULL)
 2807                         error = EPROTONOSUPPORT;
 2808                 else
 2809                         error = (*carp_ioctl_p)(ifr, cmd, td);
 2810                 CURVNET_RESTORE();
 2811                 return (error);
 2812 #endif
 2813         }
 2814 
 2815         ifp = ifunit_ref(ifr->ifr_name);
 2816         if (ifp == NULL) {
 2817                 CURVNET_RESTORE();
 2818                 return (ENXIO);
 2819         }
 2820 
 2821         error = ifhwioctl(cmd, ifp, data, td);
 2822         if (error != ENOIOCTL) {
 2823                 if_rele(ifp);
 2824                 CURVNET_RESTORE();
 2825                 return (error);
 2826         }
 2827 
 2828         oif_flags = ifp->if_flags;
 2829         if (so->so_proto == NULL) {
 2830                 if_rele(ifp);
 2831                 CURVNET_RESTORE();
 2832                 return (EOPNOTSUPP);
 2833         }
 2834 
 2835         /*
 2836          * Pass the request on to the socket control method, and if the
 2837          * latter returns EOPNOTSUPP, directly to the interface.
 2838          *
 2839          * Make an exception for the legacy SIOCSIF* requests.  Drivers
 2840          * trust SIOCSIFADDR et al to come from an already privileged
 2841          * layer, and do not perform any credentials checks or input
 2842          * validation.
 2843          */
 2844         error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
 2845             ifp, td));
 2846         if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
 2847             cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
 2848             cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
 2849                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 2850 
 2851         if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
 2852 #ifdef INET6
 2853                 if (ifp->if_flags & IFF_UP)
 2854                         in6_if_up(ifp);
 2855 #endif
 2856         }
 2857         if_rele(ifp);
 2858         CURVNET_RESTORE();
 2859         return (error);
 2860 }
 2861 
 2862 /*
 2863  * The code common to handling reference counted flags,
 2864  * e.g., in ifpromisc() and if_allmulti().
 2865  * The "pflag" argument can specify a permanent mode flag to check,
 2866  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
 2867  *
 2868  * Only to be used on stack-owned flags, not driver-owned flags.
 2869  */
 2870 static int
 2871 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
 2872 {
 2873         struct ifreq ifr;
 2874         int error;
 2875         int oldflags, oldcount;
 2876 
 2877         /* Sanity checks to catch programming errors */
 2878         KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
 2879             ("%s: setting driver-owned flag %d", __func__, flag));
 2880 
 2881         if (onswitch)
 2882                 KASSERT(*refcount >= 0,
 2883                     ("%s: increment negative refcount %d for flag %d",
 2884                     __func__, *refcount, flag));
 2885         else
 2886                 KASSERT(*refcount > 0,
 2887                     ("%s: decrement non-positive refcount %d for flag %d",
 2888                     __func__, *refcount, flag));
 2889 
 2890         /* In case this mode is permanent, just touch refcount */
 2891         if (ifp->if_flags & pflag) {
 2892                 *refcount += onswitch ? 1 : -1;
 2893                 return (0);
 2894         }
 2895 
 2896         /* Save ifnet parameters for if_ioctl() may fail */
 2897         oldcount = *refcount;
 2898         oldflags = ifp->if_flags;
 2899         
 2900         /*
 2901          * See if we aren't the only and touching refcount is enough.
 2902          * Actually toggle interface flag if we are the first or last.
 2903          */
 2904         if (onswitch) {
 2905                 if ((*refcount)++)
 2906                         return (0);
 2907                 ifp->if_flags |= flag;
 2908         } else {
 2909                 if (--(*refcount))
 2910                         return (0);
 2911                 ifp->if_flags &= ~flag;
 2912         }
 2913 
 2914         /* Call down the driver since we've changed interface flags */
 2915         if (ifp->if_ioctl == NULL) {
 2916                 error = EOPNOTSUPP;
 2917                 goto recover;
 2918         }
 2919         ifr.ifr_flags = ifp->if_flags & 0xffff;
 2920         ifr.ifr_flagshigh = ifp->if_flags >> 16;
 2921         error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
 2922         if (error)
 2923                 goto recover;
 2924         /* Notify userland that interface flags have changed */
 2925         rt_ifmsg(ifp);
 2926         return (0);
 2927 
 2928 recover:
 2929         /* Recover after driver error */
 2930         *refcount = oldcount;
 2931         ifp->if_flags = oldflags;
 2932         return (error);
 2933 }
 2934 
 2935 /*
 2936  * Set/clear promiscuous mode on interface ifp based on the truth value
 2937  * of pswitch.  The calls are reference counted so that only the first
 2938  * "on" request actually has an effect, as does the final "off" request.
 2939  * Results are undefined if the "off" and "on" requests are not matched.
 2940  */
 2941 int
 2942 ifpromisc(struct ifnet *ifp, int pswitch)
 2943 {
 2944         int error;
 2945         int oldflags = ifp->if_flags;
 2946 
 2947         error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
 2948                            &ifp->if_pcount, pswitch);
 2949         /* If promiscuous mode status has changed, log a message */
 2950         if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
 2951             log_promisc_mode_change)
 2952                 log(LOG_INFO, "%s: promiscuous mode %s\n",
 2953                     ifp->if_xname,
 2954                     (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
 2955         return (error);
 2956 }
 2957 
 2958 /*
 2959  * Return interface configuration
 2960  * of system.  List may be used
 2961  * in later ioctl's (above) to get
 2962  * other information.
 2963  */
 2964 /*ARGSUSED*/
 2965 static int
 2966 ifconf(u_long cmd, caddr_t data)
 2967 {
 2968         struct ifconf *ifc = (struct ifconf *)data;
 2969         struct ifnet *ifp;
 2970         struct ifaddr *ifa;
 2971         struct ifreq ifr;
 2972         struct sbuf *sb;
 2973         int error, full = 0, valid_len, max_len;
 2974 
 2975         /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
 2976         max_len = MAXPHYS - 1;
 2977 
 2978         /* Prevent hostile input from being able to crash the system */
 2979         if (ifc->ifc_len <= 0)
 2980                 return (EINVAL);
 2981 
 2982 again:
 2983         if (ifc->ifc_len <= max_len) {
 2984                 max_len = ifc->ifc_len;
 2985                 full = 1;
 2986         }
 2987         sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
 2988         max_len = 0;
 2989         valid_len = 0;
 2990 
 2991         IFNET_RLOCK();
 2992         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 2993                 int addrs;
 2994 
 2995                 /*
 2996                  * Zero the ifr_name buffer to make sure we don't
 2997                  * disclose the contents of the stack.
 2998                  */
 2999                 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
 3000 
 3001                 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
 3002                     >= sizeof(ifr.ifr_name)) {
 3003                         sbuf_delete(sb);
 3004                         IFNET_RUNLOCK();
 3005                         return (ENAMETOOLONG);
 3006                 }
 3007 
 3008                 addrs = 0;
 3009                 IF_ADDR_RLOCK(ifp);
 3010                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 3011                         struct sockaddr *sa = ifa->ifa_addr;
 3012 
 3013                         if (prison_if(curthread->td_ucred, sa) != 0)
 3014                                 continue;
 3015                         addrs++;
 3016                         /* COMPAT_SVR4 */
 3017                         if (cmd == OSIOCGIFCONF) {
 3018                                 struct osockaddr *osa =
 3019                                     (struct osockaddr *)&ifr.ifr_addr;
 3020                                 ifr.ifr_addr = *sa;
 3021                                 osa->sa_family = sa->sa_family;
 3022                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
 3023                                 max_len += sizeof(ifr);
 3024                         } else
 3025                         if (sa->sa_len <= sizeof(*sa)) {
 3026                                 ifr.ifr_addr = *sa;
 3027                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
 3028                                 max_len += sizeof(ifr);
 3029                         } else {
 3030                                 sbuf_bcat(sb, &ifr,
 3031                                     offsetof(struct ifreq, ifr_addr));
 3032                                 max_len += offsetof(struct ifreq, ifr_addr);
 3033                                 sbuf_bcat(sb, sa, sa->sa_len);
 3034                                 max_len += sa->sa_len;
 3035                         }
 3036 
 3037                         if (sbuf_error(sb) == 0)
 3038                                 valid_len = sbuf_len(sb);
 3039                 }
 3040                 IF_ADDR_RUNLOCK(ifp);
 3041                 if (addrs == 0) {
 3042                         bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
 3043                         sbuf_bcat(sb, &ifr, sizeof(ifr));
 3044                         max_len += sizeof(ifr);
 3045 
 3046                         if (sbuf_error(sb) == 0)
 3047                                 valid_len = sbuf_len(sb);
 3048                 }
 3049         }
 3050         IFNET_RUNLOCK();
 3051 
 3052         /*
 3053          * If we didn't allocate enough space (uncommon), try again.  If
 3054          * we have already allocated as much space as we are allowed,
 3055          * return what we've got.
 3056          */
 3057         if (valid_len != max_len && !full) {
 3058                 sbuf_delete(sb);
 3059                 goto again;
 3060         }
 3061 
 3062         ifc->ifc_len = valid_len;
 3063         sbuf_finish(sb);
 3064         error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
 3065         sbuf_delete(sb);
 3066         return (error);
 3067 }
 3068 
 3069 /*
 3070  * Just like ifpromisc(), but for all-multicast-reception mode.
 3071  */
 3072 int
 3073 if_allmulti(struct ifnet *ifp, int onswitch)
 3074 {
 3075 
 3076         return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
 3077 }
 3078 
 3079 struct ifmultiaddr *
 3080 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
 3081 {
 3082         struct ifmultiaddr *ifma;
 3083 
 3084         IF_ADDR_LOCK_ASSERT(ifp);
 3085 
 3086         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 3087                 if (sa->sa_family == AF_LINK) {
 3088                         if (sa_dl_equal(ifma->ifma_addr, sa))
 3089                                 break;
 3090                 } else {
 3091                         if (sa_equal(ifma->ifma_addr, sa))
 3092                                 break;
 3093                 }
 3094         }
 3095 
 3096         return ifma;
 3097 }
 3098 
 3099 /*
 3100  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
 3101  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
 3102  * the ifnet multicast address list here, so the caller must do that and
 3103  * other setup work (such as notifying the device driver).  The reference
 3104  * count is initialized to 1.
 3105  */
 3106 static struct ifmultiaddr *
 3107 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
 3108     int mflags)
 3109 {
 3110         struct ifmultiaddr *ifma;
 3111         struct sockaddr *dupsa;
 3112 
 3113         ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
 3114             M_ZERO);
 3115         if (ifma == NULL)
 3116                 return (NULL);
 3117 
 3118         dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
 3119         if (dupsa == NULL) {
 3120                 free(ifma, M_IFMADDR);
 3121                 return (NULL);
 3122         }
 3123         bcopy(sa, dupsa, sa->sa_len);
 3124         ifma->ifma_addr = dupsa;
 3125 
 3126         ifma->ifma_ifp = ifp;
 3127         ifma->ifma_refcount = 1;
 3128         ifma->ifma_protospec = NULL;
 3129 
 3130         if (llsa == NULL) {
 3131                 ifma->ifma_lladdr = NULL;
 3132                 return (ifma);
 3133         }
 3134 
 3135         dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
 3136         if (dupsa == NULL) {
 3137                 free(ifma->ifma_addr, M_IFMADDR);
 3138                 free(ifma, M_IFMADDR);
 3139                 return (NULL);
 3140         }
 3141         bcopy(llsa, dupsa, llsa->sa_len);
 3142         ifma->ifma_lladdr = dupsa;
 3143 
 3144         return (ifma);
 3145 }
 3146 
 3147 /*
 3148  * if_freemulti: free ifmultiaddr structure and possibly attached related
 3149  * addresses.  The caller is responsible for implementing reference
 3150  * counting, notifying the driver, handling routing messages, and releasing
 3151  * any dependent link layer state.
 3152  */
 3153 static void
 3154 if_freemulti(struct ifmultiaddr *ifma)
 3155 {
 3156 
 3157         KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
 3158             ifma->ifma_refcount));
 3159 
 3160         if (ifma->ifma_lladdr != NULL)
 3161                 free(ifma->ifma_lladdr, M_IFMADDR);
 3162         free(ifma->ifma_addr, M_IFMADDR);
 3163         free(ifma, M_IFMADDR);
 3164 }
 3165 
 3166 /*
 3167  * Register an additional multicast address with a network interface.
 3168  *
 3169  * - If the address is already present, bump the reference count on the
 3170  *   address and return.
 3171  * - If the address is not link-layer, look up a link layer address.
 3172  * - Allocate address structures for one or both addresses, and attach to the
 3173  *   multicast address list on the interface.  If automatically adding a link
 3174  *   layer address, the protocol address will own a reference to the link
 3175  *   layer address, to be freed when it is freed.
 3176  * - Notify the network device driver of an addition to the multicast address
 3177  *   list.
 3178  *
 3179  * 'sa' points to caller-owned memory with the desired multicast address.
 3180  *
 3181  * 'retifma' will be used to return a pointer to the resulting multicast
 3182  * address reference, if desired.
 3183  */
 3184 int
 3185 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
 3186     struct ifmultiaddr **retifma)
 3187 {
 3188         struct ifmultiaddr *ifma, *ll_ifma;
 3189         struct sockaddr *llsa;
 3190         struct sockaddr_dl sdl;
 3191         int error;
 3192 
 3193         /*
 3194          * If the address is already present, return a new reference to it;
 3195          * otherwise, allocate storage and set up a new address.
 3196          */
 3197         IF_ADDR_WLOCK(ifp);
 3198         ifma = if_findmulti(ifp, sa);
 3199         if (ifma != NULL) {
 3200                 ifma->ifma_refcount++;
 3201                 if (retifma != NULL)
 3202                         *retifma = ifma;
 3203                 IF_ADDR_WUNLOCK(ifp);
 3204                 return (0);
 3205         }
 3206 
 3207         /*
 3208          * The address isn't already present; resolve the protocol address
 3209          * into a link layer address, and then look that up, bump its
 3210          * refcount or allocate an ifma for that also.
 3211          * Most link layer resolving functions returns address data which
 3212          * fits inside default sockaddr_dl structure. However callback
 3213          * can allocate another sockaddr structure, in that case we need to
 3214          * free it later.
 3215          */
 3216         llsa = NULL;
 3217         ll_ifma = NULL;
 3218         if (ifp->if_resolvemulti != NULL) {
 3219                 /* Provide called function with buffer size information */
 3220                 sdl.sdl_len = sizeof(sdl);
 3221                 llsa = (struct sockaddr *)&sdl;
 3222                 error = ifp->if_resolvemulti(ifp, &llsa, sa);
 3223                 if (error)
 3224                         goto unlock_out;
 3225         }
 3226 
 3227         /*
 3228          * Allocate the new address.  Don't hook it up yet, as we may also
 3229          * need to allocate a link layer multicast address.
 3230          */
 3231         ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
 3232         if (ifma == NULL) {
 3233                 error = ENOMEM;
 3234                 goto free_llsa_out;
 3235         }
 3236 
 3237         /*
 3238          * If a link layer address is found, we'll need to see if it's
 3239          * already present in the address list, or allocate is as well.
 3240          * When this block finishes, the link layer address will be on the
 3241          * list.
 3242          */
 3243         if (llsa != NULL) {
 3244                 ll_ifma = if_findmulti(ifp, llsa);
 3245                 if (ll_ifma == NULL) {
 3246                         ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
 3247                         if (ll_ifma == NULL) {
 3248                                 --ifma->ifma_refcount;
 3249                                 if_freemulti(ifma);
 3250                                 error = ENOMEM;
 3251                                 goto free_llsa_out;
 3252                         }
 3253                         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
 3254                             ifma_link);
 3255                 } else
 3256                         ll_ifma->ifma_refcount++;
 3257                 ifma->ifma_llifma = ll_ifma;
 3258         }
 3259 
 3260         /*
 3261          * We now have a new multicast address, ifma, and possibly a new or
 3262          * referenced link layer address.  Add the primary address to the
 3263          * ifnet address list.
 3264          */
 3265         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
 3266 
 3267         if (retifma != NULL)
 3268                 *retifma = ifma;
 3269 
 3270         /*
 3271          * Must generate the message while holding the lock so that 'ifma'
 3272          * pointer is still valid.
 3273          */
 3274         rt_newmaddrmsg(RTM_NEWMADDR, ifma);
 3275         IF_ADDR_WUNLOCK(ifp);
 3276 
 3277         /*
 3278          * We are certain we have added something, so call down to the
 3279          * interface to let them know about it.
 3280          */
 3281         if (ifp->if_ioctl != NULL) {
 3282                 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
 3283         }
 3284 
 3285         if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
 3286                 link_free_sdl(llsa);
 3287 
 3288         return (0);
 3289 
 3290 free_llsa_out:
 3291         if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
 3292                 link_free_sdl(llsa);
 3293 
 3294 unlock_out:
 3295         IF_ADDR_WUNLOCK(ifp);
 3296         return (error);
 3297 }
 3298 
 3299 /*
 3300  * Delete a multicast group membership by network-layer group address.
 3301  *
 3302  * Returns ENOENT if the entry could not be found. If ifp no longer
 3303  * exists, results are undefined. This entry point should only be used
 3304  * from subsystems which do appropriate locking to hold ifp for the
 3305  * duration of the call.
 3306  * Network-layer protocol domains must use if_delmulti_ifma().
 3307  */
 3308 int
 3309 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
 3310 {
 3311         struct ifmultiaddr *ifma;
 3312         int lastref;
 3313 #ifdef INVARIANTS
 3314         struct ifnet *oifp;
 3315 
 3316         IFNET_RLOCK_NOSLEEP();
 3317         TAILQ_FOREACH(oifp, &V_ifnet, if_link)
 3318                 if (ifp == oifp)
 3319                         break;
 3320         if (ifp != oifp)
 3321                 ifp = NULL;
 3322         IFNET_RUNLOCK_NOSLEEP();
 3323 
 3324         KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
 3325 #endif
 3326         if (ifp == NULL)
 3327                 return (ENOENT);
 3328 
 3329         IF_ADDR_WLOCK(ifp);
 3330         lastref = 0;
 3331         ifma = if_findmulti(ifp, sa);
 3332         if (ifma != NULL)
 3333                 lastref = if_delmulti_locked(ifp, ifma, 0);
 3334         IF_ADDR_WUNLOCK(ifp);
 3335 
 3336         if (ifma == NULL)
 3337                 return (ENOENT);
 3338 
 3339         if (lastref && ifp->if_ioctl != NULL) {
 3340                 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
 3341         }
 3342 
 3343         return (0);
 3344 }
 3345 
 3346 /*
 3347  * Delete all multicast group membership for an interface.
 3348  * Should be used to quickly flush all multicast filters.
 3349  */
 3350 void
 3351 if_delallmulti(struct ifnet *ifp)
 3352 {
 3353         struct ifmultiaddr *ifma;
 3354         struct ifmultiaddr *next;
 3355 
 3356         IF_ADDR_WLOCK(ifp);
 3357         TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
 3358                 if_delmulti_locked(ifp, ifma, 0);
 3359         IF_ADDR_WUNLOCK(ifp);
 3360 }
 3361 
 3362 /*
 3363  * Delete a multicast group membership by group membership pointer.
 3364  * Network-layer protocol domains must use this routine.
 3365  *
 3366  * It is safe to call this routine if the ifp disappeared.
 3367  */
 3368 void
 3369 if_delmulti_ifma(struct ifmultiaddr *ifma)
 3370 {
 3371         struct ifnet *ifp;
 3372         int lastref;
 3373 
 3374         ifp = ifma->ifma_ifp;
 3375 #ifdef DIAGNOSTIC
 3376         if (ifp == NULL) {
 3377                 printf("%s: ifma_ifp seems to be detached\n", __func__);
 3378         } else {
 3379                 struct ifnet *oifp;
 3380 
 3381                 IFNET_RLOCK_NOSLEEP();
 3382                 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
 3383                         if (ifp == oifp)
 3384                                 break;
 3385                 if (ifp != oifp) {
 3386                         printf("%s: ifnet %p disappeared\n", __func__, ifp);
 3387                         ifp = NULL;
 3388                 }
 3389                 IFNET_RUNLOCK_NOSLEEP();
 3390         }
 3391 #endif
 3392         /*
 3393          * If and only if the ifnet instance exists: Acquire the address lock.
 3394          */
 3395         if (ifp != NULL)
 3396                 IF_ADDR_WLOCK(ifp);
 3397 
 3398         lastref = if_delmulti_locked(ifp, ifma, 0);
 3399 
 3400         if (ifp != NULL) {
 3401                 /*
 3402                  * If and only if the ifnet instance exists:
 3403                  *  Release the address lock.
 3404                  *  If the group was left: update the hardware hash filter.
 3405                  */
 3406                 IF_ADDR_WUNLOCK(ifp);
 3407                 if (lastref && ifp->if_ioctl != NULL) {
 3408                         (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
 3409                 }
 3410         }
 3411 }
 3412 
 3413 /*
 3414  * Perform deletion of network-layer and/or link-layer multicast address.
 3415  *
 3416  * Return 0 if the reference count was decremented.
 3417  * Return 1 if the final reference was released, indicating that the
 3418  * hardware hash filter should be reprogrammed.
 3419  */
 3420 static int
 3421 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
 3422 {
 3423         struct ifmultiaddr *ll_ifma;
 3424 
 3425         if (ifp != NULL && ifma->ifma_ifp != NULL) {
 3426                 KASSERT(ifma->ifma_ifp == ifp,
 3427                     ("%s: inconsistent ifp %p", __func__, ifp));
 3428                 IF_ADDR_WLOCK_ASSERT(ifp);
 3429         }
 3430 
 3431         ifp = ifma->ifma_ifp;
 3432 
 3433         /*
 3434          * If the ifnet is detaching, null out references to ifnet,
 3435          * so that upper protocol layers will notice, and not attempt
 3436          * to obtain locks for an ifnet which no longer exists. The
 3437          * routing socket announcement must happen before the ifnet
 3438          * instance is detached from the system.
 3439          */
 3440         if (detaching) {
 3441 #ifdef DIAGNOSTIC
 3442                 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
 3443 #endif
 3444                 /*
 3445                  * ifp may already be nulled out if we are being reentered
 3446                  * to delete the ll_ifma.
 3447                  */
 3448                 if (ifp != NULL) {
 3449                         rt_newmaddrmsg(RTM_DELMADDR, ifma);
 3450                         ifma->ifma_ifp = NULL;
 3451                 }
 3452         }
 3453 
 3454         if (--ifma->ifma_refcount > 0)
 3455                 return 0;
 3456 
 3457         /*
 3458          * If this ifma is a network-layer ifma, a link-layer ifma may
 3459          * have been associated with it. Release it first if so.
 3460          */
 3461         ll_ifma = ifma->ifma_llifma;
 3462         if (ll_ifma != NULL) {
 3463                 KASSERT(ifma->ifma_lladdr != NULL,
 3464                     ("%s: llifma w/o lladdr", __func__));
 3465                 if (detaching)
 3466                         ll_ifma->ifma_ifp = NULL;       /* XXX */
 3467                 if (--ll_ifma->ifma_refcount == 0) {
 3468                         if (ifp != NULL) {
 3469                                 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
 3470                                     ifma_link);
 3471                         }
 3472                         if_freemulti(ll_ifma);
 3473                 }
 3474         }
 3475 
 3476         if (ifp != NULL)
 3477                 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
 3478 
 3479         if_freemulti(ifma);
 3480 
 3481         /*
 3482          * The last reference to this instance of struct ifmultiaddr
 3483          * was released; the hardware should be notified of this change.
 3484          */
 3485         return 1;
 3486 }
 3487 
 3488 /*
 3489  * Set the link layer address on an interface.
 3490  *
 3491  * At this time we only support certain types of interfaces,
 3492  * and we don't allow the length of the address to change.
 3493  *
 3494  * Set noinline to be dtrace-friendly
 3495  */
 3496 __noinline int
 3497 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
 3498 {
 3499         struct sockaddr_dl *sdl;
 3500         struct ifaddr *ifa;
 3501         struct ifreq ifr;
 3502 
 3503         IF_ADDR_RLOCK(ifp);
 3504         ifa = ifp->if_addr;
 3505         if (ifa == NULL) {
 3506                 IF_ADDR_RUNLOCK(ifp);
 3507                 return (EINVAL);
 3508         }
 3509         ifa_ref(ifa);
 3510         IF_ADDR_RUNLOCK(ifp);
 3511         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
 3512         if (sdl == NULL) {
 3513                 ifa_free(ifa);
 3514                 return (EINVAL);
 3515         }
 3516         if (len != sdl->sdl_alen) {     /* don't allow length to change */
 3517                 ifa_free(ifa);
 3518                 return (EINVAL);
 3519         }
 3520         switch (ifp->if_type) {
 3521         case IFT_ETHER:
 3522         case IFT_FDDI:
 3523         case IFT_XETHER:
 3524         case IFT_ISO88025:
 3525         case IFT_L2VLAN:
 3526         case IFT_BRIDGE:
 3527         case IFT_ARCNET:
 3528         case IFT_IEEE8023ADLAG:
 3529         case IFT_IEEE80211:
 3530                 bcopy(lladdr, LLADDR(sdl), len);
 3531                 ifa_free(ifa);
 3532                 break;
 3533         default:
 3534                 ifa_free(ifa);
 3535                 return (ENODEV);
 3536         }
 3537 
 3538         /*
 3539          * If the interface is already up, we need
 3540          * to re-init it in order to reprogram its
 3541          * address filter.
 3542          */
 3543         if ((ifp->if_flags & IFF_UP) != 0) {
 3544                 if (ifp->if_ioctl) {
 3545                         ifp->if_flags &= ~IFF_UP;
 3546                         ifr.ifr_flags = ifp->if_flags & 0xffff;
 3547                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
 3548                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
 3549                         ifp->if_flags |= IFF_UP;
 3550                         ifr.ifr_flags = ifp->if_flags & 0xffff;
 3551                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
 3552                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
 3553                 }
 3554         }
 3555         EVENTHANDLER_INVOKE(iflladdr_event, ifp);
 3556         return (0);
 3557 }
 3558 
 3559 /*
 3560  * Compat function for handling basic encapsulation requests.
 3561  * Not converted stacks (FDDI, IB, ..) supports traditional
 3562  * output model: ARP (and other similar L2 protocols) are handled
 3563  * inside output routine, arpresolve/nd6_resolve() returns MAC
 3564  * address instead of full prepend.
 3565  *
 3566  * This function creates calculated header==MAC for IPv4/IPv6 and
 3567  * returns EAFNOSUPPORT (which is then handled in ARP code) for other
 3568  * address families.
 3569  */
 3570 static int
 3571 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
 3572 {
 3573 
 3574         if (req->rtype != IFENCAP_LL)
 3575                 return (EOPNOTSUPP);
 3576 
 3577         if (req->bufsize < req->lladdr_len)
 3578                 return (ENOMEM);
 3579 
 3580         switch (req->family) {
 3581         case AF_INET:
 3582         case AF_INET6:
 3583                 break;
 3584         default:
 3585                 return (EAFNOSUPPORT);
 3586         }
 3587 
 3588         /* Copy lladdr to storage as is */
 3589         memmove(req->buf, req->lladdr, req->lladdr_len);
 3590         req->bufsize = req->lladdr_len;
 3591         req->lladdr_off = 0;
 3592 
 3593         return (0);
 3594 }
 3595 
 3596 /*
 3597  * The name argument must be a pointer to storage which will last as
 3598  * long as the interface does.  For physical devices, the result of
 3599  * device_get_name(dev) is a good choice and for pseudo-devices a
 3600  * static string works well.
 3601  */
 3602 void
 3603 if_initname(struct ifnet *ifp, const char *name, int unit)
 3604 {
 3605         ifp->if_dname = name;
 3606         ifp->if_dunit = unit;
 3607         if (unit != IF_DUNIT_NONE)
 3608                 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
 3609         else
 3610                 strlcpy(ifp->if_xname, name, IFNAMSIZ);
 3611 }
 3612 
 3613 int
 3614 if_printf(struct ifnet *ifp, const char * fmt, ...)
 3615 {
 3616         va_list ap;
 3617         int retval;
 3618 
 3619         retval = printf("%s: ", ifp->if_xname);
 3620         va_start(ap, fmt);
 3621         retval += vprintf(fmt, ap);
 3622         va_end(ap);
 3623         return (retval);
 3624 }
 3625 
 3626 void
 3627 if_start(struct ifnet *ifp)
 3628 {
 3629 
 3630         (*(ifp)->if_start)(ifp);
 3631 }
 3632 
 3633 /*
 3634  * Backwards compatibility interface for drivers 
 3635  * that have not implemented it
 3636  */
 3637 static int
 3638 if_transmit(struct ifnet *ifp, struct mbuf *m)
 3639 {
 3640         int error;
 3641 
 3642         IFQ_HANDOFF(ifp, m, error);
 3643         return (error);
 3644 }
 3645 
 3646 static void
 3647 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
 3648 {
 3649 
 3650         m_freem(m);
 3651 }
 3652 
 3653 int
 3654 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
 3655 {
 3656         int active = 0;
 3657 
 3658         IF_LOCK(ifq);
 3659         if (_IF_QFULL(ifq)) {
 3660                 IF_UNLOCK(ifq);
 3661                 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
 3662                 m_freem(m);
 3663                 return (0);
 3664         }
 3665         if (ifp != NULL) {
 3666                 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
 3667                 if (m->m_flags & (M_BCAST|M_MCAST))
 3668                         if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
 3669                 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
 3670         }
 3671         _IF_ENQUEUE(ifq, m);
 3672         IF_UNLOCK(ifq);
 3673         if (ifp != NULL && !active)
 3674                 (*(ifp)->if_start)(ifp);
 3675         return (1);
 3676 }
 3677 
 3678 void
 3679 if_register_com_alloc(u_char type,
 3680     if_com_alloc_t *a, if_com_free_t *f)
 3681 {
 3682         
 3683         KASSERT(if_com_alloc[type] == NULL,
 3684             ("if_register_com_alloc: %d already registered", type));
 3685         KASSERT(if_com_free[type] == NULL,
 3686             ("if_register_com_alloc: %d free already registered", type));
 3687 
 3688         if_com_alloc[type] = a;
 3689         if_com_free[type] = f;
 3690 }
 3691 
 3692 void
 3693 if_deregister_com_alloc(u_char type)
 3694 {
 3695         
 3696         KASSERT(if_com_alloc[type] != NULL,
 3697             ("if_deregister_com_alloc: %d not registered", type));
 3698         KASSERT(if_com_free[type] != NULL,
 3699             ("if_deregister_com_alloc: %d free not registered", type));
 3700         if_com_alloc[type] = NULL;
 3701         if_com_free[type] = NULL;
 3702 }
 3703 
 3704 /* API for driver access to network stack owned ifnet.*/
 3705 uint64_t
 3706 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
 3707 {
 3708         uint64_t oldbrate;
 3709 
 3710         oldbrate = ifp->if_baudrate;
 3711         ifp->if_baudrate = baudrate;
 3712         return (oldbrate);
 3713 }
 3714 
 3715 uint64_t
 3716 if_getbaudrate(if_t ifp)
 3717 {
 3718 
 3719         return (((struct ifnet *)ifp)->if_baudrate);
 3720 }
 3721 
 3722 int
 3723 if_setcapabilities(if_t ifp, int capabilities)
 3724 {
 3725         ((struct ifnet *)ifp)->if_capabilities = capabilities;
 3726         return (0);
 3727 }
 3728 
 3729 int
 3730 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
 3731 {
 3732         ((struct ifnet *)ifp)->if_capabilities |= setbit;
 3733         ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
 3734 
 3735         return (0);
 3736 }
 3737 
 3738 int
 3739 if_getcapabilities(if_t ifp)
 3740 {
 3741         return ((struct ifnet *)ifp)->if_capabilities;
 3742 }
 3743 
 3744 int 
 3745 if_setcapenable(if_t ifp, int capabilities)
 3746 {
 3747         ((struct ifnet *)ifp)->if_capenable = capabilities;
 3748         return (0);
 3749 }
 3750 
 3751 int 
 3752 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
 3753 {
 3754         if(setcap) 
 3755                 ((struct ifnet *)ifp)->if_capenable |= setcap;
 3756         if(clearcap)
 3757                 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
 3758 
 3759         return (0);
 3760 }
 3761 
 3762 const char *
 3763 if_getdname(if_t ifp)
 3764 {
 3765         return ((struct ifnet *)ifp)->if_dname;
 3766 }
 3767 
 3768 int 
 3769 if_togglecapenable(if_t ifp, int togglecap)
 3770 {
 3771         ((struct ifnet *)ifp)->if_capenable ^= togglecap;
 3772         return (0);
 3773 }
 3774 
 3775 int
 3776 if_getcapenable(if_t ifp)
 3777 {
 3778         return ((struct ifnet *)ifp)->if_capenable;
 3779 }
 3780 
 3781 /*
 3782  * This is largely undesirable because it ties ifnet to a device, but does
 3783  * provide flexiblity for an embedded product vendor. Should be used with
 3784  * the understanding that it violates the interface boundaries, and should be
 3785  * a last resort only.
 3786  */
 3787 int
 3788 if_setdev(if_t ifp, void *dev)
 3789 {
 3790         return (0);
 3791 }
 3792 
 3793 int
 3794 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
 3795 {
 3796         ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
 3797         ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
 3798 
 3799         return (0);
 3800 }
 3801 
 3802 int
 3803 if_getdrvflags(if_t ifp)
 3804 {
 3805         return ((struct ifnet *)ifp)->if_drv_flags;
 3806 }
 3807  
 3808 int
 3809 if_setdrvflags(if_t ifp, int flags)
 3810 {
 3811         ((struct ifnet *)ifp)->if_drv_flags = flags;
 3812         return (0);
 3813 }
 3814 
 3815 
 3816 int
 3817 if_setflags(if_t ifp, int flags)
 3818 {
 3819         ((struct ifnet *)ifp)->if_flags = flags;
 3820         return (0);
 3821 }
 3822 
 3823 int
 3824 if_setflagbits(if_t ifp, int set, int clear)
 3825 {
 3826         ((struct ifnet *)ifp)->if_flags |= set;
 3827         ((struct ifnet *)ifp)->if_flags &= ~clear;
 3828 
 3829         return (0);
 3830 }
 3831 
 3832 int
 3833 if_getflags(if_t ifp)
 3834 {
 3835         return ((struct ifnet *)ifp)->if_flags;
 3836 }
 3837 
 3838 int
 3839 if_clearhwassist(if_t ifp)
 3840 {
 3841         ((struct ifnet *)ifp)->if_hwassist = 0;
 3842         return (0);
 3843 }
 3844 
 3845 int
 3846 if_sethwassistbits(if_t ifp, int toset, int toclear)
 3847 {
 3848         ((struct ifnet *)ifp)->if_hwassist |= toset;
 3849         ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
 3850 
 3851         return (0);
 3852 }
 3853 
 3854 int
 3855 if_sethwassist(if_t ifp, int hwassist_bit)
 3856 {
 3857         ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
 3858         return (0);
 3859 }
 3860 
 3861 int
 3862 if_gethwassist(if_t ifp)
 3863 {
 3864         return ((struct ifnet *)ifp)->if_hwassist;
 3865 }
 3866 
 3867 int
 3868 if_setmtu(if_t ifp, int mtu)
 3869 {
 3870         ((struct ifnet *)ifp)->if_mtu = mtu;
 3871         return (0);
 3872 }
 3873 
 3874 int
 3875 if_getmtu(if_t ifp)
 3876 {
 3877         return ((struct ifnet *)ifp)->if_mtu;
 3878 }
 3879 
 3880 int
 3881 if_getmtu_family(if_t ifp, int family)
 3882 {
 3883         struct domain *dp;
 3884 
 3885         for (dp = domains; dp; dp = dp->dom_next) {
 3886                 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
 3887                         return (dp->dom_ifmtu((struct ifnet *)ifp));
 3888         }
 3889 
 3890         return (((struct ifnet *)ifp)->if_mtu);
 3891 }
 3892 
 3893 int
 3894 if_setsoftc(if_t ifp, void *softc)
 3895 {
 3896         ((struct ifnet *)ifp)->if_softc = softc;
 3897         return (0);
 3898 }
 3899 
 3900 void *
 3901 if_getsoftc(if_t ifp)
 3902 {
 3903         return ((struct ifnet *)ifp)->if_softc;
 3904 }
 3905 
 3906 void 
 3907 if_setrcvif(struct mbuf *m, if_t ifp)
 3908 {
 3909         m->m_pkthdr.rcvif = (struct ifnet *)ifp;
 3910 }
 3911 
 3912 void 
 3913 if_setvtag(struct mbuf *m, uint16_t tag)
 3914 {
 3915         m->m_pkthdr.ether_vtag = tag;   
 3916 }
 3917 
 3918 uint16_t
 3919 if_getvtag(struct mbuf *m)
 3920 {
 3921 
 3922         return (m->m_pkthdr.ether_vtag);
 3923 }
 3924 
 3925 int
 3926 if_sendq_empty(if_t ifp)
 3927 {
 3928         return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
 3929 }
 3930 
 3931 struct ifaddr *
 3932 if_getifaddr(if_t ifp)
 3933 {
 3934         return ((struct ifnet *)ifp)->if_addr;
 3935 }
 3936 
 3937 int
 3938 if_getamcount(if_t ifp)
 3939 {
 3940         return ((struct ifnet *)ifp)->if_amcount;
 3941 }
 3942 
 3943 
 3944 int
 3945 if_setsendqready(if_t ifp)
 3946 {
 3947         IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
 3948         return (0);
 3949 }
 3950 
 3951 int
 3952 if_setsendqlen(if_t ifp, int tx_desc_count)
 3953 {
 3954         IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
 3955         ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
 3956 
 3957         return (0);
 3958 }
 3959 
 3960 int
 3961 if_vlantrunkinuse(if_t ifp)
 3962 {
 3963         return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
 3964 }
 3965 
 3966 int
 3967 if_input(if_t ifp, struct mbuf* sendmp)
 3968 {
 3969         (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
 3970         return (0);
 3971 
 3972 }
 3973 
 3974 /* XXX */
 3975 #ifndef ETH_ADDR_LEN
 3976 #define ETH_ADDR_LEN 6
 3977 #endif
 3978 
 3979 int 
 3980 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
 3981 {
 3982         struct ifmultiaddr *ifma;
 3983         uint8_t *lmta = (uint8_t *)mta;
 3984         int mcnt = 0;
 3985 
 3986         TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
 3987                 if (ifma->ifma_addr->sa_family != AF_LINK)
 3988                         continue;
 3989 
 3990                 if (mcnt == max)
 3991                         break;
 3992 
 3993                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
 3994                     &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
 3995                 mcnt++;
 3996         }
 3997         *cnt = mcnt;
 3998 
 3999         return (0);
 4000 }
 4001 
 4002 int
 4003 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
 4004 {
 4005         int error;
 4006 
 4007         if_maddr_rlock(ifp);
 4008         error = if_setupmultiaddr(ifp, mta, cnt, max);
 4009         if_maddr_runlock(ifp);
 4010         return (error);
 4011 }
 4012 
 4013 int
 4014 if_multiaddr_count(if_t ifp, int max)
 4015 {
 4016         struct ifmultiaddr *ifma;
 4017         int count;
 4018 
 4019         count = 0;
 4020         if_maddr_rlock(ifp);
 4021         TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
 4022                 if (ifma->ifma_addr->sa_family != AF_LINK)
 4023                         continue;
 4024                 count++;
 4025                 if (count == max)
 4026                         break;
 4027         }
 4028         if_maddr_runlock(ifp);
 4029         return (count);
 4030 }
 4031 
 4032 int
 4033 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
 4034 {
 4035         struct ifmultiaddr *ifma;
 4036         int cnt = 0;
 4037 
 4038         if_maddr_rlock(ifp);
 4039         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
 4040                 cnt += filter(arg, ifma, cnt);
 4041         if_maddr_runlock(ifp);
 4042         return (cnt);
 4043 }
 4044 
 4045 struct mbuf *
 4046 if_dequeue(if_t ifp)
 4047 {
 4048         struct mbuf *m;
 4049         IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
 4050 
 4051         return (m);
 4052 }
 4053 
 4054 int
 4055 if_sendq_prepend(if_t ifp, struct mbuf *m)
 4056 {
 4057         IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
 4058         return (0);
 4059 }
 4060 
 4061 int
 4062 if_setifheaderlen(if_t ifp, int len)
 4063 {
 4064         ((struct ifnet *)ifp)->if_hdrlen = len;
 4065         return (0);
 4066 }
 4067 
 4068 caddr_t
 4069 if_getlladdr(if_t ifp)
 4070 {
 4071         return (IF_LLADDR((struct ifnet *)ifp));
 4072 }
 4073 
 4074 void *
 4075 if_gethandle(u_char type)
 4076 {
 4077         return (if_alloc(type));
 4078 }
 4079 
 4080 void
 4081 if_bpfmtap(if_t ifh, struct mbuf *m)
 4082 {
 4083         struct ifnet *ifp = (struct ifnet *)ifh;
 4084 
 4085         BPF_MTAP(ifp, m);
 4086 }
 4087 
 4088 void
 4089 if_etherbpfmtap(if_t ifh, struct mbuf *m)
 4090 {
 4091         struct ifnet *ifp = (struct ifnet *)ifh;
 4092 
 4093         ETHER_BPF_MTAP(ifp, m);
 4094 }
 4095 
 4096 void
 4097 if_vlancap(if_t ifh)
 4098 {
 4099         struct ifnet *ifp = (struct ifnet *)ifh;
 4100         VLAN_CAPABILITIES(ifp);
 4101 }
 4102 
 4103 void
 4104 if_setinitfn(if_t ifp, void (*init_fn)(void *))
 4105 {
 4106         ((struct ifnet *)ifp)->if_init = init_fn;
 4107 }
 4108 
 4109 void
 4110 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
 4111 {
 4112         ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
 4113 }
 4114 
 4115 void
 4116 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
 4117 {
 4118         ((struct ifnet *)ifp)->if_start = (void *)start_fn;
 4119 }
 4120 
 4121 void
 4122 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
 4123 {
 4124         ((struct ifnet *)ifp)->if_transmit = start_fn;
 4125 }
 4126 
 4127 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
 4128 {
 4129         ((struct ifnet *)ifp)->if_qflush = flush_fn;
 4130         
 4131 }
 4132 
 4133 void
 4134 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
 4135 {
 4136 
 4137         ifp->if_get_counter = fn;
 4138 }
 4139 
 4140 /* Revisit these - These are inline functions originally. */
 4141 int
 4142 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
 4143 {
 4144         return drbr_inuse(ifh, br);
 4145 }
 4146 
 4147 struct mbuf*
 4148 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
 4149 {
 4150         return drbr_dequeue(ifh, br);
 4151 }
 4152 
 4153 int
 4154 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
 4155 {
 4156         return drbr_needs_enqueue(ifh, br);
 4157 }
 4158 
 4159 int
 4160 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
 4161 {
 4162         return drbr_enqueue(ifh, br, m);
 4163 
 4164 }

Cache object: 98f8dfacf71e094bf9fc3a050a129a14


[ 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.