xref: /netbsd-src/sys/net/if.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: if.c,v 1.160 2005/07/19 12:58:24 gdt 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.160 2005/07/19 12:58:24 gdt 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 		/*
622 		 * XXX These PURGEIF calls are redundant with the
623 		 * purge-all-families calls below, but are left in for
624 		 * now both to make a smaller change, and to avoid
625 		 * unplanned interactions with clearing of
626 		 * ifp->if_addrlist.
627 		 */
628 		purged = 0;
629 		for (pr = dp->dom_protosw;
630 		     pr < dp->dom_protoswNPROTOSW; pr++) {
631 			so.so_proto = pr;
632 			if (pr->pr_usrreq != NULL) {
633 				(void) (*pr->pr_usrreq)(&so,
634 				    PRU_PURGEIF, NULL, NULL,
635 				    (struct mbuf *) ifp, curproc);
636 				purged = 1;
637 			}
638 		}
639 		if (purged == 0) {
640 			/*
641 			 * XXX What's really the best thing to do
642 			 * XXX here?  --thorpej@NetBSD.org
643 			 */
644 			printf("if_detach: WARNING: AF %d not purged\n",
645 			    family);
646 			TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
647 		}
648 	}
649 
650 	if_free_sadl(ifp);
651 
652 	/* Walk the routing table looking for straglers. */
653 	for (i = 0; i <= AF_MAX; i++) {
654 		if ((rnh = rt_tables[i]) != NULL)
655 			(void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
656 	}
657 
658 	DOMAIN_FOREACH(dp) {
659 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
660 			(*dp->dom_ifdetach)(ifp,
661 			    ifp->if_afdata[dp->dom_family]);
662 
663 		/*
664 		 * One would expect multicast memberships (INET and
665 		 * INET6) on UDP sockets to be purged by the PURGEIF
666 		 * calls above, but if all addresses were removed from
667 		 * the interface prior to destruction, the calls will
668 		 * not be made (e.g. ppp, for which pppd(8) generally
669 		 * removes addresses before destroying the interface).
670 		 * Because there is no invariant that multicast
671 		 * memberships only exist for interfaces with IPv4
672 		 * addresses, we must call PURGEIF regardless of
673 		 * addresses.  (Protocols which might store ifnet
674 		 * pointers are marked with PR_PURGEIF.)
675 		 */
676 		for (pr = dp->dom_protosw;
677 		     pr < dp->dom_protoswNPROTOSW; pr++) {
678 			so.so_proto = pr;
679 			if (pr->pr_usrreq != NULL &&
680 			    pr->pr_flags & PR_PURGEIF)
681 				(void) (*pr->pr_usrreq)(&so,
682 				    PRU_PURGEIF, NULL, NULL,
683 				    (struct mbuf *) ifp, curproc);
684 		}
685 	}
686 
687 	/* Announce that the interface is gone. */
688 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
689 
690 	ifindex2ifnet[ifp->if_index] = NULL;
691 
692 	TAILQ_REMOVE(&ifnet, ifp, if_list);
693 
694 	/*
695 	 * remove packets came from ifp, from software interrupt queues.
696 	 * net/netisr_dispatch.h is not usable, as some of them use
697 	 * strange queue names.
698 	 */
699 #define IF_DETACH_QUEUES(x) \
700 do { \
701 	extern struct ifqueue x; \
702 	if_detach_queues(ifp, & x); \
703 } while (/*CONSTCOND*/ 0)
704 #ifdef INET
705 #if NARP > 0
706 	IF_DETACH_QUEUES(arpintrq);
707 #endif
708 	IF_DETACH_QUEUES(ipintrq);
709 #endif
710 #ifdef INET6
711 	IF_DETACH_QUEUES(ip6intrq);
712 #endif
713 #ifdef NETATALK
714 	IF_DETACH_QUEUES(atintrq1);
715 	IF_DETACH_QUEUES(atintrq2);
716 #endif
717 #ifdef NS
718 	IF_DETACH_QUEUES(nsintrq);
719 #endif
720 #ifdef ISO
721 	IF_DETACH_QUEUES(clnlintrq);
722 #endif
723 #ifdef CCITT
724 	IF_DETACH_QUEUES(llcintrq);
725 	IF_DETACH_QUEUES(hdintrq);
726 #endif
727 #ifdef NATM
728 	IF_DETACH_QUEUES(natmintrq);
729 #endif
730 #ifdef DECNET
731 	IF_DETACH_QUEUES(decnetintrq);
732 #endif
733 #undef IF_DETACH_QUEUES
734 
735 	splx(s);
736 }
737 
738 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \
739     defined(ISO) || defined(CCITT) || defined(NATM) || defined(DECNET)
740 static void
741 if_detach_queues(ifp, q)
742 	struct ifnet *ifp;
743 	struct ifqueue *q;
744 {
745 	struct mbuf *m, *prev, *next;
746 
747 	prev = NULL;
748 	for (m = q->ifq_head; m; m = next) {
749 		next = m->m_nextpkt;
750 #ifdef DIAGNOSTIC
751 		if ((m->m_flags & M_PKTHDR) == 0) {
752 			prev = m;
753 			continue;
754 		}
755 #endif
756 		if (m->m_pkthdr.rcvif != ifp) {
757 			prev = m;
758 			continue;
759 		}
760 
761 		if (prev)
762 			prev->m_nextpkt = m->m_nextpkt;
763 		else
764 			q->ifq_head = m->m_nextpkt;
765 		if (q->ifq_tail == m)
766 			q->ifq_tail = prev;
767 		q->ifq_len--;
768 
769 		m->m_nextpkt = NULL;
770 		m_freem(m);
771 		IF_DROP(q);
772 	}
773 }
774 #endif /* defined(INET) || ... */
775 
776 /*
777  * Callback for a radix tree walk to delete all references to an
778  * ifnet.
779  */
780 int
781 if_rt_walktree(rn, v)
782 	struct radix_node *rn;
783 	void *v;
784 {
785 	struct ifnet *ifp = (struct ifnet *)v;
786 	struct rtentry *rt = (struct rtentry *)rn;
787 	int error;
788 
789 	if (rt->rt_ifp == ifp) {
790 		/* Delete the entry. */
791 		error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
792 		    rt_mask(rt), rt->rt_flags, NULL);
793 		if (error)
794 			printf("%s: warning: unable to delete rtentry @ %p, "
795 			    "error = %d\n", ifp->if_xname, rt, error);
796 	}
797 	return (0);
798 }
799 
800 /*
801  * Create a clone network interface.
802  */
803 int
804 if_clone_create(name)
805 	const char *name;
806 {
807 	struct if_clone *ifc;
808 	int unit;
809 
810 	ifc = if_clone_lookup(name, &unit);
811 	if (ifc == NULL)
812 		return (EINVAL);
813 
814 	if (ifunit(name) != NULL)
815 		return (EEXIST);
816 
817 	return ((*ifc->ifc_create)(ifc, unit));
818 }
819 
820 /*
821  * Destroy a clone network interface.
822  */
823 int
824 if_clone_destroy(name)
825 	const char *name;
826 {
827 	struct if_clone *ifc;
828 	struct ifnet *ifp;
829 
830 	ifc = if_clone_lookup(name, NULL);
831 	if (ifc == NULL)
832 		return (EINVAL);
833 
834 	ifp = ifunit(name);
835 	if (ifp == NULL)
836 		return (ENXIO);
837 
838 	if (ifc->ifc_destroy == NULL)
839 		return (EOPNOTSUPP);
840 
841 	return ((*ifc->ifc_destroy)(ifp));
842 }
843 
844 /*
845  * Look up a network interface cloner.
846  */
847 struct if_clone *
848 if_clone_lookup(name, unitp)
849 	const char *name;
850 	int *unitp;
851 {
852 	struct if_clone *ifc;
853 	const char *cp;
854 	int unit;
855 
856 	/* separate interface name from unit */
857 	for (cp = name;
858 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
859 	    cp++)
860 		continue;
861 
862 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
863 		return (NULL);	/* No name or unit number */
864 
865 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
866 		if (strlen(ifc->ifc_name) == cp - name &&
867 		    !strncmp(name, ifc->ifc_name, cp - name))
868 			break;
869 	}
870 
871 	if (ifc == NULL)
872 		return (NULL);
873 
874 	unit = 0;
875 	while (cp - name < IFNAMSIZ && *cp) {
876 		if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
877 			/* Bogus unit number. */
878 			return (NULL);
879 		}
880 		unit = (unit * 10) + (*cp++ - '0');
881 	}
882 
883 	if (unitp != NULL)
884 		*unitp = unit;
885 	return (ifc);
886 }
887 
888 /*
889  * Register a network interface cloner.
890  */
891 void
892 if_clone_attach(ifc)
893 	struct if_clone *ifc;
894 {
895 
896 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
897 	if_cloners_count++;
898 }
899 
900 /*
901  * Unregister a network interface cloner.
902  */
903 void
904 if_clone_detach(ifc)
905 	struct if_clone *ifc;
906 {
907 
908 	LIST_REMOVE(ifc, ifc_list);
909 	if_cloners_count--;
910 }
911 
912 /*
913  * Provide list of interface cloners to userspace.
914  */
915 int
916 if_clone_list(ifcr)
917 	struct if_clonereq *ifcr;
918 {
919 	char outbuf[IFNAMSIZ], *dst;
920 	struct if_clone *ifc;
921 	int count, error = 0;
922 
923 	ifcr->ifcr_total = if_cloners_count;
924 	if ((dst = ifcr->ifcr_buffer) == NULL) {
925 		/* Just asking how many there are. */
926 		return (0);
927 	}
928 
929 	if (ifcr->ifcr_count < 0)
930 		return (EINVAL);
931 
932 	count = (if_cloners_count < ifcr->ifcr_count) ?
933 	    if_cloners_count : ifcr->ifcr_count;
934 
935 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
936 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
937 		strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
938 		outbuf[IFNAMSIZ - 1] = '\0';	/* sanity */
939 		error = copyout(outbuf, dst, IFNAMSIZ);
940 		if (error)
941 			break;
942 	}
943 
944 	return (error);
945 }
946 
947 /*
948  * Locate an interface based on a complete address.
949  */
950 /*ARGSUSED*/
951 struct ifaddr *
952 ifa_ifwithaddr(addr)
953 	const struct sockaddr *addr;
954 {
955 	struct ifnet *ifp;
956 	struct ifaddr *ifa;
957 
958 #define	equal(a1, a2) \
959   (bcmp((a1), (a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
960 
961 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
962 	     ifp = TAILQ_NEXT(ifp, if_list)) {
963 		if (ifp->if_output == if_nulloutput)
964 			continue;
965 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
966 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
967 			if (ifa->ifa_addr->sa_family != addr->sa_family)
968 				continue;
969 			if (equal(addr, ifa->ifa_addr))
970 				return (ifa);
971 			if ((ifp->if_flags & IFF_BROADCAST) &&
972 			    ifa->ifa_broadaddr &&
973 			    /* IP6 doesn't have broadcast */
974 			    ifa->ifa_broadaddr->sa_len != 0 &&
975 			    equal(ifa->ifa_broadaddr, addr))
976 				return (ifa);
977 		}
978 	}
979 	return (NULL);
980 }
981 
982 /*
983  * Locate the point to point interface with a given destination address.
984  */
985 /*ARGSUSED*/
986 struct ifaddr *
987 ifa_ifwithdstaddr(addr)
988 	const struct sockaddr *addr;
989 {
990 	struct ifnet *ifp;
991 	struct ifaddr *ifa;
992 
993 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
994 	     ifp = TAILQ_NEXT(ifp, if_list)) {
995 		if (ifp->if_output == if_nulloutput)
996 			continue;
997 		if (ifp->if_flags & IFF_POINTOPOINT) {
998 			for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
999 			     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1000 				if (ifa->ifa_addr->sa_family !=
1001 				      addr->sa_family ||
1002 				    ifa->ifa_dstaddr == NULL)
1003 					continue;
1004 				if (equal(addr, ifa->ifa_dstaddr))
1005 					return (ifa);
1006 			}
1007 		}
1008 	}
1009 	return (NULL);
1010 }
1011 
1012 /*
1013  * Find an interface on a specific network.  If many, choice
1014  * is most specific found.
1015  */
1016 struct ifaddr *
1017 ifa_ifwithnet(addr)
1018 	const struct sockaddr *addr;
1019 {
1020 	struct ifnet *ifp;
1021 	struct ifaddr *ifa;
1022 	const struct sockaddr_dl *sdl;
1023 	struct ifaddr *ifa_maybe = 0;
1024 	u_int af = addr->sa_family;
1025 	char *addr_data = addr->sa_data, *cplim;
1026 
1027 	if (af == AF_LINK) {
1028 		sdl = (const struct sockaddr_dl *)addr;
1029 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1030 		    ifindex2ifnet[sdl->sdl_index] &&
1031 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
1032 			return (ifnet_addrs[sdl->sdl_index]);
1033 	}
1034 #ifdef NETATALK
1035 	if (af == AF_APPLETALK) {
1036 		const struct sockaddr_at *sat, *sat2;
1037 		sat = (const struct sockaddr_at *)addr;
1038 		for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1039 		     ifp = TAILQ_NEXT(ifp, if_list)) {
1040 			if (ifp->if_output == if_nulloutput)
1041 				continue;
1042 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1043 			if (ifa == NULL)
1044 				continue;
1045 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1046 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1047 				return (ifa); /* exact match */
1048 			if (ifa_maybe == NULL) {
1049 				/* else keep the if with the right range */
1050 				ifa_maybe = ifa;
1051 			}
1052 		}
1053 		return (ifa_maybe);
1054 	}
1055 #endif
1056 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1057 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1058 		if (ifp->if_output == if_nulloutput)
1059 			continue;
1060 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1061 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1062 			char *cp, *cp2, *cp3;
1063 
1064 			if (ifa->ifa_addr->sa_family != af ||
1065 			    ifa->ifa_netmask == 0)
1066  next:				continue;
1067 			cp = addr_data;
1068 			cp2 = ifa->ifa_addr->sa_data;
1069 			cp3 = ifa->ifa_netmask->sa_data;
1070 			cplim = (char *)ifa->ifa_netmask +
1071 			    ifa->ifa_netmask->sa_len;
1072 			while (cp3 < cplim) {
1073 				if ((*cp++ ^ *cp2++) & *cp3++) {
1074 					/* want to continue for() loop */
1075 					goto next;
1076 				}
1077 			}
1078 			if (ifa_maybe == 0 ||
1079 			    rn_refines((caddr_t)ifa->ifa_netmask,
1080 			    (caddr_t)ifa_maybe->ifa_netmask))
1081 				ifa_maybe = ifa;
1082 		}
1083 	}
1084 	return (ifa_maybe);
1085 }
1086 
1087 /*
1088  * Find the interface of the addresss.
1089  */
1090 struct ifaddr *
1091 ifa_ifwithladdr(addr)
1092 	const struct sockaddr *addr;
1093 {
1094 	struct ifaddr *ia;
1095 
1096 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1097 	    (ia = ifa_ifwithnet(addr)))
1098 		return (ia);
1099 	return (NULL);
1100 }
1101 
1102 /*
1103  * Find an interface using a specific address family
1104  */
1105 struct ifaddr *
1106 ifa_ifwithaf(af)
1107 	int af;
1108 {
1109 	struct ifnet *ifp;
1110 	struct ifaddr *ifa;
1111 
1112 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1113 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1114 		if (ifp->if_output == if_nulloutput)
1115 			continue;
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 				return (ifa);
1120 		}
1121 	}
1122 	return (NULL);
1123 }
1124 
1125 /*
1126  * Find an interface address specific to an interface best matching
1127  * a given address.
1128  */
1129 struct ifaddr *
1130 ifaof_ifpforaddr(addr, ifp)
1131 	const struct sockaddr *addr;
1132 	struct ifnet *ifp;
1133 {
1134 	struct ifaddr *ifa;
1135 	const char *cp, *cp2, *cp3;
1136 	const char *cplim;
1137 	struct ifaddr *ifa_maybe = 0;
1138 	u_int af = addr->sa_family;
1139 
1140 	if (ifp->if_output == if_nulloutput)
1141 		return (NULL);
1142 
1143 	if (af >= AF_MAX)
1144 		return (NULL);
1145 
1146 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1147 	     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1148 		if (ifa->ifa_addr->sa_family != af)
1149 			continue;
1150 		ifa_maybe = ifa;
1151 		if (ifa->ifa_netmask == 0) {
1152 			if (equal(addr, ifa->ifa_addr) ||
1153 			    (ifa->ifa_dstaddr &&
1154 			     equal(addr, ifa->ifa_dstaddr)))
1155 				return (ifa);
1156 			continue;
1157 		}
1158 		cp = addr->sa_data;
1159 		cp2 = ifa->ifa_addr->sa_data;
1160 		cp3 = ifa->ifa_netmask->sa_data;
1161 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1162 		for (; cp3 < cplim; cp3++) {
1163 			if ((*cp++ ^ *cp2++) & *cp3)
1164 				break;
1165 		}
1166 		if (cp3 == cplim)
1167 			return (ifa);
1168 	}
1169 	return (ifa_maybe);
1170 }
1171 
1172 /*
1173  * Default action when installing a route with a Link Level gateway.
1174  * Lookup an appropriate real ifa to point to.
1175  * This should be moved to /sys/net/link.c eventually.
1176  */
1177 void
1178 link_rtrequest(cmd, rt, info)
1179 	int cmd;
1180 	struct rtentry *rt;
1181 	struct rt_addrinfo *info;
1182 {
1183 	struct ifaddr *ifa;
1184 	struct sockaddr *dst;
1185 	struct ifnet *ifp;
1186 
1187 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1188 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1189 		return;
1190 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1191 		IFAFREE(rt->rt_ifa);
1192 		rt->rt_ifa = ifa;
1193 		IFAREF(ifa);
1194 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1195 			ifa->ifa_rtrequest(cmd, rt, info);
1196 	}
1197 }
1198 
1199 /*
1200  * Handle a change in the interface link state.
1201  */
1202 void
1203 if_link_state_change(struct ifnet *ifp, int link_state)
1204 {
1205 	/* Notify that the link state has changed. */
1206 	if (ifp->if_link_state != link_state) {
1207 		ifp->if_link_state = link_state;
1208 		rt_ifmsg(ifp);
1209 		log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1210 		    (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1211 	}
1212 }
1213 
1214 /*
1215  * Mark an interface down and notify protocols of
1216  * the transition.
1217  * NOTE: must be called at splsoftnet or equivalent.
1218  */
1219 void
1220 if_down(ifp)
1221 	struct ifnet *ifp;
1222 {
1223 	struct ifaddr *ifa;
1224 
1225 	ifp->if_flags &= ~IFF_UP;
1226 	microtime(&ifp->if_lastchange);
1227 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1228 	     ifa = TAILQ_NEXT(ifa, ifa_list))
1229 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1230 	IFQ_PURGE(&ifp->if_snd);
1231 	rt_ifmsg(ifp);
1232 }
1233 
1234 /*
1235  * Mark an interface up and notify protocols of
1236  * the transition.
1237  * NOTE: must be called at splsoftnet or equivalent.
1238  */
1239 void
1240 if_up(ifp)
1241 	struct ifnet *ifp;
1242 {
1243 #ifdef notyet
1244 	struct ifaddr *ifa;
1245 #endif
1246 
1247 	ifp->if_flags |= IFF_UP;
1248 	microtime(&ifp->if_lastchange);
1249 #ifdef notyet
1250 	/* this has no effect on IP, and will kill all ISO connections XXX */
1251 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1252 	     ifa = TAILQ_NEXT(ifa, ifa_list))
1253 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
1254 #endif
1255 	rt_ifmsg(ifp);
1256 #ifdef INET6
1257 	in6_if_up(ifp);
1258 #endif
1259 }
1260 
1261 /*
1262  * Handle interface watchdog timer routines.  Called
1263  * from softclock, we decrement timers (if set) and
1264  * call the appropriate interface routine on expiration.
1265  */
1266 void
1267 if_slowtimo(arg)
1268 	void *arg;
1269 {
1270 	struct ifnet *ifp;
1271 	int s = splnet();
1272 
1273 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1274 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1275 		if (ifp->if_timer == 0 || --ifp->if_timer)
1276 			continue;
1277 		if (ifp->if_watchdog)
1278 			(*ifp->if_watchdog)(ifp);
1279 	}
1280 	splx(s);
1281 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
1282 	    if_slowtimo, NULL);
1283 }
1284 
1285 /*
1286  * Set/clear promiscuous mode on interface ifp based on the truth value
1287  * of pswitch.  The calls are reference counted so that only the first
1288  * "on" request actually has an effect, as does the final "off" request.
1289  * Results are undefined if the "off" and "on" requests are not matched.
1290  */
1291 int
1292 ifpromisc(ifp, pswitch)
1293 	struct ifnet *ifp;
1294 	int pswitch;
1295 {
1296 	int pcount, ret;
1297 	short flags;
1298 	struct ifreq ifr;
1299 
1300 	pcount = ifp->if_pcount;
1301 	flags = ifp->if_flags;
1302 	if (pswitch) {
1303 		/*
1304 		 * Allow the device to be "placed" into promiscuous
1305 		 * mode even if it is not configured up.  It will
1306 		 * consult IFF_PROMISC when it is is brought up.
1307 		 */
1308 		if (ifp->if_pcount++ != 0)
1309 			return (0);
1310 		ifp->if_flags |= IFF_PROMISC;
1311 		if ((ifp->if_flags & IFF_UP) == 0)
1312 			return (0);
1313 	} else {
1314 		if (--ifp->if_pcount > 0)
1315 			return (0);
1316 		ifp->if_flags &= ~IFF_PROMISC;
1317 		/*
1318 		 * If the device is not configured up, we should not need to
1319 		 * turn off promiscuous mode (device should have turned it
1320 		 * off when interface went down; and will look at IFF_PROMISC
1321 		 * again next time interface comes up).
1322 		 */
1323 		if ((ifp->if_flags & IFF_UP) == 0)
1324 			return (0);
1325 	}
1326 	memset(&ifr, 0, sizeof(ifr));
1327 	ifr.ifr_flags = ifp->if_flags;
1328 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
1329 	/* Restore interface state if not successful. */
1330 	if (ret != 0) {
1331 		ifp->if_pcount = pcount;
1332 		ifp->if_flags = flags;
1333 	}
1334 	return (ret);
1335 }
1336 
1337 /*
1338  * Map interface name to
1339  * interface structure pointer.
1340  */
1341 struct ifnet *
1342 ifunit(name)
1343 	const char *name;
1344 {
1345 	struct ifnet *ifp;
1346 	const char *cp = name;
1347 	u_int unit = 0;
1348 	u_int i;
1349 
1350 	/*
1351 	 * If the entire name is a number, treat it as an ifindex.
1352 	 */
1353 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1354 		unit = unit * 10 + (*cp - '0');
1355 	}
1356 
1357 	/*
1358 	 * If the number took all of the name, then it's a valid ifindex.
1359 	 */
1360 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1361 		if (unit >= if_indexlim)
1362 			return (NULL);
1363 		ifp = ifindex2ifnet[unit];
1364 		if (ifp == NULL || ifp->if_output == if_nulloutput)
1365 			return (NULL);
1366 		return (ifp);
1367 	}
1368 
1369 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1370 	     ifp = TAILQ_NEXT(ifp, if_list)) {
1371 		if (ifp->if_output == if_nulloutput)
1372 			continue;
1373 	 	if (strcmp(ifp->if_xname, name) == 0)
1374 			return (ifp);
1375 	}
1376 	return (NULL);
1377 }
1378 
1379 /*
1380  * Interface ioctls.
1381  */
1382 int
1383 ifioctl(so, cmd, data, p)
1384 	struct socket *so;
1385 	u_long cmd;
1386 	caddr_t data;
1387 	struct proc *p;
1388 {
1389 	struct ifnet *ifp;
1390 	struct ifreq *ifr;
1391 	struct ifcapreq *ifcr;
1392 	struct ifdatareq *ifdr;
1393 	int s, error = 0;
1394 	short oif_flags;
1395 
1396 	switch (cmd) {
1397 
1398 	case SIOCGIFCONF:
1399 	case OSIOCGIFCONF:
1400 		return (ifconf(cmd, data));
1401 	}
1402 	ifr = (struct ifreq *)data;
1403 	ifcr = (struct ifcapreq *)data;
1404 	ifdr = (struct ifdatareq *)data;
1405 
1406 	switch (cmd) {
1407 	case SIOCIFCREATE:
1408 	case SIOCIFDESTROY:
1409 		if (p) {
1410 			error = suser(p->p_ucred, &p->p_acflag);
1411 			if (error)
1412 				return error;
1413 		}
1414 		return ((cmd == SIOCIFCREATE) ?
1415 			if_clone_create(ifr->ifr_name) :
1416 			if_clone_destroy(ifr->ifr_name));
1417 
1418 	case SIOCIFGCLONERS:
1419 		return (if_clone_list((struct if_clonereq *)data));
1420 	}
1421 
1422 	ifp = ifunit(ifr->ifr_name);
1423 	if (ifp == 0)
1424 		return (ENXIO);
1425 
1426 	switch (cmd) {
1427 	case SIOCSIFFLAGS:
1428 	case SIOCSIFCAP:
1429 	case SIOCSIFMETRIC:
1430 	case SIOCZIFDATA:
1431 	case SIOCSIFMTU:
1432 	case SIOCSIFPHYADDR:
1433 	case SIOCDIFPHYADDR:
1434 #ifdef INET6
1435 	case SIOCSIFPHYADDR_IN6:
1436 #endif
1437 	case SIOCSLIFPHYADDR:
1438 	case SIOCADDMULTI:
1439 	case SIOCDELMULTI:
1440 	case SIOCSIFMEDIA:
1441 	case SIOCSDRVSPEC:
1442 	case SIOCS80211NWID:
1443 	case SIOCS80211NWKEY:
1444 	case SIOCS80211POWER:
1445 	case SIOCS80211BSSID:
1446 	case SIOCS80211CHANNEL:
1447 		if (p) {
1448 			error = suser(p->p_ucred, &p->p_acflag);
1449 			if (error)
1450 				return error;
1451 		}
1452 	}
1453 
1454 	oif_flags = ifp->if_flags;
1455 	switch (cmd) {
1456 
1457 	case SIOCGIFFLAGS:
1458 		ifr->ifr_flags = ifp->if_flags;
1459 		break;
1460 
1461 	case SIOCGIFMETRIC:
1462 		ifr->ifr_metric = ifp->if_metric;
1463 		break;
1464 
1465 	case SIOCGIFMTU:
1466 		ifr->ifr_mtu = ifp->if_mtu;
1467 		break;
1468 
1469 	case SIOCGIFDLT:
1470 		ifr->ifr_dlt = ifp->if_dlt;
1471 		break;
1472 
1473 	case SIOCSIFFLAGS:
1474 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1475 			s = splnet();
1476 			if_down(ifp);
1477 			splx(s);
1478 		}
1479 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1480 			s = splnet();
1481 			if_up(ifp);
1482 			splx(s);
1483 		}
1484 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1485 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
1486 		if (ifp->if_ioctl)
1487 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
1488 		break;
1489 
1490 	case SIOCGIFCAP:
1491 		ifcr->ifcr_capabilities = ifp->if_capabilities;
1492 		ifcr->ifcr_capenable = ifp->if_capenable;
1493 		break;
1494 
1495 	case SIOCSIFCAP:
1496 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1497 			return (EINVAL);
1498 		if (ifp->if_ioctl == NULL)
1499 			return (EOPNOTSUPP);
1500 
1501 		/* Must prevent race with packet reception here. */
1502 		s = splnet();
1503 		if (ifcr->ifcr_capenable != ifp->if_capenable) {
1504 			struct ifreq ifrq;
1505 
1506 			ifrq.ifr_flags = ifp->if_flags;
1507 			ifp->if_capenable = ifcr->ifcr_capenable;
1508 
1509 			/* Pre-compute the checksum flags mask. */
1510 			ifp->if_csum_flags_tx = 0;
1511 			ifp->if_csum_flags_rx = 0;
1512 			if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1513 				ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1514 			}
1515 			if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1516 				ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1517 			}
1518 
1519 			if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1520 				ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1521 			}
1522 			if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1523 				ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1524 			}
1525 
1526 			if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1527 				ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1528 			}
1529 			if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1530 				ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1531 			}
1532 
1533 			if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1534 				ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1535 			}
1536 			if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1537 				ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1538 			}
1539 
1540 			if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1541 				ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1542 			}
1543 			if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1544 				ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1545 			}
1546 
1547 			/*
1548 			 * Only kick the interface if it's up.  If it's
1549 			 * not up now, it will notice the cap enables
1550 			 * when it is brought up later.
1551 			 */
1552 			if (ifp->if_flags & IFF_UP)
1553 				(void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1554 				    (caddr_t) &ifrq);
1555 		}
1556 		splx(s);
1557 		break;
1558 
1559 	case SIOCSIFMETRIC:
1560 		ifp->if_metric = ifr->ifr_metric;
1561 		break;
1562 
1563 	case SIOCGIFDATA:
1564 		ifdr->ifdr_data = ifp->if_data;
1565 		break;
1566 
1567 	case SIOCZIFDATA:
1568 		ifdr->ifdr_data = ifp->if_data;
1569 		/*
1570 		 * Assumes that the volatile counters that can be
1571 		 * zero'ed are at the end of if_data.
1572 		 */
1573 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1574 		    offsetof(struct if_data, ifi_ipackets));
1575 		break;
1576 
1577 	case SIOCSIFMTU:
1578 	{
1579 		u_long oldmtu = ifp->if_mtu;
1580 
1581 		if (ifp->if_ioctl == NULL)
1582 			return (EOPNOTSUPP);
1583 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1584 
1585 		/*
1586 		 * If the link MTU changed, do network layer specific procedure.
1587 		 */
1588 		if (ifp->if_mtu != oldmtu) {
1589 #ifdef INET6
1590 			nd6_setmtu(ifp);
1591 #endif
1592 		}
1593 		break;
1594 	}
1595 	case SIOCSIFPHYADDR:
1596 	case SIOCDIFPHYADDR:
1597 #ifdef INET6
1598 	case SIOCSIFPHYADDR_IN6:
1599 #endif
1600 	case SIOCSLIFPHYADDR:
1601 	case SIOCADDMULTI:
1602 	case SIOCDELMULTI:
1603 	case SIOCSIFMEDIA:
1604 	case SIOCGIFPSRCADDR:
1605 	case SIOCGIFPDSTADDR:
1606 	case SIOCGLIFPHYADDR:
1607 	case SIOCGIFMEDIA:
1608 		if (ifp->if_ioctl == 0)
1609 			return (EOPNOTSUPP);
1610 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1611 		break;
1612 
1613 	case SIOCSDRVSPEC:
1614 	case SIOCS80211NWID:
1615 	case SIOCS80211NWKEY:
1616 	case SIOCS80211POWER:
1617 	case SIOCS80211BSSID:
1618 	case SIOCS80211CHANNEL:
1619 	default:
1620 		if (so->so_proto == 0)
1621 			return (EOPNOTSUPP);
1622 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(COMPAT_ULTRIX) && !defined(LKM)
1623 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1624 		    (struct mbuf *)cmd, (struct mbuf *)data,
1625 		    (struct mbuf *)ifp, p));
1626 #else
1627 	    {
1628 		int ocmd = cmd;
1629 
1630 		switch (cmd) {
1631 
1632 		case SIOCSIFADDR:
1633 		case SIOCSIFDSTADDR:
1634 		case SIOCSIFBRDADDR:
1635 		case SIOCSIFNETMASK:
1636 #if BYTE_ORDER != BIG_ENDIAN
1637 			if (ifr->ifr_addr.sa_family == 0 &&
1638 			    ifr->ifr_addr.sa_len < 16) {
1639 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1640 				ifr->ifr_addr.sa_len = 16;
1641 			}
1642 #else
1643 			if (ifr->ifr_addr.sa_len == 0)
1644 				ifr->ifr_addr.sa_len = 16;
1645 #endif
1646 			break;
1647 
1648 		case OSIOCGIFADDR:
1649 			cmd = SIOCGIFADDR;
1650 			break;
1651 
1652 		case OSIOCGIFDSTADDR:
1653 			cmd = SIOCGIFDSTADDR;
1654 			break;
1655 
1656 		case OSIOCGIFBRDADDR:
1657 			cmd = SIOCGIFBRDADDR;
1658 			break;
1659 
1660 		case OSIOCGIFNETMASK:
1661 			cmd = SIOCGIFNETMASK;
1662 		}
1663 
1664 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1665 		    (struct mbuf *)cmd, (struct mbuf *)data,
1666 		    (struct mbuf *)ifp, p));
1667 
1668 		switch (ocmd) {
1669 		case OSIOCGIFADDR:
1670 		case OSIOCGIFDSTADDR:
1671 		case OSIOCGIFBRDADDR:
1672 		case OSIOCGIFNETMASK:
1673 			*(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1674 		}
1675 	    }
1676 #endif /* COMPAT_43 */
1677 		break;
1678 	}
1679 
1680 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1681 #ifdef INET6
1682 		if ((ifp->if_flags & IFF_UP) != 0) {
1683 			s = splnet();
1684 			in6_if_up(ifp);
1685 			splx(s);
1686 		}
1687 #endif
1688 	}
1689 
1690 	return (error);
1691 }
1692 
1693 /*
1694  * Return interface configuration
1695  * of system.  List may be used
1696  * in later ioctl's (above) to get
1697  * other information.
1698  */
1699 /*ARGSUSED*/
1700 int
1701 ifconf(cmd, data)
1702 	u_long cmd;
1703 	caddr_t data;
1704 {
1705 	struct ifconf *ifc = (struct ifconf *)data;
1706 	struct ifnet *ifp;
1707 	struct ifaddr *ifa;
1708 	struct ifreq ifr, *ifrp;
1709 	int space = ifc->ifc_len, error = 0;
1710 	const int sz = (int)sizeof(ifr);
1711 	int sign;
1712 
1713 	if ((ifrp = ifc->ifc_req) == NULL) {
1714 		space = 0;
1715 		sign = -1;
1716 	} else {
1717 		sign = 1;
1718 	}
1719 	IFNET_FOREACH(ifp) {
1720 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1721 		if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) {
1722 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1723 			if (ifrp != NULL && space >= sz) {
1724 				error = copyout(&ifr, ifrp, sz);
1725 				if (error)
1726 					break;
1727 				ifrp++;
1728 			}
1729 			space -= sizeof(ifr) * sign;
1730 			continue;
1731 		}
1732 
1733 		for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
1734 			struct sockaddr *sa = ifa->ifa_addr;
1735 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) || defined(COMPAT_ULTRIX)
1736 			if (cmd == OSIOCGIFCONF) {
1737 				struct osockaddr *osa =
1738 					 (struct osockaddr *)&ifr.ifr_addr;
1739 				/*
1740 				 * If it does not fit, we don't bother with it
1741 				 */
1742 				if (sa->sa_len > sizeof(*osa))
1743 					continue;
1744 				ifr.ifr_addr = *sa;
1745 				osa->sa_family = sa->sa_family;
1746 				if (ifrp != NULL && space >= sz) {
1747 					error = copyout(&ifr, ifrp, sz);
1748 					ifrp++;
1749 				}
1750 			} else
1751 #endif
1752 			if (sa->sa_len <= sizeof(*sa)) {
1753 				ifr.ifr_addr = *sa;
1754 				if (ifrp != NULL && space >= sz) {
1755 					error = copyout(&ifr, ifrp, sz);
1756 					ifrp++;
1757 				}
1758 			} else {
1759 				space -= (sa->sa_len - sizeof(*sa)) * sign;
1760 				if (ifrp != NULL && space >= sz) {
1761 					error = copyout(&ifr, ifrp,
1762 					    sizeof(ifr.ifr_name));
1763 					if (error == 0) {
1764 						error = copyout(sa,
1765 						    &ifrp->ifr_addr,
1766 						    sa->sa_len);
1767 					}
1768 					ifrp = (struct ifreq *)
1769 						(sa->sa_len +
1770 						 (caddr_t)&ifrp->ifr_addr);
1771 				}
1772 			}
1773 			if (error)
1774 				break;
1775 			space -= sz * sign;
1776 		}
1777 	}
1778 	if (ifrp != NULL)
1779 		ifc->ifc_len -= space;
1780 	else
1781 		ifc->ifc_len = space;
1782 	return (error);
1783 }
1784 
1785 /*
1786  * Queue message on interface, and start output if interface
1787  * not yet active.
1788  */
1789 int
1790 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1791     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1792 {
1793 	int len = m->m_pkthdr.len;
1794 	int mflags = m->m_flags;
1795 	int s = splnet();
1796 	int error;
1797 
1798 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1799 	if (error) {
1800 		splx(s);
1801 		return error;
1802 	}
1803 	ifp->if_obytes += len;
1804 	if (mflags & M_MCAST)
1805 		ifp->if_omcasts++;
1806 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
1807 		(*ifp->if_start)(ifp);
1808 	splx(s);
1809 	return error;
1810 }
1811 
1812 /*
1813  * Queue message on interface, possibly using a second fast queue
1814  */
1815 int
1816 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1817     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1818 {
1819 	int error = 0;
1820 
1821 	if (ifq != NULL
1822 #ifdef ALTQ
1823 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1824 #endif
1825 	    ) {
1826 		if (IF_QFULL(ifq)) {
1827 			IF_DROP(&ifp->if_snd);
1828 			m_freem(m);
1829 			if (error == 0)
1830 				error = ENOBUFS;
1831 		}
1832 		else
1833 			IF_ENQUEUE(ifq, m);
1834 	} else
1835 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1836 	if (error != 0) {
1837 		++ifp->if_oerrors;
1838 		return error;
1839 	}
1840 
1841 	return 0;
1842 }
1843 
1844 
1845 #if defined(INET) || defined(INET6)
1846 static void
1847 sysctl_net_ifq_setup(struct sysctllog **clog,
1848 		     int pf, const char *pfname,
1849 		     int ipn, const char *ipname,
1850 		     int qid, struct ifqueue *ifq)
1851 {
1852 
1853 	sysctl_createv(clog, 0, NULL, NULL,
1854 		       CTLFLAG_PERMANENT,
1855 		       CTLTYPE_NODE, "net", NULL,
1856 		       NULL, 0, NULL, 0,
1857 		       CTL_NET, CTL_EOL);
1858 	sysctl_createv(clog, 0, NULL, NULL,
1859 		       CTLFLAG_PERMANENT,
1860 		       CTLTYPE_NODE, pfname, NULL,
1861 		       NULL, 0, NULL, 0,
1862 		       CTL_NET, pf, CTL_EOL);
1863 	sysctl_createv(clog, 0, NULL, NULL,
1864 		       CTLFLAG_PERMANENT,
1865 		       CTLTYPE_NODE, ipname, NULL,
1866 		       NULL, 0, NULL, 0,
1867 		       CTL_NET, pf, ipn, CTL_EOL);
1868 	sysctl_createv(clog, 0, NULL, NULL,
1869 		       CTLFLAG_PERMANENT,
1870 		       CTLTYPE_NODE, "ifq",
1871 		       SYSCTL_DESCR("Protocol input queue controls"),
1872 		       NULL, 0, NULL, 0,
1873 		       CTL_NET, pf, ipn, qid, CTL_EOL);
1874 
1875 	sysctl_createv(clog, 0, NULL, NULL,
1876 		       CTLFLAG_PERMANENT,
1877 		       CTLTYPE_INT, "len",
1878 		       SYSCTL_DESCR("Current input queue length"),
1879 		       NULL, 0, &ifq->ifq_len, 0,
1880 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1881 	sysctl_createv(clog, 0, NULL, NULL,
1882 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1883 		       CTLTYPE_INT, "maxlen",
1884 		       SYSCTL_DESCR("Maximum allowed input queue length"),
1885 		       NULL, 0, &ifq->ifq_maxlen, 0,
1886 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1887 #ifdef notyet
1888 	sysctl_createv(clog, 0, NULL, NULL,
1889 		       CTLFLAG_PERMANENT,
1890 		       CTLTYPE_INT, "peak",
1891 		       SYSCTL_DESCR("Highest input queue length"),
1892 		       NULL, 0, &ifq->ifq_peak, 0,
1893 		       CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1894 #endif
1895 	sysctl_createv(clog, 0, NULL, NULL,
1896 		       CTLFLAG_PERMANENT,
1897 		       CTLTYPE_INT, "drops",
1898 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
1899 		       NULL, 0, &ifq->ifq_drops, 0,
1900 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1901 }
1902 
1903 #ifdef INET
1904 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1905 	     "sysctl net.inet.ip.ifq subtree setup")
1906 {
1907 	extern struct ifqueue ipintrq;
1908 
1909 	sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1910 			     IPCTL_IFQ, &ipintrq);
1911 }
1912 #endif /* INET */
1913 
1914 #ifdef INET6
1915 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1916 	     "sysctl net.inet6.ip6.ifq subtree setup")
1917 {
1918 	extern struct ifqueue ip6intrq;
1919 
1920 	sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1921 			     IPV6CTL_IFQ, &ip6intrq);
1922 }
1923 #endif /* INET6 */
1924 #endif /* INET || INET6 */
1925