xref: /netbsd-src/sys/net/if.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: if.c,v 1.170 2006/10/13 10:29:41 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000, 2001 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the project nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 /*
69  * Copyright (c) 1980, 1986, 1993
70  *	The Regents of the University of California.  All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  * 3. Neither the name of the University nor the names of its contributors
81  *    may be used to endorse or promote products derived from this software
82  *    without specific prior written permission.
83  *
84  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94  * SUCH DAMAGE.
95  *
96  *	@(#)if.c	8.5 (Berkeley) 1/9/95
97  */
98 
99 #include <sys/cdefs.h>
100 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.170 2006/10/13 10:29:41 hannken Exp $");
101 
102 #include "opt_inet.h"
103 
104 #include "opt_compat_linux.h"
105 #include "opt_compat_svr4.h"
106 #include "opt_compat_ultrix.h"
107 #include "opt_compat_43.h"
108 #include "opt_atalk.h"
109 #include "opt_natm.h"
110 #include "opt_pfil_hooks.h"
111 
112 #include <sys/param.h>
113 #include <sys/mbuf.h>
114 #include <sys/systm.h>
115 #include <sys/callout.h>
116 #include <sys/proc.h>
117 #include <sys/socket.h>
118 #include <sys/socketvar.h>
119 #include <sys/domain.h>
120 #include <sys/protosw.h>
121 #include <sys/kernel.h>
122 #include <sys/ioctl.h>
123 #include <sys/sysctl.h>
124 #include <sys/syslog.h>
125 #include <sys/kauth.h>
126 
127 #include <net/if.h>
128 #include <net/if_dl.h>
129 #include <net/if_ether.h>
130 #include <net/if_media.h>
131 #include <net80211/ieee80211.h>
132 #include <net80211/ieee80211_ioctl.h>
133 #include <net/if_types.h>
134 #include <net/radix.h>
135 #include <net/route.h>
136 #include <net/netisr.h>
137 #ifdef NETATALK
138 #include <netatalk/at_extern.h>
139 #include <netatalk/at.h>
140 #endif
141 #include <net/pfil.h>
142 
143 #ifdef INET6
144 #include <netinet/in.h>
145 #include <netinet6/in6_var.h>
146 #include <netinet6/nd6.h>
147 #endif
148 
149 #include "carp.h"
150 #if NCARP > 0
151 #include <netinet/ip_carp.h>
152 #endif
153 
154 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) || defined(COMPAT_ULTRIX) || defined(LKM)
155 #define COMPAT_OSOCK
156 #include <compat/sys/socket.h>
157 #endif
158 
159 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
160 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
161 
162 int	ifqmaxlen = IFQ_MAXLEN;
163 struct	callout if_slowtimo_ch;
164 
165 int netisr;			/* scheduling bits for network */
166 
167 static int	if_rt_walktree(struct radix_node *, void *);
168 
169 static struct if_clone *if_clone_lookup(const char *, int *);
170 static int	if_clone_list(struct if_clonereq *);
171 
172 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
173 static int if_cloners_count;
174 
175 #ifdef PFIL_HOOKS
176 struct pfil_head if_pfil;	/* packet filtering hook for interfaces */
177 #endif
178 
179 static void if_detach_queues(struct ifnet *, struct ifqueue *);
180 
181 /*
182  * Network interface utility routines.
183  *
184  * Routines with ifa_ifwith* names take sockaddr *'s as
185  * parameters.
186  */
187 void
188 ifinit(void)
189 {
190 
191 	callout_init(&if_slowtimo_ch);
192 	if_slowtimo(NULL);
193 #ifdef PFIL_HOOKS
194 	if_pfil.ph_type = PFIL_TYPE_IFNET;
195 	if_pfil.ph_ifnet = NULL;
196 	if (pfil_head_register(&if_pfil) != 0)
197 		printf("WARNING: unable to register pfil hook\n");
198 #endif
199 }
200 
201 /*
202  * Null routines used while an interface is going away.  These routines
203  * just return an error.
204  */
205 
206 int
207 if_nulloutput(struct ifnet *ifp __unused, struct mbuf *m __unused,
208     struct sockaddr *so __unused, struct rtentry *rt __unused)
209 {
210 
211 	return (ENXIO);
212 }
213 
214 void
215 if_nullinput(struct ifnet *ifp __unused, struct mbuf *m __unused)
216 {
217 
218 	/* Nothing. */
219 }
220 
221 void
222 if_nullstart(struct ifnet *ifp __unused)
223 {
224 
225 	/* Nothing. */
226 }
227 
228 int
229 if_nullioctl(struct ifnet *ifp __unused, u_long cmd __unused, caddr_t data __unused)
230 {
231 
232 	return (ENXIO);
233 }
234 
235 int
236 if_nullinit(struct ifnet *ifp __unused)
237 {
238 
239 	return (ENXIO);
240 }
241 
242 void
243 if_nullstop(struct ifnet *ifp __unused, int disable __unused)
244 {
245 
246 	/* Nothing. */
247 }
248 
249 void
250 if_nullwatchdog(struct ifnet *ifp __unused)
251 {
252 
253 	/* Nothing. */
254 }
255 
256 void
257 if_nulldrain(struct ifnet *ifp __unused)
258 {
259 
260 	/* Nothing. */
261 }
262 
263 static u_int if_index = 1;
264 struct ifnet_head ifnet;
265 size_t if_indexlim = 0;
266 struct ifaddr **ifnet_addrs = NULL;
267 struct ifnet **ifindex2ifnet = NULL;
268 struct ifnet *lo0ifp;
269 
270 /*
271  * Allocate the link level name for the specified interface.  This
272  * is an attachment helper.  It must be called after ifp->if_addrlen
273  * is initialized, which may not be the case when if_attach() is
274  * called.
275  */
276 void
277 if_alloc_sadl(struct ifnet *ifp)
278 {
279 	unsigned socksize, ifasize;
280 	int namelen, masklen;
281 	struct sockaddr_dl *sdl;
282 	struct ifaddr *ifa;
283 
284 	/*
285 	 * If the interface already has a link name, release it
286 	 * now.  This is useful for interfaces that can change
287 	 * link types, and thus switch link names often.
288 	 */
289 	if (ifp->if_sadl != NULL)
290 		if_free_sadl(ifp);
291 
292 	namelen = strlen(ifp->if_xname);
293 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
294 	socksize = masklen + ifp->if_addrlen;
295 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
296 	if (socksize < sizeof(*sdl))
297 		socksize = sizeof(*sdl);
298 	socksize = ROUNDUP(socksize);
299 	ifasize = sizeof(*ifa) + 2 * socksize;
300 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
301 	memset((caddr_t)ifa, 0, ifasize);
302 	sdl = (struct sockaddr_dl *)(ifa + 1);
303 	sdl->sdl_len = socksize;
304 	sdl->sdl_family = AF_LINK;
305 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
306 	sdl->sdl_nlen = namelen;
307 	sdl->sdl_alen = ifp->if_addrlen;
308 	sdl->sdl_index = ifp->if_index;
309 	sdl->sdl_type = ifp->if_type;
310 	ifnet_addrs[ifp->if_index] = ifa;
311 	IFAREF(ifa);
312 	ifa->ifa_ifp = ifp;
313 	ifa->ifa_rtrequest = link_rtrequest;
314 	TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
315 	IFAREF(ifa);
316 	ifa->ifa_addr = (struct sockaddr *)sdl;
317 	ifp->if_sadl = sdl;
318 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
319 	ifa->ifa_netmask = (struct sockaddr *)sdl;
320 	sdl->sdl_len = masklen;
321 	while (namelen != 0)
322 		sdl->sdl_data[--namelen] = 0xff;
323 }
324 
325 /*
326  * Free the link level name for the specified interface.  This is
327  * a detach helper.  This is called from if_detach() or from
328  * link layer type specific detach functions.
329  */
330 void
331 if_free_sadl(struct ifnet *ifp)
332 {
333 	struct ifaddr *ifa;
334 	int s;
335 
336 	ifa = ifnet_addrs[ifp->if_index];
337 	if (ifa == NULL) {
338 		KASSERT(ifp->if_sadl == NULL);
339 		return;
340 	}
341 
342 	KASSERT(ifp->if_sadl != NULL);
343 
344 	s = splnet();
345 	rtinit(ifa, RTM_DELETE, 0);
346 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
347 	IFAFREE(ifa);
348 
349 	ifp->if_sadl = NULL;
350 
351 	ifnet_addrs[ifp->if_index] = NULL;
352 	IFAFREE(ifa);
353 	splx(s);
354 }
355 
356 /*
357  * Attach an interface to the
358  * list of "active" interfaces.
359  */
360 void
361 if_attach(struct ifnet *ifp)
362 {
363 	int indexlim = 0;
364 
365 	if (if_indexlim == 0) {
366 		TAILQ_INIT(&ifnet);
367 		if_indexlim = 8;
368 	}
369 	TAILQ_INIT(&ifp->if_addrlist);
370 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
371 	ifp->if_index = if_index;
372 	if (ifindex2ifnet == 0)
373 		if_index++;
374 	else
375 		while (ifp->if_index < if_indexlim &&
376 		    ifindex2ifnet[ifp->if_index] != NULL) {
377 			++if_index;
378 			if (if_index == 0)
379 				if_index = 1;
380 			/*
381 			 * If we hit USHRT_MAX, we skip back to 0 since
382 			 * there are a number of places where the value
383 			 * of if_index or if_index itself is compared
384 			 * to or stored in an unsigned short.  By
385 			 * jumping back, we won't botch those assignments
386 			 * or comparisons.
387 			 */
388 			else if (if_index == USHRT_MAX) {
389 				/*
390 				 * However, if we have to jump back to
391 				 * zero *twice* without finding an empty
392 				 * slot in ifindex2ifnet[], then there
393 				 * there are too many (>65535) interfaces.
394 				 */
395 				if (indexlim++)
396 					panic("too many interfaces");
397 				else
398 					if_index = 1;
399 			}
400 			ifp->if_index = if_index;
401 		}
402 
403 	/*
404 	 * We have some arrays that should be indexed by if_index.
405 	 * since if_index will grow dynamically, they should grow too.
406 	 *	struct ifadd **ifnet_addrs
407 	 *	struct ifnet **ifindex2ifnet
408 	 */
409 	if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
410 	    ifp->if_index >= if_indexlim) {
411 		size_t m, n, oldlim;
412 		caddr_t q;
413 
414 		oldlim = if_indexlim;
415 		while (ifp->if_index >= if_indexlim)
416 			if_indexlim <<= 1;
417 
418 		/* grow ifnet_addrs */
419 		m = oldlim * sizeof(struct ifaddr *);
420 		n = if_indexlim * sizeof(struct ifaddr *);
421 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
422 		memset(q, 0, n);
423 		if (ifnet_addrs) {
424 			bcopy((caddr_t)ifnet_addrs, q, m);
425 			free((caddr_t)ifnet_addrs, M_IFADDR);
426 		}
427 		ifnet_addrs = (struct ifaddr **)q;
428 
429 		/* grow ifindex2ifnet */
430 		m = oldlim * sizeof(struct ifnet *);
431 		n = if_indexlim * sizeof(struct ifnet *);
432 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
433 		memset(q, 0, n);
434 		if (ifindex2ifnet) {
435 			bcopy((caddr_t)ifindex2ifnet, q, m);
436 			free((caddr_t)ifindex2ifnet, M_IFADDR);
437 		}
438 		ifindex2ifnet = (struct ifnet **)q;
439 	}
440 
441 	ifindex2ifnet[ifp->if_index] = ifp;
442 
443 	/*
444 	 * Link level name is allocated later by a separate call to
445 	 * if_alloc_sadl().
446 	 */
447 
448 	if (ifp->if_snd.ifq_maxlen == 0)
449 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
450 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
451 
452 	ifp->if_link_state = LINK_STATE_UNKNOWN;
453 
454 	ifp->if_capenable = 0;
455 	ifp->if_csum_flags_tx = 0;
456 	ifp->if_csum_flags_rx = 0;
457 
458 #ifdef ALTQ
459 	ifp->if_snd.altq_type = 0;
460 	ifp->if_snd.altq_disc = NULL;
461 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
462 	ifp->if_snd.altq_tbr  = NULL;
463 	ifp->if_snd.altq_ifp  = ifp;
464 #endif
465 
466 #ifdef PFIL_HOOKS
467 	ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
468 	ifp->if_pfil.ph_ifnet = ifp;
469 	if (pfil_head_register(&ifp->if_pfil) != 0)
470 		printf("%s: WARNING: unable to register pfil hook\n",
471 		    ifp->if_xname);
472 	(void)pfil_run_hooks(&if_pfil,
473 	    (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
474 #endif
475 
476 	if (!STAILQ_EMPTY(&domains))
477 		if_attachdomain1(ifp);
478 
479 	/* Announce the interface. */
480 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
481 }
482 
483 void
484 if_attachdomain(void)
485 {
486 	struct ifnet *ifp;
487 	int s;
488 
489 	s = splnet();
490 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
491 		if_attachdomain1(ifp);
492 	splx(s);
493 }
494 
495 void
496 if_attachdomain1(struct ifnet *ifp)
497 {
498 	struct domain *dp;
499 	int s;
500 
501 	s = splnet();
502 
503 	/* address family dependent data region */
504 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
505 	DOMAIN_FOREACH(dp) {
506 		if (dp->dom_ifattach)
507 			ifp->if_afdata[dp->dom_family] =
508 			    (*dp->dom_ifattach)(ifp);
509 	}
510 
511 	splx(s);
512 }
513 
514 /*
515  * Deactivate an interface.  This points all of the procedure
516  * handles at error stubs.  May be called from interrupt context.
517  */
518 void
519 if_deactivate(struct ifnet *ifp)
520 {
521 	int s;
522 
523 	s = splnet();
524 
525 	ifp->if_output	 = if_nulloutput;
526 	ifp->if_input	 = if_nullinput;
527 	ifp->if_start	 = if_nullstart;
528 	ifp->if_ioctl	 = if_nullioctl;
529 	ifp->if_init	 = if_nullinit;
530 	ifp->if_stop	 = if_nullstop;
531 	ifp->if_watchdog = if_nullwatchdog;
532 	ifp->if_drain	 = if_nulldrain;
533 
534 	/* No more packets may be enqueued. */
535 	ifp->if_snd.ifq_maxlen = 0;
536 
537 	splx(s);
538 }
539 
540 /*
541  * Detach an interface from the list of "active" interfaces,
542  * freeing any resources as we go along.
543  *
544  * NOTE: This routine must be called with a valid thread context,
545  * as it may block.
546  */
547 void
548 if_detach(struct ifnet *ifp)
549 {
550 	struct socket so;
551 	struct ifaddr *ifa, **ifap;
552 #ifdef IFAREF_DEBUG
553 	struct ifaddr *last_ifa = NULL;
554 #endif
555 	struct domain *dp;
556 	const struct protosw *pr;
557 	struct radix_node_head *rnh;
558 	int s, i, family, purged;
559 
560 	/*
561 	 * XXX It's kind of lame that we have to have the
562 	 * XXX socket structure...
563 	 */
564 	memset(&so, 0, sizeof(so));
565 
566 	s = splnet();
567 
568 	/*
569 	 * Do an if_down() to give protocols a chance to do something.
570 	 */
571 	if_down(ifp);
572 
573 #ifdef ALTQ
574 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
575 		altq_disable(&ifp->if_snd);
576 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
577 		altq_detach(&ifp->if_snd);
578 #endif
579 
580 
581 #if NCARP > 0
582 	/* Remove the interface from any carp group it is a part of.  */
583 	if (ifp->if_carp && ifp->if_type != IFT_CARP)
584 		carp_ifdetach(ifp);
585 #endif
586 
587 #ifdef PFIL_HOOKS
588 	(void)pfil_run_hooks(&if_pfil,
589 	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
590 	(void)pfil_head_unregister(&ifp->if_pfil);
591 #endif
592 
593 	/*
594 	 * Rip all the addresses off the interface.  This should make
595 	 * all of the routes go away.
596 	 */
597 	ifap = &TAILQ_FIRST(&ifp->if_addrlist); /* XXX abstraction violation */
598 	while ((ifa = *ifap)) {
599 		family = ifa->ifa_addr->sa_family;
600 #ifdef IFAREF_DEBUG
601 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
602 		    ifa, family, ifa->ifa_refcnt);
603 		if (last_ifa != NULL && ifa == last_ifa)
604 			panic("if_detach: loop detected");
605 		last_ifa = ifa;
606 #endif
607 		if (family == AF_LINK) {
608 			ifap = &TAILQ_NEXT(ifa, ifa_list);
609 			continue;
610 		}
611 		dp = pffinddomain(family);
612 #ifdef DIAGNOSTIC
613 		if (dp == NULL)
614 			panic("if_detach: no domain for AF %d",
615 			    family);
616 #endif
617 		/*
618 		 * XXX These PURGEIF calls are redundant with the
619 		 * purge-all-families calls below, but are left in for
620 		 * now both to make a smaller change, and to avoid
621 		 * unplanned interactions with clearing of
622 		 * ifp->if_addrlist.
623 		 */
624 		purged = 0;
625 		for (pr = dp->dom_protosw;
626 		     pr < dp->dom_protoswNPROTOSW; pr++) {
627 			so.so_proto = pr;
628 			if (pr->pr_usrreq != NULL) {
629 				(void) (*pr->pr_usrreq)(&so,
630 				    PRU_PURGEIF, NULL, NULL,
631 				    (struct mbuf *) ifp, curlwp);
632 				purged = 1;
633 			}
634 		}
635 		if (purged == 0) {
636 			/*
637 			 * XXX What's really the best thing to do
638 			 * XXX here?  --thorpej@NetBSD.org
639 			 */
640 			printf("if_detach: WARNING: AF %d not purged\n",
641 			    family);
642 			TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
643 		}
644 	}
645 
646 	if_free_sadl(ifp);
647 
648 	/* Walk the routing table looking for straglers. */
649 	for (i = 0; i <= AF_MAX; i++) {
650 		if ((rnh = rt_tables[i]) != NULL)
651 			(void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
652 	}
653 
654 	DOMAIN_FOREACH(dp) {
655 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
656 			(*dp->dom_ifdetach)(ifp,
657 			    ifp->if_afdata[dp->dom_family]);
658 
659 		/*
660 		 * One would expect multicast memberships (INET and
661 		 * INET6) on UDP sockets to be purged by the PURGEIF
662 		 * calls above, but if all addresses were removed from
663 		 * the interface prior to destruction, the calls will
664 		 * not be made (e.g. ppp, for which pppd(8) generally
665 		 * removes addresses before destroying the interface).
666 		 * Because there is no invariant that multicast
667 		 * memberships only exist for interfaces with IPv4
668 		 * addresses, we must call PURGEIF regardless of
669 		 * addresses.  (Protocols which might store ifnet
670 		 * pointers are marked with PR_PURGEIF.)
671 		 */
672 		for (pr = dp->dom_protosw;
673 		     pr < dp->dom_protoswNPROTOSW; pr++) {
674 			so.so_proto = pr;
675 			if (pr->pr_usrreq != NULL &&
676 			    pr->pr_flags & PR_PURGEIF)
677 				(void) (*pr->pr_usrreq)(&so,
678 				    PRU_PURGEIF, NULL, NULL,
679 				    (struct mbuf *) ifp, curlwp);
680 		}
681 	}
682 
683 	/* Announce that the interface is gone. */
684 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
685 
686 	ifindex2ifnet[ifp->if_index] = NULL;
687 
688 	TAILQ_REMOVE(&ifnet, ifp, if_list);
689 
690 	/*
691 	 * remove packets that came from ifp, from software interrupt queues.
692 	 */
693 	DOMAIN_FOREACH(dp) {
694 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
695 			if (dp->dom_ifqueues[i] == NULL)
696 				break;
697 			if_detach_queues(ifp, dp->dom_ifqueues[i]);
698 		}
699 	}
700 
701 	splx(s);
702 }
703 
704 static void
705 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
706 {
707 	struct mbuf *m, *prev, *next;
708 
709 	prev = NULL;
710 	for (m = q->ifq_head; m; m = next) {
711 		next = m->m_nextpkt;
712 #ifdef DIAGNOSTIC
713 		if ((m->m_flags & M_PKTHDR) == 0) {
714 			prev = m;
715 			continue;
716 		}
717 #endif
718 		if (m->m_pkthdr.rcvif != ifp) {
719 			prev = m;
720 			continue;
721 		}
722 
723 		if (prev)
724 			prev->m_nextpkt = m->m_nextpkt;
725 		else
726 			q->ifq_head = m->m_nextpkt;
727 		if (q->ifq_tail == m)
728 			q->ifq_tail = prev;
729 		q->ifq_len--;
730 
731 		m->m_nextpkt = NULL;
732 		m_freem(m);
733 		IF_DROP(q);
734 	}
735 }
736 
737 /*
738  * Callback for a radix tree walk to delete all references to an
739  * ifnet.
740  */
741 static int
742 if_rt_walktree(struct radix_node *rn, void *v)
743 {
744 	struct ifnet *ifp = (struct ifnet *)v;
745 	struct rtentry *rt = (struct rtentry *)rn;
746 	int error;
747 
748 	if (rt->rt_ifp == ifp) {
749 		/* Delete the entry. */
750 		error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
751 		    rt_mask(rt), rt->rt_flags, NULL);
752 		if (error)
753 			printf("%s: warning: unable to delete rtentry @ %p, "
754 			    "error = %d\n", ifp->if_xname, rt, error);
755 	}
756 	return (0);
757 }
758 
759 /*
760  * Create a clone network interface.
761  */
762 int
763 if_clone_create(const char *name)
764 {
765 	struct if_clone *ifc;
766 	int unit;
767 
768 	ifc = if_clone_lookup(name, &unit);
769 	if (ifc == NULL)
770 		return (EINVAL);
771 
772 	if (ifunit(name) != NULL)
773 		return (EEXIST);
774 
775 	return ((*ifc->ifc_create)(ifc, unit));
776 }
777 
778 /*
779  * Destroy a clone network interface.
780  */
781 int
782 if_clone_destroy(const char *name)
783 {
784 	struct if_clone *ifc;
785 	struct ifnet *ifp;
786 
787 	ifc = if_clone_lookup(name, NULL);
788 	if (ifc == NULL)
789 		return (EINVAL);
790 
791 	ifp = ifunit(name);
792 	if (ifp == NULL)
793 		return (ENXIO);
794 
795 	if (ifc->ifc_destroy == NULL)
796 		return (EOPNOTSUPP);
797 
798 	return ((*ifc->ifc_destroy)(ifp));
799 }
800 
801 /*
802  * Look up a network interface cloner.
803  */
804 static struct if_clone *
805 if_clone_lookup(const char *name, int *unitp)
806 {
807 	struct if_clone *ifc;
808 	const char *cp;
809 	int unit;
810 
811 	/* separate interface name from unit */
812 	for (cp = name;
813 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
814 	    cp++)
815 		continue;
816 
817 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
818 		return (NULL);	/* No name or unit number */
819 
820 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
821 		if (strlen(ifc->ifc_name) == cp - name &&
822 		    !strncmp(name, ifc->ifc_name, cp - name))
823 			break;
824 	}
825 
826 	if (ifc == NULL)
827 		return (NULL);
828 
829 	unit = 0;
830 	while (cp - name < IFNAMSIZ && *cp) {
831 		if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
832 			/* Bogus unit number. */
833 			return (NULL);
834 		}
835 		unit = (unit * 10) + (*cp++ - '0');
836 	}
837 
838 	if (unitp != NULL)
839 		*unitp = unit;
840 	return (ifc);
841 }
842 
843 /*
844  * Register a network interface cloner.
845  */
846 void
847 if_clone_attach(struct if_clone *ifc)
848 {
849 
850 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
851 	if_cloners_count++;
852 }
853 
854 /*
855  * Unregister a network interface cloner.
856  */
857 void
858 if_clone_detach(struct if_clone *ifc)
859 {
860 
861 	LIST_REMOVE(ifc, ifc_list);
862 	if_cloners_count--;
863 }
864 
865 /*
866  * Provide list of interface cloners to userspace.
867  */
868 static int
869 if_clone_list(struct if_clonereq *ifcr)
870 {
871 	char outbuf[IFNAMSIZ], *dst;
872 	struct if_clone *ifc;
873 	int count, error = 0;
874 
875 	ifcr->ifcr_total = if_cloners_count;
876 	if ((dst = ifcr->ifcr_buffer) == NULL) {
877 		/* Just asking how many there are. */
878 		return (0);
879 	}
880 
881 	if (ifcr->ifcr_count < 0)
882 		return (EINVAL);
883 
884 	count = (if_cloners_count < ifcr->ifcr_count) ?
885 	    if_cloners_count : ifcr->ifcr_count;
886 
887 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
888 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
889 		strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
890 		outbuf[IFNAMSIZ - 1] = '\0';	/* sanity */
891 		error = copyout(outbuf, dst, IFNAMSIZ);
892 		if (error)
893 			break;
894 	}
895 
896 	return (error);
897 }
898 
899 /*
900  * Locate an interface based on a complete address.
901  */
902 /*ARGSUSED*/
903 struct ifaddr *
904 ifa_ifwithaddr(const struct sockaddr *addr)
905 {
906 	struct ifnet *ifp;
907 	struct ifaddr *ifa;
908 
909 #define	equal(a1, a2) \
910   (bcmp((a1), (a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
911 
912 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
913 	     ifp = TAILQ_NEXT(ifp, if_list)) {
914 		if (ifp->if_output == if_nulloutput)
915 			continue;
916 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
917 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
918 			if (ifa->ifa_addr->sa_family != addr->sa_family)
919 				continue;
920 			if (equal(addr, ifa->ifa_addr))
921 				return (ifa);
922 			if ((ifp->if_flags & IFF_BROADCAST) &&
923 			    ifa->ifa_broadaddr &&
924 			    /* IP6 doesn't have broadcast */
925 			    ifa->ifa_broadaddr->sa_len != 0 &&
926 			    equal(ifa->ifa_broadaddr, addr))
927 				return (ifa);
928 		}
929 	}
930 	return (NULL);
931 }
932 
933 /*
934  * Locate the point to point interface with a given destination address.
935  */
936 /*ARGSUSED*/
937 struct ifaddr *
938 ifa_ifwithdstaddr(const struct sockaddr *addr)
939 {
940 	struct ifnet *ifp;
941 	struct ifaddr *ifa;
942 
943 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
944 	     ifp = TAILQ_NEXT(ifp, if_list)) {
945 		if (ifp->if_output == if_nulloutput)
946 			continue;
947 		if (ifp->if_flags & IFF_POINTOPOINT) {
948 			for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
949 			     ifa = TAILQ_NEXT(ifa, ifa_list)) {
950 				if (ifa->ifa_addr->sa_family !=
951 				      addr->sa_family ||
952 				    ifa->ifa_dstaddr == NULL)
953 					continue;
954 				if (equal(addr, ifa->ifa_dstaddr))
955 					return (ifa);
956 			}
957 		}
958 	}
959 	return (NULL);
960 }
961 
962 /*
963  * Find an interface on a specific network.  If many, choice
964  * is most specific found.
965  */
966 struct ifaddr *
967 ifa_ifwithnet(const struct sockaddr *addr)
968 {
969 	struct ifnet *ifp;
970 	struct ifaddr *ifa;
971 	const struct sockaddr_dl *sdl;
972 	struct ifaddr *ifa_maybe = 0;
973 	u_int af = addr->sa_family;
974 	char *addr_data = addr->sa_data, *cplim;
975 
976 	if (af == AF_LINK) {
977 		sdl = (const struct sockaddr_dl *)addr;
978 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
979 		    ifindex2ifnet[sdl->sdl_index] &&
980 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
981 			return (ifnet_addrs[sdl->sdl_index]);
982 	}
983 #ifdef NETATALK
984 	if (af == AF_APPLETALK) {
985 		const struct sockaddr_at *sat, *sat2;
986 		sat = (const struct sockaddr_at *)addr;
987 		for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
988 		     ifp = TAILQ_NEXT(ifp, if_list)) {
989 			if (ifp->if_output == if_nulloutput)
990 				continue;
991 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
992 			if (ifa == NULL)
993 				continue;
994 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
995 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
996 				return (ifa); /* exact match */
997 			if (ifa_maybe == NULL) {
998 				/* else keep the if with the right range */
999 				ifa_maybe = ifa;
1000 			}
1001 		}
1002 		return (ifa_maybe);
1003 	}
1004 #endif
1005 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1006 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1007 		if (ifp->if_output == if_nulloutput)
1008 			continue;
1009 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1010 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1011 			char *cp, *cp2, *cp3;
1012 
1013 			if (ifa->ifa_addr->sa_family != af ||
1014 			    ifa->ifa_netmask == 0)
1015  next:				continue;
1016 			cp = addr_data;
1017 			cp2 = ifa->ifa_addr->sa_data;
1018 			cp3 = ifa->ifa_netmask->sa_data;
1019 			cplim = (char *)ifa->ifa_netmask +
1020 			    ifa->ifa_netmask->sa_len;
1021 			while (cp3 < cplim) {
1022 				if ((*cp++ ^ *cp2++) & *cp3++) {
1023 					/* want to continue for() loop */
1024 					goto next;
1025 				}
1026 			}
1027 			if (ifa_maybe == 0 ||
1028 			    rn_refines((caddr_t)ifa->ifa_netmask,
1029 			    (caddr_t)ifa_maybe->ifa_netmask))
1030 				ifa_maybe = ifa;
1031 		}
1032 	}
1033 	return (ifa_maybe);
1034 }
1035 
1036 /*
1037  * Find the interface of the addresss.
1038  */
1039 struct ifaddr *
1040 ifa_ifwithladdr(const struct sockaddr *addr)
1041 {
1042 	struct ifaddr *ia;
1043 
1044 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1045 	    (ia = ifa_ifwithnet(addr)))
1046 		return (ia);
1047 	return (NULL);
1048 }
1049 
1050 /*
1051  * Find an interface using a specific address family
1052  */
1053 struct ifaddr *
1054 ifa_ifwithaf(int af)
1055 {
1056 	struct ifnet *ifp;
1057 	struct ifaddr *ifa;
1058 
1059 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1060 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1061 		if (ifp->if_output == if_nulloutput)
1062 			continue;
1063 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1064 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1065 			if (ifa->ifa_addr->sa_family == af)
1066 				return (ifa);
1067 		}
1068 	}
1069 	return (NULL);
1070 }
1071 
1072 /*
1073  * Find an interface address specific to an interface best matching
1074  * a given address.
1075  */
1076 struct ifaddr *
1077 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1078 {
1079 	struct ifaddr *ifa;
1080 	const char *cp, *cp2, *cp3;
1081 	const char *cplim;
1082 	struct ifaddr *ifa_maybe = 0;
1083 	u_int af = addr->sa_family;
1084 
1085 	if (ifp->if_output == if_nulloutput)
1086 		return (NULL);
1087 
1088 	if (af >= AF_MAX)
1089 		return (NULL);
1090 
1091 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1092 	     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1093 		if (ifa->ifa_addr->sa_family != af)
1094 			continue;
1095 		ifa_maybe = ifa;
1096 		if (ifa->ifa_netmask == 0) {
1097 			if (equal(addr, ifa->ifa_addr) ||
1098 			    (ifa->ifa_dstaddr &&
1099 			     equal(addr, ifa->ifa_dstaddr)))
1100 				return (ifa);
1101 			continue;
1102 		}
1103 		cp = addr->sa_data;
1104 		cp2 = ifa->ifa_addr->sa_data;
1105 		cp3 = ifa->ifa_netmask->sa_data;
1106 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1107 		for (; cp3 < cplim; cp3++) {
1108 			if ((*cp++ ^ *cp2++) & *cp3)
1109 				break;
1110 		}
1111 		if (cp3 == cplim)
1112 			return (ifa);
1113 	}
1114 	return (ifa_maybe);
1115 }
1116 
1117 /*
1118  * Default action when installing a route with a Link Level gateway.
1119  * Lookup an appropriate real ifa to point to.
1120  * This should be moved to /sys/net/link.c eventually.
1121  */
1122 void
1123 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1124 {
1125 	struct ifaddr *ifa;
1126 	struct sockaddr *dst;
1127 	struct ifnet *ifp;
1128 
1129 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1130 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1131 		return;
1132 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1133 		IFAFREE(rt->rt_ifa);
1134 		rt->rt_ifa = ifa;
1135 		IFAREF(ifa);
1136 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1137 			ifa->ifa_rtrequest(cmd, rt, info);
1138 	}
1139 }
1140 
1141 /*
1142  * Handle a change in the interface link state.
1143  */
1144 void
1145 if_link_state_change(struct ifnet *ifp, int link_state)
1146 {
1147 	/* Notify that the link state has changed. */
1148 	if (ifp->if_link_state != link_state) {
1149 		ifp->if_link_state = link_state;
1150 		rt_ifmsg(ifp);
1151 #if NCARP > 0
1152 		if (ifp->if_carp)
1153 			carp_carpdev_state(ifp);
1154 #endif
1155 	}
1156 }
1157 
1158 /*
1159  * Mark an interface down and notify protocols of
1160  * the transition.
1161  * NOTE: must be called at splsoftnet or equivalent.
1162  */
1163 void
1164 if_down(struct ifnet *ifp)
1165 {
1166 	struct ifaddr *ifa;
1167 
1168 	ifp->if_flags &= ~IFF_UP;
1169 	microtime(&ifp->if_lastchange);
1170 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1171 	     ifa = TAILQ_NEXT(ifa, ifa_list))
1172 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1173 	IFQ_PURGE(&ifp->if_snd);
1174 #if NCARP > 0
1175 	if (ifp->if_carp)
1176 		carp_carpdev_state(ifp);
1177 #endif
1178 	rt_ifmsg(ifp);
1179 }
1180 
1181 /*
1182  * Mark an interface up and notify protocols of
1183  * the transition.
1184  * NOTE: must be called at splsoftnet or equivalent.
1185  */
1186 void
1187 if_up(struct ifnet *ifp)
1188 {
1189 #ifdef notyet
1190 	struct ifaddr *ifa;
1191 #endif
1192 
1193 	ifp->if_flags |= IFF_UP;
1194 	microtime(&ifp->if_lastchange);
1195 #ifdef notyet
1196 	/* this has no effect on IP, and will kill all ISO connections XXX */
1197 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1198 	     ifa = TAILQ_NEXT(ifa, ifa_list))
1199 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
1200 #endif
1201 #if NCARP > 0
1202 	if (ifp->if_carp)
1203 		carp_carpdev_state(ifp);
1204 #endif
1205 	rt_ifmsg(ifp);
1206 #ifdef INET6
1207 	in6_if_up(ifp);
1208 #endif
1209 }
1210 
1211 /*
1212  * Handle interface watchdog timer routines.  Called
1213  * from softclock, we decrement timers (if set) and
1214  * call the appropriate interface routine on expiration.
1215  */
1216 void
1217 if_slowtimo(void *arg __unused)
1218 {
1219 	struct ifnet *ifp;
1220 	int s = splnet();
1221 
1222 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1223 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1224 		if (ifp->if_timer == 0 || --ifp->if_timer)
1225 			continue;
1226 		if (ifp->if_watchdog)
1227 			(*ifp->if_watchdog)(ifp);
1228 	}
1229 	splx(s);
1230 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
1231 	    if_slowtimo, NULL);
1232 }
1233 
1234 /*
1235  * Set/clear promiscuous mode on interface ifp based on the truth value
1236  * of pswitch.  The calls are reference counted so that only the first
1237  * "on" request actually has an effect, as does the final "off" request.
1238  * Results are undefined if the "off" and "on" requests are not matched.
1239  */
1240 int
1241 ifpromisc(struct ifnet *ifp, int pswitch)
1242 {
1243 	int pcount, ret;
1244 	short flags;
1245 	struct ifreq ifr;
1246 
1247 	pcount = ifp->if_pcount;
1248 	flags = ifp->if_flags;
1249 	if (pswitch) {
1250 		/*
1251 		 * Allow the device to be "placed" into promiscuous
1252 		 * mode even if it is not configured up.  It will
1253 		 * consult IFF_PROMISC when it is is brought up.
1254 		 */
1255 		if (ifp->if_pcount++ != 0)
1256 			return (0);
1257 		ifp->if_flags |= IFF_PROMISC;
1258 		if ((ifp->if_flags & IFF_UP) == 0)
1259 			return (0);
1260 	} else {
1261 		if (--ifp->if_pcount > 0)
1262 			return (0);
1263 		ifp->if_flags &= ~IFF_PROMISC;
1264 		/*
1265 		 * If the device is not configured up, we should not need to
1266 		 * turn off promiscuous mode (device should have turned it
1267 		 * off when interface went down; and will look at IFF_PROMISC
1268 		 * again next time interface comes up).
1269 		 */
1270 		if ((ifp->if_flags & IFF_UP) == 0)
1271 			return (0);
1272 	}
1273 	memset(&ifr, 0, sizeof(ifr));
1274 	ifr.ifr_flags = ifp->if_flags;
1275 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
1276 	/* Restore interface state if not successful. */
1277 	if (ret != 0) {
1278 		ifp->if_pcount = pcount;
1279 		ifp->if_flags = flags;
1280 	}
1281 	return (ret);
1282 }
1283 
1284 /*
1285  * Map interface name to
1286  * interface structure pointer.
1287  */
1288 struct ifnet *
1289 ifunit(const char *name)
1290 {
1291 	struct ifnet *ifp;
1292 	const char *cp = name;
1293 	u_int unit = 0;
1294 	u_int i;
1295 
1296 	/*
1297 	 * If the entire name is a number, treat it as an ifindex.
1298 	 */
1299 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1300 		unit = unit * 10 + (*cp - '0');
1301 	}
1302 
1303 	/*
1304 	 * If the number took all of the name, then it's a valid ifindex.
1305 	 */
1306 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1307 		if (unit >= if_indexlim)
1308 			return (NULL);
1309 		ifp = ifindex2ifnet[unit];
1310 		if (ifp == NULL || ifp->if_output == if_nulloutput)
1311 			return (NULL);
1312 		return (ifp);
1313 	}
1314 
1315 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1316 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1317 		if (ifp->if_output == if_nulloutput)
1318 			continue;
1319 	 	if (strcmp(ifp->if_xname, name) == 0)
1320 			return (ifp);
1321 	}
1322 	return (NULL);
1323 }
1324 
1325 /*
1326  * Interface ioctls.
1327  */
1328 int
1329 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct lwp *l)
1330 {
1331 	struct ifnet *ifp;
1332 	struct ifreq *ifr;
1333 	struct ifcapreq *ifcr;
1334 	struct ifdatareq *ifdr;
1335 	int s, error = 0;
1336 	short oif_flags;
1337 
1338 	switch (cmd) {
1339 
1340 	case SIOCGIFCONF:
1341 	case OSIOCGIFCONF:
1342 		return (ifconf(cmd, data));
1343 	}
1344 	ifr = (struct ifreq *)data;
1345 	ifcr = (struct ifcapreq *)data;
1346 	ifdr = (struct ifdatareq *)data;
1347 
1348 	switch (cmd) {
1349 	case SIOCIFCREATE:
1350 	case SIOCIFDESTROY:
1351 		if (l) {
1352 			error = kauth_authorize_generic(l->l_cred,
1353 			    KAUTH_GENERIC_ISSUSER, &l->l_acflag);
1354 			if (error)
1355 				return error;
1356 		}
1357 		return ((cmd == SIOCIFCREATE) ?
1358 			if_clone_create(ifr->ifr_name) :
1359 			if_clone_destroy(ifr->ifr_name));
1360 
1361 	case SIOCIFGCLONERS:
1362 		return (if_clone_list((struct if_clonereq *)data));
1363 	}
1364 
1365 	ifp = ifunit(ifr->ifr_name);
1366 	if (ifp == 0)
1367 		return (ENXIO);
1368 
1369 	switch (cmd) {
1370 	case SIOCSIFFLAGS:
1371 	case SIOCSIFCAP:
1372 	case SIOCSIFMETRIC:
1373 	case SIOCZIFDATA:
1374 	case SIOCSIFMTU:
1375 	case SIOCSIFPHYADDR:
1376 	case SIOCDIFPHYADDR:
1377 #ifdef INET6
1378 	case SIOCSIFPHYADDR_IN6:
1379 #endif
1380 	case SIOCSLIFPHYADDR:
1381 	case SIOCADDMULTI:
1382 	case SIOCDELMULTI:
1383 	case SIOCSIFMEDIA:
1384 	case SIOCSDRVSPEC:
1385 	case SIOCS80211NWID:
1386 	case SIOCS80211NWKEY:
1387 	case SIOCS80211POWER:
1388 	case SIOCS80211BSSID:
1389 	case SIOCS80211CHANNEL:
1390 		if (l) {
1391 			error = kauth_authorize_generic(l->l_cred,
1392 			    KAUTH_GENERIC_ISSUSER, &l->l_acflag);
1393 			if (error)
1394 				return error;
1395 		}
1396 	}
1397 
1398 	oif_flags = ifp->if_flags;
1399 	switch (cmd) {
1400 
1401 	case SIOCGIFFLAGS:
1402 		ifr->ifr_flags = ifp->if_flags;
1403 		break;
1404 
1405 	case SIOCGIFMETRIC:
1406 		ifr->ifr_metric = ifp->if_metric;
1407 		break;
1408 
1409 	case SIOCGIFMTU:
1410 		ifr->ifr_mtu = ifp->if_mtu;
1411 		break;
1412 
1413 	case SIOCGIFDLT:
1414 		ifr->ifr_dlt = ifp->if_dlt;
1415 		break;
1416 
1417 	case SIOCSIFFLAGS:
1418 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1419 			s = splnet();
1420 			if_down(ifp);
1421 			splx(s);
1422 		}
1423 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1424 			s = splnet();
1425 			if_up(ifp);
1426 			splx(s);
1427 		}
1428 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1429 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
1430 		if (ifp->if_ioctl)
1431 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
1432 		break;
1433 
1434 	case SIOCGIFCAP:
1435 		ifcr->ifcr_capabilities = ifp->if_capabilities;
1436 		ifcr->ifcr_capenable = ifp->if_capenable;
1437 		break;
1438 
1439 	case SIOCSIFCAP:
1440 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1441 			return (EINVAL);
1442 		if (ifp->if_ioctl == NULL)
1443 			return (EOPNOTSUPP);
1444 
1445 		/* Must prevent race with packet reception here. */
1446 		s = splnet();
1447 		if (ifcr->ifcr_capenable != ifp->if_capenable) {
1448 			struct ifreq ifrq;
1449 
1450 			ifrq.ifr_flags = ifp->if_flags;
1451 			ifp->if_capenable = ifcr->ifcr_capenable;
1452 
1453 			/* Pre-compute the checksum flags mask. */
1454 			ifp->if_csum_flags_tx = 0;
1455 			ifp->if_csum_flags_rx = 0;
1456 			if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1457 				ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1458 			}
1459 			if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1460 				ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1461 			}
1462 
1463 			if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1464 				ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1465 			}
1466 			if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1467 				ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1468 			}
1469 
1470 			if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1471 				ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1472 			}
1473 			if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1474 				ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1475 			}
1476 
1477 			if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1478 				ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1479 			}
1480 			if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1481 				ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1482 			}
1483 
1484 			if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1485 				ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1486 			}
1487 			if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1488 				ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1489 			}
1490 
1491 			/*
1492 			 * Only kick the interface if it's up.  If it's
1493 			 * not up now, it will notice the cap enables
1494 			 * when it is brought up later.
1495 			 */
1496 			if (ifp->if_flags & IFF_UP)
1497 				(void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1498 				    (caddr_t) &ifrq);
1499 		}
1500 		splx(s);
1501 		break;
1502 
1503 	case SIOCSIFMETRIC:
1504 		ifp->if_metric = ifr->ifr_metric;
1505 		break;
1506 
1507 	case SIOCGIFDATA:
1508 		ifdr->ifdr_data = ifp->if_data;
1509 		break;
1510 
1511 	case SIOCZIFDATA:
1512 		ifdr->ifdr_data = ifp->if_data;
1513 		/*
1514 		 * Assumes that the volatile counters that can be
1515 		 * zero'ed are at the end of if_data.
1516 		 */
1517 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1518 		    offsetof(struct if_data, ifi_ipackets));
1519 		break;
1520 
1521 	case SIOCSIFMTU:
1522 	{
1523 		u_long oldmtu = ifp->if_mtu;
1524 
1525 		if (ifp->if_ioctl == NULL)
1526 			return (EOPNOTSUPP);
1527 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1528 
1529 		/*
1530 		 * If the link MTU changed, do network layer specific procedure.
1531 		 */
1532 		if (ifp->if_mtu != oldmtu) {
1533 #ifdef INET6
1534 			nd6_setmtu(ifp);
1535 #endif
1536 		}
1537 		break;
1538 	}
1539 	case SIOCSIFPHYADDR:
1540 	case SIOCDIFPHYADDR:
1541 #ifdef INET6
1542 	case SIOCSIFPHYADDR_IN6:
1543 #endif
1544 	case SIOCSLIFPHYADDR:
1545 	case SIOCADDMULTI:
1546 	case SIOCDELMULTI:
1547 	case SIOCSIFMEDIA:
1548 	case SIOCGIFPSRCADDR:
1549 	case SIOCGIFPDSTADDR:
1550 	case SIOCGLIFPHYADDR:
1551 	case SIOCGIFMEDIA:
1552 		if (ifp->if_ioctl == 0)
1553 			return (EOPNOTSUPP);
1554 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1555 		break;
1556 
1557 	case SIOCSDRVSPEC:
1558 	case SIOCS80211NWID:
1559 	case SIOCS80211NWKEY:
1560 	case SIOCS80211POWER:
1561 	case SIOCS80211BSSID:
1562 	case SIOCS80211CHANNEL:
1563 	default:
1564 		if (so->so_proto == 0)
1565 			return (EOPNOTSUPP);
1566 #ifdef COMPAT_OSOCK
1567 		error = compat_ifioctl(so, cmd, data, l);
1568 #else
1569 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1570 		    (struct mbuf *)cmd, (struct mbuf *)data,
1571 		    (struct mbuf *)ifp, l));
1572 #endif
1573 		break;
1574 	}
1575 
1576 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1577 #ifdef INET6
1578 		if ((ifp->if_flags & IFF_UP) != 0) {
1579 			s = splnet();
1580 			in6_if_up(ifp);
1581 			splx(s);
1582 		}
1583 #endif
1584 	}
1585 
1586 	return (error);
1587 }
1588 
1589 /*
1590  * Return interface configuration
1591  * of system.  List may be used
1592  * in later ioctl's (above) to get
1593  * other information.
1594  */
1595 /*ARGSUSED*/
1596 int
1597 ifconf(u_long cmd __unused, caddr_t data)
1598 {
1599 	struct ifconf *ifc = (struct ifconf *)data;
1600 	struct ifnet *ifp;
1601 	struct ifaddr *ifa;
1602 	struct ifreq ifr, *ifrp;
1603 	int space = ifc->ifc_len, error = 0;
1604 	const int sz = (int)sizeof(ifr);
1605 	int sign;
1606 
1607 	if ((ifrp = ifc->ifc_req) == NULL) {
1608 		space = 0;
1609 		sign = -1;
1610 	} else {
1611 		sign = 1;
1612 	}
1613 	IFNET_FOREACH(ifp) {
1614 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1615 		if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) {
1616 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1617 			if (ifrp != NULL && space >= sz) {
1618 				error = copyout(&ifr, ifrp, sz);
1619 				if (error)
1620 					break;
1621 				ifrp++;
1622 			}
1623 			space -= sizeof(ifr) * sign;
1624 			continue;
1625 		}
1626 
1627 		for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
1628 			struct sockaddr *sa = ifa->ifa_addr;
1629 #ifdef COMPAT_OSOCK
1630 			if (cmd == OSIOCGIFCONF) {
1631 				struct osockaddr *osa =
1632 					 (struct osockaddr *)&ifr.ifr_addr;
1633 				/*
1634 				 * If it does not fit, we don't bother with it
1635 				 */
1636 				if (sa->sa_len > sizeof(*osa))
1637 					continue;
1638 				ifr.ifr_addr = *sa;
1639 				osa->sa_family = sa->sa_family;
1640 				if (ifrp != NULL && space >= sz) {
1641 					error = copyout(&ifr, ifrp, sz);
1642 					ifrp++;
1643 				}
1644 			} else
1645 #endif
1646 			if (sa->sa_len <= sizeof(*sa)) {
1647 				ifr.ifr_addr = *sa;
1648 				if (ifrp != NULL && space >= sz) {
1649 					error = copyout(&ifr, ifrp, sz);
1650 					ifrp++;
1651 				}
1652 			} else {
1653 				space -= (sa->sa_len - sizeof(*sa)) * sign;
1654 				if (ifrp != NULL && space >= sz) {
1655 					error = copyout(&ifr, ifrp,
1656 					    sizeof(ifr.ifr_name));
1657 					if (error == 0) {
1658 						error = copyout(sa,
1659 						    &ifrp->ifr_addr,
1660 						    sa->sa_len);
1661 					}
1662 					ifrp = (struct ifreq *)
1663 						(sa->sa_len +
1664 						 (caddr_t)&ifrp->ifr_addr);
1665 				}
1666 			}
1667 			if (error)
1668 				break;
1669 			space -= sz * sign;
1670 		}
1671 	}
1672 	if (ifrp != NULL)
1673 		ifc->ifc_len -= space;
1674 	else
1675 		ifc->ifc_len = space;
1676 	return (error);
1677 }
1678 
1679 /*
1680  * Queue message on interface, and start output if interface
1681  * not yet active.
1682  */
1683 int
1684 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1685     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1686 {
1687 	int len = m->m_pkthdr.len;
1688 	int mflags = m->m_flags;
1689 	int s = splnet();
1690 	int error;
1691 
1692 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1693 	if (error) {
1694 		splx(s);
1695 		return error;
1696 	}
1697 	ifp->if_obytes += len;
1698 	if (mflags & M_MCAST)
1699 		ifp->if_omcasts++;
1700 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
1701 		(*ifp->if_start)(ifp);
1702 	splx(s);
1703 	return error;
1704 }
1705 
1706 /*
1707  * Queue message on interface, possibly using a second fast queue
1708  */
1709 int
1710 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1711     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1712 {
1713 	int error = 0;
1714 
1715 	if (ifq != NULL
1716 #ifdef ALTQ
1717 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1718 #endif
1719 	    ) {
1720 		if (IF_QFULL(ifq)) {
1721 			IF_DROP(&ifp->if_snd);
1722 			m_freem(m);
1723 			if (error == 0)
1724 				error = ENOBUFS;
1725 		}
1726 		else
1727 			IF_ENQUEUE(ifq, m);
1728 	} else
1729 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1730 	if (error != 0) {
1731 		++ifp->if_oerrors;
1732 		return error;
1733 	}
1734 
1735 	return 0;
1736 }
1737 
1738 
1739 #if defined(INET) || defined(INET6)
1740 static void
1741 sysctl_net_ifq_setup(struct sysctllog **clog,
1742 		     int pf, const char *pfname,
1743 		     int ipn, const char *ipname,
1744 		     int qid, struct ifqueue *ifq)
1745 {
1746 
1747 	sysctl_createv(clog, 0, NULL, NULL,
1748 		       CTLFLAG_PERMANENT,
1749 		       CTLTYPE_NODE, "net", NULL,
1750 		       NULL, 0, NULL, 0,
1751 		       CTL_NET, CTL_EOL);
1752 	sysctl_createv(clog, 0, NULL, NULL,
1753 		       CTLFLAG_PERMANENT,
1754 		       CTLTYPE_NODE, pfname, NULL,
1755 		       NULL, 0, NULL, 0,
1756 		       CTL_NET, pf, CTL_EOL);
1757 	sysctl_createv(clog, 0, NULL, NULL,
1758 		       CTLFLAG_PERMANENT,
1759 		       CTLTYPE_NODE, ipname, NULL,
1760 		       NULL, 0, NULL, 0,
1761 		       CTL_NET, pf, ipn, CTL_EOL);
1762 	sysctl_createv(clog, 0, NULL, NULL,
1763 		       CTLFLAG_PERMANENT,
1764 		       CTLTYPE_NODE, "ifq",
1765 		       SYSCTL_DESCR("Protocol input queue controls"),
1766 		       NULL, 0, NULL, 0,
1767 		       CTL_NET, pf, ipn, qid, CTL_EOL);
1768 
1769 	sysctl_createv(clog, 0, NULL, NULL,
1770 		       CTLFLAG_PERMANENT,
1771 		       CTLTYPE_INT, "len",
1772 		       SYSCTL_DESCR("Current input queue length"),
1773 		       NULL, 0, &ifq->ifq_len, 0,
1774 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1775 	sysctl_createv(clog, 0, NULL, NULL,
1776 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1777 		       CTLTYPE_INT, "maxlen",
1778 		       SYSCTL_DESCR("Maximum allowed input queue length"),
1779 		       NULL, 0, &ifq->ifq_maxlen, 0,
1780 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1781 #ifdef notyet
1782 	sysctl_createv(clog, 0, NULL, NULL,
1783 		       CTLFLAG_PERMANENT,
1784 		       CTLTYPE_INT, "peak",
1785 		       SYSCTL_DESCR("Highest input queue length"),
1786 		       NULL, 0, &ifq->ifq_peak, 0,
1787 		       CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1788 #endif
1789 	sysctl_createv(clog, 0, NULL, NULL,
1790 		       CTLFLAG_PERMANENT,
1791 		       CTLTYPE_INT, "drops",
1792 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
1793 		       NULL, 0, &ifq->ifq_drops, 0,
1794 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1795 }
1796 
1797 #ifdef INET
1798 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1799 	     "sysctl net.inet.ip.ifq subtree setup")
1800 {
1801 	extern struct ifqueue ipintrq;
1802 
1803 	sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1804 			     IPCTL_IFQ, &ipintrq);
1805 }
1806 #endif /* INET */
1807 
1808 #ifdef INET6
1809 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1810 	     "sysctl net.inet6.ip6.ifq subtree setup")
1811 {
1812 	extern struct ifqueue ip6intrq;
1813 
1814 	sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1815 			     IPV6CTL_IFQ, &ip6intrq);
1816 }
1817 #endif /* INET6 */
1818 #endif /* INET || INET6 */
1819