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