Home | History | Annotate | Line # | Download | only in net
if.c revision 1.59
      1 /*	$NetBSD: if.c,v 1.59 2000/03/23 07:03:24 thorpej 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_types.h>
    125 #include <net/radix.h>
    126 #include <net/route.h>
    127 #ifdef NETATALK
    128 #include <netatalk/at_extern.h>
    129 #include <netatalk/at.h>
    130 #endif
    131 
    132 #ifdef INET6
    133 /*XXX*/
    134 #include <netinet/in.h>
    135 #endif
    136 
    137 int	ifqmaxlen = IFQ_MAXLEN;
    138 void	if_slowtimo __P((void *arg));
    139 
    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 /*
    153  * Network interface utility routines.
    154  *
    155  * Routines with ifa_ifwith* names take sockaddr *'s as
    156  * parameters.
    157  */
    158 void
    159 ifinit()
    160 {
    161 
    162 	callout_init(&if_slowtimo_ch);
    163 	if_slowtimo(NULL);
    164 }
    165 
    166 /*
    167  * Null routines used while an interface is going away.  These routines
    168  * just return an error.
    169  */
    170 int	if_nulloutput __P((struct ifnet *, struct mbuf *,
    171 	    struct sockaddr *, struct rtentry *));
    172 void	if_nullinput __P((struct ifnet *, struct mbuf *));
    173 void	if_nullstart __P((struct ifnet *));
    174 int	if_nullioctl __P((struct ifnet *, u_long, caddr_t));
    175 int	if_nullreset __P((struct ifnet *));
    176 void	if_nullwatchdog __P((struct ifnet *));
    177 void	if_nulldrain __P((struct ifnet *));
    178 
    179 int
    180 if_nulloutput(ifp, m, so, rt)
    181 	struct ifnet *ifp;
    182 	struct mbuf *m;
    183 	struct sockaddr *so;
    184 	struct rtentry *rt;
    185 {
    186 
    187 	return (ENXIO);
    188 }
    189 
    190 void
    191 if_nullinput(ifp, m)
    192 	struct ifnet *ifp;
    193 	struct mbuf *m;
    194 {
    195 
    196 	/* Nothing. */
    197 }
    198 
    199 void
    200 if_nullstart(ifp)
    201 	struct ifnet *ifp;
    202 {
    203 
    204 	/* Nothing. */
    205 }
    206 
    207 int
    208 if_nullioctl(ifp, cmd, data)
    209 	struct ifnet *ifp;
    210 	u_long cmd;
    211 	caddr_t data;
    212 {
    213 
    214 	return (ENXIO);
    215 }
    216 
    217 int
    218 if_nullreset(ifp)
    219 	struct ifnet *ifp;
    220 {
    221 
    222 	return (ENXIO);
    223 }
    224 
    225 void
    226 if_nullwatchdog(ifp)
    227 	struct ifnet *ifp;
    228 {
    229 
    230 	/* Nothing. */
    231 }
    232 
    233 void
    234 if_nulldrain(ifp)
    235 	struct ifnet *ifp;
    236 {
    237 
    238 	/* Nothing. */
    239 }
    240 
    241 int if_index = 0;
    242 struct ifaddr **ifnet_addrs = NULL;
    243 struct ifnet **ifindex2ifnet = NULL;
    244 
    245 /*
    246  * Attach an interface to the
    247  * list of "active" interfaces.
    248  */
    249 void
    250 if_attach(ifp)
    251 	struct ifnet *ifp;
    252 {
    253 	unsigned socksize, ifasize;
    254 	int namelen, masklen;
    255 	register struct sockaddr_dl *sdl;
    256 	register struct ifaddr *ifa;
    257 	static size_t if_indexlim = 8;
    258 
    259 	if (if_index == 0)
    260 		TAILQ_INIT(&ifnet);
    261 	TAILQ_INIT(&ifp->if_addrlist);
    262 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
    263 	ifp->if_index = ++if_index;
    264 
    265 	/*
    266 	 * We have some arrays that should be indexed by if_index.
    267 	 * since if_index will grow dynamically, they should grow too.
    268 	 *	struct ifadd **ifnet_addrs
    269 	 *	struct ifnet **ifindex2ifnet
    270 	 */
    271 	if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
    272 	    ifp->if_index >= if_indexlim) {
    273 		size_t n;
    274 		caddr_t q;
    275 
    276 		while (ifp->if_index >= if_indexlim)
    277 			if_indexlim <<= 1;
    278 
    279 		/* grow ifnet_addrs */
    280 		n = if_indexlim * sizeof(ifa);
    281 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    282 		bzero(q, n);
    283 		if (ifnet_addrs) {
    284 			bcopy((caddr_t)ifnet_addrs, q, n/2);
    285 			free((caddr_t)ifnet_addrs, M_IFADDR);
    286 		}
    287 		ifnet_addrs = (struct ifaddr **)q;
    288 
    289 		/* grow ifindex2ifnet */
    290 		n = if_indexlim * sizeof(struct ifnet *);
    291 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    292 		bzero(q, n);
    293 		if (ifindex2ifnet) {
    294 			bcopy((caddr_t)ifindex2ifnet, q, n/2);
    295 			free((caddr_t)ifindex2ifnet, M_IFADDR);
    296 		}
    297 		ifindex2ifnet = (struct ifnet **)q;
    298 	}
    299 
    300 	ifindex2ifnet[ifp->if_index] = ifp;
    301 
    302 	/*
    303 	 * create a Link Level name for this device
    304 	 */
    305 	namelen = strlen(ifp->if_xname);
    306 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
    307 	socksize = masklen + ifp->if_addrlen;
    308 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
    309 	if (socksize < sizeof(*sdl))
    310 		socksize = sizeof(*sdl);
    311 	socksize = ROUNDUP(socksize);
    312 	ifasize = sizeof(*ifa) + 2 * socksize;
    313 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
    314 	bzero((caddr_t)ifa, ifasize);
    315 	sdl = (struct sockaddr_dl *)(ifa + 1);
    316 	sdl->sdl_len = socksize;
    317 	sdl->sdl_family = AF_LINK;
    318 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
    319 	sdl->sdl_nlen = namelen;
    320 	sdl->sdl_index = ifp->if_index;
    321 	sdl->sdl_type = ifp->if_type;
    322 	ifnet_addrs[ifp->if_index] = ifa;
    323 	IFAREF(ifa);
    324 	ifa->ifa_ifp = ifp;
    325 	ifa->ifa_rtrequest = link_rtrequest;
    326 	TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
    327 	IFAREF(ifa);
    328 	ifa->ifa_addr = (struct sockaddr *)sdl;
    329 	ifp->if_sadl = sdl;
    330 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
    331 	ifa->ifa_netmask = (struct sockaddr *)sdl;
    332 	sdl->sdl_len = masklen;
    333 	while (namelen != 0)
    334 		sdl->sdl_data[--namelen] = 0xff;
    335 	if (ifp->if_snd.ifq_maxlen == 0)
    336 	    ifp->if_snd.ifq_maxlen = ifqmaxlen;
    337 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    338 
    339 	ifp->if_link_state = LINK_STATE_UNKNOWN;
    340 
    341 	/* Announce the interface. */
    342 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
    343 }
    344 
    345 /*
    346  * Deactivate an interface.  This points all of the procedure
    347  * handles at error stubs.  May be called from interrupt context.
    348  */
    349 void
    350 if_deactivate(ifp)
    351 	struct ifnet *ifp;
    352 {
    353 	int s;
    354 
    355 	s = splimp();
    356 
    357 	ifp->if_output	 = if_nulloutput;
    358 	ifp->if_input	 = if_nullinput;
    359 	ifp->if_start	 = if_nullstart;
    360 	ifp->if_ioctl	 = if_nullioctl;
    361 	ifp->if_reset	 = if_nullreset;
    362 	ifp->if_watchdog = if_nullwatchdog;
    363 	ifp->if_drain	 = if_nulldrain;
    364 
    365 	/* No more packets may be enqueued. */
    366 	ifp->if_snd.ifq_maxlen = 0;
    367 
    368 	splx(s);
    369 }
    370 
    371 /*
    372  * Detach an interface from the list of "active" interfaces,
    373  * freeing any resources as we go along.
    374  *
    375  * NOTE: This routine must be called with a valid thread context,
    376  * as it may block.
    377  */
    378 void
    379 if_detach(ifp)
    380 	struct ifnet *ifp;
    381 {
    382 	struct socket so;
    383 	struct ifaddr *ifa;
    384 #ifdef IFAREF_DEBUG
    385 	struct ifaddr *last_ifa = NULL;
    386 #endif
    387 	struct domain *dp;
    388 	struct protosw *pr;
    389 	struct radix_node_head *rnh;
    390 	int s, i, family, purged;
    391 
    392 	/*
    393 	 * XXX It's kind of lame that we have to have the
    394 	 * XXX socket structure...
    395 	 */
    396 	memset(&so, 0, sizeof(so));
    397 
    398 	s = splimp();
    399 
    400 	/*
    401 	 * Do an if_down() to give protocols a chance to do something.
    402 	 */
    403 	if_down(ifp);
    404 
    405 	/*
    406 	 * Rip all the addresses off the interface.  This should make
    407 	 * all of the routes go away.
    408 	 */
    409 	while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
    410 		family = ifa->ifa_addr->sa_family;
    411 #ifdef IFAREF_DEBUG
    412 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
    413 		    ifa, family, ifa->ifa_refcnt);
    414 		if (last_ifa != NULL && ifa == last_ifa)
    415 			panic("if_detach: loop detected");
    416 		last_ifa = ifa;
    417 #endif
    418 		if (family == AF_LINK) {
    419 			rtinit(ifa, RTM_DELETE, 0);
    420 			TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
    421 			IFAFREE(ifa);
    422 		} else {
    423 			dp = pffinddomain(family);
    424 #ifdef DIAGNOSTIC
    425 			if (dp == NULL)
    426 				panic("if_detach: no domain for AF %d\n",
    427 				    family);
    428 #endif
    429 			purged = 0;
    430 			for (pr = dp->dom_protosw;
    431 			     pr < dp->dom_protoswNPROTOSW; pr++) {
    432 				so.so_proto = pr;
    433 				if (pr->pr_usrreq != NULL) {
    434 					(void) (*pr->pr_usrreq)(&so,
    435 					    PRU_PURGEIF, NULL, NULL,
    436 					    (struct mbuf *) ifp, curproc);
    437 					purged = 1;
    438 				}
    439 			}
    440 			if (purged == 0) {
    441 				/*
    442 				 * XXX What's really the best thing to do
    443 				 * XXX here?  --thorpej (at) netbsd.org
    444 				 */
    445 				printf("if_detach: WARNING: AF %d not purged\n",
    446 				    family);
    447 			}
    448 		}
    449 	}
    450 
    451 	/* Walk the routing table looking for straglers. */
    452 	for (i = 0; i <= AF_MAX; i++) {
    453 		if ((rnh = rt_tables[i]) != NULL)
    454 			(void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
    455 	}
    456 
    457 	IFAFREE(ifnet_addrs[ifp->if_index]);
    458 	ifnet_addrs[ifp->if_index] = NULL;
    459 
    460 	/* Announce that the interface is gone. */
    461 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
    462 
    463 	TAILQ_REMOVE(&ifnet, ifp, if_list);
    464 
    465 	splx(s);
    466 }
    467 
    468 /*
    469  * Callback for a radix tree walk to delete all references to an
    470  * ifnet.
    471  */
    472 int
    473 if_rt_walktree(rn, v)
    474 	struct radix_node *rn;
    475 	void *v;
    476 {
    477 	struct ifnet *ifp = (struct ifnet *)v;
    478 	struct rtentry *rt = (struct rtentry *)rn;
    479 	int error;
    480 
    481 	if (rt->rt_ifp == ifp) {
    482 		/* Delete the entry. */
    483 		error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
    484 		    rt_mask(rt), rt->rt_flags, NULL);
    485 		if (error)
    486 			printf("%s: warning: unable to delete rtentry @ %p, "
    487 			    "error = %d\n", ifp->if_xname, rt, error);
    488 	}
    489 	return (0);
    490 }
    491 
    492 /*
    493  * Locate an interface based on a complete address.
    494  */
    495 /*ARGSUSED*/
    496 struct ifaddr *
    497 ifa_ifwithaddr(addr)
    498 	register struct sockaddr *addr;
    499 {
    500 	register struct ifnet *ifp;
    501 	register struct ifaddr *ifa;
    502 
    503 #define	equal(a1, a2) \
    504   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
    505 
    506 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    507 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    508 		if (ifp->if_output == if_nulloutput)
    509 			continue;
    510 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    511 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    512 			if (ifa->ifa_addr->sa_family != addr->sa_family)
    513 				continue;
    514 			if (equal(addr, ifa->ifa_addr))
    515 				return (ifa);
    516 			if ((ifp->if_flags & IFF_BROADCAST) &&
    517 			    ifa->ifa_broadaddr &&
    518 			    /* IP6 doesn't have broadcast */
    519 			    ifa->ifa_broadaddr->sa_len != 0 &&
    520 			    equal(ifa->ifa_broadaddr, addr))
    521 				return (ifa);
    522 		}
    523 	}
    524 	return (NULL);
    525 }
    526 
    527 /*
    528  * Locate the point to point interface with a given destination address.
    529  */
    530 /*ARGSUSED*/
    531 struct ifaddr *
    532 ifa_ifwithdstaddr(addr)
    533 	register struct sockaddr *addr;
    534 {
    535 	register struct ifnet *ifp;
    536 	register struct ifaddr *ifa;
    537 
    538 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    539 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    540 		if (ifp->if_output == if_nulloutput)
    541 			continue;
    542 		if (ifp->if_flags & IFF_POINTOPOINT) {
    543 			for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    544 			     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    545 				if (ifa->ifa_addr->sa_family !=
    546 				      addr->sa_family ||
    547 				    ifa->ifa_dstaddr == NULL)
    548 					continue;
    549 				if (equal(addr, ifa->ifa_dstaddr))
    550 					return (ifa);
    551 			}
    552 		}
    553 	}
    554 	return (NULL);
    555 }
    556 
    557 /*
    558  * Find an interface on a specific network.  If many, choice
    559  * is most specific found.
    560  */
    561 struct ifaddr *
    562 ifa_ifwithnet(addr)
    563 	struct sockaddr *addr;
    564 {
    565 	register struct ifnet *ifp;
    566 	register struct ifaddr *ifa;
    567 	register struct sockaddr_dl *sdl;
    568 	struct ifaddr *ifa_maybe = 0;
    569 	u_int af = addr->sa_family;
    570 	char *addr_data = addr->sa_data, *cplim;
    571 
    572 	if (af == AF_LINK) {
    573 		sdl = (struct sockaddr_dl *)addr;
    574 		if (sdl->sdl_index && sdl->sdl_index <= if_index &&
    575 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
    576 			return (ifnet_addrs[sdl->sdl_index]);
    577 	}
    578 #ifdef NETATALK
    579 	if (af == AF_APPLETALK) {
    580 		for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    581 		     ifp = TAILQ_NEXT(ifp, if_list)) {
    582 			if (ifp->if_output == if_nulloutput)
    583 				continue;
    584 			ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp);
    585 			if (ifa)
    586 				return (ifa);
    587 		}
    588 		return (NULL);
    589 	}
    590 #endif
    591 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    592 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    593 		if (ifp->if_output == if_nulloutput)
    594 			continue;
    595 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    596 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    597 			register char *cp, *cp2, *cp3;
    598 
    599 			if (ifa->ifa_addr->sa_family != af ||
    600 			    ifa->ifa_netmask == 0)
    601  next:				continue;
    602 			cp = addr_data;
    603 			cp2 = ifa->ifa_addr->sa_data;
    604 			cp3 = ifa->ifa_netmask->sa_data;
    605 			cplim = (char *)ifa->ifa_netmask +
    606 			    ifa->ifa_netmask->sa_len;
    607 			while (cp3 < cplim) {
    608 				if ((*cp++ ^ *cp2++) & *cp3++) {
    609 					/* want to continue for() loop */
    610 					goto next;
    611 				}
    612 			}
    613 			if (ifa_maybe == 0 ||
    614 			    rn_refines((caddr_t)ifa->ifa_netmask,
    615 			    (caddr_t)ifa_maybe->ifa_netmask))
    616 				ifa_maybe = ifa;
    617 		}
    618 	}
    619 	return (ifa_maybe);
    620 }
    621 
    622 /*
    623  * Find the interface of the addresss.
    624  */
    625 struct ifaddr *
    626 ifa_ifwithladdr(addr)
    627 	struct sockaddr *addr;
    628 {
    629 	struct ifaddr *ia;
    630 
    631 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
    632 	    (ia = ifa_ifwithnet(addr)))
    633 		return (ia);
    634 	return (NULL);
    635 }
    636 
    637 /*
    638  * Find an interface using a specific address family
    639  */
    640 struct ifaddr *
    641 ifa_ifwithaf(af)
    642 	register int af;
    643 {
    644 	register struct ifnet *ifp;
    645 	register struct ifaddr *ifa;
    646 
    647 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    648 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    649 		if (ifp->if_output == if_nulloutput)
    650 			continue;
    651 		for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    652 		     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    653 			if (ifa->ifa_addr->sa_family == af)
    654 				return (ifa);
    655 		}
    656 	}
    657 	return (NULL);
    658 }
    659 
    660 /*
    661  * Find an interface address specific to an interface best matching
    662  * a given address.
    663  */
    664 struct ifaddr *
    665 ifaof_ifpforaddr(addr, ifp)
    666 	struct sockaddr *addr;
    667 	register struct ifnet *ifp;
    668 {
    669 	register struct ifaddr *ifa;
    670 	register char *cp, *cp2, *cp3;
    671 	register char *cplim;
    672 	struct ifaddr *ifa_maybe = 0;
    673 	u_int af = addr->sa_family;
    674 
    675 	if (ifp->if_output == if_nulloutput)
    676 		return (NULL);
    677 
    678 	if (af >= AF_MAX)
    679 		return (NULL);
    680 
    681 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    682 	     ifa = TAILQ_NEXT(ifa, ifa_list)) {
    683 		if (ifa->ifa_addr->sa_family != af)
    684 			continue;
    685 		ifa_maybe = ifa;
    686 		if (ifa->ifa_netmask == 0) {
    687 			if (equal(addr, ifa->ifa_addr) ||
    688 			    (ifa->ifa_dstaddr &&
    689 			     equal(addr, ifa->ifa_dstaddr)))
    690 				return (ifa);
    691 			continue;
    692 		}
    693 		cp = addr->sa_data;
    694 		cp2 = ifa->ifa_addr->sa_data;
    695 		cp3 = ifa->ifa_netmask->sa_data;
    696 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
    697 		for (; cp3 < cplim; cp3++) {
    698 			if ((*cp++ ^ *cp2++) & *cp3)
    699 				break;
    700 		}
    701 		if (cp3 == cplim)
    702 			return (ifa);
    703 	}
    704 	return (ifa_maybe);
    705 }
    706 
    707 /*
    708  * Default action when installing a route with a Link Level gateway.
    709  * Lookup an appropriate real ifa to point to.
    710  * This should be moved to /sys/net/link.c eventually.
    711  */
    712 void
    713 link_rtrequest(cmd, rt, sa)
    714 	int cmd;
    715 	register struct rtentry *rt;
    716 	struct sockaddr *sa;
    717 {
    718 	register struct ifaddr *ifa;
    719 	struct sockaddr *dst;
    720 	struct ifnet *ifp;
    721 
    722 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
    723 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
    724 		return;
    725 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
    726 		IFAFREE(rt->rt_ifa);
    727 		rt->rt_ifa = ifa;
    728 		IFAREF(ifa);
    729 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
    730 			ifa->ifa_rtrequest(cmd, rt, sa);
    731 	}
    732 }
    733 
    734 /*
    735  * Mark an interface down and notify protocols of
    736  * the transition.
    737  * NOTE: must be called at splsoftnet or equivalent.
    738  */
    739 void
    740 if_down(ifp)
    741 	register struct ifnet *ifp;
    742 {
    743 	register struct ifaddr *ifa;
    744 
    745 	ifp->if_flags &= ~IFF_UP;
    746 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    747 	     ifa = TAILQ_NEXT(ifa, ifa_list))
    748 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
    749 	if_qflush(&ifp->if_snd);
    750 	rt_ifmsg(ifp);
    751 }
    752 
    753 /*
    754  * Mark an interface up and notify protocols of
    755  * the transition.
    756  * NOTE: must be called at splsoftnet or equivalent.
    757  */
    758 void
    759 if_up(ifp)
    760 	register struct ifnet *ifp;
    761 {
    762 #ifdef notyet
    763 	register struct ifaddr *ifa;
    764 #endif
    765 
    766 	ifp->if_flags |= IFF_UP;
    767 #ifdef notyet
    768 	/* this has no effect on IP, and will kill all ISO connections XXX */
    769 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
    770 	     ifa = TAILQ_NEXT(ifa, ifa_list))
    771 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
    772 #endif
    773 	rt_ifmsg(ifp);
    774 #ifdef INET6
    775 	in6_if_up(ifp);
    776 #endif
    777 }
    778 
    779 /*
    780  * Flush an interface queue.
    781  */
    782 void
    783 if_qflush(ifq)
    784 	register struct ifqueue *ifq;
    785 {
    786 	register struct mbuf *m, *n;
    787 
    788 	n = ifq->ifq_head;
    789 	while ((m = n) != NULL) {
    790 		n = m->m_act;
    791 		m_freem(m);
    792 	}
    793 	ifq->ifq_head = 0;
    794 	ifq->ifq_tail = 0;
    795 	ifq->ifq_len = 0;
    796 }
    797 
    798 /*
    799  * Handle interface watchdog timer routines.  Called
    800  * from softclock, we decrement timers (if set) and
    801  * call the appropriate interface routine on expiration.
    802  */
    803 void
    804 if_slowtimo(arg)
    805 	void *arg;
    806 {
    807 	register struct ifnet *ifp;
    808 	int s = splimp();
    809 
    810 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    811 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    812 		if (ifp->if_timer == 0 || --ifp->if_timer)
    813 			continue;
    814 		if (ifp->if_watchdog)
    815 			(*ifp->if_watchdog)(ifp);
    816 	}
    817 	splx(s);
    818 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
    819 	    if_slowtimo, NULL);
    820 }
    821 
    822 /*
    823  * Map interface name to
    824  * interface structure pointer.
    825  */
    826 struct ifnet *
    827 ifunit(name)
    828 	const char *name;
    829 {
    830 	register struct ifnet *ifp;
    831 
    832 	for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
    833 	     ifp = TAILQ_NEXT(ifp, if_list)) {
    834 		if (ifp->if_output == if_nulloutput)
    835 			continue;
    836 	 	if (strcmp(ifp->if_xname, name) == 0)
    837 			return (ifp);
    838 	}
    839 	return (NULL);
    840 }
    841 
    842 /*
    843  * Interface ioctls.
    844  */
    845 int
    846 ifioctl(so, cmd, data, p)
    847 	struct socket *so;
    848 	u_long cmd;
    849 	caddr_t data;
    850 	struct proc *p;
    851 {
    852 	register struct ifnet *ifp;
    853 	register struct ifreq *ifr;
    854 	int error = 0;
    855 	short oif_flags;
    856 
    857 	switch (cmd) {
    858 
    859 	case SIOCGIFCONF:
    860 	case OSIOCGIFCONF:
    861 		return (ifconf(cmd, data));
    862 	}
    863 	ifr = (struct ifreq *)data;
    864 	ifp = ifunit(ifr->ifr_name);
    865 	if (ifp == 0)
    866 		return (ENXIO);
    867 	oif_flags = ifp->if_flags;
    868 	switch (cmd) {
    869 
    870 	case SIOCGIFFLAGS:
    871 		ifr->ifr_flags = ifp->if_flags;
    872 		break;
    873 
    874 	case SIOCGIFMETRIC:
    875 		ifr->ifr_metric = ifp->if_metric;
    876 		break;
    877 
    878 	case SIOCGIFMTU:
    879 		ifr->ifr_mtu = ifp->if_mtu;
    880 		break;
    881 
    882 	case SIOCSIFFLAGS:
    883 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    884 			return (error);
    885 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
    886 			int s = splimp();
    887 			if_down(ifp);
    888 			splx(s);
    889 		}
    890 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
    891 			int s = splimp();
    892 			if_up(ifp);
    893 			splx(s);
    894 		}
    895 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
    896 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
    897 		if (ifp->if_ioctl)
    898 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
    899 		break;
    900 
    901 	case SIOCSIFMETRIC:
    902 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    903 			return (error);
    904 		ifp->if_metric = ifr->ifr_metric;
    905 		break;
    906 
    907 	case SIOCSIFMTU:
    908 	{
    909 		u_long oldmtu = ifp->if_mtu;
    910 
    911 		error = suser(p->p_ucred, &p->p_acflag);
    912 		if (error)
    913 			return (error);
    914 		if (ifp->if_ioctl == NULL)
    915 			return (EOPNOTSUPP);
    916 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    917 
    918 		/*
    919 		 * If the link MTU changed, do network layer specific procedure.
    920 		 */
    921 		if (ifp->if_mtu != oldmtu) {
    922 #ifdef INET6
    923 			nd6_setmtu(ifp);
    924 #endif
    925 		}
    926 		break;
    927 	}
    928 	case SIOCADDMULTI:
    929 	case SIOCDELMULTI:
    930 	case SIOCSIFMEDIA:
    931 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    932 			return (error);
    933 		/* FALLTHROUGH */
    934 	case SIOCGIFMEDIA:
    935 		if (ifp->if_ioctl == 0)
    936 			return (EOPNOTSUPP);
    937 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    938 		break;
    939 
    940 	case SIOCSDRVSPEC:
    941 		/* XXX:  need to pass proc pointer through to driver... */
    942 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    943 			return (error);
    944 	/* FALLTHROUGH */
    945 	default:
    946 		if (so->so_proto == 0)
    947 			return (EOPNOTSUPP);
    948 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4)
    949 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
    950 		    (struct mbuf *)cmd, (struct mbuf *)data,
    951 		    (struct mbuf *)ifp, p));
    952 #else
    953 	    {
    954 		int ocmd = cmd;
    955 
    956 		switch (cmd) {
    957 
    958 		case SIOCSIFADDR:
    959 		case SIOCSIFDSTADDR:
    960 		case SIOCSIFBRDADDR:
    961 		case SIOCSIFNETMASK:
    962 #if BYTE_ORDER != BIG_ENDIAN
    963 			if (ifr->ifr_addr.sa_family == 0 &&
    964 			    ifr->ifr_addr.sa_len < 16) {
    965 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
    966 				ifr->ifr_addr.sa_len = 16;
    967 			}
    968 #else
    969 			if (ifr->ifr_addr.sa_len == 0)
    970 				ifr->ifr_addr.sa_len = 16;
    971 #endif
    972 			break;
    973 
    974 		case OSIOCGIFADDR:
    975 			cmd = SIOCGIFADDR;
    976 			break;
    977 
    978 		case OSIOCGIFDSTADDR:
    979 			cmd = SIOCGIFDSTADDR;
    980 			break;
    981 
    982 		case OSIOCGIFBRDADDR:
    983 			cmd = SIOCGIFBRDADDR;
    984 			break;
    985 
    986 		case OSIOCGIFNETMASK:
    987 			cmd = SIOCGIFNETMASK;
    988 		}
    989 
    990 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
    991 		    (struct mbuf *)cmd, (struct mbuf *)data,
    992 		    (struct mbuf *)ifp, p));
    993 
    994 		switch (ocmd) {
    995 		case OSIOCGIFADDR:
    996 		case OSIOCGIFDSTADDR:
    997 		case OSIOCGIFBRDADDR:
    998 		case OSIOCGIFNETMASK:
    999 			*(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
   1000 		}
   1001 	    }
   1002 #endif /* COMPAT_43 */
   1003 		break;
   1004 	}
   1005 
   1006 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
   1007 #ifdef INET6
   1008 		if ((ifp->if_flags & IFF_UP) != 0) {
   1009 			int s = splimp();
   1010 			in6_if_up(ifp);
   1011 			splx(s);
   1012 		}
   1013 #endif
   1014 	}
   1015 
   1016 	return (error);
   1017 }
   1018 
   1019 /*
   1020  * Return interface configuration
   1021  * of system.  List may be used
   1022  * in later ioctl's (above) to get
   1023  * other information.
   1024  */
   1025 /*ARGSUSED*/
   1026 int
   1027 ifconf(cmd, data)
   1028 	u_long cmd;
   1029 	caddr_t data;
   1030 {
   1031 	register struct ifconf *ifc = (struct ifconf *)data;
   1032 	register struct ifnet *ifp;
   1033 	register struct ifaddr *ifa;
   1034 	struct ifreq ifr, *ifrp;
   1035 	int space = ifc->ifc_len, error = 0;
   1036 
   1037 	ifrp = ifc->ifc_req;
   1038 	for (ifp = ifnet.tqh_first;
   1039 	    space >= sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
   1040 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
   1041 		if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
   1042 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
   1043 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1044 			    sizeof(ifr));
   1045 			if (error)
   1046 				break;
   1047 			space -= sizeof (ifr), ifrp++;
   1048 		} else
   1049 		    for (; space >= sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
   1050 			register struct sockaddr *sa = ifa->ifa_addr;
   1051 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
   1052 			if (cmd == OSIOCGIFCONF) {
   1053 				struct osockaddr *osa =
   1054 					 (struct osockaddr *)&ifr.ifr_addr;
   1055 				ifr.ifr_addr = *sa;
   1056 				osa->sa_family = sa->sa_family;
   1057 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1058 						sizeof (ifr));
   1059 				ifrp++;
   1060 			} else
   1061 #endif
   1062 			if (sa->sa_len <= sizeof(*sa)) {
   1063 				ifr.ifr_addr = *sa;
   1064 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1065 						sizeof (ifr));
   1066 				ifrp++;
   1067 			} else {
   1068 				space -= sa->sa_len - sizeof(*sa);
   1069 				if (space < sizeof (ifr))
   1070 					break;
   1071 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
   1072 						sizeof (ifr.ifr_name));
   1073 				if (error == 0)
   1074 				    error = copyout((caddr_t)sa,
   1075 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
   1076 				ifrp = (struct ifreq *)
   1077 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
   1078 			}
   1079 			if (error)
   1080 				break;
   1081 			space -= sizeof (ifr);
   1082 		}
   1083 	}
   1084 	ifc->ifc_len -= space;
   1085 	return (error);
   1086 }
   1087