Home | History | Annotate | Line # | Download | only in net
if.c revision 1.179
      1 /*	$NetBSD: if.c,v 1.179 2006/12/02 20:48:13 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by William Studenmund and Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the project nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  */
     67 
     68 /*
     69  * Copyright (c) 1980, 1986, 1993
     70  *	The Regents of the University of California.  All rights reserved.
     71  *
     72  * Redistribution and use in source and binary forms, with or without
     73  * modification, are permitted provided that the following conditions
     74  * are met:
     75  * 1. Redistributions of source code must retain the above copyright
     76  *    notice, this list of conditions and the following disclaimer.
     77  * 2. Redistributions in binary form must reproduce the above copyright
     78  *    notice, this list of conditions and the following disclaimer in the
     79  *    documentation and/or other materials provided with the distribution.
     80  * 3. Neither the name of the University nor the names of its contributors
     81  *    may be used to endorse or promote products derived from this software
     82  *    without specific prior written permission.
     83  *
     84  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     85  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     86  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     87  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     88  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     90  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94  * SUCH DAMAGE.
     95  *
     96  *	@(#)if.c	8.5 (Berkeley) 1/9/95
     97  */
     98 
     99 #include <sys/cdefs.h>
    100 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.179 2006/12/02 20:48:13 dyoung Exp $");
    101 
    102 #include "opt_inet.h"
    103 
    104 #include "opt_compat_linux.h"
    105 #include "opt_compat_svr4.h"
    106 #include "opt_compat_ultrix.h"
    107 #include "opt_compat_43.h"
    108 #include "opt_atalk.h"
    109 #include "opt_natm.h"
    110 #include "opt_pfil_hooks.h"
    111 
    112 #include <sys/param.h>
    113 #include <sys/mbuf.h>
    114 #include <sys/systm.h>
    115 #include <sys/callout.h>
    116 #include <sys/proc.h>
    117 #include <sys/socket.h>
    118 #include <sys/socketvar.h>
    119 #include <sys/domain.h>
    120 #include <sys/protosw.h>
    121 #include <sys/kernel.h>
    122 #include <sys/ioctl.h>
    123 #include <sys/sysctl.h>
    124 #include <sys/syslog.h>
    125 #include <sys/kauth.h>
    126 
    127 #include <net/if.h>
    128 #include <net/if_dl.h>
    129 #include <net/if_ether.h>
    130 #include <net/if_media.h>
    131 #include <net80211/ieee80211.h>
    132 #include <net80211/ieee80211_ioctl.h>
    133 #include <net/if_types.h>
    134 #include <net/radix.h>
    135 #include <net/route.h>
    136 #include <net/netisr.h>
    137 #ifdef NETATALK
    138 #include <netatalk/at_extern.h>
    139 #include <netatalk/at.h>
    140 #endif
    141 #include <net/pfil.h>
    142 
    143 #ifdef INET6
    144 #include <netinet/in.h>
    145 #include <netinet6/in6_var.h>
    146 #include <netinet6/nd6.h>
    147 #endif
    148 
    149 #include "carp.h"
    150 #if NCARP > 0
    151 #include <netinet/ip_carp.h>
    152 #endif
    153 
    154 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) || defined(COMPAT_ULTRIX) || defined(LKM)
    155 #define COMPAT_OSOCK
    156 #include <compat/sys/socket.h>
    157 #endif
    158 
    159 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
    160 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
    161 
    162 int	ifqmaxlen = IFQ_MAXLEN;
    163 struct	callout if_slowtimo_ch;
    164 
    165 int netisr;			/* scheduling bits for network */
    166 
    167 static int	if_rt_walktree(struct radix_node *, void *);
    168 
    169 static struct if_clone *if_clone_lookup(const char *, int *);
    170 static int	if_clone_list(struct if_clonereq *);
    171 
    172 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
    173 static int if_cloners_count;
    174 
    175 #ifdef PFIL_HOOKS
    176 struct pfil_head if_pfil;	/* packet filtering hook for interfaces */
    177 #endif
    178 
    179 static void if_detach_queues(struct ifnet *, struct ifqueue *);
    180 
    181 /*
    182  * Network interface utility routines.
    183  *
    184  * Routines with ifa_ifwith* names take sockaddr *'s as
    185  * parameters.
    186  */
    187 void
    188 ifinit(void)
    189 {
    190 
    191 	callout_init(&if_slowtimo_ch);
    192 	if_slowtimo(NULL);
    193 #ifdef PFIL_HOOKS
    194 	if_pfil.ph_type = PFIL_TYPE_IFNET;
    195 	if_pfil.ph_ifnet = NULL;
    196 	if (pfil_head_register(&if_pfil) != 0)
    197 		printf("WARNING: unable to register pfil hook\n");
    198 #endif
    199 }
    200 
    201 /*
    202  * Null routines used while an interface is going away.  These routines
    203  * just return an error.
    204  */
    205 
    206 int
    207 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
    208     struct sockaddr *so, struct rtentry *rt)
    209 {
    210 
    211 	return (ENXIO);
    212 }
    213 
    214 void
    215 if_nullinput(struct ifnet *ifp, struct mbuf *m)
    216 {
    217 
    218 	/* Nothing. */
    219 }
    220 
    221 void
    222 if_nullstart(struct ifnet *ifp)
    223 {
    224 
    225 	/* Nothing. */
    226 }
    227 
    228 int
    229 if_nullioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    230 {
    231 
    232 	return (ENXIO);
    233 }
    234 
    235 int
    236 if_nullinit(struct ifnet *ifp)
    237 {
    238 
    239 	return (ENXIO);
    240 }
    241 
    242 void
    243 if_nullstop(struct ifnet *ifp, int disable)
    244 {
    245 
    246 	/* Nothing. */
    247 }
    248 
    249 void
    250 if_nullwatchdog(struct ifnet *ifp)
    251 {
    252 
    253 	/* Nothing. */
    254 }
    255 
    256 void
    257 if_nulldrain(struct ifnet *ifp)
    258 {
    259 
    260 	/* Nothing. */
    261 }
    262 
    263 static u_int if_index = 1;
    264 struct ifnet_head ifnet;
    265 size_t if_indexlim = 0;
    266 struct ifaddr **ifnet_addrs = NULL;
    267 struct ifnet **ifindex2ifnet = NULL;
    268 struct ifnet *lo0ifp;
    269 
    270 /*
    271  * Allocate the link level name for the specified interface.  This
    272  * is an attachment helper.  It must be called after ifp->if_addrlen
    273  * is initialized, which may not be the case when if_attach() is
    274  * called.
    275  */
    276 void
    277 if_alloc_sadl(struct ifnet *ifp)
    278 {
    279 	unsigned socksize, ifasize;
    280 	int namelen, masklen;
    281 	struct sockaddr_dl *sdl;
    282 	struct ifaddr *ifa;
    283 
    284 	/*
    285 	 * If the interface already has a link name, release it
    286 	 * now.  This is useful for interfaces that can change
    287 	 * link types, and thus switch link names often.
    288 	 */
    289 	if (ifp->if_sadl != NULL)
    290 		if_free_sadl(ifp);
    291 
    292 	namelen = strlen(ifp->if_xname);
    293 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
    294 	socksize = masklen + ifp->if_addrlen;
    295 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
    296 	if (socksize < sizeof(*sdl))
    297 		socksize = sizeof(*sdl);
    298 	socksize = ROUNDUP(socksize);
    299 	ifasize = sizeof(*ifa) + 2 * socksize;
    300 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
    301 	memset((caddr_t)ifa, 0, ifasize);
    302 	sdl = (struct sockaddr_dl *)(ifa + 1);
    303 	sdl->sdl_len = socksize;
    304 	sdl->sdl_family = AF_LINK;
    305 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
    306 	sdl->sdl_nlen = namelen;
    307 	sdl->sdl_alen = ifp->if_addrlen;
    308 	sdl->sdl_index = ifp->if_index;
    309 	sdl->sdl_type = ifp->if_type;
    310 	ifnet_addrs[ifp->if_index] = ifa;
    311 	IFAREF(ifa);
    312 	ifa->ifa_ifp = ifp;
    313 	ifa->ifa_rtrequest = link_rtrequest;
    314 	TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
    315 	IFAREF(ifa);
    316 	ifa->ifa_addr = (struct sockaddr *)sdl;
    317 	ifp->if_sadl = sdl;
    318 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
    319 	ifa->ifa_netmask = (struct sockaddr *)sdl;
    320 	sdl->sdl_len = masklen;
    321 	while (namelen != 0)
    322 		sdl->sdl_data[--namelen] = 0xff;
    323 }
    324 
    325 /*
    326  * Free the link level name for the specified interface.  This is
    327  * a detach helper.  This is called from if_detach() or from
    328  * link layer type specific detach functions.
    329  */
    330 void
    331 if_free_sadl(struct ifnet *ifp)
    332 {
    333 	struct ifaddr *ifa;
    334 	int s;
    335 
    336 	ifa = ifnet_addrs[ifp->if_index];
    337 	if (ifa == NULL) {
    338 		KASSERT(ifp->if_sadl == NULL);
    339 		return;
    340 	}
    341 
    342 	KASSERT(ifp->if_sadl != NULL);
    343 
    344 	s = splnet();
    345 	rtinit(ifa, RTM_DELETE, 0);
    346 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
    347 	IFAFREE(ifa);
    348 
    349 	ifp->if_sadl = NULL;
    350 
    351 	ifnet_addrs[ifp->if_index] = NULL;
    352 	IFAFREE(ifa);
    353 	splx(s);
    354 }
    355 
    356 /*
    357  * Attach an interface to the
    358  * list of "active" interfaces.
    359  */
    360 void
    361 if_attach(struct ifnet *ifp)
    362 {
    363 	int indexlim = 0;
    364 
    365 	if (if_indexlim == 0) {
    366 		TAILQ_INIT(&ifnet);
    367 		if_indexlim = 8;
    368 	}
    369 	TAILQ_INIT(&ifp->if_addrlist);
    370 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
    371 	ifp->if_index = if_index;
    372 	if (ifindex2ifnet == 0)
    373 		if_index++;
    374 	else
    375 		while (ifp->if_index < if_indexlim &&
    376 		    ifindex2ifnet[ifp->if_index] != NULL) {
    377 			++if_index;
    378 			if (if_index == 0)
    379 				if_index = 1;
    380 			/*
    381 			 * If we hit USHRT_MAX, we skip back to 0 since
    382 			 * there are a number of places where the value
    383 			 * of if_index or if_index itself is compared
    384 			 * to or stored in an unsigned short.  By
    385 			 * jumping back, we won't botch those assignments
    386 			 * or comparisons.
    387 			 */
    388 			else if (if_index == USHRT_MAX) {
    389 				/*
    390 				 * However, if we have to jump back to
    391 				 * zero *twice* without finding an empty
    392 				 * slot in ifindex2ifnet[], then there
    393 				 * there are too many (>65535) interfaces.
    394 				 */
    395 				if (indexlim++)
    396 					panic("too many interfaces");
    397 				else
    398 					if_index = 1;
    399 			}
    400 			ifp->if_index = if_index;
    401 		}
    402 
    403 	/*
    404 	 * We have some arrays that should be indexed by if_index.
    405 	 * since if_index will grow dynamically, they should grow too.
    406 	 *	struct ifadd **ifnet_addrs
    407 	 *	struct ifnet **ifindex2ifnet
    408 	 */
    409 	if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
    410 	    ifp->if_index >= if_indexlim) {
    411 		size_t m, n, oldlim;
    412 		caddr_t q;
    413 
    414 		oldlim = if_indexlim;
    415 		while (ifp->if_index >= if_indexlim)
    416 			if_indexlim <<= 1;
    417 
    418 		/* grow ifnet_addrs */
    419 		m = oldlim * sizeof(struct ifaddr *);
    420 		n = if_indexlim * sizeof(struct ifaddr *);
    421 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    422 		memset(q, 0, n);
    423 		if (ifnet_addrs) {
    424 			bcopy((caddr_t)ifnet_addrs, q, m);
    425 			free((caddr_t)ifnet_addrs, M_IFADDR);
    426 		}
    427 		ifnet_addrs = (struct ifaddr **)q;
    428 
    429 		/* grow ifindex2ifnet */
    430 		m = oldlim * sizeof(struct ifnet *);
    431 		n = if_indexlim * sizeof(struct ifnet *);
    432 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    433 		memset(q, 0, n);
    434 		if (ifindex2ifnet) {
    435 			bcopy((caddr_t)ifindex2ifnet, q, m);
    436 			free((caddr_t)ifindex2ifnet, M_IFADDR);
    437 		}
    438 		ifindex2ifnet = (struct ifnet **)q;
    439 	}
    440 
    441 	ifindex2ifnet[ifp->if_index] = ifp;
    442 
    443 	/*
    444 	 * Link level name is allocated later by a separate call to
    445 	 * if_alloc_sadl().
    446 	 */
    447 
    448 	if (ifp->if_snd.ifq_maxlen == 0)
    449 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
    450 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    451 
    452 	ifp->if_link_state = LINK_STATE_UNKNOWN;
    453 
    454 	ifp->if_capenable = 0;
    455 	ifp->if_csum_flags_tx = 0;
    456 	ifp->if_csum_flags_rx = 0;
    457 
    458 #ifdef ALTQ
    459 	ifp->if_snd.altq_type = 0;
    460 	ifp->if_snd.altq_disc = NULL;
    461 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
    462 	ifp->if_snd.altq_tbr  = NULL;
    463 	ifp->if_snd.altq_ifp  = ifp;
    464 #endif
    465 
    466 #ifdef PFIL_HOOKS
    467 	ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
    468 	ifp->if_pfil.ph_ifnet = ifp;
    469 	if (pfil_head_register(&ifp->if_pfil) != 0)
    470 		printf("%s: WARNING: unable to register pfil hook\n",
    471 		    ifp->if_xname);
    472 	(void)pfil_run_hooks(&if_pfil,
    473 	    (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
    474 #endif
    475 
    476 	if (!STAILQ_EMPTY(&domains))
    477 		if_attachdomain1(ifp);
    478 
    479 	/* Announce the interface. */
    480 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
    481 }
    482 
    483 void
    484 if_attachdomain(void)
    485 {
    486 	struct ifnet *ifp;
    487 	int s;
    488 
    489 	s = splnet();
    490 	TAILQ_FOREACH(ifp, &ifnet, if_list)
    491 		if_attachdomain1(ifp);
    492 	splx(s);
    493 }
    494 
    495 void
    496 if_attachdomain1(struct ifnet *ifp)
    497 {
    498 	struct domain *dp;
    499 	int s;
    500 
    501 	s = splnet();
    502 
    503 	/* address family dependent data region */
    504 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
    505 	DOMAIN_FOREACH(dp) {
    506 		if (dp->dom_ifattach)
    507 			ifp->if_afdata[dp->dom_family] =
    508 			    (*dp->dom_ifattach)(ifp);
    509 	}
    510 
    511 	splx(s);
    512 }
    513 
    514 /*
    515  * Deactivate an interface.  This points all of the procedure
    516  * handles at error stubs.  May be called from interrupt context.
    517  */
    518 void
    519 if_deactivate(struct ifnet *ifp)
    520 {
    521 	int s;
    522 
    523 	s = splnet();
    524 
    525 	ifp->if_output	 = if_nulloutput;
    526 	ifp->if_input	 = if_nullinput;
    527 	ifp->if_start	 = if_nullstart;
    528 	ifp->if_ioctl	 = if_nullioctl;
    529 	ifp->if_init	 = if_nullinit;
    530 	ifp->if_stop	 = if_nullstop;
    531 	ifp->if_watchdog = if_nullwatchdog;
    532 	ifp->if_drain	 = if_nulldrain;
    533 
    534 	/* No more packets may be enqueued. */
    535 	ifp->if_snd.ifq_maxlen = 0;
    536 
    537 	splx(s);
    538 }
    539 
    540 /*
    541  * Detach an interface from the list of "active" interfaces,
    542  * freeing any resources as we go along.
    543  *
    544  * NOTE: This routine must be called with a valid thread context,
    545  * as it may block.
    546  */
    547 void
    548 if_detach(struct ifnet *ifp)
    549 {
    550 	struct socket so;
    551 	struct ifaddr *ifa;
    552 #ifdef IFAREF_DEBUG
    553 	struct ifaddr *last_ifa = NULL;
    554 #endif
    555 	struct domain *dp;
    556 	const struct protosw *pr;
    557 	struct radix_node_head *rnh;
    558 	int s, i, family, purged;
    559 
    560 	/*
    561 	 * XXX It's kind of lame that we have to have the
    562 	 * XXX socket structure...
    563 	 */
    564 	memset(&so, 0, sizeof(so));
    565 
    566 	s = splnet();
    567 
    568 	/*
    569 	 * Do an if_down() to give protocols a chance to do something.
    570 	 */
    571 	if_down(ifp);
    572 
    573 #ifdef ALTQ
    574 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
    575 		altq_disable(&ifp->if_snd);
    576 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
    577 		altq_detach(&ifp->if_snd);
    578 #endif
    579 
    580 
    581 #if NCARP > 0
    582 	/* Remove the interface from any carp group it is a part of.  */
    583 	if (ifp->if_carp && ifp->if_type != IFT_CARP)
    584 		carp_ifdetach(ifp);
    585 #endif
    586 
    587 #ifdef PFIL_HOOKS
    588 	(void)pfil_run_hooks(&if_pfil,
    589 	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
    590 	(void)pfil_head_unregister(&ifp->if_pfil);
    591 #endif
    592 
    593 	/*
    594 	 * Rip all the addresses off the interface.  This should make
    595 	 * all of the routes go away.
    596 	 *
    597 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
    598 	 * from the list, including our "cursor", ifa.  For safety,
    599 	 * and to honor the TAILQ abstraction, I just restart the
    600 	 * loop after each removal.  Note that the loop will exit
    601 	 * when all of the remaining ifaddrs belong to the AF_LINK
    602 	 * family.  I am counting on the historical fact that at
    603 	 * least one pr_usrreq in each address domain removes at
    604 	 * least one ifaddr.
    605 	 */
    606 again:
    607 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    608 		family = ifa->ifa_addr->sa_family;
    609 #ifdef IFAREF_DEBUG
    610 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
    611 		    ifa, family, ifa->ifa_refcnt);
    612 		if (last_ifa != NULL && ifa == last_ifa)
    613 			panic("if_detach: loop detected");
    614 		last_ifa = ifa;
    615 #endif
    616 		if (family == AF_LINK)
    617 			continue;
    618 		dp = pffinddomain(family);
    619 #ifdef DIAGNOSTIC
    620 		if (dp == NULL)
    621 			panic("if_detach: no domain for AF %d",
    622 			    family);
    623 #endif
    624 		/*
    625 		 * XXX These PURGEIF calls are redundant with the
    626 		 * purge-all-families calls below, but are left in for
    627 		 * now both to make a smaller change, and to avoid
    628 		 * unplanned interactions with clearing of
    629 		 * ifp->if_addrlist.
    630 		 */
    631 		purged = 0;
    632 		for (pr = dp->dom_protosw;
    633 		     pr < dp->dom_protoswNPROTOSW; pr++) {
    634 			so.so_proto = pr;
    635 			if (pr->pr_usrreq != NULL) {
    636 				(void) (*pr->pr_usrreq)(&so,
    637 				    PRU_PURGEIF, NULL, NULL,
    638 				    (struct mbuf *) ifp, curlwp);
    639 				purged = 1;
    640 			}
    641 		}
    642 		if (purged == 0) {
    643 			/*
    644 			 * XXX What's really the best thing to do
    645 			 * XXX here?  --thorpej (at) NetBSD.org
    646 			 */
    647 			printf("if_detach: WARNING: AF %d not purged\n",
    648 			    family);
    649 			TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
    650 		}
    651 		goto again;
    652 	}
    653 
    654 	if_free_sadl(ifp);
    655 
    656 	/* Walk the routing table looking for straglers. */
    657 	for (i = 0; i <= AF_MAX; i++) {
    658 		if ((rnh = rt_tables[i]) != NULL)
    659 			(void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
    660 	}
    661 
    662 	DOMAIN_FOREACH(dp) {
    663 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
    664 			(*dp->dom_ifdetach)(ifp,
    665 			    ifp->if_afdata[dp->dom_family]);
    666 
    667 		/*
    668 		 * One would expect multicast memberships (INET and
    669 		 * INET6) on UDP sockets to be purged by the PURGEIF
    670 		 * calls above, but if all addresses were removed from
    671 		 * the interface prior to destruction, the calls will
    672 		 * not be made (e.g. ppp, for which pppd(8) generally
    673 		 * removes addresses before destroying the interface).
    674 		 * Because there is no invariant that multicast
    675 		 * memberships only exist for interfaces with IPv4
    676 		 * addresses, we must call PURGEIF regardless of
    677 		 * addresses.  (Protocols which might store ifnet
    678 		 * pointers are marked with PR_PURGEIF.)
    679 		 */
    680 		for (pr = dp->dom_protosw;
    681 		     pr < dp->dom_protoswNPROTOSW; pr++) {
    682 			so.so_proto = pr;
    683 			if (pr->pr_usrreq != NULL &&
    684 			    pr->pr_flags & PR_PURGEIF)
    685 				(void) (*pr->pr_usrreq)(&so,
    686 				    PRU_PURGEIF, NULL, NULL,
    687 				    (struct mbuf *) ifp, curlwp);
    688 		}
    689 	}
    690 
    691 	/* Announce that the interface is gone. */
    692 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
    693 
    694 	ifindex2ifnet[ifp->if_index] = NULL;
    695 
    696 	TAILQ_REMOVE(&ifnet, ifp, if_list);
    697 
    698 	/*
    699 	 * remove packets that came from ifp, from software interrupt queues.
    700 	 */
    701 	DOMAIN_FOREACH(dp) {
    702 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
    703 			if (dp->dom_ifqueues[i] == NULL)
    704 				break;
    705 			if_detach_queues(ifp, dp->dom_ifqueues[i]);
    706 		}
    707 	}
    708 
    709 	splx(s);
    710 }
    711 
    712 static void
    713 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
    714 {
    715 	struct mbuf *m, *prev, *next;
    716 
    717 	prev = NULL;
    718 	for (m = q->ifq_head; m; m = next) {
    719 		next = m->m_nextpkt;
    720 #ifdef DIAGNOSTIC
    721 		if ((m->m_flags & M_PKTHDR) == 0) {
    722 			prev = m;
    723 			continue;
    724 		}
    725 #endif
    726 		if (m->m_pkthdr.rcvif != ifp) {
    727 			prev = m;
    728 			continue;
    729 		}
    730 
    731 		if (prev)
    732 			prev->m_nextpkt = m->m_nextpkt;
    733 		else
    734 			q->ifq_head = m->m_nextpkt;
    735 		if (q->ifq_tail == m)
    736 			q->ifq_tail = prev;
    737 		q->ifq_len--;
    738 
    739 		m->m_nextpkt = NULL;
    740 		m_freem(m);
    741 		IF_DROP(q);
    742 	}
    743 }
    744 
    745 /*
    746  * Callback for a radix tree walk to delete all references to an
    747  * ifnet.
    748  */
    749 static int
    750 if_rt_walktree(struct radix_node *rn, void *v)
    751 {
    752 	struct ifnet *ifp = (struct ifnet *)v;
    753 	struct rtentry *rt = (struct rtentry *)rn;
    754 	int error;
    755 
    756 	if (rt->rt_ifp == ifp) {
    757 		/* Delete the entry. */
    758 		++rt->rt_refcnt;
    759 		error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
    760 		    rt_mask(rt), rt->rt_flags, NULL);
    761 		KASSERT((rt->rt_flags & RTF_UP) == 0);
    762 		rt->rt_ifp = NULL;
    763 		RTFREE(rt);
    764 		if (error)
    765 			printf("%s: warning: unable to delete rtentry @ %p, "
    766 			    "error = %d\n", ifp->if_xname, rt, error);
    767 	}
    768 	return (0);
    769 }
    770 
    771 /*
    772  * Create a clone network interface.
    773  */
    774 int
    775 if_clone_create(const char *name)
    776 {
    777 	struct if_clone *ifc;
    778 	int unit;
    779 
    780 	ifc = if_clone_lookup(name, &unit);
    781 	if (ifc == NULL)
    782 		return (EINVAL);
    783 
    784 	if (ifunit(name) != NULL)
    785 		return (EEXIST);
    786 
    787 	return ((*ifc->ifc_create)(ifc, unit));
    788 }
    789 
    790 /*
    791  * Destroy a clone network interface.
    792  */
    793 int
    794 if_clone_destroy(const char *name)
    795 {
    796 	struct if_clone *ifc;
    797 	struct ifnet *ifp;
    798 
    799 	ifc = if_clone_lookup(name, NULL);
    800 	if (ifc == NULL)
    801 		return (EINVAL);
    802 
    803 	ifp = ifunit(name);
    804 	if (ifp == NULL)
    805 		return (ENXIO);
    806 
    807 	if (ifc->ifc_destroy == NULL)
    808 		return (EOPNOTSUPP);
    809 
    810 	return ((*ifc->ifc_destroy)(ifp));
    811 }
    812 
    813 /*
    814  * Look up a network interface cloner.
    815  */
    816 static struct if_clone *
    817 if_clone_lookup(const char *name, int *unitp)
    818 {
    819 	struct if_clone *ifc;
    820 	const char *cp;
    821 	int unit;
    822 
    823 	/* separate interface name from unit */
    824 	for (cp = name;
    825 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
    826 	    cp++)
    827 		continue;
    828 
    829 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
    830 		return (NULL);	/* No name or unit number */
    831 
    832 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
    833 		if (strlen(ifc->ifc_name) == cp - name &&
    834 		    !strncmp(name, ifc->ifc_name, cp - name))
    835 			break;
    836 	}
    837 
    838 	if (ifc == NULL)
    839 		return (NULL);
    840 
    841 	unit = 0;
    842 	while (cp - name < IFNAMSIZ && *cp) {
    843 		if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
    844 			/* Bogus unit number. */
    845 			return (NULL);
    846 		}
    847 		unit = (unit * 10) + (*cp++ - '0');
    848 	}
    849 
    850 	if (unitp != NULL)
    851 		*unitp = unit;
    852 	return (ifc);
    853 }
    854 
    855 /*
    856  * Register a network interface cloner.
    857  */
    858 void
    859 if_clone_attach(struct if_clone *ifc)
    860 {
    861 
    862 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
    863 	if_cloners_count++;
    864 }
    865 
    866 /*
    867  * Unregister a network interface cloner.
    868  */
    869 void
    870 if_clone_detach(struct if_clone *ifc)
    871 {
    872 
    873 	LIST_REMOVE(ifc, ifc_list);
    874 	if_cloners_count--;
    875 }
    876 
    877 /*
    878  * Provide list of interface cloners to userspace.
    879  */
    880 static int
    881 if_clone_list(struct if_clonereq *ifcr)
    882 {
    883 	char outbuf[IFNAMSIZ], *dst;
    884 	struct if_clone *ifc;
    885 	int count, error = 0;
    886 
    887 	ifcr->ifcr_total = if_cloners_count;
    888 	if ((dst = ifcr->ifcr_buffer) == NULL) {
    889 		/* Just asking how many there are. */
    890 		return (0);
    891 	}
    892 
    893 	if (ifcr->ifcr_count < 0)
    894 		return (EINVAL);
    895 
    896 	count = (if_cloners_count < ifcr->ifcr_count) ?
    897 	    if_cloners_count : ifcr->ifcr_count;
    898 
    899 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
    900 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
    901 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
    902 		if (outbuf[sizeof(outbuf) - 1] != '\0')
    903 			return ENAMETOOLONG;
    904 		error = copyout(outbuf, dst, sizeof(outbuf));
    905 		if (error)
    906 			break;
    907 	}
    908 
    909 	return (error);
    910 }
    911 
    912 /*
    913  * Locate an interface based on a complete address.
    914  */
    915 /*ARGSUSED*/
    916 struct ifaddr *
    917 ifa_ifwithaddr(const struct sockaddr *addr)
    918 {
    919 	struct ifnet *ifp;
    920 	struct ifaddr *ifa;
    921 
    922 #define	equal(a1, a2) \
    923   (bcmp((a1), (a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
    924 
    925 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
    926 		if (ifp->if_output == if_nulloutput)
    927 			continue;
    928 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    929 			if (ifa->ifa_addr->sa_family != addr->sa_family)
    930 				continue;
    931 			if (equal(addr, ifa->ifa_addr))
    932 				return (ifa);
    933 			if ((ifp->if_flags & IFF_BROADCAST) &&
    934 			    ifa->ifa_broadaddr &&
    935 			    /* IP6 doesn't have broadcast */
    936 			    ifa->ifa_broadaddr->sa_len != 0 &&
    937 			    equal(ifa->ifa_broadaddr, addr))
    938 				return (ifa);
    939 		}
    940 	}
    941 	return (NULL);
    942 }
    943 
    944 /*
    945  * Locate the point to point interface with a given destination address.
    946  */
    947 /*ARGSUSED*/
    948 struct ifaddr *
    949 ifa_ifwithdstaddr(const struct sockaddr *addr)
    950 {
    951 	struct ifnet *ifp;
    952 	struct ifaddr *ifa;
    953 
    954 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
    955 		if (ifp->if_output == if_nulloutput)
    956 			continue;
    957 		if (ifp->if_flags & IFF_POINTOPOINT) {
    958 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    959 				if (ifa->ifa_addr->sa_family !=
    960 				      addr->sa_family ||
    961 				    ifa->ifa_dstaddr == NULL)
    962 					continue;
    963 				if (equal(addr, ifa->ifa_dstaddr))
    964 					return (ifa);
    965 			}
    966 		}
    967 	}
    968 	return (NULL);
    969 }
    970 
    971 /*
    972  * Find an interface on a specific network.  If many, choice
    973  * is most specific found.
    974  */
    975 struct ifaddr *
    976 ifa_ifwithnet(const struct sockaddr *addr)
    977 {
    978 	struct ifnet *ifp;
    979 	struct ifaddr *ifa;
    980 	const struct sockaddr_dl *sdl;
    981 	struct ifaddr *ifa_maybe = 0;
    982 	u_int af = addr->sa_family;
    983 	const char *addr_data = addr->sa_data, *cplim;
    984 
    985 	if (af == AF_LINK) {
    986 		sdl = (const struct sockaddr_dl *)addr;
    987 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
    988 		    ifindex2ifnet[sdl->sdl_index] &&
    989 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
    990 			return (ifnet_addrs[sdl->sdl_index]);
    991 	}
    992 #ifdef NETATALK
    993 	if (af == AF_APPLETALK) {
    994 		const struct sockaddr_at *sat, *sat2;
    995 		sat = (const struct sockaddr_at *)addr;
    996 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
    997 			if (ifp->if_output == if_nulloutput)
    998 				continue;
    999 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
   1000 			if (ifa == NULL)
   1001 				continue;
   1002 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
   1003 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
   1004 				return (ifa); /* exact match */
   1005 			if (ifa_maybe == NULL) {
   1006 				/* else keep the if with the right range */
   1007 				ifa_maybe = ifa;
   1008 			}
   1009 		}
   1010 		return (ifa_maybe);
   1011 	}
   1012 #endif
   1013 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
   1014 		if (ifp->if_output == if_nulloutput)
   1015 			continue;
   1016 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
   1017 			const char *cp, *cp2, *cp3;
   1018 
   1019 			if (ifa->ifa_addr->sa_family != af ||
   1020 			    ifa->ifa_netmask == 0)
   1021  next:				continue;
   1022 			cp = addr_data;
   1023 			cp2 = ifa->ifa_addr->sa_data;
   1024 			cp3 = ifa->ifa_netmask->sa_data;
   1025 			cplim = (const char *)ifa->ifa_netmask +
   1026 			    ifa->ifa_netmask->sa_len;
   1027 			while (cp3 < cplim) {
   1028 				if ((*cp++ ^ *cp2++) & *cp3++) {
   1029 					/* want to continue for() loop */
   1030 					goto next;
   1031 				}
   1032 			}
   1033 			if (ifa_maybe == 0 ||
   1034 			    rn_refines((caddr_t)ifa->ifa_netmask,
   1035 			    (caddr_t)ifa_maybe->ifa_netmask))
   1036 				ifa_maybe = ifa;
   1037 		}
   1038 	}
   1039 	return (ifa_maybe);
   1040 }
   1041 
   1042 /*
   1043  * Find the interface of the addresss.
   1044  */
   1045 struct ifaddr *
   1046 ifa_ifwithladdr(const struct sockaddr *addr)
   1047 {
   1048 	struct ifaddr *ia;
   1049 
   1050 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
   1051 	    (ia = ifa_ifwithnet(addr)))
   1052 		return (ia);
   1053 	return (NULL);
   1054 }
   1055 
   1056 /*
   1057  * Find an interface using a specific address family
   1058  */
   1059 struct ifaddr *
   1060 ifa_ifwithaf(int af)
   1061 {
   1062 	struct ifnet *ifp;
   1063 	struct ifaddr *ifa;
   1064 
   1065 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
   1066 		if (ifp->if_output == if_nulloutput)
   1067 			continue;
   1068 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
   1069 			if (ifa->ifa_addr->sa_family == af)
   1070 				return ifa;
   1071 		}
   1072 	}
   1073 	return NULL;
   1074 }
   1075 
   1076 /*
   1077  * Find an interface address specific to an interface best matching
   1078  * a given address.
   1079  */
   1080 struct ifaddr *
   1081 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
   1082 {
   1083 	struct ifaddr *ifa;
   1084 	const char *cp, *cp2, *cp3;
   1085 	const char *cplim;
   1086 	struct ifaddr *ifa_maybe = 0;
   1087 	u_int af = addr->sa_family;
   1088 
   1089 	if (ifp->if_output == if_nulloutput)
   1090 		return (NULL);
   1091 
   1092 	if (af >= AF_MAX)
   1093 		return (NULL);
   1094 
   1095 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
   1096 		if (ifa->ifa_addr->sa_family != af)
   1097 			continue;
   1098 		ifa_maybe = ifa;
   1099 		if (ifa->ifa_netmask == 0) {
   1100 			if (equal(addr, ifa->ifa_addr) ||
   1101 			    (ifa->ifa_dstaddr &&
   1102 			     equal(addr, ifa->ifa_dstaddr)))
   1103 				return (ifa);
   1104 			continue;
   1105 		}
   1106 		cp = addr->sa_data;
   1107 		cp2 = ifa->ifa_addr->sa_data;
   1108 		cp3 = ifa->ifa_netmask->sa_data;
   1109 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
   1110 		for (; cp3 < cplim; cp3++) {
   1111 			if ((*cp++ ^ *cp2++) & *cp3)
   1112 				break;
   1113 		}
   1114 		if (cp3 == cplim)
   1115 			return (ifa);
   1116 	}
   1117 	return (ifa_maybe);
   1118 }
   1119 
   1120 /*
   1121  * Default action when installing a route with a Link Level gateway.
   1122  * Lookup an appropriate real ifa to point to.
   1123  * This should be moved to /sys/net/link.c eventually.
   1124  */
   1125 void
   1126 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
   1127 {
   1128 	struct ifaddr *ifa;
   1129 	struct sockaddr *dst;
   1130 	struct ifnet *ifp;
   1131 
   1132 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
   1133 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
   1134 		return;
   1135 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
   1136 		rt_replace_ifa(rt, ifa);
   1137 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
   1138 			ifa->ifa_rtrequest(cmd, rt, info);
   1139 	}
   1140 }
   1141 
   1142 /*
   1143  * Handle a change in the interface link state.
   1144  */
   1145 void
   1146 if_link_state_change(struct ifnet *ifp, int link_state)
   1147 {
   1148 	/* Notify that the link state has changed. */
   1149 	if (ifp->if_link_state != link_state) {
   1150 		ifp->if_link_state = link_state;
   1151 		rt_ifmsg(ifp);
   1152 #if NCARP > 0
   1153 		if (ifp->if_carp)
   1154 			carp_carpdev_state(ifp);
   1155 #endif
   1156 	}
   1157 }
   1158 
   1159 /*
   1160  * Mark an interface down and notify protocols of
   1161  * the transition.
   1162  * NOTE: must be called at splsoftnet or equivalent.
   1163  */
   1164 void
   1165 if_down(struct ifnet *ifp)
   1166 {
   1167 	struct ifaddr *ifa;
   1168 
   1169 	ifp->if_flags &= ~IFF_UP;
   1170 	microtime(&ifp->if_lastchange);
   1171 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
   1172 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
   1173 	IFQ_PURGE(&ifp->if_snd);
   1174 #if NCARP > 0
   1175 	if (ifp->if_carp)
   1176 		carp_carpdev_state(ifp);
   1177 #endif
   1178 	rt_ifmsg(ifp);
   1179 }
   1180 
   1181 /*
   1182  * Mark an interface up and notify protocols of
   1183  * the transition.
   1184  * NOTE: must be called at splsoftnet or equivalent.
   1185  */
   1186 void
   1187 if_up(struct ifnet *ifp)
   1188 {
   1189 #ifdef notyet
   1190 	struct ifaddr *ifa;
   1191 #endif
   1192 
   1193 	ifp->if_flags |= IFF_UP;
   1194 	microtime(&ifp->if_lastchange);
   1195 #ifdef notyet
   1196 	/* this has no effect on IP, and will kill all ISO connections XXX */
   1197 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
   1198 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
   1199 #endif
   1200 #if NCARP > 0
   1201 	if (ifp->if_carp)
   1202 		carp_carpdev_state(ifp);
   1203 #endif
   1204 	rt_ifmsg(ifp);
   1205 #ifdef INET6
   1206 	in6_if_up(ifp);
   1207 #endif
   1208 }
   1209 
   1210 /*
   1211  * Handle interface watchdog timer routines.  Called
   1212  * from softclock, we decrement timers (if set) and
   1213  * call the appropriate interface routine on expiration.
   1214  */
   1215 void
   1216 if_slowtimo(void *arg)
   1217 {
   1218 	struct ifnet *ifp;
   1219 	int s = splnet();
   1220 
   1221 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
   1222 		if (ifp->if_timer == 0 || --ifp->if_timer)
   1223 			continue;
   1224 		if (ifp->if_watchdog)
   1225 			(*ifp->if_watchdog)(ifp);
   1226 	}
   1227 	splx(s);
   1228 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
   1229 	    if_slowtimo, NULL);
   1230 }
   1231 
   1232 /*
   1233  * Set/clear promiscuous mode on interface ifp based on the truth value
   1234  * of pswitch.  The calls are reference counted so that only the first
   1235  * "on" request actually has an effect, as does the final "off" request.
   1236  * Results are undefined if the "off" and "on" requests are not matched.
   1237  */
   1238 int
   1239 ifpromisc(struct ifnet *ifp, int pswitch)
   1240 {
   1241 	int pcount, ret;
   1242 	short flags;
   1243 	struct ifreq ifr;
   1244 
   1245 	pcount = ifp->if_pcount;
   1246 	flags = ifp->if_flags;
   1247 	if (pswitch) {
   1248 		/*
   1249 		 * Allow the device to be "placed" into promiscuous
   1250 		 * mode even if it is not configured up.  It will
   1251 		 * consult IFF_PROMISC when it is is brought up.
   1252 		 */
   1253 		if (ifp->if_pcount++ != 0)
   1254 			return (0);
   1255 		ifp->if_flags |= IFF_PROMISC;
   1256 		if ((ifp->if_flags & IFF_UP) == 0)
   1257 			return (0);
   1258 	} else {
   1259 		if (--ifp->if_pcount > 0)
   1260 			return (0);
   1261 		ifp->if_flags &= ~IFF_PROMISC;
   1262 		/*
   1263 		 * If the device is not configured up, we should not need to
   1264 		 * turn off promiscuous mode (device should have turned it
   1265 		 * off when interface went down; and will look at IFF_PROMISC
   1266 		 * again next time interface comes up).
   1267 		 */
   1268 		if ((ifp->if_flags & IFF_UP) == 0)
   1269 			return (0);
   1270 	}
   1271 	memset(&ifr, 0, sizeof(ifr));
   1272 	ifr.ifr_flags = ifp->if_flags;
   1273 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
   1274 	/* Restore interface state if not successful. */
   1275 	if (ret != 0) {
   1276 		ifp->if_pcount = pcount;
   1277 		ifp->if_flags = flags;
   1278 	}
   1279 	return (ret);
   1280 }
   1281 
   1282 /*
   1283  * Map interface name to
   1284  * interface structure pointer.
   1285  */
   1286 struct ifnet *
   1287 ifunit(const char *name)
   1288 {
   1289 	struct ifnet *ifp;
   1290 	const char *cp = name;
   1291 	u_int unit = 0;
   1292 	u_int i;
   1293 
   1294 	/*
   1295 	 * If the entire name is a number, treat it as an ifindex.
   1296 	 */
   1297 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   1298 		unit = unit * 10 + (*cp - '0');
   1299 	}
   1300 
   1301 	/*
   1302 	 * If the number took all of the name, then it's a valid ifindex.
   1303 	 */
   1304 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
   1305 		if (unit >= if_indexlim)
   1306 			return (NULL);
   1307 		ifp = ifindex2ifnet[unit];
   1308 		if (ifp == NULL || ifp->if_output == if_nulloutput)
   1309 			return (NULL);
   1310 		return (ifp);
   1311 	}
   1312 
   1313 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
   1314 		if (ifp->if_output == if_nulloutput)
   1315 			continue;
   1316 	 	if (strcmp(ifp->if_xname, name) == 0)
   1317 			return (ifp);
   1318 	}
   1319 	return (NULL);
   1320 }
   1321 
   1322 /*
   1323  * Interface ioctls.
   1324  */
   1325 int
   1326 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct lwp *l)
   1327 {
   1328 	struct ifnet *ifp;
   1329 	struct ifreq *ifr;
   1330 	struct ifcapreq *ifcr;
   1331 	struct ifdatareq *ifdr;
   1332 	int s, error = 0;
   1333 	short oif_flags;
   1334 
   1335 	switch (cmd) {
   1336 
   1337 	case SIOCGIFCONF:
   1338 	case OSIOCGIFCONF:
   1339 		return (ifconf(cmd, data));
   1340 	}
   1341 	ifr = (struct ifreq *)data;
   1342 	ifcr = (struct ifcapreq *)data;
   1343 	ifdr = (struct ifdatareq *)data;
   1344 
   1345 	ifp = ifunit(ifr->ifr_name);
   1346 
   1347 	switch (cmd) {
   1348 	case SIOCIFCREATE:
   1349 	case SIOCIFDESTROY:
   1350 		if (l) {
   1351 			error = kauth_authorize_network(l->l_cred,
   1352 			    KAUTH_NETWORK_INTERFACE,
   1353 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   1354 			    (void *)cmd, NULL);
   1355 			if (error)
   1356 				return error;
   1357 		}
   1358 		return ((cmd == SIOCIFCREATE) ?
   1359 			if_clone_create(ifr->ifr_name) :
   1360 			if_clone_destroy(ifr->ifr_name));
   1361 
   1362 	case SIOCIFGCLONERS:
   1363 		return (if_clone_list((struct if_clonereq *)data));
   1364 	}
   1365 
   1366 	if (ifp == 0)
   1367 		return (ENXIO);
   1368 
   1369 	switch (cmd) {
   1370 	case SIOCSIFFLAGS:
   1371 	case SIOCSIFCAP:
   1372 	case SIOCSIFMETRIC:
   1373 	case SIOCZIFDATA:
   1374 	case SIOCSIFMTU:
   1375 	case SIOCSIFPHYADDR:
   1376 	case SIOCDIFPHYADDR:
   1377 #ifdef INET6
   1378 	case SIOCSIFPHYADDR_IN6:
   1379 #endif
   1380 	case SIOCSLIFPHYADDR:
   1381 	case SIOCADDMULTI:
   1382 	case SIOCDELMULTI:
   1383 	case SIOCSIFMEDIA:
   1384 	case SIOCSDRVSPEC:
   1385 	case SIOCS80211NWID:
   1386 	case SIOCS80211NWKEY:
   1387 	case SIOCS80211POWER:
   1388 	case SIOCS80211BSSID:
   1389 	case SIOCS80211CHANNEL:
   1390 		if (l) {
   1391 			error = kauth_authorize_network(l->l_cred,
   1392 			    KAUTH_NETWORK_INTERFACE,
   1393 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   1394 			    (void *)cmd, NULL);
   1395 			if (error)
   1396 				return error;
   1397 		}
   1398 	}
   1399 
   1400 	oif_flags = ifp->if_flags;
   1401 	switch (cmd) {
   1402 
   1403 	case SIOCGIFFLAGS:
   1404 		ifr->ifr_flags = ifp->if_flags;
   1405 		break;
   1406 
   1407 	case SIOCGIFMETRIC:
   1408 		ifr->ifr_metric = ifp->if_metric;
   1409 		break;
   1410 
   1411 	case SIOCGIFMTU:
   1412 		ifr->ifr_mtu = ifp->if_mtu;
   1413 		break;
   1414 
   1415 	case SIOCGIFDLT:
   1416 		ifr->ifr_dlt = ifp->if_dlt;
   1417 		break;
   1418 
   1419 	case SIOCSIFFLAGS:
   1420 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
   1421 			s = splnet();
   1422 			if_down(ifp);
   1423 			splx(s);
   1424 		}
   1425 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
   1426 			s = splnet();
   1427 			if_up(ifp);
   1428 			splx(s);
   1429 		}
   1430 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
   1431 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
   1432 		if (ifp->if_ioctl)
   1433 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
   1434 		break;
   1435 
   1436 	case SIOCGIFCAP:
   1437 		ifcr->ifcr_capabilities = ifp->if_capabilities;
   1438 		ifcr->ifcr_capenable = ifp->if_capenable;
   1439 		break;
   1440 
   1441 	case SIOCSIFCAP:
   1442 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
   1443 			return (EINVAL);
   1444 		if (ifp->if_ioctl == NULL)
   1445 			return (EOPNOTSUPP);
   1446 
   1447 		/* Must prevent race with packet reception here. */
   1448 		s = splnet();
   1449 		if (ifcr->ifcr_capenable != ifp->if_capenable) {
   1450 			struct ifreq ifrq;
   1451 
   1452 			ifrq.ifr_flags = ifp->if_flags;
   1453 			ifp->if_capenable = ifcr->ifcr_capenable;
   1454 
   1455 			/* Pre-compute the checksum flags mask. */
   1456 			ifp->if_csum_flags_tx = 0;
   1457 			ifp->if_csum_flags_rx = 0;
   1458 			if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
   1459 				ifp->if_csum_flags_tx |= M_CSUM_IPv4;
   1460 			}
   1461 			if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
   1462 				ifp->if_csum_flags_rx |= M_CSUM_IPv4;
   1463 			}
   1464 
   1465 			if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
   1466 				ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
   1467 			}
   1468 			if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
   1469 				ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
   1470 			}
   1471 
   1472 			if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
   1473 				ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
   1474 			}
   1475 			if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
   1476 				ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
   1477 			}
   1478 
   1479 			if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
   1480 				ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
   1481 			}
   1482 			if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
   1483 				ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
   1484 			}
   1485 
   1486 			if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
   1487 				ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
   1488 			}
   1489 			if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
   1490 				ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
   1491 			}
   1492 
   1493 			/*
   1494 			 * Only kick the interface if it's up.  If it's
   1495 			 * not up now, it will notice the cap enables
   1496 			 * when it is brought up later.
   1497 			 */
   1498 			if (ifp->if_flags & IFF_UP)
   1499 				(void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
   1500 				    (caddr_t) &ifrq);
   1501 		}
   1502 		splx(s);
   1503 		break;
   1504 
   1505 	case SIOCSIFMETRIC:
   1506 		ifp->if_metric = ifr->ifr_metric;
   1507 		break;
   1508 
   1509 	case SIOCGIFDATA:
   1510 		ifdr->ifdr_data = ifp->if_data;
   1511 		break;
   1512 
   1513 	case SIOCZIFDATA:
   1514 		ifdr->ifdr_data = ifp->if_data;
   1515 		/*
   1516 		 * Assumes that the volatile counters that can be
   1517 		 * zero'ed are at the end of if_data.
   1518 		 */
   1519 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
   1520 		    offsetof(struct if_data, ifi_ipackets));
   1521 		break;
   1522 
   1523 	case SIOCSIFMTU:
   1524 	{
   1525 		u_long oldmtu = ifp->if_mtu;
   1526 
   1527 		if (ifp->if_ioctl == NULL)
   1528 			return (EOPNOTSUPP);
   1529 		error = (*ifp->if_ioctl)(ifp, cmd, data);
   1530 
   1531 		/*
   1532 		 * If the link MTU changed, do network layer specific procedure.
   1533 		 */
   1534 		if (ifp->if_mtu != oldmtu) {
   1535 #ifdef INET6
   1536 			nd6_setmtu(ifp);
   1537 #endif
   1538 		}
   1539 		break;
   1540 	}
   1541 	case SIOCSIFPHYADDR:
   1542 	case SIOCDIFPHYADDR:
   1543 #ifdef INET6
   1544 	case SIOCSIFPHYADDR_IN6:
   1545 #endif
   1546 	case SIOCSLIFPHYADDR:
   1547 	case SIOCADDMULTI:
   1548 	case SIOCDELMULTI:
   1549 	case SIOCSIFMEDIA:
   1550 	case SIOCGIFPSRCADDR:
   1551 	case SIOCGIFPDSTADDR:
   1552 	case SIOCGLIFPHYADDR:
   1553 	case SIOCGIFMEDIA:
   1554 		if (ifp->if_ioctl == 0)
   1555 			return (EOPNOTSUPP);
   1556 		error = (*ifp->if_ioctl)(ifp, cmd, data);
   1557 		break;
   1558 
   1559 	case SIOCSDRVSPEC:
   1560 	case SIOCS80211NWID:
   1561 	case SIOCS80211NWKEY:
   1562 	case SIOCS80211POWER:
   1563 	case SIOCS80211BSSID:
   1564 	case SIOCS80211CHANNEL:
   1565 	default:
   1566 		if (so->so_proto == 0)
   1567 			return (EOPNOTSUPP);
   1568 #ifdef COMPAT_OSOCK
   1569 		error = compat_ifioctl(so, cmd, data, l);
   1570 #else
   1571 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
   1572 		    (struct mbuf *)cmd, (struct mbuf *)data,
   1573 		    (struct mbuf *)ifp, l));
   1574 #endif
   1575 		break;
   1576 	}
   1577 
   1578 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   1579 #ifdef INET6
   1580 		if ((ifp->if_flags & IFF_UP) != 0) {
   1581 			s = splnet();
   1582 			in6_if_up(ifp);
   1583 			splx(s);
   1584 		}
   1585 #endif
   1586 	}
   1587 
   1588 	return (error);
   1589 }
   1590 
   1591 /*
   1592  * Return interface configuration
   1593  * of system.  List may be used
   1594  * in later ioctl's (above) to get
   1595  * other information.
   1596  */
   1597 /*ARGSUSED*/
   1598 int
   1599 ifconf(u_long cmd, caddr_t data)
   1600 {
   1601 	struct ifconf *ifc = (struct ifconf *)data;
   1602 	struct ifnet *ifp;
   1603 	struct ifaddr *ifa;
   1604 	struct ifreq ifr, *ifrp;
   1605 	int space = ifc->ifc_len, error = 0;
   1606 	const int sz = (int)sizeof(ifr);
   1607 	int sign;
   1608 
   1609 	if ((ifrp = ifc->ifc_req) == NULL) {
   1610 		space = 0;
   1611 		sign = -1;
   1612 	} else {
   1613 		sign = 1;
   1614 	}
   1615 	IFNET_FOREACH(ifp) {
   1616 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
   1617 		    sizeof(ifr.ifr_name));
   1618 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
   1619 			return ENAMETOOLONG;
   1620 		if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == NULL) {
   1621 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1622 			if (ifrp != NULL && space >= sz) {
   1623 				error = copyout(&ifr, ifrp, sz);
   1624 				if (error)
   1625 					break;
   1626 				ifrp++;
   1627 			}
   1628 			space -= sizeof(ifr) * sign;
   1629 			continue;
   1630 		}
   1631 
   1632 		for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
   1633 			struct sockaddr *sa = ifa->ifa_addr;
   1634 #ifdef COMPAT_OSOCK
   1635 			if (cmd == OSIOCGIFCONF) {
   1636 				struct osockaddr *osa =
   1637 					 (struct osockaddr *)&ifr.ifr_addr;
   1638 				/*
   1639 				 * If it does not fit, we don't bother with it
   1640 				 */
   1641 				if (sa->sa_len > sizeof(*osa))
   1642 					continue;
   1643 				ifr.ifr_addr = *sa;
   1644 				osa->sa_family = sa->sa_family;
   1645 				if (ifrp != NULL && space >= sz) {
   1646 					error = copyout(&ifr, ifrp, sz);
   1647 					ifrp++;
   1648 				}
   1649 			} else
   1650 #endif
   1651 			if (sa->sa_len <= sizeof(*sa)) {
   1652 				ifr.ifr_addr = *sa;
   1653 				if (ifrp != NULL && space >= sz) {
   1654 					error = copyout(&ifr, ifrp, sz);
   1655 					ifrp++;
   1656 				}
   1657 			} else {
   1658 				space -= (sa->sa_len - sizeof(*sa)) * sign;
   1659 				if (ifrp != NULL && space >= sz) {
   1660 					error = copyout(&ifr, ifrp,
   1661 					    sizeof(ifr.ifr_name));
   1662 					if (error == 0) {
   1663 						error = copyout(sa,
   1664 						    &ifrp->ifr_addr,
   1665 						    sa->sa_len);
   1666 					}
   1667 					ifrp = (struct ifreq *)
   1668 						(sa->sa_len +
   1669 						 (caddr_t)&ifrp->ifr_addr);
   1670 				}
   1671 			}
   1672 			if (error)
   1673 				break;
   1674 			space -= sz * sign;
   1675 		}
   1676 	}
   1677 	if (ifrp != NULL)
   1678 		ifc->ifc_len -= space;
   1679 	else
   1680 		ifc->ifc_len = space;
   1681 	return (error);
   1682 }
   1683 
   1684 /*
   1685  * Queue message on interface, and start output if interface
   1686  * not yet active.
   1687  */
   1688 int
   1689 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
   1690     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
   1691 {
   1692 	int len = m->m_pkthdr.len;
   1693 	int mflags = m->m_flags;
   1694 	int s = splnet();
   1695 	int error;
   1696 
   1697 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
   1698 	if (error) {
   1699 		splx(s);
   1700 		return error;
   1701 	}
   1702 	ifp->if_obytes += len;
   1703 	if (mflags & M_MCAST)
   1704 		ifp->if_omcasts++;
   1705 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
   1706 		(*ifp->if_start)(ifp);
   1707 	splx(s);
   1708 	return error;
   1709 }
   1710 
   1711 /*
   1712  * Queue message on interface, possibly using a second fast queue
   1713  */
   1714 int
   1715 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
   1716     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
   1717 {
   1718 	int error = 0;
   1719 
   1720 	if (ifq != NULL
   1721 #ifdef ALTQ
   1722 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
   1723 #endif
   1724 	    ) {
   1725 		if (IF_QFULL(ifq)) {
   1726 			IF_DROP(&ifp->if_snd);
   1727 			m_freem(m);
   1728 			if (error == 0)
   1729 				error = ENOBUFS;
   1730 		}
   1731 		else
   1732 			IF_ENQUEUE(ifq, m);
   1733 	} else
   1734 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
   1735 	if (error != 0) {
   1736 		++ifp->if_oerrors;
   1737 		return error;
   1738 	}
   1739 
   1740 	return 0;
   1741 }
   1742 
   1743 
   1744 #if defined(INET) || defined(INET6)
   1745 static void
   1746 sysctl_net_ifq_setup(struct sysctllog **clog,
   1747 		     int pf, const char *pfname,
   1748 		     int ipn, const char *ipname,
   1749 		     int qid, struct ifqueue *ifq)
   1750 {
   1751 
   1752 	sysctl_createv(clog, 0, NULL, NULL,
   1753 		       CTLFLAG_PERMANENT,
   1754 		       CTLTYPE_NODE, "net", NULL,
   1755 		       NULL, 0, NULL, 0,
   1756 		       CTL_NET, CTL_EOL);
   1757 	sysctl_createv(clog, 0, NULL, NULL,
   1758 		       CTLFLAG_PERMANENT,
   1759 		       CTLTYPE_NODE, pfname, NULL,
   1760 		       NULL, 0, NULL, 0,
   1761 		       CTL_NET, pf, CTL_EOL);
   1762 	sysctl_createv(clog, 0, NULL, NULL,
   1763 		       CTLFLAG_PERMANENT,
   1764 		       CTLTYPE_NODE, ipname, NULL,
   1765 		       NULL, 0, NULL, 0,
   1766 		       CTL_NET, pf, ipn, CTL_EOL);
   1767 	sysctl_createv(clog, 0, NULL, NULL,
   1768 		       CTLFLAG_PERMANENT,
   1769 		       CTLTYPE_NODE, "ifq",
   1770 		       SYSCTL_DESCR("Protocol input queue controls"),
   1771 		       NULL, 0, NULL, 0,
   1772 		       CTL_NET, pf, ipn, qid, CTL_EOL);
   1773 
   1774 	sysctl_createv(clog, 0, NULL, NULL,
   1775 		       CTLFLAG_PERMANENT,
   1776 		       CTLTYPE_INT, "len",
   1777 		       SYSCTL_DESCR("Current input queue length"),
   1778 		       NULL, 0, &ifq->ifq_len, 0,
   1779 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
   1780 	sysctl_createv(clog, 0, NULL, NULL,
   1781 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1782 		       CTLTYPE_INT, "maxlen",
   1783 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   1784 		       NULL, 0, &ifq->ifq_maxlen, 0,
   1785 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
   1786 #ifdef notyet
   1787 	sysctl_createv(clog, 0, NULL, NULL,
   1788 		       CTLFLAG_PERMANENT,
   1789 		       CTLTYPE_INT, "peak",
   1790 		       SYSCTL_DESCR("Highest input queue length"),
   1791 		       NULL, 0, &ifq->ifq_peak, 0,
   1792 		       CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
   1793 #endif
   1794 	sysctl_createv(clog, 0, NULL, NULL,
   1795 		       CTLFLAG_PERMANENT,
   1796 		       CTLTYPE_INT, "drops",
   1797 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
   1798 		       NULL, 0, &ifq->ifq_drops, 0,
   1799 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
   1800 }
   1801 
   1802 #ifdef INET
   1803 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
   1804 	     "sysctl net.inet.ip.ifq subtree setup")
   1805 {
   1806 	extern struct ifqueue ipintrq;
   1807 
   1808 	sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
   1809 			     IPCTL_IFQ, &ipintrq);
   1810 }
   1811 #endif /* INET */
   1812 
   1813 #ifdef INET6
   1814 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
   1815 	     "sysctl net.inet6.ip6.ifq subtree setup")
   1816 {
   1817 	extern struct ifqueue ip6intrq;
   1818 
   1819 	sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
   1820 			     IPV6CTL_IFQ, &ip6intrq);
   1821 }
   1822 #endif /* INET6 */
   1823 #endif /* INET || INET6 */
   1824