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