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