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