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