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