if.c revision 180993
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: head/sbin/routed/if.c 180993 2008-07-30 11:56:15Z phk $
30 */
31
32#include "defs.h"
33#include "pathnames.h"
34
35#ifdef __NetBSD__
36__RCSID("$NetBSD$");
37#elif defined(__FreeBSD__)
38__RCSID("$FreeBSD: head/sbin/routed/if.c 180993 2008-07-30 11:56:15Z phk $");
39#else
40__RCSID("$Revision: 2.27 $");
41#ident "$Revision: 2.27 $"
42#endif
43
44struct 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]
53struct interface *ahash_tbl[AHASH_LEN];
54
55#define BHASH_LEN 211			/* must be prime */
56#define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
57struct interface *bhash_tbl[BHASH_LEN];
58
59struct 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
66struct interface *nhash_tbl[NHASH_LEN];
67
68int	tot_interfaces;			/* # of remote and local interfaces */
69int	rip_interfaces;			/* # of interfaces doing RIP */
70int	foundloopback;			/* valid flag for loopaddr */
71naddr	loopaddr;			/* our address on loopback */
72struct	rt_spare loop_rts;
73
74struct timeval ifinit_timer;
75static 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
80int	have_ripv1_out;			/* have a RIPv1 interface */
81int	have_ripv1_in;
82
83
84static struct interface**
85nhash(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 */
99void
100if_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 */
147struct interface *
148ifwithaddr(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 */
185struct interface *
186ifwithname(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
213struct interface *
214ifwithindex(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 */
240struct interface *
241iflookup(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 */
282naddr					/* host byte order */
283std_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 */
301naddr
302ripv1_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
352naddr
353ripv1_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 */
370int					/* 0=bad */
371check_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 */
389struct interface *
390check_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 */
422int					/* 0=bad */
423check_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 */
452static void
453ifdel(struct interface *ifp)
454{
455	struct interface *ifp1;
456
457
458	trace_if("Del", ifp);
459
460	ifp->int_state |= IS_BROKE;
461
462	/* unlink the interface
463	 */
464	*ifp->int_prev = ifp->int_next;
465	if (ifp->int_next != 0)
466		ifp->int_next->int_prev = ifp->int_prev;
467	*ifp->int_ahash_prev = ifp->int_ahash;
468	if (ifp->int_ahash != 0)
469		ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
470	*ifp->int_nhash_prev = ifp->int_nhash;
471	if (ifp->int_nhash != 0)
472		ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
473	if (ifp->int_if_flags & IFF_BROADCAST) {
474		*ifp->int_bhash_prev = ifp->int_bhash;
475		if (ifp->int_bhash != 0)
476			ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
477	}
478	if (ifp->int_state & IS_REMOTE) {
479		*ifp->int_rlink_prev = ifp->int_rlink;
480		if (ifp->int_rlink != 0)
481			ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
482	}
483
484	if (!(ifp->int_state & IS_ALIAS)) {
485		/* delete aliases when the main interface dies
486		 */
487		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
488			if (ifp1 != ifp
489			    && !strcmp(ifp->int_name, ifp1->int_name))
490				ifdel(ifp1);
491		}
492
493		if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
494			struct group_req gr;
495			struct sockaddr_in *sin;
496
497			memset(&gr, 0, sizeof(gr));
498			gr.gr_interface = ifp->int_index;
499			sin = (struct sockaddr_in *)&gr.gr_group;
500			sin->sin_family = AF_INET;
501#ifdef _HAVE_SIN_LEN
502			sin->sin_len = sizeof(struct sockaddr_in);
503#endif
504			sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
505			if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
506				       &gr, sizeof(gr)) < 0
507			    && errno != EADDRNOTAVAIL
508			    && !TRACEACTIONS)
509				LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
510			if (rip_sock_mcast == ifp)
511				rip_sock_mcast = 0;
512		}
513		if (ifp->int_rip_sock >= 0) {
514			(void)close(ifp->int_rip_sock);
515			ifp->int_rip_sock = -1;
516			fix_select();
517		}
518
519		tot_interfaces--;
520		if (!IS_RIP_OFF(ifp->int_state))
521			rip_interfaces--;
522
523		/* Zap all routes associated with this interface.
524		 * Assume routes just using gateways beyond this interface
525		 * will timeout naturally, and have probably already died.
526		 */
527		(void)rn_walktree(rhead, walk_bad, 0);
528
529		set_rdisc_mg(ifp, 0);
530		if_bad_rdisc(ifp);
531	}
532
533	free(ifp);
534}
535
536
537/* Mark an interface ill.
538 */
539void
540if_sick(struct interface *ifp)
541{
542	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
543		ifp->int_state |= IS_SICK;
544		ifp->int_act_time = NEVER;
545		trace_if("Chg", ifp);
546
547		LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
548	}
549}
550
551
552/* Mark an interface dead.
553 */
554void
555if_bad(struct interface *ifp)
556{
557	struct interface *ifp1;
558
559
560	if (ifp->int_state & IS_BROKE)
561		return;
562
563	LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
564
565	ifp->int_state |= (IS_BROKE | IS_SICK);
566	ifp->int_act_time = NEVER;
567	ifp->int_query_time = NEVER;
568	ifp->int_data.ts = now.tv_sec;
569
570	trace_if("Chg", ifp);
571
572	if (!(ifp->int_state & IS_ALIAS)) {
573		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
574			if (ifp1 != ifp
575			    && !strcmp(ifp->int_name, ifp1->int_name))
576				if_bad(ifp1);
577		}
578		(void)rn_walktree(rhead, walk_bad, 0);
579		if_bad_rdisc(ifp);
580	}
581}
582
583
584/* Mark an interface alive
585 */
586int					/* 1=it was dead */
587if_ok(struct interface *ifp,
588      const char *type)
589{
590	struct interface *ifp1;
591
592
593	if (!(ifp->int_state & IS_BROKE)) {
594		if (ifp->int_state & IS_SICK) {
595			trace_act("%sinterface %s to %s working better",
596				  type,
597				  ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
598			ifp->int_state &= ~IS_SICK;
599		}
600		return 0;
601	}
602
603	msglog("%sinterface %s to %s restored",
604	       type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
605	ifp->int_state &= ~(IS_BROKE | IS_SICK);
606	ifp->int_data.ts = 0;
607
608	if (!(ifp->int_state & IS_ALIAS)) {
609		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
610			if (ifp1 != ifp
611			    && !strcmp(ifp->int_name, ifp1->int_name))
612				if_ok(ifp1, type);
613		}
614		if_ok_rdisc(ifp);
615	}
616
617	if (ifp->int_state & IS_REMOTE) {
618		if (!addrouteforif(ifp))
619			return 0;
620	}
621	return 1;
622}
623
624
625/* disassemble routing message
626 */
627void
628rt_xaddrs(struct rt_addrinfo *info,
629	  struct sockaddr *sa,
630	  struct sockaddr *lim,
631	  int addrs)
632{
633	int i;
634#ifdef _HAVE_SA_LEN
635	static struct sockaddr sa_zero;
636#endif
637
638	memset(info, 0, sizeof(*info));
639	info->rti_addrs = addrs;
640	for (i = 0; i < RTAX_MAX && sa < lim; i++) {
641		if ((addrs & (1 << i)) == 0)
642			continue;
643		info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
644		sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
645	}
646}
647
648
649/* Find the network interfaces which have configured themselves.
650 *	This must be done regularly, if only for extra addresses
651 *	that come and go on interfaces.
652 */
653void
654ifinit(void)
655{
656	static char *sysctl_buf;
657	static size_t sysctl_buf_size = 0;
658	uint complaints = 0;
659	static u_int prev_complaints = 0;
660#	define COMP_NOT_INET	0x001
661#	define COMP_NOADDR	0x002
662#	define COMP_BADADDR	0x004
663#	define COMP_NODST	0x008
664#	define COMP_NOBADR	0x010
665#	define COMP_NOMASK	0x020
666#	define COMP_DUP		0x040
667#	define COMP_BAD_METRIC	0x080
668#	define COMP_NETMASK	0x100
669
670	struct interface ifs, ifs0, *ifp, *ifp1;
671	struct rt_entry *rt;
672	size_t needed;
673	int mib[6];
674	struct if_msghdr *ifm;
675	struct ifa_msghdr *ifam, *ifam_lim, *ifam2;
676	int in, ierr, out, oerr;
677	struct intnet *intnetp;
678	struct rt_addrinfo info;
679#ifdef SIOCGIFMETRIC
680	struct ifreq ifr;
681#endif
682
683
684	last_ifinit = now;
685	ifinit_timer.tv_sec = now.tv_sec + (supplier
686					    ? CHECK_ACT_INTERVAL
687					    : CHECK_QUIET_INTERVAL);
688
689	/* mark all interfaces so we can get rid of those that disappear */
690	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next)
691		ifp->int_state &= ~(IS_CHECKED | IS_DUP);
692
693	/* Fetch the interface list, without too many system calls
694	 * since we do it repeatedly.
695	 */
696	mib[0] = CTL_NET;
697	mib[1] = PF_ROUTE;
698	mib[2] = 0;
699	mib[3] = AF_INET;
700	mib[4] = NET_RT_IFLIST;
701	mib[5] = 0;
702	for (;;) {
703		if ((needed = sysctl_buf_size) != 0) {
704			if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
705				break;
706			/* retry if the table grew */
707			if (errno != ENOMEM && errno != EFAULT)
708				BADERR(1, "ifinit: sysctl(RT_IFLIST)");
709			free(sysctl_buf);
710			needed = 0;
711		}
712		if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
713			BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
714		sysctl_buf = rtmalloc(sysctl_buf_size = needed,
715				      "ifinit sysctl");
716	}
717
718	ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
719	for (ifam = (struct ifa_msghdr *)sysctl_buf;
720	     ifam < ifam_lim;
721	     ifam = ifam2) {
722
723		ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
724
725#ifdef RTM_OIFINFO
726		if (ifam->ifam_type == RTM_OIFINFO)
727			continue;	/* just ignore compat message */
728#endif
729		if (ifam->ifam_type == RTM_IFINFO) {
730			struct sockaddr_dl *sdl;
731
732			ifm = (struct if_msghdr *)ifam;
733			/* make prototype structure for the IP aliases
734			 */
735			memset(&ifs0, 0, sizeof(ifs0));
736			ifs0.int_rip_sock = -1;
737			ifs0.int_index = ifm->ifm_index;
738			ifs0.int_if_flags = ifm->ifm_flags;
739			ifs0.int_state = IS_CHECKED;
740			ifs0.int_query_time = NEVER;
741			ifs0.int_act_time = now.tv_sec;
742			ifs0.int_data.ts = now.tv_sec;
743			ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
744			ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
745			ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
746			ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
747#ifdef sgi
748			ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
749#endif
750			sdl = (struct sockaddr_dl *)(ifm + 1);
751			sdl->sdl_data[sdl->sdl_nlen] = 0;
752			strncpy(ifs0.int_name, sdl->sdl_data,
753				MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
754			continue;
755		}
756		if (ifam->ifam_type != RTM_NEWADDR) {
757			logbad(1,"ifinit: out of sync");
758			continue;
759		}
760		rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
761			  (struct sockaddr *)ifam2,
762			  ifam->ifam_addrs);
763
764		/* Prepare for the next address of this interface, which
765		 * will be an alias.
766		 * Do not output RIP or Router-Discovery packets via aliases.
767		 */
768		memcpy(&ifs, &ifs0, sizeof(ifs));
769		ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
770
771		if (INFO_IFA(&info) == 0) {
772			if (iff_up(ifs.int_if_flags)) {
773				if (!(prev_complaints & COMP_NOADDR))
774					msglog("%s has no address",
775					       ifs.int_name);
776				complaints |= COMP_NOADDR;
777			}
778			continue;
779		}
780		if (INFO_IFA(&info)->sa_family != AF_INET) {
781			if (iff_up(ifs.int_if_flags)) {
782				if (!(prev_complaints & COMP_NOT_INET))
783					trace_act("%s: not AF_INET",
784						  ifs.int_name);
785				complaints |= COMP_NOT_INET;
786			}
787			continue;
788		}
789
790		ifs.int_addr = S_ADDR(INFO_IFA(&info));
791
792		if (ntohl(ifs.int_addr)>>24 == 0
793		    || ntohl(ifs.int_addr)>>24 == 0xff) {
794			if (iff_up(ifs.int_if_flags)) {
795				if (!(prev_complaints & COMP_BADADDR))
796					msglog("%s has a bad address",
797					       ifs.int_name);
798				complaints |= COMP_BADADDR;
799			}
800			continue;
801		}
802
803		if (ifs.int_if_flags & IFF_LOOPBACK) {
804			ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
805			if (ifs.int_addr == htonl(INADDR_LOOPBACK))
806				ifs.int_state |= IS_PASSIVE;
807			ifs.int_dstaddr = ifs.int_addr;
808			ifs.int_mask = HOST_MASK;
809			ifs.int_ripv1_mask = HOST_MASK;
810			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
811			ifs.int_net = ntohl(ifs.int_dstaddr);
812			if (!foundloopback) {
813				foundloopback = 1;
814				loopaddr = ifs.int_addr;
815				loop_rts.rts_gate = loopaddr;
816				loop_rts.rts_router = loopaddr;
817			}
818
819		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
820			if (INFO_BRD(&info) == 0
821			    || INFO_BRD(&info)->sa_family != AF_INET) {
822				if (iff_up(ifs.int_if_flags)) {
823					if (!(prev_complaints & COMP_NODST))
824						msglog("%s has a bad"
825						       " destination address",
826						       ifs.int_name);
827					complaints |= COMP_NODST;
828				}
829				continue;
830			}
831			ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
832			if (ntohl(ifs.int_dstaddr)>>24 == 0
833			    || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
834				if (iff_up(ifs.int_if_flags)) {
835					if (!(prev_complaints & COMP_NODST))
836						msglog("%s has a bad"
837						       " destination address",
838						       ifs.int_name);
839					complaints |= COMP_NODST;
840				}
841				continue;
842			}
843			ifs.int_mask = HOST_MASK;
844			ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
845			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
846			ifs.int_net = ntohl(ifs.int_dstaddr);
847
848		}  else {
849			if (INFO_MASK(&info) == 0) {
850				if (iff_up(ifs.int_if_flags)) {
851					if (!(prev_complaints & COMP_NOMASK))
852						msglog("%s has no netmask",
853						       ifs.int_name);
854					complaints |= COMP_NOMASK;
855				}
856				continue;
857			}
858			ifs.int_dstaddr = ifs.int_addr;
859			ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
860			ifs.int_ripv1_mask = ifs.int_mask;
861			ifs.int_std_mask = std_mask(ifs.int_addr);
862			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
863			if (ifs.int_mask != ifs.int_std_mask)
864				ifs.int_state |= IS_SUBNET;
865
866			if (ifs.int_if_flags & IFF_BROADCAST) {
867				if (INFO_BRD(&info) == 0) {
868					if (iff_up(ifs.int_if_flags)) {
869					    if (!(prev_complaints
870						  & COMP_NOBADR))
871						msglog("%s has"
872						       "no broadcast address",
873						       ifs.int_name);
874					    complaints |= COMP_NOBADR;
875					}
876					continue;
877				}
878				ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
879			}
880		}
881		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
882		ifs.int_std_addr = htonl(ifs.int_std_net);
883
884		/* Use a minimum metric of one.  Treat the interface metric
885		 * (default 0) as an increment to the hop count of one.
886		 *
887		 * The metric obtained from the routing socket dump of
888		 * interface addresses is wrong.  It is not set by the
889		 * SIOCSIFMETRIC ioctl.
890		 */
891#ifdef SIOCGIFMETRIC
892		strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
893		if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
894			DBGERR(1, "ioctl(SIOCGIFMETRIC)");
895			ifs.int_metric = 0;
896		} else {
897			ifs.int_metric = ifr.ifr_metric;
898		}
899#else
900		ifs.int_metric = ifam->ifam_metric;
901#endif
902		if (ifs.int_metric > HOPCNT_INFINITY) {
903			ifs.int_metric = 0;
904			if (!(prev_complaints & COMP_BAD_METRIC)
905			    && iff_up(ifs.int_if_flags)) {
906				complaints |= COMP_BAD_METRIC;
907				msglog("%s has a metric of %d",
908				       ifs.int_name, ifs.int_metric);
909			}
910		}
911
912		/* See if this is a familiar interface.
913		 * If so, stop worrying about it if it is the same.
914		 * Start it over if it now is to somewhere else, as happens
915		 * frequently with PPP and SLIP.
916		 */
917		ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
918						? ifs.int_addr
919						: 0));
920		if (ifp != 0) {
921			ifp->int_state |= IS_CHECKED;
922
923			if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
924				  & (IFF_BROADCAST
925				     | IFF_LOOPBACK
926				     | IFF_POINTOPOINT
927				     | IFF_MULTICAST))
928			    || 0 != ((ifp->int_state ^ ifs.int_state)
929				     & IS_ALIAS)
930			    || ifp->int_addr != ifs.int_addr
931			    || ifp->int_brdaddr != ifs.int_brdaddr
932			    || ifp->int_dstaddr != ifs.int_dstaddr
933			    || ifp->int_mask != ifs.int_mask
934			    || ifp->int_metric != ifs.int_metric) {
935				/* Forget old information about
936				 * a changed interface.
937				 */
938				trace_act("interface %s has changed",
939					  ifp->int_name);
940				ifdel(ifp);
941				ifp = 0;
942			}
943		}
944
945		if (ifp != 0) {
946			/* The primary representative of an alias worries
947			 * about how things are working.
948			 */
949			if (ifp->int_state & IS_ALIAS)
950				continue;
951
952			/* note interfaces that have been turned off
953			 */
954			if (!iff_up(ifs.int_if_flags)) {
955				if (iff_up(ifp->int_if_flags)) {
956					msglog("interface %s to %s turned off",
957					       ifp->int_name,
958					       naddr_ntoa(ifp->int_dstaddr));
959					if_bad(ifp);
960					ifp->int_if_flags &= ~IFF_UP;
961				} else if (now.tv_sec>(ifp->int_data.ts
962						       + CHECK_BAD_INTERVAL)) {
963					trace_act("interface %s has been off"
964						  " %ld seconds; forget it",
965						  ifp->int_name,
966						  now.tv_sec-ifp->int_data.ts);
967					ifdel(ifp);
968				}
969				continue;
970			}
971			/* or that were off and are now ok */
972			if (!iff_up(ifp->int_if_flags)) {
973				ifp->int_if_flags |= IFF_UP;
974				(void)if_ok(ifp, "");
975			}
976
977			/* If it has been long enough,
978			 * see if the interface is broken.
979			 */
980			if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
981				continue;
982
983			in = ifs.int_data.ipackets - ifp->int_data.ipackets;
984			ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
985			out = ifs.int_data.opackets - ifp->int_data.opackets;
986			oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
987#ifdef sgi
988			/* Through at least IRIX 6.2, PPP and SLIP
989			 * count packets dropped by the filters.
990			 * But FDDI rings stuck non-operational count
991			 * dropped packets as they wait for improvement.
992			 */
993			if (!(ifp->int_if_flags & IFF_POINTOPOINT))
994				oerr += (ifs.int_data.odrops
995					 - ifp->int_data.odrops);
996#endif
997			/* If the interface just awoke, restart the counters.
998			 */
999			if (ifp->int_data.ts == 0) {
1000				ifp->int_data = ifs.int_data;
1001				continue;
1002			}
1003			ifp->int_data = ifs.int_data;
1004
1005			/* Withhold judgment when the short error
1006			 * counters wrap or the interface is reset.
1007			 */
1008			if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1009				LIM_SEC(ifinit_timer,
1010					now.tv_sec+CHECK_BAD_INTERVAL);
1011				continue;
1012			}
1013
1014			/* Withhold judgement when there is no traffic
1015			 */
1016			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1017				continue;
1018
1019			/* It is bad if input or output is not working.
1020			 * Require presistent problems before marking it dead.
1021			 */
1022			if ((in <= ierr && ierr > 0)
1023			    || (out <= oerr && oerr > 0)) {
1024				if (!(ifp->int_state & IS_SICK)) {
1025					trace_act("interface %s to %s"
1026						  " sick: in=%d ierr=%d"
1027						  " out=%d oerr=%d",
1028						  ifp->int_name,
1029						  naddr_ntoa(ifp->int_dstaddr),
1030						  in, ierr, out, oerr);
1031					if_sick(ifp);
1032					continue;
1033				}
1034				if (!(ifp->int_state & IS_BROKE)) {
1035					msglog("interface %s to %s broken:"
1036					       " in=%d ierr=%d out=%d oerr=%d",
1037					       ifp->int_name,
1038					       naddr_ntoa(ifp->int_dstaddr),
1039					       in, ierr, out, oerr);
1040					if_bad(ifp);
1041				}
1042				continue;
1043			}
1044
1045			/* otherwise, it is active and healthy
1046			 */
1047			ifp->int_act_time = now.tv_sec;
1048			(void)if_ok(ifp, "");
1049			continue;
1050		}
1051
1052		/* This is a new interface.
1053		 * If it is dead, forget it.
1054		 */
1055		if (!iff_up(ifs.int_if_flags))
1056			continue;
1057
1058		/* If it duplicates an existing interface,
1059		 * complain about it, mark the other one
1060		 * duplicated, and forget this one.
1061		 */
1062		ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1063				ifs.int_if_flags);
1064		if (ifp != 0) {
1065			/* Ignore duplicates of itself, caused by having
1066			 * IP aliases on the same network.
1067			 */
1068			if (!strcmp(ifp->int_name, ifs.int_name))
1069				continue;
1070
1071			if (!(prev_complaints & COMP_DUP)) {
1072				complaints |= COMP_DUP;
1073				msglog("%s (%s%s%s) is duplicated by"
1074				       " %s (%s%s%s)",
1075				       ifs.int_name,
1076				       addrname(ifs.int_addr,ifs.int_mask,1),
1077				       ((ifs.int_if_flags & IFF_POINTOPOINT)
1078					? "-->" : ""),
1079				       ((ifs.int_if_flags & IFF_POINTOPOINT)
1080					? naddr_ntoa(ifs.int_dstaddr) : ""),
1081				       ifp->int_name,
1082				       addrname(ifp->int_addr,ifp->int_mask,1),
1083				       ((ifp->int_if_flags & IFF_POINTOPOINT)
1084					? "-->" : ""),
1085				       ((ifp->int_if_flags & IFF_POINTOPOINT)
1086					? naddr_ntoa(ifp->int_dstaddr) : ""));
1087			}
1088			ifp->int_state |= IS_DUP;
1089			continue;
1090		}
1091
1092		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
1093			trace_act("%s is neither broadcast, point-to-point,"
1094				  " nor loopback",
1095				  ifs.int_name);
1096			if (!(ifs.int_state & IFF_MULTICAST))
1097				ifs.int_state |= IS_NO_RDISC;
1098		}
1099
1100
1101		/* It is new and ok.   Add it to the list of interfaces
1102		 */
1103		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1104		memcpy(ifp, &ifs, sizeof(*ifp));
1105		get_parms(ifp);
1106		if_link(ifp);
1107		trace_if("Add", ifp);
1108
1109		/* Notice likely bad netmask.
1110		 */
1111		if (!(prev_complaints & COMP_NETMASK)
1112		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
1113		    && ifp->int_addr != RIP_DEFAULT) {
1114			for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
1115				if (ifp1->int_mask == ifp->int_mask)
1116					continue;
1117				if (ifp1->int_if_flags & IFF_POINTOPOINT)
1118					continue;
1119				if (ifp1->int_dstaddr == RIP_DEFAULT)
1120					continue;
1121				/* ignore aliases on the right network */
1122				if (!strcmp(ifp->int_name, ifp1->int_name))
1123					continue;
1124				if (on_net(ifp->int_dstaddr,
1125					   ifp1->int_net, ifp1->int_mask)
1126				    || on_net(ifp1->int_dstaddr,
1127					      ifp->int_net, ifp->int_mask)) {
1128					msglog("possible netmask problem"
1129					       " between %s:%s and %s:%s",
1130					       ifp->int_name,
1131					       addrname(htonl(ifp->int_net),
1132							ifp->int_mask, 1),
1133					       ifp1->int_name,
1134					       addrname(htonl(ifp1->int_net),
1135							ifp1->int_mask, 1));
1136					complaints |= COMP_NETMASK;
1137				}
1138			}
1139		}
1140
1141		if (!(ifp->int_state & IS_ALIAS)) {
1142			/* Count the # of directly connected networks.
1143			 */
1144			if (!(ifp->int_if_flags & IFF_LOOPBACK))
1145				tot_interfaces++;
1146			if (!IS_RIP_OFF(ifp->int_state))
1147				rip_interfaces++;
1148
1149			/* turn on router discovery and RIP If needed */
1150			if_ok_rdisc(ifp);
1151			rip_on(ifp);
1152		}
1153	}
1154
1155	/* If we are multi-homed and have at least two interfaces
1156	 * listening to RIP, then output by default.
1157	 */
1158	if (!supplier_set && rip_interfaces > 1)
1159		set_supplier();
1160
1161	/* If we are multi-homed, optionally advertise a route to
1162	 * our main address.
1163	 */
1164	if (advertise_mhome
1165	    || (tot_interfaces > 1
1166		&& mhome
1167		&& (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1168		&& foundloopback)) {
1169		advertise_mhome = 1;
1170		rt = rtget(myaddr, HOST_MASK);
1171		if (rt != 0) {
1172			if (rt->rt_ifp != ifp
1173			    || rt->rt_router != loopaddr) {
1174				rtdelete(rt);
1175				rt = 0;
1176			} else {
1177				loop_rts.rts_ifp = ifp;
1178				loop_rts.rts_metric = 0;
1179				loop_rts.rts_time = rt->rt_time;
1180				rtchange(rt, rt->rt_state | RS_MHOME,
1181					 &loop_rts, 0);
1182			}
1183		}
1184		if (rt == 0) {
1185			loop_rts.rts_ifp = ifp;
1186			loop_rts.rts_metric = 0;
1187			rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1188		}
1189	}
1190
1191	for (ifp = ifnet; ifp != 0; ifp = ifp1) {
1192		ifp1 = ifp->int_next;	/* because we may delete it */
1193
1194		/* Forget any interfaces that have disappeared.
1195		 */
1196		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1197			trace_act("interface %s has disappeared",
1198				  ifp->int_name);
1199			ifdel(ifp);
1200			continue;
1201		}
1202
1203		if ((ifp->int_state & IS_BROKE)
1204		    && !(ifp->int_state & IS_PASSIVE))
1205			LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1206
1207		/* If we ever have a RIPv1 interface, assume we always will.
1208		 * It might come back if it ever goes away.
1209		 */
1210		if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1211			have_ripv1_out = 1;
1212		if (!(ifp->int_state & IS_NO_RIPV1_IN))
1213			have_ripv1_in = 1;
1214	}
1215
1216	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1217		/* Ensure there is always a network route for interfaces,
1218		 * after any dead interfaces have been deleted, which
1219		 * might affect routes for point-to-point links.
1220		 */
1221		if (!addrouteforif(ifp))
1222			continue;
1223
1224		/* Add routes to the local end of point-to-point interfaces
1225		 * using loopback.
1226		 */
1227		if ((ifp->int_if_flags & IFF_POINTOPOINT)
1228		    && !(ifp->int_state & IS_REMOTE)
1229		    && foundloopback) {
1230			/* Delete any routes to the network address through
1231			 * foreign routers. Remove even static routes.
1232			 */
1233			del_static(ifp->int_addr, HOST_MASK, 0, 0);
1234			rt = rtget(ifp->int_addr, HOST_MASK);
1235			if (rt != 0 && rt->rt_router != loopaddr) {
1236				rtdelete(rt);
1237				rt = 0;
1238			}
1239			if (rt != 0) {
1240				if (!(rt->rt_state & RS_LOCAL)
1241				    || rt->rt_metric > ifp->int_metric) {
1242					ifp1 = ifp;
1243				} else {
1244					ifp1 = rt->rt_ifp;
1245				}
1246				loop_rts.rts_ifp = ifp1;
1247				loop_rts.rts_metric = 0;
1248				loop_rts.rts_time = rt->rt_time;
1249				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1250					      | (RS_IF|RS_LOCAL)),
1251					 &loop_rts, 0);
1252			} else {
1253				loop_rts.rts_ifp = ifp;
1254				loop_rts.rts_metric = 0;
1255				rtadd(ifp->int_addr, HOST_MASK,
1256				      (RS_IF | RS_LOCAL), &loop_rts);
1257			}
1258		}
1259	}
1260
1261	/* add the authority routes */
1262	for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1263		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1264		if (rt != 0
1265		    && !(rt->rt_state & RS_NO_NET_SYN)
1266		    && !(rt->rt_state & RS_NET_INT)) {
1267			rtdelete(rt);
1268			rt = 0;
1269		}
1270		if (rt == 0) {
1271			loop_rts.rts_ifp = 0;
1272			loop_rts.rts_metric = intnetp->intnet_metric-1;
1273			rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1274			      RS_NET_SYN | RS_NET_INT, &loop_rts);
1275		}
1276	}
1277
1278	prev_complaints = complaints;
1279}
1280
1281
1282static void
1283check_net_syn(struct interface *ifp)
1284{
1285	struct rt_entry *rt;
1286	static struct rt_spare new;
1287
1288
1289	/* Turn on the need to automatically synthesize a network route
1290	 * for this interface only if we are running RIPv1 on some other
1291	 * interface that is on a different class-A,B,or C network.
1292	 */
1293	if (have_ripv1_out || have_ripv1_in) {
1294		ifp->int_state |= IS_NEED_NET_SYN;
1295		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1296		if (rt != 0
1297		    && 0 == (rt->rt_state & RS_NO_NET_SYN)
1298		    && (!(rt->rt_state & RS_NET_SYN)
1299			|| rt->rt_metric > ifp->int_metric)) {
1300			rtdelete(rt);
1301			rt = 0;
1302		}
1303		if (rt == 0) {
1304			new.rts_ifp = ifp;
1305			new.rts_gate = ifp->int_addr;
1306			new.rts_router = ifp->int_addr;
1307			new.rts_metric = ifp->int_metric;
1308			rtadd(ifp->int_std_addr, ifp->int_std_mask,
1309			      RS_NET_SYN, &new);
1310		}
1311
1312	} else {
1313		ifp->int_state &= ~IS_NEED_NET_SYN;
1314
1315		rt = rtget(ifp->int_std_addr,
1316			   ifp->int_std_mask);
1317		if (rt != 0
1318		    && (rt->rt_state & RS_NET_SYN)
1319		    && rt->rt_ifp == ifp)
1320			rtbad_sub(rt);
1321	}
1322}
1323
1324
1325/* Add route for interface if not currently installed.
1326 * Create route to other end if a point-to-point link,
1327 * otherwise a route to this (sub)network.
1328 */
1329int					/* 0=bad interface */
1330addrouteforif(struct interface *ifp)
1331{
1332	struct rt_entry *rt;
1333	static struct rt_spare new;
1334	naddr dst;
1335
1336
1337	/* skip sick interfaces
1338	 */
1339	if (ifp->int_state & IS_BROKE)
1340		return 0;
1341
1342	/* If the interface on a subnet, then install a RIPv1 route to
1343	 * the network as well (unless it is sick).
1344	 */
1345	if (ifp->int_state & IS_SUBNET)
1346		check_net_syn(ifp);
1347
1348	dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1349	       ? ifp->int_dstaddr
1350	       : htonl(ifp->int_net));
1351
1352	new.rts_ifp = ifp;
1353	new.rts_router = ifp->int_addr;
1354	new.rts_gate = ifp->int_addr;
1355	new.rts_metric = ifp->int_metric;
1356	new.rts_time = now.tv_sec;
1357
1358	/* If we are going to send packets to the gateway,
1359	 * it must be reachable using our physical interfaces
1360	 */
1361	if ((ifp->int_state & IS_REMOTE)
1362	    && !(ifp->int_state & IS_EXTERNAL)
1363	    && !check_remote(ifp))
1364		return 0;
1365
1366	/* We are finished if the correct main interface route exists.
1367	 * The right route must be for the right interface, not synthesized
1368	 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1369	 */
1370	del_static(dst, ifp->int_mask, 0, 0);
1371	rt = rtget(dst, ifp->int_mask);
1372	if (rt != 0) {
1373		if ((rt->rt_ifp != ifp
1374		     || rt->rt_router != ifp->int_addr)
1375		    && (!(ifp->int_state & IS_DUP)
1376			|| rt->rt_ifp == 0
1377			|| (rt->rt_ifp->int_state & IS_BROKE))) {
1378			rtdelete(rt);
1379			rt = 0;
1380		} else {
1381			rtchange(rt, ((rt->rt_state | RS_IF)
1382				      & ~(RS_NET_SYN | RS_LOCAL)),
1383				 &new, 0);
1384		}
1385	}
1386	if (rt == 0) {
1387		if (ifp->int_transitions++ > 0)
1388			trace_act("re-install interface %s",
1389				  ifp->int_name);
1390
1391		rtadd(dst, ifp->int_mask, RS_IF, &new);
1392	}
1393
1394	return 1;
1395}
1396