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