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