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