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