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