Home | History | Annotate | Line # | Download | only in net
if.c revision 1.49
      1  1.49    itojun /*	$NetBSD: if.c,v 1.49 1999/07/01 08:12:47 itojun Exp $	*/
      2  1.49    itojun 
      3  1.49    itojun /*
      4  1.49    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  1.49    itojun  * All rights reserved.
      6  1.49    itojun  *
      7  1.49    itojun  * Redistribution and use in source and binary forms, with or without
      8  1.49    itojun  * modification, are permitted provided that the following conditions
      9  1.49    itojun  * are met:
     10  1.49    itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.49    itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.49    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.49    itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.49    itojun  *    documentation and/or other materials provided with the distribution.
     15  1.49    itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.49    itojun  *    may be used to endorse or promote products derived from this software
     17  1.49    itojun  *    without specific prior written permission.
     18  1.49    itojun  *
     19  1.49    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.49    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.49    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.49    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.49    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.49    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.49    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.49    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.49    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.49    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.49    itojun  * SUCH DAMAGE.
     30  1.49    itojun  */
     31  1.16       cgd 
     32   1.1       cgd /*
     33  1.15   mycroft  * Copyright (c) 1980, 1986, 1993
     34  1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
     35   1.1       cgd  *
     36   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37   1.1       cgd  * modification, are permitted provided that the following conditions
     38   1.1       cgd  * are met:
     39   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     45   1.1       cgd  *    must display the following acknowledgement:
     46   1.1       cgd  *	This product includes software developed by the University of
     47   1.1       cgd  *	California, Berkeley and its contributors.
     48   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     49   1.1       cgd  *    may be used to endorse or promote products derived from this software
     50   1.1       cgd  *    without specific prior written permission.
     51   1.1       cgd  *
     52   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.1       cgd  * SUCH DAMAGE.
     63   1.1       cgd  *
     64  1.44      fvdl  *	@(#)if.c	8.5 (Berkeley) 1/9/95
     65   1.1       cgd  */
     66  1.46   thorpej 
     67  1.46   thorpej #include "opt_compat_linux.h"
     68  1.47   thorpej #include "opt_compat_svr4.h"
     69  1.48  christos #include "opt_compat_43.h"
     70   1.1       cgd 
     71   1.8   mycroft #include <sys/param.h>
     72   1.8   mycroft #include <sys/mbuf.h>
     73   1.8   mycroft #include <sys/systm.h>
     74  1.15   mycroft #include <sys/proc.h>
     75   1.8   mycroft #include <sys/socket.h>
     76   1.8   mycroft #include <sys/socketvar.h>
     77   1.8   mycroft #include <sys/protosw.h>
     78   1.8   mycroft #include <sys/kernel.h>
     79   1.8   mycroft #include <sys/ioctl.h>
     80   1.1       cgd 
     81   1.8   mycroft #include <net/if.h>
     82   1.8   mycroft #include <net/if_dl.h>
     83   1.8   mycroft #include <net/if_types.h>
     84  1.24  christos #include <net/radix.h>
     85   1.1       cgd 
     86  1.49    itojun #ifdef INET6
     87  1.49    itojun /*XXX*/
     88  1.49    itojun #include <netinet/in.h>
     89  1.49    itojun #endif
     90  1.49    itojun 
     91   1.1       cgd int	ifqmaxlen = IFQ_MAXLEN;
     92  1.15   mycroft void	if_slowtimo __P((void *arg));
     93   1.4    andrew 
     94  1.49    itojun #ifdef INET6
     95  1.49    itojun /*
     96  1.49    itojun  * XXX: declare here to avoid to include many inet6 related files..
     97  1.49    itojun  * should be more generalized?
     98  1.49    itojun  */
     99  1.49    itojun extern void nd6_setmtu __P((struct ifnet *));
    100  1.49    itojun #endif
    101  1.49    itojun 
    102   1.1       cgd /*
    103   1.1       cgd  * Network interface utility routines.
    104   1.1       cgd  *
    105   1.1       cgd  * Routines with ifa_ifwith* names take sockaddr *'s as
    106   1.1       cgd  * parameters.
    107   1.1       cgd  */
    108   1.4    andrew void
    109   1.1       cgd ifinit()
    110   1.1       cgd {
    111   1.1       cgd 
    112   1.4    andrew 	if_slowtimo(NULL);
    113   1.1       cgd }
    114   1.1       cgd 
    115   1.1       cgd int if_index = 0;
    116  1.49    itojun struct ifaddr **ifnet_addrs = NULL;
    117  1.49    itojun struct ifnet **ifindex2ifnet = NULL;
    118   1.1       cgd 
    119   1.1       cgd /*
    120   1.1       cgd  * Attach an interface to the
    121   1.1       cgd  * list of "active" interfaces.
    122   1.1       cgd  */
    123  1.15   mycroft void
    124   1.1       cgd if_attach(ifp)
    125   1.1       cgd 	struct ifnet *ifp;
    126   1.1       cgd {
    127   1.1       cgd 	unsigned socksize, ifasize;
    128  1.34   thorpej 	int namelen, masklen;
    129   1.1       cgd 	register struct sockaddr_dl *sdl;
    130   1.1       cgd 	register struct ifaddr *ifa;
    131  1.49    itojun 	static size_t if_indexlim = 8;
    132   1.1       cgd 
    133  1.22   mycroft 	if (if_index == 0)
    134  1.22   mycroft 		TAILQ_INIT(&ifnet);
    135  1.22   mycroft 	TAILQ_INIT(&ifp->if_addrlist);
    136  1.21   mycroft 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
    137   1.1       cgd 	ifp->if_index = ++if_index;
    138  1.49    itojun 
    139  1.49    itojun 	/*
    140  1.49    itojun 	 * We have some arrays that should be indexed by if_index.
    141  1.49    itojun 	 * since if_index will grow dynamically, they should grow too.
    142  1.49    itojun 	 *	struct ifadd **ifnet_addrs
    143  1.49    itojun 	 *	struct ifnet **ifindex2ifnet
    144  1.49    itojun 	 */
    145  1.49    itojun 	if (ifnet_addrs == 0 || ifindex2ifnet == 0 || if_index >= if_indexlim) {
    146  1.49    itojun 		size_t n;
    147  1.49    itojun 		caddr_t q;
    148  1.49    itojun 
    149  1.49    itojun 		while (if_index >= if_indexlim)
    150  1.49    itojun 			if_indexlim <<= 1;
    151  1.49    itojun 
    152  1.49    itojun 		/* grow ifnet_addrs */
    153  1.49    itojun 		n = if_indexlim * sizeof(ifa);
    154  1.49    itojun 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    155  1.49    itojun 		bzero(q, n);
    156   1.1       cgd 		if (ifnet_addrs) {
    157  1.49    itojun 			bcopy((caddr_t)ifnet_addrs, q, n/2);
    158   1.1       cgd 			free((caddr_t)ifnet_addrs, M_IFADDR);
    159   1.1       cgd 		}
    160  1.49    itojun 		ifnet_addrs = (struct ifaddr **)q;
    161  1.49    itojun 
    162  1.49    itojun 		/* grow ifindex2ifnet */
    163  1.49    itojun 		n = if_indexlim * sizeof(struct ifnet *);
    164  1.49    itojun 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
    165  1.49    itojun 		bzero(q, n);
    166  1.49    itojun 		if (ifindex2ifnet) {
    167  1.49    itojun 			bcopy((caddr_t)ifindex2ifnet, q, n/2);
    168  1.49    itojun 			free((caddr_t)ifindex2ifnet, M_IFADDR);
    169  1.49    itojun 		}
    170  1.49    itojun 		ifindex2ifnet = (struct ifnet **)q;
    171   1.1       cgd 	}
    172  1.49    itojun 
    173  1.49    itojun 	ifindex2ifnet[if_index] = ifp;
    174  1.49    itojun 
    175   1.1       cgd 	/*
    176   1.1       cgd 	 * create a Link Level name for this device
    177   1.1       cgd 	 */
    178  1.34   thorpej 	namelen = strlen(ifp->if_xname);
    179  1.43   thorpej 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
    180  1.15   mycroft 	socksize = masklen + ifp->if_addrlen;
    181   1.1       cgd #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
    182   1.1       cgd 	if (socksize < sizeof(*sdl))
    183   1.1       cgd 		socksize = sizeof(*sdl);
    184  1.20       cgd 	socksize = ROUNDUP(socksize);
    185   1.1       cgd 	ifasize = sizeof(*ifa) + 2 * socksize;
    186   1.1       cgd 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
    187   1.1       cgd 	bzero((caddr_t)ifa, ifasize);
    188   1.1       cgd 	sdl = (struct sockaddr_dl *)(ifa + 1);
    189   1.1       cgd 	sdl->sdl_len = socksize;
    190   1.1       cgd 	sdl->sdl_family = AF_LINK;
    191  1.34   thorpej 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
    192  1.34   thorpej 	sdl->sdl_nlen = namelen;
    193   1.1       cgd 	sdl->sdl_index = ifp->if_index;
    194  1.15   mycroft 	sdl->sdl_type = ifp->if_type;
    195  1.49    itojun 	ifnet_addrs[if_index] = ifa;
    196  1.15   mycroft 	ifa->ifa_ifp = ifp;
    197  1.15   mycroft 	ifa->ifa_rtrequest = link_rtrequest;
    198  1.21   mycroft 	TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
    199  1.15   mycroft 	ifa->ifa_addr = (struct sockaddr *)sdl;
    200  1.38        is 	ifp->if_sadl = sdl;
    201   1.1       cgd 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
    202   1.1       cgd 	ifa->ifa_netmask = (struct sockaddr *)sdl;
    203  1.15   mycroft 	sdl->sdl_len = masklen;
    204   1.1       cgd 	while (namelen != 0)
    205   1.1       cgd 		sdl->sdl_data[--namelen] = 0xff;
    206  1.40   thorpej 	if (ifp->if_snd.ifq_maxlen == 0)
    207  1.40   thorpej 	    ifp->if_snd.ifq_maxlen = ifqmaxlen;
    208  1.42        is 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
    209   1.1       cgd }
    210   1.1       cgd /*
    211   1.1       cgd  * Locate an interface based on a complete address.
    212   1.1       cgd  */
    213   1.1       cgd /*ARGSUSED*/
    214   1.1       cgd struct ifaddr *
    215   1.1       cgd ifa_ifwithaddr(addr)
    216   1.1       cgd 	register struct sockaddr *addr;
    217   1.1       cgd {
    218   1.1       cgd 	register struct ifnet *ifp;
    219   1.1       cgd 	register struct ifaddr *ifa;
    220   1.1       cgd 
    221   1.1       cgd #define	equal(a1, a2) \
    222   1.1       cgd   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
    223  1.21   mycroft 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
    224  1.21   mycroft 	    for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
    225   1.1       cgd 		if (ifa->ifa_addr->sa_family != addr->sa_family)
    226   1.1       cgd 			continue;
    227   1.1       cgd 		if (equal(addr, ifa->ifa_addr))
    228   1.1       cgd 			return (ifa);
    229   1.1       cgd 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
    230  1.49    itojun 		    /* IP6 doesn't have broadcast */
    231  1.49    itojun 		    ifa->ifa_broadaddr->sa_len != 0 &&
    232   1.1       cgd 		    equal(ifa->ifa_broadaddr, addr))
    233   1.1       cgd 			return (ifa);
    234   1.1       cgd 	}
    235   1.1       cgd 	return ((struct ifaddr *)0);
    236   1.1       cgd }
    237  1.49    itojun 
    238   1.1       cgd /*
    239   1.1       cgd  * Locate the point to point interface with a given destination address.
    240   1.1       cgd  */
    241   1.1       cgd /*ARGSUSED*/
    242   1.1       cgd struct ifaddr *
    243   1.1       cgd ifa_ifwithdstaddr(addr)
    244   1.1       cgd 	register struct sockaddr *addr;
    245   1.1       cgd {
    246   1.1       cgd 	register struct ifnet *ifp;
    247   1.1       cgd 	register struct ifaddr *ifa;
    248   1.1       cgd 
    249  1.21   mycroft 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
    250   1.1       cgd 	    if (ifp->if_flags & IFF_POINTOPOINT)
    251  1.21   mycroft 		for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
    252  1.30       mrg 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
    253  1.30       mrg 			    ifa->ifa_dstaddr == NULL)
    254   1.1       cgd 				continue;
    255   1.1       cgd 			if (equal(addr, ifa->ifa_dstaddr))
    256   1.1       cgd 				return (ifa);
    257   1.1       cgd 	}
    258   1.1       cgd 	return ((struct ifaddr *)0);
    259   1.1       cgd }
    260   1.1       cgd 
    261   1.1       cgd /*
    262   1.1       cgd  * Find an interface on a specific network.  If many, choice
    263  1.15   mycroft  * is most specific found.
    264   1.1       cgd  */
    265   1.1       cgd struct ifaddr *
    266   1.1       cgd ifa_ifwithnet(addr)
    267   1.1       cgd 	struct sockaddr *addr;
    268   1.1       cgd {
    269   1.1       cgd 	register struct ifnet *ifp;
    270   1.1       cgd 	register struct ifaddr *ifa;
    271  1.15   mycroft 	struct ifaddr *ifa_maybe = 0;
    272   1.1       cgd 	u_int af = addr->sa_family;
    273  1.15   mycroft 	char *addr_data = addr->sa_data, *cplim;
    274   1.1       cgd 
    275   1.1       cgd 	if (af == AF_LINK) {
    276   1.1       cgd 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
    277   1.1       cgd 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
    278  1.49    itojun 		return (ifnet_addrs[sdl->sdl_index]);
    279   1.1       cgd 	}
    280  1.21   mycroft 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
    281  1.21   mycroft 		for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
    282  1.15   mycroft 			register char *cp, *cp2, *cp3;
    283  1.15   mycroft 
    284  1.15   mycroft 			if (ifa->ifa_addr->sa_family != af ||
    285  1.15   mycroft 			    ifa->ifa_netmask == 0)
    286  1.32       mrg 				next: continue;
    287  1.15   mycroft 			cp = addr_data;
    288  1.15   mycroft 			cp2 = ifa->ifa_addr->sa_data;
    289  1.15   mycroft 			cp3 = ifa->ifa_netmask->sa_data;
    290  1.15   mycroft 			cplim = (char *)ifa->ifa_netmask +
    291  1.15   mycroft 				ifa->ifa_netmask->sa_len;
    292  1.15   mycroft 			while (cp3 < cplim)
    293  1.15   mycroft 				if ((*cp++ ^ *cp2++) & *cp3++)
    294  1.32       mrg 				    /* want to continue for() loop */
    295  1.32       mrg 					goto next;
    296  1.15   mycroft 			if (ifa_maybe == 0 ||
    297  1.15   mycroft 			    rn_refines((caddr_t)ifa->ifa_netmask,
    298  1.15   mycroft 			    (caddr_t)ifa_maybe->ifa_netmask))
    299  1.15   mycroft 				ifa_maybe = ifa;
    300  1.15   mycroft 		}
    301  1.15   mycroft 	return (ifa_maybe);
    302  1.26       mrg }
    303  1.26       mrg /*
    304  1.26       mrg  * Find the interface of the addresss.
    305  1.26       mrg  */
    306  1.26       mrg struct ifaddr *
    307  1.26       mrg ifa_ifwithladdr(addr)
    308  1.26       mrg 	struct sockaddr *addr;
    309  1.26       mrg {
    310  1.26       mrg 	struct ifaddr *ia;
    311  1.26       mrg 
    312  1.26       mrg 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr))
    313  1.26       mrg 	    || (ia = ifa_ifwithnet(addr)))
    314  1.26       mrg 		return (ia);
    315  1.26       mrg 	return (NULL);
    316   1.1       cgd }
    317   1.1       cgd 
    318   1.1       cgd /*
    319   1.1       cgd  * Find an interface using a specific address family
    320   1.1       cgd  */
    321   1.1       cgd struct ifaddr *
    322   1.1       cgd ifa_ifwithaf(af)
    323   1.1       cgd 	register int af;
    324   1.1       cgd {
    325   1.1       cgd 	register struct ifnet *ifp;
    326   1.1       cgd 	register struct ifaddr *ifa;
    327   1.1       cgd 
    328  1.21   mycroft 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
    329  1.21   mycroft 		for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
    330  1.21   mycroft 			if (ifa->ifa_addr->sa_family == af)
    331  1.21   mycroft 				return (ifa);
    332   1.1       cgd 	return ((struct ifaddr *)0);
    333   1.1       cgd }
    334   1.1       cgd 
    335   1.1       cgd /*
    336   1.1       cgd  * Find an interface address specific to an interface best matching
    337   1.1       cgd  * a given address.
    338   1.1       cgd  */
    339   1.1       cgd struct ifaddr *
    340   1.1       cgd ifaof_ifpforaddr(addr, ifp)
    341   1.1       cgd 	struct sockaddr *addr;
    342   1.1       cgd 	register struct ifnet *ifp;
    343   1.1       cgd {
    344   1.1       cgd 	register struct ifaddr *ifa;
    345   1.1       cgd 	register char *cp, *cp2, *cp3;
    346   1.1       cgd 	register char *cplim;
    347   1.1       cgd 	struct ifaddr *ifa_maybe = 0;
    348   1.1       cgd 	u_int af = addr->sa_family;
    349   1.1       cgd 
    350   1.1       cgd 	if (af >= AF_MAX)
    351   1.1       cgd 		return (0);
    352  1.21   mycroft 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
    353   1.1       cgd 		if (ifa->ifa_addr->sa_family != af)
    354   1.1       cgd 			continue;
    355   1.1       cgd 		ifa_maybe = ifa;
    356   1.1       cgd 		if (ifa->ifa_netmask == 0) {
    357   1.1       cgd 			if (equal(addr, ifa->ifa_addr) ||
    358   1.1       cgd 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
    359   1.1       cgd 				return (ifa);
    360   1.1       cgd 			continue;
    361   1.1       cgd 		}
    362   1.1       cgd 		cp = addr->sa_data;
    363   1.1       cgd 		cp2 = ifa->ifa_addr->sa_data;
    364   1.1       cgd 		cp3 = ifa->ifa_netmask->sa_data;
    365   1.1       cgd 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
    366   1.1       cgd 		for (; cp3 < cplim; cp3++)
    367   1.1       cgd 			if ((*cp++ ^ *cp2++) & *cp3)
    368   1.1       cgd 				break;
    369   1.1       cgd 		if (cp3 == cplim)
    370   1.1       cgd 			return (ifa);
    371   1.1       cgd 	}
    372   1.1       cgd 	return (ifa_maybe);
    373   1.1       cgd }
    374   1.9   mycroft 
    375  1.15   mycroft #include <net/route.h>
    376  1.15   mycroft 
    377   1.1       cgd /*
    378   1.1       cgd  * Default action when installing a route with a Link Level gateway.
    379   1.1       cgd  * Lookup an appropriate real ifa to point to.
    380   1.1       cgd  * This should be moved to /sys/net/link.c eventually.
    381   1.1       cgd  */
    382  1.15   mycroft void
    383   1.1       cgd link_rtrequest(cmd, rt, sa)
    384  1.15   mycroft 	int cmd;
    385  1.15   mycroft 	register struct rtentry *rt;
    386  1.15   mycroft 	struct sockaddr *sa;
    387   1.1       cgd {
    388   1.1       cgd 	register struct ifaddr *ifa;
    389   1.1       cgd 	struct sockaddr *dst;
    390  1.15   mycroft 	struct ifnet *ifp;
    391   1.1       cgd 
    392   1.1       cgd 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
    393   1.1       cgd 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
    394   1.1       cgd 		return;
    395  1.24  christos 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
    396  1.15   mycroft 		IFAFREE(rt->rt_ifa);
    397   1.1       cgd 		rt->rt_ifa = ifa;
    398  1.15   mycroft 		ifa->ifa_refcnt++;
    399   1.1       cgd 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
    400   1.1       cgd 			ifa->ifa_rtrequest(cmd, rt, sa);
    401   1.1       cgd 	}
    402   1.1       cgd }
    403   1.1       cgd 
    404   1.1       cgd /*
    405   1.1       cgd  * Mark an interface down and notify protocols of
    406   1.1       cgd  * the transition.
    407  1.23   mycroft  * NOTE: must be called at splsoftnet or equivalent.
    408   1.1       cgd  */
    409  1.15   mycroft void
    410   1.1       cgd if_down(ifp)
    411   1.1       cgd 	register struct ifnet *ifp;
    412   1.1       cgd {
    413   1.1       cgd 	register struct ifaddr *ifa;
    414   1.1       cgd 
    415   1.1       cgd 	ifp->if_flags &= ~IFF_UP;
    416  1.21   mycroft 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
    417   1.1       cgd 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
    418   1.1       cgd 	if_qflush(&ifp->if_snd);
    419  1.15   mycroft 	rt_ifmsg(ifp);
    420  1.15   mycroft }
    421  1.15   mycroft 
    422  1.15   mycroft /*
    423  1.15   mycroft  * Mark an interface up and notify protocols of
    424  1.15   mycroft  * the transition.
    425  1.23   mycroft  * NOTE: must be called at splsoftnet or equivalent.
    426  1.15   mycroft  */
    427  1.15   mycroft void
    428  1.15   mycroft if_up(ifp)
    429  1.15   mycroft 	register struct ifnet *ifp;
    430  1.15   mycroft {
    431  1.24  christos #ifdef notyet
    432  1.15   mycroft 	register struct ifaddr *ifa;
    433  1.24  christos #endif
    434  1.15   mycroft 
    435  1.15   mycroft 	ifp->if_flags |= IFF_UP;
    436  1.15   mycroft #ifdef notyet
    437  1.15   mycroft 	/* this has no effect on IP, and will kill all ISO connections XXX */
    438  1.24  christos 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    439  1.24  christos 	     ifa = ifa->ifa_list.tqe_next)
    440  1.15   mycroft 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
    441  1.15   mycroft #endif
    442  1.15   mycroft 	rt_ifmsg(ifp);
    443  1.49    itojun #ifdef INET6
    444  1.49    itojun 	in6_if_up(ifp);
    445  1.49    itojun #endif
    446   1.1       cgd }
    447   1.1       cgd 
    448   1.1       cgd /*
    449   1.1       cgd  * Flush an interface queue.
    450   1.1       cgd  */
    451  1.15   mycroft void
    452   1.1       cgd if_qflush(ifq)
    453   1.1       cgd 	register struct ifqueue *ifq;
    454   1.1       cgd {
    455   1.1       cgd 	register struct mbuf *m, *n;
    456   1.1       cgd 
    457   1.1       cgd 	n = ifq->ifq_head;
    458  1.24  christos 	while ((m = n) != NULL) {
    459   1.1       cgd 		n = m->m_act;
    460   1.1       cgd 		m_freem(m);
    461   1.1       cgd 	}
    462   1.1       cgd 	ifq->ifq_head = 0;
    463   1.1       cgd 	ifq->ifq_tail = 0;
    464   1.1       cgd 	ifq->ifq_len = 0;
    465   1.1       cgd }
    466   1.1       cgd 
    467   1.1       cgd /*
    468   1.1       cgd  * Handle interface watchdog timer routines.  Called
    469   1.1       cgd  * from softclock, we decrement timers (if set) and
    470   1.1       cgd  * call the appropriate interface routine on expiration.
    471   1.1       cgd  */
    472   1.4    andrew void
    473  1.13       cgd if_slowtimo(arg)
    474  1.13       cgd 	void *arg;
    475   1.1       cgd {
    476   1.1       cgd 	register struct ifnet *ifp;
    477   1.1       cgd 	int s = splimp();
    478   1.1       cgd 
    479  1.21   mycroft 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
    480   1.1       cgd 		if (ifp->if_timer == 0 || --ifp->if_timer)
    481   1.1       cgd 			continue;
    482   1.1       cgd 		if (ifp->if_watchdog)
    483  1.34   thorpej 			(*ifp->if_watchdog)(ifp);
    484   1.1       cgd 	}
    485   1.1       cgd 	splx(s);
    486  1.14   mycroft 	timeout(if_slowtimo, NULL, hz / IFNET_SLOWHZ);
    487   1.1       cgd }
    488   1.1       cgd 
    489   1.1       cgd /*
    490   1.1       cgd  * Map interface name to
    491   1.1       cgd  * interface structure pointer.
    492   1.1       cgd  */
    493   1.1       cgd struct ifnet *
    494   1.1       cgd ifunit(name)
    495   1.1       cgd 	register char *name;
    496   1.1       cgd {
    497   1.1       cgd 	register struct ifnet *ifp;
    498  1.34   thorpej 
    499  1.34   thorpej 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
    500  1.35   thorpej 		if (strcmp(ifp->if_xname, name) == 0)
    501  1.34   thorpej 			return (ifp);
    502  1.34   thorpej 
    503  1.34   thorpej 	return (NULL);
    504   1.1       cgd }
    505   1.1       cgd 
    506  1.49    itojun 
    507  1.49    itojun /*
    508  1.49    itojun  * Map interface name in a sockaddr_dl to
    509  1.49    itojun  * interface structure pointer.
    510  1.49    itojun  */
    511  1.49    itojun struct ifnet *
    512  1.49    itojun if_withname(sa)
    513  1.49    itojun 	struct sockaddr *sa;
    514  1.49    itojun {
    515  1.49    itojun 	char ifname[IFNAMSIZ+1];
    516  1.49    itojun 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
    517  1.49    itojun 
    518  1.49    itojun 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
    519  1.49    itojun 	     (sdl->sdl_nlen > IFNAMSIZ) )
    520  1.49    itojun 		return NULL;
    521  1.49    itojun 
    522  1.49    itojun 	/*
    523  1.49    itojun 	 * ifunit wants a null-terminated name.  It may not be null-terminated
    524  1.49    itojun 	 * in the sockaddr.  We don't want to change the caller's sockaddr,
    525  1.49    itojun 	 * and there might not be room to put the trailing null anyway, so we
    526  1.49    itojun 	 * make a local copy that we know we can null terminate safely.
    527  1.49    itojun 	 */
    528  1.49    itojun 
    529  1.49    itojun 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
    530  1.49    itojun 	ifname[sdl->sdl_nlen] = '\0';
    531  1.49    itojun 	return ifunit(ifname);
    532  1.49    itojun }
    533  1.49    itojun 
    534  1.49    itojun 
    535   1.1       cgd /*
    536   1.1       cgd  * Interface ioctls.
    537   1.1       cgd  */
    538  1.15   mycroft int
    539   1.1       cgd ifioctl(so, cmd, data, p)
    540   1.1       cgd 	struct socket *so;
    541  1.18       cgd 	u_long cmd;
    542   1.1       cgd 	caddr_t data;
    543   1.1       cgd 	struct proc *p;
    544   1.1       cgd {
    545   1.1       cgd 	register struct ifnet *ifp;
    546   1.1       cgd 	register struct ifreq *ifr;
    547  1.49    itojun 	int error = 0;
    548  1.49    itojun 	short oif_flags;
    549   1.1       cgd 
    550   1.1       cgd 	switch (cmd) {
    551   1.1       cgd 
    552   1.1       cgd 	case SIOCGIFCONF:
    553   1.1       cgd 	case OSIOCGIFCONF:
    554   1.1       cgd 		return (ifconf(cmd, data));
    555   1.1       cgd 	}
    556   1.1       cgd 	ifr = (struct ifreq *)data;
    557   1.1       cgd 	ifp = ifunit(ifr->ifr_name);
    558   1.1       cgd 	if (ifp == 0)
    559   1.1       cgd 		return (ENXIO);
    560  1.49    itojun 	oif_flags = ifp->if_flags;
    561   1.1       cgd 	switch (cmd) {
    562   1.1       cgd 
    563   1.1       cgd 	case SIOCGIFFLAGS:
    564   1.1       cgd 		ifr->ifr_flags = ifp->if_flags;
    565   1.1       cgd 		break;
    566   1.1       cgd 
    567   1.1       cgd 	case SIOCGIFMETRIC:
    568   1.1       cgd 		ifr->ifr_metric = ifp->if_metric;
    569   1.1       cgd 		break;
    570   1.7   hpeyerl 
    571  1.36   mycroft 	case SIOCGIFMTU:
    572  1.37       cgd 		ifr->ifr_mtu = ifp->if_mtu;
    573  1.37       cgd 		break;
    574  1.36   mycroft 
    575   1.1       cgd 	case SIOCSIFFLAGS:
    576  1.24  christos 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    577   1.1       cgd 			return (error);
    578   1.1       cgd 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
    579   1.1       cgd 			int s = splimp();
    580   1.1       cgd 			if_down(ifp);
    581   1.1       cgd 			splx(s);
    582   1.1       cgd 		}
    583  1.15   mycroft 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
    584  1.15   mycroft 			int s = splimp();
    585  1.15   mycroft 			if_up(ifp);
    586  1.15   mycroft 			splx(s);
    587  1.15   mycroft 		}
    588   1.1       cgd 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
    589   1.1       cgd 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
    590   1.1       cgd 		if (ifp->if_ioctl)
    591   1.1       cgd 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
    592   1.1       cgd 		break;
    593   1.1       cgd 
    594   1.1       cgd 	case SIOCSIFMETRIC:
    595  1.24  christos 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    596   1.1       cgd 			return (error);
    597   1.1       cgd 		ifp->if_metric = ifr->ifr_metric;
    598   1.5   deraadt 		break;
    599   1.5   deraadt 
    600  1.36   mycroft 	case SIOCSIFMTU:
    601  1.49    itojun 	{
    602  1.49    itojun 		u_long oldmtu = ifp->if_mtu;
    603  1.49    itojun 
    604  1.49    itojun 		error = suser(p->p_ucred, &p->p_acflag);
    605  1.49    itojun 		if (error)
    606  1.49    itojun 			return (error);
    607  1.49    itojun 		if (ifp->if_ioctl == NULL)
    608  1.49    itojun 			return (EOPNOTSUPP);
    609  1.49    itojun 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    610  1.49    itojun 
    611  1.49    itojun 		/*
    612  1.49    itojun 		 * If the link MTU changed, do network layer specific procedure.
    613  1.49    itojun 		 */
    614  1.49    itojun 		if (ifp->if_mtu != oldmtu) {
    615  1.49    itojun #ifdef INET6
    616  1.49    itojun 			nd6_setmtu(ifp);
    617  1.49    itojun #endif
    618  1.49    itojun 		}
    619  1.49    itojun 		break;
    620  1.49    itojun 	}
    621  1.15   mycroft 	case SIOCADDMULTI:
    622  1.15   mycroft 	case SIOCDELMULTI:
    623  1.39   thorpej 	case SIOCSIFMEDIA:
    624  1.24  christos 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    625  1.15   mycroft 			return (error);
    626  1.39   thorpej 		/* FALLTHROUGH */
    627  1.39   thorpej 	case SIOCGIFMEDIA:
    628  1.15   mycroft 		if (ifp->if_ioctl == 0)
    629   1.5   deraadt 			return (EOPNOTSUPP);
    630  1.49    itojun 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    631  1.49    itojun 		break;
    632   1.1       cgd 
    633  1.45       kml 	case SIOCSDRVSPEC:
    634  1.45       kml 		/* XXX:  need to pass proc pointer through to driver... */
    635  1.45       kml 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    636  1.45       kml 			return (error);
    637  1.45       kml 	/* FALLTHROUGH */
    638   1.1       cgd 	default:
    639   1.1       cgd 		if (so->so_proto == 0)
    640   1.1       cgd 			return (EOPNOTSUPP);
    641  1.33  christos #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4)
    642  1.49    itojun 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
    643  1.36   mycroft 		    (struct mbuf *)cmd, (struct mbuf *)data,
    644  1.36   mycroft 		    (struct mbuf *)ifp, p));
    645   1.1       cgd #else
    646   1.1       cgd 	    {
    647   1.1       cgd 		int ocmd = cmd;
    648   1.1       cgd 
    649   1.1       cgd 		switch (cmd) {
    650   1.1       cgd 
    651  1.28   mycroft 		case SIOCSIFADDR:
    652   1.1       cgd 		case SIOCSIFDSTADDR:
    653   1.1       cgd 		case SIOCSIFBRDADDR:
    654   1.1       cgd 		case SIOCSIFNETMASK:
    655   1.1       cgd #if BYTE_ORDER != BIG_ENDIAN
    656   1.1       cgd 			if (ifr->ifr_addr.sa_family == 0 &&
    657   1.1       cgd 			    ifr->ifr_addr.sa_len < 16) {
    658   1.1       cgd 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
    659   1.1       cgd 				ifr->ifr_addr.sa_len = 16;
    660   1.1       cgd 			}
    661   1.1       cgd #else
    662   1.1       cgd 			if (ifr->ifr_addr.sa_len == 0)
    663   1.1       cgd 				ifr->ifr_addr.sa_len = 16;
    664   1.1       cgd #endif
    665   1.1       cgd 			break;
    666   1.1       cgd 
    667   1.1       cgd 		case OSIOCGIFADDR:
    668   1.1       cgd 			cmd = SIOCGIFADDR;
    669   1.1       cgd 			break;
    670   1.1       cgd 
    671   1.1       cgd 		case OSIOCGIFDSTADDR:
    672   1.1       cgd 			cmd = SIOCGIFDSTADDR;
    673   1.1       cgd 			break;
    674   1.1       cgd 
    675   1.1       cgd 		case OSIOCGIFBRDADDR:
    676   1.1       cgd 			cmd = SIOCGIFBRDADDR;
    677   1.1       cgd 			break;
    678   1.1       cgd 
    679   1.1       cgd 		case OSIOCGIFNETMASK:
    680   1.1       cgd 			cmd = SIOCGIFNETMASK;
    681   1.1       cgd 		}
    682  1.49    itojun 
    683  1.24  christos 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
    684  1.36   mycroft 		    (struct mbuf *)cmd, (struct mbuf *)data,
    685  1.36   mycroft 		    (struct mbuf *)ifp, p));
    686  1.49    itojun 
    687   1.1       cgd 		switch (ocmd) {
    688   1.1       cgd 		case OSIOCGIFADDR:
    689   1.1       cgd 		case OSIOCGIFDSTADDR:
    690   1.1       cgd 		case OSIOCGIFBRDADDR:
    691   1.1       cgd 		case OSIOCGIFNETMASK:
    692  1.20       cgd 			*(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
    693   1.1       cgd 		}
    694  1.49    itojun 	    }
    695  1.49    itojun #endif /* COMPAT_43 */
    696  1.49    itojun 		break;
    697  1.49    itojun 	}
    698   1.1       cgd 
    699  1.49    itojun 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
    700  1.49    itojun #ifdef INET6
    701  1.49    itojun 		if ((ifp->if_flags & IFF_UP) != 0) {
    702  1.49    itojun 			int s = splimp();
    703  1.49    itojun 			in6_if_up(ifp);
    704  1.49    itojun 			splx(s);
    705  1.49    itojun 		}
    706   1.1       cgd #endif
    707   1.1       cgd 	}
    708  1.49    itojun 
    709  1.49    itojun 	return (error);
    710   1.1       cgd }
    711   1.1       cgd 
    712   1.1       cgd /*
    713   1.1       cgd  * Return interface configuration
    714   1.1       cgd  * of system.  List may be used
    715   1.1       cgd  * in later ioctl's (above) to get
    716   1.1       cgd  * other information.
    717   1.1       cgd  */
    718   1.1       cgd /*ARGSUSED*/
    719  1.15   mycroft int
    720   1.1       cgd ifconf(cmd, data)
    721  1.19   mycroft 	u_long cmd;
    722   1.1       cgd 	caddr_t data;
    723   1.1       cgd {
    724   1.1       cgd 	register struct ifconf *ifc = (struct ifconf *)data;
    725  1.21   mycroft 	register struct ifnet *ifp;
    726   1.1       cgd 	register struct ifaddr *ifa;
    727   1.1       cgd 	struct ifreq ifr, *ifrp;
    728   1.1       cgd 	int space = ifc->ifc_len, error = 0;
    729   1.1       cgd 
    730   1.1       cgd 	ifrp = ifc->ifc_req;
    731  1.21   mycroft 	for (ifp = ifnet.tqh_first;
    732  1.33  christos 	    space >= sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
    733  1.34   thorpej 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
    734  1.21   mycroft 		if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
    735   1.1       cgd 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
    736  1.15   mycroft 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
    737  1.15   mycroft 			    sizeof(ifr));
    738   1.1       cgd 			if (error)
    739   1.1       cgd 				break;
    740   1.1       cgd 			space -= sizeof (ifr), ifrp++;
    741   1.1       cgd 		} else
    742  1.33  christos 		    for (; space >= sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
    743   1.1       cgd 			register struct sockaddr *sa = ifa->ifa_addr;
    744  1.33  christos #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
    745   1.1       cgd 			if (cmd == OSIOCGIFCONF) {
    746   1.1       cgd 				struct osockaddr *osa =
    747   1.1       cgd 					 (struct osockaddr *)&ifr.ifr_addr;
    748   1.1       cgd 				ifr.ifr_addr = *sa;
    749   1.1       cgd 				osa->sa_family = sa->sa_family;
    750   1.1       cgd 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
    751   1.1       cgd 						sizeof (ifr));
    752   1.1       cgd 				ifrp++;
    753   1.1       cgd 			} else
    754   1.1       cgd #endif
    755   1.1       cgd 			if (sa->sa_len <= sizeof(*sa)) {
    756   1.1       cgd 				ifr.ifr_addr = *sa;
    757   1.1       cgd 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
    758   1.1       cgd 						sizeof (ifr));
    759   1.1       cgd 				ifrp++;
    760   1.1       cgd 			} else {
    761   1.1       cgd 				space -= sa->sa_len - sizeof(*sa);
    762   1.1       cgd 				if (space < sizeof (ifr))
    763   1.1       cgd 					break;
    764   1.1       cgd 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
    765   1.1       cgd 						sizeof (ifr.ifr_name));
    766   1.1       cgd 				if (error == 0)
    767   1.1       cgd 				    error = copyout((caddr_t)sa,
    768   1.1       cgd 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
    769   1.1       cgd 				ifrp = (struct ifreq *)
    770   1.1       cgd 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
    771   1.1       cgd 			}
    772   1.1       cgd 			if (error)
    773   1.1       cgd 				break;
    774   1.1       cgd 			space -= sizeof (ifr);
    775   1.1       cgd 		}
    776   1.1       cgd 	}
    777   1.1       cgd 	ifc->ifc_len -= space;
    778   1.1       cgd 	return (error);
    779   1.1       cgd }
    780