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


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

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

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

    1 /*-
    2  * Copyright (c) 1980, 1986, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)if.c        8.5 (Berkeley) 1/9/95
   30  * $FreeBSD: releng/6.0/sys/net/if.c 150731 2005-09-29 20:57:07Z rwatson $
   31  */
   32 
   33 #include "opt_compat.h"
   34 #include "opt_inet6.h"
   35 #include "opt_inet.h"
   36 #include "opt_mac.h"
   37 #include "opt_carp.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/types.h>
   41 #include <sys/conf.h>
   42 #include <sys/mac.h>
   43 #include <sys/malloc.h>
   44 #include <sys/sbuf.h>
   45 #include <sys/bus.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/systm.h>
   48 #include <sys/proc.h>
   49 #include <sys/socket.h>
   50 #include <sys/socketvar.h>
   51 #include <sys/protosw.h>
   52 #include <sys/kernel.h>
   53 #include <sys/sockio.h>
   54 #include <sys/syslog.h>
   55 #include <sys/sysctl.h>
   56 #include <sys/taskqueue.h>
   57 #include <sys/domain.h>
   58 #include <sys/jail.h>
   59 #include <machine/stdarg.h>
   60 
   61 #include <net/if.h>
   62 #include <net/if_arp.h>
   63 #include <net/if_clone.h>
   64 #include <net/if_dl.h>
   65 #include <net/if_types.h>
   66 #include <net/if_var.h>
   67 #include <net/radix.h>
   68 #include <net/route.h>
   69 
   70 #if defined(INET) || defined(INET6)
   71 /*XXX*/
   72 #include <netinet/in.h>
   73 #include <netinet/in_var.h>
   74 #ifdef INET6
   75 #include <netinet6/in6_var.h>
   76 #include <netinet6/in6_ifattach.h>
   77 #endif
   78 #endif
   79 #ifdef INET
   80 #include <netinet/if_ether.h>
   81 #endif
   82 #ifdef DEV_CARP
   83 #include <netinet/ip_carp.h>
   84 #endif
   85 
   86 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
   87 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
   88 
   89 /* Log link state change events */
   90 static int log_link_state_change = 1;
   91 
   92 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
   93         &log_link_state_change, 0,
   94         "log interface link state change events");
   95 
   96 void    (*bstp_linkstate_p)(struct ifnet *ifp, int state);
   97 void    (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
   98 
   99 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
  100 
  101 static void     if_attachdomain(void *);
  102 static void     if_attachdomain1(struct ifnet *);
  103 static int      ifconf(u_long, caddr_t);
  104 static void     if_grow(void);
  105 static void     if_init(void *);
  106 static void     if_check(void *);
  107 static void     if_qflush(struct ifaltq *);
  108 static void     if_route(struct ifnet *, int flag, int fam);
  109 static int      if_setflag(struct ifnet *, int, int, int *, int);
  110 static void     if_slowtimo(void *);
  111 static void     if_unroute(struct ifnet *, int flag, int fam);
  112 static void     link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
  113 static int      if_rtdel(struct radix_node *, void *);
  114 static int      ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
  115 static void     if_start_deferred(void *context, int pending);
  116 static void     do_link_state_change(void *, int);
  117 #ifdef INET6
  118 /*
  119  * XXX: declare here to avoid to include many inet6 related files..
  120  * should be more generalized?
  121  */
  122 extern void     nd6_setmtu(struct ifnet *);
  123 #endif
  124 
  125 int     if_index = 0;
  126 struct  ifindex_entry *ifindex_table = NULL;
  127 int     ifqmaxlen = IFQ_MAXLEN;
  128 struct  ifnethead ifnet;        /* depend on static init XXX */
  129 struct  mtx ifnet_lock;
  130 static  if_com_alloc_t *if_com_alloc[256];
  131 static  if_com_free_t *if_com_free[256];
  132 
  133 static int      if_indexlim = 8;
  134 static struct   knlist ifklist;
  135 
  136 static void     filt_netdetach(struct knote *kn);
  137 static int      filt_netdev(struct knote *kn, long hint);
  138 
  139 static struct filterops netdev_filtops =
  140     { 1, NULL, filt_netdetach, filt_netdev };
  141 
  142 /*
  143  * System initialization
  144  */
  145 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
  146 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
  147 
  148 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
  149 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
  150 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
  151 
  152 static d_open_t         netopen;
  153 static d_close_t        netclose;
  154 static d_ioctl_t        netioctl;
  155 static d_kqfilter_t     netkqfilter;
  156 
  157 static struct cdevsw net_cdevsw = {
  158         .d_version =    D_VERSION,
  159         .d_flags =      D_NEEDGIANT,
  160         .d_open =       netopen,
  161         .d_close =      netclose,
  162         .d_ioctl =      netioctl,
  163         .d_name =       "net",
  164         .d_kqfilter =   netkqfilter,
  165 };
  166 
  167 static int
  168 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
  169 {
  170         return (0);
  171 }
  172 
  173 static int
  174 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
  175 {
  176         return (0);
  177 }
  178 
  179 static int
  180 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  181 {
  182         struct ifnet *ifp;
  183         int error, idx;
  184 
  185         /* only support interface specific ioctls */
  186         if (IOCGROUP(cmd) != 'i')
  187                 return (EOPNOTSUPP);
  188         idx = minor(dev);
  189         if (idx == 0) {
  190                 /*
  191                  * special network device, not interface.
  192                  */
  193                 if (cmd == SIOCGIFCONF)
  194                         return (ifconf(cmd, data));     /* XXX remove cmd */
  195                 return (EOPNOTSUPP);
  196         }
  197 
  198         ifp = ifnet_byindex(idx);
  199         if (ifp == NULL)
  200                 return (ENXIO);
  201 
  202         error = ifhwioctl(cmd, ifp, data, td);
  203         if (error == ENOIOCTL)
  204                 error = EOPNOTSUPP;
  205         return (error);
  206 }
  207 
  208 static int
  209 netkqfilter(struct cdev *dev, struct knote *kn)
  210 {
  211         struct knlist *klist;
  212         struct ifnet *ifp;
  213         int idx;
  214 
  215         switch (kn->kn_filter) {
  216         case EVFILT_NETDEV:
  217                 kn->kn_fop = &netdev_filtops;
  218                 break;
  219         default:
  220                 return (EINVAL);
  221         }
  222 
  223         idx = minor(dev);
  224         if (idx == 0) {
  225                 klist = &ifklist;
  226         } else {
  227                 ifp = ifnet_byindex(idx);
  228                 if (ifp == NULL)
  229                         return (1);
  230                 klist = &ifp->if_klist;
  231         }
  232 
  233         kn->kn_hook = (caddr_t)klist;
  234 
  235         knlist_add(klist, kn, 0);
  236 
  237         return (0);
  238 }
  239 
  240 static void
  241 filt_netdetach(struct knote *kn)
  242 {
  243         struct knlist *klist = (struct knlist *)kn->kn_hook;
  244 
  245         knlist_remove(klist, kn, 0);
  246 }
  247 
  248 static int
  249 filt_netdev(struct knote *kn, long hint)
  250 {
  251         struct knlist *klist = (struct knlist *)kn->kn_hook;
  252 
  253         /*
  254          * Currently NOTE_EXIT is abused to indicate device detach.
  255          */
  256         if (hint == NOTE_EXIT) {
  257                 kn->kn_data = NOTE_LINKINV;
  258                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
  259                 knlist_remove_inevent(klist, kn);
  260                 return (1);
  261         }
  262         if (hint != 0)
  263                 kn->kn_data = hint;                     /* current status */
  264         if (kn->kn_sfflags & hint)
  265                 kn->kn_fflags |= hint;
  266         return (kn->kn_fflags != 0);
  267 }
  268 
  269 /*
  270  * Network interface utility routines.
  271  *
  272  * Routines with ifa_ifwith* names take sockaddr *'s as
  273  * parameters.
  274  */
  275 /* ARGSUSED*/
  276 static void
  277 if_init(void *dummy __unused)
  278 {
  279 
  280         IFNET_LOCK_INIT();
  281         TAILQ_INIT(&ifnet);
  282         knlist_init(&ifklist, NULL, NULL, NULL, NULL);
  283         if_grow();                              /* create initial table */
  284         ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
  285             UID_ROOT, GID_WHEEL, 0600, "network");
  286         if_clone_init();
  287 }
  288 
  289 static void
  290 if_grow(void)
  291 {
  292         u_int n;
  293         struct ifindex_entry *e;
  294 
  295         if_indexlim <<= 1;
  296         n = if_indexlim * sizeof(*e);
  297         e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
  298         if (ifindex_table != NULL) {
  299                 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
  300                 free((caddr_t)ifindex_table, M_IFNET);
  301         }
  302         ifindex_table = e;
  303 }
  304 
  305 /* ARGSUSED*/
  306 static void
  307 if_check(void *dummy __unused)
  308 {
  309         struct ifnet *ifp;
  310         int s;
  311 
  312         s = splimp();
  313         IFNET_RLOCK();  /* could sleep on rare error; mostly okay XXX */
  314         TAILQ_FOREACH(ifp, &ifnet, if_link) {
  315                 if (ifp->if_snd.ifq_maxlen == 0) {
  316                         if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
  317                         ifp->if_snd.ifq_maxlen = ifqmaxlen;
  318                 }
  319                 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
  320                         if_printf(ifp,
  321                             "XXX: driver didn't initialize queue mtx\n");
  322                         mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
  323                             MTX_NETWORK_LOCK, MTX_DEF);
  324                 }
  325         }
  326         IFNET_RUNLOCK();
  327         splx(s);
  328         if_slowtimo(0);
  329 }
  330 
  331 /*
  332  * Allocate a struct ifnet and in index for an interface.
  333  */
  334 struct ifnet*
  335 if_alloc(u_char type)
  336 {
  337         struct ifnet *ifp;
  338 
  339         ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
  340 
  341         /*
  342          * Try to find an empty slot below if_index.  If we fail, take
  343          * the next slot.
  344          *
  345          * XXX: should be locked!
  346          */
  347         for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
  348                 if (ifnet_byindex(ifp->if_index) == NULL)
  349                         break;
  350         }
  351         /* Catch if_index overflow. */
  352         if (ifp->if_index < 1) {
  353                 free(ifp, M_IFNET);
  354                 return (NULL);
  355         }
  356         if (ifp->if_index > if_index)
  357                 if_index = ifp->if_index;
  358         if (if_index >= if_indexlim)
  359                 if_grow();
  360         ifnet_byindex(ifp->if_index) = ifp;
  361 
  362         ifp->if_type = type;
  363 
  364         if (if_com_alloc[type] != NULL) {
  365                 ifp->if_l2com = if_com_alloc[type](type, ifp);
  366                 if (ifp->if_l2com == NULL) {
  367                         free(ifp, M_IFNET);
  368                         return (NULL);
  369                 }
  370         }
  371         IF_ADDR_LOCK_INIT(ifp);
  372 
  373         return (ifp);
  374 }
  375 
  376 void
  377 if_free(struct ifnet *ifp)
  378 {
  379 
  380         /* Do not add code to this function!  Add it to if_free_type(). */
  381         if_free_type(ifp, ifp->if_type);
  382 }
  383 
  384 void
  385 if_free_type(struct ifnet *ifp, u_char type)
  386 {
  387 
  388         if (ifp != ifnet_byindex(ifp->if_index)) {
  389                 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
  390                     __func__);
  391                 return;
  392         }
  393 
  394         IF_ADDR_LOCK_DESTROY(ifp);
  395 
  396         ifnet_byindex(ifp->if_index) = NULL;
  397 
  398         /* XXX: should be locked with if_findindex() */
  399         while (if_index > 0 && ifnet_byindex(if_index) == NULL)
  400                 if_index--;
  401 
  402         if (if_com_free[type] != NULL)
  403                 if_com_free[type](ifp->if_l2com, type);
  404 
  405         free(ifp, M_IFNET);
  406 };
  407 
  408 /*
  409  * Attach an interface to the
  410  * list of "active" interfaces.
  411  */
  412 void
  413 if_attach(struct ifnet *ifp)
  414 {
  415         unsigned socksize, ifasize;
  416         int namelen, masklen;
  417         struct sockaddr_dl *sdl;
  418         struct ifaddr *ifa;
  419 
  420         if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
  421                 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
  422                     ifp->if_xname);
  423 
  424         TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
  425         TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
  426         IF_AFDATA_LOCK_INIT(ifp);
  427         ifp->if_afdata_initialized = 0;
  428         IFNET_WLOCK();
  429         TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
  430         IFNET_WUNLOCK();
  431         /*
  432          * XXX -
  433          * The old code would work if the interface passed a pre-existing
  434          * chain of ifaddrs to this code.  We don't trust our callers to
  435          * properly initialize the tailq, however, so we no longer allow
  436          * this unlikely case.
  437          */
  438         TAILQ_INIT(&ifp->if_addrhead);
  439         TAILQ_INIT(&ifp->if_prefixhead);
  440         TAILQ_INIT(&ifp->if_multiaddrs);
  441         knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
  442         getmicrotime(&ifp->if_lastchange);
  443         ifp->if_data.ifi_epoch = time_uptime;
  444         ifp->if_data.ifi_datalen = sizeof(struct if_data);
  445 
  446 #ifdef MAC
  447         mac_init_ifnet(ifp);
  448         mac_create_ifnet(ifp);
  449 #endif
  450 
  451         ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
  452             unit2minor(ifp->if_index),
  453             UID_ROOT, GID_WHEEL, 0600, "%s/%s",
  454             net_cdevsw.d_name, ifp->if_xname);
  455         make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
  456             net_cdevsw.d_name, ifp->if_index);
  457 
  458         mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
  459 
  460         /*
  461          * create a Link Level name for this device
  462          */
  463         namelen = strlen(ifp->if_xname);
  464         /*
  465          * Always save enough space for any possiable name so we can do
  466          * a rename in place later.
  467          */
  468         masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
  469         socksize = masklen + ifp->if_addrlen;
  470         if (socksize < sizeof(*sdl))
  471                 socksize = sizeof(*sdl);
  472         socksize = roundup2(socksize, sizeof(long));
  473         ifasize = sizeof(*ifa) + 2 * socksize;
  474         ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
  475         IFA_LOCK_INIT(ifa);
  476         sdl = (struct sockaddr_dl *)(ifa + 1);
  477         sdl->sdl_len = socksize;
  478         sdl->sdl_family = AF_LINK;
  479         bcopy(ifp->if_xname, sdl->sdl_data, namelen);
  480         sdl->sdl_nlen = namelen;
  481         sdl->sdl_index = ifp->if_index;
  482         sdl->sdl_type = ifp->if_type;
  483         ifaddr_byindex(ifp->if_index) = ifa;
  484         ifa->ifa_ifp = ifp;
  485         ifa->ifa_rtrequest = link_rtrequest;
  486         ifa->ifa_addr = (struct sockaddr *)sdl;
  487         sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
  488         ifa->ifa_netmask = (struct sockaddr *)sdl;
  489         sdl->sdl_len = masklen;
  490         while (namelen != 0)
  491                 sdl->sdl_data[--namelen] = 0xff;
  492         ifa->ifa_refcnt = 1;
  493         TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
  494         ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
  495         ifp->if_snd.altq_type = 0;
  496         ifp->if_snd.altq_disc = NULL;
  497         ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
  498         ifp->if_snd.altq_tbr  = NULL;
  499         ifp->if_snd.altq_ifp  = ifp;
  500 
  501         if (domain_init_status >= 2)
  502                 if_attachdomain1(ifp);
  503 
  504         EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
  505 
  506         /* Announce the interface. */
  507         rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
  508 }
  509 
  510 static void
  511 if_attachdomain(void *dummy)
  512 {
  513         struct ifnet *ifp;
  514         int s;
  515 
  516         s = splnet();
  517         TAILQ_FOREACH(ifp, &ifnet, if_link)
  518                 if_attachdomain1(ifp);
  519         splx(s);
  520 }
  521 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
  522     if_attachdomain, NULL);
  523 
  524 static void
  525 if_attachdomain1(struct ifnet *ifp)
  526 {
  527         struct domain *dp;
  528         int s;
  529 
  530         s = splnet();
  531 
  532         /*
  533          * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
  534          * cannot lock ifp->if_afdata initialization, entirely.
  535          */
  536         if (IF_AFDATA_TRYLOCK(ifp) == 0) {
  537                 splx(s);
  538                 return;
  539         }
  540         if (ifp->if_afdata_initialized >= domain_init_status) {
  541                 IF_AFDATA_UNLOCK(ifp);
  542                 splx(s);
  543                 printf("if_attachdomain called more than once on %s\n",
  544                     ifp->if_xname);
  545                 return;
  546         }
  547         ifp->if_afdata_initialized = domain_init_status;
  548         IF_AFDATA_UNLOCK(ifp);
  549 
  550         /* address family dependent data region */
  551         bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
  552         for (dp = domains; dp; dp = dp->dom_next) {
  553                 if (dp->dom_ifattach)
  554                         ifp->if_afdata[dp->dom_family] =
  555                             (*dp->dom_ifattach)(ifp);
  556         }
  557 
  558         splx(s);
  559 }
  560 
  561 /*
  562  * Remove any network addresses from an interface.
  563  */
  564 
  565 void
  566 if_purgeaddrs(struct ifnet *ifp)
  567 {
  568         struct ifaddr *ifa, *next;
  569 
  570         TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
  571 
  572                 if (ifa->ifa_addr->sa_family == AF_LINK)
  573                         continue;
  574 #ifdef INET
  575                 /* XXX: Ugly!! ad hoc just for INET */
  576                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
  577                         struct ifaliasreq ifr;
  578 
  579                         bzero(&ifr, sizeof(ifr));
  580                         ifr.ifra_addr = *ifa->ifa_addr;
  581                         if (ifa->ifa_dstaddr)
  582                                 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
  583                         if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
  584                             NULL) == 0)
  585                                 continue;
  586                 }
  587 #endif /* INET */
  588 #ifdef INET6
  589                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
  590                         in6_purgeaddr(ifa);
  591                         /* ifp_addrhead is already updated */
  592                         continue;
  593                 }
  594 #endif /* INET6 */
  595                 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
  596                 IFAFREE(ifa);
  597         }
  598 }
  599 
  600 /*
  601  * Detach an interface, removing it from the
  602  * list of "active" interfaces and freeing the struct ifnet.
  603  *
  604  * XXXRW: There are some significant questions about event ordering, and
  605  * how to prevent things from starting to use the interface during detach.
  606  */
  607 void
  608 if_detach(struct ifnet *ifp)
  609 {
  610         struct ifaddr *ifa;
  611         struct radix_node_head  *rnh;
  612         int s;
  613         int i;
  614         struct domain *dp;
  615         struct ifnet *iter;
  616         int found;
  617 
  618         /*
  619          * Remove/wait for pending events.
  620          */
  621         taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
  622 
  623 #ifdef DEV_CARP
  624         /* Maybe hook to the generalized departure handler above?!? */
  625         if (ifp->if_carp)
  626                 carp_ifdetach(ifp);
  627 #endif
  628 
  629         /*
  630          * Remove routes and flush queues.
  631          */
  632         s = splnet();
  633         if_down(ifp);
  634 #ifdef ALTQ
  635         if (ALTQ_IS_ENABLED(&ifp->if_snd))
  636                 altq_disable(&ifp->if_snd);
  637         if (ALTQ_IS_ATTACHED(&ifp->if_snd))
  638                 altq_detach(&ifp->if_snd);
  639 #endif
  640 
  641         if_purgeaddrs(ifp);
  642 
  643 #ifdef INET
  644         in_ifdetach(ifp);
  645 #endif
  646 
  647 #ifdef INET6
  648         /*
  649          * Remove all IPv6 kernel structs related to ifp.  This should be done
  650          * before removing routing entries below, since IPv6 interface direct
  651          * routes are expected to be removed by the IPv6-specific kernel API.
  652          * Otherwise, the kernel will detect some inconsistency and bark it.
  653          */
  654         in6_ifdetach(ifp);
  655 #endif
  656         /*
  657          * Remove address from ifindex_table[] and maybe decrement if_index.
  658          * Clean up all addresses.
  659          */
  660         ifaddr_byindex(ifp->if_index) = NULL;
  661         destroy_dev(ifdev_byindex(ifp->if_index));
  662         ifdev_byindex(ifp->if_index) = NULL;
  663 
  664         /* We can now free link ifaddr. */
  665         if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
  666                 ifa = TAILQ_FIRST(&ifp->if_addrhead);
  667                 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
  668                 IFAFREE(ifa);
  669         }
  670 
  671         /*
  672          * Delete all remaining routes using this interface
  673          * Unfortuneatly the only way to do this is to slog through
  674          * the entire routing table looking for routes which point
  675          * to this interface...oh well...
  676          */
  677         for (i = 1; i <= AF_MAX; i++) {
  678                 if ((rnh = rt_tables[i]) == NULL)
  679                         continue;
  680                 RADIX_NODE_HEAD_LOCK(rnh);
  681                 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
  682                 RADIX_NODE_HEAD_UNLOCK(rnh);
  683         }
  684 
  685         /* Announce that the interface is gone. */
  686         rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
  687         EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
  688 
  689         IF_AFDATA_LOCK(ifp);
  690         for (dp = domains; dp; dp = dp->dom_next) {
  691                 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
  692                         (*dp->dom_ifdetach)(ifp,
  693                             ifp->if_afdata[dp->dom_family]);
  694         }
  695         IF_AFDATA_UNLOCK(ifp);
  696 
  697 #ifdef MAC
  698         mac_destroy_ifnet(ifp);
  699 #endif /* MAC */
  700         KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
  701         knlist_clear(&ifp->if_klist, 0);
  702         knlist_destroy(&ifp->if_klist);
  703         IFNET_WLOCK();
  704         found = 0;
  705         TAILQ_FOREACH(iter, &ifnet, if_link)
  706                 if (iter == ifp) {
  707                         found = 1;
  708                         break;
  709                 }
  710         if (found)
  711                 TAILQ_REMOVE(&ifnet, ifp, if_link);
  712         IFNET_WUNLOCK();
  713         mtx_destroy(&ifp->if_snd.ifq_mtx);
  714         IF_AFDATA_DESTROY(ifp);
  715         splx(s);
  716 }
  717 
  718 /*
  719  * Delete Routes for a Network Interface
  720  *
  721  * Called for each routing entry via the rnh->rnh_walktree() call above
  722  * to delete all route entries referencing a detaching network interface.
  723  *
  724  * Arguments:
  725  *      rn      pointer to node in the routing table
  726  *      arg     argument passed to rnh->rnh_walktree() - detaching interface
  727  *
  728  * Returns:
  729  *      0       successful
  730  *      errno   failed - reason indicated
  731  *
  732  */
  733 static int
  734 if_rtdel(struct radix_node *rn, void *arg)
  735 {
  736         struct rtentry  *rt = (struct rtentry *)rn;
  737         struct ifnet    *ifp = arg;
  738         int             err;
  739 
  740         if (rt->rt_ifp == ifp) {
  741 
  742                 /*
  743                  * Protect (sorta) against walktree recursion problems
  744                  * with cloned routes
  745                  */
  746                 if ((rt->rt_flags & RTF_UP) == 0)
  747                         return (0);
  748 
  749                 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
  750                                 rt_mask(rt), rt->rt_flags,
  751                                 (struct rtentry **) NULL);
  752                 if (err) {
  753                         log(LOG_WARNING, "if_rtdel: error %d\n", err);
  754                 }
  755         }
  756 
  757         return (0);
  758 }
  759 
  760 #define sa_equal(a1, a2)        (bcmp((a1), (a2), ((a1))->sa_len) == 0)
  761 
  762 /*
  763  * Locate an interface based on a complete address.
  764  */
  765 /*ARGSUSED*/
  766 struct ifaddr *
  767 ifa_ifwithaddr(struct sockaddr *addr)
  768 {
  769         struct ifnet *ifp;
  770         struct ifaddr *ifa;
  771 
  772         IFNET_RLOCK();
  773         TAILQ_FOREACH(ifp, &ifnet, if_link)
  774                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  775                         if (ifa->ifa_addr->sa_family != addr->sa_family)
  776                                 continue;
  777                         if (sa_equal(addr, ifa->ifa_addr))
  778                                 goto done;
  779                         /* IP6 doesn't have broadcast */
  780                         if ((ifp->if_flags & IFF_BROADCAST) &&
  781                             ifa->ifa_broadaddr &&
  782                             ifa->ifa_broadaddr->sa_len != 0 &&
  783                             sa_equal(ifa->ifa_broadaddr, addr))
  784                                 goto done;
  785                 }
  786         ifa = NULL;
  787 done:
  788         IFNET_RUNLOCK();
  789         return (ifa);
  790 }
  791 
  792 /*
  793  * Locate the point to point interface with a given destination address.
  794  */
  795 /*ARGSUSED*/
  796 struct ifaddr *
  797 ifa_ifwithdstaddr(struct sockaddr *addr)
  798 {
  799         struct ifnet *ifp;
  800         struct ifaddr *ifa;
  801 
  802         IFNET_RLOCK();
  803         TAILQ_FOREACH(ifp, &ifnet, if_link) {
  804                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  805                         continue;
  806                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  807                         if (ifa->ifa_addr->sa_family != addr->sa_family)
  808                                 continue;
  809                         if (ifa->ifa_dstaddr &&
  810                             sa_equal(addr, ifa->ifa_dstaddr))
  811                                 goto done;
  812                 }
  813         }
  814         ifa = NULL;
  815 done:
  816         IFNET_RUNLOCK();
  817         return (ifa);
  818 }
  819 
  820 /*
  821  * Find an interface on a specific network.  If many, choice
  822  * is most specific found.
  823  */
  824 struct ifaddr *
  825 ifa_ifwithnet(struct sockaddr *addr)
  826 {
  827         struct ifnet *ifp;
  828         struct ifaddr *ifa;
  829         struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
  830         u_int af = addr->sa_family;
  831         char *addr_data = addr->sa_data, *cplim;
  832 
  833         /*
  834          * AF_LINK addresses can be looked up directly by their index number,
  835          * so do that if we can.
  836          */
  837         if (af == AF_LINK) {
  838             struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
  839             if (sdl->sdl_index && sdl->sdl_index <= if_index)
  840                 return (ifaddr_byindex(sdl->sdl_index));
  841         }
  842 
  843         /*
  844          * Scan though each interface, looking for ones that have
  845          * addresses in this address family.
  846          */
  847         IFNET_RLOCK();
  848         TAILQ_FOREACH(ifp, &ifnet, if_link) {
  849                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  850                         char *cp, *cp2, *cp3;
  851 
  852                         if (ifa->ifa_addr->sa_family != af)
  853 next:                           continue;
  854                         if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
  855                                 /*
  856                                  * This is a bit broken as it doesn't
  857                                  * take into account that the remote end may
  858                                  * be a single node in the network we are
  859                                  * looking for.
  860                                  * The trouble is that we don't know the
  861                                  * netmask for the remote end.
  862                                  */
  863                                 if (ifa->ifa_dstaddr != 0 &&
  864                                     sa_equal(addr, ifa->ifa_dstaddr))
  865                                         goto done;
  866                         } else {
  867                                 /*
  868                                  * if we have a special address handler,
  869                                  * then use it instead of the generic one.
  870                                  */
  871                                 if (ifa->ifa_claim_addr) {
  872                                         if ((*ifa->ifa_claim_addr)(ifa, addr))
  873                                                 goto done;
  874                                         continue;
  875                                 }
  876 
  877                                 /*
  878                                  * Scan all the bits in the ifa's address.
  879                                  * If a bit dissagrees with what we are
  880                                  * looking for, mask it with the netmask
  881                                  * to see if it really matters.
  882                                  * (A byte at a time)
  883                                  */
  884                                 if (ifa->ifa_netmask == 0)
  885                                         continue;
  886                                 cp = addr_data;
  887                                 cp2 = ifa->ifa_addr->sa_data;
  888                                 cp3 = ifa->ifa_netmask->sa_data;
  889                                 cplim = ifa->ifa_netmask->sa_len
  890                                         + (char *)ifa->ifa_netmask;
  891                                 while (cp3 < cplim)
  892                                         if ((*cp++ ^ *cp2++) & *cp3++)
  893                                                 goto next; /* next address! */
  894                                 /*
  895                                  * If the netmask of what we just found
  896                                  * is more specific than what we had before
  897                                  * (if we had one) then remember the new one
  898                                  * before continuing to search
  899                                  * for an even better one.
  900                                  */
  901                                 if (ifa_maybe == 0 ||
  902                                     rn_refines((caddr_t)ifa->ifa_netmask,
  903                                     (caddr_t)ifa_maybe->ifa_netmask))
  904                                         ifa_maybe = ifa;
  905                         }
  906                 }
  907         }
  908         ifa = ifa_maybe;
  909 done:
  910         IFNET_RUNLOCK();
  911         return (ifa);
  912 }
  913 
  914 /*
  915  * Find an interface address specific to an interface best matching
  916  * a given address.
  917  */
  918 struct ifaddr *
  919 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
  920 {
  921         struct ifaddr *ifa;
  922         char *cp, *cp2, *cp3;
  923         char *cplim;
  924         struct ifaddr *ifa_maybe = 0;
  925         u_int af = addr->sa_family;
  926 
  927         if (af >= AF_MAX)
  928                 return (0);
  929         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  930                 if (ifa->ifa_addr->sa_family != af)
  931                         continue;
  932                 if (ifa_maybe == 0)
  933                         ifa_maybe = ifa;
  934                 if (ifa->ifa_netmask == 0) {
  935                         if (sa_equal(addr, ifa->ifa_addr) ||
  936                             (ifa->ifa_dstaddr &&
  937                             sa_equal(addr, ifa->ifa_dstaddr)))
  938                                 goto done;
  939                         continue;
  940                 }
  941                 if (ifp->if_flags & IFF_POINTOPOINT) {
  942                         if (sa_equal(addr, ifa->ifa_dstaddr))
  943                                 goto done;
  944                 } else {
  945                         cp = addr->sa_data;
  946                         cp2 = ifa->ifa_addr->sa_data;
  947                         cp3 = ifa->ifa_netmask->sa_data;
  948                         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
  949                         for (; cp3 < cplim; cp3++)
  950                                 if ((*cp++ ^ *cp2++) & *cp3)
  951                                         break;
  952                         if (cp3 == cplim)
  953                                 goto done;
  954                 }
  955         }
  956         ifa = ifa_maybe;
  957 done:
  958         return (ifa);
  959 }
  960 
  961 #include <net/route.h>
  962 
  963 /*
  964  * Default action when installing a route with a Link Level gateway.
  965  * Lookup an appropriate real ifa to point to.
  966  * This should be moved to /sys/net/link.c eventually.
  967  */
  968 static void
  969 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
  970 {
  971         struct ifaddr *ifa, *oifa;
  972         struct sockaddr *dst;
  973         struct ifnet *ifp;
  974 
  975         RT_LOCK_ASSERT(rt);
  976 
  977         if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
  978             ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
  979                 return;
  980         ifa = ifaof_ifpforaddr(dst, ifp);
  981         if (ifa) {
  982                 IFAREF(ifa);            /* XXX */
  983                 oifa = rt->rt_ifa;
  984                 rt->rt_ifa = ifa;
  985                 IFAFREE(oifa);
  986                 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
  987                         ifa->ifa_rtrequest(cmd, rt, info);
  988         }
  989 }
  990 
  991 /*
  992  * Mark an interface down and notify protocols of
  993  * the transition.
  994  * NOTE: must be called at splnet or eqivalent.
  995  */
  996 static void
  997 if_unroute(struct ifnet *ifp, int flag, int fam)
  998 {
  999         struct ifaddr *ifa;
 1000 
 1001         KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
 1002 
 1003         ifp->if_flags &= ~flag;
 1004         getmicrotime(&ifp->if_lastchange);
 1005         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
 1006                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
 1007                         pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
 1008         if_qflush(&ifp->if_snd);
 1009 #ifdef DEV_CARP
 1010         if (ifp->if_carp)
 1011                 carp_carpdev_state(ifp->if_carp);
 1012 #endif
 1013         rt_ifmsg(ifp);
 1014 }
 1015 
 1016 /*
 1017  * Mark an interface up and notify protocols of
 1018  * the transition.
 1019  * NOTE: must be called at splnet or eqivalent.
 1020  */
 1021 static void
 1022 if_route(struct ifnet *ifp, int flag, int fam)
 1023 {
 1024         struct ifaddr *ifa;
 1025 
 1026         KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
 1027 
 1028         ifp->if_flags |= flag;
 1029         getmicrotime(&ifp->if_lastchange);
 1030         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
 1031                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
 1032                         pfctlinput(PRC_IFUP, ifa->ifa_addr);
 1033 #ifdef DEV_CARP
 1034         if (ifp->if_carp)
 1035                 carp_carpdev_state(ifp->if_carp);
 1036 #endif
 1037         rt_ifmsg(ifp);
 1038 #ifdef INET6
 1039         in6_if_up(ifp);
 1040 #endif
 1041 }
 1042 
 1043 void    (*vlan_link_state_p)(struct ifnet *, int);      /* XXX: private from if_vlan */
 1044 
 1045 /*
 1046  * Handle a change in the interface link state. To avoid LORs
 1047  * between driver lock and upper layer locks, as well as possible
 1048  * recursions, we post event to taskqueue, and all job
 1049  * is done in static do_link_state_change().
 1050  */
 1051 void
 1052 if_link_state_change(struct ifnet *ifp, int link_state)
 1053 {
 1054         /* Return if state hasn't changed. */
 1055         if (ifp->if_link_state == link_state)
 1056                 return;
 1057 
 1058         ifp->if_link_state = link_state;
 1059 
 1060         taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
 1061 }
 1062 
 1063 static void
 1064 do_link_state_change(void *arg, int pending)
 1065 {
 1066         struct ifnet *ifp = (struct ifnet *)arg;
 1067         int link_state = ifp->if_link_state;
 1068         int link;
 1069 
 1070         /* Notify that the link state has changed. */
 1071         rt_ifmsg(ifp);
 1072         if (link_state == LINK_STATE_UP)
 1073                 link = NOTE_LINKUP;
 1074         else if (link_state == LINK_STATE_DOWN)
 1075                 link = NOTE_LINKDOWN;
 1076         else
 1077                 link = NOTE_LINKINV;
 1078         KNOTE_UNLOCKED(&ifp->if_klist, link);
 1079         if (ifp->if_nvlans != 0)
 1080                 (*vlan_link_state_p)(ifp, link);
 1081 
 1082         if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
 1083             IFP2AC(ifp)->ac_netgraph != NULL)
 1084                 (*ng_ether_link_state_p)(ifp, link_state);
 1085 #ifdef DEV_CARP
 1086         if (ifp->if_carp)
 1087                 carp_carpdev_state(ifp->if_carp);
 1088 #endif
 1089         if (ifp->if_bridge) {
 1090                 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
 1091                 (*bstp_linkstate_p)(ifp, link_state);
 1092         }
 1093 
 1094         devctl_notify("IFNET", ifp->if_xname,
 1095             (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
 1096         if (pending > 1)
 1097                 if_printf(ifp, "%d link states coalesced\n", pending);
 1098         if (log_link_state_change)
 1099                 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
 1100                     (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
 1101 }
 1102 
 1103 /*
 1104  * Mark an interface down and notify protocols of
 1105  * the transition.
 1106  * NOTE: must be called at splnet or eqivalent.
 1107  */
 1108 void
 1109 if_down(struct ifnet *ifp)
 1110 {
 1111 
 1112         if_unroute(ifp, IFF_UP, AF_UNSPEC);
 1113 }
 1114 
 1115 /*
 1116  * Mark an interface up and notify protocols of
 1117  * the transition.
 1118  * NOTE: must be called at splnet or eqivalent.
 1119  */
 1120 void
 1121 if_up(struct ifnet *ifp)
 1122 {
 1123 
 1124         if_route(ifp, IFF_UP, AF_UNSPEC);
 1125 }
 1126 
 1127 /*
 1128  * Flush an interface queue.
 1129  */
 1130 static void
 1131 if_qflush(struct ifaltq *ifq)
 1132 {
 1133         struct mbuf *m, *n;
 1134 
 1135         IFQ_LOCK(ifq);
 1136 #ifdef ALTQ
 1137         if (ALTQ_IS_ENABLED(ifq))
 1138                 ALTQ_PURGE(ifq);
 1139 #endif
 1140         n = ifq->ifq_head;
 1141         while ((m = n) != 0) {
 1142                 n = m->m_act;
 1143                 m_freem(m);
 1144         }
 1145         ifq->ifq_head = 0;
 1146         ifq->ifq_tail = 0;
 1147         ifq->ifq_len = 0;
 1148         IFQ_UNLOCK(ifq);
 1149 }
 1150 
 1151 /*
 1152  * Handle interface watchdog timer routines.  Called
 1153  * from softclock, we decrement timers (if set) and
 1154  * call the appropriate interface routine on expiration.
 1155  *
 1156  * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
 1157  * holding Giant.  If we switch to an MPSAFE callout, we likely need to grab
 1158  * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
 1159  */
 1160 static void
 1161 if_slowtimo(void *arg)
 1162 {
 1163         struct ifnet *ifp;
 1164         int s = splimp();
 1165 
 1166         IFNET_RLOCK();
 1167         TAILQ_FOREACH(ifp, &ifnet, if_link) {
 1168                 if (ifp->if_timer == 0 || --ifp->if_timer)
 1169                         continue;
 1170                 if (ifp->if_watchdog)
 1171                         (*ifp->if_watchdog)(ifp);
 1172         }
 1173         IFNET_RUNLOCK();
 1174         splx(s);
 1175         timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
 1176 }
 1177 
 1178 /*
 1179  * Map interface name to
 1180  * interface structure pointer.
 1181  */
 1182 struct ifnet *
 1183 ifunit(const char *name)
 1184 {
 1185         struct ifnet *ifp;
 1186 
 1187         IFNET_RLOCK();
 1188         TAILQ_FOREACH(ifp, &ifnet, if_link) {
 1189                 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
 1190                         break;
 1191         }
 1192         IFNET_RUNLOCK();
 1193         return (ifp);
 1194 }
 1195 
 1196 /*
 1197  * Hardware specific interface ioctls.
 1198  */
 1199 static int
 1200 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
 1201 {
 1202         struct ifreq *ifr;
 1203         struct ifstat *ifs;
 1204         int error = 0;
 1205         int new_flags, temp_flags;
 1206         size_t namelen, onamelen;
 1207         char new_name[IFNAMSIZ];
 1208         struct ifaddr *ifa;
 1209         struct sockaddr_dl *sdl;
 1210 
 1211         ifr = (struct ifreq *)data;
 1212         switch (cmd) {
 1213         case SIOCGIFINDEX:
 1214                 ifr->ifr_index = ifp->if_index;
 1215                 break;
 1216 
 1217         case SIOCGIFFLAGS:
 1218                 temp_flags = ifp->if_flags | ifp->if_drv_flags;
 1219                 ifr->ifr_flags = temp_flags & 0xffff;
 1220                 ifr->ifr_flagshigh = temp_flags >> 16;
 1221                 break;
 1222 
 1223         case SIOCGIFCAP:
 1224                 ifr->ifr_reqcap = ifp->if_capabilities;
 1225                 ifr->ifr_curcap = ifp->if_capenable;
 1226                 break;
 1227 
 1228 #ifdef MAC
 1229         case SIOCGIFMAC:
 1230                 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
 1231                 break;
 1232 #endif
 1233 
 1234         case SIOCGIFMETRIC:
 1235                 ifr->ifr_metric = ifp->if_metric;
 1236                 break;
 1237 
 1238         case SIOCGIFMTU:
 1239                 ifr->ifr_mtu = ifp->if_mtu;
 1240                 break;
 1241 
 1242         case SIOCGIFPHYS:
 1243                 ifr->ifr_phys = ifp->if_physical;
 1244                 break;
 1245 
 1246         case SIOCSIFFLAGS:
 1247                 error = suser(td);
 1248                 if (error)
 1249                         return (error);
 1250                 /*
 1251                  * Currently, no driver owned flags pass the IFF_CANTCHANGE
 1252                  * check, so we don't need special handling here yet.
 1253                  */
 1254                 new_flags = (ifr->ifr_flags & 0xffff) |
 1255                     (ifr->ifr_flagshigh << 16);
 1256                 if (ifp->if_flags & IFF_SMART) {
 1257                         /* Smart drivers twiddle their own routes */
 1258                 } else if (ifp->if_flags & IFF_UP &&
 1259                     (new_flags & IFF_UP) == 0) {
 1260                         int s = splimp();
 1261                         if_down(ifp);
 1262                         splx(s);
 1263                 } else if (new_flags & IFF_UP &&
 1264                     (ifp->if_flags & IFF_UP) == 0) {
 1265                         int s = splimp();
 1266                         if_up(ifp);
 1267                         splx(s);
 1268                 }
 1269                 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
 1270                         (new_flags &~ IFF_CANTCHANGE);
 1271                 if (new_flags & IFF_PPROMISC) {
 1272                         /* Permanently promiscuous mode requested */
 1273                         ifp->if_flags |= IFF_PROMISC;
 1274                 } else if (ifp->if_pcount == 0) {
 1275                         ifp->if_flags &= ~IFF_PROMISC;
 1276                 }
 1277                 if (ifp->if_ioctl != NULL) {
 1278                         IFF_LOCKGIANT(ifp);
 1279                         (void) (*ifp->if_ioctl)(ifp, cmd, data);
 1280                         IFF_UNLOCKGIANT(ifp);
 1281                 }
 1282                 getmicrotime(&ifp->if_lastchange);
 1283                 break;
 1284 
 1285         case SIOCSIFCAP:
 1286                 error = suser(td);
 1287                 if (error)
 1288                         return (error);
 1289                 if (ifp->if_ioctl == NULL)
 1290                         return (EOPNOTSUPP);
 1291                 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
 1292                         return (EINVAL);
 1293                 IFF_LOCKGIANT(ifp);
 1294                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 1295                 IFF_UNLOCKGIANT(ifp);
 1296                 if (error == 0)
 1297                         getmicrotime(&ifp->if_lastchange);
 1298                 break;
 1299 
 1300 #ifdef MAC
 1301         case SIOCSIFMAC:
 1302                 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
 1303                 break;
 1304 #endif
 1305 
 1306         case SIOCSIFNAME:
 1307                 error = suser(td);
 1308                 if (error != 0)
 1309                         return (error);
 1310                 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
 1311                 if (error != 0)
 1312                         return (error);
 1313                 if (new_name[0] == '\0')
 1314                         return (EINVAL);
 1315                 if (ifunit(new_name) != NULL)
 1316                         return (EEXIST);
 1317                 
 1318                 /* Announce the departure of the interface. */
 1319                 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
 1320                 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
 1321 
 1322                 log(LOG_INFO, "%s: changing name to '%s'\n",
 1323                     ifp->if_xname, new_name);
 1324 
 1325                 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
 1326                 ifa = ifaddr_byindex(ifp->if_index);
 1327                 IFA_LOCK(ifa);
 1328                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
 1329                 namelen = strlen(new_name);
 1330                 onamelen = sdl->sdl_nlen;
 1331                 /*
 1332                  * Move the address if needed.  This is safe because we
 1333                  * allocate space for a name of length IFNAMSIZ when we
 1334                  * create this in if_attach().
 1335                  */
 1336                 if (namelen != onamelen) {
 1337                         bcopy(sdl->sdl_data + onamelen,
 1338                             sdl->sdl_data + namelen, sdl->sdl_alen);
 1339                 }
 1340                 bcopy(new_name, sdl->sdl_data, namelen);
 1341                 sdl->sdl_nlen = namelen;
 1342                 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
 1343                 bzero(sdl->sdl_data, onamelen);
 1344                 while (namelen != 0)
 1345                         sdl->sdl_data[--namelen] = 0xff;
 1346                 IFA_UNLOCK(ifa);
 1347 
 1348                 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
 1349                 /* Announce the return of the interface. */
 1350                 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
 1351                 break;
 1352 
 1353         case SIOCSIFMETRIC:
 1354                 error = suser(td);
 1355                 if (error)
 1356                         return (error);
 1357                 ifp->if_metric = ifr->ifr_metric;
 1358                 getmicrotime(&ifp->if_lastchange);
 1359                 break;
 1360 
 1361         case SIOCSIFPHYS:
 1362                 error = suser(td);
 1363                 if (error)
 1364                         return (error);
 1365                 if (ifp->if_ioctl == NULL)
 1366                         return (EOPNOTSUPP);
 1367                 IFF_LOCKGIANT(ifp);
 1368                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 1369                 IFF_UNLOCKGIANT(ifp);
 1370                 if (error == 0)
 1371                         getmicrotime(&ifp->if_lastchange);
 1372                 break;
 1373 
 1374         case SIOCSIFMTU:
 1375         {
 1376                 u_long oldmtu = ifp->if_mtu;
 1377 
 1378                 error = suser(td);
 1379                 if (error)
 1380                         return (error);
 1381                 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
 1382                         return (EINVAL);
 1383                 if (ifp->if_ioctl == NULL)
 1384                         return (EOPNOTSUPP);
 1385                 IFF_LOCKGIANT(ifp);
 1386                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 1387                 IFF_UNLOCKGIANT(ifp);
 1388                 if (error == 0) {
 1389                         getmicrotime(&ifp->if_lastchange);
 1390                         rt_ifmsg(ifp);
 1391                 }
 1392                 /*
 1393                  * If the link MTU changed, do network layer specific procedure.
 1394                  */
 1395                 if (ifp->if_mtu != oldmtu) {
 1396 #ifdef INET6
 1397                         nd6_setmtu(ifp);
 1398 #endif
 1399                 }
 1400                 break;
 1401         }
 1402 
 1403         case SIOCADDMULTI:
 1404         case SIOCDELMULTI:
 1405                 error = suser(td);
 1406                 if (error)
 1407                         return (error);
 1408 
 1409                 /* Don't allow group membership on non-multicast interfaces. */
 1410                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
 1411                         return (EOPNOTSUPP);
 1412 
 1413                 /* Don't let users screw up protocols' entries. */
 1414                 if (ifr->ifr_addr.sa_family != AF_LINK)
 1415                         return (EINVAL);
 1416 
 1417                 if (cmd == SIOCADDMULTI) {
 1418                         struct ifmultiaddr *ifma;
 1419                         error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
 1420                 } else {
 1421                         error = if_delmulti(ifp, &ifr->ifr_addr);
 1422                 }
 1423                 if (error == 0)
 1424                         getmicrotime(&ifp->if_lastchange);
 1425                 break;
 1426 
 1427         case SIOCSIFPHYADDR:
 1428         case SIOCDIFPHYADDR:
 1429 #ifdef INET6
 1430         case SIOCSIFPHYADDR_IN6:
 1431 #endif
 1432         case SIOCSLIFPHYADDR:
 1433         case SIOCSIFMEDIA:
 1434         case SIOCSIFGENERIC:
 1435                 error = suser(td);
 1436                 if (error)
 1437                         return (error);
 1438                 if (ifp->if_ioctl == NULL)
 1439                         return (EOPNOTSUPP);
 1440                 IFF_LOCKGIANT(ifp);
 1441                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 1442                 IFF_UNLOCKGIANT(ifp);
 1443                 if (error == 0)
 1444                         getmicrotime(&ifp->if_lastchange);
 1445                 break;
 1446 
 1447         case SIOCGIFSTATUS:
 1448                 ifs = (struct ifstat *)data;
 1449                 ifs->ascii[0] = '\0';
 1450 
 1451         case SIOCGIFPSRCADDR:
 1452         case SIOCGIFPDSTADDR:
 1453         case SIOCGLIFPHYADDR:
 1454         case SIOCGIFMEDIA:
 1455         case SIOCGIFGENERIC:
 1456                 if (ifp->if_ioctl == NULL)
 1457                         return (EOPNOTSUPP);
 1458                 IFF_LOCKGIANT(ifp);
 1459                 error = (*ifp->if_ioctl)(ifp, cmd, data);
 1460                 IFF_UNLOCKGIANT(ifp);
 1461                 break;
 1462 
 1463         case SIOCSIFLLADDR:
 1464                 error = suser(td);
 1465                 if (error)
 1466                         return (error);
 1467                 error = if_setlladdr(ifp,
 1468                     ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
 1469                 break;
 1470 
 1471         default:
 1472                 error = ENOIOCTL;
 1473                 break;
 1474         }
 1475         return (error);
 1476 }
 1477 
 1478 /*
 1479  * Interface ioctls.
 1480  */
 1481 int
 1482 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
 1483 {
 1484         struct ifnet *ifp;
 1485         struct ifreq *ifr;
 1486         int error;
 1487         int oif_flags;
 1488 
 1489         switch (cmd) {
 1490         case SIOCGIFCONF:
 1491         case OSIOCGIFCONF:
 1492                 return (ifconf(cmd, data));
 1493         }
 1494         ifr = (struct ifreq *)data;
 1495 
 1496         switch (cmd) {
 1497         case SIOCIFCREATE:
 1498         case SIOCIFDESTROY:
 1499                 if ((error = suser(td)) != 0)
 1500                         return (error);
 1501                 return ((cmd == SIOCIFCREATE) ?
 1502                         if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
 1503                         if_clone_destroy(ifr->ifr_name));
 1504 
 1505         case SIOCIFGCLONERS:
 1506                 return (if_clone_list((struct if_clonereq *)data));
 1507         }
 1508 
 1509         ifp = ifunit(ifr->ifr_name);
 1510         if (ifp == 0)
 1511                 return (ENXIO);
 1512 
 1513         error = ifhwioctl(cmd, ifp, data, td);
 1514         if (error != ENOIOCTL)
 1515                 return (error);
 1516 
 1517         oif_flags = ifp->if_flags;
 1518         if (so->so_proto == 0)
 1519                 return (EOPNOTSUPP);
 1520 #ifndef COMPAT_43
 1521         error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
 1522                                                                  data,
 1523                                                                  ifp, td));
 1524 #else
 1525         {
 1526                 int ocmd = cmd;
 1527 
 1528                 switch (cmd) {
 1529 
 1530                 case SIOCSIFDSTADDR:
 1531                 case SIOCSIFADDR:
 1532                 case SIOCSIFBRDADDR:
 1533                 case SIOCSIFNETMASK:
 1534 #if BYTE_ORDER != BIG_ENDIAN
 1535                         if (ifr->ifr_addr.sa_family == 0 &&
 1536                             ifr->ifr_addr.sa_len < 16) {
 1537                                 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
 1538                                 ifr->ifr_addr.sa_len = 16;
 1539                         }
 1540 #else
 1541                         if (ifr->ifr_addr.sa_len == 0)
 1542                                 ifr->ifr_addr.sa_len = 16;
 1543 #endif
 1544                         break;
 1545 
 1546                 case OSIOCGIFADDR:
 1547                         cmd = SIOCGIFADDR;
 1548                         break;
 1549 
 1550                 case OSIOCGIFDSTADDR:
 1551                         cmd = SIOCGIFDSTADDR;
 1552                         break;
 1553 
 1554                 case OSIOCGIFBRDADDR:
 1555                         cmd = SIOCGIFBRDADDR;
 1556                         break;
 1557 
 1558                 case OSIOCGIFNETMASK:
 1559                         cmd = SIOCGIFNETMASK;
 1560                 }
 1561                 error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
 1562                                                                    cmd,
 1563                                                                    data,
 1564                                                                    ifp, td));
 1565                 switch (ocmd) {
 1566 
 1567                 case OSIOCGIFADDR:
 1568                 case OSIOCGIFDSTADDR:
 1569                 case OSIOCGIFBRDADDR:
 1570                 case OSIOCGIFNETMASK:
 1571                         *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
 1572 
 1573                 }
 1574         }
 1575 #endif /* COMPAT_43 */
 1576 
 1577         if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
 1578 #ifdef INET6
 1579                 DELAY(100);/* XXX: temporary workaround for fxp issue*/
 1580                 if (ifp->if_flags & IFF_UP) {
 1581                         int s = splimp();
 1582                         in6_if_up(ifp);
 1583                         splx(s);
 1584                 }
 1585 #endif
 1586         }
 1587         return (error);
 1588 }
 1589 
 1590 /*
 1591  * The code common to handling reference counted flags,
 1592  * e.g., in ifpromisc() and if_allmulti().
 1593  * The "pflag" argument can specify a permanent mode flag,
 1594  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
 1595  *
 1596  * Only to be used on stack-owned flags, not driver-owned flags.
 1597  */
 1598 static int
 1599 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
 1600 {
 1601         struct ifreq ifr;
 1602         int error;
 1603         int oldflags, oldcount;
 1604 
 1605         KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
 1606             ("if_setflag: setting driver-ownded flag %d", flag));
 1607 
 1608         /* Sanity checks to catch programming errors */
 1609         if (onswitch) {
 1610                 if (*refcount < 0) {
 1611                         if_printf(ifp,
 1612                             "refusing to increment negative refcount %d "
 1613                             "for interface flag %d\n", *refcount, flag);
 1614                         return (EINVAL);
 1615                 }
 1616         } else {
 1617                 if (*refcount <= 0) {
 1618                         if_printf(ifp,
 1619                             "refusing to decrement non-positive refcount %d"
 1620                             "for interface flag %d\n", *refcount, flag);
 1621                         return (EINVAL);
 1622                 }
 1623         }
 1624 
 1625         /* In case this mode is permanent, just touch refcount */
 1626         if (ifp->if_flags & pflag) {
 1627                 *refcount += onswitch ? 1 : -1;
 1628                 return (0);
 1629         }
 1630 
 1631         /* Save ifnet parameters for if_ioctl() may fail */
 1632         oldcount = *refcount;
 1633         oldflags = ifp->if_flags;
 1634         
 1635         /*
 1636          * See if we aren't the only and touching refcount is enough.
 1637          * Actually toggle interface flag if we are the first or last.
 1638          */
 1639         if (onswitch) {
 1640                 if ((*refcount)++)
 1641                         return (0);
 1642                 ifp->if_flags |= flag;
 1643         } else {
 1644                 if (--(*refcount))
 1645                         return (0);
 1646                 ifp->if_flags &= ~flag;
 1647         }
 1648 
 1649         /* Call down the driver since we've changed interface flags */
 1650         if (ifp->if_ioctl == NULL) {
 1651                 error = EOPNOTSUPP;
 1652                 goto recover;
 1653         }
 1654         ifr.ifr_flags = ifp->if_flags & 0xffff;
 1655         ifr.ifr_flagshigh = ifp->if_flags >> 16;
 1656         IFF_LOCKGIANT(ifp);
 1657         error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
 1658         IFF_UNLOCKGIANT(ifp);
 1659         if (error)
 1660                 goto recover;
 1661         /* Notify userland that interface flags have changed */
 1662         rt_ifmsg(ifp);
 1663         return (0);
 1664 
 1665 recover:
 1666         /* Recover after driver error */
 1667         *refcount = oldcount;
 1668         ifp->if_flags = oldflags;
 1669         return (error);
 1670 }
 1671 
 1672 /*
 1673  * Set/clear promiscuous mode on interface ifp based on the truth value
 1674  * of pswitch.  The calls are reference counted so that only the first
 1675  * "on" request actually has an effect, as does the final "off" request.
 1676  * Results are undefined if the "off" and "on" requests are not matched.
 1677  */
 1678 int
 1679 ifpromisc(struct ifnet *ifp, int pswitch)
 1680 {
 1681         int error;
 1682         int oldflags = ifp->if_flags;
 1683 
 1684         error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
 1685                            &ifp->if_pcount, pswitch);
 1686         /* If promiscuous mode status has changed, log a message */
 1687         if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
 1688                 log(LOG_INFO, "%s: promiscuous mode %s\n",
 1689                     ifp->if_xname,
 1690                     (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
 1691         return (error);
 1692 }
 1693 
 1694 /*
 1695  * Return interface configuration
 1696  * of system.  List may be used
 1697  * in later ioctl's (above) to get
 1698  * other information.
 1699  */
 1700 /*ARGSUSED*/
 1701 static int
 1702 ifconf(u_long cmd, caddr_t data)
 1703 {
 1704         struct ifconf *ifc = (struct ifconf *)data;
 1705         struct ifnet *ifp;
 1706         struct ifaddr *ifa;
 1707         struct ifreq ifr;
 1708         struct sbuf *sb;
 1709         int error, full = 0, valid_len, max_len;
 1710 
 1711         /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
 1712         max_len = MAXPHYS - 1;
 1713 
 1714         /* Prevent hostile input from being able to crash the system */
 1715         if (ifc->ifc_len <= 0)
 1716                 return (EINVAL);
 1717 
 1718 again:
 1719         if (ifc->ifc_len <= max_len) {
 1720                 max_len = ifc->ifc_len;
 1721                 full = 1;
 1722         }
 1723         sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
 1724         max_len = 0;
 1725         valid_len = 0;
 1726 
 1727         IFNET_RLOCK();          /* could sleep XXX */
 1728         TAILQ_FOREACH(ifp, &ifnet, if_link) {
 1729                 int addrs;
 1730 
 1731                 /*
 1732                  * Zero the ifr_name buffer to make sure we don't
 1733                  * disclose the contents of the stack.
 1734                  */
 1735                 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
 1736 
 1737                 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
 1738                     >= sizeof(ifr.ifr_name)) {
 1739                         sbuf_delete(sb);
 1740                         IFNET_RUNLOCK();
 1741                         return (ENAMETOOLONG);
 1742                 }
 1743 
 1744                 addrs = 0;
 1745                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1746                         struct sockaddr *sa = ifa->ifa_addr;
 1747 
 1748                         if (jailed(curthread->td_ucred) &&
 1749                             prison_if(curthread->td_ucred, sa))
 1750                                 continue;
 1751                         addrs++;
 1752 #ifdef COMPAT_43
 1753                         if (cmd == OSIOCGIFCONF) {
 1754                                 struct osockaddr *osa =
 1755                                          (struct osockaddr *)&ifr.ifr_addr;
 1756                                 ifr.ifr_addr = *sa;
 1757                                 osa->sa_family = sa->sa_family;
 1758                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
 1759                                 max_len += sizeof(ifr);
 1760                         } else
 1761 #endif
 1762                         if (sa->sa_len <= sizeof(*sa)) {
 1763                                 ifr.ifr_addr = *sa;
 1764                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
 1765                                 max_len += sizeof(ifr);
 1766                         } else {
 1767                                 sbuf_bcat(sb, &ifr,
 1768                                     offsetof(struct ifreq, ifr_addr));
 1769                                 max_len += offsetof(struct ifreq, ifr_addr);
 1770                                 sbuf_bcat(sb, sa, sa->sa_len);
 1771                                 max_len += sa->sa_len;
 1772                         }
 1773 
 1774                         if (!sbuf_overflowed(sb))
 1775                                 valid_len = sbuf_len(sb);
 1776                 }
 1777                 if (addrs == 0) {
 1778                         bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
 1779                         sbuf_bcat(sb, &ifr, sizeof(ifr));
 1780                         max_len += sizeof(ifr);
 1781 
 1782                         if (!sbuf_overflowed(sb))
 1783                                 valid_len = sbuf_len(sb);
 1784                 }
 1785         }
 1786         IFNET_RUNLOCK();
 1787 
 1788         /*
 1789          * If we didn't allocate enough space (uncommon), try again.  If
 1790          * we have already allocated as much space as we are allowed,
 1791          * return what we've got.
 1792          */
 1793         if (valid_len != max_len && !full) {
 1794                 sbuf_delete(sb);
 1795                 goto again;
 1796         }
 1797 
 1798         ifc->ifc_len = valid_len;
 1799         sbuf_finish(sb);
 1800         error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
 1801         sbuf_delete(sb);
 1802         return (error);
 1803 }
 1804 
 1805 /*
 1806  * Just like ifpromisc(), but for all-multicast-reception mode.
 1807  */
 1808 int
 1809 if_allmulti(struct ifnet *ifp, int onswitch)
 1810 {
 1811 
 1812         return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
 1813 }
 1814 
 1815 static struct ifmultiaddr *
 1816 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
 1817 {
 1818         struct ifmultiaddr *ifma;
 1819 
 1820         IF_ADDR_LOCK_ASSERT(ifp);
 1821 
 1822         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 1823                 if (sa_equal(ifma->ifma_addr, sa))
 1824                         break;
 1825         }
 1826 
 1827         return ifma;
 1828 }
 1829 
 1830 /*
 1831  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
 1832  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
 1833  * the ifnet multicast address list here, so the caller must do that and
 1834  * other setup work (such as notifying the device driver).  The reference
 1835  * count is initialized to 1.
 1836  */
 1837 static struct ifmultiaddr *
 1838 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
 1839     int mflags)
 1840 {
 1841         struct ifmultiaddr *ifma;
 1842         struct sockaddr *dupsa;
 1843 
 1844         MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
 1845             M_ZERO);
 1846         if (ifma == NULL)
 1847                 return (NULL);
 1848 
 1849         MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
 1850         if (dupsa == NULL) {
 1851                 FREE(ifma, M_IFMADDR);
 1852                 return (NULL);
 1853         }
 1854         bcopy(sa, dupsa, sa->sa_len);
 1855         ifma->ifma_addr = dupsa;
 1856 
 1857         ifma->ifma_ifp = ifp;
 1858         ifma->ifma_refcount = 1;
 1859         ifma->ifma_protospec = NULL;
 1860 
 1861         if (llsa == NULL) {
 1862                 ifma->ifma_lladdr = NULL;
 1863                 return (ifma);
 1864         }
 1865 
 1866         MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
 1867         if (dupsa == NULL) {
 1868                 FREE(ifma->ifma_addr, M_IFMADDR);
 1869                 FREE(ifma, M_IFMADDR);
 1870                 return (NULL);
 1871         }
 1872         bcopy(llsa, dupsa, llsa->sa_len);
 1873         ifma->ifma_lladdr = dupsa;
 1874 
 1875         return (ifma);
 1876 }
 1877 
 1878 /*
 1879  * if_freemulti: free ifmultiaddr structure and possibly attached related
 1880  * addresses.  The caller is responsible for implementing reference
 1881  * counting, notifying the driver, handling routing messages, and releasing
 1882  * any dependent link layer state.
 1883  */
 1884 static void
 1885 if_freemulti(struct ifmultiaddr *ifma)
 1886 {
 1887 
 1888         KASSERT(ifma->ifma_refcount == 1, ("if_freemulti: refcount %d",
 1889             ifma->ifma_refcount));
 1890         KASSERT(ifma->ifma_protospec == NULL,
 1891             ("if_freemulti: protospec not NULL"));
 1892 
 1893         if (ifma->ifma_lladdr != NULL)
 1894                 FREE(ifma->ifma_lladdr, M_IFMADDR);
 1895         FREE(ifma->ifma_addr, M_IFMADDR);
 1896         FREE(ifma, M_IFMADDR);
 1897 }
 1898 
 1899 /*
 1900  * Register an additional multicast address with a network interface.
 1901  *
 1902  * - If the address is already present, bump the reference count on the
 1903  *   address and return.
 1904  * - If the address is not link-layer, look up a link layer address.
 1905  * - Allocate address structures for one or both addresses, and attach to the
 1906  *   multicast address list on the interface.  If automatically adding a link
 1907  *   layer address, the protocol address will own a reference to the link
 1908  *   layer address, to be freed when it is freed.
 1909  * - Notify the network device driver of an addition to the multicast address
 1910  *   list.
 1911  *
 1912  * 'sa' points to caller-owned memory with the desired multicast address.
 1913  *
 1914  * 'retifma' will be used to return a pointer to the resulting multicast
 1915  * address reference, if desired.
 1916  */
 1917 int
 1918 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
 1919     struct ifmultiaddr **retifma)
 1920 {
 1921         struct ifmultiaddr *ifma, *ll_ifma;
 1922         struct sockaddr *llsa;
 1923         int error;
 1924 
 1925         /*
 1926          * If the address is already present, return a new reference to it;
 1927          * otherwise, allocate storage and set up a new address.
 1928          */
 1929         IF_ADDR_LOCK(ifp);
 1930         ifma = if_findmulti(ifp, sa);
 1931         if (ifma != NULL) {
 1932                 ifma->ifma_refcount++;
 1933                 if (retifma != NULL)
 1934                         *retifma = ifma;
 1935                 IF_ADDR_UNLOCK(ifp);
 1936                 return (0);
 1937         }
 1938 
 1939         /*
 1940          * The address isn't already present; resolve the protocol address
 1941          * into a link layer address, and then look that up, bump its
 1942          * refcount or allocate an ifma for that also.  If 'llsa' was
 1943          * returned, we will need to free it later.
 1944          */
 1945         llsa = NULL;
 1946         ll_ifma = NULL;
 1947         if (ifp->if_resolvemulti != NULL) {
 1948                 error = ifp->if_resolvemulti(ifp, &llsa, sa);
 1949                 if (error)
 1950                         goto unlock_out;
 1951         }
 1952 
 1953         /*
 1954          * Allocate the new address.  Don't hook it up yet, as we may also
 1955          * need to allocate a link layer multicast address.
 1956          */
 1957         ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
 1958         if (ifma == NULL) {
 1959                 error = ENOMEM;
 1960                 goto free_llsa_out;
 1961         }
 1962 
 1963         /*
 1964          * If a link layer address is found, we'll need to see if it's
 1965          * already present in the address list, or allocate is as well.
 1966          * When this block finishes, the link layer address will be on the
 1967          * list.
 1968          */
 1969         if (llsa != NULL) {
 1970                 ll_ifma = if_findmulti(ifp, llsa);
 1971                 if (ll_ifma == NULL) {
 1972                         ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
 1973                         if (ll_ifma == NULL) {
 1974                                 if_freemulti(ifma);
 1975                                 error = ENOMEM;
 1976                                 goto free_llsa_out;
 1977                         }
 1978                         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
 1979                             ifma_link);
 1980                 } else
 1981                         ll_ifma->ifma_refcount++;
 1982         }
 1983 
 1984         /*
 1985          * We now have a new multicast address, ifma, and possibly a new or
 1986          * referenced link layer address.  Add the primary address to the
 1987          * ifnet address list.
 1988          */
 1989         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
 1990 
 1991         if (retifma != NULL)
 1992                 *retifma = ifma;
 1993 
 1994         /*
 1995          * Must generate the message while holding the lock so that 'ifma'
 1996          * pointer is still valid.
 1997          *
 1998          * XXXRW: How come we don't announce ll_ifma?
 1999          */
 2000         rt_newmaddrmsg(RTM_NEWMADDR, ifma);
 2001         IF_ADDR_UNLOCK(ifp);
 2002 
 2003         /*
 2004          * We are certain we have added something, so call down to the
 2005          * interface to let them know about it.
 2006          */
 2007         if (ifp->if_ioctl != NULL) {
 2008                 IFF_LOCKGIANT(ifp);
 2009                 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
 2010                 IFF_UNLOCKGIANT(ifp);
 2011         }
 2012 
 2013         if (llsa != NULL)
 2014                 FREE(llsa, M_IFMADDR);
 2015 
 2016         return (0);
 2017 
 2018 free_llsa_out:
 2019         if (llsa != NULL)
 2020                 FREE(llsa, M_IFMADDR);
 2021 
 2022 unlock_out:
 2023         IF_ADDR_UNLOCK(ifp);
 2024         return (error);
 2025 }
 2026 
 2027 /*
 2028  * Remove a reference to a multicast address on this interface.  Yell
 2029  * if the request does not match an existing membership.
 2030  */
 2031 int
 2032 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
 2033 {
 2034         struct ifmultiaddr *ifma, *ll_ifma;
 2035 
 2036         IF_ADDR_LOCK(ifp);
 2037         ifma = if_findmulti(ifp, sa);
 2038         if (ifma == NULL) {
 2039                 IF_ADDR_UNLOCK(ifp);
 2040                 return ENOENT;
 2041         }
 2042 
 2043         if (ifma->ifma_refcount > 1) {
 2044                 ifma->ifma_refcount--;
 2045                 IF_ADDR_UNLOCK(ifp);
 2046                 return 0;
 2047         }
 2048 
 2049         sa = ifma->ifma_lladdr;
 2050         if (sa != NULL)
 2051                 ll_ifma = if_findmulti(ifp, sa);
 2052         else
 2053                 ll_ifma = NULL;
 2054 
 2055         /*
 2056          * XXXRW: How come we don't announce ll_ifma?
 2057          */
 2058         rt_newmaddrmsg(RTM_DELMADDR, ifma);
 2059 
 2060         TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
 2061         if_freemulti(ifma);
 2062 
 2063         if (ll_ifma != NULL) {
 2064                 if (ll_ifma->ifma_refcount == 1) {
 2065                         TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifma_link);
 2066                         if_freemulti(ll_ifma);
 2067                 } else
 2068                         ll_ifma->ifma_refcount--;
 2069         }
 2070         IF_ADDR_UNLOCK(ifp);
 2071 
 2072         /*
 2073          * Make sure the interface driver is notified
 2074          * in the case of a link layer mcast group being left.
 2075          */
 2076         if (ifp->if_ioctl) {
 2077                 IFF_LOCKGIANT(ifp);
 2078                 (void) (*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
 2079                 IFF_UNLOCKGIANT(ifp);
 2080         }
 2081 
 2082         return 0;
 2083 }
 2084 
 2085 /*
 2086  * Set the link layer address on an interface.
 2087  *
 2088  * At this time we only support certain types of interfaces,
 2089  * and we don't allow the length of the address to change.
 2090  */
 2091 int
 2092 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
 2093 {
 2094         struct sockaddr_dl *sdl;
 2095         struct ifaddr *ifa;
 2096         struct ifreq ifr;
 2097 
 2098         ifa = ifaddr_byindex(ifp->if_index);
 2099         if (ifa == NULL)
 2100                 return (EINVAL);
 2101         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
 2102         if (sdl == NULL)
 2103                 return (EINVAL);
 2104         if (len != sdl->sdl_alen)       /* don't allow length to change */
 2105                 return (EINVAL);
 2106         switch (ifp->if_type) {
 2107         case IFT_ETHER:                 /* these types use struct arpcom */
 2108         case IFT_FDDI:
 2109         case IFT_XETHER:
 2110         case IFT_ISO88025:
 2111         case IFT_L2VLAN:
 2112         case IFT_BRIDGE:
 2113                 bcopy(lladdr, IFP2ENADDR(ifp), len);
 2114                 /*
 2115                  * XXX We also need to store the lladdr in LLADDR(sdl),
 2116                  * which is done below. This is a pain because we must
 2117                  * remember to keep the info in sync.
 2118                  */
 2119                 /* FALLTHROUGH */
 2120         case IFT_ARCNET:
 2121                 bcopy(lladdr, LLADDR(sdl), len);
 2122                 break;
 2123         default:
 2124                 return (ENODEV);
 2125         }
 2126         /*
 2127          * If the interface is already up, we need
 2128          * to re-init it in order to reprogram its
 2129          * address filter.
 2130          */
 2131         if ((ifp->if_flags & IFF_UP) != 0) {
 2132                 if (ifp->if_ioctl) {
 2133                         IFF_LOCKGIANT(ifp);
 2134                         ifp->if_flags &= ~IFF_UP;
 2135                         ifr.ifr_flags = ifp->if_flags & 0xffff;
 2136                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
 2137                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
 2138                         ifp->if_flags |= IFF_UP;
 2139                         ifr.ifr_flags = ifp->if_flags & 0xffff;
 2140                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
 2141                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
 2142                         IFF_UNLOCKGIANT(ifp);
 2143                 }
 2144 #ifdef INET
 2145                 /*
 2146                  * Also send gratuitous ARPs to notify other nodes about
 2147                  * the address change.
 2148                  */
 2149                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 2150                         if (ifa->ifa_addr != NULL &&
 2151                             ifa->ifa_addr->sa_family == AF_INET)
 2152                                 arp_ifinit(ifp, ifa);
 2153                 }
 2154 #endif
 2155         }
 2156         return (0);
 2157 }
 2158 
 2159 /*
 2160  * The name argument must be a pointer to storage which will last as
 2161  * long as the interface does.  For physical devices, the result of
 2162  * device_get_name(dev) is a good choice and for pseudo-devices a
 2163  * static string works well.
 2164  */
 2165 void
 2166 if_initname(struct ifnet *ifp, const char *name, int unit)
 2167 {
 2168         ifp->if_dname = name;
 2169         ifp->if_dunit = unit;
 2170         if (unit != IF_DUNIT_NONE)
 2171                 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
 2172         else
 2173                 strlcpy(ifp->if_xname, name, IFNAMSIZ);
 2174 }
 2175 
 2176 int
 2177 if_printf(struct ifnet *ifp, const char * fmt, ...)
 2178 {
 2179         va_list ap;
 2180         int retval;
 2181 
 2182         retval = printf("%s: ", ifp->if_xname);
 2183         va_start(ap, fmt);
 2184         retval += vprintf(fmt, ap);
 2185         va_end(ap);
 2186         return (retval);
 2187 }
 2188 
 2189 /*
 2190  * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
 2191  * be called without Giant.  However, we often can't acquire the Giant lock
 2192  * at those points; instead, we run it via a task queue that holds Giant via
 2193  * if_start_deferred.
 2194  *
 2195  * XXXRW: We need to make sure that the ifnet isn't fully detached until any
 2196  * outstanding if_start_deferred() tasks that will run after the free.  This
 2197  * probably means waiting in if_detach().
 2198  */
 2199 void
 2200 if_start(struct ifnet *ifp)
 2201 {
 2202 
 2203         NET_ASSERT_GIANT();
 2204 
 2205         if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
 2206                 if (mtx_owned(&Giant))
 2207                         (*(ifp)->if_start)(ifp);
 2208                 else
 2209                         taskqueue_enqueue(taskqueue_swi_giant,
 2210                             &ifp->if_starttask);
 2211         } else
 2212                 (*(ifp)->if_start)(ifp);
 2213 }
 2214 
 2215 static void
 2216 if_start_deferred(void *context, int pending)
 2217 {
 2218         struct ifnet *ifp;
 2219 
 2220         /*
 2221          * This code must be entered with Giant, and should never run if
 2222          * we're not running with debug.mpsafenet.
 2223          */
 2224         KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
 2225         GIANT_REQUIRED;
 2226 
 2227         ifp = context;
 2228         (ifp->if_start)(ifp);
 2229 }
 2230 
 2231 int
 2232 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
 2233 {
 2234         int active = 0;
 2235 
 2236         IF_LOCK(ifq);
 2237         if (_IF_QFULL(ifq)) {
 2238                 _IF_DROP(ifq);
 2239                 IF_UNLOCK(ifq);
 2240                 m_freem(m);
 2241                 return (0);
 2242         }
 2243         if (ifp != NULL) {
 2244                 ifp->if_obytes += m->m_pkthdr.len + adjust;
 2245                 if (m->m_flags & (M_BCAST|M_MCAST))
 2246                         ifp->if_omcasts++;
 2247                 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
 2248         }
 2249         _IF_ENQUEUE(ifq, m);
 2250         IF_UNLOCK(ifq);
 2251         if (ifp != NULL && !active)
 2252                 if_start(ifp);
 2253         return (1);
 2254 }
 2255 
 2256 void
 2257 if_register_com_alloc(u_char type,
 2258     if_com_alloc_t *a, if_com_free_t *f)
 2259 {
 2260         
 2261         KASSERT(if_com_alloc[type] == NULL,
 2262             ("if_register_com_alloc: %d already registered", type));
 2263         KASSERT(if_com_free[type] == NULL,
 2264             ("if_register_com_alloc: %d free already registered", type));
 2265 
 2266         if_com_alloc[type] = a;
 2267         if_com_free[type] = f;
 2268 }
 2269 
 2270 void
 2271 if_deregister_com_alloc(u_char type)
 2272 {
 2273         
 2274         KASSERT(if_com_alloc[type] == NULL,
 2275             ("if_deregister_com_alloc: %d not registered", type));
 2276         KASSERT(if_com_free[type] == NULL,
 2277             ("if_deregister_com_alloc: %d free not registered", type));
 2278         if_com_alloc[type] = NULL;
 2279         if_com_free[type] = NULL;
 2280 }

Cache object: 8abf8994cd8c47341e6e9e4801bb3dcf


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