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