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