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

Cache object: c9052376495b68cb48911e1dc98d7069


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