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