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