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


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

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

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

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

Cache object: cc6b5936f9fac83f2973b04017a91adf


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