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