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