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