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