1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1988, 1993
6*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
7*7c478bd9Sstevel@tonic-gate  *
8*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*7c478bd9Sstevel@tonic-gate  * are met:
11*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
12*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
13*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
14*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
15*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
16*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
17*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgment:
18*7c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
19*7c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
20*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
21*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
22*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
23*7c478bd9Sstevel@tonic-gate  *
24*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * $FreeBSD: src/sbin/routed/table.c,v 1.15 2000/08/11 08:24:38 sheldonh Exp $
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "defs.h"
42*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include <stropts.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
45*7c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
46*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /* This structure is used to store a disassembled routing socket message. */
49*7c478bd9Sstevel@tonic-gate struct rt_addrinfo {
50*7c478bd9Sstevel@tonic-gate 	int	rti_addrs;
51*7c478bd9Sstevel@tonic-gate 	struct sockaddr_storage *rti_info[RTAX_MAX];
52*7c478bd9Sstevel@tonic-gate };
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate static struct rt_spare *rts_better(struct rt_entry *);
55*7c478bd9Sstevel@tonic-gate static struct rt_spare rts_empty = EMPTY_RT_SPARE;
56*7c478bd9Sstevel@tonic-gate static void set_need_flash(void);
57*7c478bd9Sstevel@tonic-gate static void rtbad(struct rt_entry *, struct interface *);
58*7c478bd9Sstevel@tonic-gate static int rt_xaddrs(struct rt_addrinfo *, struct sockaddr_storage *,
59*7c478bd9Sstevel@tonic-gate     char *, int);
60*7c478bd9Sstevel@tonic-gate static struct interface *gwkludge_iflookup(in_addr_t, in_addr_t, in_addr_t);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate struct radix_node_head *rhead;		/* root of the radix tree */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /* Flash update needed.  _B_TRUE to suppress the 1st. */
65*7c478bd9Sstevel@tonic-gate boolean_t need_flash = _B_TRUE;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate struct timeval age_timer;		/* next check of old routes */
68*7c478bd9Sstevel@tonic-gate struct timeval need_kern = {		/* need to update kernel table */
69*7c478bd9Sstevel@tonic-gate 	EPOCH+MIN_WAITTIME-1, 0
70*7c478bd9Sstevel@tonic-gate };
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static uint32_t	total_routes;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
75*7c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*
78*7c478bd9Sstevel@tonic-gate  * It is desirable to "aggregate" routes, to combine differing routes of
79*7c478bd9Sstevel@tonic-gate  * the same metric and next hop into a common route with a smaller netmask
80*7c478bd9Sstevel@tonic-gate  * or to suppress redundant routes, routes that add no information to
81*7c478bd9Sstevel@tonic-gate  * routes with smaller netmasks.
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  * A route is redundant if and only if any and all routes with smaller
84*7c478bd9Sstevel@tonic-gate  * but matching netmasks and nets are the same.  Since routes are
85*7c478bd9Sstevel@tonic-gate  * kept sorted in the radix tree, redundant routes always come second.
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  * There are two kinds of aggregations.  First, two routes of the same bit
88*7c478bd9Sstevel@tonic-gate  * mask and differing only in the least significant bit of the network
89*7c478bd9Sstevel@tonic-gate  * number can be combined into a single route with a coarser mask.
90*7c478bd9Sstevel@tonic-gate  *
91*7c478bd9Sstevel@tonic-gate  * Second, a route can be suppressed in favor of another route with a more
92*7c478bd9Sstevel@tonic-gate  * coarse mask provided no incompatible routes with intermediate masks
93*7c478bd9Sstevel@tonic-gate  * are present.  The second kind of aggregation involves suppressing routes.
94*7c478bd9Sstevel@tonic-gate  * A route must not be suppressed if an incompatible route exists with
95*7c478bd9Sstevel@tonic-gate  * an intermediate mask, since the suppressed route would be covered
96*7c478bd9Sstevel@tonic-gate  * by the intermediate.
97*7c478bd9Sstevel@tonic-gate  *
98*7c478bd9Sstevel@tonic-gate  * This code relies on the radix tree walk encountering routes
99*7c478bd9Sstevel@tonic-gate  * sorted first by address, with the smallest address first.
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest,
103*7c478bd9Sstevel@tonic-gate 	*ag_finest;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_AG
106*7c478bd9Sstevel@tonic-gate #define	CHECK_AG() do { int acnt = 0; struct ag_info *cag;	\
107*7c478bd9Sstevel@tonic-gate 	for (cag = ag_avail; cag != NULL; cag = cag->ag_fine)	\
108*7c478bd9Sstevel@tonic-gate 		acnt++;						\
109*7c478bd9Sstevel@tonic-gate 	for (cag = ag_corsest; cag != NULL; cag = cag->ag_fine)	\
110*7c478bd9Sstevel@tonic-gate 		acnt++;						\
111*7c478bd9Sstevel@tonic-gate 	if (acnt != NUM_AG_SLOTS)				\
112*7c478bd9Sstevel@tonic-gate 		abort();					\
113*7c478bd9Sstevel@tonic-gate } while (_B_FALSE)
114*7c478bd9Sstevel@tonic-gate #else
115*7c478bd9Sstevel@tonic-gate #define	CHECK_AG()	(void)0
116*7c478bd9Sstevel@tonic-gate #endif
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate /*
120*7c478bd9Sstevel@tonic-gate  * Output the contents of an aggregation table slot.
121*7c478bd9Sstevel@tonic-gate  *	This function must always be immediately followed with the deletion
122*7c478bd9Sstevel@tonic-gate  *	of the target slot.
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate static void
125*7c478bd9Sstevel@tonic-gate ag_out(struct ag_info *ag, void (*out)(struct ag_info *))
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	struct ag_info *ag_cors;
128*7c478bd9Sstevel@tonic-gate 	uint32_t bit;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	/* Forget it if this route should not be output for split-horizon. */
132*7c478bd9Sstevel@tonic-gate 	if (ag->ag_state & AGS_SPLIT_HZ)
133*7c478bd9Sstevel@tonic-gate 		return;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	/*
136*7c478bd9Sstevel@tonic-gate 	 * If we output both the even and odd twins, then the immediate parent,
137*7c478bd9Sstevel@tonic-gate 	 * if it is present, is redundant, unless the parent manages to
138*7c478bd9Sstevel@tonic-gate 	 * aggregate into something coarser.
139*7c478bd9Sstevel@tonic-gate 	 * On successive calls, this code detects the even and odd twins,
140*7c478bd9Sstevel@tonic-gate 	 * and marks the parent.
141*7c478bd9Sstevel@tonic-gate 	 *
142*7c478bd9Sstevel@tonic-gate 	 * Note that the order in which the radix tree code emits routes
143*7c478bd9Sstevel@tonic-gate 	 * ensures that the twins are seen before the parent is emitted.
144*7c478bd9Sstevel@tonic-gate 	 */
145*7c478bd9Sstevel@tonic-gate 	ag_cors = ag->ag_cors;
146*7c478bd9Sstevel@tonic-gate 	if (ag_cors != NULL &&
147*7c478bd9Sstevel@tonic-gate 	    ag_cors->ag_mask == (ag->ag_mask << 1) &&
148*7c478bd9Sstevel@tonic-gate 	    ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) {
149*7c478bd9Sstevel@tonic-gate 		ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h) ?
150*7c478bd9Sstevel@tonic-gate 		    AGS_REDUN0 : AGS_REDUN1);
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/*
154*7c478bd9Sstevel@tonic-gate 	 * Skip it if this route is itself redundant.
155*7c478bd9Sstevel@tonic-gate 	 *
156*7c478bd9Sstevel@tonic-gate 	 * It is ok to change the contents of the slot here, since it is
157*7c478bd9Sstevel@tonic-gate 	 * always deleted next.
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate 	if (ag->ag_state & AGS_REDUN0) {
160*7c478bd9Sstevel@tonic-gate 		if (ag->ag_state & AGS_REDUN1)
161*7c478bd9Sstevel@tonic-gate 			return;		/* quit if fully redundant */
162*7c478bd9Sstevel@tonic-gate 		/* make it finer if it is half-redundant */
163*7c478bd9Sstevel@tonic-gate 		bit = (-ag->ag_mask) >> 1;
164*7c478bd9Sstevel@tonic-gate 		ag->ag_dst_h |= bit;
165*7c478bd9Sstevel@tonic-gate 		ag->ag_mask |= bit;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	} else if (ag->ag_state & AGS_REDUN1) {
168*7c478bd9Sstevel@tonic-gate 		/* make it finer if it is half-redundant */
169*7c478bd9Sstevel@tonic-gate 		bit = (-ag->ag_mask) >> 1;
170*7c478bd9Sstevel@tonic-gate 		ag->ag_mask |= bit;
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	out(ag);
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate static void
177*7c478bd9Sstevel@tonic-gate ag_del(struct ag_info *ag)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	CHECK_AG();
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	if (ag->ag_cors == NULL)
182*7c478bd9Sstevel@tonic-gate 		ag_corsest = ag->ag_fine;
183*7c478bd9Sstevel@tonic-gate 	else
184*7c478bd9Sstevel@tonic-gate 		ag->ag_cors->ag_fine = ag->ag_fine;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	if (ag->ag_fine == NULL)
187*7c478bd9Sstevel@tonic-gate 		ag_finest = ag->ag_cors;
188*7c478bd9Sstevel@tonic-gate 	else
189*7c478bd9Sstevel@tonic-gate 		ag->ag_fine->ag_cors = ag->ag_cors;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	ag->ag_fine = ag_avail;
192*7c478bd9Sstevel@tonic-gate 	ag_avail = ag;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	CHECK_AG();
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate /* Look for a route that can suppress the given route. */
199*7c478bd9Sstevel@tonic-gate static struct ag_info *
200*7c478bd9Sstevel@tonic-gate ag_find_suppressor(struct ag_info *ag)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	struct ag_info *ag_cors;
203*7c478bd9Sstevel@tonic-gate 	in_addr_t dst_h = ag->ag_dst_h;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	for (ag_cors = ag->ag_cors; ag_cors != NULL;
206*7c478bd9Sstevel@tonic-gate 	    ag_cors = ag_cors->ag_cors) {
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 		if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) {
209*7c478bd9Sstevel@tonic-gate 			/*
210*7c478bd9Sstevel@tonic-gate 			 * We found a route with a coarser mask that covers
211*7c478bd9Sstevel@tonic-gate 			 * the given target.  It can suppress the target
212*7c478bd9Sstevel@tonic-gate 			 * only if it has a good enough metric and it
213*7c478bd9Sstevel@tonic-gate 			 * either has the same (gateway, ifp), or if its state
214*7c478bd9Sstevel@tonic-gate 			 * includes AGS_CORS_GATE or the target's state
215*7c478bd9Sstevel@tonic-gate 			 * includes AGS_FINE_GATE.
216*7c478bd9Sstevel@tonic-gate 			 */
217*7c478bd9Sstevel@tonic-gate 			if (ag_cors->ag_pref <= ag->ag_pref &&
218*7c478bd9Sstevel@tonic-gate 			    (((ag->ag_nhop == ag_cors->ag_nhop) &&
219*7c478bd9Sstevel@tonic-gate 			    (ag->ag_ifp == ag_cors->ag_ifp)) ||
220*7c478bd9Sstevel@tonic-gate 			    ag_cors->ag_state & AGS_CORS_GATE ||
221*7c478bd9Sstevel@tonic-gate 			    ag->ag_state & AGS_FINE_GATE)) {
222*7c478bd9Sstevel@tonic-gate 				return (ag_cors);
223*7c478bd9Sstevel@tonic-gate 			}
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	return (NULL);
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate /*
232*7c478bd9Sstevel@tonic-gate  * Flush routes waiting for aggregation.
233*7c478bd9Sstevel@tonic-gate  * This must not suppress a route unless it is known that among all routes
234*7c478bd9Sstevel@tonic-gate  * with coarser masks that match it, the one with the longest mask is
235*7c478bd9Sstevel@tonic-gate  * appropriate.  This is ensured by scanning the routes in lexical order,
236*7c478bd9Sstevel@tonic-gate  * and with the most restrictive mask first among routes to the same
237*7c478bd9Sstevel@tonic-gate  * destination.
238*7c478bd9Sstevel@tonic-gate  */
239*7c478bd9Sstevel@tonic-gate void
240*7c478bd9Sstevel@tonic-gate ag_flush(in_addr_t lim_dst_h,	/* flush routes to here */
241*7c478bd9Sstevel@tonic-gate     in_addr_t lim_mask,		/* matching this mask */
242*7c478bd9Sstevel@tonic-gate     void (*out)(struct ag_info *))
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	struct ag_info *ag, *ag_cors, *ag_supr;
245*7c478bd9Sstevel@tonic-gate 	in_addr_t dst_h;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	for (ag = ag_finest; ag != NULL && ag->ag_mask >= lim_mask;
249*7c478bd9Sstevel@tonic-gate 	    ag = ag_cors) {
250*7c478bd9Sstevel@tonic-gate 		/* Get the next route now, before we delete ag. */
251*7c478bd9Sstevel@tonic-gate 		ag_cors = ag->ag_cors;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 		/* Work on only the specified routes. */
254*7c478bd9Sstevel@tonic-gate 		dst_h = ag->ag_dst_h;
255*7c478bd9Sstevel@tonic-gate 		if ((dst_h & lim_mask) != lim_dst_h)
256*7c478bd9Sstevel@tonic-gate 			continue;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 		/*
259*7c478bd9Sstevel@tonic-gate 		 * Don't try to suppress the route if its state doesn't
260*7c478bd9Sstevel@tonic-gate 		 * include AGS_SUPPRESS.
261*7c478bd9Sstevel@tonic-gate 		 */
262*7c478bd9Sstevel@tonic-gate 		if (!(ag->ag_state & AGS_SUPPRESS)) {
263*7c478bd9Sstevel@tonic-gate 			ag_out(ag, out);
264*7c478bd9Sstevel@tonic-gate 			ag_del(ag);
265*7c478bd9Sstevel@tonic-gate 			continue;
266*7c478bd9Sstevel@tonic-gate 		}
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 		ag_supr = ag_find_suppressor(ag);
269*7c478bd9Sstevel@tonic-gate 		if (ag_supr == NULL) {
270*7c478bd9Sstevel@tonic-gate 			/*
271*7c478bd9Sstevel@tonic-gate 			 * We didn't find a route which suppresses the
272*7c478bd9Sstevel@tonic-gate 			 * target, so the target can go out.
273*7c478bd9Sstevel@tonic-gate 			 */
274*7c478bd9Sstevel@tonic-gate 			ag_out(ag, out);
275*7c478bd9Sstevel@tonic-gate 		} else {
276*7c478bd9Sstevel@tonic-gate 			/*
277*7c478bd9Sstevel@tonic-gate 			 * We found a route which suppresses the target, so
278*7c478bd9Sstevel@tonic-gate 			 * don't output the target.
279*7c478bd9Sstevel@tonic-gate 			 */
280*7c478bd9Sstevel@tonic-gate 			if (TRACEACTIONS) {
281*7c478bd9Sstevel@tonic-gate 				trace_misc("aggregated away %s",
282*7c478bd9Sstevel@tonic-gate 				    rtname(htonl(ag->ag_dst_h), ag->ag_mask,
283*7c478bd9Sstevel@tonic-gate 				    ag->ag_nhop));
284*7c478bd9Sstevel@tonic-gate 				trace_misc("on coarser route %s",
285*7c478bd9Sstevel@tonic-gate 				    rtname(htonl(ag_supr->ag_dst_h),
286*7c478bd9Sstevel@tonic-gate 				    ag_supr->ag_mask, ag_supr->ag_nhop));
287*7c478bd9Sstevel@tonic-gate 			}
288*7c478bd9Sstevel@tonic-gate 			/*
289*7c478bd9Sstevel@tonic-gate 			 * If the suppressed target was redundant, then
290*7c478bd9Sstevel@tonic-gate 			 * mark the suppressor as redundant.
291*7c478bd9Sstevel@tonic-gate 			 */
292*7c478bd9Sstevel@tonic-gate 			if (AG_IS_REDUN(ag->ag_state) &&
293*7c478bd9Sstevel@tonic-gate 			    ag_supr->ag_mask == (ag->ag_mask<<1)) {
294*7c478bd9Sstevel@tonic-gate 				if (ag_supr->ag_dst_h == dst_h)
295*7c478bd9Sstevel@tonic-gate 					ag_supr->ag_state |= AGS_REDUN0;
296*7c478bd9Sstevel@tonic-gate 				else
297*7c478bd9Sstevel@tonic-gate 					ag_supr->ag_state |= AGS_REDUN1;
298*7c478bd9Sstevel@tonic-gate 			}
299*7c478bd9Sstevel@tonic-gate 			if (ag->ag_tag != ag_supr->ag_tag)
300*7c478bd9Sstevel@tonic-gate 				ag_supr->ag_tag = 0;
301*7c478bd9Sstevel@tonic-gate 			if (ag->ag_nhop != ag_supr->ag_nhop)
302*7c478bd9Sstevel@tonic-gate 				ag_supr->ag_nhop = 0;
303*7c478bd9Sstevel@tonic-gate 		}
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 		/* The route has either been output or suppressed */
306*7c478bd9Sstevel@tonic-gate 		ag_del(ag);
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	CHECK_AG();
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate /* Try to aggregate a route with previous routes. */
314*7c478bd9Sstevel@tonic-gate void
315*7c478bd9Sstevel@tonic-gate ag_check(in_addr_t dst,
316*7c478bd9Sstevel@tonic-gate     in_addr_t	mask,
317*7c478bd9Sstevel@tonic-gate     in_addr_t	gate,
318*7c478bd9Sstevel@tonic-gate     struct interface *ifp,
319*7c478bd9Sstevel@tonic-gate     in_addr_t	nhop,
320*7c478bd9Sstevel@tonic-gate     uint8_t	metric,
321*7c478bd9Sstevel@tonic-gate     uint8_t	pref,
322*7c478bd9Sstevel@tonic-gate     uint32_t	seqno,
323*7c478bd9Sstevel@tonic-gate     uint16_t	tag,
324*7c478bd9Sstevel@tonic-gate     uint16_t	state,
325*7c478bd9Sstevel@tonic-gate     void (*out)(struct ag_info *))	/* output using this */
326*7c478bd9Sstevel@tonic-gate {
327*7c478bd9Sstevel@tonic-gate 	struct ag_info *ag, *nag, *ag_cors;
328*7c478bd9Sstevel@tonic-gate 	in_addr_t xaddr;
329*7c478bd9Sstevel@tonic-gate 	int tmp;
330*7c478bd9Sstevel@tonic-gate 	struct interface *xifp;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	dst = ntohl(dst);
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	/*
335*7c478bd9Sstevel@tonic-gate 	 * Don't bother trying to aggregate routes with non-contiguous
336*7c478bd9Sstevel@tonic-gate 	 * subnet masks.
337*7c478bd9Sstevel@tonic-gate 	 *
338*7c478bd9Sstevel@tonic-gate 	 * (X & -X) contains a single bit if and only if X is a power of 2.
339*7c478bd9Sstevel@tonic-gate 	 * (X + (X & -X)) == 0 if and only if X is a power of 2.
340*7c478bd9Sstevel@tonic-gate 	 */
341*7c478bd9Sstevel@tonic-gate 	if ((mask & -mask) + mask != 0) {
342*7c478bd9Sstevel@tonic-gate 		struct ag_info nc_ag;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_dst_h = dst;
345*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_mask = mask;
346*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_gate = gate;
347*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_ifp = ifp;
348*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_nhop = nhop;
349*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_metric = metric;
350*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_pref = pref;
351*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_tag = tag;
352*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_state = state;
353*7c478bd9Sstevel@tonic-gate 		nc_ag.ag_seqno = seqno;
354*7c478bd9Sstevel@tonic-gate 		out(&nc_ag);
355*7c478bd9Sstevel@tonic-gate 		return;
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	/* Search for the right slot in the aggregation table. */
359*7c478bd9Sstevel@tonic-gate 	ag_cors = NULL;
360*7c478bd9Sstevel@tonic-gate 	ag = ag_corsest;
361*7c478bd9Sstevel@tonic-gate 	while (ag != NULL) {
362*7c478bd9Sstevel@tonic-gate 		if (ag->ag_mask >= mask)
363*7c478bd9Sstevel@tonic-gate 			break;
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 		/*
366*7c478bd9Sstevel@tonic-gate 		 * Suppress old routes (i.e. combine with compatible routes
367*7c478bd9Sstevel@tonic-gate 		 * with coarser masks) as we look for the right slot in the
368*7c478bd9Sstevel@tonic-gate 		 * aggregation table for the new route.
369*7c478bd9Sstevel@tonic-gate 		 * A route to an address less than the current destination
370*7c478bd9Sstevel@tonic-gate 		 * will not be affected by the current route or any route
371*7c478bd9Sstevel@tonic-gate 		 * seen hereafter.  That means it is safe to suppress it.
372*7c478bd9Sstevel@tonic-gate 		 * This check keeps poor routes (e.g. with large hop counts)
373*7c478bd9Sstevel@tonic-gate 		 * from preventing suppression of finer routes.
374*7c478bd9Sstevel@tonic-gate 		 */
375*7c478bd9Sstevel@tonic-gate 		if (ag_cors != NULL && ag->ag_dst_h < dst &&
376*7c478bd9Sstevel@tonic-gate 		    (ag->ag_state & AGS_SUPPRESS) &&
377*7c478bd9Sstevel@tonic-gate 		    ag_cors->ag_pref <= ag->ag_pref &&
378*7c478bd9Sstevel@tonic-gate 		    (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h &&
379*7c478bd9Sstevel@tonic-gate 		    ((ag_cors->ag_nhop == ag->ag_nhop &&
380*7c478bd9Sstevel@tonic-gate 		    (ag_cors->ag_ifp == ag->ag_ifp))||
381*7c478bd9Sstevel@tonic-gate 			(ag->ag_state & AGS_FINE_GATE) ||
382*7c478bd9Sstevel@tonic-gate 			(ag_cors->ag_state & AGS_CORS_GATE))) {
383*7c478bd9Sstevel@tonic-gate 			/*
384*7c478bd9Sstevel@tonic-gate 			 * If the suppressed target was redundant,
385*7c478bd9Sstevel@tonic-gate 			 * then mark the suppressor redundant.
386*7c478bd9Sstevel@tonic-gate 			 */
387*7c478bd9Sstevel@tonic-gate 			if (AG_IS_REDUN(ag->ag_state) &&
388*7c478bd9Sstevel@tonic-gate 			    ag_cors->ag_mask == (ag->ag_mask << 1)) {
389*7c478bd9Sstevel@tonic-gate 				if (ag_cors->ag_dst_h == dst)
390*7c478bd9Sstevel@tonic-gate 					ag_cors->ag_state |= AGS_REDUN0;
391*7c478bd9Sstevel@tonic-gate 				else
392*7c478bd9Sstevel@tonic-gate 					ag_cors->ag_state |= AGS_REDUN1;
393*7c478bd9Sstevel@tonic-gate 			}
394*7c478bd9Sstevel@tonic-gate 			if (ag->ag_tag != ag_cors->ag_tag)
395*7c478bd9Sstevel@tonic-gate 				ag_cors->ag_tag = 0;
396*7c478bd9Sstevel@tonic-gate 			if (ag->ag_nhop != ag_cors->ag_nhop)
397*7c478bd9Sstevel@tonic-gate 				ag_cors->ag_nhop = 0;
398*7c478bd9Sstevel@tonic-gate 			ag_del(ag);
399*7c478bd9Sstevel@tonic-gate 			CHECK_AG();
400*7c478bd9Sstevel@tonic-gate 		} else {
401*7c478bd9Sstevel@tonic-gate 			ag_cors = ag;
402*7c478bd9Sstevel@tonic-gate 		}
403*7c478bd9Sstevel@tonic-gate 		ag = ag_cors->ag_fine;
404*7c478bd9Sstevel@tonic-gate 	}
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 	/*
407*7c478bd9Sstevel@tonic-gate 	 * If we find the even/odd twin of the new route, and if the
408*7c478bd9Sstevel@tonic-gate 	 * masks and so forth are equal, we can aggregate them.
409*7c478bd9Sstevel@tonic-gate 	 * We can probably promote one of the pair.
410*7c478bd9Sstevel@tonic-gate 	 *
411*7c478bd9Sstevel@tonic-gate 	 * Since the routes are encountered in lexical order,
412*7c478bd9Sstevel@tonic-gate 	 * the new route must be odd.  However, the second or later
413*7c478bd9Sstevel@tonic-gate 	 * times around this loop, it could be the even twin promoted
414*7c478bd9Sstevel@tonic-gate 	 * from the even/odd pair of twins of the finer route.
415*7c478bd9Sstevel@tonic-gate 	 */
416*7c478bd9Sstevel@tonic-gate 	while (ag != NULL && ag->ag_mask == mask &&
417*7c478bd9Sstevel@tonic-gate 	    ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) {
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 		/*
420*7c478bd9Sstevel@tonic-gate 		 * Here we know the target route and the route in the current
421*7c478bd9Sstevel@tonic-gate 		 * slot have the same netmasks and differ by at most the
422*7c478bd9Sstevel@tonic-gate 		 * last bit.  They are either for the same destination, or
423*7c478bd9Sstevel@tonic-gate 		 * for an even/odd pair of destinations.
424*7c478bd9Sstevel@tonic-gate 		 */
425*7c478bd9Sstevel@tonic-gate 		if (ag->ag_dst_h == dst) {
426*7c478bd9Sstevel@tonic-gate 			if (ag->ag_nhop == nhop && ag->ag_ifp == ifp) {
427*7c478bd9Sstevel@tonic-gate 				/*
428*7c478bd9Sstevel@tonic-gate 				 * We have two routes to the same destination,
429*7c478bd9Sstevel@tonic-gate 				 * with the same nexthop and interface.
430*7c478bd9Sstevel@tonic-gate 				 * Routes are encountered in lexical order,
431*7c478bd9Sstevel@tonic-gate 				 * so a route is never promoted until the
432*7c478bd9Sstevel@tonic-gate 				 * parent route is already present.  So we
433*7c478bd9Sstevel@tonic-gate 				 * know that the new route is a promoted (or
434*7c478bd9Sstevel@tonic-gate 				 * aggregated) pair and the route already in
435*7c478bd9Sstevel@tonic-gate 				 * the slot is the explicit route.
436*7c478bd9Sstevel@tonic-gate 				 *
437*7c478bd9Sstevel@tonic-gate 				 * Prefer the best route if their metrics
438*7c478bd9Sstevel@tonic-gate 				 * differ, or the aggregated one if not,
439*7c478bd9Sstevel@tonic-gate 				 * following a sort of longest-match rule.
440*7c478bd9Sstevel@tonic-gate 				 */
441*7c478bd9Sstevel@tonic-gate 				if (pref <= ag->ag_pref) {
442*7c478bd9Sstevel@tonic-gate 					ag->ag_gate = gate;
443*7c478bd9Sstevel@tonic-gate 					ag->ag_ifp = ifp;
444*7c478bd9Sstevel@tonic-gate 					ag->ag_nhop = nhop;
445*7c478bd9Sstevel@tonic-gate 					ag->ag_tag = tag;
446*7c478bd9Sstevel@tonic-gate 					ag->ag_metric = metric;
447*7c478bd9Sstevel@tonic-gate 					ag->ag_pref = pref;
448*7c478bd9Sstevel@tonic-gate 					if (seqno > ag->ag_seqno)
449*7c478bd9Sstevel@tonic-gate 						ag->ag_seqno = seqno;
450*7c478bd9Sstevel@tonic-gate 					tmp = ag->ag_state;
451*7c478bd9Sstevel@tonic-gate 					ag->ag_state = state;
452*7c478bd9Sstevel@tonic-gate 					state = tmp;
453*7c478bd9Sstevel@tonic-gate 				}
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 				/*
456*7c478bd9Sstevel@tonic-gate 				 * Some bits are set if they are set on
457*7c478bd9Sstevel@tonic-gate 				 * either route, except when the route is
458*7c478bd9Sstevel@tonic-gate 				 * for an interface.
459*7c478bd9Sstevel@tonic-gate 				 */
460*7c478bd9Sstevel@tonic-gate 				if (!(ag->ag_state & AGS_IF))
461*7c478bd9Sstevel@tonic-gate 					ag->ag_state |=
462*7c478bd9Sstevel@tonic-gate 					    (state & (AGS_AGGREGATE_EITHER |
463*7c478bd9Sstevel@tonic-gate 					    AGS_REDUN0 | AGS_REDUN1));
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 				return;
466*7c478bd9Sstevel@tonic-gate 			} else {
467*7c478bd9Sstevel@tonic-gate 				/*
468*7c478bd9Sstevel@tonic-gate 				 * multiple routes to same dest/mask with
469*7c478bd9Sstevel@tonic-gate 				 * differing gate nexthop/or ifp. Flush
470*7c478bd9Sstevel@tonic-gate 				 * both out.
471*7c478bd9Sstevel@tonic-gate 				 */
472*7c478bd9Sstevel@tonic-gate 				break;
473*7c478bd9Sstevel@tonic-gate 			}
474*7c478bd9Sstevel@tonic-gate 		}
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 		/*
477*7c478bd9Sstevel@tonic-gate 		 * If one of the routes can be promoted and the other can
478*7c478bd9Sstevel@tonic-gate 		 * be suppressed, it may be possible to combine them or
479*7c478bd9Sstevel@tonic-gate 		 * worthwhile to promote one.
480*7c478bd9Sstevel@tonic-gate 		 *
481*7c478bd9Sstevel@tonic-gate 		 * Any route that can be promoted is always
482*7c478bd9Sstevel@tonic-gate 		 * marked to be eligible to be suppressed.
483*7c478bd9Sstevel@tonic-gate 		 */
484*7c478bd9Sstevel@tonic-gate 		if (!((state & AGS_AGGREGATE) &&
485*7c478bd9Sstevel@tonic-gate 		    (ag->ag_state & AGS_SUPPRESS)) &&
486*7c478bd9Sstevel@tonic-gate 		    !((ag->ag_state & AGS_AGGREGATE) && (state & AGS_SUPPRESS)))
487*7c478bd9Sstevel@tonic-gate 			break;
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 		/*
490*7c478bd9Sstevel@tonic-gate 		 * A pair of even/odd twin routes can be combined
491*7c478bd9Sstevel@tonic-gate 		 * if either is redundant, or if they are via the
492*7c478bd9Sstevel@tonic-gate 		 * same gateway and have the same metric.
493*7c478bd9Sstevel@tonic-gate 		 */
494*7c478bd9Sstevel@tonic-gate 		if (AG_IS_REDUN(ag->ag_state) || AG_IS_REDUN(state) ||
495*7c478bd9Sstevel@tonic-gate 		    (ag->ag_nhop == nhop && ag->ag_ifp == ifp &&
496*7c478bd9Sstevel@tonic-gate 		    ag->ag_pref == pref &&
497*7c478bd9Sstevel@tonic-gate 		    (state & ag->ag_state & AGS_AGGREGATE) != 0)) {
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 			/*
500*7c478bd9Sstevel@tonic-gate 			 * We have both the even and odd pairs.
501*7c478bd9Sstevel@tonic-gate 			 * Since the routes are encountered in order,
502*7c478bd9Sstevel@tonic-gate 			 * the route in the slot must be the even twin.
503*7c478bd9Sstevel@tonic-gate 			 *
504*7c478bd9Sstevel@tonic-gate 			 * Combine and promote (aggregate) the pair of routes.
505*7c478bd9Sstevel@tonic-gate 			 */
506*7c478bd9Sstevel@tonic-gate 			if (seqno < ag->ag_seqno)
507*7c478bd9Sstevel@tonic-gate 				seqno = ag->ag_seqno;
508*7c478bd9Sstevel@tonic-gate 			if (!AG_IS_REDUN(state))
509*7c478bd9Sstevel@tonic-gate 				state &= ~AGS_REDUN1;
510*7c478bd9Sstevel@tonic-gate 			if (AG_IS_REDUN(ag->ag_state))
511*7c478bd9Sstevel@tonic-gate 				state |= AGS_REDUN0;
512*7c478bd9Sstevel@tonic-gate 			else
513*7c478bd9Sstevel@tonic-gate 				state &= ~AGS_REDUN0;
514*7c478bd9Sstevel@tonic-gate 			state |= (ag->ag_state & AGS_AGGREGATE_EITHER);
515*7c478bd9Sstevel@tonic-gate 			if (ag->ag_tag != tag)
516*7c478bd9Sstevel@tonic-gate 				tag = 0;
517*7c478bd9Sstevel@tonic-gate 			if (ag->ag_nhop != nhop)
518*7c478bd9Sstevel@tonic-gate 				nhop = 0;
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 			/*
521*7c478bd9Sstevel@tonic-gate 			 * Get rid of the even twin that was already
522*7c478bd9Sstevel@tonic-gate 			 * in the slot.
523*7c478bd9Sstevel@tonic-gate 			 */
524*7c478bd9Sstevel@tonic-gate 			ag_del(ag);
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 		} else if (ag->ag_pref >= pref &&
527*7c478bd9Sstevel@tonic-gate 		    (ag->ag_state & AGS_AGGREGATE)) {
528*7c478bd9Sstevel@tonic-gate 			/*
529*7c478bd9Sstevel@tonic-gate 			 * If we cannot combine the pair, maybe the route
530*7c478bd9Sstevel@tonic-gate 			 * with the worse metric can be promoted.
531*7c478bd9Sstevel@tonic-gate 			 *
532*7c478bd9Sstevel@tonic-gate 			 * Promote the old, even twin, by giving its slot
533*7c478bd9Sstevel@tonic-gate 			 * in the table to the new, odd twin.
534*7c478bd9Sstevel@tonic-gate 			 */
535*7c478bd9Sstevel@tonic-gate 			ag->ag_dst_h = dst;
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 			xaddr = ag->ag_gate;
538*7c478bd9Sstevel@tonic-gate 			ag->ag_gate = gate;
539*7c478bd9Sstevel@tonic-gate 			gate = xaddr;
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 			xifp = ag->ag_ifp;
542*7c478bd9Sstevel@tonic-gate 			ag->ag_ifp = ifp;
543*7c478bd9Sstevel@tonic-gate 			ifp = xifp;
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 			xaddr = ag->ag_nhop;
546*7c478bd9Sstevel@tonic-gate 			ag->ag_nhop = nhop;
547*7c478bd9Sstevel@tonic-gate 			nhop = xaddr;
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 			tmp = ag->ag_tag;
550*7c478bd9Sstevel@tonic-gate 			ag->ag_tag = tag;
551*7c478bd9Sstevel@tonic-gate 			tag = tmp;
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 			/*
554*7c478bd9Sstevel@tonic-gate 			 * The promoted route is even-redundant only if the
555*7c478bd9Sstevel@tonic-gate 			 * even twin was fully redundant.  It is not
556*7c478bd9Sstevel@tonic-gate 			 * odd-redundant because the odd-twin will still be
557*7c478bd9Sstevel@tonic-gate 			 * in the table.
558*7c478bd9Sstevel@tonic-gate 			 */
559*7c478bd9Sstevel@tonic-gate 			tmp = ag->ag_state;
560*7c478bd9Sstevel@tonic-gate 			if (!AG_IS_REDUN(tmp))
561*7c478bd9Sstevel@tonic-gate 				tmp &= ~AGS_REDUN0;
562*7c478bd9Sstevel@tonic-gate 			tmp &= ~AGS_REDUN1;
563*7c478bd9Sstevel@tonic-gate 			ag->ag_state = state;
564*7c478bd9Sstevel@tonic-gate 			state = tmp;
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 			tmp = ag->ag_metric;
567*7c478bd9Sstevel@tonic-gate 			ag->ag_metric = metric;
568*7c478bd9Sstevel@tonic-gate 			metric = tmp;
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate 			tmp = ag->ag_pref;
571*7c478bd9Sstevel@tonic-gate 			ag->ag_pref = pref;
572*7c478bd9Sstevel@tonic-gate 			pref = tmp;
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 			/* take the newest sequence number */
575*7c478bd9Sstevel@tonic-gate 			if (seqno <= ag->ag_seqno)
576*7c478bd9Sstevel@tonic-gate 				seqno = ag->ag_seqno;
577*7c478bd9Sstevel@tonic-gate 			else
578*7c478bd9Sstevel@tonic-gate 				ag->ag_seqno = seqno;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 		} else {
581*7c478bd9Sstevel@tonic-gate 			if (!(state & AGS_AGGREGATE))
582*7c478bd9Sstevel@tonic-gate 				break;	/* cannot promote either twin */
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 			/*
585*7c478bd9Sstevel@tonic-gate 			 * Promote the new, odd twin by shaving its
586*7c478bd9Sstevel@tonic-gate 			 * mask and address.
587*7c478bd9Sstevel@tonic-gate 			 * The promoted route is odd-redundant only if the
588*7c478bd9Sstevel@tonic-gate 			 * odd twin was fully redundant.  It is not
589*7c478bd9Sstevel@tonic-gate 			 * even-redundant because the even twin is still in
590*7c478bd9Sstevel@tonic-gate 			 * the table.
591*7c478bd9Sstevel@tonic-gate 			 */
592*7c478bd9Sstevel@tonic-gate 			if (!AG_IS_REDUN(state))
593*7c478bd9Sstevel@tonic-gate 				state &= ~AGS_REDUN1;
594*7c478bd9Sstevel@tonic-gate 			state &= ~AGS_REDUN0;
595*7c478bd9Sstevel@tonic-gate 			if (seqno < ag->ag_seqno)
596*7c478bd9Sstevel@tonic-gate 				seqno = ag->ag_seqno;
597*7c478bd9Sstevel@tonic-gate 			else
598*7c478bd9Sstevel@tonic-gate 				ag->ag_seqno = seqno;
599*7c478bd9Sstevel@tonic-gate 		}
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 		mask <<= 1;
602*7c478bd9Sstevel@tonic-gate 		dst &= mask;
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 		if (ag_cors == NULL) {
605*7c478bd9Sstevel@tonic-gate 			ag = ag_corsest;
606*7c478bd9Sstevel@tonic-gate 			break;
607*7c478bd9Sstevel@tonic-gate 		}
608*7c478bd9Sstevel@tonic-gate 		ag = ag_cors;
609*7c478bd9Sstevel@tonic-gate 		ag_cors = ag->ag_cors;
610*7c478bd9Sstevel@tonic-gate 	}
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	/*
613*7c478bd9Sstevel@tonic-gate 	 * When we can no longer promote and combine routes,
614*7c478bd9Sstevel@tonic-gate 	 * flush the old route in the target slot.  Also flush
615*7c478bd9Sstevel@tonic-gate 	 * any finer routes that we know will never be aggregated by
616*7c478bd9Sstevel@tonic-gate 	 * the new route.
617*7c478bd9Sstevel@tonic-gate 	 *
618*7c478bd9Sstevel@tonic-gate 	 * In case we moved toward coarser masks,
619*7c478bd9Sstevel@tonic-gate 	 * get back where we belong
620*7c478bd9Sstevel@tonic-gate 	 */
621*7c478bd9Sstevel@tonic-gate 	if (ag != NULL && ag->ag_mask < mask) {
622*7c478bd9Sstevel@tonic-gate 		ag_cors = ag;
623*7c478bd9Sstevel@tonic-gate 		ag = ag->ag_fine;
624*7c478bd9Sstevel@tonic-gate 	}
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 	/* Empty the target slot */
627*7c478bd9Sstevel@tonic-gate 	if (ag != NULL && ag->ag_mask == mask) {
628*7c478bd9Sstevel@tonic-gate 		ag_flush(ag->ag_dst_h, ag->ag_mask, out);
629*7c478bd9Sstevel@tonic-gate 		ag = (ag_cors == NULL) ? ag_corsest : ag_cors->ag_fine;
630*7c478bd9Sstevel@tonic-gate 	}
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_AG
633*7c478bd9Sstevel@tonic-gate 	if (ag == NULL && ag_cors != ag_finest)
634*7c478bd9Sstevel@tonic-gate 		abort();
635*7c478bd9Sstevel@tonic-gate 	if (ag_cors == NULL && ag != ag_corsest)
636*7c478bd9Sstevel@tonic-gate 		abort();
637*7c478bd9Sstevel@tonic-gate 	if (ag != NULL && ag->ag_cors != ag_cors)
638*7c478bd9Sstevel@tonic-gate 		abort();
639*7c478bd9Sstevel@tonic-gate 	if (ag_cors != NULL && ag_cors->ag_fine != ag)
640*7c478bd9Sstevel@tonic-gate 		abort();
641*7c478bd9Sstevel@tonic-gate 	CHECK_AG();
642*7c478bd9Sstevel@tonic-gate #endif
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	/* Save the new route on the end of the table. */
645*7c478bd9Sstevel@tonic-gate 	nag = ag_avail;
646*7c478bd9Sstevel@tonic-gate 	ag_avail = nag->ag_fine;
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 	nag->ag_dst_h = dst;
649*7c478bd9Sstevel@tonic-gate 	nag->ag_mask = mask;
650*7c478bd9Sstevel@tonic-gate 	nag->ag_ifp = ifp;
651*7c478bd9Sstevel@tonic-gate 	nag->ag_gate = gate;
652*7c478bd9Sstevel@tonic-gate 	nag->ag_nhop = nhop;
653*7c478bd9Sstevel@tonic-gate 	nag->ag_metric = metric;
654*7c478bd9Sstevel@tonic-gate 	nag->ag_pref = pref;
655*7c478bd9Sstevel@tonic-gate 	nag->ag_tag = tag;
656*7c478bd9Sstevel@tonic-gate 	nag->ag_state = state;
657*7c478bd9Sstevel@tonic-gate 	nag->ag_seqno = seqno;
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 	nag->ag_fine = ag;
660*7c478bd9Sstevel@tonic-gate 	if (ag != NULL)
661*7c478bd9Sstevel@tonic-gate 		ag->ag_cors = nag;
662*7c478bd9Sstevel@tonic-gate 	else
663*7c478bd9Sstevel@tonic-gate 		ag_finest = nag;
664*7c478bd9Sstevel@tonic-gate 	nag->ag_cors = ag_cors;
665*7c478bd9Sstevel@tonic-gate 	if (ag_cors == NULL)
666*7c478bd9Sstevel@tonic-gate 		ag_corsest = nag;
667*7c478bd9Sstevel@tonic-gate 	else
668*7c478bd9Sstevel@tonic-gate 		ag_cors->ag_fine = nag;
669*7c478bd9Sstevel@tonic-gate 	CHECK_AG();
670*7c478bd9Sstevel@tonic-gate }
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate static const char *
674*7c478bd9Sstevel@tonic-gate rtm_type_name(uchar_t type)
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate 	static const char *rtm_types[] = {
677*7c478bd9Sstevel@tonic-gate 		"RTM_ADD",
678*7c478bd9Sstevel@tonic-gate 		"RTM_DELETE",
679*7c478bd9Sstevel@tonic-gate 		"RTM_CHANGE",
680*7c478bd9Sstevel@tonic-gate 		"RTM_GET",
681*7c478bd9Sstevel@tonic-gate 		"RTM_LOSING",
682*7c478bd9Sstevel@tonic-gate 		"RTM_REDIRECT",
683*7c478bd9Sstevel@tonic-gate 		"RTM_MISS",
684*7c478bd9Sstevel@tonic-gate 		"RTM_LOCK",
685*7c478bd9Sstevel@tonic-gate 		"RTM_OLDADD",
686*7c478bd9Sstevel@tonic-gate 		"RTM_OLDDEL",
687*7c478bd9Sstevel@tonic-gate 		"RTM_RESOLVE",
688*7c478bd9Sstevel@tonic-gate 		"RTM_NEWADDR",
689*7c478bd9Sstevel@tonic-gate 		"RTM_DELADDR",
690*7c478bd9Sstevel@tonic-gate 		"RTM_IFINFO",
691*7c478bd9Sstevel@tonic-gate 		"RTM_NEWMADDR",
692*7c478bd9Sstevel@tonic-gate 		"RTM_DELMADDR"
693*7c478bd9Sstevel@tonic-gate 	};
694*7c478bd9Sstevel@tonic-gate #define	NEW_RTM_PAT	"RTM type %#x"
695*7c478bd9Sstevel@tonic-gate 	static char name0[sizeof (NEW_RTM_PAT) + 2];
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 	if (type > sizeof (rtm_types) / sizeof (rtm_types[0]) || type == 0) {
698*7c478bd9Sstevel@tonic-gate 		(void) snprintf(name0, sizeof (name0), NEW_RTM_PAT, type);
699*7c478bd9Sstevel@tonic-gate 		return (name0);
700*7c478bd9Sstevel@tonic-gate 	} else {
701*7c478bd9Sstevel@tonic-gate 		return (rtm_types[type-1]);
702*7c478bd9Sstevel@tonic-gate 	}
703*7c478bd9Sstevel@tonic-gate #undef	NEW_RTM_PAT
704*7c478bd9Sstevel@tonic-gate }
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate static void
708*7c478bd9Sstevel@tonic-gate dump_rt_msg(const char *act, struct rt_msghdr *rtm, int mlen)
709*7c478bd9Sstevel@tonic-gate {
710*7c478bd9Sstevel@tonic-gate 	const char *mtype;
711*7c478bd9Sstevel@tonic-gate 	uchar_t *cp;
712*7c478bd9Sstevel@tonic-gate 	int i, j;
713*7c478bd9Sstevel@tonic-gate 	char buffer[16*3 + 1], *ibs;
714*7c478bd9Sstevel@tonic-gate 	struct ifa_msghdr *ifam;
715*7c478bd9Sstevel@tonic-gate 	struct if_msghdr *ifm;
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 	switch (rtm->rtm_type) {
718*7c478bd9Sstevel@tonic-gate 	case RTM_NEWADDR:
719*7c478bd9Sstevel@tonic-gate 	case RTM_DELADDR:
720*7c478bd9Sstevel@tonic-gate 		mtype = "ifam";
721*7c478bd9Sstevel@tonic-gate 		break;
722*7c478bd9Sstevel@tonic-gate 	case RTM_IFINFO:
723*7c478bd9Sstevel@tonic-gate 		mtype = "ifm";
724*7c478bd9Sstevel@tonic-gate 		break;
725*7c478bd9Sstevel@tonic-gate 	default:
726*7c478bd9Sstevel@tonic-gate 		mtype = "rtm";
727*7c478bd9Sstevel@tonic-gate 		break;
728*7c478bd9Sstevel@tonic-gate 	}
729*7c478bd9Sstevel@tonic-gate 	trace_misc("%s %s %d bytes", act, mtype, mlen);
730*7c478bd9Sstevel@tonic-gate 	if (mlen > rtm->rtm_msglen) {
731*7c478bd9Sstevel@tonic-gate 		trace_misc("%s: extra %d bytes ignored", mtype,
732*7c478bd9Sstevel@tonic-gate 		    mlen - rtm->rtm_msglen);
733*7c478bd9Sstevel@tonic-gate 		mlen = rtm->rtm_msglen;
734*7c478bd9Sstevel@tonic-gate 	} else if (mlen < rtm->rtm_msglen) {
735*7c478bd9Sstevel@tonic-gate 		trace_misc("%s: truncated by %d bytes", mtype,
736*7c478bd9Sstevel@tonic-gate 		    rtm->rtm_msglen - mlen);
737*7c478bd9Sstevel@tonic-gate 	}
738*7c478bd9Sstevel@tonic-gate 	switch (rtm->rtm_type) {
739*7c478bd9Sstevel@tonic-gate 	case RTM_NEWADDR:
740*7c478bd9Sstevel@tonic-gate 	case RTM_DELADDR:
741*7c478bd9Sstevel@tonic-gate 		ifam = (struct ifa_msghdr *)rtm;
742*7c478bd9Sstevel@tonic-gate 		trace_misc("ifam: msglen %d version %d type %d addrs %X",
743*7c478bd9Sstevel@tonic-gate 		    ifam->ifam_msglen, ifam->ifam_version, ifam->ifam_type,
744*7c478bd9Sstevel@tonic-gate 		    ifam->ifam_addrs);
745*7c478bd9Sstevel@tonic-gate 		trace_misc("ifam: flags %X index %d metric %d",
746*7c478bd9Sstevel@tonic-gate 		    ifam->ifam_flags, ifam->ifam_index, ifam->ifam_metric);
747*7c478bd9Sstevel@tonic-gate 		cp = (uchar_t *)(ifam + 1);
748*7c478bd9Sstevel@tonic-gate 		break;
749*7c478bd9Sstevel@tonic-gate 	case RTM_IFINFO:
750*7c478bd9Sstevel@tonic-gate 		ifm = (struct if_msghdr *)rtm;
751*7c478bd9Sstevel@tonic-gate 		trace_misc("ifm: msglen %d version %d type %d addrs %X",
752*7c478bd9Sstevel@tonic-gate 		    ifm->ifm_msglen, ifm->ifm_version, ifm->ifm_type,
753*7c478bd9Sstevel@tonic-gate 		    ifm->ifm_addrs);
754*7c478bd9Sstevel@tonic-gate 		ibs = if_bit_string(ifm->ifm_flags, _B_TRUE);
755*7c478bd9Sstevel@tonic-gate 		if (ibs == NULL) {
756*7c478bd9Sstevel@tonic-gate 			trace_misc("ifm: flags %#x index %d", ifm->ifm_flags,
757*7c478bd9Sstevel@tonic-gate 			    ifm->ifm_index);
758*7c478bd9Sstevel@tonic-gate 		} else {
759*7c478bd9Sstevel@tonic-gate 			trace_misc("ifm: flags %s index %d", ibs,
760*7c478bd9Sstevel@tonic-gate 			    ifm->ifm_index);
761*7c478bd9Sstevel@tonic-gate 			free(ibs);
762*7c478bd9Sstevel@tonic-gate 		}
763*7c478bd9Sstevel@tonic-gate 		cp = (uchar_t *)(ifm + 1);
764*7c478bd9Sstevel@tonic-gate 		break;
765*7c478bd9Sstevel@tonic-gate 	default:
766*7c478bd9Sstevel@tonic-gate 		trace_misc("rtm: msglen %d version %d type %d index %d",
767*7c478bd9Sstevel@tonic-gate 		    rtm->rtm_msglen, rtm->rtm_version, rtm->rtm_type,
768*7c478bd9Sstevel@tonic-gate 		    rtm->rtm_index);
769*7c478bd9Sstevel@tonic-gate 		trace_misc("rtm: flags %X addrs %X pid %d seq %d",
770*7c478bd9Sstevel@tonic-gate 		    rtm->rtm_flags, rtm->rtm_addrs, rtm->rtm_pid, rtm->rtm_seq);
771*7c478bd9Sstevel@tonic-gate 		trace_misc("rtm: errno %d use %d inits %X", rtm->rtm_errno,
772*7c478bd9Sstevel@tonic-gate 		    rtm->rtm_use, rtm->rtm_inits);
773*7c478bd9Sstevel@tonic-gate 		cp = (uchar_t *)(rtm + 1);
774*7c478bd9Sstevel@tonic-gate 		break;
775*7c478bd9Sstevel@tonic-gate 	}
776*7c478bd9Sstevel@tonic-gate 	i = mlen - (cp - (uint8_t *)rtm);
777*7c478bd9Sstevel@tonic-gate 	while (i > 0) {
778*7c478bd9Sstevel@tonic-gate 		buffer[0] = '\0';
779*7c478bd9Sstevel@tonic-gate 		ibs = buffer;
780*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < 16 && i > 0; j++, i--)
781*7c478bd9Sstevel@tonic-gate 			ibs += sprintf(ibs, " %02X", *cp++);
782*7c478bd9Sstevel@tonic-gate 		trace_misc("addr%s", buffer);
783*7c478bd9Sstevel@tonic-gate 	}
784*7c478bd9Sstevel@tonic-gate }
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate /*
787*7c478bd9Sstevel@tonic-gate  * Tell the kernel to add, delete or change a route
788*7c478bd9Sstevel@tonic-gate  * Pass k_state from khash in for diagnostic info.
789*7c478bd9Sstevel@tonic-gate  */
790*7c478bd9Sstevel@tonic-gate static void
791*7c478bd9Sstevel@tonic-gate rtioctl(int action,			/* RTM_DELETE, etc */
792*7c478bd9Sstevel@tonic-gate     in_addr_t dst,
793*7c478bd9Sstevel@tonic-gate     in_addr_t gate,
794*7c478bd9Sstevel@tonic-gate     in_addr_t mask,
795*7c478bd9Sstevel@tonic-gate     struct interface *ifp,
796*7c478bd9Sstevel@tonic-gate     uint8_t metric,
797*7c478bd9Sstevel@tonic-gate     int flags)
798*7c478bd9Sstevel@tonic-gate {
799*7c478bd9Sstevel@tonic-gate 	static int rt_sock_seqno = 0;
800*7c478bd9Sstevel@tonic-gate 	struct {
801*7c478bd9Sstevel@tonic-gate 		struct rt_msghdr w_rtm;
802*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in w_dst;
803*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in w_gate;
804*7c478bd9Sstevel@tonic-gate 		uint8_t w_space[512];
805*7c478bd9Sstevel@tonic-gate 	} w;
806*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in w_mask;
807*7c478bd9Sstevel@tonic-gate 	struct sockaddr_dl w_ifp;
808*7c478bd9Sstevel@tonic-gate 	uint8_t *cp;
809*7c478bd9Sstevel@tonic-gate 	long cc;
810*7c478bd9Sstevel@tonic-gate #define	PAT " %-10s %s metric=%d flags=%#x"
811*7c478bd9Sstevel@tonic-gate #define	ARGS rtm_type_name(action), rtname(dst, mask, gate), metric, flags
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate again:
814*7c478bd9Sstevel@tonic-gate 	(void) memset(&w, 0, sizeof (w));
815*7c478bd9Sstevel@tonic-gate 	(void) memset(&w_mask, 0, sizeof (w_mask));
816*7c478bd9Sstevel@tonic-gate 	(void) memset(&w_ifp, 0, sizeof (w_ifp));
817*7c478bd9Sstevel@tonic-gate 	cp = w.w_space;
818*7c478bd9Sstevel@tonic-gate 	w.w_rtm.rtm_msglen = sizeof (struct rt_msghdr) +
819*7c478bd9Sstevel@tonic-gate 	    2 * ROUNDUP_LONG(sizeof (struct sockaddr_in));
820*7c478bd9Sstevel@tonic-gate 	w.w_rtm.rtm_version = RTM_VERSION;
821*7c478bd9Sstevel@tonic-gate 	w.w_rtm.rtm_type = action;
822*7c478bd9Sstevel@tonic-gate 	w.w_rtm.rtm_flags = flags;
823*7c478bd9Sstevel@tonic-gate 	w.w_rtm.rtm_seq = ++rt_sock_seqno;
824*7c478bd9Sstevel@tonic-gate 	w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
825*7c478bd9Sstevel@tonic-gate 	if (metric != 0 || action == RTM_CHANGE) {
826*7c478bd9Sstevel@tonic-gate 		w.w_rtm.rtm_rmx.rmx_hopcount = metric;
827*7c478bd9Sstevel@tonic-gate 		w.w_rtm.rtm_inits |= RTV_HOPCOUNT;
828*7c478bd9Sstevel@tonic-gate 	}
829*7c478bd9Sstevel@tonic-gate 	w.w_dst.sin_family = AF_INET;
830*7c478bd9Sstevel@tonic-gate 	w.w_dst.sin_addr.s_addr = dst;
831*7c478bd9Sstevel@tonic-gate 	w.w_gate.sin_family = AF_INET;
832*7c478bd9Sstevel@tonic-gate 	w.w_gate.sin_addr.s_addr = gate;
833*7c478bd9Sstevel@tonic-gate 	if (mask == HOST_MASK) {
834*7c478bd9Sstevel@tonic-gate 		w.w_rtm.rtm_flags |= RTF_HOST;
835*7c478bd9Sstevel@tonic-gate 	} else {
836*7c478bd9Sstevel@tonic-gate 		w.w_rtm.rtm_addrs |= RTA_NETMASK;
837*7c478bd9Sstevel@tonic-gate 		w_mask.sin_family = AF_INET;
838*7c478bd9Sstevel@tonic-gate 		w_mask.sin_addr.s_addr = htonl(mask);
839*7c478bd9Sstevel@tonic-gate 		(void) memmove(cp, &w_mask, sizeof (w_mask));
840*7c478bd9Sstevel@tonic-gate 		cp += ROUNDUP_LONG(sizeof (struct sockaddr_in));
841*7c478bd9Sstevel@tonic-gate 		w.w_rtm.rtm_msglen += ROUNDUP_LONG(sizeof (struct sockaddr_in));
842*7c478bd9Sstevel@tonic-gate 	}
843*7c478bd9Sstevel@tonic-gate 	if (ifp == NULL)
844*7c478bd9Sstevel@tonic-gate 		ifp = iflookup(gate);
845*7c478bd9Sstevel@tonic-gate 
846*7c478bd9Sstevel@tonic-gate 	if ((ifp == NULL) || (ifp->int_phys == NULL)) {
847*7c478bd9Sstevel@tonic-gate 		trace_misc("no ifp for" PAT, ARGS);
848*7c478bd9Sstevel@tonic-gate 	} else {
849*7c478bd9Sstevel@tonic-gate 		if (ifp->int_phys->phyi_index > UINT16_MAX) {
850*7c478bd9Sstevel@tonic-gate 			trace_misc("ifindex %d is too big for sdl_index",
851*7c478bd9Sstevel@tonic-gate 			    ifp->int_phys->phyi_index);
852*7c478bd9Sstevel@tonic-gate 		} else {
853*7c478bd9Sstevel@tonic-gate 			w_ifp.sdl_family = AF_LINK;
854*7c478bd9Sstevel@tonic-gate 			w.w_rtm.rtm_addrs |= RTA_IFP;
855*7c478bd9Sstevel@tonic-gate 			w_ifp.sdl_index = ifp->int_phys->phyi_index;
856*7c478bd9Sstevel@tonic-gate 			(void) memmove(cp, &w_ifp, sizeof (w_ifp));
857*7c478bd9Sstevel@tonic-gate 			w.w_rtm.rtm_msglen +=
858*7c478bd9Sstevel@tonic-gate 			    ROUNDUP_LONG(sizeof (struct sockaddr_dl));
859*7c478bd9Sstevel@tonic-gate 		}
860*7c478bd9Sstevel@tonic-gate 	}
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 
863*7c478bd9Sstevel@tonic-gate 	if (!no_install) {
864*7c478bd9Sstevel@tonic-gate 		if (TRACERTS)
865*7c478bd9Sstevel@tonic-gate 			dump_rt_msg("write", &w.w_rtm, w.w_rtm.rtm_msglen);
866*7c478bd9Sstevel@tonic-gate 		cc = write(rt_sock, &w, w.w_rtm.rtm_msglen);
867*7c478bd9Sstevel@tonic-gate 		if (cc < 0) {
868*7c478bd9Sstevel@tonic-gate 			if (errno == ESRCH && (action == RTM_CHANGE ||
869*7c478bd9Sstevel@tonic-gate 			    action == RTM_DELETE)) {
870*7c478bd9Sstevel@tonic-gate 				trace_act("route disappeared before" PAT, ARGS);
871*7c478bd9Sstevel@tonic-gate 				if (action == RTM_CHANGE) {
872*7c478bd9Sstevel@tonic-gate 					action = RTM_ADD;
873*7c478bd9Sstevel@tonic-gate 					goto again;
874*7c478bd9Sstevel@tonic-gate 				}
875*7c478bd9Sstevel@tonic-gate 				return;
876*7c478bd9Sstevel@tonic-gate 			}
877*7c478bd9Sstevel@tonic-gate 			writelog(LOG_WARNING, "write(rt_sock)" PAT ": %s ",
878*7c478bd9Sstevel@tonic-gate 			    ARGS, rip_strerror(errno));
879*7c478bd9Sstevel@tonic-gate 			return;
880*7c478bd9Sstevel@tonic-gate 		} else if (cc != w.w_rtm.rtm_msglen) {
881*7c478bd9Sstevel@tonic-gate 			msglog("write(rt_sock) wrote %ld instead of %d for" PAT,
882*7c478bd9Sstevel@tonic-gate 			    cc, w.w_rtm.rtm_msglen, ARGS);
883*7c478bd9Sstevel@tonic-gate 			return;
884*7c478bd9Sstevel@tonic-gate 		}
885*7c478bd9Sstevel@tonic-gate 	}
886*7c478bd9Sstevel@tonic-gate 	if (TRACEKERNEL)
887*7c478bd9Sstevel@tonic-gate 		trace_misc("write kernel" PAT, ARGS);
888*7c478bd9Sstevel@tonic-gate #undef PAT
889*7c478bd9Sstevel@tonic-gate #undef ARGS
890*7c478bd9Sstevel@tonic-gate }
891*7c478bd9Sstevel@tonic-gate 
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate /* Hash table containing our image of the kernel forwarding table. */
894*7c478bd9Sstevel@tonic-gate #define	KHASH_SIZE 71			/* should be prime */
895*7c478bd9Sstevel@tonic-gate #define	KHASH(a, m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
896*7c478bd9Sstevel@tonic-gate static struct khash *khash_bins[KHASH_SIZE];
897*7c478bd9Sstevel@tonic-gate 
898*7c478bd9Sstevel@tonic-gate #define	K_KEEP_LIM	30	/* k_keep */
899*7c478bd9Sstevel@tonic-gate 
900*7c478bd9Sstevel@tonic-gate static struct khash *
901*7c478bd9Sstevel@tonic-gate kern_find(in_addr_t dst, in_addr_t mask, in_addr_t gate,
902*7c478bd9Sstevel@tonic-gate     struct interface *ifp, struct khash ***ppk)
903*7c478bd9Sstevel@tonic-gate {
904*7c478bd9Sstevel@tonic-gate 	struct khash *k, **pk;
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate 	for (pk = &KHASH(dst, mask); (k = *pk) != NULL; pk = &k->k_next) {
907*7c478bd9Sstevel@tonic-gate 		if (k->k_dst == dst && k->k_mask == mask &&
908*7c478bd9Sstevel@tonic-gate 		    (gate == 0 || k->k_gate == gate) &&
909*7c478bd9Sstevel@tonic-gate 		    (ifp == NULL || k->k_ifp == ifp)) {
910*7c478bd9Sstevel@tonic-gate 			break;
911*7c478bd9Sstevel@tonic-gate 		}
912*7c478bd9Sstevel@tonic-gate 	}
913*7c478bd9Sstevel@tonic-gate 	if (ppk != NULL)
914*7c478bd9Sstevel@tonic-gate 		*ppk = pk;
915*7c478bd9Sstevel@tonic-gate 	return (k);
916*7c478bd9Sstevel@tonic-gate }
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate /*
920*7c478bd9Sstevel@tonic-gate  * Find out if there is an alternate route to a given destination
921*7c478bd9Sstevel@tonic-gate  * off of a given interface.
922*7c478bd9Sstevel@tonic-gate  */
923*7c478bd9Sstevel@tonic-gate static struct khash *
924*7c478bd9Sstevel@tonic-gate kern_alternate(in_addr_t dst, in_addr_t mask, in_addr_t gate,
925*7c478bd9Sstevel@tonic-gate     struct interface *ifp, struct khash ***ppk)
926*7c478bd9Sstevel@tonic-gate {
927*7c478bd9Sstevel@tonic-gate 	struct khash *k, **pk;
928*7c478bd9Sstevel@tonic-gate 
929*7c478bd9Sstevel@tonic-gate 	for (pk = &KHASH(dst, mask); (k = *pk) != NULL; pk = &k->k_next) {
930*7c478bd9Sstevel@tonic-gate 		if (k->k_dst == dst && k->k_mask == mask &&
931*7c478bd9Sstevel@tonic-gate 		    (k->k_gate != gate) &&
932*7c478bd9Sstevel@tonic-gate 		    (k->k_ifp == ifp)) {
933*7c478bd9Sstevel@tonic-gate 			break;
934*7c478bd9Sstevel@tonic-gate 		}
935*7c478bd9Sstevel@tonic-gate 	}
936*7c478bd9Sstevel@tonic-gate 	if (ppk != NULL)
937*7c478bd9Sstevel@tonic-gate 		*ppk = pk;
938*7c478bd9Sstevel@tonic-gate 	return (k);
939*7c478bd9Sstevel@tonic-gate }
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate static struct khash *
942*7c478bd9Sstevel@tonic-gate kern_add(in_addr_t dst, uint32_t mask, in_addr_t gate, struct interface *ifp)
943*7c478bd9Sstevel@tonic-gate {
944*7c478bd9Sstevel@tonic-gate 	struct khash *k, **pk;
945*7c478bd9Sstevel@tonic-gate 
946*7c478bd9Sstevel@tonic-gate 	k = kern_find(dst, mask, gate, ifp, &pk);
947*7c478bd9Sstevel@tonic-gate 	if (k != NULL)
948*7c478bd9Sstevel@tonic-gate 		return (k);
949*7c478bd9Sstevel@tonic-gate 
950*7c478bd9Sstevel@tonic-gate 	k = rtmalloc(sizeof (*k), "kern_add");
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 	(void) memset(k, 0, sizeof (*k));
953*7c478bd9Sstevel@tonic-gate 	k->k_dst = dst;
954*7c478bd9Sstevel@tonic-gate 	k->k_mask = mask;
955*7c478bd9Sstevel@tonic-gate 	k->k_state = KS_NEW;
956*7c478bd9Sstevel@tonic-gate 	k->k_keep = now.tv_sec;
957*7c478bd9Sstevel@tonic-gate 	k->k_gate = gate;
958*7c478bd9Sstevel@tonic-gate 	k->k_ifp = ifp;
959*7c478bd9Sstevel@tonic-gate 	*pk = k;
960*7c478bd9Sstevel@tonic-gate 
961*7c478bd9Sstevel@tonic-gate 	return (k);
962*7c478bd9Sstevel@tonic-gate }
963*7c478bd9Sstevel@tonic-gate 
964*7c478bd9Sstevel@tonic-gate /* delete all khash entries that are wired through the interface ifp */
965*7c478bd9Sstevel@tonic-gate void
966*7c478bd9Sstevel@tonic-gate kern_flush_ifp(struct interface *ifp)
967*7c478bd9Sstevel@tonic-gate {
968*7c478bd9Sstevel@tonic-gate 	struct khash *k, *kprev, *knext;
969*7c478bd9Sstevel@tonic-gate 	int i;
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KHASH_SIZE; i++) {
972*7c478bd9Sstevel@tonic-gate 		kprev = NULL;
973*7c478bd9Sstevel@tonic-gate 		for (k = khash_bins[i]; k != NULL; k = knext) {
974*7c478bd9Sstevel@tonic-gate 			knext = k->k_next;
975*7c478bd9Sstevel@tonic-gate 			if (k->k_ifp == ifp) {
976*7c478bd9Sstevel@tonic-gate 				if (kprev != NULL)
977*7c478bd9Sstevel@tonic-gate 					kprev->k_next = k->k_next;
978*7c478bd9Sstevel@tonic-gate 				else
979*7c478bd9Sstevel@tonic-gate 					khash_bins[i] = k->k_next;
980*7c478bd9Sstevel@tonic-gate 				free(k);
981*7c478bd9Sstevel@tonic-gate 				continue;
982*7c478bd9Sstevel@tonic-gate 			}
983*7c478bd9Sstevel@tonic-gate 			kprev = k;
984*7c478bd9Sstevel@tonic-gate 		}
985*7c478bd9Sstevel@tonic-gate 	}
986*7c478bd9Sstevel@tonic-gate }
987*7c478bd9Sstevel@tonic-gate 
988*7c478bd9Sstevel@tonic-gate /*
989*7c478bd9Sstevel@tonic-gate  * rewire khash entries that currently go through oldifp to
990*7c478bd9Sstevel@tonic-gate  * go through newifp.
991*7c478bd9Sstevel@tonic-gate  */
992*7c478bd9Sstevel@tonic-gate void
993*7c478bd9Sstevel@tonic-gate kern_rewire_ifp(struct interface *oldifp, struct interface *newifp)
994*7c478bd9Sstevel@tonic-gate {
995*7c478bd9Sstevel@tonic-gate 	struct khash *k;
996*7c478bd9Sstevel@tonic-gate 	int i;
997*7c478bd9Sstevel@tonic-gate 
998*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KHASH_SIZE; i++) {
999*7c478bd9Sstevel@tonic-gate 		for (k = khash_bins[i]; k; k = k->k_next) {
1000*7c478bd9Sstevel@tonic-gate 			if (k->k_ifp == oldifp) {
1001*7c478bd9Sstevel@tonic-gate 				k->k_ifp = newifp;
1002*7c478bd9Sstevel@tonic-gate 				trace_misc("kern_rewire_ifp k 0x%lx "
1003*7c478bd9Sstevel@tonic-gate 				    "from %s to %s", k, oldifp->int_name,
1004*7c478bd9Sstevel@tonic-gate 				    newifp->int_name);
1005*7c478bd9Sstevel@tonic-gate 			}
1006*7c478bd9Sstevel@tonic-gate 		}
1007*7c478bd9Sstevel@tonic-gate 	}
1008*7c478bd9Sstevel@tonic-gate }
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate /*
1012*7c478bd9Sstevel@tonic-gate  * Check that a static route it is still in the daemon table, and not
1013*7c478bd9Sstevel@tonic-gate  * deleted by interfaces coming and going.  This is also the routine
1014*7c478bd9Sstevel@tonic-gate  * responsible for adding new static routes to the daemon table.
1015*7c478bd9Sstevel@tonic-gate  */
1016*7c478bd9Sstevel@tonic-gate static void
1017*7c478bd9Sstevel@tonic-gate kern_check_static(struct khash *k, struct interface *ifp)
1018*7c478bd9Sstevel@tonic-gate {
1019*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
1020*7c478bd9Sstevel@tonic-gate 	struct rt_spare new;
1021*7c478bd9Sstevel@tonic-gate 	uint16_t rt_state = RS_STATIC;
1022*7c478bd9Sstevel@tonic-gate 
1023*7c478bd9Sstevel@tonic-gate 	(void) memset(&new, 0, sizeof (new));
1024*7c478bd9Sstevel@tonic-gate 	new.rts_ifp = ifp;
1025*7c478bd9Sstevel@tonic-gate 	new.rts_gate = k->k_gate;
1026*7c478bd9Sstevel@tonic-gate 	new.rts_router = (ifp != NULL) ? ifp->int_addr : loopaddr;
1027*7c478bd9Sstevel@tonic-gate 	new.rts_metric = k->k_metric;
1028*7c478bd9Sstevel@tonic-gate 	new.rts_time = now.tv_sec;
1029*7c478bd9Sstevel@tonic-gate 	new.rts_origin = RO_STATIC;
1030*7c478bd9Sstevel@tonic-gate 
1031*7c478bd9Sstevel@tonic-gate 	rt = rtget(k->k_dst, k->k_mask);
1032*7c478bd9Sstevel@tonic-gate 	if ((ifp != NULL && !IS_IFF_ROUTING(ifp->int_if_flags)) ||
1033*7c478bd9Sstevel@tonic-gate 	    (k->k_state & KS_PRIVATE))
1034*7c478bd9Sstevel@tonic-gate 		rt_state |= RS_NOPROPAGATE;
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate 	if (rt != NULL) {
1037*7c478bd9Sstevel@tonic-gate 		if ((rt->rt_state & RS_STATIC) == 0) {
1038*7c478bd9Sstevel@tonic-gate 			/*
1039*7c478bd9Sstevel@tonic-gate 			 * We are already tracking this dest/mask
1040*7c478bd9Sstevel@tonic-gate 			 * via RIP/RDISC. Ignore the static route,
1041*7c478bd9Sstevel@tonic-gate 			 * because we don't currently have a good
1042*7c478bd9Sstevel@tonic-gate 			 * way to compare metrics on static routes
1043*7c478bd9Sstevel@tonic-gate 			 * with rip metrics, and therefore cannot
1044*7c478bd9Sstevel@tonic-gate 			 * mix and match the two.
1045*7c478bd9Sstevel@tonic-gate 			 */
1046*7c478bd9Sstevel@tonic-gate 			return;
1047*7c478bd9Sstevel@tonic-gate 		}
1048*7c478bd9Sstevel@tonic-gate 		rt_state |= rt->rt_state;
1049*7c478bd9Sstevel@tonic-gate 		if (rt->rt_state != rt_state)
1050*7c478bd9Sstevel@tonic-gate 			rtchange(rt, rt_state, &new, 0);
1051*7c478bd9Sstevel@tonic-gate 	} else {
1052*7c478bd9Sstevel@tonic-gate 		rtadd(k->k_dst, k->k_mask, rt_state, &new);
1053*7c478bd9Sstevel@tonic-gate 	}
1054*7c478bd9Sstevel@tonic-gate }
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate 
1057*7c478bd9Sstevel@tonic-gate /* operate on a kernel entry */
1058*7c478bd9Sstevel@tonic-gate static void
1059*7c478bd9Sstevel@tonic-gate kern_ioctl(struct khash *k,
1060*7c478bd9Sstevel@tonic-gate     int action,			/* RTM_DELETE, etc */
1061*7c478bd9Sstevel@tonic-gate     int flags)
1062*7c478bd9Sstevel@tonic-gate {
1063*7c478bd9Sstevel@tonic-gate 	if (((k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF) ||
1064*7c478bd9Sstevel@tonic-gate 	    (k->k_state & KS_DEPRE_IF)) {
1065*7c478bd9Sstevel@tonic-gate 		/*
1066*7c478bd9Sstevel@tonic-gate 		 * Prevent execution of RTM_DELETE, RTM_ADD or
1067*7c478bd9Sstevel@tonic-gate 		 * RTM_CHANGE of interface routes
1068*7c478bd9Sstevel@tonic-gate 		 */
1069*7c478bd9Sstevel@tonic-gate 		trace_act("Blocking execution of %s  %s --> %s ",
1070*7c478bd9Sstevel@tonic-gate 		    rtm_type_name(action),
1071*7c478bd9Sstevel@tonic-gate 		    addrname(k->k_dst, k->k_mask, 0), naddr_ntoa(k->k_gate));
1072*7c478bd9Sstevel@tonic-gate 		return;
1073*7c478bd9Sstevel@tonic-gate 	}
1074*7c478bd9Sstevel@tonic-gate 
1075*7c478bd9Sstevel@tonic-gate 	switch (action) {
1076*7c478bd9Sstevel@tonic-gate 	case RTM_DELETE:
1077*7c478bd9Sstevel@tonic-gate 		k->k_state &= ~KS_DYNAMIC;
1078*7c478bd9Sstevel@tonic-gate 		if (k->k_state & KS_DELETED)
1079*7c478bd9Sstevel@tonic-gate 			return;
1080*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_DELETED;
1081*7c478bd9Sstevel@tonic-gate 		break;
1082*7c478bd9Sstevel@tonic-gate 	case RTM_ADD:
1083*7c478bd9Sstevel@tonic-gate 		k->k_state &= ~KS_DELETED;
1084*7c478bd9Sstevel@tonic-gate 		break;
1085*7c478bd9Sstevel@tonic-gate 	case RTM_CHANGE:
1086*7c478bd9Sstevel@tonic-gate 		if (k->k_state & KS_DELETED) {
1087*7c478bd9Sstevel@tonic-gate 			action = RTM_ADD;
1088*7c478bd9Sstevel@tonic-gate 			k->k_state &= ~KS_DELETED;
1089*7c478bd9Sstevel@tonic-gate 		}
1090*7c478bd9Sstevel@tonic-gate 		break;
1091*7c478bd9Sstevel@tonic-gate 	}
1092*7c478bd9Sstevel@tonic-gate 
1093*7c478bd9Sstevel@tonic-gate 	rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_ifp,
1094*7c478bd9Sstevel@tonic-gate 	    k->k_metric, flags);
1095*7c478bd9Sstevel@tonic-gate }
1096*7c478bd9Sstevel@tonic-gate 
1097*7c478bd9Sstevel@tonic-gate 
1098*7c478bd9Sstevel@tonic-gate /* add a route the kernel told us */
1099*7c478bd9Sstevel@tonic-gate static void
1100*7c478bd9Sstevel@tonic-gate rtm_add(struct rt_msghdr *rtm,
1101*7c478bd9Sstevel@tonic-gate     struct rt_addrinfo *info,
1102*7c478bd9Sstevel@tonic-gate     time_t keep,
1103*7c478bd9Sstevel@tonic-gate     boolean_t interf_route,
1104*7c478bd9Sstevel@tonic-gate     struct interface *ifptr)
1105*7c478bd9Sstevel@tonic-gate {
1106*7c478bd9Sstevel@tonic-gate 	struct khash *k;
1107*7c478bd9Sstevel@tonic-gate 	struct interface *ifp = ifptr;
1108*7c478bd9Sstevel@tonic-gate 	in_addr_t mask, gate = 0;
1109*7c478bd9Sstevel@tonic-gate 	static struct msg_limit msg_no_ifp;
1110*7c478bd9Sstevel@tonic-gate 
1111*7c478bd9Sstevel@tonic-gate 	if (rtm->rtm_flags & RTF_HOST) {
1112*7c478bd9Sstevel@tonic-gate 		mask = HOST_MASK;
1113*7c478bd9Sstevel@tonic-gate 	} else if (INFO_MASK(info) != 0) {
1114*7c478bd9Sstevel@tonic-gate 		mask = ntohl(S_ADDR(INFO_MASK(info)));
1115*7c478bd9Sstevel@tonic-gate 	} else {
1116*7c478bd9Sstevel@tonic-gate 		writelog(LOG_WARNING,
1117*7c478bd9Sstevel@tonic-gate 		    "ignore %s without mask", rtm_type_name(rtm->rtm_type));
1118*7c478bd9Sstevel@tonic-gate 		return;
1119*7c478bd9Sstevel@tonic-gate 	}
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate 	/*
1122*7c478bd9Sstevel@tonic-gate 	 * Find the interface toward the gateway.
1123*7c478bd9Sstevel@tonic-gate 	 */
1124*7c478bd9Sstevel@tonic-gate 	if (INFO_GATE(info) != NULL)
1125*7c478bd9Sstevel@tonic-gate 		gate = S_ADDR(INFO_GATE(info));
1126*7c478bd9Sstevel@tonic-gate 
1127*7c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
1128*7c478bd9Sstevel@tonic-gate 		if (INFO_GATE(info) != NULL)
1129*7c478bd9Sstevel@tonic-gate 			ifp = iflookup(gate);
1130*7c478bd9Sstevel@tonic-gate 		if (ifp == NULL)
1131*7c478bd9Sstevel@tonic-gate 			msglim(&msg_no_ifp, gate,
1132*7c478bd9Sstevel@tonic-gate 			    "route %s --> %s nexthop is not directly connected",
1133*7c478bd9Sstevel@tonic-gate 			    addrname(S_ADDR(INFO_DST(info)), mask, 0),
1134*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(gate));
1135*7c478bd9Sstevel@tonic-gate 	}
1136*7c478bd9Sstevel@tonic-gate 
1137*7c478bd9Sstevel@tonic-gate 	k = kern_add(S_ADDR(INFO_DST(info)), mask, gate, ifp);
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate 	if (k->k_state & KS_NEW)
1140*7c478bd9Sstevel@tonic-gate 		k->k_keep = now.tv_sec+keep;
1141*7c478bd9Sstevel@tonic-gate 	if (INFO_GATE(info) == 0) {
1142*7c478bd9Sstevel@tonic-gate 		trace_act("note %s without gateway",
1143*7c478bd9Sstevel@tonic-gate 		    rtm_type_name(rtm->rtm_type));
1144*7c478bd9Sstevel@tonic-gate 		k->k_metric = HOPCNT_INFINITY;
1145*7c478bd9Sstevel@tonic-gate 	} else if (INFO_GATE(info)->ss_family != AF_INET) {
1146*7c478bd9Sstevel@tonic-gate 		trace_act("note %s with gateway AF=%d",
1147*7c478bd9Sstevel@tonic-gate 		    rtm_type_name(rtm->rtm_type),
1148*7c478bd9Sstevel@tonic-gate 		    INFO_GATE(info)->ss_family);
1149*7c478bd9Sstevel@tonic-gate 		k->k_metric = HOPCNT_INFINITY;
1150*7c478bd9Sstevel@tonic-gate 	} else {
1151*7c478bd9Sstevel@tonic-gate 		k->k_gate = S_ADDR(INFO_GATE(info));
1152*7c478bd9Sstevel@tonic-gate 		k->k_metric = rtm->rtm_rmx.rmx_hopcount;
1153*7c478bd9Sstevel@tonic-gate 		if (k->k_metric < 0)
1154*7c478bd9Sstevel@tonic-gate 			k->k_metric = 0;
1155*7c478bd9Sstevel@tonic-gate 		else if (k->k_metric > HOPCNT_INFINITY-1)
1156*7c478bd9Sstevel@tonic-gate 			k->k_metric = HOPCNT_INFINITY-1;
1157*7c478bd9Sstevel@tonic-gate 	}
1158*7c478bd9Sstevel@tonic-gate 
1159*7c478bd9Sstevel@tonic-gate 	if ((k->k_state & KS_NEW) && interf_route) {
1160*7c478bd9Sstevel@tonic-gate 		if (k->k_gate != 0 && findifaddr(k->k_gate) == NULL)
1161*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_DEPRE_IF;
1162*7c478bd9Sstevel@tonic-gate 		else
1163*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_IF;
1164*7c478bd9Sstevel@tonic-gate 	}
1165*7c478bd9Sstevel@tonic-gate 
1166*7c478bd9Sstevel@tonic-gate 	k->k_state &= ~(KS_NEW | KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD |
1167*7c478bd9Sstevel@tonic-gate 	    KS_STATIC | KS_GATEWAY | KS_DELETED | KS_PRIVATE | KS_CHECK);
1168*7c478bd9Sstevel@tonic-gate 	if (rtm->rtm_flags & RTF_GATEWAY)
1169*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_GATEWAY;
1170*7c478bd9Sstevel@tonic-gate 	if (rtm->rtm_flags & RTF_STATIC)
1171*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_STATIC;
1172*7c478bd9Sstevel@tonic-gate 	if (rtm->rtm_flags & RTF_PRIVATE)
1173*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_PRIVATE;
1174*7c478bd9Sstevel@tonic-gate 
1175*7c478bd9Sstevel@tonic-gate 
1176*7c478bd9Sstevel@tonic-gate 	if (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED)) {
1177*7c478bd9Sstevel@tonic-gate 		if (INFO_AUTHOR(info) != 0 &&
1178*7c478bd9Sstevel@tonic-gate 		    INFO_AUTHOR(info)->ss_family == AF_INET)
1179*7c478bd9Sstevel@tonic-gate 			ifp = iflookup(S_ADDR(INFO_AUTHOR(info)));
1180*7c478bd9Sstevel@tonic-gate 		else
1181*7c478bd9Sstevel@tonic-gate 			ifp = NULL;
1182*7c478bd9Sstevel@tonic-gate 		if (should_supply(ifp) && (ifp == NULL ||
1183*7c478bd9Sstevel@tonic-gate 		    !(ifp->int_state & IS_REDIRECT_OK))) {
1184*7c478bd9Sstevel@tonic-gate 			/*
1185*7c478bd9Sstevel@tonic-gate 			 * Routers are not supposed to listen to redirects,
1186*7c478bd9Sstevel@tonic-gate 			 * so delete it if it came via an unknown interface
1187*7c478bd9Sstevel@tonic-gate 			 * or the interface does not have special permission.
1188*7c478bd9Sstevel@tonic-gate 			 */
1189*7c478bd9Sstevel@tonic-gate 			k->k_state &= ~KS_DYNAMIC;
1190*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_DELETE;
1191*7c478bd9Sstevel@tonic-gate 			LIM_SEC(need_kern, 0);
1192*7c478bd9Sstevel@tonic-gate 			trace_act("mark for deletion redirected %s --> %s"
1193*7c478bd9Sstevel@tonic-gate 			    " via %s",
1194*7c478bd9Sstevel@tonic-gate 			    addrname(k->k_dst, k->k_mask, 0),
1195*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(k->k_gate),
1196*7c478bd9Sstevel@tonic-gate 			    ifp ? ifp->int_name : "unknown interface");
1197*7c478bd9Sstevel@tonic-gate 		} else {
1198*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_DYNAMIC;
1199*7c478bd9Sstevel@tonic-gate 			k->k_redirect_time = now.tv_sec;
1200*7c478bd9Sstevel@tonic-gate 			trace_act("accept redirected %s --> %s via %s",
1201*7c478bd9Sstevel@tonic-gate 			    addrname(k->k_dst, k->k_mask, 0),
1202*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(k->k_gate),
1203*7c478bd9Sstevel@tonic-gate 			    ifp ? ifp->int_name : "unknown interface");
1204*7c478bd9Sstevel@tonic-gate 		}
1205*7c478bd9Sstevel@tonic-gate 		return;
1206*7c478bd9Sstevel@tonic-gate 	}
1207*7c478bd9Sstevel@tonic-gate 
1208*7c478bd9Sstevel@tonic-gate 	/*
1209*7c478bd9Sstevel@tonic-gate 	 * If it is not a static route, quit until the next comparison
1210*7c478bd9Sstevel@tonic-gate 	 * between the kernel and daemon tables, when it will be deleted.
1211*7c478bd9Sstevel@tonic-gate 	 */
1212*7c478bd9Sstevel@tonic-gate 	if (!(k->k_state & KS_STATIC)) {
1213*7c478bd9Sstevel@tonic-gate 		if (!(k->k_state & (KS_IF|KS_DEPRE_IF|KS_FILE)))
1214*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_DELETE;
1215*7c478bd9Sstevel@tonic-gate 		LIM_SEC(need_kern, k->k_keep);
1216*7c478bd9Sstevel@tonic-gate 		return;
1217*7c478bd9Sstevel@tonic-gate 	}
1218*7c478bd9Sstevel@tonic-gate 
1219*7c478bd9Sstevel@tonic-gate 	/*
1220*7c478bd9Sstevel@tonic-gate 	 * Put static routes with real metrics into the daemon table so
1221*7c478bd9Sstevel@tonic-gate 	 * they can be advertised.
1222*7c478bd9Sstevel@tonic-gate 	 */
1223*7c478bd9Sstevel@tonic-gate 
1224*7c478bd9Sstevel@tonic-gate 	kern_check_static(k, ifp);
1225*7c478bd9Sstevel@tonic-gate }
1226*7c478bd9Sstevel@tonic-gate 
1227*7c478bd9Sstevel@tonic-gate 
1228*7c478bd9Sstevel@tonic-gate /* deal with packet loss */
1229*7c478bd9Sstevel@tonic-gate static void
1230*7c478bd9Sstevel@tonic-gate rtm_lose(struct rt_msghdr *rtm, struct rt_addrinfo *info)
1231*7c478bd9Sstevel@tonic-gate {
1232*7c478bd9Sstevel@tonic-gate 	if (INFO_GATE(info) == NULL || INFO_GATE(info)->ss_family != AF_INET) {
1233*7c478bd9Sstevel@tonic-gate 		trace_act("ignore %s without gateway",
1234*7c478bd9Sstevel@tonic-gate 		    rtm_type_name(rtm->rtm_type));
1235*7c478bd9Sstevel@tonic-gate 		age(0);
1236*7c478bd9Sstevel@tonic-gate 		return;
1237*7c478bd9Sstevel@tonic-gate 	}
1238*7c478bd9Sstevel@tonic-gate 
1239*7c478bd9Sstevel@tonic-gate 	if (rdisc_ok)
1240*7c478bd9Sstevel@tonic-gate 		rdisc_age(S_ADDR(INFO_GATE(info)));
1241*7c478bd9Sstevel@tonic-gate 	age(S_ADDR(INFO_GATE(info)));
1242*7c478bd9Sstevel@tonic-gate }
1243*7c478bd9Sstevel@tonic-gate 
1244*7c478bd9Sstevel@tonic-gate 
1245*7c478bd9Sstevel@tonic-gate /*
1246*7c478bd9Sstevel@tonic-gate  * Make the gateway slot of an info structure point to something
1247*7c478bd9Sstevel@tonic-gate  * useful.  If it is not already useful, but it specifies an interface,
1248*7c478bd9Sstevel@tonic-gate  * then fill in the sockaddr_in provided and point it there.
1249*7c478bd9Sstevel@tonic-gate  */
1250*7c478bd9Sstevel@tonic-gate static int
1251*7c478bd9Sstevel@tonic-gate get_info_gate(struct sockaddr_storage **ssp, struct sockaddr_in *sin)
1252*7c478bd9Sstevel@tonic-gate {
1253*7c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)*ssp;
1254*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
1255*7c478bd9Sstevel@tonic-gate 
1256*7c478bd9Sstevel@tonic-gate 	if (sdl == NULL)
1257*7c478bd9Sstevel@tonic-gate 		return (0);
1258*7c478bd9Sstevel@tonic-gate 	if ((sdl)->sdl_family == AF_INET)
1259*7c478bd9Sstevel@tonic-gate 		return (1);
1260*7c478bd9Sstevel@tonic-gate 	if ((sdl)->sdl_family != AF_LINK)
1261*7c478bd9Sstevel@tonic-gate 		return (0);
1262*7c478bd9Sstevel@tonic-gate 
1263*7c478bd9Sstevel@tonic-gate 	ifp = ifwithindex(sdl->sdl_index, _B_TRUE);
1264*7c478bd9Sstevel@tonic-gate 	if (ifp == NULL)
1265*7c478bd9Sstevel@tonic-gate 		return (0);
1266*7c478bd9Sstevel@tonic-gate 
1267*7c478bd9Sstevel@tonic-gate 	sin->sin_addr.s_addr = ifp->int_addr;
1268*7c478bd9Sstevel@tonic-gate 	sin->sin_family = AF_INET;
1269*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1270*7c478bd9Sstevel@tonic-gate 	*ssp = (struct sockaddr_storage *)sin;
1271*7c478bd9Sstevel@tonic-gate 
1272*7c478bd9Sstevel@tonic-gate 	return (1);
1273*7c478bd9Sstevel@tonic-gate }
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate 
1276*7c478bd9Sstevel@tonic-gate /*
1277*7c478bd9Sstevel@tonic-gate  * Clean the kernel table by copying it to the daemon image.
1278*7c478bd9Sstevel@tonic-gate  * Eventually the daemon will delete any extra routes.
1279*7c478bd9Sstevel@tonic-gate  */
1280*7c478bd9Sstevel@tonic-gate void
1281*7c478bd9Sstevel@tonic-gate sync_kern(void)
1282*7c478bd9Sstevel@tonic-gate {
1283*7c478bd9Sstevel@tonic-gate 	int i;
1284*7c478bd9Sstevel@tonic-gate 	struct khash *k;
1285*7c478bd9Sstevel@tonic-gate 	struct {
1286*7c478bd9Sstevel@tonic-gate 		struct T_optmgmt_req req;
1287*7c478bd9Sstevel@tonic-gate 		struct opthdr hdr;
1288*7c478bd9Sstevel@tonic-gate 	} req;
1289*7c478bd9Sstevel@tonic-gate 	union {
1290*7c478bd9Sstevel@tonic-gate 		struct T_optmgmt_ack ack;
1291*7c478bd9Sstevel@tonic-gate 		unsigned char space[64];
1292*7c478bd9Sstevel@tonic-gate 	} ack;
1293*7c478bd9Sstevel@tonic-gate 	struct opthdr *rh;
1294*7c478bd9Sstevel@tonic-gate 	struct strbuf cbuf, dbuf;
1295*7c478bd9Sstevel@tonic-gate 	int ipfd, nroutes, flags, r;
1296*7c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t routes[8];
1297*7c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t *rp;
1298*7c478bd9Sstevel@tonic-gate 	struct rt_msghdr rtm;
1299*7c478bd9Sstevel@tonic-gate 	struct rt_addrinfo info;
1300*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin_dst;
1301*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin_gate;
1302*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin_mask;
1303*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin_author;
1304*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
1305*7c478bd9Sstevel@tonic-gate 	char ifname[LIFNAMSIZ + 1];
1306*7c478bd9Sstevel@tonic-gate 
1307*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KHASH_SIZE; i++) {
1308*7c478bd9Sstevel@tonic-gate 		for (k = khash_bins[i]; k != NULL; k = k->k_next) {
1309*7c478bd9Sstevel@tonic-gate 			if (!(k->k_state & (KS_IF|KS_DEPRE_IF)))
1310*7c478bd9Sstevel@tonic-gate 				k->k_state |= KS_CHECK;
1311*7c478bd9Sstevel@tonic-gate 		}
1312*7c478bd9Sstevel@tonic-gate 	}
1313*7c478bd9Sstevel@tonic-gate 
1314*7c478bd9Sstevel@tonic-gate 	ipfd = open(IP_DEV_NAME, O_RDWR);
1315*7c478bd9Sstevel@tonic-gate 	if (ipfd == -1) {
1316*7c478bd9Sstevel@tonic-gate 		msglog("open " IP_DEV_NAME ": %s", rip_strerror(errno));
1317*7c478bd9Sstevel@tonic-gate 		goto hash_clean;
1318*7c478bd9Sstevel@tonic-gate 	}
1319*7c478bd9Sstevel@tonic-gate 
1320*7c478bd9Sstevel@tonic-gate 	req.req.PRIM_type = T_OPTMGMT_REQ;
1321*7c478bd9Sstevel@tonic-gate 	req.req.OPT_offset = (caddr_t)&req.hdr - (caddr_t)&req;
1322*7c478bd9Sstevel@tonic-gate 	req.req.OPT_length = sizeof (req.hdr);
1323*7c478bd9Sstevel@tonic-gate 	req.req.MGMT_flags = T_CURRENT;
1324*7c478bd9Sstevel@tonic-gate 
1325*7c478bd9Sstevel@tonic-gate 	req.hdr.level = MIB2_IP;
1326*7c478bd9Sstevel@tonic-gate 	req.hdr.name = 0;
1327*7c478bd9Sstevel@tonic-gate 	req.hdr.len = 0;
1328*7c478bd9Sstevel@tonic-gate 
1329*7c478bd9Sstevel@tonic-gate 	cbuf.buf = (caddr_t)&req;
1330*7c478bd9Sstevel@tonic-gate 	cbuf.len = sizeof (req);
1331*7c478bd9Sstevel@tonic-gate 
1332*7c478bd9Sstevel@tonic-gate 	if (putmsg(ipfd, &cbuf, NULL, 0) == -1) {
1333*7c478bd9Sstevel@tonic-gate 		msglog("T_OPTMGMT_REQ putmsg: %s", rip_strerror(errno));
1334*7c478bd9Sstevel@tonic-gate 		goto hash_clean;
1335*7c478bd9Sstevel@tonic-gate 	}
1336*7c478bd9Sstevel@tonic-gate 
1337*7c478bd9Sstevel@tonic-gate 	for (;;) {
1338*7c478bd9Sstevel@tonic-gate 		cbuf.buf = (caddr_t)&ack;
1339*7c478bd9Sstevel@tonic-gate 		cbuf.maxlen = sizeof (ack);
1340*7c478bd9Sstevel@tonic-gate 		dbuf.buf = (caddr_t)routes;
1341*7c478bd9Sstevel@tonic-gate 		dbuf.maxlen = sizeof (routes);
1342*7c478bd9Sstevel@tonic-gate 		flags = 0;
1343*7c478bd9Sstevel@tonic-gate 		r = getmsg(ipfd, &cbuf, &dbuf, &flags);
1344*7c478bd9Sstevel@tonic-gate 		if (r == -1) {
1345*7c478bd9Sstevel@tonic-gate 			msglog("T_OPTMGMT_REQ getmsg: %s", rip_strerror(errno));
1346*7c478bd9Sstevel@tonic-gate 			goto hash_clean;
1347*7c478bd9Sstevel@tonic-gate 		}
1348*7c478bd9Sstevel@tonic-gate 
1349*7c478bd9Sstevel@tonic-gate 		if (cbuf.len < sizeof (struct T_optmgmt_ack) ||
1350*7c478bd9Sstevel@tonic-gate 		    ack.ack.PRIM_type != T_OPTMGMT_ACK ||
1351*7c478bd9Sstevel@tonic-gate 		    ack.ack.MGMT_flags != T_SUCCESS ||
1352*7c478bd9Sstevel@tonic-gate 		    ack.ack.OPT_length < sizeof (struct opthdr)) {
1353*7c478bd9Sstevel@tonic-gate 			msglog("bad T_OPTMGMT response; len=%d prim=%d "
1354*7c478bd9Sstevel@tonic-gate 			    "flags=%d optlen=%d", cbuf.len, ack.ack.PRIM_type,
1355*7c478bd9Sstevel@tonic-gate 			    ack.ack.MGMT_flags, ack.ack.OPT_length);
1356*7c478bd9Sstevel@tonic-gate 			goto hash_clean;
1357*7c478bd9Sstevel@tonic-gate 		}
1358*7c478bd9Sstevel@tonic-gate 		/* LINTED */
1359*7c478bd9Sstevel@tonic-gate 		rh = (struct opthdr *)((caddr_t)&ack + ack.ack.OPT_offset);
1360*7c478bd9Sstevel@tonic-gate 		if (rh->level == 0 && rh->name == 0) {
1361*7c478bd9Sstevel@tonic-gate 			break;
1362*7c478bd9Sstevel@tonic-gate 		}
1363*7c478bd9Sstevel@tonic-gate 		if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) {
1364*7c478bd9Sstevel@tonic-gate 			while (r == MOREDATA) {
1365*7c478bd9Sstevel@tonic-gate 				r = getmsg(ipfd, NULL, &dbuf, &flags);
1366*7c478bd9Sstevel@tonic-gate 			}
1367*7c478bd9Sstevel@tonic-gate 			continue;
1368*7c478bd9Sstevel@tonic-gate 		}
1369*7c478bd9Sstevel@tonic-gate 		break;
1370*7c478bd9Sstevel@tonic-gate 	}
1371*7c478bd9Sstevel@tonic-gate 
1372*7c478bd9Sstevel@tonic-gate 	(void) memset(&rtm, 0, sizeof (rtm));
1373*7c478bd9Sstevel@tonic-gate 	(void) memset(&info, 0, sizeof (info));
1374*7c478bd9Sstevel@tonic-gate 	(void) memset(&sin_dst, 0, sizeof (sin_dst));
1375*7c478bd9Sstevel@tonic-gate 	(void) memset(&sin_gate, 0, sizeof (sin_gate));
1376*7c478bd9Sstevel@tonic-gate 	(void) memset(&sin_mask, 0, sizeof (sin_mask));
1377*7c478bd9Sstevel@tonic-gate 	(void) memset(&sin_author, 0, sizeof (sin_author));
1378*7c478bd9Sstevel@tonic-gate 	sin_dst.sin_family = AF_INET;
1379*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1380*7c478bd9Sstevel@tonic-gate 	info.rti_info[RTAX_DST] = (struct sockaddr_storage *)&sin_dst;
1381*7c478bd9Sstevel@tonic-gate 	sin_gate.sin_family = AF_INET;
1382*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1383*7c478bd9Sstevel@tonic-gate 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr_storage *)&sin_gate;
1384*7c478bd9Sstevel@tonic-gate 	sin_mask.sin_family = AF_INET;
1385*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1386*7c478bd9Sstevel@tonic-gate 	info.rti_info[RTAX_NETMASK] = (struct sockaddr_storage *)&sin_mask;
1387*7c478bd9Sstevel@tonic-gate 	sin_dst.sin_family = AF_INET;
1388*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1389*7c478bd9Sstevel@tonic-gate 	info.rti_info[RTAX_AUTHOR] = (struct sockaddr_storage *)&sin_author;
1390*7c478bd9Sstevel@tonic-gate 
1391*7c478bd9Sstevel@tonic-gate 	for (;;) {
1392*7c478bd9Sstevel@tonic-gate 		nroutes = dbuf.len / sizeof (mib2_ipRouteEntry_t);
1393*7c478bd9Sstevel@tonic-gate 		for (rp = routes; nroutes > 0; ++rp, nroutes--) {
1394*7c478bd9Sstevel@tonic-gate 
1395*7c478bd9Sstevel@tonic-gate 			/*
1396*7c478bd9Sstevel@tonic-gate 			 * Ignore IRE cache, broadcast, and local address
1397*7c478bd9Sstevel@tonic-gate 			 * entries; they're not subject to routing socket
1398*7c478bd9Sstevel@tonic-gate 			 * control.
1399*7c478bd9Sstevel@tonic-gate 			 */
1400*7c478bd9Sstevel@tonic-gate 			if (rp->ipRouteInfo.re_ire_type &
1401*7c478bd9Sstevel@tonic-gate 			    (IRE_BROADCAST | IRE_CACHE | IRE_LOCAL))
1402*7c478bd9Sstevel@tonic-gate 				continue;
1403*7c478bd9Sstevel@tonic-gate 
1404*7c478bd9Sstevel@tonic-gate 			/* ignore multicast addresses */
1405*7c478bd9Sstevel@tonic-gate 			if (IN_MULTICAST(ntohl(rp->ipRouteDest)))
1406*7c478bd9Sstevel@tonic-gate 				continue;
1407*7c478bd9Sstevel@tonic-gate 
1408*7c478bd9Sstevel@tonic-gate 
1409*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_KERNEL_ROUTE_READ
1410*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "route type %d, ire type %08X, "
1411*7c478bd9Sstevel@tonic-gate 			    "flags %08X: %s", rp->ipRouteType,
1412*7c478bd9Sstevel@tonic-gate 			    rp->ipRouteInfo.re_ire_type,
1413*7c478bd9Sstevel@tonic-gate 			    rp->ipRouteInfo.re_flags,
1414*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(rp->ipRouteDest));
1415*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %s",
1416*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(rp->ipRouteMask));
1417*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " %s\n",
1418*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(rp->ipRouteNextHop));
1419*7c478bd9Sstevel@tonic-gate #endif
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate 			/* Fake up the needed entries */
1422*7c478bd9Sstevel@tonic-gate 			rtm.rtm_flags = rp->ipRouteInfo.re_flags;
1423*7c478bd9Sstevel@tonic-gate 			rtm.rtm_type = RTM_GET;
1424*7c478bd9Sstevel@tonic-gate 			rtm.rtm_rmx.rmx_hopcount = rp->ipRouteMetric1;
1425*7c478bd9Sstevel@tonic-gate 
1426*7c478bd9Sstevel@tonic-gate 			(void) memset(ifname, 0, sizeof (ifname));
1427*7c478bd9Sstevel@tonic-gate 			if (rp->ipRouteIfIndex.o_length <
1428*7c478bd9Sstevel@tonic-gate 			    sizeof (rp->ipRouteIfIndex.o_bytes))
1429*7c478bd9Sstevel@tonic-gate 				rp->ipRouteIfIndex.o_bytes[
1430*7c478bd9Sstevel@tonic-gate 				    rp->ipRouteIfIndex.o_length] = '\0';
1431*7c478bd9Sstevel@tonic-gate 				(void) strncpy(ifname,
1432*7c478bd9Sstevel@tonic-gate 				    rp->ipRouteIfIndex.o_bytes,
1433*7c478bd9Sstevel@tonic-gate 				    sizeof (ifname));
1434*7c478bd9Sstevel@tonic-gate 
1435*7c478bd9Sstevel@tonic-gate 			/*
1436*7c478bd9Sstevel@tonic-gate 			 * First try to match up on gwkludge entries
1437*7c478bd9Sstevel@tonic-gate 			 * before trying to match ifp by name.
1438*7c478bd9Sstevel@tonic-gate 			 */
1439*7c478bd9Sstevel@tonic-gate 			if ((ifp = gwkludge_iflookup(rp->ipRouteDest,
1440*7c478bd9Sstevel@tonic-gate 			    rp->ipRouteNextHop, rp->ipRouteMask)) == NULL)
1441*7c478bd9Sstevel@tonic-gate 				ifp = ifwithname(ifname);
1442*7c478bd9Sstevel@tonic-gate 
1443*7c478bd9Sstevel@tonic-gate 			info.rti_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
1444*7c478bd9Sstevel@tonic-gate 			if (rp->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
1445*7c478bd9Sstevel@tonic-gate 				info.rti_addrs |= RTA_AUTHOR;
1446*7c478bd9Sstevel@tonic-gate 			sin_dst.sin_addr.s_addr = rp->ipRouteDest;
1447*7c478bd9Sstevel@tonic-gate 			sin_gate.sin_addr.s_addr = rp->ipRouteNextHop;
1448*7c478bd9Sstevel@tonic-gate 			sin_mask.sin_addr.s_addr = rp->ipRouteMask;
1449*7c478bd9Sstevel@tonic-gate 			sin_author.sin_addr.s_addr =
1450*7c478bd9Sstevel@tonic-gate 			    rp->ipRouteInfo.re_src_addr;
1451*7c478bd9Sstevel@tonic-gate 
1452*7c478bd9Sstevel@tonic-gate 			/*
1453*7c478bd9Sstevel@tonic-gate 			 * Note static routes and interface routes, and also
1454*7c478bd9Sstevel@tonic-gate 			 * preload the image of the kernel table so that
1455*7c478bd9Sstevel@tonic-gate 			 * we can later clean it, as well as avoid making
1456*7c478bd9Sstevel@tonic-gate 			 * unneeded changes.  Keep the old kernel routes for a
1457*7c478bd9Sstevel@tonic-gate 			 * few seconds to allow a RIP or router-discovery
1458*7c478bd9Sstevel@tonic-gate 			 * response to be heard.
1459*7c478bd9Sstevel@tonic-gate 			 */
1460*7c478bd9Sstevel@tonic-gate 			rtm_add(&rtm, &info, MAX_WAITTIME,
1461*7c478bd9Sstevel@tonic-gate 			    ((rp->ipRouteInfo.re_ire_type &
1462*7c478bd9Sstevel@tonic-gate 			    (IRE_INTERFACE|IRE_LOOPBACK)) != 0), ifp);
1463*7c478bd9Sstevel@tonic-gate 		}
1464*7c478bd9Sstevel@tonic-gate 		if (r == 0) {
1465*7c478bd9Sstevel@tonic-gate 			break;
1466*7c478bd9Sstevel@tonic-gate 		}
1467*7c478bd9Sstevel@tonic-gate 		r = getmsg(ipfd, NULL, &dbuf, &flags);
1468*7c478bd9Sstevel@tonic-gate 	}
1469*7c478bd9Sstevel@tonic-gate 
1470*7c478bd9Sstevel@tonic-gate hash_clean:
1471*7c478bd9Sstevel@tonic-gate 	if (ipfd != -1)
1472*7c478bd9Sstevel@tonic-gate 		(void) close(ipfd);
1473*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KHASH_SIZE; i++) {
1474*7c478bd9Sstevel@tonic-gate 		for (k = khash_bins[i]; k != NULL; k = k->k_next) {
1475*7c478bd9Sstevel@tonic-gate 
1476*7c478bd9Sstevel@tonic-gate 			/*
1477*7c478bd9Sstevel@tonic-gate 			 * KS_DELETED routes have been removed from the
1478*7c478bd9Sstevel@tonic-gate 			 * kernel, but we keep them around for reasons
1479*7c478bd9Sstevel@tonic-gate 			 * stated in del_static(), so we skip the check
1480*7c478bd9Sstevel@tonic-gate 			 * for KS_DELETED routes here.
1481*7c478bd9Sstevel@tonic-gate 			 */
1482*7c478bd9Sstevel@tonic-gate 			if ((k->k_state & (KS_CHECK|KS_DELETED)) == KS_CHECK) {
1483*7c478bd9Sstevel@tonic-gate 
1484*7c478bd9Sstevel@tonic-gate 				if (!(k->k_state & KS_DYNAMIC))
1485*7c478bd9Sstevel@tonic-gate 				    writelog(LOG_WARNING,
1486*7c478bd9Sstevel@tonic-gate 					"%s --> %s disappeared from kernel",
1487*7c478bd9Sstevel@tonic-gate 					addrname(k->k_dst, k->k_mask, 0),
1488*7c478bd9Sstevel@tonic-gate 					naddr_ntoa(k->k_gate));
1489*7c478bd9Sstevel@tonic-gate 				del_static(k->k_dst, k->k_mask, k->k_gate,
1490*7c478bd9Sstevel@tonic-gate 				    k->k_ifp, 1);
1491*7c478bd9Sstevel@tonic-gate 
1492*7c478bd9Sstevel@tonic-gate 			}
1493*7c478bd9Sstevel@tonic-gate 		}
1494*7c478bd9Sstevel@tonic-gate 	}
1495*7c478bd9Sstevel@tonic-gate }
1496*7c478bd9Sstevel@tonic-gate 
1497*7c478bd9Sstevel@tonic-gate 
1498*7c478bd9Sstevel@tonic-gate /* Listen to announcements from the kernel */
1499*7c478bd9Sstevel@tonic-gate void
1500*7c478bd9Sstevel@tonic-gate read_rt(void)
1501*7c478bd9Sstevel@tonic-gate {
1502*7c478bd9Sstevel@tonic-gate 	long cc;
1503*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
1504*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in gate_sin;
1505*7c478bd9Sstevel@tonic-gate 	in_addr_t mask, gate;
1506*7c478bd9Sstevel@tonic-gate 	union {
1507*7c478bd9Sstevel@tonic-gate 		struct {
1508*7c478bd9Sstevel@tonic-gate 			struct rt_msghdr rtm;
1509*7c478bd9Sstevel@tonic-gate 			struct sockaddr_storage addrs[RTA_NUMBITS];
1510*7c478bd9Sstevel@tonic-gate 		} r;
1511*7c478bd9Sstevel@tonic-gate 		struct if_msghdr ifm;
1512*7c478bd9Sstevel@tonic-gate 	} m;
1513*7c478bd9Sstevel@tonic-gate 	char str[100], *strp;
1514*7c478bd9Sstevel@tonic-gate 	struct rt_addrinfo info;
1515*7c478bd9Sstevel@tonic-gate 
1516*7c478bd9Sstevel@tonic-gate 
1517*7c478bd9Sstevel@tonic-gate 	for (;;) {
1518*7c478bd9Sstevel@tonic-gate 		cc = read(rt_sock, &m, sizeof (m));
1519*7c478bd9Sstevel@tonic-gate 		if (cc <= 0) {
1520*7c478bd9Sstevel@tonic-gate 			if (cc < 0 && errno != EWOULDBLOCK)
1521*7c478bd9Sstevel@tonic-gate 				LOGERR("read(rt_sock)");
1522*7c478bd9Sstevel@tonic-gate 			return;
1523*7c478bd9Sstevel@tonic-gate 		}
1524*7c478bd9Sstevel@tonic-gate 
1525*7c478bd9Sstevel@tonic-gate 		if (TRACERTS)
1526*7c478bd9Sstevel@tonic-gate 			dump_rt_msg("read", &m.r.rtm, cc);
1527*7c478bd9Sstevel@tonic-gate 
1528*7c478bd9Sstevel@tonic-gate 		if (cc < m.r.rtm.rtm_msglen) {
1529*7c478bd9Sstevel@tonic-gate 			msglog("routing message truncated (%d < %d)",
1530*7c478bd9Sstevel@tonic-gate 			    cc, m.r.rtm.rtm_msglen);
1531*7c478bd9Sstevel@tonic-gate 		}
1532*7c478bd9Sstevel@tonic-gate 
1533*7c478bd9Sstevel@tonic-gate 		if (m.r.rtm.rtm_version != RTM_VERSION) {
1534*7c478bd9Sstevel@tonic-gate 			msglog("bogus routing message version %d",
1535*7c478bd9Sstevel@tonic-gate 			    m.r.rtm.rtm_version);
1536*7c478bd9Sstevel@tonic-gate 			continue;
1537*7c478bd9Sstevel@tonic-gate 		}
1538*7c478bd9Sstevel@tonic-gate 
1539*7c478bd9Sstevel@tonic-gate 		ifp = NULL;
1540*7c478bd9Sstevel@tonic-gate 
1541*7c478bd9Sstevel@tonic-gate 		if (m.r.rtm.rtm_type == RTM_IFINFO ||
1542*7c478bd9Sstevel@tonic-gate 		    m.r.rtm.rtm_type == RTM_NEWADDR ||
1543*7c478bd9Sstevel@tonic-gate 		    m.r.rtm.rtm_type == RTM_DELADDR) {
1544*7c478bd9Sstevel@tonic-gate 			strp = if_bit_string(m.ifm.ifm_flags, _B_TRUE);
1545*7c478bd9Sstevel@tonic-gate 			if (strp == NULL) {
1546*7c478bd9Sstevel@tonic-gate 				strp = str;
1547*7c478bd9Sstevel@tonic-gate 				(void) sprintf(str, "%#x", m.ifm.ifm_flags);
1548*7c478bd9Sstevel@tonic-gate 			}
1549*7c478bd9Sstevel@tonic-gate 			ifp = ifwithindex(m.ifm.ifm_index,
1550*7c478bd9Sstevel@tonic-gate 			    m.r.rtm.rtm_type != RTM_DELADDR);
1551*7c478bd9Sstevel@tonic-gate 			if (ifp == NULL) {
1552*7c478bd9Sstevel@tonic-gate 				char ifname[LIFNAMSIZ], *ifnamep;
1553*7c478bd9Sstevel@tonic-gate 
1554*7c478bd9Sstevel@tonic-gate 				ifnamep = if_indextoname(m.ifm.ifm_index,
1555*7c478bd9Sstevel@tonic-gate 				    ifname);
1556*7c478bd9Sstevel@tonic-gate 				if (ifnamep == NULL) {
1557*7c478bd9Sstevel@tonic-gate 					trace_act("note %s with flags %s"
1558*7c478bd9Sstevel@tonic-gate 					    " for unknown interface index #%d",
1559*7c478bd9Sstevel@tonic-gate 					    rtm_type_name(m.r.rtm.rtm_type),
1560*7c478bd9Sstevel@tonic-gate 					    strp, m.ifm.ifm_index);
1561*7c478bd9Sstevel@tonic-gate 				} else {
1562*7c478bd9Sstevel@tonic-gate 					trace_act("note %s with flags %s"
1563*7c478bd9Sstevel@tonic-gate 					    " for unknown interface %s",
1564*7c478bd9Sstevel@tonic-gate 					    rtm_type_name(m.r.rtm.rtm_type),
1565*7c478bd9Sstevel@tonic-gate 					    strp, ifnamep);
1566*7c478bd9Sstevel@tonic-gate 				}
1567*7c478bd9Sstevel@tonic-gate 			} else {
1568*7c478bd9Sstevel@tonic-gate 				trace_act("note %s with flags %s for %s",
1569*7c478bd9Sstevel@tonic-gate 				    rtm_type_name(m.r.rtm.rtm_type),
1570*7c478bd9Sstevel@tonic-gate 				    strp, ifp->int_name);
1571*7c478bd9Sstevel@tonic-gate 			}
1572*7c478bd9Sstevel@tonic-gate 			if (strp != str)
1573*7c478bd9Sstevel@tonic-gate 				free(strp);
1574*7c478bd9Sstevel@tonic-gate 
1575*7c478bd9Sstevel@tonic-gate 			/*
1576*7c478bd9Sstevel@tonic-gate 			 * After being informed of a change to an interface,
1577*7c478bd9Sstevel@tonic-gate 			 * check them all now if the check would otherwise
1578*7c478bd9Sstevel@tonic-gate 			 * be a long time from now, if the interface is
1579*7c478bd9Sstevel@tonic-gate 			 * not known, or if the interface has been turned
1580*7c478bd9Sstevel@tonic-gate 			 * off or on.
1581*7c478bd9Sstevel@tonic-gate 			 */
1582*7c478bd9Sstevel@tonic-gate 			if (ifscan_timer.tv_sec-now.tv_sec >=
1583*7c478bd9Sstevel@tonic-gate 			    CHECK_BAD_INTERVAL || ifp == NULL ||
1584*7c478bd9Sstevel@tonic-gate 			    ((ifp->int_if_flags ^ m.ifm.ifm_flags) &
1585*7c478bd9Sstevel@tonic-gate 				IFF_UP) != 0)
1586*7c478bd9Sstevel@tonic-gate 				ifscan_timer.tv_sec = now.tv_sec;
1587*7c478bd9Sstevel@tonic-gate 			continue;
1588*7c478bd9Sstevel@tonic-gate 		} else {
1589*7c478bd9Sstevel@tonic-gate 			if (m.r.rtm.rtm_index != 0)
1590*7c478bd9Sstevel@tonic-gate 				ifp = ifwithindex(m.r.rtm.rtm_index, 1);
1591*7c478bd9Sstevel@tonic-gate 		}
1592*7c478bd9Sstevel@tonic-gate 
1593*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(str, rtm_type_name(m.r.rtm.rtm_type),
1594*7c478bd9Sstevel@tonic-gate 		    sizeof (str));
1595*7c478bd9Sstevel@tonic-gate 		strp = &str[strlen(str)];
1596*7c478bd9Sstevel@tonic-gate 		if (m.r.rtm.rtm_type <= RTM_CHANGE)
1597*7c478bd9Sstevel@tonic-gate 			strp += snprintf(strp, sizeof (str) - (strp - str),
1598*7c478bd9Sstevel@tonic-gate 			    " from pid %d", (int)m.r.rtm.rtm_pid);
1599*7c478bd9Sstevel@tonic-gate 
1600*7c478bd9Sstevel@tonic-gate 		/* LINTED */
1601*7c478bd9Sstevel@tonic-gate 		(void) rt_xaddrs(&info, (struct sockaddr_storage *)(&m.r.rtm +
1602*7c478bd9Sstevel@tonic-gate 		    1), (char *)&m + cc, m.r.rtm.rtm_addrs);
1603*7c478bd9Sstevel@tonic-gate 
1604*7c478bd9Sstevel@tonic-gate 		if (INFO_DST(&info) == 0) {
1605*7c478bd9Sstevel@tonic-gate 			trace_act("ignore %s without dst", str);
1606*7c478bd9Sstevel@tonic-gate 			continue;
1607*7c478bd9Sstevel@tonic-gate 		}
1608*7c478bd9Sstevel@tonic-gate 
1609*7c478bd9Sstevel@tonic-gate 		if (INFO_DST(&info)->ss_family != AF_INET) {
1610*7c478bd9Sstevel@tonic-gate 			trace_act("ignore %s for AF %d", str,
1611*7c478bd9Sstevel@tonic-gate 			    INFO_DST(&info)->ss_family);
1612*7c478bd9Sstevel@tonic-gate 			continue;
1613*7c478bd9Sstevel@tonic-gate 		}
1614*7c478bd9Sstevel@tonic-gate 
1615*7c478bd9Sstevel@tonic-gate 		mask = ((INFO_MASK(&info) != 0) ?
1616*7c478bd9Sstevel@tonic-gate 		    ntohl(S_ADDR(INFO_MASK(&info))) :
1617*7c478bd9Sstevel@tonic-gate 		    (m.r.rtm.rtm_flags & RTF_HOST) ?
1618*7c478bd9Sstevel@tonic-gate 		    HOST_MASK : std_mask(S_ADDR(INFO_DST(&info))));
1619*7c478bd9Sstevel@tonic-gate 
1620*7c478bd9Sstevel@tonic-gate 		strp += snprintf(strp, sizeof (str) - (strp - str), ": %s",
1621*7c478bd9Sstevel@tonic-gate 		    addrname(S_ADDR(INFO_DST(&info)), mask, 0));
1622*7c478bd9Sstevel@tonic-gate 
1623*7c478bd9Sstevel@tonic-gate 		if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) {
1624*7c478bd9Sstevel@tonic-gate 			trace_act("ignore multicast %s", str);
1625*7c478bd9Sstevel@tonic-gate 			continue;
1626*7c478bd9Sstevel@tonic-gate 		}
1627*7c478bd9Sstevel@tonic-gate 
1628*7c478bd9Sstevel@tonic-gate 		if (m.r.rtm.rtm_flags & RTF_LLINFO) {
1629*7c478bd9Sstevel@tonic-gate 			trace_act("ignore ARP %s", str);
1630*7c478bd9Sstevel@tonic-gate 			continue;
1631*7c478bd9Sstevel@tonic-gate 		}
1632*7c478bd9Sstevel@tonic-gate 
1633*7c478bd9Sstevel@tonic-gate 		if (get_info_gate(&INFO_GATE(&info), &gate_sin)) {
1634*7c478bd9Sstevel@tonic-gate 			gate = S_ADDR(INFO_GATE(&info));
1635*7c478bd9Sstevel@tonic-gate 			strp += snprintf(strp, sizeof (str) - (strp - str),
1636*7c478bd9Sstevel@tonic-gate 			    " --> %s", naddr_ntoa(gate));
1637*7c478bd9Sstevel@tonic-gate 		} else {
1638*7c478bd9Sstevel@tonic-gate 			gate = 0;
1639*7c478bd9Sstevel@tonic-gate 		}
1640*7c478bd9Sstevel@tonic-gate 
1641*7c478bd9Sstevel@tonic-gate 		if (INFO_AUTHOR(&info) != 0)
1642*7c478bd9Sstevel@tonic-gate 			strp += snprintf(strp, sizeof (str) - (strp - str),
1643*7c478bd9Sstevel@tonic-gate 			    " by authority of %s",
1644*7c478bd9Sstevel@tonic-gate 			    saddr_ntoa(INFO_AUTHOR(&info)));
1645*7c478bd9Sstevel@tonic-gate 
1646*7c478bd9Sstevel@tonic-gate 		switch (m.r.rtm.rtm_type) {
1647*7c478bd9Sstevel@tonic-gate 		case RTM_ADD:
1648*7c478bd9Sstevel@tonic-gate 		case RTM_CHANGE:
1649*7c478bd9Sstevel@tonic-gate 		case RTM_REDIRECT:
1650*7c478bd9Sstevel@tonic-gate 			if (m.r.rtm.rtm_errno != 0) {
1651*7c478bd9Sstevel@tonic-gate 				trace_act("ignore %s with \"%s\" error",
1652*7c478bd9Sstevel@tonic-gate 				    str, rip_strerror(m.r.rtm.rtm_errno));
1653*7c478bd9Sstevel@tonic-gate 			} else {
1654*7c478bd9Sstevel@tonic-gate 				trace_act("%s", str);
1655*7c478bd9Sstevel@tonic-gate 				rtm_add(&m.r.rtm, &info, 0,
1656*7c478bd9Sstevel@tonic-gate 				    !(m.r.rtm.rtm_flags & RTF_GATEWAY) &&
1657*7c478bd9Sstevel@tonic-gate 				    m.r.rtm.rtm_type != RTM_REDIRECT, ifp);
1658*7c478bd9Sstevel@tonic-gate 
1659*7c478bd9Sstevel@tonic-gate 			}
1660*7c478bd9Sstevel@tonic-gate 			break;
1661*7c478bd9Sstevel@tonic-gate 
1662*7c478bd9Sstevel@tonic-gate 		case RTM_DELETE:
1663*7c478bd9Sstevel@tonic-gate 			if (m.r.rtm.rtm_errno != 0 &&
1664*7c478bd9Sstevel@tonic-gate 			    m.r.rtm.rtm_errno != ESRCH) {
1665*7c478bd9Sstevel@tonic-gate 				trace_act("ignore %s with \"%s\" error",
1666*7c478bd9Sstevel@tonic-gate 				    str, rip_strerror(m.r.rtm.rtm_errno));
1667*7c478bd9Sstevel@tonic-gate 			} else {
1668*7c478bd9Sstevel@tonic-gate 				trace_act("%s", str);
1669*7c478bd9Sstevel@tonic-gate 				del_static(S_ADDR(INFO_DST(&info)), mask,
1670*7c478bd9Sstevel@tonic-gate 				    gate, ifp, 1);
1671*7c478bd9Sstevel@tonic-gate 			}
1672*7c478bd9Sstevel@tonic-gate 			break;
1673*7c478bd9Sstevel@tonic-gate 
1674*7c478bd9Sstevel@tonic-gate 		case RTM_LOSING:
1675*7c478bd9Sstevel@tonic-gate 			trace_act("%s", str);
1676*7c478bd9Sstevel@tonic-gate 			rtm_lose(&m.r.rtm, &info);
1677*7c478bd9Sstevel@tonic-gate 			break;
1678*7c478bd9Sstevel@tonic-gate 
1679*7c478bd9Sstevel@tonic-gate 		default:
1680*7c478bd9Sstevel@tonic-gate 			trace_act("ignore %s", str);
1681*7c478bd9Sstevel@tonic-gate 			break;
1682*7c478bd9Sstevel@tonic-gate 		}
1683*7c478bd9Sstevel@tonic-gate 	}
1684*7c478bd9Sstevel@tonic-gate }
1685*7c478bd9Sstevel@tonic-gate 
1686*7c478bd9Sstevel@tonic-gate 
1687*7c478bd9Sstevel@tonic-gate /*
1688*7c478bd9Sstevel@tonic-gate  * Disassemble a routing message.  The result is an array of pointers
1689*7c478bd9Sstevel@tonic-gate  * to sockaddr_storage structures stored in the info argument.
1690*7c478bd9Sstevel@tonic-gate  *
1691*7c478bd9Sstevel@tonic-gate  * ss is a pointer to the beginning of the data following the
1692*7c478bd9Sstevel@tonic-gate  * rt_msghdr contained in the routing socket message, which consists
1693*7c478bd9Sstevel@tonic-gate  * of a string of concatenated sockaddr structure of different types.
1694*7c478bd9Sstevel@tonic-gate  */
1695*7c478bd9Sstevel@tonic-gate static int
1696*7c478bd9Sstevel@tonic-gate rt_xaddrs(struct rt_addrinfo *info,
1697*7c478bd9Sstevel@tonic-gate     struct sockaddr_storage *ss,
1698*7c478bd9Sstevel@tonic-gate     char *lim,
1699*7c478bd9Sstevel@tonic-gate     int addrs)
1700*7c478bd9Sstevel@tonic-gate {
1701*7c478bd9Sstevel@tonic-gate 	int retv = 0;
1702*7c478bd9Sstevel@tonic-gate 	int i;
1703*7c478bd9Sstevel@tonic-gate 	int abit;
1704*7c478bd9Sstevel@tonic-gate 	int complaints;
1705*7c478bd9Sstevel@tonic-gate 	static int prev_complaints;
1706*7c478bd9Sstevel@tonic-gate 
1707*7c478bd9Sstevel@tonic-gate #define	XBAD_AF		0x1
1708*7c478bd9Sstevel@tonic-gate #define	XBAD_SHORT	0x2
1709*7c478bd9Sstevel@tonic-gate #define	XBAD_LONG	0x4
1710*7c478bd9Sstevel@tonic-gate 
1711*7c478bd9Sstevel@tonic-gate 	(void) memset(info, 0, sizeof (*info));
1712*7c478bd9Sstevel@tonic-gate 	info->rti_addrs = addrs;
1713*7c478bd9Sstevel@tonic-gate 	complaints = 0;
1714*7c478bd9Sstevel@tonic-gate 	for (i = 0, abit = 1; i < RTAX_MAX && (char *)ss < lim;
1715*7c478bd9Sstevel@tonic-gate 	    i++, abit <<= 1) {
1716*7c478bd9Sstevel@tonic-gate 		if ((addrs & abit) == 0)
1717*7c478bd9Sstevel@tonic-gate 			continue;
1718*7c478bd9Sstevel@tonic-gate 		info->rti_info[i] = ss;
1719*7c478bd9Sstevel@tonic-gate 		/* Horrible interface here */
1720*7c478bd9Sstevel@tonic-gate 		switch (ss->ss_family) {
1721*7c478bd9Sstevel@tonic-gate 		case AF_UNIX:
1722*7c478bd9Sstevel@tonic-gate 			/* LINTED */
1723*7c478bd9Sstevel@tonic-gate 			ss = (struct sockaddr_storage *)(
1724*7c478bd9Sstevel@tonic-gate 			    (struct sockaddr_un *)ss + 1);
1725*7c478bd9Sstevel@tonic-gate 			break;
1726*7c478bd9Sstevel@tonic-gate 		case AF_INET:
1727*7c478bd9Sstevel@tonic-gate 			/* LINTED */
1728*7c478bd9Sstevel@tonic-gate 			ss = (struct sockaddr_storage *)(
1729*7c478bd9Sstevel@tonic-gate 			    (struct sockaddr_in *)ss + 1);
1730*7c478bd9Sstevel@tonic-gate 			break;
1731*7c478bd9Sstevel@tonic-gate 		case AF_LINK:
1732*7c478bd9Sstevel@tonic-gate 			/* LINTED */
1733*7c478bd9Sstevel@tonic-gate 			ss = (struct sockaddr_storage *)(
1734*7c478bd9Sstevel@tonic-gate 			    (struct sockaddr_dl *)ss + 1);
1735*7c478bd9Sstevel@tonic-gate 			break;
1736*7c478bd9Sstevel@tonic-gate 		case AF_INET6:
1737*7c478bd9Sstevel@tonic-gate 			/* LINTED */
1738*7c478bd9Sstevel@tonic-gate 			ss = (struct sockaddr_storage *)(
1739*7c478bd9Sstevel@tonic-gate 			    (struct sockaddr_in6 *)ss + 1);
1740*7c478bd9Sstevel@tonic-gate 			break;
1741*7c478bd9Sstevel@tonic-gate 		default:
1742*7c478bd9Sstevel@tonic-gate 			if (!(prev_complaints & XBAD_AF))
1743*7c478bd9Sstevel@tonic-gate 				writelog(LOG_WARNING,
1744*7c478bd9Sstevel@tonic-gate 				    "unknown address family %d "
1745*7c478bd9Sstevel@tonic-gate 				    "encountered", ss->ss_family);
1746*7c478bd9Sstevel@tonic-gate 			if (complaints & XBAD_AF)
1747*7c478bd9Sstevel@tonic-gate 				goto xaddr_done;
1748*7c478bd9Sstevel@tonic-gate 			/* LINTED */
1749*7c478bd9Sstevel@tonic-gate 			ss = (struct sockaddr_storage *)(
1750*7c478bd9Sstevel@tonic-gate 			    (struct sockaddr *)ss + 1);
1751*7c478bd9Sstevel@tonic-gate 			complaints |= XBAD_AF;
1752*7c478bd9Sstevel@tonic-gate 			info->rti_addrs &= abit - 1;
1753*7c478bd9Sstevel@tonic-gate 			addrs = info->rti_addrs;
1754*7c478bd9Sstevel@tonic-gate 			retv = -1;
1755*7c478bd9Sstevel@tonic-gate 			break;
1756*7c478bd9Sstevel@tonic-gate 		}
1757*7c478bd9Sstevel@tonic-gate 		if ((char *)ss > lim) {
1758*7c478bd9Sstevel@tonic-gate 			if (!(prev_complaints & XBAD_SHORT))
1759*7c478bd9Sstevel@tonic-gate 				msglog("sockaddr %d too short by %d "
1760*7c478bd9Sstevel@tonic-gate 				    "bytes", i + 1, (char *)ss - lim);
1761*7c478bd9Sstevel@tonic-gate 			complaints |= XBAD_SHORT;
1762*7c478bd9Sstevel@tonic-gate 			info->rti_info[i] = NULL;
1763*7c478bd9Sstevel@tonic-gate 			info->rti_addrs &= abit - 1;
1764*7c478bd9Sstevel@tonic-gate 			retv = -1;
1765*7c478bd9Sstevel@tonic-gate 			goto xaddr_done;
1766*7c478bd9Sstevel@tonic-gate 		}
1767*7c478bd9Sstevel@tonic-gate 	}
1768*7c478bd9Sstevel@tonic-gate 	if ((char *)ss != lim) {
1769*7c478bd9Sstevel@tonic-gate 		if (!(prev_complaints & XBAD_LONG))
1770*7c478bd9Sstevel@tonic-gate 			msglog("%d bytes of routing message left over",
1771*7c478bd9Sstevel@tonic-gate 			    lim - (char *)ss);
1772*7c478bd9Sstevel@tonic-gate 		complaints |= XBAD_LONG;
1773*7c478bd9Sstevel@tonic-gate 		retv = -1;
1774*7c478bd9Sstevel@tonic-gate 	}
1775*7c478bd9Sstevel@tonic-gate xaddr_done:
1776*7c478bd9Sstevel@tonic-gate 	prev_complaints = complaints;
1777*7c478bd9Sstevel@tonic-gate 	return (retv);
1778*7c478bd9Sstevel@tonic-gate }
1779*7c478bd9Sstevel@tonic-gate 
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate /* after aggregating, note routes that belong in the kernel */
1782*7c478bd9Sstevel@tonic-gate static void
1783*7c478bd9Sstevel@tonic-gate kern_out(struct ag_info *ag)
1784*7c478bd9Sstevel@tonic-gate {
1785*7c478bd9Sstevel@tonic-gate 	struct khash *k;
1786*7c478bd9Sstevel@tonic-gate 
1787*7c478bd9Sstevel@tonic-gate 	/*
1788*7c478bd9Sstevel@tonic-gate 	 * Do not install bad routes if they are not already present.
1789*7c478bd9Sstevel@tonic-gate 	 * This includes routes that had RS_NET_SYN for interfaces that
1790*7c478bd9Sstevel@tonic-gate 	 * recently died.
1791*7c478bd9Sstevel@tonic-gate 	 */
1792*7c478bd9Sstevel@tonic-gate 	if (ag->ag_metric == HOPCNT_INFINITY) {
1793*7c478bd9Sstevel@tonic-gate 		k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask,
1794*7c478bd9Sstevel@tonic-gate 		    ag->ag_nhop, ag->ag_ifp, NULL);
1795*7c478bd9Sstevel@tonic-gate 		if (k == NULL)
1796*7c478bd9Sstevel@tonic-gate 			return;
1797*7c478bd9Sstevel@tonic-gate 	} else {
1798*7c478bd9Sstevel@tonic-gate 		k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask, ag->ag_nhop,
1799*7c478bd9Sstevel@tonic-gate 		    ag->ag_ifp);
1800*7c478bd9Sstevel@tonic-gate 	}
1801*7c478bd9Sstevel@tonic-gate 
1802*7c478bd9Sstevel@tonic-gate 	if (k->k_state & KS_NEW) {
1803*7c478bd9Sstevel@tonic-gate 		/* will need to add new entry to the kernel table */
1804*7c478bd9Sstevel@tonic-gate 		k->k_state = KS_ADD;
1805*7c478bd9Sstevel@tonic-gate 		if (ag->ag_state & AGS_GATEWAY)
1806*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_GATEWAY;
1807*7c478bd9Sstevel@tonic-gate 		if (ag->ag_state & AGS_IF)
1808*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_IF;
1809*7c478bd9Sstevel@tonic-gate 		if (ag->ag_state & AGS_PASSIVE)
1810*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_PASSIVE;
1811*7c478bd9Sstevel@tonic-gate 		if (ag->ag_state & AGS_FILE)
1812*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_FILE;
1813*7c478bd9Sstevel@tonic-gate 		k->k_gate = ag->ag_nhop;
1814*7c478bd9Sstevel@tonic-gate 		k->k_ifp = ag->ag_ifp;
1815*7c478bd9Sstevel@tonic-gate 		k->k_metric = ag->ag_metric;
1816*7c478bd9Sstevel@tonic-gate 		return;
1817*7c478bd9Sstevel@tonic-gate 	}
1818*7c478bd9Sstevel@tonic-gate 
1819*7c478bd9Sstevel@tonic-gate 	if ((k->k_state & (KS_STATIC|KS_DEPRE_IF)) ||
1820*7c478bd9Sstevel@tonic-gate 	    ((k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF)) {
1821*7c478bd9Sstevel@tonic-gate 		return;
1822*7c478bd9Sstevel@tonic-gate 	}
1823*7c478bd9Sstevel@tonic-gate 
1824*7c478bd9Sstevel@tonic-gate 	/* modify existing kernel entry if necessary */
1825*7c478bd9Sstevel@tonic-gate 	if (k->k_gate == ag->ag_nhop && k->k_ifp == ag->ag_ifp &&
1826*7c478bd9Sstevel@tonic-gate 	    k->k_metric != ag->ag_metric) {
1827*7c478bd9Sstevel@tonic-gate 			/*
1828*7c478bd9Sstevel@tonic-gate 			 * Must delete bad interface routes etc.
1829*7c478bd9Sstevel@tonic-gate 			 * to change them.
1830*7c478bd9Sstevel@tonic-gate 			 */
1831*7c478bd9Sstevel@tonic-gate 			if (k->k_metric == HOPCNT_INFINITY)
1832*7c478bd9Sstevel@tonic-gate 				k->k_state |= KS_DEL_ADD;
1833*7c478bd9Sstevel@tonic-gate 			k->k_gate = ag->ag_nhop;
1834*7c478bd9Sstevel@tonic-gate 			k->k_metric = ag->ag_metric;
1835*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_CHANGE;
1836*7c478bd9Sstevel@tonic-gate 	}
1837*7c478bd9Sstevel@tonic-gate 
1838*7c478bd9Sstevel@tonic-gate 	/*
1839*7c478bd9Sstevel@tonic-gate 	 * If the daemon thinks the route should exist, forget
1840*7c478bd9Sstevel@tonic-gate 	 * about any redirections.
1841*7c478bd9Sstevel@tonic-gate 	 * If the daemon thinks the route should exist, eventually
1842*7c478bd9Sstevel@tonic-gate 	 * override manual intervention by the operator.
1843*7c478bd9Sstevel@tonic-gate 	 */
1844*7c478bd9Sstevel@tonic-gate 	if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) {
1845*7c478bd9Sstevel@tonic-gate 		k->k_state &= ~KS_DYNAMIC;
1846*7c478bd9Sstevel@tonic-gate 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1847*7c478bd9Sstevel@tonic-gate 	}
1848*7c478bd9Sstevel@tonic-gate 
1849*7c478bd9Sstevel@tonic-gate 	if ((k->k_state & KS_GATEWAY) && !(ag->ag_state & AGS_GATEWAY)) {
1850*7c478bd9Sstevel@tonic-gate 		k->k_state &= ~KS_GATEWAY;
1851*7c478bd9Sstevel@tonic-gate 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1852*7c478bd9Sstevel@tonic-gate 	} else if (!(k->k_state & KS_GATEWAY) && (ag->ag_state & AGS_GATEWAY)) {
1853*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_GATEWAY;
1854*7c478bd9Sstevel@tonic-gate 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1855*7c478bd9Sstevel@tonic-gate 	}
1856*7c478bd9Sstevel@tonic-gate 
1857*7c478bd9Sstevel@tonic-gate 	/*
1858*7c478bd9Sstevel@tonic-gate 	 * Deleting-and-adding is necessary to change aspects of a route.
1859*7c478bd9Sstevel@tonic-gate 	 * Just delete instead of deleting and then adding a bad route.
1860*7c478bd9Sstevel@tonic-gate 	 * Otherwise, we want to keep the route in the kernel.
1861*7c478bd9Sstevel@tonic-gate 	 */
1862*7c478bd9Sstevel@tonic-gate 	if (k->k_metric == HOPCNT_INFINITY && (k->k_state & KS_DEL_ADD))
1863*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_DELETE;
1864*7c478bd9Sstevel@tonic-gate 	else
1865*7c478bd9Sstevel@tonic-gate 		k->k_state &= ~KS_DELETE;
1866*7c478bd9Sstevel@tonic-gate #undef RT
1867*7c478bd9Sstevel@tonic-gate }
1868*7c478bd9Sstevel@tonic-gate 
1869*7c478bd9Sstevel@tonic-gate /*
1870*7c478bd9Sstevel@tonic-gate  * Update our image of the kernel forwarding table using the given
1871*7c478bd9Sstevel@tonic-gate  * route from our internal routing table.
1872*7c478bd9Sstevel@tonic-gate  */
1873*7c478bd9Sstevel@tonic-gate 
1874*7c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1875*7c478bd9Sstevel@tonic-gate static int
1876*7c478bd9Sstevel@tonic-gate walk_kern(struct radix_node *rn, void *argp)
1877*7c478bd9Sstevel@tonic-gate {
1878*7c478bd9Sstevel@tonic-gate #define	RT ((struct rt_entry *)rn)
1879*7c478bd9Sstevel@tonic-gate 	uint8_t metric, pref;
1880*7c478bd9Sstevel@tonic-gate 	uint_t ags = 0;
1881*7c478bd9Sstevel@tonic-gate 	int i;
1882*7c478bd9Sstevel@tonic-gate 	struct rt_spare *rts;
1883*7c478bd9Sstevel@tonic-gate 
1884*7c478bd9Sstevel@tonic-gate 	/* Do not install synthetic routes */
1885*7c478bd9Sstevel@tonic-gate 	if (RT->rt_state & RS_NET_SYN)
1886*7c478bd9Sstevel@tonic-gate 		return (0);
1887*7c478bd9Sstevel@tonic-gate 
1888*7c478bd9Sstevel@tonic-gate 	/*
1889*7c478bd9Sstevel@tonic-gate 	 * Do not install static routes here. Only
1890*7c478bd9Sstevel@tonic-gate 	 * read_rt->rtm_add->kern_add should install those
1891*7c478bd9Sstevel@tonic-gate 	 */
1892*7c478bd9Sstevel@tonic-gate 	if ((RT->rt_state & RS_STATIC) &&
1893*7c478bd9Sstevel@tonic-gate 	    (RT->rt_spares[0].rts_origin != RO_FILE))
1894*7c478bd9Sstevel@tonic-gate 		return (0);
1895*7c478bd9Sstevel@tonic-gate 
1896*7c478bd9Sstevel@tonic-gate 	/* Do not clobber kernel if this is a route for a dead interface */
1897*7c478bd9Sstevel@tonic-gate 	if (RT->rt_state & RS_BADIF)
1898*7c478bd9Sstevel@tonic-gate 		return (0);
1899*7c478bd9Sstevel@tonic-gate 
1900*7c478bd9Sstevel@tonic-gate 	if (!(RT->rt_state & RS_IF)) {
1901*7c478bd9Sstevel@tonic-gate 		/* This is an ordinary route, not for an interface. */
1902*7c478bd9Sstevel@tonic-gate 
1903*7c478bd9Sstevel@tonic-gate 		/*
1904*7c478bd9Sstevel@tonic-gate 		 * aggregate, ordinary good routes without regard to
1905*7c478bd9Sstevel@tonic-gate 		 * their metric
1906*7c478bd9Sstevel@tonic-gate 		 */
1907*7c478bd9Sstevel@tonic-gate 		pref = 1;
1908*7c478bd9Sstevel@tonic-gate 		ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1909*7c478bd9Sstevel@tonic-gate 
1910*7c478bd9Sstevel@tonic-gate 		/*
1911*7c478bd9Sstevel@tonic-gate 		 * Do not install host routes directly to hosts, to avoid
1912*7c478bd9Sstevel@tonic-gate 		 * interfering with ARP entries in the kernel table.
1913*7c478bd9Sstevel@tonic-gate 		 */
1914*7c478bd9Sstevel@tonic-gate 		if (RT_ISHOST(RT) && ntohl(RT->rt_dst) == RT->rt_gate)
1915*7c478bd9Sstevel@tonic-gate 			return (0);
1916*7c478bd9Sstevel@tonic-gate 
1917*7c478bd9Sstevel@tonic-gate 	} else {
1918*7c478bd9Sstevel@tonic-gate 		/*
1919*7c478bd9Sstevel@tonic-gate 		 * This is an interface route.
1920*7c478bd9Sstevel@tonic-gate 		 * Do not install routes for "external" remote interfaces.
1921*7c478bd9Sstevel@tonic-gate 		 */
1922*7c478bd9Sstevel@tonic-gate 		if (RT->rt_ifp != NULL && (RT->rt_ifp->int_state & IS_EXTERNAL))
1923*7c478bd9Sstevel@tonic-gate 			return (0);
1924*7c478bd9Sstevel@tonic-gate 
1925*7c478bd9Sstevel@tonic-gate 		/* Interfaces should override received routes. */
1926*7c478bd9Sstevel@tonic-gate 		pref = 0;
1927*7c478bd9Sstevel@tonic-gate 		ags |= (AGS_IF | AGS_CORS_GATE);
1928*7c478bd9Sstevel@tonic-gate 		if (RT->rt_ifp != NULL &&
1929*7c478bd9Sstevel@tonic-gate 		    !(RT->rt_ifp->int_if_flags & IFF_LOOPBACK) &&
1930*7c478bd9Sstevel@tonic-gate 		    (RT->rt_ifp->int_state & (IS_PASSIVE|IS_ALIAS)) ==
1931*7c478bd9Sstevel@tonic-gate 		    IS_PASSIVE) {
1932*7c478bd9Sstevel@tonic-gate 			ags |= AGS_PASSIVE;
1933*7c478bd9Sstevel@tonic-gate 		}
1934*7c478bd9Sstevel@tonic-gate 
1935*7c478bd9Sstevel@tonic-gate 		/*
1936*7c478bd9Sstevel@tonic-gate 		 * If it is not an interface, or an alias for an interface,
1937*7c478bd9Sstevel@tonic-gate 		 * it must be a "gateway."
1938*7c478bd9Sstevel@tonic-gate 		 *
1939*7c478bd9Sstevel@tonic-gate 		 * If it is a "remote" interface, it is also a "gateway" to
1940*7c478bd9Sstevel@tonic-gate 		 * the kernel if is not a alias.
1941*7c478bd9Sstevel@tonic-gate 		 */
1942*7c478bd9Sstevel@tonic-gate 		if (RT->rt_ifp == NULL || (RT->rt_ifp->int_state & IS_REMOTE))
1943*7c478bd9Sstevel@tonic-gate 			ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1944*7c478bd9Sstevel@tonic-gate 	}
1945*7c478bd9Sstevel@tonic-gate 
1946*7c478bd9Sstevel@tonic-gate 	metric = RT->rt_metric;
1947*7c478bd9Sstevel@tonic-gate 	if (metric == HOPCNT_INFINITY) {
1948*7c478bd9Sstevel@tonic-gate 		/* If the route is dead, try hard to aggregate. */
1949*7c478bd9Sstevel@tonic-gate 		pref = HOPCNT_INFINITY;
1950*7c478bd9Sstevel@tonic-gate 		ags |= (AGS_FINE_GATE | AGS_SUPPRESS);
1951*7c478bd9Sstevel@tonic-gate 		ags &= ~(AGS_IF | AGS_CORS_GATE);
1952*7c478bd9Sstevel@tonic-gate 	}
1953*7c478bd9Sstevel@tonic-gate 
1954*7c478bd9Sstevel@tonic-gate 	/*
1955*7c478bd9Sstevel@tonic-gate 	 * dump all routes that have the same metric as rt_spares[0]
1956*7c478bd9Sstevel@tonic-gate 	 * into the kern_table, to be added to the kernel.
1957*7c478bd9Sstevel@tonic-gate 	 */
1958*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < RT->rt_num_spares; i++) {
1959*7c478bd9Sstevel@tonic-gate 		rts = &RT->rt_spares[i];
1960*7c478bd9Sstevel@tonic-gate 
1961*7c478bd9Sstevel@tonic-gate 		/* Do not install external routes */
1962*7c478bd9Sstevel@tonic-gate 		if (rts->rts_flags & RTS_EXTERNAL)
1963*7c478bd9Sstevel@tonic-gate 			continue;
1964*7c478bd9Sstevel@tonic-gate 
1965*7c478bd9Sstevel@tonic-gate 		if (rts->rts_metric == metric) {
1966*7c478bd9Sstevel@tonic-gate 			ag_check(RT->rt_dst, RT->rt_mask,
1967*7c478bd9Sstevel@tonic-gate 			    rts->rts_router, rts->rts_ifp, rts->rts_gate,
1968*7c478bd9Sstevel@tonic-gate 			    metric, pref, 0, 0,
1969*7c478bd9Sstevel@tonic-gate 			    (rts->rts_origin & RO_FILE) ? (ags|AGS_FILE) : ags,
1970*7c478bd9Sstevel@tonic-gate 			    kern_out);
1971*7c478bd9Sstevel@tonic-gate 		}
1972*7c478bd9Sstevel@tonic-gate 	}
1973*7c478bd9Sstevel@tonic-gate 	return (0);
1974*7c478bd9Sstevel@tonic-gate #undef RT
1975*7c478bd9Sstevel@tonic-gate }
1976*7c478bd9Sstevel@tonic-gate 
1977*7c478bd9Sstevel@tonic-gate 
1978*7c478bd9Sstevel@tonic-gate /* Update the kernel table to match the daemon table. */
1979*7c478bd9Sstevel@tonic-gate static void
1980*7c478bd9Sstevel@tonic-gate fix_kern(void)
1981*7c478bd9Sstevel@tonic-gate {
1982*7c478bd9Sstevel@tonic-gate 	int i;
1983*7c478bd9Sstevel@tonic-gate 	struct khash *k, *pk, *knext;
1984*7c478bd9Sstevel@tonic-gate 
1985*7c478bd9Sstevel@tonic-gate 
1986*7c478bd9Sstevel@tonic-gate 	need_kern = age_timer;
1987*7c478bd9Sstevel@tonic-gate 
1988*7c478bd9Sstevel@tonic-gate 	/* Walk daemon table, updating the copy of the kernel table. */
1989*7c478bd9Sstevel@tonic-gate 	(void) rn_walktree(rhead, walk_kern, NULL);
1990*7c478bd9Sstevel@tonic-gate 	ag_flush(0, 0, kern_out);
1991*7c478bd9Sstevel@tonic-gate 
1992*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KHASH_SIZE; i++) {
1993*7c478bd9Sstevel@tonic-gate 		pk = NULL;
1994*7c478bd9Sstevel@tonic-gate 		for (k = khash_bins[i]; k != NULL;  k = knext) {
1995*7c478bd9Sstevel@tonic-gate 			knext = k->k_next;
1996*7c478bd9Sstevel@tonic-gate 
1997*7c478bd9Sstevel@tonic-gate 			/* Do not touch local interface routes */
1998*7c478bd9Sstevel@tonic-gate 			if ((k->k_state & KS_DEPRE_IF) ||
1999*7c478bd9Sstevel@tonic-gate 			    (k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF) {
2000*7c478bd9Sstevel@tonic-gate 				pk = k;
2001*7c478bd9Sstevel@tonic-gate 				continue;
2002*7c478bd9Sstevel@tonic-gate 			}
2003*7c478bd9Sstevel@tonic-gate 
2004*7c478bd9Sstevel@tonic-gate 			/* Do not touch static routes */
2005*7c478bd9Sstevel@tonic-gate 			if (k->k_state & KS_STATIC) {
2006*7c478bd9Sstevel@tonic-gate 				kern_check_static(k, 0);
2007*7c478bd9Sstevel@tonic-gate 				pk = k;
2008*7c478bd9Sstevel@tonic-gate 				continue;
2009*7c478bd9Sstevel@tonic-gate 			}
2010*7c478bd9Sstevel@tonic-gate 
2011*7c478bd9Sstevel@tonic-gate 			/* check hold on routes deleted by the operator */
2012*7c478bd9Sstevel@tonic-gate 			if (k->k_keep > now.tv_sec) {
2013*7c478bd9Sstevel@tonic-gate 				/* ensure we check when the hold is over */
2014*7c478bd9Sstevel@tonic-gate 				LIM_SEC(need_kern, k->k_keep);
2015*7c478bd9Sstevel@tonic-gate 				pk = k;
2016*7c478bd9Sstevel@tonic-gate 				continue;
2017*7c478bd9Sstevel@tonic-gate 			}
2018*7c478bd9Sstevel@tonic-gate 
2019*7c478bd9Sstevel@tonic-gate 			if ((k->k_state & KS_DELETE) &&
2020*7c478bd9Sstevel@tonic-gate 			    !(k->k_state & KS_DYNAMIC)) {
2021*7c478bd9Sstevel@tonic-gate 				if ((k->k_dst == RIP_DEFAULT) &&
2022*7c478bd9Sstevel@tonic-gate 				    (k->k_ifp != NULL) &&
2023*7c478bd9Sstevel@tonic-gate 				    (kern_alternate(RIP_DEFAULT,
2024*7c478bd9Sstevel@tonic-gate 				    k->k_mask, k->k_gate, k->k_ifp,
2025*7c478bd9Sstevel@tonic-gate 				    NULL) == NULL))
2026*7c478bd9Sstevel@tonic-gate 					rdisc_restore(k->k_ifp);
2027*7c478bd9Sstevel@tonic-gate 				kern_ioctl(k, RTM_DELETE, 0);
2028*7c478bd9Sstevel@tonic-gate 				if (pk != NULL)
2029*7c478bd9Sstevel@tonic-gate 					pk->k_next = knext;
2030*7c478bd9Sstevel@tonic-gate 				else
2031*7c478bd9Sstevel@tonic-gate 					khash_bins[i] = knext;
2032*7c478bd9Sstevel@tonic-gate 				free(k);
2033*7c478bd9Sstevel@tonic-gate 				continue;
2034*7c478bd9Sstevel@tonic-gate 			}
2035*7c478bd9Sstevel@tonic-gate 
2036*7c478bd9Sstevel@tonic-gate 			if (k->k_state & KS_DEL_ADD)
2037*7c478bd9Sstevel@tonic-gate 				kern_ioctl(k, RTM_DELETE, 0);
2038*7c478bd9Sstevel@tonic-gate 
2039*7c478bd9Sstevel@tonic-gate 			if (k->k_state & KS_ADD) {
2040*7c478bd9Sstevel@tonic-gate 				if ((k->k_dst == RIP_DEFAULT) &&
2041*7c478bd9Sstevel@tonic-gate 				    (k->k_ifp != NULL))
2042*7c478bd9Sstevel@tonic-gate 					rdisc_suppress(k->k_ifp);
2043*7c478bd9Sstevel@tonic-gate 				kern_ioctl(k, RTM_ADD,
2044*7c478bd9Sstevel@tonic-gate 				    ((0 != (k->k_state & (KS_GATEWAY |
2045*7c478bd9Sstevel@tonic-gate 					KS_DYNAMIC))) ? RTF_GATEWAY : 0));
2046*7c478bd9Sstevel@tonic-gate 			} else if (k->k_state & KS_CHANGE) {
2047*7c478bd9Sstevel@tonic-gate 				/*
2048*7c478bd9Sstevel@tonic-gate 				 * Should be using RTM_CHANGE here, but
2049*7c478bd9Sstevel@tonic-gate 				 * since RTM_CHANGE is currently
2050*7c478bd9Sstevel@tonic-gate 				 * not multipath-aware, and assumes
2051*7c478bd9Sstevel@tonic-gate 				 * that RTF_GATEWAY implies the gateway
2052*7c478bd9Sstevel@tonic-gate 				 * of the route for dst has to be
2053*7c478bd9Sstevel@tonic-gate 				 * changed, we play safe, and do a del + add.
2054*7c478bd9Sstevel@tonic-gate 				 */
2055*7c478bd9Sstevel@tonic-gate 				kern_ioctl(k,  RTM_DELETE, 0);
2056*7c478bd9Sstevel@tonic-gate 				kern_ioctl(k, RTM_ADD,
2057*7c478bd9Sstevel@tonic-gate 				    ((0 != (k->k_state & (KS_GATEWAY |
2058*7c478bd9Sstevel@tonic-gate 					KS_DYNAMIC))) ? RTF_GATEWAY : 0));
2059*7c478bd9Sstevel@tonic-gate 			}
2060*7c478bd9Sstevel@tonic-gate 			k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD);
2061*7c478bd9Sstevel@tonic-gate 
2062*7c478bd9Sstevel@tonic-gate 			/*
2063*7c478bd9Sstevel@tonic-gate 			 * Mark this route to be deleted in the next cycle.
2064*7c478bd9Sstevel@tonic-gate 			 * This deletes routes that disappear from the
2065*7c478bd9Sstevel@tonic-gate 			 * daemon table, since the normal aging code
2066*7c478bd9Sstevel@tonic-gate 			 * will clear the bit for routes that have not
2067*7c478bd9Sstevel@tonic-gate 			 * disappeared from the daemon table.
2068*7c478bd9Sstevel@tonic-gate 			 */
2069*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_DELETE;
2070*7c478bd9Sstevel@tonic-gate 			pk = k;
2071*7c478bd9Sstevel@tonic-gate 		}
2072*7c478bd9Sstevel@tonic-gate 	}
2073*7c478bd9Sstevel@tonic-gate }
2074*7c478bd9Sstevel@tonic-gate 
2075*7c478bd9Sstevel@tonic-gate 
2076*7c478bd9Sstevel@tonic-gate /* Delete a static route in the image of the kernel table. */
2077*7c478bd9Sstevel@tonic-gate void
2078*7c478bd9Sstevel@tonic-gate del_static(in_addr_t dst, in_addr_t mask, in_addr_t gate,
2079*7c478bd9Sstevel@tonic-gate     struct interface *ifp, int gone)
2080*7c478bd9Sstevel@tonic-gate {
2081*7c478bd9Sstevel@tonic-gate 	struct khash *k;
2082*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
2083*7c478bd9Sstevel@tonic-gate 
2084*7c478bd9Sstevel@tonic-gate 	/*
2085*7c478bd9Sstevel@tonic-gate 	 * Just mark it in the table to be deleted next time the kernel
2086*7c478bd9Sstevel@tonic-gate 	 * table is updated.
2087*7c478bd9Sstevel@tonic-gate 	 * If it has already been deleted, mark it as such, and set its
2088*7c478bd9Sstevel@tonic-gate 	 * keep-timer so that it will not be deleted again for a while.
2089*7c478bd9Sstevel@tonic-gate 	 * This lets the operator delete a route added by the daemon
2090*7c478bd9Sstevel@tonic-gate 	 * and add a replacement.
2091*7c478bd9Sstevel@tonic-gate 	 */
2092*7c478bd9Sstevel@tonic-gate 	k = kern_find(dst, mask, gate, ifp, NULL);
2093*7c478bd9Sstevel@tonic-gate 	if (k != NULL && (gate == 0 || k->k_gate == gate)) {
2094*7c478bd9Sstevel@tonic-gate 		k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK);
2095*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_DELETE;
2096*7c478bd9Sstevel@tonic-gate 		if (gone) {
2097*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_DELETED;
2098*7c478bd9Sstevel@tonic-gate 			k->k_keep = now.tv_sec + K_KEEP_LIM;
2099*7c478bd9Sstevel@tonic-gate 		}
2100*7c478bd9Sstevel@tonic-gate 	}
2101*7c478bd9Sstevel@tonic-gate 
2102*7c478bd9Sstevel@tonic-gate 	rt = rtget(dst, mask);
2103*7c478bd9Sstevel@tonic-gate 	if (rt != NULL && (rt->rt_state & RS_STATIC))
2104*7c478bd9Sstevel@tonic-gate 		rtbad(rt, NULL);
2105*7c478bd9Sstevel@tonic-gate }
2106*7c478bd9Sstevel@tonic-gate 
2107*7c478bd9Sstevel@tonic-gate 
2108*7c478bd9Sstevel@tonic-gate /*
2109*7c478bd9Sstevel@tonic-gate  * Delete all routes generated from ICMP Redirects that use a given gateway,
2110*7c478bd9Sstevel@tonic-gate  * as well as old redirected routes.
2111*7c478bd9Sstevel@tonic-gate  */
2112*7c478bd9Sstevel@tonic-gate void
2113*7c478bd9Sstevel@tonic-gate del_redirects(in_addr_t bad_gate, time_t old)
2114*7c478bd9Sstevel@tonic-gate {
2115*7c478bd9Sstevel@tonic-gate 	int i;
2116*7c478bd9Sstevel@tonic-gate 	struct khash *k;
2117*7c478bd9Sstevel@tonic-gate 	boolean_t dosupply = should_supply(NULL);
2118*7c478bd9Sstevel@tonic-gate 
2119*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KHASH_SIZE; i++) {
2120*7c478bd9Sstevel@tonic-gate 		for (k = khash_bins[i]; k != NULL; k = k->k_next) {
2121*7c478bd9Sstevel@tonic-gate 			if (!(k->k_state & KS_DYNAMIC) ||
2122*7c478bd9Sstevel@tonic-gate 			    (k->k_state & (KS_STATIC|KS_IF|KS_DEPRE_IF)))
2123*7c478bd9Sstevel@tonic-gate 				continue;
2124*7c478bd9Sstevel@tonic-gate 
2125*7c478bd9Sstevel@tonic-gate 			if (k->k_gate != bad_gate && k->k_redirect_time > old &&
2126*7c478bd9Sstevel@tonic-gate 			    !dosupply)
2127*7c478bd9Sstevel@tonic-gate 				continue;
2128*7c478bd9Sstevel@tonic-gate 
2129*7c478bd9Sstevel@tonic-gate 			k->k_state |= KS_DELETE;
2130*7c478bd9Sstevel@tonic-gate 			k->k_state &= ~KS_DYNAMIC;
2131*7c478bd9Sstevel@tonic-gate 			need_kern.tv_sec = now.tv_sec;
2132*7c478bd9Sstevel@tonic-gate 			trace_act("mark redirected %s --> %s for deletion",
2133*7c478bd9Sstevel@tonic-gate 			    addrname(k->k_dst, k->k_mask, 0),
2134*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(k->k_gate));
2135*7c478bd9Sstevel@tonic-gate 		}
2136*7c478bd9Sstevel@tonic-gate 	}
2137*7c478bd9Sstevel@tonic-gate }
2138*7c478bd9Sstevel@tonic-gate 
2139*7c478bd9Sstevel@tonic-gate /* Start the daemon tables. */
2140*7c478bd9Sstevel@tonic-gate void
2141*7c478bd9Sstevel@tonic-gate rtinit(void)
2142*7c478bd9Sstevel@tonic-gate {
2143*7c478bd9Sstevel@tonic-gate 	int i;
2144*7c478bd9Sstevel@tonic-gate 	struct ag_info *ag;
2145*7c478bd9Sstevel@tonic-gate 
2146*7c478bd9Sstevel@tonic-gate 	/* Initialize the radix trees */
2147*7c478bd9Sstevel@tonic-gate 	rn_init();
2148*7c478bd9Sstevel@tonic-gate 	(void) rn_inithead((void**)&rhead, 32);
2149*7c478bd9Sstevel@tonic-gate 
2150*7c478bd9Sstevel@tonic-gate 	/* mark all of the slots in the table free */
2151*7c478bd9Sstevel@tonic-gate 	ag_avail = ag_slots;
2152*7c478bd9Sstevel@tonic-gate 	for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) {
2153*7c478bd9Sstevel@tonic-gate 		ag->ag_fine = ag+1;
2154*7c478bd9Sstevel@tonic-gate 		ag++;
2155*7c478bd9Sstevel@tonic-gate 	}
2156*7c478bd9Sstevel@tonic-gate }
2157*7c478bd9Sstevel@tonic-gate 
2158*7c478bd9Sstevel@tonic-gate 
2159*7c478bd9Sstevel@tonic-gate static struct sockaddr_in dst_sock = {AF_INET};
2160*7c478bd9Sstevel@tonic-gate static struct sockaddr_in mask_sock = {AF_INET};
2161*7c478bd9Sstevel@tonic-gate 
2162*7c478bd9Sstevel@tonic-gate 
2163*7c478bd9Sstevel@tonic-gate static void
2164*7c478bd9Sstevel@tonic-gate set_need_flash(void)
2165*7c478bd9Sstevel@tonic-gate {
2166*7c478bd9Sstevel@tonic-gate 	if (!need_flash) {
2167*7c478bd9Sstevel@tonic-gate 		need_flash = _B_TRUE;
2168*7c478bd9Sstevel@tonic-gate 		/*
2169*7c478bd9Sstevel@tonic-gate 		 * Do not send the flash update immediately.  Wait a little
2170*7c478bd9Sstevel@tonic-gate 		 * while to hear from other routers.
2171*7c478bd9Sstevel@tonic-gate 		 */
2172*7c478bd9Sstevel@tonic-gate 		no_flash.tv_sec = now.tv_sec + MIN_WAITTIME;
2173*7c478bd9Sstevel@tonic-gate 	}
2174*7c478bd9Sstevel@tonic-gate }
2175*7c478bd9Sstevel@tonic-gate 
2176*7c478bd9Sstevel@tonic-gate 
2177*7c478bd9Sstevel@tonic-gate /* Get a particular routing table entry */
2178*7c478bd9Sstevel@tonic-gate struct rt_entry *
2179*7c478bd9Sstevel@tonic-gate rtget(in_addr_t dst, in_addr_t mask)
2180*7c478bd9Sstevel@tonic-gate {
2181*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
2182*7c478bd9Sstevel@tonic-gate 
2183*7c478bd9Sstevel@tonic-gate 	dst_sock.sin_addr.s_addr = dst;
2184*7c478bd9Sstevel@tonic-gate 	mask_sock.sin_addr.s_addr = htonl(mask);
2185*7c478bd9Sstevel@tonic-gate 	rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock, &mask_sock, rhead);
2186*7c478bd9Sstevel@tonic-gate 	if (rt == NULL || rt->rt_dst != dst || rt->rt_mask != mask)
2187*7c478bd9Sstevel@tonic-gate 		return (NULL);
2188*7c478bd9Sstevel@tonic-gate 
2189*7c478bd9Sstevel@tonic-gate 	return (rt);
2190*7c478bd9Sstevel@tonic-gate }
2191*7c478bd9Sstevel@tonic-gate 
2192*7c478bd9Sstevel@tonic-gate 
2193*7c478bd9Sstevel@tonic-gate /* Find a route to dst as the kernel would. */
2194*7c478bd9Sstevel@tonic-gate struct rt_entry *
2195*7c478bd9Sstevel@tonic-gate rtfind(in_addr_t dst)
2196*7c478bd9Sstevel@tonic-gate {
2197*7c478bd9Sstevel@tonic-gate 	dst_sock.sin_addr.s_addr = dst;
2198*7c478bd9Sstevel@tonic-gate 	return ((struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead));
2199*7c478bd9Sstevel@tonic-gate }
2200*7c478bd9Sstevel@tonic-gate 
2201*7c478bd9Sstevel@tonic-gate 
2202*7c478bd9Sstevel@tonic-gate /* add a route to the table */
2203*7c478bd9Sstevel@tonic-gate void
2204*7c478bd9Sstevel@tonic-gate rtadd(in_addr_t	dst,
2205*7c478bd9Sstevel@tonic-gate     in_addr_t	mask,
2206*7c478bd9Sstevel@tonic-gate     uint16_t	state,			/* rt_state for the entry */
2207*7c478bd9Sstevel@tonic-gate     struct	rt_spare *new)
2208*7c478bd9Sstevel@tonic-gate {
2209*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
2210*7c478bd9Sstevel@tonic-gate 	in_addr_t smask;
2211*7c478bd9Sstevel@tonic-gate 	int i;
2212*7c478bd9Sstevel@tonic-gate 	struct rt_spare *rts;
2213*7c478bd9Sstevel@tonic-gate 
2214*7c478bd9Sstevel@tonic-gate 	/* This is the only function that increments total_routes. */
2215*7c478bd9Sstevel@tonic-gate 	if (total_routes == MAX_ROUTES) {
2216*7c478bd9Sstevel@tonic-gate 		msglog("have maximum (%d) routes", total_routes);
2217*7c478bd9Sstevel@tonic-gate 		return;
2218*7c478bd9Sstevel@tonic-gate 	}
2219*7c478bd9Sstevel@tonic-gate 
2220*7c478bd9Sstevel@tonic-gate 	rt = rtmalloc(sizeof (*rt), "rtadd");
2221*7c478bd9Sstevel@tonic-gate 	(void) memset(rt, 0, sizeof (*rt));
2222*7c478bd9Sstevel@tonic-gate 	rt->rt_spares = rtmalloc(SPARE_INC  * sizeof (struct rt_spare),
2223*7c478bd9Sstevel@tonic-gate 	    "rtadd");
2224*7c478bd9Sstevel@tonic-gate 	rt->rt_num_spares = SPARE_INC;
2225*7c478bd9Sstevel@tonic-gate 	(void) memset(rt->rt_spares, 0, SPARE_INC  * sizeof (struct rt_spare));
2226*7c478bd9Sstevel@tonic-gate 	for (rts = rt->rt_spares, i = rt->rt_num_spares; i != 0; i--, rts++)
2227*7c478bd9Sstevel@tonic-gate 		rts->rts_metric = HOPCNT_INFINITY;
2228*7c478bd9Sstevel@tonic-gate 
2229*7c478bd9Sstevel@tonic-gate 	rt->rt_nodes->rn_key = (uint8_t *)&rt->rt_dst_sock;
2230*7c478bd9Sstevel@tonic-gate 	rt->rt_dst = dst;
2231*7c478bd9Sstevel@tonic-gate 	rt->rt_dst_sock.sin_family = AF_INET;
2232*7c478bd9Sstevel@tonic-gate 	if (mask != HOST_MASK) {
2233*7c478bd9Sstevel@tonic-gate 		smask = std_mask(dst);
2234*7c478bd9Sstevel@tonic-gate 		if ((smask & ~mask) == 0 && mask > smask)
2235*7c478bd9Sstevel@tonic-gate 			state |= RS_SUBNET;
2236*7c478bd9Sstevel@tonic-gate 	}
2237*7c478bd9Sstevel@tonic-gate 	mask_sock.sin_addr.s_addr = htonl(mask);
2238*7c478bd9Sstevel@tonic-gate 	rt->rt_mask = mask;
2239*7c478bd9Sstevel@tonic-gate 	rt->rt_spares[0] = *new;
2240*7c478bd9Sstevel@tonic-gate 	rt->rt_state = state;
2241*7c478bd9Sstevel@tonic-gate 	rt->rt_time = now.tv_sec;
2242*7c478bd9Sstevel@tonic-gate 	rt->rt_poison_metric = HOPCNT_INFINITY;
2243*7c478bd9Sstevel@tonic-gate 	rt->rt_seqno = update_seqno;
2244*7c478bd9Sstevel@tonic-gate 
2245*7c478bd9Sstevel@tonic-gate 	if (TRACEACTIONS)
2246*7c478bd9Sstevel@tonic-gate 		trace_add_del("Add", rt);
2247*7c478bd9Sstevel@tonic-gate 
2248*7c478bd9Sstevel@tonic-gate 	need_kern.tv_sec = now.tv_sec;
2249*7c478bd9Sstevel@tonic-gate 	set_need_flash();
2250*7c478bd9Sstevel@tonic-gate 
2251*7c478bd9Sstevel@tonic-gate 	if (NULL == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock, rhead,
2252*7c478bd9Sstevel@tonic-gate 	    rt->rt_nodes)) {
2253*7c478bd9Sstevel@tonic-gate 		msglog("rnh_addaddr() failed for %s mask=%s",
2254*7c478bd9Sstevel@tonic-gate 		    naddr_ntoa(dst), naddr_ntoa(htonl(mask)));
2255*7c478bd9Sstevel@tonic-gate 		free(rt);
2256*7c478bd9Sstevel@tonic-gate 	}
2257*7c478bd9Sstevel@tonic-gate 
2258*7c478bd9Sstevel@tonic-gate 	total_routes++;
2259*7c478bd9Sstevel@tonic-gate }
2260*7c478bd9Sstevel@tonic-gate 
2261*7c478bd9Sstevel@tonic-gate 
2262*7c478bd9Sstevel@tonic-gate /* notice a changed route */
2263*7c478bd9Sstevel@tonic-gate void
2264*7c478bd9Sstevel@tonic-gate rtchange(struct rt_entry *rt,
2265*7c478bd9Sstevel@tonic-gate     uint16_t	state,			/* new state bits */
2266*7c478bd9Sstevel@tonic-gate     struct rt_spare *new,
2267*7c478bd9Sstevel@tonic-gate     char	*label)
2268*7c478bd9Sstevel@tonic-gate {
2269*7c478bd9Sstevel@tonic-gate 	if (rt->rt_metric != new->rts_metric) {
2270*7c478bd9Sstevel@tonic-gate 		/*
2271*7c478bd9Sstevel@tonic-gate 		 * Fix the kernel immediately if it seems the route
2272*7c478bd9Sstevel@tonic-gate 		 * has gone bad, since there may be a working route that
2273*7c478bd9Sstevel@tonic-gate 		 * aggregates this route.
2274*7c478bd9Sstevel@tonic-gate 		 */
2275*7c478bd9Sstevel@tonic-gate 		if (new->rts_metric == HOPCNT_INFINITY) {
2276*7c478bd9Sstevel@tonic-gate 			need_kern.tv_sec = now.tv_sec;
2277*7c478bd9Sstevel@tonic-gate 			if (new->rts_time >= now.tv_sec - EXPIRE_TIME)
2278*7c478bd9Sstevel@tonic-gate 				new->rts_time = now.tv_sec - EXPIRE_TIME;
2279*7c478bd9Sstevel@tonic-gate 		}
2280*7c478bd9Sstevel@tonic-gate 		rt->rt_seqno = update_seqno;
2281*7c478bd9Sstevel@tonic-gate 		set_need_flash();
2282*7c478bd9Sstevel@tonic-gate 	}
2283*7c478bd9Sstevel@tonic-gate 
2284*7c478bd9Sstevel@tonic-gate 	if (rt->rt_gate != new->rts_gate) {
2285*7c478bd9Sstevel@tonic-gate 		need_kern.tv_sec = now.tv_sec;
2286*7c478bd9Sstevel@tonic-gate 		rt->rt_seqno = update_seqno;
2287*7c478bd9Sstevel@tonic-gate 		set_need_flash();
2288*7c478bd9Sstevel@tonic-gate 	}
2289*7c478bd9Sstevel@tonic-gate 
2290*7c478bd9Sstevel@tonic-gate 	state |= (rt->rt_state & RS_SUBNET);
2291*7c478bd9Sstevel@tonic-gate 
2292*7c478bd9Sstevel@tonic-gate 	/* Keep various things from deciding ageless routes are stale. */
2293*7c478bd9Sstevel@tonic-gate 	if (!AGE_RT(state, rt->rt_spares[0].rts_origin, new->rts_ifp))
2294*7c478bd9Sstevel@tonic-gate 		new->rts_time = now.tv_sec;
2295*7c478bd9Sstevel@tonic-gate 
2296*7c478bd9Sstevel@tonic-gate 	if (TRACEACTIONS)
2297*7c478bd9Sstevel@tonic-gate 		trace_change(rt, state, new,
2298*7c478bd9Sstevel@tonic-gate 		    label ? label : "Chg   ");
2299*7c478bd9Sstevel@tonic-gate 
2300*7c478bd9Sstevel@tonic-gate 	rt->rt_state = state;
2301*7c478bd9Sstevel@tonic-gate 	/*
2302*7c478bd9Sstevel@tonic-gate 	 * If the interface state of the new primary route is good,
2303*7c478bd9Sstevel@tonic-gate 	 * turn off RS_BADIF flag
2304*7c478bd9Sstevel@tonic-gate 	 */
2305*7c478bd9Sstevel@tonic-gate 	if ((rt->rt_state & RS_BADIF) &&
2306*7c478bd9Sstevel@tonic-gate 	    IS_IFF_UP(new->rts_ifp->int_if_flags) &&
2307*7c478bd9Sstevel@tonic-gate 	    !(new->rts_ifp->int_state & (IS_BROKE | IS_SICK)))
2308*7c478bd9Sstevel@tonic-gate 		rt->rt_state &= ~(RS_BADIF);
2309*7c478bd9Sstevel@tonic-gate 
2310*7c478bd9Sstevel@tonic-gate 	rt->rt_spares[0] = *new;
2311*7c478bd9Sstevel@tonic-gate }
2312*7c478bd9Sstevel@tonic-gate 
2313*7c478bd9Sstevel@tonic-gate 
2314*7c478bd9Sstevel@tonic-gate /* check for a better route among the spares */
2315*7c478bd9Sstevel@tonic-gate static struct rt_spare *
2316*7c478bd9Sstevel@tonic-gate rts_better(struct rt_entry *rt)
2317*7c478bd9Sstevel@tonic-gate {
2318*7c478bd9Sstevel@tonic-gate 	struct rt_spare *rts, *rts1;
2319*7c478bd9Sstevel@tonic-gate 	int i;
2320*7c478bd9Sstevel@tonic-gate 
2321*7c478bd9Sstevel@tonic-gate 	/* find the best alternative among the spares */
2322*7c478bd9Sstevel@tonic-gate 	rts = rt->rt_spares+1;
2323*7c478bd9Sstevel@tonic-gate 	for (i = rt->rt_num_spares, rts1 = rts+1; i > 2; i--, rts1++) {
2324*7c478bd9Sstevel@tonic-gate 		if (BETTER_LINK(rt, rts1, rts))
2325*7c478bd9Sstevel@tonic-gate 			rts = rts1;
2326*7c478bd9Sstevel@tonic-gate 	}
2327*7c478bd9Sstevel@tonic-gate 
2328*7c478bd9Sstevel@tonic-gate 	return (rts);
2329*7c478bd9Sstevel@tonic-gate }
2330*7c478bd9Sstevel@tonic-gate 
2331*7c478bd9Sstevel@tonic-gate 
2332*7c478bd9Sstevel@tonic-gate /* switch to a backup route */
2333*7c478bd9Sstevel@tonic-gate void
2334*7c478bd9Sstevel@tonic-gate rtswitch(struct rt_entry *rt,
2335*7c478bd9Sstevel@tonic-gate     struct rt_spare *rts)
2336*7c478bd9Sstevel@tonic-gate {
2337*7c478bd9Sstevel@tonic-gate 	struct rt_spare swap;
2338*7c478bd9Sstevel@tonic-gate 	char label[10];
2339*7c478bd9Sstevel@tonic-gate 
2340*7c478bd9Sstevel@tonic-gate 	/* Do not change permanent routes */
2341*7c478bd9Sstevel@tonic-gate 	if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC |
2342*7c478bd9Sstevel@tonic-gate 	    RS_NET_SYN | RS_IF)))
2343*7c478bd9Sstevel@tonic-gate 		return;
2344*7c478bd9Sstevel@tonic-gate 
2345*7c478bd9Sstevel@tonic-gate 	/* find the best alternative among the spares */
2346*7c478bd9Sstevel@tonic-gate 	if (rts == NULL)
2347*7c478bd9Sstevel@tonic-gate 		rts = rts_better(rt);
2348*7c478bd9Sstevel@tonic-gate 
2349*7c478bd9Sstevel@tonic-gate 	/* Do not bother if it is not worthwhile. */
2350*7c478bd9Sstevel@tonic-gate 	if (!BETTER_LINK(rt, rts, rt->rt_spares))
2351*7c478bd9Sstevel@tonic-gate 		return;
2352*7c478bd9Sstevel@tonic-gate 
2353*7c478bd9Sstevel@tonic-gate 	swap = rt->rt_spares[0];
2354*7c478bd9Sstevel@tonic-gate 	(void) snprintf(label, sizeof (label), "Use #%d",
2355*7c478bd9Sstevel@tonic-gate 	    (int)(rts - rt->rt_spares));
2356*7c478bd9Sstevel@tonic-gate 	rtchange(rt, rt->rt_state & ~(RS_NET_SYN), rts, label);
2357*7c478bd9Sstevel@tonic-gate 
2358*7c478bd9Sstevel@tonic-gate 	if (swap.rts_metric == HOPCNT_INFINITY) {
2359*7c478bd9Sstevel@tonic-gate 		*rts = rts_empty;
2360*7c478bd9Sstevel@tonic-gate 	} else {
2361*7c478bd9Sstevel@tonic-gate 		*rts = swap;
2362*7c478bd9Sstevel@tonic-gate 	}
2363*7c478bd9Sstevel@tonic-gate 
2364*7c478bd9Sstevel@tonic-gate }
2365*7c478bd9Sstevel@tonic-gate 
2366*7c478bd9Sstevel@tonic-gate 
2367*7c478bd9Sstevel@tonic-gate void
2368*7c478bd9Sstevel@tonic-gate rtdelete(struct rt_entry *rt)
2369*7c478bd9Sstevel@tonic-gate {
2370*7c478bd9Sstevel@tonic-gate 	struct rt_entry *deleted_rt;
2371*7c478bd9Sstevel@tonic-gate 	struct rt_spare *rts;
2372*7c478bd9Sstevel@tonic-gate 	int i;
2373*7c478bd9Sstevel@tonic-gate 	in_addr_t gate = rt->rt_gate; /* for debugging */
2374*7c478bd9Sstevel@tonic-gate 
2375*7c478bd9Sstevel@tonic-gate 	if (TRACEACTIONS)
2376*7c478bd9Sstevel@tonic-gate 		trace_add_del("Del", rt);
2377*7c478bd9Sstevel@tonic-gate 
2378*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < rt->rt_num_spares; i++) {
2379*7c478bd9Sstevel@tonic-gate 		rts = &rt->rt_spares[i];
2380*7c478bd9Sstevel@tonic-gate 		rts_delete(rt, rts);
2381*7c478bd9Sstevel@tonic-gate 	}
2382*7c478bd9Sstevel@tonic-gate 
2383*7c478bd9Sstevel@tonic-gate 	dst_sock.sin_addr.s_addr = rt->rt_dst;
2384*7c478bd9Sstevel@tonic-gate 	mask_sock.sin_addr.s_addr = htonl(rt->rt_mask);
2385*7c478bd9Sstevel@tonic-gate 	if (rt != (deleted_rt =
2386*7c478bd9Sstevel@tonic-gate 	    ((struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock,
2387*7c478bd9Sstevel@tonic-gate 	    rhead)))) {
2388*7c478bd9Sstevel@tonic-gate 		msglog("rnh_deladdr(%s) failed; found rt 0x%lx",
2389*7c478bd9Sstevel@tonic-gate 		    rtname(rt->rt_dst, rt->rt_mask, gate), deleted_rt);
2390*7c478bd9Sstevel@tonic-gate 		if (deleted_rt != NULL)
2391*7c478bd9Sstevel@tonic-gate 			free(deleted_rt);
2392*7c478bd9Sstevel@tonic-gate 	}
2393*7c478bd9Sstevel@tonic-gate 	total_routes--;
2394*7c478bd9Sstevel@tonic-gate 	free(rt);
2395*7c478bd9Sstevel@tonic-gate 
2396*7c478bd9Sstevel@tonic-gate 	if (dst_sock.sin_addr.s_addr == RIP_DEFAULT) {
2397*7c478bd9Sstevel@tonic-gate 		/*
2398*7c478bd9Sstevel@tonic-gate 		 * we just deleted the default route. Trigger rdisc_sort
2399*7c478bd9Sstevel@tonic-gate 		 * so that we can recover from any rdisc information that
2400*7c478bd9Sstevel@tonic-gate 		 * is valid
2401*7c478bd9Sstevel@tonic-gate 		 */
2402*7c478bd9Sstevel@tonic-gate 		rdisc_timer.tv_sec = 0;
2403*7c478bd9Sstevel@tonic-gate 	}
2404*7c478bd9Sstevel@tonic-gate }
2405*7c478bd9Sstevel@tonic-gate 
2406*7c478bd9Sstevel@tonic-gate void
2407*7c478bd9Sstevel@tonic-gate rts_delete(struct rt_entry *rt, struct rt_spare *rts)
2408*7c478bd9Sstevel@tonic-gate {
2409*7c478bd9Sstevel@tonic-gate 	struct khash *k;
2410*7c478bd9Sstevel@tonic-gate 
2411*7c478bd9Sstevel@tonic-gate 	trace_upslot(rt, rts, &rts_empty);
2412*7c478bd9Sstevel@tonic-gate 	k = kern_find(rt->rt_dst, rt->rt_mask,
2413*7c478bd9Sstevel@tonic-gate 	    rts->rts_gate, rts->rts_ifp, NULL);
2414*7c478bd9Sstevel@tonic-gate 	if (k != NULL &&
2415*7c478bd9Sstevel@tonic-gate 	    !(k->k_state & KS_DEPRE_IF) &&
2416*7c478bd9Sstevel@tonic-gate 	    ((k->k_state & (KS_IF|KS_PASSIVE)) != KS_IF)) {
2417*7c478bd9Sstevel@tonic-gate 		k->k_state |= KS_DELETE;
2418*7c478bd9Sstevel@tonic-gate 		need_kern.tv_sec = now.tv_sec;
2419*7c478bd9Sstevel@tonic-gate 	}
2420*7c478bd9Sstevel@tonic-gate 
2421*7c478bd9Sstevel@tonic-gate 	*rts = rts_empty;
2422*7c478bd9Sstevel@tonic-gate }
2423*7c478bd9Sstevel@tonic-gate 
2424*7c478bd9Sstevel@tonic-gate /*
2425*7c478bd9Sstevel@tonic-gate  * Get rid of a bad route, and try to switch to a replacement.
2426*7c478bd9Sstevel@tonic-gate  * If the route has gone bad because of a bad interface,
2427*7c478bd9Sstevel@tonic-gate  * the information about the dead interface is available in badifp
2428*7c478bd9Sstevel@tonic-gate  * for the purpose of sanity checks, if_flags checks etc.
2429*7c478bd9Sstevel@tonic-gate  */
2430*7c478bd9Sstevel@tonic-gate static void
2431*7c478bd9Sstevel@tonic-gate rtbad(struct rt_entry *rt, struct interface *badifp)
2432*7c478bd9Sstevel@tonic-gate {
2433*7c478bd9Sstevel@tonic-gate 	struct rt_spare new;
2434*7c478bd9Sstevel@tonic-gate 	uint16_t rt_state;
2435*7c478bd9Sstevel@tonic-gate 
2436*7c478bd9Sstevel@tonic-gate 
2437*7c478bd9Sstevel@tonic-gate 	if (badifp == NULL || (rt->rt_spares[0].rts_ifp == badifp)) {
2438*7c478bd9Sstevel@tonic-gate 		/* Poison the route */
2439*7c478bd9Sstevel@tonic-gate 		new = rt->rt_spares[0];
2440*7c478bd9Sstevel@tonic-gate 		new.rts_metric = HOPCNT_INFINITY;
2441*7c478bd9Sstevel@tonic-gate 		rt_state = rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC);
2442*7c478bd9Sstevel@tonic-gate 	}
2443*7c478bd9Sstevel@tonic-gate 
2444*7c478bd9Sstevel@tonic-gate 	if (badifp != NULL) {
2445*7c478bd9Sstevel@tonic-gate 		/*
2446*7c478bd9Sstevel@tonic-gate 		 * Dont mark the rtentry bad unless the ifp for the primary
2447*7c478bd9Sstevel@tonic-gate 		 * route is the bad ifp
2448*7c478bd9Sstevel@tonic-gate 		 */
2449*7c478bd9Sstevel@tonic-gate 		if (rt->rt_spares[0].rts_ifp != badifp)
2450*7c478bd9Sstevel@tonic-gate 			return;
2451*7c478bd9Sstevel@tonic-gate 		/*
2452*7c478bd9Sstevel@tonic-gate 		 * badifp has just gone bad. We want to keep this
2453*7c478bd9Sstevel@tonic-gate 		 * rt_entry around so that we tell our rip-neighbors
2454*7c478bd9Sstevel@tonic-gate 		 * about the bad route, but we can't do anything
2455*7c478bd9Sstevel@tonic-gate 		 * to the kernel itself, so mark it as RS_BADIF
2456*7c478bd9Sstevel@tonic-gate 		 */
2457*7c478bd9Sstevel@tonic-gate 		trace_misc("rtbad:Setting RS_BADIF (%s)", badifp->int_name);
2458*7c478bd9Sstevel@tonic-gate 		rt_state |= RS_BADIF;
2459*7c478bd9Sstevel@tonic-gate 		new.rts_ifp = &dummy_ifp;
2460*7c478bd9Sstevel@tonic-gate 	}
2461*7c478bd9Sstevel@tonic-gate 	rtchange(rt, rt_state, &new, 0);
2462*7c478bd9Sstevel@tonic-gate 	rtswitch(rt, 0);
2463*7c478bd9Sstevel@tonic-gate }
2464*7c478bd9Sstevel@tonic-gate 
2465*7c478bd9Sstevel@tonic-gate 
2466*7c478bd9Sstevel@tonic-gate /*
2467*7c478bd9Sstevel@tonic-gate  * Junk a RS_NET_SYN or RS_LOCAL route,
2468*7c478bd9Sstevel@tonic-gate  *	unless it is needed by another interface.
2469*7c478bd9Sstevel@tonic-gate  */
2470*7c478bd9Sstevel@tonic-gate void
2471*7c478bd9Sstevel@tonic-gate rtbad_sub(struct rt_entry *rt, struct interface *badifp)
2472*7c478bd9Sstevel@tonic-gate {
2473*7c478bd9Sstevel@tonic-gate 	struct interface *ifp, *ifp1;
2474*7c478bd9Sstevel@tonic-gate 	struct intnet *intnetp;
2475*7c478bd9Sstevel@tonic-gate 	uint_t state;
2476*7c478bd9Sstevel@tonic-gate 
2477*7c478bd9Sstevel@tonic-gate 
2478*7c478bd9Sstevel@tonic-gate 	ifp1 = NULL;
2479*7c478bd9Sstevel@tonic-gate 	state = 0;
2480*7c478bd9Sstevel@tonic-gate 
2481*7c478bd9Sstevel@tonic-gate 	if (rt->rt_state & RS_LOCAL) {
2482*7c478bd9Sstevel@tonic-gate 		/*
2483*7c478bd9Sstevel@tonic-gate 		 * Is this the route through loopback for the interface?
2484*7c478bd9Sstevel@tonic-gate 		 * If so, see if it is used by any other interfaces, such
2485*7c478bd9Sstevel@tonic-gate 		 * as a point-to-point interface with the same local address.
2486*7c478bd9Sstevel@tonic-gate 		 */
2487*7c478bd9Sstevel@tonic-gate 		for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
2488*7c478bd9Sstevel@tonic-gate 			/* Retain it if another interface needs it. */
2489*7c478bd9Sstevel@tonic-gate 			if (ifp->int_addr == rt->rt_ifp->int_addr) {
2490*7c478bd9Sstevel@tonic-gate 				state |= RS_LOCAL;
2491*7c478bd9Sstevel@tonic-gate 				ifp1 = ifp;
2492*7c478bd9Sstevel@tonic-gate 				break;
2493*7c478bd9Sstevel@tonic-gate 			}
2494*7c478bd9Sstevel@tonic-gate 		}
2495*7c478bd9Sstevel@tonic-gate 
2496*7c478bd9Sstevel@tonic-gate 	}
2497*7c478bd9Sstevel@tonic-gate 
2498*7c478bd9Sstevel@tonic-gate 	if (!(state & RS_LOCAL)) {
2499*7c478bd9Sstevel@tonic-gate 		/*
2500*7c478bd9Sstevel@tonic-gate 		 * Retain RIPv1 logical network route if there is another
2501*7c478bd9Sstevel@tonic-gate 		 * interface that justifies it.
2502*7c478bd9Sstevel@tonic-gate 		 */
2503*7c478bd9Sstevel@tonic-gate 		if (rt->rt_state & RS_NET_SYN) {
2504*7c478bd9Sstevel@tonic-gate 			for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
2505*7c478bd9Sstevel@tonic-gate 				if ((ifp->int_state & IS_NEED_NET_SYN) &&
2506*7c478bd9Sstevel@tonic-gate 				    rt->rt_mask == ifp->int_std_mask &&
2507*7c478bd9Sstevel@tonic-gate 				    rt->rt_dst == ifp->int_std_addr) {
2508*7c478bd9Sstevel@tonic-gate 					state |= RS_NET_SYN;
2509*7c478bd9Sstevel@tonic-gate 					ifp1 = ifp;
2510*7c478bd9Sstevel@tonic-gate 					break;
2511*7c478bd9Sstevel@tonic-gate 				}
2512*7c478bd9Sstevel@tonic-gate 			}
2513*7c478bd9Sstevel@tonic-gate 		}
2514*7c478bd9Sstevel@tonic-gate 
2515*7c478bd9Sstevel@tonic-gate 		/* or if there is an authority route that needs it. */
2516*7c478bd9Sstevel@tonic-gate 		for (intnetp = intnets; intnetp != NULL;
2517*7c478bd9Sstevel@tonic-gate 		    intnetp = intnetp->intnet_next) {
2518*7c478bd9Sstevel@tonic-gate 			if (intnetp->intnet_addr == rt->rt_dst &&
2519*7c478bd9Sstevel@tonic-gate 			    intnetp->intnet_mask == rt->rt_mask) {
2520*7c478bd9Sstevel@tonic-gate 				state |= (RS_NET_SYN | RS_NET_INT);
2521*7c478bd9Sstevel@tonic-gate 				break;
2522*7c478bd9Sstevel@tonic-gate 			}
2523*7c478bd9Sstevel@tonic-gate 		}
2524*7c478bd9Sstevel@tonic-gate 	}
2525*7c478bd9Sstevel@tonic-gate 
2526*7c478bd9Sstevel@tonic-gate 	if (ifp1 != NULL || (state & RS_NET_SYN)) {
2527*7c478bd9Sstevel@tonic-gate 		struct rt_spare new = rt->rt_spares[0];
2528*7c478bd9Sstevel@tonic-gate 		new.rts_ifp = ifp1;
2529*7c478bd9Sstevel@tonic-gate 		rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state),
2530*7c478bd9Sstevel@tonic-gate 		    &new, 0);
2531*7c478bd9Sstevel@tonic-gate 	} else {
2532*7c478bd9Sstevel@tonic-gate 		rtbad(rt, badifp);
2533*7c478bd9Sstevel@tonic-gate 	}
2534*7c478bd9Sstevel@tonic-gate }
2535*7c478bd9Sstevel@tonic-gate 
2536*7c478bd9Sstevel@tonic-gate /*
2537*7c478bd9Sstevel@tonic-gate  * Called while walking the table looking for sick interfaces
2538*7c478bd9Sstevel@tonic-gate  * or after a time change.
2539*7c478bd9Sstevel@tonic-gate  */
2540*7c478bd9Sstevel@tonic-gate int
2541*7c478bd9Sstevel@tonic-gate walk_bad(struct radix_node *rn,
2542*7c478bd9Sstevel@tonic-gate     void *argp)
2543*7c478bd9Sstevel@tonic-gate {
2544*7c478bd9Sstevel@tonic-gate #define	RT ((struct rt_entry *)rn)
2545*7c478bd9Sstevel@tonic-gate 	struct rt_spare *rts;
2546*7c478bd9Sstevel@tonic-gate 	int i, j = -1;
2547*7c478bd9Sstevel@tonic-gate 
2548*7c478bd9Sstevel@tonic-gate 	/* fix any spare routes through the interface */
2549*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < RT->rt_num_spares; i++) {
2550*7c478bd9Sstevel@tonic-gate 		rts = &((struct rt_entry *)rn)->rt_spares[i];
2551*7c478bd9Sstevel@tonic-gate 
2552*7c478bd9Sstevel@tonic-gate 		if (rts->rts_metric < HOPCNT_INFINITY &&
2553*7c478bd9Sstevel@tonic-gate 		    (rts->rts_ifp == NULL ||
2554*7c478bd9Sstevel@tonic-gate 		    (rts->rts_ifp->int_state & IS_BROKE)))
2555*7c478bd9Sstevel@tonic-gate 			rts_delete(RT, rts);
2556*7c478bd9Sstevel@tonic-gate 		else {
2557*7c478bd9Sstevel@tonic-gate 			if (rts->rts_origin != RO_NONE)
2558*7c478bd9Sstevel@tonic-gate 				j = i;
2559*7c478bd9Sstevel@tonic-gate 		}
2560*7c478bd9Sstevel@tonic-gate 	}
2561*7c478bd9Sstevel@tonic-gate 
2562*7c478bd9Sstevel@tonic-gate 	/*
2563*7c478bd9Sstevel@tonic-gate 	 * Deal with the main route
2564*7c478bd9Sstevel@tonic-gate 	 * finished if it has been handled before or if its interface is ok
2565*7c478bd9Sstevel@tonic-gate 	 */
2566*7c478bd9Sstevel@tonic-gate 	if (RT->rt_ifp == NULL || !(RT->rt_ifp->int_state & IS_BROKE))
2567*7c478bd9Sstevel@tonic-gate 		return (0);
2568*7c478bd9Sstevel@tonic-gate 
2569*7c478bd9Sstevel@tonic-gate 	/* Bad routes for other than interfaces are easy. */
2570*7c478bd9Sstevel@tonic-gate 	if (!(RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) {
2571*7c478bd9Sstevel@tonic-gate 		if (j > 0)
2572*7c478bd9Sstevel@tonic-gate 			rtswitch(RT, NULL);
2573*7c478bd9Sstevel@tonic-gate 		else
2574*7c478bd9Sstevel@tonic-gate 			rtbad(RT, (struct interface *)argp);
2575*7c478bd9Sstevel@tonic-gate 		return (0);
2576*7c478bd9Sstevel@tonic-gate 	}
2577*7c478bd9Sstevel@tonic-gate 
2578*7c478bd9Sstevel@tonic-gate 	rtbad_sub(RT, (struct interface *)argp);
2579*7c478bd9Sstevel@tonic-gate 	return (0);
2580*7c478bd9Sstevel@tonic-gate #undef RT
2581*7c478bd9Sstevel@tonic-gate }
2582*7c478bd9Sstevel@tonic-gate 
2583*7c478bd9Sstevel@tonic-gate /*
2584*7c478bd9Sstevel@tonic-gate  * Called while walking the table to replace a duplicate interface
2585*7c478bd9Sstevel@tonic-gate  * with a backup.
2586*7c478bd9Sstevel@tonic-gate  */
2587*7c478bd9Sstevel@tonic-gate int
2588*7c478bd9Sstevel@tonic-gate walk_rewire(struct radix_node *rn, void *argp)
2589*7c478bd9Sstevel@tonic-gate {
2590*7c478bd9Sstevel@tonic-gate 	struct rt_entry *RT = (struct rt_entry *)rn;
2591*7c478bd9Sstevel@tonic-gate 	struct rewire_data *wire = (struct rewire_data *)argp;
2592*7c478bd9Sstevel@tonic-gate 	struct rt_spare *rts;
2593*7c478bd9Sstevel@tonic-gate 	int i;
2594*7c478bd9Sstevel@tonic-gate 
2595*7c478bd9Sstevel@tonic-gate 	/* fix any spare routes through the interface */
2596*7c478bd9Sstevel@tonic-gate 	rts = RT->rt_spares;
2597*7c478bd9Sstevel@tonic-gate 	for (i = RT->rt_num_spares; i > 0; i--, rts++) {
2598*7c478bd9Sstevel@tonic-gate 		if (rts->rts_ifp == wire->if_old) {
2599*7c478bd9Sstevel@tonic-gate 			rts->rts_ifp = wire->if_new;
2600*7c478bd9Sstevel@tonic-gate 			if ((RT->rt_dst == RIP_DEFAULT) &&
2601*7c478bd9Sstevel@tonic-gate 			    (wire->if_old->int_state & IS_SUPPRESS_RDISC))
2602*7c478bd9Sstevel@tonic-gate 				rdisc_suppress(rts->rts_ifp);
2603*7c478bd9Sstevel@tonic-gate 			if ((rts->rts_metric += wire->metric_delta) >
2604*7c478bd9Sstevel@tonic-gate 			    HOPCNT_INFINITY)
2605*7c478bd9Sstevel@tonic-gate 				rts->rts_metric = HOPCNT_INFINITY;
2606*7c478bd9Sstevel@tonic-gate 
2607*7c478bd9Sstevel@tonic-gate 			/*
2608*7c478bd9Sstevel@tonic-gate 			 * If the main route is getting a worse metric,
2609*7c478bd9Sstevel@tonic-gate 			 * then it may be time to switch to a backup.
2610*7c478bd9Sstevel@tonic-gate 			 */
2611*7c478bd9Sstevel@tonic-gate 			if (i == RT->rt_num_spares && wire->metric_delta > 0) {
2612*7c478bd9Sstevel@tonic-gate 				rtswitch(RT, NULL);
2613*7c478bd9Sstevel@tonic-gate 			}
2614*7c478bd9Sstevel@tonic-gate 		}
2615*7c478bd9Sstevel@tonic-gate 	}
2616*7c478bd9Sstevel@tonic-gate 
2617*7c478bd9Sstevel@tonic-gate 	return (0);
2618*7c478bd9Sstevel@tonic-gate }
2619*7c478bd9Sstevel@tonic-gate 
2620*7c478bd9Sstevel@tonic-gate /* Check the age of an individual route. */
2621*7c478bd9Sstevel@tonic-gate static int
2622*7c478bd9Sstevel@tonic-gate walk_age(struct radix_node *rn, void *argp)
2623*7c478bd9Sstevel@tonic-gate {
2624*7c478bd9Sstevel@tonic-gate #define	RT ((struct rt_entry *)rn)
2625*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
2626*7c478bd9Sstevel@tonic-gate 	struct rt_spare *rts;
2627*7c478bd9Sstevel@tonic-gate 	int i;
2628*7c478bd9Sstevel@tonic-gate 	in_addr_t age_bad_gate = *(in_addr_t *)argp;
2629*7c478bd9Sstevel@tonic-gate 
2630*7c478bd9Sstevel@tonic-gate 
2631*7c478bd9Sstevel@tonic-gate 	/*
2632*7c478bd9Sstevel@tonic-gate 	 * age all of the spare routes, including the primary route
2633*7c478bd9Sstevel@tonic-gate 	 * currently in use
2634*7c478bd9Sstevel@tonic-gate 	 */
2635*7c478bd9Sstevel@tonic-gate 	rts = RT->rt_spares;
2636*7c478bd9Sstevel@tonic-gate 	for (i = RT->rt_num_spares; i != 0; i--, rts++) {
2637*7c478bd9Sstevel@tonic-gate 
2638*7c478bd9Sstevel@tonic-gate 		ifp = rts->rts_ifp;
2639*7c478bd9Sstevel@tonic-gate 		if (i == RT->rt_num_spares) {
2640*7c478bd9Sstevel@tonic-gate 			if (!AGE_RT(RT->rt_state, rts->rts_origin, ifp)) {
2641*7c478bd9Sstevel@tonic-gate 				/*
2642*7c478bd9Sstevel@tonic-gate 				 * Keep various things from deciding ageless
2643*7c478bd9Sstevel@tonic-gate 				 * routes are stale
2644*7c478bd9Sstevel@tonic-gate 				 */
2645*7c478bd9Sstevel@tonic-gate 				rts->rts_time = now.tv_sec;
2646*7c478bd9Sstevel@tonic-gate 				continue;
2647*7c478bd9Sstevel@tonic-gate 			}
2648*7c478bd9Sstevel@tonic-gate 
2649*7c478bd9Sstevel@tonic-gate 			/* forget RIP routes after RIP has been turned off. */
2650*7c478bd9Sstevel@tonic-gate 			if (rip_sock < 0) {
2651*7c478bd9Sstevel@tonic-gate 				rts->rts_time = now_stale + 1;
2652*7c478bd9Sstevel@tonic-gate 			}
2653*7c478bd9Sstevel@tonic-gate 		}
2654*7c478bd9Sstevel@tonic-gate 
2655*7c478bd9Sstevel@tonic-gate 		/* age failing routes */
2656*7c478bd9Sstevel@tonic-gate 		if (age_bad_gate == rts->rts_gate &&
2657*7c478bd9Sstevel@tonic-gate 		    rts->rts_time >= now_stale) {
2658*7c478bd9Sstevel@tonic-gate 			rts->rts_time -= SUPPLY_INTERVAL;
2659*7c478bd9Sstevel@tonic-gate 		}
2660*7c478bd9Sstevel@tonic-gate 
2661*7c478bd9Sstevel@tonic-gate 		/* trash the spare routes when they go bad */
2662*7c478bd9Sstevel@tonic-gate 		if (rts->rts_origin == RO_RIP &&
2663*7c478bd9Sstevel@tonic-gate 		    ((rip_sock < 0) ||
2664*7c478bd9Sstevel@tonic-gate 		    (rts->rts_metric < HOPCNT_INFINITY &&
2665*7c478bd9Sstevel@tonic-gate 		    now_garbage > rts->rts_time)) &&
2666*7c478bd9Sstevel@tonic-gate 		    i != RT->rt_num_spares) {
2667*7c478bd9Sstevel@tonic-gate 			rts_delete(RT, rts);
2668*7c478bd9Sstevel@tonic-gate 		}
2669*7c478bd9Sstevel@tonic-gate 	}
2670*7c478bd9Sstevel@tonic-gate 
2671*7c478bd9Sstevel@tonic-gate 
2672*7c478bd9Sstevel@tonic-gate 	/* finished if the active route is still fresh */
2673*7c478bd9Sstevel@tonic-gate 	if (now_stale <= RT->rt_time)
2674*7c478bd9Sstevel@tonic-gate 		return (0);
2675*7c478bd9Sstevel@tonic-gate 
2676*7c478bd9Sstevel@tonic-gate 	/* try to switch to an alternative */
2677*7c478bd9Sstevel@tonic-gate 	rtswitch(RT, NULL);
2678*7c478bd9Sstevel@tonic-gate 
2679*7c478bd9Sstevel@tonic-gate 	/* Delete a dead route after it has been publically mourned. */
2680*7c478bd9Sstevel@tonic-gate 	if (now_garbage > RT->rt_time) {
2681*7c478bd9Sstevel@tonic-gate 		rtdelete(RT);
2682*7c478bd9Sstevel@tonic-gate 		return (0);
2683*7c478bd9Sstevel@tonic-gate 	}
2684*7c478bd9Sstevel@tonic-gate 
2685*7c478bd9Sstevel@tonic-gate 	/* Start poisoning a bad route before deleting it. */
2686*7c478bd9Sstevel@tonic-gate 	if (now.tv_sec - RT->rt_time > EXPIRE_TIME) {
2687*7c478bd9Sstevel@tonic-gate 		struct rt_spare new = RT->rt_spares[0];
2688*7c478bd9Sstevel@tonic-gate 
2689*7c478bd9Sstevel@tonic-gate 		new.rts_metric = HOPCNT_INFINITY;
2690*7c478bd9Sstevel@tonic-gate 		rtchange(RT, RT->rt_state, &new, 0);
2691*7c478bd9Sstevel@tonic-gate 	}
2692*7c478bd9Sstevel@tonic-gate 	return (0);
2693*7c478bd9Sstevel@tonic-gate }
2694*7c478bd9Sstevel@tonic-gate 
2695*7c478bd9Sstevel@tonic-gate 
2696*7c478bd9Sstevel@tonic-gate /* Watch for dead routes and interfaces. */
2697*7c478bd9Sstevel@tonic-gate void
2698*7c478bd9Sstevel@tonic-gate age(in_addr_t bad_gate)
2699*7c478bd9Sstevel@tonic-gate {
2700*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
2701*7c478bd9Sstevel@tonic-gate 	int need_query = 0;
2702*7c478bd9Sstevel@tonic-gate 
2703*7c478bd9Sstevel@tonic-gate 	/*
2704*7c478bd9Sstevel@tonic-gate 	 * If not listening to RIP, there is no need to age the routes in
2705*7c478bd9Sstevel@tonic-gate 	 * the table.
2706*7c478bd9Sstevel@tonic-gate 	 */
2707*7c478bd9Sstevel@tonic-gate 	age_timer.tv_sec = (now.tv_sec
2708*7c478bd9Sstevel@tonic-gate 	    + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL));
2709*7c478bd9Sstevel@tonic-gate 
2710*7c478bd9Sstevel@tonic-gate 	/*
2711*7c478bd9Sstevel@tonic-gate 	 * Check for dead IS_REMOTE interfaces by timing their
2712*7c478bd9Sstevel@tonic-gate 	 * transmissions.
2713*7c478bd9Sstevel@tonic-gate 	 */
2714*7c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
2715*7c478bd9Sstevel@tonic-gate 		if (!(ifp->int_state & IS_REMOTE))
2716*7c478bd9Sstevel@tonic-gate 			continue;
2717*7c478bd9Sstevel@tonic-gate 
2718*7c478bd9Sstevel@tonic-gate 		/* ignore unreachable remote interfaces */
2719*7c478bd9Sstevel@tonic-gate 		if (!check_remote(ifp))
2720*7c478bd9Sstevel@tonic-gate 			continue;
2721*7c478bd9Sstevel@tonic-gate 
2722*7c478bd9Sstevel@tonic-gate 		/* Restore remote interface that has become reachable */
2723*7c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_BROKE)
2724*7c478bd9Sstevel@tonic-gate 			if_ok(ifp, "remote ", _B_FALSE);
2725*7c478bd9Sstevel@tonic-gate 
2726*7c478bd9Sstevel@tonic-gate 		if (ifp->int_act_time != NEVER &&
2727*7c478bd9Sstevel@tonic-gate 		    now.tv_sec - ifp->int_act_time > EXPIRE_TIME) {
2728*7c478bd9Sstevel@tonic-gate 			writelog(LOG_NOTICE,
2729*7c478bd9Sstevel@tonic-gate 			    "remote interface %s to %s timed out after"
2730*7c478bd9Sstevel@tonic-gate 			    " %ld:%ld",
2731*7c478bd9Sstevel@tonic-gate 			    ifp->int_name,
2732*7c478bd9Sstevel@tonic-gate 			    naddr_ntoa(ifp->int_dstaddr),
2733*7c478bd9Sstevel@tonic-gate 			    (now.tv_sec - ifp->int_act_time)/60,
2734*7c478bd9Sstevel@tonic-gate 			    (now.tv_sec - ifp->int_act_time)%60);
2735*7c478bd9Sstevel@tonic-gate 			if_sick(ifp, _B_FALSE);
2736*7c478bd9Sstevel@tonic-gate 		}
2737*7c478bd9Sstevel@tonic-gate 
2738*7c478bd9Sstevel@tonic-gate 		/*
2739*7c478bd9Sstevel@tonic-gate 		 * If we have not heard from the other router
2740*7c478bd9Sstevel@tonic-gate 		 * recently, ask it.
2741*7c478bd9Sstevel@tonic-gate 		 */
2742*7c478bd9Sstevel@tonic-gate 		if (now.tv_sec >= ifp->int_query_time) {
2743*7c478bd9Sstevel@tonic-gate 			ifp->int_query_time = NEVER;
2744*7c478bd9Sstevel@tonic-gate 			need_query = 1;
2745*7c478bd9Sstevel@tonic-gate 		}
2746*7c478bd9Sstevel@tonic-gate 	}
2747*7c478bd9Sstevel@tonic-gate 
2748*7c478bd9Sstevel@tonic-gate 	/* Age routes. */
2749*7c478bd9Sstevel@tonic-gate 	(void) rn_walktree(rhead, walk_age, &bad_gate);
2750*7c478bd9Sstevel@tonic-gate 
2751*7c478bd9Sstevel@tonic-gate 	/*
2752*7c478bd9Sstevel@tonic-gate 	 * delete old redirected routes to keep the kernel table small
2753*7c478bd9Sstevel@tonic-gate 	 * and prevent blackholes
2754*7c478bd9Sstevel@tonic-gate 	 */
2755*7c478bd9Sstevel@tonic-gate 	del_redirects(bad_gate, now.tv_sec-STALE_TIME);
2756*7c478bd9Sstevel@tonic-gate 
2757*7c478bd9Sstevel@tonic-gate 	/* Update the kernel routing table. */
2758*7c478bd9Sstevel@tonic-gate 	fix_kern();
2759*7c478bd9Sstevel@tonic-gate 
2760*7c478bd9Sstevel@tonic-gate 	/* poke reticent remote gateways */
2761*7c478bd9Sstevel@tonic-gate 	if (need_query)
2762*7c478bd9Sstevel@tonic-gate 		rip_query();
2763*7c478bd9Sstevel@tonic-gate }
2764*7c478bd9Sstevel@tonic-gate 
2765*7c478bd9Sstevel@tonic-gate void
2766*7c478bd9Sstevel@tonic-gate kern_dump(void)
2767*7c478bd9Sstevel@tonic-gate {
2768*7c478bd9Sstevel@tonic-gate 	int i;
2769*7c478bd9Sstevel@tonic-gate 	struct khash *k;
2770*7c478bd9Sstevel@tonic-gate 
2771*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KHASH_SIZE; i++) {
2772*7c478bd9Sstevel@tonic-gate 		for (k = khash_bins[i]; k != NULL; k = k->k_next)
2773*7c478bd9Sstevel@tonic-gate 			trace_khash(k);
2774*7c478bd9Sstevel@tonic-gate 	}
2775*7c478bd9Sstevel@tonic-gate }
2776*7c478bd9Sstevel@tonic-gate 
2777*7c478bd9Sstevel@tonic-gate 
2778*7c478bd9Sstevel@tonic-gate static struct interface *
2779*7c478bd9Sstevel@tonic-gate gwkludge_iflookup(in_addr_t dstaddr, in_addr_t addr, in_addr_t mask)
2780*7c478bd9Sstevel@tonic-gate {
2781*7c478bd9Sstevel@tonic-gate 	uint32_t int_state;
2782*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
2783*7c478bd9Sstevel@tonic-gate 
2784*7c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
2785*7c478bd9Sstevel@tonic-gate 		int_state = ifp->int_state;
2786*7c478bd9Sstevel@tonic-gate 
2787*7c478bd9Sstevel@tonic-gate 		if (!(int_state & IS_REMOTE))
2788*7c478bd9Sstevel@tonic-gate 			continue;
2789*7c478bd9Sstevel@tonic-gate 
2790*7c478bd9Sstevel@tonic-gate 		if (ifp->int_dstaddr == dstaddr && ifp->int_addr == addr &&
2791*7c478bd9Sstevel@tonic-gate 		    ifp->int_mask == mask)
2792*7c478bd9Sstevel@tonic-gate 			return (ifp);
2793*7c478bd9Sstevel@tonic-gate 	}
2794*7c478bd9Sstevel@tonic-gate 	return (NULL);
2795*7c478bd9Sstevel@tonic-gate }
2796