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

Cache object: c993e6121e04e81da4aa46f42749aca1


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