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