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


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

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

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

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

Cache object: 81602bfec9ad6fd9fec1af494d490bdb


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