Home | History | Annotate | Line # | Download | only in net
if.c revision 1.515
      1 /*	$NetBSD: if.c,v 1.515 2022/08/20 12:42:51 riastradh 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.515 2022/08/20 12:42:51 riastradh Exp $");
     94 
     95 #if defined(_KERNEL_OPT)
     96 #include "opt_inet.h"
     97 #include "opt_ipsec.h"
     98 #include "opt_atalk.h"
     99 #include "opt_wlan.h"
    100 #include "opt_net_mpsafe.h"
    101 #include "opt_mrouting.h"
    102 #endif
    103 
    104 #include <sys/param.h>
    105 #include <sys/mbuf.h>
    106 #include <sys/systm.h>
    107 #include <sys/callout.h>
    108 #include <sys/proc.h>
    109 #include <sys/socket.h>
    110 #include <sys/socketvar.h>
    111 #include <sys/domain.h>
    112 #include <sys/protosw.h>
    113 #include <sys/kernel.h>
    114 #include <sys/ioctl.h>
    115 #include <sys/sysctl.h>
    116 #include <sys/syslog.h>
    117 #include <sys/kauth.h>
    118 #include <sys/kmem.h>
    119 #include <sys/xcall.h>
    120 #include <sys/cpu.h>
    121 #include <sys/intr.h>
    122 #include <sys/module_hook.h>
    123 #include <sys/compat_stub.h>
    124 #include <sys/msan.h>
    125 #include <sys/hook.h>
    126 
    127 #include <net/if.h>
    128 #include <net/if_dl.h>
    129 #include <net/if_ether.h>
    130 #include <net/if_media.h>
    131 #include <net80211/ieee80211.h>
    132 #include <net80211/ieee80211_ioctl.h>
    133 #include <net/if_types.h>
    134 #include <net/route.h>
    135 #include <net/netisr.h>
    136 #include <sys/module.h>
    137 #ifdef NETATALK
    138 #include <netatalk/at_extern.h>
    139 #include <netatalk/at.h>
    140 #endif
    141 #include <net/pfil.h>
    142 #include <netinet/in.h>
    143 #include <netinet/in_var.h>
    144 #include <netinet/ip_encap.h>
    145 #include <net/bpf.h>
    146 
    147 #ifdef INET6
    148 #include <netinet6/in6_var.h>
    149 #include <netinet6/nd6.h>
    150 #endif
    151 
    152 #include "ether.h"
    153 
    154 #include "bridge.h"
    155 #if NBRIDGE > 0
    156 #include <net/if_bridgevar.h>
    157 #endif
    158 
    159 #include "carp.h"
    160 #if NCARP > 0
    161 #include <netinet/ip_carp.h>
    162 #endif
    163 
    164 #include <compat/sys/sockio.h>
    165 
    166 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
    167 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
    168 
    169 /*
    170  * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex
    171  * for each ifnet.  It doesn't matter because:
    172  * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on
    173  *   ifq_lock don't happen
    174  * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock
    175  *   because if_snd, if_link_state_change and if_link_state_change_process
    176  *   are all called with KERNEL_LOCK
    177  */
    178 #define IF_LINK_STATE_CHANGE_LOCK(ifp)		\
    179 	mutex_enter((ifp)->if_snd.ifq_lock)
    180 #define IF_LINK_STATE_CHANGE_UNLOCK(ifp)	\
    181 	mutex_exit((ifp)->if_snd.ifq_lock)
    182 
    183 /*
    184  * Global list of interfaces.
    185  */
    186 /* DEPRECATED. Remove it once kvm(3) users disappeared */
    187 struct ifnet_head		ifnet_list;
    188 
    189 struct pslist_head		ifnet_pslist;
    190 static ifnet_t **		ifindex2ifnet = NULL;
    191 static u_int			if_index = 1;
    192 static size_t			if_indexlim = 0;
    193 static uint64_t			index_gen;
    194 /* Mutex to protect the above objects. */
    195 kmutex_t			ifnet_mtx __cacheline_aligned;
    196 static struct psref_class	*ifnet_psref_class __read_mostly;
    197 static pserialize_t		ifnet_psz;
    198 static struct workqueue		*ifnet_link_state_wq __read_mostly;
    199 
    200 static struct workqueue		*if_slowtimo_wq __read_mostly;
    201 
    202 static kmutex_t			if_clone_mtx;
    203 
    204 struct ifnet *lo0ifp;
    205 int	ifqmaxlen = IFQ_MAXLEN;
    206 
    207 struct psref_class		*ifa_psref_class __read_mostly;
    208 
    209 static int	if_delroute_matcher(struct rtentry *, void *);
    210 
    211 static bool if_is_unit(const char *);
    212 static struct if_clone *if_clone_lookup(const char *, int *);
    213 
    214 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
    215 static int if_cloners_count;
    216 
    217 /* Packet filtering hook for interfaces. */
    218 pfil_head_t *			if_pfil __read_mostly;
    219 
    220 static kauth_listener_t if_listener;
    221 
    222 static int doifioctl(struct socket *, u_long, void *, struct lwp *);
    223 static void if_detach_queues(struct ifnet *, struct ifqueue *);
    224 static void sysctl_sndq_setup(struct sysctllog **, const char *,
    225     struct ifaltq *);
    226 static void if_slowtimo_intr(void *);
    227 static void if_slowtimo_work(struct work *, void *);
    228 static int sysctl_if_watchdog(SYSCTLFN_PROTO);
    229 static void sysctl_watchdog_setup(struct ifnet *);
    230 static void if_attachdomain1(struct ifnet *);
    231 static int ifconf(u_long, void *);
    232 static int if_transmit(struct ifnet *, struct mbuf *);
    233 static int if_clone_create(const char *);
    234 static int if_clone_destroy(const char *);
    235 static void if_link_state_change_work(struct work *, void *);
    236 static void if_up_locked(struct ifnet *);
    237 static void _if_down(struct ifnet *);
    238 static void if_down_deactivated(struct ifnet *);
    239 
    240 struct if_percpuq {
    241 	struct ifnet	*ipq_ifp;
    242 	void		*ipq_si;
    243 	struct percpu	*ipq_ifqs;	/* struct ifqueue */
    244 };
    245 
    246 static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
    247 
    248 static void if_percpuq_drops(void *, void *, struct cpu_info *);
    249 static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
    250 static void sysctl_percpuq_setup(struct sysctllog **, const char *,
    251     struct if_percpuq *);
    252 
    253 struct if_deferred_start {
    254 	struct ifnet	*ids_ifp;
    255 	void		(*ids_if_start)(struct ifnet *);
    256 	void		*ids_si;
    257 };
    258 
    259 static void if_deferred_start_softint(void *);
    260 static void if_deferred_start_common(struct ifnet *);
    261 static void if_deferred_start_destroy(struct ifnet *);
    262 
    263 struct if_slowtimo_data {
    264 	kmutex_t		isd_lock;
    265 	struct callout		isd_ch;
    266 	struct work		isd_work;
    267 	struct ifnet		*isd_ifp;
    268 	bool			isd_queued;
    269 	bool			isd_dying;
    270 	bool			isd_trigger;
    271 };
    272 
    273 #if defined(INET) || defined(INET6)
    274 static void sysctl_net_pktq_setup(struct sysctllog **, int);
    275 #endif
    276 
    277 /*
    278  * Hook for if_vlan - needed by if_agr
    279  */
    280 struct if_vlan_vlan_input_hook_t if_vlan_vlan_input_hook;
    281 
    282 static void if_sysctl_setup(struct sysctllog **);
    283 
    284 static int
    285 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    286     void *arg0, void *arg1, void *arg2, void *arg3)
    287 {
    288 	int result;
    289 	enum kauth_network_req req;
    290 
    291 	result = KAUTH_RESULT_DEFER;
    292 	req = (enum kauth_network_req)(uintptr_t)arg1;
    293 
    294 	if (action != KAUTH_NETWORK_INTERFACE)
    295 		return result;
    296 
    297 	if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
    298 	    (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
    299 		result = KAUTH_RESULT_ALLOW;
    300 
    301 	return result;
    302 }
    303 
    304 /*
    305  * Network interface utility routines.
    306  *
    307  * Routines with ifa_ifwith* names take sockaddr *'s as
    308  * parameters.
    309  */
    310 void
    311 ifinit(void)
    312 {
    313 
    314 #if (defined(INET) || defined(INET6))
    315 	encapinit();
    316 #endif
    317 
    318 	if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
    319 	    if_listener_cb, NULL);
    320 
    321 	/* interfaces are available, inform socket code */
    322 	ifioctl = doifioctl;
    323 }
    324 
    325 /*
    326  * XXX Initialization before configure().
    327  * XXX hack to get pfil_add_hook working in autoconf.
    328  */
    329 void
    330 ifinit1(void)
    331 {
    332 	int error __diagused;
    333 
    334 #ifdef NET_MPSAFE
    335 	printf("NET_MPSAFE enabled\n");
    336 #endif
    337 
    338 	mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
    339 
    340 	TAILQ_INIT(&ifnet_list);
    341 	mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
    342 	ifnet_psz = pserialize_create();
    343 	ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
    344 	ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET);
    345 	error = workqueue_create(&ifnet_link_state_wq, "iflnkst",
    346 	    if_link_state_change_work, NULL, PRI_SOFTNET, IPL_NET,
    347 	    WQ_MPSAFE);
    348 	KASSERT(error == 0);
    349 	PSLIST_INIT(&ifnet_pslist);
    350 
    351 	error = workqueue_create(&if_slowtimo_wq, "ifwdog",
    352 	    if_slowtimo_work, NULL, PRI_SOFTNET, IPL_SOFTCLOCK, WQ_MPSAFE);
    353 	KASSERTMSG(error == 0, "error=%d", error);
    354 
    355 	if_indexlim = 8;
    356 
    357 	if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
    358 	KASSERT(if_pfil != NULL);
    359 
    360 #if NETHER > 0 || defined(NETATALK) || defined(WLAN)
    361 	etherinit();
    362 #endif
    363 }
    364 
    365 /* XXX must be after domaininit() */
    366 void
    367 ifinit_post(void)
    368 {
    369 
    370 	if_sysctl_setup(NULL);
    371 }
    372 
    373 ifnet_t *
    374 if_alloc(u_char type)
    375 {
    376 	return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
    377 }
    378 
    379 void
    380 if_free(ifnet_t *ifp)
    381 {
    382 	kmem_free(ifp, sizeof(ifnet_t));
    383 }
    384 
    385 void
    386 if_initname(struct ifnet *ifp, const char *name, int unit)
    387 {
    388 	(void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
    389 	    "%s%d", name, unit);
    390 }
    391 
    392 /*
    393  * Null routines used while an interface is going away.  These routines
    394  * just return an error.
    395  */
    396 
    397 int
    398 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
    399     const struct sockaddr *so, const struct rtentry *rt)
    400 {
    401 
    402 	return ENXIO;
    403 }
    404 
    405 void
    406 if_nullinput(struct ifnet *ifp, struct mbuf *m)
    407 {
    408 
    409 	/* Nothing. */
    410 }
    411 
    412 void
    413 if_nullstart(struct ifnet *ifp)
    414 {
    415 
    416 	/* Nothing. */
    417 }
    418 
    419 int
    420 if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
    421 {
    422 
    423 	m_freem(m);
    424 	return ENXIO;
    425 }
    426 
    427 int
    428 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
    429 {
    430 
    431 	return ENXIO;
    432 }
    433 
    434 int
    435 if_nullinit(struct ifnet *ifp)
    436 {
    437 
    438 	return ENXIO;
    439 }
    440 
    441 void
    442 if_nullstop(struct ifnet *ifp, int disable)
    443 {
    444 
    445 	/* Nothing. */
    446 }
    447 
    448 void
    449 if_nullslowtimo(struct ifnet *ifp)
    450 {
    451 
    452 	/* Nothing. */
    453 }
    454 
    455 void
    456 if_nulldrain(struct ifnet *ifp)
    457 {
    458 
    459 	/* Nothing. */
    460 }
    461 
    462 void
    463 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
    464 {
    465 	struct ifaddr *ifa;
    466 	struct sockaddr_dl *sdl;
    467 
    468 	ifp->if_addrlen = addrlen;
    469 	if_alloc_sadl(ifp);
    470 	ifa = ifp->if_dl;
    471 	sdl = satosdl(ifa->ifa_addr);
    472 
    473 	(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
    474 	if (factory) {
    475 		KASSERT(ifp->if_hwdl == NULL);
    476 		ifp->if_hwdl = ifp->if_dl;
    477 		ifaref(ifp->if_hwdl);
    478 	}
    479 	/* TBD routing socket */
    480 }
    481 
    482 struct ifaddr *
    483 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
    484 {
    485 	unsigned socksize, ifasize;
    486 	int addrlen, namelen;
    487 	struct sockaddr_dl *mask, *sdl;
    488 	struct ifaddr *ifa;
    489 
    490 	namelen = strlen(ifp->if_xname);
    491 	addrlen = ifp->if_addrlen;
    492 	socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
    493 	ifasize = sizeof(*ifa) + 2 * socksize;
    494 	ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
    495 
    496 	sdl = (struct sockaddr_dl *)(ifa + 1);
    497 	mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
    498 
    499 	sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
    500 	    ifp->if_xname, namelen, NULL, addrlen);
    501 	mask->sdl_family = AF_LINK;
    502 	mask->sdl_len = sockaddr_dl_measure(namelen, 0);
    503 	memset(&mask->sdl_data[0], 0xff, namelen);
    504 	ifa->ifa_rtrequest = link_rtrequest;
    505 	ifa->ifa_addr = (struct sockaddr *)sdl;
    506 	ifa->ifa_netmask = (struct sockaddr *)mask;
    507 	ifa_psref_init(ifa);
    508 
    509 	*sdlp = sdl;
    510 
    511 	return ifa;
    512 }
    513 
    514 static void
    515 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
    516 {
    517 	const struct sockaddr_dl *sdl;
    518 
    519 	ifp->if_dl = ifa;
    520 	ifaref(ifa);
    521 	sdl = satosdl(ifa->ifa_addr);
    522 	ifp->if_sadl = sdl;
    523 }
    524 
    525 /*
    526  * Allocate the link level name for the specified interface.  This
    527  * is an attachment helper.  It must be called after ifp->if_addrlen
    528  * is initialized, which may not be the case when if_attach() is
    529  * called.
    530  */
    531 void
    532 if_alloc_sadl(struct ifnet *ifp)
    533 {
    534 	struct ifaddr *ifa;
    535 	const struct sockaddr_dl *sdl;
    536 
    537 	/*
    538 	 * If the interface already has a link name, release it
    539 	 * now.  This is useful for interfaces that can change
    540 	 * link types, and thus switch link names often.
    541 	 */
    542 	if (ifp->if_sadl != NULL)
    543 		if_free_sadl(ifp, 0);
    544 
    545 	ifa = if_dl_create(ifp, &sdl);
    546 
    547 	ifa_insert(ifp, ifa);
    548 	if_sadl_setrefs(ifp, ifa);
    549 }
    550 
    551 static void
    552 if_deactivate_sadl(struct ifnet *ifp)
    553 {
    554 	struct ifaddr *ifa;
    555 
    556 	KASSERT(ifp->if_dl != NULL);
    557 
    558 	ifa = ifp->if_dl;
    559 
    560 	ifp->if_sadl = NULL;
    561 
    562 	ifp->if_dl = NULL;
    563 	ifafree(ifa);
    564 }
    565 
    566 static void
    567 if_replace_sadl(struct ifnet *ifp, struct ifaddr *ifa)
    568 {
    569 	struct ifaddr *old;
    570 
    571 	KASSERT(ifp->if_dl != NULL);
    572 
    573 	old = ifp->if_dl;
    574 
    575 	ifaref(ifa);
    576 	/* XXX Update if_dl and if_sadl atomically */
    577 	ifp->if_dl = ifa;
    578 	ifp->if_sadl = satosdl(ifa->ifa_addr);
    579 
    580 	ifafree(old);
    581 }
    582 
    583 void
    584 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa0,
    585     const struct sockaddr_dl *sdl)
    586 {
    587 	int s, ss;
    588 	struct ifaddr *ifa;
    589 	int bound = curlwp_bind();
    590 
    591 	KASSERT(ifa_held(ifa0));
    592 
    593 	s = splsoftnet();
    594 
    595 	if_replace_sadl(ifp, ifa0);
    596 
    597 	ss = pserialize_read_enter();
    598 	IFADDR_READER_FOREACH(ifa, ifp) {
    599 		struct psref psref;
    600 		ifa_acquire(ifa, &psref);
    601 		pserialize_read_exit(ss);
    602 
    603 		rtinit(ifa, RTM_LLINFO_UPD, 0);
    604 
    605 		ss = pserialize_read_enter();
    606 		ifa_release(ifa, &psref);
    607 	}
    608 	pserialize_read_exit(ss);
    609 
    610 	splx(s);
    611 	curlwp_bindx(bound);
    612 }
    613 
    614 /*
    615  * Free the link level name for the specified interface.  This is
    616  * a detach helper.  This is called from if_detach().
    617  */
    618 void
    619 if_free_sadl(struct ifnet *ifp, int factory)
    620 {
    621 	struct ifaddr *ifa;
    622 	int s;
    623 
    624 	if (factory && ifp->if_hwdl != NULL) {
    625 		ifa = ifp->if_hwdl;
    626 		ifp->if_hwdl = NULL;
    627 		ifafree(ifa);
    628 	}
    629 
    630 	ifa = ifp->if_dl;
    631 	if (ifa == NULL) {
    632 		KASSERT(ifp->if_sadl == NULL);
    633 		return;
    634 	}
    635 
    636 	KASSERT(ifp->if_sadl != NULL);
    637 
    638 	s = splsoftnet();
    639 	KASSERT(ifa->ifa_addr->sa_family == AF_LINK);
    640 	ifa_remove(ifp, ifa);
    641 	if_deactivate_sadl(ifp);
    642 	splx(s);
    643 }
    644 
    645 static void
    646 if_getindex(ifnet_t *ifp)
    647 {
    648 	bool hitlimit = false;
    649 	char xnamebuf[HOOKNAMSIZ];
    650 
    651 	ifp->if_index_gen = index_gen++;
    652 	snprintf(xnamebuf, sizeof(xnamebuf),
    653 	    "%s-lshk", ifp->if_xname);
    654 	ifp->if_linkstate_hooks = simplehook_create(IPL_NET,
    655 	    xnamebuf);
    656 
    657 	ifp->if_index = if_index;
    658 	if (ifindex2ifnet == NULL) {
    659 		if_index++;
    660 		goto skip;
    661 	}
    662 	while (if_byindex(ifp->if_index)) {
    663 		/*
    664 		 * If we hit USHRT_MAX, we skip back to 0 since
    665 		 * there are a number of places where the value
    666 		 * of if_index or if_index itself is compared
    667 		 * to or stored in an unsigned short.  By
    668 		 * jumping back, we won't botch those assignments
    669 		 * or comparisons.
    670 		 */
    671 		if (++if_index == 0) {
    672 			if_index = 1;
    673 		} else if (if_index == USHRT_MAX) {
    674 			/*
    675 			 * However, if we have to jump back to
    676 			 * zero *twice* without finding an empty
    677 			 * slot in ifindex2ifnet[], then there
    678 			 * there are too many (>65535) interfaces.
    679 			 */
    680 			if (hitlimit) {
    681 				panic("too many interfaces");
    682 			}
    683 			hitlimit = true;
    684 			if_index = 1;
    685 		}
    686 		ifp->if_index = if_index;
    687 	}
    688 skip:
    689 	/*
    690 	 * ifindex2ifnet is indexed by if_index. Since if_index will
    691 	 * grow dynamically, it should grow too.
    692 	 */
    693 	if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
    694 		size_t m, n, oldlim;
    695 		void *q;
    696 
    697 		oldlim = if_indexlim;
    698 		while (ifp->if_index >= if_indexlim)
    699 			if_indexlim <<= 1;
    700 
    701 		/* grow ifindex2ifnet */
    702 		m = oldlim * sizeof(struct ifnet *);
    703 		n = if_indexlim * sizeof(struct ifnet *);
    704 		q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
    705 		if (ifindex2ifnet != NULL) {
    706 			memcpy(q, ifindex2ifnet, m);
    707 			free(ifindex2ifnet, M_IFADDR);
    708 		}
    709 		ifindex2ifnet = (struct ifnet **)q;
    710 	}
    711 	ifindex2ifnet[ifp->if_index] = ifp;
    712 }
    713 
    714 /*
    715  * Initialize an interface and assign an index for it.
    716  *
    717  * It must be called prior to a device specific attach routine
    718  * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
    719  * and be followed by if_register:
    720  *
    721  *     if_initialize(ifp);
    722  *     ether_ifattach(ifp, enaddr);
    723  *     if_register(ifp);
    724  */
    725 void
    726 if_initialize(ifnet_t *ifp)
    727 {
    728 
    729 	KASSERT(if_indexlim > 0);
    730 	TAILQ_INIT(&ifp->if_addrlist);
    731 
    732 	/*
    733 	 * Link level name is allocated later by a separate call to
    734 	 * if_alloc_sadl().
    735 	 */
    736 
    737 	if (ifp->if_snd.ifq_maxlen == 0)
    738 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
    739 
    740 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    741 
    742 	ifp->if_link_state = LINK_STATE_UNKNOWN;
    743 	ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
    744 	ifp->if_link_scheduled = false;
    745 
    746 	ifp->if_capenable = 0;
    747 	ifp->if_csum_flags_tx = 0;
    748 	ifp->if_csum_flags_rx = 0;
    749 
    750 #ifdef ALTQ
    751 	ifp->if_snd.altq_type = 0;
    752 	ifp->if_snd.altq_disc = NULL;
    753 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
    754 	ifp->if_snd.altq_tbr  = NULL;
    755 	ifp->if_snd.altq_ifp  = ifp;
    756 #endif
    757 
    758 	IFQ_LOCK_INIT(&ifp->if_snd);
    759 
    760 	ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
    761 	pfil_run_ifhooks(if_pfil, PFIL_IFNET_ATTACH, ifp);
    762 
    763 	IF_AFDATA_LOCK_INIT(ifp);
    764 
    765 	PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
    766 	PSLIST_INIT(&ifp->if_addr_pslist);
    767 	psref_target_init(&ifp->if_psref, ifnet_psref_class);
    768 	ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    769 	LIST_INIT(&ifp->if_multiaddrs);
    770 	if_stats_init(ifp);
    771 
    772 	IFNET_GLOBAL_LOCK();
    773 	if_getindex(ifp);
    774 	IFNET_GLOBAL_UNLOCK();
    775 }
    776 
    777 /*
    778  * Register an interface to the list of "active" interfaces.
    779  */
    780 void
    781 if_register(ifnet_t *ifp)
    782 {
    783 	/*
    784 	 * If the driver has not supplied its own if_ioctl or if_stop,
    785 	 * then supply the default.
    786 	 */
    787 	if (ifp->if_ioctl == NULL)
    788 		ifp->if_ioctl = ifioctl_common;
    789 	if (ifp->if_stop == NULL)
    790 		ifp->if_stop = if_nullstop;
    791 
    792 	sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
    793 
    794 	if (!STAILQ_EMPTY(&domains))
    795 		if_attachdomain1(ifp);
    796 
    797 	/* Announce the interface. */
    798 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
    799 
    800 	if (ifp->if_slowtimo != NULL) {
    801 		struct if_slowtimo_data *isd;
    802 
    803 		isd = kmem_zalloc(sizeof(*isd), KM_SLEEP);
    804 		mutex_init(&isd->isd_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    805 		callout_init(&isd->isd_ch, CALLOUT_MPSAFE);
    806 		callout_setfunc(&isd->isd_ch, if_slowtimo_intr, ifp);
    807 		isd->isd_ifp = ifp;
    808 
    809 		ifp->if_slowtimo_data = isd;
    810 
    811 		if_slowtimo_intr(ifp);
    812 
    813 		sysctl_watchdog_setup(ifp);
    814 	}
    815 
    816 	if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
    817 		ifp->if_transmit = if_transmit;
    818 
    819 	IFNET_GLOBAL_LOCK();
    820 	TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
    821 	IFNET_WRITER_INSERT_TAIL(ifp);
    822 	IFNET_GLOBAL_UNLOCK();
    823 }
    824 
    825 /*
    826  * The if_percpuq framework
    827  *
    828  * It allows network device drivers to execute the network stack
    829  * in softint (so called softint-based if_input). It utilizes
    830  * softint and percpu ifqueue. It doesn't distribute any packets
    831  * between CPUs, unlike pktqueue(9).
    832  *
    833  * Currently we support two options for device drivers to apply the framework:
    834  * - Use it implicitly with less changes
    835  *   - If you use if_attach in driver's _attach function and if_input in
    836  *     driver's Rx interrupt handler, a packet is queued and a softint handles
    837  *     the packet implicitly
    838  * - Use it explicitly in each driver (recommended)
    839  *   - You can use if_percpuq_* directly in your driver
    840  *   - In this case, you need to allocate struct if_percpuq in driver's softc
    841  *   - See wm(4) as a reference implementation
    842  */
    843 
    844 static void
    845 if_percpuq_softint(void *arg)
    846 {
    847 	struct if_percpuq *ipq = arg;
    848 	struct ifnet *ifp = ipq->ipq_ifp;
    849 	struct mbuf *m;
    850 
    851 	while ((m = if_percpuq_dequeue(ipq)) != NULL) {
    852 		if_statinc(ifp, if_ipackets);
    853 		bpf_mtap(ifp, m, BPF_D_IN);
    854 
    855 		ifp->_if_input(ifp, m);
    856 	}
    857 }
    858 
    859 static void
    860 if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
    861 {
    862 	struct ifqueue *const ifq = p;
    863 
    864 	memset(ifq, 0, sizeof(*ifq));
    865 	ifq->ifq_maxlen = IFQ_MAXLEN;
    866 }
    867 
    868 struct if_percpuq *
    869 if_percpuq_create(struct ifnet *ifp)
    870 {
    871 	struct if_percpuq *ipq;
    872 	u_int flags = SOFTINT_NET;
    873 
    874 	flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
    875 
    876 	ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
    877 	ipq->ipq_ifp = ifp;
    878 	ipq->ipq_si = softint_establish(flags, if_percpuq_softint, ipq);
    879 	ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
    880 	percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
    881 
    882 	sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
    883 
    884 	return ipq;
    885 }
    886 
    887 static struct mbuf *
    888 if_percpuq_dequeue(struct if_percpuq *ipq)
    889 {
    890 	struct mbuf *m;
    891 	struct ifqueue *ifq;
    892 	int s;
    893 
    894 	s = splnet();
    895 	ifq = percpu_getref(ipq->ipq_ifqs);
    896 	IF_DEQUEUE(ifq, m);
    897 	percpu_putref(ipq->ipq_ifqs);
    898 	splx(s);
    899 
    900 	return m;
    901 }
    902 
    903 static void
    904 if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
    905 {
    906 	struct ifqueue *const ifq = p;
    907 
    908 	IF_PURGE(ifq);
    909 }
    910 
    911 void
    912 if_percpuq_destroy(struct if_percpuq *ipq)
    913 {
    914 
    915 	/* if_detach may already destroy it */
    916 	if (ipq == NULL)
    917 		return;
    918 
    919 	softint_disestablish(ipq->ipq_si);
    920 	percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
    921 	percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
    922 	kmem_free(ipq, sizeof(*ipq));
    923 }
    924 
    925 void
    926 if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
    927 {
    928 	struct ifqueue *ifq;
    929 	int s;
    930 
    931 	KASSERT(ipq != NULL);
    932 
    933 	s = splnet();
    934 	ifq = percpu_getref(ipq->ipq_ifqs);
    935 	if (IF_QFULL(ifq)) {
    936 		IF_DROP(ifq);
    937 		percpu_putref(ipq->ipq_ifqs);
    938 		m_freem(m);
    939 		goto out;
    940 	}
    941 	IF_ENQUEUE(ifq, m);
    942 	percpu_putref(ipq->ipq_ifqs);
    943 
    944 	softint_schedule(ipq->ipq_si);
    945 out:
    946 	splx(s);
    947 }
    948 
    949 static void
    950 if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
    951 {
    952 	struct ifqueue *const ifq = p;
    953 	int *sum = arg;
    954 
    955 	*sum += ifq->ifq_drops;
    956 }
    957 
    958 static int
    959 sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
    960 {
    961 	struct sysctlnode node;
    962 	struct if_percpuq *ipq;
    963 	int sum = 0;
    964 	int error;
    965 
    966 	node = *rnode;
    967 	ipq = node.sysctl_data;
    968 
    969 	percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
    970 
    971 	node.sysctl_data = &sum;
    972 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    973 	if (error != 0 || newp == NULL)
    974 		return error;
    975 
    976 	return 0;
    977 }
    978 
    979 static void
    980 sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
    981     struct if_percpuq *ipq)
    982 {
    983 	const struct sysctlnode *cnode, *rnode;
    984 
    985 	if (sysctl_createv(clog, 0, NULL, &rnode,
    986 		       CTLFLAG_PERMANENT,
    987 		       CTLTYPE_NODE, "interfaces",
    988 		       SYSCTL_DESCR("Per-interface controls"),
    989 		       NULL, 0, NULL, 0,
    990 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
    991 		goto bad;
    992 
    993 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    994 		       CTLFLAG_PERMANENT,
    995 		       CTLTYPE_NODE, ifname,
    996 		       SYSCTL_DESCR("Interface controls"),
    997 		       NULL, 0, NULL, 0,
    998 		       CTL_CREATE, CTL_EOL) != 0)
    999 		goto bad;
   1000 
   1001 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   1002 		       CTLFLAG_PERMANENT,
   1003 		       CTLTYPE_NODE, "rcvq",
   1004 		       SYSCTL_DESCR("Interface input queue controls"),
   1005 		       NULL, 0, NULL, 0,
   1006 		       CTL_CREATE, CTL_EOL) != 0)
   1007 		goto bad;
   1008 
   1009 #ifdef NOTYET
   1010 	/* XXX Should show each per-CPU queue length? */
   1011 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   1012 		       CTLFLAG_PERMANENT,
   1013 		       CTLTYPE_INT, "len",
   1014 		       SYSCTL_DESCR("Current input queue length"),
   1015 		       sysctl_percpuq_len, 0, NULL, 0,
   1016 		       CTL_CREATE, CTL_EOL) != 0)
   1017 		goto bad;
   1018 
   1019 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   1020 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1021 		       CTLTYPE_INT, "maxlen",
   1022 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   1023 		       sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
   1024 		       CTL_CREATE, CTL_EOL) != 0)
   1025 		goto bad;
   1026 #endif
   1027 
   1028 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   1029 		       CTLFLAG_PERMANENT,
   1030 		       CTLTYPE_INT, "drops",
   1031 		       SYSCTL_DESCR("Total packets dropped due to full input queue"),
   1032 		       sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
   1033 		       CTL_CREATE, CTL_EOL) != 0)
   1034 		goto bad;
   1035 
   1036 	return;
   1037 bad:
   1038 	printf("%s: could not attach sysctl nodes\n", ifname);
   1039 	return;
   1040 }
   1041 
   1042 /*
   1043  * The deferred if_start framework
   1044  *
   1045  * The common APIs to defer if_start to softint when if_start is requested
   1046  * from a device driver running in hardware interrupt context.
   1047  */
   1048 /*
   1049  * Call ifp->if_start (or equivalent) in a dedicated softint for
   1050  * deferred if_start.
   1051  */
   1052 static void
   1053 if_deferred_start_softint(void *arg)
   1054 {
   1055 	struct if_deferred_start *ids = arg;
   1056 	struct ifnet *ifp = ids->ids_ifp;
   1057 
   1058 	ids->ids_if_start(ifp);
   1059 }
   1060 
   1061 /*
   1062  * The default callback function for deferred if_start.
   1063  */
   1064 static void
   1065 if_deferred_start_common(struct ifnet *ifp)
   1066 {
   1067 	int s;
   1068 
   1069 	s = splnet();
   1070 	if_start_lock(ifp);
   1071 	splx(s);
   1072 }
   1073 
   1074 static inline bool
   1075 if_snd_is_used(struct ifnet *ifp)
   1076 {
   1077 
   1078 	return ALTQ_IS_ENABLED(&ifp->if_snd) ||
   1079 		ifp->if_transmit == if_transmit ||
   1080 		ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit;
   1081 }
   1082 
   1083 /*
   1084  * Schedule deferred if_start.
   1085  */
   1086 void
   1087 if_schedule_deferred_start(struct ifnet *ifp)
   1088 {
   1089 
   1090 	KASSERT(ifp->if_deferred_start != NULL);
   1091 
   1092 	if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd))
   1093 		return;
   1094 
   1095 	softint_schedule(ifp->if_deferred_start->ids_si);
   1096 }
   1097 
   1098 /*
   1099  * Create an instance of deferred if_start. A driver should call the function
   1100  * only if the driver needs deferred if_start. Drivers can setup their own
   1101  * deferred if_start function via 2nd argument.
   1102  */
   1103 void
   1104 if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *))
   1105 {
   1106 	struct if_deferred_start *ids;
   1107 	u_int flags = SOFTINT_NET;
   1108 
   1109 	flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
   1110 
   1111 	ids = kmem_zalloc(sizeof(*ids), KM_SLEEP);
   1112 	ids->ids_ifp = ifp;
   1113 	ids->ids_si = softint_establish(flags, if_deferred_start_softint, ids);
   1114 	if (func != NULL)
   1115 		ids->ids_if_start = func;
   1116 	else
   1117 		ids->ids_if_start = if_deferred_start_common;
   1118 
   1119 	ifp->if_deferred_start = ids;
   1120 }
   1121 
   1122 static void
   1123 if_deferred_start_destroy(struct ifnet *ifp)
   1124 {
   1125 
   1126 	if (ifp->if_deferred_start == NULL)
   1127 		return;
   1128 
   1129 	softint_disestablish(ifp->if_deferred_start->ids_si);
   1130 	kmem_free(ifp->if_deferred_start, sizeof(*ifp->if_deferred_start));
   1131 	ifp->if_deferred_start = NULL;
   1132 }
   1133 
   1134 /*
   1135  * The common interface input routine that is called by device drivers,
   1136  * which should be used only when the driver's rx handler already runs
   1137  * in softint.
   1138  */
   1139 void
   1140 if_input(struct ifnet *ifp, struct mbuf *m)
   1141 {
   1142 
   1143 	KASSERT(ifp->if_percpuq == NULL);
   1144 	KASSERT(!cpu_intr_p());
   1145 
   1146 	if_statinc(ifp, if_ipackets);
   1147 	bpf_mtap(ifp, m, BPF_D_IN);
   1148 
   1149 	ifp->_if_input(ifp, m);
   1150 }
   1151 
   1152 /*
   1153  * DEPRECATED. Use if_initialize and if_register instead.
   1154  * See the above comment of if_initialize.
   1155  *
   1156  * Note that it implicitly enables if_percpuq to make drivers easy to
   1157  * migrate softint-based if_input without much changes. If you don't
   1158  * want to enable it, use if_initialize instead.
   1159  */
   1160 void
   1161 if_attach(ifnet_t *ifp)
   1162 {
   1163 
   1164 	if_initialize(ifp);
   1165 	ifp->if_percpuq = if_percpuq_create(ifp);
   1166 	if_register(ifp);
   1167 }
   1168 
   1169 void
   1170 if_attachdomain(void)
   1171 {
   1172 	struct ifnet *ifp;
   1173 	int s;
   1174 	int bound = curlwp_bind();
   1175 
   1176 	s = pserialize_read_enter();
   1177 	IFNET_READER_FOREACH(ifp) {
   1178 		struct psref psref;
   1179 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
   1180 		pserialize_read_exit(s);
   1181 		if_attachdomain1(ifp);
   1182 		s = pserialize_read_enter();
   1183 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   1184 	}
   1185 	pserialize_read_exit(s);
   1186 	curlwp_bindx(bound);
   1187 }
   1188 
   1189 static void
   1190 if_attachdomain1(struct ifnet *ifp)
   1191 {
   1192 	struct domain *dp;
   1193 	int s;
   1194 
   1195 	s = splsoftnet();
   1196 
   1197 	/* address family dependent data region */
   1198 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
   1199 	DOMAIN_FOREACH(dp) {
   1200 		if (dp->dom_ifattach != NULL)
   1201 			ifp->if_afdata[dp->dom_family] =
   1202 			    (*dp->dom_ifattach)(ifp);
   1203 	}
   1204 
   1205 	splx(s);
   1206 }
   1207 
   1208 /*
   1209  * Deactivate an interface.  This points all of the procedure
   1210  * handles at error stubs.  May be called from interrupt context.
   1211  */
   1212 void
   1213 if_deactivate(struct ifnet *ifp)
   1214 {
   1215 	int s;
   1216 
   1217 	s = splsoftnet();
   1218 
   1219 	ifp->if_output	 = if_nulloutput;
   1220 	ifp->_if_input	 = if_nullinput;
   1221 	ifp->if_start	 = if_nullstart;
   1222 	ifp->if_transmit = if_nulltransmit;
   1223 	ifp->if_ioctl	 = if_nullioctl;
   1224 	ifp->if_init	 = if_nullinit;
   1225 	ifp->if_stop	 = if_nullstop;
   1226 	ifp->if_slowtimo = if_nullslowtimo;
   1227 	ifp->if_drain	 = if_nulldrain;
   1228 
   1229 	/* No more packets may be enqueued. */
   1230 	ifp->if_snd.ifq_maxlen = 0;
   1231 
   1232 	splx(s);
   1233 }
   1234 
   1235 bool
   1236 if_is_deactivated(const struct ifnet *ifp)
   1237 {
   1238 
   1239 	return ifp->if_output == if_nulloutput;
   1240 }
   1241 
   1242 void
   1243 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
   1244 {
   1245 	struct ifaddr *ifa, *nifa;
   1246 	int s;
   1247 
   1248 	s = pserialize_read_enter();
   1249 	for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = nifa) {
   1250 		nifa = IFADDR_READER_NEXT(ifa);
   1251 		if (ifa->ifa_addr->sa_family != family)
   1252 			continue;
   1253 		pserialize_read_exit(s);
   1254 
   1255 		(*purgeaddr)(ifa);
   1256 
   1257 		s = pserialize_read_enter();
   1258 	}
   1259 	pserialize_read_exit(s);
   1260 }
   1261 
   1262 #ifdef IFAREF_DEBUG
   1263 static struct ifaddr **ifa_list;
   1264 static int ifa_list_size;
   1265 
   1266 /* Depends on only one if_attach runs at once */
   1267 static void
   1268 if_build_ifa_list(struct ifnet *ifp)
   1269 {
   1270 	struct ifaddr *ifa;
   1271 	int i;
   1272 
   1273 	KASSERT(ifa_list == NULL);
   1274 	KASSERT(ifa_list_size == 0);
   1275 
   1276 	IFADDR_READER_FOREACH(ifa, ifp)
   1277 		ifa_list_size++;
   1278 
   1279 	ifa_list = kmem_alloc(sizeof(*ifa) * ifa_list_size, KM_SLEEP);
   1280 	i = 0;
   1281 	IFADDR_READER_FOREACH(ifa, ifp) {
   1282 		ifa_list[i++] = ifa;
   1283 		ifaref(ifa);
   1284 	}
   1285 }
   1286 
   1287 static void
   1288 if_check_and_free_ifa_list(struct ifnet *ifp)
   1289 {
   1290 	int i;
   1291 	struct ifaddr *ifa;
   1292 
   1293 	if (ifa_list == NULL)
   1294 		return;
   1295 
   1296 	for (i = 0; i < ifa_list_size; i++) {
   1297 		char buf[64];
   1298 
   1299 		ifa = ifa_list[i];
   1300 		sockaddr_format(ifa->ifa_addr, buf, sizeof(buf));
   1301 		if (ifa->ifa_refcnt > 1) {
   1302 			log(LOG_WARNING,
   1303 			    "ifa(%s) still referenced (refcnt=%d)\n",
   1304 			    buf, ifa->ifa_refcnt - 1);
   1305 		} else
   1306 			log(LOG_DEBUG,
   1307 			    "ifa(%s) not referenced (refcnt=%d)\n",
   1308 			    buf, ifa->ifa_refcnt - 1);
   1309 		ifafree(ifa);
   1310 	}
   1311 
   1312 	kmem_free(ifa_list, sizeof(*ifa) * ifa_list_size);
   1313 	ifa_list = NULL;
   1314 	ifa_list_size = 0;
   1315 }
   1316 #endif
   1317 
   1318 /*
   1319  * Detach an interface from the list of "active" interfaces,
   1320  * freeing any resources as we go along.
   1321  *
   1322  * NOTE: This routine must be called with a valid thread context,
   1323  * as it may block.
   1324  */
   1325 void
   1326 if_detach(struct ifnet *ifp)
   1327 {
   1328 	struct socket so;
   1329 	struct ifaddr *ifa;
   1330 #ifdef IFAREF_DEBUG
   1331 	struct ifaddr *last_ifa = NULL;
   1332 #endif
   1333 	struct domain *dp;
   1334 	const struct protosw *pr;
   1335 	int s, i, family, purged;
   1336 
   1337 #ifdef IFAREF_DEBUG
   1338 	if_build_ifa_list(ifp);
   1339 #endif
   1340 	/*
   1341 	 * XXX It's kind of lame that we have to have the
   1342 	 * XXX socket structure...
   1343 	 */
   1344 	memset(&so, 0, sizeof(so));
   1345 
   1346 	s = splnet();
   1347 
   1348 	sysctl_teardown(&ifp->if_sysctl_log);
   1349 
   1350 	IFNET_LOCK(ifp);
   1351 
   1352 	/*
   1353 	 * Unset all queued link states and pretend a
   1354 	 * link state change is scheduled.
   1355 	 * This stops any more link state changes occurring for this
   1356 	 * interface while it's being detached so it's safe
   1357 	 * to drain the workqueue.
   1358 	 */
   1359 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   1360 	ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
   1361 	ifp->if_link_scheduled = true;
   1362 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   1363 	workqueue_wait(ifnet_link_state_wq, &ifp->if_link_work);
   1364 
   1365 	if_deactivate(ifp);
   1366 	IFNET_UNLOCK(ifp);
   1367 
   1368 	/*
   1369 	 * Unlink from the list and wait for all readers to leave
   1370 	 * from pserialize read sections.  Note that we can't do
   1371 	 * psref_target_destroy here.  See below.
   1372 	 */
   1373 	IFNET_GLOBAL_LOCK();
   1374 	ifindex2ifnet[ifp->if_index] = NULL;
   1375 	TAILQ_REMOVE(&ifnet_list, ifp, if_list);
   1376 	IFNET_WRITER_REMOVE(ifp);
   1377 	pserialize_perform(ifnet_psz);
   1378 	IFNET_GLOBAL_UNLOCK();
   1379 
   1380 	if (ifp->if_slowtimo != NULL) {
   1381 		struct if_slowtimo_data *isd = ifp->if_slowtimo_data;
   1382 
   1383 		mutex_enter(&isd->isd_lock);
   1384 		isd->isd_dying = true;
   1385 		mutex_exit(&isd->isd_lock);
   1386 		callout_halt(&isd->isd_ch, NULL);
   1387 		workqueue_wait(if_slowtimo_wq, &isd->isd_work);
   1388 		callout_destroy(&isd->isd_ch);
   1389 		mutex_destroy(&isd->isd_lock);
   1390 		kmem_free(isd, sizeof(*isd));
   1391 
   1392 		ifp->if_slowtimo_data = NULL; /* paraonia */
   1393 		ifp->if_slowtimo = NULL;      /* paranoia */
   1394 	}
   1395 	if_deferred_start_destroy(ifp);
   1396 
   1397 	/*
   1398 	 * Do an if_down() to give protocols a chance to do something.
   1399 	 */
   1400 	if_down_deactivated(ifp);
   1401 
   1402 #ifdef ALTQ
   1403 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
   1404 		altq_disable(&ifp->if_snd);
   1405 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
   1406 		altq_detach(&ifp->if_snd);
   1407 #endif
   1408 
   1409 #if NCARP > 0
   1410 	/* Remove the interface from any carp group it is a part of.  */
   1411 	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
   1412 		carp_ifdetach(ifp);
   1413 #endif
   1414 
   1415 	/*
   1416 	 * Rip all the addresses off the interface.  This should make
   1417 	 * all of the routes go away.
   1418 	 *
   1419 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
   1420 	 * from the list, including our "cursor", ifa.  For safety,
   1421 	 * and to honor the TAILQ abstraction, I just restart the
   1422 	 * loop after each removal.  Note that the loop will exit
   1423 	 * when all of the remaining ifaddrs belong to the AF_LINK
   1424 	 * family.  I am counting on the historical fact that at
   1425 	 * least one pr_usrreq in each address domain removes at
   1426 	 * least one ifaddr.
   1427 	 */
   1428 again:
   1429 	/*
   1430 	 * At this point, no other one tries to remove ifa in the list,
   1431 	 * so we don't need to take a lock or psref.  Avoid using
   1432 	 * IFADDR_READER_FOREACH to pass over an inspection of contract
   1433 	 * violations of pserialize.
   1434 	 */
   1435 	IFADDR_WRITER_FOREACH(ifa, ifp) {
   1436 		family = ifa->ifa_addr->sa_family;
   1437 #ifdef IFAREF_DEBUG
   1438 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
   1439 		    ifa, family, ifa->ifa_refcnt);
   1440 		if (last_ifa != NULL && ifa == last_ifa)
   1441 			panic("if_detach: loop detected");
   1442 		last_ifa = ifa;
   1443 #endif
   1444 		if (family == AF_LINK)
   1445 			continue;
   1446 		dp = pffinddomain(family);
   1447 		KASSERTMSG(dp != NULL, "no domain for AF %d", family);
   1448 		/*
   1449 		 * XXX These PURGEIF calls are redundant with the
   1450 		 * purge-all-families calls below, but are left in for
   1451 		 * now both to make a smaller change, and to avoid
   1452 		 * unplanned interactions with clearing of
   1453 		 * ifp->if_addrlist.
   1454 		 */
   1455 		purged = 0;
   1456 		for (pr = dp->dom_protosw;
   1457 		     pr < dp->dom_protoswNPROTOSW; pr++) {
   1458 			so.so_proto = pr;
   1459 			if (pr->pr_usrreqs) {
   1460 				(void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
   1461 				purged = 1;
   1462 			}
   1463 		}
   1464 		if (purged == 0) {
   1465 			/*
   1466 			 * XXX What's really the best thing to do
   1467 			 * XXX here?  --thorpej (at) NetBSD.org
   1468 			 */
   1469 			printf("if_detach: WARNING: AF %d not purged\n",
   1470 			    family);
   1471 			ifa_remove(ifp, ifa);
   1472 		}
   1473 		goto again;
   1474 	}
   1475 
   1476 	if_free_sadl(ifp, 1);
   1477 
   1478 restart:
   1479 	IFADDR_WRITER_FOREACH(ifa, ifp) {
   1480 		family = ifa->ifa_addr->sa_family;
   1481 		KASSERT(family == AF_LINK);
   1482 		ifa_remove(ifp, ifa);
   1483 		goto restart;
   1484 	}
   1485 
   1486 	/* Delete stray routes from the routing table. */
   1487 	for (i = 0; i <= AF_MAX; i++)
   1488 		rt_delete_matched_entries(i, if_delroute_matcher, ifp);
   1489 
   1490 	DOMAIN_FOREACH(dp) {
   1491 		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
   1492 		{
   1493 			void *p = ifp->if_afdata[dp->dom_family];
   1494 			if (p) {
   1495 				ifp->if_afdata[dp->dom_family] = NULL;
   1496 				(*dp->dom_ifdetach)(ifp, p);
   1497 			}
   1498 		}
   1499 
   1500 		/*
   1501 		 * One would expect multicast memberships (INET and
   1502 		 * INET6) on UDP sockets to be purged by the PURGEIF
   1503 		 * calls above, but if all addresses were removed from
   1504 		 * the interface prior to destruction, the calls will
   1505 		 * not be made (e.g. ppp, for which pppd(8) generally
   1506 		 * removes addresses before destroying the interface).
   1507 		 * Because there is no invariant that multicast
   1508 		 * memberships only exist for interfaces with IPv4
   1509 		 * addresses, we must call PURGEIF regardless of
   1510 		 * addresses.  (Protocols which might store ifnet
   1511 		 * pointers are marked with PR_PURGEIF.)
   1512 		 */
   1513 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
   1514 			so.so_proto = pr;
   1515 			if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
   1516 				(void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
   1517 		}
   1518 	}
   1519 
   1520 	/*
   1521 	 * Must be done after the above pr_purgeif because if_psref may be
   1522 	 * still used in pr_purgeif.
   1523 	 */
   1524 	psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
   1525 	PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
   1526 
   1527 	pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
   1528 	(void)pfil_head_destroy(ifp->if_pfil);
   1529 
   1530 	/* Announce that the interface is gone. */
   1531 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
   1532 
   1533 	IF_AFDATA_LOCK_DESTROY(ifp);
   1534 
   1535 	/*
   1536 	 * remove packets that came from ifp, from software interrupt queues.
   1537 	 */
   1538 	DOMAIN_FOREACH(dp) {
   1539 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
   1540 			struct ifqueue *iq = dp->dom_ifqueues[i];
   1541 			if (iq == NULL)
   1542 				break;
   1543 			dp->dom_ifqueues[i] = NULL;
   1544 			if_detach_queues(ifp, iq);
   1545 		}
   1546 	}
   1547 
   1548 	/*
   1549 	 * IP queues have to be processed separately: net-queue barrier
   1550 	 * ensures that the packets are dequeued while a cross-call will
   1551 	 * ensure that the interrupts have completed. FIXME: not quite..
   1552 	 */
   1553 #ifdef INET
   1554 	pktq_barrier(ip_pktq);
   1555 #endif
   1556 #ifdef INET6
   1557 	if (in6_present)
   1558 		pktq_barrier(ip6_pktq);
   1559 #endif
   1560 	xc_barrier(0);
   1561 
   1562 	if (ifp->if_percpuq != NULL) {
   1563 		if_percpuq_destroy(ifp->if_percpuq);
   1564 		ifp->if_percpuq = NULL;
   1565 	}
   1566 
   1567 	mutex_obj_free(ifp->if_ioctl_lock);
   1568 	ifp->if_ioctl_lock = NULL;
   1569 	mutex_obj_free(ifp->if_snd.ifq_lock);
   1570 	if_stats_fini(ifp);
   1571 	KASSERT(!simplehook_has_hooks(ifp->if_linkstate_hooks));
   1572 	simplehook_destroy(ifp->if_linkstate_hooks);
   1573 
   1574 	splx(s);
   1575 
   1576 #ifdef IFAREF_DEBUG
   1577 	if_check_and_free_ifa_list(ifp);
   1578 #endif
   1579 }
   1580 
   1581 static void
   1582 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
   1583 {
   1584 	struct mbuf *m, *prev, *next;
   1585 
   1586 	prev = NULL;
   1587 	for (m = q->ifq_head; m != NULL; m = next) {
   1588 		KASSERT((m->m_flags & M_PKTHDR) != 0);
   1589 
   1590 		next = m->m_nextpkt;
   1591 		if (m->m_pkthdr.rcvif_index != ifp->if_index) {
   1592 			prev = m;
   1593 			continue;
   1594 		}
   1595 
   1596 		if (prev != NULL)
   1597 			prev->m_nextpkt = m->m_nextpkt;
   1598 		else
   1599 			q->ifq_head = m->m_nextpkt;
   1600 		if (q->ifq_tail == m)
   1601 			q->ifq_tail = prev;
   1602 		q->ifq_len--;
   1603 
   1604 		m->m_nextpkt = NULL;
   1605 		m_freem(m);
   1606 		IF_DROP(q);
   1607 	}
   1608 }
   1609 
   1610 /*
   1611  * Callback for a radix tree walk to delete all references to an
   1612  * ifnet.
   1613  */
   1614 static int
   1615 if_delroute_matcher(struct rtentry *rt, void *v)
   1616 {
   1617 	struct ifnet *ifp = (struct ifnet *)v;
   1618 
   1619 	if (rt->rt_ifp == ifp)
   1620 		return 1;
   1621 	else
   1622 		return 0;
   1623 }
   1624 
   1625 /*
   1626  * Create a clone network interface.
   1627  */
   1628 static int
   1629 if_clone_create(const char *name)
   1630 {
   1631 	struct if_clone *ifc;
   1632 	int unit;
   1633 	struct ifnet *ifp;
   1634 	struct psref psref;
   1635 
   1636 	KASSERT(mutex_owned(&if_clone_mtx));
   1637 
   1638 	ifc = if_clone_lookup(name, &unit);
   1639 	if (ifc == NULL)
   1640 		return EINVAL;
   1641 
   1642 	ifp = if_get(name, &psref);
   1643 	if (ifp != NULL) {
   1644 		if_put(ifp, &psref);
   1645 		return EEXIST;
   1646 	}
   1647 
   1648 	return (*ifc->ifc_create)(ifc, unit);
   1649 }
   1650 
   1651 /*
   1652  * Destroy a clone network interface.
   1653  */
   1654 static int
   1655 if_clone_destroy(const char *name)
   1656 {
   1657 	struct if_clone *ifc;
   1658 	struct ifnet *ifp;
   1659 	struct psref psref;
   1660 	int error;
   1661 	int (*if_ioctlfn)(struct ifnet *, u_long, void *);
   1662 
   1663 	KASSERT(mutex_owned(&if_clone_mtx));
   1664 
   1665 	ifc = if_clone_lookup(name, NULL);
   1666 	if (ifc == NULL)
   1667 		return EINVAL;
   1668 
   1669 	if (ifc->ifc_destroy == NULL)
   1670 		return EOPNOTSUPP;
   1671 
   1672 	ifp = if_get(name, &psref);
   1673 	if (ifp == NULL)
   1674 		return ENXIO;
   1675 
   1676 	/* We have to disable ioctls here */
   1677 	IFNET_LOCK(ifp);
   1678 	if_ioctlfn = ifp->if_ioctl;
   1679 	ifp->if_ioctl = if_nullioctl;
   1680 	IFNET_UNLOCK(ifp);
   1681 
   1682 	/*
   1683 	 * We cannot call ifc_destroy with holding ifp.
   1684 	 * Releasing ifp here is safe thanks to if_clone_mtx.
   1685 	 */
   1686 	if_put(ifp, &psref);
   1687 
   1688 	error = (*ifc->ifc_destroy)(ifp);
   1689 
   1690 	if (error != 0) {
   1691 		/* We have to restore if_ioctl on error */
   1692 		IFNET_LOCK(ifp);
   1693 		ifp->if_ioctl = if_ioctlfn;
   1694 		IFNET_UNLOCK(ifp);
   1695 	}
   1696 
   1697 	return error;
   1698 }
   1699 
   1700 static bool
   1701 if_is_unit(const char *name)
   1702 {
   1703 
   1704 	while (*name != '\0') {
   1705 		if (*name < '0' || *name > '9')
   1706 			return false;
   1707 		name++;
   1708 	}
   1709 
   1710 	return true;
   1711 }
   1712 
   1713 /*
   1714  * Look up a network interface cloner.
   1715  */
   1716 static struct if_clone *
   1717 if_clone_lookup(const char *name, int *unitp)
   1718 {
   1719 	struct if_clone *ifc;
   1720 	const char *cp;
   1721 	char *dp, ifname[IFNAMSIZ + 3];
   1722 	int unit;
   1723 
   1724 	KASSERT(mutex_owned(&if_clone_mtx));
   1725 
   1726 	strcpy(ifname, "if_");
   1727 	/* separate interface name from unit */
   1728 	/* TODO: search unit number from backward */
   1729 	for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
   1730 	    *cp && !if_is_unit(cp);)
   1731 		*dp++ = *cp++;
   1732 
   1733 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
   1734 		return NULL;	/* No name or unit number */
   1735 	*dp++ = '\0';
   1736 
   1737 again:
   1738 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
   1739 		if (strcmp(ifname + 3, ifc->ifc_name) == 0)
   1740 			break;
   1741 	}
   1742 
   1743 	if (ifc == NULL) {
   1744 		int error;
   1745 		if (*ifname == '\0')
   1746 			return NULL;
   1747 		mutex_exit(&if_clone_mtx);
   1748 		error = module_autoload(ifname, MODULE_CLASS_DRIVER);
   1749 		mutex_enter(&if_clone_mtx);
   1750 		if (error)
   1751 			return NULL;
   1752 		*ifname = '\0';
   1753 		goto again;
   1754 	}
   1755 
   1756 	unit = 0;
   1757 	while (cp - name < IFNAMSIZ && *cp) {
   1758 		if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
   1759 			/* Bogus unit number. */
   1760 			return NULL;
   1761 		}
   1762 		unit = (unit * 10) + (*cp++ - '0');
   1763 	}
   1764 
   1765 	if (unitp != NULL)
   1766 		*unitp = unit;
   1767 	return ifc;
   1768 }
   1769 
   1770 /*
   1771  * Register a network interface cloner.
   1772  */
   1773 void
   1774 if_clone_attach(struct if_clone *ifc)
   1775 {
   1776 
   1777 	mutex_enter(&if_clone_mtx);
   1778 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
   1779 	if_cloners_count++;
   1780 	mutex_exit(&if_clone_mtx);
   1781 }
   1782 
   1783 /*
   1784  * Unregister a network interface cloner.
   1785  */
   1786 void
   1787 if_clone_detach(struct if_clone *ifc)
   1788 {
   1789 
   1790 	mutex_enter(&if_clone_mtx);
   1791 	LIST_REMOVE(ifc, ifc_list);
   1792 	if_cloners_count--;
   1793 	mutex_exit(&if_clone_mtx);
   1794 }
   1795 
   1796 /*
   1797  * Provide list of interface cloners to userspace.
   1798  */
   1799 int
   1800 if_clone_list(int buf_count, char *buffer, int *total)
   1801 {
   1802 	char outbuf[IFNAMSIZ], *dst;
   1803 	struct if_clone *ifc;
   1804 	int count, error = 0;
   1805 
   1806 	mutex_enter(&if_clone_mtx);
   1807 	*total = if_cloners_count;
   1808 	if ((dst = buffer) == NULL) {
   1809 		/* Just asking how many there are. */
   1810 		goto out;
   1811 	}
   1812 
   1813 	if (buf_count < 0) {
   1814 		error = EINVAL;
   1815 		goto out;
   1816 	}
   1817 
   1818 	count = (if_cloners_count < buf_count) ?
   1819 	    if_cloners_count : buf_count;
   1820 
   1821 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
   1822 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
   1823 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
   1824 		if (outbuf[sizeof(outbuf) - 1] != '\0') {
   1825 			error = ENAMETOOLONG;
   1826 			goto out;
   1827 		}
   1828 		error = copyout(outbuf, dst, sizeof(outbuf));
   1829 		if (error != 0)
   1830 			break;
   1831 	}
   1832 
   1833 out:
   1834 	mutex_exit(&if_clone_mtx);
   1835 	return error;
   1836 }
   1837 
   1838 void
   1839 ifa_psref_init(struct ifaddr *ifa)
   1840 {
   1841 
   1842 	psref_target_init(&ifa->ifa_psref, ifa_psref_class);
   1843 }
   1844 
   1845 void
   1846 ifaref(struct ifaddr *ifa)
   1847 {
   1848 
   1849 	atomic_inc_uint(&ifa->ifa_refcnt);
   1850 }
   1851 
   1852 void
   1853 ifafree(struct ifaddr *ifa)
   1854 {
   1855 	KASSERT(ifa != NULL);
   1856 	KASSERTMSG(ifa->ifa_refcnt > 0, "ifa_refcnt=%d", ifa->ifa_refcnt);
   1857 
   1858 #ifndef __HAVE_ATOMIC_AS_MEMBAR
   1859 	membar_release();
   1860 #endif
   1861 	if (atomic_dec_uint_nv(&ifa->ifa_refcnt) != 0)
   1862 		return;
   1863 #ifndef __HAVE_ATOMIC_AS_MEMBAR
   1864 	membar_acquire();
   1865 #endif
   1866 	free(ifa, M_IFADDR);
   1867 }
   1868 
   1869 bool
   1870 ifa_is_destroying(struct ifaddr *ifa)
   1871 {
   1872 
   1873 	return ISSET(ifa->ifa_flags, IFA_DESTROYING);
   1874 }
   1875 
   1876 void
   1877 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
   1878 {
   1879 
   1880 	ifa->ifa_ifp = ifp;
   1881 
   1882 	/*
   1883 	 * Check MP-safety for IFEF_MPSAFE drivers.
   1884 	 * Check !IFF_RUNNING for initialization routines that normally don't
   1885 	 * take IFNET_LOCK but it's safe because there is no competitor.
   1886 	 * XXX there are false positive cases because IFF_RUNNING can be off on
   1887 	 * if_stop.
   1888 	 */
   1889 	KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) ||
   1890 	    IFNET_LOCKED(ifp));
   1891 
   1892 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
   1893 	IFADDR_ENTRY_INIT(ifa);
   1894 	IFADDR_WRITER_INSERT_TAIL(ifp, ifa);
   1895 
   1896 	ifaref(ifa);
   1897 }
   1898 
   1899 void
   1900 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
   1901 {
   1902 
   1903 	KASSERT(ifa->ifa_ifp == ifp);
   1904 	/*
   1905 	 * Check MP-safety for IFEF_MPSAFE drivers.
   1906 	 * if_is_deactivated indicates ifa_remove is called from if_detach
   1907 	 * where it is safe even if IFNET_LOCK isn't held.
   1908 	 */
   1909 	KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp));
   1910 
   1911 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
   1912 	IFADDR_WRITER_REMOVE(ifa);
   1913 #ifdef NET_MPSAFE
   1914 	IFNET_GLOBAL_LOCK();
   1915 	pserialize_perform(ifnet_psz);
   1916 	IFNET_GLOBAL_UNLOCK();
   1917 #endif
   1918 
   1919 #ifdef NET_MPSAFE
   1920 	psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
   1921 #endif
   1922 	IFADDR_ENTRY_DESTROY(ifa);
   1923 	ifafree(ifa);
   1924 }
   1925 
   1926 void
   1927 ifa_acquire(struct ifaddr *ifa, struct psref *psref)
   1928 {
   1929 
   1930 	PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
   1931 	psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class);
   1932 }
   1933 
   1934 void
   1935 ifa_release(struct ifaddr *ifa, struct psref *psref)
   1936 {
   1937 
   1938 	if (ifa == NULL)
   1939 		return;
   1940 
   1941 	psref_release(psref, &ifa->ifa_psref, ifa_psref_class);
   1942 }
   1943 
   1944 bool
   1945 ifa_held(struct ifaddr *ifa)
   1946 {
   1947 
   1948 	return psref_held(&ifa->ifa_psref, ifa_psref_class);
   1949 }
   1950 
   1951 static inline int
   1952 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
   1953 {
   1954 	return sockaddr_cmp(sa1, sa2) == 0;
   1955 }
   1956 
   1957 /*
   1958  * Locate an interface based on a complete address.
   1959  */
   1960 /*ARGSUSED*/
   1961 struct ifaddr *
   1962 ifa_ifwithaddr(const struct sockaddr *addr)
   1963 {
   1964 	struct ifnet *ifp;
   1965 	struct ifaddr *ifa;
   1966 
   1967 	IFNET_READER_FOREACH(ifp) {
   1968 		if (if_is_deactivated(ifp))
   1969 			continue;
   1970 		IFADDR_READER_FOREACH(ifa, ifp) {
   1971 			if (ifa->ifa_addr->sa_family != addr->sa_family)
   1972 				continue;
   1973 			if (equal(addr, ifa->ifa_addr))
   1974 				return ifa;
   1975 			if ((ifp->if_flags & IFF_BROADCAST) &&
   1976 			    ifa->ifa_broadaddr &&
   1977 			    /* IP6 doesn't have broadcast */
   1978 			    ifa->ifa_broadaddr->sa_len != 0 &&
   1979 			    equal(ifa->ifa_broadaddr, addr))
   1980 				return ifa;
   1981 		}
   1982 	}
   1983 	return NULL;
   1984 }
   1985 
   1986 struct ifaddr *
   1987 ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref)
   1988 {
   1989 	struct ifaddr *ifa;
   1990 	int s = pserialize_read_enter();
   1991 
   1992 	ifa = ifa_ifwithaddr(addr);
   1993 	if (ifa != NULL)
   1994 		ifa_acquire(ifa, psref);
   1995 	pserialize_read_exit(s);
   1996 
   1997 	return ifa;
   1998 }
   1999 
   2000 /*
   2001  * Locate the point to point interface with a given destination address.
   2002  */
   2003 /*ARGSUSED*/
   2004 struct ifaddr *
   2005 ifa_ifwithdstaddr(const struct sockaddr *addr)
   2006 {
   2007 	struct ifnet *ifp;
   2008 	struct ifaddr *ifa;
   2009 
   2010 	IFNET_READER_FOREACH(ifp) {
   2011 		if (if_is_deactivated(ifp))
   2012 			continue;
   2013 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   2014 			continue;
   2015 		IFADDR_READER_FOREACH(ifa, ifp) {
   2016 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
   2017 			    ifa->ifa_dstaddr == NULL)
   2018 				continue;
   2019 			if (equal(addr, ifa->ifa_dstaddr))
   2020 				return ifa;
   2021 		}
   2022 	}
   2023 
   2024 	return NULL;
   2025 }
   2026 
   2027 struct ifaddr *
   2028 ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref)
   2029 {
   2030 	struct ifaddr *ifa;
   2031 	int s;
   2032 
   2033 	s = pserialize_read_enter();
   2034 	ifa = ifa_ifwithdstaddr(addr);
   2035 	if (ifa != NULL)
   2036 		ifa_acquire(ifa, psref);
   2037 	pserialize_read_exit(s);
   2038 
   2039 	return ifa;
   2040 }
   2041 
   2042 /*
   2043  * Find an interface on a specific network.  If many, choice
   2044  * is most specific found.
   2045  */
   2046 struct ifaddr *
   2047 ifa_ifwithnet(const struct sockaddr *addr)
   2048 {
   2049 	struct ifnet *ifp;
   2050 	struct ifaddr *ifa, *ifa_maybe = NULL;
   2051 	const struct sockaddr_dl *sdl;
   2052 	u_int af = addr->sa_family;
   2053 	const char *addr_data = addr->sa_data, *cplim;
   2054 
   2055 	if (af == AF_LINK) {
   2056 		sdl = satocsdl(addr);
   2057 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
   2058 		    ifindex2ifnet[sdl->sdl_index] &&
   2059 		    !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) {
   2060 			return ifindex2ifnet[sdl->sdl_index]->if_dl;
   2061 		}
   2062 	}
   2063 #ifdef NETATALK
   2064 	if (af == AF_APPLETALK) {
   2065 		const struct sockaddr_at *sat, *sat2;
   2066 		sat = (const struct sockaddr_at *)addr;
   2067 		IFNET_READER_FOREACH(ifp) {
   2068 			if (if_is_deactivated(ifp))
   2069 				continue;
   2070 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
   2071 			if (ifa == NULL)
   2072 				continue;
   2073 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
   2074 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
   2075 				return ifa; /* exact match */
   2076 			if (ifa_maybe == NULL) {
   2077 				/* else keep the if with the right range */
   2078 				ifa_maybe = ifa;
   2079 			}
   2080 		}
   2081 		return ifa_maybe;
   2082 	}
   2083 #endif
   2084 	IFNET_READER_FOREACH(ifp) {
   2085 		if (if_is_deactivated(ifp))
   2086 			continue;
   2087 		IFADDR_READER_FOREACH(ifa, ifp) {
   2088 			const char *cp, *cp2, *cp3;
   2089 
   2090 			if (ifa->ifa_addr->sa_family != af ||
   2091 			    ifa->ifa_netmask == NULL)
   2092  next:				continue;
   2093 			cp = addr_data;
   2094 			cp2 = ifa->ifa_addr->sa_data;
   2095 			cp3 = ifa->ifa_netmask->sa_data;
   2096 			cplim = (const char *)ifa->ifa_netmask +
   2097 			    ifa->ifa_netmask->sa_len;
   2098 			while (cp3 < cplim) {
   2099 				if ((*cp++ ^ *cp2++) & *cp3++) {
   2100 					/* want to continue for() loop */
   2101 					goto next;
   2102 				}
   2103 			}
   2104 			if (ifa_maybe == NULL ||
   2105 			    rt_refines(ifa->ifa_netmask,
   2106 			               ifa_maybe->ifa_netmask))
   2107 				ifa_maybe = ifa;
   2108 		}
   2109 	}
   2110 	return ifa_maybe;
   2111 }
   2112 
   2113 struct ifaddr *
   2114 ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref)
   2115 {
   2116 	struct ifaddr *ifa;
   2117 	int s;
   2118 
   2119 	s = pserialize_read_enter();
   2120 	ifa = ifa_ifwithnet(addr);
   2121 	if (ifa != NULL)
   2122 		ifa_acquire(ifa, psref);
   2123 	pserialize_read_exit(s);
   2124 
   2125 	return ifa;
   2126 }
   2127 
   2128 /*
   2129  * Find the interface of the address.
   2130  */
   2131 struct ifaddr *
   2132 ifa_ifwithladdr(const struct sockaddr *addr)
   2133 {
   2134 	struct ifaddr *ia;
   2135 
   2136 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
   2137 	    (ia = ifa_ifwithnet(addr)))
   2138 		return ia;
   2139 	return NULL;
   2140 }
   2141 
   2142 struct ifaddr *
   2143 ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
   2144 {
   2145 	struct ifaddr *ifa;
   2146 	int s;
   2147 
   2148 	s = pserialize_read_enter();
   2149 	ifa = ifa_ifwithladdr(addr);
   2150 	if (ifa != NULL)
   2151 		ifa_acquire(ifa, psref);
   2152 	pserialize_read_exit(s);
   2153 
   2154 	return ifa;
   2155 }
   2156 
   2157 /*
   2158  * Find an interface using a specific address family
   2159  */
   2160 struct ifaddr *
   2161 ifa_ifwithaf(int af)
   2162 {
   2163 	struct ifnet *ifp;
   2164 	struct ifaddr *ifa = NULL;
   2165 	int s;
   2166 
   2167 	s = pserialize_read_enter();
   2168 	IFNET_READER_FOREACH(ifp) {
   2169 		if (if_is_deactivated(ifp))
   2170 			continue;
   2171 		IFADDR_READER_FOREACH(ifa, ifp) {
   2172 			if (ifa->ifa_addr->sa_family == af)
   2173 				goto out;
   2174 		}
   2175 	}
   2176 out:
   2177 	pserialize_read_exit(s);
   2178 	return ifa;
   2179 }
   2180 
   2181 /*
   2182  * Find an interface address specific to an interface best matching
   2183  * a given address.
   2184  */
   2185 struct ifaddr *
   2186 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
   2187 {
   2188 	struct ifaddr *ifa;
   2189 	const char *cp, *cp2, *cp3;
   2190 	const char *cplim;
   2191 	struct ifaddr *ifa_maybe = 0;
   2192 	u_int af = addr->sa_family;
   2193 
   2194 	if (if_is_deactivated(ifp))
   2195 		return NULL;
   2196 
   2197 	if (af >= AF_MAX)
   2198 		return NULL;
   2199 
   2200 	IFADDR_READER_FOREACH(ifa, ifp) {
   2201 		if (ifa->ifa_addr->sa_family != af)
   2202 			continue;
   2203 		ifa_maybe = ifa;
   2204 		if (ifa->ifa_netmask == NULL) {
   2205 			if (equal(addr, ifa->ifa_addr) ||
   2206 			    (ifa->ifa_dstaddr &&
   2207 			     equal(addr, ifa->ifa_dstaddr)))
   2208 				return ifa;
   2209 			continue;
   2210 		}
   2211 		cp = addr->sa_data;
   2212 		cp2 = ifa->ifa_addr->sa_data;
   2213 		cp3 = ifa->ifa_netmask->sa_data;
   2214 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
   2215 		for (; cp3 < cplim; cp3++) {
   2216 			if ((*cp++ ^ *cp2++) & *cp3)
   2217 				break;
   2218 		}
   2219 		if (cp3 == cplim)
   2220 			return ifa;
   2221 	}
   2222 	return ifa_maybe;
   2223 }
   2224 
   2225 struct ifaddr *
   2226 ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp,
   2227     struct psref *psref)
   2228 {
   2229 	struct ifaddr *ifa;
   2230 	int s;
   2231 
   2232 	s = pserialize_read_enter();
   2233 	ifa = ifaof_ifpforaddr(addr, ifp);
   2234 	if (ifa != NULL)
   2235 		ifa_acquire(ifa, psref);
   2236 	pserialize_read_exit(s);
   2237 
   2238 	return ifa;
   2239 }
   2240 
   2241 /*
   2242  * Default action when installing a route with a Link Level gateway.
   2243  * Lookup an appropriate real ifa to point to.
   2244  * This should be moved to /sys/net/link.c eventually.
   2245  */
   2246 void
   2247 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
   2248 {
   2249 	struct ifaddr *ifa;
   2250 	const struct sockaddr *dst;
   2251 	struct ifnet *ifp;
   2252 	struct psref psref;
   2253 
   2254 	if (cmd != RTM_ADD || ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
   2255 		return;
   2256 	ifp = rt->rt_ifa->ifa_ifp;
   2257 	dst = rt_getkey(rt);
   2258 	if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) {
   2259 		rt_replace_ifa(rt, ifa);
   2260 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
   2261 			ifa->ifa_rtrequest(cmd, rt, info);
   2262 		ifa_release(ifa, &psref);
   2263 	}
   2264 }
   2265 
   2266 /*
   2267  * bitmask macros to manage a densely packed link_state change queue.
   2268  * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
   2269  * LINK_STATE_UP(2) we need 2 bits for each state change.
   2270  * As a state change to store is 0, treat all bits set as an unset item.
   2271  */
   2272 #define LQ_ITEM_BITS		2
   2273 #define LQ_ITEM_MASK		((1 << LQ_ITEM_BITS) - 1)
   2274 #define LQ_MASK(i)		(LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
   2275 #define LINK_STATE_UNSET	LQ_ITEM_MASK
   2276 #define LQ_ITEM(q, i)		(((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
   2277 #define LQ_STORE(q, i, v)						      \
   2278 	do {								      \
   2279 		(q) &= ~LQ_MASK((i));					      \
   2280 		(q) |= (v) << (i) * LQ_ITEM_BITS;			      \
   2281 	} while (0 /* CONSTCOND */)
   2282 #define LQ_MAX(q)		((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
   2283 #define LQ_POP(q, v)							      \
   2284 	do {								      \
   2285 		(v) = LQ_ITEM((q), 0);					      \
   2286 		(q) >>= LQ_ITEM_BITS;					      \
   2287 		(q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;  \
   2288 	} while (0 /* CONSTCOND */)
   2289 #define LQ_PUSH(q, v)							      \
   2290 	do {								      \
   2291 		(q) >>= LQ_ITEM_BITS;					      \
   2292 		(q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;		      \
   2293 	} while (0 /* CONSTCOND */)
   2294 #define LQ_FIND_UNSET(q, i)						      \
   2295 	for ((i) = 0; i < LQ_MAX((q)); (i)++) {				      \
   2296 		if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET)		      \
   2297 			break;						      \
   2298 	}
   2299 
   2300 /*
   2301  * Handle a change in the interface link state and
   2302  * queue notifications.
   2303  */
   2304 void
   2305 if_link_state_change(struct ifnet *ifp, int link_state)
   2306 {
   2307 	int idx;
   2308 
   2309 	/* Ensure change is to a valid state */
   2310 	switch (link_state) {
   2311 	case LINK_STATE_UNKNOWN:	/* FALLTHROUGH */
   2312 	case LINK_STATE_DOWN:		/* FALLTHROUGH */
   2313 	case LINK_STATE_UP:
   2314 		break;
   2315 	default:
   2316 #ifdef DEBUG
   2317 		printf("%s: invalid link state %d\n",
   2318 		    ifp->if_xname, link_state);
   2319 #endif
   2320 		return;
   2321 	}
   2322 
   2323 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2324 
   2325 	/* Find the last unset event in the queue. */
   2326 	LQ_FIND_UNSET(ifp->if_link_queue, idx);
   2327 
   2328 	if (idx == 0) {
   2329 		/*
   2330 		 * There is no queue of link state changes.
   2331 		 * As we have the lock we can safely compare against the
   2332 		 * current link state and return if the same.
   2333 		 * Otherwise, if scheduled is true then the interface is being
   2334 		 * detached and the queue is being drained so we need
   2335 		 * to avoid queuing more work.
   2336 		 */
   2337 		 if (ifp->if_link_state == link_state || ifp->if_link_scheduled)
   2338 			goto out;
   2339 	} else {
   2340 		/* Ensure link_state doesn't match the last queued state. */
   2341 		if (LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
   2342 			goto out;
   2343 	}
   2344 
   2345 	/* Handle queue overflow. */
   2346 	if (idx == LQ_MAX(ifp->if_link_queue)) {
   2347 		uint8_t lost;
   2348 
   2349 		/*
   2350 		 * The DOWN state must be protected from being pushed off
   2351 		 * the queue to ensure that userland will always be
   2352 		 * in a sane state.
   2353 		 * Because DOWN is protected, there is no need to protect
   2354 		 * UNKNOWN.
   2355 		 * It should be invalid to change from any other state to
   2356 		 * UNKNOWN anyway ...
   2357 		 */
   2358 		lost = LQ_ITEM(ifp->if_link_queue, 0);
   2359 		LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
   2360 		if (lost == LINK_STATE_DOWN) {
   2361 			lost = LQ_ITEM(ifp->if_link_queue, 0);
   2362 			LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
   2363 		}
   2364 		printf("%s: lost link state change %s\n",
   2365 		    ifp->if_xname,
   2366 		    lost == LINK_STATE_UP ? "UP" :
   2367 		    lost == LINK_STATE_DOWN ? "DOWN" :
   2368 		    "UNKNOWN");
   2369 	} else
   2370 		LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
   2371 
   2372 	if (ifp->if_link_scheduled)
   2373 		goto out;
   2374 
   2375 	ifp->if_link_scheduled = true;
   2376 	workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL);
   2377 
   2378 out:
   2379 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2380 }
   2381 
   2382 /*
   2383  * Handle interface link state change notifications.
   2384  */
   2385 static void
   2386 if_link_state_change_process(struct ifnet *ifp, int link_state)
   2387 {
   2388 	struct domain *dp;
   2389 	int s = splnet();
   2390 	bool notify;
   2391 
   2392 	KASSERT(!cpu_intr_p());
   2393 
   2394 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2395 
   2396 	/* Ensure the change is still valid. */
   2397 	if (ifp->if_link_state == link_state) {
   2398 		IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2399 		splx(s);
   2400 		return;
   2401 	}
   2402 
   2403 #ifdef DEBUG
   2404 	log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
   2405 		link_state == LINK_STATE_UP ? "UP" :
   2406 		link_state == LINK_STATE_DOWN ? "DOWN" :
   2407 		"UNKNOWN",
   2408 		ifp->if_link_state == LINK_STATE_UP ? "UP" :
   2409 		ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
   2410 		"UNKNOWN");
   2411 #endif
   2412 
   2413 	/*
   2414 	 * When going from UNKNOWN to UP, we need to mark existing
   2415 	 * addresses as tentative and restart DAD as we may have
   2416 	 * erroneously not found a duplicate.
   2417 	 *
   2418 	 * This needs to happen before rt_ifmsg to avoid a race where
   2419 	 * listeners would have an address and expect it to work right
   2420 	 * away.
   2421 	 */
   2422 	notify = (link_state == LINK_STATE_UP &&
   2423 	    ifp->if_link_state == LINK_STATE_UNKNOWN);
   2424 	ifp->if_link_state = link_state;
   2425 	/* The following routines may sleep so release the spin mutex */
   2426 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2427 
   2428 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2429 	if (notify) {
   2430 		DOMAIN_FOREACH(dp) {
   2431 			if (dp->dom_if_link_state_change != NULL)
   2432 				dp->dom_if_link_state_change(ifp,
   2433 				    LINK_STATE_DOWN);
   2434 		}
   2435 	}
   2436 
   2437 	/* Notify that the link state has changed. */
   2438 	rt_ifmsg(ifp);
   2439 
   2440 	simplehook_dohooks(ifp->if_linkstate_hooks);
   2441 
   2442 	DOMAIN_FOREACH(dp) {
   2443 		if (dp->dom_if_link_state_change != NULL)
   2444 			dp->dom_if_link_state_change(ifp, link_state);
   2445 	}
   2446 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2447 	splx(s);
   2448 }
   2449 
   2450 /*
   2451  * Process the interface link state change queue.
   2452  */
   2453 static void
   2454 if_link_state_change_work(struct work *work, void *arg)
   2455 {
   2456 	struct ifnet *ifp = container_of(work, struct ifnet, if_link_work);
   2457 	int s;
   2458 	uint8_t state;
   2459 
   2460 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2461 	s = splnet();
   2462 
   2463 	/*
   2464 	 * Pop a link state change from the queue and process it.
   2465 	 * If there is nothing to process then if_detach() has been called.
   2466 	 * We keep if_link_scheduled = true so the queue can safely drain
   2467 	 * without more work being queued.
   2468 	 */
   2469 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2470 	LQ_POP(ifp->if_link_queue, state);
   2471 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2472 	if (state == LINK_STATE_UNSET)
   2473 		goto out;
   2474 
   2475 	if_link_state_change_process(ifp, state);
   2476 
   2477 	/* If there is a link state change to come, schedule it. */
   2478 	IF_LINK_STATE_CHANGE_LOCK(ifp);
   2479 	if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET) {
   2480 		ifp->if_link_scheduled = true;
   2481 		workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL);
   2482 	} else
   2483 		ifp->if_link_scheduled = false;
   2484 	IF_LINK_STATE_CHANGE_UNLOCK(ifp);
   2485 
   2486 out:
   2487 	splx(s);
   2488 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2489 }
   2490 
   2491 void *
   2492 if_linkstate_change_establish(struct ifnet *ifp, void (*fn)(void *), void *arg)
   2493 {
   2494 	khook_t *hk;
   2495 
   2496 	hk = simplehook_establish(ifp->if_linkstate_hooks, fn, arg);
   2497 
   2498 	return (void *)hk;
   2499 }
   2500 
   2501 void
   2502 if_linkstate_change_disestablish(struct ifnet *ifp, void *vhook, kmutex_t *lock)
   2503 {
   2504 
   2505 	simplehook_disestablish(ifp->if_linkstate_hooks, vhook, lock);
   2506 }
   2507 
   2508 /*
   2509  * Used to mark addresses on an interface as DETATCHED or TENTATIVE
   2510  * and thus start Duplicate Address Detection without changing the
   2511  * real link state.
   2512  */
   2513 void
   2514 if_domain_link_state_change(struct ifnet *ifp, int link_state)
   2515 {
   2516 	struct domain *dp;
   2517 	int s = splnet();
   2518 
   2519 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2520 
   2521 	DOMAIN_FOREACH(dp) {
   2522 		if (dp->dom_if_link_state_change != NULL)
   2523 			dp->dom_if_link_state_change(ifp, link_state);
   2524 	}
   2525 
   2526 	splx(s);
   2527 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2528 }
   2529 
   2530 /*
   2531  * Default action when installing a local route on a point-to-point
   2532  * interface.
   2533  */
   2534 void
   2535 p2p_rtrequest(int req, struct rtentry *rt,
   2536     __unused const struct rt_addrinfo *info)
   2537 {
   2538 	struct ifnet *ifp = rt->rt_ifp;
   2539 	struct ifaddr *ifa, *lo0ifa;
   2540 	int s = pserialize_read_enter();
   2541 
   2542 	switch (req) {
   2543 	case RTM_ADD:
   2544 		if ((rt->rt_flags & RTF_LOCAL) == 0)
   2545 			break;
   2546 
   2547 		rt->rt_ifp = lo0ifp;
   2548 
   2549 		if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
   2550 			break;
   2551 
   2552 		IFADDR_READER_FOREACH(ifa, ifp) {
   2553 			if (equal(rt_getkey(rt), ifa->ifa_addr))
   2554 				break;
   2555 		}
   2556 		if (ifa == NULL)
   2557 			break;
   2558 
   2559 		/*
   2560 		 * Ensure lo0 has an address of the same family.
   2561 		 */
   2562 		IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
   2563 			if (lo0ifa->ifa_addr->sa_family ==
   2564 			    ifa->ifa_addr->sa_family)
   2565 				break;
   2566 		}
   2567 		if (lo0ifa == NULL)
   2568 			break;
   2569 
   2570 		/*
   2571 		 * Make sure to set rt->rt_ifa to the interface
   2572 		 * address we are using, otherwise we will have trouble
   2573 		 * with source address selection.
   2574 		 */
   2575 		if (ifa != rt->rt_ifa)
   2576 			rt_replace_ifa(rt, ifa);
   2577 		break;
   2578 	case RTM_DELETE:
   2579 	default:
   2580 		break;
   2581 	}
   2582 	pserialize_read_exit(s);
   2583 }
   2584 
   2585 static void
   2586 _if_down(struct ifnet *ifp)
   2587 {
   2588 	struct ifaddr *ifa;
   2589 	struct domain *dp;
   2590 	int s, bound;
   2591 	struct psref psref;
   2592 
   2593 	ifp->if_flags &= ~IFF_UP;
   2594 	nanotime(&ifp->if_lastchange);
   2595 
   2596 	bound = curlwp_bind();
   2597 	s = pserialize_read_enter();
   2598 	IFADDR_READER_FOREACH(ifa, ifp) {
   2599 		ifa_acquire(ifa, &psref);
   2600 		pserialize_read_exit(s);
   2601 
   2602 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
   2603 
   2604 		s = pserialize_read_enter();
   2605 		ifa_release(ifa, &psref);
   2606 	}
   2607 	pserialize_read_exit(s);
   2608 	curlwp_bindx(bound);
   2609 
   2610 	IFQ_PURGE(&ifp->if_snd);
   2611 #if NCARP > 0
   2612 	if (ifp->if_carp)
   2613 		carp_carpdev_state(ifp);
   2614 #endif
   2615 	rt_ifmsg(ifp);
   2616 	DOMAIN_FOREACH(dp) {
   2617 		if (dp->dom_if_down)
   2618 			dp->dom_if_down(ifp);
   2619 	}
   2620 }
   2621 
   2622 static void
   2623 if_down_deactivated(struct ifnet *ifp)
   2624 {
   2625 
   2626 	KASSERT(if_is_deactivated(ifp));
   2627 	_if_down(ifp);
   2628 }
   2629 
   2630 void
   2631 if_down_locked(struct ifnet *ifp)
   2632 {
   2633 
   2634 	KASSERT(IFNET_LOCKED(ifp));
   2635 	_if_down(ifp);
   2636 }
   2637 
   2638 /*
   2639  * Mark an interface down and notify protocols of
   2640  * the transition.
   2641  * NOTE: must be called at splsoftnet or equivalent.
   2642  */
   2643 void
   2644 if_down(struct ifnet *ifp)
   2645 {
   2646 
   2647 	IFNET_LOCK(ifp);
   2648 	if_down_locked(ifp);
   2649 	IFNET_UNLOCK(ifp);
   2650 }
   2651 
   2652 /*
   2653  * Must be called with holding if_ioctl_lock.
   2654  */
   2655 static void
   2656 if_up_locked(struct ifnet *ifp)
   2657 {
   2658 #ifdef notyet
   2659 	struct ifaddr *ifa;
   2660 #endif
   2661 	struct domain *dp;
   2662 
   2663 	KASSERT(IFNET_LOCKED(ifp));
   2664 
   2665 	KASSERT(!if_is_deactivated(ifp));
   2666 	ifp->if_flags |= IFF_UP;
   2667 	nanotime(&ifp->if_lastchange);
   2668 #ifdef notyet
   2669 	/* this has no effect on IP, and will kill all ISO connections XXX */
   2670 	IFADDR_READER_FOREACH(ifa, ifp)
   2671 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
   2672 #endif
   2673 #if NCARP > 0
   2674 	if (ifp->if_carp)
   2675 		carp_carpdev_state(ifp);
   2676 #endif
   2677 	rt_ifmsg(ifp);
   2678 	DOMAIN_FOREACH(dp) {
   2679 		if (dp->dom_if_up)
   2680 			dp->dom_if_up(ifp);
   2681 	}
   2682 }
   2683 
   2684 /*
   2685  * Handle interface slowtimo timer routine.  Called
   2686  * from softclock, we decrement timer (if set) and
   2687  * call the appropriate interface routine on expiration.
   2688  */
   2689 static bool
   2690 if_slowtimo_countdown(struct ifnet *ifp)
   2691 {
   2692 	bool fire = false;
   2693 
   2694 	KERNEL_LOCK(1, NULL); /* for ifp->ifp_timer */
   2695 	if (ifp->if_timer != 0 && --ifp->if_timer == 0)
   2696 		fire = true;
   2697 	KERNEL_UNLOCK_ONE(NULL);
   2698 
   2699 	return fire;
   2700 }
   2701 
   2702 static void
   2703 if_slowtimo_intr(void *arg)
   2704 {
   2705 	struct ifnet *ifp = arg;
   2706 	struct if_slowtimo_data *isd = ifp->if_slowtimo_data;
   2707 
   2708 	mutex_enter(&isd->isd_lock);
   2709 	if (!isd->isd_dying) {
   2710 		if (isd->isd_trigger || if_slowtimo_countdown(ifp)) {
   2711 			if (!isd->isd_queued) {
   2712 				isd->isd_queued = true;
   2713 				workqueue_enqueue(if_slowtimo_wq,
   2714 				    &isd->isd_work, NULL);
   2715 			}
   2716 		} else {
   2717 			callout_schedule(&isd->isd_ch, hz / IFNET_SLOWHZ);
   2718 		}
   2719 	}
   2720 	mutex_exit(&isd->isd_lock);
   2721 }
   2722 
   2723 static void
   2724 if_slowtimo_work(struct work *work, void *arg)
   2725 {
   2726 	struct if_slowtimo_data *isd =
   2727 	    container_of(work, struct if_slowtimo_data, isd_work);
   2728 	struct ifnet *ifp = isd->isd_ifp;
   2729 
   2730 	KERNEL_LOCK(1, NULL);
   2731 	(*ifp->if_slowtimo)(ifp);
   2732 	KERNEL_UNLOCK_ONE(NULL);
   2733 
   2734 	mutex_enter(&isd->isd_lock);
   2735 	if (isd->isd_trigger) {
   2736 		isd->isd_trigger = false;
   2737 		printf("%s: watchdog triggered\n", ifp->if_xname);
   2738 	}
   2739 	isd->isd_queued = false;
   2740 	if (!isd->isd_dying)
   2741 		callout_schedule(&isd->isd_ch, hz / IFNET_SLOWHZ);
   2742 	mutex_exit(&isd->isd_lock);
   2743 }
   2744 
   2745 static int
   2746 sysctl_if_watchdog(SYSCTLFN_ARGS)
   2747 {
   2748 	struct sysctlnode node = *rnode;
   2749 	struct ifnet *ifp = node.sysctl_data;
   2750 	struct if_slowtimo_data *isd = ifp->if_slowtimo_data;
   2751 	int arg = 0;
   2752 	int error;
   2753 
   2754 	node.sysctl_data = &arg;
   2755 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2756 	if (error || newp == NULL)
   2757 		return error;
   2758 	if (arg) {
   2759 		mutex_enter(&isd->isd_lock);
   2760 		KASSERT(!isd->isd_dying);
   2761 		isd->isd_trigger = true;
   2762 		callout_schedule(&isd->isd_ch, 0);
   2763 		mutex_exit(&isd->isd_lock);
   2764 	}
   2765 
   2766 	return 0;
   2767 }
   2768 
   2769 static void
   2770 sysctl_watchdog_setup(struct ifnet *ifp)
   2771 {
   2772 	struct sysctllog **clog = &ifp->if_sysctl_log;
   2773 	const struct sysctlnode *rnode;
   2774 
   2775 	if (sysctl_createv(clog, 0, NULL, &rnode,
   2776 		CTLFLAG_PERMANENT, CTLTYPE_NODE, "interfaces",
   2777 		SYSCTL_DESCR("Per-interface controls"),
   2778 		NULL, 0, NULL, 0,
   2779 		CTL_NET, CTL_CREATE, CTL_EOL) != 0)
   2780 		goto bad;
   2781 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2782 		CTLFLAG_PERMANENT, CTLTYPE_NODE, ifp->if_xname,
   2783 		SYSCTL_DESCR("Interface controls"),
   2784 		NULL, 0, NULL, 0,
   2785 		CTL_CREATE, CTL_EOL) != 0)
   2786 		goto bad;
   2787 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   2788 		CTLFLAG_PERMANENT, CTLTYPE_NODE, "watchdog",
   2789 		SYSCTL_DESCR("Interface watchdog controls"),
   2790 		NULL, 0, NULL, 0,
   2791 		CTL_CREATE, CTL_EOL) != 0)
   2792 		goto bad;
   2793 	if (sysctl_createv(clog, 0, &rnode, NULL,
   2794 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "trigger",
   2795 		SYSCTL_DESCR("Trigger watchdog timeout"),
   2796 		sysctl_if_watchdog, 0, (int *)ifp, 0,
   2797 		CTL_CREATE, CTL_EOL) != 0)
   2798 		goto bad;
   2799 
   2800 	return;
   2801 
   2802 bad:
   2803 	printf("%s: could not attach sysctl watchdog nodes\n", ifp->if_xname);
   2804 }
   2805 
   2806 /*
   2807  * Mark an interface up and notify protocols of
   2808  * the transition.
   2809  * NOTE: must be called at splsoftnet or equivalent.
   2810  */
   2811 void
   2812 if_up(struct ifnet *ifp)
   2813 {
   2814 
   2815 	IFNET_LOCK(ifp);
   2816 	if_up_locked(ifp);
   2817 	IFNET_UNLOCK(ifp);
   2818 }
   2819 
   2820 /*
   2821  * Set/clear promiscuous mode on interface ifp based on the truth value
   2822  * of pswitch.  The calls are reference counted so that only the first
   2823  * "on" request actually has an effect, as does the final "off" request.
   2824  * Results are undefined if the "off" and "on" requests are not matched.
   2825  */
   2826 int
   2827 ifpromisc_locked(struct ifnet *ifp, int pswitch)
   2828 {
   2829 	int pcount, ret = 0;
   2830 	u_short nflags;
   2831 
   2832 	KASSERT(IFNET_LOCKED(ifp));
   2833 
   2834 	pcount = ifp->if_pcount;
   2835 	if (pswitch) {
   2836 		/*
   2837 		 * Allow the device to be "placed" into promiscuous
   2838 		 * mode even if it is not configured up.  It will
   2839 		 * consult IFF_PROMISC when it is brought up.
   2840 		 */
   2841 		if (ifp->if_pcount++ != 0)
   2842 			goto out;
   2843 		nflags = ifp->if_flags | IFF_PROMISC;
   2844 	} else {
   2845 		if (--ifp->if_pcount > 0)
   2846 			goto out;
   2847 		nflags = ifp->if_flags & ~IFF_PROMISC;
   2848 	}
   2849 	ret = if_flags_set(ifp, nflags);
   2850 	/* Restore interface state if not successful. */
   2851 	if (ret != 0) {
   2852 		ifp->if_pcount = pcount;
   2853 	}
   2854 out:
   2855 	return ret;
   2856 }
   2857 
   2858 int
   2859 ifpromisc(struct ifnet *ifp, int pswitch)
   2860 {
   2861 	int e;
   2862 
   2863 	IFNET_LOCK(ifp);
   2864 	e = ifpromisc_locked(ifp, pswitch);
   2865 	IFNET_UNLOCK(ifp);
   2866 
   2867 	return e;
   2868 }
   2869 
   2870 /*
   2871  * if_ioctl(ifp, cmd, data)
   2872  *
   2873  *	Apply an ioctl command to the interface.  Returns 0 on success,
   2874  *	nonzero errno(3) number on failure.
   2875  *
   2876  *	For SIOCADDMULTI/SIOCDELMULTI, caller need not hold locks -- it
   2877  *	is the driver's responsibility to take any internal locks.
   2878  *	(Kernel logic should generally invoke these only through
   2879  *	if_mcast_op.)
   2880  *
   2881  *	For all other ioctls, caller must hold ifp->if_ioctl_lock,
   2882  *	a.k.a. IFNET_LOCK.  May sleep.
   2883  */
   2884 int
   2885 if_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2886 {
   2887 
   2888 	switch (cmd) {
   2889 	case SIOCADDMULTI:
   2890 	case SIOCDELMULTI:
   2891 		break;
   2892 	default:
   2893 		KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
   2894 	}
   2895 
   2896 	return (*ifp->if_ioctl)(ifp, cmd, data);
   2897 }
   2898 
   2899 /*
   2900  * if_init(ifp)
   2901  *
   2902  *	Prepare the hardware underlying ifp to process packets
   2903  *	according to its current configuration.  Returns 0 on success,
   2904  *	nonzero errno(3) number on failure.
   2905  *
   2906  *	May sleep.  Caller must hold ifp->if_ioctl_lock, a.k.a
   2907  *	IFNET_LOCK.
   2908  */
   2909 int
   2910 if_init(struct ifnet *ifp)
   2911 {
   2912 
   2913 	KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
   2914 
   2915 	return (*ifp->if_init)(ifp);
   2916 }
   2917 
   2918 /*
   2919  * if_stop(ifp, disable)
   2920  *
   2921  *	Stop the hardware underlying ifp from processing packets.
   2922  *
   2923  *	If disable is true, ... XXX(?)
   2924  *
   2925  *	May sleep.  Caller must hold ifp->if_ioctl_lock, a.k.a
   2926  *	IFNET_LOCK.
   2927  */
   2928 void
   2929 if_stop(struct ifnet *ifp, int disable)
   2930 {
   2931 
   2932 	KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
   2933 
   2934 	(*ifp->if_stop)(ifp, disable);
   2935 }
   2936 
   2937 /*
   2938  * Map interface name to
   2939  * interface structure pointer.
   2940  */
   2941 struct ifnet *
   2942 ifunit(const char *name)
   2943 {
   2944 	struct ifnet *ifp;
   2945 	const char *cp = name;
   2946 	u_int unit = 0;
   2947 	u_int i;
   2948 	int s;
   2949 
   2950 	/*
   2951 	 * If the entire name is a number, treat it as an ifindex.
   2952 	 */
   2953 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   2954 		unit = unit * 10 + (*cp - '0');
   2955 	}
   2956 
   2957 	/*
   2958 	 * If the number took all of the name, then it's a valid ifindex.
   2959 	 */
   2960 	if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
   2961 		return if_byindex(unit);
   2962 
   2963 	ifp = NULL;
   2964 	s = pserialize_read_enter();
   2965 	IFNET_READER_FOREACH(ifp) {
   2966 		if (if_is_deactivated(ifp))
   2967 			continue;
   2968 	 	if (strcmp(ifp->if_xname, name) == 0)
   2969 			goto out;
   2970 	}
   2971 out:
   2972 	pserialize_read_exit(s);
   2973 	return ifp;
   2974 }
   2975 
   2976 /*
   2977  * Get a reference of an ifnet object by an interface name.
   2978  * The returned reference is protected by psref(9). The caller
   2979  * must release a returned reference by if_put after use.
   2980  */
   2981 struct ifnet *
   2982 if_get(const char *name, struct psref *psref)
   2983 {
   2984 	struct ifnet *ifp;
   2985 	const char *cp = name;
   2986 	u_int unit = 0;
   2987 	u_int i;
   2988 	int s;
   2989 
   2990 	/*
   2991 	 * If the entire name is a number, treat it as an ifindex.
   2992 	 */
   2993 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
   2994 		unit = unit * 10 + (*cp - '0');
   2995 	}
   2996 
   2997 	/*
   2998 	 * If the number took all of the name, then it's a valid ifindex.
   2999 	 */
   3000 	if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
   3001 		return if_get_byindex(unit, psref);
   3002 
   3003 	ifp = NULL;
   3004 	s = pserialize_read_enter();
   3005 	IFNET_READER_FOREACH(ifp) {
   3006 		if (if_is_deactivated(ifp))
   3007 			continue;
   3008 		if (strcmp(ifp->if_xname, name) == 0) {
   3009 			PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
   3010 			psref_acquire(psref, &ifp->if_psref,
   3011 			    ifnet_psref_class);
   3012 			goto out;
   3013 		}
   3014 	}
   3015 out:
   3016 	pserialize_read_exit(s);
   3017 	return ifp;
   3018 }
   3019 
   3020 /*
   3021  * Release a reference of an ifnet object given by if_get, if_get_byindex
   3022  * or if_get_bylla.
   3023  */
   3024 void
   3025 if_put(const struct ifnet *ifp, struct psref *psref)
   3026 {
   3027 
   3028 	if (ifp == NULL)
   3029 		return;
   3030 
   3031 	psref_release(psref, &ifp->if_psref, ifnet_psref_class);
   3032 }
   3033 
   3034 /*
   3035  * Return ifp having idx. Return NULL if not found.  Normally if_byindex
   3036  * should be used.
   3037  */
   3038 ifnet_t *
   3039 _if_byindex(u_int idx)
   3040 {
   3041 
   3042 	return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
   3043 }
   3044 
   3045 /*
   3046  * Return ifp having idx. Return NULL if not found or the found ifp is
   3047  * already deactivated.
   3048  */
   3049 ifnet_t *
   3050 if_byindex(u_int idx)
   3051 {
   3052 	ifnet_t *ifp;
   3053 
   3054 	ifp = _if_byindex(idx);
   3055 	if (ifp != NULL && if_is_deactivated(ifp))
   3056 		ifp = NULL;
   3057 	return ifp;
   3058 }
   3059 
   3060 /*
   3061  * Get a reference of an ifnet object by an interface index.
   3062  * The returned reference is protected by psref(9). The caller
   3063  * must release a returned reference by if_put after use.
   3064  */
   3065 ifnet_t *
   3066 if_get_byindex(u_int idx, struct psref *psref)
   3067 {
   3068 	ifnet_t *ifp;
   3069 	int s;
   3070 
   3071 	s = pserialize_read_enter();
   3072 	ifp = if_byindex(idx);
   3073 	if (__predict_true(ifp != NULL)) {
   3074 		PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
   3075 		psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
   3076 	}
   3077 	pserialize_read_exit(s);
   3078 
   3079 	return ifp;
   3080 }
   3081 
   3082 ifnet_t *
   3083 if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref)
   3084 {
   3085 	ifnet_t *ifp;
   3086 	int s;
   3087 
   3088 	s = pserialize_read_enter();
   3089 	IFNET_READER_FOREACH(ifp) {
   3090 		if (if_is_deactivated(ifp))
   3091 			continue;
   3092 		if (ifp->if_addrlen != lla_len)
   3093 			continue;
   3094 		if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) {
   3095 			psref_acquire(psref, &ifp->if_psref,
   3096 			    ifnet_psref_class);
   3097 			break;
   3098 		}
   3099 	}
   3100 	pserialize_read_exit(s);
   3101 
   3102 	return ifp;
   3103 }
   3104 
   3105 /*
   3106  * Note that it's safe only if the passed ifp is guaranteed to not be freed,
   3107  * for example using pserialize or the ifp is already held or some other
   3108  * object is held which guarantes the ifp to not be freed indirectly.
   3109  */
   3110 void
   3111 if_acquire(struct ifnet *ifp, struct psref *psref)
   3112 {
   3113 
   3114 	KASSERT(ifp->if_index != 0);
   3115 	psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
   3116 }
   3117 
   3118 bool
   3119 if_held(struct ifnet *ifp)
   3120 {
   3121 
   3122 	return psref_held(&ifp->if_psref, ifnet_psref_class);
   3123 }
   3124 
   3125 /*
   3126  * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4.
   3127  * Check the tunnel nesting count.
   3128  * Return > 0, if tunnel nesting count is more than limit.
   3129  * Return 0, if tunnel nesting count is equal or less than limit.
   3130  */
   3131 int
   3132 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit)
   3133 {
   3134 	struct m_tag *mtag;
   3135 	int *count;
   3136 
   3137 	mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO);
   3138 	if (mtag != NULL) {
   3139 		count = (int *)(mtag + 1);
   3140 		if (++(*count) > limit) {
   3141 			log(LOG_NOTICE,
   3142 			    "%s: recursively called too many times(%d)\n",
   3143 			    ifp->if_xname, *count);
   3144 			return EIO;
   3145 		}
   3146 	} else {
   3147 		mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
   3148 		    M_NOWAIT);
   3149 		if (mtag != NULL) {
   3150 			m_tag_prepend(m, mtag);
   3151 			count = (int *)(mtag + 1);
   3152 			*count = 0;
   3153 		} else {
   3154 			log(LOG_DEBUG,
   3155 			    "%s: m_tag_get() failed, recursion calls are not prevented.\n",
   3156 			    ifp->if_xname);
   3157 		}
   3158 	}
   3159 
   3160 	return 0;
   3161 }
   3162 
   3163 static void
   3164 if_tunnel_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
   3165 {
   3166 	struct tunnel_ro *tro = p;
   3167 
   3168 	tro->tr_ro = kmem_zalloc(sizeof(*tro->tr_ro), KM_SLEEP);
   3169 	tro->tr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
   3170 }
   3171 
   3172 static void
   3173 if_tunnel_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
   3174 {
   3175 	struct tunnel_ro *tro = p;
   3176 
   3177 	rtcache_free(tro->tr_ro);
   3178 	kmem_free(tro->tr_ro, sizeof(*tro->tr_ro));
   3179 
   3180 	mutex_obj_free(tro->tr_lock);
   3181 }
   3182 
   3183 percpu_t *
   3184 if_tunnel_alloc_ro_percpu(void)
   3185 {
   3186 
   3187 	return percpu_create(sizeof(struct tunnel_ro),
   3188 	    if_tunnel_ro_init_pc, if_tunnel_ro_fini_pc, NULL);
   3189 }
   3190 
   3191 void
   3192 if_tunnel_free_ro_percpu(percpu_t *ro_percpu)
   3193 {
   3194 
   3195 	percpu_free(ro_percpu, sizeof(struct tunnel_ro));
   3196 }
   3197 
   3198 
   3199 static void
   3200 if_tunnel_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
   3201 {
   3202 	struct tunnel_ro *tro = p;
   3203 
   3204 	mutex_enter(tro->tr_lock);
   3205 	rtcache_free(tro->tr_ro);
   3206 	mutex_exit(tro->tr_lock);
   3207 }
   3208 
   3209 void if_tunnel_ro_percpu_rtcache_free(percpu_t *ro_percpu)
   3210 {
   3211 
   3212 	percpu_foreach(ro_percpu, if_tunnel_rtcache_free_pc, NULL);
   3213 }
   3214 
   3215 void
   3216 if_export_if_data(ifnet_t * const ifp, struct if_data *ifi, bool zero_stats)
   3217 {
   3218 
   3219 	/* Collect the volatile stats first; this zeros *ifi. */
   3220 	if_stats_to_if_data(ifp, ifi, zero_stats);
   3221 
   3222 	ifi->ifi_type = ifp->if_type;
   3223 	ifi->ifi_addrlen = ifp->if_addrlen;
   3224 	ifi->ifi_hdrlen = ifp->if_hdrlen;
   3225 	ifi->ifi_link_state = ifp->if_link_state;
   3226 	ifi->ifi_mtu = ifp->if_mtu;
   3227 	ifi->ifi_metric = ifp->if_metric;
   3228 	ifi->ifi_baudrate = ifp->if_baudrate;
   3229 	ifi->ifi_lastchange = ifp->if_lastchange;
   3230 }
   3231 
   3232 /* common */
   3233 int
   3234 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
   3235 {
   3236 	int s;
   3237 	struct ifreq *ifr;
   3238 	struct ifcapreq *ifcr;
   3239 	struct ifdatareq *ifdr;
   3240 	unsigned short flags;
   3241 	char *descr;
   3242 	int error;
   3243 
   3244 	switch (cmd) {
   3245 	case SIOCSIFCAP:
   3246 		ifcr = data;
   3247 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
   3248 			return EINVAL;
   3249 
   3250 		if (ifcr->ifcr_capenable == ifp->if_capenable)
   3251 			return 0;
   3252 
   3253 		ifp->if_capenable = ifcr->ifcr_capenable;
   3254 
   3255 		/* Pre-compute the checksum flags mask. */
   3256 		ifp->if_csum_flags_tx = 0;
   3257 		ifp->if_csum_flags_rx = 0;
   3258 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx)
   3259 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
   3260 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
   3261 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
   3262 
   3263 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx)
   3264 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
   3265 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx)
   3266 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
   3267 
   3268 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx)
   3269 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
   3270 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx)
   3271 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
   3272 
   3273 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx)
   3274 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
   3275 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx)
   3276 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
   3277 
   3278 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx)
   3279 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
   3280 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx)
   3281 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
   3282 
   3283 		if (ifp->if_capenable & IFCAP_TSOv4)
   3284 			ifp->if_csum_flags_tx |= M_CSUM_TSOv4;
   3285 		if (ifp->if_capenable & IFCAP_TSOv6)
   3286 			ifp->if_csum_flags_tx |= M_CSUM_TSOv6;
   3287 
   3288 #if NBRIDGE > 0
   3289 		if (ifp->if_bridge != NULL)
   3290 			bridge_calc_csum_flags(ifp->if_bridge);
   3291 #endif
   3292 
   3293 		if (ifp->if_flags & IFF_UP)
   3294 			return ENETRESET;
   3295 		return 0;
   3296 	case SIOCSIFFLAGS:
   3297 		ifr = data;
   3298 		/*
   3299 		 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up
   3300 		 * and if_down aren't MP-safe yet, so we must hold the lock.
   3301 		 */
   3302 		KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
   3303 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
   3304 			s = splsoftnet();
   3305 			if_down_locked(ifp);
   3306 			splx(s);
   3307 		}
   3308 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
   3309 			s = splsoftnet();
   3310 			if_up_locked(ifp);
   3311 			splx(s);
   3312 		}
   3313 		KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
   3314 		flags = (ifp->if_flags & IFF_CANTCHANGE) |
   3315 		    (ifr->ifr_flags &~ IFF_CANTCHANGE);
   3316 		if (ifp->if_flags != flags) {
   3317 			ifp->if_flags = flags;
   3318 			/* Notify that the flags have changed. */
   3319 			rt_ifmsg(ifp);
   3320 		}
   3321 		break;
   3322 	case SIOCGIFFLAGS:
   3323 		ifr = data;
   3324 		ifr->ifr_flags = ifp->if_flags;
   3325 		break;
   3326 
   3327 	case SIOCGIFMETRIC:
   3328 		ifr = data;
   3329 		ifr->ifr_metric = ifp->if_metric;
   3330 		break;
   3331 
   3332 	case SIOCGIFMTU:
   3333 		ifr = data;
   3334 		ifr->ifr_mtu = ifp->if_mtu;
   3335 		break;
   3336 
   3337 	case SIOCGIFDLT:
   3338 		ifr = data;
   3339 		ifr->ifr_dlt = ifp->if_dlt;
   3340 		break;
   3341 
   3342 	case SIOCGIFCAP:
   3343 		ifcr = data;
   3344 		ifcr->ifcr_capabilities = ifp->if_capabilities;
   3345 		ifcr->ifcr_capenable = ifp->if_capenable;
   3346 		break;
   3347 
   3348 	case SIOCSIFMETRIC:
   3349 		ifr = data;
   3350 		ifp->if_metric = ifr->ifr_metric;
   3351 		break;
   3352 
   3353 	case SIOCGIFDATA:
   3354 		ifdr = data;
   3355 		if_export_if_data(ifp, &ifdr->ifdr_data, false);
   3356 		break;
   3357 
   3358 	case SIOCGIFINDEX:
   3359 		ifr = data;
   3360 		ifr->ifr_index = ifp->if_index;
   3361 		break;
   3362 
   3363 	case SIOCZIFDATA:
   3364 		ifdr = data;
   3365 		if_export_if_data(ifp, &ifdr->ifdr_data, true);
   3366 		getnanotime(&ifp->if_lastchange);
   3367 		break;
   3368 	case SIOCSIFMTU:
   3369 		ifr = data;
   3370 		if (ifp->if_mtu == ifr->ifr_mtu)
   3371 			break;
   3372 		ifp->if_mtu = ifr->ifr_mtu;
   3373 		return ENETRESET;
   3374 	case SIOCSIFDESCR:
   3375 		error = kauth_authorize_network(kauth_cred_get(),
   3376 		    KAUTH_NETWORK_INTERFACE,
   3377 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
   3378 		    NULL);
   3379 		if (error)
   3380 			return error;
   3381 
   3382 		ifr = data;
   3383 
   3384 		if (ifr->ifr_buflen > IFDESCRSIZE)
   3385 			return ENAMETOOLONG;
   3386 
   3387 		if (ifr->ifr_buf == NULL || ifr->ifr_buflen == 0) {
   3388 			/* unset description */
   3389 			descr = NULL;
   3390 		} else {
   3391 			descr = kmem_zalloc(IFDESCRSIZE, KM_SLEEP);
   3392 			/*
   3393 			 * copy (IFDESCRSIZE - 1) bytes to ensure
   3394 			 * terminating nul
   3395 			 */
   3396 			error = copyin(ifr->ifr_buf, descr, IFDESCRSIZE - 1);
   3397 			if (error) {
   3398 				kmem_free(descr, IFDESCRSIZE);
   3399 				return error;
   3400 			}
   3401 		}
   3402 
   3403 		if (ifp->if_description != NULL)
   3404 			kmem_free(ifp->if_description, IFDESCRSIZE);
   3405 
   3406 		ifp->if_description = descr;
   3407 		break;
   3408 
   3409  	case SIOCGIFDESCR:
   3410 		ifr = data;
   3411 		descr = ifp->if_description;
   3412 
   3413 		if (descr == NULL)
   3414 			return ENOMSG;
   3415 
   3416 		if (ifr->ifr_buflen < IFDESCRSIZE)
   3417 			return EINVAL;
   3418 
   3419 		error = copyout(descr, ifr->ifr_buf, IFDESCRSIZE);
   3420 		if (error)
   3421 			return error;
   3422  		break;
   3423 
   3424 	default:
   3425 		return ENOTTY;
   3426 	}
   3427 	return 0;
   3428 }
   3429 
   3430 int
   3431 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
   3432 {
   3433 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
   3434 	struct ifaddr *ifa;
   3435 	const struct sockaddr *any, *sa;
   3436 	union {
   3437 		struct sockaddr sa;
   3438 		struct sockaddr_storage ss;
   3439 	} u, v;
   3440 	int s, error = 0;
   3441 
   3442 	switch (cmd) {
   3443 	case SIOCSIFADDRPREF:
   3444 		error = kauth_authorize_network(kauth_cred_get(),
   3445 		    KAUTH_NETWORK_INTERFACE,
   3446 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
   3447 		    NULL);
   3448 		if (error)
   3449 			return error;
   3450 		break;
   3451 	case SIOCGIFADDRPREF:
   3452 		break;
   3453 	default:
   3454 		return EOPNOTSUPP;
   3455 	}
   3456 
   3457 	/* sanity checks */
   3458 	if (data == NULL || ifp == NULL) {
   3459 		panic("invalid argument to %s", __func__);
   3460 		/*NOTREACHED*/
   3461 	}
   3462 
   3463 	/* address must be specified on ADD and DELETE */
   3464 	sa = sstocsa(&ifap->ifap_addr);
   3465 	if (sa->sa_family != sofamily(so))
   3466 		return EINVAL;
   3467 	if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
   3468 		return EINVAL;
   3469 
   3470 	sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
   3471 
   3472 	s = pserialize_read_enter();
   3473 	IFADDR_READER_FOREACH(ifa, ifp) {
   3474 		if (ifa->ifa_addr->sa_family != sa->sa_family)
   3475 			continue;
   3476 		sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
   3477 		if (sockaddr_cmp(&u.sa, &v.sa) == 0)
   3478 			break;
   3479 	}
   3480 	if (ifa == NULL) {
   3481 		error = EADDRNOTAVAIL;
   3482 		goto out;
   3483 	}
   3484 
   3485 	switch (cmd) {
   3486 	case SIOCSIFADDRPREF:
   3487 		ifa->ifa_preference = ifap->ifap_preference;
   3488 		goto out;
   3489 	case SIOCGIFADDRPREF:
   3490 		/* fill in the if_laddrreq structure */
   3491 		(void)sockaddr_copy(sstosa(&ifap->ifap_addr),
   3492 		    sizeof(ifap->ifap_addr), ifa->ifa_addr);
   3493 		ifap->ifap_preference = ifa->ifa_preference;
   3494 		goto out;
   3495 	default:
   3496 		error = EOPNOTSUPP;
   3497 	}
   3498 out:
   3499 	pserialize_read_exit(s);
   3500 	return error;
   3501 }
   3502 
   3503 /*
   3504  * Interface ioctls.
   3505  */
   3506 static int
   3507 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
   3508 {
   3509 	struct ifnet *ifp;
   3510 	struct ifreq *ifr;
   3511 	int error = 0;
   3512 	u_long ocmd = cmd;
   3513 	u_short oif_flags;
   3514 	struct ifreq ifrb;
   3515 	struct oifreq *oifr = NULL;
   3516 	int r;
   3517 	struct psref psref;
   3518 	int bound;
   3519 	bool do_if43_post = false;
   3520 	bool do_ifm80_post = false;
   3521 
   3522 	switch (cmd) {
   3523 	case SIOCGIFCONF:
   3524 		return ifconf(cmd, data);
   3525 	case SIOCINITIFADDR:
   3526 		return EPERM;
   3527 	default:
   3528 		MODULE_HOOK_CALL(uipc_syscalls_40_hook, (cmd, data), enosys(),
   3529 		    error);
   3530 		if (error != ENOSYS)
   3531 			return error;
   3532 		MODULE_HOOK_CALL(uipc_syscalls_50_hook, (l, cmd, data),
   3533 		    enosys(), error);
   3534 		if (error != ENOSYS)
   3535 			return error;
   3536 		error = 0;
   3537 		break;
   3538 	}
   3539 
   3540 	ifr = data;
   3541 	/* Pre-conversion */
   3542 	MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), error);
   3543 	if (cmd != ocmd) {
   3544 		oifr = data;
   3545 		data = ifr = &ifrb;
   3546 		IFREQO2N_43(oifr, ifr);
   3547 		do_if43_post = true;
   3548 	}
   3549 	MODULE_HOOK_CALL(ifmedia_80_pre_hook, (ifr, &cmd, &do_ifm80_post),
   3550 	    enosys(), error);
   3551 
   3552 	switch (cmd) {
   3553 	case SIOCIFCREATE:
   3554 	case SIOCIFDESTROY:
   3555 		bound = curlwp_bind();
   3556 		if (l != NULL) {
   3557 			ifp = if_get(ifr->ifr_name, &psref);
   3558 			error = kauth_authorize_network(l->l_cred,
   3559 			    KAUTH_NETWORK_INTERFACE,
   3560 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   3561 			    KAUTH_ARG(cmd), NULL);
   3562 			if (ifp != NULL)
   3563 				if_put(ifp, &psref);
   3564 			if (error != 0) {
   3565 				curlwp_bindx(bound);
   3566 				return error;
   3567 			}
   3568 		}
   3569 		KERNEL_LOCK_UNLESS_NET_MPSAFE();
   3570 		mutex_enter(&if_clone_mtx);
   3571 		r = (cmd == SIOCIFCREATE) ?
   3572 			if_clone_create(ifr->ifr_name) :
   3573 			if_clone_destroy(ifr->ifr_name);
   3574 		mutex_exit(&if_clone_mtx);
   3575 		KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   3576 		curlwp_bindx(bound);
   3577 		return r;
   3578 
   3579 	case SIOCIFGCLONERS:
   3580 		{
   3581 			struct if_clonereq *req = (struct if_clonereq *)data;
   3582 			return if_clone_list(req->ifcr_count, req->ifcr_buffer,
   3583 			    &req->ifcr_total);
   3584 		}
   3585 	}
   3586 
   3587 	if ((cmd & IOC_IN) == 0 || IOCPARM_LEN(cmd) < sizeof(ifr->ifr_name))
   3588 		return EINVAL;
   3589 
   3590 	bound = curlwp_bind();
   3591 	ifp = if_get(ifr->ifr_name, &psref);
   3592 	if (ifp == NULL) {
   3593 		curlwp_bindx(bound);
   3594 		return ENXIO;
   3595 	}
   3596 
   3597 	switch (cmd) {
   3598 	case SIOCALIFADDR:
   3599 	case SIOCDLIFADDR:
   3600 	case SIOCSIFADDRPREF:
   3601 	case SIOCSIFFLAGS:
   3602 	case SIOCSIFCAP:
   3603 	case SIOCSIFMETRIC:
   3604 	case SIOCZIFDATA:
   3605 	case SIOCSIFMTU:
   3606 	case SIOCSIFPHYADDR:
   3607 	case SIOCDIFPHYADDR:
   3608 #ifdef INET6
   3609 	case SIOCSIFPHYADDR_IN6:
   3610 #endif
   3611 	case SIOCSLIFPHYADDR:
   3612 	case SIOCADDMULTI:
   3613 	case SIOCDELMULTI:
   3614 	case SIOCSETHERCAP:
   3615 	case SIOCSIFMEDIA:
   3616 	case SIOCSDRVSPEC:
   3617 	case SIOCG80211:
   3618 	case SIOCS80211:
   3619 	case SIOCS80211NWID:
   3620 	case SIOCS80211NWKEY:
   3621 	case SIOCS80211POWER:
   3622 	case SIOCS80211BSSID:
   3623 	case SIOCS80211CHANNEL:
   3624 	case SIOCSLINKSTR:
   3625 		if (l != NULL) {
   3626 			error = kauth_authorize_network(l->l_cred,
   3627 			    KAUTH_NETWORK_INTERFACE,
   3628 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
   3629 			    KAUTH_ARG(cmd), NULL);
   3630 			if (error != 0)
   3631 				goto out;
   3632 		}
   3633 	}
   3634 
   3635 	oif_flags = ifp->if_flags;
   3636 
   3637 	KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
   3638 	IFNET_LOCK(ifp);
   3639 
   3640 	error = if_ioctl(ifp, cmd, data);
   3641 	if (error != ENOTTY)
   3642 		;
   3643 	else if (so->so_proto == NULL)
   3644 		error = EOPNOTSUPP;
   3645 	else {
   3646 		KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
   3647 		MODULE_HOOK_CALL(if_ifioctl_43_hook,
   3648 			     (so, ocmd, cmd, data, l), enosys(), error);
   3649 		if (error == ENOSYS)
   3650 			error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
   3651 			    cmd, data, ifp);
   3652 		KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
   3653 	}
   3654 
   3655 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   3656 		if ((ifp->if_flags & IFF_UP) != 0) {
   3657 			int s = splsoftnet();
   3658 			if_up_locked(ifp);
   3659 			splx(s);
   3660 		}
   3661 	}
   3662 
   3663 	/* Post-conversion */
   3664 	if (do_ifm80_post && (error == 0))
   3665 		MODULE_HOOK_CALL(ifmedia_80_post_hook, (ifr, cmd),
   3666 		    enosys(), error);
   3667 	if (do_if43_post)
   3668 		IFREQN2O_43(oifr, ifr);
   3669 
   3670 	IFNET_UNLOCK(ifp);
   3671 	KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
   3672 out:
   3673 	if_put(ifp, &psref);
   3674 	curlwp_bindx(bound);
   3675 	return error;
   3676 }
   3677 
   3678 /*
   3679  * Return interface configuration
   3680  * of system.  List may be used
   3681  * in later ioctl's (above) to get
   3682  * other information.
   3683  *
   3684  * Each record is a struct ifreq.  Before the addition of
   3685  * sockaddr_storage, the API rule was that sockaddr flavors that did
   3686  * not fit would extend beyond the struct ifreq, with the next struct
   3687  * ifreq starting sa_len beyond the struct sockaddr.  Because the
   3688  * union in struct ifreq includes struct sockaddr_storage, every kind
   3689  * of sockaddr must fit.  Thus, there are no longer any overlength
   3690  * records.
   3691  *
   3692  * Records are added to the user buffer if they fit, and ifc_len is
   3693  * adjusted to the length that was written.  Thus, the user is only
   3694  * assured of getting the complete list if ifc_len on return is at
   3695  * least sizeof(struct ifreq) less than it was on entry.
   3696  *
   3697  * If the user buffer pointer is NULL, this routine copies no data and
   3698  * returns the amount of space that would be needed.
   3699  *
   3700  * Invariants:
   3701  * ifrp points to the next part of the user's buffer to be used.  If
   3702  * ifrp != NULL, space holds the number of bytes remaining that we may
   3703  * write at ifrp.  Otherwise, space holds the number of bytes that
   3704  * would have been written had there been adequate space.
   3705  */
   3706 /*ARGSUSED*/
   3707 static int
   3708 ifconf(u_long cmd, void *data)
   3709 {
   3710 	struct ifconf *ifc = (struct ifconf *)data;
   3711 	struct ifnet *ifp;
   3712 	struct ifaddr *ifa;
   3713 	struct ifreq ifr, *ifrp = NULL;
   3714 	int space = 0, error = 0;
   3715 	const int sz = (int)sizeof(struct ifreq);
   3716 	const bool docopy = ifc->ifc_req != NULL;
   3717 	int s;
   3718 	int bound;
   3719 	struct psref psref;
   3720 
   3721 	if (docopy) {
   3722 		if (ifc->ifc_len < 0)
   3723 			return EINVAL;
   3724 
   3725 		space = ifc->ifc_len;
   3726 		ifrp = ifc->ifc_req;
   3727 	}
   3728 	memset(&ifr, 0, sizeof(ifr));
   3729 
   3730 	bound = curlwp_bind();
   3731 	s = pserialize_read_enter();
   3732 	IFNET_READER_FOREACH(ifp) {
   3733 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
   3734 		pserialize_read_exit(s);
   3735 
   3736 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
   3737 		    sizeof(ifr.ifr_name));
   3738 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
   3739 			error = ENAMETOOLONG;
   3740 			goto release_exit;
   3741 		}
   3742 		if (IFADDR_READER_EMPTY(ifp)) {
   3743 			/* Interface with no addresses - send zero sockaddr. */
   3744 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   3745 			if (!docopy) {
   3746 				space += sz;
   3747 				goto next;
   3748 			}
   3749 			if (space >= sz) {
   3750 				error = copyout(&ifr, ifrp, sz);
   3751 				if (error != 0)
   3752 					goto release_exit;
   3753 				ifrp++;
   3754 				space -= sz;
   3755 			}
   3756 		}
   3757 
   3758 		s = pserialize_read_enter();
   3759 		IFADDR_READER_FOREACH(ifa, ifp) {
   3760 			struct sockaddr *sa = ifa->ifa_addr;
   3761 			/* all sockaddrs must fit in sockaddr_storage */
   3762 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
   3763 
   3764 			if (!docopy) {
   3765 				space += sz;
   3766 				continue;
   3767 			}
   3768 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
   3769 			pserialize_read_exit(s);
   3770 
   3771 			if (space >= sz) {
   3772 				error = copyout(&ifr, ifrp, sz);
   3773 				if (error != 0)
   3774 					goto release_exit;
   3775 				ifrp++; space -= sz;
   3776 			}
   3777 			s = pserialize_read_enter();
   3778 		}
   3779 		pserialize_read_exit(s);
   3780 
   3781         next:
   3782 		s = pserialize_read_enter();
   3783 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   3784 	}
   3785 	pserialize_read_exit(s);
   3786 	curlwp_bindx(bound);
   3787 
   3788 	if (docopy) {
   3789 		KASSERT(0 <= space && space <= ifc->ifc_len);
   3790 		ifc->ifc_len -= space;
   3791 	} else {
   3792 		KASSERT(space >= 0);
   3793 		ifc->ifc_len = space;
   3794 	}
   3795 	return (0);
   3796 
   3797 release_exit:
   3798 	psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
   3799 	curlwp_bindx(bound);
   3800 	return error;
   3801 }
   3802 
   3803 int
   3804 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
   3805 {
   3806 	uint8_t len = sizeof(ifr->ifr_ifru.ifru_space);
   3807 	struct ifreq ifrb;
   3808 	struct oifreq *oifr = NULL;
   3809 	u_long ocmd = cmd;
   3810 	int hook;
   3811 
   3812 	MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), hook);
   3813 	if (hook != ENOSYS) {
   3814 		if (cmd != ocmd) {
   3815 			oifr = (struct oifreq *)(void *)ifr;
   3816 			ifr = &ifrb;
   3817 			IFREQO2N_43(oifr, ifr);
   3818 				len = sizeof(oifr->ifr_addr);
   3819 		}
   3820 	}
   3821 
   3822 	if (len < sa->sa_len)
   3823 		return EFBIG;
   3824 
   3825 	memset(&ifr->ifr_addr, 0, len);
   3826 	sockaddr_copy(&ifr->ifr_addr, len, sa);
   3827 
   3828 	if (cmd != ocmd)
   3829 		IFREQN2O_43(oifr, ifr);
   3830 	return 0;
   3831 }
   3832 
   3833 /*
   3834  * wrapper function for the drivers which doesn't have if_transmit().
   3835  */
   3836 static int
   3837 if_transmit(struct ifnet *ifp, struct mbuf *m)
   3838 {
   3839 	int s, error;
   3840 	size_t pktlen = m->m_pkthdr.len;
   3841 	bool mcast = (m->m_flags & M_MCAST) != 0;
   3842 
   3843 	s = splnet();
   3844 
   3845 	IFQ_ENQUEUE(&ifp->if_snd, m, error);
   3846 	if (error != 0) {
   3847 		/* mbuf is already freed */
   3848 		goto out;
   3849 	}
   3850 
   3851 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   3852 	if_statadd_ref(nsr, if_obytes, pktlen);
   3853 	if (mcast)
   3854 		if_statinc_ref(nsr, if_omcasts);
   3855 	IF_STAT_PUTREF(ifp);
   3856 
   3857 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
   3858 		if_start_lock(ifp);
   3859 out:
   3860 	splx(s);
   3861 
   3862 	return error;
   3863 }
   3864 
   3865 int
   3866 if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
   3867 {
   3868 	int error;
   3869 
   3870 	kmsan_check_mbuf(m);
   3871 
   3872 #ifdef ALTQ
   3873 	KERNEL_LOCK(1, NULL);
   3874 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
   3875 		error = if_transmit(ifp, m);
   3876 		KERNEL_UNLOCK_ONE(NULL);
   3877 	} else {
   3878 		KERNEL_UNLOCK_ONE(NULL);
   3879 		error = (*ifp->if_transmit)(ifp, m);
   3880 		/* mbuf is already freed */
   3881 	}
   3882 #else /* !ALTQ */
   3883 	error = (*ifp->if_transmit)(ifp, m);
   3884 	/* mbuf is already freed */
   3885 #endif /* !ALTQ */
   3886 
   3887 	return error;
   3888 }
   3889 
   3890 /*
   3891  * Queue message on interface, and start output if interface
   3892  * not yet active.
   3893  */
   3894 int
   3895 ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
   3896 {
   3897 
   3898 	return if_transmit_lock(ifp, m);
   3899 }
   3900 
   3901 /*
   3902  * Queue message on interface, possibly using a second fast queue
   3903  */
   3904 int
   3905 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
   3906 {
   3907 	int error = 0;
   3908 
   3909 	if (ifq != NULL
   3910 #ifdef ALTQ
   3911 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
   3912 #endif
   3913 	    ) {
   3914 		if (IF_QFULL(ifq)) {
   3915 			IF_DROP(&ifp->if_snd);
   3916 			m_freem(m);
   3917 			if (error == 0)
   3918 				error = ENOBUFS;
   3919 		} else
   3920 			IF_ENQUEUE(ifq, m);
   3921 	} else
   3922 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
   3923 	if (error != 0) {
   3924 		if_statinc(ifp, if_oerrors);
   3925 		return error;
   3926 	}
   3927 	return 0;
   3928 }
   3929 
   3930 int
   3931 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
   3932 {
   3933 	int rc;
   3934 
   3935 	KASSERT(IFNET_LOCKED(ifp));
   3936 	if (ifp->if_initaddr != NULL)
   3937 		rc = (*ifp->if_initaddr)(ifp, ifa, src);
   3938 	else if (src ||
   3939 	         (rc = if_ioctl(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
   3940 		rc = if_ioctl(ifp, SIOCINITIFADDR, ifa);
   3941 
   3942 	return rc;
   3943 }
   3944 
   3945 int
   3946 if_do_dad(struct ifnet *ifp)
   3947 {
   3948 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
   3949 		return 0;
   3950 
   3951 	switch (ifp->if_type) {
   3952 	case IFT_FAITH:
   3953 		/*
   3954 		 * These interfaces do not have the IFF_LOOPBACK flag,
   3955 		 * but loop packets back.  We do not have to do DAD on such
   3956 		 * interfaces.  We should even omit it, because loop-backed
   3957 		 * responses would confuse the DAD procedure.
   3958 		 */
   3959 		return 0;
   3960 	default:
   3961 		/*
   3962 		 * Our DAD routine requires the interface up and running.
   3963 		 * However, some interfaces can be up before the RUNNING
   3964 		 * status.  Additionally, users may try to assign addresses
   3965 		 * before the interface becomes up (or running).
   3966 		 * We simply skip DAD in such a case as a work around.
   3967 		 * XXX: we should rather mark "tentative" on such addresses,
   3968 		 * and do DAD after the interface becomes ready.
   3969 		 */
   3970 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
   3971 		    (IFF_UP | IFF_RUNNING))
   3972 			return 0;
   3973 
   3974 		return 1;
   3975 	}
   3976 }
   3977 
   3978 /*
   3979  * if_flags_set(ifp, flags)
   3980  *
   3981  *	Ask ifp to change ifp->if_flags to flags, as if with the
   3982  *	SIOCSIFFLAGS ioctl command.
   3983  *
   3984  *	May sleep.  Caller must hold ifp->if_ioctl_lock, a.k.a
   3985  *	IFNET_LOCK.
   3986  */
   3987 int
   3988 if_flags_set(ifnet_t *ifp, const u_short flags)
   3989 {
   3990 	int rc;
   3991 
   3992 	KASSERT(IFNET_LOCKED(ifp));
   3993 
   3994 	if (ifp->if_setflags != NULL)
   3995 		rc = (*ifp->if_setflags)(ifp, flags);
   3996 	else {
   3997 		u_short cantflags, chgdflags;
   3998 		struct ifreq ifr;
   3999 
   4000 		chgdflags = ifp->if_flags ^ flags;
   4001 		cantflags = chgdflags & IFF_CANTCHANGE;
   4002 
   4003 		if (cantflags != 0)
   4004 			ifp->if_flags ^= cantflags;
   4005 
   4006                 /*
   4007 		 * Traditionally, we do not call if_ioctl after
   4008                  * setting/clearing only IFF_PROMISC if the interface
   4009                  * isn't IFF_UP.  Uphold that tradition.
   4010 		 */
   4011 		if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
   4012 			return 0;
   4013 
   4014 		memset(&ifr, 0, sizeof(ifr));
   4015 
   4016 		ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
   4017 		rc = if_ioctl(ifp, SIOCSIFFLAGS, &ifr);
   4018 
   4019 		if (rc != 0 && cantflags != 0)
   4020 			ifp->if_flags ^= cantflags;
   4021 	}
   4022 
   4023 	return rc;
   4024 }
   4025 
   4026 /*
   4027  * if_mcast_op(ifp, cmd, sa)
   4028  *
   4029  *	Apply a multicast command, SIOCADDMULTI/SIOCDELMULTI, to the
   4030  *	interface.  Returns 0 on success, nonzero errno(3) number on
   4031  *	failure.
   4032  *
   4033  *	May sleep.
   4034  *
   4035  *	Use this, not if_ioctl, for the multicast commands.
   4036  */
   4037 int
   4038 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
   4039 {
   4040 	int rc;
   4041 	struct ifreq ifr;
   4042 
   4043 	switch (cmd) {
   4044 	case SIOCADDMULTI:
   4045 	case SIOCDELMULTI:
   4046 		break;
   4047 	default:
   4048 		panic("invalid ifnet multicast command: 0x%lx", cmd);
   4049 	}
   4050 
   4051 	ifreq_setaddr(cmd, &ifr, sa);
   4052 	rc = if_ioctl(ifp, cmd, &ifr);
   4053 
   4054 	return rc;
   4055 }
   4056 
   4057 static void
   4058 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
   4059     struct ifaltq *ifq)
   4060 {
   4061 	const struct sysctlnode *cnode, *rnode;
   4062 
   4063 	if (sysctl_createv(clog, 0, NULL, &rnode,
   4064 		       CTLFLAG_PERMANENT,
   4065 		       CTLTYPE_NODE, "interfaces",
   4066 		       SYSCTL_DESCR("Per-interface controls"),
   4067 		       NULL, 0, NULL, 0,
   4068 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
   4069 		goto bad;
   4070 
   4071 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   4072 		       CTLFLAG_PERMANENT,
   4073 		       CTLTYPE_NODE, ifname,
   4074 		       SYSCTL_DESCR("Interface controls"),
   4075 		       NULL, 0, NULL, 0,
   4076 		       CTL_CREATE, CTL_EOL) != 0)
   4077 		goto bad;
   4078 
   4079 	if (sysctl_createv(clog, 0, &rnode, &rnode,
   4080 		       CTLFLAG_PERMANENT,
   4081 		       CTLTYPE_NODE, "sndq",
   4082 		       SYSCTL_DESCR("Interface output queue controls"),
   4083 		       NULL, 0, NULL, 0,
   4084 		       CTL_CREATE, CTL_EOL) != 0)
   4085 		goto bad;
   4086 
   4087 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   4088 		       CTLFLAG_PERMANENT,
   4089 		       CTLTYPE_INT, "len",
   4090 		       SYSCTL_DESCR("Current output queue length"),
   4091 		       NULL, 0, &ifq->ifq_len, 0,
   4092 		       CTL_CREATE, CTL_EOL) != 0)
   4093 		goto bad;
   4094 
   4095 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   4096 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   4097 		       CTLTYPE_INT, "maxlen",
   4098 		       SYSCTL_DESCR("Maximum allowed output queue length"),
   4099 		       NULL, 0, &ifq->ifq_maxlen, 0,
   4100 		       CTL_CREATE, CTL_EOL) != 0)
   4101 		goto bad;
   4102 
   4103 	if (sysctl_createv(clog, 0, &rnode, &cnode,
   4104 		       CTLFLAG_PERMANENT,
   4105 		       CTLTYPE_INT, "drops",
   4106 		       SYSCTL_DESCR("Packets dropped due to full output queue"),
   4107 		       NULL, 0, &ifq->ifq_drops, 0,
   4108 		       CTL_CREATE, CTL_EOL) != 0)
   4109 		goto bad;
   4110 
   4111 	return;
   4112 bad:
   4113 	printf("%s: could not attach sysctl nodes\n", ifname);
   4114 	return;
   4115 }
   4116 
   4117 #if defined(INET) || defined(INET6)
   4118 
   4119 #define	SYSCTL_NET_PKTQ(q, cn, c)					\
   4120 	static int							\
   4121 	sysctl_net_##q##_##cn(SYSCTLFN_ARGS)				\
   4122 	{								\
   4123 		return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);	\
   4124 	}
   4125 
   4126 #if defined(INET)
   4127 static int
   4128 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
   4129 {
   4130 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
   4131 }
   4132 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
   4133 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
   4134 #endif
   4135 
   4136 #if defined(INET6)
   4137 static int
   4138 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
   4139 {
   4140 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
   4141 }
   4142 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
   4143 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
   4144 #endif
   4145 
   4146 static void
   4147 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
   4148 {
   4149 	sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
   4150 	const char *pfname = NULL, *ipname = NULL;
   4151 	int ipn = 0, qid = 0;
   4152 
   4153 	switch (pf) {
   4154 #if defined(INET)
   4155 	case PF_INET:
   4156 		len_func = sysctl_net_ip_pktq_items;
   4157 		maxlen_func = sysctl_net_ip_pktq_maxlen;
   4158 		drops_func = sysctl_net_ip_pktq_drops;
   4159 		pfname = "inet", ipn = IPPROTO_IP;
   4160 		ipname = "ip", qid = IPCTL_IFQ;
   4161 		break;
   4162 #endif
   4163 #if defined(INET6)
   4164 	case PF_INET6:
   4165 		len_func = sysctl_net_ip6_pktq_items;
   4166 		maxlen_func = sysctl_net_ip6_pktq_maxlen;
   4167 		drops_func = sysctl_net_ip6_pktq_drops;
   4168 		pfname = "inet6", ipn = IPPROTO_IPV6;
   4169 		ipname = "ip6", qid = IPV6CTL_IFQ;
   4170 		break;
   4171 #endif
   4172 	default:
   4173 		KASSERT(false);
   4174 	}
   4175 
   4176 	sysctl_createv(clog, 0, NULL, NULL,
   4177 		       CTLFLAG_PERMANENT,
   4178 		       CTLTYPE_NODE, pfname, NULL,
   4179 		       NULL, 0, NULL, 0,
   4180 		       CTL_NET, pf, CTL_EOL);
   4181 	sysctl_createv(clog, 0, NULL, NULL,
   4182 		       CTLFLAG_PERMANENT,
   4183 		       CTLTYPE_NODE, ipname, NULL,
   4184 		       NULL, 0, NULL, 0,
   4185 		       CTL_NET, pf, ipn, CTL_EOL);
   4186 	sysctl_createv(clog, 0, NULL, NULL,
   4187 		       CTLFLAG_PERMANENT,
   4188 		       CTLTYPE_NODE, "ifq",
   4189 		       SYSCTL_DESCR("Protocol input queue controls"),
   4190 		       NULL, 0, NULL, 0,
   4191 		       CTL_NET, pf, ipn, qid, CTL_EOL);
   4192 
   4193 	sysctl_createv(clog, 0, NULL, NULL,
   4194 		       CTLFLAG_PERMANENT,
   4195 		       CTLTYPE_QUAD, "len",
   4196 		       SYSCTL_DESCR("Current input queue length"),
   4197 		       len_func, 0, NULL, 0,
   4198 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
   4199 	sysctl_createv(clog, 0, NULL, NULL,
   4200 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   4201 		       CTLTYPE_INT, "maxlen",
   4202 		       SYSCTL_DESCR("Maximum allowed input queue length"),
   4203 		       maxlen_func, 0, NULL, 0,
   4204 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
   4205 	sysctl_createv(clog, 0, NULL, NULL,
   4206 		       CTLFLAG_PERMANENT,
   4207 		       CTLTYPE_QUAD, "drops",
   4208 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
   4209 		       drops_func, 0, NULL, 0,
   4210 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
   4211 }
   4212 #endif /* INET || INET6 */
   4213 
   4214 static int
   4215 if_sdl_sysctl(SYSCTLFN_ARGS)
   4216 {
   4217 	struct ifnet *ifp;
   4218 	const struct sockaddr_dl *sdl;
   4219 	struct psref psref;
   4220 	int error = 0;
   4221 	int bound;
   4222 
   4223 	if (namelen != 1)
   4224 		return EINVAL;
   4225 
   4226 	bound = curlwp_bind();
   4227 	ifp = if_get_byindex(name[0], &psref);
   4228 	if (ifp == NULL) {
   4229 		error = ENODEV;
   4230 		goto out0;
   4231 	}
   4232 
   4233 	sdl = ifp->if_sadl;
   4234 	if (sdl == NULL) {
   4235 		*oldlenp = 0;
   4236 		goto out1;
   4237 	}
   4238 
   4239 	if (oldp == NULL) {
   4240 		*oldlenp = sdl->sdl_alen;
   4241 		goto out1;
   4242 	}
   4243 
   4244 	if (*oldlenp >= sdl->sdl_alen)
   4245 		*oldlenp = sdl->sdl_alen;
   4246 	error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
   4247 out1:
   4248 	if_put(ifp, &psref);
   4249 out0:
   4250 	curlwp_bindx(bound);
   4251 	return error;
   4252 }
   4253 
   4254 static void
   4255 if_sysctl_setup(struct sysctllog **clog)
   4256 {
   4257 	const struct sysctlnode *rnode = NULL;
   4258 
   4259 	sysctl_createv(clog, 0, NULL, &rnode,
   4260 		       CTLFLAG_PERMANENT,
   4261 		       CTLTYPE_NODE, "sdl",
   4262 		       SYSCTL_DESCR("Get active link-layer address"),
   4263 		       if_sdl_sysctl, 0, NULL, 0,
   4264 		       CTL_NET, CTL_CREATE, CTL_EOL);
   4265 
   4266 #if defined(INET)
   4267 	sysctl_net_pktq_setup(NULL, PF_INET);
   4268 #endif
   4269 #ifdef INET6
   4270 	if (in6_present)
   4271 		sysctl_net_pktq_setup(NULL, PF_INET6);
   4272 #endif
   4273 }
   4274