xref: /freebsd/sbin/routed/if.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
47b6ab19dSGarrett Wollman  * Copyright (c) 1983, 1993
57b6ab19dSGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
67b6ab19dSGarrett Wollman  *
77b6ab19dSGarrett Wollman  * Redistribution and use in source and binary forms, with or without
87b6ab19dSGarrett Wollman  * modification, are permitted provided that the following conditions
97b6ab19dSGarrett Wollman  * are met:
107b6ab19dSGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
117b6ab19dSGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
127b6ab19dSGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
137b6ab19dSGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
147b6ab19dSGarrett Wollman  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
167b6ab19dSGarrett Wollman  *    may be used to endorse or promote products derived from this software
177b6ab19dSGarrett Wollman  *    without specific prior written permission.
187b6ab19dSGarrett Wollman  *
197b6ab19dSGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
207b6ab19dSGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217b6ab19dSGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227b6ab19dSGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237b6ab19dSGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247b6ab19dSGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257b6ab19dSGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267b6ab19dSGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277b6ab19dSGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287b6ab19dSGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297b6ab19dSGarrett Wollman  * SUCH DAMAGE.
307b6ab19dSGarrett Wollman  */
317b6ab19dSGarrett Wollman 
32b108792dSUlrich Spörlein #include <stdint.h>
33b108792dSUlrich Spörlein 
347b6ab19dSGarrett Wollman #include "defs.h"
357b6ab19dSGarrett Wollman #include "pathnames.h"
3638ae6523SPoul-Henning Kamp struct ifhead ifnet = LIST_HEAD_INITIALIZER(ifnet);	/* all interfaces */
3713e403fdSAntoine Brodin struct ifhead remote_if = LIST_HEAD_INITIALIZER(remote_if);	/* remote interfaces */
38d5b718b3SGarrett Wollman 
39d5b718b3SGarrett Wollman /* hash table for all interfaces, big enough to tolerate ridiculous
40d5b718b3SGarrett Wollman  * numbers of IP aliases.  Crazy numbers of aliases such as 7000
41d5b718b3SGarrett Wollman  * still will not do well, but not just in looking up interfaces
42d5b718b3SGarrett Wollman  * by name or address.
43d5b718b3SGarrett Wollman  */
44d5b718b3SGarrett Wollman #define AHASH_LEN 211			/* must be prime */
45cdb877c3SGarrett Wollman #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
46f2ca6d8aSPoul-Henning Kamp static struct interface *ahash_tbl[AHASH_LEN];
47d5b718b3SGarrett Wollman 
48d5b718b3SGarrett Wollman #define BHASH_LEN 211			/* must be prime */
49cdb877c3SGarrett Wollman #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
50f2ca6d8aSPoul-Henning Kamp static struct interface *bhash_tbl[BHASH_LEN];
51d5b718b3SGarrett Wollman 
52d5b718b3SGarrett Wollman 
53d5b718b3SGarrett Wollman /* hash for physical interface names.
54d5b718b3SGarrett Wollman  * Assume there are never more 100 or 200 real interfaces, and that
55337e72d0SGarrett Wollman  * aliases are put on the end of the hash chains.
56d5b718b3SGarrett Wollman  */
57d5b718b3SGarrett Wollman #define NHASH_LEN 97
58f2ca6d8aSPoul-Henning Kamp static struct interface *nhash_tbl[NHASH_LEN];
59d5b718b3SGarrett Wollman 
607b6ab19dSGarrett Wollman int	tot_interfaces;			/* # of remote and local interfaces */
617b6ab19dSGarrett Wollman int	rip_interfaces;			/* # of interfaces doing RIP */
62f2ca6d8aSPoul-Henning Kamp static int foundloopback;			/* valid flag for loopaddr */
637b6ab19dSGarrett Wollman naddr	loopaddr;			/* our address on loopback */
64f2ca6d8aSPoul-Henning Kamp static struct rt_spare loop_rts;
657b6ab19dSGarrett Wollman 
667b6ab19dSGarrett Wollman struct timeval ifinit_timer;
67337e72d0SGarrett Wollman static struct timeval last_ifinit;
682c7a9781SMark Murray #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec		\
692c7a9781SMark Murray 			   && last_ifinit.tv_usec == now.tv_usec	\
702c7a9781SMark Murray 			   && timercmp(&ifinit_timer, &now, >))
717b6ab19dSGarrett Wollman 
727b6ab19dSGarrett Wollman int	have_ripv1_out;			/* have a RIPv1 interface */
73f2ca6d8aSPoul-Henning Kamp static int have_ripv1_in;
747b6ab19dSGarrett Wollman 
757b6ab19dSGarrett Wollman 
76f2ca6d8aSPoul-Henning Kamp static void if_bad(struct interface *);
77f2ca6d8aSPoul-Henning Kamp static int addrouteforif(struct interface *);
78f2ca6d8aSPoul-Henning Kamp 
79cdb877c3SGarrett Wollman static struct interface**
nhash(char * p)802c7a9781SMark Murray nhash(char *p)
81cdb877c3SGarrett Wollman {
822c7a9781SMark Murray 	u_int i;
83cdb877c3SGarrett Wollman 
84cdb877c3SGarrett Wollman 	for (i = 0; *p != '\0'; p++) {
85cdb877c3SGarrett Wollman 		i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
86cdb877c3SGarrett Wollman 		i ^= *p;
87cdb877c3SGarrett Wollman 	}
88cdb877c3SGarrett Wollman 	return &nhash_tbl[i % NHASH_LEN];
89cdb877c3SGarrett Wollman }
90cdb877c3SGarrett Wollman 
91cdb877c3SGarrett Wollman 
92337e72d0SGarrett Wollman /* Link a new interface into the lists and hash tables.
93337e72d0SGarrett Wollman  */
94d5b718b3SGarrett Wollman void
if_link(struct interface * ifp)95d5b718b3SGarrett Wollman if_link(struct interface *ifp)
96d5b718b3SGarrett Wollman {
97d5b718b3SGarrett Wollman 	struct interface **hifp;
98d5b718b3SGarrett Wollman 
9938ae6523SPoul-Henning Kamp 	LIST_INSERT_HEAD(&ifnet, ifp, int_list);
100d5b718b3SGarrett Wollman 
101d5b718b3SGarrett Wollman 	hifp = AHASH(ifp->int_addr);
102d5b718b3SGarrett Wollman 	ifp->int_ahash_prev = hifp;
103562c5a82SPedro F. Giffuni 	if ((ifp->int_ahash = *hifp) != NULL)
104d5b718b3SGarrett Wollman 		(*hifp)->int_ahash_prev = &ifp->int_ahash;
105d5b718b3SGarrett Wollman 	*hifp = ifp;
106d5b718b3SGarrett Wollman 
107d5b718b3SGarrett Wollman 	if (ifp->int_if_flags & IFF_BROADCAST) {
108d5b718b3SGarrett Wollman 		hifp = BHASH(ifp->int_brdaddr);
109d5b718b3SGarrett Wollman 		ifp->int_bhash_prev = hifp;
110562c5a82SPedro F. Giffuni 		if ((ifp->int_bhash = *hifp) != NULL)
111d5b718b3SGarrett Wollman 			(*hifp)->int_bhash_prev = &ifp->int_bhash;
112d5b718b3SGarrett Wollman 		*hifp = ifp;
113d5b718b3SGarrett Wollman 	}
114d5b718b3SGarrett Wollman 
1156b6b8280SPoul-Henning Kamp 	if (ifp->int_state & IS_REMOTE)
1166b6b8280SPoul-Henning Kamp 		LIST_INSERT_HEAD(&remote_if, ifp, remote_list);
117d5b718b3SGarrett Wollman 
118cdb877c3SGarrett Wollman 	hifp = nhash(ifp->int_name);
119d5b718b3SGarrett Wollman 	if (ifp->int_state & IS_ALIAS) {
120337e72d0SGarrett Wollman 		/* put aliases on the end of the hash chain */
121562c5a82SPedro F. Giffuni 		while (*hifp != NULL)
122d5b718b3SGarrett Wollman 			hifp = &(*hifp)->int_nhash;
123d5b718b3SGarrett Wollman 	}
124d5b718b3SGarrett Wollman 	ifp->int_nhash_prev = hifp;
125562c5a82SPedro F. Giffuni 	if ((ifp->int_nhash = *hifp) != NULL)
126d5b718b3SGarrett Wollman 		(*hifp)->int_nhash_prev = &ifp->int_nhash;
127d5b718b3SGarrett Wollman 	*hifp = ifp;
128d5b718b3SGarrett Wollman }
129d5b718b3SGarrett Wollman 
130d5b718b3SGarrett Wollman 
1317b6ab19dSGarrett Wollman /* Find the interface with an address
1327b6ab19dSGarrett Wollman  */
1337b6ab19dSGarrett Wollman struct interface *
ifwithaddr(naddr addr,int bcast,int remote)1347b6ab19dSGarrett Wollman ifwithaddr(naddr addr,
1357b6ab19dSGarrett Wollman 	   int	bcast,			/* notice IFF_BROADCAST address */
1367b6ab19dSGarrett Wollman 	   int	remote)			/* include IS_REMOTE interfaces */
1377b6ab19dSGarrett Wollman {
138562c5a82SPedro F. Giffuni 	struct interface *ifp, *possible = NULL;
1397b6ab19dSGarrett Wollman 
140d5b718b3SGarrett Wollman 	remote = (remote == 0) ? IS_REMOTE : 0;
141d5b718b3SGarrett Wollman 
142d5b718b3SGarrett Wollman 	for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
143d5b718b3SGarrett Wollman 		if (ifp->int_addr != addr)
1447b6ab19dSGarrett Wollman 			continue;
145d5b718b3SGarrett Wollman 		if ((ifp->int_state & remote) != 0)
146d5b718b3SGarrett Wollman 			continue;
147d5b718b3SGarrett Wollman 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
1487b6ab19dSGarrett Wollman 			return ifp;
1497b6ab19dSGarrett Wollman 		possible = ifp;
1507b6ab19dSGarrett Wollman 	}
151d5b718b3SGarrett Wollman 
152d5b718b3SGarrett Wollman 	if (possible || !bcast)
153d5b718b3SGarrett Wollman 		return possible;
154d5b718b3SGarrett Wollman 
155d5b718b3SGarrett Wollman 	for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
156d5b718b3SGarrett Wollman 		if (ifp->int_brdaddr != addr)
157d5b718b3SGarrett Wollman 			continue;
158d5b718b3SGarrett Wollman 		if ((ifp->int_state & remote) != 0)
159d5b718b3SGarrett Wollman 			continue;
160d5b718b3SGarrett Wollman 		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
161d5b718b3SGarrett Wollman 			return ifp;
162d5b718b3SGarrett Wollman 		possible = ifp;
1637b6ab19dSGarrett Wollman 	}
1647b6ab19dSGarrett Wollman 
1657b6ab19dSGarrett Wollman 	return possible;
1667b6ab19dSGarrett Wollman }
1677b6ab19dSGarrett Wollman 
1687b6ab19dSGarrett Wollman 
1697b6ab19dSGarrett Wollman /* find the interface with a name
1707b6ab19dSGarrett Wollman  */
171f2ca6d8aSPoul-Henning Kamp static struct interface *
ifwithname(char * name,naddr addr)1727b6ab19dSGarrett Wollman ifwithname(char *name,			/* "ec0" or whatever */
1737b6ab19dSGarrett Wollman 	   naddr addr)			/* 0 or network address */
1747b6ab19dSGarrett Wollman {
1757b6ab19dSGarrett Wollman 	struct interface *ifp;
1767b6ab19dSGarrett Wollman 
177337e72d0SGarrett Wollman 	for (;;) {
178562c5a82SPedro F. Giffuni 		for (ifp = *nhash(name); ifp != NULL; ifp = ifp->int_nhash) {
179d5b718b3SGarrett Wollman 			/* If the network address is not specified,
180d5b718b3SGarrett Wollman 			 * ignore any alias interfaces.  Otherwise, look
181d5b718b3SGarrett Wollman 			 * for the interface with the target name and address.
182d5b718b3SGarrett Wollman 			 */
1837b6ab19dSGarrett Wollman 			if (!strcmp(ifp->int_name, name)
184d5b718b3SGarrett Wollman 			    && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
185d5b718b3SGarrett Wollman 				|| (ifp->int_addr == addr)))
1867b6ab19dSGarrett Wollman 				return ifp;
1877b6ab19dSGarrett Wollman 		}
188337e72d0SGarrett Wollman 
189337e72d0SGarrett Wollman 		/* If there is no known interface, maybe there is a
190337e72d0SGarrett Wollman 		 * new interface.  So just once look for new interfaces.
191337e72d0SGarrett Wollman 		 */
1922c7a9781SMark Murray 		if (IF_RESCAN_DELAY())
1937b6ab19dSGarrett Wollman 			return 0;
194337e72d0SGarrett Wollman 		ifinit();
195337e72d0SGarrett Wollman 	}
1967b6ab19dSGarrett Wollman }
1977b6ab19dSGarrett Wollman 
1987b6ab19dSGarrett Wollman 
1997b6ab19dSGarrett Wollman struct interface *
ifwithindex(u_short ifindex,int rescan_ok)200caa79e36SBruce M Simpson ifwithindex(u_short ifindex,
2012c7a9781SMark Murray 	    int rescan_ok)
2027b6ab19dSGarrett Wollman {
2037b6ab19dSGarrett Wollman 	struct interface *ifp;
2047b6ab19dSGarrett Wollman 
2052c7a9781SMark Murray 	for (;;) {
20638ae6523SPoul-Henning Kamp 		LIST_FOREACH(ifp, &ifnet, int_list) {
207caa79e36SBruce M Simpson 			if (ifp->int_index == ifindex)
2087b6ab19dSGarrett Wollman 				return ifp;
2097b6ab19dSGarrett Wollman 		}
2102c7a9781SMark Murray 
2112c7a9781SMark Murray 		/* If there is no known interface, maybe there is a
2122c7a9781SMark Murray 		 * new interface.  So just once look for new interfaces.
2132c7a9781SMark Murray 		 */
2142c7a9781SMark Murray 		if (!rescan_ok
2152c7a9781SMark Murray 		    || IF_RESCAN_DELAY())
2167b6ab19dSGarrett Wollman 			return 0;
2172c7a9781SMark Murray 		ifinit();
2182c7a9781SMark Murray 	}
2197b6ab19dSGarrett Wollman }
2207b6ab19dSGarrett Wollman 
2217b6ab19dSGarrett Wollman 
2227b6ab19dSGarrett Wollman /* Find an interface from which the specified address
2237b6ab19dSGarrett Wollman  * should have come from.  Used for figuring out which
22471965874SGarrett Wollman  * interface a packet came in on.
2257b6ab19dSGarrett Wollman  */
2267b6ab19dSGarrett Wollman struct interface *
iflookup(naddr addr)2277b6ab19dSGarrett Wollman iflookup(naddr addr)
2287b6ab19dSGarrett Wollman {
2297b6ab19dSGarrett Wollman 	struct interface *ifp, *maybe;
230caa79e36SBruce M Simpson 	int once = 0;
2317b6ab19dSGarrett Wollman 
232562c5a82SPedro F. Giffuni 	maybe = NULL;
23371965874SGarrett Wollman 	for (;;) {
23438ae6523SPoul-Henning Kamp 		LIST_FOREACH(ifp, &ifnet, int_list) {
2357b6ab19dSGarrett Wollman 			if (ifp->int_if_flags & IFF_POINTOPOINT) {
2367b6ab19dSGarrett Wollman 				/* finished with a match */
237d5b718b3SGarrett Wollman 				if (ifp->int_dstaddr == addr)
2387b6ab19dSGarrett Wollman 					return ifp;
2397b6ab19dSGarrett Wollman 
2407b6ab19dSGarrett Wollman 			} else {
2417b6ab19dSGarrett Wollman 				/* finished with an exact match */
2427b6ab19dSGarrett Wollman 				if (ifp->int_addr == addr)
2437b6ab19dSGarrett Wollman 					return ifp;
2447b6ab19dSGarrett Wollman 
2457b6ab19dSGarrett Wollman 				/* Look for the longest approximate match.
2467b6ab19dSGarrett Wollman 				 */
2477b6ab19dSGarrett Wollman 				if (on_net(addr, ifp->int_net, ifp->int_mask)
248562c5a82SPedro F. Giffuni 				    && (maybe == NULL
2497b6ab19dSGarrett Wollman 					|| ifp->int_mask > maybe->int_mask))
2507b6ab19dSGarrett Wollman 					maybe = ifp;
2517b6ab19dSGarrett Wollman 			}
2527b6ab19dSGarrett Wollman 		}
2537b6ab19dSGarrett Wollman 
254562c5a82SPedro F. Giffuni 		if (maybe != NULL || once || IF_RESCAN_DELAY())
2557b6ab19dSGarrett Wollman 			return maybe;
256caa79e36SBruce M Simpson 		once = 1;
25771965874SGarrett Wollman 
25871965874SGarrett Wollman 		/* If there is no known interface, maybe there is a
25971965874SGarrett Wollman 		 * new interface.  So just once look for new interfaces.
26071965874SGarrett Wollman 		 */
26171965874SGarrett Wollman 		ifinit();
26271965874SGarrett Wollman 	}
2637b6ab19dSGarrett Wollman }
2647b6ab19dSGarrett Wollman 
2657b6ab19dSGarrett Wollman 
2667b6ab19dSGarrett Wollman /* Return the classical netmask for an IP address.
2677b6ab19dSGarrett Wollman  */
26871965874SGarrett Wollman naddr					/* host byte order */
std_mask(naddr addr)26971965874SGarrett Wollman std_mask(naddr addr)			/* network byte order */
2707b6ab19dSGarrett Wollman {
271fd8e4ebcSMike Barcroft 	addr = ntohl(addr);			/* was a host, not a network */
2727b6ab19dSGarrett Wollman 
2737b6ab19dSGarrett Wollman 	if (addr == 0)			/* default route has mask 0 */
2747b6ab19dSGarrett Wollman 		return 0;
2757b6ab19dSGarrett Wollman 	if (IN_CLASSA(addr))
2767b6ab19dSGarrett Wollman 		return IN_CLASSA_NET;
2777b6ab19dSGarrett Wollman 	if (IN_CLASSB(addr))
2787b6ab19dSGarrett Wollman 		return IN_CLASSB_NET;
2797b6ab19dSGarrett Wollman 	return IN_CLASSC_NET;
2807b6ab19dSGarrett Wollman }
2817b6ab19dSGarrett Wollman 
2827b6ab19dSGarrett Wollman 
2837b6ab19dSGarrett Wollman /* Find the netmask that would be inferred by RIPv1 listeners
2847b6ab19dSGarrett Wollman  *	on the given interface for a given network.
2857b6ab19dSGarrett Wollman  *	If no interface is specified, look for the best fitting	interface.
2867b6ab19dSGarrett Wollman  */
2877b6ab19dSGarrett Wollman naddr
ripv1_mask_net(naddr addr,struct interface * ifp)2887b6ab19dSGarrett Wollman ripv1_mask_net(naddr addr,		/* in network byte order */
2897b6ab19dSGarrett Wollman 	       struct interface *ifp)	/* as seen on this interface */
2907b6ab19dSGarrett Wollman {
2912c7a9781SMark Murray 	struct r1net *r1p;
2927b6ab19dSGarrett Wollman 	naddr mask = 0;
2937b6ab19dSGarrett Wollman 
2947b6ab19dSGarrett Wollman 	if (addr == 0)			/* default always has 0 mask */
2957b6ab19dSGarrett Wollman 		return mask;
2967b6ab19dSGarrett Wollman 
297562c5a82SPedro F. Giffuni 	if (ifp != NULL && ifp->int_ripv1_mask != HOST_MASK) {
2987b6ab19dSGarrett Wollman 		/* If the target network is that of the associated interface
2997b6ab19dSGarrett Wollman 		 * on which it arrived, then use the netmask of the interface.
3007b6ab19dSGarrett Wollman 		 */
3017b6ab19dSGarrett Wollman 		if (on_net(addr, ifp->int_net, ifp->int_std_mask))
3027b6ab19dSGarrett Wollman 			mask = ifp->int_ripv1_mask;
3037b6ab19dSGarrett Wollman 
3047b6ab19dSGarrett Wollman 	} else {
3057b6ab19dSGarrett Wollman 		/* Examine all interfaces, and if it the target seems
3067b6ab19dSGarrett Wollman 		 * to have the same network number of an interface, use the
3077b6ab19dSGarrett Wollman 		 * netmask of that interface.  If there is more than one
3087b6ab19dSGarrett Wollman 		 * such interface, prefer the interface with the longest
3097b6ab19dSGarrett Wollman 		 * match.
3107b6ab19dSGarrett Wollman 		 */
31138ae6523SPoul-Henning Kamp 		LIST_FOREACH(ifp, &ifnet, int_list) {
3127b6ab19dSGarrett Wollman 			if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
3132c7a9781SMark Murray 			    && ifp->int_ripv1_mask > mask
3142c7a9781SMark Murray 			    && ifp->int_ripv1_mask != HOST_MASK)
3157b6ab19dSGarrett Wollman 				mask = ifp->int_ripv1_mask;
3167b6ab19dSGarrett Wollman 		}
3172c7a9781SMark Murray 
3182c7a9781SMark Murray 	}
3192c7a9781SMark Murray 
3202c7a9781SMark Murray 	/* check special definitions */
3212c7a9781SMark Murray 	if (mask == 0) {
322562c5a82SPedro F. Giffuni 		for (r1p = r1nets; r1p != NULL; r1p = r1p->r1net_next) {
3232c7a9781SMark Murray 			if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
3242c7a9781SMark Murray 			    && r1p->r1net_mask > mask)
3252c7a9781SMark Murray 				mask = r1p->r1net_mask;
3267b6ab19dSGarrett Wollman 		}
3277b6ab19dSGarrett Wollman 
3287b6ab19dSGarrett Wollman 		/* Otherwise, make the classic A/B/C guess.
3297b6ab19dSGarrett Wollman 		 */
3307b6ab19dSGarrett Wollman 		if (mask == 0)
3317b6ab19dSGarrett Wollman 			mask = std_mask(addr);
3322c7a9781SMark Murray 	}
3337b6ab19dSGarrett Wollman 
3347b6ab19dSGarrett Wollman 	return mask;
3357b6ab19dSGarrett Wollman }
3367b6ab19dSGarrett Wollman 
3377b6ab19dSGarrett Wollman 
3387b6ab19dSGarrett Wollman naddr
ripv1_mask_host(naddr addr,struct interface * ifp)3397b6ab19dSGarrett Wollman ripv1_mask_host(naddr addr,		/* in network byte order */
3407b6ab19dSGarrett Wollman 		struct interface *ifp)	/* as seen on this interface */
3417b6ab19dSGarrett Wollman {
3427b6ab19dSGarrett Wollman 	naddr mask = ripv1_mask_net(addr, ifp);
3437b6ab19dSGarrett Wollman 
3447b6ab19dSGarrett Wollman 
3457b6ab19dSGarrett Wollman 	/* If the computed netmask does not mask the address,
3467b6ab19dSGarrett Wollman 	 * then assume it is a host address
3477b6ab19dSGarrett Wollman 	 */
3487b6ab19dSGarrett Wollman 	if ((ntohl(addr) & ~mask) != 0)
3497b6ab19dSGarrett Wollman 		mask = HOST_MASK;
3507b6ab19dSGarrett Wollman 	return mask;
3517b6ab19dSGarrett Wollman }
3527b6ab19dSGarrett Wollman 
3537b6ab19dSGarrett Wollman 
3549d5abbddSJens Schweikhardt /* See if an IP address looks reasonable as a destination.
3557b6ab19dSGarrett Wollman  */
3567b6ab19dSGarrett Wollman int					/* 0=bad */
check_dst(naddr addr)3577b6ab19dSGarrett Wollman check_dst(naddr addr)
3587b6ab19dSGarrett Wollman {
359fd8e4ebcSMike Barcroft 	addr = ntohl(addr);
3607b6ab19dSGarrett Wollman 
3617b6ab19dSGarrett Wollman 	if (IN_CLASSA(addr)) {
3627b6ab19dSGarrett Wollman 		if (addr == 0)
3637b6ab19dSGarrett Wollman 			return 1;	/* default */
3647b6ab19dSGarrett Wollman 
3657b6ab19dSGarrett Wollman 		addr >>= IN_CLASSA_NSHIFT;
3667b6ab19dSGarrett Wollman 		return (addr != 0 && addr != IN_LOOPBACKNET);
3677b6ab19dSGarrett Wollman 	}
3687b6ab19dSGarrett Wollman 
3697b6ab19dSGarrett Wollman 	return (IN_CLASSB(addr) || IN_CLASSC(addr));
3707b6ab19dSGarrett Wollman }
3717b6ab19dSGarrett Wollman 
3727b6ab19dSGarrett Wollman 
373d5b718b3SGarrett Wollman /* See a new interface duplicates an existing interface.
374d5b718b3SGarrett Wollman  */
375d5b718b3SGarrett Wollman struct interface *
check_dup(naddr addr,naddr dstaddr,naddr mask,int if_flags)37671965874SGarrett Wollman check_dup(naddr addr,			/* IP address, so network byte order */
37771965874SGarrett Wollman 	  naddr dstaddr,		/* ditto */
37871965874SGarrett Wollman 	  naddr mask,			/* mask, so host byte order */
379d5b718b3SGarrett Wollman 	  int if_flags)
380d5b718b3SGarrett Wollman {
381d5b718b3SGarrett Wollman 	struct interface *ifp;
382d5b718b3SGarrett Wollman 
38338ae6523SPoul-Henning Kamp 	LIST_FOREACH(ifp, &ifnet, int_list) {
384d5b718b3SGarrett Wollman 		if (ifp->int_mask != mask)
385d5b718b3SGarrett Wollman 			continue;
386d5b718b3SGarrett Wollman 
3872c7a9781SMark Murray 		if (!iff_up(ifp->int_if_flags))
388d5b718b3SGarrett Wollman 			continue;
389d5b718b3SGarrett Wollman 
3902c7a9781SMark Murray 		/* The local address can only be shared with a point-to-point
3912c7a9781SMark Murray 		 * link.
392d5b718b3SGarrett Wollman 		 */
393caa79e36SBruce M Simpson 		if ((!(ifp->int_state & IS_REMOTE) || !(if_flags & IS_REMOTE))
394caa79e36SBruce M Simpson 		    && ifp->int_addr == addr
395d5b718b3SGarrett Wollman 		    && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
396d5b718b3SGarrett Wollman 			return ifp;
397d5b718b3SGarrett Wollman 
398d5b718b3SGarrett Wollman 		if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
399d5b718b3SGarrett Wollman 			return ifp;
400d5b718b3SGarrett Wollman 	}
401d5b718b3SGarrett Wollman 	return 0;
402d5b718b3SGarrett Wollman }
403d5b718b3SGarrett Wollman 
404d5b718b3SGarrett Wollman 
405d5b718b3SGarrett Wollman /* See that a remote gateway is reachable.
406d5b718b3SGarrett Wollman  *	Note that the answer can change as real interfaces come and go.
407d5b718b3SGarrett Wollman  */
408d5b718b3SGarrett Wollman int					/* 0=bad */
check_remote(struct interface * ifp)409d5b718b3SGarrett Wollman check_remote(struct interface *ifp)
410d5b718b3SGarrett Wollman {
411d5b718b3SGarrett Wollman 	struct rt_entry *rt;
412d5b718b3SGarrett Wollman 
413d5b718b3SGarrett Wollman 	/* do not worry about other kinds */
414d5b718b3SGarrett Wollman 	if (!(ifp->int_state & IS_REMOTE))
415d5b718b3SGarrett Wollman 	    return 1;
416d5b718b3SGarrett Wollman 
417d5b718b3SGarrett Wollman 	rt = rtfind(ifp->int_addr);
418562c5a82SPedro F. Giffuni 	if (rt != NULL
419d5b718b3SGarrett Wollman 	    && rt->rt_ifp != 0
420d5b718b3SGarrett Wollman 	    &&on_net(ifp->int_addr,
421d5b718b3SGarrett Wollman 		     rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
422d5b718b3SGarrett Wollman 		return 1;
423d5b718b3SGarrett Wollman 
424d5b718b3SGarrett Wollman 	/* the gateway cannot be reached directly from one of our
425d5b718b3SGarrett Wollman 	 * interfaces
426d5b718b3SGarrett Wollman 	 */
427d5b718b3SGarrett Wollman 	if (!(ifp->int_state & IS_BROKE)) {
428d5b718b3SGarrett Wollman 		msglog("unreachable gateway %s in "_PATH_GATEWAYS,
429d5b718b3SGarrett Wollman 		       naddr_ntoa(ifp->int_addr));
430d5b718b3SGarrett Wollman 		if_bad(ifp);
431d5b718b3SGarrett Wollman 	}
432d5b718b3SGarrett Wollman 	return 0;
433d5b718b3SGarrett Wollman }
434d5b718b3SGarrett Wollman 
435d5b718b3SGarrett Wollman 
4367b6ab19dSGarrett Wollman /* Delete an interface.
4377b6ab19dSGarrett Wollman  */
4387b6ab19dSGarrett Wollman static void
ifdel(struct interface * ifp)4397b6ab19dSGarrett Wollman ifdel(struct interface *ifp)
4407b6ab19dSGarrett Wollman {
4417b6ab19dSGarrett Wollman 	struct interface *ifp1;
4427b6ab19dSGarrett Wollman 
4437b6ab19dSGarrett Wollman 
4447b6ab19dSGarrett Wollman 	trace_if("Del", ifp);
4457b6ab19dSGarrett Wollman 
4467b6ab19dSGarrett Wollman 	ifp->int_state |= IS_BROKE;
4477b6ab19dSGarrett Wollman 
44838ae6523SPoul-Henning Kamp 	LIST_REMOVE(ifp, int_list);
449d5b718b3SGarrett Wollman 	*ifp->int_ahash_prev = ifp->int_ahash;
450d5b718b3SGarrett Wollman 	if (ifp->int_ahash != 0)
451d5b718b3SGarrett Wollman 		ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
452cdb877c3SGarrett Wollman 	*ifp->int_nhash_prev = ifp->int_nhash;
453cdb877c3SGarrett Wollman 	if (ifp->int_nhash != 0)
454cdb877c3SGarrett Wollman 		ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
455d5b718b3SGarrett Wollman 	if (ifp->int_if_flags & IFF_BROADCAST) {
456d5b718b3SGarrett Wollman 		*ifp->int_bhash_prev = ifp->int_bhash;
457d5b718b3SGarrett Wollman 		if (ifp->int_bhash != 0)
458d5b718b3SGarrett Wollman 			ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
459d5b718b3SGarrett Wollman 	}
4606b6b8280SPoul-Henning Kamp 	if (ifp->int_state & IS_REMOTE)
4616b6b8280SPoul-Henning Kamp 		LIST_REMOVE(ifp, remote_list);
4627b6ab19dSGarrett Wollman 
4637b6ab19dSGarrett Wollman 	if (!(ifp->int_state & IS_ALIAS)) {
464d5b718b3SGarrett Wollman 		/* delete aliases when the main interface dies
4657b6ab19dSGarrett Wollman 		 */
46638ae6523SPoul-Henning Kamp 		LIST_FOREACH(ifp1, &ifnet, int_list) {
4677b6ab19dSGarrett Wollman 			if (ifp1 != ifp
4687b6ab19dSGarrett Wollman 			    && !strcmp(ifp->int_name, ifp1->int_name))
4697b6ab19dSGarrett Wollman 				ifdel(ifp1);
4707b6ab19dSGarrett Wollman 		}
4717b6ab19dSGarrett Wollman 
47268b22388SPoul-Henning Kamp 		if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
47368b22388SPoul-Henning Kamp 			struct group_req gr;
47468b22388SPoul-Henning Kamp 			struct sockaddr_in *sin;
47568b22388SPoul-Henning Kamp 
47668b22388SPoul-Henning Kamp 			memset(&gr, 0, sizeof(gr));
47768b22388SPoul-Henning Kamp 			gr.gr_interface = ifp->int_index;
47868b22388SPoul-Henning Kamp 			sin = (struct sockaddr_in *)&gr.gr_group;
47968b22388SPoul-Henning Kamp 			sin->sin_family = AF_INET;
48068b22388SPoul-Henning Kamp #ifdef _HAVE_SIN_LEN
48168b22388SPoul-Henning Kamp 			sin->sin_len = sizeof(struct sockaddr_in);
4827b6ab19dSGarrett Wollman #endif
48368b22388SPoul-Henning Kamp 			sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
48468b22388SPoul-Henning Kamp 			if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
48568b22388SPoul-Henning Kamp 				       &gr, sizeof(gr)) < 0
4867b6ab19dSGarrett Wollman 			    && errno != EADDRNOTAVAIL
4877b6ab19dSGarrett Wollman 			    && !TRACEACTIONS)
48868b22388SPoul-Henning Kamp 				LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
489d5b718b3SGarrett Wollman 			if (rip_sock_mcast == ifp)
490562c5a82SPedro F. Giffuni 				rip_sock_mcast = NULL;
4917b6ab19dSGarrett Wollman 		}
4927b6ab19dSGarrett Wollman 		if (ifp->int_rip_sock >= 0) {
4937b6ab19dSGarrett Wollman 			(void)close(ifp->int_rip_sock);
4947b6ab19dSGarrett Wollman 			ifp->int_rip_sock = -1;
4957b6ab19dSGarrett Wollman 			fix_select();
4967b6ab19dSGarrett Wollman 		}
4977b6ab19dSGarrett Wollman 
4987b6ab19dSGarrett Wollman 		tot_interfaces--;
4997b6ab19dSGarrett Wollman 		if (!IS_RIP_OFF(ifp->int_state))
5007b6ab19dSGarrett Wollman 			rip_interfaces--;
5017b6ab19dSGarrett Wollman 
5027b6ab19dSGarrett Wollman 		/* Zap all routes associated with this interface.
5032c7a9781SMark Murray 		 * Assume routes just using gateways beyond this interface
5042c7a9781SMark Murray 		 * will timeout naturally, and have probably already died.
5057b6ab19dSGarrett Wollman 		 */
5067b6ab19dSGarrett Wollman 		(void)rn_walktree(rhead, walk_bad, 0);
5077b6ab19dSGarrett Wollman 
5087b6ab19dSGarrett Wollman 		set_rdisc_mg(ifp, 0);
5097b6ab19dSGarrett Wollman 		if_bad_rdisc(ifp);
5107b6ab19dSGarrett Wollman 	}
5117b6ab19dSGarrett Wollman 
5127b6ab19dSGarrett Wollman 	free(ifp);
5137b6ab19dSGarrett Wollman }
5147b6ab19dSGarrett Wollman 
5157b6ab19dSGarrett Wollman 
5167b6ab19dSGarrett Wollman /* Mark an interface ill.
5177b6ab19dSGarrett Wollman  */
5187b6ab19dSGarrett Wollman void
if_sick(struct interface * ifp)5197b6ab19dSGarrett Wollman if_sick(struct interface *ifp)
5207b6ab19dSGarrett Wollman {
5217b6ab19dSGarrett Wollman 	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
5227b6ab19dSGarrett Wollman 		ifp->int_state |= IS_SICK;
523d5b718b3SGarrett Wollman 		ifp->int_act_time = NEVER;
5247b6ab19dSGarrett Wollman 		trace_if("Chg", ifp);
5257b6ab19dSGarrett Wollman 
5267b6ab19dSGarrett Wollman 		LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
5277b6ab19dSGarrett Wollman 	}
5287b6ab19dSGarrett Wollman }
5297b6ab19dSGarrett Wollman 
5307b6ab19dSGarrett Wollman 
5317b6ab19dSGarrett Wollman /* Mark an interface dead.
5327b6ab19dSGarrett Wollman  */
533f2ca6d8aSPoul-Henning Kamp static void
if_bad(struct interface * ifp)5347b6ab19dSGarrett Wollman if_bad(struct interface *ifp)
5357b6ab19dSGarrett Wollman {
5367b6ab19dSGarrett Wollman 	struct interface *ifp1;
5377b6ab19dSGarrett Wollman 
5387b6ab19dSGarrett Wollman 
5397b6ab19dSGarrett Wollman 	if (ifp->int_state & IS_BROKE)
5407b6ab19dSGarrett Wollman 		return;
5417b6ab19dSGarrett Wollman 
5427b6ab19dSGarrett Wollman 	LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
5437b6ab19dSGarrett Wollman 
5447b6ab19dSGarrett Wollman 	ifp->int_state |= (IS_BROKE | IS_SICK);
545d5b718b3SGarrett Wollman 	ifp->int_act_time = NEVER;
546d5b718b3SGarrett Wollman 	ifp->int_query_time = NEVER;
5472c7a9781SMark Murray 	ifp->int_data.ts = now.tv_sec;
5487b6ab19dSGarrett Wollman 
5497b6ab19dSGarrett Wollman 	trace_if("Chg", ifp);
5507b6ab19dSGarrett Wollman 
5517b6ab19dSGarrett Wollman 	if (!(ifp->int_state & IS_ALIAS)) {
55238ae6523SPoul-Henning Kamp 		LIST_FOREACH(ifp1, &ifnet, int_list) {
5537b6ab19dSGarrett Wollman 			if (ifp1 != ifp
5547b6ab19dSGarrett Wollman 			    && !strcmp(ifp->int_name, ifp1->int_name))
5557b6ab19dSGarrett Wollman 				if_bad(ifp1);
5567b6ab19dSGarrett Wollman 		}
5577b6ab19dSGarrett Wollman 		(void)rn_walktree(rhead, walk_bad, 0);
5587b6ab19dSGarrett Wollman 		if_bad_rdisc(ifp);
5597b6ab19dSGarrett Wollman 	}
5607b6ab19dSGarrett Wollman }
5617b6ab19dSGarrett Wollman 
5627b6ab19dSGarrett Wollman 
5637b6ab19dSGarrett Wollman /* Mark an interface alive
5647b6ab19dSGarrett Wollman  */
5657b6ab19dSGarrett Wollman int					/* 1=it was dead */
if_ok(struct interface * ifp,const char * type)5667b6ab19dSGarrett Wollman if_ok(struct interface *ifp,
5672c7a9781SMark Murray       const char *type)
5687b6ab19dSGarrett Wollman {
5697b6ab19dSGarrett Wollman 	struct interface *ifp1;
5707b6ab19dSGarrett Wollman 
5717b6ab19dSGarrett Wollman 
5727b6ab19dSGarrett Wollman 	if (!(ifp->int_state & IS_BROKE)) {
5737b6ab19dSGarrett Wollman 		if (ifp->int_state & IS_SICK) {
574d5b718b3SGarrett Wollman 			trace_act("%sinterface %s to %s working better",
5757b6ab19dSGarrett Wollman 				  type,
576d5b718b3SGarrett Wollman 				  ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
5777b6ab19dSGarrett Wollman 			ifp->int_state &= ~IS_SICK;
5787b6ab19dSGarrett Wollman 		}
5797b6ab19dSGarrett Wollman 		return 0;
5807b6ab19dSGarrett Wollman 	}
5817b6ab19dSGarrett Wollman 
5827b6ab19dSGarrett Wollman 	msglog("%sinterface %s to %s restored",
583d5b718b3SGarrett Wollman 	       type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
5847b6ab19dSGarrett Wollman 	ifp->int_state &= ~(IS_BROKE | IS_SICK);
5857b6ab19dSGarrett Wollman 	ifp->int_data.ts = 0;
5867b6ab19dSGarrett Wollman 
5877b6ab19dSGarrett Wollman 	if (!(ifp->int_state & IS_ALIAS)) {
58838ae6523SPoul-Henning Kamp 		LIST_FOREACH(ifp1, &ifnet, int_list) {
5897b6ab19dSGarrett Wollman 			if (ifp1 != ifp
5907b6ab19dSGarrett Wollman 			    && !strcmp(ifp->int_name, ifp1->int_name))
5917b6ab19dSGarrett Wollman 				if_ok(ifp1, type);
5927b6ab19dSGarrett Wollman 		}
5937b6ab19dSGarrett Wollman 		if_ok_rdisc(ifp);
5947b6ab19dSGarrett Wollman 	}
595d5b718b3SGarrett Wollman 
596d5b718b3SGarrett Wollman 	if (ifp->int_state & IS_REMOTE) {
597d5b718b3SGarrett Wollman 		if (!addrouteforif(ifp))
598d5b718b3SGarrett Wollman 			return 0;
599d5b718b3SGarrett Wollman 	}
6007b6ab19dSGarrett Wollman 	return 1;
6017b6ab19dSGarrett Wollman }
6027b6ab19dSGarrett Wollman 
6037b6ab19dSGarrett Wollman 
6047b6ab19dSGarrett Wollman /* disassemble routing message
6057b6ab19dSGarrett Wollman  */
6067b6ab19dSGarrett Wollman void
rt_xaddrs(struct rt_addrinfo * info,struct sockaddr * sa,struct sockaddr * lim,int addrs)6077b6ab19dSGarrett Wollman rt_xaddrs(struct rt_addrinfo *info,
6087b6ab19dSGarrett Wollman 	  struct sockaddr *sa,
6097b6ab19dSGarrett Wollman 	  struct sockaddr *lim,
6107b6ab19dSGarrett Wollman 	  int addrs)
6117b6ab19dSGarrett Wollman {
6127b6ab19dSGarrett Wollman 	int i;
6137b6ab19dSGarrett Wollman #ifdef _HAVE_SA_LEN
6147b6ab19dSGarrett Wollman 	static struct sockaddr sa_zero;
6157b6ab19dSGarrett Wollman #endif
6167b6ab19dSGarrett Wollman 
6172c7a9781SMark Murray 	memset(info, 0, sizeof(*info));
6187b6ab19dSGarrett Wollman 	info->rti_addrs = addrs;
6197b6ab19dSGarrett Wollman 	for (i = 0; i < RTAX_MAX && sa < lim; i++) {
6207b6ab19dSGarrett Wollman 		if ((addrs & (1 << i)) == 0)
6217b6ab19dSGarrett Wollman 			continue;
6227b6ab19dSGarrett Wollman 		info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
6230b46c085SLuigi Rizzo 		sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
6247b6ab19dSGarrett Wollman 	}
6257b6ab19dSGarrett Wollman }
6267b6ab19dSGarrett Wollman 
6277b6ab19dSGarrett Wollman 
6287b6ab19dSGarrett Wollman /* Find the network interfaces which have configured themselves.
6297b6ab19dSGarrett Wollman  *	This must be done regularly, if only for extra addresses
6307b6ab19dSGarrett Wollman  *	that come and go on interfaces.
6317b6ab19dSGarrett Wollman  */
6327b6ab19dSGarrett Wollman void
ifinit(void)6337b6ab19dSGarrett Wollman ifinit(void)
6347b6ab19dSGarrett Wollman {
635960b861bSPoul-Henning Kamp 	static struct ifa_msghdr *sysctl_buf;
6367b6ab19dSGarrett Wollman 	static size_t sysctl_buf_size = 0;
6377b6ab19dSGarrett Wollman 	uint complaints = 0;
6387b6ab19dSGarrett Wollman 	static u_int prev_complaints = 0;
6397b6ab19dSGarrett Wollman #	define COMP_NOT_INET	0x001
640d5b718b3SGarrett Wollman #	define COMP_NOADDR	0x002
641d5b718b3SGarrett Wollman #	define COMP_BADADDR	0x004
642d5b718b3SGarrett Wollman #	define COMP_NODST	0x008
643d5b718b3SGarrett Wollman #	define COMP_NOBADR	0x010
644d5b718b3SGarrett Wollman #	define COMP_NOMASK	0x020
645d5b718b3SGarrett Wollman #	define COMP_DUP		0x040
646d5b718b3SGarrett Wollman #	define COMP_BAD_METRIC	0x080
647d5b718b3SGarrett Wollman #	define COMP_NETMASK	0x100
6487b6ab19dSGarrett Wollman 
6497b6ab19dSGarrett Wollman 	struct interface ifs, ifs0, *ifp, *ifp1;
6507b6ab19dSGarrett Wollman 	struct rt_entry *rt;
6517b6ab19dSGarrett Wollman 	size_t needed;
6527b6ab19dSGarrett Wollman 	int mib[6];
6537b6ab19dSGarrett Wollman 	struct if_msghdr *ifm;
654960b861bSPoul-Henning Kamp 	void *ifam_lim;
655960b861bSPoul-Henning Kamp 	struct ifa_msghdr *ifam, *ifam2;
6567b6ab19dSGarrett Wollman 	int in, ierr, out, oerr;
6577b6ab19dSGarrett Wollman 	struct intnet *intnetp;
6587b6ab19dSGarrett Wollman 	struct rt_addrinfo info;
6597b6ab19dSGarrett Wollman #ifdef SIOCGIFMETRIC
6607b6ab19dSGarrett Wollman 	struct ifreq ifr;
6617b6ab19dSGarrett Wollman #endif
6627b6ab19dSGarrett Wollman 
6637b6ab19dSGarrett Wollman 
664337e72d0SGarrett Wollman 	last_ifinit = now;
6657b6ab19dSGarrett Wollman 	ifinit_timer.tv_sec = now.tv_sec + (supplier
6667b6ab19dSGarrett Wollman 					    ? CHECK_ACT_INTERVAL
6677b6ab19dSGarrett Wollman 					    : CHECK_QUIET_INTERVAL);
6687b6ab19dSGarrett Wollman 
66990100551SPhilippe Charnier 	/* mark all interfaces so we can get rid of those that disappear */
67038ae6523SPoul-Henning Kamp 	LIST_FOREACH(ifp, &ifnet, int_list)
6717b6ab19dSGarrett Wollman 		ifp->int_state &= ~(IS_CHECKED | IS_DUP);
6727b6ab19dSGarrett Wollman 
6737b6ab19dSGarrett Wollman 	/* Fetch the interface list, without too many system calls
6747b6ab19dSGarrett Wollman 	 * since we do it repeatedly.
6757b6ab19dSGarrett Wollman 	 */
6767b6ab19dSGarrett Wollman 	mib[0] = CTL_NET;
6777b6ab19dSGarrett Wollman 	mib[1] = PF_ROUTE;
6787b6ab19dSGarrett Wollman 	mib[2] = 0;
6797b6ab19dSGarrett Wollman 	mib[3] = AF_INET;
6807b6ab19dSGarrett Wollman 	mib[4] = NET_RT_IFLIST;
6817b6ab19dSGarrett Wollman 	mib[5] = 0;
6827b6ab19dSGarrett Wollman 	for (;;) {
6837b6ab19dSGarrett Wollman 		if ((needed = sysctl_buf_size) != 0) {
6847b6ab19dSGarrett Wollman 			if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
6857b6ab19dSGarrett Wollman 				break;
6862c7a9781SMark Murray 			/* retry if the table grew */
6877b6ab19dSGarrett Wollman 			if (errno != ENOMEM && errno != EFAULT)
6882c7a9781SMark Murray 				BADERR(1, "ifinit: sysctl(RT_IFLIST)");
6897b6ab19dSGarrett Wollman 			free(sysctl_buf);
6907b6ab19dSGarrett Wollman 			needed = 0;
6917b6ab19dSGarrett Wollman 		}
6927b6ab19dSGarrett Wollman 		if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
6932c7a9781SMark Murray 			BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
6942c7a9781SMark Murray 		sysctl_buf = rtmalloc(sysctl_buf_size = needed,
6952c7a9781SMark Murray 				      "ifinit sysctl");
6967b6ab19dSGarrett Wollman 	}
6977b6ab19dSGarrett Wollman 
698960b861bSPoul-Henning Kamp 	/* XXX: thanks to malloc(3), alignment can be presumed OK */
699960b861bSPoul-Henning Kamp 	ifam_lim = (char *)sysctl_buf + needed;
700960b861bSPoul-Henning Kamp 	for (ifam = sysctl_buf; (void *)ifam < ifam_lim; ifam = ifam2) {
7017b6ab19dSGarrett Wollman 
7027b6ab19dSGarrett Wollman 		ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
7037b6ab19dSGarrett Wollman 
704caa79e36SBruce M Simpson #ifdef RTM_OIFINFO
705caa79e36SBruce M Simpson 		if (ifam->ifam_type == RTM_OIFINFO)
706caa79e36SBruce M Simpson 			continue;	/* just ignore compat message */
707caa79e36SBruce M Simpson #endif
7087b6ab19dSGarrett Wollman 		if (ifam->ifam_type == RTM_IFINFO) {
709d5b718b3SGarrett Wollman 			struct sockaddr_dl *sdl;
710d5b718b3SGarrett Wollman 
7117b6ab19dSGarrett Wollman 			ifm = (struct if_msghdr *)ifam;
7127b6ab19dSGarrett Wollman 			/* make prototype structure for the IP aliases
7137b6ab19dSGarrett Wollman 			 */
7142c7a9781SMark Murray 			memset(&ifs0, 0, sizeof(ifs0));
7157b6ab19dSGarrett Wollman 			ifs0.int_rip_sock = -1;
7167b6ab19dSGarrett Wollman 			ifs0.int_index = ifm->ifm_index;
7177b6ab19dSGarrett Wollman 			ifs0.int_if_flags = ifm->ifm_flags;
7187b6ab19dSGarrett Wollman 			ifs0.int_state = IS_CHECKED;
71971965874SGarrett Wollman 			ifs0.int_query_time = NEVER;
7207b6ab19dSGarrett Wollman 			ifs0.int_act_time = now.tv_sec;
7217b6ab19dSGarrett Wollman 			ifs0.int_data.ts = now.tv_sec;
7227b6ab19dSGarrett Wollman 			ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
7237b6ab19dSGarrett Wollman 			ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
7247b6ab19dSGarrett Wollman 			ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
7257b6ab19dSGarrett Wollman 			ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
7267b6ab19dSGarrett Wollman 			sdl = (struct sockaddr_dl *)(ifm + 1);
7277b6ab19dSGarrett Wollman 			sdl->sdl_data[sdl->sdl_nlen] = 0;
728d5b718b3SGarrett Wollman 			strncpy(ifs0.int_name, sdl->sdl_data,
729d5b718b3SGarrett Wollman 				MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
7307b6ab19dSGarrett Wollman 			continue;
7317b6ab19dSGarrett Wollman 		}
7327b6ab19dSGarrett Wollman 		if (ifam->ifam_type != RTM_NEWADDR) {
7337b6ab19dSGarrett Wollman 			logbad(1,"ifinit: out of sync");
7347b6ab19dSGarrett Wollman 			continue;
7357b6ab19dSGarrett Wollman 		}
7367b6ab19dSGarrett Wollman 		rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
7377b6ab19dSGarrett Wollman 			  (struct sockaddr *)ifam2,
7387b6ab19dSGarrett Wollman 			  ifam->ifam_addrs);
7397b6ab19dSGarrett Wollman 
740d5b718b3SGarrett Wollman 		/* Prepare for the next address of this interface, which
741d5b718b3SGarrett Wollman 		 * will be an alias.
742d5b718b3SGarrett Wollman 		 * Do not output RIP or Router-Discovery packets via aliases.
743d5b718b3SGarrett Wollman 		 */
7442c7a9781SMark Murray 		memcpy(&ifs, &ifs0, sizeof(ifs));
7452c7a9781SMark Murray 		ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
746d5b718b3SGarrett Wollman 
7477b6ab19dSGarrett Wollman 		if (INFO_IFA(&info) == 0) {
7482c7a9781SMark Murray 			if (iff_up(ifs.int_if_flags)) {
7497b6ab19dSGarrett Wollman 				if (!(prev_complaints & COMP_NOADDR))
7507b6ab19dSGarrett Wollman 					msglog("%s has no address",
751d5b718b3SGarrett Wollman 					       ifs.int_name);
7527b6ab19dSGarrett Wollman 				complaints |= COMP_NOADDR;
7537b6ab19dSGarrett Wollman 			}
7547b6ab19dSGarrett Wollman 			continue;
7557b6ab19dSGarrett Wollman 		}
7567b6ab19dSGarrett Wollman 		if (INFO_IFA(&info)->sa_family != AF_INET) {
7572c7a9781SMark Murray 			if (iff_up(ifs.int_if_flags)) {
7587b6ab19dSGarrett Wollman 				if (!(prev_complaints & COMP_NOT_INET))
759d5b718b3SGarrett Wollman 					trace_act("%s: not AF_INET",
760d5b718b3SGarrett Wollman 						  ifs.int_name);
7617b6ab19dSGarrett Wollman 				complaints |= COMP_NOT_INET;
7627b6ab19dSGarrett Wollman 			}
7637b6ab19dSGarrett Wollman 			continue;
7647b6ab19dSGarrett Wollman 		}
7657b6ab19dSGarrett Wollman 
7667b6ab19dSGarrett Wollman 		ifs.int_addr = S_ADDR(INFO_IFA(&info));
7677b6ab19dSGarrett Wollman 
7687b6ab19dSGarrett Wollman 		if (ntohl(ifs.int_addr)>>24 == 0
7697b6ab19dSGarrett Wollman 		    || ntohl(ifs.int_addr)>>24 == 0xff) {
7702c7a9781SMark Murray 			if (iff_up(ifs.int_if_flags)) {
7717b6ab19dSGarrett Wollman 				if (!(prev_complaints & COMP_BADADDR))
7727b6ab19dSGarrett Wollman 					msglog("%s has a bad address",
773d5b718b3SGarrett Wollman 					       ifs.int_name);
7747b6ab19dSGarrett Wollman 				complaints |= COMP_BADADDR;
7757b6ab19dSGarrett Wollman 			}
7767b6ab19dSGarrett Wollman 			continue;
7777b6ab19dSGarrett Wollman 		}
7787b6ab19dSGarrett Wollman 
779d5b718b3SGarrett Wollman 		if (ifs.int_if_flags & IFF_LOOPBACK) {
780e53915c9SPoul-Henning Kamp 			ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
78152146665SMaxim Konovalov 			if (ifs.int_addr == htonl(INADDR_LOOPBACK))
782e53915c9SPoul-Henning Kamp 				ifs.int_state |= IS_PASSIVE;
7837b6ab19dSGarrett Wollman 			ifs.int_dstaddr = ifs.int_addr;
784d5b718b3SGarrett Wollman 			ifs.int_mask = HOST_MASK;
785d5b718b3SGarrett Wollman 			ifs.int_ripv1_mask = HOST_MASK;
786d5b718b3SGarrett Wollman 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
787d5b718b3SGarrett Wollman 			ifs.int_net = ntohl(ifs.int_dstaddr);
788d5b718b3SGarrett Wollman 			if (!foundloopback) {
789d5b718b3SGarrett Wollman 				foundloopback = 1;
790d5b718b3SGarrett Wollman 				loopaddr = ifs.int_addr;
7912c7a9781SMark Murray 				loop_rts.rts_gate = loopaddr;
7922c7a9781SMark Murray 				loop_rts.rts_router = loopaddr;
7937b6ab19dSGarrett Wollman 			}
7947b6ab19dSGarrett Wollman 
7957b6ab19dSGarrett Wollman 		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
7967b6ab19dSGarrett Wollman 			if (INFO_BRD(&info) == 0
7977b6ab19dSGarrett Wollman 			    || INFO_BRD(&info)->sa_family != AF_INET) {
7982c7a9781SMark Murray 				if (iff_up(ifs.int_if_flags)) {
7997b6ab19dSGarrett Wollman 					if (!(prev_complaints & COMP_NODST))
8007b6ab19dSGarrett Wollman 						msglog("%s has a bad"
8017b6ab19dSGarrett Wollman 						       " destination address",
802d5b718b3SGarrett Wollman 						       ifs.int_name);
8037b6ab19dSGarrett Wollman 					complaints |= COMP_NODST;
8047b6ab19dSGarrett Wollman 				}
8057b6ab19dSGarrett Wollman 				continue;
8067b6ab19dSGarrett Wollman 			}
8077b6ab19dSGarrett Wollman 			ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
8087b6ab19dSGarrett Wollman 			if (ntohl(ifs.int_dstaddr)>>24 == 0
8097b6ab19dSGarrett Wollman 			    || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
8102c7a9781SMark Murray 				if (iff_up(ifs.int_if_flags)) {
8117b6ab19dSGarrett Wollman 					if (!(prev_complaints & COMP_NODST))
8127b6ab19dSGarrett Wollman 						msglog("%s has a bad"
8137b6ab19dSGarrett Wollman 						       " destination address",
814d5b718b3SGarrett Wollman 						       ifs.int_name);
8157b6ab19dSGarrett Wollman 					complaints |= COMP_NODST;
8167b6ab19dSGarrett Wollman 				}
8177b6ab19dSGarrett Wollman 				continue;
8187b6ab19dSGarrett Wollman 			}
8197b6ab19dSGarrett Wollman 			ifs.int_mask = HOST_MASK;
8207b6ab19dSGarrett Wollman 			ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
8217b6ab19dSGarrett Wollman 			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
8227b6ab19dSGarrett Wollman 			ifs.int_net = ntohl(ifs.int_dstaddr);
8237b6ab19dSGarrett Wollman 
8247b6ab19dSGarrett Wollman 		}  else {
825d5b718b3SGarrett Wollman 			if (INFO_MASK(&info) == 0) {
8262c7a9781SMark Murray 				if (iff_up(ifs.int_if_flags)) {
827d5b718b3SGarrett Wollman 					if (!(prev_complaints & COMP_NOMASK))
828d5b718b3SGarrett Wollman 						msglog("%s has no netmask",
829d5b718b3SGarrett Wollman 						       ifs.int_name);
830d5b718b3SGarrett Wollman 					complaints |= COMP_NOMASK;
831d5b718b3SGarrett Wollman 				}
8327b6ab19dSGarrett Wollman 				continue;
8337b6ab19dSGarrett Wollman 			}
834d5b718b3SGarrett Wollman 			ifs.int_dstaddr = ifs.int_addr;
835d5b718b3SGarrett Wollman 			ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
836d5b718b3SGarrett Wollman 			ifs.int_ripv1_mask = ifs.int_mask;
837d5b718b3SGarrett Wollman 			ifs.int_std_mask = std_mask(ifs.int_addr);
838d5b718b3SGarrett Wollman 			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
839d5b718b3SGarrett Wollman 			if (ifs.int_mask != ifs.int_std_mask)
840d5b718b3SGarrett Wollman 				ifs.int_state |= IS_SUBNET;
841d5b718b3SGarrett Wollman 
842d5b718b3SGarrett Wollman 			if (ifs.int_if_flags & IFF_BROADCAST) {
843d5b718b3SGarrett Wollman 				if (INFO_BRD(&info) == 0) {
8442c7a9781SMark Murray 					if (iff_up(ifs.int_if_flags)) {
845d5b718b3SGarrett Wollman 					    if (!(prev_complaints
846d5b718b3SGarrett Wollman 						  & COMP_NOBADR))
847d5b718b3SGarrett Wollman 						msglog("%s has"
848d5b718b3SGarrett Wollman 						       "no broadcast address",
849d5b718b3SGarrett Wollman 						       ifs.int_name);
850d5b718b3SGarrett Wollman 					    complaints |= COMP_NOBADR;
851d5b718b3SGarrett Wollman 					}
852d5b718b3SGarrett Wollman 					continue;
853d5b718b3SGarrett Wollman 				}
854d5b718b3SGarrett Wollman 				ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
855d5b718b3SGarrett Wollman 			}
856d5b718b3SGarrett Wollman 		}
8577b6ab19dSGarrett Wollman 		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
8587b6ab19dSGarrett Wollman 		ifs.int_std_addr = htonl(ifs.int_std_net);
8597b6ab19dSGarrett Wollman 
8607b6ab19dSGarrett Wollman 		/* Use a minimum metric of one.  Treat the interface metric
8617b6ab19dSGarrett Wollman 		 * (default 0) as an increment to the hop count of one.
8627b6ab19dSGarrett Wollman 		 *
8637b6ab19dSGarrett Wollman 		 * The metric obtained from the routing socket dump of
8647b6ab19dSGarrett Wollman 		 * interface addresses is wrong.  It is not set by the
8657b6ab19dSGarrett Wollman 		 * SIOCSIFMETRIC ioctl.
8667b6ab19dSGarrett Wollman 		 */
8677b6ab19dSGarrett Wollman #ifdef SIOCGIFMETRIC
868d5b718b3SGarrett Wollman 		strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
8697b6ab19dSGarrett Wollman 		if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
8707b6ab19dSGarrett Wollman 			DBGERR(1, "ioctl(SIOCGIFMETRIC)");
8717b6ab19dSGarrett Wollman 			ifs.int_metric = 0;
8727b6ab19dSGarrett Wollman 		} else {
8737b6ab19dSGarrett Wollman 			ifs.int_metric = ifr.ifr_metric;
8747b6ab19dSGarrett Wollman 		}
8757b6ab19dSGarrett Wollman #else
8767b6ab19dSGarrett Wollman 		ifs.int_metric = ifam->ifam_metric;
8777b6ab19dSGarrett Wollman #endif
8787b6ab19dSGarrett Wollman 		if (ifs.int_metric > HOPCNT_INFINITY) {
8797b6ab19dSGarrett Wollman 			ifs.int_metric = 0;
8807b6ab19dSGarrett Wollman 			if (!(prev_complaints & COMP_BAD_METRIC)
8812c7a9781SMark Murray 			    && iff_up(ifs.int_if_flags)) {
8827b6ab19dSGarrett Wollman 				complaints |= COMP_BAD_METRIC;
8837b6ab19dSGarrett Wollman 				msglog("%s has a metric of %d",
884d5b718b3SGarrett Wollman 				       ifs.int_name, ifs.int_metric);
8857b6ab19dSGarrett Wollman 			}
8867b6ab19dSGarrett Wollman 		}
8877b6ab19dSGarrett Wollman 
8887b6ab19dSGarrett Wollman 		/* See if this is a familiar interface.
8897b6ab19dSGarrett Wollman 		 * If so, stop worrying about it if it is the same.
8907b6ab19dSGarrett Wollman 		 * Start it over if it now is to somewhere else, as happens
8917b6ab19dSGarrett Wollman 		 * frequently with PPP and SLIP.
8927b6ab19dSGarrett Wollman 		 */
893d5b718b3SGarrett Wollman 		ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
8947b6ab19dSGarrett Wollman 						? ifs.int_addr
8957b6ab19dSGarrett Wollman 						: 0));
896562c5a82SPedro F. Giffuni 		if (ifp != NULL) {
8977b6ab19dSGarrett Wollman 			ifp->int_state |= IS_CHECKED;
8987b6ab19dSGarrett Wollman 
8997b6ab19dSGarrett Wollman 			if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
9007b6ab19dSGarrett Wollman 				  & (IFF_BROADCAST
9017b6ab19dSGarrett Wollman 				     | IFF_LOOPBACK
9027b6ab19dSGarrett Wollman 				     | IFF_POINTOPOINT
9037b6ab19dSGarrett Wollman 				     | IFF_MULTICAST))
9047b6ab19dSGarrett Wollman 			    || 0 != ((ifp->int_state ^ ifs.int_state)
9057b6ab19dSGarrett Wollman 				     & IS_ALIAS)
9067b6ab19dSGarrett Wollman 			    || ifp->int_addr != ifs.int_addr
9077b6ab19dSGarrett Wollman 			    || ifp->int_brdaddr != ifs.int_brdaddr
9087b6ab19dSGarrett Wollman 			    || ifp->int_dstaddr != ifs.int_dstaddr
9097b6ab19dSGarrett Wollman 			    || ifp->int_mask != ifs.int_mask
9107b6ab19dSGarrett Wollman 			    || ifp->int_metric != ifs.int_metric) {
9117b6ab19dSGarrett Wollman 				/* Forget old information about
9127b6ab19dSGarrett Wollman 				 * a changed interface.
9137b6ab19dSGarrett Wollman 				 */
914d5b718b3SGarrett Wollman 				trace_act("interface %s has changed",
9157b6ab19dSGarrett Wollman 					  ifp->int_name);
9167b6ab19dSGarrett Wollman 				ifdel(ifp);
917562c5a82SPedro F. Giffuni 				ifp = NULL;
9187b6ab19dSGarrett Wollman 			}
9197b6ab19dSGarrett Wollman 		}
9207b6ab19dSGarrett Wollman 
921562c5a82SPedro F. Giffuni 		if (ifp != NULL) {
9227b6ab19dSGarrett Wollman 			/* The primary representative of an alias worries
9237b6ab19dSGarrett Wollman 			 * about how things are working.
9247b6ab19dSGarrett Wollman 			 */
9257b6ab19dSGarrett Wollman 			if (ifp->int_state & IS_ALIAS)
9267b6ab19dSGarrett Wollman 				continue;
9277b6ab19dSGarrett Wollman 
9287b6ab19dSGarrett Wollman 			/* note interfaces that have been turned off
9297b6ab19dSGarrett Wollman 			 */
9302c7a9781SMark Murray 			if (!iff_up(ifs.int_if_flags)) {
9312c7a9781SMark Murray 				if (iff_up(ifp->int_if_flags)) {
9327b6ab19dSGarrett Wollman 					msglog("interface %s to %s turned off",
9337b6ab19dSGarrett Wollman 					       ifp->int_name,
934d5b718b3SGarrett Wollman 					       naddr_ntoa(ifp->int_dstaddr));
9357b6ab19dSGarrett Wollman 					if_bad(ifp);
9362c7a9781SMark Murray 					ifp->int_if_flags &= ~IFF_UP;
9372c7a9781SMark Murray 				} else if (now.tv_sec>(ifp->int_data.ts
9382c7a9781SMark Murray 						       + CHECK_BAD_INTERVAL)) {
9392c7a9781SMark Murray 					trace_act("interface %s has been off"
940b108792dSUlrich Spörlein 						  " %jd seconds; forget it",
9412c7a9781SMark Murray 						  ifp->int_name,
942b108792dSUlrich Spörlein 						  (intmax_t)now.tv_sec -
9439940236eSPoul-Henning Kamp 						      ifp->int_data.ts);
9442c7a9781SMark Murray 					ifdel(ifp);
9450b279f8cSPedro F. Giffuni 					ifp = NULL;
9467b6ab19dSGarrett Wollman 				}
9477b6ab19dSGarrett Wollman 				continue;
9487b6ab19dSGarrett Wollman 			}
9497b6ab19dSGarrett Wollman 			/* or that were off and are now ok */
9502c7a9781SMark Murray 			if (!iff_up(ifp->int_if_flags)) {
9512c7a9781SMark Murray 				ifp->int_if_flags |= IFF_UP;
9527b6ab19dSGarrett Wollman 				(void)if_ok(ifp, "");
9537b6ab19dSGarrett Wollman 			}
9547b6ab19dSGarrett Wollman 
9557b6ab19dSGarrett Wollman 			/* If it has been long enough,
9567b6ab19dSGarrett Wollman 			 * see if the interface is broken.
9577b6ab19dSGarrett Wollman 			 */
9587b6ab19dSGarrett Wollman 			if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
9597b6ab19dSGarrett Wollman 				continue;
9607b6ab19dSGarrett Wollman 
9617b6ab19dSGarrett Wollman 			in = ifs.int_data.ipackets - ifp->int_data.ipackets;
9627b6ab19dSGarrett Wollman 			ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
9637b6ab19dSGarrett Wollman 			out = ifs.int_data.opackets - ifp->int_data.opackets;
9647b6ab19dSGarrett Wollman 			oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
9657b6ab19dSGarrett Wollman 			/* If the interface just awoke, restart the counters.
9667b6ab19dSGarrett Wollman 			 */
9677b6ab19dSGarrett Wollman 			if (ifp->int_data.ts == 0) {
9687b6ab19dSGarrett Wollman 				ifp->int_data = ifs.int_data;
9697b6ab19dSGarrett Wollman 				continue;
9707b6ab19dSGarrett Wollman 			}
9717b6ab19dSGarrett Wollman 			ifp->int_data = ifs.int_data;
9727b6ab19dSGarrett Wollman 
97390100551SPhilippe Charnier 			/* Withhold judgment when the short error
9747b6ab19dSGarrett Wollman 			 * counters wrap or the interface is reset.
9757b6ab19dSGarrett Wollman 			 */
9767b6ab19dSGarrett Wollman 			if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
9777b6ab19dSGarrett Wollman 				LIM_SEC(ifinit_timer,
9787b6ab19dSGarrett Wollman 					now.tv_sec+CHECK_BAD_INTERVAL);
9797b6ab19dSGarrett Wollman 				continue;
9807b6ab19dSGarrett Wollman 			}
9817b6ab19dSGarrett Wollman 
9827b6ab19dSGarrett Wollman 			/* Withhold judgement when there is no traffic
9837b6ab19dSGarrett Wollman 			 */
9847b6ab19dSGarrett Wollman 			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
9857b6ab19dSGarrett Wollman 				continue;
9867b6ab19dSGarrett Wollman 
9877b6ab19dSGarrett Wollman 			/* It is bad if input or output is not working.
9887b6ab19dSGarrett Wollman 			 * Require presistent problems before marking it dead.
9897b6ab19dSGarrett Wollman 			 */
9907b6ab19dSGarrett Wollman 			if ((in <= ierr && ierr > 0)
9917b6ab19dSGarrett Wollman 			    || (out <= oerr && oerr > 0)) {
9927b6ab19dSGarrett Wollman 				if (!(ifp->int_state & IS_SICK)) {
9937b6ab19dSGarrett Wollman 					trace_act("interface %s to %s"
9947b6ab19dSGarrett Wollman 						  " sick: in=%d ierr=%d"
995d5b718b3SGarrett Wollman 						  " out=%d oerr=%d",
9967b6ab19dSGarrett Wollman 						  ifp->int_name,
997d5b718b3SGarrett Wollman 						  naddr_ntoa(ifp->int_dstaddr),
9987b6ab19dSGarrett Wollman 						  in, ierr, out, oerr);
9997b6ab19dSGarrett Wollman 					if_sick(ifp);
10007b6ab19dSGarrett Wollman 					continue;
10017b6ab19dSGarrett Wollman 				}
10027b6ab19dSGarrett Wollman 				if (!(ifp->int_state & IS_BROKE)) {
1003d5b718b3SGarrett Wollman 					msglog("interface %s to %s broken:"
10047b6ab19dSGarrett Wollman 					       " in=%d ierr=%d out=%d oerr=%d",
10057b6ab19dSGarrett Wollman 					       ifp->int_name,
1006d5b718b3SGarrett Wollman 					       naddr_ntoa(ifp->int_dstaddr),
10077b6ab19dSGarrett Wollman 					       in, ierr, out, oerr);
10087b6ab19dSGarrett Wollman 					if_bad(ifp);
10097b6ab19dSGarrett Wollman 				}
10107b6ab19dSGarrett Wollman 				continue;
10117b6ab19dSGarrett Wollman 			}
10127b6ab19dSGarrett Wollman 
10137b6ab19dSGarrett Wollman 			/* otherwise, it is active and healthy
10147b6ab19dSGarrett Wollman 			 */
10157b6ab19dSGarrett Wollman 			ifp->int_act_time = now.tv_sec;
10167b6ab19dSGarrett Wollman 			(void)if_ok(ifp, "");
10177b6ab19dSGarrett Wollman 			continue;
10187b6ab19dSGarrett Wollman 		}
10197b6ab19dSGarrett Wollman 
10207b6ab19dSGarrett Wollman 		/* This is a new interface.
10217b6ab19dSGarrett Wollman 		 * If it is dead, forget it.
10227b6ab19dSGarrett Wollman 		 */
10232c7a9781SMark Murray 		if (!iff_up(ifs.int_if_flags))
10247b6ab19dSGarrett Wollman 			continue;
10257b6ab19dSGarrett Wollman 
1026d5b718b3SGarrett Wollman 		/* If it duplicates an existing interface,
1027d5b718b3SGarrett Wollman 		 * complain about it, mark the other one
10287b6ab19dSGarrett Wollman 		 * duplicated, and forget this one.
10297b6ab19dSGarrett Wollman 		 */
1030d5b718b3SGarrett Wollman 		ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1031d5b718b3SGarrett Wollman 				ifs.int_if_flags);
1032562c5a82SPedro F. Giffuni 		if (ifp != NULL) {
10332c7a9781SMark Murray 			/* Ignore duplicates of itself, caused by having
10342c7a9781SMark Murray 			 * IP aliases on the same network.
10352c7a9781SMark Murray 			 */
10362c7a9781SMark Murray 			if (!strcmp(ifp->int_name, ifs.int_name))
10372c7a9781SMark Murray 				continue;
10382c7a9781SMark Murray 
10397b6ab19dSGarrett Wollman 			if (!(prev_complaints & COMP_DUP)) {
10407b6ab19dSGarrett Wollman 				complaints |= COMP_DUP;
104171965874SGarrett Wollman 				msglog("%s (%s%s%s) is duplicated by"
104271965874SGarrett Wollman 				       " %s (%s%s%s)",
104371965874SGarrett Wollman 				       ifs.int_name,
104471965874SGarrett Wollman 				       addrname(ifs.int_addr,ifs.int_mask,1),
104571965874SGarrett Wollman 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
104671965874SGarrett Wollman 					? "-->" : ""),
104771965874SGarrett Wollman 				       ((ifs.int_if_flags & IFF_POINTOPOINT)
104871965874SGarrett Wollman 					? naddr_ntoa(ifs.int_dstaddr) : ""),
104971965874SGarrett Wollman 				       ifp->int_name,
105071965874SGarrett Wollman 				       addrname(ifp->int_addr,ifp->int_mask,1),
105171965874SGarrett Wollman 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
105271965874SGarrett Wollman 					? "-->" : ""),
105371965874SGarrett Wollman 				       ((ifp->int_if_flags & IFF_POINTOPOINT)
105471965874SGarrett Wollman 					? naddr_ntoa(ifp->int_dstaddr) : ""));
10557b6ab19dSGarrett Wollman 			}
10567b6ab19dSGarrett Wollman 			ifp->int_state |= IS_DUP;
10577b6ab19dSGarrett Wollman 			continue;
1058d5b718b3SGarrett Wollman 		}
10597b6ab19dSGarrett Wollman 
1060e53915c9SPoul-Henning Kamp 		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
1061d5b718b3SGarrett Wollman 			trace_act("%s is neither broadcast, point-to-point,"
1062d5b718b3SGarrett Wollman 				  " nor loopback",
1063d5b718b3SGarrett Wollman 				  ifs.int_name);
1064d5b718b3SGarrett Wollman 			if (!(ifs.int_state & IFF_MULTICAST))
1065d5b718b3SGarrett Wollman 				ifs.int_state |= IS_NO_RDISC;
1066d5b718b3SGarrett Wollman 		}
10677b6ab19dSGarrett Wollman 
1068d5b718b3SGarrett Wollman 
1069d5b718b3SGarrett Wollman 		/* It is new and ok.   Add it to the list of interfaces
10707b6ab19dSGarrett Wollman 		 */
10712c7a9781SMark Murray 		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
10722c7a9781SMark Murray 		memcpy(ifp, &ifs, sizeof(*ifp));
1073d5b718b3SGarrett Wollman 		get_parms(ifp);
1074d5b718b3SGarrett Wollman 		if_link(ifp);
10757b6ab19dSGarrett Wollman 		trace_if("Add", ifp);
10767b6ab19dSGarrett Wollman 
10777b6ab19dSGarrett Wollman 		/* Notice likely bad netmask.
10787b6ab19dSGarrett Wollman 		 */
10797b6ab19dSGarrett Wollman 		if (!(prev_complaints & COMP_NETMASK)
1080d5b718b3SGarrett Wollman 		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
1081d5b718b3SGarrett Wollman 		    && ifp->int_addr != RIP_DEFAULT) {
108238ae6523SPoul-Henning Kamp 			LIST_FOREACH(ifp1, &ifnet, int_list) {
10837b6ab19dSGarrett Wollman 				if (ifp1->int_mask == ifp->int_mask)
10847b6ab19dSGarrett Wollman 					continue;
10857b6ab19dSGarrett Wollman 				if (ifp1->int_if_flags & IFF_POINTOPOINT)
10867b6ab19dSGarrett Wollman 					continue;
1087d5b718b3SGarrett Wollman 				if (ifp1->int_dstaddr == RIP_DEFAULT)
1088d5b718b3SGarrett Wollman 					continue;
10898b336df1SSheldon Hearn 				/* ignore aliases on the right network */
10908b336df1SSheldon Hearn 				if (!strcmp(ifp->int_name, ifp1->int_name))
10918b336df1SSheldon Hearn 					continue;
1092d5b718b3SGarrett Wollman 				if (on_net(ifp->int_dstaddr,
10937b6ab19dSGarrett Wollman 					   ifp1->int_net, ifp1->int_mask)
1094d5b718b3SGarrett Wollman 				    || on_net(ifp1->int_dstaddr,
10957b6ab19dSGarrett Wollman 					      ifp->int_net, ifp->int_mask)) {
10967b6ab19dSGarrett Wollman 					msglog("possible netmask problem"
1097d5b718b3SGarrett Wollman 					       " between %s:%s and %s:%s",
10987b6ab19dSGarrett Wollman 					       ifp->int_name,
10997b6ab19dSGarrett Wollman 					       addrname(htonl(ifp->int_net),
11007b6ab19dSGarrett Wollman 							ifp->int_mask, 1),
11017b6ab19dSGarrett Wollman 					       ifp1->int_name,
11027b6ab19dSGarrett Wollman 					       addrname(htonl(ifp1->int_net),
11037b6ab19dSGarrett Wollman 							ifp1->int_mask, 1));
11047b6ab19dSGarrett Wollman 					complaints |= COMP_NETMASK;
11057b6ab19dSGarrett Wollman 				}
11067b6ab19dSGarrett Wollman 			}
11077b6ab19dSGarrett Wollman 		}
11087b6ab19dSGarrett Wollman 
1109d5b718b3SGarrett Wollman 		if (!(ifp->int_state & IS_ALIAS)) {
11107b6ab19dSGarrett Wollman 			/* Count the # of directly connected networks.
11117b6ab19dSGarrett Wollman 			 */
11127b6ab19dSGarrett Wollman 			if (!(ifp->int_if_flags & IFF_LOOPBACK))
11137b6ab19dSGarrett Wollman 				tot_interfaces++;
11147b6ab19dSGarrett Wollman 			if (!IS_RIP_OFF(ifp->int_state))
11157b6ab19dSGarrett Wollman 				rip_interfaces++;
11167b6ab19dSGarrett Wollman 
1117d5b718b3SGarrett Wollman 			/* turn on router discovery and RIP If needed */
11187b6ab19dSGarrett Wollman 			if_ok_rdisc(ifp);
11197b6ab19dSGarrett Wollman 			rip_on(ifp);
11207b6ab19dSGarrett Wollman 		}
1121d5b718b3SGarrett Wollman 	}
11227b6ab19dSGarrett Wollman 
1123d5b718b3SGarrett Wollman 	/* If we are multi-homed and have at least two interfaces
11247b6ab19dSGarrett Wollman 	 * listening to RIP, then output by default.
11257b6ab19dSGarrett Wollman 	 */
11267b6ab19dSGarrett Wollman 	if (!supplier_set && rip_interfaces > 1)
11277b6ab19dSGarrett Wollman 		set_supplier();
11287b6ab19dSGarrett Wollman 
11297b6ab19dSGarrett Wollman 	/* If we are multi-homed, optionally advertise a route to
11307b6ab19dSGarrett Wollman 	 * our main address.
11317b6ab19dSGarrett Wollman 	 */
11320b279f8cSPedro F. Giffuni 	if ((advertise_mhome && ifp)
11337b6ab19dSGarrett Wollman 	    || (tot_interfaces > 1
11347b6ab19dSGarrett Wollman 		&& mhome
1135562c5a82SPedro F. Giffuni 		&& (ifp = ifwithaddr(myaddr, 0, 0)) != NULL
11367b6ab19dSGarrett Wollman 		&& foundloopback)) {
11377b6ab19dSGarrett Wollman 		advertise_mhome = 1;
11387b6ab19dSGarrett Wollman 		rt = rtget(myaddr, HOST_MASK);
1139562c5a82SPedro F. Giffuni 		if (rt != NULL) {
11407b6ab19dSGarrett Wollman 			if (rt->rt_ifp != ifp
11417b6ab19dSGarrett Wollman 			    || rt->rt_router != loopaddr) {
11427b6ab19dSGarrett Wollman 				rtdelete(rt);
1143562c5a82SPedro F. Giffuni 				rt = NULL;
11447b6ab19dSGarrett Wollman 			} else {
11452c7a9781SMark Murray 				loop_rts.rts_ifp = ifp;
11462c7a9781SMark Murray 				loop_rts.rts_metric = 0;
11472c7a9781SMark Murray 				loop_rts.rts_time = rt->rt_time;
11487b6ab19dSGarrett Wollman 				rtchange(rt, rt->rt_state | RS_MHOME,
11492c7a9781SMark Murray 					 &loop_rts, 0);
11507b6ab19dSGarrett Wollman 			}
11517b6ab19dSGarrett Wollman 		}
1152562c5a82SPedro F. Giffuni 		if (rt == NULL) {
11532c7a9781SMark Murray 			loop_rts.rts_ifp = ifp;
11542c7a9781SMark Murray 			loop_rts.rts_metric = 0;
11552c7a9781SMark Murray 			rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
11562c7a9781SMark Murray 		}
11577b6ab19dSGarrett Wollman 	}
11587b6ab19dSGarrett Wollman 
115938ae6523SPoul-Henning Kamp 	LIST_FOREACH_SAFE(ifp, &ifnet, int_list, ifp1) {
11607b6ab19dSGarrett Wollman 		/* Forget any interfaces that have disappeared.
11617b6ab19dSGarrett Wollman 		 */
11627b6ab19dSGarrett Wollman 		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1163d5b718b3SGarrett Wollman 			trace_act("interface %s has disappeared",
11647b6ab19dSGarrett Wollman 				  ifp->int_name);
11657b6ab19dSGarrett Wollman 			ifdel(ifp);
11667b6ab19dSGarrett Wollman 			continue;
11677b6ab19dSGarrett Wollman 		}
11687b6ab19dSGarrett Wollman 
11697b6ab19dSGarrett Wollman 		if ((ifp->int_state & IS_BROKE)
11707b6ab19dSGarrett Wollman 		    && !(ifp->int_state & IS_PASSIVE))
11717b6ab19dSGarrett Wollman 			LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
11727b6ab19dSGarrett Wollman 
11737b6ab19dSGarrett Wollman 		/* If we ever have a RIPv1 interface, assume we always will.
11747b6ab19dSGarrett Wollman 		 * It might come back if it ever goes away.
11757b6ab19dSGarrett Wollman 		 */
11767b6ab19dSGarrett Wollman 		if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
11777b6ab19dSGarrett Wollman 			have_ripv1_out = 1;
11787b6ab19dSGarrett Wollman 		if (!(ifp->int_state & IS_NO_RIPV1_IN))
11797b6ab19dSGarrett Wollman 			have_ripv1_in = 1;
11807b6ab19dSGarrett Wollman 	}
11817b6ab19dSGarrett Wollman 
118238ae6523SPoul-Henning Kamp 	LIST_FOREACH(ifp, &ifnet, int_list) {
11837b6ab19dSGarrett Wollman 		/* Ensure there is always a network route for interfaces,
11847b6ab19dSGarrett Wollman 		 * after any dead interfaces have been deleted, which
11857b6ab19dSGarrett Wollman 		 * might affect routes for point-to-point links.
11867b6ab19dSGarrett Wollman 		 */
1187d5b718b3SGarrett Wollman 		if (!addrouteforif(ifp))
1188d5b718b3SGarrett Wollman 			continue;
11897b6ab19dSGarrett Wollman 
11907b6ab19dSGarrett Wollman 		/* Add routes to the local end of point-to-point interfaces
11917b6ab19dSGarrett Wollman 		 * using loopback.
11927b6ab19dSGarrett Wollman 		 */
11937b6ab19dSGarrett Wollman 		if ((ifp->int_if_flags & IFF_POINTOPOINT)
11947b6ab19dSGarrett Wollman 		    && !(ifp->int_state & IS_REMOTE)
11957b6ab19dSGarrett Wollman 		    && foundloopback) {
11967b6ab19dSGarrett Wollman 			/* Delete any routes to the network address through
11977b6ab19dSGarrett Wollman 			 * foreign routers. Remove even static routes.
11987b6ab19dSGarrett Wollman 			 */
11992c7a9781SMark Murray 			del_static(ifp->int_addr, HOST_MASK, 0, 0);
12007b6ab19dSGarrett Wollman 			rt = rtget(ifp->int_addr, HOST_MASK);
1201562c5a82SPedro F. Giffuni 			if (rt != NULL && rt->rt_router != loopaddr) {
12027b6ab19dSGarrett Wollman 				rtdelete(rt);
1203562c5a82SPedro F. Giffuni 				rt = NULL;
12047b6ab19dSGarrett Wollman 			}
1205562c5a82SPedro F. Giffuni 			if (rt != NULL) {
12067b6ab19dSGarrett Wollman 				if (!(rt->rt_state & RS_LOCAL)
12077b6ab19dSGarrett Wollman 				    || rt->rt_metric > ifp->int_metric) {
12087b6ab19dSGarrett Wollman 					ifp1 = ifp;
12097b6ab19dSGarrett Wollman 				} else {
12107b6ab19dSGarrett Wollman 					ifp1 = rt->rt_ifp;
12117b6ab19dSGarrett Wollman 				}
12122c7a9781SMark Murray 				loop_rts.rts_ifp = ifp1;
12132c7a9781SMark Murray 				loop_rts.rts_metric = 0;
12142c7a9781SMark Murray 				loop_rts.rts_time = rt->rt_time;
12157b6ab19dSGarrett Wollman 				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
12167b6ab19dSGarrett Wollman 					      | (RS_IF|RS_LOCAL)),
12172c7a9781SMark Murray 					 &loop_rts, 0);
12187b6ab19dSGarrett Wollman 			} else {
12192c7a9781SMark Murray 				loop_rts.rts_ifp = ifp;
12202c7a9781SMark Murray 				loop_rts.rts_metric = 0;
12217b6ab19dSGarrett Wollman 				rtadd(ifp->int_addr, HOST_MASK,
12222c7a9781SMark Murray 				      (RS_IF | RS_LOCAL), &loop_rts);
12237b6ab19dSGarrett Wollman 			}
12247b6ab19dSGarrett Wollman 		}
12257b6ab19dSGarrett Wollman 	}
12267b6ab19dSGarrett Wollman 
12277b6ab19dSGarrett Wollman 	/* add the authority routes */
1228562c5a82SPedro F. Giffuni 	for (intnetp = intnets; intnetp != NULL;
1229562c5a82SPedro F. Giffuni 	    intnetp = intnetp->intnet_next) {
12307b6ab19dSGarrett Wollman 		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1231562c5a82SPedro F. Giffuni 		if (rt != NULL
12327b6ab19dSGarrett Wollman 		    && !(rt->rt_state & RS_NO_NET_SYN)
12337b6ab19dSGarrett Wollman 		    && !(rt->rt_state & RS_NET_INT)) {
12347b6ab19dSGarrett Wollman 			rtdelete(rt);
1235562c5a82SPedro F. Giffuni 			rt = NULL;
12367b6ab19dSGarrett Wollman 		}
1237562c5a82SPedro F. Giffuni 		if (rt == NULL) {
1238562c5a82SPedro F. Giffuni 			loop_rts.rts_ifp = NULL;
12392c7a9781SMark Murray 			loop_rts.rts_metric = intnetp->intnet_metric-1;
12407b6ab19dSGarrett Wollman 			rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
12412c7a9781SMark Murray 			      RS_NET_SYN | RS_NET_INT, &loop_rts);
12422c7a9781SMark Murray 		}
12437b6ab19dSGarrett Wollman 	}
12447b6ab19dSGarrett Wollman 
12457b6ab19dSGarrett Wollman 	prev_complaints = complaints;
12467b6ab19dSGarrett Wollman }
12477b6ab19dSGarrett Wollman 
12487b6ab19dSGarrett Wollman 
12497b6ab19dSGarrett Wollman static void
check_net_syn(struct interface * ifp)12507b6ab19dSGarrett Wollman check_net_syn(struct interface *ifp)
12517b6ab19dSGarrett Wollman {
12527b6ab19dSGarrett Wollman 	struct rt_entry *rt;
12532c7a9781SMark Murray 	static struct rt_spare new;
12547b6ab19dSGarrett Wollman 
12557b6ab19dSGarrett Wollman 
12567b6ab19dSGarrett Wollman 	/* Turn on the need to automatically synthesize a network route
12577b6ab19dSGarrett Wollman 	 * for this interface only if we are running RIPv1 on some other
12587b6ab19dSGarrett Wollman 	 * interface that is on a different class-A,B,or C network.
12597b6ab19dSGarrett Wollman 	 */
12607b6ab19dSGarrett Wollman 	if (have_ripv1_out || have_ripv1_in) {
12617b6ab19dSGarrett Wollman 		ifp->int_state |= IS_NEED_NET_SYN;
12627b6ab19dSGarrett Wollman 		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1263562c5a82SPedro F. Giffuni 		if (rt != NULL
12647b6ab19dSGarrett Wollman 		    && 0 == (rt->rt_state & RS_NO_NET_SYN)
12657b6ab19dSGarrett Wollman 		    && (!(rt->rt_state & RS_NET_SYN)
12667b6ab19dSGarrett Wollman 			|| rt->rt_metric > ifp->int_metric)) {
12677b6ab19dSGarrett Wollman 			rtdelete(rt);
1268562c5a82SPedro F. Giffuni 			rt = NULL;
12697b6ab19dSGarrett Wollman 		}
1270562c5a82SPedro F. Giffuni 		if (rt == NULL) {
12712c7a9781SMark Murray 			new.rts_ifp = ifp;
12722c7a9781SMark Murray 			new.rts_gate = ifp->int_addr;
12732c7a9781SMark Murray 			new.rts_router = ifp->int_addr;
12742c7a9781SMark Murray 			new.rts_metric = ifp->int_metric;
12757b6ab19dSGarrett Wollman 			rtadd(ifp->int_std_addr, ifp->int_std_mask,
12762c7a9781SMark Murray 			      RS_NET_SYN, &new);
12772c7a9781SMark Murray 		}
12787b6ab19dSGarrett Wollman 
12797b6ab19dSGarrett Wollman 	} else {
12807b6ab19dSGarrett Wollman 		ifp->int_state &= ~IS_NEED_NET_SYN;
12817b6ab19dSGarrett Wollman 
12827b6ab19dSGarrett Wollman 		rt = rtget(ifp->int_std_addr,
12837b6ab19dSGarrett Wollman 			   ifp->int_std_mask);
1284562c5a82SPedro F. Giffuni 		if (rt != NULL
12857b6ab19dSGarrett Wollman 		    && (rt->rt_state & RS_NET_SYN)
12867b6ab19dSGarrett Wollman 		    && rt->rt_ifp == ifp)
12877b6ab19dSGarrett Wollman 			rtbad_sub(rt);
12887b6ab19dSGarrett Wollman 	}
12897b6ab19dSGarrett Wollman }
12907b6ab19dSGarrett Wollman 
12917b6ab19dSGarrett Wollman 
12927b6ab19dSGarrett Wollman /* Add route for interface if not currently installed.
12937b6ab19dSGarrett Wollman  * Create route to other end if a point-to-point link,
12947b6ab19dSGarrett Wollman  * otherwise a route to this (sub)network.
12957b6ab19dSGarrett Wollman  */
1296f2ca6d8aSPoul-Henning Kamp static int					/* 0=bad interface */
addrouteforif(struct interface * ifp)12977b6ab19dSGarrett Wollman addrouteforif(struct interface *ifp)
12987b6ab19dSGarrett Wollman {
12997b6ab19dSGarrett Wollman 	struct rt_entry *rt;
13002c7a9781SMark Murray 	static struct rt_spare new;
13012c7a9781SMark Murray 	naddr dst;
13027b6ab19dSGarrett Wollman 
13037b6ab19dSGarrett Wollman 
13047b6ab19dSGarrett Wollman 	/* skip sick interfaces
13057b6ab19dSGarrett Wollman 	 */
13067b6ab19dSGarrett Wollman 	if (ifp->int_state & IS_BROKE)
1307d5b718b3SGarrett Wollman 		return 0;
13087b6ab19dSGarrett Wollman 
13097b6ab19dSGarrett Wollman 	/* If the interface on a subnet, then install a RIPv1 route to
13107b6ab19dSGarrett Wollman 	 * the network as well (unless it is sick).
13117b6ab19dSGarrett Wollman 	 */
13127b6ab19dSGarrett Wollman 	if (ifp->int_state & IS_SUBNET)
13137b6ab19dSGarrett Wollman 		check_net_syn(ifp);
13147b6ab19dSGarrett Wollman 
1315d5b718b3SGarrett Wollman 	dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1316d5b718b3SGarrett Wollman 	       ? ifp->int_dstaddr
1317d5b718b3SGarrett Wollman 	       : htonl(ifp->int_net));
1318d5b718b3SGarrett Wollman 
13192c7a9781SMark Murray 	new.rts_ifp = ifp;
13202c7a9781SMark Murray 	new.rts_router = ifp->int_addr;
13212c7a9781SMark Murray 	new.rts_gate = ifp->int_addr;
13222c7a9781SMark Murray 	new.rts_metric = ifp->int_metric;
13232c7a9781SMark Murray 	new.rts_time = now.tv_sec;
13242c7a9781SMark Murray 
13257b6ab19dSGarrett Wollman 	/* If we are going to send packets to the gateway,
13267b6ab19dSGarrett Wollman 	 * it must be reachable using our physical interfaces
13277b6ab19dSGarrett Wollman 	 */
1328d5b718b3SGarrett Wollman 	if ((ifp->int_state & IS_REMOTE)
132971965874SGarrett Wollman 	    && !(ifp->int_state & IS_EXTERNAL)
1330d5b718b3SGarrett Wollman 	    && !check_remote(ifp))
1331d5b718b3SGarrett Wollman 		return 0;
13327b6ab19dSGarrett Wollman 
13337b6ab19dSGarrett Wollman 	/* We are finished if the correct main interface route exists.
13347b6ab19dSGarrett Wollman 	 * The right route must be for the right interface, not synthesized
13357b6ab19dSGarrett Wollman 	 * from a subnet, be a "gateway" or not as appropriate, and so forth.
13367b6ab19dSGarrett Wollman 	 */
13372c7a9781SMark Murray 	del_static(dst, ifp->int_mask, 0, 0);
13387b6ab19dSGarrett Wollman 	rt = rtget(dst, ifp->int_mask);
1339562c5a82SPedro F. Giffuni 	if (rt != NULL) {
13407b6ab19dSGarrett Wollman 		if ((rt->rt_ifp != ifp
13417b6ab19dSGarrett Wollman 		     || rt->rt_router != ifp->int_addr)
13427b6ab19dSGarrett Wollman 		    && (!(ifp->int_state & IS_DUP)
13437b6ab19dSGarrett Wollman 			|| rt->rt_ifp == 0
13447b6ab19dSGarrett Wollman 			|| (rt->rt_ifp->int_state & IS_BROKE))) {
13457b6ab19dSGarrett Wollman 			rtdelete(rt);
1346562c5a82SPedro F. Giffuni 			rt = NULL;
13477b6ab19dSGarrett Wollman 		} else {
13487b6ab19dSGarrett Wollman 			rtchange(rt, ((rt->rt_state | RS_IF)
13497b6ab19dSGarrett Wollman 				      & ~(RS_NET_SYN | RS_LOCAL)),
13502c7a9781SMark Murray 				 &new, 0);
13517b6ab19dSGarrett Wollman 		}
13527b6ab19dSGarrett Wollman 	}
1353562c5a82SPedro F. Giffuni 	if (rt == NULL) {
13547b6ab19dSGarrett Wollman 		if (ifp->int_transitions++ > 0)
1355d5b718b3SGarrett Wollman 			trace_act("re-install interface %s",
13567b6ab19dSGarrett Wollman 				  ifp->int_name);
13577b6ab19dSGarrett Wollman 
13582c7a9781SMark Murray 		rtadd(dst, ifp->int_mask, RS_IF, &new);
13597b6ab19dSGarrett Wollman 	}
1360d5b718b3SGarrett Wollman 
1361d5b718b3SGarrett Wollman 	return 1;
13627b6ab19dSGarrett Wollman }
1363