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


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

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

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

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

Cache object: 2687255cc98ce6971eb770263d848dd3


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