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