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