xref: /netbsd-src/sys/net/if.c (revision de4fa6c51a9708fc05f88b618fa6fad87c9508ec)
1 /*	$NetBSD: if.c,v 1.234 2009/08/13 00:23:31 dyoung Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000, 2001, 2008 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the project nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 /*
62  * Copyright (c) 1980, 1986, 1993
63  *	The Regents of the University of California.  All rights reserved.
64  *
65  * Redistribution and use in source and binary forms, with or without
66  * modification, are permitted provided that the following conditions
67  * are met:
68  * 1. Redistributions of source code must retain the above copyright
69  *    notice, this list of conditions and the following disclaimer.
70  * 2. Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in the
72  *    documentation and/or other materials provided with the distribution.
73  * 3. Neither the name of the University nor the names of its contributors
74  *    may be used to endorse or promote products derived from this software
75  *    without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87  * SUCH DAMAGE.
88  *
89  *	@(#)if.c	8.5 (Berkeley) 1/9/95
90  */
91 
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.234 2009/08/13 00:23:31 dyoung Exp $");
94 
95 #include "opt_inet.h"
96 
97 #include "opt_atalk.h"
98 #include "opt_natm.h"
99 #include "opt_pfil_hooks.h"
100 
101 #include <sys/param.h>
102 #include <sys/mbuf.h>
103 #include <sys/systm.h>
104 #include <sys/callout.h>
105 #include <sys/proc.h>
106 #include <sys/socket.h>
107 #include <sys/socketvar.h>
108 #include <sys/domain.h>
109 #include <sys/protosw.h>
110 #include <sys/kernel.h>
111 #include <sys/ioctl.h>
112 #include <sys/sysctl.h>
113 #include <sys/syslog.h>
114 #include <sys/kauth.h>
115 
116 #include <net/if.h>
117 #include <net/if_dl.h>
118 #include <net/if_ether.h>
119 #include <net/if_media.h>
120 #include <net80211/ieee80211.h>
121 #include <net80211/ieee80211_ioctl.h>
122 #include <net/if_types.h>
123 #include <net/radix.h>
124 #include <net/route.h>
125 #include <net/netisr.h>
126 #ifdef NETATALK
127 #include <netatalk/at_extern.h>
128 #include <netatalk/at.h>
129 #endif
130 #include <net/pfil.h>
131 
132 #ifdef INET6
133 #include <netinet/in.h>
134 #include <netinet6/in6_var.h>
135 #include <netinet6/nd6.h>
136 #endif
137 
138 #include "carp.h"
139 #if NCARP > 0
140 #include <netinet/ip_carp.h>
141 #endif
142 
143 #include <compat/sys/sockio.h>
144 #include <compat/sys/socket.h>
145 
146 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
147 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
148 
149 int	ifqmaxlen = IFQ_MAXLEN;
150 callout_t if_slowtimo_ch;
151 
152 int netisr;			/* scheduling bits for network */
153 
154 static int	if_rt_walktree(struct rtentry *, void *);
155 
156 static struct if_clone *if_clone_lookup(const char *, int *);
157 static int	if_clone_list(struct if_clonereq *);
158 
159 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
160 static int if_cloners_count;
161 
162 static uint64_t index_gen;
163 static kmutex_t index_gen_mtx;
164 
165 #ifdef PFIL_HOOKS
166 struct pfil_head if_pfil;	/* packet filtering hook for interfaces */
167 #endif
168 
169 static void if_detach_queues(struct ifnet *, struct ifqueue *);
170 static void sysctl_sndq_setup(struct sysctllog **, const char *,
171     struct ifaltq *);
172 
173 /*
174  * Network interface utility routines.
175  *
176  * Routines with ifa_ifwith* names take sockaddr *'s as
177  * parameters.
178  */
179 void
180 ifinit(void)
181 {
182 
183 	mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE);
184 	callout_init(&if_slowtimo_ch, 0);
185 	if_slowtimo(NULL);
186 }
187 
188 /*
189  * XXX Initialization before configure().
190  * XXX hack to get pfil_add_hook working in autoconf.
191  */
192 void
193 ifinit1(void)
194 {
195 
196 #ifdef PFIL_HOOKS
197 	if_pfil.ph_type = PFIL_TYPE_IFNET;
198 	if_pfil.ph_ifnet = NULL;
199 	if (pfil_head_register(&if_pfil) != 0)
200 		printf("WARNING: unable to register pfil hook\n");
201 #endif
202 }
203 
204 struct ifnet *
205 if_alloc(u_char type)
206 {
207 	return malloc(sizeof(struct ifnet), M_DEVBUF, M_WAITOK|M_ZERO);
208 }
209 
210 void
211 if_initname(struct ifnet *ifp, const char *name, int unit)
212 {
213 	(void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
214 	    "%s%d", name, unit);
215 }
216 
217 /*
218  * Null routines used while an interface is going away.  These routines
219  * just return an error.
220  */
221 
222 int
223 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
224     const struct sockaddr *so, struct rtentry *rt)
225 {
226 
227 	return ENXIO;
228 }
229 
230 void
231 if_nullinput(struct ifnet *ifp, struct mbuf *m)
232 {
233 
234 	/* Nothing. */
235 }
236 
237 void
238 if_nullstart(struct ifnet *ifp)
239 {
240 
241 	/* Nothing. */
242 }
243 
244 int
245 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
246 {
247 
248 	return ENXIO;
249 }
250 
251 int
252 if_nullinit(struct ifnet *ifp)
253 {
254 
255 	return ENXIO;
256 }
257 
258 void
259 if_nullstop(struct ifnet *ifp, int disable)
260 {
261 
262 	/* Nothing. */
263 }
264 
265 void
266 if_nullwatchdog(struct ifnet *ifp)
267 {
268 
269 	/* Nothing. */
270 }
271 
272 void
273 if_nulldrain(struct ifnet *ifp)
274 {
275 
276 	/* Nothing. */
277 }
278 
279 static u_int if_index = 1;
280 struct ifnet_head ifnet;
281 size_t if_indexlim = 0;
282 struct ifaddr **ifnet_addrs = NULL;
283 struct ifnet **ifindex2ifnet = NULL;
284 struct ifnet *lo0ifp;
285 
286 void
287 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
288 {
289 	struct ifaddr *ifa;
290 	struct sockaddr_dl *sdl;
291 
292 	ifp->if_addrlen = addrlen;
293 	if_alloc_sadl(ifp);
294 	ifa = ifp->if_dl;
295 	sdl = satosdl(ifa->ifa_addr);
296 
297 	(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
298 	if (factory) {
299 		ifp->if_hwdl = ifp->if_dl;
300 		IFAREF(ifp->if_hwdl);
301 	}
302 	/* TBD routing socket */
303 }
304 
305 struct ifaddr *
306 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
307 {
308 	unsigned socksize, ifasize;
309 	int addrlen, namelen;
310 	struct sockaddr_dl *mask, *sdl;
311 	struct ifaddr *ifa;
312 
313 	namelen = strlen(ifp->if_xname);
314 	addrlen = ifp->if_addrlen;
315 	socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
316 	ifasize = sizeof(*ifa) + 2 * socksize;
317 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
318 
319 	sdl = (struct sockaddr_dl *)(ifa + 1);
320 	mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
321 
322 	sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
323 	    ifp->if_xname, namelen, NULL, addrlen);
324 	mask->sdl_len = sockaddr_dl_measure(namelen, 0);
325 	memset(&mask->sdl_data[0], 0xff, namelen);
326 	ifa->ifa_rtrequest = link_rtrequest;
327 	ifa->ifa_addr = (struct sockaddr *)sdl;
328 	ifa->ifa_netmask = (struct sockaddr *)mask;
329 
330 	*sdlp = sdl;
331 
332 	return ifa;
333 }
334 
335 static void
336 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
337 {
338 	const struct sockaddr_dl *sdl;
339 	ifnet_addrs[ifp->if_index] = ifa;
340 	IFAREF(ifa);
341 	ifp->if_dl = ifa;
342 	IFAREF(ifa);
343 	sdl = satosdl(ifa->ifa_addr);
344 	ifp->if_sadl = sdl;
345 }
346 
347 /*
348  * Allocate the link level name for the specified interface.  This
349  * is an attachment helper.  It must be called after ifp->if_addrlen
350  * is initialized, which may not be the case when if_attach() is
351  * called.
352  */
353 void
354 if_alloc_sadl(struct ifnet *ifp)
355 {
356 	struct ifaddr *ifa;
357 	const struct sockaddr_dl *sdl;
358 
359 	/*
360 	 * If the interface already has a link name, release it
361 	 * now.  This is useful for interfaces that can change
362 	 * link types, and thus switch link names often.
363 	 */
364 	if (ifp->if_sadl != NULL)
365 		if_free_sadl(ifp);
366 
367 	ifa = if_dl_create(ifp, &sdl);
368 
369 	ifa_insert(ifp, ifa);
370 	if_sadl_setrefs(ifp, ifa);
371 }
372 
373 static void
374 if_deactivate_sadl(struct ifnet *ifp)
375 {
376 	struct ifaddr *ifa;
377 
378 	KASSERT(ifp->if_dl != NULL);
379 
380 	ifa = ifp->if_dl;
381 
382 	ifp->if_sadl = NULL;
383 
384 	ifnet_addrs[ifp->if_index] = NULL;
385 	IFAFREE(ifa);
386 	ifp->if_dl = NULL;
387 	IFAFREE(ifa);
388 }
389 
390 void
391 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa,
392     const struct sockaddr_dl *sdl)
393 {
394 	int s;
395 
396 	s = splnet();
397 
398 	if_deactivate_sadl(ifp);
399 
400 	if_sadl_setrefs(ifp, ifa);
401 	IFADDR_FOREACH(ifa, ifp)
402 		rtinit(ifa, RTM_LLINFO_UPD, 0);
403 	splx(s);
404 }
405 
406 /*
407  * Free the link level name for the specified interface.  This is
408  * a detach helper.  This is called from if_detach() or from
409  * link layer type specific detach functions.
410  */
411 void
412 if_free_sadl(struct ifnet *ifp)
413 {
414 	struct ifaddr *ifa;
415 	int s;
416 
417 	ifa = ifnet_addrs[ifp->if_index];
418 	if (ifa == NULL) {
419 		KASSERT(ifp->if_sadl == NULL);
420 		KASSERT(ifp->if_dl == NULL);
421 		return;
422 	}
423 
424 	KASSERT(ifp->if_sadl != NULL);
425 	KASSERT(ifp->if_dl != NULL);
426 
427 	s = splnet();
428 	rtinit(ifa, RTM_DELETE, 0);
429 	ifa_remove(ifp, ifa);
430 	if_deactivate_sadl(ifp);
431 	if (ifp->if_hwdl == ifa) {
432 		IFAFREE(ifa);
433 		ifp->if_hwdl = NULL;
434 	}
435 	splx(s);
436 }
437 
438 /*
439  * Attach an interface to the
440  * list of "active" interfaces.
441  */
442 void
443 if_attach(struct ifnet *ifp)
444 {
445 	int indexlim = 0;
446 
447 	if (if_indexlim == 0) {
448 		TAILQ_INIT(&ifnet);
449 		if_indexlim = 8;
450 	}
451 	TAILQ_INIT(&ifp->if_addrlist);
452 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
453 	if (ifp->if_ioctl == NULL)
454 		ifp->if_ioctl = ifioctl_common;
455 
456 	mutex_enter(&index_gen_mtx);
457 	ifp->if_index_gen = index_gen++;
458 	mutex_exit(&index_gen_mtx);
459 
460 	ifp->if_index = if_index;
461 	if (ifindex2ifnet == NULL)
462 		if_index++;
463 	else
464 		while (ifp->if_index < if_indexlim &&
465 		    ifindex2ifnet[ifp->if_index] != NULL) {
466 			++if_index;
467 			if (if_index == 0)
468 				if_index = 1;
469 			/*
470 			 * If we hit USHRT_MAX, we skip back to 0 since
471 			 * there are a number of places where the value
472 			 * of if_index or if_index itself is compared
473 			 * to or stored in an unsigned short.  By
474 			 * jumping back, we won't botch those assignments
475 			 * or comparisons.
476 			 */
477 			else if (if_index == USHRT_MAX) {
478 				/*
479 				 * However, if we have to jump back to
480 				 * zero *twice* without finding an empty
481 				 * slot in ifindex2ifnet[], then there
482 				 * there are too many (>65535) interfaces.
483 				 */
484 				if (indexlim++)
485 					panic("too many interfaces");
486 				else
487 					if_index = 1;
488 			}
489 			ifp->if_index = if_index;
490 		}
491 
492 	/*
493 	 * We have some arrays that should be indexed by if_index.
494 	 * since if_index will grow dynamically, they should grow too.
495 	 *	struct ifadd **ifnet_addrs
496 	 *	struct ifnet **ifindex2ifnet
497 	 */
498 	if (ifnet_addrs == NULL || ifindex2ifnet == NULL ||
499 	    ifp->if_index >= if_indexlim) {
500 		size_t m, n, oldlim;
501 		void *q;
502 
503 		oldlim = if_indexlim;
504 		while (ifp->if_index >= if_indexlim)
505 			if_indexlim <<= 1;
506 
507 		/* grow ifnet_addrs */
508 		m = oldlim * sizeof(struct ifaddr *);
509 		n = if_indexlim * sizeof(struct ifaddr *);
510 		q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
511 		if (ifnet_addrs != NULL) {
512 			memcpy(q, ifnet_addrs, m);
513 			free(ifnet_addrs, M_IFADDR);
514 		}
515 		ifnet_addrs = (struct ifaddr **)q;
516 
517 		/* grow ifindex2ifnet */
518 		m = oldlim * sizeof(struct ifnet *);
519 		n = if_indexlim * sizeof(struct ifnet *);
520 		q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
521 		if (ifindex2ifnet != NULL) {
522 			memcpy(q, ifindex2ifnet, m);
523 			free(ifindex2ifnet, M_IFADDR);
524 		}
525 		ifindex2ifnet = (struct ifnet **)q;
526 	}
527 
528 	ifindex2ifnet[ifp->if_index] = ifp;
529 
530 	/*
531 	 * Link level name is allocated later by a separate call to
532 	 * if_alloc_sadl().
533 	 */
534 
535 	if (ifp->if_snd.ifq_maxlen == 0)
536 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
537 
538 	sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
539 
540 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
541 
542 	ifp->if_link_state = LINK_STATE_UNKNOWN;
543 
544 	ifp->if_capenable = 0;
545 	ifp->if_csum_flags_tx = 0;
546 	ifp->if_csum_flags_rx = 0;
547 
548 #ifdef ALTQ
549 	ifp->if_snd.altq_type = 0;
550 	ifp->if_snd.altq_disc = NULL;
551 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
552 	ifp->if_snd.altq_tbr  = NULL;
553 	ifp->if_snd.altq_ifp  = ifp;
554 #endif
555 
556 #ifdef PFIL_HOOKS
557 	ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
558 	ifp->if_pfil.ph_ifnet = ifp;
559 	if (pfil_head_register(&ifp->if_pfil) != 0)
560 		printf("%s: WARNING: unable to register pfil hook\n",
561 		    ifp->if_xname);
562 	(void)pfil_run_hooks(&if_pfil,
563 	    (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
564 #endif
565 
566 	if (!STAILQ_EMPTY(&domains))
567 		if_attachdomain1(ifp);
568 
569 	/* Announce the interface. */
570 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
571 }
572 
573 void
574 if_attachdomain(void)
575 {
576 	struct ifnet *ifp;
577 	int s;
578 
579 	s = splnet();
580 	IFNET_FOREACH(ifp)
581 		if_attachdomain1(ifp);
582 	splx(s);
583 }
584 
585 void
586 if_attachdomain1(struct ifnet *ifp)
587 {
588 	struct domain *dp;
589 	int s;
590 
591 	s = splnet();
592 
593 	/* address family dependent data region */
594 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
595 	DOMAIN_FOREACH(dp) {
596 		if (dp->dom_ifattach != NULL)
597 			ifp->if_afdata[dp->dom_family] =
598 			    (*dp->dom_ifattach)(ifp);
599 	}
600 
601 	splx(s);
602 }
603 
604 /*
605  * Deactivate an interface.  This points all of the procedure
606  * handles at error stubs.  May be called from interrupt context.
607  */
608 void
609 if_deactivate(struct ifnet *ifp)
610 {
611 	int s;
612 
613 	s = splnet();
614 
615 	ifp->if_output	 = if_nulloutput;
616 	ifp->if_input	 = if_nullinput;
617 	ifp->if_start	 = if_nullstart;
618 	ifp->if_ioctl	 = if_nullioctl;
619 	ifp->if_init	 = if_nullinit;
620 	ifp->if_stop	 = if_nullstop;
621 	ifp->if_watchdog = if_nullwatchdog;
622 	ifp->if_drain	 = if_nulldrain;
623 
624 	/* No more packets may be enqueued. */
625 	ifp->if_snd.ifq_maxlen = 0;
626 
627 	splx(s);
628 }
629 
630 void
631 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
632 {
633 	struct ifaddr *ifa, *nifa;
634 
635 	for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) {
636 		nifa = IFADDR_NEXT(ifa);
637 		if (ifa->ifa_addr->sa_family != family)
638 			continue;
639 		(*purgeaddr)(ifa);
640 	}
641 }
642 
643 /*
644  * Detach an interface from the list of "active" interfaces,
645  * freeing any resources as we go along.
646  *
647  * NOTE: This routine must be called with a valid thread context,
648  * as it may block.
649  */
650 void
651 if_detach(struct ifnet *ifp)
652 {
653 	struct socket so;
654 	struct ifaddr *ifa;
655 #ifdef IFAREF_DEBUG
656 	struct ifaddr *last_ifa = NULL;
657 #endif
658 	struct domain *dp;
659 	const struct protosw *pr;
660 	int s, i, family, purged;
661 
662 	/*
663 	 * XXX It's kind of lame that we have to have the
664 	 * XXX socket structure...
665 	 */
666 	memset(&so, 0, sizeof(so));
667 
668 	s = splnet();
669 
670 	/*
671 	 * Do an if_down() to give protocols a chance to do something.
672 	 */
673 	if_down(ifp);
674 
675 #ifdef ALTQ
676 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
677 		altq_disable(&ifp->if_snd);
678 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
679 		altq_detach(&ifp->if_snd);
680 #endif
681 
682 	sysctl_teardown(&ifp->if_sysctl_log);
683 
684 #if NCARP > 0
685 	/* Remove the interface from any carp group it is a part of.  */
686 	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
687 		carp_ifdetach(ifp);
688 #endif
689 
690 	/*
691 	 * Rip all the addresses off the interface.  This should make
692 	 * all of the routes go away.
693 	 *
694 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
695 	 * from the list, including our "cursor", ifa.  For safety,
696 	 * and to honor the TAILQ abstraction, I just restart the
697 	 * loop after each removal.  Note that the loop will exit
698 	 * when all of the remaining ifaddrs belong to the AF_LINK
699 	 * family.  I am counting on the historical fact that at
700 	 * least one pr_usrreq in each address domain removes at
701 	 * least one ifaddr.
702 	 */
703 again:
704 	IFADDR_FOREACH(ifa, ifp) {
705 		family = ifa->ifa_addr->sa_family;
706 #ifdef IFAREF_DEBUG
707 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
708 		    ifa, family, ifa->ifa_refcnt);
709 		if (last_ifa != NULL && ifa == last_ifa)
710 			panic("if_detach: loop detected");
711 		last_ifa = ifa;
712 #endif
713 		if (family == AF_LINK)
714 			continue;
715 		dp = pffinddomain(family);
716 #ifdef DIAGNOSTIC
717 		if (dp == NULL)
718 			panic("if_detach: no domain for AF %d",
719 			    family);
720 #endif
721 		/*
722 		 * XXX These PURGEIF calls are redundant with the
723 		 * purge-all-families calls below, but are left in for
724 		 * now both to make a smaller change, and to avoid
725 		 * unplanned interactions with clearing of
726 		 * ifp->if_addrlist.
727 		 */
728 		purged = 0;
729 		for (pr = dp->dom_protosw;
730 		     pr < dp->dom_protoswNPROTOSW; pr++) {
731 			so.so_proto = pr;
732 			if (pr->pr_usrreq != NULL) {
733 				(void) (*pr->pr_usrreq)(&so,
734 				    PRU_PURGEIF, NULL, NULL,
735 				    (struct mbuf *) ifp, curlwp);
736 				purged = 1;
737 			}
738 		}
739 		if (purged == 0) {
740 			/*
741 			 * XXX What's really the best thing to do
742 			 * XXX here?  --thorpej@NetBSD.org
743 			 */
744 			printf("if_detach: WARNING: AF %d not purged\n",
745 			    family);
746 			ifa_remove(ifp, ifa);
747 		}
748 		goto again;
749 	}
750 
751 	if_free_sadl(ifp);
752 
753 	/* Walk the routing table looking for stragglers. */
754 	for (i = 0; i <= AF_MAX; i++)
755 		(void)rt_walktree(i, if_rt_walktree, ifp);
756 
757 	DOMAIN_FOREACH(dp) {
758 		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
759 			(*dp->dom_ifdetach)(ifp,
760 			    ifp->if_afdata[dp->dom_family]);
761 
762 		/*
763 		 * One would expect multicast memberships (INET and
764 		 * INET6) on UDP sockets to be purged by the PURGEIF
765 		 * calls above, but if all addresses were removed from
766 		 * the interface prior to destruction, the calls will
767 		 * not be made (e.g. ppp, for which pppd(8) generally
768 		 * removes addresses before destroying the interface).
769 		 * Because there is no invariant that multicast
770 		 * memberships only exist for interfaces with IPv4
771 		 * addresses, we must call PURGEIF regardless of
772 		 * addresses.  (Protocols which might store ifnet
773 		 * pointers are marked with PR_PURGEIF.)
774 		 */
775 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
776 			so.so_proto = pr;
777 			if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF)
778 				(void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL,
779 				    NULL, (struct mbuf *)ifp, curlwp);
780 		}
781 	}
782 
783 #ifdef PFIL_HOOKS
784 	(void)pfil_run_hooks(&if_pfil,
785 	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
786 	(void)pfil_head_unregister(&ifp->if_pfil);
787 #endif
788 
789 	/* Announce that the interface is gone. */
790 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
791 
792 	ifindex2ifnet[ifp->if_index] = NULL;
793 
794 	TAILQ_REMOVE(&ifnet, ifp, if_list);
795 
796 	/*
797 	 * remove packets that came from ifp, from software interrupt queues.
798 	 */
799 	DOMAIN_FOREACH(dp) {
800 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
801 			if (dp->dom_ifqueues[i] == NULL)
802 				break;
803 			if_detach_queues(ifp, dp->dom_ifqueues[i]);
804 		}
805 	}
806 
807 	splx(s);
808 }
809 
810 static void
811 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
812 {
813 	struct mbuf *m, *prev, *next;
814 
815 	prev = NULL;
816 	for (m = q->ifq_head; m != NULL; m = next) {
817 		next = m->m_nextpkt;
818 #ifdef DIAGNOSTIC
819 		if ((m->m_flags & M_PKTHDR) == 0) {
820 			prev = m;
821 			continue;
822 		}
823 #endif
824 		if (m->m_pkthdr.rcvif != ifp) {
825 			prev = m;
826 			continue;
827 		}
828 
829 		if (prev != NULL)
830 			prev->m_nextpkt = m->m_nextpkt;
831 		else
832 			q->ifq_head = m->m_nextpkt;
833 		if (q->ifq_tail == m)
834 			q->ifq_tail = prev;
835 		q->ifq_len--;
836 
837 		m->m_nextpkt = NULL;
838 		m_freem(m);
839 		IF_DROP(q);
840 	}
841 }
842 
843 /*
844  * Callback for a radix tree walk to delete all references to an
845  * ifnet.
846  */
847 static int
848 if_rt_walktree(struct rtentry *rt, void *v)
849 {
850 	struct ifnet *ifp = (struct ifnet *)v;
851 	int error;
852 
853 	if (rt->rt_ifp != ifp)
854 		return 0;
855 
856 	/* Delete the entry. */
857 	++rt->rt_refcnt;
858 	error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
859 	    rt_mask(rt), rt->rt_flags, NULL);
860 	KASSERT((rt->rt_flags & RTF_UP) == 0);
861 	rt->rt_ifp = NULL;
862 	RTFREE(rt);
863 	if (error != 0)
864 		printf("%s: warning: unable to delete rtentry @ %p, "
865 		    "error = %d\n", ifp->if_xname, rt, error);
866 	return 0;
867 }
868 
869 /*
870  * Create a clone network interface.
871  */
872 int
873 if_clone_create(const char *name)
874 {
875 	struct if_clone *ifc;
876 	int unit;
877 
878 	ifc = if_clone_lookup(name, &unit);
879 	if (ifc == NULL)
880 		return EINVAL;
881 
882 	if (ifunit(name) != NULL)
883 		return EEXIST;
884 
885 	return (*ifc->ifc_create)(ifc, unit);
886 }
887 
888 /*
889  * Destroy a clone network interface.
890  */
891 int
892 if_clone_destroy(const char *name)
893 {
894 	struct if_clone *ifc;
895 	struct ifnet *ifp;
896 
897 	ifc = if_clone_lookup(name, NULL);
898 	if (ifc == NULL)
899 		return EINVAL;
900 
901 	ifp = ifunit(name);
902 	if (ifp == NULL)
903 		return ENXIO;
904 
905 	if (ifc->ifc_destroy == NULL)
906 		return EOPNOTSUPP;
907 
908 	return (*ifc->ifc_destroy)(ifp);
909 }
910 
911 /*
912  * Look up a network interface cloner.
913  */
914 static struct if_clone *
915 if_clone_lookup(const char *name, int *unitp)
916 {
917 	struct if_clone *ifc;
918 	const char *cp;
919 	int unit;
920 
921 	/* separate interface name from unit */
922 	for (cp = name;
923 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
924 	    cp++)
925 		continue;
926 
927 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
928 		return NULL;	/* No name or unit number */
929 
930 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
931 		if (strlen(ifc->ifc_name) == cp - name &&
932 		    strncmp(name, ifc->ifc_name, cp - name) == 0)
933 			break;
934 	}
935 
936 	if (ifc == NULL)
937 		return NULL;
938 
939 	unit = 0;
940 	while (cp - name < IFNAMSIZ && *cp) {
941 		if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
942 			/* Bogus unit number. */
943 			return NULL;
944 		}
945 		unit = (unit * 10) + (*cp++ - '0');
946 	}
947 
948 	if (unitp != NULL)
949 		*unitp = unit;
950 	return ifc;
951 }
952 
953 /*
954  * Register a network interface cloner.
955  */
956 void
957 if_clone_attach(struct if_clone *ifc)
958 {
959 
960 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
961 	if_cloners_count++;
962 }
963 
964 /*
965  * Unregister a network interface cloner.
966  */
967 void
968 if_clone_detach(struct if_clone *ifc)
969 {
970 
971 	LIST_REMOVE(ifc, ifc_list);
972 	if_cloners_count--;
973 }
974 
975 /*
976  * Provide list of interface cloners to userspace.
977  */
978 static int
979 if_clone_list(struct if_clonereq *ifcr)
980 {
981 	char outbuf[IFNAMSIZ], *dst;
982 	struct if_clone *ifc;
983 	int count, error = 0;
984 
985 	ifcr->ifcr_total = if_cloners_count;
986 	if ((dst = ifcr->ifcr_buffer) == NULL) {
987 		/* Just asking how many there are. */
988 		return 0;
989 	}
990 
991 	if (ifcr->ifcr_count < 0)
992 		return EINVAL;
993 
994 	count = (if_cloners_count < ifcr->ifcr_count) ?
995 	    if_cloners_count : ifcr->ifcr_count;
996 
997 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
998 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
999 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1000 		if (outbuf[sizeof(outbuf) - 1] != '\0')
1001 			return ENAMETOOLONG;
1002 		error = copyout(outbuf, dst, sizeof(outbuf));
1003 		if (error != 0)
1004 			break;
1005 	}
1006 
1007 	return error;
1008 }
1009 
1010 void
1011 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1012 {
1013 	ifa->ifa_ifp = ifp;
1014 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1015 	IFAREF(ifa);
1016 }
1017 
1018 void
1019 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1020 {
1021 	KASSERT(ifa->ifa_ifp == ifp);
1022 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1023 	IFAFREE(ifa);
1024 }
1025 
1026 static inline int
1027 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1028 {
1029 	return sockaddr_cmp(sa1, sa2) == 0;
1030 }
1031 
1032 /*
1033  * Locate an interface based on a complete address.
1034  */
1035 /*ARGSUSED*/
1036 struct ifaddr *
1037 ifa_ifwithaddr(const struct sockaddr *addr)
1038 {
1039 	struct ifnet *ifp;
1040 	struct ifaddr *ifa;
1041 
1042 	IFNET_FOREACH(ifp) {
1043 		if (ifp->if_output == if_nulloutput)
1044 			continue;
1045 		IFADDR_FOREACH(ifa, ifp) {
1046 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1047 				continue;
1048 			if (equal(addr, ifa->ifa_addr))
1049 				return ifa;
1050 			if ((ifp->if_flags & IFF_BROADCAST) &&
1051 			    ifa->ifa_broadaddr &&
1052 			    /* IP6 doesn't have broadcast */
1053 			    ifa->ifa_broadaddr->sa_len != 0 &&
1054 			    equal(ifa->ifa_broadaddr, addr))
1055 				return ifa;
1056 		}
1057 	}
1058 	return NULL;
1059 }
1060 
1061 /*
1062  * Locate the point to point interface with a given destination address.
1063  */
1064 /*ARGSUSED*/
1065 struct ifaddr *
1066 ifa_ifwithdstaddr(const struct sockaddr *addr)
1067 {
1068 	struct ifnet *ifp;
1069 	struct ifaddr *ifa;
1070 
1071 	IFNET_FOREACH(ifp) {
1072 		if (ifp->if_output == if_nulloutput)
1073 			continue;
1074 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1075 			continue;
1076 		IFADDR_FOREACH(ifa, ifp) {
1077 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
1078 			    ifa->ifa_dstaddr == NULL)
1079 				continue;
1080 			if (equal(addr, ifa->ifa_dstaddr))
1081 				return ifa;
1082 		}
1083 	}
1084 	return NULL;
1085 }
1086 
1087 /*
1088  * Find an interface on a specific network.  If many, choice
1089  * is most specific found.
1090  */
1091 struct ifaddr *
1092 ifa_ifwithnet(const struct sockaddr *addr)
1093 {
1094 	struct ifnet *ifp;
1095 	struct ifaddr *ifa;
1096 	const struct sockaddr_dl *sdl;
1097 	struct ifaddr *ifa_maybe = 0;
1098 	u_int af = addr->sa_family;
1099 	const char *addr_data = addr->sa_data, *cplim;
1100 
1101 	if (af == AF_LINK) {
1102 		sdl = satocsdl(addr);
1103 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1104 		    ifindex2ifnet[sdl->sdl_index] &&
1105 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
1106 			return ifnet_addrs[sdl->sdl_index];
1107 	}
1108 #ifdef NETATALK
1109 	if (af == AF_APPLETALK) {
1110 		const struct sockaddr_at *sat, *sat2;
1111 		sat = (const struct sockaddr_at *)addr;
1112 		IFNET_FOREACH(ifp) {
1113 			if (ifp->if_output == if_nulloutput)
1114 				continue;
1115 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1116 			if (ifa == NULL)
1117 				continue;
1118 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1119 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1120 				return ifa; /* exact match */
1121 			if (ifa_maybe == NULL) {
1122 				/* else keep the if with the right range */
1123 				ifa_maybe = ifa;
1124 			}
1125 		}
1126 		return ifa_maybe;
1127 	}
1128 #endif
1129 	IFNET_FOREACH(ifp) {
1130 		if (ifp->if_output == if_nulloutput)
1131 			continue;
1132 		IFADDR_FOREACH(ifa, ifp) {
1133 			const char *cp, *cp2, *cp3;
1134 
1135 			if (ifa->ifa_addr->sa_family != af ||
1136 			    ifa->ifa_netmask == NULL)
1137  next:				continue;
1138 			cp = addr_data;
1139 			cp2 = ifa->ifa_addr->sa_data;
1140 			cp3 = ifa->ifa_netmask->sa_data;
1141 			cplim = (const char *)ifa->ifa_netmask +
1142 			    ifa->ifa_netmask->sa_len;
1143 			while (cp3 < cplim) {
1144 				if ((*cp++ ^ *cp2++) & *cp3++) {
1145 					/* want to continue for() loop */
1146 					goto next;
1147 				}
1148 			}
1149 			if (ifa_maybe == NULL ||
1150 			    rn_refines((void *)ifa->ifa_netmask,
1151 			    (void *)ifa_maybe->ifa_netmask))
1152 				ifa_maybe = ifa;
1153 		}
1154 	}
1155 	return ifa_maybe;
1156 }
1157 
1158 /*
1159  * Find the interface of the addresss.
1160  */
1161 struct ifaddr *
1162 ifa_ifwithladdr(const struct sockaddr *addr)
1163 {
1164 	struct ifaddr *ia;
1165 
1166 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1167 	    (ia = ifa_ifwithnet(addr)))
1168 		return ia;
1169 	return NULL;
1170 }
1171 
1172 /*
1173  * Find an interface using a specific address family
1174  */
1175 struct ifaddr *
1176 ifa_ifwithaf(int af)
1177 {
1178 	struct ifnet *ifp;
1179 	struct ifaddr *ifa;
1180 
1181 	IFNET_FOREACH(ifp) {
1182 		if (ifp->if_output == if_nulloutput)
1183 			continue;
1184 		IFADDR_FOREACH(ifa, ifp) {
1185 			if (ifa->ifa_addr->sa_family == af)
1186 				return ifa;
1187 		}
1188 	}
1189 	return NULL;
1190 }
1191 
1192 /*
1193  * Find an interface address specific to an interface best matching
1194  * a given address.
1195  */
1196 struct ifaddr *
1197 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1198 {
1199 	struct ifaddr *ifa;
1200 	const char *cp, *cp2, *cp3;
1201 	const char *cplim;
1202 	struct ifaddr *ifa_maybe = 0;
1203 	u_int af = addr->sa_family;
1204 
1205 	if (ifp->if_output == if_nulloutput)
1206 		return NULL;
1207 
1208 	if (af >= AF_MAX)
1209 		return NULL;
1210 
1211 	IFADDR_FOREACH(ifa, ifp) {
1212 		if (ifa->ifa_addr->sa_family != af)
1213 			continue;
1214 		ifa_maybe = ifa;
1215 		if (ifa->ifa_netmask == NULL) {
1216 			if (equal(addr, ifa->ifa_addr) ||
1217 			    (ifa->ifa_dstaddr &&
1218 			     equal(addr, ifa->ifa_dstaddr)))
1219 				return ifa;
1220 			continue;
1221 		}
1222 		cp = addr->sa_data;
1223 		cp2 = ifa->ifa_addr->sa_data;
1224 		cp3 = ifa->ifa_netmask->sa_data;
1225 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1226 		for (; cp3 < cplim; cp3++) {
1227 			if ((*cp++ ^ *cp2++) & *cp3)
1228 				break;
1229 		}
1230 		if (cp3 == cplim)
1231 			return ifa;
1232 	}
1233 	return ifa_maybe;
1234 }
1235 
1236 /*
1237  * Default action when installing a route with a Link Level gateway.
1238  * Lookup an appropriate real ifa to point to.
1239  * This should be moved to /sys/net/link.c eventually.
1240  */
1241 void
1242 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1243 {
1244 	struct ifaddr *ifa;
1245 	const struct sockaddr *dst;
1246 	struct ifnet *ifp;
1247 
1248 	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1249 	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1250 		return;
1251 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1252 		rt_replace_ifa(rt, ifa);
1253 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1254 			ifa->ifa_rtrequest(cmd, rt, info);
1255 	}
1256 }
1257 
1258 /*
1259  * Handle a change in the interface link state.
1260  */
1261 void
1262 if_link_state_change(struct ifnet *ifp, int link_state)
1263 {
1264 	if (ifp->if_link_state == link_state)
1265 		return;
1266 	ifp->if_link_state = link_state;
1267 	/* Notify that the link state has changed. */
1268 	rt_ifmsg(ifp);
1269 #if NCARP > 0
1270 	if (ifp->if_carp)
1271 		carp_carpdev_state(ifp);
1272 #endif
1273 }
1274 
1275 /*
1276  * Mark an interface down and notify protocols of
1277  * the transition.
1278  * NOTE: must be called at splsoftnet or equivalent.
1279  */
1280 void
1281 if_down(struct ifnet *ifp)
1282 {
1283 	struct ifaddr *ifa;
1284 
1285 	ifp->if_flags &= ~IFF_UP;
1286 	nanotime(&ifp->if_lastchange);
1287 	IFADDR_FOREACH(ifa, ifp)
1288 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1289 	IFQ_PURGE(&ifp->if_snd);
1290 #if NCARP > 0
1291 	if (ifp->if_carp)
1292 		carp_carpdev_state(ifp);
1293 #endif
1294 	rt_ifmsg(ifp);
1295 }
1296 
1297 /*
1298  * Mark an interface up and notify protocols of
1299  * the transition.
1300  * NOTE: must be called at splsoftnet or equivalent.
1301  */
1302 void
1303 if_up(struct ifnet *ifp)
1304 {
1305 #ifdef notyet
1306 	struct ifaddr *ifa;
1307 #endif
1308 
1309 	ifp->if_flags |= IFF_UP;
1310 	nanotime(&ifp->if_lastchange);
1311 #ifdef notyet
1312 	/* this has no effect on IP, and will kill all ISO connections XXX */
1313 	IFADDR_FOREACH(ifa, ifp)
1314 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
1315 #endif
1316 #if NCARP > 0
1317 	if (ifp->if_carp)
1318 		carp_carpdev_state(ifp);
1319 #endif
1320 	rt_ifmsg(ifp);
1321 #ifdef INET6
1322 	in6_if_up(ifp);
1323 #endif
1324 }
1325 
1326 /*
1327  * Handle interface watchdog timer routines.  Called
1328  * from softclock, we decrement timers (if set) and
1329  * call the appropriate interface routine on expiration.
1330  */
1331 void
1332 if_slowtimo(void *arg)
1333 {
1334 	struct ifnet *ifp;
1335 	int s = splnet();
1336 
1337 	IFNET_FOREACH(ifp) {
1338 		if (ifp->if_timer == 0 || --ifp->if_timer)
1339 			continue;
1340 		if (ifp->if_watchdog != NULL)
1341 			(*ifp->if_watchdog)(ifp);
1342 	}
1343 	splx(s);
1344 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1345 }
1346 
1347 /*
1348  * Set/clear promiscuous mode on interface ifp based on the truth value
1349  * of pswitch.  The calls are reference counted so that only the first
1350  * "on" request actually has an effect, as does the final "off" request.
1351  * Results are undefined if the "off" and "on" requests are not matched.
1352  */
1353 int
1354 ifpromisc(struct ifnet *ifp, int pswitch)
1355 {
1356 	int pcount, ret;
1357 	short flags;
1358 	struct ifreq ifr;
1359 
1360 	pcount = ifp->if_pcount;
1361 	flags = ifp->if_flags;
1362 	if (pswitch) {
1363 		/*
1364 		 * Allow the device to be "placed" into promiscuous
1365 		 * mode even if it is not configured up.  It will
1366 		 * consult IFF_PROMISC when it is is brought up.
1367 		 */
1368 		if (ifp->if_pcount++ != 0)
1369 			return 0;
1370 		ifp->if_flags |= IFF_PROMISC;
1371 		if ((ifp->if_flags & IFF_UP) == 0)
1372 			return 0;
1373 	} else {
1374 		if (--ifp->if_pcount > 0)
1375 			return 0;
1376 		ifp->if_flags &= ~IFF_PROMISC;
1377 		/*
1378 		 * If the device is not configured up, we should not need to
1379 		 * turn off promiscuous mode (device should have turned it
1380 		 * off when interface went down; and will look at IFF_PROMISC
1381 		 * again next time interface comes up).
1382 		 */
1383 		if ((ifp->if_flags & IFF_UP) == 0)
1384 			return 0;
1385 	}
1386 	memset(&ifr, 0, sizeof(ifr));
1387 	ifr.ifr_flags = ifp->if_flags;
1388 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
1389 	/* Restore interface state if not successful. */
1390 	if (ret != 0) {
1391 		ifp->if_pcount = pcount;
1392 		ifp->if_flags = flags;
1393 	}
1394 	return ret;
1395 }
1396 
1397 /*
1398  * Map interface name to
1399  * interface structure pointer.
1400  */
1401 struct ifnet *
1402 ifunit(const char *name)
1403 {
1404 	struct ifnet *ifp;
1405 	const char *cp = name;
1406 	u_int unit = 0;
1407 	u_int i;
1408 
1409 	/*
1410 	 * If the entire name is a number, treat it as an ifindex.
1411 	 */
1412 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1413 		unit = unit * 10 + (*cp - '0');
1414 	}
1415 
1416 	/*
1417 	 * If the number took all of the name, then it's a valid ifindex.
1418 	 */
1419 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1420 		if (unit >= if_indexlim)
1421 			return NULL;
1422 		ifp = ifindex2ifnet[unit];
1423 		if (ifp == NULL || ifp->if_output == if_nulloutput)
1424 			return NULL;
1425 		return ifp;
1426 	}
1427 
1428 	IFNET_FOREACH(ifp) {
1429 		if (ifp->if_output == if_nulloutput)
1430 			continue;
1431 	 	if (strcmp(ifp->if_xname, name) == 0)
1432 			return ifp;
1433 	}
1434 	return NULL;
1435 }
1436 
1437 /* common */
1438 int
1439 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1440 {
1441 	int s;
1442 	struct ifreq *ifr;
1443 	struct ifcapreq *ifcr;
1444 	struct ifdatareq *ifdr;
1445 
1446 	switch (cmd) {
1447 	case SIOCSIFCAP:
1448 		ifcr = data;
1449 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1450 			return EINVAL;
1451 
1452 		if (ifcr->ifcr_capenable == ifp->if_capenable)
1453 			return 0;
1454 
1455 		ifp->if_capenable = ifcr->ifcr_capenable;
1456 
1457 		/* Pre-compute the checksum flags mask. */
1458 		ifp->if_csum_flags_tx = 0;
1459 		ifp->if_csum_flags_rx = 0;
1460 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1461 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1462 		}
1463 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1464 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1465 		}
1466 
1467 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1468 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1469 		}
1470 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1471 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1472 		}
1473 
1474 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1475 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1476 		}
1477 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1478 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1479 		}
1480 
1481 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1482 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1483 		}
1484 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1485 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1486 		}
1487 
1488 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1489 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1490 		}
1491 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1492 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1493 		}
1494 		if (ifp->if_flags & IFF_UP)
1495 			return ENETRESET;
1496 		return 0;
1497 	case SIOCSIFFLAGS:
1498 		ifr = data;
1499 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1500 			s = splnet();
1501 			if_down(ifp);
1502 			splx(s);
1503 		}
1504 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1505 			s = splnet();
1506 			if_up(ifp);
1507 			splx(s);
1508 		}
1509 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1510 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
1511 		break;
1512 	case SIOCGIFFLAGS:
1513 		ifr = data;
1514 		ifr->ifr_flags = ifp->if_flags;
1515 		break;
1516 
1517 	case SIOCGIFMETRIC:
1518 		ifr = data;
1519 		ifr->ifr_metric = ifp->if_metric;
1520 		break;
1521 
1522 	case SIOCGIFMTU:
1523 		ifr = data;
1524 		ifr->ifr_mtu = ifp->if_mtu;
1525 		break;
1526 
1527 	case SIOCGIFDLT:
1528 		ifr = data;
1529 		ifr->ifr_dlt = ifp->if_dlt;
1530 		break;
1531 
1532 	case SIOCGIFCAP:
1533 		ifcr = data;
1534 		ifcr->ifcr_capabilities = ifp->if_capabilities;
1535 		ifcr->ifcr_capenable = ifp->if_capenable;
1536 		break;
1537 
1538 	case SIOCSIFMETRIC:
1539 		ifr = data;
1540 		ifp->if_metric = ifr->ifr_metric;
1541 		break;
1542 
1543 	case SIOCGIFDATA:
1544 		ifdr = data;
1545 		ifdr->ifdr_data = ifp->if_data;
1546 		break;
1547 
1548 	case SIOCZIFDATA:
1549 		ifdr = data;
1550 		ifdr->ifdr_data = ifp->if_data;
1551 		/*
1552 		 * Assumes that the volatile counters that can be
1553 		 * zero'ed are at the end of if_data.
1554 		 */
1555 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1556 		    offsetof(struct if_data, ifi_ipackets));
1557 		break;
1558 	case SIOCSIFMTU:
1559 		ifr = data;
1560 		if (ifp->if_mtu == ifr->ifr_mtu)
1561 			break;
1562 		ifp->if_mtu = ifr->ifr_mtu;
1563 		/*
1564 		 * If the link MTU changed, do network layer specific procedure.
1565 		 */
1566 #ifdef INET6
1567 		nd6_setmtu(ifp);
1568 #endif
1569 		return ENETRESET;
1570 	default:
1571 		return ENOTTY;
1572 	}
1573 	return 0;
1574 }
1575 
1576 /*
1577  * Interface ioctls.
1578  */
1579 int
1580 ifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1581 {
1582 	struct ifnet *ifp;
1583 	struct ifreq *ifr;
1584 	struct ifcapreq *ifcr;
1585 	struct ifdatareq *ifdr;
1586 	int error = 0;
1587 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1588 	u_long ocmd = cmd;
1589 #endif
1590 	short oif_flags;
1591 #ifdef COMPAT_OIFREQ
1592 	struct ifreq ifrb;
1593 	struct oifreq *oifr = NULL;
1594 #endif
1595 
1596 	switch (cmd) {
1597 #ifdef COMPAT_OIFREQ
1598 	case OSIOCGIFCONF:
1599 	case OOSIOCGIFCONF:
1600 		return compat_ifconf(cmd, data);
1601 #endif
1602 #ifdef COMPAT_OIFDATA
1603 	case OSIOCGIFDATA:
1604 	case OSIOCZIFDATA:
1605 		return compat_ifdatareq(l, cmd, data);
1606 #endif
1607 	case SIOCGIFCONF:
1608 		return ifconf(cmd, data);
1609 	case SIOCINITIFADDR:
1610 		return EPERM;
1611 	}
1612 
1613 #ifdef COMPAT_OIFREQ
1614 	cmd = compat_cvtcmd(cmd);
1615 	if (cmd != ocmd) {
1616 		oifr = data;
1617 		data = ifr = &ifrb;
1618 		ifreqo2n(oifr, ifr);
1619 	} else
1620 #endif
1621 		ifr = data;
1622 	ifcr = data;
1623 	ifdr = data;
1624 
1625 	ifp = ifunit(ifr->ifr_name);
1626 
1627 	switch (cmd) {
1628 	case SIOCIFCREATE:
1629 	case SIOCIFDESTROY:
1630 		if (l != NULL) {
1631 			error = kauth_authorize_network(l->l_cred,
1632 			    KAUTH_NETWORK_INTERFACE,
1633 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1634 			    (void *)cmd, NULL);
1635 			if (error != 0)
1636 				return error;
1637 		}
1638 		return (cmd == SIOCIFCREATE) ?
1639 			if_clone_create(ifr->ifr_name) :
1640 			if_clone_destroy(ifr->ifr_name);
1641 
1642 	case SIOCIFGCLONERS:
1643 		return if_clone_list((struct if_clonereq *)data);
1644 	}
1645 
1646 	if (ifp == NULL)
1647 		return ENXIO;
1648 
1649 	switch (cmd) {
1650 	case SIOCALIFADDR:
1651 	case SIOCDLIFADDR:
1652 	case SIOCSIFADDRPREF:
1653 	case SIOCSIFFLAGS:
1654 	case SIOCSIFCAP:
1655 	case SIOCSIFMETRIC:
1656 	case SIOCZIFDATA:
1657 	case SIOCSIFMTU:
1658 	case SIOCSIFPHYADDR:
1659 	case SIOCDIFPHYADDR:
1660 #ifdef INET6
1661 	case SIOCSIFPHYADDR_IN6:
1662 #endif
1663 	case SIOCSLIFPHYADDR:
1664 	case SIOCADDMULTI:
1665 	case SIOCDELMULTI:
1666 	case SIOCSIFMEDIA:
1667 	case SIOCSDRVSPEC:
1668 	case SIOCG80211:
1669 	case SIOCS80211:
1670 	case SIOCS80211NWID:
1671 	case SIOCS80211NWKEY:
1672 	case SIOCS80211POWER:
1673 	case SIOCS80211BSSID:
1674 	case SIOCS80211CHANNEL:
1675 		if (l != NULL) {
1676 			error = kauth_authorize_network(l->l_cred,
1677 			    KAUTH_NETWORK_INTERFACE,
1678 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1679 			    (void *)cmd, NULL);
1680 			if (error != 0)
1681 				return error;
1682 		}
1683 	}
1684 
1685 	oif_flags = ifp->if_flags;
1686 
1687 	error = (*ifp->if_ioctl)(ifp, cmd, data);
1688 	if (error != ENOTTY)
1689 		;
1690 	else if (so->so_proto == NULL)
1691 		return EOPNOTSUPP;
1692 	else {
1693 #ifdef COMPAT_OSOCK
1694 		error = compat_ifioctl(so, ocmd, cmd, data, l);
1695 #else
1696 		error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1697 		    (struct mbuf *)cmd, (struct mbuf *)data,
1698 		    (struct mbuf *)ifp, l);
1699 #endif
1700 	}
1701 
1702 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1703 #ifdef INET6
1704 		if ((ifp->if_flags & IFF_UP) != 0) {
1705 			int s = splnet();
1706 			in6_if_up(ifp);
1707 			splx(s);
1708 		}
1709 #endif
1710 	}
1711 #ifdef COMPAT_OIFREQ
1712 	if (cmd != ocmd)
1713 		ifreqn2o(oifr, ifr);
1714 #endif
1715 
1716 	return error;
1717 }
1718 
1719 /*
1720  * Return interface configuration
1721  * of system.  List may be used
1722  * in later ioctl's (above) to get
1723  * other information.
1724  *
1725  * Each record is a struct ifreq.  Before the addition of
1726  * sockaddr_storage, the API rule was that sockaddr flavors that did
1727  * not fit would extend beyond the struct ifreq, with the next struct
1728  * ifreq starting sa_len beyond the struct sockaddr.  Because the
1729  * union in struct ifreq includes struct sockaddr_storage, every kind
1730  * of sockaddr must fit.  Thus, there are no longer any overlength
1731  * records.
1732  *
1733  * Records are added to the user buffer if they fit, and ifc_len is
1734  * adjusted to the length that was written.  Thus, the user is only
1735  * assured of getting the complete list if ifc_len on return is at
1736  * least sizeof(struct ifreq) less than it was on entry.
1737  *
1738  * If the user buffer pointer is NULL, this routine copies no data and
1739  * returns the amount of space that would be needed.
1740  *
1741  * Invariants:
1742  * ifrp points to the next part of the user's buffer to be used.  If
1743  * ifrp != NULL, space holds the number of bytes remaining that we may
1744  * write at ifrp.  Otherwise, space holds the number of bytes that
1745  * would have been written had there been adequate space.
1746  */
1747 /*ARGSUSED*/
1748 int
1749 ifconf(u_long cmd, void *data)
1750 {
1751 	struct ifconf *ifc = (struct ifconf *)data;
1752 	struct ifnet *ifp;
1753 	struct ifaddr *ifa;
1754 	struct ifreq ifr, *ifrp;
1755 	int space, error = 0;
1756 	const int sz = (int)sizeof(struct ifreq);
1757 
1758 	if ((ifrp = ifc->ifc_req) == NULL)
1759 		space = 0;
1760 	else
1761 		space = ifc->ifc_len;
1762 	IFNET_FOREACH(ifp) {
1763 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
1764 		    sizeof(ifr.ifr_name));
1765 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1766 			return ENAMETOOLONG;
1767 		if (IFADDR_EMPTY(ifp)) {
1768 			/* Interface with no addresses - send zero sockaddr. */
1769 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1770 			if (ifrp == NULL) {
1771 				space += sz;
1772 				continue;
1773 			}
1774 			if (space >= sz) {
1775 				error = copyout(&ifr, ifrp, sz);
1776 				if (error != 0)
1777 					return error;
1778 				ifrp++;
1779 				space -= sz;
1780 			}
1781 		}
1782 
1783 		IFADDR_FOREACH(ifa, ifp) {
1784 			struct sockaddr *sa = ifa->ifa_addr;
1785 			/* all sockaddrs must fit in sockaddr_storage */
1786 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
1787 
1788 			if (ifrp == NULL) {
1789 				space += sz;
1790 				continue;
1791 			}
1792 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
1793 			if (space >= sz) {
1794 				error = copyout(&ifr, ifrp, sz);
1795 				if (error != 0)
1796 					return (error);
1797 				ifrp++; space -= sz;
1798 			}
1799 		}
1800 	}
1801 	if (ifrp != NULL) {
1802 		KASSERT(0 <= space && space <= ifc->ifc_len);
1803 		ifc->ifc_len -= space;
1804 	} else {
1805 		KASSERT(space >= 0);
1806 		ifc->ifc_len = space;
1807 	}
1808 	return (0);
1809 }
1810 
1811 int
1812 ifreq_setaddr(const u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1813 {
1814 	uint8_t len;
1815 	u_long ncmd;
1816 	const uint8_t osockspace = sizeof(ifr->ifr_addr);
1817 	const uint8_t sockspace = sizeof(ifr->ifr_ifru.ifru_space);
1818 
1819 #ifdef INET6
1820 	if (cmd == SIOCGIFPSRCADDR_IN6 || cmd == SIOCGIFPDSTADDR_IN6)
1821 		len = MIN(sizeof(struct sockaddr_in6), sa->sa_len);
1822 	else
1823 #endif /* INET6 */
1824 	if ((ncmd = compat_cvtcmd(cmd)) != cmd)
1825 		len = MIN(osockspace, sa->sa_len);
1826 	else
1827 		len = MIN(sockspace, sa->sa_len);
1828 	if (len < sa->sa_len)
1829 		return EFBIG;
1830 	sockaddr_copy(&ifr->ifr_addr, len, sa);
1831 	return 0;
1832 }
1833 
1834 /*
1835  * Queue message on interface, and start output if interface
1836  * not yet active.
1837  */
1838 int
1839 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1840     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1841 {
1842 	int len = m->m_pkthdr.len;
1843 	int mflags = m->m_flags;
1844 	int s = splnet();
1845 	int error;
1846 
1847 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1848 	if (error != 0)
1849 		goto out;
1850 	ifp->if_obytes += len;
1851 	if (mflags & M_MCAST)
1852 		ifp->if_omcasts++;
1853 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
1854 		(*ifp->if_start)(ifp);
1855 out:
1856 	splx(s);
1857 	return error;
1858 }
1859 
1860 /*
1861  * Queue message on interface, possibly using a second fast queue
1862  */
1863 int
1864 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1865     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1866 {
1867 	int error = 0;
1868 
1869 	if (ifq != NULL
1870 #ifdef ALTQ
1871 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1872 #endif
1873 	    ) {
1874 		if (IF_QFULL(ifq)) {
1875 			IF_DROP(&ifp->if_snd);
1876 			m_freem(m);
1877 			if (error == 0)
1878 				error = ENOBUFS;
1879 		} else
1880 			IF_ENQUEUE(ifq, m);
1881 	} else
1882 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1883 	if (error != 0) {
1884 		++ifp->if_oerrors;
1885 		return error;
1886 	}
1887 	return 0;
1888 }
1889 
1890 
1891 static void
1892 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
1893     struct ifaltq *ifq)
1894 {
1895 	const struct sysctlnode *cnode, *rnode;
1896 
1897 	if (sysctl_createv(clog, 0, NULL, &rnode,
1898 		       CTLFLAG_PERMANENT,
1899 		       CTLTYPE_NODE, "net", NULL,
1900 		       NULL, 0, NULL, 0,
1901 		       CTL_NET, CTL_EOL) != 0)
1902 		goto bad;
1903 
1904 	if (sysctl_createv(clog, 0, &rnode, &rnode,
1905 		       CTLFLAG_PERMANENT,
1906 		       CTLTYPE_NODE, "interfaces",
1907 		       SYSCTL_DESCR("Per-interface controls"),
1908 		       NULL, 0, NULL, 0,
1909 		       CTL_CREATE, CTL_EOL) != 0)
1910 		goto bad;
1911 
1912 	if (sysctl_createv(clog, 0, &rnode, &rnode,
1913 		       CTLFLAG_PERMANENT,
1914 		       CTLTYPE_NODE, ifname,
1915 		       SYSCTL_DESCR("Interface controls"),
1916 		       NULL, 0, NULL, 0,
1917 		       CTL_CREATE, CTL_EOL) != 0)
1918 		goto bad;
1919 
1920 	if (sysctl_createv(clog, 0, &rnode, &rnode,
1921 		       CTLFLAG_PERMANENT,
1922 		       CTLTYPE_NODE, "sndq",
1923 		       SYSCTL_DESCR("Interface output queue controls"),
1924 		       NULL, 0, NULL, 0,
1925 		       CTL_CREATE, CTL_EOL) != 0)
1926 		goto bad;
1927 
1928 	if (sysctl_createv(clog, 0, &rnode, &cnode,
1929 		       CTLFLAG_PERMANENT,
1930 		       CTLTYPE_INT, "len",
1931 		       SYSCTL_DESCR("Current output queue length"),
1932 		       NULL, 0, &ifq->ifq_len, 0,
1933 		       CTL_CREATE, CTL_EOL) != 0)
1934 		goto bad;
1935 
1936 	if (sysctl_createv(clog, 0, &rnode, &cnode,
1937 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1938 		       CTLTYPE_INT, "maxlen",
1939 		       SYSCTL_DESCR("Maximum allowed output queue length"),
1940 		       NULL, 0, &ifq->ifq_maxlen, 0,
1941 		       CTL_CREATE, CTL_EOL) != 0)
1942 		goto bad;
1943 
1944 	if (sysctl_createv(clog, 0, &rnode, &cnode,
1945 		       CTLFLAG_PERMANENT,
1946 		       CTLTYPE_INT, "drops",
1947 		       SYSCTL_DESCR("Packets dropped due to full output queue"),
1948 		       NULL, 0, &ifq->ifq_drops, 0,
1949 		       CTL_CREATE, CTL_EOL) != 0)
1950 		goto bad;
1951 
1952 	return;
1953 bad:
1954 	printf("%s: could not attach sysctl nodes\n", ifname);
1955 	return;
1956 }
1957 
1958 #if defined(INET) || defined(INET6)
1959 static void
1960 sysctl_net_ifq_setup(struct sysctllog **clog,
1961 		     int pf, const char *pfname,
1962 		     int ipn, const char *ipname,
1963 		     int qid, struct ifqueue *ifq)
1964 {
1965 
1966 	sysctl_createv(clog, 0, NULL, NULL,
1967 		       CTLFLAG_PERMANENT,
1968 		       CTLTYPE_NODE, "net", NULL,
1969 		       NULL, 0, NULL, 0,
1970 		       CTL_NET, CTL_EOL);
1971 	sysctl_createv(clog, 0, NULL, NULL,
1972 		       CTLFLAG_PERMANENT,
1973 		       CTLTYPE_NODE, pfname, NULL,
1974 		       NULL, 0, NULL, 0,
1975 		       CTL_NET, pf, CTL_EOL);
1976 	sysctl_createv(clog, 0, NULL, NULL,
1977 		       CTLFLAG_PERMANENT,
1978 		       CTLTYPE_NODE, ipname, NULL,
1979 		       NULL, 0, NULL, 0,
1980 		       CTL_NET, pf, ipn, CTL_EOL);
1981 	sysctl_createv(clog, 0, NULL, NULL,
1982 		       CTLFLAG_PERMANENT,
1983 		       CTLTYPE_NODE, "ifq",
1984 		       SYSCTL_DESCR("Protocol input queue controls"),
1985 		       NULL, 0, NULL, 0,
1986 		       CTL_NET, pf, ipn, qid, CTL_EOL);
1987 
1988 	sysctl_createv(clog, 0, NULL, NULL,
1989 		       CTLFLAG_PERMANENT,
1990 		       CTLTYPE_INT, "len",
1991 		       SYSCTL_DESCR("Current input queue length"),
1992 		       NULL, 0, &ifq->ifq_len, 0,
1993 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1994 	sysctl_createv(clog, 0, NULL, NULL,
1995 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1996 		       CTLTYPE_INT, "maxlen",
1997 		       SYSCTL_DESCR("Maximum allowed input queue length"),
1998 		       NULL, 0, &ifq->ifq_maxlen, 0,
1999 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
2000 #ifdef notyet
2001 	sysctl_createv(clog, 0, NULL, NULL,
2002 		       CTLFLAG_PERMANENT,
2003 		       CTLTYPE_INT, "peak",
2004 		       SYSCTL_DESCR("Highest input queue length"),
2005 		       NULL, 0, &ifq->ifq_peak, 0,
2006 		       CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
2007 #endif
2008 	sysctl_createv(clog, 0, NULL, NULL,
2009 		       CTLFLAG_PERMANENT,
2010 		       CTLTYPE_INT, "drops",
2011 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
2012 		       NULL, 0, &ifq->ifq_drops, 0,
2013 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
2014 }
2015 
2016 #ifdef INET
2017 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
2018 	     "sysctl net.inet.ip.ifq subtree setup")
2019 {
2020 	extern struct ifqueue ipintrq;
2021 
2022 	sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
2023 			     IPCTL_IFQ, &ipintrq);
2024 }
2025 #endif /* INET */
2026 
2027 #ifdef INET6
2028 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
2029 	     "sysctl net.inet6.ip6.ifq subtree setup")
2030 {
2031 	extern struct ifqueue ip6intrq;
2032 
2033 	sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
2034 			     IPV6CTL_IFQ, &ip6intrq);
2035 }
2036 #endif /* INET6 */
2037 #endif /* INET || INET6 */
2038