Home | History | Annotate | Line # | Download | only in net
if.c revision 1.308
      1 /*	$NetBSD: if.c,v 1.308 2015/01/16 10:36:14 ozaki-r Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by William Studenmund and Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the project nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 /*
     62  * Copyright (c) 1980, 1986, 1993
     63  *	The Regents of the University of California.  All rights reserved.
     64  *
     65  * Redistribution and use in source and binary forms, with or without
     66  * modification, are permitted provided that the following conditions
     67  * are met:
     68  * 1. Redistributions of source code must retain the above copyright
     69  *    notice, this list of conditions and the following disclaimer.
     70  * 2. Redistributions in binary form must reproduce the above copyright
     71  *    notice, this list of conditions and the following disclaimer in the
     72  *    documentation and/or other materials provided with the distribution.
     73  * 3. Neither the name of the University nor the names of its contributors
     74  *    may be used to endorse or promote products derived from this software
     75  *    without specific prior written permission.
     76  *
     77  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     78  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     79  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     80  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     81  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     82  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     83  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     85  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     86  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     87  * SUCH DAMAGE.
     88  *
     89  *	@(#)if.c	8.5 (Berkeley) 1/9/95
     90  */
     91 
     92 #include <sys/cdefs.h>
     93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.308 2015/01/16 10:36:14 ozaki-r Exp $");
     94 
     95 #if defined(_KERNEL_OPT)
     96 #include "opt_inet.h"
     97 
     98 #include "opt_atalk.h"
     99 #include "opt_natm.h"
    100 #include "opt_wlan.h"
    101 #include "opt_net_mpsafe.h"
    102 #endif
    103 
    104 #include <sys/param.h>
    105 #include <sys/mbuf.h>
    106 #include <sys/systm.h>
    107 #include <sys/callout.h>
    108 #include <sys/proc.h>
    109 #include <sys/socket.h>
    110 #include <sys/socketvar.h>
    111 #include <sys/domain.h>
    112 #include <sys/protosw.h>
    113 #include <sys/kernel.h>
    114 #include <sys/ioctl.h>
    115 #include <sys/sysctl.h>
    116 #include <sys/syslog.h>
    117 #include <sys/kauth.h>
    118 #include <sys/kmem.h>
    119 #include <sys/xcall.h>
    120 
    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 static int	if_clone_list(struct if_clonereq *);
    181 
    182 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
    183 static int if_cloners_count;
    184 
    185 /* Packet filtering hook for interfaces. */
    186 pfil_head_t *	if_pfil;
    187 
    188 static kauth_listener_t if_listener;
    189 
    190 static int doifioctl(struct socket *, u_long, void *, struct lwp *);
    191 static int ifioctl_attach(struct ifnet *);
    192 static void ifioctl_detach(struct ifnet *);
    193 static void ifnet_lock_enter(struct ifnet_lock *);
    194 static void ifnet_lock_exit(struct ifnet_lock *);
    195 static void if_detach_queues(struct ifnet *, struct ifqueue *);
    196 static void sysctl_sndq_setup(struct sysctllog **, const char *,
    197     struct ifaltq *);
    198 static void if_slowtimo(void *);
    199 static void if_free_sadl(struct ifnet *);
    200 static void if_attachdomain1(struct ifnet *);
    201 static int ifconf(u_long, void *);
    202 static int if_clone_create(const char *);
    203 static int if_clone_destroy(const char *);
    204 
    205 #if defined(INET) || defined(INET6)
    206 static void sysctl_net_pktq_setup(struct sysctllog **, int);
    207 #endif
    208 
    209 static int
    210 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    211     void *arg0, void *arg1, void *arg2, void *arg3)
    212 {
    213 	int result;
    214 	enum kauth_network_req req;
    215 
    216 	result = KAUTH_RESULT_DEFER;
    217 	req = (enum kauth_network_req)arg1;
    218 
    219 	if (action != KAUTH_NETWORK_INTERFACE)
    220 		return result;
    221 
    222 	if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
    223 	    (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
    224 		result = KAUTH_RESULT_ALLOW;
    225 
    226 	return result;
    227 }
    228 
    229 /*
    230  * Network interface utility routines.
    231  *
    232  * Routines with ifa_ifwith* names take sockaddr *'s as
    233  * parameters.
    234  */
    235 void
    236 ifinit(void)
    237 {
    238 #if defined(INET)
    239 	sysctl_net_pktq_setup(NULL, PF_INET);
    240 #endif
    241 #ifdef INET6
    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 static int
   1120 if_clone_list(struct if_clonereq *ifcr)
   1121 {
   1122 	char outbuf[IFNAMSIZ], *dst;
   1123 	struct if_clone *ifc;
   1124 	int count, error = 0;
   1125 
   1126 	ifcr->ifcr_total = if_cloners_count;
   1127 	if ((dst = ifcr->ifcr_buffer) == NULL) {
   1128 		/* Just asking how many there are. */
   1129 		return 0;
   1130 	}
   1131 
   1132 	if (ifcr->ifcr_count < 0)
   1133 		return EINVAL;
   1134 
   1135 	count = (if_cloners_count < ifcr->ifcr_count) ?
   1136 	    if_cloners_count : ifcr->ifcr_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 #if defined(DEBUG) || defined(INET6)
   1426 	int old_link_state;
   1427 #endif
   1428 
   1429 	s = splnet();
   1430 	if (ifp->if_link_state == link_state) {
   1431 		splx(s);
   1432 		return;
   1433 	}
   1434 
   1435 #if defined(DEBUG) || defined(INET6)
   1436 	old_link_state = ifp->if_link_state;
   1437 #endif
   1438 	ifp->if_link_state = link_state;
   1439 #ifdef DEBUG
   1440 	log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
   1441 		link_state == LINK_STATE_UP ? "UP" :
   1442 		link_state == LINK_STATE_DOWN ? "DOWN" :
   1443 		"UNKNOWN",
   1444 		 old_link_state == LINK_STATE_UP ? "UP" :
   1445 		old_link_state == LINK_STATE_DOWN ? "DOWN" :
   1446 		"UNKNOWN");
   1447 #endif
   1448 
   1449 #ifdef INET6
   1450 	/*
   1451 	 * When going from UNKNOWN to UP, we need to mark existing
   1452 	 * IPv6 addresses as tentative and restart DAD as we may have
   1453 	 * erroneously not found a duplicate.
   1454 	 *
   1455 	 * This needs to happen before rt_ifmsg to avoid a race where
   1456 	 * listeners would have an address and expect it to work right
   1457 	 * away.
   1458 	 */
   1459 	if (in6_present && link_state == LINK_STATE_UP &&
   1460 	    old_link_state == LINK_STATE_UNKNOWN)
   1461 		in6_if_link_down(ifp);
   1462 #endif
   1463 
   1464 	/* Notify that the link state has changed. */
   1465 	rt_ifmsg(ifp);
   1466 
   1467 #if NCARP > 0
   1468 	if (ifp->if_carp)
   1469 		carp_carpdev_state(ifp);
   1470 #endif
   1471 
   1472 #ifdef INET6
   1473 	if (in6_present) {
   1474 		if (link_state == LINK_STATE_DOWN)
   1475 			in6_if_link_down(ifp);
   1476 		else if (link_state == LINK_STATE_UP)
   1477 			in6_if_link_up(ifp);
   1478 	}
   1479 #endif
   1480 
   1481 	splx(s);
   1482 }
   1483 
   1484 /*
   1485  * Mark an interface down and notify protocols of
   1486  * the transition.
   1487  * NOTE: must be called at splsoftnet or equivalent.
   1488  */
   1489 void
   1490 if_down(struct ifnet *ifp)
   1491 {
   1492 	struct ifaddr *ifa;
   1493 
   1494 	ifp->if_flags &= ~IFF_UP;
   1495 	nanotime(&ifp->if_lastchange);
   1496 	IFADDR_FOREACH(ifa, ifp)
   1497 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
   1498 	IFQ_PURGE(&ifp->if_snd);
   1499 #if NCARP > 0
   1500 	if (ifp->if_carp)
   1501 		carp_carpdev_state(ifp);
   1502 #endif
   1503 	rt_ifmsg(ifp);
   1504 #ifdef INET6
   1505 	if (in6_present)
   1506 		in6_if_down(ifp);
   1507 #endif
   1508 }
   1509 
   1510 /*
   1511  * Mark an interface up and notify protocols of
   1512  * the transition.
   1513  * NOTE: must be called at splsoftnet or equivalent.
   1514  */
   1515 void
   1516 if_up(struct ifnet *ifp)
   1517 {
   1518 #ifdef notyet
   1519 	struct ifaddr *ifa;
   1520 #endif
   1521 
   1522 	ifp->if_flags |= IFF_UP;
   1523 	nanotime(&ifp->if_lastchange);
   1524 #ifdef notyet
   1525 	/* this has no effect on IP, and will kill all ISO connections XXX */
   1526 	IFADDR_FOREACH(ifa, ifp)
   1527 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
   1528 #endif
   1529 #if NCARP > 0
   1530 	if (ifp->if_carp)
   1531 		carp_carpdev_state(ifp);
   1532 #endif
   1533 	rt_ifmsg(ifp);
   1534 #ifdef INET6
   1535 	if (in6_present)
   1536 		in6_if_up(ifp);
   1537 #endif
   1538 }
   1539 
   1540 /*
   1541  * Handle interface slowtimo timer routine.  Called
   1542  * from softclock, we decrement timer (if set) and
   1543  * call the appropriate interface routine on expiration.
   1544  */
   1545 static void
   1546 if_slowtimo(void *arg)
   1547 {
   1548 	void (*slowtimo)(struct ifnet *);
   1549 	struct ifnet *ifp = arg;
   1550 	int s;
   1551 
   1552 	slowtimo = ifp->if_slowtimo;
   1553 	if (__predict_false(slowtimo == NULL))
   1554 		return;
   1555 
   1556 	s = splnet();
   1557 	if (ifp->if_timer != 0 && --ifp->if_timer == 0)
   1558 		(*slowtimo)(ifp);
   1559 
   1560 	splx(s);
   1561 
   1562 	if (__predict_true(ifp->if_slowtimo != NULL))
   1563 		callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
   1564 }
   1565 
   1566 /*
   1567  * Set/clear promiscuous mode on interface ifp based on the truth value
   1568  * of pswitch.  The calls are reference counted so that only the first
   1569  * "on" request actually has an effect, as does the final "off" request.
   1570  * Results are undefined if the "off" and "on" requests are not matched.
   1571  */
   1572 int
   1573 ifpromisc(struct ifnet *ifp, int pswitch)
   1574 {
   1575 	int pcount, ret;
   1576 	short nflags;
   1577 
   1578 	pcount = ifp->if_pcount;
   1579 	if (pswitch) {
   1580 		/*
   1581 		 * Allow the device to be "placed" into promiscuous
   1582 		 * mode even if it is not configured up.  It will
   1583 		 * consult IFF_PROMISC when it is brought up.
   1584 		 */
   1585 		if (ifp->if_pcount++ != 0)
   1586 			return 0;
   1587 		nflags = ifp->if_flags | IFF_PROMISC;
   1588 	} else {
   1589 		if (--ifp->if_pcount > 0)
   1590 			return 0;
   1591 		nflags = ifp->if_flags & ~IFF_PROMISC;
   1592 	}
   1593 	ret = if_flags_set(ifp, nflags);
   1594 	/* Restore interface state if not successful. */
   1595 	if (ret != 0) {
   1596 		ifp->if_pcount = pcount;
   1597 	}
   1598 	return ret;
   1599 }
   1600 
   1601 /*
   1602  * Map interface name to
   1603  * interface structure pointer.
   1604  */
   1605 struct ifnet *
   1606 ifunit(const char *name)
   1607 {
   1608 	struct ifnet *ifp;
   1609 	const char *cp = name;
   1610 	u_int unit = 0;
   1611 	u_int i;
   1612 
   1613 	/*
   1614 	 * If the entire name is a number, treat it as an ifindex.
   1615 	 */
   1616 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   1617 		unit = unit * 10 + (*cp - '0');
   1618 	}
   1619 
   1620 	/*
   1621 	 * If the number took all of the name, then it's a valid ifindex.
   1622 	 */
   1623 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
   1624 		if (unit >= if_indexlim)
   1625 			return NULL;
   1626 		ifp = ifindex2ifnet[unit];
   1627 		if (ifp == NULL || ifp->if_output == if_nulloutput)
   1628 			return NULL;
   1629 		return ifp;
   1630 	}
   1631 
   1632 	IFNET_FOREACH(ifp) {
   1633 		if (ifp->if_output == if_nulloutput)
   1634 			continue;
   1635 	 	if (strcmp(ifp->if_xname, name) == 0)
   1636 			return ifp;
   1637 	}
   1638 	return NULL;
   1639 }
   1640 
   1641 ifnet_t *
   1642 if_byindex(u_int idx)
   1643 {
   1644 	return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
   1645 }
   1646 
   1647 /* common */
   1648 int
   1649 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
   1650 {
   1651 	int s;
   1652 	struct ifreq *ifr;
   1653 	struct ifcapreq *ifcr;
   1654 	struct ifdatareq *ifdr;
   1655 
   1656 	switch (cmd) {
   1657 	case SIOCSIFCAP:
   1658 		ifcr = data;
   1659 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
   1660 			return EINVAL;
   1661 
   1662 		if (ifcr->ifcr_capenable == ifp->if_capenable)
   1663 			return 0;
   1664 
   1665 		ifp->if_capenable = ifcr->ifcr_capenable;
   1666 
   1667 		/* Pre-compute the checksum flags mask. */
   1668 		ifp->if_csum_flags_tx = 0;
   1669 		ifp->if_csum_flags_rx = 0;
   1670 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
   1671 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
   1672 		}
   1673 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
   1674 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
   1675 		}
   1676 
   1677 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
   1678 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
   1679 		}
   1680 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
   1681 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
   1682 		}
   1683 
   1684 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
   1685 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
   1686 		}
   1687 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
   1688 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
   1689 		}
   1690 
   1691 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
   1692 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
   1693 		}
   1694 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
   1695 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
   1696 		}
   1697 
   1698 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
   1699 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
   1700 		}
   1701 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
   1702 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
   1703 		}
   1704 		if (ifp->if_flags & IFF_UP)
   1705 			return ENETRESET;
   1706 		return 0;
   1707 	case SIOCSIFFLAGS:
   1708 		ifr = data;
   1709 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
   1710 			s = splnet();
   1711 			if_down(ifp);
   1712 			splx(s);
   1713 		}
   1714 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
   1715 			s = splnet();
   1716 			if_up(ifp);
   1717 			splx(s);
   1718 		}
   1719 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
   1720 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
   1721 		break;
   1722 	case SIOCGIFFLAGS:
   1723 		ifr = data;
   1724 		ifr->ifr_flags = ifp->if_flags;
   1725 		break;
   1726 
   1727 	case SIOCGIFMETRIC:
   1728 		ifr = data;
   1729 		ifr->ifr_metric = ifp->if_metric;
   1730 		break;
   1731 
   1732 	case SIOCGIFMTU:
   1733 		ifr = data;
   1734 		ifr->ifr_mtu = ifp->if_mtu;
   1735 		break;
   1736 
   1737 	case SIOCGIFDLT:
   1738 		ifr = data;
   1739 		ifr->ifr_dlt = ifp->if_dlt;
   1740 		break;
   1741 
   1742 	case SIOCGIFCAP:
   1743 		ifcr = data;
   1744 		ifcr->ifcr_capabilities = ifp->if_capabilities;
   1745 		ifcr->ifcr_capenable = ifp->if_capenable;
   1746 		break;
   1747 
   1748 	case SIOCSIFMETRIC:
   1749 		ifr = data;
   1750 		ifp->if_metric = ifr->ifr_metric;
   1751 		break;
   1752 
   1753 	case SIOCGIFDATA:
   1754 		ifdr = data;
   1755 		ifdr->ifdr_data = ifp->if_data;
   1756 		break;
   1757 
   1758 	case SIOCGIFINDEX:
   1759 		ifr = data;
   1760 		ifr->ifr_index = ifp->if_index;
   1761 		break;
   1762 
   1763 	case SIOCZIFDATA:
   1764 		ifdr = data;
   1765 		ifdr->ifdr_data = ifp->if_data;
   1766 		/*
   1767 		 * Assumes that the volatile counters that can be
   1768 		 * zero'ed are at the end of if_data.
   1769 		 */
   1770 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
   1771 		    offsetof(struct if_data, ifi_ipackets));
   1772 		/*
   1773 		 * The memset() clears to the bottm of if_data. In the area,
   1774 		 * if_lastchange is included. Please be careful if new entry
   1775 		 * will be added into if_data or rewite this.
   1776 		 *
   1777 		 * And also, update if_lastchnage.
   1778 		 */
   1779 		getnanotime(&ifp->if_lastchange);
   1780 		break;
   1781 	case SIOCSIFMTU:
   1782 		ifr = data;
   1783 		if (ifp->if_mtu == ifr->ifr_mtu)
   1784 			break;
   1785 		ifp->if_mtu = ifr->ifr_mtu;
   1786 		/*
   1787 		 * If the link MTU changed, do network layer specific procedure.
   1788 		 */
   1789 #ifdef INET6
   1790 		if (in6_present)
   1791 			nd6_setmtu(ifp);
   1792 #endif
   1793 		return ENETRESET;
   1794 	default:
   1795 		return ENOTTY;
   1796 	}
   1797 	return 0;
   1798 }
   1799 
   1800 int
   1801 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
   1802 {
   1803 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
   1804 	struct ifaddr *ifa;
   1805 	const struct sockaddr *any, *sa;
   1806 	union {
   1807 		struct sockaddr sa;
   1808 		struct sockaddr_storage ss;
   1809 	} u, v;
   1810 
   1811 	switch (cmd) {
   1812 	case SIOCSIFADDRPREF:
   1813 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
   1814 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1815 		    NULL) != 0)
   1816 			return EPERM;
   1817 	case SIOCGIFADDRPREF:
   1818 		break;
   1819 	default:
   1820 		return EOPNOTSUPP;
   1821 	}
   1822 
   1823 	/* sanity checks */
   1824 	if (data == NULL || ifp == NULL) {
   1825 		panic("invalid argument to %s", __func__);
   1826 		/*NOTREACHED*/
   1827 	}
   1828 
   1829 	/* address must be specified on ADD and DELETE */
   1830 	sa = sstocsa(&ifap->ifap_addr);
   1831 	if (sa->sa_family != sofamily(so))
   1832 		return EINVAL;
   1833 	if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
   1834 		return EINVAL;
   1835 
   1836 	sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
   1837 
   1838 	IFADDR_FOREACH(ifa, ifp) {
   1839 		if (ifa->ifa_addr->sa_family != sa->sa_family)
   1840 			continue;
   1841 		sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
   1842 		if (sockaddr_cmp(&u.sa, &v.sa) == 0)
   1843 			break;
   1844 	}
   1845 	if (ifa == NULL)
   1846 		return EADDRNOTAVAIL;
   1847 
   1848 	switch (cmd) {
   1849 	case SIOCSIFADDRPREF:
   1850 		ifa->ifa_preference = ifap->ifap_preference;
   1851 		return 0;
   1852 	case SIOCGIFADDRPREF:
   1853 		/* fill in the if_laddrreq structure */
   1854 		(void)sockaddr_copy(sstosa(&ifap->ifap_addr),
   1855 		    sizeof(ifap->ifap_addr), ifa->ifa_addr);
   1856 		ifap->ifap_preference = ifa->ifa_preference;
   1857 		return 0;
   1858 	default:
   1859 		return EOPNOTSUPP;
   1860 	}
   1861 }
   1862 
   1863 static void
   1864 ifnet_lock_enter(struct ifnet_lock *il)
   1865 {
   1866 	uint64_t *nenter;
   1867 
   1868 	/* Before trying to acquire the mutex, increase the count of threads
   1869 	 * who have entered or who wait to enter the critical section.
   1870 	 * Avoid one costly locked memory transaction by keeping a count for
   1871 	 * each CPU.
   1872 	 */
   1873 	nenter = percpu_getref(il->il_nenter);
   1874 	(*nenter)++;
   1875 	percpu_putref(il->il_nenter);
   1876 	mutex_enter(&il->il_lock);
   1877 }
   1878 
   1879 static void
   1880 ifnet_lock_exit(struct ifnet_lock *il)
   1881 {
   1882 	/* Increase the count of threads who have exited the critical
   1883 	 * section.  Increase while we still hold the lock.
   1884 	 */
   1885 	il->il_nexit++;
   1886 	mutex_exit(&il->il_lock);
   1887 }
   1888 
   1889 /*
   1890  * Interface ioctls.
   1891  */
   1892 static int
   1893 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
   1894 {
   1895 	struct ifnet *ifp;
   1896 	struct ifreq *ifr;
   1897 	int error = 0;
   1898 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
   1899 	u_long ocmd = cmd;
   1900 #endif
   1901 	short oif_flags;
   1902 #ifdef COMPAT_OIFREQ
   1903 	struct ifreq ifrb;
   1904 	struct oifreq *oifr = NULL;
   1905 #endif
   1906 	int r;
   1907 
   1908 	switch (cmd) {
   1909 #ifdef COMPAT_OIFREQ
   1910 	case OSIOCGIFCONF:
   1911 	case OOSIOCGIFCONF:
   1912 		return compat_ifconf(cmd, data);
   1913 #endif
   1914 #ifdef COMPAT_OIFDATA
   1915 	case OSIOCGIFDATA:
   1916 	case OSIOCZIFDATA:
   1917 		return compat_ifdatareq(l, cmd, data);
   1918 #endif
   1919 	case SIOCGIFCONF:
   1920 		return ifconf(cmd, data);
   1921 	case SIOCINITIFADDR:
   1922 		return EPERM;
   1923 	}
   1924 
   1925 #ifdef COMPAT_OIFREQ
   1926 	cmd = compat_cvtcmd(cmd);
   1927 	if (cmd != ocmd) {
   1928 		oifr = data;
   1929 		data = ifr = &ifrb;
   1930 		ifreqo2n(oifr, ifr);
   1931 	} else
   1932 #endif
   1933 		ifr = data;
   1934 
   1935 	ifp = ifunit(ifr->ifr_name);
   1936 
   1937 	switch (cmd) {
   1938 	case SIOCIFCREATE:
   1939 	case SIOCIFDESTROY:
   1940 		if (l != NULL) {
   1941 			error = kauth_authorize_network(l->l_cred,
   1942 			    KAUTH_NETWORK_INTERFACE,
   1943 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   1944 			    (void *)cmd, NULL);
   1945 			if (error != 0)
   1946 				return error;
   1947 		}
   1948 		mutex_enter(&if_clone_mtx);
   1949 		r = (cmd == SIOCIFCREATE) ?
   1950 			if_clone_create(ifr->ifr_name) :
   1951 			if_clone_destroy(ifr->ifr_name);
   1952 		mutex_exit(&if_clone_mtx);
   1953 		return r;
   1954 
   1955 	case SIOCIFGCLONERS:
   1956 		return if_clone_list((struct if_clonereq *)data);
   1957 	}
   1958 
   1959 	if (ifp == NULL)
   1960 		return ENXIO;
   1961 
   1962 	switch (cmd) {
   1963 	case SIOCALIFADDR:
   1964 	case SIOCDLIFADDR:
   1965 	case SIOCSIFADDRPREF:
   1966 	case SIOCSIFFLAGS:
   1967 	case SIOCSIFCAP:
   1968 	case SIOCSIFMETRIC:
   1969 	case SIOCZIFDATA:
   1970 	case SIOCSIFMTU:
   1971 	case SIOCSIFPHYADDR:
   1972 	case SIOCDIFPHYADDR:
   1973 #ifdef INET6
   1974 	case SIOCSIFPHYADDR_IN6:
   1975 #endif
   1976 	case SIOCSLIFPHYADDR:
   1977 	case SIOCADDMULTI:
   1978 	case SIOCDELMULTI:
   1979 	case SIOCSIFMEDIA:
   1980 	case SIOCSDRVSPEC:
   1981 	case SIOCG80211:
   1982 	case SIOCS80211:
   1983 	case SIOCS80211NWID:
   1984 	case SIOCS80211NWKEY:
   1985 	case SIOCS80211POWER:
   1986 	case SIOCS80211BSSID:
   1987 	case SIOCS80211CHANNEL:
   1988 	case SIOCSLINKSTR:
   1989 		if (l != NULL) {
   1990 			error = kauth_authorize_network(l->l_cred,
   1991 			    KAUTH_NETWORK_INTERFACE,
   1992 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   1993 			    (void *)cmd, NULL);
   1994 			if (error != 0)
   1995 				return error;
   1996 		}
   1997 	}
   1998 
   1999 	oif_flags = ifp->if_flags;
   2000 
   2001 	ifnet_lock_enter(ifp->if_ioctl_lock);
   2002 	error = (*ifp->if_ioctl)(ifp, cmd, data);
   2003 	if (error != ENOTTY)
   2004 		;
   2005 	else if (so->so_proto == NULL)
   2006 		error = EOPNOTSUPP;
   2007 	else {
   2008 #ifdef COMPAT_OSOCK
   2009 		error = compat_ifioctl(so, ocmd, cmd, data, l);
   2010 #else
   2011 		error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
   2012 		    cmd, data, ifp);
   2013 #endif
   2014 	}
   2015 
   2016 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   2017 #ifdef INET6
   2018 		if (in6_present && (ifp->if_flags & IFF_UP) != 0) {
   2019 			int s = splnet();
   2020 			in6_if_up(ifp);
   2021 			splx(s);
   2022 		}
   2023 #endif
   2024 	}
   2025 #ifdef COMPAT_OIFREQ
   2026 	if (cmd != ocmd)
   2027 		ifreqn2o(oifr, ifr);
   2028 #endif
   2029 
   2030 	ifnet_lock_exit(ifp->if_ioctl_lock);
   2031 	return error;
   2032 }
   2033 
   2034 /* This callback adds to the sum in `arg' the number of
   2035  * threads on `ci' who have entered or who wait to enter the
   2036  * critical section.
   2037  */
   2038 static void
   2039 ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci)
   2040 {
   2041 	uint64_t *sum = arg, *nenter = p;
   2042 
   2043 	*sum += *nenter;
   2044 }
   2045 
   2046 /* Return the number of threads who have entered or who wait
   2047  * to enter the critical section on all CPUs.
   2048  */
   2049 static uint64_t
   2050 ifnet_lock_entrances(struct ifnet_lock *il)
   2051 {
   2052 	uint64_t sum = 0;
   2053 
   2054 	percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum);
   2055 
   2056 	return sum;
   2057 }
   2058 
   2059 static int
   2060 ifioctl_attach(struct ifnet *ifp)
   2061 {
   2062 	struct ifnet_lock *il;
   2063 
   2064 	/* If the driver has not supplied its own if_ioctl, then
   2065 	 * supply the default.
   2066 	 */
   2067 	if (ifp->if_ioctl == NULL)
   2068 		ifp->if_ioctl = ifioctl_common;
   2069 
   2070 	/* Create an ifnet_lock for synchronizing ifioctls. */
   2071 	if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL)
   2072 		return ENOMEM;
   2073 
   2074 	il->il_nenter = percpu_alloc(sizeof(uint64_t));
   2075 	if (il->il_nenter == NULL) {
   2076 		kmem_free(il, sizeof(*il));
   2077 		return ENOMEM;
   2078 	}
   2079 
   2080 	mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE);
   2081 	cv_init(&il->il_emptied, ifp->if_xname);
   2082 
   2083 	ifp->if_ioctl_lock = il;
   2084 
   2085 	return 0;
   2086 }
   2087 
   2088 /*
   2089  * This must not be called until after `ifp' has been withdrawn from the
   2090  * ifnet tables so that ifioctl() cannot get a handle on it by calling
   2091  * ifunit().
   2092  */
   2093 static void
   2094 ifioctl_detach(struct ifnet *ifp)
   2095 {
   2096 	struct ifnet_lock *il;
   2097 
   2098 	il = ifp->if_ioctl_lock;
   2099 	mutex_enter(&il->il_lock);
   2100 	/* Install if_nullioctl to make sure that any thread that
   2101 	 * subsequently enters the critical section will quit it
   2102 	 * immediately and signal the condition variable that we
   2103 	 * wait on, below.
   2104 	 */
   2105 	ifp->if_ioctl = if_nullioctl;
   2106 	/* Sleep while threads are still in the critical section or
   2107 	 * wait to enter it.
   2108 	 */
   2109 	while (ifnet_lock_entrances(il) != il->il_nexit)
   2110 		cv_wait(&il->il_emptied, &il->il_lock);
   2111 	/* At this point, we are the only thread still in the critical
   2112 	 * section, and no new thread can get a handle on the ifioctl
   2113 	 * lock, so it is safe to free its memory.
   2114 	 */
   2115 	mutex_exit(&il->il_lock);
   2116 	ifp->if_ioctl_lock = NULL;
   2117 	percpu_free(il->il_nenter, sizeof(uint64_t));
   2118 	il->il_nenter = NULL;
   2119 	cv_destroy(&il->il_emptied);
   2120 	mutex_destroy(&il->il_lock);
   2121 	kmem_free(il, sizeof(*il));
   2122 }
   2123 
   2124 /*
   2125  * Return interface configuration
   2126  * of system.  List may be used
   2127  * in later ioctl's (above) to get
   2128  * other information.
   2129  *
   2130  * Each record is a struct ifreq.  Before the addition of
   2131  * sockaddr_storage, the API rule was that sockaddr flavors that did
   2132  * not fit would extend beyond the struct ifreq, with the next struct
   2133  * ifreq starting sa_len beyond the struct sockaddr.  Because the
   2134  * union in struct ifreq includes struct sockaddr_storage, every kind
   2135  * of sockaddr must fit.  Thus, there are no longer any overlength
   2136  * records.
   2137  *
   2138  * Records are added to the user buffer if they fit, and ifc_len is
   2139  * adjusted to the length that was written.  Thus, the user is only
   2140  * assured of getting the complete list if ifc_len on return is at
   2141  * least sizeof(struct ifreq) less than it was on entry.
   2142  *
   2143  * If the user buffer pointer is NULL, this routine copies no data and
   2144  * returns the amount of space that would be needed.
   2145  *
   2146  * Invariants:
   2147  * ifrp points to the next part of the user's buffer to be used.  If
   2148  * ifrp != NULL, space holds the number of bytes remaining that we may
   2149  * write at ifrp.  Otherwise, space holds the number of bytes that
   2150  * would have been written had there been adequate space.
   2151  */
   2152 /*ARGSUSED*/
   2153 static int
   2154 ifconf(u_long cmd, void *data)
   2155 {
   2156 	struct ifconf *ifc = (struct ifconf *)data;
   2157 	struct ifnet *ifp;
   2158 	struct ifaddr *ifa;
   2159 	struct ifreq ifr, *ifrp = NULL;
   2160 	int space = 0, error = 0;
   2161 	const int sz = (int)sizeof(struct ifreq);
   2162 	const bool docopy = ifc->ifc_req != NULL;
   2163 
   2164 	if (docopy) {
   2165 		space = ifc->ifc_len;
   2166 		ifrp = ifc->ifc_req;
   2167 	}
   2168 
   2169 	IFNET_FOREACH(ifp) {
   2170 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
   2171 		    sizeof(ifr.ifr_name));
   2172 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
   2173 			return ENAMETOOLONG;
   2174 		if (IFADDR_EMPTY(ifp)) {
   2175 			/* Interface with no addresses - send zero sockaddr. */
   2176 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2177 			if (!docopy) {
   2178 				space += sz;
   2179 				continue;
   2180 			}
   2181 			if (space >= sz) {
   2182 				error = copyout(&ifr, ifrp, sz);
   2183 				if (error != 0)
   2184 					return error;
   2185 				ifrp++;
   2186 				space -= sz;
   2187 			}
   2188 		}
   2189 
   2190 		IFADDR_FOREACH(ifa, ifp) {
   2191 			struct sockaddr *sa = ifa->ifa_addr;
   2192 			/* all sockaddrs must fit in sockaddr_storage */
   2193 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
   2194 
   2195 			if (!docopy) {
   2196 				space += sz;
   2197 				continue;
   2198 			}
   2199 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
   2200 			if (space >= sz) {
   2201 				error = copyout(&ifr, ifrp, sz);
   2202 				if (error != 0)
   2203 					return (error);
   2204 				ifrp++; space -= sz;
   2205 			}
   2206 		}
   2207 	}
   2208 	if (docopy) {
   2209 		KASSERT(0 <= space && space <= ifc->ifc_len);
   2210 		ifc->ifc_len -= space;
   2211 	} else {
   2212 		KASSERT(space >= 0);
   2213 		ifc->ifc_len = space;
   2214 	}
   2215 	return (0);
   2216 }
   2217 
   2218 int
   2219 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
   2220 {
   2221 	uint8_t len;
   2222 #ifdef COMPAT_OIFREQ
   2223 	struct ifreq ifrb;
   2224 	struct oifreq *oifr = NULL;
   2225 	u_long ocmd = cmd;
   2226 	cmd = compat_cvtcmd(cmd);
   2227 	if (cmd != ocmd) {
   2228 		oifr = (struct oifreq *)(void *)ifr;
   2229 		ifr = &ifrb;
   2230 		ifreqo2n(oifr, ifr);
   2231 		len = sizeof(oifr->ifr_addr);
   2232 	} else
   2233 #endif
   2234 		len = sizeof(ifr->ifr_ifru.ifru_space);
   2235 
   2236 	if (len < sa->sa_len)
   2237 		return EFBIG;
   2238 
   2239 	memset(&ifr->ifr_addr, 0, len);
   2240 	sockaddr_copy(&ifr->ifr_addr, len, sa);
   2241 
   2242 #ifdef COMPAT_OIFREQ
   2243 	if (cmd != ocmd)
   2244 		ifreqn2o(oifr, ifr);
   2245 #endif
   2246 	return 0;
   2247 }
   2248 
   2249 /*
   2250  * Queue message on interface, and start output if interface
   2251  * not yet active.
   2252  */
   2253 int
   2254 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
   2255     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
   2256 {
   2257 	int len = m->m_pkthdr.len;
   2258 	int mflags = m->m_flags;
   2259 	int s = splnet();
   2260 	int error;
   2261 
   2262 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
   2263 	if (error != 0)
   2264 		goto out;
   2265 	ifp->if_obytes += len;
   2266 	if (mflags & M_MCAST)
   2267 		ifp->if_omcasts++;
   2268 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
   2269 		(*ifp->if_start)(ifp);
   2270 out:
   2271 	splx(s);
   2272 	return error;
   2273 }
   2274 
   2275 /*
   2276  * Queue message on interface, possibly using a second fast queue
   2277  */
   2278 int
   2279 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
   2280     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
   2281 {
   2282 	int error = 0;
   2283 
   2284 	if (ifq != NULL
   2285 #ifdef ALTQ
   2286 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
   2287 #endif
   2288 	    ) {
   2289 		if (IF_QFULL(ifq)) {
   2290 			IF_DROP(&ifp->if_snd);
   2291 			m_freem(m);
   2292 			if (error == 0)
   2293 				error = ENOBUFS;
   2294 		} else
   2295 			IF_ENQUEUE(ifq, m);
   2296 	} else
   2297 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
   2298 	if (error != 0) {
   2299 		++ifp->if_oerrors;
   2300 		return error;
   2301 	}
   2302 	return 0;
   2303 }
   2304 
   2305 int
   2306 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
   2307 {
   2308 	int rc;
   2309 
   2310 	if (ifp->if_initaddr != NULL)
   2311 		rc = (*ifp->if_initaddr)(ifp, ifa, src);
   2312 	else if (src ||
   2313 	         (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
   2314 		rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
   2315 
   2316 	return rc;
   2317 }
   2318 
   2319 int
   2320 if_flags_set(ifnet_t *ifp, const short flags)
   2321 {
   2322 	int rc;
   2323 
   2324 	if (ifp->if_setflags != NULL)
   2325 		rc = (*ifp->if_setflags)(ifp, flags);
   2326 	else {
   2327 		short cantflags, chgdflags;
   2328 		struct ifreq ifr;
   2329 
   2330 		chgdflags = ifp->if_flags ^ flags;
   2331 		cantflags = chgdflags & IFF_CANTCHANGE;
   2332 
   2333 		if (cantflags != 0)
   2334 			ifp->if_flags ^= cantflags;
   2335 
   2336                 /* Traditionally, we do not call if_ioctl after
   2337                  * setting/clearing only IFF_PROMISC if the interface
   2338                  * isn't IFF_UP.  Uphold that tradition.
   2339 		 */
   2340 		if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
   2341 			return 0;
   2342 
   2343 		memset(&ifr, 0, sizeof(ifr));
   2344 
   2345 		ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
   2346 		rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
   2347 
   2348 		if (rc != 0 && cantflags != 0)
   2349 			ifp->if_flags ^= cantflags;
   2350 	}
   2351 
   2352 	return rc;
   2353 }
   2354 
   2355 int
   2356 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
   2357 {
   2358 	int rc;
   2359 	struct ifreq ifr;
   2360 
   2361 	if (ifp->if_mcastop != NULL)
   2362 		rc = (*ifp->if_mcastop)(ifp, cmd, sa);
   2363 	else {
   2364 		ifreq_setaddr(cmd, &ifr, sa);
   2365 		rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
   2366 	}
   2367 
   2368 	return rc;
   2369 }
   2370 
   2371 static void
   2372 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
   2373     struct ifaltq *ifq)
   2374 {
   2375 	const struct sysctlnode *cnode, *rnode;
   2376 
   2377 	if (sysctl_createv(clog, 0, NULL, &rnode,
   2378 		       CTLFLAG_PERMANENT,
   2379 		       CTLTYPE_NODE, "interfaces",
   2380 		       SYSCTL_DESCR("Per-interface controls"),
   2381 		       NULL, 0, NULL, 0,
   2382 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
   2383 		goto bad;
   2384 
   2385 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2386 		       CTLFLAG_PERMANENT,
   2387 		       CTLTYPE_NODE, ifname,
   2388 		       SYSCTL_DESCR("Interface controls"),
   2389 		       NULL, 0, NULL, 0,
   2390 		       CTL_CREATE, CTL_EOL) != 0)
   2391 		goto bad;
   2392 
   2393 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2394 		       CTLFLAG_PERMANENT,
   2395 		       CTLTYPE_NODE, "sndq",
   2396 		       SYSCTL_DESCR("Interface output queue controls"),
   2397 		       NULL, 0, NULL, 0,
   2398 		       CTL_CREATE, CTL_EOL) != 0)
   2399 		goto bad;
   2400 
   2401 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2402 		       CTLFLAG_PERMANENT,
   2403 		       CTLTYPE_INT, "len",
   2404 		       SYSCTL_DESCR("Current output queue length"),
   2405 		       NULL, 0, &ifq->ifq_len, 0,
   2406 		       CTL_CREATE, CTL_EOL) != 0)
   2407 		goto bad;
   2408 
   2409 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2410 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2411 		       CTLTYPE_INT, "maxlen",
   2412 		       SYSCTL_DESCR("Maximum allowed output queue length"),
   2413 		       NULL, 0, &ifq->ifq_maxlen, 0,
   2414 		       CTL_CREATE, CTL_EOL) != 0)
   2415 		goto bad;
   2416 
   2417 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   2418 		       CTLFLAG_PERMANENT,
   2419 		       CTLTYPE_INT, "drops",
   2420 		       SYSCTL_DESCR("Packets dropped due to full output queue"),
   2421 		       NULL, 0, &ifq->ifq_drops, 0,
   2422 		       CTL_CREATE, CTL_EOL) != 0)
   2423 		goto bad;
   2424 
   2425 	return;
   2426 bad:
   2427 	printf("%s: could not attach sysctl nodes\n", ifname);
   2428 	return;
   2429 }
   2430 
   2431 #if defined(INET) || defined(INET6)
   2432 
   2433 #define	SYSCTL_NET_PKTQ(q, cn, c)					\
   2434 	static int							\
   2435 	sysctl_net_##q##_##cn(SYSCTLFN_ARGS)				\
   2436 	{								\
   2437 		return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);	\
   2438 	}
   2439 
   2440 #if defined(INET)
   2441 static int
   2442 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
   2443 {
   2444 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
   2445 }
   2446 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
   2447 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
   2448 #endif
   2449 
   2450 #if defined(INET6)
   2451 static int
   2452 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
   2453 {
   2454 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
   2455 }
   2456 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
   2457 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
   2458 #endif
   2459 
   2460 static void
   2461 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
   2462 {
   2463 	sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
   2464 	const char *pfname = NULL, *ipname = NULL;
   2465 	int ipn = 0, qid = 0;
   2466 
   2467 	switch (pf) {
   2468 #if defined(INET)
   2469 	case PF_INET:
   2470 		len_func = sysctl_net_ip_pktq_items;
   2471 		maxlen_func = sysctl_net_ip_pktq_maxlen;
   2472 		drops_func = sysctl_net_ip_pktq_drops;
   2473 		pfname = "inet", ipn = IPPROTO_IP;
   2474 		ipname = "ip", qid = IPCTL_IFQ;
   2475 		break;
   2476 #endif
   2477 #if defined(INET6)
   2478 	case PF_INET6:
   2479 		len_func = sysctl_net_ip6_pktq_items;
   2480 		maxlen_func = sysctl_net_ip6_pktq_maxlen;
   2481 		drops_func = sysctl_net_ip6_pktq_drops;
   2482 		pfname = "inet6", ipn = IPPROTO_IPV6;
   2483 		ipname = "ip6", qid = IPV6CTL_IFQ;
   2484 		break;
   2485 #endif
   2486 	default:
   2487 		KASSERT(false);
   2488 	}
   2489 
   2490 	sysctl_createv(clog, 0, NULL, NULL,
   2491 		       CTLFLAG_PERMANENT,
   2492 		       CTLTYPE_NODE, pfname, NULL,
   2493 		       NULL, 0, NULL, 0,
   2494 		       CTL_NET, pf, CTL_EOL);
   2495 	sysctl_createv(clog, 0, NULL, NULL,
   2496 		       CTLFLAG_PERMANENT,
   2497 		       CTLTYPE_NODE, ipname, NULL,
   2498 		       NULL, 0, NULL, 0,
   2499 		       CTL_NET, pf, ipn, CTL_EOL);
   2500 	sysctl_createv(clog, 0, NULL, NULL,
   2501 		       CTLFLAG_PERMANENT,
   2502 		       CTLTYPE_NODE, "ifq",
   2503 		       SYSCTL_DESCR("Protocol input queue controls"),
   2504 		       NULL, 0, NULL, 0,
   2505 		       CTL_NET, pf, ipn, qid, CTL_EOL);
   2506 
   2507 	sysctl_createv(clog, 0, NULL, NULL,
   2508 		       CTLFLAG_PERMANENT,
   2509 		       CTLTYPE_INT, "len",
   2510 		       SYSCTL_DESCR("Current input queue length"),
   2511 		       len_func, 0, NULL, 0,
   2512 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
   2513 	sysctl_createv(clog, 0, NULL, NULL,
   2514 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2515 		       CTLTYPE_INT, "maxlen",
   2516 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   2517 		       maxlen_func, 0, NULL, 0,
   2518 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
   2519 	sysctl_createv(clog, 0, NULL, NULL,
   2520 		       CTLFLAG_PERMANENT,
   2521 		       CTLTYPE_INT, "drops",
   2522 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
   2523 		       drops_func, 0, NULL, 0,
   2524 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
   2525 }
   2526 #endif /* INET || INET6 */
   2527 
   2528 static int
   2529 if_sdl_sysctl(SYSCTLFN_ARGS)
   2530 {
   2531 	struct ifnet *ifp;
   2532 	const struct sockaddr_dl *sdl;
   2533 
   2534 	if (namelen != 1)
   2535 		return EINVAL;
   2536 
   2537 	ifp = if_byindex(name[0]);
   2538 	if (ifp == NULL)
   2539 		return ENODEV;
   2540 
   2541 	sdl = ifp->if_sadl;
   2542 	if (sdl == NULL) {
   2543 		*oldlenp = 0;
   2544 		return 0;
   2545 	}
   2546 
   2547 	if (oldp == NULL) {
   2548 		*oldlenp = sdl->sdl_alen;
   2549 		return 0;
   2550 	}
   2551 
   2552 	if (*oldlenp >= sdl->sdl_alen)
   2553 		*oldlenp = sdl->sdl_alen;
   2554 	return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
   2555 }
   2556 
   2557 SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
   2558 {
   2559 	const struct sysctlnode *rnode = NULL;
   2560 
   2561 	sysctl_createv(clog, 0, NULL, &rnode,
   2562 		       CTLFLAG_PERMANENT,
   2563 		       CTLTYPE_NODE, "sdl",
   2564 		       SYSCTL_DESCR("Get active link-layer address"),
   2565 		       if_sdl_sysctl, 0, NULL, 0,
   2566 		       CTL_NET, CTL_CREATE, CTL_EOL);
   2567 }
   2568