Home | History | Annotate | Line # | Download | only in net
if.c revision 1.66
      1 /*	$NetBSD: if.c,v 1.66 2000/07/19 06:00:39 onoe Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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. All advertising materials mentioning features or use of this software
     81  *    must display the following acknowledgement:
     82  *	This product includes software developed by the University of
     83  *	California, Berkeley and its contributors.
     84  * 4. Neither the name of the University nor the names of its contributors
     85  *    may be used to endorse or promote products derived from this software
     86  *    without specific prior written permission.
     87  *
     88  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     89  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     90  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     91  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     92  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     93  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     94  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     95  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     96  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     97  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     98  * SUCH DAMAGE.
     99  *
    100  *	@(#)if.c	8.5 (Berkeley) 1/9/95
    101  */
    102 
    103 #include "opt_inet.h"
    104 
    105 #include "opt_compat_linux.h"
    106 #include "opt_compat_svr4.h"
    107 #include "opt_compat_43.h"
    108 #include "opt_atalk.h"
    109 
    110 #include <sys/param.h>
    111 #include <sys/mbuf.h>
    112 #include <sys/systm.h>
    113 #include <sys/callout.h>
    114 #include <sys/proc.h>
    115 #include <sys/socket.h>
    116 #include <sys/socketvar.h>
    117 #include <sys/domain.h>
    118 #include <sys/protosw.h>
    119 #include <sys/kernel.h>
    120 #include <sys/ioctl.h>
    121 
    122 #include <net/if.h>
    123 #include <net/if_dl.h>
    124 #include <net/if_ether.h>
    125 #include <net/if_ieee80211.h>
    126 #include <net/if_types.h>
    127 #include <net/radix.h>
    128 #include <net/route.h>
    129 #ifdef NETATALK
    130 #include <netatalk/at_extern.h>
    131 #include <netatalk/at.h>
    132 #endif
    133 
    134 #ifdef INET6
    135 /*XXX*/
    136 #include <netinet/in.h>
    137 #endif
    138 
    139 int	ifqmaxlen = IFQ_MAXLEN;
    140 struct	callout if_slowtimo_ch;
    141 
    142 #ifdef INET6
    143 /*
    144  * XXX: declare here to avoid to include many inet6 related files..
    145  * should be more generalized?
    146  */
    147 extern void nd6_setmtu __P((struct ifnet *));
    148 #endif
    149 
    150 int	if_rt_walktree __P((struct radix_node *, void *));
    151 
    152 struct if_clone *if_clone_lookup __P((const char *, int *));
    153 
    154 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
    155 
    156 /*
    157  * Network interface utility routines.
    158  *
    159  * Routines with ifa_ifwith* names take sockaddr *'s as
    160  * parameters.
    161  */
    162 void
    163 ifinit()
    164 {
    165 
    166 	callout_init(&if_slowtimo_ch);
    167 	if_slowtimo(NULL);
    168 }
    169 
    170 /*
    171  * Null routines used while an interface is going away.  These routines
    172  * just return an error.
    173  */
    174 
    175 int
    176 if_nulloutput(ifp, m, so, rt)
    177 	struct ifnet *ifp;
    178 	struct mbuf *m;
    179 	struct sockaddr *so;
    180 	struct rtentry *rt;
    181 {
    182 
    183 	return (ENXIO);
    184 }
    185 
    186 void
    187 if_nullinput(ifp, m)
    188 	struct ifnet *ifp;
    189 	struct mbuf *m;
    190 {
    191 
    192 	/* Nothing. */
    193 }
    194 
    195 void
    196 if_nullstart(ifp)
    197 	struct ifnet *ifp;
    198 {
    199 
    200 	/* Nothing. */
    201 }
    202 
    203 int
    204 if_nullioctl(ifp, cmd, data)
    205 	struct ifnet *ifp;
    206 	u_long cmd;
    207 	caddr_t data;
    208 {
    209 
    210 	return (ENXIO);
    211 }
    212 
    213 int
    214 if_nullreset(ifp)
    215 	struct ifnet *ifp;
    216 {
    217 
    218 	return (ENXIO);
    219 }
    220 
    221 void
    222 if_nullwatchdog(ifp)
    223 	struct ifnet *ifp;
    224 {
    225 
    226 	/* Nothing. */
    227 }
    228 
    229 void
    230 if_nulldrain(ifp)
    231 	struct ifnet *ifp;
    232 {
    233 
    234 	/* Nothing. */
    235 }
    236 
    237 int if_index = 0;
    238 struct ifaddr **ifnet_addrs = NULL;
    239 struct ifnet **ifindex2ifnet = NULL;
    240 
    241 /*
    242  * Attach an interface to the
    243  * list of "active" interfaces.
    244  */
    245 void
    246 if_attach(ifp)
    247 	struct ifnet *ifp;
    248 {
    249 	unsigned socksize, ifasize;
    250 	int namelen, masklen;
    251 	struct sockaddr_dl *sdl;
    252 	struct ifaddr *ifa;
    253 	static size_t if_indexlim = 8;
    254 
    255 	if (if_index == 0)
    256 		TAILQ_INIT(&ifnet);
    257 	TAILQ_INIT(&ifp->if_addrlist);
    258 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
    259 	ifp->if_index = ++if_index;
    260 
    261 	/*
    262 	 * We have some arrays that should be indexed by if_index.
    263 	 * since if_index will grow dynamically, they should grow too.
    264 	 *	struct ifadd **ifnet_addrs
    265 	 *	struct ifnet **ifindex2ifnet
    266 	 */
    267 	if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
    268 	    ifp->if_index >= if_indexlim) {
    269 		size_t n;
    270 		caddr_t q;
    271 
    272 		while (ifp->if_index >= if_indexlim)
    273 			if_indexlim <<= 1;
    274 
    275 		/* grow ifnet_addrs */
    276 		n = if_indexlim * sizeof(ifa);
    277 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    278 		bzero(q, n);
    279 		if (ifnet_addrs) {
    280 			bcopy((caddr_t)ifnet_addrs, q, n/2);
    281 			free((caddr_t)ifnet_addrs, M_IFADDR);
    282 		}
    283 		ifnet_addrs = (struct ifaddr **)q;
    284 
    285 		/* grow ifindex2ifnet */
    286 		n = if_indexlim * sizeof(struct ifnet *);
    287 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    288 		bzero(q, n);
    289 		if (ifindex2ifnet) {
    290 			bcopy((caddr_t)ifindex2ifnet, q, n/2);
    291 			free((caddr_t)ifindex2ifnet, M_IFADDR);
    292 		}
    293 		ifindex2ifnet = (struct ifnet **)q;
    294 	}
    295 
    296 	ifindex2ifnet[ifp->if_index] = ifp;
    297 
    298 	/*
    299 	 * create a Link Level name for this device
    300 	 */
    301 	namelen = strlen(ifp->if_xname);
    302 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
    303 	socksize = masklen + ifp->if_addrlen;
    304 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
    305 	if (socksize < sizeof(*sdl))
    306 		socksize = sizeof(*sdl);
    307 	socksize = ROUNDUP(socksize);
    308 	ifasize = sizeof(*ifa) + 2 * socksize;
    309 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
    310 	bzero((caddr_t)ifa, ifasize);
    311 	sdl = (struct sockaddr_dl *)(ifa + 1);
    312 	sdl->sdl_len = socksize;
    313 	sdl->sdl_family = AF_LINK;
    314 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
    315 	sdl->sdl_nlen = namelen;
    316 	sdl->sdl_index = ifp->if_index;
    317 	sdl->sdl_type = ifp->if_type;
    318 	ifnet_addrs[ifp->if_index] = ifa;
    319 	IFAREF(ifa);
    320 	ifa->ifa_ifp = ifp;
    321 	ifa->ifa_rtrequest = link_rtrequest;
    322 	TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
    323 	IFAREF(ifa);
    324 	ifa->ifa_addr = (struct sockaddr *)sdl;
    325 	ifp->if_sadl = sdl;
    326 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
    327 	ifa->ifa_netmask = (struct sockaddr *)sdl;
    328 	sdl->sdl_len = masklen;
    329 	while (namelen != 0)
    330 		sdl->sdl_data[--namelen] = 0xff;
    331 	if (ifp->if_snd.ifq_maxlen == 0)
    332 	    ifp->if_snd.ifq_maxlen = ifqmaxlen;
    333 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    334 
    335 	ifp->if_link_state = LINK_STATE_UNKNOWN;
    336 
    337 	/* Announce the interface. */
    338 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
    339 }
    340 
    341 /*
    342  * Deactivate an interface.  This points all of the procedure
    343  * handles at error stubs.  May be called from interrupt context.
    344  */
    345 void
    346 if_deactivate(ifp)
    347 	struct ifnet *ifp;
    348 {
    349 	int s;
    350 
    351 	s = splimp();
    352 
    353 	ifp->if_output	 = if_nulloutput;
    354 	ifp->if_input	 = if_nullinput;
    355 	ifp->if_start	 = if_nullstart;
    356 	ifp->if_ioctl	 = if_nullioctl;
    357 	ifp->if_reset	 = if_nullreset;
    358 	ifp->if_watchdog = if_nullwatchdog;
    359 	ifp->if_drain	 = if_nulldrain;
    360 
    361 	/* No more packets may be enqueued. */
    362 	ifp->if_snd.ifq_maxlen = 0;
    363 
    364 	splx(s);
    365 }
    366 
    367 /*
    368  * Detach an interface from the list of "active" interfaces,
    369  * freeing any resources as we go along.
    370  *
    371  * NOTE: This routine must be called with a valid thread context,
    372  * as it may block.
    373  */
    374 void
    375 if_detach(ifp)
    376 	struct ifnet *ifp;
    377 {
    378 	struct socket so;
    379 	struct ifaddr *ifa;
    380 #ifdef IFAREF_DEBUG
    381 	struct ifaddr *last_ifa = NULL;
    382 #endif
    383 	struct domain *dp;
    384 	struct protosw *pr;
    385 	struct radix_node_head *rnh;
    386 	int s, i, family, purged;
    387 
    388 	/*
    389 	 * XXX It's kind of lame that we have to have the
    390 	 * XXX socket structure...
    391 	 */
    392 	memset(&so, 0, sizeof(so));
    393 
    394 	s = splimp();
    395 
    396 	/*
    397 	 * Do an if_down() to give protocols a chance to do something.
    398 	 */
    399 	if_down(ifp);
    400 
    401 	/*
    402 	 * Rip all the addresses off the interface.  This should make
    403 	 * all of the routes go away.
    404 	 */
    405 	while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
    406 		family = ifa->ifa_addr->sa_family;
    407 #ifdef IFAREF_DEBUG
    408 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
    409 		    ifa, family, ifa->ifa_refcnt);
    410 		if (last_ifa != NULL && ifa == last_ifa)
    411 			panic("if_detach: loop detected");
    412 		last_ifa = ifa;
    413 #endif
    414 		if (family == AF_LINK) {
    415 			rtinit(ifa, RTM_DELETE, 0);
    416 			TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
    417 			IFAFREE(ifa);
    418 		} else {
    419 			dp = pffinddomain(family);
    420 #ifdef DIAGNOSTIC
    421 			if (dp == NULL)
    422 				panic("if_detach: no domain for AF %d\n",
    423 				    family);
    424 #endif
    425 			purged = 0;
    426 			for (pr = dp->dom_protosw;
    427 			     pr < dp->dom_protoswNPROTOSW; pr++) {
    428 				so.so_proto = pr;
    429 				if (pr->pr_usrreq != NULL) {
    430 					(void) (*pr->pr_usrreq)(&so,
    431 					    PRU_PURGEIF, NULL, NULL,
    432 					    (struct mbuf *) ifp, curproc);
    433 					purged = 1;
    434 				}
    435 			}
    436 			if (purged == 0) {
    437 				/*
    438 				 * XXX What's really the best thing to do
    439 				 * XXX here?  --thorpej (at) netbsd.org
    440 				 */
    441 				printf("if_detach: WARNING: AF %d not purged\n",
    442 				    family);
    443 			}
    444 		}
    445 	}
    446 
    447 	/* Walk the routing table looking for straglers. */
    448 	for (i = 0; i <= AF_MAX; i++) {
    449 		if ((rnh = rt_tables[i]) != NULL)
    450 			(void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
    451 	}
    452 
    453 	IFAFREE(ifnet_addrs[ifp->if_index]);
    454 	ifnet_addrs[ifp->if_index] = NULL;
    455 
    456 	/* Announce that the interface is gone. */
    457 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
    458 
    459 	TAILQ_REMOVE(&ifnet, ifp, if_list);
    460 
    461 	splx(s);
    462 }
    463 
    464 /*
    465  * Callback for a radix tree walk to delete all references to an
    466  * ifnet.
    467  */
    468 int
    469 if_rt_walktree(rn, v)
    470 	struct radix_node *rn;
    471 	void *v;
    472 {
    473 	struct ifnet *ifp = (struct ifnet *)v;
    474 	struct rtentry *rt = (struct rtentry *)rn;
    475 	int error;
    476 
    477 	if (rt->rt_ifp == ifp) {
    478 		/* Delete the entry. */
    479 		error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
    480 		    rt_mask(rt), rt->rt_flags, NULL);
    481 		if (error)
    482 			printf("%s: warning: unable to delete rtentry @ %p, "
    483 			    "error = %d\n", ifp->if_xname, rt, error);
    484 	}
    485 	return (0);
    486 }
    487 
    488 /*
    489  * Create a clone network interface.
    490  */
    491 int
    492 if_clone_create(name)
    493 	const char *name;
    494 {
    495 	struct if_clone *ifc;
    496 	int unit;
    497 
    498 	ifc = if_clone_lookup(name, &unit);
    499 	if (ifc == NULL)
    500 		return (EINVAL);
    501 
    502 	if (ifunit(name) != NULL)
    503 		return (EEXIST);
    504 
    505 	return ((*ifc->ifc_create)(ifc, unit));
    506 }
    507 
    508 /*
    509  * Destroy a clone network interface.
    510  */
    511 int
    512 if_clone_destroy(name)
    513 	const char *name;
    514 {
    515 	struct if_clone *ifc;
    516 	struct ifnet *ifp;
    517 
    518 	ifc = if_clone_lookup(name, NULL);
    519 	if (ifc == NULL)
    520 		return (EINVAL);
    521 
    522 	ifp = ifunit(name);
    523 	if (ifp == NULL)
    524 		return (ENXIO);
    525 
    526 	if (ifc->ifc_destroy == NULL)
    527 		return (EOPNOTSUPP);
    528 
    529 	(*ifc->ifc_destroy)(ifp);
    530 	return (0);
    531 }
    532 
    533 /*
    534  * Look up a network interface cloner.
    535  */
    536 struct if_clone *
    537 if_clone_lookup(name, unitp)
    538 	const char *name;
    539 	int *unitp;
    540 {
    541 	struct if_clone *ifc;
    542 	const char *cp;
    543 	int i;
    544 
    545 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
    546 		for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
    547 			if (ifc->ifc_name[i] != *cp)
    548 				goto next_ifc;
    549 		}
    550 		goto found_name;
    551  next_ifc:
    552 		ifc = LIST_NEXT(ifc, ifc_list);
    553 	}
    554 
    555 	/* No match. */
    556 	return (NULL);
    557 
    558  found_name:
    559 	for (i = 0; *cp != '\0'; cp++) {
    560 		if (*cp < '0' || *cp > '9') {
    561 			/* Bogus unit number. */
    562 			return (NULL);
    563 		}
    564 		i = (i * 10) + (*cp - '0');
    565 	}
    566 
    567 	if (unitp != NULL)
    568 		*unitp = i;
    569 	return (ifc);
    570 }
    571 
    572 /*
    573  * Register a network interface cloner.
    574  */
    575 void
    576 if_clone_attach(ifc)
    577 	struct if_clone *ifc;
    578 {
    579 
    580 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
    581 }
    582 
    583 /*
    584  * Unregister a network interface cloner.
    585  */
    586 void
    587 if_clone_detach(ifc)
    588 	struct if_clone *ifc;
    589 {
    590 
    591 	LIST_REMOVE(ifc, ifc_list);
    592 }
    593 
    594 /*
    595  * Locate an interface based on a complete address.
    596  */
    597 /*ARGSUSED*/
    598 struct ifaddr *
    599 ifa_ifwithaddr(addr)
    600 	struct sockaddr *addr;
    601 {
    602 	struct ifnet *ifp;
    603 	struct ifaddr *ifa;
    604 
    605 #define	equal(a1, a2) \
    606   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
    607 
    608 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    609 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    610 		if (ifp->if_output == if_nulloutput)
    611 			continue;
    612 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    613 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    614 			if (ifa->ifa_addr->sa_family != addr->sa_family)
    615 				continue;
    616 			if (equal(addr, ifa->ifa_addr))
    617 				return (ifa);
    618 			if ((ifp->if_flags & IFF_BROADCAST) &&
    619 			    ifa->ifa_broadaddr &&
    620 			    /* IP6 doesn't have broadcast */
    621 			    ifa->ifa_broadaddr->sa_len != 0 &&
    622 			    equal(ifa->ifa_broadaddr, addr))
    623 				return (ifa);
    624 		}
    625 	}
    626 	return (NULL);
    627 }
    628 
    629 /*
    630  * Locate the point to point interface with a given destination address.
    631  */
    632 /*ARGSUSED*/
    633 struct ifaddr *
    634 ifa_ifwithdstaddr(addr)
    635 	struct sockaddr *addr;
    636 {
    637 	struct ifnet *ifp;
    638 	struct ifaddr *ifa;
    639 
    640 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    641 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    642 		if (ifp->if_output == if_nulloutput)
    643 			continue;
    644 		if (ifp->if_flags & IFF_POINTOPOINT) {
    645 			for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    646 			     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    647 				if (ifa->ifa_addr->sa_family !=
    648 				      addr->sa_family ||
    649 				    ifa->ifa_dstaddr == NULL)
    650 					continue;
    651 				if (equal(addr, ifa->ifa_dstaddr))
    652 					return (ifa);
    653 			}
    654 		}
    655 	}
    656 	return (NULL);
    657 }
    658 
    659 /*
    660  * Find an interface on a specific network.  If many, choice
    661  * is most specific found.
    662  */
    663 struct ifaddr *
    664 ifa_ifwithnet(addr)
    665 	struct sockaddr *addr;
    666 {
    667 	struct ifnet *ifp;
    668 	struct ifaddr *ifa;
    669 	struct sockaddr_dl *sdl;
    670 	struct ifaddr *ifa_maybe = 0;
    671 	u_int af = addr->sa_family;
    672 	char *addr_data = addr->sa_data, *cplim;
    673 
    674 	if (af == AF_LINK) {
    675 		sdl = (struct sockaddr_dl *)addr;
    676 		if (sdl->sdl_index && sdl->sdl_index <= if_index &&
    677 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
    678 			return (ifnet_addrs[sdl->sdl_index]);
    679 	}
    680 #ifdef NETATALK
    681 	if (af == AF_APPLETALK) {
    682 		struct sockaddr_at *sat, *sat2;
    683 		sat = (struct sockaddr_at *)addr;
    684 		for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    685 		     ifp = TAILQ_NEXT(ifp, if_list)) {
    686 			if (ifp->if_output == if_nulloutput)
    687 				continue;
    688 			ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp);
    689 			if (ifa == NULL)
    690 				continue;
    691 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
    692 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
    693 				return (ifa); /* exact match */
    694 			if (ifa_maybe == NULL) {
    695 				/* else keep the if with the rigth range */
    696 				ifa_maybe = ifa;
    697 			}
    698 		}
    699 		return (ifa_maybe);
    700 	}
    701 #endif
    702 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    703 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    704 		if (ifp->if_output == if_nulloutput)
    705 			continue;
    706 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    707 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    708 			char *cp, *cp2, *cp3;
    709 
    710 			if (ifa->ifa_addr->sa_family != af ||
    711 			    ifa->ifa_netmask == 0)
    712  next:				continue;
    713 			cp = addr_data;
    714 			cp2 = ifa->ifa_addr->sa_data;
    715 			cp3 = ifa->ifa_netmask->sa_data;
    716 			cplim = (char *)ifa->ifa_netmask +
    717 			    ifa->ifa_netmask->sa_len;
    718 			while (cp3 < cplim) {
    719 				if ((*cp++ ^ *cp2++) & *cp3++) {
    720 					/* want to continue for() loop */
    721 					goto next;
    722 				}
    723 			}
    724 			if (ifa_maybe == 0 ||
    725 			    rn_refines((caddr_t)ifa->ifa_netmask,
    726 			    (caddr_t)ifa_maybe->ifa_netmask))
    727 				ifa_maybe = ifa;
    728 		}
    729 	}
    730 	return (ifa_maybe);
    731 }
    732 
    733 /*
    734  * Find the interface of the addresss.
    735  */
    736 struct ifaddr *
    737 ifa_ifwithladdr(addr)
    738 	struct sockaddr *addr;
    739 {
    740 	struct ifaddr *ia;
    741 
    742 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
    743 	    (ia = ifa_ifwithnet(addr)))
    744 		return (ia);
    745 	return (NULL);
    746 }
    747 
    748 /*
    749  * Find an interface using a specific address family
    750  */
    751 struct ifaddr *
    752 ifa_ifwithaf(af)
    753 	int af;
    754 {
    755 	struct ifnet *ifp;
    756 	struct ifaddr *ifa;
    757 
    758 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    759 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    760 		if (ifp->if_output == if_nulloutput)
    761 			continue;
    762 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    763 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    764 			if (ifa->ifa_addr->sa_family == af)
    765 				return (ifa);
    766 		}
    767 	}
    768 	return (NULL);
    769 }
    770 
    771 /*
    772  * Find an interface address specific to an interface best matching
    773  * a given address.
    774  */
    775 struct ifaddr *
    776 ifaof_ifpforaddr(addr, ifp)
    777 	struct sockaddr *addr;
    778 	struct ifnet *ifp;
    779 {
    780 	struct ifaddr *ifa;
    781 	char *cp, *cp2, *cp3;
    782 	char *cplim;
    783 	struct ifaddr *ifa_maybe = 0;
    784 	u_int af = addr->sa_family;
    785 
    786 	if (ifp->if_output == if_nulloutput)
    787 		return (NULL);
    788 
    789 	if (af >= AF_MAX)
    790 		return (NULL);
    791 
    792 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    793 	     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    794 		if (ifa->ifa_addr->sa_family != af)
    795 			continue;
    796 		ifa_maybe = ifa;
    797 		if (ifa->ifa_netmask == 0) {
    798 			if (equal(addr, ifa->ifa_addr) ||
    799 			    (ifa->ifa_dstaddr &&
    800 			     equal(addr, ifa->ifa_dstaddr)))
    801 				return (ifa);
    802 			continue;
    803 		}
    804 		cp = addr->sa_data;
    805 		cp2 = ifa->ifa_addr->sa_data;
    806 		cp3 = ifa->ifa_netmask->sa_data;
    807 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
    808 		for (; cp3 < cplim; cp3++) {
    809 			if ((*cp++ ^ *cp2++) & *cp3)
    810 				break;
    811 		}
    812 		if (cp3 == cplim)
    813 			return (ifa);
    814 	}
    815 	return (ifa_maybe);
    816 }
    817 
    818 /*
    819  * Default action when installing a route with a Link Level gateway.
    820  * Lookup an appropriate real ifa to point to.
    821  * This should be moved to /sys/net/link.c eventually.
    822  */
    823 void
    824 link_rtrequest(cmd, rt, sa)
    825 	int cmd;
    826 	struct rtentry *rt;
    827 	struct sockaddr *sa;
    828 {
    829 	struct ifaddr *ifa;
    830 	struct sockaddr *dst;
    831 	struct ifnet *ifp;
    832 
    833 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
    834 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
    835 		return;
    836 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
    837 		IFAFREE(rt->rt_ifa);
    838 		rt->rt_ifa = ifa;
    839 		IFAREF(ifa);
    840 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
    841 			ifa->ifa_rtrequest(cmd, rt, sa);
    842 	}
    843 }
    844 
    845 /*
    846  * Mark an interface down and notify protocols of
    847  * the transition.
    848  * NOTE: must be called at splsoftnet or equivalent.
    849  */
    850 void
    851 if_down(ifp)
    852 	struct ifnet *ifp;
    853 {
    854 	struct ifaddr *ifa;
    855 
    856 	ifp->if_flags &= ~IFF_UP;
    857 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    858 	     ifa = TAILQ_NEXT(ifa, ifa_list))
    859 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
    860 	if_qflush(&ifp->if_snd);
    861 	rt_ifmsg(ifp);
    862 }
    863 
    864 /*
    865  * Mark an interface up and notify protocols of
    866  * the transition.
    867  * NOTE: must be called at splsoftnet or equivalent.
    868  */
    869 void
    870 if_up(ifp)
    871 	struct ifnet *ifp;
    872 {
    873 #ifdef notyet
    874 	struct ifaddr *ifa;
    875 #endif
    876 
    877 	ifp->if_flags |= IFF_UP;
    878 #ifdef notyet
    879 	/* this has no effect on IP, and will kill all ISO connections XXX */
    880 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    881 	     ifa = TAILQ_NEXT(ifa, ifa_list))
    882 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
    883 #endif
    884 	rt_ifmsg(ifp);
    885 #ifdef INET6
    886 	in6_if_up(ifp);
    887 #endif
    888 }
    889 
    890 /*
    891  * Flush an interface queue.
    892  */
    893 void
    894 if_qflush(ifq)
    895 	struct ifqueue *ifq;
    896 {
    897 	struct mbuf *m, *n;
    898 
    899 	n = ifq->ifq_head;
    900 	while ((m = n) != NULL) {
    901 		n = m->m_act;
    902 		m_freem(m);
    903 	}
    904 	ifq->ifq_head = 0;
    905 	ifq->ifq_tail = 0;
    906 	ifq->ifq_len = 0;
    907 }
    908 
    909 /*
    910  * Handle interface watchdog timer routines.  Called
    911  * from softclock, we decrement timers (if set) and
    912  * call the appropriate interface routine on expiration.
    913  */
    914 void
    915 if_slowtimo(arg)
    916 	void *arg;
    917 {
    918 	struct ifnet *ifp;
    919 	int s = splimp();
    920 
    921 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    922 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    923 		if (ifp->if_timer == 0 || --ifp->if_timer)
    924 			continue;
    925 		if (ifp->if_watchdog)
    926 			(*ifp->if_watchdog)(ifp);
    927 	}
    928 	splx(s);
    929 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
    930 	    if_slowtimo, NULL);
    931 }
    932 
    933 /*
    934  * Set/clear promiscuous mode on interface ifp based on the truth value
    935  * of pswitch.  The calls are reference counted so that only the first
    936  * "on" request actually has an effect, as does the final "off" request.
    937  * Results are undefined if the "off" and "on" requests are not matched.
    938  */
    939 int
    940 ifpromisc(ifp, pswitch)
    941 	struct ifnet *ifp;
    942 	int pswitch;
    943 {
    944 	int pcount, ret;
    945 	short flags;
    946 	struct ifreq ifr;
    947 
    948 	pcount = ifp->if_pcount;
    949 	flags = ifp->if_flags;
    950 	if (pswitch) {
    951 		/*
    952 		 * If the device is not configured up, we cannot put it in
    953 		 * promiscuous mode.
    954 		 */
    955 		if ((ifp->if_flags & IFF_UP) == 0)
    956 			return (ENETDOWN);
    957 		if (ifp->if_pcount != 0)
    958 			return (0);
    959 		ifp->if_flags |= IFF_PROMISC;
    960 	} else {
    961 		if (--ifp->if_pcount > 0)
    962 			return (0);
    963 		ifp->if_flags &= ~IFF_PROMISC;
    964 		/*
    965 		 * If the device is not configured up, we should not need to
    966 		 * turn off promiscuous mode (device should have turned it
    967 		 * off when interface went down; and will look at IFF_PROMISC
    968 		 * again next time interface comes up).
    969 		 */
    970 		if ((ifp->if_flags & IFF_UP) == 0)
    971 			return (0);
    972 	}
    973 	memset(&ifr, 0, sizeof(ifr));
    974 	ifr.ifr_flags = ifp->if_flags;
    975 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
    976 	/* Restore interface state if not successful. */
    977 	if (ret != 0) {
    978 		ifp->if_pcount = pcount;
    979 		ifp->if_flags = flags;
    980 	}
    981 	return (ret);
    982 }
    983 
    984 /*
    985  * Map interface name to
    986  * interface structure pointer.
    987  */
    988 struct ifnet *
    989 ifunit(name)
    990 	const char *name;
    991 {
    992 	struct ifnet *ifp;
    993 
    994 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    995 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    996 		if (ifp->if_output == if_nulloutput)
    997 			continue;
    998 	 	if (strcmp(ifp->if_xname, name) == 0)
    999 			return (ifp);
   1000 	}
   1001 	return (NULL);
   1002 }
   1003 
   1004 /*
   1005  * Interface ioctls.
   1006  */
   1007 int
   1008 ifioctl(so, cmd, data, p)
   1009 	struct socket *so;
   1010 	u_long cmd;
   1011 	caddr_t data;
   1012 	struct proc *p;
   1013 {
   1014 	struct ifnet *ifp;
   1015 	struct ifreq *ifr;
   1016 	int error = 0;
   1017 	short oif_flags;
   1018 
   1019 	switch (cmd) {
   1020 
   1021 	case SIOCGIFCONF:
   1022 	case OSIOCGIFCONF:
   1023 		return (ifconf(cmd, data));
   1024 	}
   1025 	ifr = (struct ifreq *)data;
   1026 
   1027 	switch (cmd) {
   1028 	case SIOCIFCREATE:
   1029 	case SIOCIFDESTROY:
   1030 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1031 			return (error);
   1032 		return ((cmd == SIOCIFCREATE) ?
   1033 			if_clone_create(ifr->ifr_name) :
   1034 			if_clone_destroy(ifr->ifr_name));
   1035 	}
   1036 
   1037 	ifp = ifunit(ifr->ifr_name);
   1038 	if (ifp == 0)
   1039 		return (ENXIO);
   1040 	oif_flags = ifp->if_flags;
   1041 	switch (cmd) {
   1042 
   1043 	case SIOCGIFFLAGS:
   1044 		ifr->ifr_flags = ifp->if_flags;
   1045 		break;
   1046 
   1047 	case SIOCGIFMETRIC:
   1048 		ifr->ifr_metric = ifp->if_metric;
   1049 		break;
   1050 
   1051 	case SIOCGIFMTU:
   1052 		ifr->ifr_mtu = ifp->if_mtu;
   1053 		break;
   1054 
   1055 	case SIOCSIFFLAGS:
   1056 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1057 			return (error);
   1058 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
   1059 			int s = splimp();
   1060 			if_down(ifp);
   1061 			splx(s);
   1062 		}
   1063 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
   1064 			int s = splimp();
   1065 			if_up(ifp);
   1066 			splx(s);
   1067 		}
   1068 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
   1069 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
   1070 		if (ifp->if_ioctl)
   1071 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
   1072 		break;
   1073 
   1074 	case SIOCSIFMETRIC:
   1075 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1076 			return (error);
   1077 		ifp->if_metric = ifr->ifr_metric;
   1078 		break;
   1079 
   1080 	case SIOCSIFMTU:
   1081 	{
   1082 		u_long oldmtu = ifp->if_mtu;
   1083 
   1084 		error = suser(p->p_ucred, &p->p_acflag);
   1085 		if (error)
   1086 			return (error);
   1087 		if (ifp->if_ioctl == NULL)
   1088 			return (EOPNOTSUPP);
   1089 		error = (*ifp->if_ioctl)(ifp, cmd, data);
   1090 
   1091 		/*
   1092 		 * If the link MTU changed, do network layer specific procedure.
   1093 		 */
   1094 		if (ifp->if_mtu != oldmtu) {
   1095 #ifdef INET6
   1096 			nd6_setmtu(ifp);
   1097 #endif
   1098 		}
   1099 		break;
   1100 	}
   1101 	case SIOCADDMULTI:
   1102 	case SIOCDELMULTI:
   1103 	case SIOCSIFMEDIA:
   1104 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1105 			return (error);
   1106 		/* FALLTHROUGH */
   1107 	case SIOCGIFMEDIA:
   1108 		if (ifp->if_ioctl == 0)
   1109 			return (EOPNOTSUPP);
   1110 		error = (*ifp->if_ioctl)(ifp, cmd, data);
   1111 		break;
   1112 
   1113 	case SIOCSDRVSPEC:
   1114 	case SIOCS80211NWID:
   1115 		/* XXX:  need to pass proc pointer through to driver... */
   1116 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1117 			return (error);
   1118 	/* FALLTHROUGH */
   1119 	default:
   1120 		if (so->so_proto == 0)
   1121 			return (EOPNOTSUPP);
   1122 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4)
   1123 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
   1124 		    (struct mbuf *)cmd, (struct mbuf *)data,
   1125 		    (struct mbuf *)ifp, p));
   1126 #else
   1127 	    {
   1128 		int ocmd = cmd;
   1129 
   1130 		switch (cmd) {
   1131 
   1132 		case SIOCSIFADDR:
   1133 		case SIOCSIFDSTADDR:
   1134 		case SIOCSIFBRDADDR:
   1135 		case SIOCSIFNETMASK:
   1136 #if BYTE_ORDER != BIG_ENDIAN
   1137 			if (ifr->ifr_addr.sa_family == 0 &&
   1138 			    ifr->ifr_addr.sa_len < 16) {
   1139 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
   1140 				ifr->ifr_addr.sa_len = 16;
   1141 			}
   1142 #else
   1143 			if (ifr->ifr_addr.sa_len == 0)
   1144 				ifr->ifr_addr.sa_len = 16;
   1145 #endif
   1146 			break;
   1147 
   1148 		case OSIOCGIFADDR:
   1149 			cmd = SIOCGIFADDR;
   1150 			break;
   1151 
   1152 		case OSIOCGIFDSTADDR:
   1153 			cmd = SIOCGIFDSTADDR;
   1154 			break;
   1155 
   1156 		case OSIOCGIFBRDADDR:
   1157 			cmd = SIOCGIFBRDADDR;
   1158 			break;
   1159 
   1160 		case OSIOCGIFNETMASK:
   1161 			cmd = SIOCGIFNETMASK;
   1162 		}
   1163 
   1164 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
   1165 		    (struct mbuf *)cmd, (struct mbuf *)data,
   1166 		    (struct mbuf *)ifp, p));
   1167 
   1168 		switch (ocmd) {
   1169 		case OSIOCGIFADDR:
   1170 		case OSIOCGIFDSTADDR:
   1171 		case OSIOCGIFBRDADDR:
   1172 		case OSIOCGIFNETMASK:
   1173 			*(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
   1174 		}
   1175 	    }
   1176 #endif /* COMPAT_43 */
   1177 		break;
   1178 	}
   1179 
   1180 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   1181 #ifdef INET6
   1182 		if ((ifp->if_flags & IFF_UP) != 0) {
   1183 			int s = splimp();
   1184 			in6_if_up(ifp);
   1185 			splx(s);
   1186 		}
   1187 #endif
   1188 	}
   1189 
   1190 	return (error);
   1191 }
   1192 
   1193 /*
   1194  * Return interface configuration
   1195  * of system.  List may be used
   1196  * in later ioctl's (above) to get
   1197  * other information.
   1198  */
   1199 /*ARGSUSED*/
   1200 int
   1201 ifconf(cmd, data)
   1202 	u_long cmd;
   1203 	caddr_t data;
   1204 {
   1205 	struct ifconf *ifc = (struct ifconf *)data;
   1206 	struct ifnet *ifp;
   1207 	struct ifaddr *ifa;
   1208 	struct ifreq ifr, *ifrp;
   1209 	int space = ifc->ifc_len, error = 0;
   1210 
   1211 	ifrp = ifc->ifc_req;
   1212 	for (ifp = ifnet.tqh_first;
   1213 	    space >= sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
   1214 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
   1215 		if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
   1216 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
   1217 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1218 			    sizeof(ifr));
   1219 			if (error)
   1220 				break;
   1221 			space -= sizeof (ifr), ifrp++;
   1222 		} else
   1223 		    for (; space >= sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
   1224 			struct sockaddr *sa = ifa->ifa_addr;
   1225 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
   1226 			if (cmd == OSIOCGIFCONF) {
   1227 				struct osockaddr *osa =
   1228 					 (struct osockaddr *)&ifr.ifr_addr;
   1229 				ifr.ifr_addr = *sa;
   1230 				osa->sa_family = sa->sa_family;
   1231 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1232 						sizeof (ifr));
   1233 				ifrp++;
   1234 			} else
   1235 #endif
   1236 			if (sa->sa_len <= sizeof(*sa)) {
   1237 				ifr.ifr_addr = *sa;
   1238 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1239 						sizeof (ifr));
   1240 				ifrp++;
   1241 			} else {
   1242 				space -= sa->sa_len - sizeof(*sa);
   1243 				if (space < sizeof (ifr))
   1244 					break;
   1245 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1246 						sizeof (ifr.ifr_name));
   1247 				if (error == 0)
   1248 				    error = copyout((caddr_t)sa,
   1249 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
   1250 				ifrp = (struct ifreq *)
   1251 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
   1252 			}
   1253 			if (error)
   1254 				break;
   1255 			space -= sizeof (ifr);
   1256 		}
   1257 	}
   1258 	ifc->ifc_len -= space;
   1259 	return (error);
   1260 }
   1261