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