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