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

Cache object: ff59de3ba4290f053439739ea934c3d7


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