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