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