xref: /freebsd/sbin/routed/if.c (revision 38ae65235e00a1e87239372f973d057aeff1f2d8)
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  * 4. 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 "defs.h"
33 #include "pathnames.h"
34 
35 #ifdef __NetBSD__
36 __RCSID("$NetBSD$");
37 #elif defined(__FreeBSD__)
38 __RCSID("$FreeBSD$");
39 #else
40 __RCSID("$Revision: 2.27 $");
41 #ident "$Revision: 2.27 $"
42 #endif
43 
44 struct ifhead ifnet = LIST_HEAD_INITIALIZER(ifnet);	/* all interfaces */
45 
46 /* hash table for all interfaces, big enough to tolerate ridiculous
47  * numbers of IP aliases.  Crazy numbers of aliases such as 7000
48  * still will not do well, but not just in looking up interfaces
49  * by name or address.
50  */
51 #define AHASH_LEN 211			/* must be prime */
52 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
53 struct interface *ahash_tbl[AHASH_LEN];
54 
55 #define BHASH_LEN 211			/* must be prime */
56 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
57 struct interface *bhash_tbl[BHASH_LEN];
58 
59 struct interface *remote_if;		/* remote interfaces */
60 
61 /* hash for physical interface names.
62  * Assume there are never more 100 or 200 real interfaces, and that
63  * aliases are put on the end of the hash chains.
64  */
65 #define NHASH_LEN 97
66 struct interface *nhash_tbl[NHASH_LEN];
67 
68 int	tot_interfaces;			/* # of remote and local interfaces */
69 int	rip_interfaces;			/* # of interfaces doing RIP */
70 int	foundloopback;			/* valid flag for loopaddr */
71 naddr	loopaddr;			/* our address on loopback */
72 struct	rt_spare loop_rts;
73 
74 struct timeval ifinit_timer;
75 static struct timeval last_ifinit;
76 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec		\
77 			   && last_ifinit.tv_usec == now.tv_usec	\
78 			   && timercmp(&ifinit_timer, &now, >))
79 
80 int	have_ripv1_out;			/* have a RIPv1 interface */
81 int	have_ripv1_in;
82 
83 
84 static struct interface**
85 nhash(char *p)
86 {
87 	u_int i;
88 
89 	for (i = 0; *p != '\0'; p++) {
90 		i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
91 		i ^= *p;
92 	}
93 	return &nhash_tbl[i % NHASH_LEN];
94 }
95 
96 
97 /* Link a new interface into the lists and hash tables.
98  */
99 void
100 if_link(struct interface *ifp)
101 {
102 	struct interface **hifp;
103 
104 	LIST_INSERT_HEAD(&ifnet, ifp, int_list);
105 
106 	hifp = AHASH(ifp->int_addr);
107 	ifp->int_ahash_prev = hifp;
108 	if ((ifp->int_ahash = *hifp) != 0)
109 		(*hifp)->int_ahash_prev = &ifp->int_ahash;
110 	*hifp = ifp;
111 
112 	if (ifp->int_if_flags & IFF_BROADCAST) {
113 		hifp = BHASH(ifp->int_brdaddr);
114 		ifp->int_bhash_prev = hifp;
115 		if ((ifp->int_bhash = *hifp) != 0)
116 			(*hifp)->int_bhash_prev = &ifp->int_bhash;
117 		*hifp = ifp;
118 	}
119 
120 	if (ifp->int_state & IS_REMOTE) {
121 		ifp->int_rlink_prev = &remote_if;
122 		ifp->int_rlink = remote_if;
123 		if (remote_if != 0)
124 			remote_if->int_rlink_prev = &ifp->int_rlink;
125 		remote_if = ifp;
126 	}
127 
128 	hifp = nhash(ifp->int_name);
129 	if (ifp->int_state & IS_ALIAS) {
130 		/* put aliases on the end of the hash chain */
131 		while (*hifp != 0)
132 			hifp = &(*hifp)->int_nhash;
133 	}
134 	ifp->int_nhash_prev = hifp;
135 	if ((ifp->int_nhash = *hifp) != 0)
136 		(*hifp)->int_nhash_prev = &ifp->int_nhash;
137 	*hifp = ifp;
138 }
139 
140 
141 /* Find the interface with an address
142  */
143 struct interface *
144 ifwithaddr(naddr addr,
145 	   int	bcast,			/* notice IFF_BROADCAST address */
146 	   int	remote)			/* include IS_REMOTE interfaces */
147 {
148 	struct interface *ifp, *possible = 0;
149 
150 	remote = (remote == 0) ? IS_REMOTE : 0;
151 
152 	for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
153 		if (ifp->int_addr != addr)
154 			continue;
155 		if ((ifp->int_state & remote) != 0)
156 			continue;
157 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
158 			return ifp;
159 		possible = ifp;
160 	}
161 
162 	if (possible || !bcast)
163 		return possible;
164 
165 	for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
166 		if (ifp->int_brdaddr != addr)
167 			continue;
168 		if ((ifp->int_state & remote) != 0)
169 			continue;
170 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
171 			return ifp;
172 		possible = ifp;
173 	}
174 
175 	return possible;
176 }
177 
178 
179 /* find the interface with a name
180  */
181 struct interface *
182 ifwithname(char *name,			/* "ec0" or whatever */
183 	   naddr addr)			/* 0 or network address */
184 {
185 	struct interface *ifp;
186 
187 	for (;;) {
188 		for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
189 			/* If the network address is not specified,
190 			 * ignore any alias interfaces.  Otherwise, look
191 			 * for the interface with the target name and address.
192 			 */
193 			if (!strcmp(ifp->int_name, name)
194 			    && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
195 				|| (ifp->int_addr == addr)))
196 				return ifp;
197 		}
198 
199 		/* If there is no known interface, maybe there is a
200 		 * new interface.  So just once look for new interfaces.
201 		 */
202 		if (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 = 0;
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 == 0
259 					|| ifp->int_mask > maybe->int_mask))
260 					maybe = ifp;
261 			}
262 		}
263 
264 		if (maybe != 0 || 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 != 0 && 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 != 0; 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 != 0
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 		*ifp->int_rlink_prev = ifp->int_rlink;
472 		if (ifp->int_rlink != 0)
473 			ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
474 	}
475 
476 	if (!(ifp->int_state & IS_ALIAS)) {
477 		/* delete aliases when the main interface dies
478 		 */
479 		LIST_FOREACH(ifp1, &ifnet, int_list) {
480 			if (ifp1 != ifp
481 			    && !strcmp(ifp->int_name, ifp1->int_name))
482 				ifdel(ifp1);
483 		}
484 
485 		if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
486 			struct group_req gr;
487 			struct sockaddr_in *sin;
488 
489 			memset(&gr, 0, sizeof(gr));
490 			gr.gr_interface = ifp->int_index;
491 			sin = (struct sockaddr_in *)&gr.gr_group;
492 			sin->sin_family = AF_INET;
493 #ifdef _HAVE_SIN_LEN
494 			sin->sin_len = sizeof(struct sockaddr_in);
495 #endif
496 			sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
497 			if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
498 				       &gr, sizeof(gr)) < 0
499 			    && errno != EADDRNOTAVAIL
500 			    && !TRACEACTIONS)
501 				LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
502 			if (rip_sock_mcast == ifp)
503 				rip_sock_mcast = 0;
504 		}
505 		if (ifp->int_rip_sock >= 0) {
506 			(void)close(ifp->int_rip_sock);
507 			ifp->int_rip_sock = -1;
508 			fix_select();
509 		}
510 
511 		tot_interfaces--;
512 		if (!IS_RIP_OFF(ifp->int_state))
513 			rip_interfaces--;
514 
515 		/* Zap all routes associated with this interface.
516 		 * Assume routes just using gateways beyond this interface
517 		 * will timeout naturally, and have probably already died.
518 		 */
519 		(void)rn_walktree(rhead, walk_bad, 0);
520 
521 		set_rdisc_mg(ifp, 0);
522 		if_bad_rdisc(ifp);
523 	}
524 
525 	free(ifp);
526 }
527 
528 
529 /* Mark an interface ill.
530  */
531 void
532 if_sick(struct interface *ifp)
533 {
534 	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
535 		ifp->int_state |= IS_SICK;
536 		ifp->int_act_time = NEVER;
537 		trace_if("Chg", ifp);
538 
539 		LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
540 	}
541 }
542 
543 
544 /* Mark an interface dead.
545  */
546 void
547 if_bad(struct interface *ifp)
548 {
549 	struct interface *ifp1;
550 
551 
552 	if (ifp->int_state & IS_BROKE)
553 		return;
554 
555 	LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
556 
557 	ifp->int_state |= (IS_BROKE | IS_SICK);
558 	ifp->int_act_time = NEVER;
559 	ifp->int_query_time = NEVER;
560 	ifp->int_data.ts = now.tv_sec;
561 
562 	trace_if("Chg", ifp);
563 
564 	if (!(ifp->int_state & IS_ALIAS)) {
565 		LIST_FOREACH(ifp1, &ifnet, int_list) {
566 			if (ifp1 != ifp
567 			    && !strcmp(ifp->int_name, ifp1->int_name))
568 				if_bad(ifp1);
569 		}
570 		(void)rn_walktree(rhead, walk_bad, 0);
571 		if_bad_rdisc(ifp);
572 	}
573 }
574 
575 
576 /* Mark an interface alive
577  */
578 int					/* 1=it was dead */
579 if_ok(struct interface *ifp,
580       const char *type)
581 {
582 	struct interface *ifp1;
583 
584 
585 	if (!(ifp->int_state & IS_BROKE)) {
586 		if (ifp->int_state & IS_SICK) {
587 			trace_act("%sinterface %s to %s working better",
588 				  type,
589 				  ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
590 			ifp->int_state &= ~IS_SICK;
591 		}
592 		return 0;
593 	}
594 
595 	msglog("%sinterface %s to %s restored",
596 	       type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
597 	ifp->int_state &= ~(IS_BROKE | IS_SICK);
598 	ifp->int_data.ts = 0;
599 
600 	if (!(ifp->int_state & IS_ALIAS)) {
601 		LIST_FOREACH(ifp1, &ifnet, int_list) {
602 			if (ifp1 != ifp
603 			    && !strcmp(ifp->int_name, ifp1->int_name))
604 				if_ok(ifp1, type);
605 		}
606 		if_ok_rdisc(ifp);
607 	}
608 
609 	if (ifp->int_state & IS_REMOTE) {
610 		if (!addrouteforif(ifp))
611 			return 0;
612 	}
613 	return 1;
614 }
615 
616 
617 /* disassemble routing message
618  */
619 void
620 rt_xaddrs(struct rt_addrinfo *info,
621 	  struct sockaddr *sa,
622 	  struct sockaddr *lim,
623 	  int addrs)
624 {
625 	int i;
626 #ifdef _HAVE_SA_LEN
627 	static struct sockaddr sa_zero;
628 #endif
629 
630 	memset(info, 0, sizeof(*info));
631 	info->rti_addrs = addrs;
632 	for (i = 0; i < RTAX_MAX && sa < lim; i++) {
633 		if ((addrs & (1 << i)) == 0)
634 			continue;
635 		info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
636 		sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
637 	}
638 }
639 
640 
641 /* Find the network interfaces which have configured themselves.
642  *	This must be done regularly, if only for extra addresses
643  *	that come and go on interfaces.
644  */
645 void
646 ifinit(void)
647 {
648 	static char *sysctl_buf;
649 	static size_t sysctl_buf_size = 0;
650 	uint complaints = 0;
651 	static u_int prev_complaints = 0;
652 #	define COMP_NOT_INET	0x001
653 #	define COMP_NOADDR	0x002
654 #	define COMP_BADADDR	0x004
655 #	define COMP_NODST	0x008
656 #	define COMP_NOBADR	0x010
657 #	define COMP_NOMASK	0x020
658 #	define COMP_DUP		0x040
659 #	define COMP_BAD_METRIC	0x080
660 #	define COMP_NETMASK	0x100
661 
662 	struct interface ifs, ifs0, *ifp, *ifp1;
663 	struct rt_entry *rt;
664 	size_t needed;
665 	int mib[6];
666 	struct if_msghdr *ifm;
667 	struct ifa_msghdr *ifam, *ifam_lim, *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 	ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
711 	for (ifam = (struct ifa_msghdr *)sysctl_buf;
712 	     ifam < ifam_lim;
713 	     ifam = ifam2) {
714 
715 		ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
716 
717 #ifdef RTM_OIFINFO
718 		if (ifam->ifam_type == RTM_OIFINFO)
719 			continue;	/* just ignore compat message */
720 #endif
721 		if (ifam->ifam_type == RTM_IFINFO) {
722 			struct sockaddr_dl *sdl;
723 
724 			ifm = (struct if_msghdr *)ifam;
725 			/* make prototype structure for the IP aliases
726 			 */
727 			memset(&ifs0, 0, sizeof(ifs0));
728 			ifs0.int_rip_sock = -1;
729 			ifs0.int_index = ifm->ifm_index;
730 			ifs0.int_if_flags = ifm->ifm_flags;
731 			ifs0.int_state = IS_CHECKED;
732 			ifs0.int_query_time = NEVER;
733 			ifs0.int_act_time = now.tv_sec;
734 			ifs0.int_data.ts = now.tv_sec;
735 			ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
736 			ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
737 			ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
738 			ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
739 #ifdef sgi
740 			ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
741 #endif
742 			sdl = (struct sockaddr_dl *)(ifm + 1);
743 			sdl->sdl_data[sdl->sdl_nlen] = 0;
744 			strncpy(ifs0.int_name, sdl->sdl_data,
745 				MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
746 			continue;
747 		}
748 		if (ifam->ifam_type != RTM_NEWADDR) {
749 			logbad(1,"ifinit: out of sync");
750 			continue;
751 		}
752 		rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
753 			  (struct sockaddr *)ifam2,
754 			  ifam->ifam_addrs);
755 
756 		/* Prepare for the next address of this interface, which
757 		 * will be an alias.
758 		 * Do not output RIP or Router-Discovery packets via aliases.
759 		 */
760 		memcpy(&ifs, &ifs0, sizeof(ifs));
761 		ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
762 
763 		if (INFO_IFA(&info) == 0) {
764 			if (iff_up(ifs.int_if_flags)) {
765 				if (!(prev_complaints & COMP_NOADDR))
766 					msglog("%s has no address",
767 					       ifs.int_name);
768 				complaints |= COMP_NOADDR;
769 			}
770 			continue;
771 		}
772 		if (INFO_IFA(&info)->sa_family != AF_INET) {
773 			if (iff_up(ifs.int_if_flags)) {
774 				if (!(prev_complaints & COMP_NOT_INET))
775 					trace_act("%s: not AF_INET",
776 						  ifs.int_name);
777 				complaints |= COMP_NOT_INET;
778 			}
779 			continue;
780 		}
781 
782 		ifs.int_addr = S_ADDR(INFO_IFA(&info));
783 
784 		if (ntohl(ifs.int_addr)>>24 == 0
785 		    || ntohl(ifs.int_addr)>>24 == 0xff) {
786 			if (iff_up(ifs.int_if_flags)) {
787 				if (!(prev_complaints & COMP_BADADDR))
788 					msglog("%s has a bad address",
789 					       ifs.int_name);
790 				complaints |= COMP_BADADDR;
791 			}
792 			continue;
793 		}
794 
795 		if (ifs.int_if_flags & IFF_LOOPBACK) {
796 			ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
797 			if (ifs.int_addr == htonl(INADDR_LOOPBACK))
798 				ifs.int_state |= IS_PASSIVE;
799 			ifs.int_dstaddr = ifs.int_addr;
800 			ifs.int_mask = HOST_MASK;
801 			ifs.int_ripv1_mask = HOST_MASK;
802 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
803 			ifs.int_net = ntohl(ifs.int_dstaddr);
804 			if (!foundloopback) {
805 				foundloopback = 1;
806 				loopaddr = ifs.int_addr;
807 				loop_rts.rts_gate = loopaddr;
808 				loop_rts.rts_router = loopaddr;
809 			}
810 
811 		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
812 			if (INFO_BRD(&info) == 0
813 			    || INFO_BRD(&info)->sa_family != AF_INET) {
814 				if (iff_up(ifs.int_if_flags)) {
815 					if (!(prev_complaints & COMP_NODST))
816 						msglog("%s has a bad"
817 						       " destination address",
818 						       ifs.int_name);
819 					complaints |= COMP_NODST;
820 				}
821 				continue;
822 			}
823 			ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
824 			if (ntohl(ifs.int_dstaddr)>>24 == 0
825 			    || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
826 				if (iff_up(ifs.int_if_flags)) {
827 					if (!(prev_complaints & COMP_NODST))
828 						msglog("%s has a bad"
829 						       " destination address",
830 						       ifs.int_name);
831 					complaints |= COMP_NODST;
832 				}
833 				continue;
834 			}
835 			ifs.int_mask = HOST_MASK;
836 			ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
837 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
838 			ifs.int_net = ntohl(ifs.int_dstaddr);
839 
840 		}  else {
841 			if (INFO_MASK(&info) == 0) {
842 				if (iff_up(ifs.int_if_flags)) {
843 					if (!(prev_complaints & COMP_NOMASK))
844 						msglog("%s has no netmask",
845 						       ifs.int_name);
846 					complaints |= COMP_NOMASK;
847 				}
848 				continue;
849 			}
850 			ifs.int_dstaddr = ifs.int_addr;
851 			ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
852 			ifs.int_ripv1_mask = ifs.int_mask;
853 			ifs.int_std_mask = std_mask(ifs.int_addr);
854 			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
855 			if (ifs.int_mask != ifs.int_std_mask)
856 				ifs.int_state |= IS_SUBNET;
857 
858 			if (ifs.int_if_flags & IFF_BROADCAST) {
859 				if (INFO_BRD(&info) == 0) {
860 					if (iff_up(ifs.int_if_flags)) {
861 					    if (!(prev_complaints
862 						  & COMP_NOBADR))
863 						msglog("%s has"
864 						       "no broadcast address",
865 						       ifs.int_name);
866 					    complaints |= COMP_NOBADR;
867 					}
868 					continue;
869 				}
870 				ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
871 			}
872 		}
873 		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
874 		ifs.int_std_addr = htonl(ifs.int_std_net);
875 
876 		/* Use a minimum metric of one.  Treat the interface metric
877 		 * (default 0) as an increment to the hop count of one.
878 		 *
879 		 * The metric obtained from the routing socket dump of
880 		 * interface addresses is wrong.  It is not set by the
881 		 * SIOCSIFMETRIC ioctl.
882 		 */
883 #ifdef SIOCGIFMETRIC
884 		strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
885 		if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
886 			DBGERR(1, "ioctl(SIOCGIFMETRIC)");
887 			ifs.int_metric = 0;
888 		} else {
889 			ifs.int_metric = ifr.ifr_metric;
890 		}
891 #else
892 		ifs.int_metric = ifam->ifam_metric;
893 #endif
894 		if (ifs.int_metric > HOPCNT_INFINITY) {
895 			ifs.int_metric = 0;
896 			if (!(prev_complaints & COMP_BAD_METRIC)
897 			    && iff_up(ifs.int_if_flags)) {
898 				complaints |= COMP_BAD_METRIC;
899 				msglog("%s has a metric of %d",
900 				       ifs.int_name, ifs.int_metric);
901 			}
902 		}
903 
904 		/* See if this is a familiar interface.
905 		 * If so, stop worrying about it if it is the same.
906 		 * Start it over if it now is to somewhere else, as happens
907 		 * frequently with PPP and SLIP.
908 		 */
909 		ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
910 						? ifs.int_addr
911 						: 0));
912 		if (ifp != 0) {
913 			ifp->int_state |= IS_CHECKED;
914 
915 			if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
916 				  & (IFF_BROADCAST
917 				     | IFF_LOOPBACK
918 				     | IFF_POINTOPOINT
919 				     | IFF_MULTICAST))
920 			    || 0 != ((ifp->int_state ^ ifs.int_state)
921 				     & IS_ALIAS)
922 			    || ifp->int_addr != ifs.int_addr
923 			    || ifp->int_brdaddr != ifs.int_brdaddr
924 			    || ifp->int_dstaddr != ifs.int_dstaddr
925 			    || ifp->int_mask != ifs.int_mask
926 			    || ifp->int_metric != ifs.int_metric) {
927 				/* Forget old information about
928 				 * a changed interface.
929 				 */
930 				trace_act("interface %s has changed",
931 					  ifp->int_name);
932 				ifdel(ifp);
933 				ifp = 0;
934 			}
935 		}
936 
937 		if (ifp != 0) {
938 			/* The primary representative of an alias worries
939 			 * about how things are working.
940 			 */
941 			if (ifp->int_state & IS_ALIAS)
942 				continue;
943 
944 			/* note interfaces that have been turned off
945 			 */
946 			if (!iff_up(ifs.int_if_flags)) {
947 				if (iff_up(ifp->int_if_flags)) {
948 					msglog("interface %s to %s turned off",
949 					       ifp->int_name,
950 					       naddr_ntoa(ifp->int_dstaddr));
951 					if_bad(ifp);
952 					ifp->int_if_flags &= ~IFF_UP;
953 				} else if (now.tv_sec>(ifp->int_data.ts
954 						       + CHECK_BAD_INTERVAL)) {
955 					trace_act("interface %s has been off"
956 						  " %ld seconds; forget it",
957 						  ifp->int_name,
958 						  now.tv_sec-ifp->int_data.ts);
959 					ifdel(ifp);
960 				}
961 				continue;
962 			}
963 			/* or that were off and are now ok */
964 			if (!iff_up(ifp->int_if_flags)) {
965 				ifp->int_if_flags |= IFF_UP;
966 				(void)if_ok(ifp, "");
967 			}
968 
969 			/* If it has been long enough,
970 			 * see if the interface is broken.
971 			 */
972 			if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
973 				continue;
974 
975 			in = ifs.int_data.ipackets - ifp->int_data.ipackets;
976 			ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
977 			out = ifs.int_data.opackets - ifp->int_data.opackets;
978 			oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
979 #ifdef sgi
980 			/* Through at least IRIX 6.2, PPP and SLIP
981 			 * count packets dropped by the filters.
982 			 * But FDDI rings stuck non-operational count
983 			 * dropped packets as they wait for improvement.
984 			 */
985 			if (!(ifp->int_if_flags & IFF_POINTOPOINT))
986 				oerr += (ifs.int_data.odrops
987 					 - ifp->int_data.odrops);
988 #endif
989 			/* If the interface just awoke, restart the counters.
990 			 */
991 			if (ifp->int_data.ts == 0) {
992 				ifp->int_data = ifs.int_data;
993 				continue;
994 			}
995 			ifp->int_data = ifs.int_data;
996 
997 			/* Withhold judgment when the short error
998 			 * counters wrap or the interface is reset.
999 			 */
1000 			if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1001 				LIM_SEC(ifinit_timer,
1002 					now.tv_sec+CHECK_BAD_INTERVAL);
1003 				continue;
1004 			}
1005 
1006 			/* Withhold judgement when there is no traffic
1007 			 */
1008 			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1009 				continue;
1010 
1011 			/* It is bad if input or output is not working.
1012 			 * Require presistent problems before marking it dead.
1013 			 */
1014 			if ((in <= ierr && ierr > 0)
1015 			    || (out <= oerr && oerr > 0)) {
1016 				if (!(ifp->int_state & IS_SICK)) {
1017 					trace_act("interface %s to %s"
1018 						  " sick: in=%d ierr=%d"
1019 						  " out=%d oerr=%d",
1020 						  ifp->int_name,
1021 						  naddr_ntoa(ifp->int_dstaddr),
1022 						  in, ierr, out, oerr);
1023 					if_sick(ifp);
1024 					continue;
1025 				}
1026 				if (!(ifp->int_state & IS_BROKE)) {
1027 					msglog("interface %s to %s broken:"
1028 					       " in=%d ierr=%d out=%d oerr=%d",
1029 					       ifp->int_name,
1030 					       naddr_ntoa(ifp->int_dstaddr),
1031 					       in, ierr, out, oerr);
1032 					if_bad(ifp);
1033 				}
1034 				continue;
1035 			}
1036 
1037 			/* otherwise, it is active and healthy
1038 			 */
1039 			ifp->int_act_time = now.tv_sec;
1040 			(void)if_ok(ifp, "");
1041 			continue;
1042 		}
1043 
1044 		/* This is a new interface.
1045 		 * If it is dead, forget it.
1046 		 */
1047 		if (!iff_up(ifs.int_if_flags))
1048 			continue;
1049 
1050 		/* If it duplicates an existing interface,
1051 		 * complain about it, mark the other one
1052 		 * duplicated, and forget this one.
1053 		 */
1054 		ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1055 				ifs.int_if_flags);
1056 		if (ifp != 0) {
1057 			/* Ignore duplicates of itself, caused by having
1058 			 * IP aliases on the same network.
1059 			 */
1060 			if (!strcmp(ifp->int_name, ifs.int_name))
1061 				continue;
1062 
1063 			if (!(prev_complaints & COMP_DUP)) {
1064 				complaints |= COMP_DUP;
1065 				msglog("%s (%s%s%s) is duplicated by"
1066 				       " %s (%s%s%s)",
1067 				       ifs.int_name,
1068 				       addrname(ifs.int_addr,ifs.int_mask,1),
1069 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
1070 					? "-->" : ""),
1071 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
1072 					? naddr_ntoa(ifs.int_dstaddr) : ""),
1073 				       ifp->int_name,
1074 				       addrname(ifp->int_addr,ifp->int_mask,1),
1075 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
1076 					? "-->" : ""),
1077 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
1078 					? naddr_ntoa(ifp->int_dstaddr) : ""));
1079 			}
1080 			ifp->int_state |= IS_DUP;
1081 			continue;
1082 		}
1083 
1084 		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
1085 			trace_act("%s is neither broadcast, point-to-point,"
1086 				  " nor loopback",
1087 				  ifs.int_name);
1088 			if (!(ifs.int_state & IFF_MULTICAST))
1089 				ifs.int_state |= IS_NO_RDISC;
1090 		}
1091 
1092 
1093 		/* It is new and ok.   Add it to the list of interfaces
1094 		 */
1095 		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1096 		memcpy(ifp, &ifs, sizeof(*ifp));
1097 		get_parms(ifp);
1098 		if_link(ifp);
1099 		trace_if("Add", ifp);
1100 
1101 		/* Notice likely bad netmask.
1102 		 */
1103 		if (!(prev_complaints & COMP_NETMASK)
1104 		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
1105 		    && ifp->int_addr != RIP_DEFAULT) {
1106 			LIST_FOREACH(ifp1, &ifnet, int_list) {
1107 				if (ifp1->int_mask == ifp->int_mask)
1108 					continue;
1109 				if (ifp1->int_if_flags & IFF_POINTOPOINT)
1110 					continue;
1111 				if (ifp1->int_dstaddr == RIP_DEFAULT)
1112 					continue;
1113 				/* ignore aliases on the right network */
1114 				if (!strcmp(ifp->int_name, ifp1->int_name))
1115 					continue;
1116 				if (on_net(ifp->int_dstaddr,
1117 					   ifp1->int_net, ifp1->int_mask)
1118 				    || on_net(ifp1->int_dstaddr,
1119 					      ifp->int_net, ifp->int_mask)) {
1120 					msglog("possible netmask problem"
1121 					       " between %s:%s and %s:%s",
1122 					       ifp->int_name,
1123 					       addrname(htonl(ifp->int_net),
1124 							ifp->int_mask, 1),
1125 					       ifp1->int_name,
1126 					       addrname(htonl(ifp1->int_net),
1127 							ifp1->int_mask, 1));
1128 					complaints |= COMP_NETMASK;
1129 				}
1130 			}
1131 		}
1132 
1133 		if (!(ifp->int_state & IS_ALIAS)) {
1134 			/* Count the # of directly connected networks.
1135 			 */
1136 			if (!(ifp->int_if_flags & IFF_LOOPBACK))
1137 				tot_interfaces++;
1138 			if (!IS_RIP_OFF(ifp->int_state))
1139 				rip_interfaces++;
1140 
1141 			/* turn on router discovery and RIP If needed */
1142 			if_ok_rdisc(ifp);
1143 			rip_on(ifp);
1144 		}
1145 	}
1146 
1147 	/* If we are multi-homed and have at least two interfaces
1148 	 * listening to RIP, then output by default.
1149 	 */
1150 	if (!supplier_set && rip_interfaces > 1)
1151 		set_supplier();
1152 
1153 	/* If we are multi-homed, optionally advertise a route to
1154 	 * our main address.
1155 	 */
1156 	if (advertise_mhome
1157 	    || (tot_interfaces > 1
1158 		&& mhome
1159 		&& (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1160 		&& foundloopback)) {
1161 		advertise_mhome = 1;
1162 		rt = rtget(myaddr, HOST_MASK);
1163 		if (rt != 0) {
1164 			if (rt->rt_ifp != ifp
1165 			    || rt->rt_router != loopaddr) {
1166 				rtdelete(rt);
1167 				rt = 0;
1168 			} else {
1169 				loop_rts.rts_ifp = ifp;
1170 				loop_rts.rts_metric = 0;
1171 				loop_rts.rts_time = rt->rt_time;
1172 				rtchange(rt, rt->rt_state | RS_MHOME,
1173 					 &loop_rts, 0);
1174 			}
1175 		}
1176 		if (rt == 0) {
1177 			loop_rts.rts_ifp = ifp;
1178 			loop_rts.rts_metric = 0;
1179 			rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1180 		}
1181 	}
1182 
1183 	LIST_FOREACH_SAFE(ifp, &ifnet, int_list, ifp1) {
1184 		/* Forget any interfaces that have disappeared.
1185 		 */
1186 		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1187 			trace_act("interface %s has disappeared",
1188 				  ifp->int_name);
1189 			ifdel(ifp);
1190 			continue;
1191 		}
1192 
1193 		if ((ifp->int_state & IS_BROKE)
1194 		    && !(ifp->int_state & IS_PASSIVE))
1195 			LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1196 
1197 		/* If we ever have a RIPv1 interface, assume we always will.
1198 		 * It might come back if it ever goes away.
1199 		 */
1200 		if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1201 			have_ripv1_out = 1;
1202 		if (!(ifp->int_state & IS_NO_RIPV1_IN))
1203 			have_ripv1_in = 1;
1204 	}
1205 
1206 	LIST_FOREACH(ifp, &ifnet, int_list) {
1207 		/* Ensure there is always a network route for interfaces,
1208 		 * after any dead interfaces have been deleted, which
1209 		 * might affect routes for point-to-point links.
1210 		 */
1211 		if (!addrouteforif(ifp))
1212 			continue;
1213 
1214 		/* Add routes to the local end of point-to-point interfaces
1215 		 * using loopback.
1216 		 */
1217 		if ((ifp->int_if_flags & IFF_POINTOPOINT)
1218 		    && !(ifp->int_state & IS_REMOTE)
1219 		    && foundloopback) {
1220 			/* Delete any routes to the network address through
1221 			 * foreign routers. Remove even static routes.
1222 			 */
1223 			del_static(ifp->int_addr, HOST_MASK, 0, 0);
1224 			rt = rtget(ifp->int_addr, HOST_MASK);
1225 			if (rt != 0 && rt->rt_router != loopaddr) {
1226 				rtdelete(rt);
1227 				rt = 0;
1228 			}
1229 			if (rt != 0) {
1230 				if (!(rt->rt_state & RS_LOCAL)
1231 				    || rt->rt_metric > ifp->int_metric) {
1232 					ifp1 = ifp;
1233 				} else {
1234 					ifp1 = rt->rt_ifp;
1235 				}
1236 				loop_rts.rts_ifp = ifp1;
1237 				loop_rts.rts_metric = 0;
1238 				loop_rts.rts_time = rt->rt_time;
1239 				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1240 					      | (RS_IF|RS_LOCAL)),
1241 					 &loop_rts, 0);
1242 			} else {
1243 				loop_rts.rts_ifp = ifp;
1244 				loop_rts.rts_metric = 0;
1245 				rtadd(ifp->int_addr, HOST_MASK,
1246 				      (RS_IF | RS_LOCAL), &loop_rts);
1247 			}
1248 		}
1249 	}
1250 
1251 	/* add the authority routes */
1252 	for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1253 		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1254 		if (rt != 0
1255 		    && !(rt->rt_state & RS_NO_NET_SYN)
1256 		    && !(rt->rt_state & RS_NET_INT)) {
1257 			rtdelete(rt);
1258 			rt = 0;
1259 		}
1260 		if (rt == 0) {
1261 			loop_rts.rts_ifp = 0;
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 != 0
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 = 0;
1292 		}
1293 		if (rt == 0) {
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 != 0
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 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 != 0) {
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 = 0;
1370 		} else {
1371 			rtchange(rt, ((rt->rt_state | RS_IF)
1372 				      & ~(RS_NET_SYN | RS_LOCAL)),
1373 				 &new, 0);
1374 		}
1375 	}
1376 	if (rt == 0) {
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