Home | History | Annotate | Line # | Download | only in routed
if.c revision 1.21
      1  1.21    itojun /*	$NetBSD: if.c,v 1.21 2001/01/15 13:19:12 itojun Exp $	*/
      2   1.7       cgd 
      3   1.1       cgd /*
      4   1.5   mycroft  * Copyright (c) 1983, 1993
      5   1.5   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.16  christos  *    must display the following acknowledgment:
     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.1       cgd 
     36  1.10  christos #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
     37  1.17  christos static char sccsid[] __attribute__((unused)) = "@(#)if.c	8.1 (Berkeley) 6/5/93";
     38  1.10  christos #elif defined(__NetBSD__)
     39  1.12     lukem #include <sys/cdefs.h>
     40  1.21    itojun __RCSID("$NetBSD: if.c,v 1.21 2001/01/15 13:19:12 itojun Exp $");
     41   1.7       cgd #endif
     42   1.1       cgd 
     43   1.1       cgd #include "defs.h"
     44   1.9   thorpej #include "pathnames.h"
     45   1.9   thorpej 
     46  1.11  christos struct interface *ifnet;		/* all interfaces */
     47  1.11  christos 
     48  1.11  christos /* hash table for all interfaces, big enough to tolerate ridiculous
     49  1.11  christos  * numbers of IP aliases.  Crazy numbers of aliases such as 7000
     50  1.11  christos  * still will not do well, but not just in looking up interfaces
     51  1.11  christos  * by name or address.
     52  1.11  christos  */
     53  1.11  christos #define AHASH_LEN 211			/* must be prime */
     54  1.11  christos #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
     55  1.11  christos struct interface *ahash_tbl[AHASH_LEN];
     56  1.11  christos 
     57  1.11  christos #define BHASH_LEN 211			/* must be prime */
     58  1.11  christos #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
     59  1.11  christos struct interface *bhash_tbl[BHASH_LEN];
     60  1.11  christos 
     61  1.11  christos struct interface *remote_if;		/* remote interfaces */
     62  1.11  christos 
     63  1.11  christos /* hash for physical interface names.
     64  1.11  christos  * Assume there are never more 100 or 200 real interfaces, and that
     65  1.11  christos  * aliases are put on the end of the hash chains.
     66  1.11  christos  */
     67  1.11  christos #define NHASH_LEN 97
     68  1.11  christos struct interface *nhash_tbl[NHASH_LEN];
     69  1.11  christos 
     70   1.9   thorpej int	tot_interfaces;			/* # of remote and local interfaces */
     71   1.9   thorpej int	rip_interfaces;			/* # of interfaces doing RIP */
     72   1.9   thorpej int	foundloopback;			/* valid flag for loopaddr */
     73   1.9   thorpej naddr	loopaddr;			/* our address on loopback */
     74  1.14   thorpej struct	rt_spare loop_rts;
     75   1.9   thorpej 
     76   1.9   thorpej struct timeval ifinit_timer;
     77  1.11  christos static struct timeval last_ifinit;
     78  1.14   thorpej #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec		\
     79  1.14   thorpej 			   && last_ifinit.tv_usec == now.tv_usec	\
     80  1.14   thorpej 			   && timercmp(&ifinit_timer, &now, >))
     81   1.1       cgd 
     82   1.9   thorpej int	have_ripv1_out;			/* have a RIPv1 interface */
     83   1.9   thorpej int	have_ripv1_in;
     84   1.1       cgd 
     85   1.9   thorpej 
     86  1.11  christos static struct interface**
     87  1.12     lukem nhash(char *p)
     88  1.11  christos {
     89  1.12     lukem 	u_int i;
     90  1.11  christos 
     91  1.11  christos 	for (i = 0; *p != '\0'; p++) {
     92  1.11  christos 		i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
     93  1.11  christos 		i ^= *p;
     94  1.11  christos 	}
     95  1.11  christos 	return &nhash_tbl[i % NHASH_LEN];
     96  1.11  christos }
     97  1.11  christos 
     98  1.11  christos 
     99  1.11  christos /* Link a new interface into the lists and hash tables.
    100  1.11  christos  */
    101  1.11  christos void
    102  1.11  christos if_link(struct interface *ifp)
    103  1.11  christos {
    104  1.11  christos 	struct interface **hifp;
    105  1.11  christos 
    106  1.11  christos 	ifp->int_prev = &ifnet;
    107  1.11  christos 	ifp->int_next = ifnet;
    108  1.11  christos 	if (ifnet != 0)
    109  1.11  christos 		ifnet->int_prev = &ifp->int_next;
    110  1.11  christos 	ifnet = ifp;
    111  1.11  christos 
    112  1.11  christos 	hifp = AHASH(ifp->int_addr);
    113  1.11  christos 	ifp->int_ahash_prev = hifp;
    114  1.11  christos 	if ((ifp->int_ahash = *hifp) != 0)
    115  1.11  christos 		(*hifp)->int_ahash_prev = &ifp->int_ahash;
    116  1.11  christos 	*hifp = ifp;
    117  1.11  christos 
    118  1.11  christos 	if (ifp->int_if_flags & IFF_BROADCAST) {
    119  1.11  christos 		hifp = BHASH(ifp->int_brdaddr);
    120  1.11  christos 		ifp->int_bhash_prev = hifp;
    121  1.11  christos 		if ((ifp->int_bhash = *hifp) != 0)
    122  1.11  christos 			(*hifp)->int_bhash_prev = &ifp->int_bhash;
    123  1.11  christos 		*hifp = ifp;
    124  1.11  christos 	}
    125  1.11  christos 
    126  1.11  christos 	if (ifp->int_state & IS_REMOTE) {
    127  1.11  christos 		ifp->int_rlink_prev = &remote_if;
    128  1.11  christos 		ifp->int_rlink = remote_if;
    129  1.11  christos 		if (remote_if != 0)
    130  1.11  christos 			remote_if->int_rlink_prev = &ifp->int_rlink;
    131  1.11  christos 		remote_if = ifp;
    132  1.11  christos 	}
    133  1.11  christos 
    134  1.11  christos 	hifp = nhash(ifp->int_name);
    135  1.11  christos 	if (ifp->int_state & IS_ALIAS) {
    136  1.11  christos 		/* put aliases on the end of the hash chain */
    137  1.11  christos 		while (*hifp != 0)
    138  1.11  christos 			hifp = &(*hifp)->int_nhash;
    139  1.11  christos 	}
    140  1.11  christos 	ifp->int_nhash_prev = hifp;
    141  1.11  christos 	if ((ifp->int_nhash = *hifp) != 0)
    142  1.11  christos 		(*hifp)->int_nhash_prev = &ifp->int_nhash;
    143  1.11  christos 	*hifp = ifp;
    144  1.11  christos }
    145  1.11  christos 
    146  1.11  christos 
    147   1.9   thorpej /* Find the interface with an address
    148   1.1       cgd  */
    149   1.1       cgd struct interface *
    150   1.9   thorpej ifwithaddr(naddr addr,
    151   1.9   thorpej 	   int	bcast,			/* notice IFF_BROADCAST address */
    152   1.9   thorpej 	   int	remote)			/* include IS_REMOTE interfaces */
    153   1.1       cgd {
    154   1.9   thorpej 	struct interface *ifp, *possible = 0;
    155   1.1       cgd 
    156  1.11  christos 	remote = (remote == 0) ? IS_REMOTE : 0;
    157  1.11  christos 
    158  1.11  christos 	for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
    159  1.11  christos 		if (ifp->int_addr != addr)
    160  1.11  christos 			continue;
    161  1.11  christos 		if ((ifp->int_state & remote) != 0)
    162  1.11  christos 			continue;
    163  1.11  christos 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
    164  1.11  christos 			return ifp;
    165  1.11  christos 		possible = ifp;
    166  1.11  christos 	}
    167   1.9   thorpej 
    168  1.11  christos 	if (possible || !bcast)
    169  1.11  christos 		return possible;
    170   1.9   thorpej 
    171  1.11  christos 	for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
    172  1.11  christos 		if (ifp->int_brdaddr != addr)
    173  1.11  christos 			continue;
    174  1.11  christos 		if ((ifp->int_state & remote) != 0)
    175  1.11  christos 			continue;
    176  1.11  christos 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
    177  1.11  christos 			return ifp;
    178  1.11  christos 		possible = ifp;
    179   1.1       cgd 	}
    180   1.9   thorpej 
    181   1.9   thorpej 	return possible;
    182   1.1       cgd }
    183   1.1       cgd 
    184   1.9   thorpej 
    185   1.9   thorpej /* find the interface with a name
    186   1.1       cgd  */
    187   1.1       cgd struct interface *
    188   1.9   thorpej ifwithname(char *name,			/* "ec0" or whatever */
    189   1.9   thorpej 	   naddr addr)			/* 0 or network address */
    190   1.1       cgd {
    191   1.9   thorpej 	struct interface *ifp;
    192   1.9   thorpej 
    193  1.11  christos 	for (;;) {
    194  1.11  christos 		for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
    195  1.11  christos 			/* If the network address is not specified,
    196  1.11  christos 			 * ignore any alias interfaces.  Otherwise, look
    197  1.11  christos 			 * for the interface with the target name and address.
    198  1.11  christos 			 */
    199  1.11  christos 			if (!strcmp(ifp->int_name, name)
    200  1.11  christos 			    && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
    201  1.11  christos 				|| (ifp->int_addr == addr)))
    202  1.11  christos 				return ifp;
    203  1.11  christos 		}
    204   1.1       cgd 
    205  1.11  christos 		/* If there is no known interface, maybe there is a
    206  1.11  christos 		 * new interface.  So just once look for new interfaces.
    207  1.11  christos 		 */
    208  1.14   thorpej 		if (IF_RESCAN_DELAY())
    209  1.11  christos 			return 0;
    210  1.11  christos 		ifinit();
    211   1.1       cgd 	}
    212   1.1       cgd }
    213   1.1       cgd 
    214   1.9   thorpej 
    215   1.1       cgd struct interface *
    216  1.11  christos ifwithindex(u_short index,
    217  1.11  christos 	    int rescan_ok)
    218   1.1       cgd {
    219   1.9   thorpej 	struct interface *ifp;
    220   1.9   thorpej 
    221  1.11  christos 	for (;;) {
    222  1.11  christos 		for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
    223  1.11  christos 			if (ifp->int_index == index)
    224  1.11  christos 				return ifp;
    225  1.11  christos 		}
    226   1.9   thorpej 
    227  1.11  christos 		/* If there is no known interface, maybe there is a
    228  1.11  christos 		 * new interface.  So just once look for new interfaces.
    229  1.11  christos 		 */
    230  1.11  christos 		if (!rescan_ok
    231  1.14   thorpej 		    || IF_RESCAN_DELAY())
    232  1.11  christos 			return 0;
    233  1.11  christos 		ifinit();
    234   1.1       cgd 	}
    235   1.1       cgd }
    236   1.1       cgd 
    237   1.9   thorpej 
    238   1.9   thorpej /* Find an interface from which the specified address
    239   1.1       cgd  * should have come from.  Used for figuring out which
    240  1.11  christos  * interface a packet came in on.
    241   1.1       cgd  */
    242   1.1       cgd struct interface *
    243   1.9   thorpej iflookup(naddr addr)
    244   1.1       cgd {
    245   1.9   thorpej 	struct interface *ifp, *maybe;
    246  1.20  christos 	int once = 0;
    247   1.1       cgd 
    248   1.1       cgd 	maybe = 0;
    249  1.11  christos 	for (;;) {
    250  1.11  christos 		for (ifp = ifnet; ifp; ifp = ifp->int_next) {
    251  1.11  christos 			if (ifp->int_if_flags & IFF_POINTOPOINT) {
    252   1.9   thorpej 				/* finished with a match */
    253  1.11  christos 				if (ifp->int_dstaddr == addr)
    254  1.11  christos 					return ifp;
    255   1.9   thorpej 
    256  1.11  christos 			} else {
    257  1.11  christos 				/* finished with an exact match */
    258  1.11  christos 				if (ifp->int_addr == addr)
    259  1.11  christos 					return ifp;
    260   1.9   thorpej 
    261  1.11  christos 				/* Look for the longest approximate match.
    262  1.11  christos 				 */
    263  1.11  christos 				if (on_net(addr, ifp->int_net, ifp->int_mask)
    264  1.11  christos 				    && (maybe == 0
    265  1.11  christos 					|| ifp->int_mask > maybe->int_mask))
    266  1.11  christos 					maybe = ifp;
    267  1.11  christos 			}
    268   1.9   thorpej 		}
    269  1.11  christos 
    270  1.20  christos 		if (maybe != 0 || once)
    271  1.11  christos 			return maybe;
    272  1.11  christos 
    273  1.11  christos 		/* If there is no known interface, maybe there is a
    274  1.11  christos 		 * new interface.  So just once look for new interfaces.
    275  1.11  christos 		 */
    276  1.11  christos 		ifinit();
    277  1.20  christos 		once = 1;
    278   1.9   thorpej 	}
    279   1.9   thorpej }
    280   1.9   thorpej 
    281   1.9   thorpej 
    282   1.9   thorpej /* Return the classical netmask for an IP address.
    283   1.9   thorpej  */
    284  1.11  christos naddr					/* host byte order */
    285  1.11  christos std_mask(naddr addr)			/* network byte order */
    286   1.9   thorpej {
    287   1.9   thorpej 	NTOHL(addr);			/* was a host, not a network */
    288   1.9   thorpej 
    289   1.9   thorpej 	if (addr == 0)			/* default route has mask 0 */
    290   1.9   thorpej 		return 0;
    291   1.9   thorpej 	if (IN_CLASSA(addr))
    292   1.9   thorpej 		return IN_CLASSA_NET;
    293   1.9   thorpej 	if (IN_CLASSB(addr))
    294   1.9   thorpej 		return IN_CLASSB_NET;
    295   1.9   thorpej 	return IN_CLASSC_NET;
    296   1.9   thorpej }
    297   1.9   thorpej 
    298   1.9   thorpej 
    299  1.10  christos /* Find the netmask that would be inferred by RIPv1 listeners
    300   1.9   thorpej  *	on the given interface for a given network.
    301   1.9   thorpej  *	If no interface is specified, look for the best fitting	interface.
    302   1.9   thorpej  */
    303   1.9   thorpej naddr
    304   1.9   thorpej ripv1_mask_net(naddr addr,		/* in network byte order */
    305   1.9   thorpej 	       struct interface *ifp)	/* as seen on this interface */
    306   1.9   thorpej {
    307  1.14   thorpej 	struct r1net *r1p;
    308   1.9   thorpej 	naddr mask = 0;
    309   1.9   thorpej 
    310   1.9   thorpej 	if (addr == 0)			/* default always has 0 mask */
    311   1.9   thorpej 		return mask;
    312   1.9   thorpej 
    313  1.14   thorpej 	if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
    314   1.9   thorpej 		/* If the target network is that of the associated interface
    315   1.9   thorpej 		 * on which it arrived, then use the netmask of the interface.
    316   1.9   thorpej 		 */
    317   1.9   thorpej 		if (on_net(addr, ifp->int_net, ifp->int_std_mask))
    318   1.9   thorpej 			mask = ifp->int_ripv1_mask;
    319   1.9   thorpej 
    320   1.9   thorpej 	} else {
    321   1.9   thorpej 		/* Examine all interfaces, and if it the target seems
    322   1.9   thorpej 		 * to have the same network number of an interface, use the
    323   1.9   thorpej 		 * netmask of that interface.  If there is more than one
    324   1.9   thorpej 		 * such interface, prefer the interface with the longest
    325   1.9   thorpej 		 * match.
    326   1.9   thorpej 		 */
    327   1.9   thorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    328   1.9   thorpej 			if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
    329  1.14   thorpej 			    && ifp->int_ripv1_mask > mask
    330  1.14   thorpej 			    && ifp->int_ripv1_mask != HOST_MASK)
    331   1.9   thorpej 				mask = ifp->int_ripv1_mask;
    332   1.9   thorpej 		}
    333  1.14   thorpej 
    334   1.9   thorpej 	}
    335   1.9   thorpej 
    336  1.14   thorpej 	/* check special definitions */
    337  1.14   thorpej 	if (mask == 0) {
    338  1.14   thorpej 		for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
    339  1.14   thorpej 			if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
    340  1.14   thorpej 			    && r1p->r1net_mask > mask)
    341  1.16  christos 				mask = r1p->r1net_mask;
    342  1.14   thorpej 		}
    343  1.14   thorpej 
    344  1.14   thorpej 		/* Otherwise, make the classic A/B/C guess.
    345  1.14   thorpej 		 */
    346  1.14   thorpej 		if (mask == 0)
    347  1.14   thorpej 			mask = std_mask(addr);
    348  1.14   thorpej 	}
    349   1.9   thorpej 
    350   1.9   thorpej 	return mask;
    351   1.9   thorpej }
    352   1.9   thorpej 
    353   1.9   thorpej 
    354   1.9   thorpej naddr
    355   1.9   thorpej ripv1_mask_host(naddr addr,		/* in network byte order */
    356   1.9   thorpej 		struct interface *ifp)	/* as seen on this interface */
    357   1.9   thorpej {
    358   1.9   thorpej 	naddr mask = ripv1_mask_net(addr, ifp);
    359   1.9   thorpej 
    360   1.9   thorpej 
    361   1.9   thorpej 	/* If the computed netmask does not mask the address,
    362   1.9   thorpej 	 * then assume it is a host address
    363   1.9   thorpej 	 */
    364   1.9   thorpej 	if ((ntohl(addr) & ~mask) != 0)
    365   1.9   thorpej 		mask = HOST_MASK;
    366   1.9   thorpej 	return mask;
    367   1.9   thorpej }
    368   1.9   thorpej 
    369   1.9   thorpej 
    370   1.9   thorpej /* See if a IP address looks reasonable as a destination
    371   1.9   thorpej  */
    372   1.9   thorpej int					/* 0=bad */
    373   1.9   thorpej check_dst(naddr addr)
    374   1.9   thorpej {
    375   1.9   thorpej 	NTOHL(addr);
    376   1.9   thorpej 
    377   1.9   thorpej 	if (IN_CLASSA(addr)) {
    378   1.9   thorpej 		if (addr == 0)
    379   1.9   thorpej 			return 1;	/* default */
    380   1.9   thorpej 
    381   1.9   thorpej 		addr >>= IN_CLASSA_NSHIFT;
    382   1.9   thorpej 		return (addr != 0 && addr != IN_LOOPBACKNET);
    383   1.9   thorpej 	}
    384   1.9   thorpej 
    385   1.9   thorpej 	return (IN_CLASSB(addr) || IN_CLASSC(addr));
    386   1.9   thorpej }
    387   1.9   thorpej 
    388   1.9   thorpej 
    389  1.11  christos /* See a new interface duplicates an existing interface.
    390  1.11  christos  */
    391  1.11  christos struct interface *
    392  1.11  christos check_dup(naddr addr,			/* IP address, so network byte order */
    393  1.11  christos 	  naddr dstaddr,		/* ditto */
    394  1.11  christos 	  naddr mask,			/* mask, so host byte order */
    395  1.11  christos 	  int if_flags)
    396  1.11  christos {
    397  1.11  christos 	struct interface *ifp;
    398  1.11  christos 
    399  1.11  christos 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
    400  1.11  christos 		if (ifp->int_mask != mask)
    401  1.11  christos 			continue;
    402  1.11  christos 
    403  1.14   thorpej 		if (!iff_up(ifp->int_if_flags))
    404  1.11  christos 			continue;
    405  1.11  christos 
    406  1.11  christos 		/* The local address can only be shared with a point-to-point
    407  1.11  christos 		 * link.
    408  1.11  christos 		 */
    409  1.11  christos 		if (ifp->int_addr == addr
    410  1.11  christos 		    && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
    411  1.11  christos 			return ifp;
    412  1.11  christos 
    413  1.11  christos 		if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
    414  1.11  christos 			return ifp;
    415  1.11  christos 	}
    416  1.11  christos 	return 0;
    417  1.11  christos }
    418  1.11  christos 
    419  1.11  christos 
    420  1.11  christos /* See that a remote gateway is reachable.
    421  1.11  christos  *	Note that the answer can change as real interfaces come and go.
    422  1.11  christos  */
    423  1.11  christos int					/* 0=bad */
    424  1.11  christos check_remote(struct interface *ifp)
    425  1.11  christos {
    426  1.11  christos 	struct rt_entry *rt;
    427  1.11  christos 
    428  1.11  christos 	/* do not worry about other kinds */
    429  1.11  christos 	if (!(ifp->int_state & IS_REMOTE))
    430  1.11  christos 	    return 1;
    431  1.11  christos 
    432  1.11  christos 	rt = rtfind(ifp->int_addr);
    433  1.11  christos 	if (rt != 0
    434  1.11  christos 	    && rt->rt_ifp != 0
    435  1.11  christos 	    &&on_net(ifp->int_addr,
    436  1.11  christos 		     rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
    437  1.11  christos 		return 1;
    438  1.11  christos 
    439  1.11  christos 	/* the gateway cannot be reached directly from one of our
    440  1.11  christos 	 * interfaces
    441  1.11  christos 	 */
    442  1.11  christos 	if (!(ifp->int_state & IS_BROKE)) {
    443  1.11  christos 		msglog("unreachable gateway %s in "_PATH_GATEWAYS,
    444  1.11  christos 		       naddr_ntoa(ifp->int_addr));
    445  1.11  christos 		if_bad(ifp);
    446  1.11  christos 	}
    447  1.11  christos 	return 0;
    448  1.11  christos }
    449  1.11  christos 
    450  1.11  christos 
    451   1.9   thorpej /* Delete an interface.
    452   1.9   thorpej  */
    453   1.9   thorpej static void
    454   1.9   thorpej ifdel(struct interface *ifp)
    455   1.9   thorpej {
    456   1.9   thorpej 	struct ip_mreq m;
    457   1.9   thorpej 	struct interface *ifp1;
    458   1.9   thorpej 
    459   1.9   thorpej 
    460   1.9   thorpej 	trace_if("Del", ifp);
    461   1.9   thorpej 
    462   1.9   thorpej 	ifp->int_state |= IS_BROKE;
    463   1.9   thorpej 
    464   1.9   thorpej 	/* unlink the interface
    465   1.9   thorpej 	 */
    466  1.11  christos 	*ifp->int_prev = ifp->int_next;
    467   1.9   thorpej 	if (ifp->int_next != 0)
    468   1.9   thorpej 		ifp->int_next->int_prev = ifp->int_prev;
    469  1.11  christos 	*ifp->int_ahash_prev = ifp->int_ahash;
    470  1.11  christos 	if (ifp->int_ahash != 0)
    471  1.11  christos 		ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
    472  1.11  christos 	*ifp->int_nhash_prev = ifp->int_nhash;
    473  1.11  christos 	if (ifp->int_nhash != 0)
    474  1.11  christos 		ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
    475  1.11  christos 	if (ifp->int_if_flags & IFF_BROADCAST) {
    476  1.11  christos 		*ifp->int_bhash_prev = ifp->int_bhash;
    477  1.11  christos 		if (ifp->int_bhash != 0)
    478  1.11  christos 			ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
    479  1.11  christos 	}
    480  1.11  christos 	if (ifp->int_state & IS_REMOTE) {
    481  1.11  christos 		*ifp->int_rlink_prev = ifp->int_rlink;
    482  1.11  christos 		if (ifp->int_rlink != 0)
    483  1.11  christos 			ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
    484  1.11  christos 	}
    485   1.9   thorpej 
    486   1.9   thorpej 	if (!(ifp->int_state & IS_ALIAS)) {
    487  1.11  christos 		/* delete aliases when the main interface dies
    488   1.9   thorpej 		 */
    489   1.9   thorpej 		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
    490   1.9   thorpej 			if (ifp1 != ifp
    491   1.9   thorpej 			    && !strcmp(ifp->int_name, ifp1->int_name))
    492   1.9   thorpej 				ifdel(ifp1);
    493   1.9   thorpej 		}
    494   1.9   thorpej 
    495   1.9   thorpej 		if ((ifp->int_if_flags & IFF_MULTICAST)
    496   1.9   thorpej #ifdef MCAST_PPP_BUG
    497   1.9   thorpej 		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
    498   1.9   thorpej #endif
    499   1.9   thorpej 		    && rip_sock >= 0) {
    500   1.9   thorpej 			m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
    501  1.21    itojun #ifdef MCAST_IFINDEX
    502  1.21    itojun 			m.imr_interface.s_addr = htonl(ifp->int_index);
    503  1.21    itojun #else
    504   1.9   thorpej 			m.imr_interface.s_addr = ((ifp->int_if_flags
    505   1.9   thorpej 						   & IFF_POINTOPOINT)
    506   1.9   thorpej 						  ? ifp->int_dstaddr
    507   1.9   thorpej 						  : ifp->int_addr);
    508  1.21    itojun #endif
    509   1.9   thorpej 			if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP,
    510   1.9   thorpej 				       &m, sizeof(m)) < 0
    511   1.9   thorpej 			    && errno != EADDRNOTAVAIL
    512   1.9   thorpej 			    && !TRACEACTIONS)
    513   1.9   thorpej 				LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
    514  1.11  christos 			if (rip_sock_mcast == ifp)
    515  1.11  christos 				rip_sock_mcast = 0;
    516   1.9   thorpej 		}
    517   1.9   thorpej 		if (ifp->int_rip_sock >= 0) {
    518   1.9   thorpej 			(void)close(ifp->int_rip_sock);
    519   1.9   thorpej 			ifp->int_rip_sock = -1;
    520   1.9   thorpej 			fix_select();
    521   1.9   thorpej 		}
    522   1.9   thorpej 
    523   1.9   thorpej 		tot_interfaces--;
    524   1.9   thorpej 		if (!IS_RIP_OFF(ifp->int_state))
    525   1.9   thorpej 			rip_interfaces--;
    526   1.9   thorpej 
    527   1.9   thorpej 		/* Zap all routes associated with this interface.
    528  1.14   thorpej 		 * Assume routes just using gateways beyond this interface
    529  1.14   thorpej 		 * will timeout naturally, and have probably already died.
    530   1.9   thorpej 		 */
    531   1.9   thorpej 		(void)rn_walktree(rhead, walk_bad, 0);
    532   1.9   thorpej 
    533   1.9   thorpej 		set_rdisc_mg(ifp, 0);
    534   1.9   thorpej 		if_bad_rdisc(ifp);
    535   1.9   thorpej 	}
    536   1.9   thorpej 
    537   1.9   thorpej 	free(ifp);
    538   1.9   thorpej }
    539   1.9   thorpej 
    540   1.9   thorpej 
    541   1.9   thorpej /* Mark an interface ill.
    542   1.9   thorpej  */
    543   1.9   thorpej void
    544   1.9   thorpej if_sick(struct interface *ifp)
    545   1.9   thorpej {
    546   1.9   thorpej 	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
    547   1.9   thorpej 		ifp->int_state |= IS_SICK;
    548  1.11  christos 		ifp->int_act_time = NEVER;
    549   1.9   thorpej 		trace_if("Chg", ifp);
    550   1.9   thorpej 
    551   1.9   thorpej 		LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
    552   1.9   thorpej 	}
    553   1.9   thorpej }
    554   1.9   thorpej 
    555   1.9   thorpej 
    556   1.9   thorpej /* Mark an interface dead.
    557   1.9   thorpej  */
    558   1.9   thorpej void
    559   1.9   thorpej if_bad(struct interface *ifp)
    560   1.9   thorpej {
    561   1.9   thorpej 	struct interface *ifp1;
    562   1.9   thorpej 
    563   1.9   thorpej 
    564   1.9   thorpej 	if (ifp->int_state & IS_BROKE)
    565   1.9   thorpej 		return;
    566   1.9   thorpej 
    567   1.9   thorpej 	LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
    568   1.9   thorpej 
    569   1.9   thorpej 	ifp->int_state |= (IS_BROKE | IS_SICK);
    570  1.11  christos 	ifp->int_act_time = NEVER;
    571  1.11  christos 	ifp->int_query_time = NEVER;
    572  1.14   thorpej 	ifp->int_data.ts = now.tv_sec;
    573   1.9   thorpej 
    574   1.9   thorpej 	trace_if("Chg", ifp);
    575   1.9   thorpej 
    576   1.9   thorpej 	if (!(ifp->int_state & IS_ALIAS)) {
    577   1.9   thorpej 		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
    578   1.9   thorpej 			if (ifp1 != ifp
    579   1.9   thorpej 			    && !strcmp(ifp->int_name, ifp1->int_name))
    580   1.9   thorpej 				if_bad(ifp1);
    581   1.9   thorpej 		}
    582   1.9   thorpej 		(void)rn_walktree(rhead, walk_bad, 0);
    583   1.9   thorpej 		if_bad_rdisc(ifp);
    584   1.9   thorpej 	}
    585   1.9   thorpej }
    586   1.9   thorpej 
    587   1.9   thorpej 
    588   1.9   thorpej /* Mark an interface alive
    589   1.9   thorpej  */
    590   1.9   thorpej int					/* 1=it was dead */
    591   1.9   thorpej if_ok(struct interface *ifp,
    592  1.17  christos       const char *type)
    593   1.9   thorpej {
    594   1.9   thorpej 	struct interface *ifp1;
    595   1.9   thorpej 
    596   1.9   thorpej 
    597   1.9   thorpej 	if (!(ifp->int_state & IS_BROKE)) {
    598   1.9   thorpej 		if (ifp->int_state & IS_SICK) {
    599  1.11  christos 			trace_act("%sinterface %s to %s working better",
    600   1.9   thorpej 				  type,
    601  1.11  christos 				  ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
    602   1.9   thorpej 			ifp->int_state &= ~IS_SICK;
    603   1.9   thorpej 		}
    604   1.9   thorpej 		return 0;
    605   1.9   thorpej 	}
    606   1.9   thorpej 
    607   1.9   thorpej 	msglog("%sinterface %s to %s restored",
    608  1.11  christos 	       type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
    609   1.9   thorpej 	ifp->int_state &= ~(IS_BROKE | IS_SICK);
    610   1.9   thorpej 	ifp->int_data.ts = 0;
    611   1.9   thorpej 
    612   1.9   thorpej 	if (!(ifp->int_state & IS_ALIAS)) {
    613   1.9   thorpej 		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
    614   1.9   thorpej 			if (ifp1 != ifp
    615   1.9   thorpej 			    && !strcmp(ifp->int_name, ifp1->int_name))
    616   1.9   thorpej 				if_ok(ifp1, type);
    617   1.9   thorpej 		}
    618   1.9   thorpej 		if_ok_rdisc(ifp);
    619   1.9   thorpej 	}
    620  1.11  christos 
    621  1.11  christos 	if (ifp->int_state & IS_REMOTE) {
    622  1.11  christos 		if (!addrouteforif(ifp))
    623  1.11  christos 			return 0;
    624  1.11  christos 	}
    625   1.9   thorpej 	return 1;
    626   1.9   thorpej }
    627   1.9   thorpej 
    628   1.9   thorpej 
    629   1.9   thorpej /* disassemble routing message
    630   1.9   thorpej  */
    631   1.9   thorpej void
    632   1.9   thorpej rt_xaddrs(struct rt_addrinfo *info,
    633   1.9   thorpej 	  struct sockaddr *sa,
    634   1.9   thorpej 	  struct sockaddr *lim,
    635   1.9   thorpej 	  int addrs)
    636   1.9   thorpej {
    637   1.9   thorpej 	int i;
    638   1.9   thorpej #ifdef _HAVE_SA_LEN
    639   1.9   thorpej 	static struct sockaddr sa_zero;
    640   1.9   thorpej #endif
    641   1.9   thorpej #ifdef sgi
    642   1.9   thorpej #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
    643   1.9   thorpej 		    : sizeof(__uint64_t))
    644   1.9   thorpej #else
    645   1.9   thorpej #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
    646   1.9   thorpej 		    : sizeof(long))
    647   1.9   thorpej #endif
    648   1.9   thorpej 
    649   1.9   thorpej 
    650  1.12     lukem 	memset(info, 0, sizeof(*info));
    651   1.9   thorpej 	info->rti_addrs = addrs;
    652   1.9   thorpej 	for (i = 0; i < RTAX_MAX && sa < lim; i++) {
    653   1.9   thorpej 		if ((addrs & (1 << i)) == 0)
    654   1.9   thorpej 			continue;
    655   1.9   thorpej #ifdef _HAVE_SA_LEN
    656   1.9   thorpej 		info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
    657   1.9   thorpej 		sa = (struct sockaddr *)((char*)(sa)
    658   1.9   thorpej 					 + ROUNDUP(sa->sa_len));
    659   1.9   thorpej #else
    660   1.9   thorpej 		info->rti_info[i] = sa;
    661   1.9   thorpej 		sa = (struct sockaddr *)((char*)(sa)
    662   1.9   thorpej 					 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
    663   1.9   thorpej #endif
    664   1.9   thorpej 	}
    665   1.9   thorpej }
    666   1.9   thorpej 
    667   1.9   thorpej 
    668   1.9   thorpej /* Find the network interfaces which have configured themselves.
    669   1.9   thorpej  *	This must be done regularly, if only for extra addresses
    670   1.9   thorpej  *	that come and go on interfaces.
    671   1.9   thorpej  */
    672   1.9   thorpej void
    673   1.9   thorpej ifinit(void)
    674   1.9   thorpej {
    675   1.9   thorpej 	static char *sysctl_buf;
    676   1.9   thorpej 	static size_t sysctl_buf_size = 0;
    677   1.9   thorpej 	uint complaints = 0;
    678   1.9   thorpej 	static u_int prev_complaints = 0;
    679  1.10  christos #	define COMP_NOT_INET	0x001
    680  1.11  christos #	define COMP_NOADDR	0x002
    681  1.11  christos #	define COMP_BADADDR	0x004
    682  1.11  christos #	define COMP_NODST	0x008
    683  1.11  christos #	define COMP_NOBADR	0x010
    684  1.11  christos #	define COMP_NOMASK	0x020
    685  1.11  christos #	define COMP_DUP		0x040
    686  1.11  christos #	define COMP_BAD_METRIC	0x080
    687  1.11  christos #	define COMP_NETMASK	0x100
    688   1.9   thorpej 
    689   1.9   thorpej 	struct interface ifs, ifs0, *ifp, *ifp1;
    690   1.9   thorpej 	struct rt_entry *rt;
    691   1.9   thorpej 	size_t needed;
    692   1.9   thorpej 	int mib[6];
    693   1.9   thorpej 	struct if_msghdr *ifm;
    694   1.9   thorpej 	struct ifa_msghdr *ifam, *ifam_lim, *ifam2;
    695   1.9   thorpej 	int in, ierr, out, oerr;
    696   1.9   thorpej 	struct intnet *intnetp;
    697   1.9   thorpej 	struct rt_addrinfo info;
    698   1.9   thorpej #ifdef SIOCGIFMETRIC
    699   1.9   thorpej 	struct ifreq ifr;
    700   1.9   thorpej #endif
    701   1.9   thorpej 
    702   1.9   thorpej 
    703  1.11  christos 	last_ifinit = now;
    704   1.9   thorpej 	ifinit_timer.tv_sec = now.tv_sec + (supplier
    705   1.9   thorpej 					    ? CHECK_ACT_INTERVAL
    706   1.9   thorpej 					    : CHECK_QUIET_INTERVAL);
    707   1.9   thorpej 
    708  1.16  christos 	/* mark all interfaces so we can get rid of those that disappear */
    709   1.9   thorpej 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next)
    710   1.9   thorpej 		ifp->int_state &= ~(IS_CHECKED | IS_DUP);
    711   1.9   thorpej 
    712   1.9   thorpej 	/* Fetch the interface list, without too many system calls
    713   1.9   thorpej 	 * since we do it repeatedly.
    714   1.9   thorpej 	 */
    715   1.9   thorpej 	mib[0] = CTL_NET;
    716   1.9   thorpej 	mib[1] = PF_ROUTE;
    717   1.9   thorpej 	mib[2] = 0;
    718   1.9   thorpej 	mib[3] = AF_INET;
    719   1.9   thorpej 	mib[4] = NET_RT_IFLIST;
    720   1.9   thorpej 	mib[5] = 0;
    721   1.9   thorpej 	for (;;) {
    722   1.9   thorpej 		if ((needed = sysctl_buf_size) != 0) {
    723   1.9   thorpej 			if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
    724   1.9   thorpej 				break;
    725  1.14   thorpej 			/* retry if the table grew */
    726   1.9   thorpej 			if (errno != ENOMEM && errno != EFAULT)
    727  1.14   thorpej 				BADERR(1, "ifinit: sysctl(RT_IFLIST)");
    728   1.9   thorpej 			free(sysctl_buf);
    729   1.9   thorpej 			needed = 0;
    730   1.9   thorpej 		}
    731   1.9   thorpej 		if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
    732  1.14   thorpej 			BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
    733  1.14   thorpej 		sysctl_buf = rtmalloc(sysctl_buf_size = needed,
    734  1.14   thorpej 				      "ifinit sysctl");
    735   1.9   thorpej 	}
    736   1.9   thorpej 
    737   1.9   thorpej 	ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
    738   1.9   thorpej 	for (ifam = (struct ifa_msghdr *)sysctl_buf;
    739   1.9   thorpej 	     ifam < ifam_lim;
    740   1.9   thorpej 	     ifam = ifam2) {
    741   1.9   thorpej 
    742   1.9   thorpej 		ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
    743   1.9   thorpej 
    744  1.18    bouyer #ifdef RTM_OIFINFO
    745  1.18    bouyer 		if (ifam->ifam_type == RTM_OIFINFO) {
    746  1.18    bouyer 			continue; /* just ignore compat message */
    747  1.18    bouyer 		}
    748  1.18    bouyer #endif
    749   1.9   thorpej 		if (ifam->ifam_type == RTM_IFINFO) {
    750  1.11  christos 			struct sockaddr_dl *sdl;
    751  1.11  christos 
    752   1.9   thorpej 			ifm = (struct if_msghdr *)ifam;
    753   1.9   thorpej 			/* make prototype structure for the IP aliases
    754   1.9   thorpej 			 */
    755  1.12     lukem 			memset(&ifs0, 0, sizeof(ifs0));
    756   1.9   thorpej 			ifs0.int_rip_sock = -1;
    757   1.9   thorpej 			ifs0.int_index = ifm->ifm_index;
    758   1.9   thorpej 			ifs0.int_if_flags = ifm->ifm_flags;
    759   1.9   thorpej 			ifs0.int_state = IS_CHECKED;
    760  1.11  christos 			ifs0.int_query_time = NEVER;
    761   1.9   thorpej 			ifs0.int_act_time = now.tv_sec;
    762   1.9   thorpej 			ifs0.int_data.ts = now.tv_sec;
    763   1.9   thorpej 			ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
    764   1.9   thorpej 			ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
    765   1.9   thorpej 			ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
    766   1.9   thorpej 			ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
    767   1.9   thorpej #ifdef sgi
    768   1.9   thorpej 			ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
    769   1.9   thorpej #endif
    770   1.9   thorpej 			sdl = (struct sockaddr_dl *)(ifm + 1);
    771   1.9   thorpej 			sdl->sdl_data[sdl->sdl_nlen] = 0;
    772  1.11  christos 			strncpy(ifs0.int_name, sdl->sdl_data,
    773  1.11  christos 				MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
    774   1.9   thorpej 			continue;
    775   1.9   thorpej 		}
    776   1.9   thorpej 		if (ifam->ifam_type != RTM_NEWADDR) {
    777  1.10  christos 			logbad(1,"ifinit: out of sync");
    778   1.9   thorpej 			continue;
    779   1.9   thorpej 		}
    780   1.9   thorpej 		rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
    781   1.9   thorpej 			  (struct sockaddr *)ifam2,
    782   1.9   thorpej 			  ifam->ifam_addrs);
    783   1.9   thorpej 
    784  1.11  christos 		/* Prepare for the next address of this interface, which
    785  1.11  christos 		 * will be an alias.
    786  1.11  christos 		 * Do not output RIP or Router-Discovery packets via aliases.
    787  1.11  christos 		 */
    788  1.16  christos 		memcpy(&ifs, &ifs0, sizeof(ifs));
    789  1.14   thorpej 		ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
    790  1.11  christos 
    791   1.9   thorpej 		if (INFO_IFA(&info) == 0) {
    792  1.14   thorpej 			if (iff_up(ifs.int_if_flags)) {
    793   1.9   thorpej 				if (!(prev_complaints & COMP_NOADDR))
    794  1.10  christos 					msglog("%s has no address",
    795  1.11  christos 					       ifs.int_name);
    796   1.9   thorpej 				complaints |= COMP_NOADDR;
    797   1.9   thorpej 			}
    798   1.9   thorpej 			continue;
    799   1.9   thorpej 		}
    800   1.9   thorpej 		if (INFO_IFA(&info)->sa_family != AF_INET) {
    801  1.14   thorpej 			if (iff_up(ifs.int_if_flags)) {
    802   1.9   thorpej 				if (!(prev_complaints & COMP_NOT_INET))
    803  1.11  christos 					trace_act("%s: not AF_INET",
    804  1.11  christos 						  ifs.int_name);
    805   1.9   thorpej 				complaints |= COMP_NOT_INET;
    806   1.9   thorpej 			}
    807   1.9   thorpej 			continue;
    808   1.9   thorpej 		}
    809   1.9   thorpej 
    810   1.9   thorpej 		ifs.int_addr = S_ADDR(INFO_IFA(&info));
    811   1.9   thorpej 
    812  1.10  christos 		if (ntohl(ifs.int_addr)>>24 == 0
    813  1.10  christos 		    || ntohl(ifs.int_addr)>>24 == 0xff) {
    814  1.14   thorpej 			if (iff_up(ifs.int_if_flags)) {
    815  1.10  christos 				if (!(prev_complaints & COMP_BADADDR))
    816  1.10  christos 					msglog("%s has a bad address",
    817  1.11  christos 					       ifs.int_name);
    818  1.10  christos 				complaints |= COMP_BADADDR;
    819  1.10  christos 			}
    820  1.10  christos 			continue;
    821  1.10  christos 		}
    822  1.10  christos 
    823  1.11  christos 		if (ifs.int_if_flags & IFF_LOOPBACK) {
    824  1.11  christos 			ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
    825   1.9   thorpej 			ifs.int_dstaddr = ifs.int_addr;
    826  1.11  christos 			ifs.int_mask = HOST_MASK;
    827  1.11  christos 			ifs.int_ripv1_mask = HOST_MASK;
    828  1.11  christos 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
    829  1.11  christos 			ifs.int_net = ntohl(ifs.int_dstaddr);
    830  1.11  christos 			if (!foundloopback) {
    831  1.11  christos 				foundloopback = 1;
    832  1.11  christos 				loopaddr = ifs.int_addr;
    833  1.14   thorpej 				loop_rts.rts_gate = loopaddr;
    834  1.14   thorpej 				loop_rts.rts_router = loopaddr;
    835   1.9   thorpej 			}
    836   1.9   thorpej 
    837   1.9   thorpej 		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
    838   1.9   thorpej 			if (INFO_BRD(&info) == 0
    839   1.9   thorpej 			    || INFO_BRD(&info)->sa_family != AF_INET) {
    840  1.14   thorpej 				if (iff_up(ifs.int_if_flags)) {
    841   1.9   thorpej 					if (!(prev_complaints & COMP_NODST))
    842   1.9   thorpej 						msglog("%s has a bad"
    843   1.9   thorpej 						       " destination address",
    844  1.11  christos 						       ifs.int_name);
    845   1.9   thorpej 					complaints |= COMP_NODST;
    846   1.9   thorpej 				}
    847   1.9   thorpej 				continue;
    848   1.9   thorpej 			}
    849   1.9   thorpej 			ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
    850  1.10  christos 			if (ntohl(ifs.int_dstaddr)>>24 == 0
    851  1.10  christos 			    || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
    852  1.14   thorpej 				if (iff_up(ifs.int_if_flags)) {
    853  1.10  christos 					if (!(prev_complaints & COMP_NODST))
    854  1.10  christos 						msglog("%s has a bad"
    855  1.10  christos 						       " destination address",
    856  1.11  christos 						       ifs.int_name);
    857  1.10  christos 					complaints |= COMP_NODST;
    858  1.10  christos 				}
    859  1.10  christos 				continue;
    860  1.10  christos 			}
    861   1.9   thorpej 			ifs.int_mask = HOST_MASK;
    862   1.9   thorpej 			ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
    863  1.11  christos 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
    864   1.9   thorpej 			ifs.int_net = ntohl(ifs.int_dstaddr);
    865   1.9   thorpej 
    866  1.11  christos 		}  else {
    867  1.11  christos 			if (INFO_MASK(&info) == 0) {
    868  1.14   thorpej 				if (iff_up(ifs.int_if_flags)) {
    869  1.11  christos 					if (!(prev_complaints & COMP_NOMASK))
    870  1.11  christos 						msglog("%s has no netmask",
    871  1.11  christos 						       ifs.int_name);
    872  1.11  christos 					complaints |= COMP_NOMASK;
    873  1.11  christos 				}
    874  1.11  christos 				continue;
    875  1.11  christos 			}
    876   1.9   thorpej 			ifs.int_dstaddr = ifs.int_addr;
    877  1.11  christos 			ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
    878  1.11  christos 			ifs.int_ripv1_mask = ifs.int_mask;
    879  1.11  christos 			ifs.int_std_mask = std_mask(ifs.int_addr);
    880  1.11  christos 			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
    881  1.11  christos 			if (ifs.int_mask != ifs.int_std_mask)
    882  1.11  christos 				ifs.int_state |= IS_SUBNET;
    883  1.11  christos 
    884  1.11  christos 			if (ifs.int_if_flags & IFF_BROADCAST) {
    885  1.11  christos 				if (INFO_BRD(&info) == 0) {
    886  1.14   thorpej 					if (iff_up(ifs.int_if_flags)) {
    887  1.11  christos 					    if (!(prev_complaints
    888  1.11  christos 						  & COMP_NOBADR))
    889  1.11  christos 						msglog("%s has"
    890  1.11  christos 						       "no broadcast address",
    891  1.11  christos 						       ifs.int_name);
    892  1.11  christos 					    complaints |= COMP_NOBADR;
    893  1.11  christos 					}
    894  1.11  christos 					continue;
    895  1.11  christos 				}
    896  1.11  christos 				ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
    897   1.9   thorpej 			}
    898   1.9   thorpej 		}
    899   1.9   thorpej 		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
    900   1.9   thorpej 		ifs.int_std_addr = htonl(ifs.int_std_net);
    901   1.9   thorpej 
    902   1.9   thorpej 		/* Use a minimum metric of one.  Treat the interface metric
    903   1.9   thorpej 		 * (default 0) as an increment to the hop count of one.
    904   1.9   thorpej 		 *
    905   1.9   thorpej 		 * The metric obtained from the routing socket dump of
    906   1.9   thorpej 		 * interface addresses is wrong.  It is not set by the
    907   1.9   thorpej 		 * SIOCSIFMETRIC ioctl.
    908   1.9   thorpej 		 */
    909   1.9   thorpej #ifdef SIOCGIFMETRIC
    910  1.11  christos 		strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
    911   1.9   thorpej 		if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
    912   1.9   thorpej 			DBGERR(1, "ioctl(SIOCGIFMETRIC)");
    913   1.9   thorpej 			ifs.int_metric = 0;
    914   1.9   thorpej 		} else {
    915   1.9   thorpej 			ifs.int_metric = ifr.ifr_metric;
    916   1.9   thorpej 		}
    917   1.9   thorpej #else
    918   1.9   thorpej 		ifs.int_metric = ifam->ifam_metric;
    919   1.9   thorpej #endif
    920   1.9   thorpej 		if (ifs.int_metric > HOPCNT_INFINITY) {
    921   1.9   thorpej 			ifs.int_metric = 0;
    922   1.9   thorpej 			if (!(prev_complaints & COMP_BAD_METRIC)
    923  1.14   thorpej 			    && iff_up(ifs.int_if_flags)) {
    924   1.9   thorpej 				complaints |= COMP_BAD_METRIC;
    925   1.9   thorpej 				msglog("%s has a metric of %d",
    926  1.11  christos 				       ifs.int_name, ifs.int_metric);
    927   1.9   thorpej 			}
    928   1.9   thorpej 		}
    929   1.9   thorpej 
    930   1.9   thorpej 		/* See if this is a familiar interface.
    931   1.9   thorpej 		 * If so, stop worrying about it if it is the same.
    932   1.9   thorpej 		 * Start it over if it now is to somewhere else, as happens
    933   1.9   thorpej 		 * frequently with PPP and SLIP.
    934   1.9   thorpej 		 */
    935  1.11  christos 		ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
    936  1.11  christos 						? ifs.int_addr
    937  1.11  christos 						: 0));
    938   1.9   thorpej 		if (ifp != 0) {
    939   1.9   thorpej 			ifp->int_state |= IS_CHECKED;
    940   1.9   thorpej 
    941   1.9   thorpej 			if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
    942   1.9   thorpej 				  & (IFF_BROADCAST
    943   1.9   thorpej 				     | IFF_LOOPBACK
    944   1.9   thorpej 				     | IFF_POINTOPOINT
    945   1.9   thorpej 				     | IFF_MULTICAST))
    946   1.9   thorpej 			    || 0 != ((ifp->int_state ^ ifs.int_state)
    947   1.9   thorpej 				     & IS_ALIAS)
    948   1.9   thorpej 			    || ifp->int_addr != ifs.int_addr
    949   1.9   thorpej 			    || ifp->int_brdaddr != ifs.int_brdaddr
    950   1.9   thorpej 			    || ifp->int_dstaddr != ifs.int_dstaddr
    951   1.9   thorpej 			    || ifp->int_mask != ifs.int_mask
    952   1.9   thorpej 			    || ifp->int_metric != ifs.int_metric) {
    953   1.9   thorpej 				/* Forget old information about
    954   1.9   thorpej 				 * a changed interface.
    955   1.9   thorpej 				 */
    956  1.11  christos 				trace_act("interface %s has changed",
    957   1.9   thorpej 					  ifp->int_name);
    958   1.9   thorpej 				ifdel(ifp);
    959   1.9   thorpej 				ifp = 0;
    960   1.9   thorpej 			}
    961   1.9   thorpej 		}
    962   1.9   thorpej 
    963   1.9   thorpej 		if (ifp != 0) {
    964  1.10  christos 			/* The primary representative of an alias worries
    965  1.10  christos 			 * about how things are working.
    966  1.10  christos 			 */
    967   1.9   thorpej 			if (ifp->int_state & IS_ALIAS)
    968   1.9   thorpej 				continue;
    969   1.9   thorpej 
    970   1.9   thorpej 			/* note interfaces that have been turned off
    971   1.9   thorpej 			 */
    972  1.14   thorpej 			if (!iff_up(ifs.int_if_flags)) {
    973  1.14   thorpej 				if (iff_up(ifp->int_if_flags)) {
    974   1.9   thorpej 					msglog("interface %s to %s turned off",
    975   1.9   thorpej 					       ifp->int_name,
    976  1.11  christos 					       naddr_ntoa(ifp->int_dstaddr));
    977   1.9   thorpej 					if_bad(ifp);
    978  1.14   thorpej 					ifp->int_if_flags &= ~IFF_UP;
    979  1.14   thorpej 				} else if (now.tv_sec>(ifp->int_data.ts
    980  1.14   thorpej 						       + CHECK_BAD_INTERVAL)) {
    981  1.14   thorpej 					trace_act("interface %s has been off"
    982  1.17  christos 						  " %ld seconds; forget it",
    983  1.14   thorpej 						  ifp->int_name,
    984  1.14   thorpej 						  now.tv_sec-ifp->int_data.ts);
    985  1.14   thorpej 					ifdel(ifp);
    986   1.9   thorpej 				}
    987   1.9   thorpej 				continue;
    988   1.9   thorpej 			}
    989   1.9   thorpej 			/* or that were off and are now ok */
    990  1.14   thorpej 			if (!iff_up(ifp->int_if_flags)) {
    991  1.14   thorpej 				ifp->int_if_flags |= IFF_UP;
    992   1.9   thorpej 				(void)if_ok(ifp, "");
    993   1.9   thorpej 			}
    994   1.9   thorpej 
    995   1.9   thorpej 			/* If it has been long enough,
    996   1.9   thorpej 			 * see if the interface is broken.
    997   1.9   thorpej 			 */
    998   1.9   thorpej 			if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
    999   1.9   thorpej 				continue;
   1000   1.9   thorpej 
   1001   1.9   thorpej 			in = ifs.int_data.ipackets - ifp->int_data.ipackets;
   1002   1.9   thorpej 			ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
   1003   1.9   thorpej 			out = ifs.int_data.opackets - ifp->int_data.opackets;
   1004   1.9   thorpej 			oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
   1005   1.9   thorpej #ifdef sgi
   1006   1.9   thorpej 			/* Through at least IRIX 6.2, PPP and SLIP
   1007  1.11  christos 			 * count packets dropped by the filters.
   1008   1.9   thorpej 			 * But FDDI rings stuck non-operational count
   1009   1.9   thorpej 			 * dropped packets as they wait for improvement.
   1010   1.9   thorpej 			 */
   1011   1.9   thorpej 			if (!(ifp->int_if_flags & IFF_POINTOPOINT))
   1012   1.9   thorpej 				oerr += (ifs.int_data.odrops
   1013   1.9   thorpej 					 - ifp->int_data.odrops);
   1014   1.9   thorpej #endif
   1015   1.9   thorpej 			/* If the interface just awoke, restart the counters.
   1016   1.9   thorpej 			 */
   1017   1.9   thorpej 			if (ifp->int_data.ts == 0) {
   1018   1.9   thorpej 				ifp->int_data = ifs.int_data;
   1019   1.9   thorpej 				continue;
   1020   1.9   thorpej 			}
   1021   1.9   thorpej 			ifp->int_data = ifs.int_data;
   1022   1.9   thorpej 
   1023  1.16  christos 			/* Withhold judgment when the short error
   1024   1.9   thorpej 			 * counters wrap or the interface is reset.
   1025   1.9   thorpej 			 */
   1026   1.9   thorpej 			if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
   1027   1.9   thorpej 				LIM_SEC(ifinit_timer,
   1028   1.9   thorpej 					now.tv_sec+CHECK_BAD_INTERVAL);
   1029   1.9   thorpej 				continue;
   1030   1.9   thorpej 			}
   1031   1.9   thorpej 
   1032   1.9   thorpej 			/* Withhold judgement when there is no traffic
   1033   1.9   thorpej 			 */
   1034   1.9   thorpej 			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
   1035   1.9   thorpej 				continue;
   1036   1.9   thorpej 
   1037   1.9   thorpej 			/* It is bad if input or output is not working.
   1038   1.9   thorpej 			 * Require presistent problems before marking it dead.
   1039   1.9   thorpej 			 */
   1040   1.9   thorpej 			if ((in <= ierr && ierr > 0)
   1041   1.9   thorpej 			    || (out <= oerr && oerr > 0)) {
   1042   1.9   thorpej 				if (!(ifp->int_state & IS_SICK)) {
   1043   1.9   thorpej 					trace_act("interface %s to %s"
   1044   1.9   thorpej 						  " sick: in=%d ierr=%d"
   1045  1.11  christos 						  " out=%d oerr=%d",
   1046   1.9   thorpej 						  ifp->int_name,
   1047  1.11  christos 						  naddr_ntoa(ifp->int_dstaddr),
   1048   1.9   thorpej 						  in, ierr, out, oerr);
   1049   1.9   thorpej 					if_sick(ifp);
   1050   1.9   thorpej 					continue;
   1051   1.9   thorpej 				}
   1052   1.9   thorpej 				if (!(ifp->int_state & IS_BROKE)) {
   1053  1.11  christos 					msglog("interface %s to %s broken:"
   1054   1.9   thorpej 					       " in=%d ierr=%d out=%d oerr=%d",
   1055   1.9   thorpej 					       ifp->int_name,
   1056  1.11  christos 					       naddr_ntoa(ifp->int_dstaddr),
   1057   1.9   thorpej 					       in, ierr, out, oerr);
   1058   1.9   thorpej 					if_bad(ifp);
   1059   1.9   thorpej 				}
   1060   1.9   thorpej 				continue;
   1061   1.9   thorpej 			}
   1062   1.9   thorpej 
   1063   1.9   thorpej 			/* otherwise, it is active and healthy
   1064   1.9   thorpej 			 */
   1065   1.9   thorpej 			ifp->int_act_time = now.tv_sec;
   1066   1.9   thorpej 			(void)if_ok(ifp, "");
   1067   1.9   thorpej 			continue;
   1068   1.9   thorpej 		}
   1069   1.9   thorpej 
   1070   1.9   thorpej 		/* This is a new interface.
   1071   1.9   thorpej 		 * If it is dead, forget it.
   1072   1.9   thorpej 		 */
   1073  1.14   thorpej 		if (!iff_up(ifs.int_if_flags))
   1074   1.9   thorpej 			continue;
   1075   1.9   thorpej 
   1076  1.11  christos 		/* If it duplicates an existing interface,
   1077  1.11  christos 		 * complain about it, mark the other one
   1078  1.11  christos 		 * duplicated, and forget this one.
   1079   1.9   thorpej 		 */
   1080  1.11  christos 		ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
   1081  1.11  christos 				ifs.int_if_flags);
   1082  1.11  christos 		if (ifp != 0) {
   1083  1.11  christos 			/* Ignore duplicates of itself, caused by having
   1084  1.11  christos 			 * IP aliases on the same network.
   1085   1.9   thorpej 			 */
   1086  1.11  christos 			if (!strcmp(ifp->int_name, ifs.int_name))
   1087   1.9   thorpej 				continue;
   1088   1.9   thorpej 
   1089   1.9   thorpej 			if (!(prev_complaints & COMP_DUP)) {
   1090   1.9   thorpej 				complaints |= COMP_DUP;
   1091  1.11  christos 				msglog("%s (%s%s%s) is duplicated by"
   1092  1.11  christos 				       " %s (%s%s%s)",
   1093  1.11  christos 				       ifs.int_name,
   1094  1.11  christos 				       addrname(ifs.int_addr,ifs.int_mask,1),
   1095  1.11  christos 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
   1096  1.11  christos 					? "-->" : ""),
   1097  1.11  christos 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
   1098  1.11  christos 					? naddr_ntoa(ifs.int_dstaddr) : ""),
   1099  1.11  christos 				       ifp->int_name,
   1100  1.11  christos 				       addrname(ifp->int_addr,ifp->int_mask,1),
   1101  1.11  christos 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
   1102  1.11  christos 					? "-->" : ""),
   1103  1.11  christos 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
   1104  1.11  christos 					? naddr_ntoa(ifp->int_dstaddr) : ""));
   1105   1.9   thorpej 			}
   1106   1.9   thorpej 			ifp->int_state |= IS_DUP;
   1107  1.11  christos 			continue;
   1108  1.11  christos 		}
   1109  1.11  christos 
   1110  1.11  christos 		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST))
   1111  1.11  christos 		    && !(ifs.int_state & IS_PASSIVE)) {
   1112  1.11  christos 			trace_act("%s is neither broadcast, point-to-point,"
   1113  1.11  christos 				  " nor loopback",
   1114  1.11  christos 				  ifs.int_name);
   1115  1.11  christos 			if (!(ifs.int_state & IFF_MULTICAST))
   1116  1.11  christos 				ifs.int_state |= IS_NO_RDISC;
   1117   1.9   thorpej 		}
   1118   1.9   thorpej 
   1119   1.9   thorpej 
   1120  1.11  christos 		/* It is new and ok.   Add it to the list of interfaces
   1121   1.9   thorpej 		 */
   1122  1.14   thorpej 		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
   1123  1.16  christos 		memcpy(ifp, &ifs, sizeof(*ifp));
   1124  1.11  christos 		get_parms(ifp);
   1125  1.11  christos 		if_link(ifp);
   1126   1.9   thorpej 		trace_if("Add", ifp);
   1127   1.9   thorpej 
   1128  1.10  christos 		/* Notice likely bad netmask.
   1129  1.10  christos 		 */
   1130  1.10  christos 		if (!(prev_complaints & COMP_NETMASK)
   1131  1.11  christos 		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
   1132  1.11  christos 		    && ifp->int_addr != RIP_DEFAULT) {
   1133  1.10  christos 			for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
   1134  1.10  christos 				if (ifp1->int_mask == ifp->int_mask)
   1135  1.10  christos 					continue;
   1136  1.10  christos 				if (ifp1->int_if_flags & IFF_POINTOPOINT)
   1137  1.10  christos 					continue;
   1138  1.11  christos 				if (ifp1->int_dstaddr == RIP_DEFAULT)
   1139  1.11  christos 					continue;
   1140  1.11  christos 				if (on_net(ifp->int_dstaddr,
   1141  1.10  christos 					   ifp1->int_net, ifp1->int_mask)
   1142  1.11  christos 				    || on_net(ifp1->int_dstaddr,
   1143  1.10  christos 					      ifp->int_net, ifp->int_mask)) {
   1144  1.10  christos 					msglog("possible netmask problem"
   1145  1.11  christos 					       " between %s:%s and %s:%s",
   1146  1.10  christos 					       ifp->int_name,
   1147  1.10  christos 					       addrname(htonl(ifp->int_net),
   1148  1.10  christos 							ifp->int_mask, 1),
   1149  1.10  christos 					       ifp1->int_name,
   1150  1.10  christos 					       addrname(htonl(ifp1->int_net),
   1151  1.10  christos 							ifp1->int_mask, 1));
   1152  1.10  christos 					complaints |= COMP_NETMASK;
   1153  1.10  christos 				}
   1154  1.10  christos 			}
   1155  1.10  christos 		}
   1156  1.10  christos 
   1157   1.9   thorpej 		if (!(ifp->int_state & IS_ALIAS)) {
   1158  1.11  christos 			/* Count the # of directly connected networks.
   1159  1.11  christos 			 */
   1160   1.9   thorpej 			if (!(ifp->int_if_flags & IFF_LOOPBACK))
   1161   1.9   thorpej 				tot_interfaces++;
   1162   1.9   thorpej 			if (!IS_RIP_OFF(ifp->int_state))
   1163   1.9   thorpej 				rip_interfaces++;
   1164  1.11  christos 
   1165  1.11  christos 			/* turn on router discovery and RIP If needed */
   1166  1.11  christos 			if_ok_rdisc(ifp);
   1167  1.11  christos 			rip_on(ifp);
   1168   1.9   thorpej 		}
   1169   1.9   thorpej 	}
   1170   1.9   thorpej 
   1171  1.11  christos 	/* If we are multi-homed and have at least two interfaces
   1172   1.9   thorpej 	 * listening to RIP, then output by default.
   1173   1.9   thorpej 	 */
   1174   1.9   thorpej 	if (!supplier_set && rip_interfaces > 1)
   1175   1.9   thorpej 		set_supplier();
   1176   1.9   thorpej 
   1177   1.9   thorpej 	/* If we are multi-homed, optionally advertise a route to
   1178   1.9   thorpej 	 * our main address.
   1179   1.9   thorpej 	 */
   1180   1.9   thorpej 	if (advertise_mhome
   1181   1.9   thorpej 	    || (tot_interfaces > 1
   1182   1.9   thorpej 		&& mhome
   1183   1.9   thorpej 		&& (ifp = ifwithaddr(myaddr, 0, 0)) != 0
   1184   1.9   thorpej 		&& foundloopback)) {
   1185   1.9   thorpej 		advertise_mhome = 1;
   1186   1.9   thorpej 		rt = rtget(myaddr, HOST_MASK);
   1187   1.9   thorpej 		if (rt != 0) {
   1188   1.9   thorpej 			if (rt->rt_ifp != ifp
   1189   1.9   thorpej 			    || rt->rt_router != loopaddr) {
   1190   1.9   thorpej 				rtdelete(rt);
   1191   1.9   thorpej 				rt = 0;
   1192   1.9   thorpej 			} else {
   1193  1.14   thorpej 				loop_rts.rts_ifp = ifp;
   1194  1.14   thorpej 				loop_rts.rts_metric = 0;
   1195  1.14   thorpej 				loop_rts.rts_time = rt->rt_time;
   1196   1.9   thorpej 				rtchange(rt, rt->rt_state | RS_MHOME,
   1197  1.14   thorpej 					 &loop_rts, 0);
   1198   1.9   thorpej 			}
   1199   1.9   thorpej 		}
   1200  1.14   thorpej 		if (rt == 0) {
   1201  1.14   thorpej 			loop_rts.rts_ifp = ifp;
   1202  1.14   thorpej 			loop_rts.rts_metric = 0;
   1203  1.14   thorpej 			rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
   1204  1.14   thorpej 		}
   1205   1.9   thorpej 	}
   1206   1.9   thorpej 
   1207   1.9   thorpej 	for (ifp = ifnet; ifp != 0; ifp = ifp1) {
   1208   1.9   thorpej 		ifp1 = ifp->int_next;	/* because we may delete it */
   1209   1.9   thorpej 
   1210   1.9   thorpej 		/* Forget any interfaces that have disappeared.
   1211   1.9   thorpej 		 */
   1212   1.9   thorpej 		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
   1213  1.11  christos 			trace_act("interface %s has disappeared",
   1214   1.9   thorpej 				  ifp->int_name);
   1215   1.9   thorpej 			ifdel(ifp);
   1216   1.9   thorpej 			continue;
   1217   1.9   thorpej 		}
   1218   1.9   thorpej 
   1219   1.9   thorpej 		if ((ifp->int_state & IS_BROKE)
   1220   1.9   thorpej 		    && !(ifp->int_state & IS_PASSIVE))
   1221   1.9   thorpej 			LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
   1222   1.9   thorpej 
   1223   1.9   thorpej 		/* If we ever have a RIPv1 interface, assume we always will.
   1224   1.9   thorpej 		 * It might come back if it ever goes away.
   1225   1.9   thorpej 		 */
   1226  1.10  christos 		if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
   1227  1.10  christos 			have_ripv1_out = 1;
   1228  1.10  christos 		if (!(ifp->int_state & IS_NO_RIPV1_IN))
   1229  1.10  christos 			have_ripv1_in = 1;
   1230   1.9   thorpej 	}
   1231   1.9   thorpej 
   1232   1.9   thorpej 	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
   1233   1.9   thorpej 		/* Ensure there is always a network route for interfaces,
   1234   1.9   thorpej 		 * after any dead interfaces have been deleted, which
   1235   1.9   thorpej 		 * might affect routes for point-to-point links.
   1236   1.9   thorpej 		 */
   1237  1.11  christos 		if (!addrouteforif(ifp))
   1238  1.11  christos 			continue;
   1239   1.9   thorpej 
   1240   1.9   thorpej 		/* Add routes to the local end of point-to-point interfaces
   1241   1.9   thorpej 		 * using loopback.
   1242   1.9   thorpej 		 */
   1243   1.9   thorpej 		if ((ifp->int_if_flags & IFF_POINTOPOINT)
   1244   1.9   thorpej 		    && !(ifp->int_state & IS_REMOTE)
   1245   1.9   thorpej 		    && foundloopback) {
   1246   1.9   thorpej 			/* Delete any routes to the network address through
   1247   1.9   thorpej 			 * foreign routers. Remove even static routes.
   1248   1.9   thorpej 			 */
   1249  1.14   thorpej 			del_static(ifp->int_addr, HOST_MASK, 0, 0);
   1250   1.9   thorpej 			rt = rtget(ifp->int_addr, HOST_MASK);
   1251   1.9   thorpej 			if (rt != 0 && rt->rt_router != loopaddr) {
   1252   1.9   thorpej 				rtdelete(rt);
   1253   1.9   thorpej 				rt = 0;
   1254   1.9   thorpej 			}
   1255   1.9   thorpej 			if (rt != 0) {
   1256   1.9   thorpej 				if (!(rt->rt_state & RS_LOCAL)
   1257   1.9   thorpej 				    || rt->rt_metric > ifp->int_metric) {
   1258   1.9   thorpej 					ifp1 = ifp;
   1259   1.9   thorpej 				} else {
   1260   1.9   thorpej 					ifp1 = rt->rt_ifp;
   1261   1.9   thorpej 				}
   1262  1.14   thorpej 				loop_rts.rts_ifp = ifp1;
   1263  1.14   thorpej 				loop_rts.rts_metric = 0;
   1264  1.14   thorpej 				loop_rts.rts_time = rt->rt_time;
   1265  1.14   thorpej 				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
   1266  1.14   thorpej 					      | (RS_IF|RS_LOCAL)),
   1267  1.14   thorpej 					 &loop_rts, 0);
   1268   1.9   thorpej 			} else {
   1269  1.14   thorpej 				loop_rts.rts_ifp = ifp;
   1270  1.14   thorpej 				loop_rts.rts_metric = 0;
   1271   1.9   thorpej 				rtadd(ifp->int_addr, HOST_MASK,
   1272  1.14   thorpej 				      (RS_IF | RS_LOCAL), &loop_rts);
   1273   1.9   thorpej 			}
   1274   1.9   thorpej 		}
   1275   1.9   thorpej 	}
   1276   1.9   thorpej 
   1277   1.9   thorpej 	/* add the authority routes */
   1278   1.9   thorpej 	for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
   1279   1.9   thorpej 		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
   1280   1.9   thorpej 		if (rt != 0
   1281   1.9   thorpej 		    && !(rt->rt_state & RS_NO_NET_SYN)
   1282   1.9   thorpej 		    && !(rt->rt_state & RS_NET_INT)) {
   1283   1.9   thorpej 			rtdelete(rt);
   1284   1.9   thorpej 			rt = 0;
   1285   1.9   thorpej 		}
   1286  1.14   thorpej 		if (rt == 0) {
   1287  1.14   thorpej 			loop_rts.rts_ifp = 0;
   1288  1.14   thorpej 			loop_rts.rts_metric = intnetp->intnet_metric-1;
   1289   1.9   thorpej 			rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
   1290  1.14   thorpej 			      RS_NET_SYN | RS_NET_INT, &loop_rts);
   1291  1.14   thorpej 		}
   1292   1.9   thorpej 	}
   1293   1.9   thorpej 
   1294   1.9   thorpej 	prev_complaints = complaints;
   1295   1.9   thorpej }
   1296   1.9   thorpej 
   1297   1.9   thorpej 
   1298   1.9   thorpej static void
   1299   1.9   thorpej check_net_syn(struct interface *ifp)
   1300   1.9   thorpej {
   1301   1.9   thorpej 	struct rt_entry *rt;
   1302  1.14   thorpej 	static struct rt_spare new;
   1303   1.9   thorpej 
   1304   1.9   thorpej 
   1305   1.9   thorpej 	/* Turn on the need to automatically synthesize a network route
   1306   1.9   thorpej 	 * for this interface only if we are running RIPv1 on some other
   1307   1.9   thorpej 	 * interface that is on a different class-A,B,or C network.
   1308   1.9   thorpej 	 */
   1309   1.9   thorpej 	if (have_ripv1_out || have_ripv1_in) {
   1310   1.9   thorpej 		ifp->int_state |= IS_NEED_NET_SYN;
   1311   1.9   thorpej 		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
   1312   1.9   thorpej 		if (rt != 0
   1313   1.9   thorpej 		    && 0 == (rt->rt_state & RS_NO_NET_SYN)
   1314   1.9   thorpej 		    && (!(rt->rt_state & RS_NET_SYN)
   1315   1.9   thorpej 			|| rt->rt_metric > ifp->int_metric)) {
   1316   1.9   thorpej 			rtdelete(rt);
   1317   1.9   thorpej 			rt = 0;
   1318   1.9   thorpej 		}
   1319  1.14   thorpej 		if (rt == 0) {
   1320  1.14   thorpej 			new.rts_ifp = ifp;
   1321  1.14   thorpej 			new.rts_gate = ifp->int_addr;
   1322  1.14   thorpej 			new.rts_router = ifp->int_addr;
   1323  1.14   thorpej 			new.rts_metric = ifp->int_metric;
   1324   1.9   thorpej 			rtadd(ifp->int_std_addr, ifp->int_std_mask,
   1325  1.14   thorpej 			      RS_NET_SYN, &new);
   1326  1.14   thorpej 		}
   1327   1.9   thorpej 
   1328   1.9   thorpej 	} else {
   1329   1.9   thorpej 		ifp->int_state &= ~IS_NEED_NET_SYN;
   1330   1.9   thorpej 
   1331   1.9   thorpej 		rt = rtget(ifp->int_std_addr,
   1332   1.9   thorpej 			   ifp->int_std_mask);
   1333   1.9   thorpej 		if (rt != 0
   1334   1.9   thorpej 		    && (rt->rt_state & RS_NET_SYN)
   1335   1.9   thorpej 		    && rt->rt_ifp == ifp)
   1336   1.9   thorpej 			rtbad_sub(rt);
   1337   1.9   thorpej 	}
   1338   1.9   thorpej }
   1339   1.9   thorpej 
   1340   1.9   thorpej 
   1341   1.9   thorpej /* Add route for interface if not currently installed.
   1342   1.9   thorpej  * Create route to other end if a point-to-point link,
   1343   1.9   thorpej  * otherwise a route to this (sub)network.
   1344   1.9   thorpej  */
   1345  1.11  christos int					/* 0=bad interface */
   1346   1.9   thorpej addrouteforif(struct interface *ifp)
   1347   1.9   thorpej {
   1348   1.9   thorpej 	struct rt_entry *rt;
   1349  1.14   thorpej 	static struct rt_spare new;
   1350  1.14   thorpej 	naddr dst;
   1351   1.9   thorpej 
   1352   1.9   thorpej 
   1353   1.9   thorpej 	/* skip sick interfaces
   1354   1.9   thorpej 	 */
   1355   1.9   thorpej 	if (ifp->int_state & IS_BROKE)
   1356  1.11  christos 		return 0;
   1357   1.9   thorpej 
   1358   1.9   thorpej 	/* If the interface on a subnet, then install a RIPv1 route to
   1359   1.9   thorpej 	 * the network as well (unless it is sick).
   1360   1.9   thorpej 	 */
   1361   1.9   thorpej 	if (ifp->int_state & IS_SUBNET)
   1362   1.9   thorpej 		check_net_syn(ifp);
   1363   1.9   thorpej 
   1364  1.11  christos 	dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
   1365  1.11  christos 	       ? ifp->int_dstaddr
   1366  1.11  christos 	       : htonl(ifp->int_net));
   1367   1.9   thorpej 
   1368  1.14   thorpej 	new.rts_ifp = ifp;
   1369  1.14   thorpej 	new.rts_router = ifp->int_addr;
   1370  1.14   thorpej 	new.rts_gate = ifp->int_addr;
   1371  1.14   thorpej 	new.rts_metric = ifp->int_metric;
   1372  1.14   thorpej 	new.rts_time = now.tv_sec;
   1373  1.14   thorpej 
   1374  1.11  christos 	/* If we are going to send packets to the gateway,
   1375  1.11  christos 	 * it must be reachable using our physical interfaces
   1376  1.11  christos 	 */
   1377  1.11  christos 	if ((ifp->int_state & IS_REMOTE)
   1378  1.11  christos 	    && !(ifp->int_state & IS_EXTERNAL)
   1379  1.11  christos 	    && !check_remote(ifp))
   1380  1.11  christos 		return 0;
   1381   1.9   thorpej 
   1382   1.9   thorpej 	/* We are finished if the correct main interface route exists.
   1383   1.9   thorpej 	 * The right route must be for the right interface, not synthesized
   1384   1.9   thorpej 	 * from a subnet, be a "gateway" or not as appropriate, and so forth.
   1385   1.9   thorpej 	 */
   1386  1.14   thorpej 	del_static(dst, ifp->int_mask, 0, 0);
   1387   1.9   thorpej 	rt = rtget(dst, ifp->int_mask);
   1388   1.9   thorpej 	if (rt != 0) {
   1389   1.9   thorpej 		if ((rt->rt_ifp != ifp
   1390   1.9   thorpej 		     || rt->rt_router != ifp->int_addr)
   1391   1.9   thorpej 		    && (!(ifp->int_state & IS_DUP)
   1392   1.9   thorpej 			|| rt->rt_ifp == 0
   1393   1.9   thorpej 			|| (rt->rt_ifp->int_state & IS_BROKE))) {
   1394   1.9   thorpej 			rtdelete(rt);
   1395   1.9   thorpej 			rt = 0;
   1396   1.9   thorpej 		} else {
   1397   1.9   thorpej 			rtchange(rt, ((rt->rt_state | RS_IF)
   1398   1.9   thorpej 				      & ~(RS_NET_SYN | RS_LOCAL)),
   1399  1.14   thorpej 				 &new, 0);
   1400   1.9   thorpej 		}
   1401   1.9   thorpej 	}
   1402   1.9   thorpej 	if (rt == 0) {
   1403   1.9   thorpej 		if (ifp->int_transitions++ > 0)
   1404  1.11  christos 			trace_act("re-install interface %s",
   1405   1.9   thorpej 				  ifp->int_name);
   1406   1.9   thorpej 
   1407  1.14   thorpej 		rtadd(dst, ifp->int_mask, RS_IF, &new);
   1408   1.1       cgd 	}
   1409  1.11  christos 
   1410  1.11  christos 	return 1;
   1411   1.1       cgd }
   1412