xref: /netbsd-src/sbin/routed/if.c (revision 79d830d6a01a1c0a8debd023345baa2b0267c0ed)
1*79d830d6Smaya /*	$NetBSD: if.c,v 1.31 2017/10/02 11:02:19 maya Exp $	*/
20114e805Scgd 
361f28255Scgd /*
44c8599d3Smycroft  * Copyright (c) 1983, 1993
54c8599d3Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1561f28255Scgd  * 3. All advertising materials mentioning features or use of this software
1694b2d428Schristos  *    must display the following acknowledgment:
1761f28255Scgd  *	This product includes software developed by the University of
1861f28255Scgd  *	California, Berkeley and its contributors.
1961f28255Scgd  * 4. Neither the name of the University nor the names of its contributors
2061f28255Scgd  *    may be used to endorse or promote products derived from this software
2161f28255Scgd  *    without specific prior written permission.
2261f28255Scgd  *
2361f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2461f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2561f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2661f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2761f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2861f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2961f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3061f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3161f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3261f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3361f28255Scgd  * SUCH DAMAGE.
3461f28255Scgd  */
3561f28255Scgd 
3661f28255Scgd #include "defs.h"
37fc1a5246Sthorpej #include "pathnames.h"
3861f28255Scgd 
394fea751dSchristos #ifdef __NetBSD__
40*79d830d6Smaya __RCSID("$NetBSD: if.c,v 1.31 2017/10/02 11:02:19 maya Exp $");
414fea751dSchristos #elif defined(__FreeBSD__)
424fea751dSchristos __RCSID("$FreeBSD$");
434fea751dSchristos #else
44f93fe60aSchristos __RCSID("Revision: 2.27 ");
45f93fe60aSchristos #ident "Revision: 2.27 "
464fea751dSchristos #endif
474fea751dSchristos 
48fc1a5246Sthorpej struct interface *ifnet;		/* all interfaces */
49e7512e5aSchristos 
50e7512e5aSchristos /* hash table for all interfaces, big enough to tolerate ridiculous
51e7512e5aSchristos  * numbers of IP aliases.  Crazy numbers of aliases such as 7000
52e7512e5aSchristos  * still will not do well, but not just in looking up interfaces
53e7512e5aSchristos  * by name or address.
54e7512e5aSchristos  */
55e7512e5aSchristos #define AHASH_LEN 211			/* must be prime */
56e7512e5aSchristos #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
57e7512e5aSchristos struct interface *ahash_tbl[AHASH_LEN];
58e7512e5aSchristos 
59e7512e5aSchristos #define BHASH_LEN 211			/* must be prime */
60e7512e5aSchristos #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
61e7512e5aSchristos struct interface *bhash_tbl[BHASH_LEN];
62e7512e5aSchristos 
63e7512e5aSchristos struct interface *remote_if;		/* remote interfaces */
64e7512e5aSchristos 
65e7512e5aSchristos /* hash for physical interface names.
66e7512e5aSchristos  * Assume there are never more 100 or 200 real interfaces, and that
67e7512e5aSchristos  * aliases are put on the end of the hash chains.
68e7512e5aSchristos  */
69e7512e5aSchristos #define NHASH_LEN 97
70e7512e5aSchristos struct interface *nhash_tbl[NHASH_LEN];
71e7512e5aSchristos 
72fc1a5246Sthorpej int	tot_interfaces;			/* # of remote and local interfaces */
73fc1a5246Sthorpej int	rip_interfaces;			/* # of interfaces doing RIP */
74fc1a5246Sthorpej int	foundloopback;			/* valid flag for loopaddr */
75fc1a5246Sthorpej naddr	loopaddr;			/* our address on loopback */
766d8ef4dfSthorpej struct	rt_spare loop_rts;
7761f28255Scgd 
78fc1a5246Sthorpej struct timeval ifinit_timer;
79e7512e5aSchristos static struct timeval last_ifinit;
806d8ef4dfSthorpej #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec		\
816d8ef4dfSthorpej 			   && last_ifinit.tv_usec == now.tv_usec	\
826d8ef4dfSthorpej 			   && timercmp(&ifinit_timer, &now, >))
83fc1a5246Sthorpej 
84fc1a5246Sthorpej int	have_ripv1_out;			/* have a RIPv1 interface */
85fc1a5246Sthorpej int	have_ripv1_in;
86fc1a5246Sthorpej 
87fc1a5246Sthorpej 
88e7512e5aSchristos static struct interface**
nhash(char * p)893f50343aSlukem nhash(char *p)
90e7512e5aSchristos {
913f50343aSlukem 	u_int i;
92e7512e5aSchristos 
93e7512e5aSchristos 	for (i = 0; *p != '\0'; p++) {
94e7512e5aSchristos 		i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
95e7512e5aSchristos 		i ^= *p;
96e7512e5aSchristos 	}
97e7512e5aSchristos 	return &nhash_tbl[i % NHASH_LEN];
98e7512e5aSchristos }
99e7512e5aSchristos 
100e7512e5aSchristos 
101e7512e5aSchristos /* Link a new interface into the lists and hash tables.
102e7512e5aSchristos  */
103e7512e5aSchristos void
if_link(struct interface * ifp)104e7512e5aSchristos if_link(struct interface *ifp)
105e7512e5aSchristos {
106e7512e5aSchristos 	struct interface **hifp;
107e7512e5aSchristos 
108e7512e5aSchristos 	ifp->int_prev = &ifnet;
109e7512e5aSchristos 	ifp->int_next = ifnet;
110e7512e5aSchristos 	if (ifnet != 0)
111e7512e5aSchristos 		ifnet->int_prev = &ifp->int_next;
112e7512e5aSchristos 	ifnet = ifp;
113e7512e5aSchristos 
114e7512e5aSchristos 	hifp = AHASH(ifp->int_addr);
115e7512e5aSchristos 	ifp->int_ahash_prev = hifp;
116e7512e5aSchristos 	if ((ifp->int_ahash = *hifp) != 0)
117e7512e5aSchristos 		(*hifp)->int_ahash_prev = &ifp->int_ahash;
118e7512e5aSchristos 	*hifp = ifp;
119e7512e5aSchristos 
120e7512e5aSchristos 	if (ifp->int_if_flags & IFF_BROADCAST) {
121e7512e5aSchristos 		hifp = BHASH(ifp->int_brdaddr);
122e7512e5aSchristos 		ifp->int_bhash_prev = hifp;
123e7512e5aSchristos 		if ((ifp->int_bhash = *hifp) != 0)
124e7512e5aSchristos 			(*hifp)->int_bhash_prev = &ifp->int_bhash;
125e7512e5aSchristos 		*hifp = ifp;
126e7512e5aSchristos 	}
127e7512e5aSchristos 
128e7512e5aSchristos 	if (ifp->int_state & IS_REMOTE) {
129e7512e5aSchristos 		ifp->int_rlink_prev = &remote_if;
130e7512e5aSchristos 		ifp->int_rlink = remote_if;
131e7512e5aSchristos 		if (remote_if != 0)
132e7512e5aSchristos 			remote_if->int_rlink_prev = &ifp->int_rlink;
133e7512e5aSchristos 		remote_if = ifp;
134e7512e5aSchristos 	}
135e7512e5aSchristos 
136e7512e5aSchristos 	hifp = nhash(ifp->int_name);
137e7512e5aSchristos 	if (ifp->int_state & IS_ALIAS) {
138e7512e5aSchristos 		/* put aliases on the end of the hash chain */
139e7512e5aSchristos 		while (*hifp != 0)
140e7512e5aSchristos 			hifp = &(*hifp)->int_nhash;
141e7512e5aSchristos 	}
142e7512e5aSchristos 	ifp->int_nhash_prev = hifp;
143e7512e5aSchristos 	if ((ifp->int_nhash = *hifp) != 0)
144e7512e5aSchristos 		(*hifp)->int_nhash_prev = &ifp->int_nhash;
145e7512e5aSchristos 	*hifp = ifp;
146e7512e5aSchristos }
147e7512e5aSchristos 
148e7512e5aSchristos 
149fc1a5246Sthorpej /* Find the interface with an address
15061f28255Scgd  */
15161f28255Scgd struct interface *
ifwithaddr(naddr addr,int bcast,int remote)152fc1a5246Sthorpej ifwithaddr(naddr addr,
153fc1a5246Sthorpej 	   int	bcast,			/* notice IFF_BROADCAST address */
154fc1a5246Sthorpej 	   int	remote)			/* include IS_REMOTE interfaces */
15561f28255Scgd {
156fc1a5246Sthorpej 	struct interface *ifp, *possible = 0;
15761f28255Scgd 
158e7512e5aSchristos 	remote = (remote == 0) ? IS_REMOTE : 0;
159e7512e5aSchristos 
160e7512e5aSchristos 	for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
161e7512e5aSchristos 		if (ifp->int_addr != addr)
16261f28255Scgd 			continue;
163e7512e5aSchristos 		if ((ifp->int_state & remote) != 0)
164e7512e5aSchristos 			continue;
165e7512e5aSchristos 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
166fc1a5246Sthorpej 			return ifp;
167fc1a5246Sthorpej 		possible = ifp;
16861f28255Scgd 	}
169e7512e5aSchristos 
170e7512e5aSchristos 	if (possible || !bcast)
171e7512e5aSchristos 		return possible;
172e7512e5aSchristos 
173e7512e5aSchristos 	for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
174e7512e5aSchristos 		if (ifp->int_brdaddr != addr)
175e7512e5aSchristos 			continue;
176e7512e5aSchristos 		if ((ifp->int_state & remote) != 0)
177e7512e5aSchristos 			continue;
178e7512e5aSchristos 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
179e7512e5aSchristos 			return ifp;
180e7512e5aSchristos 		possible = ifp;
18161f28255Scgd 	}
18261f28255Scgd 
183fc1a5246Sthorpej 	return possible;
184fc1a5246Sthorpej }
185fc1a5246Sthorpej 
186fc1a5246Sthorpej 
187fc1a5246Sthorpej /* find the interface with a name
18861f28255Scgd  */
18961f28255Scgd struct interface *
ifwithname(char * name,naddr addr)190fc1a5246Sthorpej ifwithname(char *name,			/* "ec0" or whatever */
191fc1a5246Sthorpej 	   naddr addr)			/* 0 or network address */
19261f28255Scgd {
193fc1a5246Sthorpej 	struct interface *ifp;
19461f28255Scgd 
195e7512e5aSchristos 	for (;;) {
196e7512e5aSchristos 		for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
197e7512e5aSchristos 			/* If the network address is not specified,
198e7512e5aSchristos 			 * ignore any alias interfaces.  Otherwise, look
199e7512e5aSchristos 			 * for the interface with the target name and address.
200e7512e5aSchristos 			 */
201fc1a5246Sthorpej 			if (!strcmp(ifp->int_name, name)
202e7512e5aSchristos 			    && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
203e7512e5aSchristos 				|| (ifp->int_addr == addr)))
204fc1a5246Sthorpej 				return ifp;
20561f28255Scgd 		}
206e7512e5aSchristos 
207e7512e5aSchristos 		/* If there is no known interface, maybe there is a
208e7512e5aSchristos 		 * new interface.  So just once look for new interfaces.
209e7512e5aSchristos 		 */
2106d8ef4dfSthorpej 		if (IF_RESCAN_DELAY())
211fc1a5246Sthorpej 			return 0;
212e7512e5aSchristos 		ifinit();
213e7512e5aSchristos 	}
21461f28255Scgd }
21561f28255Scgd 
216fc1a5246Sthorpej 
21761f28255Scgd struct interface *
ifwithindex(u_short ifindex,int rescan_ok)218cbbd79f7Slukem ifwithindex(u_short ifindex,
219e7512e5aSchristos 	    int rescan_ok)
22061f28255Scgd {
221fc1a5246Sthorpej 	struct interface *ifp;
22261f28255Scgd 
223e7512e5aSchristos 	for (;;) {
224fc1a5246Sthorpej 		for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
225cbbd79f7Slukem 			if (ifp->int_index == ifindex)
226fc1a5246Sthorpej 				return ifp;
22761f28255Scgd 		}
228e7512e5aSchristos 
229e7512e5aSchristos 		/* If there is no known interface, maybe there is a
230e7512e5aSchristos 		 * new interface.  So just once look for new interfaces.
231e7512e5aSchristos 		 */
232e7512e5aSchristos 		if (!rescan_ok
2336d8ef4dfSthorpej 		    || IF_RESCAN_DELAY())
234fc1a5246Sthorpej 			return 0;
235e7512e5aSchristos 		ifinit();
236e7512e5aSchristos 	}
23761f28255Scgd }
23861f28255Scgd 
239fc1a5246Sthorpej 
240fc1a5246Sthorpej /* Find an interface from which the specified address
24161f28255Scgd  * should have come from.  Used for figuring out which
242e7512e5aSchristos  * interface a packet came in on.
24361f28255Scgd  */
24461f28255Scgd struct interface *
iflookup(naddr addr)245fc1a5246Sthorpej iflookup(naddr addr)
24661f28255Scgd {
247fc1a5246Sthorpej 	struct interface *ifp, *maybe;
24888f6fad4Schristos 	int once = 0;
24961f28255Scgd 
25061f28255Scgd 	maybe = 0;
251e7512e5aSchristos 	for (;;) {
25261f28255Scgd 		for (ifp = ifnet; ifp; ifp = ifp->int_next) {
253fc1a5246Sthorpej 			if (ifp->int_if_flags & IFF_POINTOPOINT) {
254fc1a5246Sthorpej 				/* finished with a match */
255e7512e5aSchristos 				if (ifp->int_dstaddr == addr)
256fc1a5246Sthorpej 					return ifp;
257fc1a5246Sthorpej 
258fc1a5246Sthorpej 			} else {
259fc1a5246Sthorpej 				/* finished with an exact match */
260fc1a5246Sthorpej 				if (ifp->int_addr == addr)
261fc1a5246Sthorpej 					return ifp;
262fc1a5246Sthorpej 
263fc1a5246Sthorpej 				/* Look for the longest approximate match.
264fc1a5246Sthorpej 				 */
265fc1a5246Sthorpej 				if (on_net(addr, ifp->int_net, ifp->int_mask)
266fc1a5246Sthorpej 				    && (maybe == 0
267fc1a5246Sthorpej 					|| ifp->int_mask > maybe->int_mask))
26861f28255Scgd 					maybe = ifp;
26961f28255Scgd 			}
270fc1a5246Sthorpej 		}
271fc1a5246Sthorpej 
2724fea751dSchristos 		if (maybe != 0 || once || IF_RESCAN_DELAY())
273fc1a5246Sthorpej 			return maybe;
2744fea751dSchristos 		once = 1;
275e7512e5aSchristos 
276e7512e5aSchristos 		/* If there is no known interface, maybe there is a
277e7512e5aSchristos 		 * new interface.  So just once look for new interfaces.
278e7512e5aSchristos 		 */
279e7512e5aSchristos 		ifinit();
280e7512e5aSchristos 	}
281fc1a5246Sthorpej }
282fc1a5246Sthorpej 
283fc1a5246Sthorpej 
284fc1a5246Sthorpej /* Return the classical netmask for an IP address.
285fc1a5246Sthorpej  */
286e7512e5aSchristos naddr					/* host byte order */
std_mask(naddr addr)287e7512e5aSchristos std_mask(naddr addr)			/* network byte order */
288fc1a5246Sthorpej {
289cad376bdSchristos 	addr = ntohl(addr);		/* was a host, not a network */
290fc1a5246Sthorpej 
291fc1a5246Sthorpej 	if (addr == 0)			/* default route has mask 0 */
292fc1a5246Sthorpej 		return 0;
293fc1a5246Sthorpej 	if (IN_CLASSA(addr))
294fc1a5246Sthorpej 		return IN_CLASSA_NET;
295fc1a5246Sthorpej 	if (IN_CLASSB(addr))
296fc1a5246Sthorpej 		return IN_CLASSB_NET;
297fc1a5246Sthorpej 	return IN_CLASSC_NET;
298fc1a5246Sthorpej }
299fc1a5246Sthorpej 
300fc1a5246Sthorpej 
3014d3fba59Schristos /* Find the netmask that would be inferred by RIPv1 listeners
302fc1a5246Sthorpej  *	on the given interface for a given network.
303fc1a5246Sthorpej  *	If no interface is specified, look for the best fitting	interface.
304fc1a5246Sthorpej  */
305fc1a5246Sthorpej naddr
ripv1_mask_net(naddr addr,struct interface * ifp)306fc1a5246Sthorpej ripv1_mask_net(naddr addr,		/* in network byte order */
307fc1a5246Sthorpej 	       struct interface *ifp)	/* as seen on this interface */
308fc1a5246Sthorpej {
3096d8ef4dfSthorpej 	struct r1net *r1p;
310fc1a5246Sthorpej 	naddr mask = 0;
311fc1a5246Sthorpej 
312fc1a5246Sthorpej 	if (addr == 0)			/* default always has 0 mask */
313fc1a5246Sthorpej 		return mask;
314fc1a5246Sthorpej 
3156d8ef4dfSthorpej 	if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
316fc1a5246Sthorpej 		/* If the target network is that of the associated interface
317fc1a5246Sthorpej 		 * on which it arrived, then use the netmask of the interface.
318fc1a5246Sthorpej 		 */
319fc1a5246Sthorpej 		if (on_net(addr, ifp->int_net, ifp->int_std_mask))
320fc1a5246Sthorpej 			mask = ifp->int_ripv1_mask;
321fc1a5246Sthorpej 
322fc1a5246Sthorpej 	} else {
323fc1a5246Sthorpej 		/* Examine all interfaces, and if it the target seems
324fc1a5246Sthorpej 		 * to have the same network number of an interface, use the
325fc1a5246Sthorpej 		 * netmask of that interface.  If there is more than one
326fc1a5246Sthorpej 		 * such interface, prefer the interface with the longest
327fc1a5246Sthorpej 		 * match.
328fc1a5246Sthorpej 		 */
329fc1a5246Sthorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
330fc1a5246Sthorpej 			if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
3316d8ef4dfSthorpej 			    && ifp->int_ripv1_mask > mask
3326d8ef4dfSthorpej 			    && ifp->int_ripv1_mask != HOST_MASK)
333fc1a5246Sthorpej 				mask = ifp->int_ripv1_mask;
334fc1a5246Sthorpej 		}
3356d8ef4dfSthorpej 
3366d8ef4dfSthorpej 	}
3376d8ef4dfSthorpej 
3386d8ef4dfSthorpej 	/* check special definitions */
3396d8ef4dfSthorpej 	if (mask == 0) {
3406d8ef4dfSthorpej 		for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
3416d8ef4dfSthorpej 			if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
3426d8ef4dfSthorpej 			    && r1p->r1net_mask > mask)
34394b2d428Schristos 				mask = r1p->r1net_mask;
344fc1a5246Sthorpej 		}
345fc1a5246Sthorpej 
346fc1a5246Sthorpej 		/* Otherwise, make the classic A/B/C guess.
347fc1a5246Sthorpej 		 */
348fc1a5246Sthorpej 		if (mask == 0)
349fc1a5246Sthorpej 			mask = std_mask(addr);
3506d8ef4dfSthorpej 	}
351fc1a5246Sthorpej 
352fc1a5246Sthorpej 	return mask;
353fc1a5246Sthorpej }
354fc1a5246Sthorpej 
355fc1a5246Sthorpej 
356fc1a5246Sthorpej naddr
ripv1_mask_host(naddr addr,struct interface * ifp)357fc1a5246Sthorpej ripv1_mask_host(naddr addr,		/* in network byte order */
358fc1a5246Sthorpej 		struct interface *ifp)	/* as seen on this interface */
359fc1a5246Sthorpej {
360fc1a5246Sthorpej 	naddr mask = ripv1_mask_net(addr, ifp);
361fc1a5246Sthorpej 
362fc1a5246Sthorpej 
363fc1a5246Sthorpej 	/* If the computed netmask does not mask the address,
364fc1a5246Sthorpej 	 * then assume it is a host address
365fc1a5246Sthorpej 	 */
366fc1a5246Sthorpej 	if ((ntohl(addr) & ~mask) != 0)
367fc1a5246Sthorpej 		mask = HOST_MASK;
368fc1a5246Sthorpej 	return mask;
369fc1a5246Sthorpej }
370fc1a5246Sthorpej 
371fc1a5246Sthorpej 
372fc1a5246Sthorpej /* See if a IP address looks reasonable as a destination
373fc1a5246Sthorpej  */
374fc1a5246Sthorpej int					/* 0=bad */
check_dst(naddr addr)375fc1a5246Sthorpej check_dst(naddr addr)
376fc1a5246Sthorpej {
377cad376bdSchristos 	addr = ntohl(addr);
378fc1a5246Sthorpej 
379fc1a5246Sthorpej 	if (IN_CLASSA(addr)) {
380fc1a5246Sthorpej 		if (addr == 0)
381fc1a5246Sthorpej 			return 1;	/* default */
382fc1a5246Sthorpej 
383fc1a5246Sthorpej 		addr >>= IN_CLASSA_NSHIFT;
384fc1a5246Sthorpej 		return (addr != 0 && addr != IN_LOOPBACKNET);
385fc1a5246Sthorpej 	}
386fc1a5246Sthorpej 
387fc1a5246Sthorpej 	return (IN_CLASSB(addr) || IN_CLASSC(addr));
388fc1a5246Sthorpej }
389fc1a5246Sthorpej 
390fc1a5246Sthorpej 
391e7512e5aSchristos /* See a new interface duplicates an existing interface.
392e7512e5aSchristos  */
393e7512e5aSchristos struct interface *
check_dup(naddr addr,naddr dstaddr,naddr mask,int if_flags)394e7512e5aSchristos check_dup(naddr addr,			/* IP address, so network byte order */
395e7512e5aSchristos 	  naddr dstaddr,		/* ditto */
396e7512e5aSchristos 	  naddr mask,			/* mask, so host byte order */
397e7512e5aSchristos 	  int if_flags)
398e7512e5aSchristos {
399e7512e5aSchristos 	struct interface *ifp;
400e7512e5aSchristos 
401e7512e5aSchristos 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
402e7512e5aSchristos 		if (ifp->int_mask != mask)
403e7512e5aSchristos 			continue;
404e7512e5aSchristos 
4056d8ef4dfSthorpej 		if (!iff_up(ifp->int_if_flags))
406e7512e5aSchristos 			continue;
407e7512e5aSchristos 
408e7512e5aSchristos 		/* The local address can only be shared with a point-to-point
409e7512e5aSchristos 		 * link.
410e7512e5aSchristos 		 */
4114fea751dSchristos 		if ((!(ifp->int_state & IS_REMOTE) || !(if_flags & IS_REMOTE))
4124fea751dSchristos 		    && ifp->int_addr == addr
413e7512e5aSchristos 		    && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
414e7512e5aSchristos 			return ifp;
415e7512e5aSchristos 
416e7512e5aSchristos 		if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
417e7512e5aSchristos 			return ifp;
418e7512e5aSchristos 	}
419e7512e5aSchristos 	return 0;
420e7512e5aSchristos }
421e7512e5aSchristos 
422e7512e5aSchristos 
423e7512e5aSchristos /* See that a remote gateway is reachable.
424e7512e5aSchristos  *	Note that the answer can change as real interfaces come and go.
425e7512e5aSchristos  */
426e7512e5aSchristos int					/* 0=bad */
check_remote(struct interface * ifp)427e7512e5aSchristos check_remote(struct interface *ifp)
428e7512e5aSchristos {
429e7512e5aSchristos 	struct rt_entry *rt;
430e7512e5aSchristos 
431e7512e5aSchristos 	/* do not worry about other kinds */
432e7512e5aSchristos 	if (!(ifp->int_state & IS_REMOTE))
433e7512e5aSchristos 	    return 1;
434e7512e5aSchristos 
435e7512e5aSchristos 	rt = rtfind(ifp->int_addr);
436e7512e5aSchristos 	if (rt != 0
437e7512e5aSchristos 	    && rt->rt_ifp != 0
438e7512e5aSchristos 	    &&on_net(ifp->int_addr,
439e7512e5aSchristos 		     rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
440e7512e5aSchristos 		return 1;
441e7512e5aSchristos 
442e7512e5aSchristos 	/* the gateway cannot be reached directly from one of our
443e7512e5aSchristos 	 * interfaces
444e7512e5aSchristos 	 */
445e7512e5aSchristos 	if (!(ifp->int_state & IS_BROKE)) {
446e7512e5aSchristos 		msglog("unreachable gateway %s in "_PATH_GATEWAYS,
447e7512e5aSchristos 		       naddr_ntoa(ifp->int_addr));
448e7512e5aSchristos 		if_bad(ifp);
449e7512e5aSchristos 	}
450e7512e5aSchristos 	return 0;
451e7512e5aSchristos }
452e7512e5aSchristos 
453e7512e5aSchristos 
454fc1a5246Sthorpej /* Delete an interface.
455fc1a5246Sthorpej  */
456fc1a5246Sthorpej static void
ifdel(struct interface * ifp)457fc1a5246Sthorpej ifdel(struct interface *ifp)
458fc1a5246Sthorpej {
459fc1a5246Sthorpej 	struct ip_mreq m;
460fc1a5246Sthorpej 	struct interface *ifp1;
461fc1a5246Sthorpej 
462fc1a5246Sthorpej 
463fc1a5246Sthorpej 	trace_if("Del", ifp);
464fc1a5246Sthorpej 
465fc1a5246Sthorpej 	ifp->int_state |= IS_BROKE;
466fc1a5246Sthorpej 
467fc1a5246Sthorpej 	/* unlink the interface
468fc1a5246Sthorpej 	 */
469e7512e5aSchristos 	*ifp->int_prev = ifp->int_next;
470fc1a5246Sthorpej 	if (ifp->int_next != 0)
471fc1a5246Sthorpej 		ifp->int_next->int_prev = ifp->int_prev;
472e7512e5aSchristos 	*ifp->int_ahash_prev = ifp->int_ahash;
473e7512e5aSchristos 	if (ifp->int_ahash != 0)
474e7512e5aSchristos 		ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
475e7512e5aSchristos 	*ifp->int_nhash_prev = ifp->int_nhash;
476e7512e5aSchristos 	if (ifp->int_nhash != 0)
477e7512e5aSchristos 		ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
478e7512e5aSchristos 	if (ifp->int_if_flags & IFF_BROADCAST) {
479e7512e5aSchristos 		*ifp->int_bhash_prev = ifp->int_bhash;
480e7512e5aSchristos 		if (ifp->int_bhash != 0)
481e7512e5aSchristos 			ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
482e7512e5aSchristos 	}
483e7512e5aSchristos 	if (ifp->int_state & IS_REMOTE) {
484e7512e5aSchristos 		*ifp->int_rlink_prev = ifp->int_rlink;
485e7512e5aSchristos 		if (ifp->int_rlink != 0)
486e7512e5aSchristos 			ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
487e7512e5aSchristos 	}
488fc1a5246Sthorpej 
489fc1a5246Sthorpej 	if (!(ifp->int_state & IS_ALIAS)) {
490e7512e5aSchristos 		/* delete aliases when the main interface dies
491fc1a5246Sthorpej 		 */
492fc1a5246Sthorpej 		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
493fc1a5246Sthorpej 			if (ifp1 != ifp
494fc1a5246Sthorpej 			    && !strcmp(ifp->int_name, ifp1->int_name))
495fc1a5246Sthorpej 				ifdel(ifp1);
496fc1a5246Sthorpej 		}
497fc1a5246Sthorpej 
498fc1a5246Sthorpej 		if ((ifp->int_if_flags & IFF_MULTICAST)
499fc1a5246Sthorpej #ifdef MCAST_PPP_BUG
500fc1a5246Sthorpej 		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
501fc1a5246Sthorpej #endif
502fc1a5246Sthorpej 		    && rip_sock >= 0) {
503fc1a5246Sthorpej 			m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
5047846de58Sitojun #ifdef MCAST_IFINDEX
5057846de58Sitojun 			m.imr_interface.s_addr = htonl(ifp->int_index);
5067846de58Sitojun #else
507fc1a5246Sthorpej 			m.imr_interface.s_addr = ((ifp->int_if_flags
508fc1a5246Sthorpej 						   & IFF_POINTOPOINT)
509fc1a5246Sthorpej 						  ? ifp->int_dstaddr
510fc1a5246Sthorpej 						  : ifp->int_addr);
5117846de58Sitojun #endif
512fc1a5246Sthorpej 			if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP,
513fc1a5246Sthorpej 				       &m, sizeof(m)) < 0
514fc1a5246Sthorpej 			    && errno != EADDRNOTAVAIL
515fc1a5246Sthorpej 			    && !TRACEACTIONS)
516fc1a5246Sthorpej 				LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
517e7512e5aSchristos 			if (rip_sock_mcast == ifp)
518e7512e5aSchristos 				rip_sock_mcast = 0;
519fc1a5246Sthorpej 		}
520fc1a5246Sthorpej 		if (ifp->int_rip_sock >= 0) {
521fc1a5246Sthorpej 			(void)close(ifp->int_rip_sock);
522fc1a5246Sthorpej 			ifp->int_rip_sock = -1;
523fc1a5246Sthorpej 			fix_select();
524fc1a5246Sthorpej 		}
525fc1a5246Sthorpej 
526fc1a5246Sthorpej 		tot_interfaces--;
527fc1a5246Sthorpej 		if (!IS_RIP_OFF(ifp->int_state))
528fc1a5246Sthorpej 			rip_interfaces--;
529fc1a5246Sthorpej 
530fc1a5246Sthorpej 		/* Zap all routes associated with this interface.
5316d8ef4dfSthorpej 		 * Assume routes just using gateways beyond this interface
5326d8ef4dfSthorpej 		 * will timeout naturally, and have probably already died.
533fc1a5246Sthorpej 		 */
534fc1a5246Sthorpej 		(void)rn_walktree(rhead, walk_bad, 0);
535fc1a5246Sthorpej 
536fc1a5246Sthorpej 		set_rdisc_mg(ifp, 0);
537fc1a5246Sthorpej 		if_bad_rdisc(ifp);
538fc1a5246Sthorpej 	}
539fc1a5246Sthorpej 
540fc1a5246Sthorpej 	free(ifp);
541fc1a5246Sthorpej }
542fc1a5246Sthorpej 
543fc1a5246Sthorpej 
544fc1a5246Sthorpej /* Mark an interface ill.
545fc1a5246Sthorpej  */
546fc1a5246Sthorpej void
if_sick(struct interface * ifp)547fc1a5246Sthorpej if_sick(struct interface *ifp)
548fc1a5246Sthorpej {
549fc1a5246Sthorpej 	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
550fc1a5246Sthorpej 		ifp->int_state |= IS_SICK;
551e7512e5aSchristos 		ifp->int_act_time = NEVER;
552fc1a5246Sthorpej 		trace_if("Chg", ifp);
553fc1a5246Sthorpej 
554fc1a5246Sthorpej 		LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
555fc1a5246Sthorpej 	}
556fc1a5246Sthorpej }
557fc1a5246Sthorpej 
558fc1a5246Sthorpej 
559fc1a5246Sthorpej /* Mark an interface dead.
560fc1a5246Sthorpej  */
561fc1a5246Sthorpej void
if_bad(struct interface * ifp)562fc1a5246Sthorpej if_bad(struct interface *ifp)
563fc1a5246Sthorpej {
564fc1a5246Sthorpej 	struct interface *ifp1;
565fc1a5246Sthorpej 
566fc1a5246Sthorpej 
567fc1a5246Sthorpej 	if (ifp->int_state & IS_BROKE)
568fc1a5246Sthorpej 		return;
569fc1a5246Sthorpej 
570fc1a5246Sthorpej 	LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
571fc1a5246Sthorpej 
572fc1a5246Sthorpej 	ifp->int_state |= (IS_BROKE | IS_SICK);
573e7512e5aSchristos 	ifp->int_act_time = NEVER;
574e7512e5aSchristos 	ifp->int_query_time = NEVER;
5756d8ef4dfSthorpej 	ifp->int_data.ts = now.tv_sec;
576fc1a5246Sthorpej 
577fc1a5246Sthorpej 	trace_if("Chg", ifp);
578fc1a5246Sthorpej 
579fc1a5246Sthorpej 	if (!(ifp->int_state & IS_ALIAS)) {
580fc1a5246Sthorpej 		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
581fc1a5246Sthorpej 			if (ifp1 != ifp
582fc1a5246Sthorpej 			    && !strcmp(ifp->int_name, ifp1->int_name))
583fc1a5246Sthorpej 				if_bad(ifp1);
584fc1a5246Sthorpej 		}
585fc1a5246Sthorpej 		(void)rn_walktree(rhead, walk_bad, 0);
586fc1a5246Sthorpej 		if_bad_rdisc(ifp);
587fc1a5246Sthorpej 	}
588fc1a5246Sthorpej }
589fc1a5246Sthorpej 
590fc1a5246Sthorpej 
591fc1a5246Sthorpej /* Mark an interface alive
592fc1a5246Sthorpej  */
593fc1a5246Sthorpej int					/* 1=it was dead */
if_ok(struct interface * ifp,const char * type)594fc1a5246Sthorpej if_ok(struct interface *ifp,
595756b1291Schristos       const char *type)
596fc1a5246Sthorpej {
597fc1a5246Sthorpej 	struct interface *ifp1;
598fc1a5246Sthorpej 
599fc1a5246Sthorpej 
600fc1a5246Sthorpej 	if (!(ifp->int_state & IS_BROKE)) {
601fc1a5246Sthorpej 		if (ifp->int_state & IS_SICK) {
602e7512e5aSchristos 			trace_act("%sinterface %s to %s working better",
603fc1a5246Sthorpej 				  type,
604e7512e5aSchristos 				  ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
605fc1a5246Sthorpej 			ifp->int_state &= ~IS_SICK;
606fc1a5246Sthorpej 		}
607fc1a5246Sthorpej 		return 0;
608fc1a5246Sthorpej 	}
609fc1a5246Sthorpej 
610fc1a5246Sthorpej 	msglog("%sinterface %s to %s restored",
611e7512e5aSchristos 	       type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
612fc1a5246Sthorpej 	ifp->int_state &= ~(IS_BROKE | IS_SICK);
613fc1a5246Sthorpej 	ifp->int_data.ts = 0;
614fc1a5246Sthorpej 
615fc1a5246Sthorpej 	if (!(ifp->int_state & IS_ALIAS)) {
616fc1a5246Sthorpej 		for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
617fc1a5246Sthorpej 			if (ifp1 != ifp
618fc1a5246Sthorpej 			    && !strcmp(ifp->int_name, ifp1->int_name))
619fc1a5246Sthorpej 				if_ok(ifp1, type);
620fc1a5246Sthorpej 		}
621fc1a5246Sthorpej 		if_ok_rdisc(ifp);
622fc1a5246Sthorpej 	}
623e7512e5aSchristos 
624e7512e5aSchristos 	if (ifp->int_state & IS_REMOTE) {
625e7512e5aSchristos 		if (!addrouteforif(ifp))
626e7512e5aSchristos 			return 0;
627e7512e5aSchristos 	}
628fc1a5246Sthorpej 	return 1;
629fc1a5246Sthorpej }
630fc1a5246Sthorpej 
631fc1a5246Sthorpej 
632fc1a5246Sthorpej /* disassemble routing message
633fc1a5246Sthorpej  */
634fc1a5246Sthorpej void
rt_xaddrs(struct rt_addrinfo * info,struct sockaddr * sa,struct sockaddr * lim,int addrs)635fc1a5246Sthorpej rt_xaddrs(struct rt_addrinfo *info,
636fc1a5246Sthorpej 	  struct sockaddr *sa,
637fc1a5246Sthorpej 	  struct sockaddr *lim,
638fc1a5246Sthorpej 	  int addrs)
639fc1a5246Sthorpej {
640fc1a5246Sthorpej 	int i;
641fc1a5246Sthorpej #ifdef _HAVE_SA_LEN
642fc1a5246Sthorpej 	static struct sockaddr sa_zero;
643fc1a5246Sthorpej #endif
644b01c74c5Skardel #if defined(__NetBSD__) && defined(RT_ROUNDUP)
645b01c74c5Skardel #define ROUNDUP(a) RT_ROUNDUP(a)
646b01c74c5Skardel #else
647fc1a5246Sthorpej #ifdef sgi
648fc1a5246Sthorpej #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
649fc1a5246Sthorpej 		    : sizeof(__uint64_t))
650fc1a5246Sthorpej #else
651fc1a5246Sthorpej #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
652fc1a5246Sthorpej 		    : sizeof(long))
653fc1a5246Sthorpej #endif
654b01c74c5Skardel #endif /* defined(__NetBSD__) && defined(RT_ROUNDUP) */
655fc1a5246Sthorpej 
656fc1a5246Sthorpej 
6573f50343aSlukem 	memset(info, 0, sizeof(*info));
658fc1a5246Sthorpej 	info->rti_addrs = addrs;
659fc1a5246Sthorpej 	for (i = 0; i < RTAX_MAX && sa < lim; i++) {
660fc1a5246Sthorpej 		if ((addrs & (1 << i)) == 0)
661fc1a5246Sthorpej 			continue;
662fc1a5246Sthorpej #ifdef _HAVE_SA_LEN
663fc1a5246Sthorpej 		info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
664fc1a5246Sthorpej 		sa = (struct sockaddr *)((char*)(sa)
665fc1a5246Sthorpej 					 + ROUNDUP(sa->sa_len));
666fc1a5246Sthorpej #else
667fc1a5246Sthorpej 		info->rti_info[i] = sa;
668fc1a5246Sthorpej 		sa = (struct sockaddr *)((char*)(sa)
669fc1a5246Sthorpej 					 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
670fc1a5246Sthorpej #endif
671fc1a5246Sthorpej 	}
672fc1a5246Sthorpej }
673fc1a5246Sthorpej 
674fc1a5246Sthorpej 
675fc1a5246Sthorpej /* Find the network interfaces which have configured themselves.
676fc1a5246Sthorpej  *	This must be done regularly, if only for extra addresses
677fc1a5246Sthorpej  *	that come and go on interfaces.
678fc1a5246Sthorpej  */
679fc1a5246Sthorpej void
ifinit(void)680fc1a5246Sthorpej ifinit(void)
681fc1a5246Sthorpej {
682fc1a5246Sthorpej 	static char *sysctl_buf;
683fc1a5246Sthorpej 	static size_t sysctl_buf_size = 0;
684fc1a5246Sthorpej 	uint complaints = 0;
685fc1a5246Sthorpej 	static u_int prev_complaints = 0;
6864d3fba59Schristos #	define COMP_NOT_INET	0x001
687e7512e5aSchristos #	define COMP_NOADDR	0x002
688e7512e5aSchristos #	define COMP_BADADDR	0x004
689e7512e5aSchristos #	define COMP_NODST	0x008
690e7512e5aSchristos #	define COMP_NOBADR	0x010
691e7512e5aSchristos #	define COMP_NOMASK	0x020
692e7512e5aSchristos #	define COMP_DUP		0x040
693e7512e5aSchristos #	define COMP_BAD_METRIC	0x080
694e7512e5aSchristos #	define COMP_NETMASK	0x100
695fc1a5246Sthorpej 
696fc1a5246Sthorpej 	struct interface ifs, ifs0, *ifp, *ifp1;
697fc1a5246Sthorpej 	struct rt_entry *rt;
698fc1a5246Sthorpej 	size_t needed;
699fc1a5246Sthorpej 	int mib[6];
700d524fd17Smartin 	struct if_msghdr ifm;
701fc1a5246Sthorpej 	struct ifa_msghdr *ifam, *ifam_lim, *ifam2;
702fc1a5246Sthorpej 	int in, ierr, out, oerr;
703fc1a5246Sthorpej 	struct intnet *intnetp;
704fc1a5246Sthorpej 	struct rt_addrinfo info;
705fc1a5246Sthorpej #ifdef SIOCGIFMETRIC
706fc1a5246Sthorpej 	struct ifreq ifr;
707fc1a5246Sthorpej #endif
708fc1a5246Sthorpej 
709fc1a5246Sthorpej 
710e7512e5aSchristos 	last_ifinit = now;
711fc1a5246Sthorpej 	ifinit_timer.tv_sec = now.tv_sec + (supplier
712fc1a5246Sthorpej 					    ? CHECK_ACT_INTERVAL
713fc1a5246Sthorpej 					    : CHECK_QUIET_INTERVAL);
714fc1a5246Sthorpej 
71594b2d428Schristos 	/* mark all interfaces so we can get rid of those that disappear */
716fc1a5246Sthorpej 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next)
717fc1a5246Sthorpej 		ifp->int_state &= ~(IS_CHECKED | IS_DUP);
718fc1a5246Sthorpej 
719fc1a5246Sthorpej 	/* Fetch the interface list, without too many system calls
720fc1a5246Sthorpej 	 * since we do it repeatedly.
721fc1a5246Sthorpej 	 */
722fc1a5246Sthorpej 	mib[0] = CTL_NET;
723fc1a5246Sthorpej 	mib[1] = PF_ROUTE;
724fc1a5246Sthorpej 	mib[2] = 0;
725fc1a5246Sthorpej 	mib[3] = AF_INET;
726fc1a5246Sthorpej 	mib[4] = NET_RT_IFLIST;
727fc1a5246Sthorpej 	mib[5] = 0;
728fc1a5246Sthorpej 	for (;;) {
729fc1a5246Sthorpej 		if ((needed = sysctl_buf_size) != 0) {
730fc1a5246Sthorpej 			if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
731fc1a5246Sthorpej 				break;
7326d8ef4dfSthorpej 			/* retry if the table grew */
733fc1a5246Sthorpej 			if (errno != ENOMEM && errno != EFAULT)
7346d8ef4dfSthorpej 				BADERR(1, "ifinit: sysctl(RT_IFLIST)");
735fc1a5246Sthorpej 			free(sysctl_buf);
736fc1a5246Sthorpej 			needed = 0;
737fc1a5246Sthorpej 		}
738fc1a5246Sthorpej 		if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
7396d8ef4dfSthorpej 			BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
7406d8ef4dfSthorpej 		sysctl_buf = rtmalloc(sysctl_buf_size = needed,
7416d8ef4dfSthorpej 				      "ifinit sysctl");
742fc1a5246Sthorpej 	}
743fc1a5246Sthorpej 
744fc1a5246Sthorpej 	ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
745fc1a5246Sthorpej 	for (ifam = (struct ifa_msghdr *)sysctl_buf;
746fc1a5246Sthorpej 	     ifam < ifam_lim;
747fc1a5246Sthorpej 	     ifam = ifam2) {
748fc1a5246Sthorpej 
749fc1a5246Sthorpej 		ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
750fc1a5246Sthorpej 
751a1d8cb95Sbouyer #ifdef RTM_OIFINFO
752f93fe60aSchristos 		if (ifam->ifam_type == RTM_OIFINFO)
753a1d8cb95Sbouyer 			continue;	/* just ignore compat message */
754a1d8cb95Sbouyer #endif
755fc1a5246Sthorpej 		if (ifam->ifam_type == RTM_IFINFO) {
75642871754Sdyoung 			const struct sockaddr_dl *sdl;
757e7512e5aSchristos 
758d524fd17Smartin 			memcpy(&ifm, ifam, sizeof ifm);
759fc1a5246Sthorpej 			/* make prototype structure for the IP aliases
760fc1a5246Sthorpej 			 */
7613f50343aSlukem 			memset(&ifs0, 0, sizeof(ifs0));
762fc1a5246Sthorpej 			ifs0.int_rip_sock = -1;
763d524fd17Smartin 			ifs0.int_index = ifm.ifm_index;
764d524fd17Smartin 			ifs0.int_if_flags = ifm.ifm_flags;
765fc1a5246Sthorpej 			ifs0.int_state = IS_CHECKED;
766e7512e5aSchristos 			ifs0.int_query_time = NEVER;
767fc1a5246Sthorpej 			ifs0.int_act_time = now.tv_sec;
768fc1a5246Sthorpej 			ifs0.int_data.ts = now.tv_sec;
769d524fd17Smartin 			ifs0.int_data.ipackets = ifm.ifm_data.ifi_ipackets;
770d524fd17Smartin 			ifs0.int_data.ierrors = ifm.ifm_data.ifi_ierrors;
771d524fd17Smartin 			ifs0.int_data.opackets = ifm.ifm_data.ifi_opackets;
772d524fd17Smartin 			ifs0.int_data.oerrors = ifm.ifm_data.ifi_oerrors;
773*79d830d6Smaya 
774d524fd17Smartin 			sdl = (const struct sockaddr_dl *)
775d524fd17Smartin 				((struct if_msghdr *)ifam + 1);
77642871754Sdyoung 			/* NUL-termination by memset, above. */
77742871754Sdyoung 			memcpy(ifs0.int_name, sdl->sdl_data,
77842871754Sdyoung 				MIN(sizeof(ifs0.int_name) - 1, sdl->sdl_nlen));
779fc1a5246Sthorpej 			continue;
780fc1a5246Sthorpej 		}
781fc1a5246Sthorpej 		if (ifam->ifam_type != RTM_NEWADDR) {
7824d3fba59Schristos 			logbad(1,"ifinit: out of sync");
783fc1a5246Sthorpej 			continue;
784fc1a5246Sthorpej 		}
785fc1a5246Sthorpej 		rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
786fc1a5246Sthorpej 			  (struct sockaddr *)ifam2,
787fc1a5246Sthorpej 			  ifam->ifam_addrs);
788fc1a5246Sthorpej 
789e7512e5aSchristos 		/* Prepare for the next address of this interface, which
790e7512e5aSchristos 		 * will be an alias.
791e7512e5aSchristos 		 * Do not output RIP or Router-Discovery packets via aliases.
792e7512e5aSchristos 		 */
79394b2d428Schristos 		memcpy(&ifs, &ifs0, sizeof(ifs));
7946d8ef4dfSthorpej 		ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
795e7512e5aSchristos 
796fc1a5246Sthorpej 		if (INFO_IFA(&info) == 0) {
7976d8ef4dfSthorpej 			if (iff_up(ifs.int_if_flags)) {
798fc1a5246Sthorpej 				if (!(prev_complaints & COMP_NOADDR))
7994d3fba59Schristos 					msglog("%s has no address",
800e7512e5aSchristos 					       ifs.int_name);
801fc1a5246Sthorpej 				complaints |= COMP_NOADDR;
802fc1a5246Sthorpej 			}
803fc1a5246Sthorpej 			continue;
804fc1a5246Sthorpej 		}
805fc1a5246Sthorpej 		if (INFO_IFA(&info)->sa_family != AF_INET) {
8066d8ef4dfSthorpej 			if (iff_up(ifs.int_if_flags)) {
807fc1a5246Sthorpej 				if (!(prev_complaints & COMP_NOT_INET))
808e7512e5aSchristos 					trace_act("%s: not AF_INET",
809e7512e5aSchristos 						  ifs.int_name);
810fc1a5246Sthorpej 				complaints |= COMP_NOT_INET;
811fc1a5246Sthorpej 			}
812fc1a5246Sthorpej 			continue;
813fc1a5246Sthorpej 		}
814fc1a5246Sthorpej 
815fc1a5246Sthorpej 		ifs.int_addr = S_ADDR(INFO_IFA(&info));
816fc1a5246Sthorpej 
8174d3fba59Schristos 		if (ntohl(ifs.int_addr)>>24 == 0
8184d3fba59Schristos 		    || ntohl(ifs.int_addr)>>24 == 0xff) {
8196d8ef4dfSthorpej 			if (iff_up(ifs.int_if_flags)) {
8204d3fba59Schristos 				if (!(prev_complaints & COMP_BADADDR))
8214d3fba59Schristos 					msglog("%s has a bad address",
822e7512e5aSchristos 					       ifs.int_name);
8234d3fba59Schristos 				complaints |= COMP_BADADDR;
8244d3fba59Schristos 			}
8254d3fba59Schristos 			continue;
8264d3fba59Schristos 		}
8274d3fba59Schristos 
828e7512e5aSchristos 		if (ifs.int_if_flags & IFF_LOOPBACK) {
829e7512e5aSchristos 			ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
830fc1a5246Sthorpej 			ifs.int_dstaddr = ifs.int_addr;
831e7512e5aSchristos 			ifs.int_mask = HOST_MASK;
832e7512e5aSchristos 			ifs.int_ripv1_mask = HOST_MASK;
833e7512e5aSchristos 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
834e7512e5aSchristos 			ifs.int_net = ntohl(ifs.int_dstaddr);
835e7512e5aSchristos 			if (!foundloopback) {
836e7512e5aSchristos 				foundloopback = 1;
837e7512e5aSchristos 				loopaddr = ifs.int_addr;
8386d8ef4dfSthorpej 				loop_rts.rts_gate = loopaddr;
8396d8ef4dfSthorpej 				loop_rts.rts_router = loopaddr;
840fc1a5246Sthorpej 			}
841fc1a5246Sthorpej 
842fc1a5246Sthorpej 		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
843fc1a5246Sthorpej 			if (INFO_BRD(&info) == 0
844fc1a5246Sthorpej 			    || INFO_BRD(&info)->sa_family != AF_INET) {
8456d8ef4dfSthorpej 				if (iff_up(ifs.int_if_flags)) {
846fc1a5246Sthorpej 					if (!(prev_complaints & COMP_NODST))
847fc1a5246Sthorpej 						msglog("%s has a bad"
848fc1a5246Sthorpej 						       " destination address",
849e7512e5aSchristos 						       ifs.int_name);
850fc1a5246Sthorpej 					complaints |= COMP_NODST;
851fc1a5246Sthorpej 				}
852fc1a5246Sthorpej 				continue;
853fc1a5246Sthorpej 			}
854fc1a5246Sthorpej 			ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
8554d3fba59Schristos 			if (ntohl(ifs.int_dstaddr)>>24 == 0
8564d3fba59Schristos 			    || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
8576d8ef4dfSthorpej 				if (iff_up(ifs.int_if_flags)) {
8584d3fba59Schristos 					if (!(prev_complaints & COMP_NODST))
8594d3fba59Schristos 						msglog("%s has a bad"
8604d3fba59Schristos 						       " destination address",
861e7512e5aSchristos 						       ifs.int_name);
8624d3fba59Schristos 					complaints |= COMP_NODST;
8634d3fba59Schristos 				}
8644d3fba59Schristos 				continue;
8654d3fba59Schristos 			}
866fc1a5246Sthorpej 			ifs.int_mask = HOST_MASK;
867fc1a5246Sthorpej 			ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
868fc1a5246Sthorpej 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
869fc1a5246Sthorpej 			ifs.int_net = ntohl(ifs.int_dstaddr);
870fc1a5246Sthorpej 
871fc1a5246Sthorpej 		}  else {
872e7512e5aSchristos 			if (INFO_MASK(&info) == 0) {
8736d8ef4dfSthorpej 				if (iff_up(ifs.int_if_flags)) {
874e7512e5aSchristos 					if (!(prev_complaints & COMP_NOMASK))
875e7512e5aSchristos 						msglog("%s has no netmask",
876e7512e5aSchristos 						       ifs.int_name);
877e7512e5aSchristos 					complaints |= COMP_NOMASK;
878e7512e5aSchristos 				}
879fc1a5246Sthorpej 				continue;
880fc1a5246Sthorpej 			}
881e7512e5aSchristos 			ifs.int_dstaddr = ifs.int_addr;
882e7512e5aSchristos 			ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
883e7512e5aSchristos 			ifs.int_ripv1_mask = ifs.int_mask;
884e7512e5aSchristos 			ifs.int_std_mask = std_mask(ifs.int_addr);
885e7512e5aSchristos 			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
886e7512e5aSchristos 			if (ifs.int_mask != ifs.int_std_mask)
887e7512e5aSchristos 				ifs.int_state |= IS_SUBNET;
888e7512e5aSchristos 
889e7512e5aSchristos 			if (ifs.int_if_flags & IFF_BROADCAST) {
890e7512e5aSchristos 				if (INFO_BRD(&info) == 0) {
8916d8ef4dfSthorpej 					if (iff_up(ifs.int_if_flags)) {
892e7512e5aSchristos 					    if (!(prev_complaints
893e7512e5aSchristos 						  & COMP_NOBADR))
894e7512e5aSchristos 						msglog("%s has"
895e7512e5aSchristos 						       "no broadcast address",
896e7512e5aSchristos 						       ifs.int_name);
897e7512e5aSchristos 					    complaints |= COMP_NOBADR;
898e7512e5aSchristos 					}
899e7512e5aSchristos 					continue;
900e7512e5aSchristos 				}
901e7512e5aSchristos 				ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
902e7512e5aSchristos 			}
903e7512e5aSchristos 		}
904fc1a5246Sthorpej 		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
905fc1a5246Sthorpej 		ifs.int_std_addr = htonl(ifs.int_std_net);
906fc1a5246Sthorpej 
907fc1a5246Sthorpej 		/* Use a minimum metric of one.  Treat the interface metric
908fc1a5246Sthorpej 		 * (default 0) as an increment to the hop count of one.
909fc1a5246Sthorpej 		 *
910fc1a5246Sthorpej 		 * The metric obtained from the routing socket dump of
911fc1a5246Sthorpej 		 * interface addresses is wrong.  It is not set by the
912fc1a5246Sthorpej 		 * SIOCSIFMETRIC ioctl.
913fc1a5246Sthorpej 		 */
914fc1a5246Sthorpej #ifdef SIOCGIFMETRIC
915e7512e5aSchristos 		strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
916fc1a5246Sthorpej 		if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
917fc1a5246Sthorpej 			DBGERR(1, "ioctl(SIOCGIFMETRIC)");
918fc1a5246Sthorpej 			ifs.int_metric = 0;
919fc1a5246Sthorpej 		} else {
920fc1a5246Sthorpej 			ifs.int_metric = ifr.ifr_metric;
921fc1a5246Sthorpej 		}
922fc1a5246Sthorpej #else
923fc1a5246Sthorpej 		ifs.int_metric = ifam->ifam_metric;
924fc1a5246Sthorpej #endif
925fc1a5246Sthorpej 		if (ifs.int_metric > HOPCNT_INFINITY) {
926fc1a5246Sthorpej 			ifs.int_metric = 0;
927fc1a5246Sthorpej 			if (!(prev_complaints & COMP_BAD_METRIC)
9286d8ef4dfSthorpej 			    && iff_up(ifs.int_if_flags)) {
929fc1a5246Sthorpej 				complaints |= COMP_BAD_METRIC;
930fc1a5246Sthorpej 				msglog("%s has a metric of %d",
931e7512e5aSchristos 				       ifs.int_name, ifs.int_metric);
932fc1a5246Sthorpej 			}
933fc1a5246Sthorpej 		}
934fc1a5246Sthorpej 
935fc1a5246Sthorpej 		/* See if this is a familiar interface.
936fc1a5246Sthorpej 		 * If so, stop worrying about it if it is the same.
937fc1a5246Sthorpej 		 * Start it over if it now is to somewhere else, as happens
938fc1a5246Sthorpej 		 * frequently with PPP and SLIP.
939fc1a5246Sthorpej 		 */
940e7512e5aSchristos 		ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
941fc1a5246Sthorpej 						? ifs.int_addr
942fc1a5246Sthorpej 						: 0));
943fc1a5246Sthorpej 		if (ifp != 0) {
944fc1a5246Sthorpej 			ifp->int_state |= IS_CHECKED;
945fc1a5246Sthorpej 
946fc1a5246Sthorpej 			if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
947fc1a5246Sthorpej 				  & (IFF_BROADCAST
948fc1a5246Sthorpej 				     | IFF_LOOPBACK
949fc1a5246Sthorpej 				     | IFF_POINTOPOINT
950fc1a5246Sthorpej 				     | IFF_MULTICAST))
951fc1a5246Sthorpej 			    || 0 != ((ifp->int_state ^ ifs.int_state)
952fc1a5246Sthorpej 				     & IS_ALIAS)
953fc1a5246Sthorpej 			    || ifp->int_addr != ifs.int_addr
954fc1a5246Sthorpej 			    || ifp->int_brdaddr != ifs.int_brdaddr
955fc1a5246Sthorpej 			    || ifp->int_dstaddr != ifs.int_dstaddr
956fc1a5246Sthorpej 			    || ifp->int_mask != ifs.int_mask
957fc1a5246Sthorpej 			    || ifp->int_metric != ifs.int_metric) {
958fc1a5246Sthorpej 				/* Forget old information about
959fc1a5246Sthorpej 				 * a changed interface.
960fc1a5246Sthorpej 				 */
961e7512e5aSchristos 				trace_act("interface %s has changed",
962fc1a5246Sthorpej 					  ifp->int_name);
963fc1a5246Sthorpej 				ifdel(ifp);
964fc1a5246Sthorpej 				ifp = 0;
965fc1a5246Sthorpej 			}
966fc1a5246Sthorpej 		}
967fc1a5246Sthorpej 
968fc1a5246Sthorpej 		if (ifp != 0) {
9694d3fba59Schristos 			/* The primary representative of an alias worries
9704d3fba59Schristos 			 * about how things are working.
9714d3fba59Schristos 			 */
972fc1a5246Sthorpej 			if (ifp->int_state & IS_ALIAS)
973fc1a5246Sthorpej 				continue;
974fc1a5246Sthorpej 
975fc1a5246Sthorpej 			/* note interfaces that have been turned off
976fc1a5246Sthorpej 			 */
9776d8ef4dfSthorpej 			if (!iff_up(ifs.int_if_flags)) {
9786d8ef4dfSthorpej 				if (iff_up(ifp->int_if_flags)) {
979fc1a5246Sthorpej 					msglog("interface %s to %s turned off",
980fc1a5246Sthorpej 					       ifp->int_name,
981e7512e5aSchristos 					       naddr_ntoa(ifp->int_dstaddr));
982fc1a5246Sthorpej 					if_bad(ifp);
9836d8ef4dfSthorpej 					ifp->int_if_flags &= ~IFF_UP;
9846d8ef4dfSthorpej 				} else if (now.tv_sec>(ifp->int_data.ts
9856d8ef4dfSthorpej 						       + CHECK_BAD_INTERVAL)) {
9866d8ef4dfSthorpej 					trace_act("interface %s has been off"
987b935e079Schristos 						  " %lld seconds; forget it",
9886d8ef4dfSthorpej 						  ifp->int_name,
989b935e079Schristos 						  (long long)now.tv_sec -
990b935e079Schristos 						  ifp->int_data.ts);
9916d8ef4dfSthorpej 					ifdel(ifp);
9922044d4f5Schristos 					ifp = 0;
993fc1a5246Sthorpej 				}
994fc1a5246Sthorpej 				continue;
995fc1a5246Sthorpej 			}
996fc1a5246Sthorpej 			/* or that were off and are now ok */
9976d8ef4dfSthorpej 			if (!iff_up(ifp->int_if_flags)) {
9986d8ef4dfSthorpej 				ifp->int_if_flags |= IFF_UP;
999fc1a5246Sthorpej 				(void)if_ok(ifp, "");
1000fc1a5246Sthorpej 			}
1001fc1a5246Sthorpej 
1002fc1a5246Sthorpej 			/* If it has been long enough,
1003fc1a5246Sthorpej 			 * see if the interface is broken.
1004fc1a5246Sthorpej 			 */
1005fc1a5246Sthorpej 			if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
1006fc1a5246Sthorpej 				continue;
1007fc1a5246Sthorpej 
1008fc1a5246Sthorpej 			in = ifs.int_data.ipackets - ifp->int_data.ipackets;
1009fc1a5246Sthorpej 			ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
1010fc1a5246Sthorpej 			out = ifs.int_data.opackets - ifp->int_data.opackets;
1011fc1a5246Sthorpej 			oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
1012*79d830d6Smaya 
1013fc1a5246Sthorpej 			/* If the interface just awoke, restart the counters.
1014fc1a5246Sthorpej 			 */
1015fc1a5246Sthorpej 			if (ifp->int_data.ts == 0) {
1016fc1a5246Sthorpej 				ifp->int_data = ifs.int_data;
1017fc1a5246Sthorpej 				continue;
1018fc1a5246Sthorpej 			}
1019fc1a5246Sthorpej 			ifp->int_data = ifs.int_data;
1020fc1a5246Sthorpej 
102194b2d428Schristos 			/* Withhold judgment when the short error
1022fc1a5246Sthorpej 			 * counters wrap or the interface is reset.
1023fc1a5246Sthorpej 			 */
1024fc1a5246Sthorpej 			if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1025fc1a5246Sthorpej 				LIM_SEC(ifinit_timer,
1026fc1a5246Sthorpej 					now.tv_sec+CHECK_BAD_INTERVAL);
1027fc1a5246Sthorpej 				continue;
1028fc1a5246Sthorpej 			}
1029fc1a5246Sthorpej 
1030fc1a5246Sthorpej 			/* Withhold judgement when there is no traffic
1031fc1a5246Sthorpej 			 */
1032fc1a5246Sthorpej 			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1033fc1a5246Sthorpej 				continue;
1034fc1a5246Sthorpej 
1035fc1a5246Sthorpej 			/* It is bad if input or output is not working.
1036fc1a5246Sthorpej 			 * Require presistent problems before marking it dead.
1037fc1a5246Sthorpej 			 */
1038fc1a5246Sthorpej 			if ((in <= ierr && ierr > 0)
1039fc1a5246Sthorpej 			    || (out <= oerr && oerr > 0)) {
1040fc1a5246Sthorpej 				if (!(ifp->int_state & IS_SICK)) {
1041fc1a5246Sthorpej 					trace_act("interface %s to %s"
1042fc1a5246Sthorpej 						  " sick: in=%d ierr=%d"
1043e7512e5aSchristos 						  " out=%d oerr=%d",
1044fc1a5246Sthorpej 						  ifp->int_name,
1045e7512e5aSchristos 						  naddr_ntoa(ifp->int_dstaddr),
1046fc1a5246Sthorpej 						  in, ierr, out, oerr);
1047fc1a5246Sthorpej 					if_sick(ifp);
1048fc1a5246Sthorpej 					continue;
1049fc1a5246Sthorpej 				}
1050fc1a5246Sthorpej 				if (!(ifp->int_state & IS_BROKE)) {
1051e7512e5aSchristos 					msglog("interface %s to %s broken:"
1052fc1a5246Sthorpej 					       " in=%d ierr=%d out=%d oerr=%d",
1053fc1a5246Sthorpej 					       ifp->int_name,
1054e7512e5aSchristos 					       naddr_ntoa(ifp->int_dstaddr),
1055fc1a5246Sthorpej 					       in, ierr, out, oerr);
1056fc1a5246Sthorpej 					if_bad(ifp);
1057fc1a5246Sthorpej 				}
1058fc1a5246Sthorpej 				continue;
1059fc1a5246Sthorpej 			}
1060fc1a5246Sthorpej 
1061fc1a5246Sthorpej 			/* otherwise, it is active and healthy
1062fc1a5246Sthorpej 			 */
1063fc1a5246Sthorpej 			ifp->int_act_time = now.tv_sec;
1064fc1a5246Sthorpej 			(void)if_ok(ifp, "");
1065fc1a5246Sthorpej 			continue;
1066fc1a5246Sthorpej 		}
1067fc1a5246Sthorpej 
1068fc1a5246Sthorpej 		/* This is a new interface.
1069fc1a5246Sthorpej 		 * If it is dead, forget it.
1070fc1a5246Sthorpej 		 */
10716d8ef4dfSthorpej 		if (!iff_up(ifs.int_if_flags))
1072fc1a5246Sthorpej 			continue;
1073fc1a5246Sthorpej 
1074e7512e5aSchristos 		/* If it duplicates an existing interface,
1075e7512e5aSchristos 		 * complain about it, mark the other one
1076fc1a5246Sthorpej 		 * duplicated, and forget this one.
1077fc1a5246Sthorpej 		 */
1078e7512e5aSchristos 		ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1079e7512e5aSchristos 				ifs.int_if_flags);
1080e7512e5aSchristos 		if (ifp != 0) {
1081e7512e5aSchristos 			/* Ignore duplicates of itself, caused by having
1082e7512e5aSchristos 			 * IP aliases on the same network.
1083e7512e5aSchristos 			 */
1084e7512e5aSchristos 			if (!strcmp(ifp->int_name, ifs.int_name))
1085fc1a5246Sthorpej 				continue;
1086fc1a5246Sthorpej 
1087e7512e5aSchristos 			if (!(prev_complaints & COMP_DUP)) {
1088e7512e5aSchristos 				complaints |= COMP_DUP;
1089e7512e5aSchristos 				msglog("%s (%s%s%s) is duplicated by"
1090e7512e5aSchristos 				       " %s (%s%s%s)",
1091e7512e5aSchristos 				       ifs.int_name,
1092e7512e5aSchristos 				       addrname(ifs.int_addr,ifs.int_mask,1),
1093e7512e5aSchristos 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
1094e7512e5aSchristos 					? "-->" : ""),
1095e7512e5aSchristos 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
1096e7512e5aSchristos 					? naddr_ntoa(ifs.int_dstaddr) : ""),
1097e7512e5aSchristos 				       ifp->int_name,
1098e7512e5aSchristos 				       addrname(ifp->int_addr,ifp->int_mask,1),
1099e7512e5aSchristos 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
1100e7512e5aSchristos 					? "-->" : ""),
1101e7512e5aSchristos 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
1102e7512e5aSchristos 					? naddr_ntoa(ifp->int_dstaddr) : ""));
1103e7512e5aSchristos 			}
1104e7512e5aSchristos 			ifp->int_state |= IS_DUP;
1105e7512e5aSchristos 			continue;
1106e7512e5aSchristos 		}
1107fc1a5246Sthorpej 
1108e7512e5aSchristos 		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST))
1109e7512e5aSchristos 		    && !(ifs.int_state & IS_PASSIVE)) {
1110e7512e5aSchristos 			trace_act("%s is neither broadcast, point-to-point,"
1111e7512e5aSchristos 				  " nor loopback",
1112e7512e5aSchristos 				  ifs.int_name);
1113e7512e5aSchristos 			if (!(ifs.int_state & IFF_MULTICAST))
1114e7512e5aSchristos 				ifs.int_state |= IS_NO_RDISC;
1115e7512e5aSchristos 		}
1116e7512e5aSchristos 
1117e7512e5aSchristos 
1118e7512e5aSchristos 		/* It is new and ok.   Add it to the list of interfaces
1119fc1a5246Sthorpej 		 */
11206d8ef4dfSthorpej 		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
112194b2d428Schristos 		memcpy(ifp, &ifs, sizeof(*ifp));
1122e7512e5aSchristos 		get_parms(ifp);
1123e7512e5aSchristos 		if_link(ifp);
1124fc1a5246Sthorpej 		trace_if("Add", ifp);
1125fc1a5246Sthorpej 
11264d3fba59Schristos 		/* Notice likely bad netmask.
11274d3fba59Schristos 		 */
11284d3fba59Schristos 		if (!(prev_complaints & COMP_NETMASK)
1129e7512e5aSchristos 		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
1130e7512e5aSchristos 		    && ifp->int_addr != RIP_DEFAULT) {
11314d3fba59Schristos 			for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
11324d3fba59Schristos 				if (ifp1->int_mask == ifp->int_mask)
11334d3fba59Schristos 					continue;
11344d3fba59Schristos 				if (ifp1->int_if_flags & IFF_POINTOPOINT)
11354d3fba59Schristos 					continue;
1136e7512e5aSchristos 				if (ifp1->int_dstaddr == RIP_DEFAULT)
1137e7512e5aSchristos 					continue;
11384fea751dSchristos 				/* ignore aliases on the right network */
11394fea751dSchristos 				if (!strcmp(ifp->int_name, ifp1->int_name))
11404fea751dSchristos 					continue;
1141e7512e5aSchristos 				if (on_net(ifp->int_dstaddr,
11424d3fba59Schristos 					   ifp1->int_net, ifp1->int_mask)
1143e7512e5aSchristos 				    || on_net(ifp1->int_dstaddr,
11444d3fba59Schristos 					      ifp->int_net, ifp->int_mask)) {
11454d3fba59Schristos 					msglog("possible netmask problem"
1146e7512e5aSchristos 					       " between %s:%s and %s:%s",
11474d3fba59Schristos 					       ifp->int_name,
11484d3fba59Schristos 					       addrname(htonl(ifp->int_net),
11494d3fba59Schristos 							ifp->int_mask, 1),
11504d3fba59Schristos 					       ifp1->int_name,
11514d3fba59Schristos 					       addrname(htonl(ifp1->int_net),
11524d3fba59Schristos 							ifp1->int_mask, 1));
11534d3fba59Schristos 					complaints |= COMP_NETMASK;
11544d3fba59Schristos 				}
11554d3fba59Schristos 			}
11564d3fba59Schristos 		}
11574d3fba59Schristos 
1158e7512e5aSchristos 		if (!(ifp->int_state & IS_ALIAS)) {
1159fc1a5246Sthorpej 			/* Count the # of directly connected networks.
1160fc1a5246Sthorpej 			 */
1161fc1a5246Sthorpej 			if (!(ifp->int_if_flags & IFF_LOOPBACK))
1162fc1a5246Sthorpej 				tot_interfaces++;
1163fc1a5246Sthorpej 			if (!IS_RIP_OFF(ifp->int_state))
1164fc1a5246Sthorpej 				rip_interfaces++;
1165fc1a5246Sthorpej 
1166e7512e5aSchristos 			/* turn on router discovery and RIP If needed */
1167fc1a5246Sthorpej 			if_ok_rdisc(ifp);
1168fc1a5246Sthorpej 			rip_on(ifp);
1169fc1a5246Sthorpej 		}
1170e7512e5aSchristos 	}
1171fc1a5246Sthorpej 
1172e7512e5aSchristos 	/* If we are multi-homed and have at least two interfaces
1173fc1a5246Sthorpej 	 * listening to RIP, then output by default.
1174fc1a5246Sthorpej 	 */
1175fc1a5246Sthorpej 	if (!supplier_set && rip_interfaces > 1)
1176fc1a5246Sthorpej 		set_supplier();
1177fc1a5246Sthorpej 
1178fc1a5246Sthorpej 	/* If we are multi-homed, optionally advertise a route to
1179fc1a5246Sthorpej 	 * our main address.
1180fc1a5246Sthorpej 	 */
11812044d4f5Schristos 	if ((advertise_mhome && ifp)
1182fc1a5246Sthorpej 	    || (tot_interfaces > 1
1183fc1a5246Sthorpej 		&& mhome
1184fc1a5246Sthorpej 		&& (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1185fc1a5246Sthorpej 		&& foundloopback)) {
1186fc1a5246Sthorpej 		advertise_mhome = 1;
1187fc1a5246Sthorpej 		rt = rtget(myaddr, HOST_MASK);
1188fc1a5246Sthorpej 		if (rt != 0) {
1189fc1a5246Sthorpej 			if (rt->rt_ifp != ifp
1190fc1a5246Sthorpej 			    || rt->rt_router != loopaddr) {
1191fc1a5246Sthorpej 				rtdelete(rt);
1192fc1a5246Sthorpej 				rt = 0;
1193fc1a5246Sthorpej 			} else {
11946d8ef4dfSthorpej 				loop_rts.rts_ifp = ifp;
11956d8ef4dfSthorpej 				loop_rts.rts_metric = 0;
11966d8ef4dfSthorpej 				loop_rts.rts_time = rt->rt_time;
1197fc1a5246Sthorpej 				rtchange(rt, rt->rt_state | RS_MHOME,
11986d8ef4dfSthorpej 					 &loop_rts, 0);
1199fc1a5246Sthorpej 			}
1200fc1a5246Sthorpej 		}
12016d8ef4dfSthorpej 		if (rt == 0) {
12026d8ef4dfSthorpej 			loop_rts.rts_ifp = ifp;
12036d8ef4dfSthorpej 			loop_rts.rts_metric = 0;
12046d8ef4dfSthorpej 			rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
12056d8ef4dfSthorpej 		}
1206fc1a5246Sthorpej 	}
1207fc1a5246Sthorpej 
1208fc1a5246Sthorpej 	for (ifp = ifnet; ifp != 0; ifp = ifp1) {
1209fc1a5246Sthorpej 		ifp1 = ifp->int_next;	/* because we may delete it */
1210fc1a5246Sthorpej 
1211fc1a5246Sthorpej 		/* Forget any interfaces that have disappeared.
1212fc1a5246Sthorpej 		 */
1213fc1a5246Sthorpej 		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1214e7512e5aSchristos 			trace_act("interface %s has disappeared",
1215fc1a5246Sthorpej 				  ifp->int_name);
1216fc1a5246Sthorpej 			ifdel(ifp);
1217fc1a5246Sthorpej 			continue;
1218fc1a5246Sthorpej 		}
1219fc1a5246Sthorpej 
1220fc1a5246Sthorpej 		if ((ifp->int_state & IS_BROKE)
1221fc1a5246Sthorpej 		    && !(ifp->int_state & IS_PASSIVE))
1222fc1a5246Sthorpej 			LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1223fc1a5246Sthorpej 
1224fc1a5246Sthorpej 		/* If we ever have a RIPv1 interface, assume we always will.
1225fc1a5246Sthorpej 		 * It might come back if it ever goes away.
1226fc1a5246Sthorpej 		 */
12274d3fba59Schristos 		if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1228fc1a5246Sthorpej 			have_ripv1_out = 1;
1229fc1a5246Sthorpej 		if (!(ifp->int_state & IS_NO_RIPV1_IN))
1230fc1a5246Sthorpej 			have_ripv1_in = 1;
1231fc1a5246Sthorpej 	}
1232fc1a5246Sthorpej 
1233fc1a5246Sthorpej 	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1234fc1a5246Sthorpej 		/* Ensure there is always a network route for interfaces,
1235fc1a5246Sthorpej 		 * after any dead interfaces have been deleted, which
1236fc1a5246Sthorpej 		 * might affect routes for point-to-point links.
1237fc1a5246Sthorpej 		 */
1238e7512e5aSchristos 		if (!addrouteforif(ifp))
1239e7512e5aSchristos 			continue;
1240fc1a5246Sthorpej 
1241fc1a5246Sthorpej 		/* Add routes to the local end of point-to-point interfaces
1242fc1a5246Sthorpej 		 * using loopback.
1243fc1a5246Sthorpej 		 */
1244fc1a5246Sthorpej 		if ((ifp->int_if_flags & IFF_POINTOPOINT)
1245fc1a5246Sthorpej 		    && !(ifp->int_state & IS_REMOTE)
1246fc1a5246Sthorpej 		    && foundloopback) {
1247fc1a5246Sthorpej 			/* Delete any routes to the network address through
1248fc1a5246Sthorpej 			 * foreign routers. Remove even static routes.
1249fc1a5246Sthorpej 			 */
12506d8ef4dfSthorpej 			del_static(ifp->int_addr, HOST_MASK, 0, 0);
1251fc1a5246Sthorpej 			rt = rtget(ifp->int_addr, HOST_MASK);
1252fc1a5246Sthorpej 			if (rt != 0 && rt->rt_router != loopaddr) {
1253fc1a5246Sthorpej 				rtdelete(rt);
1254fc1a5246Sthorpej 				rt = 0;
1255fc1a5246Sthorpej 			}
1256fc1a5246Sthorpej 			if (rt != 0) {
1257fc1a5246Sthorpej 				if (!(rt->rt_state & RS_LOCAL)
1258fc1a5246Sthorpej 				    || rt->rt_metric > ifp->int_metric) {
1259fc1a5246Sthorpej 					ifp1 = ifp;
1260fc1a5246Sthorpej 				} else {
1261fc1a5246Sthorpej 					ifp1 = rt->rt_ifp;
1262fc1a5246Sthorpej 				}
12636d8ef4dfSthorpej 				loop_rts.rts_ifp = ifp1;
12646d8ef4dfSthorpej 				loop_rts.rts_metric = 0;
12656d8ef4dfSthorpej 				loop_rts.rts_time = rt->rt_time;
1266fc1a5246Sthorpej 				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1267fc1a5246Sthorpej 					      | (RS_IF|RS_LOCAL)),
12686d8ef4dfSthorpej 					 &loop_rts, 0);
1269fc1a5246Sthorpej 			} else {
12706d8ef4dfSthorpej 				loop_rts.rts_ifp = ifp;
12716d8ef4dfSthorpej 				loop_rts.rts_metric = 0;
1272fc1a5246Sthorpej 				rtadd(ifp->int_addr, HOST_MASK,
12736d8ef4dfSthorpej 				      (RS_IF | RS_LOCAL), &loop_rts);
1274fc1a5246Sthorpej 			}
1275fc1a5246Sthorpej 		}
1276fc1a5246Sthorpej 	}
1277fc1a5246Sthorpej 
1278fc1a5246Sthorpej 	/* add the authority routes */
1279fc1a5246Sthorpej 	for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1280fc1a5246Sthorpej 		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1281fc1a5246Sthorpej 		if (rt != 0
1282fc1a5246Sthorpej 		    && !(rt->rt_state & RS_NO_NET_SYN)
1283fc1a5246Sthorpej 		    && !(rt->rt_state & RS_NET_INT)) {
1284fc1a5246Sthorpej 			rtdelete(rt);
1285fc1a5246Sthorpej 			rt = 0;
1286fc1a5246Sthorpej 		}
12876d8ef4dfSthorpej 		if (rt == 0) {
12886d8ef4dfSthorpej 			loop_rts.rts_ifp = 0;
12896d8ef4dfSthorpej 			loop_rts.rts_metric = intnetp->intnet_metric-1;
1290fc1a5246Sthorpej 			rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
12916d8ef4dfSthorpej 			      RS_NET_SYN | RS_NET_INT, &loop_rts);
12926d8ef4dfSthorpej 		}
1293fc1a5246Sthorpej 	}
1294fc1a5246Sthorpej 
1295fc1a5246Sthorpej 	prev_complaints = complaints;
1296fc1a5246Sthorpej }
1297fc1a5246Sthorpej 
1298fc1a5246Sthorpej 
1299fc1a5246Sthorpej static void
check_net_syn(struct interface * ifp)1300fc1a5246Sthorpej check_net_syn(struct interface *ifp)
1301fc1a5246Sthorpej {
1302fc1a5246Sthorpej 	struct rt_entry *rt;
13036d8ef4dfSthorpej 	static struct rt_spare new;
1304fc1a5246Sthorpej 
1305fc1a5246Sthorpej 
1306fc1a5246Sthorpej 	/* Turn on the need to automatically synthesize a network route
1307fc1a5246Sthorpej 	 * for this interface only if we are running RIPv1 on some other
1308fc1a5246Sthorpej 	 * interface that is on a different class-A,B,or C network.
1309fc1a5246Sthorpej 	 */
1310fc1a5246Sthorpej 	if (have_ripv1_out || have_ripv1_in) {
1311fc1a5246Sthorpej 		ifp->int_state |= IS_NEED_NET_SYN;
1312fc1a5246Sthorpej 		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1313fc1a5246Sthorpej 		if (rt != 0
1314fc1a5246Sthorpej 		    && 0 == (rt->rt_state & RS_NO_NET_SYN)
1315fc1a5246Sthorpej 		    && (!(rt->rt_state & RS_NET_SYN)
1316fc1a5246Sthorpej 			|| rt->rt_metric > ifp->int_metric)) {
1317fc1a5246Sthorpej 			rtdelete(rt);
1318fc1a5246Sthorpej 			rt = 0;
1319fc1a5246Sthorpej 		}
13206d8ef4dfSthorpej 		if (rt == 0) {
13216d8ef4dfSthorpej 			new.rts_ifp = ifp;
13226d8ef4dfSthorpej 			new.rts_gate = ifp->int_addr;
13236d8ef4dfSthorpej 			new.rts_router = ifp->int_addr;
13246d8ef4dfSthorpej 			new.rts_metric = ifp->int_metric;
1325fc1a5246Sthorpej 			rtadd(ifp->int_std_addr, ifp->int_std_mask,
13266d8ef4dfSthorpej 			      RS_NET_SYN, &new);
13276d8ef4dfSthorpej 		}
1328fc1a5246Sthorpej 
1329fc1a5246Sthorpej 	} else {
1330fc1a5246Sthorpej 		ifp->int_state &= ~IS_NEED_NET_SYN;
1331fc1a5246Sthorpej 
1332fc1a5246Sthorpej 		rt = rtget(ifp->int_std_addr,
1333fc1a5246Sthorpej 			   ifp->int_std_mask);
1334fc1a5246Sthorpej 		if (rt != 0
1335fc1a5246Sthorpej 		    && (rt->rt_state & RS_NET_SYN)
1336fc1a5246Sthorpej 		    && rt->rt_ifp == ifp)
1337fc1a5246Sthorpej 			rtbad_sub(rt);
1338fc1a5246Sthorpej 	}
1339fc1a5246Sthorpej }
1340fc1a5246Sthorpej 
1341fc1a5246Sthorpej 
1342fc1a5246Sthorpej /* Add route for interface if not currently installed.
1343fc1a5246Sthorpej  * Create route to other end if a point-to-point link,
1344fc1a5246Sthorpej  * otherwise a route to this (sub)network.
1345fc1a5246Sthorpej  */
1346e7512e5aSchristos int					/* 0=bad interface */
addrouteforif(struct interface * ifp)1347fc1a5246Sthorpej addrouteforif(struct interface *ifp)
1348fc1a5246Sthorpej {
1349fc1a5246Sthorpej 	struct rt_entry *rt;
13506d8ef4dfSthorpej 	static struct rt_spare new;
13516d8ef4dfSthorpej 	naddr dst;
1352fc1a5246Sthorpej 
1353fc1a5246Sthorpej 
1354fc1a5246Sthorpej 	/* skip sick interfaces
1355fc1a5246Sthorpej 	 */
1356fc1a5246Sthorpej 	if (ifp->int_state & IS_BROKE)
1357e7512e5aSchristos 		return 0;
1358fc1a5246Sthorpej 
1359fc1a5246Sthorpej 	/* If the interface on a subnet, then install a RIPv1 route to
1360fc1a5246Sthorpej 	 * the network as well (unless it is sick).
1361fc1a5246Sthorpej 	 */
1362fc1a5246Sthorpej 	if (ifp->int_state & IS_SUBNET)
1363fc1a5246Sthorpej 		check_net_syn(ifp);
1364fc1a5246Sthorpej 
1365e7512e5aSchristos 	dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1366e7512e5aSchristos 	       ? ifp->int_dstaddr
1367e7512e5aSchristos 	       : htonl(ifp->int_net));
1368e7512e5aSchristos 
13696d8ef4dfSthorpej 	new.rts_ifp = ifp;
13706d8ef4dfSthorpej 	new.rts_router = ifp->int_addr;
13716d8ef4dfSthorpej 	new.rts_gate = ifp->int_addr;
13726d8ef4dfSthorpej 	new.rts_metric = ifp->int_metric;
13736d8ef4dfSthorpej 	new.rts_time = now.tv_sec;
13746d8ef4dfSthorpej 
1375fc1a5246Sthorpej 	/* If we are going to send packets to the gateway,
1376fc1a5246Sthorpej 	 * it must be reachable using our physical interfaces
1377fc1a5246Sthorpej 	 */
1378e7512e5aSchristos 	if ((ifp->int_state & IS_REMOTE)
1379e7512e5aSchristos 	    && !(ifp->int_state & IS_EXTERNAL)
1380e7512e5aSchristos 	    && !check_remote(ifp))
1381e7512e5aSchristos 		return 0;
1382fc1a5246Sthorpej 
1383fc1a5246Sthorpej 	/* We are finished if the correct main interface route exists.
1384fc1a5246Sthorpej 	 * The right route must be for the right interface, not synthesized
1385fc1a5246Sthorpej 	 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1386fc1a5246Sthorpej 	 */
13876d8ef4dfSthorpej 	del_static(dst, ifp->int_mask, 0, 0);
1388fc1a5246Sthorpej 	rt = rtget(dst, ifp->int_mask);
1389fc1a5246Sthorpej 	if (rt != 0) {
1390fc1a5246Sthorpej 		if ((rt->rt_ifp != ifp
1391fc1a5246Sthorpej 		     || rt->rt_router != ifp->int_addr)
1392fc1a5246Sthorpej 		    && (!(ifp->int_state & IS_DUP)
1393fc1a5246Sthorpej 			|| rt->rt_ifp == 0
1394fc1a5246Sthorpej 			|| (rt->rt_ifp->int_state & IS_BROKE))) {
1395fc1a5246Sthorpej 			rtdelete(rt);
1396fc1a5246Sthorpej 			rt = 0;
1397fc1a5246Sthorpej 		} else {
1398fc1a5246Sthorpej 			rtchange(rt, ((rt->rt_state | RS_IF)
1399fc1a5246Sthorpej 				      & ~(RS_NET_SYN | RS_LOCAL)),
14006d8ef4dfSthorpej 				 &new, 0);
1401fc1a5246Sthorpej 		}
1402fc1a5246Sthorpej 	}
1403fc1a5246Sthorpej 	if (rt == 0) {
1404fc1a5246Sthorpej 		if (ifp->int_transitions++ > 0)
1405e7512e5aSchristos 			trace_act("re-install interface %s",
1406fc1a5246Sthorpej 				  ifp->int_name);
1407fc1a5246Sthorpej 
14086d8ef4dfSthorpej 		rtadd(dst, ifp->int_mask, RS_IF, &new);
1409fc1a5246Sthorpej 	}
1410e7512e5aSchristos 
1411e7512e5aSchristos 	return 1;
141261f28255Scgd }
1413