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