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