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