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