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