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