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