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