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