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