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