Home | History | Annotate | Line # | Download | only in net
if.c revision 1.278
      1 /*	$NetBSD: if.c,v 1.278 2014/06/07 13:25:33 he Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2008 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the project nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 /*
     62  * Copyright (c) 1980, 1986, 1993
     63  *	The Regents of the University of California.  All rights reserved.
     64  *
     65  * Redistribution and use in source and binary forms, with or without
     66  * modification, are permitted provided that the following conditions
     67  * are met:
     68  * 1. Redistributions of source code must retain the above copyright
     69  *    notice, this list of conditions and the following disclaimer.
     70  * 2. Redistributions in binary form must reproduce the above copyright
     71  *    notice, this list of conditions and the following disclaimer in the
     72  *    documentation and/or other materials provided with the distribution.
     73  * 3. Neither the name of the University nor the names of its contributors
     74  *    may be used to endorse or promote products derived from this software
     75  *    without specific prior written permission.
     76  *
     77  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     78  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     79  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     80  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     81  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     82  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     83  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     85  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     86  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     87  * SUCH DAMAGE.
     88  *
     89  *	@(#)if.c	8.5 (Berkeley) 1/9/95
     90  */
     91 
     92 #include <sys/cdefs.h>
     93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.278 2014/06/07 13:25:33 he Exp $");
     94 
     95 #include "opt_inet.h"
     96 
     97 #include "opt_atalk.h"
     98 #include "opt_natm.h"
     99 
    100 #include <sys/param.h>
    101 #include <sys/mbuf.h>
    102 #include <sys/systm.h>
    103 #include <sys/callout.h>
    104 #include <sys/proc.h>
    105 #include <sys/socket.h>
    106 #include <sys/socketvar.h>
    107 #include <sys/domain.h>
    108 #include <sys/protosw.h>
    109 #include <sys/kernel.h>
    110 #include <sys/ioctl.h>
    111 #include <sys/sysctl.h>
    112 #include <sys/syslog.h>
    113 #include <sys/kauth.h>
    114 #include <sys/kmem.h>
    115 #include <sys/xcall.h>
    116 
    117 #include <net/if.h>
    118 #include <net/if_dl.h>
    119 #include <net/if_ether.h>
    120 #include <net/if_media.h>
    121 #include <net80211/ieee80211.h>
    122 #include <net80211/ieee80211_ioctl.h>
    123 #include <net/if_types.h>
    124 #include <net/radix.h>
    125 #include <net/route.h>
    126 #include <net/netisr.h>
    127 #include <sys/module.h>
    128 #ifdef NETATALK
    129 #include <netatalk/at_extern.h>
    130 #include <netatalk/at.h>
    131 #endif
    132 #include <net/pfil.h>
    133 #include <netinet/in.h>
    134 #include <netinet/in_var.h>
    135 
    136 #ifdef INET6
    137 #include <netinet6/in6_var.h>
    138 #include <netinet6/nd6.h>
    139 #endif
    140 
    141 #include "carp.h"
    142 #if NCARP > 0
    143 #include <netinet/ip_carp.h>
    144 #endif
    145 
    146 #include <compat/sys/sockio.h>
    147 #include <compat/sys/socket.h>
    148 
    149 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
    150 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
    151 
    152 /*
    153  * Global list of interfaces.
    154  */
    155 struct ifnet_head		ifnet_list;
    156 static ifnet_t **		ifindex2ifnet = NULL;
    157 
    158 static u_int			if_index = 1;
    159 static size_t			if_indexlim = 0;
    160 static uint64_t			index_gen;
    161 static kmutex_t			index_gen_mtx;
    162 
    163 static struct ifaddr **		ifnet_addrs = NULL;
    164 
    165 static callout_t		if_slowtimo_ch;
    166 
    167 struct ifnet *lo0ifp;
    168 int	ifqmaxlen = IFQ_MAXLEN;
    169 
    170 static int	if_rt_walktree(struct rtentry *, void *);
    171 
    172 static struct if_clone *if_clone_lookup(const char *, int *);
    173 static int	if_clone_list(struct if_clonereq *);
    174 
    175 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
    176 static int if_cloners_count;
    177 
    178 /* Packet filtering hook for interfaces. */
    179 pfil_head_t *	if_pfil;
    180 
    181 static kauth_listener_t if_listener;
    182 
    183 static int doifioctl(struct socket *, u_long, void *, struct lwp *);
    184 static int ifioctl_attach(struct ifnet *);
    185 static void ifioctl_detach(struct ifnet *);
    186 static void ifnet_lock_enter(struct ifnet_lock *);
    187 static void ifnet_lock_exit(struct ifnet_lock *);
    188 static void if_detach_queues(struct ifnet *, struct ifqueue *);
    189 static void sysctl_sndq_setup(struct sysctllog **, const char *,
    190     struct ifaltq *);
    191 
    192 #if defined(INET) || defined(INET6)
    193 static void sysctl_net_pktq_setup(struct sysctllog **, int);
    194 #endif
    195 
    196 static int
    197 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    198     void *arg0, void *arg1, void *arg2, void *arg3)
    199 {
    200 	int result;
    201 	enum kauth_network_req req;
    202 
    203 	result = KAUTH_RESULT_DEFER;
    204 	req = (enum kauth_network_req)arg1;
    205 
    206 	if (action != KAUTH_NETWORK_INTERFACE)
    207 		return result;
    208 
    209 	if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
    210 	    (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
    211 		result = KAUTH_RESULT_ALLOW;
    212 
    213 	return result;
    214 }
    215 
    216 /*
    217  * Network interface utility routines.
    218  *
    219  * Routines with ifa_ifwith* names take sockaddr *'s as
    220  * parameters.
    221  */
    222 void
    223 ifinit(void)
    224 {
    225 #if defined(INET)
    226 	sysctl_net_pktq_setup(NULL, PF_INET);
    227 #endif
    228 #ifdef INET6
    229 	sysctl_net_pktq_setup(NULL, PF_INET6);
    230 #endif
    231 
    232 	callout_init(&if_slowtimo_ch, 0);
    233 	if_slowtimo(NULL);
    234 
    235 	if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
    236 	    if_listener_cb, NULL);
    237 
    238 	/* interfaces are available, inform socket code */
    239 	ifioctl = doifioctl;
    240 }
    241 
    242 /*
    243  * XXX Initialization before configure().
    244  * XXX hack to get pfil_add_hook working in autoconf.
    245  */
    246 void
    247 ifinit1(void)
    248 {
    249 	mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE);
    250 	TAILQ_INIT(&ifnet_list);
    251 	if_indexlim = 8;
    252 
    253 	if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
    254 	KASSERT(if_pfil != NULL);
    255 }
    256 
    257 ifnet_t *
    258 if_alloc(u_char type)
    259 {
    260 	return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
    261 }
    262 
    263 void
    264 if_free(ifnet_t *ifp)
    265 {
    266 	kmem_free(ifp, sizeof(ifnet_t));
    267 }
    268 
    269 void
    270 if_initname(struct ifnet *ifp, const char *name, int unit)
    271 {
    272 	(void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
    273 	    "%s%d", name, unit);
    274 }
    275 
    276 /*
    277  * Null routines used while an interface is going away.  These routines
    278  * just return an error.
    279  */
    280 
    281 int
    282 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
    283     const struct sockaddr *so, struct rtentry *rt)
    284 {
    285 
    286 	return ENXIO;
    287 }
    288 
    289 void
    290 if_nullinput(struct ifnet *ifp, struct mbuf *m)
    291 {
    292 
    293 	/* Nothing. */
    294 }
    295 
    296 void
    297 if_nullstart(struct ifnet *ifp)
    298 {
    299 
    300 	/* Nothing. */
    301 }
    302 
    303 int
    304 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
    305 {
    306 
    307 	/* Wake ifioctl_detach(), who may wait for all threads to
    308 	 * quit the critical section.
    309 	 */
    310 	cv_signal(&ifp->if_ioctl_lock->il_emptied);
    311 	return ENXIO;
    312 }
    313 
    314 int
    315 if_nullinit(struct ifnet *ifp)
    316 {
    317 
    318 	return ENXIO;
    319 }
    320 
    321 void
    322 if_nullstop(struct ifnet *ifp, int disable)
    323 {
    324 
    325 	/* Nothing. */
    326 }
    327 
    328 void
    329 if_nullwatchdog(struct ifnet *ifp)
    330 {
    331 
    332 	/* Nothing. */
    333 }
    334 
    335 void
    336 if_nulldrain(struct ifnet *ifp)
    337 {
    338 
    339 	/* Nothing. */
    340 }
    341 
    342 void
    343 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
    344 {
    345 	struct ifaddr *ifa;
    346 	struct sockaddr_dl *sdl;
    347 
    348 	ifp->if_addrlen = addrlen;
    349 	if_alloc_sadl(ifp);
    350 	ifa = ifp->if_dl;
    351 	sdl = satosdl(ifa->ifa_addr);
    352 
    353 	(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
    354 	if (factory) {
    355 		ifp->if_hwdl = ifp->if_dl;
    356 		IFAREF(ifp->if_hwdl);
    357 	}
    358 	/* TBD routing socket */
    359 }
    360 
    361 struct ifaddr *
    362 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
    363 {
    364 	unsigned socksize, ifasize;
    365 	int addrlen, namelen;
    366 	struct sockaddr_dl *mask, *sdl;
    367 	struct ifaddr *ifa;
    368 
    369 	namelen = strlen(ifp->if_xname);
    370 	addrlen = ifp->if_addrlen;
    371 	socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
    372 	ifasize = sizeof(*ifa) + 2 * socksize;
    373 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
    374 
    375 	sdl = (struct sockaddr_dl *)(ifa + 1);
    376 	mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
    377 
    378 	sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
    379 	    ifp->if_xname, namelen, NULL, addrlen);
    380 	mask->sdl_len = sockaddr_dl_measure(namelen, 0);
    381 	memset(&mask->sdl_data[0], 0xff, namelen);
    382 	ifa->ifa_rtrequest = link_rtrequest;
    383 	ifa->ifa_addr = (struct sockaddr *)sdl;
    384 	ifa->ifa_netmask = (struct sockaddr *)mask;
    385 
    386 	*sdlp = sdl;
    387 
    388 	return ifa;
    389 }
    390 
    391 static void
    392 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
    393 {
    394 	const struct sockaddr_dl *sdl;
    395 	ifnet_addrs[ifp->if_index] = ifa;
    396 	IFAREF(ifa);
    397 	ifp->if_dl = ifa;
    398 	IFAREF(ifa);
    399 	sdl = satosdl(ifa->ifa_addr);
    400 	ifp->if_sadl = sdl;
    401 }
    402 
    403 /*
    404  * Allocate the link level name for the specified interface.  This
    405  * is an attachment helper.  It must be called after ifp->if_addrlen
    406  * is initialized, which may not be the case when if_attach() is
    407  * called.
    408  */
    409 void
    410 if_alloc_sadl(struct ifnet *ifp)
    411 {
    412 	struct ifaddr *ifa;
    413 	const struct sockaddr_dl *sdl;
    414 
    415 	/*
    416 	 * If the interface already has a link name, release it
    417 	 * now.  This is useful for interfaces that can change
    418 	 * link types, and thus switch link names often.
    419 	 */
    420 	if (ifp->if_sadl != NULL)
    421 		if_free_sadl(ifp);
    422 
    423 	ifa = if_dl_create(ifp, &sdl);
    424 
    425 	ifa_insert(ifp, ifa);
    426 	if_sadl_setrefs(ifp, ifa);
    427 }
    428 
    429 static void
    430 if_deactivate_sadl(struct ifnet *ifp)
    431 {
    432 	struct ifaddr *ifa;
    433 
    434 	KASSERT(ifp->if_dl != NULL);
    435 
    436 	ifa = ifp->if_dl;
    437 
    438 	ifp->if_sadl = NULL;
    439 
    440 	ifnet_addrs[ifp->if_index] = NULL;
    441 	IFAFREE(ifa);
    442 	ifp->if_dl = NULL;
    443 	IFAFREE(ifa);
    444 }
    445 
    446 void
    447 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa,
    448     const struct sockaddr_dl *sdl)
    449 {
    450 	int s;
    451 
    452 	s = splnet();
    453 
    454 	if_deactivate_sadl(ifp);
    455 
    456 	if_sadl_setrefs(ifp, ifa);
    457 	IFADDR_FOREACH(ifa, ifp)
    458 		rtinit(ifa, RTM_LLINFO_UPD, 0);
    459 	splx(s);
    460 }
    461 
    462 /*
    463  * Free the link level name for the specified interface.  This is
    464  * a detach helper.  This is called from if_detach() or from
    465  * link layer type specific detach functions.
    466  */
    467 void
    468 if_free_sadl(struct ifnet *ifp)
    469 {
    470 	struct ifaddr *ifa;
    471 	int s;
    472 
    473 	ifa = ifnet_addrs[ifp->if_index];
    474 	if (ifa == NULL) {
    475 		KASSERT(ifp->if_sadl == NULL);
    476 		KASSERT(ifp->if_dl == NULL);
    477 		return;
    478 	}
    479 
    480 	KASSERT(ifp->if_sadl != NULL);
    481 	KASSERT(ifp->if_dl != NULL);
    482 
    483 	s = splnet();
    484 	rtinit(ifa, RTM_DELETE, 0);
    485 	ifa_remove(ifp, ifa);
    486 	if_deactivate_sadl(ifp);
    487 	if (ifp->if_hwdl == ifa) {
    488 		IFAFREE(ifa);
    489 		ifp->if_hwdl = NULL;
    490 	}
    491 	splx(s);
    492 }
    493 
    494 static void
    495 if_getindex(ifnet_t *ifp)
    496 {
    497 	bool hitlimit = false;
    498 
    499 	mutex_enter(&index_gen_mtx);
    500 	ifp->if_index_gen = index_gen++;
    501 	mutex_exit(&index_gen_mtx);
    502 
    503 	ifp->if_index = if_index;
    504 	if (ifindex2ifnet == NULL) {
    505 		if_index++;
    506 		goto skip;
    507 	}
    508 	while (if_byindex(ifp->if_index)) {
    509 		/*
    510 		 * If we hit USHRT_MAX, we skip back to 0 since
    511 		 * there are a number of places where the value
    512 		 * of if_index or if_index itself is compared
    513 		 * to or stored in an unsigned short.  By
    514 		 * jumping back, we won't botch those assignments
    515 		 * or comparisons.
    516 		 */
    517 		if (++if_index == 0) {
    518 			if_index = 1;
    519 		} else if (if_index == USHRT_MAX) {
    520 			/*
    521 			 * However, if we have to jump back to
    522 			 * zero *twice* without finding an empty
    523 			 * slot in ifindex2ifnet[], then there
    524 			 * there are too many (>65535) interfaces.
    525 			 */
    526 			if (hitlimit) {
    527 				panic("too many interfaces");
    528 			}
    529 			hitlimit = true;
    530 			if_index = 1;
    531 		}
    532 		ifp->if_index = if_index;
    533 	}
    534 skip:
    535 	/*
    536 	 * We have some arrays that should be indexed by if_index.
    537 	 * since if_index will grow dynamically, they should grow too.
    538 	 *	struct ifadd **ifnet_addrs
    539 	 *	struct ifnet **ifindex2ifnet
    540 	 */
    541 	if (ifnet_addrs == NULL || ifindex2ifnet == NULL ||
    542 	    ifp->if_index >= if_indexlim) {
    543 		size_t m, n, oldlim;
    544 		void *q;
    545 
    546 		oldlim = if_indexlim;
    547 		while (ifp->if_index >= if_indexlim)
    548 			if_indexlim <<= 1;
    549 
    550 		/* grow ifnet_addrs */
    551 		m = oldlim * sizeof(struct ifaddr *);
    552 		n = if_indexlim * sizeof(struct ifaddr *);
    553 		q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
    554 		if (ifnet_addrs != NULL) {
    555 			memcpy(q, ifnet_addrs, m);
    556 			free(ifnet_addrs, M_IFADDR);
    557 		}
    558 		ifnet_addrs = (struct ifaddr **)q;
    559 
    560 		/* grow ifindex2ifnet */
    561 		m = oldlim * sizeof(struct ifnet *);
    562 		n = if_indexlim * sizeof(struct ifnet *);
    563 		q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
    564 		if (ifindex2ifnet != NULL) {
    565 			memcpy(q, ifindex2ifnet, m);
    566 			free(ifindex2ifnet, M_IFADDR);
    567 		}
    568 		ifindex2ifnet = (struct ifnet **)q;
    569 	}
    570 	ifindex2ifnet[ifp->if_index] = ifp;
    571 }
    572 
    573 /*
    574  * Attach an interface to the list of "active" interfaces.
    575  */
    576 void
    577 if_attach(ifnet_t *ifp)
    578 {
    579 	KASSERT(if_indexlim > 0);
    580 	TAILQ_INIT(&ifp->if_addrlist);
    581 	TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
    582 
    583 	if (ifioctl_attach(ifp) != 0)
    584 		panic("%s: ifioctl_attach() failed", __func__);
    585 
    586 	if_getindex(ifp);
    587 
    588 	/*
    589 	 * Link level name is allocated later by a separate call to
    590 	 * if_alloc_sadl().
    591 	 */
    592 
    593 	if (ifp->if_snd.ifq_maxlen == 0)
    594 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
    595 
    596 	sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
    597 
    598 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    599 
    600 	ifp->if_link_state = LINK_STATE_UNKNOWN;
    601 
    602 	ifp->if_capenable = 0;
    603 	ifp->if_csum_flags_tx = 0;
    604 	ifp->if_csum_flags_rx = 0;
    605 
    606 #ifdef ALTQ
    607 	ifp->if_snd.altq_type = 0;
    608 	ifp->if_snd.altq_disc = NULL;
    609 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
    610 	ifp->if_snd.altq_tbr  = NULL;
    611 	ifp->if_snd.altq_ifp  = ifp;
    612 #endif
    613 
    614 	ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
    615 	(void)pfil_run_hooks(if_pfil,
    616 	    (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
    617 
    618 	if (!STAILQ_EMPTY(&domains))
    619 		if_attachdomain1(ifp);
    620 
    621 	/* Announce the interface. */
    622 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
    623 }
    624 
    625 void
    626 if_attachdomain(void)
    627 {
    628 	struct ifnet *ifp;
    629 	int s;
    630 
    631 	s = splnet();
    632 	IFNET_FOREACH(ifp)
    633 		if_attachdomain1(ifp);
    634 	splx(s);
    635 }
    636 
    637 void
    638 if_attachdomain1(struct ifnet *ifp)
    639 {
    640 	struct domain *dp;
    641 	int s;
    642 
    643 	s = splnet();
    644 
    645 	/* address family dependent data region */
    646 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
    647 	DOMAIN_FOREACH(dp) {
    648 		if (dp->dom_ifattach != NULL)
    649 			ifp->if_afdata[dp->dom_family] =
    650 			    (*dp->dom_ifattach)(ifp);
    651 	}
    652 
    653 	splx(s);
    654 }
    655 
    656 /*
    657  * Deactivate an interface.  This points all of the procedure
    658  * handles at error stubs.  May be called from interrupt context.
    659  */
    660 void
    661 if_deactivate(struct ifnet *ifp)
    662 {
    663 	int s;
    664 
    665 	s = splnet();
    666 
    667 	ifp->if_output	 = if_nulloutput;
    668 	ifp->if_input	 = if_nullinput;
    669 	ifp->if_start	 = if_nullstart;
    670 	ifp->if_ioctl	 = if_nullioctl;
    671 	ifp->if_init	 = if_nullinit;
    672 	ifp->if_stop	 = if_nullstop;
    673 	ifp->if_watchdog = if_nullwatchdog;
    674 	ifp->if_drain	 = if_nulldrain;
    675 
    676 	/* No more packets may be enqueued. */
    677 	ifp->if_snd.ifq_maxlen = 0;
    678 
    679 	splx(s);
    680 }
    681 
    682 void
    683 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
    684 {
    685 	struct ifaddr *ifa, *nifa;
    686 
    687 	for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) {
    688 		nifa = IFADDR_NEXT(ifa);
    689 		if (ifa->ifa_addr->sa_family != family)
    690 			continue;
    691 		(*purgeaddr)(ifa);
    692 	}
    693 }
    694 
    695 /*
    696  * Detach an interface from the list of "active" interfaces,
    697  * freeing any resources as we go along.
    698  *
    699  * NOTE: This routine must be called with a valid thread context,
    700  * as it may block.
    701  */
    702 void
    703 if_detach(struct ifnet *ifp)
    704 {
    705 	struct socket so;
    706 	struct ifaddr *ifa;
    707 #ifdef IFAREF_DEBUG
    708 	struct ifaddr *last_ifa = NULL;
    709 #endif
    710 	struct domain *dp;
    711 	const struct protosw *pr;
    712 	int s, i, family, purged;
    713 	uint64_t xc;
    714 
    715 	/*
    716 	 * XXX It's kind of lame that we have to have the
    717 	 * XXX socket structure...
    718 	 */
    719 	memset(&so, 0, sizeof(so));
    720 
    721 	s = splnet();
    722 
    723 	/*
    724 	 * Do an if_down() to give protocols a chance to do something.
    725 	 */
    726 	if_down(ifp);
    727 
    728 #ifdef ALTQ
    729 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
    730 		altq_disable(&ifp->if_snd);
    731 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
    732 		altq_detach(&ifp->if_snd);
    733 #endif
    734 
    735 	sysctl_teardown(&ifp->if_sysctl_log);
    736 
    737 #if NCARP > 0
    738 	/* Remove the interface from any carp group it is a part of.  */
    739 	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
    740 		carp_ifdetach(ifp);
    741 #endif
    742 
    743 	/*
    744 	 * Rip all the addresses off the interface.  This should make
    745 	 * all of the routes go away.
    746 	 *
    747 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
    748 	 * from the list, including our "cursor", ifa.  For safety,
    749 	 * and to honor the TAILQ abstraction, I just restart the
    750 	 * loop after each removal.  Note that the loop will exit
    751 	 * when all of the remaining ifaddrs belong to the AF_LINK
    752 	 * family.  I am counting on the historical fact that at
    753 	 * least one pr_usrreq in each address domain removes at
    754 	 * least one ifaddr.
    755 	 */
    756 again:
    757 	IFADDR_FOREACH(ifa, ifp) {
    758 		family = ifa->ifa_addr->sa_family;
    759 #ifdef IFAREF_DEBUG
    760 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
    761 		    ifa, family, ifa->ifa_refcnt);
    762 		if (last_ifa != NULL && ifa == last_ifa)
    763 			panic("if_detach: loop detected");
    764 		last_ifa = ifa;
    765 #endif
    766 		if (family == AF_LINK)
    767 			continue;
    768 		dp = pffinddomain(family);
    769 #ifdef DIAGNOSTIC
    770 		if (dp == NULL)
    771 			panic("if_detach: no domain for AF %d",
    772 			    family);
    773 #endif
    774 		/*
    775 		 * XXX These PURGEIF calls are redundant with the
    776 		 * purge-all-families calls below, but are left in for
    777 		 * now both to make a smaller change, and to avoid
    778 		 * unplanned interactions with clearing of
    779 		 * ifp->if_addrlist.
    780 		 */
    781 		purged = 0;
    782 		for (pr = dp->dom_protosw;
    783 		     pr < dp->dom_protoswNPROTOSW; pr++) {
    784 			so.so_proto = pr;
    785 			if (pr->pr_usrreqs) {
    786 				(void) (*pr->pr_usrreqs->pr_generic)(&so,
    787 				    PRU_PURGEIF, NULL, NULL,
    788 				    (struct mbuf *) ifp, curlwp);
    789 				purged = 1;
    790 			}
    791 		}
    792 		if (purged == 0) {
    793 			/*
    794 			 * XXX What's really the best thing to do
    795 			 * XXX here?  --thorpej (at) NetBSD.org
    796 			 */
    797 			printf("if_detach: WARNING: AF %d not purged\n",
    798 			    family);
    799 			ifa_remove(ifp, ifa);
    800 		}
    801 		goto again;
    802 	}
    803 
    804 	if_free_sadl(ifp);
    805 
    806 	/* Walk the routing table looking for stragglers. */
    807 	for (i = 0; i <= AF_MAX; i++) {
    808 		while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
    809 			continue;
    810 	}
    811 
    812 	DOMAIN_FOREACH(dp) {
    813 		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
    814 		{
    815 			void *p = ifp->if_afdata[dp->dom_family];
    816 			if (p) {
    817 				ifp->if_afdata[dp->dom_family] = NULL;
    818 				(*dp->dom_ifdetach)(ifp, p);
    819 			}
    820 		}
    821 
    822 		/*
    823 		 * One would expect multicast memberships (INET and
    824 		 * INET6) on UDP sockets to be purged by the PURGEIF
    825 		 * calls above, but if all addresses were removed from
    826 		 * the interface prior to destruction, the calls will
    827 		 * not be made (e.g. ppp, for which pppd(8) generally
    828 		 * removes addresses before destroying the interface).
    829 		 * Because there is no invariant that multicast
    830 		 * memberships only exist for interfaces with IPv4
    831 		 * addresses, we must call PURGEIF regardless of
    832 		 * addresses.  (Protocols which might store ifnet
    833 		 * pointers are marked with PR_PURGEIF.)
    834 		 */
    835 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
    836 			so.so_proto = pr;
    837 			if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
    838 				(void)(*pr->pr_usrreqs->pr_generic)(&so,
    839 				    PRU_PURGEIF, NULL, NULL,
    840 				    (struct mbuf *)ifp, curlwp);
    841 		}
    842 	}
    843 
    844 	(void)pfil_run_hooks(if_pfil,
    845 	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
    846 	(void)pfil_head_destroy(ifp->if_pfil);
    847 
    848 	/* Announce that the interface is gone. */
    849 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
    850 
    851 	ifindex2ifnet[ifp->if_index] = NULL;
    852 
    853 	TAILQ_REMOVE(&ifnet_list, ifp, if_list);
    854 
    855 	ifioctl_detach(ifp);
    856 
    857 	/*
    858 	 * remove packets that came from ifp, from software interrupt queues.
    859 	 */
    860 	DOMAIN_FOREACH(dp) {
    861 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
    862 			struct ifqueue *iq = dp->dom_ifqueues[i];
    863 			if (iq == NULL)
    864 				break;
    865 			dp->dom_ifqueues[i] = NULL;
    866 			if_detach_queues(ifp, iq);
    867 		}
    868 	}
    869 
    870 	/*
    871 	 * IP queues have to be processed separately: net-queue barrier
    872 	 * ensures that the packets are dequeued while a cross-call will
    873 	 * ensure that the interrupts have completed. FIXME: not quite..
    874 	 */
    875 #ifdef INET
    876 	pktq_barrier(ip_pktq);
    877 #endif
    878 	xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    879 	xc_wait(xc);
    880 
    881 	splx(s);
    882 }
    883 
    884 static void
    885 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
    886 {
    887 	struct mbuf *m, *prev, *next;
    888 
    889 	prev = NULL;
    890 	for (m = q->ifq_head; m != NULL; m = next) {
    891 		KASSERT((m->m_flags & M_PKTHDR) != 0);
    892 
    893 		next = m->m_nextpkt;
    894 		if (m->m_pkthdr.rcvif != ifp) {
    895 			prev = m;
    896 			continue;
    897 		}
    898 
    899 		if (prev != NULL)
    900 			prev->m_nextpkt = m->m_nextpkt;
    901 		else
    902 			q->ifq_head = m->m_nextpkt;
    903 		if (q->ifq_tail == m)
    904 			q->ifq_tail = prev;
    905 		q->ifq_len--;
    906 
    907 		m->m_nextpkt = NULL;
    908 		m_freem(m);
    909 		IF_DROP(q);
    910 	}
    911 }
    912 
    913 /*
    914  * Callback for a radix tree walk to delete all references to an
    915  * ifnet.
    916  */
    917 static int
    918 if_rt_walktree(struct rtentry *rt, void *v)
    919 {
    920 	struct ifnet *ifp = (struct ifnet *)v;
    921 	int error;
    922 
    923 	if (rt->rt_ifp != ifp)
    924 		return 0;
    925 
    926 	/* Delete the entry. */
    927 	++rt->rt_refcnt;
    928 	error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
    929 	    rt_mask(rt), rt->rt_flags, NULL);
    930 	KASSERT((rt->rt_flags & RTF_UP) == 0);
    931 	rt->rt_ifp = NULL;
    932 	rtfree(rt);
    933 	if (error != 0)
    934 		printf("%s: warning: unable to delete rtentry @ %p, "
    935 		    "error = %d\n", ifp->if_xname, rt, error);
    936 	return ERESTART;
    937 }
    938 
    939 /*
    940  * Create a clone network interface.
    941  */
    942 int
    943 if_clone_create(const char *name)
    944 {
    945 	struct if_clone *ifc;
    946 	int unit;
    947 
    948 	ifc = if_clone_lookup(name, &unit);
    949 	if (ifc == NULL)
    950 		return EINVAL;
    951 
    952 	if (ifunit(name) != NULL)
    953 		return EEXIST;
    954 
    955 	return (*ifc->ifc_create)(ifc, unit);
    956 }
    957 
    958 /*
    959  * Destroy a clone network interface.
    960  */
    961 int
    962 if_clone_destroy(const char *name)
    963 {
    964 	struct if_clone *ifc;
    965 	struct ifnet *ifp;
    966 
    967 	ifc = if_clone_lookup(name, NULL);
    968 	if (ifc == NULL)
    969 		return EINVAL;
    970 
    971 	ifp = ifunit(name);
    972 	if (ifp == NULL)
    973 		return ENXIO;
    974 
    975 	if (ifc->ifc_destroy == NULL)
    976 		return EOPNOTSUPP;
    977 
    978 	return (*ifc->ifc_destroy)(ifp);
    979 }
    980 
    981 /*
    982  * Look up a network interface cloner.
    983  */
    984 static struct if_clone *
    985 if_clone_lookup(const char *name, int *unitp)
    986 {
    987 	struct if_clone *ifc;
    988 	const char *cp;
    989 	char *dp, ifname[IFNAMSIZ + 3];
    990 	int unit;
    991 
    992 	strcpy(ifname, "if_");
    993 	/* separate interface name from unit */
    994 	for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
    995 	    *cp && (*cp < '0' || *cp > '9');)
    996 		*dp++ = *cp++;
    997 
    998 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
    999 		return NULL;	/* No name or unit number */
   1000 	*dp++ = '\0';
   1001 
   1002 again:
   1003 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
   1004 		if (strcmp(ifname + 3, ifc->ifc_name) == 0)
   1005 			break;
   1006 	}
   1007 
   1008 	if (ifc == NULL) {
   1009 		if (*ifname == '\0' ||
   1010 		    module_autoload(ifname, MODULE_CLASS_DRIVER))
   1011 			return NULL;
   1012 		*ifname = '\0';
   1013 		goto again;
   1014 	}
   1015 
   1016 	unit = 0;
   1017 	while (cp - name < IFNAMSIZ && *cp) {
   1018 		if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
   1019 			/* Bogus unit number. */
   1020 			return NULL;
   1021 		}
   1022 		unit = (unit * 10) + (*cp++ - '0');
   1023 	}
   1024 
   1025 	if (unitp != NULL)
   1026 		*unitp = unit;
   1027 	return ifc;
   1028 }
   1029 
   1030 /*
   1031  * Register a network interface cloner.
   1032  */
   1033 void
   1034 if_clone_attach(struct if_clone *ifc)
   1035 {
   1036 
   1037 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
   1038 	if_cloners_count++;
   1039 }
   1040 
   1041 /*
   1042  * Unregister a network interface cloner.
   1043  */
   1044 void
   1045 if_clone_detach(struct if_clone *ifc)
   1046 {
   1047 
   1048 	LIST_REMOVE(ifc, ifc_list);
   1049 	if_cloners_count--;
   1050 }
   1051 
   1052 /*
   1053  * Provide list of interface cloners to userspace.
   1054  */
   1055 static int
   1056 if_clone_list(struct if_clonereq *ifcr)
   1057 {
   1058 	char outbuf[IFNAMSIZ], *dst;
   1059 	struct if_clone *ifc;
   1060 	int count, error = 0;
   1061 
   1062 	ifcr->ifcr_total = if_cloners_count;
   1063 	if ((dst = ifcr->ifcr_buffer) == NULL) {
   1064 		/* Just asking how many there are. */
   1065 		return 0;
   1066 	}
   1067 
   1068 	if (ifcr->ifcr_count < 0)
   1069 		return EINVAL;
   1070 
   1071 	count = (if_cloners_count < ifcr->ifcr_count) ?
   1072 	    if_cloners_count : ifcr->ifcr_count;
   1073 
   1074 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
   1075 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
   1076 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
   1077 		if (outbuf[sizeof(outbuf) - 1] != '\0')
   1078 			return ENAMETOOLONG;
   1079 		error = copyout(outbuf, dst, sizeof(outbuf));
   1080 		if (error != 0)
   1081 			break;
   1082 	}
   1083 
   1084 	return error;
   1085 }
   1086 
   1087 void
   1088 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
   1089 {
   1090 	ifa->ifa_ifp = ifp;
   1091 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
   1092 	IFAREF(ifa);
   1093 }
   1094 
   1095 void
   1096 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
   1097 {
   1098 	KASSERT(ifa->ifa_ifp == ifp);
   1099 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
   1100 	IFAFREE(ifa);
   1101 }
   1102 
   1103 static inline int
   1104 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
   1105 {
   1106 	return sockaddr_cmp(sa1, sa2) == 0;
   1107 }
   1108 
   1109 /*
   1110  * Locate an interface based on a complete address.
   1111  */
   1112 /*ARGSUSED*/
   1113 struct ifaddr *
   1114 ifa_ifwithaddr(const struct sockaddr *addr)
   1115 {
   1116 	struct ifnet *ifp;
   1117 	struct ifaddr *ifa;
   1118 
   1119 	IFNET_FOREACH(ifp) {
   1120 		if (ifp->if_output == if_nulloutput)
   1121 			continue;
   1122 		IFADDR_FOREACH(ifa, ifp) {
   1123 			if (ifa->ifa_addr->sa_family != addr->sa_family)
   1124 				continue;
   1125 			if (equal(addr, ifa->ifa_addr))
   1126 				return ifa;
   1127 			if ((ifp->if_flags & IFF_BROADCAST) &&
   1128 			    ifa->ifa_broadaddr &&
   1129 			    /* IP6 doesn't have broadcast */
   1130 			    ifa->ifa_broadaddr->sa_len != 0 &&
   1131 			    equal(ifa->ifa_broadaddr, addr))
   1132 				return ifa;
   1133 		}
   1134 	}
   1135 	return NULL;
   1136 }
   1137 
   1138 /*
   1139  * Locate the point to point interface with a given destination address.
   1140  */
   1141 /*ARGSUSED*/
   1142 struct ifaddr *
   1143 ifa_ifwithdstaddr(const struct sockaddr *addr)
   1144 {
   1145 	struct ifnet *ifp;
   1146 	struct ifaddr *ifa;
   1147 
   1148 	IFNET_FOREACH(ifp) {
   1149 		if (ifp->if_output == if_nulloutput)
   1150 			continue;
   1151 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   1152 			continue;
   1153 		IFADDR_FOREACH(ifa, ifp) {
   1154 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
   1155 			    ifa->ifa_dstaddr == NULL)
   1156 				continue;
   1157 			if (equal(addr, ifa->ifa_dstaddr))
   1158 				return ifa;
   1159 		}
   1160 	}
   1161 	return NULL;
   1162 }
   1163 
   1164 /*
   1165  * Find an interface on a specific network.  If many, choice
   1166  * is most specific found.
   1167  */
   1168 struct ifaddr *
   1169 ifa_ifwithnet(const struct sockaddr *addr)
   1170 {
   1171 	struct ifnet *ifp;
   1172 	struct ifaddr *ifa;
   1173 	const struct sockaddr_dl *sdl;
   1174 	struct ifaddr *ifa_maybe = 0;
   1175 	u_int af = addr->sa_family;
   1176 	const char *addr_data = addr->sa_data, *cplim;
   1177 
   1178 	if (af == AF_LINK) {
   1179 		sdl = satocsdl(addr);
   1180 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
   1181 		    ifindex2ifnet[sdl->sdl_index] &&
   1182 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
   1183 			return ifnet_addrs[sdl->sdl_index];
   1184 	}
   1185 #ifdef NETATALK
   1186 	if (af == AF_APPLETALK) {
   1187 		const struct sockaddr_at *sat, *sat2;
   1188 		sat = (const struct sockaddr_at *)addr;
   1189 		IFNET_FOREACH(ifp) {
   1190 			if (ifp->if_output == if_nulloutput)
   1191 				continue;
   1192 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
   1193 			if (ifa == NULL)
   1194 				continue;
   1195 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
   1196 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
   1197 				return ifa; /* exact match */
   1198 			if (ifa_maybe == NULL) {
   1199 				/* else keep the if with the right range */
   1200 				ifa_maybe = ifa;
   1201 			}
   1202 		}
   1203 		return ifa_maybe;
   1204 	}
   1205 #endif
   1206 	IFNET_FOREACH(ifp) {
   1207 		if (ifp->if_output == if_nulloutput)
   1208 			continue;
   1209 		IFADDR_FOREACH(ifa, ifp) {
   1210 			const char *cp, *cp2, *cp3;
   1211 
   1212 			if (ifa->ifa_addr->sa_family != af ||
   1213 			    ifa->ifa_netmask == NULL)
   1214  next:				continue;
   1215 			cp = addr_data;
   1216 			cp2 = ifa->ifa_addr->sa_data;
   1217 			cp3 = ifa->ifa_netmask->sa_data;
   1218 			cplim = (const char *)ifa->ifa_netmask +
   1219 			    ifa->ifa_netmask->sa_len;
   1220 			while (cp3 < cplim) {
   1221 				if ((*cp++ ^ *cp2++) & *cp3++) {
   1222 					/* want to continue for() loop */
   1223 					goto next;
   1224 				}
   1225 			}
   1226 			if (ifa_maybe == NULL ||
   1227 			    rn_refines((void *)ifa->ifa_netmask,
   1228 			    (void *)ifa_maybe->ifa_netmask))
   1229 				ifa_maybe = ifa;
   1230 		}
   1231 	}
   1232 	return ifa_maybe;
   1233 }
   1234 
   1235 /*
   1236  * Find the interface of the addresss.
   1237  */
   1238 struct ifaddr *
   1239 ifa_ifwithladdr(const struct sockaddr *addr)
   1240 {
   1241 	struct ifaddr *ia;
   1242 
   1243 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
   1244 	    (ia = ifa_ifwithnet(addr)))
   1245 		return ia;
   1246 	return NULL;
   1247 }
   1248 
   1249 /*
   1250  * Find an interface using a specific address family
   1251  */
   1252 struct ifaddr *
   1253 ifa_ifwithaf(int af)
   1254 {
   1255 	struct ifnet *ifp;
   1256 	struct ifaddr *ifa;
   1257 
   1258 	IFNET_FOREACH(ifp) {
   1259 		if (ifp->if_output == if_nulloutput)
   1260 			continue;
   1261 		IFADDR_FOREACH(ifa, ifp) {
   1262 			if (ifa->ifa_addr->sa_family == af)
   1263 				return ifa;
   1264 		}
   1265 	}
   1266 	return NULL;
   1267 }
   1268 
   1269 /*
   1270  * Find an interface address specific to an interface best matching
   1271  * a given address.
   1272  */
   1273 struct ifaddr *
   1274 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
   1275 {
   1276 	struct ifaddr *ifa;
   1277 	const char *cp, *cp2, *cp3;
   1278 	const char *cplim;
   1279 	struct ifaddr *ifa_maybe = 0;
   1280 	u_int af = addr->sa_family;
   1281 
   1282 	if (ifp->if_output == if_nulloutput)
   1283 		return NULL;
   1284 
   1285 	if (af >= AF_MAX)
   1286 		return NULL;
   1287 
   1288 	IFADDR_FOREACH(ifa, ifp) {
   1289 		if (ifa->ifa_addr->sa_family != af)
   1290 			continue;
   1291 		ifa_maybe = ifa;
   1292 		if (ifa->ifa_netmask == NULL) {
   1293 			if (equal(addr, ifa->ifa_addr) ||
   1294 			    (ifa->ifa_dstaddr &&
   1295 			     equal(addr, ifa->ifa_dstaddr)))
   1296 				return ifa;
   1297 			continue;
   1298 		}
   1299 		cp = addr->sa_data;
   1300 		cp2 = ifa->ifa_addr->sa_data;
   1301 		cp3 = ifa->ifa_netmask->sa_data;
   1302 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
   1303 		for (; cp3 < cplim; cp3++) {
   1304 			if ((*cp++ ^ *cp2++) & *cp3)
   1305 				break;
   1306 		}
   1307 		if (cp3 == cplim)
   1308 			return ifa;
   1309 	}
   1310 	return ifa_maybe;
   1311 }
   1312 
   1313 /*
   1314  * Default action when installing a route with a Link Level gateway.
   1315  * Lookup an appropriate real ifa to point to.
   1316  * This should be moved to /sys/net/link.c eventually.
   1317  */
   1318 void
   1319 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
   1320 {
   1321 	struct ifaddr *ifa;
   1322 	const struct sockaddr *dst;
   1323 	struct ifnet *ifp;
   1324 
   1325 	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
   1326 	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
   1327 		return;
   1328 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
   1329 		rt_replace_ifa(rt, ifa);
   1330 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
   1331 			ifa->ifa_rtrequest(cmd, rt, info);
   1332 	}
   1333 }
   1334 
   1335 /*
   1336  * Handle a change in the interface link state.
   1337  * XXX: We should listen to the routing socket in-kernel rather
   1338  * than calling in6_if_link_* functions directly from here.
   1339  */
   1340 void
   1341 if_link_state_change(struct ifnet *ifp, int link_state)
   1342 {
   1343 	int s;
   1344 #if defined(DEBUG) || defined(INET6)
   1345 	int old_link_state;
   1346 #endif
   1347 
   1348 	s = splnet();
   1349 	if (ifp->if_link_state == link_state) {
   1350 		splx(s);
   1351 		return;
   1352 	}
   1353 
   1354 #if defined(DEBUG) || defined(INET6)
   1355 	old_link_state = ifp->if_link_state;
   1356 #endif
   1357 	ifp->if_link_state = link_state;
   1358 #ifdef DEBUG
   1359 	log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
   1360 		link_state == LINK_STATE_UP ? "UP" :
   1361 		link_state == LINK_STATE_DOWN ? "DOWN" :
   1362 		"UNKNOWN",
   1363 		 old_link_state == LINK_STATE_UP ? "UP" :
   1364 		old_link_state == LINK_STATE_DOWN ? "DOWN" :
   1365 		"UNKNOWN");
   1366 #endif
   1367 
   1368 #ifdef INET6
   1369 	/*
   1370 	 * When going from UNKNOWN to UP, we need to mark existing
   1371 	 * IPv6 addresses as tentative and restart DAD as we may have
   1372 	 * erroneously not found a duplicate.
   1373 	 *
   1374 	 * This needs to happen before rt_ifmsg to avoid a race where
   1375 	 * listeners would have an address and expect it to work right
   1376 	 * away.
   1377 	 */
   1378 	if (in6_present && link_state == LINK_STATE_UP &&
   1379 	    old_link_state == LINK_STATE_UNKNOWN)
   1380 		in6_if_link_down(ifp);
   1381 #endif
   1382 
   1383 	/* Notify that the link state has changed. */
   1384 	rt_ifmsg(ifp);
   1385 
   1386 #if NCARP > 0
   1387 	if (ifp->if_carp)
   1388 		carp_carpdev_state(ifp);
   1389 #endif
   1390 
   1391 #ifdef INET6
   1392 	if (in6_present) {
   1393 		if (link_state == LINK_STATE_DOWN)
   1394 			in6_if_link_down(ifp);
   1395 		else if (link_state == LINK_STATE_UP)
   1396 			in6_if_link_up(ifp);
   1397 	}
   1398 #endif
   1399 
   1400 	splx(s);
   1401 }
   1402 
   1403 /*
   1404  * Mark an interface down and notify protocols of
   1405  * the transition.
   1406  * NOTE: must be called at splsoftnet or equivalent.
   1407  */
   1408 void
   1409 if_down(struct ifnet *ifp)
   1410 {
   1411 	struct ifaddr *ifa;
   1412 
   1413 	ifp->if_flags &= ~IFF_UP;
   1414 	nanotime(&ifp->if_lastchange);
   1415 	IFADDR_FOREACH(ifa, ifp)
   1416 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
   1417 	IFQ_PURGE(&ifp->if_snd);
   1418 #if NCARP > 0
   1419 	if (ifp->if_carp)
   1420 		carp_carpdev_state(ifp);
   1421 #endif
   1422 	rt_ifmsg(ifp);
   1423 #ifdef INET6
   1424 	if (in6_present)
   1425 		in6_if_down(ifp);
   1426 #endif
   1427 }
   1428 
   1429 /*
   1430  * Mark an interface up and notify protocols of
   1431  * the transition.
   1432  * NOTE: must be called at splsoftnet or equivalent.
   1433  */
   1434 void
   1435 if_up(struct ifnet *ifp)
   1436 {
   1437 #ifdef notyet
   1438 	struct ifaddr *ifa;
   1439 #endif
   1440 
   1441 	ifp->if_flags |= IFF_UP;
   1442 	nanotime(&ifp->if_lastchange);
   1443 #ifdef notyet
   1444 	/* this has no effect on IP, and will kill all ISO connections XXX */
   1445 	IFADDR_FOREACH(ifa, ifp)
   1446 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
   1447 #endif
   1448 #if NCARP > 0
   1449 	if (ifp->if_carp)
   1450 		carp_carpdev_state(ifp);
   1451 #endif
   1452 	rt_ifmsg(ifp);
   1453 #ifdef INET6
   1454 	if (in6_present)
   1455 		in6_if_up(ifp);
   1456 #endif
   1457 }
   1458 
   1459 /*
   1460  * Handle interface watchdog timer routines.  Called
   1461  * from softclock, we decrement timers (if set) and
   1462  * call the appropriate interface routine on expiration.
   1463  */
   1464 void
   1465 if_slowtimo(void *arg)
   1466 {
   1467 	struct ifnet *ifp;
   1468 	int s = splnet();
   1469 
   1470 	IFNET_FOREACH(ifp) {
   1471 		if (ifp->if_timer == 0 || --ifp->if_timer)
   1472 			continue;
   1473 		if (ifp->if_watchdog != NULL)
   1474 			(*ifp->if_watchdog)(ifp);
   1475 	}
   1476 	splx(s);
   1477 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
   1478 }
   1479 
   1480 /*
   1481  * Set/clear promiscuous mode on interface ifp based on the truth value
   1482  * of pswitch.  The calls are reference counted so that only the first
   1483  * "on" request actually has an effect, as does the final "off" request.
   1484  * Results are undefined if the "off" and "on" requests are not matched.
   1485  */
   1486 int
   1487 ifpromisc(struct ifnet *ifp, int pswitch)
   1488 {
   1489 	int pcount, ret;
   1490 	short nflags;
   1491 
   1492 	pcount = ifp->if_pcount;
   1493 	if (pswitch) {
   1494 		/*
   1495 		 * Allow the device to be "placed" into promiscuous
   1496 		 * mode even if it is not configured up.  It will
   1497 		 * consult IFF_PROMISC when it is brought up.
   1498 		 */
   1499 		if (ifp->if_pcount++ != 0)
   1500 			return 0;
   1501 		nflags = ifp->if_flags | IFF_PROMISC;
   1502 	} else {
   1503 		if (--ifp->if_pcount > 0)
   1504 			return 0;
   1505 		nflags = ifp->if_flags & ~IFF_PROMISC;
   1506 	}
   1507 	ret = if_flags_set(ifp, nflags);
   1508 	/* Restore interface state if not successful. */
   1509 	if (ret != 0) {
   1510 		ifp->if_pcount = pcount;
   1511 	}
   1512 	return ret;
   1513 }
   1514 
   1515 /*
   1516  * Map interface name to
   1517  * interface structure pointer.
   1518  */
   1519 struct ifnet *
   1520 ifunit(const char *name)
   1521 {
   1522 	struct ifnet *ifp;
   1523 	const char *cp = name;
   1524 	u_int unit = 0;
   1525 	u_int i;
   1526 
   1527 	/*
   1528 	 * If the entire name is a number, treat it as an ifindex.
   1529 	 */
   1530 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   1531 		unit = unit * 10 + (*cp - '0');
   1532 	}
   1533 
   1534 	/*
   1535 	 * If the number took all of the name, then it's a valid ifindex.
   1536 	 */
   1537 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
   1538 		if (unit >= if_indexlim)
   1539 			return NULL;
   1540 		ifp = ifindex2ifnet[unit];
   1541 		if (ifp == NULL || ifp->if_output == if_nulloutput)
   1542 			return NULL;
   1543 		return ifp;
   1544 	}
   1545 
   1546 	IFNET_FOREACH(ifp) {
   1547 		if (ifp->if_output == if_nulloutput)
   1548 			continue;
   1549 	 	if (strcmp(ifp->if_xname, name) == 0)
   1550 			return ifp;
   1551 	}
   1552 	return NULL;
   1553 }
   1554 
   1555 ifnet_t *
   1556 if_byindex(u_int idx)
   1557 {
   1558 	return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
   1559 }
   1560 
   1561 /* common */
   1562 int
   1563 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
   1564 {
   1565 	int s;
   1566 	struct ifreq *ifr;
   1567 	struct ifcapreq *ifcr;
   1568 	struct ifdatareq *ifdr;
   1569 
   1570 	switch (cmd) {
   1571 	case SIOCSIFCAP:
   1572 		ifcr = data;
   1573 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
   1574 			return EINVAL;
   1575 
   1576 		if (ifcr->ifcr_capenable == ifp->if_capenable)
   1577 			return 0;
   1578 
   1579 		ifp->if_capenable = ifcr->ifcr_capenable;
   1580 
   1581 		/* Pre-compute the checksum flags mask. */
   1582 		ifp->if_csum_flags_tx = 0;
   1583 		ifp->if_csum_flags_rx = 0;
   1584 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
   1585 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
   1586 		}
   1587 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
   1588 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
   1589 		}
   1590 
   1591 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
   1592 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
   1593 		}
   1594 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
   1595 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
   1596 		}
   1597 
   1598 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
   1599 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
   1600 		}
   1601 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
   1602 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
   1603 		}
   1604 
   1605 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
   1606 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
   1607 		}
   1608 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
   1609 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
   1610 		}
   1611 
   1612 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
   1613 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
   1614 		}
   1615 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
   1616 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
   1617 		}
   1618 		if (ifp->if_flags & IFF_UP)
   1619 			return ENETRESET;
   1620 		return 0;
   1621 	case SIOCSIFFLAGS:
   1622 		ifr = data;
   1623 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
   1624 			s = splnet();
   1625 			if_down(ifp);
   1626 			splx(s);
   1627 		}
   1628 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
   1629 			s = splnet();
   1630 			if_up(ifp);
   1631 			splx(s);
   1632 		}
   1633 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
   1634 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
   1635 		break;
   1636 	case SIOCGIFFLAGS:
   1637 		ifr = data;
   1638 		ifr->ifr_flags = ifp->if_flags;
   1639 		break;
   1640 
   1641 	case SIOCGIFMETRIC:
   1642 		ifr = data;
   1643 		ifr->ifr_metric = ifp->if_metric;
   1644 		break;
   1645 
   1646 	case SIOCGIFMTU:
   1647 		ifr = data;
   1648 		ifr->ifr_mtu = ifp->if_mtu;
   1649 		break;
   1650 
   1651 	case SIOCGIFDLT:
   1652 		ifr = data;
   1653 		ifr->ifr_dlt = ifp->if_dlt;
   1654 		break;
   1655 
   1656 	case SIOCGIFCAP:
   1657 		ifcr = data;
   1658 		ifcr->ifcr_capabilities = ifp->if_capabilities;
   1659 		ifcr->ifcr_capenable = ifp->if_capenable;
   1660 		break;
   1661 
   1662 	case SIOCSIFMETRIC:
   1663 		ifr = data;
   1664 		ifp->if_metric = ifr->ifr_metric;
   1665 		break;
   1666 
   1667 	case SIOCGIFDATA:
   1668 		ifdr = data;
   1669 		ifdr->ifdr_data = ifp->if_data;
   1670 		break;
   1671 
   1672 	case SIOCGIFINDEX:
   1673 		ifr = data;
   1674 		ifr->ifr_index = ifp->if_index;
   1675 		break;
   1676 
   1677 	case SIOCZIFDATA:
   1678 		ifdr = data;
   1679 		ifdr->ifdr_data = ifp->if_data;
   1680 		/*
   1681 		 * Assumes that the volatile counters that can be
   1682 		 * zero'ed are at the end of if_data.
   1683 		 */
   1684 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
   1685 		    offsetof(struct if_data, ifi_ipackets));
   1686 		/*
   1687 		 * The memset() clears to the bottm of if_data. In the area,
   1688 		 * if_lastchange is included. Please be careful if new entry
   1689 		 * will be added into if_data or rewite this.
   1690 		 *
   1691 		 * And also, update if_lastchnage.
   1692 		 */
   1693 		getnanotime(&ifp->if_lastchange);
   1694 		break;
   1695 	case SIOCSIFMTU:
   1696 		ifr = data;
   1697 		if (ifp->if_mtu == ifr->ifr_mtu)
   1698 			break;
   1699 		ifp->if_mtu = ifr->ifr_mtu;
   1700 		/*
   1701 		 * If the link MTU changed, do network layer specific procedure.
   1702 		 */
   1703 #ifdef INET6
   1704 		if (in6_present)
   1705 			nd6_setmtu(ifp);
   1706 #endif
   1707 		return ENETRESET;
   1708 	default:
   1709 		return ENOTTY;
   1710 	}
   1711 	return 0;
   1712 }
   1713 
   1714 int
   1715 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
   1716     lwp_t *l)
   1717 {
   1718 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
   1719 	struct ifaddr *ifa;
   1720 	const struct sockaddr *any, *sa;
   1721 	union {
   1722 		struct sockaddr sa;
   1723 		struct sockaddr_storage ss;
   1724 	} u, v;
   1725 
   1726 	switch (cmd) {
   1727 	case SIOCSIFADDRPREF:
   1728 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
   1729 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1730 		    NULL) != 0)
   1731 			return EPERM;
   1732 	case SIOCGIFADDRPREF:
   1733 		break;
   1734 	default:
   1735 		return EOPNOTSUPP;
   1736 	}
   1737 
   1738 	/* sanity checks */
   1739 	if (data == NULL || ifp == NULL) {
   1740 		panic("invalid argument to %s", __func__);
   1741 		/*NOTREACHED*/
   1742 	}
   1743 
   1744 	/* address must be specified on ADD and DELETE */
   1745 	sa = sstocsa(&ifap->ifap_addr);
   1746 	if (sa->sa_family != sofamily(so))
   1747 		return EINVAL;
   1748 	if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
   1749 		return EINVAL;
   1750 
   1751 	sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
   1752 
   1753 	IFADDR_FOREACH(ifa, ifp) {
   1754 		if (ifa->ifa_addr->sa_family != sa->sa_family)
   1755 			continue;
   1756 		sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
   1757 		if (sockaddr_cmp(&u.sa, &v.sa) == 0)
   1758 			break;
   1759 	}
   1760 	if (ifa == NULL)
   1761 		return EADDRNOTAVAIL;
   1762 
   1763 	switch (cmd) {
   1764 	case SIOCSIFADDRPREF:
   1765 		ifa->ifa_preference = ifap->ifap_preference;
   1766 		return 0;
   1767 	case SIOCGIFADDRPREF:
   1768 		/* fill in the if_laddrreq structure */
   1769 		(void)sockaddr_copy(sstosa(&ifap->ifap_addr),
   1770 		    sizeof(ifap->ifap_addr), ifa->ifa_addr);
   1771 		ifap->ifap_preference = ifa->ifa_preference;
   1772 		return 0;
   1773 	default:
   1774 		return EOPNOTSUPP;
   1775 	}
   1776 }
   1777 
   1778 static void
   1779 ifnet_lock_enter(struct ifnet_lock *il)
   1780 {
   1781 	uint64_t *nenter;
   1782 
   1783 	/* Before trying to acquire the mutex, increase the count of threads
   1784 	 * who have entered or who wait to enter the critical section.
   1785 	 * Avoid one costly locked memory transaction by keeping a count for
   1786 	 * each CPU.
   1787 	 */
   1788 	nenter = percpu_getref(il->il_nenter);
   1789 	(*nenter)++;
   1790 	percpu_putref(il->il_nenter);
   1791 	mutex_enter(&il->il_lock);
   1792 }
   1793 
   1794 static void
   1795 ifnet_lock_exit(struct ifnet_lock *il)
   1796 {
   1797 	/* Increase the count of threads who have exited the critical
   1798 	 * section.  Increase while we still hold the lock.
   1799 	 */
   1800 	il->il_nexit++;
   1801 	mutex_exit(&il->il_lock);
   1802 }
   1803 
   1804 /*
   1805  * Interface ioctls.
   1806  */
   1807 static int
   1808 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
   1809 {
   1810 	struct ifnet *ifp;
   1811 	struct ifreq *ifr;
   1812 	int error = 0;
   1813 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
   1814 	u_long ocmd = cmd;
   1815 #endif
   1816 	short oif_flags;
   1817 #ifdef COMPAT_OIFREQ
   1818 	struct ifreq ifrb;
   1819 	struct oifreq *oifr = NULL;
   1820 #endif
   1821 
   1822 	switch (cmd) {
   1823 #ifdef COMPAT_OIFREQ
   1824 	case OSIOCGIFCONF:
   1825 	case OOSIOCGIFCONF:
   1826 		return compat_ifconf(cmd, data);
   1827 #endif
   1828 #ifdef COMPAT_OIFDATA
   1829 	case OSIOCGIFDATA:
   1830 	case OSIOCZIFDATA:
   1831 		return compat_ifdatareq(l, cmd, data);
   1832 #endif
   1833 	case SIOCGIFCONF:
   1834 		return ifconf(cmd, data);
   1835 	case SIOCINITIFADDR:
   1836 		return EPERM;
   1837 	}
   1838 
   1839 #ifdef COMPAT_OIFREQ
   1840 	cmd = compat_cvtcmd(cmd);
   1841 	if (cmd != ocmd) {
   1842 		oifr = data;
   1843 		data = ifr = &ifrb;
   1844 		ifreqo2n(oifr, ifr);
   1845 	} else
   1846 #endif
   1847 		ifr = data;
   1848 
   1849 	ifp = ifunit(ifr->ifr_name);
   1850 
   1851 	switch (cmd) {
   1852 	case SIOCIFCREATE:
   1853 	case SIOCIFDESTROY:
   1854 		if (l != NULL) {
   1855 			error = kauth_authorize_network(l->l_cred,
   1856 			    KAUTH_NETWORK_INTERFACE,
   1857 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   1858 			    (void *)cmd, NULL);
   1859 			if (error != 0)
   1860 				return error;
   1861 		}
   1862 		return (cmd == SIOCIFCREATE) ?
   1863 			if_clone_create(ifr->ifr_name) :
   1864 			if_clone_destroy(ifr->ifr_name);
   1865 
   1866 	case SIOCIFGCLONERS:
   1867 		return if_clone_list((struct if_clonereq *)data);
   1868 	}
   1869 
   1870 	if (ifp == NULL)
   1871 		return ENXIO;
   1872 
   1873 	switch (cmd) {
   1874 	case SIOCALIFADDR:
   1875 	case SIOCDLIFADDR:
   1876 	case SIOCSIFADDRPREF:
   1877 	case SIOCSIFFLAGS:
   1878 	case SIOCSIFCAP:
   1879 	case SIOCSIFMETRIC:
   1880 	case SIOCZIFDATA:
   1881 	case SIOCSIFMTU:
   1882 	case SIOCSIFPHYADDR:
   1883 	case SIOCDIFPHYADDR:
   1884 #ifdef INET6
   1885 	case SIOCSIFPHYADDR_IN6:
   1886 #endif
   1887 	case SIOCSLIFPHYADDR:
   1888 	case SIOCADDMULTI:
   1889 	case SIOCDELMULTI:
   1890 	case SIOCSIFMEDIA:
   1891 	case SIOCSDRVSPEC:
   1892 	case SIOCG80211:
   1893 	case SIOCS80211:
   1894 	case SIOCS80211NWID:
   1895 	case SIOCS80211NWKEY:
   1896 	case SIOCS80211POWER:
   1897 	case SIOCS80211BSSID:
   1898 	case SIOCS80211CHANNEL:
   1899 	case SIOCSLINKSTR:
   1900 		if (l != NULL) {
   1901 			error = kauth_authorize_network(l->l_cred,
   1902 			    KAUTH_NETWORK_INTERFACE,
   1903 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   1904 			    (void *)cmd, NULL);
   1905 			if (error != 0)
   1906 				return error;
   1907 		}
   1908 	}
   1909 
   1910 	oif_flags = ifp->if_flags;
   1911 
   1912 	ifnet_lock_enter(ifp->if_ioctl_lock);
   1913 	error = (*ifp->if_ioctl)(ifp, cmd, data);
   1914 	if (error != ENOTTY)
   1915 		;
   1916 	else if (so->so_proto == NULL)
   1917 		error = EOPNOTSUPP;
   1918 	else {
   1919 #ifdef COMPAT_OSOCK
   1920 		error = compat_ifioctl(so, ocmd, cmd, data, l);
   1921 #else
   1922 		error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
   1923 		    PRU_CONTROL, (struct mbuf *)cmd, (struct mbuf *)data,
   1924 		    (struct mbuf *)ifp, l);
   1925 #endif
   1926 	}
   1927 
   1928 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   1929 #ifdef INET6
   1930 		if (in6_present && (ifp->if_flags & IFF_UP) != 0) {
   1931 			int s = splnet();
   1932 			in6_if_up(ifp);
   1933 			splx(s);
   1934 		}
   1935 #endif
   1936 	}
   1937 #ifdef COMPAT_OIFREQ
   1938 	if (cmd != ocmd)
   1939 		ifreqn2o(oifr, ifr);
   1940 #endif
   1941 
   1942 	ifnet_lock_exit(ifp->if_ioctl_lock);
   1943 	return error;
   1944 }
   1945 
   1946 /* This callback adds to the sum in `arg' the number of
   1947  * threads on `ci' who have entered or who wait to enter the
   1948  * critical section.
   1949  */
   1950 static void
   1951 ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci)
   1952 {
   1953 	uint64_t *sum = arg, *nenter = p;
   1954 
   1955 	*sum += *nenter;
   1956 }
   1957 
   1958 /* Return the number of threads who have entered or who wait
   1959  * to enter the critical section on all CPUs.
   1960  */
   1961 static uint64_t
   1962 ifnet_lock_entrances(struct ifnet_lock *il)
   1963 {
   1964 	uint64_t sum = 0;
   1965 
   1966 	percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum);
   1967 
   1968 	return sum;
   1969 }
   1970 
   1971 static int
   1972 ifioctl_attach(struct ifnet *ifp)
   1973 {
   1974 	struct ifnet_lock *il;
   1975 
   1976 	/* If the driver has not supplied its own if_ioctl, then
   1977 	 * supply the default.
   1978 	 */
   1979 	if (ifp->if_ioctl == NULL)
   1980 		ifp->if_ioctl = ifioctl_common;
   1981 
   1982 	/* Create an ifnet_lock for synchronizing ifioctls. */
   1983 	if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL)
   1984 		return ENOMEM;
   1985 
   1986 	il->il_nenter = percpu_alloc(sizeof(uint64_t));
   1987 	if (il->il_nenter == NULL) {
   1988 		kmem_free(il, sizeof(*il));
   1989 		return ENOMEM;
   1990 	}
   1991 
   1992 	mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE);
   1993 	cv_init(&il->il_emptied, ifp->if_xname);
   1994 
   1995 	ifp->if_ioctl_lock = il;
   1996 
   1997 	return 0;
   1998 }
   1999 
   2000 /*
   2001  * This must not be called until after `ifp' has been withdrawn from the
   2002  * ifnet tables so that ifioctl() cannot get a handle on it by calling
   2003  * ifunit().
   2004  */
   2005 static void
   2006 ifioctl_detach(struct ifnet *ifp)
   2007 {
   2008 	struct ifnet_lock *il;
   2009 
   2010 	il = ifp->if_ioctl_lock;
   2011 	mutex_enter(&il->il_lock);
   2012 	/* Install if_nullioctl to make sure that any thread that
   2013 	 * subsequently enters the critical section will quit it
   2014 	 * immediately and signal the condition variable that we
   2015 	 * wait on, below.
   2016 	 */
   2017 	ifp->if_ioctl = if_nullioctl;
   2018 	/* Sleep while threads are still in the critical section or
   2019 	 * wait to enter it.
   2020 	 */
   2021 	while (ifnet_lock_entrances(il) != il->il_nexit)
   2022 		cv_wait(&il->il_emptied, &il->il_lock);
   2023 	/* At this point, we are the only thread still in the critical
   2024 	 * section, and no new thread can get a handle on the ifioctl
   2025 	 * lock, so it is safe to free its memory.
   2026 	 */
   2027 	mutex_exit(&il->il_lock);
   2028 	ifp->if_ioctl_lock = NULL;
   2029 	percpu_free(il->il_nenter, sizeof(uint64_t));
   2030 	il->il_nenter = NULL;
   2031 	cv_destroy(&il->il_emptied);
   2032 	mutex_destroy(&il->il_lock);
   2033 	kmem_free(il, sizeof(*il));
   2034 }
   2035 
   2036 /*
   2037  * Return interface configuration
   2038  * of system.  List may be used
   2039  * in later ioctl's (above) to get
   2040  * other information.
   2041  *
   2042  * Each record is a struct ifreq.  Before the addition of
   2043  * sockaddr_storage, the API rule was that sockaddr flavors that did
   2044  * not fit would extend beyond the struct ifreq, with the next struct
   2045  * ifreq starting sa_len beyond the struct sockaddr.  Because the
   2046  * union in struct ifreq includes struct sockaddr_storage, every kind
   2047  * of sockaddr must fit.  Thus, there are no longer any overlength
   2048  * records.
   2049  *
   2050  * Records are added to the user buffer if they fit, and ifc_len is
   2051  * adjusted to the length that was written.  Thus, the user is only
   2052  * assured of getting the complete list if ifc_len on return is at
   2053  * least sizeof(struct ifreq) less than it was on entry.
   2054  *
   2055  * If the user buffer pointer is NULL, this routine copies no data and
   2056  * returns the amount of space that would be needed.
   2057  *
   2058  * Invariants:
   2059  * ifrp points to the next part of the user's buffer to be used.  If
   2060  * ifrp != NULL, space holds the number of bytes remaining that we may
   2061  * write at ifrp.  Otherwise, space holds the number of bytes that
   2062  * would have been written had there been adequate space.
   2063  */
   2064 /*ARGSUSED*/
   2065 int
   2066 ifconf(u_long cmd, void *data)
   2067 {
   2068 	struct ifconf *ifc = (struct ifconf *)data;
   2069 	struct ifnet *ifp;
   2070 	struct ifaddr *ifa;
   2071 	struct ifreq ifr, *ifrp;
   2072 	int space, error = 0;
   2073 	const int sz = (int)sizeof(struct ifreq);
   2074 
   2075 	if ((ifrp = ifc->ifc_req) == NULL)
   2076 		space = 0;
   2077 	else
   2078 		space = ifc->ifc_len;
   2079 	IFNET_FOREACH(ifp) {
   2080 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
   2081 		    sizeof(ifr.ifr_name));
   2082 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
   2083 			return ENAMETOOLONG;
   2084 		if (IFADDR_EMPTY(ifp)) {
   2085 			/* Interface with no addresses - send zero sockaddr. */
   2086 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2087 			if (ifrp == NULL) {
   2088 				space += sz;
   2089 				continue;
   2090 			}
   2091 			if (space >= sz) {
   2092 				error = copyout(&ifr, ifrp, sz);
   2093 				if (error != 0)
   2094 					return error;
   2095 				ifrp++;
   2096 				space -= sz;
   2097 			}
   2098 		}
   2099 
   2100 		IFADDR_FOREACH(ifa, ifp) {
   2101 			struct sockaddr *sa = ifa->ifa_addr;
   2102 			/* all sockaddrs must fit in sockaddr_storage */
   2103 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
   2104 
   2105 			if (ifrp == NULL) {
   2106 				space += sz;
   2107 				continue;
   2108 			}
   2109 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
   2110 			if (space >= sz) {
   2111 				error = copyout(&ifr, ifrp, sz);
   2112 				if (error != 0)
   2113 					return (error);
   2114 				ifrp++; space -= sz;
   2115 			}
   2116 		}
   2117 	}
   2118 	if (ifrp != NULL) {
   2119 		KASSERT(0 <= space && space <= ifc->ifc_len);
   2120 		ifc->ifc_len -= space;
   2121 	} else {
   2122 		KASSERT(space >= 0);
   2123 		ifc->ifc_len = space;
   2124 	}
   2125 	return (0);
   2126 }
   2127 
   2128 int
   2129 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
   2130 {
   2131 	uint8_t len;
   2132 #ifdef COMPAT_OIFREQ
   2133 	struct ifreq ifrb;
   2134 	struct oifreq *oifr = NULL;
   2135 	u_long ocmd = cmd;
   2136 	cmd = compat_cvtcmd(cmd);
   2137 	if (cmd != ocmd) {
   2138 		oifr = (struct oifreq *)(void *)ifr;
   2139 		ifr = &ifrb;
   2140 		ifreqo2n(oifr, ifr);
   2141 		len = sizeof(oifr->ifr_addr);
   2142 	} else
   2143 #endif
   2144 		len = sizeof(ifr->ifr_ifru.ifru_space);
   2145 
   2146 	if (len < sa->sa_len)
   2147 		return EFBIG;
   2148 
   2149 	memset(&ifr->ifr_addr, 0, len);
   2150 	sockaddr_copy(&ifr->ifr_addr, len, sa);
   2151 
   2152 #ifdef COMPAT_OIFREQ
   2153 	if (cmd != ocmd)
   2154 		ifreqn2o(oifr, ifr);
   2155 #endif
   2156 	return 0;
   2157 }
   2158 
   2159 /*
   2160  * Queue message on interface, and start output if interface
   2161  * not yet active.
   2162  */
   2163 int
   2164 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
   2165     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
   2166 {
   2167 	int len = m->m_pkthdr.len;
   2168 	int mflags = m->m_flags;
   2169 	int s = splnet();
   2170 	int error;
   2171 
   2172 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
   2173 	if (error != 0)
   2174 		goto out;
   2175 	ifp->if_obytes += len;
   2176 	if (mflags & M_MCAST)
   2177 		ifp->if_omcasts++;
   2178 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
   2179 		(*ifp->if_start)(ifp);
   2180 out:
   2181 	splx(s);
   2182 	return error;
   2183 }
   2184 
   2185 /*
   2186  * Queue message on interface, possibly using a second fast queue
   2187  */
   2188 int
   2189 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
   2190     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
   2191 {
   2192 	int error = 0;
   2193 
   2194 	if (ifq != NULL
   2195 #ifdef ALTQ
   2196 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
   2197 #endif
   2198 	    ) {
   2199 		if (IF_QFULL(ifq)) {
   2200 			IF_DROP(&ifp->if_snd);
   2201 			m_freem(m);
   2202 			if (error == 0)
   2203 				error = ENOBUFS;
   2204 		} else
   2205 			IF_ENQUEUE(ifq, m);
   2206 	} else
   2207 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
   2208 	if (error != 0) {
   2209 		++ifp->if_oerrors;
   2210 		return error;
   2211 	}
   2212 	return 0;
   2213 }
   2214 
   2215 int
   2216 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
   2217 {
   2218 	int rc;
   2219 
   2220 	if (ifp->if_initaddr != NULL)
   2221 		rc = (*ifp->if_initaddr)(ifp, ifa, src);
   2222 	else if (src ||
   2223 	         (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
   2224 		rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
   2225 
   2226 	return rc;
   2227 }
   2228 
   2229 int
   2230 if_flags_set(ifnet_t *ifp, const short flags)
   2231 {
   2232 	int rc;
   2233 
   2234 	if (ifp->if_setflags != NULL)
   2235 		rc = (*ifp->if_setflags)(ifp, flags);
   2236 	else {
   2237 		short cantflags, chgdflags;
   2238 		struct ifreq ifr;
   2239 
   2240 		chgdflags = ifp->if_flags ^ flags;
   2241 		cantflags = chgdflags & IFF_CANTCHANGE;
   2242 
   2243 		if (cantflags != 0)
   2244 			ifp->if_flags ^= cantflags;
   2245 
   2246                 /* Traditionally, we do not call if_ioctl after
   2247                  * setting/clearing only IFF_PROMISC if the interface
   2248                  * isn't IFF_UP.  Uphold that tradition.
   2249 		 */
   2250 		if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
   2251 			return 0;
   2252 
   2253 		memset(&ifr, 0, sizeof(ifr));
   2254 
   2255 		ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
   2256 		rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
   2257 
   2258 		if (rc != 0 && cantflags != 0)
   2259 			ifp->if_flags ^= cantflags;
   2260 	}
   2261 
   2262 	return rc;
   2263 }
   2264 
   2265 int
   2266 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
   2267 {
   2268 	int rc;
   2269 	struct ifreq ifr;
   2270 
   2271 	if (ifp->if_mcastop != NULL)
   2272 		rc = (*ifp->if_mcastop)(ifp, cmd, sa);
   2273 	else {
   2274 		ifreq_setaddr(cmd, &ifr, sa);
   2275 		rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
   2276 	}
   2277 
   2278 	return rc;
   2279 }
   2280 
   2281 static void
   2282 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
   2283     struct ifaltq *ifq)
   2284 {
   2285 	const struct sysctlnode *cnode, *rnode;
   2286 
   2287 	if (sysctl_createv(clog, 0, NULL, &rnode,
   2288 		       CTLFLAG_PERMANENT,
   2289 		       CTLTYPE_NODE, "interfaces",
   2290 		       SYSCTL_DESCR("Per-interface controls"),
   2291 		       NULL, 0, NULL, 0,
   2292 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
   2293 		goto bad;
   2294 
   2295 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2296 		       CTLFLAG_PERMANENT,
   2297 		       CTLTYPE_NODE, ifname,
   2298 		       SYSCTL_DESCR("Interface controls"),
   2299 		       NULL, 0, NULL, 0,
   2300 		       CTL_CREATE, CTL_EOL) != 0)
   2301 		goto bad;
   2302 
   2303 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2304 		       CTLFLAG_PERMANENT,
   2305 		       CTLTYPE_NODE, "sndq",
   2306 		       SYSCTL_DESCR("Interface output queue controls"),
   2307 		       NULL, 0, NULL, 0,
   2308 		       CTL_CREATE, CTL_EOL) != 0)
   2309 		goto bad;
   2310 
   2311 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2312 		       CTLFLAG_PERMANENT,
   2313 		       CTLTYPE_INT, "len",
   2314 		       SYSCTL_DESCR("Current output queue length"),
   2315 		       NULL, 0, &ifq->ifq_len, 0,
   2316 		       CTL_CREATE, CTL_EOL) != 0)
   2317 		goto bad;
   2318 
   2319 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2320 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2321 		       CTLTYPE_INT, "maxlen",
   2322 		       SYSCTL_DESCR("Maximum allowed output queue length"),
   2323 		       NULL, 0, &ifq->ifq_maxlen, 0,
   2324 		       CTL_CREATE, CTL_EOL) != 0)
   2325 		goto bad;
   2326 
   2327 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2328 		       CTLFLAG_PERMANENT,
   2329 		       CTLTYPE_INT, "drops",
   2330 		       SYSCTL_DESCR("Packets dropped due to full output queue"),
   2331 		       NULL, 0, &ifq->ifq_drops, 0,
   2332 		       CTL_CREATE, CTL_EOL) != 0)
   2333 		goto bad;
   2334 
   2335 	return;
   2336 bad:
   2337 	printf("%s: could not attach sysctl nodes\n", ifname);
   2338 	return;
   2339 }
   2340 
   2341 #if defined(INET) || defined(INET6)
   2342 
   2343 static int
   2344 sysctl_pktq_count(SYSCTLFN_ARGS, pktqueue_t *pq, u_int count_id)
   2345 {
   2346 	int count = pktq_get_count(pq, count_id);
   2347 	struct sysctlnode node = *rnode;
   2348 	node.sysctl_data = &count;
   2349 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2350 }
   2351 
   2352 #define	SYSCTL_NET_PKTQ(q, cn, c)					\
   2353 	static int							\
   2354 	sysctl_net_##q##_##cn(SYSCTLFN_ARGS)				\
   2355 	{								\
   2356 		return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);	\
   2357 	}
   2358 
   2359 #if defined(INET)
   2360 SYSCTL_NET_PKTQ(ip_pktq, maxlen, PKTQ_MAXLEN)
   2361 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
   2362 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
   2363 #endif
   2364 #if defined(INET6)
   2365 SYSCTL_NET_PKTQ(ip6_pktq, maxlen, PKTQ_MAXLEN)
   2366 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
   2367 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
   2368 #endif
   2369 
   2370 static void
   2371 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
   2372 {
   2373 	sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
   2374 	const char *pfname = NULL, *ipname = NULL;
   2375 	int ipn = 0, qid = 0;
   2376 
   2377 	switch (pf) {
   2378 #if defined(INET)
   2379 	case PF_INET:
   2380 		len_func = sysctl_net_ip_pktq_items;
   2381 		maxlen_func = sysctl_net_ip_pktq_maxlen;
   2382 		drops_func = sysctl_net_ip_pktq_drops;
   2383 		pfname = "inet", ipn = IPPROTO_IP;
   2384 		ipname = "ip", qid = IPCTL_IFQ;
   2385 		break;
   2386 #endif
   2387 #if defined(INET6)
   2388 	case PF_INET6:
   2389 		len_func = sysctl_net_ip6_pktq_items;
   2390 		maxlen_func = sysctl_net_ip6_pktq_maxlen;
   2391 		drops_func = sysctl_net_ip6_pktq_drops;
   2392 		pfname = "inet6", ipn = IPPROTO_IPV6;
   2393 		ipname = "ip6", qid = IPV6CTL_IFQ;
   2394 		break;
   2395 #endif
   2396 	default:
   2397 		KASSERT(false);
   2398 	}
   2399 
   2400 	sysctl_createv(clog, 0, NULL, NULL,
   2401 		       CTLFLAG_PERMANENT,
   2402 		       CTLTYPE_NODE, pfname, NULL,
   2403 		       NULL, 0, NULL, 0,
   2404 		       CTL_NET, pf, CTL_EOL);
   2405 	sysctl_createv(clog, 0, NULL, NULL,
   2406 		       CTLFLAG_PERMANENT,
   2407 		       CTLTYPE_NODE, ipname, NULL,
   2408 		       NULL, 0, NULL, 0,
   2409 		       CTL_NET, pf, ipn, CTL_EOL);
   2410 	sysctl_createv(clog, 0, NULL, NULL,
   2411 		       CTLFLAG_PERMANENT,
   2412 		       CTLTYPE_NODE, "ifq",
   2413 		       SYSCTL_DESCR("Protocol input queue controls"),
   2414 		       NULL, 0, NULL, 0,
   2415 		       CTL_NET, pf, ipn, qid, CTL_EOL);
   2416 
   2417 	sysctl_createv(clog, 0, NULL, NULL,
   2418 		       CTLFLAG_PERMANENT,
   2419 		       CTLTYPE_INT, "len",
   2420 		       SYSCTL_DESCR("Current input queue length"),
   2421 		       len_func, 0, NULL, 0,
   2422 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
   2423 	sysctl_createv(clog, 0, NULL, NULL,
   2424 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2425 		       CTLTYPE_INT, "maxlen",
   2426 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   2427 		       maxlen_func, 0, NULL, 0,
   2428 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
   2429 	sysctl_createv(clog, 0, NULL, NULL,
   2430 		       CTLFLAG_PERMANENT,
   2431 		       CTLTYPE_INT, "drops",
   2432 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
   2433 		       drops_func, 0, NULL, 0,
   2434 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
   2435 }
   2436 #endif /* INET || INET6 */
   2437