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