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