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

Cache object: acafa0a0068432f0fa503b7cb219e4d1


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