xref: /netbsd-src/sys/net/if.c (revision a6f3f22f245acb8ee3bbf6871d7dce989204fa97)
1 /*	$NetBSD: if.c,v 1.318 2015/08/31 08:02:44 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.318 2015/08/31 08:02:44 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 	if (ifp->if_slowtimo != NULL) {
760 		ifp->if_slowtimo = NULL;
761 		callout_halt(ifp->if_slowtimo_ch, NULL);
762 		callout_destroy(ifp->if_slowtimo_ch);
763 		kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
764 	}
765 
766 	/*
767 	 * Do an if_down() to give protocols a chance to do something.
768 	 */
769 	if_down(ifp);
770 
771 #ifdef ALTQ
772 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
773 		altq_disable(&ifp->if_snd);
774 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
775 		altq_detach(&ifp->if_snd);
776 #endif
777 
778 	if (ifp->if_snd.ifq_lock)
779 		mutex_obj_free(ifp->if_snd.ifq_lock);
780 
781 	sysctl_teardown(&ifp->if_sysctl_log);
782 
783 #if NCARP > 0
784 	/* Remove the interface from any carp group it is a part of.  */
785 	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
786 		carp_ifdetach(ifp);
787 #endif
788 
789 	/*
790 	 * Rip all the addresses off the interface.  This should make
791 	 * all of the routes go away.
792 	 *
793 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
794 	 * from the list, including our "cursor", ifa.  For safety,
795 	 * and to honor the TAILQ abstraction, I just restart the
796 	 * loop after each removal.  Note that the loop will exit
797 	 * when all of the remaining ifaddrs belong to the AF_LINK
798 	 * family.  I am counting on the historical fact that at
799 	 * least one pr_usrreq in each address domain removes at
800 	 * least one ifaddr.
801 	 */
802 again:
803 	IFADDR_FOREACH(ifa, ifp) {
804 		family = ifa->ifa_addr->sa_family;
805 #ifdef IFAREF_DEBUG
806 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
807 		    ifa, family, ifa->ifa_refcnt);
808 		if (last_ifa != NULL && ifa == last_ifa)
809 			panic("if_detach: loop detected");
810 		last_ifa = ifa;
811 #endif
812 		if (family == AF_LINK)
813 			continue;
814 		dp = pffinddomain(family);
815 #ifdef DIAGNOSTIC
816 		if (dp == NULL)
817 			panic("if_detach: no domain for AF %d",
818 			    family);
819 #endif
820 		/*
821 		 * XXX These PURGEIF calls are redundant with the
822 		 * purge-all-families calls below, but are left in for
823 		 * now both to make a smaller change, and to avoid
824 		 * unplanned interactions with clearing of
825 		 * ifp->if_addrlist.
826 		 */
827 		purged = 0;
828 		for (pr = dp->dom_protosw;
829 		     pr < dp->dom_protoswNPROTOSW; pr++) {
830 			so.so_proto = pr;
831 			if (pr->pr_usrreqs) {
832 				(void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
833 				purged = 1;
834 			}
835 		}
836 		if (purged == 0) {
837 			/*
838 			 * XXX What's really the best thing to do
839 			 * XXX here?  --thorpej@NetBSD.org
840 			 */
841 			printf("if_detach: WARNING: AF %d not purged\n",
842 			    family);
843 			ifa_remove(ifp, ifa);
844 		}
845 		goto again;
846 	}
847 
848 	if_free_sadl(ifp);
849 
850 	/* Walk the routing table looking for stragglers. */
851 	for (i = 0; i <= AF_MAX; i++) {
852 		while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
853 			continue;
854 	}
855 
856 	DOMAIN_FOREACH(dp) {
857 		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
858 		{
859 			void *p = ifp->if_afdata[dp->dom_family];
860 			if (p) {
861 				ifp->if_afdata[dp->dom_family] = NULL;
862 				(*dp->dom_ifdetach)(ifp, p);
863 			}
864 		}
865 
866 		/*
867 		 * One would expect multicast memberships (INET and
868 		 * INET6) on UDP sockets to be purged by the PURGEIF
869 		 * calls above, but if all addresses were removed from
870 		 * the interface prior to destruction, the calls will
871 		 * not be made (e.g. ppp, for which pppd(8) generally
872 		 * removes addresses before destroying the interface).
873 		 * Because there is no invariant that multicast
874 		 * memberships only exist for interfaces with IPv4
875 		 * addresses, we must call PURGEIF regardless of
876 		 * addresses.  (Protocols which might store ifnet
877 		 * pointers are marked with PR_PURGEIF.)
878 		 */
879 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
880 			so.so_proto = pr;
881 			if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
882 				(void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
883 		}
884 	}
885 
886 	(void)pfil_run_hooks(if_pfil,
887 	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
888 	(void)pfil_head_destroy(ifp->if_pfil);
889 
890 	/* Announce that the interface is gone. */
891 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
892 
893 	ifindex2ifnet[ifp->if_index] = NULL;
894 
895 	TAILQ_REMOVE(&ifnet_list, ifp, if_list);
896 
897 	ifioctl_detach(ifp);
898 
899 	/*
900 	 * remove packets that came from ifp, from software interrupt queues.
901 	 */
902 	DOMAIN_FOREACH(dp) {
903 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
904 			struct ifqueue *iq = dp->dom_ifqueues[i];
905 			if (iq == NULL)
906 				break;
907 			dp->dom_ifqueues[i] = NULL;
908 			if_detach_queues(ifp, iq);
909 		}
910 	}
911 
912 	/*
913 	 * IP queues have to be processed separately: net-queue barrier
914 	 * ensures that the packets are dequeued while a cross-call will
915 	 * ensure that the interrupts have completed. FIXME: not quite..
916 	 */
917 #ifdef INET
918 	pktq_barrier(ip_pktq);
919 #endif
920 #ifdef INET6
921 	if (in6_present)
922 		pktq_barrier(ip6_pktq);
923 #endif
924 	xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
925 	xc_wait(xc);
926 
927 	splx(s);
928 }
929 
930 static void
931 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
932 {
933 	struct mbuf *m, *prev, *next;
934 
935 	prev = NULL;
936 	for (m = q->ifq_head; m != NULL; m = next) {
937 		KASSERT((m->m_flags & M_PKTHDR) != 0);
938 
939 		next = m->m_nextpkt;
940 		if (m->m_pkthdr.rcvif != ifp) {
941 			prev = m;
942 			continue;
943 		}
944 
945 		if (prev != NULL)
946 			prev->m_nextpkt = m->m_nextpkt;
947 		else
948 			q->ifq_head = m->m_nextpkt;
949 		if (q->ifq_tail == m)
950 			q->ifq_tail = prev;
951 		q->ifq_len--;
952 
953 		m->m_nextpkt = NULL;
954 		m_freem(m);
955 		IF_DROP(q);
956 	}
957 }
958 
959 /*
960  * Callback for a radix tree walk to delete all references to an
961  * ifnet.
962  */
963 static int
964 if_rt_walktree(struct rtentry *rt, void *v)
965 {
966 	struct ifnet *ifp = (struct ifnet *)v;
967 	int error;
968 	struct rtentry *retrt;
969 
970 	if (rt->rt_ifp != ifp)
971 		return 0;
972 
973 	/* Delete the entry. */
974 	error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
975 	    rt_mask(rt), rt->rt_flags, &retrt);
976 	if (error == 0) {
977 		KASSERT(retrt == rt);
978 		KASSERT((retrt->rt_flags & RTF_UP) == 0);
979 		retrt->rt_ifp = NULL;
980 		rtfree(retrt);
981 	} else {
982 		printf("%s: warning: unable to delete rtentry @ %p, "
983 		    "error = %d\n", ifp->if_xname, rt, error);
984 	}
985 	return ERESTART;
986 }
987 
988 /*
989  * Create a clone network interface.
990  */
991 static int
992 if_clone_create(const char *name)
993 {
994 	struct if_clone *ifc;
995 	int unit;
996 
997 	ifc = if_clone_lookup(name, &unit);
998 	if (ifc == NULL)
999 		return EINVAL;
1000 
1001 	if (ifunit(name) != NULL)
1002 		return EEXIST;
1003 
1004 	return (*ifc->ifc_create)(ifc, unit);
1005 }
1006 
1007 /*
1008  * Destroy a clone network interface.
1009  */
1010 static int
1011 if_clone_destroy(const char *name)
1012 {
1013 	struct if_clone *ifc;
1014 	struct ifnet *ifp;
1015 
1016 	ifc = if_clone_lookup(name, NULL);
1017 	if (ifc == NULL)
1018 		return EINVAL;
1019 
1020 	ifp = ifunit(name);
1021 	if (ifp == NULL)
1022 		return ENXIO;
1023 
1024 	if (ifc->ifc_destroy == NULL)
1025 		return EOPNOTSUPP;
1026 
1027 	return (*ifc->ifc_destroy)(ifp);
1028 }
1029 
1030 /*
1031  * Look up a network interface cloner.
1032  */
1033 static struct if_clone *
1034 if_clone_lookup(const char *name, int *unitp)
1035 {
1036 	struct if_clone *ifc;
1037 	const char *cp;
1038 	char *dp, ifname[IFNAMSIZ + 3];
1039 	int unit;
1040 
1041 	strcpy(ifname, "if_");
1042 	/* separate interface name from unit */
1043 	for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1044 	    *cp && (*cp < '0' || *cp > '9');)
1045 		*dp++ = *cp++;
1046 
1047 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
1048 		return NULL;	/* No name or unit number */
1049 	*dp++ = '\0';
1050 
1051 again:
1052 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1053 		if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1054 			break;
1055 	}
1056 
1057 	if (ifc == NULL) {
1058 		if (*ifname == '\0' ||
1059 		    module_autoload(ifname, MODULE_CLASS_DRIVER))
1060 			return NULL;
1061 		*ifname = '\0';
1062 		goto again;
1063 	}
1064 
1065 	unit = 0;
1066 	while (cp - name < IFNAMSIZ && *cp) {
1067 		if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1068 			/* Bogus unit number. */
1069 			return NULL;
1070 		}
1071 		unit = (unit * 10) + (*cp++ - '0');
1072 	}
1073 
1074 	if (unitp != NULL)
1075 		*unitp = unit;
1076 	return ifc;
1077 }
1078 
1079 /*
1080  * Register a network interface cloner.
1081  */
1082 void
1083 if_clone_attach(struct if_clone *ifc)
1084 {
1085 
1086 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1087 	if_cloners_count++;
1088 }
1089 
1090 /*
1091  * Unregister a network interface cloner.
1092  */
1093 void
1094 if_clone_detach(struct if_clone *ifc)
1095 {
1096 
1097 	LIST_REMOVE(ifc, ifc_list);
1098 	if_cloners_count--;
1099 }
1100 
1101 /*
1102  * Provide list of interface cloners to userspace.
1103  */
1104 int
1105 if_clone_list(int buf_count, char *buffer, int *total)
1106 {
1107 	char outbuf[IFNAMSIZ], *dst;
1108 	struct if_clone *ifc;
1109 	int count, error = 0;
1110 
1111 	*total = if_cloners_count;
1112 	if ((dst = buffer) == NULL) {
1113 		/* Just asking how many there are. */
1114 		return 0;
1115 	}
1116 
1117 	if (buf_count < 0)
1118 		return EINVAL;
1119 
1120 	count = (if_cloners_count < buf_count) ?
1121 	    if_cloners_count : buf_count;
1122 
1123 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1124 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1125 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1126 		if (outbuf[sizeof(outbuf) - 1] != '\0')
1127 			return ENAMETOOLONG;
1128 		error = copyout(outbuf, dst, sizeof(outbuf));
1129 		if (error != 0)
1130 			break;
1131 	}
1132 
1133 	return error;
1134 }
1135 
1136 void
1137 ifaref(struct ifaddr *ifa)
1138 {
1139 	ifa->ifa_refcnt++;
1140 }
1141 
1142 void
1143 ifafree(struct ifaddr *ifa)
1144 {
1145 	KASSERT(ifa != NULL);
1146 	KASSERT(ifa->ifa_refcnt > 0);
1147 
1148 	if (--ifa->ifa_refcnt == 0) {
1149 		free(ifa, M_IFADDR);
1150 	}
1151 }
1152 
1153 void
1154 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1155 {
1156 	ifa->ifa_ifp = ifp;
1157 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1158 	ifaref(ifa);
1159 }
1160 
1161 void
1162 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1163 {
1164 	KASSERT(ifa->ifa_ifp == ifp);
1165 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1166 	ifafree(ifa);
1167 }
1168 
1169 static inline int
1170 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1171 {
1172 	return sockaddr_cmp(sa1, sa2) == 0;
1173 }
1174 
1175 /*
1176  * Locate an interface based on a complete address.
1177  */
1178 /*ARGSUSED*/
1179 struct ifaddr *
1180 ifa_ifwithaddr(const struct sockaddr *addr)
1181 {
1182 	struct ifnet *ifp;
1183 	struct ifaddr *ifa;
1184 
1185 	IFNET_FOREACH(ifp) {
1186 		if (ifp->if_output == if_nulloutput)
1187 			continue;
1188 		IFADDR_FOREACH(ifa, ifp) {
1189 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1190 				continue;
1191 			if (equal(addr, ifa->ifa_addr))
1192 				return ifa;
1193 			if ((ifp->if_flags & IFF_BROADCAST) &&
1194 			    ifa->ifa_broadaddr &&
1195 			    /* IP6 doesn't have broadcast */
1196 			    ifa->ifa_broadaddr->sa_len != 0 &&
1197 			    equal(ifa->ifa_broadaddr, addr))
1198 				return ifa;
1199 		}
1200 	}
1201 	return NULL;
1202 }
1203 
1204 /*
1205  * Locate the point to point interface with a given destination address.
1206  */
1207 /*ARGSUSED*/
1208 struct ifaddr *
1209 ifa_ifwithdstaddr(const struct sockaddr *addr)
1210 {
1211 	struct ifnet *ifp;
1212 	struct ifaddr *ifa;
1213 
1214 	IFNET_FOREACH(ifp) {
1215 		if (ifp->if_output == if_nulloutput)
1216 			continue;
1217 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1218 			continue;
1219 		IFADDR_FOREACH(ifa, ifp) {
1220 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
1221 			    ifa->ifa_dstaddr == NULL)
1222 				continue;
1223 			if (equal(addr, ifa->ifa_dstaddr))
1224 				return ifa;
1225 		}
1226 	}
1227 	return NULL;
1228 }
1229 
1230 /*
1231  * Find an interface on a specific network.  If many, choice
1232  * is most specific found.
1233  */
1234 struct ifaddr *
1235 ifa_ifwithnet(const struct sockaddr *addr)
1236 {
1237 	struct ifnet *ifp;
1238 	struct ifaddr *ifa;
1239 	const struct sockaddr_dl *sdl;
1240 	struct ifaddr *ifa_maybe = 0;
1241 	u_int af = addr->sa_family;
1242 	const char *addr_data = addr->sa_data, *cplim;
1243 
1244 	if (af == AF_LINK) {
1245 		sdl = satocsdl(addr);
1246 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1247 		    ifindex2ifnet[sdl->sdl_index] &&
1248 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) {
1249 			return ifindex2ifnet[sdl->sdl_index]->if_dl;
1250 		}
1251 	}
1252 #ifdef NETATALK
1253 	if (af == AF_APPLETALK) {
1254 		const struct sockaddr_at *sat, *sat2;
1255 		sat = (const struct sockaddr_at *)addr;
1256 		IFNET_FOREACH(ifp) {
1257 			if (ifp->if_output == if_nulloutput)
1258 				continue;
1259 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1260 			if (ifa == NULL)
1261 				continue;
1262 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1263 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1264 				return ifa; /* exact match */
1265 			if (ifa_maybe == NULL) {
1266 				/* else keep the if with the right range */
1267 				ifa_maybe = ifa;
1268 			}
1269 		}
1270 		return ifa_maybe;
1271 	}
1272 #endif
1273 	IFNET_FOREACH(ifp) {
1274 		if (ifp->if_output == if_nulloutput)
1275 			continue;
1276 		IFADDR_FOREACH(ifa, ifp) {
1277 			const char *cp, *cp2, *cp3;
1278 
1279 			if (ifa->ifa_addr->sa_family != af ||
1280 			    ifa->ifa_netmask == NULL)
1281  next:				continue;
1282 			cp = addr_data;
1283 			cp2 = ifa->ifa_addr->sa_data;
1284 			cp3 = ifa->ifa_netmask->sa_data;
1285 			cplim = (const char *)ifa->ifa_netmask +
1286 			    ifa->ifa_netmask->sa_len;
1287 			while (cp3 < cplim) {
1288 				if ((*cp++ ^ *cp2++) & *cp3++) {
1289 					/* want to continue for() loop */
1290 					goto next;
1291 				}
1292 			}
1293 			if (ifa_maybe == NULL ||
1294 			    rn_refines((void *)ifa->ifa_netmask,
1295 			    (void *)ifa_maybe->ifa_netmask))
1296 				ifa_maybe = ifa;
1297 		}
1298 	}
1299 	return ifa_maybe;
1300 }
1301 
1302 /*
1303  * Find the interface of the addresss.
1304  */
1305 struct ifaddr *
1306 ifa_ifwithladdr(const struct sockaddr *addr)
1307 {
1308 	struct ifaddr *ia;
1309 
1310 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1311 	    (ia = ifa_ifwithnet(addr)))
1312 		return ia;
1313 	return NULL;
1314 }
1315 
1316 /*
1317  * Find an interface using a specific address family
1318  */
1319 struct ifaddr *
1320 ifa_ifwithaf(int af)
1321 {
1322 	struct ifnet *ifp;
1323 	struct ifaddr *ifa;
1324 
1325 	IFNET_FOREACH(ifp) {
1326 		if (ifp->if_output == if_nulloutput)
1327 			continue;
1328 		IFADDR_FOREACH(ifa, ifp) {
1329 			if (ifa->ifa_addr->sa_family == af)
1330 				return ifa;
1331 		}
1332 	}
1333 	return NULL;
1334 }
1335 
1336 /*
1337  * Find an interface address specific to an interface best matching
1338  * a given address.
1339  */
1340 struct ifaddr *
1341 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1342 {
1343 	struct ifaddr *ifa;
1344 	const char *cp, *cp2, *cp3;
1345 	const char *cplim;
1346 	struct ifaddr *ifa_maybe = 0;
1347 	u_int af = addr->sa_family;
1348 
1349 	if (ifp->if_output == if_nulloutput)
1350 		return NULL;
1351 
1352 	if (af >= AF_MAX)
1353 		return NULL;
1354 
1355 	IFADDR_FOREACH(ifa, ifp) {
1356 		if (ifa->ifa_addr->sa_family != af)
1357 			continue;
1358 		ifa_maybe = ifa;
1359 		if (ifa->ifa_netmask == NULL) {
1360 			if (equal(addr, ifa->ifa_addr) ||
1361 			    (ifa->ifa_dstaddr &&
1362 			     equal(addr, ifa->ifa_dstaddr)))
1363 				return ifa;
1364 			continue;
1365 		}
1366 		cp = addr->sa_data;
1367 		cp2 = ifa->ifa_addr->sa_data;
1368 		cp3 = ifa->ifa_netmask->sa_data;
1369 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1370 		for (; cp3 < cplim; cp3++) {
1371 			if ((*cp++ ^ *cp2++) & *cp3)
1372 				break;
1373 		}
1374 		if (cp3 == cplim)
1375 			return ifa;
1376 	}
1377 	return ifa_maybe;
1378 }
1379 
1380 /*
1381  * Default action when installing a route with a Link Level gateway.
1382  * Lookup an appropriate real ifa to point to.
1383  * This should be moved to /sys/net/link.c eventually.
1384  */
1385 void
1386 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1387 {
1388 	struct ifaddr *ifa;
1389 	const struct sockaddr *dst;
1390 	struct ifnet *ifp;
1391 
1392 	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1393 	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1394 		return;
1395 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1396 		rt_replace_ifa(rt, ifa);
1397 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1398 			ifa->ifa_rtrequest(cmd, rt, info);
1399 	}
1400 }
1401 
1402 /*
1403  * Handle a change in the interface link state.
1404  * XXX: We should listen to the routing socket in-kernel rather
1405  * than calling in6_if_link_* functions directly from here.
1406  */
1407 void
1408 if_link_state_change(struct ifnet *ifp, int link_state)
1409 {
1410 	int s;
1411 	int old_link_state;
1412 	struct domain *dp;
1413 
1414 	s = splnet();
1415 	if (ifp->if_link_state == link_state) {
1416 		splx(s);
1417 		return;
1418 	}
1419 
1420 	old_link_state = ifp->if_link_state;
1421 	ifp->if_link_state = link_state;
1422 #ifdef DEBUG
1423 	log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
1424 		link_state == LINK_STATE_UP ? "UP" :
1425 		link_state == LINK_STATE_DOWN ? "DOWN" :
1426 		"UNKNOWN",
1427 		 old_link_state == LINK_STATE_UP ? "UP" :
1428 		old_link_state == LINK_STATE_DOWN ? "DOWN" :
1429 		"UNKNOWN");
1430 #endif
1431 
1432 	/*
1433 	 * When going from UNKNOWN to UP, we need to mark existing
1434 	 * addresses as tentative and restart DAD as we may have
1435 	 * erroneously not found a duplicate.
1436 	 *
1437 	 * This needs to happen before rt_ifmsg to avoid a race where
1438 	 * listeners would have an address and expect it to work right
1439 	 * away.
1440 	 */
1441 	if (link_state == LINK_STATE_UP &&
1442 	    old_link_state == LINK_STATE_UNKNOWN)
1443 	{
1444 		DOMAIN_FOREACH(dp) {
1445 			if (dp->dom_if_link_state_change != NULL)
1446 				dp->dom_if_link_state_change(ifp,
1447 				    LINK_STATE_DOWN);
1448 		}
1449 	}
1450 
1451 	/* Notify that the link state has changed. */
1452 	rt_ifmsg(ifp);
1453 
1454 #if NCARP > 0
1455 	if (ifp->if_carp)
1456 		carp_carpdev_state(ifp);
1457 #endif
1458 
1459 	DOMAIN_FOREACH(dp) {
1460 		if (dp->dom_if_link_state_change != NULL)
1461 			dp->dom_if_link_state_change(ifp, link_state);
1462 	}
1463 
1464 	splx(s);
1465 }
1466 
1467 /*
1468  * Default action when installing a local route on a point-to-point
1469  * interface.
1470  */
1471 void
1472 p2p_rtrequest(int req, struct rtentry *rt,
1473     __unused const struct rt_addrinfo *info)
1474 {
1475 	struct ifnet *ifp = rt->rt_ifp;
1476 	struct ifaddr *ifa, *lo0ifa;
1477 
1478 	switch (req) {
1479 	case RTM_ADD:
1480 		if ((rt->rt_flags & RTF_LOCAL) == 0)
1481 			break;
1482 
1483 		IFADDR_FOREACH(ifa, ifp) {
1484 			if (equal(rt_getkey(rt), ifa->ifa_addr))
1485 				break;
1486 		}
1487 		if (ifa == NULL)
1488 			break;
1489 
1490 		/*
1491 		 * Ensure lo0 has an address of the same family.
1492 		 */
1493 		IFADDR_FOREACH(lo0ifa, lo0ifp) {
1494 			if (lo0ifa->ifa_addr->sa_family ==
1495 			    ifa->ifa_addr->sa_family)
1496 				break;
1497 		}
1498 		if (lo0ifa == NULL)
1499 			break;
1500 
1501 		rt->rt_ifp = lo0ifp;
1502 		rt->rt_flags &= ~RTF_LLINFO;
1503 
1504 		/*
1505 		 * Make sure to set rt->rt_ifa to the interface
1506 		 * address we are using, otherwise we will have trouble
1507 		 * with source address selection.
1508 		 */
1509 		if (ifa != rt->rt_ifa)
1510 			rt_replace_ifa(rt, ifa);
1511 		break;
1512 	case RTM_DELETE:
1513 	case RTM_RESOLVE:
1514 	default:
1515 		break;
1516 	}
1517 }
1518 
1519 /*
1520  * Mark an interface down and notify protocols of
1521  * the transition.
1522  * NOTE: must be called at splsoftnet or equivalent.
1523  */
1524 void
1525 if_down(struct ifnet *ifp)
1526 {
1527 	struct ifaddr *ifa;
1528 	struct domain *dp;
1529 
1530 	ifp->if_flags &= ~IFF_UP;
1531 	nanotime(&ifp->if_lastchange);
1532 	IFADDR_FOREACH(ifa, ifp)
1533 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1534 	IFQ_PURGE(&ifp->if_snd);
1535 #if NCARP > 0
1536 	if (ifp->if_carp)
1537 		carp_carpdev_state(ifp);
1538 #endif
1539 	rt_ifmsg(ifp);
1540 	DOMAIN_FOREACH(dp) {
1541 		if (dp->dom_if_down)
1542 			dp->dom_if_down(ifp);
1543 	}
1544 }
1545 
1546 /*
1547  * Mark an interface up and notify protocols of
1548  * the transition.
1549  * NOTE: must be called at splsoftnet or equivalent.
1550  */
1551 void
1552 if_up(struct ifnet *ifp)
1553 {
1554 #ifdef notyet
1555 	struct ifaddr *ifa;
1556 #endif
1557 	struct domain *dp;
1558 
1559 	ifp->if_flags |= IFF_UP;
1560 	nanotime(&ifp->if_lastchange);
1561 #ifdef notyet
1562 	/* this has no effect on IP, and will kill all ISO connections XXX */
1563 	IFADDR_FOREACH(ifa, ifp)
1564 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
1565 #endif
1566 #if NCARP > 0
1567 	if (ifp->if_carp)
1568 		carp_carpdev_state(ifp);
1569 #endif
1570 	rt_ifmsg(ifp);
1571 	DOMAIN_FOREACH(dp) {
1572 		if (dp->dom_if_up)
1573 			dp->dom_if_up(ifp);
1574 	}
1575 }
1576 
1577 /*
1578  * Handle interface slowtimo timer routine.  Called
1579  * from softclock, we decrement timer (if set) and
1580  * call the appropriate interface routine on expiration.
1581  */
1582 static void
1583 if_slowtimo(void *arg)
1584 {
1585 	void (*slowtimo)(struct ifnet *);
1586 	struct ifnet *ifp = arg;
1587 	int s;
1588 
1589 	slowtimo = ifp->if_slowtimo;
1590 	if (__predict_false(slowtimo == NULL))
1591 		return;
1592 
1593 	s = splnet();
1594 	if (ifp->if_timer != 0 && --ifp->if_timer == 0)
1595 		(*slowtimo)(ifp);
1596 
1597 	splx(s);
1598 
1599 	if (__predict_true(ifp->if_slowtimo != NULL))
1600 		callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
1601 }
1602 
1603 /*
1604  * Set/clear promiscuous mode on interface ifp based on the truth value
1605  * of pswitch.  The calls are reference counted so that only the first
1606  * "on" request actually has an effect, as does the final "off" request.
1607  * Results are undefined if the "off" and "on" requests are not matched.
1608  */
1609 int
1610 ifpromisc(struct ifnet *ifp, int pswitch)
1611 {
1612 	int pcount, ret;
1613 	short nflags;
1614 
1615 	pcount = ifp->if_pcount;
1616 	if (pswitch) {
1617 		/*
1618 		 * Allow the device to be "placed" into promiscuous
1619 		 * mode even if it is not configured up.  It will
1620 		 * consult IFF_PROMISC when it is brought up.
1621 		 */
1622 		if (ifp->if_pcount++ != 0)
1623 			return 0;
1624 		nflags = ifp->if_flags | IFF_PROMISC;
1625 	} else {
1626 		if (--ifp->if_pcount > 0)
1627 			return 0;
1628 		nflags = ifp->if_flags & ~IFF_PROMISC;
1629 	}
1630 	ret = if_flags_set(ifp, nflags);
1631 	/* Restore interface state if not successful. */
1632 	if (ret != 0) {
1633 		ifp->if_pcount = pcount;
1634 	}
1635 	return ret;
1636 }
1637 
1638 /*
1639  * Map interface name to
1640  * interface structure pointer.
1641  */
1642 struct ifnet *
1643 ifunit(const char *name)
1644 {
1645 	struct ifnet *ifp;
1646 	const char *cp = name;
1647 	u_int unit = 0;
1648 	u_int i;
1649 
1650 	/*
1651 	 * If the entire name is a number, treat it as an ifindex.
1652 	 */
1653 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1654 		unit = unit * 10 + (*cp - '0');
1655 	}
1656 
1657 	/*
1658 	 * If the number took all of the name, then it's a valid ifindex.
1659 	 */
1660 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1661 		if (unit >= if_indexlim)
1662 			return NULL;
1663 		ifp = ifindex2ifnet[unit];
1664 		if (ifp == NULL || ifp->if_output == if_nulloutput)
1665 			return NULL;
1666 		return ifp;
1667 	}
1668 
1669 	IFNET_FOREACH(ifp) {
1670 		if (ifp->if_output == if_nulloutput)
1671 			continue;
1672 	 	if (strcmp(ifp->if_xname, name) == 0)
1673 			return ifp;
1674 	}
1675 	return NULL;
1676 }
1677 
1678 ifnet_t *
1679 if_byindex(u_int idx)
1680 {
1681 	return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
1682 }
1683 
1684 /* common */
1685 int
1686 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1687 {
1688 	int s;
1689 	struct ifreq *ifr;
1690 	struct ifcapreq *ifcr;
1691 	struct ifdatareq *ifdr;
1692 
1693 	switch (cmd) {
1694 	case SIOCSIFCAP:
1695 		ifcr = data;
1696 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1697 			return EINVAL;
1698 
1699 		if (ifcr->ifcr_capenable == ifp->if_capenable)
1700 			return 0;
1701 
1702 		ifp->if_capenable = ifcr->ifcr_capenable;
1703 
1704 		/* Pre-compute the checksum flags mask. */
1705 		ifp->if_csum_flags_tx = 0;
1706 		ifp->if_csum_flags_rx = 0;
1707 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1708 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1709 		}
1710 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1711 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1712 		}
1713 
1714 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1715 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1716 		}
1717 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1718 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1719 		}
1720 
1721 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1722 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1723 		}
1724 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1725 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1726 		}
1727 
1728 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1729 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1730 		}
1731 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1732 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1733 		}
1734 
1735 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1736 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1737 		}
1738 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1739 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1740 		}
1741 		if (ifp->if_flags & IFF_UP)
1742 			return ENETRESET;
1743 		return 0;
1744 	case SIOCSIFFLAGS:
1745 		ifr = data;
1746 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1747 			s = splnet();
1748 			if_down(ifp);
1749 			splx(s);
1750 		}
1751 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1752 			s = splnet();
1753 			if_up(ifp);
1754 			splx(s);
1755 		}
1756 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1757 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
1758 		break;
1759 	case SIOCGIFFLAGS:
1760 		ifr = data;
1761 		ifr->ifr_flags = ifp->if_flags;
1762 		break;
1763 
1764 	case SIOCGIFMETRIC:
1765 		ifr = data;
1766 		ifr->ifr_metric = ifp->if_metric;
1767 		break;
1768 
1769 	case SIOCGIFMTU:
1770 		ifr = data;
1771 		ifr->ifr_mtu = ifp->if_mtu;
1772 		break;
1773 
1774 	case SIOCGIFDLT:
1775 		ifr = data;
1776 		ifr->ifr_dlt = ifp->if_dlt;
1777 		break;
1778 
1779 	case SIOCGIFCAP:
1780 		ifcr = data;
1781 		ifcr->ifcr_capabilities = ifp->if_capabilities;
1782 		ifcr->ifcr_capenable = ifp->if_capenable;
1783 		break;
1784 
1785 	case SIOCSIFMETRIC:
1786 		ifr = data;
1787 		ifp->if_metric = ifr->ifr_metric;
1788 		break;
1789 
1790 	case SIOCGIFDATA:
1791 		ifdr = data;
1792 		ifdr->ifdr_data = ifp->if_data;
1793 		break;
1794 
1795 	case SIOCGIFINDEX:
1796 		ifr = data;
1797 		ifr->ifr_index = ifp->if_index;
1798 		break;
1799 
1800 	case SIOCZIFDATA:
1801 		ifdr = data;
1802 		ifdr->ifdr_data = ifp->if_data;
1803 		/*
1804 		 * Assumes that the volatile counters that can be
1805 		 * zero'ed are at the end of if_data.
1806 		 */
1807 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1808 		    offsetof(struct if_data, ifi_ipackets));
1809 		/*
1810 		 * The memset() clears to the bottm of if_data. In the area,
1811 		 * if_lastchange is included. Please be careful if new entry
1812 		 * will be added into if_data or rewite this.
1813 		 *
1814 		 * And also, update if_lastchnage.
1815 		 */
1816 		getnanotime(&ifp->if_lastchange);
1817 		break;
1818 	case SIOCSIFMTU:
1819 		ifr = data;
1820 		if (ifp->if_mtu == ifr->ifr_mtu)
1821 			break;
1822 		ifp->if_mtu = ifr->ifr_mtu;
1823 		/*
1824 		 * If the link MTU changed, do network layer specific procedure.
1825 		 */
1826 #ifdef INET6
1827 		if (in6_present)
1828 			nd6_setmtu(ifp);
1829 #endif
1830 		return ENETRESET;
1831 	default:
1832 		return ENOTTY;
1833 	}
1834 	return 0;
1835 }
1836 
1837 int
1838 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
1839 {
1840 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
1841 	struct ifaddr *ifa;
1842 	const struct sockaddr *any, *sa;
1843 	union {
1844 		struct sockaddr sa;
1845 		struct sockaddr_storage ss;
1846 	} u, v;
1847 
1848 	switch (cmd) {
1849 	case SIOCSIFADDRPREF:
1850 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
1851 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1852 		    NULL) != 0)
1853 			return EPERM;
1854 	case SIOCGIFADDRPREF:
1855 		break;
1856 	default:
1857 		return EOPNOTSUPP;
1858 	}
1859 
1860 	/* sanity checks */
1861 	if (data == NULL || ifp == NULL) {
1862 		panic("invalid argument to %s", __func__);
1863 		/*NOTREACHED*/
1864 	}
1865 
1866 	/* address must be specified on ADD and DELETE */
1867 	sa = sstocsa(&ifap->ifap_addr);
1868 	if (sa->sa_family != sofamily(so))
1869 		return EINVAL;
1870 	if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
1871 		return EINVAL;
1872 
1873 	sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
1874 
1875 	IFADDR_FOREACH(ifa, ifp) {
1876 		if (ifa->ifa_addr->sa_family != sa->sa_family)
1877 			continue;
1878 		sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
1879 		if (sockaddr_cmp(&u.sa, &v.sa) == 0)
1880 			break;
1881 	}
1882 	if (ifa == NULL)
1883 		return EADDRNOTAVAIL;
1884 
1885 	switch (cmd) {
1886 	case SIOCSIFADDRPREF:
1887 		ifa->ifa_preference = ifap->ifap_preference;
1888 		return 0;
1889 	case SIOCGIFADDRPREF:
1890 		/* fill in the if_laddrreq structure */
1891 		(void)sockaddr_copy(sstosa(&ifap->ifap_addr),
1892 		    sizeof(ifap->ifap_addr), ifa->ifa_addr);
1893 		ifap->ifap_preference = ifa->ifa_preference;
1894 		return 0;
1895 	default:
1896 		return EOPNOTSUPP;
1897 	}
1898 }
1899 
1900 static void
1901 ifnet_lock_enter(struct ifnet_lock *il)
1902 {
1903 	uint64_t *nenter;
1904 
1905 	/* Before trying to acquire the mutex, increase the count of threads
1906 	 * who have entered or who wait to enter the critical section.
1907 	 * Avoid one costly locked memory transaction by keeping a count for
1908 	 * each CPU.
1909 	 */
1910 	nenter = percpu_getref(il->il_nenter);
1911 	(*nenter)++;
1912 	percpu_putref(il->il_nenter);
1913 	mutex_enter(&il->il_lock);
1914 }
1915 
1916 static void
1917 ifnet_lock_exit(struct ifnet_lock *il)
1918 {
1919 	/* Increase the count of threads who have exited the critical
1920 	 * section.  Increase while we still hold the lock.
1921 	 */
1922 	il->il_nexit++;
1923 	mutex_exit(&il->il_lock);
1924 }
1925 
1926 /*
1927  * Interface ioctls.
1928  */
1929 static int
1930 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1931 {
1932 	struct ifnet *ifp;
1933 	struct ifreq *ifr;
1934 	int error = 0;
1935 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1936 	u_long ocmd = cmd;
1937 #endif
1938 	short oif_flags;
1939 #ifdef COMPAT_OIFREQ
1940 	struct ifreq ifrb;
1941 	struct oifreq *oifr = NULL;
1942 #endif
1943 	int r;
1944 
1945 	switch (cmd) {
1946 #ifdef COMPAT_OIFREQ
1947 	case OSIOCGIFCONF:
1948 	case OOSIOCGIFCONF:
1949 		return compat_ifconf(cmd, data);
1950 #endif
1951 #ifdef COMPAT_OIFDATA
1952 	case OSIOCGIFDATA:
1953 	case OSIOCZIFDATA:
1954 		return compat_ifdatareq(l, cmd, data);
1955 #endif
1956 	case SIOCGIFCONF:
1957 		return ifconf(cmd, data);
1958 	case SIOCINITIFADDR:
1959 		return EPERM;
1960 	}
1961 
1962 #ifdef COMPAT_OIFREQ
1963 	cmd = compat_cvtcmd(cmd);
1964 	if (cmd != ocmd) {
1965 		oifr = data;
1966 		data = ifr = &ifrb;
1967 		ifreqo2n(oifr, ifr);
1968 	} else
1969 #endif
1970 		ifr = data;
1971 
1972 	ifp = ifunit(ifr->ifr_name);
1973 
1974 	switch (cmd) {
1975 	case SIOCIFCREATE:
1976 	case SIOCIFDESTROY:
1977 		if (l != NULL) {
1978 			error = kauth_authorize_network(l->l_cred,
1979 			    KAUTH_NETWORK_INTERFACE,
1980 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1981 			    (void *)cmd, NULL);
1982 			if (error != 0)
1983 				return error;
1984 		}
1985 		mutex_enter(&if_clone_mtx);
1986 		r = (cmd == SIOCIFCREATE) ?
1987 			if_clone_create(ifr->ifr_name) :
1988 			if_clone_destroy(ifr->ifr_name);
1989 		mutex_exit(&if_clone_mtx);
1990 		return r;
1991 
1992 	case SIOCIFGCLONERS:
1993 		{
1994 			struct if_clonereq *req = (struct if_clonereq *)data;
1995 			return if_clone_list(req->ifcr_count, req->ifcr_buffer,
1996 			    &req->ifcr_total);
1997 		}
1998 	}
1999 
2000 	if (ifp == NULL)
2001 		return ENXIO;
2002 
2003 	switch (cmd) {
2004 	case SIOCALIFADDR:
2005 	case SIOCDLIFADDR:
2006 	case SIOCSIFADDRPREF:
2007 	case SIOCSIFFLAGS:
2008 	case SIOCSIFCAP:
2009 	case SIOCSIFMETRIC:
2010 	case SIOCZIFDATA:
2011 	case SIOCSIFMTU:
2012 	case SIOCSIFPHYADDR:
2013 	case SIOCDIFPHYADDR:
2014 #ifdef INET6
2015 	case SIOCSIFPHYADDR_IN6:
2016 #endif
2017 	case SIOCSLIFPHYADDR:
2018 	case SIOCADDMULTI:
2019 	case SIOCDELMULTI:
2020 	case SIOCSIFMEDIA:
2021 	case SIOCSDRVSPEC:
2022 	case SIOCG80211:
2023 	case SIOCS80211:
2024 	case SIOCS80211NWID:
2025 	case SIOCS80211NWKEY:
2026 	case SIOCS80211POWER:
2027 	case SIOCS80211BSSID:
2028 	case SIOCS80211CHANNEL:
2029 	case SIOCSLINKSTR:
2030 		if (l != NULL) {
2031 			error = kauth_authorize_network(l->l_cred,
2032 			    KAUTH_NETWORK_INTERFACE,
2033 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
2034 			    (void *)cmd, NULL);
2035 			if (error != 0)
2036 				return error;
2037 		}
2038 	}
2039 
2040 	oif_flags = ifp->if_flags;
2041 
2042 	ifnet_lock_enter(ifp->if_ioctl_lock);
2043 	error = (*ifp->if_ioctl)(ifp, cmd, data);
2044 	if (error != ENOTTY)
2045 		;
2046 	else if (so->so_proto == NULL)
2047 		error = EOPNOTSUPP;
2048 	else {
2049 #ifdef COMPAT_OSOCK
2050 		error = compat_ifioctl(so, ocmd, cmd, data, l);
2051 #else
2052 		error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
2053 		    cmd, data, ifp);
2054 #endif
2055 	}
2056 
2057 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
2058 		if ((ifp->if_flags & IFF_UP) != 0) {
2059 			int s = splnet();
2060 			if_up(ifp);
2061 			splx(s);
2062 		}
2063 	}
2064 #ifdef COMPAT_OIFREQ
2065 	if (cmd != ocmd)
2066 		ifreqn2o(oifr, ifr);
2067 #endif
2068 
2069 	ifnet_lock_exit(ifp->if_ioctl_lock);
2070 	return error;
2071 }
2072 
2073 /* This callback adds to the sum in `arg' the number of
2074  * threads on `ci' who have entered or who wait to enter the
2075  * critical section.
2076  */
2077 static void
2078 ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci)
2079 {
2080 	uint64_t *sum = arg, *nenter = p;
2081 
2082 	*sum += *nenter;
2083 }
2084 
2085 /* Return the number of threads who have entered or who wait
2086  * to enter the critical section on all CPUs.
2087  */
2088 static uint64_t
2089 ifnet_lock_entrances(struct ifnet_lock *il)
2090 {
2091 	uint64_t sum = 0;
2092 
2093 	percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum);
2094 
2095 	return sum;
2096 }
2097 
2098 static int
2099 ifioctl_attach(struct ifnet *ifp)
2100 {
2101 	struct ifnet_lock *il;
2102 
2103 	/* If the driver has not supplied its own if_ioctl, then
2104 	 * supply the default.
2105 	 */
2106 	if (ifp->if_ioctl == NULL)
2107 		ifp->if_ioctl = ifioctl_common;
2108 
2109 	/* Create an ifnet_lock for synchronizing ifioctls. */
2110 	if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL)
2111 		return ENOMEM;
2112 
2113 	il->il_nenter = percpu_alloc(sizeof(uint64_t));
2114 	if (il->il_nenter == NULL) {
2115 		kmem_free(il, sizeof(*il));
2116 		return ENOMEM;
2117 	}
2118 
2119 	mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE);
2120 	cv_init(&il->il_emptied, ifp->if_xname);
2121 
2122 	ifp->if_ioctl_lock = il;
2123 
2124 	return 0;
2125 }
2126 
2127 /*
2128  * This must not be called until after `ifp' has been withdrawn from the
2129  * ifnet tables so that ifioctl() cannot get a handle on it by calling
2130  * ifunit().
2131  */
2132 static void
2133 ifioctl_detach(struct ifnet *ifp)
2134 {
2135 	struct ifnet_lock *il;
2136 
2137 	il = ifp->if_ioctl_lock;
2138 	mutex_enter(&il->il_lock);
2139 	/* Install if_nullioctl to make sure that any thread that
2140 	 * subsequently enters the critical section will quit it
2141 	 * immediately and signal the condition variable that we
2142 	 * wait on, below.
2143 	 */
2144 	ifp->if_ioctl = if_nullioctl;
2145 	/* Sleep while threads are still in the critical section or
2146 	 * wait to enter it.
2147 	 */
2148 	while (ifnet_lock_entrances(il) != il->il_nexit)
2149 		cv_wait(&il->il_emptied, &il->il_lock);
2150 	/* At this point, we are the only thread still in the critical
2151 	 * section, and no new thread can get a handle on the ifioctl
2152 	 * lock, so it is safe to free its memory.
2153 	 */
2154 	mutex_exit(&il->il_lock);
2155 	ifp->if_ioctl_lock = NULL;
2156 	percpu_free(il->il_nenter, sizeof(uint64_t));
2157 	il->il_nenter = NULL;
2158 	cv_destroy(&il->il_emptied);
2159 	mutex_destroy(&il->il_lock);
2160 	kmem_free(il, sizeof(*il));
2161 }
2162 
2163 /*
2164  * Return interface configuration
2165  * of system.  List may be used
2166  * in later ioctl's (above) to get
2167  * other information.
2168  *
2169  * Each record is a struct ifreq.  Before the addition of
2170  * sockaddr_storage, the API rule was that sockaddr flavors that did
2171  * not fit would extend beyond the struct ifreq, with the next struct
2172  * ifreq starting sa_len beyond the struct sockaddr.  Because the
2173  * union in struct ifreq includes struct sockaddr_storage, every kind
2174  * of sockaddr must fit.  Thus, there are no longer any overlength
2175  * records.
2176  *
2177  * Records are added to the user buffer if they fit, and ifc_len is
2178  * adjusted to the length that was written.  Thus, the user is only
2179  * assured of getting the complete list if ifc_len on return is at
2180  * least sizeof(struct ifreq) less than it was on entry.
2181  *
2182  * If the user buffer pointer is NULL, this routine copies no data and
2183  * returns the amount of space that would be needed.
2184  *
2185  * Invariants:
2186  * ifrp points to the next part of the user's buffer to be used.  If
2187  * ifrp != NULL, space holds the number of bytes remaining that we may
2188  * write at ifrp.  Otherwise, space holds the number of bytes that
2189  * would have been written had there been adequate space.
2190  */
2191 /*ARGSUSED*/
2192 static int
2193 ifconf(u_long cmd, void *data)
2194 {
2195 	struct ifconf *ifc = (struct ifconf *)data;
2196 	struct ifnet *ifp;
2197 	struct ifaddr *ifa;
2198 	struct ifreq ifr, *ifrp = NULL;
2199 	int space = 0, error = 0;
2200 	const int sz = (int)sizeof(struct ifreq);
2201 	const bool docopy = ifc->ifc_req != NULL;
2202 
2203 	if (docopy) {
2204 		space = ifc->ifc_len;
2205 		ifrp = ifc->ifc_req;
2206 	}
2207 
2208 	IFNET_FOREACH(ifp) {
2209 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
2210 		    sizeof(ifr.ifr_name));
2211 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
2212 			return ENAMETOOLONG;
2213 		if (IFADDR_EMPTY(ifp)) {
2214 			/* Interface with no addresses - send zero sockaddr. */
2215 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2216 			if (!docopy) {
2217 				space += sz;
2218 				continue;
2219 			}
2220 			if (space >= sz) {
2221 				error = copyout(&ifr, ifrp, sz);
2222 				if (error != 0)
2223 					return error;
2224 				ifrp++;
2225 				space -= sz;
2226 			}
2227 		}
2228 
2229 		IFADDR_FOREACH(ifa, ifp) {
2230 			struct sockaddr *sa = ifa->ifa_addr;
2231 			/* all sockaddrs must fit in sockaddr_storage */
2232 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
2233 
2234 			if (!docopy) {
2235 				space += sz;
2236 				continue;
2237 			}
2238 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
2239 			if (space >= sz) {
2240 				error = copyout(&ifr, ifrp, sz);
2241 				if (error != 0)
2242 					return (error);
2243 				ifrp++; space -= sz;
2244 			}
2245 		}
2246 	}
2247 	if (docopy) {
2248 		KASSERT(0 <= space && space <= ifc->ifc_len);
2249 		ifc->ifc_len -= space;
2250 	} else {
2251 		KASSERT(space >= 0);
2252 		ifc->ifc_len = space;
2253 	}
2254 	return (0);
2255 }
2256 
2257 int
2258 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
2259 {
2260 	uint8_t len;
2261 #ifdef COMPAT_OIFREQ
2262 	struct ifreq ifrb;
2263 	struct oifreq *oifr = NULL;
2264 	u_long ocmd = cmd;
2265 	cmd = compat_cvtcmd(cmd);
2266 	if (cmd != ocmd) {
2267 		oifr = (struct oifreq *)(void *)ifr;
2268 		ifr = &ifrb;
2269 		ifreqo2n(oifr, ifr);
2270 		len = sizeof(oifr->ifr_addr);
2271 	} else
2272 #endif
2273 		len = sizeof(ifr->ifr_ifru.ifru_space);
2274 
2275 	if (len < sa->sa_len)
2276 		return EFBIG;
2277 
2278 	memset(&ifr->ifr_addr, 0, len);
2279 	sockaddr_copy(&ifr->ifr_addr, len, sa);
2280 
2281 #ifdef COMPAT_OIFREQ
2282 	if (cmd != ocmd)
2283 		ifreqn2o(oifr, ifr);
2284 #endif
2285 	return 0;
2286 }
2287 
2288 /*
2289  * Queue message on interface, and start output if interface
2290  * not yet active.
2291  */
2292 int
2293 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
2294     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2295 {
2296 	int len = m->m_pkthdr.len;
2297 	int mflags = m->m_flags;
2298 	int s = splnet();
2299 	int error;
2300 
2301 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2302 	if (error != 0)
2303 		goto out;
2304 	ifp->if_obytes += len;
2305 	if (mflags & M_MCAST)
2306 		ifp->if_omcasts++;
2307 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
2308 		(*ifp->if_start)(ifp);
2309 out:
2310 	splx(s);
2311 	return error;
2312 }
2313 
2314 /*
2315  * Queue message on interface, possibly using a second fast queue
2316  */
2317 int
2318 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
2319     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2320 {
2321 	int error = 0;
2322 
2323 	if (ifq != NULL
2324 #ifdef ALTQ
2325 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
2326 #endif
2327 	    ) {
2328 		if (IF_QFULL(ifq)) {
2329 			IF_DROP(&ifp->if_snd);
2330 			m_freem(m);
2331 			if (error == 0)
2332 				error = ENOBUFS;
2333 		} else
2334 			IF_ENQUEUE(ifq, m);
2335 	} else
2336 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2337 	if (error != 0) {
2338 		++ifp->if_oerrors;
2339 		return error;
2340 	}
2341 	return 0;
2342 }
2343 
2344 int
2345 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
2346 {
2347 	int rc;
2348 
2349 	if (ifp->if_initaddr != NULL)
2350 		rc = (*ifp->if_initaddr)(ifp, ifa, src);
2351 	else if (src ||
2352 	         (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
2353 		rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
2354 
2355 	return rc;
2356 }
2357 
2358 int
2359 if_do_dad(struct ifnet *ifp)
2360 {
2361 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2362 		return 0;
2363 
2364 	switch (ifp->if_type) {
2365 	case IFT_FAITH:
2366 		/*
2367 		 * These interfaces do not have the IFF_LOOPBACK flag,
2368 		 * but loop packets back.  We do not have to do DAD on such
2369 		 * interfaces.  We should even omit it, because loop-backed
2370 		 * responses would confuse the DAD procedure.
2371 		 */
2372 		return 0;
2373 	default:
2374 		/*
2375 		 * Our DAD routine requires the interface up and running.
2376 		 * However, some interfaces can be up before the RUNNING
2377 		 * status.  Additionaly, users may try to assign addresses
2378 		 * before the interface becomes up (or running).
2379 		 * We simply skip DAD in such a case as a work around.
2380 		 * XXX: we should rather mark "tentative" on such addresses,
2381 		 * and do DAD after the interface becomes ready.
2382 		 */
2383 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2384 		    (IFF_UP|IFF_RUNNING))
2385 			return 0;
2386 
2387 		return 1;
2388 	}
2389 }
2390 
2391 int
2392 if_flags_set(ifnet_t *ifp, const short flags)
2393 {
2394 	int rc;
2395 
2396 	if (ifp->if_setflags != NULL)
2397 		rc = (*ifp->if_setflags)(ifp, flags);
2398 	else {
2399 		short cantflags, chgdflags;
2400 		struct ifreq ifr;
2401 
2402 		chgdflags = ifp->if_flags ^ flags;
2403 		cantflags = chgdflags & IFF_CANTCHANGE;
2404 
2405 		if (cantflags != 0)
2406 			ifp->if_flags ^= cantflags;
2407 
2408                 /* Traditionally, we do not call if_ioctl after
2409                  * setting/clearing only IFF_PROMISC if the interface
2410                  * isn't IFF_UP.  Uphold that tradition.
2411 		 */
2412 		if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
2413 			return 0;
2414 
2415 		memset(&ifr, 0, sizeof(ifr));
2416 
2417 		ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
2418 		rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
2419 
2420 		if (rc != 0 && cantflags != 0)
2421 			ifp->if_flags ^= cantflags;
2422 	}
2423 
2424 	return rc;
2425 }
2426 
2427 int
2428 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
2429 {
2430 	int rc;
2431 	struct ifreq ifr;
2432 
2433 	if (ifp->if_mcastop != NULL)
2434 		rc = (*ifp->if_mcastop)(ifp, cmd, sa);
2435 	else {
2436 		ifreq_setaddr(cmd, &ifr, sa);
2437 		rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
2438 	}
2439 
2440 	return rc;
2441 }
2442 
2443 static void
2444 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
2445     struct ifaltq *ifq)
2446 {
2447 	const struct sysctlnode *cnode, *rnode;
2448 
2449 	if (sysctl_createv(clog, 0, NULL, &rnode,
2450 		       CTLFLAG_PERMANENT,
2451 		       CTLTYPE_NODE, "interfaces",
2452 		       SYSCTL_DESCR("Per-interface controls"),
2453 		       NULL, 0, NULL, 0,
2454 		       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
2455 		goto bad;
2456 
2457 	if (sysctl_createv(clog, 0, &rnode, &rnode,
2458 		       CTLFLAG_PERMANENT,
2459 		       CTLTYPE_NODE, ifname,
2460 		       SYSCTL_DESCR("Interface controls"),
2461 		       NULL, 0, NULL, 0,
2462 		       CTL_CREATE, CTL_EOL) != 0)
2463 		goto bad;
2464 
2465 	if (sysctl_createv(clog, 0, &rnode, &rnode,
2466 		       CTLFLAG_PERMANENT,
2467 		       CTLTYPE_NODE, "sndq",
2468 		       SYSCTL_DESCR("Interface output queue controls"),
2469 		       NULL, 0, NULL, 0,
2470 		       CTL_CREATE, CTL_EOL) != 0)
2471 		goto bad;
2472 
2473 	if (sysctl_createv(clog, 0, &rnode, &cnode,
2474 		       CTLFLAG_PERMANENT,
2475 		       CTLTYPE_INT, "len",
2476 		       SYSCTL_DESCR("Current output queue length"),
2477 		       NULL, 0, &ifq->ifq_len, 0,
2478 		       CTL_CREATE, CTL_EOL) != 0)
2479 		goto bad;
2480 
2481 	if (sysctl_createv(clog, 0, &rnode, &cnode,
2482 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2483 		       CTLTYPE_INT, "maxlen",
2484 		       SYSCTL_DESCR("Maximum allowed output queue length"),
2485 		       NULL, 0, &ifq->ifq_maxlen, 0,
2486 		       CTL_CREATE, CTL_EOL) != 0)
2487 		goto bad;
2488 
2489 	if (sysctl_createv(clog, 0, &rnode, &cnode,
2490 		       CTLFLAG_PERMANENT,
2491 		       CTLTYPE_INT, "drops",
2492 		       SYSCTL_DESCR("Packets dropped due to full output queue"),
2493 		       NULL, 0, &ifq->ifq_drops, 0,
2494 		       CTL_CREATE, CTL_EOL) != 0)
2495 		goto bad;
2496 
2497 	return;
2498 bad:
2499 	printf("%s: could not attach sysctl nodes\n", ifname);
2500 	return;
2501 }
2502 
2503 #if defined(INET) || defined(INET6)
2504 
2505 #define	SYSCTL_NET_PKTQ(q, cn, c)					\
2506 	static int							\
2507 	sysctl_net_##q##_##cn(SYSCTLFN_ARGS)				\
2508 	{								\
2509 		return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);	\
2510 	}
2511 
2512 #if defined(INET)
2513 static int
2514 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
2515 {
2516 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
2517 }
2518 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
2519 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
2520 #endif
2521 
2522 #if defined(INET6)
2523 static int
2524 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
2525 {
2526 	return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
2527 }
2528 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
2529 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
2530 #endif
2531 
2532 static void
2533 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
2534 {
2535 	sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
2536 	const char *pfname = NULL, *ipname = NULL;
2537 	int ipn = 0, qid = 0;
2538 
2539 	switch (pf) {
2540 #if defined(INET)
2541 	case PF_INET:
2542 		len_func = sysctl_net_ip_pktq_items;
2543 		maxlen_func = sysctl_net_ip_pktq_maxlen;
2544 		drops_func = sysctl_net_ip_pktq_drops;
2545 		pfname = "inet", ipn = IPPROTO_IP;
2546 		ipname = "ip", qid = IPCTL_IFQ;
2547 		break;
2548 #endif
2549 #if defined(INET6)
2550 	case PF_INET6:
2551 		len_func = sysctl_net_ip6_pktq_items;
2552 		maxlen_func = sysctl_net_ip6_pktq_maxlen;
2553 		drops_func = sysctl_net_ip6_pktq_drops;
2554 		pfname = "inet6", ipn = IPPROTO_IPV6;
2555 		ipname = "ip6", qid = IPV6CTL_IFQ;
2556 		break;
2557 #endif
2558 	default:
2559 		KASSERT(false);
2560 	}
2561 
2562 	sysctl_createv(clog, 0, NULL, NULL,
2563 		       CTLFLAG_PERMANENT,
2564 		       CTLTYPE_NODE, pfname, NULL,
2565 		       NULL, 0, NULL, 0,
2566 		       CTL_NET, pf, CTL_EOL);
2567 	sysctl_createv(clog, 0, NULL, NULL,
2568 		       CTLFLAG_PERMANENT,
2569 		       CTLTYPE_NODE, ipname, NULL,
2570 		       NULL, 0, NULL, 0,
2571 		       CTL_NET, pf, ipn, CTL_EOL);
2572 	sysctl_createv(clog, 0, NULL, NULL,
2573 		       CTLFLAG_PERMANENT,
2574 		       CTLTYPE_NODE, "ifq",
2575 		       SYSCTL_DESCR("Protocol input queue controls"),
2576 		       NULL, 0, NULL, 0,
2577 		       CTL_NET, pf, ipn, qid, CTL_EOL);
2578 
2579 	sysctl_createv(clog, 0, NULL, NULL,
2580 		       CTLFLAG_PERMANENT,
2581 		       CTLTYPE_INT, "len",
2582 		       SYSCTL_DESCR("Current input queue length"),
2583 		       len_func, 0, NULL, 0,
2584 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
2585 	sysctl_createv(clog, 0, NULL, NULL,
2586 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2587 		       CTLTYPE_INT, "maxlen",
2588 		       SYSCTL_DESCR("Maximum allowed input queue length"),
2589 		       maxlen_func, 0, NULL, 0,
2590 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
2591 	sysctl_createv(clog, 0, NULL, NULL,
2592 		       CTLFLAG_PERMANENT,
2593 		       CTLTYPE_INT, "drops",
2594 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
2595 		       drops_func, 0, NULL, 0,
2596 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
2597 }
2598 #endif /* INET || INET6 */
2599 
2600 static int
2601 if_sdl_sysctl(SYSCTLFN_ARGS)
2602 {
2603 	struct ifnet *ifp;
2604 	const struct sockaddr_dl *sdl;
2605 
2606 	if (namelen != 1)
2607 		return EINVAL;
2608 
2609 	ifp = if_byindex(name[0]);
2610 	if (ifp == NULL)
2611 		return ENODEV;
2612 
2613 	sdl = ifp->if_sadl;
2614 	if (sdl == NULL) {
2615 		*oldlenp = 0;
2616 		return 0;
2617 	}
2618 
2619 	if (oldp == NULL) {
2620 		*oldlenp = sdl->sdl_alen;
2621 		return 0;
2622 	}
2623 
2624 	if (*oldlenp >= sdl->sdl_alen)
2625 		*oldlenp = sdl->sdl_alen;
2626 	return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
2627 }
2628 
2629 SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
2630 {
2631 	const struct sysctlnode *rnode = NULL;
2632 
2633 	sysctl_createv(clog, 0, NULL, &rnode,
2634 		       CTLFLAG_PERMANENT,
2635 		       CTLTYPE_NODE, "sdl",
2636 		       SYSCTL_DESCR("Get active link-layer address"),
2637 		       if_sdl_sysctl, 0, NULL, 0,
2638 		       CTL_NET, CTL_CREATE, CTL_EOL);
2639 }
2640