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