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