Home | History | Annotate | Line # | Download | only in net
if.c revision 1.342
      1 /*	$NetBSD: if.c,v 1.342 2016/06/20 06:41:30 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.342 2016/06/20 06:41:30 ozaki-r Exp $");
     94 
     95 #if defined(_KERNEL_OPT)
     96 #include "opt_inet.h"
     97 
     98 #include "opt_atalk.h"
     99 #include "opt_natm.h"
    100 #include "opt_wlan.h"
    101 #include "opt_net_mpsafe.h"
    102 #endif
    103 
    104 #include <sys/param.h>
    105 #include <sys/mbuf.h>
    106 #include <sys/systm.h>
    107 #include <sys/callout.h>
    108 #include <sys/proc.h>
    109 #include <sys/socket.h>
    110 #include <sys/socketvar.h>
    111 #include <sys/domain.h>
    112 #include <sys/protosw.h>
    113 #include <sys/kernel.h>
    114 #include <sys/ioctl.h>
    115 #include <sys/sysctl.h>
    116 #include <sys/syslog.h>
    117 #include <sys/kauth.h>
    118 #include <sys/kmem.h>
    119 #include <sys/xcall.h>
    120 #include <sys/cpu.h>
    121 #include <sys/intr.h>
    122 
    123 #include <net/if.h>
    124 #include <net/if_dl.h>
    125 #include <net/if_ether.h>
    126 #include <net/if_media.h>
    127 #include <net80211/ieee80211.h>
    128 #include <net80211/ieee80211_ioctl.h>
    129 #include <net/if_types.h>
    130 #include <net/route.h>
    131 #include <net/netisr.h>
    132 #include <sys/module.h>
    133 #ifdef NETATALK
    134 #include <netatalk/at_extern.h>
    135 #include <netatalk/at.h>
    136 #endif
    137 #include <net/pfil.h>
    138 #include <netinet/in.h>
    139 #include <netinet/in_var.h>
    140 
    141 #ifdef INET6
    142 #include <netinet6/in6_var.h>
    143 #include <netinet6/nd6.h>
    144 #endif
    145 
    146 #include "ether.h"
    147 #include "fddi.h"
    148 #include "token.h"
    149 
    150 #include "carp.h"
    151 #if NCARP > 0
    152 #include <netinet/ip_carp.h>
    153 #endif
    154 
    155 #include <compat/sys/sockio.h>
    156 #include <compat/sys/socket.h>
    157 
    158 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
    159 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
    160 
    161 /*
    162  * Global list of interfaces.
    163  */
    164 /* DEPRECATED. Remove it once kvm(3) users disappeared */
    165 struct ifnet_head		ifnet_list;
    166 
    167 struct pslist_head		ifnet_pslist;
    168 static ifnet_t **		ifindex2ifnet = NULL;
    169 static u_int			if_index = 1;
    170 static size_t			if_indexlim = 0;
    171 static uint64_t			index_gen;
    172 /* Mutex to protect the above objects. */
    173 kmutex_t			ifnet_mtx __cacheline_aligned;
    174 struct psref_class		*ifnet_psref_class __read_mostly;
    175 static pserialize_t		ifnet_psz;
    176 
    177 static kmutex_t			if_clone_mtx;
    178 
    179 struct ifnet *lo0ifp;
    180 int	ifqmaxlen = IFQ_MAXLEN;
    181 
    182 static int	if_rt_walktree(struct rtentry *, void *);
    183 
    184 static struct if_clone *if_clone_lookup(const char *, int *);
    185 
    186 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
    187 static int if_cloners_count;
    188 
    189 /* Packet filtering hook for interfaces. */
    190 pfil_head_t *	if_pfil;
    191 
    192 static kauth_listener_t if_listener;
    193 
    194 static int doifioctl(struct socket *, u_long, void *, struct lwp *);
    195 static void if_detach_queues(struct ifnet *, struct ifqueue *);
    196 static void sysctl_sndq_setup(struct sysctllog **, const char *,
    197     struct ifaltq *);
    198 static void if_slowtimo(void *);
    199 static void if_free_sadl(struct ifnet *);
    200 static void if_attachdomain1(struct ifnet *);
    201 static int ifconf(u_long, void *);
    202 static int if_clone_create(const char *);
    203 static int if_clone_destroy(const char *);
    204 static void if_link_state_change_si(void *);
    205 
    206 struct if_percpuq {
    207 	struct ifnet	*ipq_ifp;
    208 	void		*ipq_si;
    209 	struct percpu	*ipq_ifqs;	/* struct ifqueue */
    210 };
    211 
    212 static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
    213 
    214 static void if_percpuq_drops(void *, void *, struct cpu_info *);
    215 static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
    216 static void sysctl_percpuq_setup(struct sysctllog **, const char *,
    217     struct if_percpuq *);
    218 
    219 #if defined(INET) || defined(INET6)
    220 static void sysctl_net_pktq_setup(struct sysctllog **, int);
    221 #endif
    222 
    223 static int
    224 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    225     void *arg0, void *arg1, void *arg2, void *arg3)
    226 {
    227 	int result;
    228 	enum kauth_network_req req;
    229 
    230 	result = KAUTH_RESULT_DEFER;
    231 	req = (enum kauth_network_req)arg1;
    232 
    233 	if (action != KAUTH_NETWORK_INTERFACE)
    234 		return result;
    235 
    236 	if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
    237 	    (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
    238 		result = KAUTH_RESULT_ALLOW;
    239 
    240 	return result;
    241 }
    242 
    243 /*
    244  * Network interface utility routines.
    245  *
    246  * Routines with ifa_ifwith* names take sockaddr *'s as
    247  * parameters.
    248  */
    249 void
    250 ifinit(void)
    251 {
    252 #if defined(INET)
    253 	sysctl_net_pktq_setup(NULL, PF_INET);
    254 #endif
    255 #ifdef INET6
    256 	if (in6_present)
    257 		sysctl_net_pktq_setup(NULL, PF_INET6);
    258 #endif
    259 
    260 	if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
    261 	    if_listener_cb, NULL);
    262 
    263 	/* interfaces are available, inform socket code */
    264 	ifioctl = doifioctl;
    265 }
    266 
    267 /*
    268  * XXX Initialization before configure().
    269  * XXX hack to get pfil_add_hook working in autoconf.
    270  */
    271 void
    272 ifinit1(void)
    273 {
    274 	mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
    275 	TAILQ_INIT(&ifnet_list);
    276 	mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
    277 	ifnet_psz = pserialize_create();
    278 	ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
    279 	PSLIST_INIT(&ifnet_pslist);
    280 	if_indexlim = 8;
    281 
    282 	if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
    283 	KASSERT(if_pfil != NULL);
    284 
    285 #if NETHER > 0 || NFDDI > 0 || defined(NETATALK) || NTOKEN > 0 || defined(WLAN)
    286 	etherinit();
    287 #endif
    288 }
    289 
    290 ifnet_t *
    291 if_alloc(u_char type)
    292 {
    293 	return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
    294 }
    295 
    296 void
    297 if_free(ifnet_t *ifp)
    298 {
    299 	kmem_free(ifp, sizeof(ifnet_t));
    300 }
    301 
    302 void
    303 if_initname(struct ifnet *ifp, const char *name, int unit)
    304 {
    305 	(void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
    306 	    "%s%d", name, unit);
    307 }
    308 
    309 /*
    310  * Null routines used while an interface is going away.  These routines
    311  * just return an error.
    312  */
    313 
    314 int
    315 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
    316     const struct sockaddr *so, const struct rtentry *rt)
    317 {
    318 
    319 	return ENXIO;
    320 }
    321 
    322 void
    323 if_nullinput(struct ifnet *ifp, struct mbuf *m)
    324 {
    325 
    326 	/* Nothing. */
    327 }
    328 
    329 void
    330 if_nullstart(struct ifnet *ifp)
    331 {
    332 
    333 	/* Nothing. */
    334 }
    335 
    336 int
    337 if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
    338 {
    339 
    340 	return ENXIO;
    341 }
    342 
    343 int
    344 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
    345 {
    346 
    347 	return ENXIO;
    348 }
    349 
    350 int
    351 if_nullinit(struct ifnet *ifp)
    352 {
    353 
    354 	return ENXIO;
    355 }
    356 
    357 void
    358 if_nullstop(struct ifnet *ifp, int disable)
    359 {
    360 
    361 	/* Nothing. */
    362 }
    363 
    364 void
    365 if_nullslowtimo(struct ifnet *ifp)
    366 {
    367 
    368 	/* Nothing. */
    369 }
    370 
    371 void
    372 if_nulldrain(struct ifnet *ifp)
    373 {
    374 
    375 	/* Nothing. */
    376 }
    377 
    378 void
    379 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
    380 {
    381 	struct ifaddr *ifa;
    382 	struct sockaddr_dl *sdl;
    383 
    384 	ifp->if_addrlen = addrlen;
    385 	if_alloc_sadl(ifp);
    386 	ifa = ifp->if_dl;
    387 	sdl = satosdl(ifa->ifa_addr);
    388 
    389 	(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
    390 	if (factory) {
    391 		ifp->if_hwdl = ifp->if_dl;
    392 		ifaref(ifp->if_hwdl);
    393 	}
    394 	/* TBD routing socket */
    395 }
    396 
    397 struct ifaddr *
    398 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
    399 {
    400 	unsigned socksize, ifasize;
    401 	int addrlen, namelen;
    402 	struct sockaddr_dl *mask, *sdl;
    403 	struct ifaddr *ifa;
    404 
    405 	namelen = strlen(ifp->if_xname);
    406 	addrlen = ifp->if_addrlen;
    407 	socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
    408 	ifasize = sizeof(*ifa) + 2 * socksize;
    409 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
    410 
    411 	sdl = (struct sockaddr_dl *)(ifa + 1);
    412 	mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
    413 
    414 	sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
    415 	    ifp->if_xname, namelen, NULL, addrlen);
    416 	mask->sdl_len = sockaddr_dl_measure(namelen, 0);
    417 	memset(&mask->sdl_data[0], 0xff, namelen);
    418 	ifa->ifa_rtrequest = link_rtrequest;
    419 	ifa->ifa_addr = (struct sockaddr *)sdl;
    420 	ifa->ifa_netmask = (struct sockaddr *)mask;
    421 
    422 	*sdlp = sdl;
    423 
    424 	return ifa;
    425 }
    426 
    427 static void
    428 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
    429 {
    430 	const struct sockaddr_dl *sdl;
    431 
    432 	ifp->if_dl = ifa;
    433 	ifaref(ifa);
    434 	sdl = satosdl(ifa->ifa_addr);
    435 	ifp->if_sadl = sdl;
    436 }
    437 
    438 /*
    439  * Allocate the link level name for the specified interface.  This
    440  * is an attachment helper.  It must be called after ifp->if_addrlen
    441  * is initialized, which may not be the case when if_attach() is
    442  * called.
    443  */
    444 void
    445 if_alloc_sadl(struct ifnet *ifp)
    446 {
    447 	struct ifaddr *ifa;
    448 	const struct sockaddr_dl *sdl;
    449 
    450 	/*
    451 	 * If the interface already has a link name, release it
    452 	 * now.  This is useful for interfaces that can change
    453 	 * link types, and thus switch link names often.
    454 	 */
    455 	if (ifp->if_sadl != NULL)
    456 		if_free_sadl(ifp);
    457 
    458 	ifa = if_dl_create(ifp, &sdl);
    459 
    460 	ifa_insert(ifp, ifa);
    461 	if_sadl_setrefs(ifp, ifa);
    462 }
    463 
    464 static void
    465 if_deactivate_sadl(struct ifnet *ifp)
    466 {
    467 	struct ifaddr *ifa;
    468 
    469 	KASSERT(ifp->if_dl != NULL);
    470 
    471 	ifa = ifp->if_dl;
    472 
    473 	ifp->if_sadl = NULL;
    474 
    475 	ifp->if_dl = NULL;
    476 	ifafree(ifa);
    477 }
    478 
    479 void
    480 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa,
    481     const struct sockaddr_dl *sdl)
    482 {
    483 	int s;
    484 
    485 	s = splnet();
    486 
    487 	if_deactivate_sadl(ifp);
    488 
    489 	if_sadl_setrefs(ifp, ifa);
    490 	IFADDR_FOREACH(ifa, ifp)
    491 		rtinit(ifa, RTM_LLINFO_UPD, 0);
    492 	splx(s);
    493 }
    494 
    495 /*
    496  * Free the link level name for the specified interface.  This is
    497  * a detach helper.  This is called from if_detach().
    498  */
    499 static void
    500 if_free_sadl(struct ifnet *ifp)
    501 {
    502 	struct ifaddr *ifa;
    503 	int s;
    504 
    505 	ifa = ifp->if_dl;
    506 	if (ifa == NULL) {
    507 		KASSERT(ifp->if_sadl == NULL);
    508 		return;
    509 	}
    510 
    511 	KASSERT(ifp->if_sadl != NULL);
    512 
    513 	s = splnet();
    514 	rtinit(ifa, RTM_DELETE, 0);
    515 	ifa_remove(ifp, ifa);
    516 	if_deactivate_sadl(ifp);
    517 	if (ifp->if_hwdl == ifa) {
    518 		ifafree(ifa);
    519 		ifp->if_hwdl = NULL;
    520 	}
    521 	splx(s);
    522 }
    523 
    524 static void
    525 if_getindex(ifnet_t *ifp)
    526 {
    527 	bool hitlimit = false;
    528 
    529 	ifp->if_index_gen = index_gen++;
    530 
    531 	ifp->if_index = if_index;
    532 	if (ifindex2ifnet == NULL) {
    533 		if_index++;
    534 		goto skip;
    535 	}
    536 	while (if_byindex(ifp->if_index)) {
    537 		/*
    538 		 * If we hit USHRT_MAX, we skip back to 0 since
    539 		 * there are a number of places where the value
    540 		 * of if_index or if_index itself is compared
    541 		 * to or stored in an unsigned short.  By
    542 		 * jumping back, we won't botch those assignments
    543 		 * or comparisons.
    544 		 */
    545 		if (++if_index == 0) {
    546 			if_index = 1;
    547 		} else if (if_index == USHRT_MAX) {
    548 			/*
    549 			 * However, if we have to jump back to
    550 			 * zero *twice* without finding an empty
    551 			 * slot in ifindex2ifnet[], then there
    552 			 * there are too many (>65535) interfaces.
    553 			 */
    554 			if (hitlimit) {
    555 				panic("too many interfaces");
    556 			}
    557 			hitlimit = true;
    558 			if_index = 1;
    559 		}
    560 		ifp->if_index = if_index;
    561 	}
    562 skip:
    563 	/*
    564 	 * ifindex2ifnet is indexed by if_index. Since if_index will
    565 	 * grow dynamically, it should grow too.
    566 	 */
    567 	if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
    568 		size_t m, n, oldlim;
    569 		void *q;
    570 
    571 		oldlim = if_indexlim;
    572 		while (ifp->if_index >= if_indexlim)
    573 			if_indexlim <<= 1;
    574 
    575 		/* grow ifindex2ifnet */
    576 		m = oldlim * sizeof(struct ifnet *);
    577 		n = if_indexlim * sizeof(struct ifnet *);
    578 		q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
    579 		if (ifindex2ifnet != NULL) {
    580 			memcpy(q, ifindex2ifnet, m);
    581 			free(ifindex2ifnet, M_IFADDR);
    582 		}
    583 		ifindex2ifnet = (struct ifnet **)q;
    584 	}
    585 	ifindex2ifnet[ifp->if_index] = ifp;
    586 }
    587 
    588 /*
    589  * Initialize an interface and assign an index for it.
    590  *
    591  * It must be called prior to a device specific attach routine
    592  * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
    593  * and be followed by if_register:
    594  *
    595  *     if_initialize(ifp);
    596  *     ether_ifattach(ifp, enaddr);
    597  *     if_register(ifp);
    598  */
    599 void
    600 if_initialize(ifnet_t *ifp)
    601 {
    602 	KASSERT(if_indexlim > 0);
    603 	TAILQ_INIT(&ifp->if_addrlist);
    604 
    605 	/*
    606 	 * Link level name is allocated later by a separate call to
    607 	 * if_alloc_sadl().
    608 	 */
    609 
    610 	if (ifp->if_snd.ifq_maxlen == 0)
    611 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
    612 
    613 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    614 
    615 	ifp->if_link_state = LINK_STATE_UNKNOWN;
    616 	ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
    617 
    618 	ifp->if_capenable = 0;
    619 	ifp->if_csum_flags_tx = 0;
    620 	ifp->if_csum_flags_rx = 0;
    621 
    622 #ifdef ALTQ
    623 	ifp->if_snd.altq_type = 0;
    624 	ifp->if_snd.altq_disc = NULL;
    625 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
    626 	ifp->if_snd.altq_tbr  = NULL;
    627 	ifp->if_snd.altq_ifp  = ifp;
    628 #endif
    629 
    630 #ifdef NET_MPSAFE
    631 	ifp->if_snd.ifq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
    632 #else
    633 	ifp->if_snd.ifq_lock = NULL;
    634 #endif
    635 
    636 	ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
    637 	(void)pfil_run_hooks(if_pfil,
    638 	    (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
    639 
    640 	IF_AFDATA_LOCK_INIT(ifp);
    641 
    642 	ifp->if_link_si = softint_establish(SOFTINT_NET, if_link_state_change_si, ifp);
    643 	if (ifp->if_link_si == NULL)
    644 		panic("%s: softint_establish() failed", __func__);
    645 
    646 	PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
    647 	psref_target_init(&ifp->if_psref, ifnet_psref_class);
    648 	ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    649 
    650 	IFNET_LOCK();
    651 	if_getindex(ifp);
    652 	IFNET_UNLOCK();
    653 }
    654 
    655 /*
    656  * Register an interface to the list of "active" interfaces.
    657  */
    658 void
    659 if_register(ifnet_t *ifp)
    660 {
    661 	/*
    662 	 * If the driver has not supplied its own if_ioctl, then
    663 	 * supply the default.
    664 	 */
    665 	if (ifp->if_ioctl == NULL)
    666 		ifp->if_ioctl = ifioctl_common;
    667 
    668 	sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
    669 
    670 	if (!STAILQ_EMPTY(&domains))
    671 		if_attachdomain1(ifp);
    672 
    673 	/* Announce the interface. */
    674 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
    675 
    676 	if (ifp->if_slowtimo != NULL) {
    677 		ifp->if_slowtimo_ch =
    678 		    kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP);
    679 		callout_init(ifp->if_slowtimo_ch, 0);
    680 		callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp);
    681 		if_slowtimo(ifp);
    682 	}
    683 
    684 	if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
    685 		ifp->if_transmit = if_transmit;
    686 
    687 	IFNET_LOCK();
    688 	TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
    689 	IFNET_WRITER_INSERT_TAIL(ifp);
    690 	IFNET_UNLOCK();
    691 }
    692 
    693 /*
    694  * The if_percpuq framework
    695  *
    696  * It allows network device drivers to execute the network stack
    697  * in softint (so called softint-based if_input). It utilizes
    698  * softint and percpu ifqueue. It doesn't distribute any packets
    699  * between CPUs, unlike pktqueue(9).
    700  *
    701  * Currently we support two options for device drivers to apply the framework:
    702  * - Use it implicitly with less changes
    703  *   - If you use if_attach in driver's _attach function and if_input in
    704  *     driver's Rx interrupt handler, a packet is queued and a softint handles
    705  *     the packet implicitly
    706  * - Use it explicitly in each driver (recommended)
    707  *   - You can use if_percpuq_* directly in your driver
    708  *   - In this case, you need to allocate struct if_percpuq in driver's softc
    709  *   - See wm(4) as a reference implementation
    710  */
    711 
    712 static void
    713 if_percpuq_softint(void *arg)
    714 {
    715 	struct if_percpuq *ipq = arg;
    716 	struct ifnet *ifp = ipq->ipq_ifp;
    717 	struct mbuf *m;
    718 
    719 	while ((m = if_percpuq_dequeue(ipq)) != NULL)
    720 		ifp->_if_input(ifp, m);
    721 }
    722 
    723 static void
    724 if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
    725 {
    726 	struct ifqueue *const ifq = p;
    727 
    728 	memset(ifq, 0, sizeof(*ifq));
    729 	ifq->ifq_maxlen = IFQ_MAXLEN;
    730 }
    731 
    732 struct if_percpuq *
    733 if_percpuq_create(struct ifnet *ifp)
    734 {
    735 	struct if_percpuq *ipq;
    736 
    737 	ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
    738 	if (ipq == NULL)
    739 		panic("kmem_zalloc failed");
    740 
    741 	ipq->ipq_ifp = ifp;
    742 	ipq->ipq_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
    743 	    if_percpuq_softint, ipq);
    744 	ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
    745 	percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
    746 
    747 	sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
    748 
    749 	return ipq;
    750 }
    751 
    752 static struct mbuf *
    753 if_percpuq_dequeue(struct if_percpuq *ipq)
    754 {
    755 	struct mbuf *m;
    756 	struct ifqueue *ifq;
    757 	int s;
    758 
    759 	s = splnet();
    760 	ifq = percpu_getref(ipq->ipq_ifqs);
    761 	IF_DEQUEUE(ifq, m);
    762 	percpu_putref(ipq->ipq_ifqs);
    763 	splx(s);
    764 
    765 	return m;
    766 }
    767 
    768 static void
    769 if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
    770 {
    771 	struct ifqueue *const ifq = p;
    772 
    773 	IF_PURGE(ifq);
    774 }
    775 
    776 void
    777 if_percpuq_destroy(struct if_percpuq *ipq)
    778 {
    779 
    780 	/* if_detach may already destroy it */
    781 	if (ipq == NULL)
    782 		return;
    783 
    784 	softint_disestablish(ipq->ipq_si);
    785 	percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
    786 	percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
    787 }
    788 
    789 void
    790 if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
    791 {
    792 	struct ifqueue *ifq;
    793 	int s;
    794 
    795 	KASSERT(ipq != NULL);
    796 
    797 	s = splnet();
    798 	ifq = percpu_getref(ipq->ipq_ifqs);
    799 	if (IF_QFULL(ifq)) {
    800 		IF_DROP(ifq);
    801 		percpu_putref(ipq->ipq_ifqs);
    802 		m_freem(m);
    803 		goto out;
    804 	}
    805 	IF_ENQUEUE(ifq, m);
    806 	percpu_putref(ipq->ipq_ifqs);
    807 
    808 	softint_schedule(ipq->ipq_si);
    809 out:
    810 	splx(s);
    811 }
    812 
    813 static void
    814 if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
    815 {
    816 	struct ifqueue *const ifq = p;
    817 	int *sum = arg;
    818 
    819 	*sum += ifq->ifq_drops;
    820 }
    821 
    822 static int
    823 sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
    824 {
    825 	struct sysctlnode node;
    826 	struct if_percpuq *ipq;
    827 	int sum = 0;
    828 	int error;
    829 
    830 	node = *rnode;
    831 	ipq = node.sysctl_data;
    832 
    833 	percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
    834 
    835 	node.sysctl_data = &sum;
    836 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    837 	if (error != 0 || newp == NULL)
    838 		return error;
    839 
    840 	return 0;
    841 }
    842 
    843 static void
    844 sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
    845     struct if_percpuq *ipq)
    846 {
    847 	const struct sysctlnode *cnode, *rnode;
    848 
    849 	if (sysctl_createv(clog, 0, NULL, &rnode,
    850 		       CTLFLAG_PERMANENT,
    851 		       CTLTYPE_NODE, "interfaces",
    852 		       SYSCTL_DESCR("Per-interface controls"),
    853 		       NULL, 0, NULL, 0,
    854 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
    855 		goto bad;
    856 
    857 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    858 		       CTLFLAG_PERMANENT,
    859 		       CTLTYPE_NODE, ifname,
    860 		       SYSCTL_DESCR("Interface controls"),
    861 		       NULL, 0, NULL, 0,
    862 		       CTL_CREATE, CTL_EOL) != 0)
    863 		goto bad;
    864 
    865 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    866 		       CTLFLAG_PERMANENT,
    867 		       CTLTYPE_NODE, "rcvq",
    868 		       SYSCTL_DESCR("Interface input queue controls"),
    869 		       NULL, 0, NULL, 0,
    870 		       CTL_CREATE, CTL_EOL) != 0)
    871 		goto bad;
    872 
    873 #ifdef NOTYET
    874 	/* XXX Should show each per-CPU queue length? */
    875 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    876 		       CTLFLAG_PERMANENT,
    877 		       CTLTYPE_INT, "len",
    878 		       SYSCTL_DESCR("Current input queue length"),
    879 		       sysctl_percpuq_len, 0, NULL, 0,
    880 		       CTL_CREATE, CTL_EOL) != 0)
    881 		goto bad;
    882 
    883 	if (sysctl_createv(clog, 0, &rnode, &cnode,
    884 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    885 		       CTLTYPE_INT, "maxlen",
    886 		       SYSCTL_DESCR("Maximum allowed input queue length"),
    887 		       sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
    888 		       CTL_CREATE, CTL_EOL) != 0)
    889 		goto bad;
    890 #endif
    891 
    892 	if (sysctl_createv(clog, 0, &rnode, &cnode,
    893 		       CTLFLAG_PERMANENT,
    894 		       CTLTYPE_INT, "drops",
    895 		       SYSCTL_DESCR("Total packets dropped due to full input queue"),
    896 		       sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
    897 		       CTL_CREATE, CTL_EOL) != 0)
    898 		goto bad;
    899 
    900 	return;
    901 bad:
    902 	printf("%s: could not attach sysctl nodes\n", ifname);
    903 	return;
    904 }
    905 
    906 
    907 /*
    908  * The common interface input routine that is called by device drivers,
    909  * which should be used only when the driver's rx handler already runs
    910  * in softint.
    911  */
    912 void
    913 if_input(struct ifnet *ifp, struct mbuf *m)
    914 {
    915 
    916 	KASSERT(ifp->if_percpuq == NULL);
    917 	KASSERT(!cpu_intr_p());
    918 
    919 	ifp->_if_input(ifp, m);
    920 }
    921 
    922 /*
    923  * DEPRECATED. Use if_initialize and if_register instead.
    924  * See the above comment of if_initialize.
    925  *
    926  * Note that it implicitly enables if_percpuq to make drivers easy to
    927  * migrate softint-based if_input without much changes. If you don't
    928  * want to enable it, use if_initialize instead.
    929  */
    930 void
    931 if_attach(ifnet_t *ifp)
    932 {
    933 
    934 	if_initialize(ifp);
    935 	ifp->if_percpuq = if_percpuq_create(ifp);
    936 	if_register(ifp);
    937 }
    938 
    939 void
    940 if_attachdomain(void)
    941 {
    942 	struct ifnet *ifp;
    943 	int s;
    944 	int bound = curlwp_bind();
    945 
    946 	s = pserialize_read_enter();
    947 	IFNET_READER_FOREACH(ifp) {
    948 		struct psref psref;
    949 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
    950 		pserialize_read_exit(s);
    951 		if_attachdomain1(ifp);
    952 		s = pserialize_read_enter();
    953 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
    954 	}
    955 	pserialize_read_exit(s);
    956 	curlwp_bindx(bound);
    957 }
    958 
    959 static void
    960 if_attachdomain1(struct ifnet *ifp)
    961 {
    962 	struct domain *dp;
    963 	int s;
    964 
    965 	s = splnet();
    966 
    967 	/* address family dependent data region */
    968 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
    969 	DOMAIN_FOREACH(dp) {
    970 		if (dp->dom_ifattach != NULL)
    971 			ifp->if_afdata[dp->dom_family] =
    972 			    (*dp->dom_ifattach)(ifp);
    973 	}
    974 
    975 	splx(s);
    976 }
    977 
    978 /*
    979  * Deactivate an interface.  This points all of the procedure
    980  * handles at error stubs.  May be called from interrupt context.
    981  */
    982 void
    983 if_deactivate(struct ifnet *ifp)
    984 {
    985 	int s;
    986 
    987 	s = splnet();
    988 
    989 	ifp->if_output	 = if_nulloutput;
    990 	ifp->_if_input	 = if_nullinput;
    991 	ifp->if_start	 = if_nullstart;
    992 	ifp->if_transmit = if_nulltransmit;
    993 	ifp->if_ioctl	 = if_nullioctl;
    994 	ifp->if_init	 = if_nullinit;
    995 	ifp->if_stop	 = if_nullstop;
    996 	ifp->if_slowtimo = if_nullslowtimo;
    997 	ifp->if_drain	 = if_nulldrain;
    998 
    999 	/* No more packets may be enqueued. */
   1000 	ifp->if_snd.ifq_maxlen = 0;
   1001 
   1002 	splx(s);
   1003 }
   1004 
   1005 void
   1006 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
   1007 {
   1008 	struct ifaddr *ifa, *nifa;
   1009 
   1010 	IFADDR_FOREACH_SAFE(ifa, ifp, nifa) {
   1011 		if (ifa->ifa_addr->sa_family != family)
   1012 			continue;
   1013 		(*purgeaddr)(ifa);
   1014 	}
   1015 }
   1016 
   1017 /*
   1018  * Detach an interface from the list of "active" interfaces,
   1019  * freeing any resources as we go along.
   1020  *
   1021  * NOTE: This routine must be called with a valid thread context,
   1022  * as it may block.
   1023  */
   1024 void
   1025 if_detach(struct ifnet *ifp)
   1026 {
   1027 	struct socket so;
   1028 	struct ifaddr *ifa;
   1029 #ifdef IFAREF_DEBUG
   1030 	struct ifaddr *last_ifa = NULL;
   1031 #endif
   1032 	struct domain *dp;
   1033 	const struct protosw *pr;
   1034 	int s, i, family, purged;
   1035 	uint64_t xc;
   1036 
   1037 	/*
   1038 	 * XXX It's kind of lame that we have to have the
   1039 	 * XXX socket structure...
   1040 	 */
   1041 	memset(&so, 0, sizeof(so));
   1042 
   1043 	s = splnet();
   1044 
   1045 	sysctl_teardown(&ifp->if_sysctl_log);
   1046 	mutex_enter(ifp->if_ioctl_lock);
   1047 	ifp->if_ioctl = if_nullioctl;
   1048 	mutex_exit(ifp->if_ioctl_lock);
   1049 
   1050 	IFNET_LOCK();
   1051 	ifindex2ifnet[ifp->if_index] = NULL;
   1052 	TAILQ_REMOVE(&ifnet_list, ifp, if_list);
   1053 	IFNET_WRITER_REMOVE(ifp);
   1054 	pserialize_perform(ifnet_psz);
   1055 	IFNET_UNLOCK();
   1056 
   1057 	mutex_obj_free(ifp->if_ioctl_lock);
   1058 	ifp->if_ioctl_lock = NULL;
   1059 
   1060 	if (ifp->if_slowtimo != NULL) {
   1061 		ifp->if_slowtimo = NULL;
   1062 		callout_halt(ifp->if_slowtimo_ch, NULL);
   1063 		callout_destroy(ifp->if_slowtimo_ch);
   1064 		kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
   1065 	}
   1066 
   1067 	/*
   1068 	 * Do an if_down() to give protocols a chance to do something.
   1069 	 */
   1070 	if_down(ifp);
   1071 
   1072 #ifdef ALTQ
   1073 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
   1074 		altq_disable(&ifp->if_snd);
   1075 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
   1076 		altq_detach(&ifp->if_snd);
   1077 #endif
   1078 
   1079 	if (ifp->if_snd.ifq_lock)
   1080 		mutex_obj_free(ifp->if_snd.ifq_lock);
   1081 
   1082 #if NCARP > 0
   1083 	/* Remove the interface from any carp group it is a part of.  */
   1084 	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
   1085 		carp_ifdetach(ifp);
   1086 #endif
   1087 
   1088 	/*
   1089 	 * Rip all the addresses off the interface.  This should make
   1090 	 * all of the routes go away.
   1091 	 *
   1092 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
   1093 	 * from the list, including our "cursor", ifa.  For safety,
   1094 	 * and to honor the TAILQ abstraction, I just restart the
   1095 	 * loop after each removal.  Note that the loop will exit
   1096 	 * when all of the remaining ifaddrs belong to the AF_LINK
   1097 	 * family.  I am counting on the historical fact that at
   1098 	 * least one pr_usrreq in each address domain removes at
   1099 	 * least one ifaddr.
   1100 	 */
   1101 again:
   1102 	IFADDR_FOREACH(ifa, ifp) {
   1103 		family = ifa->ifa_addr->sa_family;
   1104 #ifdef IFAREF_DEBUG
   1105 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
   1106 		    ifa, family, ifa->ifa_refcnt);
   1107 		if (last_ifa != NULL && ifa == last_ifa)
   1108 			panic("if_detach: loop detected");
   1109 		last_ifa = ifa;
   1110 #endif
   1111 		if (family == AF_LINK)
   1112 			continue;
   1113 		dp = pffinddomain(family);
   1114 #ifdef DIAGNOSTIC
   1115 		if (dp == NULL)
   1116 			panic("if_detach: no domain for AF %d",
   1117 			    family);
   1118 #endif
   1119 		/*
   1120 		 * XXX These PURGEIF calls are redundant with the
   1121 		 * purge-all-families calls below, but are left in for
   1122 		 * now both to make a smaller change, and to avoid
   1123 		 * unplanned interactions with clearing of
   1124 		 * ifp->if_addrlist.
   1125 		 */
   1126 		purged = 0;
   1127 		for (pr = dp->dom_protosw;
   1128 		     pr < dp->dom_protoswNPROTOSW; pr++) {
   1129 			so.so_proto = pr;
   1130 			if (pr->pr_usrreqs) {
   1131 				(void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
   1132 				purged = 1;
   1133 			}
   1134 		}
   1135 		if (purged == 0) {
   1136 			/*
   1137 			 * XXX What's really the best thing to do
   1138 			 * XXX here?  --thorpej (at) NetBSD.org
   1139 			 */
   1140 			printf("if_detach: WARNING: AF %d not purged\n",
   1141 			    family);
   1142 			ifa_remove(ifp, ifa);
   1143 		}
   1144 		goto again;
   1145 	}
   1146 
   1147 	if_free_sadl(ifp);
   1148 
   1149 	/* Walk the routing table looking for stragglers. */
   1150 	for (i = 0; i <= AF_MAX; i++) {
   1151 		while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
   1152 			continue;
   1153 	}
   1154 
   1155 	DOMAIN_FOREACH(dp) {
   1156 		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
   1157 		{
   1158 			void *p = ifp->if_afdata[dp->dom_family];
   1159 			if (p) {
   1160 				ifp->if_afdata[dp->dom_family] = NULL;
   1161 				(*dp->dom_ifdetach)(ifp, p);
   1162 			}
   1163 		}
   1164 
   1165 		/*
   1166 		 * One would expect multicast memberships (INET and
   1167 		 * INET6) on UDP sockets to be purged by the PURGEIF
   1168 		 * calls above, but if all addresses were removed from
   1169 		 * the interface prior to destruction, the calls will
   1170 		 * not be made (e.g. ppp, for which pppd(8) generally
   1171 		 * removes addresses before destroying the interface).
   1172 		 * Because there is no invariant that multicast
   1173 		 * memberships only exist for interfaces with IPv4
   1174 		 * addresses, we must call PURGEIF regardless of
   1175 		 * addresses.  (Protocols which might store ifnet
   1176 		 * pointers are marked with PR_PURGEIF.)
   1177 		 */
   1178 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
   1179 			so.so_proto = pr;
   1180 			if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
   1181 				(void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
   1182 		}
   1183 	}
   1184 
   1185 	(void)pfil_run_hooks(if_pfil,
   1186 	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
   1187 	(void)pfil_head_destroy(ifp->if_pfil);
   1188 
   1189 	/* Announce that the interface is gone. */
   1190 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
   1191 
   1192 	IF_AFDATA_LOCK_DESTROY(ifp);
   1193 
   1194 	softint_disestablish(ifp->if_link_si);
   1195 	ifp->if_link_si = NULL;
   1196 
   1197 	/*
   1198 	 * remove packets that came from ifp, from software interrupt queues.
   1199 	 */
   1200 	DOMAIN_FOREACH(dp) {
   1201 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
   1202 			struct ifqueue *iq = dp->dom_ifqueues[i];
   1203 			if (iq == NULL)
   1204 				break;
   1205 			dp->dom_ifqueues[i] = NULL;
   1206 			if_detach_queues(ifp, iq);
   1207 		}
   1208 	}
   1209 
   1210 	/*
   1211 	 * IP queues have to be processed separately: net-queue barrier
   1212 	 * ensures that the packets are dequeued while a cross-call will
   1213 	 * ensure that the interrupts have completed. FIXME: not quite..
   1214 	 */
   1215 #ifdef INET
   1216 	pktq_barrier(ip_pktq);
   1217 #endif
   1218 #ifdef INET6
   1219 	if (in6_present)
   1220 		pktq_barrier(ip6_pktq);
   1221 #endif
   1222 	xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
   1223 	xc_wait(xc);
   1224 
   1225 	/* Wait for all readers to drain before freeing.  */
   1226 	psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
   1227 	PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
   1228 
   1229 	if (ifp->if_percpuq != NULL) {
   1230 		if_percpuq_destroy(ifp->if_percpuq);
   1231 		ifp->if_percpuq = NULL;
   1232 	}
   1233 
   1234 	splx(s);
   1235 }
   1236 
   1237 static void
   1238 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
   1239 {
   1240 	struct mbuf *m, *prev, *next;
   1241 
   1242 	prev = NULL;
   1243 	for (m = q->ifq_head; m != NULL; m = next) {
   1244 		KASSERT((m->m_flags & M_PKTHDR) != 0);
   1245 
   1246 		next = m->m_nextpkt;
   1247 		if (m->m_pkthdr.rcvif_index != ifp->if_index) {
   1248 			prev = m;
   1249 			continue;
   1250 		}
   1251 
   1252 		if (prev != NULL)
   1253 			prev->m_nextpkt = m->m_nextpkt;
   1254 		else
   1255 			q->ifq_head = m->m_nextpkt;
   1256 		if (q->ifq_tail == m)
   1257 			q->ifq_tail = prev;
   1258 		q->ifq_len--;
   1259 
   1260 		m->m_nextpkt = NULL;
   1261 		m_freem(m);
   1262 		IF_DROP(q);
   1263 	}
   1264 }
   1265 
   1266 /*
   1267  * Callback for a radix tree walk to delete all references to an
   1268  * ifnet.
   1269  */
   1270 static int
   1271 if_rt_walktree(struct rtentry *rt, void *v)
   1272 {
   1273 	struct ifnet *ifp = (struct ifnet *)v;
   1274 	int error;
   1275 	struct rtentry *retrt;
   1276 
   1277 	if (rt->rt_ifp != ifp)
   1278 		return 0;
   1279 
   1280 	/* Delete the entry. */
   1281 	error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
   1282 	    rt_mask(rt), rt->rt_flags, &retrt);
   1283 	if (error == 0) {
   1284 		KASSERT(retrt == rt);
   1285 		KASSERT((retrt->rt_flags & RTF_UP) == 0);
   1286 		retrt->rt_ifp = NULL;
   1287 		rtfree(retrt);
   1288 	} else {
   1289 		printf("%s: warning: unable to delete rtentry @ %p, "
   1290 		    "error = %d\n", ifp->if_xname, rt, error);
   1291 	}
   1292 	return ERESTART;
   1293 }
   1294 
   1295 /*
   1296  * Create a clone network interface.
   1297  */
   1298 static int
   1299 if_clone_create(const char *name)
   1300 {
   1301 	struct if_clone *ifc;
   1302 	int unit;
   1303 	struct ifnet *ifp;
   1304 	struct psref psref;
   1305 
   1306 	ifc = if_clone_lookup(name, &unit);
   1307 	if (ifc == NULL)
   1308 		return EINVAL;
   1309 
   1310 	ifp = if_get(name, &psref);
   1311 	if (ifp != NULL) {
   1312 		if_put(ifp, &psref);
   1313 		return EEXIST;
   1314 	}
   1315 
   1316 	return (*ifc->ifc_create)(ifc, unit);
   1317 }
   1318 
   1319 /*
   1320  * Destroy a clone network interface.
   1321  */
   1322 static int
   1323 if_clone_destroy(const char *name)
   1324 {
   1325 	struct if_clone *ifc;
   1326 	struct ifnet *ifp;
   1327 	struct psref psref;
   1328 
   1329 	ifc = if_clone_lookup(name, NULL);
   1330 	if (ifc == NULL)
   1331 		return EINVAL;
   1332 
   1333 	if (ifc->ifc_destroy == NULL)
   1334 		return EOPNOTSUPP;
   1335 
   1336 	ifp = if_get(name, &psref);
   1337 	if (ifp == NULL)
   1338 		return ENXIO;
   1339 
   1340 	/* We have to disable ioctls here */
   1341 	mutex_enter(ifp->if_ioctl_lock);
   1342 	ifp->if_ioctl = if_nullioctl;
   1343 	mutex_exit(ifp->if_ioctl_lock);
   1344 
   1345 	/*
   1346 	 * We cannot call ifc_destroy with holding ifp.
   1347 	 * Releasing ifp here is safe thanks to if_clone_mtx.
   1348 	 */
   1349 	if_put(ifp, &psref);
   1350 
   1351 	return (*ifc->ifc_destroy)(ifp);
   1352 }
   1353 
   1354 /*
   1355  * Look up a network interface cloner.
   1356  */
   1357 static struct if_clone *
   1358 if_clone_lookup(const char *name, int *unitp)
   1359 {
   1360 	struct if_clone *ifc;
   1361 	const char *cp;
   1362 	char *dp, ifname[IFNAMSIZ + 3];
   1363 	int unit;
   1364 
   1365 	strcpy(ifname, "if_");
   1366 	/* separate interface name from unit */
   1367 	for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
   1368 	    *cp && (*cp < '0' || *cp > '9');)
   1369 		*dp++ = *cp++;
   1370 
   1371 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
   1372 		return NULL;	/* No name or unit number */
   1373 	*dp++ = '\0';
   1374 
   1375 again:
   1376 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
   1377 		if (strcmp(ifname + 3, ifc->ifc_name) == 0)
   1378 			break;
   1379 	}
   1380 
   1381 	if (ifc == NULL) {
   1382 		if (*ifname == '\0' ||
   1383 		    module_autoload(ifname, MODULE_CLASS_DRIVER))
   1384 			return NULL;
   1385 		*ifname = '\0';
   1386 		goto again;
   1387 	}
   1388 
   1389 	unit = 0;
   1390 	while (cp - name < IFNAMSIZ && *cp) {
   1391 		if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
   1392 			/* Bogus unit number. */
   1393 			return NULL;
   1394 		}
   1395 		unit = (unit * 10) + (*cp++ - '0');
   1396 	}
   1397 
   1398 	if (unitp != NULL)
   1399 		*unitp = unit;
   1400 	return ifc;
   1401 }
   1402 
   1403 /*
   1404  * Register a network interface cloner.
   1405  */
   1406 void
   1407 if_clone_attach(struct if_clone *ifc)
   1408 {
   1409 
   1410 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
   1411 	if_cloners_count++;
   1412 }
   1413 
   1414 /*
   1415  * Unregister a network interface cloner.
   1416  */
   1417 void
   1418 if_clone_detach(struct if_clone *ifc)
   1419 {
   1420 
   1421 	LIST_REMOVE(ifc, ifc_list);
   1422 	if_cloners_count--;
   1423 }
   1424 
   1425 /*
   1426  * Provide list of interface cloners to userspace.
   1427  */
   1428 int
   1429 if_clone_list(int buf_count, char *buffer, int *total)
   1430 {
   1431 	char outbuf[IFNAMSIZ], *dst;
   1432 	struct if_clone *ifc;
   1433 	int count, error = 0;
   1434 
   1435 	*total = if_cloners_count;
   1436 	if ((dst = buffer) == NULL) {
   1437 		/* Just asking how many there are. */
   1438 		return 0;
   1439 	}
   1440 
   1441 	if (buf_count < 0)
   1442 		return EINVAL;
   1443 
   1444 	count = (if_cloners_count < buf_count) ?
   1445 	    if_cloners_count : buf_count;
   1446 
   1447 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
   1448 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
   1449 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
   1450 		if (outbuf[sizeof(outbuf) - 1] != '\0')
   1451 			return ENAMETOOLONG;
   1452 		error = copyout(outbuf, dst, sizeof(outbuf));
   1453 		if (error != 0)
   1454 			break;
   1455 	}
   1456 
   1457 	return error;
   1458 }
   1459 
   1460 void
   1461 ifaref(struct ifaddr *ifa)
   1462 {
   1463 	ifa->ifa_refcnt++;
   1464 }
   1465 
   1466 void
   1467 ifafree(struct ifaddr *ifa)
   1468 {
   1469 	KASSERT(ifa != NULL);
   1470 	KASSERT(ifa->ifa_refcnt > 0);
   1471 
   1472 	if (--ifa->ifa_refcnt == 0) {
   1473 		free(ifa, M_IFADDR);
   1474 	}
   1475 }
   1476 
   1477 void
   1478 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
   1479 {
   1480 	ifa->ifa_ifp = ifp;
   1481 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
   1482 	ifaref(ifa);
   1483 }
   1484 
   1485 void
   1486 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
   1487 {
   1488 	KASSERT(ifa->ifa_ifp == ifp);
   1489 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
   1490 	ifafree(ifa);
   1491 }
   1492 
   1493 static inline int
   1494 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
   1495 {
   1496 	return sockaddr_cmp(sa1, sa2) == 0;
   1497 }
   1498 
   1499 /*
   1500  * Locate an interface based on a complete address.
   1501  */
   1502 /*ARGSUSED*/
   1503 struct ifaddr *
   1504 ifa_ifwithaddr(const struct sockaddr *addr)
   1505 {
   1506 	struct ifnet *ifp;
   1507 	struct ifaddr *ifa;
   1508 	int s;
   1509 
   1510 	s = pserialize_read_enter();
   1511 	IFNET_READER_FOREACH(ifp) {
   1512 		if (ifp->if_output == if_nulloutput)
   1513 			continue;
   1514 		IFADDR_FOREACH(ifa, ifp) {
   1515 			if (ifa->ifa_addr->sa_family != addr->sa_family)
   1516 				continue;
   1517 			if (equal(addr, ifa->ifa_addr))
   1518 				return ifa;
   1519 			if ((ifp->if_flags & IFF_BROADCAST) &&
   1520 			    ifa->ifa_broadaddr &&
   1521 			    /* IP6 doesn't have broadcast */
   1522 			    ifa->ifa_broadaddr->sa_len != 0 &&
   1523 			    equal(ifa->ifa_broadaddr, addr))
   1524 				return ifa;
   1525 		}
   1526 	}
   1527 	pserialize_read_exit(s);
   1528 	return NULL;
   1529 }
   1530 
   1531 /*
   1532  * Locate the point to point interface with a given destination address.
   1533  */
   1534 /*ARGSUSED*/
   1535 struct ifaddr *
   1536 ifa_ifwithdstaddr(const struct sockaddr *addr)
   1537 {
   1538 	struct ifnet *ifp;
   1539 	struct ifaddr *ifa;
   1540 	int s;
   1541 
   1542 	s = pserialize_read_enter();
   1543 	IFNET_READER_FOREACH(ifp) {
   1544 		if (ifp->if_output == if_nulloutput)
   1545 			continue;
   1546 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   1547 			continue;
   1548 		IFADDR_FOREACH(ifa, ifp) {
   1549 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
   1550 			    ifa->ifa_dstaddr == NULL)
   1551 				continue;
   1552 			if (equal(addr, ifa->ifa_dstaddr))
   1553 				return ifa;
   1554 		}
   1555 	}
   1556 	pserialize_read_exit(s);
   1557 	return NULL;
   1558 }
   1559 
   1560 /*
   1561  * Find an interface on a specific network.  If many, choice
   1562  * is most specific found.
   1563  */
   1564 struct ifaddr *
   1565 ifa_ifwithnet(const struct sockaddr *addr)
   1566 {
   1567 	struct ifnet *ifp;
   1568 	struct ifaddr *ifa;
   1569 	const struct sockaddr_dl *sdl;
   1570 	struct ifaddr *ifa_maybe = 0;
   1571 	u_int af = addr->sa_family;
   1572 	const char *addr_data = addr->sa_data, *cplim;
   1573 	int s;
   1574 
   1575 	if (af == AF_LINK) {
   1576 		sdl = satocsdl(addr);
   1577 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
   1578 		    ifindex2ifnet[sdl->sdl_index] &&
   1579 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) {
   1580 			return ifindex2ifnet[sdl->sdl_index]->if_dl;
   1581 		}
   1582 	}
   1583 #ifdef NETATALK
   1584 	if (af == AF_APPLETALK) {
   1585 		const struct sockaddr_at *sat, *sat2;
   1586 		sat = (const struct sockaddr_at *)addr;
   1587 		s = pserialize_read_enter();
   1588 		IFNET_READER_FOREACH(ifp) {
   1589 			if (ifp->if_output == if_nulloutput)
   1590 				continue;
   1591 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
   1592 			if (ifa == NULL)
   1593 				continue;
   1594 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
   1595 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
   1596 				return ifa; /* exact match */
   1597 			if (ifa_maybe == NULL) {
   1598 				/* else keep the if with the right range */
   1599 				ifa_maybe = ifa;
   1600 			}
   1601 		}
   1602 		pserialize_read_exit(s);
   1603 		return ifa_maybe;
   1604 	}
   1605 #endif
   1606 	s = pserialize_read_enter();
   1607 	IFNET_READER_FOREACH(ifp) {
   1608 		if (ifp->if_output == if_nulloutput)
   1609 			continue;
   1610 		IFADDR_FOREACH(ifa, ifp) {
   1611 			const char *cp, *cp2, *cp3;
   1612 
   1613 			if (ifa->ifa_addr->sa_family != af ||
   1614 			    ifa->ifa_netmask == NULL)
   1615  next:				continue;
   1616 			cp = addr_data;
   1617 			cp2 = ifa->ifa_addr->sa_data;
   1618 			cp3 = ifa->ifa_netmask->sa_data;
   1619 			cplim = (const char *)ifa->ifa_netmask +
   1620 			    ifa->ifa_netmask->sa_len;
   1621 			while (cp3 < cplim) {
   1622 				if ((*cp++ ^ *cp2++) & *cp3++) {
   1623 					/* want to continue for() loop */
   1624 					goto next;
   1625 				}
   1626 			}
   1627 			if (ifa_maybe == NULL ||
   1628 			    rt_refines(ifa->ifa_netmask,
   1629 			               ifa_maybe->ifa_netmask))
   1630 				ifa_maybe = ifa;
   1631 		}
   1632 	}
   1633 	pserialize_read_exit(s);
   1634 	return ifa_maybe;
   1635 }
   1636 
   1637 /*
   1638  * Find the interface of the addresss.
   1639  */
   1640 struct ifaddr *
   1641 ifa_ifwithladdr(const struct sockaddr *addr)
   1642 {
   1643 	struct ifaddr *ia;
   1644 
   1645 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
   1646 	    (ia = ifa_ifwithnet(addr)))
   1647 		return ia;
   1648 	return NULL;
   1649 }
   1650 
   1651 /*
   1652  * Find an interface using a specific address family
   1653  */
   1654 struct ifaddr *
   1655 ifa_ifwithaf(int af)
   1656 {
   1657 	struct ifnet *ifp;
   1658 	struct ifaddr *ifa = NULL;
   1659 	int s;
   1660 
   1661 	s = pserialize_read_enter();
   1662 	IFNET_READER_FOREACH(ifp) {
   1663 		if (ifp->if_output == if_nulloutput)
   1664 			continue;
   1665 		IFADDR_FOREACH(ifa, ifp) {
   1666 			if (ifa->ifa_addr->sa_family == af)
   1667 				goto out;
   1668 		}
   1669 	}
   1670 out:
   1671 	pserialize_read_exit(s);
   1672 	return ifa;
   1673 }
   1674 
   1675 /*
   1676  * Find an interface address specific to an interface best matching
   1677  * a given address.
   1678  */
   1679 struct ifaddr *
   1680 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
   1681 {
   1682 	struct ifaddr *ifa;
   1683 	const char *cp, *cp2, *cp3;
   1684 	const char *cplim;
   1685 	struct ifaddr *ifa_maybe = 0;
   1686 	u_int af = addr->sa_family;
   1687 
   1688 	if (ifp->if_output == if_nulloutput)
   1689 		return NULL;
   1690 
   1691 	if (af >= AF_MAX)
   1692 		return NULL;
   1693 
   1694 	IFADDR_FOREACH(ifa, ifp) {
   1695 		if (ifa->ifa_addr->sa_family != af)
   1696 			continue;
   1697 		ifa_maybe = ifa;
   1698 		if (ifa->ifa_netmask == NULL) {
   1699 			if (equal(addr, ifa->ifa_addr) ||
   1700 			    (ifa->ifa_dstaddr &&
   1701 			     equal(addr, ifa->ifa_dstaddr)))
   1702 				return ifa;
   1703 			continue;
   1704 		}
   1705 		cp = addr->sa_data;
   1706 		cp2 = ifa->ifa_addr->sa_data;
   1707 		cp3 = ifa->ifa_netmask->sa_data;
   1708 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
   1709 		for (; cp3 < cplim; cp3++) {
   1710 			if ((*cp++ ^ *cp2++) & *cp3)
   1711 				break;
   1712 		}
   1713 		if (cp3 == cplim)
   1714 			return ifa;
   1715 	}
   1716 	return ifa_maybe;
   1717 }
   1718 
   1719 /*
   1720  * Default action when installing a route with a Link Level gateway.
   1721  * Lookup an appropriate real ifa to point to.
   1722  * This should be moved to /sys/net/link.c eventually.
   1723  */
   1724 void
   1725 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
   1726 {
   1727 	struct ifaddr *ifa;
   1728 	const struct sockaddr *dst;
   1729 	struct ifnet *ifp;
   1730 
   1731 	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
   1732 	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
   1733 		return;
   1734 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
   1735 		rt_replace_ifa(rt, ifa);
   1736 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
   1737 			ifa->ifa_rtrequest(cmd, rt, info);
   1738 	}
   1739 }
   1740 
   1741 /*
   1742  * bitmask macros to manage a densely packed link_state change queue.
   1743  * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
   1744  * LINK_STATE_UP(2) we need 2 bits for each state change.
   1745  * As a state change to store is 0, treat all bits set as an unset item.
   1746  */
   1747 #define LQ_ITEM_BITS		2
   1748 #define LQ_ITEM_MASK		((1 << LQ_ITEM_BITS) - 1)
   1749 #define LQ_MASK(i)		(LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
   1750 #define LINK_STATE_UNSET	LQ_ITEM_MASK
   1751 #define LQ_ITEM(q, i)		(((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
   1752 #define LQ_STORE(q, i, v)						      \
   1753 	do {								      \
   1754 		(q) &= ~LQ_MASK((i));					      \
   1755 		(q) |= (v) << (i) * LQ_ITEM_BITS;			      \
   1756 	} while (0 /* CONSTCOND */)
   1757 #define LQ_MAX(q)		((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
   1758 #define LQ_POP(q, v)							      \
   1759 	do {								      \
   1760 		(v) = LQ_ITEM((q), 0);					      \
   1761 		(q) >>= LQ_ITEM_BITS;					      \
   1762 		(q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;  \
   1763 	} while (0 /* CONSTCOND */)
   1764 #define LQ_PUSH(q, v)							      \
   1765 	do {								      \
   1766 		(q) >>= LQ_ITEM_BITS;					      \
   1767 		(q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;		      \
   1768 	} while (0 /* CONSTCOND */)
   1769 #define LQ_FIND_UNSET(q, i)						      \
   1770 	for ((i) = 0; i < LQ_MAX((q)); (i)++) {				      \
   1771 		if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET)		      \
   1772 			break;						      \
   1773 	}
   1774 /*
   1775  * Handle a change in the interface link state and
   1776  * queue notifications.
   1777  */
   1778 void
   1779 if_link_state_change(struct ifnet *ifp, int link_state)
   1780 {
   1781 	int s, idx;
   1782 
   1783 	/* Ensure change is to a valid state */
   1784 	switch (link_state) {
   1785 	case LINK_STATE_UNKNOWN:	/* FALLTHROUGH */
   1786 	case LINK_STATE_DOWN:		/* FALLTHROUGH */
   1787 	case LINK_STATE_UP:
   1788 		break;
   1789 	default:
   1790 #ifdef DEBUG
   1791 		printf("%s: invalid link state %d\n",
   1792 		    ifp->if_xname, link_state);
   1793 #endif
   1794 		return;
   1795 	}
   1796 
   1797 	s = splnet();
   1798 
   1799 	/* Find the last unset event in the queue. */
   1800 	LQ_FIND_UNSET(ifp->if_link_queue, idx);
   1801 
   1802 	/*
   1803 	 * Ensure link_state doesn't match the last event in the queue.
   1804 	 * ifp->if_link_state is not checked and set here because
   1805 	 * that would present an inconsistent picture to the system.
   1806 	 */
   1807 	if (idx != 0 &&
   1808 	    LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
   1809 		goto out;
   1810 
   1811 	/* Handle queue overflow. */
   1812 	if (idx == LQ_MAX(ifp->if_link_queue)) {
   1813 		uint8_t lost;
   1814 
   1815 		/*
   1816 		 * The DOWN state must be protected from being pushed off
   1817 		 * the queue to ensure that userland will always be
   1818 		 * in a sane state.
   1819 		 * Because DOWN is protected, there is no need to protect
   1820 		 * UNKNOWN.
   1821 		 * It should be invalid to change from any other state to
   1822 		 * UNKNOWN anyway ...
   1823 		 */
   1824 		lost = LQ_ITEM(ifp->if_link_queue, 0);
   1825 		LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
   1826 		if (lost == LINK_STATE_DOWN) {
   1827 			lost = LQ_ITEM(ifp->if_link_queue, 0);
   1828 			LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
   1829 		}
   1830 		printf("%s: lost link state change %s\n",
   1831 		    ifp->if_xname,
   1832 		    lost == LINK_STATE_UP ? "UP" :
   1833 		    lost == LINK_STATE_DOWN ? "DOWN" :
   1834 		    "UNKNOWN");
   1835 	} else
   1836 		LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
   1837 
   1838 	softint_schedule(ifp->if_link_si);
   1839 
   1840 out:
   1841 	splx(s);
   1842 }
   1843 
   1844 /*
   1845  * Handle interface link state change notifications.
   1846  * Must be called at splnet().
   1847  */
   1848 static void
   1849 if_link_state_change0(struct ifnet *ifp, int link_state)
   1850 {
   1851 	struct domain *dp;
   1852 
   1853 	/* Ensure the change is still valid. */
   1854 	if (ifp->if_link_state == link_state)
   1855 		return;
   1856 
   1857 #ifdef DEBUG
   1858 	log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
   1859 		link_state == LINK_STATE_UP ? "UP" :
   1860 		link_state == LINK_STATE_DOWN ? "DOWN" :
   1861 		"UNKNOWN",
   1862 		ifp->if_link_state == LINK_STATE_UP ? "UP" :
   1863 		ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
   1864 		"UNKNOWN");
   1865 #endif
   1866 
   1867 	/*
   1868 	 * When going from UNKNOWN to UP, we need to mark existing
   1869 	 * addresses as tentative and restart DAD as we may have
   1870 	 * erroneously not found a duplicate.
   1871 	 *
   1872 	 * This needs to happen before rt_ifmsg to avoid a race where
   1873 	 * listeners would have an address and expect it to work right
   1874 	 * away.
   1875 	 */
   1876 	if (link_state == LINK_STATE_UP &&
   1877 	    ifp->if_link_state == LINK_STATE_UNKNOWN)
   1878 	{
   1879 		DOMAIN_FOREACH(dp) {
   1880 			if (dp->dom_if_link_state_change != NULL)
   1881 				dp->dom_if_link_state_change(ifp,
   1882 				    LINK_STATE_DOWN);
   1883 		}
   1884 	}
   1885 
   1886 	ifp->if_link_state = link_state;
   1887 
   1888 	/* Notify that the link state has changed. */
   1889 	rt_ifmsg(ifp);
   1890 
   1891 #if NCARP > 0
   1892 	if (ifp->if_carp)
   1893 		carp_carpdev_state(ifp);
   1894 #endif
   1895 
   1896 	DOMAIN_FOREACH(dp) {
   1897 		if (dp->dom_if_link_state_change != NULL)
   1898 			dp->dom_if_link_state_change(ifp, link_state);
   1899 	}
   1900 }
   1901 
   1902 /*
   1903  * Process the interface link state change queue.
   1904  */
   1905 static void
   1906 if_link_state_change_si(void *arg)
   1907 {
   1908 	struct ifnet *ifp = arg;
   1909 	int s;
   1910 	uint8_t state;
   1911 
   1912 	s = splnet();
   1913 
   1914 	/* Pop a link state change from the queue and process it. */
   1915 	LQ_POP(ifp->if_link_queue, state);
   1916 	if_link_state_change0(ifp, state);
   1917 
   1918 	/* If there is a link state change to come, schedule it. */
   1919 	if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET)
   1920 		softint_schedule(ifp->if_link_si);
   1921 
   1922 	splx(s);
   1923 }
   1924 
   1925 /*
   1926  * Default action when installing a local route on a point-to-point
   1927  * interface.
   1928  */
   1929 void
   1930 p2p_rtrequest(int req, struct rtentry *rt,
   1931     __unused const struct rt_addrinfo *info)
   1932 {
   1933 	struct ifnet *ifp = rt->rt_ifp;
   1934 	struct ifaddr *ifa, *lo0ifa;
   1935 
   1936 	switch (req) {
   1937 	case RTM_ADD:
   1938 		if ((rt->rt_flags & RTF_LOCAL) == 0)
   1939 			break;
   1940 
   1941 		IFADDR_FOREACH(ifa, ifp) {
   1942 			if (equal(rt_getkey(rt), ifa->ifa_addr))
   1943 				break;
   1944 		}
   1945 		if (ifa == NULL)
   1946 			break;
   1947 
   1948 		/*
   1949 		 * Ensure lo0 has an address of the same family.
   1950 		 */
   1951 		IFADDR_FOREACH(lo0ifa, lo0ifp) {
   1952 			if (lo0ifa->ifa_addr->sa_family ==
   1953 			    ifa->ifa_addr->sa_family)
   1954 				break;
   1955 		}
   1956 		if (lo0ifa == NULL)
   1957 			break;
   1958 
   1959 		rt->rt_ifp = lo0ifp;
   1960 
   1961 		/*
   1962 		 * Make sure to set rt->rt_ifa to the interface
   1963 		 * address we are using, otherwise we will have trouble
   1964 		 * with source address selection.
   1965 		 */
   1966 		if (ifa != rt->rt_ifa)
   1967 			rt_replace_ifa(rt, ifa);
   1968 		break;
   1969 	case RTM_DELETE:
   1970 	default:
   1971 		break;
   1972 	}
   1973 }
   1974 
   1975 /*
   1976  * Mark an interface down and notify protocols of
   1977  * the transition.
   1978  * NOTE: must be called at splsoftnet or equivalent.
   1979  */
   1980 void
   1981 if_down(struct ifnet *ifp)
   1982 {
   1983 	struct ifaddr *ifa;
   1984 	struct domain *dp;
   1985 
   1986 	ifp->if_flags &= ~IFF_UP;
   1987 	nanotime(&ifp->if_lastchange);
   1988 	IFADDR_FOREACH(ifa, ifp)
   1989 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
   1990 	IFQ_PURGE(&ifp->if_snd);
   1991 #if NCARP > 0
   1992 	if (ifp->if_carp)
   1993 		carp_carpdev_state(ifp);
   1994 #endif
   1995 	rt_ifmsg(ifp);
   1996 	DOMAIN_FOREACH(dp) {
   1997 		if (dp->dom_if_down)
   1998 			dp->dom_if_down(ifp);
   1999 	}
   2000 }
   2001 
   2002 /*
   2003  * Mark an interface up and notify protocols of
   2004  * the transition.
   2005  * NOTE: must be called at splsoftnet or equivalent.
   2006  */
   2007 void
   2008 if_up(struct ifnet *ifp)
   2009 {
   2010 #ifdef notyet
   2011 	struct ifaddr *ifa;
   2012 #endif
   2013 	struct domain *dp;
   2014 
   2015 	ifp->if_flags |= IFF_UP;
   2016 	nanotime(&ifp->if_lastchange);
   2017 #ifdef notyet
   2018 	/* this has no effect on IP, and will kill all ISO connections XXX */
   2019 	IFADDR_FOREACH(ifa, ifp)
   2020 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
   2021 #endif
   2022 #if NCARP > 0
   2023 	if (ifp->if_carp)
   2024 		carp_carpdev_state(ifp);
   2025 #endif
   2026 	rt_ifmsg(ifp);
   2027 	DOMAIN_FOREACH(dp) {
   2028 		if (dp->dom_if_up)
   2029 			dp->dom_if_up(ifp);
   2030 	}
   2031 }
   2032 
   2033 /*
   2034  * Handle interface slowtimo timer routine.  Called
   2035  * from softclock, we decrement timer (if set) and
   2036  * call the appropriate interface routine on expiration.
   2037  */
   2038 static void
   2039 if_slowtimo(void *arg)
   2040 {
   2041 	void (*slowtimo)(struct ifnet *);
   2042 	struct ifnet *ifp = arg;
   2043 	int s;
   2044 
   2045 	slowtimo = ifp->if_slowtimo;
   2046 	if (__predict_false(slowtimo == NULL))
   2047 		return;
   2048 
   2049 	s = splnet();
   2050 	if (ifp->if_timer != 0 && --ifp->if_timer == 0)
   2051 		(*slowtimo)(ifp);
   2052 
   2053 	splx(s);
   2054 
   2055 	if (__predict_true(ifp->if_slowtimo != NULL))
   2056 		callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
   2057 }
   2058 
   2059 /*
   2060  * Set/clear promiscuous mode on interface ifp based on the truth value
   2061  * of pswitch.  The calls are reference counted so that only the first
   2062  * "on" request actually has an effect, as does the final "off" request.
   2063  * Results are undefined if the "off" and "on" requests are not matched.
   2064  */
   2065 int
   2066 ifpromisc(struct ifnet *ifp, int pswitch)
   2067 {
   2068 	int pcount, ret;
   2069 	short nflags;
   2070 
   2071 	pcount = ifp->if_pcount;
   2072 	if (pswitch) {
   2073 		/*
   2074 		 * Allow the device to be "placed" into promiscuous
   2075 		 * mode even if it is not configured up.  It will
   2076 		 * consult IFF_PROMISC when it is brought up.
   2077 		 */
   2078 		if (ifp->if_pcount++ != 0)
   2079 			return 0;
   2080 		nflags = ifp->if_flags | IFF_PROMISC;
   2081 	} else {
   2082 		if (--ifp->if_pcount > 0)
   2083 			return 0;
   2084 		nflags = ifp->if_flags & ~IFF_PROMISC;
   2085 	}
   2086 	ret = if_flags_set(ifp, nflags);
   2087 	/* Restore interface state if not successful. */
   2088 	if (ret != 0) {
   2089 		ifp->if_pcount = pcount;
   2090 	}
   2091 	return ret;
   2092 }
   2093 
   2094 /*
   2095  * Map interface name to
   2096  * interface structure pointer.
   2097  */
   2098 struct ifnet *
   2099 ifunit(const char *name)
   2100 {
   2101 	struct ifnet *ifp;
   2102 	const char *cp = name;
   2103 	u_int unit = 0;
   2104 	u_int i;
   2105 	int s;
   2106 
   2107 	/*
   2108 	 * If the entire name is a number, treat it as an ifindex.
   2109 	 */
   2110 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   2111 		unit = unit * 10 + (*cp - '0');
   2112 	}
   2113 
   2114 	/*
   2115 	 * If the number took all of the name, then it's a valid ifindex.
   2116 	 */
   2117 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
   2118 		if (unit >= if_indexlim)
   2119 			return NULL;
   2120 		ifp = ifindex2ifnet[unit];
   2121 		if (ifp == NULL || ifp->if_output == if_nulloutput)
   2122 			return NULL;
   2123 		return ifp;
   2124 	}
   2125 
   2126 	ifp = NULL;
   2127 	s = pserialize_read_enter();
   2128 	IFNET_READER_FOREACH(ifp) {
   2129 		if (ifp->if_output == if_nulloutput)
   2130 			continue;
   2131 	 	if (strcmp(ifp->if_xname, name) == 0)
   2132 			goto out;
   2133 	}
   2134 out:
   2135 	pserialize_read_exit(s);
   2136 	return ifp;
   2137 }
   2138 
   2139 /*
   2140  * Get a reference of an ifnet object by an interface name.
   2141  * The returned reference is protected by psref(9). The caller
   2142  * must release a returned reference by if_put after use.
   2143  */
   2144 struct ifnet *
   2145 if_get(const char *name, struct psref *psref)
   2146 {
   2147 	struct ifnet *ifp;
   2148 	const char *cp = name;
   2149 	u_int unit = 0;
   2150 	u_int i;
   2151 	int s;
   2152 
   2153 	/*
   2154 	 * If the entire name is a number, treat it as an ifindex.
   2155 	 */
   2156 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   2157 		unit = unit * 10 + (*cp - '0');
   2158 	}
   2159 
   2160 	/*
   2161 	 * If the number took all of the name, then it's a valid ifindex.
   2162 	 */
   2163 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
   2164 		if (unit >= if_indexlim)
   2165 			return NULL;
   2166 		ifp = ifindex2ifnet[unit];
   2167 		if (ifp == NULL || ifp->if_output == if_nulloutput)
   2168 			return NULL;
   2169 		return ifp;
   2170 	}
   2171 
   2172 	ifp = NULL;
   2173 	s = pserialize_read_enter();
   2174 	IFNET_READER_FOREACH(ifp) {
   2175 		if (ifp->if_output == if_nulloutput)
   2176 			continue;
   2177 		if (strcmp(ifp->if_xname, name) == 0) {
   2178 			psref_acquire(psref, &ifp->if_psref,
   2179 			    ifnet_psref_class);
   2180 			goto out;
   2181 		}
   2182 	}
   2183 out:
   2184 	pserialize_read_exit(s);
   2185 	return ifp;
   2186 }
   2187 
   2188 /*
   2189  * Release a reference of an ifnet object given by if_get or
   2190  * if_get_byindex.
   2191  */
   2192 void
   2193 if_put(const struct ifnet *ifp, struct psref *psref)
   2194 {
   2195 
   2196 	psref_release(psref, &ifp->if_psref, ifnet_psref_class);
   2197 }
   2198 
   2199 ifnet_t *
   2200 if_byindex(u_int idx)
   2201 {
   2202 	return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
   2203 }
   2204 
   2205 /*
   2206  * Get a reference of an ifnet object by an interface index.
   2207  * The returned reference is protected by psref(9). The caller
   2208  * must release a returned reference by if_put after use.
   2209  */
   2210 ifnet_t *
   2211 if_get_byindex(u_int idx, struct psref *psref)
   2212 {
   2213 	ifnet_t *ifp;
   2214 	int s;
   2215 
   2216 	s = pserialize_read_enter();
   2217 	ifp = (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
   2218 	if (__predict_true(ifp != NULL))
   2219 		psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
   2220 	pserialize_read_exit(s);
   2221 
   2222 	return ifp;
   2223 }
   2224 
   2225 /*
   2226  * XXX unsafe
   2227  */
   2228 void
   2229 if_acquire_unsafe(struct ifnet *ifp, struct psref *psref)
   2230 {
   2231 
   2232 	KASSERT(ifp->if_index != 0);
   2233 	KASSERT(if_byindex(ifp->if_index) != NULL);
   2234 	psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
   2235 }
   2236 
   2237 bool
   2238 if_held(struct ifnet *ifp)
   2239 {
   2240 
   2241 	return psref_held(&ifp->if_psref, ifnet_psref_class);
   2242 }
   2243 
   2244 
   2245 /* common */
   2246 int
   2247 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
   2248 {
   2249 	int s;
   2250 	struct ifreq *ifr;
   2251 	struct ifcapreq *ifcr;
   2252 	struct ifdatareq *ifdr;
   2253 
   2254 	switch (cmd) {
   2255 	case SIOCSIFCAP:
   2256 		ifcr = data;
   2257 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
   2258 			return EINVAL;
   2259 
   2260 		if (ifcr->ifcr_capenable == ifp->if_capenable)
   2261 			return 0;
   2262 
   2263 		ifp->if_capenable = ifcr->ifcr_capenable;
   2264 
   2265 		/* Pre-compute the checksum flags mask. */
   2266 		ifp->if_csum_flags_tx = 0;
   2267 		ifp->if_csum_flags_rx = 0;
   2268 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
   2269 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
   2270 		}
   2271 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
   2272 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
   2273 		}
   2274 
   2275 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
   2276 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
   2277 		}
   2278 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
   2279 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
   2280 		}
   2281 
   2282 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
   2283 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
   2284 		}
   2285 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
   2286 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
   2287 		}
   2288 
   2289 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
   2290 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
   2291 		}
   2292 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
   2293 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
   2294 		}
   2295 
   2296 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
   2297 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
   2298 		}
   2299 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
   2300 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
   2301 		}
   2302 		if (ifp->if_flags & IFF_UP)
   2303 			return ENETRESET;
   2304 		return 0;
   2305 	case SIOCSIFFLAGS:
   2306 		ifr = data;
   2307 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
   2308 			s = splnet();
   2309 			if_down(ifp);
   2310 			splx(s);
   2311 		}
   2312 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
   2313 			s = splnet();
   2314 			if_up(ifp);
   2315 			splx(s);
   2316 		}
   2317 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
   2318 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
   2319 		break;
   2320 	case SIOCGIFFLAGS:
   2321 		ifr = data;
   2322 		ifr->ifr_flags = ifp->if_flags;
   2323 		break;
   2324 
   2325 	case SIOCGIFMETRIC:
   2326 		ifr = data;
   2327 		ifr->ifr_metric = ifp->if_metric;
   2328 		break;
   2329 
   2330 	case SIOCGIFMTU:
   2331 		ifr = data;
   2332 		ifr->ifr_mtu = ifp->if_mtu;
   2333 		break;
   2334 
   2335 	case SIOCGIFDLT:
   2336 		ifr = data;
   2337 		ifr->ifr_dlt = ifp->if_dlt;
   2338 		break;
   2339 
   2340 	case SIOCGIFCAP:
   2341 		ifcr = data;
   2342 		ifcr->ifcr_capabilities = ifp->if_capabilities;
   2343 		ifcr->ifcr_capenable = ifp->if_capenable;
   2344 		break;
   2345 
   2346 	case SIOCSIFMETRIC:
   2347 		ifr = data;
   2348 		ifp->if_metric = ifr->ifr_metric;
   2349 		break;
   2350 
   2351 	case SIOCGIFDATA:
   2352 		ifdr = data;
   2353 		ifdr->ifdr_data = ifp->if_data;
   2354 		break;
   2355 
   2356 	case SIOCGIFINDEX:
   2357 		ifr = data;
   2358 		ifr->ifr_index = ifp->if_index;
   2359 		break;
   2360 
   2361 	case SIOCZIFDATA:
   2362 		ifdr = data;
   2363 		ifdr->ifdr_data = ifp->if_data;
   2364 		/*
   2365 		 * Assumes that the volatile counters that can be
   2366 		 * zero'ed are at the end of if_data.
   2367 		 */
   2368 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
   2369 		    offsetof(struct if_data, ifi_ipackets));
   2370 		/*
   2371 		 * The memset() clears to the bottm of if_data. In the area,
   2372 		 * if_lastchange is included. Please be careful if new entry
   2373 		 * will be added into if_data or rewite this.
   2374 		 *
   2375 		 * And also, update if_lastchnage.
   2376 		 */
   2377 		getnanotime(&ifp->if_lastchange);
   2378 		break;
   2379 	case SIOCSIFMTU:
   2380 		ifr = data;
   2381 		if (ifp->if_mtu == ifr->ifr_mtu)
   2382 			break;
   2383 		ifp->if_mtu = ifr->ifr_mtu;
   2384 		/*
   2385 		 * If the link MTU changed, do network layer specific procedure.
   2386 		 */
   2387 #ifdef INET6
   2388 		if (in6_present)
   2389 			nd6_setmtu(ifp);
   2390 #endif
   2391 		return ENETRESET;
   2392 	default:
   2393 		return ENOTTY;
   2394 	}
   2395 	return 0;
   2396 }
   2397 
   2398 int
   2399 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
   2400 {
   2401 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
   2402 	struct ifaddr *ifa;
   2403 	const struct sockaddr *any, *sa;
   2404 	union {
   2405 		struct sockaddr sa;
   2406 		struct sockaddr_storage ss;
   2407 	} u, v;
   2408 
   2409 	switch (cmd) {
   2410 	case SIOCSIFADDRPREF:
   2411 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
   2412 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   2413 		    NULL) != 0)
   2414 			return EPERM;
   2415 	case SIOCGIFADDRPREF:
   2416 		break;
   2417 	default:
   2418 		return EOPNOTSUPP;
   2419 	}
   2420 
   2421 	/* sanity checks */
   2422 	if (data == NULL || ifp == NULL) {
   2423 		panic("invalid argument to %s", __func__);
   2424 		/*NOTREACHED*/
   2425 	}
   2426 
   2427 	/* address must be specified on ADD and DELETE */
   2428 	sa = sstocsa(&ifap->ifap_addr);
   2429 	if (sa->sa_family != sofamily(so))
   2430 		return EINVAL;
   2431 	if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
   2432 		return EINVAL;
   2433 
   2434 	sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
   2435 
   2436 	IFADDR_FOREACH(ifa, ifp) {
   2437 		if (ifa->ifa_addr->sa_family != sa->sa_family)
   2438 			continue;
   2439 		sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
   2440 		if (sockaddr_cmp(&u.sa, &v.sa) == 0)
   2441 			break;
   2442 	}
   2443 	if (ifa == NULL)
   2444 		return EADDRNOTAVAIL;
   2445 
   2446 	switch (cmd) {
   2447 	case SIOCSIFADDRPREF:
   2448 		ifa->ifa_preference = ifap->ifap_preference;
   2449 		return 0;
   2450 	case SIOCGIFADDRPREF:
   2451 		/* fill in the if_laddrreq structure */
   2452 		(void)sockaddr_copy(sstosa(&ifap->ifap_addr),
   2453 		    sizeof(ifap->ifap_addr), ifa->ifa_addr);
   2454 		ifap->ifap_preference = ifa->ifa_preference;
   2455 		return 0;
   2456 	default:
   2457 		return EOPNOTSUPP;
   2458 	}
   2459 }
   2460 
   2461 /*
   2462  * Interface ioctls.
   2463  */
   2464 static int
   2465 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
   2466 {
   2467 	struct ifnet *ifp;
   2468 	struct ifreq *ifr;
   2469 	int error = 0;
   2470 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
   2471 	u_long ocmd = cmd;
   2472 #endif
   2473 	short oif_flags;
   2474 #ifdef COMPAT_OIFREQ
   2475 	struct ifreq ifrb;
   2476 	struct oifreq *oifr = NULL;
   2477 #endif
   2478 	int r;
   2479 	struct psref psref;
   2480 	int bound;
   2481 
   2482 	switch (cmd) {
   2483 #ifdef COMPAT_OIFREQ
   2484 	case OSIOCGIFCONF:
   2485 	case OOSIOCGIFCONF:
   2486 		return compat_ifconf(cmd, data);
   2487 #endif
   2488 #ifdef COMPAT_OIFDATA
   2489 	case OSIOCGIFDATA:
   2490 	case OSIOCZIFDATA:
   2491 		return compat_ifdatareq(l, cmd, data);
   2492 #endif
   2493 	case SIOCGIFCONF:
   2494 		return ifconf(cmd, data);
   2495 	case SIOCINITIFADDR:
   2496 		return EPERM;
   2497 	}
   2498 
   2499 #ifdef COMPAT_OIFREQ
   2500 	cmd = compat_cvtcmd(cmd);
   2501 	if (cmd != ocmd) {
   2502 		oifr = data;
   2503 		data = ifr = &ifrb;
   2504 		ifreqo2n(oifr, ifr);
   2505 	} else
   2506 #endif
   2507 		ifr = data;
   2508 
   2509 	switch (cmd) {
   2510 	case SIOCIFCREATE:
   2511 	case SIOCIFDESTROY:
   2512 		bound = curlwp_bind();
   2513 		if (l != NULL) {
   2514 			ifp = if_get(ifr->ifr_name, &psref);
   2515 			error = kauth_authorize_network(l->l_cred,
   2516 			    KAUTH_NETWORK_INTERFACE,
   2517 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   2518 			    (void *)cmd, NULL);
   2519 			if (ifp != NULL)
   2520 				if_put(ifp, &psref);
   2521 			if (error != 0) {
   2522 				curlwp_bindx(bound);
   2523 				return error;
   2524 			}
   2525 		}
   2526 		mutex_enter(&if_clone_mtx);
   2527 		r = (cmd == SIOCIFCREATE) ?
   2528 			if_clone_create(ifr->ifr_name) :
   2529 			if_clone_destroy(ifr->ifr_name);
   2530 		mutex_exit(&if_clone_mtx);
   2531 		curlwp_bindx(bound);
   2532 		return r;
   2533 
   2534 	case SIOCIFGCLONERS:
   2535 		{
   2536 			struct if_clonereq *req = (struct if_clonereq *)data;
   2537 			return if_clone_list(req->ifcr_count, req->ifcr_buffer,
   2538 			    &req->ifcr_total);
   2539 		}
   2540 	}
   2541 
   2542 	bound = curlwp_bind();
   2543 	ifp = if_get(ifr->ifr_name, &psref);
   2544 	if (ifp == NULL) {
   2545 		curlwp_bindx(bound);
   2546 		return ENXIO;
   2547 	}
   2548 
   2549 	switch (cmd) {
   2550 	case SIOCALIFADDR:
   2551 	case SIOCDLIFADDR:
   2552 	case SIOCSIFADDRPREF:
   2553 	case SIOCSIFFLAGS:
   2554 	case SIOCSIFCAP:
   2555 	case SIOCSIFMETRIC:
   2556 	case SIOCZIFDATA:
   2557 	case SIOCSIFMTU:
   2558 	case SIOCSIFPHYADDR:
   2559 	case SIOCDIFPHYADDR:
   2560 #ifdef INET6
   2561 	case SIOCSIFPHYADDR_IN6:
   2562 #endif
   2563 	case SIOCSLIFPHYADDR:
   2564 	case SIOCADDMULTI:
   2565 	case SIOCDELMULTI:
   2566 	case SIOCSIFMEDIA:
   2567 	case SIOCSDRVSPEC:
   2568 	case SIOCG80211:
   2569 	case SIOCS80211:
   2570 	case SIOCS80211NWID:
   2571 	case SIOCS80211NWKEY:
   2572 	case SIOCS80211POWER:
   2573 	case SIOCS80211BSSID:
   2574 	case SIOCS80211CHANNEL:
   2575 	case SIOCSLINKSTR:
   2576 		if (l != NULL) {
   2577 			error = kauth_authorize_network(l->l_cred,
   2578 			    KAUTH_NETWORK_INTERFACE,
   2579 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   2580 			    (void *)cmd, NULL);
   2581 			if (error != 0)
   2582 				goto out;
   2583 		}
   2584 	}
   2585 
   2586 	oif_flags = ifp->if_flags;
   2587 
   2588 	mutex_enter(ifp->if_ioctl_lock);
   2589 
   2590 	error = (*ifp->if_ioctl)(ifp, cmd, data);
   2591 	if (error != ENOTTY)
   2592 		;
   2593 	else if (so->so_proto == NULL)
   2594 		error = EOPNOTSUPP;
   2595 	else {
   2596 #ifdef COMPAT_OSOCK
   2597 		error = compat_ifioctl(so, ocmd, cmd, data, l);
   2598 #else
   2599 		error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
   2600 		    cmd, data, ifp);
   2601 #endif
   2602 	}
   2603 
   2604 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   2605 		if ((ifp->if_flags & IFF_UP) != 0) {
   2606 			int s = splnet();
   2607 			if_up(ifp);
   2608 			splx(s);
   2609 		}
   2610 	}
   2611 #ifdef COMPAT_OIFREQ
   2612 	if (cmd != ocmd)
   2613 		ifreqn2o(oifr, ifr);
   2614 #endif
   2615 
   2616 	mutex_exit(ifp->if_ioctl_lock);
   2617 out:
   2618 	if_put(ifp, &psref);
   2619 	curlwp_bindx(bound);
   2620 	return error;
   2621 }
   2622 
   2623 /*
   2624  * Return interface configuration
   2625  * of system.  List may be used
   2626  * in later ioctl's (above) to get
   2627  * other information.
   2628  *
   2629  * Each record is a struct ifreq.  Before the addition of
   2630  * sockaddr_storage, the API rule was that sockaddr flavors that did
   2631  * not fit would extend beyond the struct ifreq, with the next struct
   2632  * ifreq starting sa_len beyond the struct sockaddr.  Because the
   2633  * union in struct ifreq includes struct sockaddr_storage, every kind
   2634  * of sockaddr must fit.  Thus, there are no longer any overlength
   2635  * records.
   2636  *
   2637  * Records are added to the user buffer if they fit, and ifc_len is
   2638  * adjusted to the length that was written.  Thus, the user is only
   2639  * assured of getting the complete list if ifc_len on return is at
   2640  * least sizeof(struct ifreq) less than it was on entry.
   2641  *
   2642  * If the user buffer pointer is NULL, this routine copies no data and
   2643  * returns the amount of space that would be needed.
   2644  *
   2645  * Invariants:
   2646  * ifrp points to the next part of the user's buffer to be used.  If
   2647  * ifrp != NULL, space holds the number of bytes remaining that we may
   2648  * write at ifrp.  Otherwise, space holds the number of bytes that
   2649  * would have been written had there been adequate space.
   2650  */
   2651 /*ARGSUSED*/
   2652 static int
   2653 ifconf(u_long cmd, void *data)
   2654 {
   2655 	struct ifconf *ifc = (struct ifconf *)data;
   2656 	struct ifnet *ifp;
   2657 	struct ifaddr *ifa;
   2658 	struct ifreq ifr, *ifrp = NULL;
   2659 	int space = 0, error = 0;
   2660 	const int sz = (int)sizeof(struct ifreq);
   2661 	const bool docopy = ifc->ifc_req != NULL;
   2662 	int s;
   2663 	int bound;
   2664 	struct psref psref;
   2665 
   2666 	if (docopy) {
   2667 		space = ifc->ifc_len;
   2668 		ifrp = ifc->ifc_req;
   2669 	}
   2670 
   2671 	bound = curlwp_bind();
   2672 	s = pserialize_read_enter();
   2673 	IFNET_READER_FOREACH(ifp) {
   2674 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
   2675 		pserialize_read_exit(s);
   2676 
   2677 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
   2678 		    sizeof(ifr.ifr_name));
   2679 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
   2680 			error = ENAMETOOLONG;
   2681 			goto release_exit;
   2682 		}
   2683 		if (IFADDR_EMPTY(ifp)) {
   2684 			/* Interface with no addresses - send zero sockaddr. */
   2685 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2686 			if (!docopy) {
   2687 				space += sz;
   2688 				continue;
   2689 			}
   2690 			if (space >= sz) {
   2691 				error = copyout(&ifr, ifrp, sz);
   2692 				if (error != 0)
   2693 					goto release_exit;
   2694 				ifrp++;
   2695 				space -= sz;
   2696 			}
   2697 		}
   2698 
   2699 		IFADDR_FOREACH(ifa, ifp) {
   2700 			struct sockaddr *sa = ifa->ifa_addr;
   2701 			/* all sockaddrs must fit in sockaddr_storage */
   2702 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
   2703 
   2704 			if (!docopy) {
   2705 				space += sz;
   2706 				continue;
   2707 			}
   2708 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
   2709 			if (space >= sz) {
   2710 				error = copyout(&ifr, ifrp, sz);
   2711 				if (error != 0)
   2712 					goto release_exit;
   2713 				ifrp++; space -= sz;
   2714 			}
   2715 		}
   2716 
   2717 		s = pserialize_read_enter();
   2718 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   2719 	}
   2720 	pserialize_read_exit(s);
   2721 	curlwp_bindx(bound);
   2722 
   2723 	if (docopy) {
   2724 		KASSERT(0 <= space && space <= ifc->ifc_len);
   2725 		ifc->ifc_len -= space;
   2726 	} else {
   2727 		KASSERT(space >= 0);
   2728 		ifc->ifc_len = space;
   2729 	}
   2730 	return (0);
   2731 
   2732 release_exit:
   2733 	psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   2734 	curlwp_bindx(bound);
   2735 	return error;
   2736 }
   2737 
   2738 int
   2739 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
   2740 {
   2741 	uint8_t len;
   2742 #ifdef COMPAT_OIFREQ
   2743 	struct ifreq ifrb;
   2744 	struct oifreq *oifr = NULL;
   2745 	u_long ocmd = cmd;
   2746 	cmd = compat_cvtcmd(cmd);
   2747 	if (cmd != ocmd) {
   2748 		oifr = (struct oifreq *)(void *)ifr;
   2749 		ifr = &ifrb;
   2750 		ifreqo2n(oifr, ifr);
   2751 		len = sizeof(oifr->ifr_addr);
   2752 	} else
   2753 #endif
   2754 		len = sizeof(ifr->ifr_ifru.ifru_space);
   2755 
   2756 	if (len < sa->sa_len)
   2757 		return EFBIG;
   2758 
   2759 	memset(&ifr->ifr_addr, 0, len);
   2760 	sockaddr_copy(&ifr->ifr_addr, len, sa);
   2761 
   2762 #ifdef COMPAT_OIFREQ
   2763 	if (cmd != ocmd)
   2764 		ifreqn2o(oifr, ifr);
   2765 #endif
   2766 	return 0;
   2767 }
   2768 
   2769 /*
   2770  * wrapper function for the drivers which doesn't have if_transmit().
   2771  */
   2772 int
   2773 if_transmit(struct ifnet *ifp, struct mbuf *m)
   2774 {
   2775 	int s, error;
   2776 
   2777 	s = splnet();
   2778 
   2779 	IFQ_ENQUEUE(&ifp->if_snd, m, error);
   2780 	if (error != 0) {
   2781 		/* mbuf is already freed */
   2782 		goto out;
   2783 	}
   2784 
   2785 	ifp->if_obytes += m->m_pkthdr.len;;
   2786 	if (m->m_flags & M_MCAST)
   2787 		ifp->if_omcasts++;
   2788 
   2789 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
   2790 		(*ifp->if_start)(ifp);
   2791 out:
   2792 	splx(s);
   2793 
   2794 	return error;
   2795 }
   2796 
   2797 /*
   2798  * Queue message on interface, and start output if interface
   2799  * not yet active.
   2800  */
   2801 int
   2802 ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
   2803 {
   2804 
   2805 	return (*ifp->if_transmit)(ifp, m);
   2806 }
   2807 
   2808 /*
   2809  * Queue message on interface, possibly using a second fast queue
   2810  */
   2811 int
   2812 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
   2813 {
   2814 	int error = 0;
   2815 
   2816 	if (ifq != NULL
   2817 #ifdef ALTQ
   2818 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
   2819 #endif
   2820 	    ) {
   2821 		if (IF_QFULL(ifq)) {
   2822 			IF_DROP(&ifp->if_snd);
   2823 			m_freem(m);
   2824 			if (error == 0)
   2825 				error = ENOBUFS;
   2826 		} else
   2827 			IF_ENQUEUE(ifq, m);
   2828 	} else
   2829 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
   2830 	if (error != 0) {
   2831 		++ifp->if_oerrors;
   2832 		return error;
   2833 	}
   2834 	return 0;
   2835 }
   2836 
   2837 int
   2838 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
   2839 {
   2840 	int rc;
   2841 
   2842 	if (ifp->if_initaddr != NULL)
   2843 		rc = (*ifp->if_initaddr)(ifp, ifa, src);
   2844 	else if (src ||
   2845 		/* FIXME: may not hold if_ioctl_lock */
   2846 	         (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
   2847 		rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
   2848 
   2849 	return rc;
   2850 }
   2851 
   2852 int
   2853 if_do_dad(struct ifnet *ifp)
   2854 {
   2855 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
   2856 		return 0;
   2857 
   2858 	switch (ifp->if_type) {
   2859 	case IFT_FAITH:
   2860 		/*
   2861 		 * These interfaces do not have the IFF_LOOPBACK flag,
   2862 		 * but loop packets back.  We do not have to do DAD on such
   2863 		 * interfaces.  We should even omit it, because loop-backed
   2864 		 * responses would confuse the DAD procedure.
   2865 		 */
   2866 		return 0;
   2867 	default:
   2868 		/*
   2869 		 * Our DAD routine requires the interface up and running.
   2870 		 * However, some interfaces can be up before the RUNNING
   2871 		 * status.  Additionaly, users may try to assign addresses
   2872 		 * before the interface becomes up (or running).
   2873 		 * We simply skip DAD in such a case as a work around.
   2874 		 * XXX: we should rather mark "tentative" on such addresses,
   2875 		 * and do DAD after the interface becomes ready.
   2876 		 */
   2877 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
   2878 		    (IFF_UP|IFF_RUNNING))
   2879 			return 0;
   2880 
   2881 		return 1;
   2882 	}
   2883 }
   2884 
   2885 int
   2886 if_flags_set(ifnet_t *ifp, const short flags)
   2887 {
   2888 	int rc;
   2889 
   2890 	if (ifp->if_setflags != NULL)
   2891 		rc = (*ifp->if_setflags)(ifp, flags);
   2892 	else {
   2893 		short cantflags, chgdflags;
   2894 		struct ifreq ifr;
   2895 
   2896 		chgdflags = ifp->if_flags ^ flags;
   2897 		cantflags = chgdflags & IFF_CANTCHANGE;
   2898 
   2899 		if (cantflags != 0)
   2900 			ifp->if_flags ^= cantflags;
   2901 
   2902                 /* Traditionally, we do not call if_ioctl after
   2903                  * setting/clearing only IFF_PROMISC if the interface
   2904                  * isn't IFF_UP.  Uphold that tradition.
   2905 		 */
   2906 		if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
   2907 			return 0;
   2908 
   2909 		memset(&ifr, 0, sizeof(ifr));
   2910 
   2911 		ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
   2912 		/* FIXME: may not hold if_ioctl_lock */
   2913 		rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
   2914 
   2915 		if (rc != 0 && cantflags != 0)
   2916 			ifp->if_flags ^= cantflags;
   2917 	}
   2918 
   2919 	return rc;
   2920 }
   2921 
   2922 int
   2923 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
   2924 {
   2925 	int rc;
   2926 	struct ifreq ifr;
   2927 
   2928 	if (ifp->if_mcastop != NULL)
   2929 		rc = (*ifp->if_mcastop)(ifp, cmd, sa);
   2930 	else {
   2931 		ifreq_setaddr(cmd, &ifr, sa);
   2932 		rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
   2933 	}
   2934 
   2935 	return rc;
   2936 }
   2937 
   2938 static void
   2939 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
   2940     struct ifaltq *ifq)
   2941 {
   2942 	const struct sysctlnode *cnode, *rnode;
   2943 
   2944 	if (sysctl_createv(clog, 0, NULL, &rnode,
   2945 		       CTLFLAG_PERMANENT,
   2946 		       CTLTYPE_NODE, "interfaces",
   2947 		       SYSCTL_DESCR("Per-interface controls"),
   2948 		       NULL, 0, NULL, 0,
   2949 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
   2950 		goto bad;
   2951 
   2952 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2953 		       CTLFLAG_PERMANENT,
   2954 		       CTLTYPE_NODE, ifname,
   2955 		       SYSCTL_DESCR("Interface controls"),
   2956 		       NULL, 0, NULL, 0,
   2957 		       CTL_CREATE, CTL_EOL) != 0)
   2958 		goto bad;
   2959 
   2960 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2961 		       CTLFLAG_PERMANENT,
   2962 		       CTLTYPE_NODE, "sndq",
   2963 		       SYSCTL_DESCR("Interface output queue controls"),
   2964 		       NULL, 0, NULL, 0,
   2965 		       CTL_CREATE, CTL_EOL) != 0)
   2966 		goto bad;
   2967 
   2968 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2969 		       CTLFLAG_PERMANENT,
   2970 		       CTLTYPE_INT, "len",
   2971 		       SYSCTL_DESCR("Current output queue length"),
   2972 		       NULL, 0, &ifq->ifq_len, 0,
   2973 		       CTL_CREATE, CTL_EOL) != 0)
   2974 		goto bad;
   2975 
   2976 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2977 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2978 		       CTLTYPE_INT, "maxlen",
   2979 		       SYSCTL_DESCR("Maximum allowed output queue length"),
   2980 		       NULL, 0, &ifq->ifq_maxlen, 0,
   2981 		       CTL_CREATE, CTL_EOL) != 0)
   2982 		goto bad;
   2983 
   2984 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2985 		       CTLFLAG_PERMANENT,
   2986 		       CTLTYPE_INT, "drops",
   2987 		       SYSCTL_DESCR("Packets dropped due to full output queue"),
   2988 		       NULL, 0, &ifq->ifq_drops, 0,
   2989 		       CTL_CREATE, CTL_EOL) != 0)
   2990 		goto bad;
   2991 
   2992 	return;
   2993 bad:
   2994 	printf("%s: could not attach sysctl nodes\n", ifname);
   2995 	return;
   2996 }
   2997 
   2998 #if defined(INET) || defined(INET6)
   2999 
   3000 #define	SYSCTL_NET_PKTQ(q, cn, c)					\
   3001 	static int							\
   3002 	sysctl_net_##q##_##cn(SYSCTLFN_ARGS)				\
   3003 	{								\
   3004 		return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);	\
   3005 	}
   3006 
   3007 #if defined(INET)
   3008 static int
   3009 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
   3010 {
   3011 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
   3012 }
   3013 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
   3014 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
   3015 #endif
   3016 
   3017 #if defined(INET6)
   3018 static int
   3019 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
   3020 {
   3021 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
   3022 }
   3023 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
   3024 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
   3025 #endif
   3026 
   3027 static void
   3028 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
   3029 {
   3030 	sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
   3031 	const char *pfname = NULL, *ipname = NULL;
   3032 	int ipn = 0, qid = 0;
   3033 
   3034 	switch (pf) {
   3035 #if defined(INET)
   3036 	case PF_INET:
   3037 		len_func = sysctl_net_ip_pktq_items;
   3038 		maxlen_func = sysctl_net_ip_pktq_maxlen;
   3039 		drops_func = sysctl_net_ip_pktq_drops;
   3040 		pfname = "inet", ipn = IPPROTO_IP;
   3041 		ipname = "ip", qid = IPCTL_IFQ;
   3042 		break;
   3043 #endif
   3044 #if defined(INET6)
   3045 	case PF_INET6:
   3046 		len_func = sysctl_net_ip6_pktq_items;
   3047 		maxlen_func = sysctl_net_ip6_pktq_maxlen;
   3048 		drops_func = sysctl_net_ip6_pktq_drops;
   3049 		pfname = "inet6", ipn = IPPROTO_IPV6;
   3050 		ipname = "ip6", qid = IPV6CTL_IFQ;
   3051 		break;
   3052 #endif
   3053 	default:
   3054 		KASSERT(false);
   3055 	}
   3056 
   3057 	sysctl_createv(clog, 0, NULL, NULL,
   3058 		       CTLFLAG_PERMANENT,
   3059 		       CTLTYPE_NODE, pfname, NULL,
   3060 		       NULL, 0, NULL, 0,
   3061 		       CTL_NET, pf, CTL_EOL);
   3062 	sysctl_createv(clog, 0, NULL, NULL,
   3063 		       CTLFLAG_PERMANENT,
   3064 		       CTLTYPE_NODE, ipname, NULL,
   3065 		       NULL, 0, NULL, 0,
   3066 		       CTL_NET, pf, ipn, CTL_EOL);
   3067 	sysctl_createv(clog, 0, NULL, NULL,
   3068 		       CTLFLAG_PERMANENT,
   3069 		       CTLTYPE_NODE, "ifq",
   3070 		       SYSCTL_DESCR("Protocol input queue controls"),
   3071 		       NULL, 0, NULL, 0,
   3072 		       CTL_NET, pf, ipn, qid, CTL_EOL);
   3073 
   3074 	sysctl_createv(clog, 0, NULL, NULL,
   3075 		       CTLFLAG_PERMANENT,
   3076 		       CTLTYPE_INT, "len",
   3077 		       SYSCTL_DESCR("Current input queue length"),
   3078 		       len_func, 0, NULL, 0,
   3079 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
   3080 	sysctl_createv(clog, 0, NULL, NULL,
   3081 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3082 		       CTLTYPE_INT, "maxlen",
   3083 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   3084 		       maxlen_func, 0, NULL, 0,
   3085 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
   3086 	sysctl_createv(clog, 0, NULL, NULL,
   3087 		       CTLFLAG_PERMANENT,
   3088 		       CTLTYPE_INT, "drops",
   3089 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
   3090 		       drops_func, 0, NULL, 0,
   3091 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
   3092 }
   3093 #endif /* INET || INET6 */
   3094 
   3095 static int
   3096 if_sdl_sysctl(SYSCTLFN_ARGS)
   3097 {
   3098 	struct ifnet *ifp;
   3099 	const struct sockaddr_dl *sdl;
   3100 	struct psref psref;
   3101 	int error = 0;
   3102 	int bound;
   3103 
   3104 	if (namelen != 1)
   3105 		return EINVAL;
   3106 
   3107 	bound = curlwp_bind();
   3108 	ifp = if_get_byindex(name[0], &psref);
   3109 	if (ifp == NULL) {
   3110 		error = ENODEV;
   3111 		goto out0;
   3112 	}
   3113 
   3114 	sdl = ifp->if_sadl;
   3115 	if (sdl == NULL) {
   3116 		*oldlenp = 0;
   3117 		goto out1;
   3118 	}
   3119 
   3120 	if (oldp == NULL) {
   3121 		*oldlenp = sdl->sdl_alen;
   3122 		goto out1;
   3123 	}
   3124 
   3125 	if (*oldlenp >= sdl->sdl_alen)
   3126 		*oldlenp = sdl->sdl_alen;
   3127 	error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
   3128 out1:
   3129 	if_put(ifp, &psref);
   3130 out0:
   3131 	curlwp_bindx(bound);
   3132 	return error;
   3133 }
   3134 
   3135 SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
   3136 {
   3137 	const struct sysctlnode *rnode = NULL;
   3138 
   3139 	sysctl_createv(clog, 0, NULL, &rnode,
   3140 		       CTLFLAG_PERMANENT,
   3141 		       CTLTYPE_NODE, "sdl",
   3142 		       SYSCTL_DESCR("Get active link-layer address"),
   3143 		       if_sdl_sysctl, 0, NULL, 0,
   3144 		       CTL_NET, CTL_CREATE, CTL_EOL);
   3145 }
   3146