xref: /netbsd-src/sbin/routed/table.c (revision 1b8d2f5ad2f0a39950620a1285b1000cebc66a84)
1*1b8d2f5aSmrg /*	$NetBSD: table.c,v 1.28 2018/02/06 09:33:07 mrg Exp $	*/
2fc1a5246Sthorpej 
3b1e0bd39Sthorpej /*
4b1e0bd39Sthorpej  * Copyright (c) 1983, 1988, 1993
5b1e0bd39Sthorpej  *	The Regents of the University of California.  All rights reserved.
6b1e0bd39Sthorpej  *
7b1e0bd39Sthorpej  * Redistribution and use in source and binary forms, with or without
8b1e0bd39Sthorpej  * modification, are permitted provided that the following conditions
9b1e0bd39Sthorpej  * are met:
10b1e0bd39Sthorpej  * 1. Redistributions of source code must retain the above copyright
11b1e0bd39Sthorpej  *    notice, this list of conditions and the following disclaimer.
12b1e0bd39Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
13b1e0bd39Sthorpej  *    notice, this list of conditions and the following disclaimer in the
14b1e0bd39Sthorpej  *    documentation and/or other materials provided with the distribution.
15b1e0bd39Sthorpej  * 3. All advertising materials mentioning features or use of this software
1694b2d428Schristos  *    must display the following acknowledgment:
17b1e0bd39Sthorpej  *	This product includes software developed by the University of
18b1e0bd39Sthorpej  *	California, Berkeley and its contributors.
19b1e0bd39Sthorpej  * 4. Neither the name of the University nor the names of its contributors
20b1e0bd39Sthorpej  *    may be used to endorse or promote products derived from this software
21b1e0bd39Sthorpej  *    without specific prior written permission.
22b1e0bd39Sthorpej  *
23b1e0bd39Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24b1e0bd39Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b1e0bd39Sthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b1e0bd39Sthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27b1e0bd39Sthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b1e0bd39Sthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b1e0bd39Sthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b1e0bd39Sthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b1e0bd39Sthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b1e0bd39Sthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b1e0bd39Sthorpej  * SUCH DAMAGE.
34b1e0bd39Sthorpej  */
35b1e0bd39Sthorpej 
36b1e0bd39Sthorpej #include "defs.h"
37b1e0bd39Sthorpej 
384fea751dSchristos #ifdef __NetBSD__
39*1b8d2f5aSmrg __RCSID("$NetBSD: table.c,v 1.28 2018/02/06 09:33:07 mrg Exp $");
404fea751dSchristos #elif defined(__FreeBSD__)
414fea751dSchristos __RCSID("$FreeBSD$");
424fea751dSchristos #else
43f93fe60aSchristos __RCSID("Revision: 2.27 ");
44f93fe60aSchristos #ident "Revision: 2.27 "
454fea751dSchristos #endif
464fea751dSchristos 
47b1e0bd39Sthorpej static struct rt_spare *rts_better(struct rt_entry *);
486d8ef4dfSthorpej static struct rt_spare rts_empty = {0,0,0,HOPCNT_INFINITY,0,0,0};
4993daf394Schristos static void  set_need_flash(void);
503f50343aSlukem #ifdef _HAVE_SIN_LEN
5193daf394Schristos static void masktrim(struct sockaddr_in *ap);
523f50343aSlukem #else
5393daf394Schristos static void masktrim(struct sockaddr_in_new *ap);
543f50343aSlukem #endif
553f50343aSlukem 
5693daf394Schristos 
57b1e0bd39Sthorpej struct radix_node_head *rhead;		/* root of the radix tree */
58b1e0bd39Sthorpej 
59b1e0bd39Sthorpej int	need_flash = 1;			/* flash update needed
60b1e0bd39Sthorpej 					 * start =1 to suppress the 1st
61b1e0bd39Sthorpej 					 */
62b1e0bd39Sthorpej 
63b1e0bd39Sthorpej struct timeval age_timer;		/* next check of old routes */
64b1e0bd39Sthorpej struct timeval need_kern = {		/* need to update kernel table */
654fea751dSchristos 	EPOCH+MIN_WAITTIME-1, 0
66b1e0bd39Sthorpej };
67b1e0bd39Sthorpej 
68b1e0bd39Sthorpej int	stopint;
69b1e0bd39Sthorpej 
704d3fba59Schristos int	total_routes;
714d3fba59Schristos 
72e7512e5aSchristos /* zap any old routes through this gateway */
73b1e0bd39Sthorpej naddr	age_bad_gate;
74b1e0bd39Sthorpej 
75b1e0bd39Sthorpej 
76b1e0bd39Sthorpej /* It is desirable to "aggregate" routes, to combine differing routes of
77b1e0bd39Sthorpej  * the same metric and next hop into a common route with a smaller netmask
78b1e0bd39Sthorpej  * or to suppress redundant routes, routes that add no information to
79b1e0bd39Sthorpej  * routes with smaller netmasks.
80b1e0bd39Sthorpej  *
81b1e0bd39Sthorpej  * A route is redundant if and only if any and all routes with smaller
82b1e0bd39Sthorpej  * but matching netmasks and nets are the same.  Since routes are
83b1e0bd39Sthorpej  * kept sorted in the radix tree, redundant routes always come second.
84b1e0bd39Sthorpej  *
85b1e0bd39Sthorpej  * There are two kinds of aggregations.  First, two routes of the same bit
86b1e0bd39Sthorpej  * mask and differing only in the least significant bit of the network
87b1e0bd39Sthorpej  * number can be combined into a single route with a coarser mask.
88b1e0bd39Sthorpej  *
89b1e0bd39Sthorpej  * Second, a route can be suppressed in favor of another route with a more
90b1e0bd39Sthorpej  * coarse mask provided no incompatible routes with intermediate masks
91b1e0bd39Sthorpej  * are present.  The second kind of aggregation involves suppressing routes.
92b1e0bd39Sthorpej  * A route must not be suppressed if an incompatible route exists with
93b1e0bd39Sthorpej  * an intermediate mask, since the suppressed route would be covered
94b1e0bd39Sthorpej  * by the intermediate.
95b1e0bd39Sthorpej  *
96b1e0bd39Sthorpej  * This code relies on the radix tree walk encountering routes
97b1e0bd39Sthorpej  * sorted first by address, with the smallest address first.
98b1e0bd39Sthorpej  */
99b1e0bd39Sthorpej 
100b1e0bd39Sthorpej struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest, *ag_finest;
101b1e0bd39Sthorpej 
102b1e0bd39Sthorpej /* #define DEBUG_AG */
103b1e0bd39Sthorpej #ifdef DEBUG_AG
104b1e0bd39Sthorpej #define CHECK_AG() {int acnt = 0; struct ag_info *cag;		\
105b1e0bd39Sthorpej 	for (cag = ag_avail; cag != 0; cag = cag->ag_fine)	\
106b1e0bd39Sthorpej 		acnt++;						\
107b1e0bd39Sthorpej 	for (cag = ag_corsest; cag != 0; cag = cag->ag_fine)	\
108b1e0bd39Sthorpej 		acnt++;						\
109b1e0bd39Sthorpej 	if (acnt != NUM_AG_SLOTS) {				\
110b1e0bd39Sthorpej 		(void)fflush(stderr);				\
111b1e0bd39Sthorpej 		abort();					\
112b1e0bd39Sthorpej 	}							\
113b1e0bd39Sthorpej }
114b1e0bd39Sthorpej #else
115b1e0bd39Sthorpej #define CHECK_AG()
116b1e0bd39Sthorpej #endif
117b1e0bd39Sthorpej 
118b1e0bd39Sthorpej 
119b1e0bd39Sthorpej /* Output the contents of an aggregation table slot.
120b1e0bd39Sthorpej  *	This function must always be immediately followed with the deletion
121b1e0bd39Sthorpej  *	of the target slot.
122b1e0bd39Sthorpej  */
123b1e0bd39Sthorpej static void
ag_out(struct ag_info * ag,void (* out)(struct ag_info *))124b1e0bd39Sthorpej ag_out(struct ag_info *ag,
125b1e0bd39Sthorpej 	 void (*out)(struct ag_info *))
126b1e0bd39Sthorpej {
127b1e0bd39Sthorpej 	struct ag_info *ag_cors;
128b1e0bd39Sthorpej 	naddr bit;
129b1e0bd39Sthorpej 
130b1e0bd39Sthorpej 
1316d8ef4dfSthorpej 	/* Forget it if this route should not be output for split-horizon. */
1326d8ef4dfSthorpej 	if (ag->ag_state & AGS_SPLIT_HZ)
1336d8ef4dfSthorpej 		return;
1346d8ef4dfSthorpej 
135b1e0bd39Sthorpej 	/* If we output both the even and odd twins, then the immediate parent,
136b1e0bd39Sthorpej 	 * if it is present, is redundant, unless the parent manages to
137b1e0bd39Sthorpej 	 * aggregate into something coarser.
138b1e0bd39Sthorpej 	 * On successive calls, this code detects the even and odd twins,
139b1e0bd39Sthorpej 	 * and marks the parent.
140b1e0bd39Sthorpej 	 *
141b1e0bd39Sthorpej 	 * Note that the order in which the radix tree code emits routes
142b1e0bd39Sthorpej 	 * ensures that the twins are seen before the parent is emitted.
143b1e0bd39Sthorpej 	 */
144b1e0bd39Sthorpej 	ag_cors = ag->ag_cors;
145b1e0bd39Sthorpej 	if (ag_cors != 0
146b1e0bd39Sthorpej 	    && ag_cors->ag_mask == ag->ag_mask<<1
147b1e0bd39Sthorpej 	    && ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) {
148b1e0bd39Sthorpej 		ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h)
149b1e0bd39Sthorpej 				      ? AGS_REDUN0
150b1e0bd39Sthorpej 				      : AGS_REDUN1);
151b1e0bd39Sthorpej 	}
152b1e0bd39Sthorpej 
153b1e0bd39Sthorpej 	/* Skip it if this route is itself redundant.
154b1e0bd39Sthorpej 	 *
155b1e0bd39Sthorpej 	 * It is ok to change the contents of the slot here, since it is
156b1e0bd39Sthorpej 	 * always deleted next.
157b1e0bd39Sthorpej 	 */
158b1e0bd39Sthorpej 	if (ag->ag_state & AGS_REDUN0) {
159b1e0bd39Sthorpej 		if (ag->ag_state & AGS_REDUN1)
1606d8ef4dfSthorpej 			return;		/* quit if fully redundant */
1616d8ef4dfSthorpej 		/* make it finer if it is half-redundant */
162b1e0bd39Sthorpej 		bit = (-ag->ag_mask) >> 1;
163b1e0bd39Sthorpej 		ag->ag_dst_h |= bit;
164b1e0bd39Sthorpej 		ag->ag_mask |= bit;
165b1e0bd39Sthorpej 
166b1e0bd39Sthorpej 	} else if (ag->ag_state & AGS_REDUN1) {
1676d8ef4dfSthorpej 		/* make it finer if it is half-redundant */
168b1e0bd39Sthorpej 		bit = (-ag->ag_mask) >> 1;
169b1e0bd39Sthorpej 		ag->ag_mask |= bit;
170b1e0bd39Sthorpej 	}
171b1e0bd39Sthorpej 	out(ag);
172b1e0bd39Sthorpej }
173b1e0bd39Sthorpej 
174b1e0bd39Sthorpej 
175b1e0bd39Sthorpej static void
ag_del(struct ag_info * ag)176b1e0bd39Sthorpej ag_del(struct ag_info *ag)
177b1e0bd39Sthorpej {
178b1e0bd39Sthorpej 	CHECK_AG();
179b1e0bd39Sthorpej 
180b1e0bd39Sthorpej 	if (ag->ag_cors == 0)
181b1e0bd39Sthorpej 		ag_corsest = ag->ag_fine;
182b1e0bd39Sthorpej 	else
183b1e0bd39Sthorpej 		ag->ag_cors->ag_fine = ag->ag_fine;
184b1e0bd39Sthorpej 
185b1e0bd39Sthorpej 	if (ag->ag_fine == 0)
186b1e0bd39Sthorpej 		ag_finest = ag->ag_cors;
187b1e0bd39Sthorpej 	else
188b1e0bd39Sthorpej 		ag->ag_fine->ag_cors = ag->ag_cors;
189b1e0bd39Sthorpej 
190b1e0bd39Sthorpej 	ag->ag_fine = ag_avail;
191b1e0bd39Sthorpej 	ag_avail = ag;
192b1e0bd39Sthorpej 
193b1e0bd39Sthorpej 	CHECK_AG();
194b1e0bd39Sthorpej }
195b1e0bd39Sthorpej 
196b1e0bd39Sthorpej 
19794b2d428Schristos /* Flush routes waiting for aggregation.
198b1e0bd39Sthorpej  *	This must not suppress a route unless it is known that among all
199b1e0bd39Sthorpej  *	routes with coarser masks that match it, the one with the longest
200b1e0bd39Sthorpej  *	mask is appropriate.  This is ensured by scanning the routes
20194b2d428Schristos  *	in lexical order, and with the most restrictive mask first
202b1e0bd39Sthorpej  *	among routes to the same destination.
203b1e0bd39Sthorpej  */
204b1e0bd39Sthorpej void
ag_flush(naddr lim_dst_h,naddr lim_mask,void (* out)(struct ag_info *))205b1e0bd39Sthorpej ag_flush(naddr lim_dst_h,		/* flush routes to here */
206b1e0bd39Sthorpej 	 naddr lim_mask,		/* matching this mask */
207b1e0bd39Sthorpej 	 void (*out)(struct ag_info *))
208b1e0bd39Sthorpej {
209b1e0bd39Sthorpej 	struct ag_info *ag, *ag_cors;
210b1e0bd39Sthorpej 	naddr dst_h;
211b1e0bd39Sthorpej 
212b1e0bd39Sthorpej 
213b1e0bd39Sthorpej 	for (ag = ag_finest;
214b1e0bd39Sthorpej 	     ag != 0 && ag->ag_mask >= lim_mask;
215b1e0bd39Sthorpej 	     ag = ag_cors) {
216b1e0bd39Sthorpej 		ag_cors = ag->ag_cors;
217b1e0bd39Sthorpej 
218b1e0bd39Sthorpej 		/* work on only the specified routes */
219b1e0bd39Sthorpej 		dst_h = ag->ag_dst_h;
220b1e0bd39Sthorpej 		if ((dst_h & lim_mask) != lim_dst_h)
221b1e0bd39Sthorpej 			continue;
222b1e0bd39Sthorpej 
223b1e0bd39Sthorpej 		if (!(ag->ag_state & AGS_SUPPRESS))
224b1e0bd39Sthorpej 			ag_out(ag, out);
225b1e0bd39Sthorpej 
226b1e0bd39Sthorpej 		else for ( ; ; ag_cors = ag_cors->ag_cors) {
227b1e0bd39Sthorpej 			/* Look for a route that can suppress the
228b1e0bd39Sthorpej 			 * current route */
229b1e0bd39Sthorpej 			if (ag_cors == 0) {
230b1e0bd39Sthorpej 				/* failed, so output it and look for
231b1e0bd39Sthorpej 				 * another route to work on
232b1e0bd39Sthorpej 				 */
233b1e0bd39Sthorpej 				ag_out(ag, out);
234b1e0bd39Sthorpej 				break;
235b1e0bd39Sthorpej 			}
236b1e0bd39Sthorpej 
237b1e0bd39Sthorpej 			if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) {
238b1e0bd39Sthorpej 				/* We found a route with a coarser mask that
239b1e0bd39Sthorpej 				 * aggregates the current target.
240b1e0bd39Sthorpej 				 *
241b1e0bd39Sthorpej 				 * If it has a different next hop, it
242b1e0bd39Sthorpej 				 * cannot replace the target, so output
243b1e0bd39Sthorpej 				 * the target.
244b1e0bd39Sthorpej 				 */
245b1e0bd39Sthorpej 				if (ag->ag_gate != ag_cors->ag_gate
246b1e0bd39Sthorpej 				    && !(ag->ag_state & AGS_FINE_GATE)
247b1e0bd39Sthorpej 				    && !(ag_cors->ag_state & AGS_CORS_GATE)) {
248b1e0bd39Sthorpej 					ag_out(ag, out);
249b1e0bd39Sthorpej 					break;
250b1e0bd39Sthorpej 				}
251b1e0bd39Sthorpej 
252b1e0bd39Sthorpej 				/* If the coarse route has a good enough
253b1e0bd39Sthorpej 				 * metric, it suppresses the target.
2546d8ef4dfSthorpej 				 * If the suppressed target was redundant,
2556d8ef4dfSthorpej 				 * then mark the suppressor redundant.
256b1e0bd39Sthorpej 				 */
257b1e0bd39Sthorpej 				if (ag_cors->ag_pref <= ag->ag_pref) {
258b1e0bd39Sthorpej 				    if (AG_IS_REDUN(ag->ag_state)
259b1e0bd39Sthorpej 					&& ag_cors->ag_mask==ag->ag_mask<<1) {
260b1e0bd39Sthorpej 					if (ag_cors->ag_dst_h == dst_h)
261b1e0bd39Sthorpej 					    ag_cors->ag_state |= AGS_REDUN0;
262b1e0bd39Sthorpej 					else
263b1e0bd39Sthorpej 					    ag_cors->ag_state |= AGS_REDUN1;
264b1e0bd39Sthorpej 				    }
265b1e0bd39Sthorpej 				    if (ag->ag_tag != ag_cors->ag_tag)
266b1e0bd39Sthorpej 					    ag_cors->ag_tag = 0;
267b1e0bd39Sthorpej 				    if (ag->ag_nhop != ag_cors->ag_nhop)
268b1e0bd39Sthorpej 					    ag_cors->ag_nhop = 0;
269b1e0bd39Sthorpej 				    break;
270b1e0bd39Sthorpej 				}
271b1e0bd39Sthorpej 			}
272b1e0bd39Sthorpej 		}
273b1e0bd39Sthorpej 
274b1e0bd39Sthorpej 		/* That route has either been output or suppressed */
275b1e0bd39Sthorpej 		ag_cors = ag->ag_cors;
276b1e0bd39Sthorpej 		ag_del(ag);
277b1e0bd39Sthorpej 	}
278b1e0bd39Sthorpej 
279b1e0bd39Sthorpej 	CHECK_AG();
280b1e0bd39Sthorpej }
281b1e0bd39Sthorpej 
282b1e0bd39Sthorpej 
283b1e0bd39Sthorpej /* Try to aggregate a route with previous routes.
284b1e0bd39Sthorpej  */
285b1e0bd39Sthorpej void
ag_check(naddr dst,naddr mask,naddr gate,naddr nhop,char metric,char pref,u_int new_seqno,u_short tag,u_short state,void (* out)(struct ag_info *))286b1e0bd39Sthorpej ag_check(naddr	dst,
287b1e0bd39Sthorpej 	 naddr	mask,
288b1e0bd39Sthorpej 	 naddr	gate,
289b1e0bd39Sthorpej 	 naddr	nhop,
290b1e0bd39Sthorpej 	 char	metric,
291b1e0bd39Sthorpej 	 char	pref,
292f93fe60aSchristos 	 u_int	new_seqno,
293b1e0bd39Sthorpej 	 u_short tag,
294b1e0bd39Sthorpej 	 u_short state,
295b1e0bd39Sthorpej 	 void (*out)(struct ag_info *))	/* output using this */
296b1e0bd39Sthorpej {
297b1e0bd39Sthorpej 	struct ag_info *ag, *nag, *ag_cors;
298b1e0bd39Sthorpej 	naddr xaddr;
299b1e0bd39Sthorpej 	int x;
300b1e0bd39Sthorpej 
301cad376bdSchristos 	dst = ntohl(dst);
302b1e0bd39Sthorpej 
303b1e0bd39Sthorpej 	/* Punt non-contiguous subnet masks.
304b1e0bd39Sthorpej 	 *
305b1e0bd39Sthorpej 	 * (X & -X) contains a single bit if and only if X is a power of 2.
306b1e0bd39Sthorpej 	 * (X + (X & -X)) == 0 if and only if X is a power of 2.
307b1e0bd39Sthorpej 	 */
308b1e0bd39Sthorpej 	if ((mask & -mask) + mask != 0) {
309b1e0bd39Sthorpej 		struct ag_info nc_ag;
310b1e0bd39Sthorpej 
311b1e0bd39Sthorpej 		nc_ag.ag_dst_h = dst;
312b1e0bd39Sthorpej 		nc_ag.ag_mask = mask;
313b1e0bd39Sthorpej 		nc_ag.ag_gate = gate;
314b1e0bd39Sthorpej 		nc_ag.ag_nhop = nhop;
315b1e0bd39Sthorpej 		nc_ag.ag_metric = metric;
316b1e0bd39Sthorpej 		nc_ag.ag_pref = pref;
317b1e0bd39Sthorpej 		nc_ag.ag_tag = tag;
318b1e0bd39Sthorpej 		nc_ag.ag_state = state;
319f93fe60aSchristos 		nc_ag.ag_seqno = new_seqno;
320b1e0bd39Sthorpej 		out(&nc_ag);
321b1e0bd39Sthorpej 		return;
322b1e0bd39Sthorpej 	}
323b1e0bd39Sthorpej 
324b1e0bd39Sthorpej 	/* Search for the right slot in the aggregation table.
325b1e0bd39Sthorpej 	 */
326b1e0bd39Sthorpej 	ag_cors = 0;
327b1e0bd39Sthorpej 	ag = ag_corsest;
328b1e0bd39Sthorpej 	while (ag != 0) {
329b1e0bd39Sthorpej 		if (ag->ag_mask >= mask)
330b1e0bd39Sthorpej 			break;
331b1e0bd39Sthorpej 
332b1e0bd39Sthorpej 		/* Suppress old routes (i.e. combine with compatible routes
333b1e0bd39Sthorpej 		 * with coarser masks) as we look for the right slot in the
334b1e0bd39Sthorpej 		 * aggregation table for the new route.
335b1e0bd39Sthorpej 		 * A route to an address less than the current destination
336b1e0bd39Sthorpej 		 * will not be affected by the current route or any route
337b1e0bd39Sthorpej 		 * seen hereafter.  That means it is safe to suppress it.
33894b2d428Schristos 		 * This check keeps poor routes (e.g. with large hop counts)
33994b2d428Schristos 		 * from preventing suppression of finer routes.
340b1e0bd39Sthorpej 		 */
341b1e0bd39Sthorpej 		if (ag_cors != 0
342b1e0bd39Sthorpej 		    && ag->ag_dst_h < dst
343b1e0bd39Sthorpej 		    && (ag->ag_state & AGS_SUPPRESS)
344b1e0bd39Sthorpej 		    && ag_cors->ag_pref <= ag->ag_pref
345b1e0bd39Sthorpej 		    && (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h
346b1e0bd39Sthorpej 		    && (ag_cors->ag_gate == ag->ag_gate
347b1e0bd39Sthorpej 			|| (ag->ag_state & AGS_FINE_GATE)
348b1e0bd39Sthorpej 			|| (ag_cors->ag_state & AGS_CORS_GATE))) {
3496d8ef4dfSthorpej 			/*  If the suppressed target was redundant,
3506d8ef4dfSthorpej 			 * then mark the suppressor redundant.
3516d8ef4dfSthorpej 			 */
352b1e0bd39Sthorpej 			if (AG_IS_REDUN(ag->ag_state)
353b1e0bd39Sthorpej 			    && ag_cors->ag_mask == ag->ag_mask<<1) {
354b1e0bd39Sthorpej 				if (ag_cors->ag_dst_h == dst)
355b1e0bd39Sthorpej 					ag_cors->ag_state |= AGS_REDUN0;
356b1e0bd39Sthorpej 				else
357b1e0bd39Sthorpej 					ag_cors->ag_state |= AGS_REDUN1;
358b1e0bd39Sthorpej 			}
359b1e0bd39Sthorpej 			if (ag->ag_tag != ag_cors->ag_tag)
360b1e0bd39Sthorpej 				ag_cors->ag_tag = 0;
361b1e0bd39Sthorpej 			if (ag->ag_nhop != ag_cors->ag_nhop)
362b1e0bd39Sthorpej 				ag_cors->ag_nhop = 0;
363b1e0bd39Sthorpej 			ag_del(ag);
364b1e0bd39Sthorpej 			CHECK_AG();
365b1e0bd39Sthorpej 		} else {
366b1e0bd39Sthorpej 			ag_cors = ag;
367b1e0bd39Sthorpej 		}
368b1e0bd39Sthorpej 		ag = ag_cors->ag_fine;
369b1e0bd39Sthorpej 	}
370b1e0bd39Sthorpej 
371b1e0bd39Sthorpej 	/* If we find the even/odd twin of the new route, and if the
372b1e0bd39Sthorpej 	 * masks and so forth are equal, we can aggregate them.
373b1e0bd39Sthorpej 	 * We can probably promote one of the pair.
374b1e0bd39Sthorpej 	 *
375b1e0bd39Sthorpej 	 * Since the routes are encountered in lexical order,
376b1e0bd39Sthorpej 	 * the new route must be odd.  However, the second or later
377b1e0bd39Sthorpej 	 * times around this loop, it could be the even twin promoted
378b1e0bd39Sthorpej 	 * from the even/odd pair of twins of the finer route.
379b1e0bd39Sthorpej 	 */
380b1e0bd39Sthorpej 	while (ag != 0
381b1e0bd39Sthorpej 	       && ag->ag_mask == mask
382b1e0bd39Sthorpej 	       && ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) {
383b1e0bd39Sthorpej 
384b1e0bd39Sthorpej 		/* Here we know the target route and the route in the current
385b1e0bd39Sthorpej 		 * slot have the same netmasks and differ by at most the
386b1e0bd39Sthorpej 		 * last bit.  They are either for the same destination, or
387b1e0bd39Sthorpej 		 * for an even/odd pair of destinations.
388b1e0bd39Sthorpej 		 */
389b1e0bd39Sthorpej 		if (ag->ag_dst_h == dst) {
390b1e0bd39Sthorpej 			/* We have two routes to the same destination.
391b1e0bd39Sthorpej 			 * Routes are encountered in lexical order, so a
392b1e0bd39Sthorpej 			 * route is never promoted until the parent route is
393b1e0bd39Sthorpej 			 * already present.  So we know that the new route is
3946d8ef4dfSthorpej 			 * a promoted (or aggregated) pair and the route
3956d8ef4dfSthorpej 			 * already in the slot is the explicit route.
396b1e0bd39Sthorpej 			 *
397b1e0bd39Sthorpej 			 * Prefer the best route if their metrics differ,
3986d8ef4dfSthorpej 			 * or the aggregated one if not, following a sort
399b1e0bd39Sthorpej 			 * of longest-match rule.
400b1e0bd39Sthorpej 			 */
401b1e0bd39Sthorpej 			if (pref <= ag->ag_pref) {
402b1e0bd39Sthorpej 				ag->ag_gate = gate;
403b1e0bd39Sthorpej 				ag->ag_nhop = nhop;
404b1e0bd39Sthorpej 				ag->ag_tag = tag;
405b1e0bd39Sthorpej 				ag->ag_metric = metric;
406b1e0bd39Sthorpej 				ag->ag_pref = pref;
407f93fe60aSchristos 				if (ag->ag_seqno < new_seqno)
408f93fe60aSchristos 					ag->ag_seqno = new_seqno;
409b1e0bd39Sthorpej 				x = ag->ag_state;
410b1e0bd39Sthorpej 				ag->ag_state = state;
411b1e0bd39Sthorpej 				state = x;
412b1e0bd39Sthorpej 			}
413b1e0bd39Sthorpej 
4146d8ef4dfSthorpej 			/* Some bits are set if they are set on either route,
4156d8ef4dfSthorpej 			 * except when the route is for an interface.
4166d8ef4dfSthorpej 			 */
4176d8ef4dfSthorpej 			if (!(ag->ag_state & AGS_IF))
4186d8ef4dfSthorpej 				ag->ag_state |= (state & (AGS_AGGREGATE_EITHER
4196d8ef4dfSthorpej 							| AGS_REDUN0
4206d8ef4dfSthorpej 							| AGS_REDUN1));
421b1e0bd39Sthorpej 			return;
422b1e0bd39Sthorpej 		}
423b1e0bd39Sthorpej 
424b1e0bd39Sthorpej 		/* If one of the routes can be promoted and the other can
425b1e0bd39Sthorpej 		 * be suppressed, it may be possible to combine them or
426b1e0bd39Sthorpej 		 * worthwhile to promote one.
427b1e0bd39Sthorpej 		 *
4286d8ef4dfSthorpej 		 * Any route that can be promoted is always
429b1e0bd39Sthorpej 		 * marked to be eligible to be suppressed.
430b1e0bd39Sthorpej 		 */
4316d8ef4dfSthorpej 		if (!((state & AGS_AGGREGATE)
432b1e0bd39Sthorpej 		      && (ag->ag_state & AGS_SUPPRESS))
4336d8ef4dfSthorpej 		    && !((ag->ag_state & AGS_AGGREGATE)
434b1e0bd39Sthorpej 			 && (state & AGS_SUPPRESS)))
435b1e0bd39Sthorpej 			break;
436b1e0bd39Sthorpej 
437b1e0bd39Sthorpej 		/* A pair of even/odd twin routes can be combined
438b1e0bd39Sthorpej 		 * if either is redundant, or if they are via the
439b1e0bd39Sthorpej 		 * same gateway and have the same metric.
440b1e0bd39Sthorpej 		 */
441b1e0bd39Sthorpej 		if (AG_IS_REDUN(ag->ag_state)
442b1e0bd39Sthorpej 		    || AG_IS_REDUN(state)
443b1e0bd39Sthorpej 		    || (ag->ag_gate == gate
444b1e0bd39Sthorpej 			&& ag->ag_pref == pref
4456d8ef4dfSthorpej 			&& (state & ag->ag_state & AGS_AGGREGATE) != 0)) {
446b1e0bd39Sthorpej 
447b1e0bd39Sthorpej 			/* We have both the even and odd pairs.
448b1e0bd39Sthorpej 			 * Since the routes are encountered in order,
449b1e0bd39Sthorpej 			 * the route in the slot must be the even twin.
450b1e0bd39Sthorpej 			 *
4516d8ef4dfSthorpej 			 * Combine and promote (aggregate) the pair of routes.
452b1e0bd39Sthorpej 			 */
453f93fe60aSchristos 			if (new_seqno < ag->ag_seqno)
454f93fe60aSchristos 				new_seqno = ag->ag_seqno;
455b1e0bd39Sthorpej 			if (!AG_IS_REDUN(state))
456b1e0bd39Sthorpej 				state &= ~AGS_REDUN1;
457b1e0bd39Sthorpej 			if (AG_IS_REDUN(ag->ag_state))
458b1e0bd39Sthorpej 				state |= AGS_REDUN0;
459b1e0bd39Sthorpej 			else
460b1e0bd39Sthorpej 				state &= ~AGS_REDUN0;
4616d8ef4dfSthorpej 			state |= (ag->ag_state & AGS_AGGREGATE_EITHER);
462b1e0bd39Sthorpej 			if (ag->ag_tag != tag)
463b1e0bd39Sthorpej 				tag = 0;
464b1e0bd39Sthorpej 			if (ag->ag_nhop != nhop)
465b1e0bd39Sthorpej 				nhop = 0;
466b1e0bd39Sthorpej 
467b1e0bd39Sthorpej 			/* Get rid of the even twin that was already
468b1e0bd39Sthorpej 			 * in the slot.
469b1e0bd39Sthorpej 			 */
470b1e0bd39Sthorpej 			ag_del(ag);
471b1e0bd39Sthorpej 
472b1e0bd39Sthorpej 		} else if (ag->ag_pref >= pref
4736d8ef4dfSthorpej 			   && (ag->ag_state & AGS_AGGREGATE)) {
474b1e0bd39Sthorpej 			/* If we cannot combine the pair, maybe the route
475b1e0bd39Sthorpej 			 * with the worse metric can be promoted.
476b1e0bd39Sthorpej 			 *
477b1e0bd39Sthorpej 			 * Promote the old, even twin, by giving its slot
478b1e0bd39Sthorpej 			 * in the table to the new, odd twin.
479b1e0bd39Sthorpej 			 */
480b1e0bd39Sthorpej 			ag->ag_dst_h = dst;
481b1e0bd39Sthorpej 
482b1e0bd39Sthorpej 			xaddr = ag->ag_gate;
483b1e0bd39Sthorpej 			ag->ag_gate = gate;
484b1e0bd39Sthorpej 			gate = xaddr;
485b1e0bd39Sthorpej 
486b1e0bd39Sthorpej 			xaddr = ag->ag_nhop;
487b1e0bd39Sthorpej 			ag->ag_nhop = nhop;
488b1e0bd39Sthorpej 			nhop = xaddr;
489b1e0bd39Sthorpej 
490b1e0bd39Sthorpej 			x = ag->ag_tag;
491b1e0bd39Sthorpej 			ag->ag_tag = tag;
492b1e0bd39Sthorpej 			tag = x;
493b1e0bd39Sthorpej 
4946d8ef4dfSthorpej 			/* The promoted route is even-redundant only if the
4956d8ef4dfSthorpej 			 * even twin was fully redundant.  It is not
4966d8ef4dfSthorpej 			 * odd-redundant because the odd-twin will still be
4976d8ef4dfSthorpej 			 * in the table.
4986d8ef4dfSthorpej 			 */
499b1e0bd39Sthorpej 			x = ag->ag_state;
5006d8ef4dfSthorpej 			if (!AG_IS_REDUN(x))
5016d8ef4dfSthorpej 				x &= ~AGS_REDUN0;
5026d8ef4dfSthorpej 			x &= ~AGS_REDUN1;
503b1e0bd39Sthorpej 			ag->ag_state = state;
504b1e0bd39Sthorpej 			state = x;
505b1e0bd39Sthorpej 
506b1e0bd39Sthorpej 			x = ag->ag_metric;
507b1e0bd39Sthorpej 			ag->ag_metric = metric;
508b1e0bd39Sthorpej 			metric = x;
509b1e0bd39Sthorpej 
510b1e0bd39Sthorpej 			x = ag->ag_pref;
511b1e0bd39Sthorpej 			ag->ag_pref = pref;
512b1e0bd39Sthorpej 			pref = x;
513b1e0bd39Sthorpej 
5146d8ef4dfSthorpej 			/* take the newest sequence number */
515f93fe60aSchristos 			if (new_seqno <= ag->ag_seqno)
516f93fe60aSchristos 				new_seqno = ag->ag_seqno;
517b1e0bd39Sthorpej 			else
518f93fe60aSchristos 				ag->ag_seqno = new_seqno;
519b1e0bd39Sthorpej 
520b1e0bd39Sthorpej 		} else {
5216d8ef4dfSthorpej 			if (!(state & AGS_AGGREGATE))
522b1e0bd39Sthorpej 				break;	/* cannot promote either twin */
523b1e0bd39Sthorpej 
5246d8ef4dfSthorpej 			/* Promote the new, odd twin by shaving its
525b1e0bd39Sthorpej 			 * mask and address.
5266d8ef4dfSthorpej 			 * The promoted route is odd-redundant only if the
5276d8ef4dfSthorpej 			 * odd twin was fully redundant.  It is not
5286d8ef4dfSthorpej 			 * even-redundant because the even twin is still in
5296d8ef4dfSthorpej 			 * the table.
530b1e0bd39Sthorpej 			 */
5316d8ef4dfSthorpej 			if (!AG_IS_REDUN(state))
5326d8ef4dfSthorpej 				state &= ~AGS_REDUN1;
5336d8ef4dfSthorpej 			state &= ~AGS_REDUN0;
534f93fe60aSchristos 			if (new_seqno < ag->ag_seqno)
535f93fe60aSchristos 				new_seqno = ag->ag_seqno;
536b1e0bd39Sthorpej 			else
537f93fe60aSchristos 				ag->ag_seqno = new_seqno;
538b1e0bd39Sthorpej 		}
539b1e0bd39Sthorpej 
540b1e0bd39Sthorpej 		mask <<= 1;
541b1e0bd39Sthorpej 		dst &= mask;
542b1e0bd39Sthorpej 
543b1e0bd39Sthorpej 		if (ag_cors == 0) {
544b1e0bd39Sthorpej 			ag = ag_corsest;
545b1e0bd39Sthorpej 			break;
546b1e0bd39Sthorpej 		}
547b1e0bd39Sthorpej 		ag = ag_cors;
548b1e0bd39Sthorpej 		ag_cors = ag->ag_cors;
549b1e0bd39Sthorpej 	}
550b1e0bd39Sthorpej 
551b1e0bd39Sthorpej 	/* When we can no longer promote and combine routes,
552b1e0bd39Sthorpej 	 * flush the old route in the target slot.  Also flush
553b1e0bd39Sthorpej 	 * any finer routes that we know will never be aggregated by
554b1e0bd39Sthorpej 	 * the new route.
555b1e0bd39Sthorpej 	 *
556b1e0bd39Sthorpej 	 * In case we moved toward coarser masks,
557b1e0bd39Sthorpej 	 * get back where we belong
558b1e0bd39Sthorpej 	 */
559b1e0bd39Sthorpej 	if (ag != 0
560b1e0bd39Sthorpej 	    && ag->ag_mask < mask) {
561b1e0bd39Sthorpej 		ag_cors = ag;
562b1e0bd39Sthorpej 		ag = ag->ag_fine;
563b1e0bd39Sthorpej 	}
564b1e0bd39Sthorpej 
565b1e0bd39Sthorpej 	/* Empty the target slot
566b1e0bd39Sthorpej 	 */
567b1e0bd39Sthorpej 	if (ag != 0 && ag->ag_mask == mask) {
568b1e0bd39Sthorpej 		ag_flush(ag->ag_dst_h, ag->ag_mask, out);
569b1e0bd39Sthorpej 		ag = (ag_cors == 0) ? ag_corsest : ag_cors->ag_fine;
570b1e0bd39Sthorpej 	}
571b1e0bd39Sthorpej 
572b1e0bd39Sthorpej #ifdef DEBUG_AG
573b1e0bd39Sthorpej 	(void)fflush(stderr);
574b1e0bd39Sthorpej 	if (ag == 0 && ag_cors != ag_finest)
575b1e0bd39Sthorpej 		abort();
576b1e0bd39Sthorpej 	if (ag_cors == 0 && ag != ag_corsest)
577b1e0bd39Sthorpej 		abort();
578b1e0bd39Sthorpej 	if (ag != 0 && ag->ag_cors != ag_cors)
579b1e0bd39Sthorpej 		abort();
580b1e0bd39Sthorpej 	if (ag_cors != 0 && ag_cors->ag_fine != ag)
581b1e0bd39Sthorpej 		abort();
582b1e0bd39Sthorpej 	CHECK_AG();
583b1e0bd39Sthorpej #endif
584b1e0bd39Sthorpej 
585b1e0bd39Sthorpej 	/* Save the new route on the end of the table.
586b1e0bd39Sthorpej 	 */
587b1e0bd39Sthorpej 	nag = ag_avail;
588b1e0bd39Sthorpej 	ag_avail = nag->ag_fine;
589b1e0bd39Sthorpej 
590b1e0bd39Sthorpej 	nag->ag_dst_h = dst;
591b1e0bd39Sthorpej 	nag->ag_mask = mask;
592b1e0bd39Sthorpej 	nag->ag_gate = gate;
593b1e0bd39Sthorpej 	nag->ag_nhop = nhop;
594b1e0bd39Sthorpej 	nag->ag_metric = metric;
595b1e0bd39Sthorpej 	nag->ag_pref = pref;
596b1e0bd39Sthorpej 	nag->ag_tag = tag;
597b1e0bd39Sthorpej 	nag->ag_state = state;
598f93fe60aSchristos 	nag->ag_seqno = new_seqno;
599b1e0bd39Sthorpej 
600b1e0bd39Sthorpej 	nag->ag_fine = ag;
601b1e0bd39Sthorpej 	if (ag != 0)
602b1e0bd39Sthorpej 		ag->ag_cors = nag;
603b1e0bd39Sthorpej 	else
604b1e0bd39Sthorpej 		ag_finest = nag;
605b1e0bd39Sthorpej 	nag->ag_cors = ag_cors;
606b1e0bd39Sthorpej 	if (ag_cors == 0)
607b1e0bd39Sthorpej 		ag_corsest = nag;
608b1e0bd39Sthorpej 	else
609b1e0bd39Sthorpej 		ag_cors->ag_fine = nag;
610b1e0bd39Sthorpej 	CHECK_AG();
611b1e0bd39Sthorpej }
612b1e0bd39Sthorpej 
613b1e0bd39Sthorpej 
614756b1291Schristos static const char *
rtm_type_name(u_char type)615b1e0bd39Sthorpej rtm_type_name(u_char type)
616b1e0bd39Sthorpej {
617756b1291Schristos 	static const char *rtm_types[] = {
618b1e0bd39Sthorpej 		"RTM_ADD",
619b1e0bd39Sthorpej 		"RTM_DELETE",
620b1e0bd39Sthorpej 		"RTM_CHANGE",
621b1e0bd39Sthorpej 		"RTM_GET",
622b1e0bd39Sthorpej 		"RTM_LOSING",
623b1e0bd39Sthorpej 		"RTM_REDIRECT",
624b1e0bd39Sthorpej 		"RTM_MISS",
625b1e0bd39Sthorpej 		"RTM_LOCK",
626b1e0bd39Sthorpej 		"RTM_OLDADD",
627b1e0bd39Sthorpej 		"RTM_OLDDEL",
628b1e0bd39Sthorpej 		"RTM_RESOLVE",
629b1e0bd39Sthorpej 		"RTM_NEWADDR",
630b1e0bd39Sthorpej 		"RTM_DELADDR",
631a1d8cb95Sbouyer #ifdef RTM_OIFINFO
632a1d8cb95Sbouyer 		"RTM_OIFINFO",
633a1d8cb95Sbouyer #endif
6344fea751dSchristos 		"RTM_IFINFO",
6354fea751dSchristos 		"RTM_NEWMADDR",
6364fea751dSchristos 		"RTM_DELMADDR"
637b1e0bd39Sthorpej 	};
6384fea751dSchristos #define NEW_RTM_PAT "RTM type %#x"
6394fea751dSchristos 	static char name0[sizeof(NEW_RTM_PAT)+2];
640b1e0bd39Sthorpej 
641b1e0bd39Sthorpej 
642b1e0bd39Sthorpej 	if (type > sizeof(rtm_types)/sizeof(rtm_types[0])
643b1e0bd39Sthorpej 	    || type == 0) {
644f93fe60aSchristos 		snprintf(name0, sizeof(name0), NEW_RTM_PAT, type);
645b1e0bd39Sthorpej 		return name0;
646b1e0bd39Sthorpej 	} else {
647b1e0bd39Sthorpej 		return rtm_types[type-1];
648b1e0bd39Sthorpej 	}
6494fea751dSchristos #undef NEW_RTM_PAT
650b1e0bd39Sthorpej }
651b1e0bd39Sthorpej 
652b1e0bd39Sthorpej 
653b1e0bd39Sthorpej /* Trim a mask in a sockaddr
654b1e0bd39Sthorpej  *	Produce a length of 0 for an address of 0.
655b1e0bd39Sthorpej  *	Otherwise produce the index of the first zero byte.
656b1e0bd39Sthorpej  */
657b1e0bd39Sthorpej void
658b1e0bd39Sthorpej #ifdef _HAVE_SIN_LEN
masktrim(struct sockaddr_in * ap)659b1e0bd39Sthorpej masktrim(struct sockaddr_in *ap)
660b1e0bd39Sthorpej #else
661b1e0bd39Sthorpej masktrim(struct sockaddr_in_new *ap)
662b1e0bd39Sthorpej #endif
663b1e0bd39Sthorpej {
6643f50343aSlukem 	char *cp;
665b1e0bd39Sthorpej 
666b1e0bd39Sthorpej 	if (ap->sin_addr.s_addr == 0) {
667b1e0bd39Sthorpej 		ap->sin_len = 0;
668b1e0bd39Sthorpej 		return;
669b1e0bd39Sthorpej 	}
67014c0cd35Sjoerg 	cp = (char *)&ap->sin_addr.s_addr + sizeof(ap->sin_addr.s_addr);
671b1e0bd39Sthorpej 	while (*--cp == 0)
672b1e0bd39Sthorpej 		continue;
673b1e0bd39Sthorpej 	ap->sin_len = cp - (char*)ap + 1;
674b1e0bd39Sthorpej }
675b1e0bd39Sthorpej 
676b1e0bd39Sthorpej 
677b1e0bd39Sthorpej /* Tell the kernel to add, delete or change a route
678b1e0bd39Sthorpej  */
679b1e0bd39Sthorpej static void
rtioctl(int action,naddr dst,naddr gate,naddr mask,int metric,int flags)680b1e0bd39Sthorpej rtioctl(int action,			/* RTM_DELETE, etc */
681b1e0bd39Sthorpej 	naddr dst,
682b1e0bd39Sthorpej 	naddr gate,
683b1e0bd39Sthorpej 	naddr mask,
684b1e0bd39Sthorpej 	int metric,
685b1e0bd39Sthorpej 	int flags)
686b1e0bd39Sthorpej {
687b1e0bd39Sthorpej 	struct {
688b1e0bd39Sthorpej 		struct rt_msghdr w_rtm;
689b1e0bd39Sthorpej 		struct sockaddr_in w_dst;
690b1e0bd39Sthorpej 		struct sockaddr_in w_gate;
691b1e0bd39Sthorpej #ifdef _HAVE_SA_LEN
692b1e0bd39Sthorpej 		struct sockaddr_in w_mask;
693b1e0bd39Sthorpej #else
694b1e0bd39Sthorpej 		struct sockaddr_in_new w_mask;
695b1e0bd39Sthorpej #endif
696b1e0bd39Sthorpej 	} w;
697b1e0bd39Sthorpej 	long cc;
698e7512e5aSchristos #   define PAT " %-10s %s metric=%d flags=%#x"
699e7512e5aSchristos #   define ARGS rtm_type_name(action), rtname(dst,mask,gate), metric, flags
700b1e0bd39Sthorpej 
701b1e0bd39Sthorpej again:
7023f50343aSlukem 	memset(&w, 0, sizeof(w));
703b1e0bd39Sthorpej 	w.w_rtm.rtm_msglen = sizeof(w);
704b1e0bd39Sthorpej 	w.w_rtm.rtm_version = RTM_VERSION;
705b1e0bd39Sthorpej 	w.w_rtm.rtm_type = action;
706b1e0bd39Sthorpej 	w.w_rtm.rtm_flags = flags;
707b1e0bd39Sthorpej 	w.w_rtm.rtm_seq = ++rt_sock_seqno;
708b1e0bd39Sthorpej 	w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
7096d8ef4dfSthorpej 	if (metric != 0 || action == RTM_CHANGE) {
710b1e0bd39Sthorpej 		w.w_rtm.rtm_rmx.rmx_hopcount = metric;
711b1e0bd39Sthorpej 		w.w_rtm.rtm_inits |= RTV_HOPCOUNT;
712b1e0bd39Sthorpej 	}
713b1e0bd39Sthorpej 	w.w_dst.sin_family = AF_INET;
714b1e0bd39Sthorpej 	w.w_dst.sin_addr.s_addr = dst;
715b1e0bd39Sthorpej 	w.w_gate.sin_family = AF_INET;
716b1e0bd39Sthorpej 	w.w_gate.sin_addr.s_addr = gate;
717b1e0bd39Sthorpej #ifdef _HAVE_SA_LEN
718b1e0bd39Sthorpej 	w.w_dst.sin_len = sizeof(w.w_dst);
719b1e0bd39Sthorpej 	w.w_gate.sin_len = sizeof(w.w_gate);
720b1e0bd39Sthorpej #endif
721b1e0bd39Sthorpej 	if (mask == HOST_MASK) {
722b1e0bd39Sthorpej 		w.w_rtm.rtm_flags |= RTF_HOST;
723b1e0bd39Sthorpej 		w.w_rtm.rtm_msglen -= sizeof(w.w_mask);
724b1e0bd39Sthorpej 	} else {
725b1e0bd39Sthorpej 		w.w_rtm.rtm_addrs |= RTA_NETMASK;
726b1e0bd39Sthorpej 		w.w_mask.sin_addr.s_addr = htonl(mask);
727b1e0bd39Sthorpej #ifdef _HAVE_SA_LEN
728b1e0bd39Sthorpej 		masktrim(&w.w_mask);
729b1e0bd39Sthorpej 		if (w.w_mask.sin_len == 0)
730b1e0bd39Sthorpej 			w.w_mask.sin_len = sizeof(long);
731b1e0bd39Sthorpej 		w.w_rtm.rtm_msglen -= (sizeof(w.w_mask) - w.w_mask.sin_len);
732b1e0bd39Sthorpej #endif
733b1e0bd39Sthorpej 	}
7344d3fba59Schristos 
735b1e0bd39Sthorpej #ifndef NO_INSTALL
736b1e0bd39Sthorpej 	cc = write(rt_sock, &w, w.w_rtm.rtm_msglen);
737b1e0bd39Sthorpej 	if (cc < 0) {
738b1e0bd39Sthorpej 		if (errno == ESRCH
739b1e0bd39Sthorpej 		    && (action == RTM_CHANGE || action == RTM_DELETE)) {
740e7512e5aSchristos 			trace_act("route disappeared before" PAT, ARGS);
741b1e0bd39Sthorpej 			if (action == RTM_CHANGE) {
742b1e0bd39Sthorpej 				action = RTM_ADD;
743b1e0bd39Sthorpej 				goto again;
744b1e0bd39Sthorpej 			}
745b1e0bd39Sthorpej 			return;
746b1e0bd39Sthorpej 		}
7476d8ef4dfSthorpej 		msglog("write(rt_sock)" PAT ": %s", ARGS, strerror(errno));
748e7512e5aSchristos 		return;
749e7512e5aSchristos 	} else if (cc != w.w_rtm.rtm_msglen) {
750756b1291Schristos 		msglog("write(rt_sock) wrote %ld instead of %d for" PAT,
751e7512e5aSchristos 		       cc, w.w_rtm.rtm_msglen, ARGS);
752e7512e5aSchristos 		return;
753b1e0bd39Sthorpej 	}
754b1e0bd39Sthorpej #endif
755e7512e5aSchristos 	if (TRACEKERNEL)
7566d8ef4dfSthorpej 		trace_misc("write kernel" PAT, ARGS);
757e7512e5aSchristos #undef PAT
758e7512e5aSchristos #undef ARGS
759b1e0bd39Sthorpej }
760b1e0bd39Sthorpej 
761b1e0bd39Sthorpej 
762b1e0bd39Sthorpej #define KHASH_SIZE 71			/* should be prime */
763b1e0bd39Sthorpej #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
764b1e0bd39Sthorpej static struct khash {
765b1e0bd39Sthorpej 	struct khash *k_next;
766b1e0bd39Sthorpej 	naddr	k_dst;
767b1e0bd39Sthorpej 	naddr	k_mask;
768b1e0bd39Sthorpej 	naddr	k_gate;
769b1e0bd39Sthorpej 	short	k_metric;
770b1e0bd39Sthorpej 	u_short	k_state;
771b1e0bd39Sthorpej #define	    KS_NEW	0x001
7726d8ef4dfSthorpej #define	    KS_DELETE	0x002		/* need to delete the route */
773b1e0bd39Sthorpej #define	    KS_ADD	0x004		/* add to the kernel */
774b1e0bd39Sthorpej #define	    KS_CHANGE	0x008		/* tell kernel to change the route */
775b1e0bd39Sthorpej #define	    KS_DEL_ADD	0x010		/* delete & add to change the kernel */
776b1e0bd39Sthorpej #define	    KS_STATIC	0x020		/* Static flag in kernel */
777b1e0bd39Sthorpej #define	    KS_GATEWAY	0x040		/* G flag in kernel */
778b1e0bd39Sthorpej #define	    KS_DYNAMIC	0x080		/* result of redirect */
7796d8ef4dfSthorpej #define	    KS_DELETED	0x100		/* already deleted from kernel */
7806d8ef4dfSthorpej #define	    KS_CHECK	0x200
781e3db1a05Sozaki-r #define	    KS_LOCAL	0x400
782b1e0bd39Sthorpej 	time_t	k_keep;
783b1e0bd39Sthorpej #define	    K_KEEP_LIM	30
784e7512e5aSchristos 	time_t	k_redirect_time;	/* when redirected route 1st seen */
785b1e0bd39Sthorpej } *khash_bins[KHASH_SIZE];
786b1e0bd39Sthorpej 
787b1e0bd39Sthorpej 
788b1e0bd39Sthorpej static struct khash*
kern_find(naddr dst,naddr mask,struct khash *** ppk)789b1e0bd39Sthorpej kern_find(naddr dst, naddr mask, struct khash ***ppk)
790b1e0bd39Sthorpej {
791b1e0bd39Sthorpej 	struct khash *k, **pk;
792b1e0bd39Sthorpej 
793b1e0bd39Sthorpej 	for (pk = &KHASH(dst,mask); (k = *pk) != 0; pk = &k->k_next) {
794b1e0bd39Sthorpej 		if (k->k_dst == dst && k->k_mask == mask)
795b1e0bd39Sthorpej 			break;
796b1e0bd39Sthorpej 	}
797b1e0bd39Sthorpej 	if (ppk != 0)
798b1e0bd39Sthorpej 		*ppk = pk;
799b1e0bd39Sthorpej 	return k;
800b1e0bd39Sthorpej }
801b1e0bd39Sthorpej 
802b1e0bd39Sthorpej 
803b1e0bd39Sthorpej static struct khash*
kern_add(naddr dst,naddr mask)804b1e0bd39Sthorpej kern_add(naddr dst, naddr mask)
805b1e0bd39Sthorpej {
806b1e0bd39Sthorpej 	struct khash *k, **pk;
807b1e0bd39Sthorpej 
808b1e0bd39Sthorpej 	k = kern_find(dst, mask, &pk);
809b1e0bd39Sthorpej 	if (k != 0)
810b1e0bd39Sthorpej 		return k;
811b1e0bd39Sthorpej 
8126d8ef4dfSthorpej 	k = (struct khash *)rtmalloc(sizeof(*k), "kern_add");
813b1e0bd39Sthorpej 
8143f50343aSlukem 	memset(k, 0, sizeof(*k));
815b1e0bd39Sthorpej 	k->k_dst = dst;
816b1e0bd39Sthorpej 	k->k_mask = mask;
817b1e0bd39Sthorpej 	k->k_state = KS_NEW;
818b1e0bd39Sthorpej 	k->k_keep = now.tv_sec;
819b1e0bd39Sthorpej 	*pk = k;
820b1e0bd39Sthorpej 
821b1e0bd39Sthorpej 	return k;
822b1e0bd39Sthorpej }
823b1e0bd39Sthorpej 
824b1e0bd39Sthorpej 
8254d3fba59Schristos /* If a kernel route has a non-zero metric, check that it is still in the
8264d3fba59Schristos  *	daemon table, and not deleted by interfaces coming and going.
827b1e0bd39Sthorpej  */
828b1e0bd39Sthorpej static void
kern_check_static(struct khash * k,struct interface * ifp)829b1e0bd39Sthorpej kern_check_static(struct khash *k,
830b1e0bd39Sthorpej 		  struct interface *ifp)
831b1e0bd39Sthorpej {
832b1e0bd39Sthorpej 	struct rt_entry *rt;
8336d8ef4dfSthorpej 	struct rt_spare new;
834b1e0bd39Sthorpej 
835b1e0bd39Sthorpej 	if (k->k_metric == 0)
836b1e0bd39Sthorpej 		return;
837b1e0bd39Sthorpej 
83894b2d428Schristos 	memset(&new, 0, sizeof(new));
8396d8ef4dfSthorpej 	new.rts_ifp = ifp;
8406d8ef4dfSthorpej 	new.rts_gate = k->k_gate;
8416d8ef4dfSthorpej 	new.rts_router = (ifp != 0) ? ifp->int_addr : loopaddr;
8426d8ef4dfSthorpej 	new.rts_metric = k->k_metric;
8436d8ef4dfSthorpej 	new.rts_time = now.tv_sec;
844b1e0bd39Sthorpej 
845b1e0bd39Sthorpej 	rt = rtget(k->k_dst, k->k_mask);
846b1e0bd39Sthorpej 	if (rt != 0) {
847b1e0bd39Sthorpej 		if (!(rt->rt_state & RS_STATIC))
8486d8ef4dfSthorpej 			rtchange(rt, rt->rt_state | RS_STATIC, &new, 0);
849b1e0bd39Sthorpej 	} else {
8506d8ef4dfSthorpej 		rtadd(k->k_dst, k->k_mask, RS_STATIC, &new);
851b1e0bd39Sthorpej 	}
852b1e0bd39Sthorpej }
853b1e0bd39Sthorpej 
854b1e0bd39Sthorpej 
855e7512e5aSchristos /* operate on a kernel entry
856e7512e5aSchristos  */
857e7512e5aSchristos static void
kern_ioctl(struct khash * k,int action,int flags)858e7512e5aSchristos kern_ioctl(struct khash *k,
859e7512e5aSchristos 	   int action,			/* RTM_DELETE, etc */
860e7512e5aSchristos 	   int flags)
861e7512e5aSchristos 
862e7512e5aSchristos {
863e7512e5aSchristos 	switch (action) {
864e7512e5aSchristos 	case RTM_DELETE:
865e7512e5aSchristos 		k->k_state &= ~KS_DYNAMIC;
866e7512e5aSchristos 		if (k->k_state & KS_DELETED)
867e7512e5aSchristos 			return;
868e7512e5aSchristos 		k->k_state |= KS_DELETED;
869e7512e5aSchristos 		break;
870e7512e5aSchristos 	case RTM_ADD:
871e7512e5aSchristos 		k->k_state &= ~KS_DELETED;
872e7512e5aSchristos 		break;
873e7512e5aSchristos 	case RTM_CHANGE:
874e7512e5aSchristos 		if (k->k_state & KS_DELETED) {
875e7512e5aSchristos 			action = RTM_ADD;
876e7512e5aSchristos 			k->k_state &= ~KS_DELETED;
877e7512e5aSchristos 		}
878e7512e5aSchristos 		break;
879e7512e5aSchristos 	}
880e7512e5aSchristos 
881e7512e5aSchristos 	rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_metric, flags);
882e7512e5aSchristos }
883e7512e5aSchristos 
884e7512e5aSchristos 
885b1e0bd39Sthorpej /* add a route the kernel told us
886b1e0bd39Sthorpej  */
887b1e0bd39Sthorpej static void
rtm_add(struct rt_msghdr * rtm,struct rt_addrinfo * info,time_t keep)888b1e0bd39Sthorpej rtm_add(struct rt_msghdr *rtm,
889b1e0bd39Sthorpej 	struct rt_addrinfo *info,
890b1e0bd39Sthorpej 	time_t keep)
891b1e0bd39Sthorpej {
892b1e0bd39Sthorpej 	struct khash *k;
893b1e0bd39Sthorpej 	struct interface *ifp;
894b1e0bd39Sthorpej 	naddr mask;
895b1e0bd39Sthorpej 
896b1e0bd39Sthorpej 
897b1e0bd39Sthorpej 	if (rtm->rtm_flags & RTF_HOST) {
898b1e0bd39Sthorpej 		mask = HOST_MASK;
899b1e0bd39Sthorpej 	} else if (INFO_MASK(info) != 0) {
900b1e0bd39Sthorpej 		mask = ntohl(S_ADDR(INFO_MASK(info)));
901b1e0bd39Sthorpej 	} else {
902e7512e5aSchristos 		msglog("ignore %s without mask", rtm_type_name(rtm->rtm_type));
903b1e0bd39Sthorpej 		return;
904b1e0bd39Sthorpej 	}
905b1e0bd39Sthorpej 
906b1e0bd39Sthorpej 	k = kern_add(S_ADDR(INFO_DST(info)), mask);
907b1e0bd39Sthorpej 	if (k->k_state & KS_NEW)
908b1e0bd39Sthorpej 		k->k_keep = now.tv_sec+keep;
9096d8ef4dfSthorpej 	if (INFO_GATE(info) == 0) {
9106d8ef4dfSthorpej 		trace_act("note %s without gateway",
9116d8ef4dfSthorpej 			  rtm_type_name(rtm->rtm_type));
9126d8ef4dfSthorpej 		k->k_metric = HOPCNT_INFINITY;
9136d8ef4dfSthorpej 	} else if (INFO_GATE(info)->sa_family != AF_INET) {
9146d8ef4dfSthorpej 		trace_act("note %s with gateway AF=%d",
9156d8ef4dfSthorpej 			  rtm_type_name(rtm->rtm_type),
9166d8ef4dfSthorpej 			  INFO_GATE(info)->sa_family);
9176d8ef4dfSthorpej 		k->k_metric = HOPCNT_INFINITY;
9186d8ef4dfSthorpej 	} else {
919b1e0bd39Sthorpej 		k->k_gate = S_ADDR(INFO_GATE(info));
920b1e0bd39Sthorpej 		k->k_metric = rtm->rtm_rmx.rmx_hopcount;
921b1e0bd39Sthorpej 		if (k->k_metric < 0)
922b1e0bd39Sthorpej 			k->k_metric = 0;
9236d8ef4dfSthorpej 		else if (k->k_metric > HOPCNT_INFINITY-1)
9246d8ef4dfSthorpej 			k->k_metric = HOPCNT_INFINITY-1;
9256d8ef4dfSthorpej 	}
9266d8ef4dfSthorpej 	k->k_state &= ~(KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD
9276d8ef4dfSthorpej 			| KS_DELETED | KS_GATEWAY | KS_STATIC
928e3db1a05Sozaki-r 			| KS_NEW | KS_CHECK | KS_LOCAL);
929b1e0bd39Sthorpej 	if (rtm->rtm_flags & RTF_GATEWAY)
930b1e0bd39Sthorpej 		k->k_state |= KS_GATEWAY;
931b1e0bd39Sthorpej 	if (rtm->rtm_flags & RTF_STATIC)
932b1e0bd39Sthorpej 		k->k_state |= KS_STATIC;
933e3db1a05Sozaki-r 	if (rtm->rtm_flags & RTF_LOCAL)
934e3db1a05Sozaki-r 		k->k_state |= KS_LOCAL;
9354d3fba59Schristos 
9364d3fba59Schristos 	if (0 != (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED))) {
937e7512e5aSchristos 		if (INFO_AUTHOR(info) != 0
938e7512e5aSchristos 		    && INFO_AUTHOR(info)->sa_family == AF_INET)
939e7512e5aSchristos 			ifp = iflookup(S_ADDR(INFO_AUTHOR(info)));
940e7512e5aSchristos 		else
941e7512e5aSchristos 			ifp = 0;
942e7512e5aSchristos 		if (supplier
943e7512e5aSchristos 		    && (ifp == 0 || !(ifp->int_state & IS_REDIRECT_OK))) {
944b1e0bd39Sthorpej 			/* Routers are not supposed to listen to redirects,
945e7512e5aSchristos 			 * so delete it if it came via an unknown interface
946e7512e5aSchristos 			 * or the interface does not have special permission.
947b1e0bd39Sthorpej 			 */
9484d3fba59Schristos 			k->k_state &= ~KS_DYNAMIC;
9494d3fba59Schristos 			k->k_state |= KS_DELETE;
9504d3fba59Schristos 			LIM_SEC(need_kern, 0);
951e7512e5aSchristos 			trace_act("mark for deletion redirected %s --> %s"
952e7512e5aSchristos 				  " via %s",
953b1e0bd39Sthorpej 				  addrname(k->k_dst, k->k_mask, 0),
954e7512e5aSchristos 				  naddr_ntoa(k->k_gate),
955e7512e5aSchristos 				  ifp ? ifp->int_name : "unknown interface");
9564d3fba59Schristos 		} else {
9574d3fba59Schristos 			k->k_state |= KS_DYNAMIC;
9584d3fba59Schristos 			k->k_redirect_time = now.tv_sec;
959e7512e5aSchristos 			trace_act("accept redirected %s --> %s via %s",
960e7512e5aSchristos 				  addrname(k->k_dst, k->k_mask, 0),
961e7512e5aSchristos 				  naddr_ntoa(k->k_gate),
962e7512e5aSchristos 				  ifp ? ifp->int_name : "unknown interface");
963b1e0bd39Sthorpej 		}
9644d3fba59Schristos 		return;
965b1e0bd39Sthorpej 	}
966b1e0bd39Sthorpej 
9674d3fba59Schristos 	/* If it is not a static route, quit until the next comparison
9684d3fba59Schristos 	 * between the kernel and daemon tables, when it will be deleted.
969b1e0bd39Sthorpej 	 */
970e3db1a05Sozaki-r 	if (!(k->k_state & KS_STATIC) && !(k->k_state & KS_LOCAL)) {
971b1e0bd39Sthorpej 		k->k_state |= KS_DELETE;
972b1e0bd39Sthorpej 		LIM_SEC(need_kern, k->k_keep);
973b1e0bd39Sthorpej 		return;
974b1e0bd39Sthorpej 	}
975b1e0bd39Sthorpej 
976b1e0bd39Sthorpej 	/* Put static routes with real metrics into the daemon table so
977b1e0bd39Sthorpej 	 * they can be advertised.
978b1e0bd39Sthorpej 	 *
979e7512e5aSchristos 	 * Find the interface toward the gateway.
980b1e0bd39Sthorpej 	 */
981b1e0bd39Sthorpej 	ifp = iflookup(k->k_gate);
982b1e0bd39Sthorpej 	if (ifp == 0)
983b1e0bd39Sthorpej 		msglog("static route %s --> %s impossibly lacks ifp",
984b1e0bd39Sthorpej 		       addrname(S_ADDR(INFO_DST(info)), mask, 0),
985b1e0bd39Sthorpej 		       naddr_ntoa(k->k_gate));
986b1e0bd39Sthorpej 
987b1e0bd39Sthorpej 	kern_check_static(k, ifp);
988b1e0bd39Sthorpej }
989b1e0bd39Sthorpej 
990b1e0bd39Sthorpej 
991b1e0bd39Sthorpej /* deal with packet loss
992b1e0bd39Sthorpej  */
993b1e0bd39Sthorpej static void
rtm_lose(struct rt_msghdr * rtm,struct rt_addrinfo * info)994b1e0bd39Sthorpej rtm_lose(struct rt_msghdr *rtm,
995b1e0bd39Sthorpej 	 struct rt_addrinfo *info)
996b1e0bd39Sthorpej {
997b1e0bd39Sthorpej 	if (INFO_GATE(info) == 0
998b1e0bd39Sthorpej 	    || INFO_GATE(info)->sa_family != AF_INET) {
999e7512e5aSchristos 		trace_act("ignore %s without gateway",
1000b1e0bd39Sthorpej 			  rtm_type_name(rtm->rtm_type));
1001b1e0bd39Sthorpej 		return;
1002b1e0bd39Sthorpej 	}
1003b1e0bd39Sthorpej 
10046d8ef4dfSthorpej 	if (rdisc_ok)
1005b1e0bd39Sthorpej 		rdisc_age(S_ADDR(INFO_GATE(info)));
1006b1e0bd39Sthorpej 	age(S_ADDR(INFO_GATE(info)));
1007b1e0bd39Sthorpej }
1008b1e0bd39Sthorpej 
1009b1e0bd39Sthorpej 
10106d8ef4dfSthorpej /* Make the gateway slot of an info structure point to something
10116d8ef4dfSthorpej  * useful.  If it is not already useful, but it specifies an interface,
10126d8ef4dfSthorpej  * then fill in the sockaddr_in provided and point it there.
10136d8ef4dfSthorpej  */
10146d8ef4dfSthorpej static int
get_info_gate(const struct sockaddr ** sap,struct sockaddr_in * rsin)1015f29f4783Schristos get_info_gate(const struct sockaddr **sap,
1016cbbd79f7Slukem 	      struct sockaddr_in *rsin)
10176d8ef4dfSthorpej {
1018f29f4783Schristos 	const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)*sap;
10196d8ef4dfSthorpej 	struct interface *ifp;
10206d8ef4dfSthorpej 
10216d8ef4dfSthorpej 	if (sdl == 0)
10226d8ef4dfSthorpej 		return 0;
10236d8ef4dfSthorpej 	if ((sdl)->sdl_family == AF_INET)
10246d8ef4dfSthorpej 		return 1;
10256d8ef4dfSthorpej 	if ((sdl)->sdl_family != AF_LINK)
10266d8ef4dfSthorpej 		return 0;
10276d8ef4dfSthorpej 
10286d8ef4dfSthorpej 	ifp = ifwithindex(sdl->sdl_index, 1);
10296d8ef4dfSthorpej 	if (ifp == 0)
10306d8ef4dfSthorpej 		return 0;
10316d8ef4dfSthorpej 
1032cbbd79f7Slukem 	rsin->sin_addr.s_addr = ifp->int_addr;
10336d8ef4dfSthorpej #ifdef _HAVE_SA_LEN
1034cbbd79f7Slukem 	rsin->sin_len = sizeof(*rsin);
10356d8ef4dfSthorpej #endif
1036cbbd79f7Slukem 	rsin->sin_family = AF_INET;
1037f29f4783Schristos 	*sap = (const struct sockaddr*)rsin;
10386d8ef4dfSthorpej 
10396d8ef4dfSthorpej 	return 1;
10406d8ef4dfSthorpej }
10416d8ef4dfSthorpej 
10426d8ef4dfSthorpej 
1043b1e0bd39Sthorpej /* Clean the kernel table by copying it to the daemon image.
1044b1e0bd39Sthorpej  * Eventually the daemon will delete any extra routes.
1045b1e0bd39Sthorpej  */
1046b1e0bd39Sthorpej void
flush_kern(void)1047b1e0bd39Sthorpej flush_kern(void)
1048b1e0bd39Sthorpej {
10496d8ef4dfSthorpej 	static char *sysctl_buf;
10506d8ef4dfSthorpej 	static size_t sysctl_buf_size = 0;
1051b1e0bd39Sthorpej 	size_t needed;
1052b1e0bd39Sthorpej 	int mib[6];
10536d8ef4dfSthorpej 	char *next, *lim;
1054b1e0bd39Sthorpej 	struct rt_msghdr *rtm;
10556d8ef4dfSthorpej 	struct sockaddr_in gate_sin;
1056b1e0bd39Sthorpej 	struct rt_addrinfo info;
10576d8ef4dfSthorpej 	int i;
10586d8ef4dfSthorpej 	struct khash *k;
1059b1e0bd39Sthorpej 
1060b1e0bd39Sthorpej 
10616d8ef4dfSthorpej 	for (i = 0; i < KHASH_SIZE; i++) {
10626d8ef4dfSthorpej 		for (k = khash_bins[i]; k != 0; k = k->k_next) {
10636d8ef4dfSthorpej 			k->k_state |= KS_CHECK;
10646d8ef4dfSthorpej 		}
10656d8ef4dfSthorpej 	}
10666d8ef4dfSthorpej 
1067b1e0bd39Sthorpej 	mib[0] = CTL_NET;
1068b1e0bd39Sthorpej 	mib[1] = PF_ROUTE;
1069b1e0bd39Sthorpej 	mib[2] = 0;		/* protocol */
1070b1e0bd39Sthorpej 	mib[3] = 0;		/* wildcard address family */
1071b1e0bd39Sthorpej 	mib[4] = NET_RT_DUMP;
1072b1e0bd39Sthorpej 	mib[5] = 0;		/* no flags */
10736d8ef4dfSthorpej 	for (;;) {
10746d8ef4dfSthorpej 		if ((needed = sysctl_buf_size) != 0) {
10756d8ef4dfSthorpej 			if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
10766d8ef4dfSthorpej 				break;
10776d8ef4dfSthorpej 			if (errno != ENOMEM && errno != EFAULT)
10786d8ef4dfSthorpej 				BADERR(1,"flush_kern: sysctl(RT_DUMP)");
10796d8ef4dfSthorpej 			free(sysctl_buf);
10806d8ef4dfSthorpej 			needed = 0;
1081b1e0bd39Sthorpej 		}
10826d8ef4dfSthorpej 		if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
10836d8ef4dfSthorpej 			BADERR(1,"flush_kern: sysctl(RT_DUMP) estimate");
10846d8ef4dfSthorpej 		/* Kludge around the habit of some systems, such as
10856d8ef4dfSthorpej 		 * BSD/OS 3.1, to not admit how many routes are in the
10866d8ef4dfSthorpej 		 * kernel, or at least to be quite wrong.
10876d8ef4dfSthorpej 		 */
10886d8ef4dfSthorpej 		needed += 50*(sizeof(*rtm)+5*sizeof(struct sockaddr));
10896d8ef4dfSthorpej 		sysctl_buf = rtmalloc(sysctl_buf_size = needed,
10906d8ef4dfSthorpej 				      "flush_kern sysctl(RT_DUMP)");
10916d8ef4dfSthorpej 	}
10926d8ef4dfSthorpej 
10936d8ef4dfSthorpej 	lim = sysctl_buf + needed;
10946d8ef4dfSthorpej 	for (next = sysctl_buf; next < lim; next += rtm->rtm_msglen) {
1095b1e0bd39Sthorpej 		rtm = (struct rt_msghdr *)next;
10966d8ef4dfSthorpej 		if (rtm->rtm_msglen == 0) {
10976d8ef4dfSthorpej 			msglog("zero length kernel route at "
1098c6509201Sross 			       " %#lx in buffer %#lx before %#lx",
109993daf394Schristos 			       (u_long)rtm, (u_long)sysctl_buf, (u_long)lim);
11006d8ef4dfSthorpej 			break;
11016d8ef4dfSthorpej 		}
1102b1e0bd39Sthorpej 
1103b1e0bd39Sthorpej 		rt_xaddrs(&info,
1104b1e0bd39Sthorpej 			  (struct sockaddr *)(rtm+1),
1105b1e0bd39Sthorpej 			  (struct sockaddr *)(next + rtm->rtm_msglen),
1106b1e0bd39Sthorpej 			  rtm->rtm_addrs);
1107b1e0bd39Sthorpej 
1108b1e0bd39Sthorpej 		if (INFO_DST(&info) == 0
1109b1e0bd39Sthorpej 		    || INFO_DST(&info)->sa_family != AF_INET)
1110b1e0bd39Sthorpej 			continue;
1111b1e0bd39Sthorpej 
1112f93fe60aSchristos 		/* ignore cloned routes
1113f93fe60aSchristos 		 */
1114cad376bdSchristos #if defined(RTF_CLONED) && defined(__bsdi__)
1115f93fe60aSchristos 		if (rtm->rtm_flags & RTF_CLONED)
1116f93fe60aSchristos 			continue;
1117f93fe60aSchristos #endif
1118cad376bdSchristos #if defined(RTF_WASCLONED) && defined(__FreeBSD__)
1119cad376bdSchristos 		if (rtm->rtm_flags & RTF_WASCLONED)
1120cad376bdSchristos 			continue;
1121cad376bdSchristos #endif
1122cad376bdSchristos 
1123f93fe60aSchristos 
1124b1e0bd39Sthorpej 		/* ignore multicast addresses
1125b1e0bd39Sthorpej 		 */
1126b1e0bd39Sthorpej 		if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info)))))
1127b1e0bd39Sthorpej 			continue;
1128b1e0bd39Sthorpej 
11296d8ef4dfSthorpej 		if (!get_info_gate(&INFO_GATE(&info), &gate_sin))
11306d8ef4dfSthorpej 			continue;
11316d8ef4dfSthorpej 
1132b1e0bd39Sthorpej 		/* Note static routes and interface routes, and also
1133b1e0bd39Sthorpej 		 * preload the image of the kernel table so that
1134b1e0bd39Sthorpej 		 * we can later clean it, as well as avoid making
1135b1e0bd39Sthorpej 		 * unneeded changes.  Keep the old kernel routes for a
1136b1e0bd39Sthorpej 		 * few seconds to allow a RIP or router-discovery
1137b1e0bd39Sthorpej 		 * response to be heard.
1138b1e0bd39Sthorpej 		 */
1139b1e0bd39Sthorpej 		rtm_add(rtm,&info,MIN_WAITTIME);
1140b1e0bd39Sthorpej 	}
11416d8ef4dfSthorpej 
11426d8ef4dfSthorpej 	for (i = 0; i < KHASH_SIZE; i++) {
11436d8ef4dfSthorpej 		for (k = khash_bins[i]; k != 0; k = k->k_next) {
11446d8ef4dfSthorpej 			if (k->k_state & KS_CHECK) {
11456d8ef4dfSthorpej 				msglog("%s --> %s disappeared from kernel",
11466d8ef4dfSthorpej 				       addrname(k->k_dst, k->k_mask, 0),
11476d8ef4dfSthorpej 				       naddr_ntoa(k->k_gate));
11486d8ef4dfSthorpej 				del_static(k->k_dst, k->k_mask, k->k_gate, 1);
11496d8ef4dfSthorpej 			}
11506d8ef4dfSthorpej 		}
11516d8ef4dfSthorpej 	}
1152b1e0bd39Sthorpej }
1153b1e0bd39Sthorpej 
1154b1e0bd39Sthorpej 
1155b1e0bd39Sthorpej /* Listen to announcements from the kernel
1156b1e0bd39Sthorpej  */
1157b1e0bd39Sthorpej void
read_rt(void)1158b1e0bd39Sthorpej read_rt(void)
1159b1e0bd39Sthorpej {
1160b1e0bd39Sthorpej 	long cc;
1161b1e0bd39Sthorpej 	struct interface *ifp;
11626d8ef4dfSthorpej 	struct sockaddr_in gate_sin;
11636d8ef4dfSthorpej 	naddr mask, gate;
1164b1e0bd39Sthorpej 	union {
1165b1e0bd39Sthorpej 		struct {
1166b1e0bd39Sthorpej 			struct rt_msghdr rtm;
1167b1e0bd39Sthorpej 			struct sockaddr addrs[RTAX_MAX];
1168b1e0bd39Sthorpej 		} r;
1169b1e0bd39Sthorpej 		struct if_msghdr ifm;
1170b1e0bd39Sthorpej 	} m;
1171b1e0bd39Sthorpej 	char str[100], *strp;
1172b1e0bd39Sthorpej 	struct rt_addrinfo info;
1173b1e0bd39Sthorpej 
1174b1e0bd39Sthorpej 
1175b1e0bd39Sthorpej 	for (;;) {
1176b1e0bd39Sthorpej 		cc = read(rt_sock, &m, sizeof(m));
1177b1e0bd39Sthorpej 		if (cc <= 0) {
1178b1e0bd39Sthorpej 			if (cc < 0 && errno != EWOULDBLOCK)
1179b1e0bd39Sthorpej 				LOGERR("read(rt_sock)");
1180b1e0bd39Sthorpej 			return;
1181b1e0bd39Sthorpej 		}
1182b1e0bd39Sthorpej 
1183b1e0bd39Sthorpej 		if (m.r.rtm.rtm_version != RTM_VERSION) {
1184b1e0bd39Sthorpej 			msglog("bogus routing message version %d",
1185b1e0bd39Sthorpej 			       m.r.rtm.rtm_version);
1186b1e0bd39Sthorpej 			continue;
1187b1e0bd39Sthorpej 		}
1188b1e0bd39Sthorpej 
1189b1e0bd39Sthorpej 		/* Ignore our own results.
1190b1e0bd39Sthorpej 		 */
1191b1e0bd39Sthorpej 		if (m.r.rtm.rtm_type <= RTM_CHANGE
1192b1e0bd39Sthorpej 		    && m.r.rtm.rtm_pid == mypid) {
1193b1e0bd39Sthorpej 			static int complained = 0;
1194b1e0bd39Sthorpej 			if (!complained) {
1195b1e0bd39Sthorpej 				msglog("receiving our own change messages");
1196b1e0bd39Sthorpej 				complained = 1;
1197b1e0bd39Sthorpej 			}
1198b1e0bd39Sthorpej 			continue;
1199b1e0bd39Sthorpej 		}
1200b1e0bd39Sthorpej 
1201b1e0bd39Sthorpej 		if (m.r.rtm.rtm_type == RTM_IFINFO
1202b1e0bd39Sthorpej 		    || m.r.rtm.rtm_type == RTM_NEWADDR
1203b1e0bd39Sthorpej 		    || m.r.rtm.rtm_type == RTM_DELADDR) {
1204e7512e5aSchristos 			ifp = ifwithindex(m.ifm.ifm_index,
1205e7512e5aSchristos 					  m.r.rtm.rtm_type != RTM_DELADDR);
1206b1e0bd39Sthorpej 			if (ifp == 0)
1207b1e0bd39Sthorpej 				trace_act("note %s with flags %#x"
12086d8ef4dfSthorpej 					  " for unknown interface index #%d",
1209b1e0bd39Sthorpej 					  rtm_type_name(m.r.rtm.rtm_type),
1210b1e0bd39Sthorpej 					  m.ifm.ifm_flags,
1211b1e0bd39Sthorpej 					  m.ifm.ifm_index);
1212b1e0bd39Sthorpej 			else
1213e7512e5aSchristos 				trace_act("note %s with flags %#x for %s",
1214b1e0bd39Sthorpej 					  rtm_type_name(m.r.rtm.rtm_type),
1215b1e0bd39Sthorpej 					  m.ifm.ifm_flags,
1216b1e0bd39Sthorpej 					  ifp->int_name);
1217b1e0bd39Sthorpej 
1218b1e0bd39Sthorpej 			/* After being informed of a change to an interface,
1219b1e0bd39Sthorpej 			 * check them all now if the check would otherwise
1220b1e0bd39Sthorpej 			 * be a long time from now, if the interface is
1221b1e0bd39Sthorpej 			 * not known, or if the interface has been turned
1222b1e0bd39Sthorpej 			 * off or on.
1223b1e0bd39Sthorpej 			 */
1224b1e0bd39Sthorpej 			if (ifinit_timer.tv_sec-now.tv_sec>=CHECK_BAD_INTERVAL
1225b1e0bd39Sthorpej 			    || ifp == 0
1226b1e0bd39Sthorpej 			    || ((ifp->int_if_flags ^ m.ifm.ifm_flags)
12276d8ef4dfSthorpej 				& IFF_UP) != 0)
1228b1e0bd39Sthorpej 				ifinit_timer.tv_sec = now.tv_sec;
1229b1e0bd39Sthorpej 			continue;
1230b1e0bd39Sthorpej 		}
1231a1d8cb95Sbouyer #ifdef RTM_OIFINFO
1232f93fe60aSchristos 		if (m.r.rtm.rtm_type == RTM_OIFINFO)
1233a1d8cb95Sbouyer 			continue;	/* ignore compat message */
1234a1d8cb95Sbouyer #endif
1235b1e0bd39Sthorpej 
123616435cdbSitojun 		strlcpy(str, rtm_type_name(m.r.rtm.rtm_type), sizeof(str));
1237b1e0bd39Sthorpej 		strp = &str[strlen(str)];
123816435cdbSitojun 		if (m.r.rtm.rtm_type <= RTM_CHANGE) {
123916435cdbSitojun 			snprintf(strp, str + sizeof(str) - strp,
124016435cdbSitojun 			    " from pid %d",m.r.rtm.rtm_pid);
124116435cdbSitojun 			strp += strlen(strp);
124216435cdbSitojun 		}
1243b1e0bd39Sthorpej 
1244b1e0bd39Sthorpej 		rt_xaddrs(&info, m.r.addrs, &m.r.addrs[RTAX_MAX],
1245b1e0bd39Sthorpej 			  m.r.rtm.rtm_addrs);
1246b1e0bd39Sthorpej 
1247b1e0bd39Sthorpej 		if (INFO_DST(&info) == 0) {
1248e7512e5aSchristos 			trace_act("ignore %s without dst", str);
1249b1e0bd39Sthorpej 			continue;
1250b1e0bd39Sthorpej 		}
1251b1e0bd39Sthorpej 
1252b1e0bd39Sthorpej 		if (INFO_DST(&info)->sa_family != AF_INET) {
1253e7512e5aSchristos 			trace_act("ignore %s for AF %d", str,
1254b1e0bd39Sthorpej 				  INFO_DST(&info)->sa_family);
1255b1e0bd39Sthorpej 			continue;
1256b1e0bd39Sthorpej 		}
1257b1e0bd39Sthorpej 
1258b1e0bd39Sthorpej 		mask = ((INFO_MASK(&info) != 0)
1259b1e0bd39Sthorpej 			? ntohl(S_ADDR(INFO_MASK(&info)))
1260b1e0bd39Sthorpej 			: (m.r.rtm.rtm_flags & RTF_HOST)
1261b1e0bd39Sthorpej 			? HOST_MASK
1262b1e0bd39Sthorpej 			: std_mask(S_ADDR(INFO_DST(&info))));
1263b1e0bd39Sthorpej 
126416435cdbSitojun 		snprintf(strp, str + sizeof(str) - strp, ": %s",
1265b1e0bd39Sthorpej 		    addrname(S_ADDR(INFO_DST(&info)), mask, 0));
126616435cdbSitojun 		strp += strlen(strp);
1267b1e0bd39Sthorpej 
1268b1e0bd39Sthorpej 		if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) {
1269e7512e5aSchristos 			trace_act("ignore multicast %s", str);
1270b1e0bd39Sthorpej 			continue;
1271b1e0bd39Sthorpej 		}
1272b1e0bd39Sthorpej 
1273f93fe60aSchristos #if defined(RTF_CLONED) && defined(__bsdi__)
1274f93fe60aSchristos 		if (m.r.rtm.rtm_flags & RTF_CLONED) {
1275f93fe60aSchristos 			trace_act("ignore cloned %s", str);
1276f93fe60aSchristos 			continue;
1277f93fe60aSchristos 		}
1278f93fe60aSchristos #endif
1279cad376bdSchristos #if defined(RTF_WASCLONED) && defined(__FreeBSD__)
1280cad376bdSchristos 		if (m.r.rtm.rtm_flags & RTF_WASCLONED) {
1281cad376bdSchristos 			trace_act("ignore cloned %s", str);
1282cad376bdSchristos 			continue;
1283cad376bdSchristos 		}
1284cad376bdSchristos #endif
1285f93fe60aSchristos 
12866d8ef4dfSthorpej 		if (get_info_gate(&INFO_GATE(&info), &gate_sin)) {
12876d8ef4dfSthorpej 			gate = S_ADDR(INFO_GATE(&info));
128816435cdbSitojun 			snprintf(strp, str + sizeof(str) - strp,
128916435cdbSitojun 			    " --> %s", naddr_ntoa(gate));
129016435cdbSitojun 			strp += strlen(strp);
12916d8ef4dfSthorpej 		} else {
12926d8ef4dfSthorpej 			gate = 0;
12936d8ef4dfSthorpej 		}
1294b1e0bd39Sthorpej 
1295*1b8d2f5aSmrg 		if (INFO_AUTHOR(&info) != 0) {
129616435cdbSitojun 			snprintf(strp, str + sizeof(str) - strp,
129716435cdbSitojun 			    " by authority of %s",
1298b1e0bd39Sthorpej 			    saddr_ntoa(INFO_AUTHOR(&info)));
129916435cdbSitojun 			strp += strlen(strp);
1300*1b8d2f5aSmrg 		}
1301b1e0bd39Sthorpej 
1302b1e0bd39Sthorpej 		switch (m.r.rtm.rtm_type) {
1303b1e0bd39Sthorpej 		case RTM_ADD:
1304b1e0bd39Sthorpej 		case RTM_CHANGE:
1305b1e0bd39Sthorpej 		case RTM_REDIRECT:
1306b1e0bd39Sthorpej 			if (m.r.rtm.rtm_errno != 0) {
1307e7512e5aSchristos 				trace_act("ignore %s with \"%s\" error",
1308b1e0bd39Sthorpej 					  str, strerror(m.r.rtm.rtm_errno));
1309b1e0bd39Sthorpej 			} else {
1310e7512e5aSchristos 				trace_act("%s", str);
1311b1e0bd39Sthorpej 				rtm_add(&m.r.rtm,&info,0);
1312b1e0bd39Sthorpej 			}
1313b1e0bd39Sthorpej 			break;
1314b1e0bd39Sthorpej 
1315b1e0bd39Sthorpej 		case RTM_DELETE:
13166d8ef4dfSthorpej 			if (m.r.rtm.rtm_errno != 0
13176d8ef4dfSthorpej 			    && m.r.rtm.rtm_errno != ESRCH) {
1318e7512e5aSchristos 				trace_act("ignore %s with \"%s\" error",
1319b1e0bd39Sthorpej 					  str, strerror(m.r.rtm.rtm_errno));
1320b1e0bd39Sthorpej 			} else {
1321e7512e5aSchristos 				trace_act("%s", str);
13226d8ef4dfSthorpej 				del_static(S_ADDR(INFO_DST(&info)), mask,
13236d8ef4dfSthorpej 					   gate, 1);
1324b1e0bd39Sthorpej 			}
1325b1e0bd39Sthorpej 			break;
1326b1e0bd39Sthorpej 
1327b1e0bd39Sthorpej 		case RTM_LOSING:
1328e7512e5aSchristos 			trace_act("%s", str);
1329b1e0bd39Sthorpej 			rtm_lose(&m.r.rtm,&info);
1330b1e0bd39Sthorpej 			break;
1331b1e0bd39Sthorpej 
1332b1e0bd39Sthorpej 		default:
1333e7512e5aSchristos 			trace_act("ignore %s", str);
1334b1e0bd39Sthorpej 			break;
1335b1e0bd39Sthorpej 		}
1336b1e0bd39Sthorpej 	}
1337b1e0bd39Sthorpej }
1338b1e0bd39Sthorpej 
1339b1e0bd39Sthorpej 
1340b1e0bd39Sthorpej /* after aggregating, note routes that belong in the kernel
1341b1e0bd39Sthorpej  */
1342b1e0bd39Sthorpej static void
kern_out(struct ag_info * ag)1343b1e0bd39Sthorpej kern_out(struct ag_info *ag)
1344b1e0bd39Sthorpej {
1345b1e0bd39Sthorpej 	struct khash *k;
1346b1e0bd39Sthorpej 
1347b1e0bd39Sthorpej 
1348b1e0bd39Sthorpej 	/* Do not install bad routes if they are not already present.
1349b1e0bd39Sthorpej 	 * This includes routes that had RS_NET_SYN for interfaces that
1350b1e0bd39Sthorpej 	 * recently died.
1351b1e0bd39Sthorpej 	 */
1352b1e0bd39Sthorpej 	if (ag->ag_metric == HOPCNT_INFINITY) {
1353b1e0bd39Sthorpej 		k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 0);
1354b1e0bd39Sthorpej 		if (k == 0)
1355b1e0bd39Sthorpej 			return;
1356b1e0bd39Sthorpej 	} else {
1357b1e0bd39Sthorpej 		k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask);
1358b1e0bd39Sthorpej 	}
1359b1e0bd39Sthorpej 
1360b1e0bd39Sthorpej 	if (k->k_state & KS_NEW) {
1361b1e0bd39Sthorpej 		/* will need to add new entry to the kernel table */
1362b1e0bd39Sthorpej 		k->k_state = KS_ADD;
1363b1e0bd39Sthorpej 		if (ag->ag_state & AGS_GATEWAY)
1364b1e0bd39Sthorpej 			k->k_state |= KS_GATEWAY;
1365b1e0bd39Sthorpej 		k->k_gate = ag->ag_gate;
1366b1e0bd39Sthorpej 		k->k_metric = ag->ag_metric;
1367b1e0bd39Sthorpej 		return;
1368b1e0bd39Sthorpej 	}
1369b1e0bd39Sthorpej 
1370e3db1a05Sozaki-r 	if ((k->k_state & KS_STATIC) || (k->k_state & KS_LOCAL))
1371b1e0bd39Sthorpej 		return;
1372b1e0bd39Sthorpej 
1373b1e0bd39Sthorpej 	/* modify existing kernel entry if necessary */
1374b1e0bd39Sthorpej 	if (k->k_gate != ag->ag_gate
1375b1e0bd39Sthorpej 	    || k->k_metric != ag->ag_metric) {
13766d8ef4dfSthorpej 		/* Must delete bad interface routes etc. to change them. */
13776d8ef4dfSthorpej 		if (k->k_metric == HOPCNT_INFINITY)
13786d8ef4dfSthorpej 			k->k_state |= KS_DEL_ADD;
1379b1e0bd39Sthorpej 		k->k_gate = ag->ag_gate;
1380b1e0bd39Sthorpej 		k->k_metric = ag->ag_metric;
1381b1e0bd39Sthorpej 		k->k_state |= KS_CHANGE;
1382b1e0bd39Sthorpej 	}
1383b1e0bd39Sthorpej 
13846d8ef4dfSthorpej 	/* If the daemon thinks the route should exist, forget
13856d8ef4dfSthorpej 	 * about any redirections.
13866d8ef4dfSthorpej 	 * If the daemon thinks the route should exist, eventually
13876d8ef4dfSthorpej 	 * override manual intervention by the operator.
13886d8ef4dfSthorpej 	 */
13896d8ef4dfSthorpej 	if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) {
1390b1e0bd39Sthorpej 		k->k_state &= ~KS_DYNAMIC;
1391b1e0bd39Sthorpej 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1392b1e0bd39Sthorpej 	}
1393b1e0bd39Sthorpej 
1394b1e0bd39Sthorpej 	if ((k->k_state & KS_GATEWAY)
1395b1e0bd39Sthorpej 	    && !(ag->ag_state & AGS_GATEWAY)) {
1396b1e0bd39Sthorpej 		k->k_state &= ~KS_GATEWAY;
1397b1e0bd39Sthorpej 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1398b1e0bd39Sthorpej 	} else if (!(k->k_state & KS_GATEWAY)
1399b1e0bd39Sthorpej 		   && (ag->ag_state & AGS_GATEWAY)) {
1400b1e0bd39Sthorpej 		k->k_state |= KS_GATEWAY;
1401b1e0bd39Sthorpej 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1402b1e0bd39Sthorpej 	}
1403b1e0bd39Sthorpej 
14044d3fba59Schristos 	/* Deleting-and-adding is necessary to change aspects of a route.
14054d3fba59Schristos 	 * Just delete instead of deleting and then adding a bad route.
1406b1e0bd39Sthorpej 	 * Otherwise, we want to keep the route in the kernel.
1407b1e0bd39Sthorpej 	 */
1408b1e0bd39Sthorpej 	if (k->k_metric == HOPCNT_INFINITY
1409b1e0bd39Sthorpej 	    && (k->k_state & KS_DEL_ADD))
1410b1e0bd39Sthorpej 		k->k_state |= KS_DELETE;
1411b1e0bd39Sthorpej 	else
1412b1e0bd39Sthorpej 		k->k_state &= ~KS_DELETE;
1413b1e0bd39Sthorpej #undef RT
1414b1e0bd39Sthorpej }
1415b1e0bd39Sthorpej 
1416b1e0bd39Sthorpej 
1417b1e0bd39Sthorpej /* ARGSUSED */
1418b1e0bd39Sthorpej static int
walk_kern(struct radix_node * rn,struct walkarg * argp UNUSED)141994b2d428Schristos walk_kern(struct radix_node *rn,
1420756b1291Schristos 	  struct walkarg *argp UNUSED)
1421b1e0bd39Sthorpej {
1422b1e0bd39Sthorpej #define RT ((struct rt_entry *)rn)
1423b1e0bd39Sthorpej 	char metric, pref;
1424b1e0bd39Sthorpej 	u_int ags = 0;
1425b1e0bd39Sthorpej 
1426b1e0bd39Sthorpej 
1427b1e0bd39Sthorpej 	/* Do not install synthetic routes */
1428b1e0bd39Sthorpej 	if (RT->rt_state & RS_NET_SYN)
1429b1e0bd39Sthorpej 		return 0;
1430b1e0bd39Sthorpej 
1431b1e0bd39Sthorpej 	if (!(RT->rt_state & RS_IF)) {
14326d8ef4dfSthorpej 		/* This is an ordinary route, not for an interface.
14336d8ef4dfSthorpej 		 */
14346d8ef4dfSthorpej 
14356d8ef4dfSthorpej 		/* aggregate, ordinary good routes without regard to
14366d8ef4dfSthorpej 		 * their metric
14376d8ef4dfSthorpej 		 */
14386d8ef4dfSthorpej 		pref = 1;
14396d8ef4dfSthorpej 		ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
14406d8ef4dfSthorpej 
14416d8ef4dfSthorpej 		/* Do not install host routes directly to hosts, to avoid
14426d8ef4dfSthorpej 		 * interfering with ARP entries in the kernel table.
14436d8ef4dfSthorpej 		 */
14446d8ef4dfSthorpej 		if (RT_ISHOST(RT)
14456d8ef4dfSthorpej 		    && ntohl(RT->rt_dst) == RT->rt_gate)
14466d8ef4dfSthorpej 			return 0;
1447b1e0bd39Sthorpej 
1448b1e0bd39Sthorpej 	} else {
14496d8ef4dfSthorpej 		/* This is an interface route.
14506d8ef4dfSthorpej 		 * Do not install routes for "external" remote interfaces.
1451b1e0bd39Sthorpej 		 */
1452b1e0bd39Sthorpej 		if (RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_EXTERNAL))
1453b1e0bd39Sthorpej 			return 0;
1454b1e0bd39Sthorpej 
14556d8ef4dfSthorpej 		/* Interfaces should override received routes.
14566d8ef4dfSthorpej 		 */
14576d8ef4dfSthorpej 		pref = 0;
14586d8ef4dfSthorpej 		ags |= (AGS_IF | AGS_CORS_GATE);
1459b1e0bd39Sthorpej 
1460b1e0bd39Sthorpej 		/* If it is not an interface, or an alias for an interface,
1461b1e0bd39Sthorpej 		 * it must be a "gateway."
1462b1e0bd39Sthorpej 		 *
1463b1e0bd39Sthorpej 		 * If it is a "remote" interface, it is also a "gateway" to
1464b1e0bd39Sthorpej 		 * the kernel if is not a alias.
1465b1e0bd39Sthorpej 		 */
1466b1e0bd39Sthorpej 		if (RT->rt_ifp == 0
1467e7512e5aSchristos 		    || (RT->rt_ifp->int_state & IS_REMOTE))
14686d8ef4dfSthorpej 			ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1469b1e0bd39Sthorpej 	}
1470b1e0bd39Sthorpej 
14716d8ef4dfSthorpej 	/* If RIP is off and IRDP is on, let the route to the discovered
14726d8ef4dfSthorpej 	 * route suppress any RIP routes.  Eventually the RIP routes
14736d8ef4dfSthorpej 	 * will time-out and be deleted.  This reaches the steady-state
14746d8ef4dfSthorpej 	 * quicker.
14756d8ef4dfSthorpej 	 */
14766d8ef4dfSthorpej 	if ((RT->rt_state & RS_RDISC) && rip_sock < 0)
1477b1e0bd39Sthorpej 		ags |= AGS_CORS_GATE;
1478b1e0bd39Sthorpej 
1479b1e0bd39Sthorpej 	metric = RT->rt_metric;
1480b1e0bd39Sthorpej 	if (metric == HOPCNT_INFINITY) {
1481b1e0bd39Sthorpej 		/* if the route is dead, so try hard to aggregate. */
1482b1e0bd39Sthorpej 		pref = HOPCNT_INFINITY;
1483b1e0bd39Sthorpej 		ags |= (AGS_FINE_GATE | AGS_SUPPRESS);
14846d8ef4dfSthorpej 		ags &= ~(AGS_IF | AGS_CORS_GATE);
1485b1e0bd39Sthorpej 	}
1486b1e0bd39Sthorpej 
1487b1e0bd39Sthorpej 	ag_check(RT->rt_dst, RT->rt_mask, RT->rt_gate, 0,
1488b1e0bd39Sthorpej 		 metric,pref, 0, 0, ags, kern_out);
1489b1e0bd39Sthorpej 	return 0;
1490b1e0bd39Sthorpej #undef RT
1491b1e0bd39Sthorpej }
1492b1e0bd39Sthorpej 
1493b1e0bd39Sthorpej 
1494b1e0bd39Sthorpej /* Update the kernel table to match the daemon table.
1495b1e0bd39Sthorpej  */
1496b1e0bd39Sthorpej static void
fix_kern(void)1497b1e0bd39Sthorpej fix_kern(void)
1498b1e0bd39Sthorpej {
1499e7512e5aSchristos 	int i;
1500b1e0bd39Sthorpej 	struct khash *k, **pk;
1501b1e0bd39Sthorpej 
1502b1e0bd39Sthorpej 
1503b1e0bd39Sthorpej 	need_kern = age_timer;
1504b1e0bd39Sthorpej 
1505b1e0bd39Sthorpej 	/* Walk daemon table, updating the copy of the kernel table.
1506b1e0bd39Sthorpej 	 */
1507b1e0bd39Sthorpej 	(void)rn_walktree(rhead, walk_kern, 0);
1508b1e0bd39Sthorpej 	ag_flush(0,0,kern_out);
1509b1e0bd39Sthorpej 
1510b1e0bd39Sthorpej 	for (i = 0; i < KHASH_SIZE; i++) {
1511b1e0bd39Sthorpej 		for (pk = &khash_bins[i]; (k = *pk) != 0; ) {
1512b1e0bd39Sthorpej 			/* Do not touch static routes */
1513b1e0bd39Sthorpej 			if (k->k_state & KS_STATIC) {
1514b1e0bd39Sthorpej 				kern_check_static(k,0);
1515b1e0bd39Sthorpej 				pk = &k->k_next;
1516b1e0bd39Sthorpej 				continue;
1517b1e0bd39Sthorpej 			}
1518b1e0bd39Sthorpej 
1519e3db1a05Sozaki-r 			/* Do not touch local routes */
1520e3db1a05Sozaki-r 			if (k->k_state & KS_LOCAL) {
1521e3db1a05Sozaki-r 				pk = &k->k_next;
1522e3db1a05Sozaki-r 				continue;
1523e3db1a05Sozaki-r 			}
1524e3db1a05Sozaki-r 
1525b1e0bd39Sthorpej 			/* check hold on routes deleted by the operator */
1526b1e0bd39Sthorpej 			if (k->k_keep > now.tv_sec) {
15276d8ef4dfSthorpej 				/* ensure we check when the hold is over */
1528b1e0bd39Sthorpej 				LIM_SEC(need_kern, k->k_keep);
15296d8ef4dfSthorpej 				/* mark for the next cycle */
1530b1e0bd39Sthorpej 				k->k_state |= KS_DELETE;
1531b1e0bd39Sthorpej 				pk = &k->k_next;
1532b1e0bd39Sthorpej 				continue;
1533b1e0bd39Sthorpej 			}
1534b1e0bd39Sthorpej 
1535e7512e5aSchristos 			if ((k->k_state & KS_DELETE)
1536e7512e5aSchristos 			    && !(k->k_state & KS_DYNAMIC)) {
1537e7512e5aSchristos 				kern_ioctl(k, RTM_DELETE, 0);
1538b1e0bd39Sthorpej 				*pk = k->k_next;
1539b1e0bd39Sthorpej 				free(k);
1540b1e0bd39Sthorpej 				continue;
1541b1e0bd39Sthorpej 			}
1542b1e0bd39Sthorpej 
1543e7512e5aSchristos 			if (k->k_state & KS_DEL_ADD)
1544e7512e5aSchristos 				kern_ioctl(k, RTM_DELETE, 0);
15454d3fba59Schristos 
1546b1e0bd39Sthorpej 			if (k->k_state & KS_ADD) {
1547e7512e5aSchristos 				kern_ioctl(k, RTM_ADD,
1548e7512e5aSchristos 					   ((0 != (k->k_state & (KS_GATEWAY
1549e7512e5aSchristos 							| KS_DYNAMIC)))
1550e7512e5aSchristos 					    ? RTF_GATEWAY : 0));
1551b1e0bd39Sthorpej 			} else if (k->k_state & KS_CHANGE) {
1552e7512e5aSchristos 				kern_ioctl(k,  RTM_CHANGE,
1553e7512e5aSchristos 					   ((0 != (k->k_state & (KS_GATEWAY
1554e7512e5aSchristos 							| KS_DYNAMIC)))
1555e7512e5aSchristos 					    ? RTF_GATEWAY : 0));
1556b1e0bd39Sthorpej 			}
1557b1e0bd39Sthorpej 			k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD);
1558b1e0bd39Sthorpej 
1559b1e0bd39Sthorpej 			/* Mark this route to be deleted in the next cycle.
1560b1e0bd39Sthorpej 			 * This deletes routes that disappear from the
1561b1e0bd39Sthorpej 			 * daemon table, since the normal aging code
1562b1e0bd39Sthorpej 			 * will clear the bit for routes that have not
1563b1e0bd39Sthorpej 			 * disappeared from the daemon table.
1564b1e0bd39Sthorpej 			 */
1565b1e0bd39Sthorpej 			k->k_state |= KS_DELETE;
1566b1e0bd39Sthorpej 			pk = &k->k_next;
1567b1e0bd39Sthorpej 		}
1568b1e0bd39Sthorpej 	}
1569b1e0bd39Sthorpej }
1570b1e0bd39Sthorpej 
1571b1e0bd39Sthorpej 
1572b1e0bd39Sthorpej /* Delete a static route in the image of the kernel table.
1573b1e0bd39Sthorpej  */
1574b1e0bd39Sthorpej void
del_static(naddr dst,naddr mask,naddr gate,int gone)1575b1e0bd39Sthorpej del_static(naddr dst,
1576b1e0bd39Sthorpej 	   naddr mask,
15776d8ef4dfSthorpej 	   naddr gate,
1578b1e0bd39Sthorpej 	   int gone)
1579b1e0bd39Sthorpej {
1580b1e0bd39Sthorpej 	struct khash *k;
1581b1e0bd39Sthorpej 	struct rt_entry *rt;
1582b1e0bd39Sthorpej 
1583b1e0bd39Sthorpej 	/* Just mark it in the table to be deleted next time the kernel
1584b1e0bd39Sthorpej 	 * table is updated.
1585b1e0bd39Sthorpej 	 * If it has already been deleted, mark it as such, and set its
1586b1e0bd39Sthorpej 	 * keep-timer so that it will not be deleted again for a while.
1587b1e0bd39Sthorpej 	 * This lets the operator delete a route added by the daemon
1588b1e0bd39Sthorpej 	 * and add a replacement.
1589b1e0bd39Sthorpej 	 */
1590b1e0bd39Sthorpej 	k = kern_find(dst, mask, 0);
15916d8ef4dfSthorpej 	if (k != 0 && (gate == 0 || k->k_gate == gate)) {
15926d8ef4dfSthorpej 		k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK);
1593b1e0bd39Sthorpej 		k->k_state |= KS_DELETE;
1594b1e0bd39Sthorpej 		if (gone) {
1595b1e0bd39Sthorpej 			k->k_state |= KS_DELETED;
1596b1e0bd39Sthorpej 			k->k_keep = now.tv_sec + K_KEEP_LIM;
1597b1e0bd39Sthorpej 		}
1598b1e0bd39Sthorpej 	}
1599b1e0bd39Sthorpej 
1600b1e0bd39Sthorpej 	rt = rtget(dst, mask);
1601b1e0bd39Sthorpej 	if (rt != 0 && (rt->rt_state & RS_STATIC))
1602b1e0bd39Sthorpej 		rtbad(rt);
1603b1e0bd39Sthorpej }
1604b1e0bd39Sthorpej 
1605b1e0bd39Sthorpej 
16064d3fba59Schristos /* Delete all routes generated from ICMP Redirects that use a given gateway,
16074d3fba59Schristos  * as well as old redirected routes.
1608b1e0bd39Sthorpej  */
1609b1e0bd39Sthorpej void
del_redirects(naddr bad_gate,time_t old)1610b1e0bd39Sthorpej del_redirects(naddr bad_gate,
1611b1e0bd39Sthorpej 	      time_t old)
1612b1e0bd39Sthorpej {
1613b1e0bd39Sthorpej 	int i;
1614b1e0bd39Sthorpej 	struct khash *k;
1615b1e0bd39Sthorpej 
1616b1e0bd39Sthorpej 
1617b1e0bd39Sthorpej 	for (i = 0; i < KHASH_SIZE; i++) {
1618b1e0bd39Sthorpej 		for (k = khash_bins[i]; k != 0; k = k->k_next) {
1619b1e0bd39Sthorpej 			if (!(k->k_state & KS_DYNAMIC)
1620b1e0bd39Sthorpej 			    || (k->k_state & KS_STATIC))
1621b1e0bd39Sthorpej 				continue;
1622b1e0bd39Sthorpej 
1623b1e0bd39Sthorpej 			if (k->k_gate != bad_gate
1624b1e0bd39Sthorpej 			    && k->k_redirect_time > old
1625b1e0bd39Sthorpej 			    && !supplier)
1626b1e0bd39Sthorpej 				continue;
1627b1e0bd39Sthorpej 
1628b1e0bd39Sthorpej 			k->k_state |= KS_DELETE;
16294d3fba59Schristos 			k->k_state &= ~KS_DYNAMIC;
1630b1e0bd39Sthorpej 			need_kern.tv_sec = now.tv_sec;
1631e7512e5aSchristos 			trace_act("mark redirected %s --> %s for deletion",
1632b1e0bd39Sthorpej 				  addrname(k->k_dst, k->k_mask, 0),
1633b1e0bd39Sthorpej 				  naddr_ntoa(k->k_gate));
1634b1e0bd39Sthorpej 		}
1635b1e0bd39Sthorpej 	}
1636b1e0bd39Sthorpej }
1637b1e0bd39Sthorpej 
1638b1e0bd39Sthorpej 
1639b1e0bd39Sthorpej /* Start the daemon tables.
1640b1e0bd39Sthorpej  */
1641756b1291Schristos extern int max_keylen;
1642756b1291Schristos 
1643b1e0bd39Sthorpej void
rtinit(void)1644b1e0bd39Sthorpej rtinit(void)
1645b1e0bd39Sthorpej {
1646b1e0bd39Sthorpej 	int i;
1647b1e0bd39Sthorpej 	struct ag_info *ag;
1648b1e0bd39Sthorpej 
1649b1e0bd39Sthorpej 	/* Initialize the radix trees */
1650b1e0bd39Sthorpej 	max_keylen = sizeof(struct sockaddr_in);
1651b1e0bd39Sthorpej 	rn_init();
1652cd81fc31Sthorpej 	rn_inithead((void*)&rhead, 32);
1653b1e0bd39Sthorpej 
1654b1e0bd39Sthorpej 	/* mark all of the slots in the table free */
1655b1e0bd39Sthorpej 	ag_avail = ag_slots;
1656b1e0bd39Sthorpej 	for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) {
1657b1e0bd39Sthorpej 		ag->ag_fine = ag+1;
1658b1e0bd39Sthorpej 		ag++;
1659b1e0bd39Sthorpej 	}
1660b1e0bd39Sthorpej }
1661b1e0bd39Sthorpej 
1662b1e0bd39Sthorpej 
1663b1e0bd39Sthorpej #ifdef _HAVE_SIN_LEN
16644fea751dSchristos static struct sockaddr_in dst_sock = {sizeof(dst_sock), AF_INET, 0, {0}, {0}};
16654fea751dSchristos static struct sockaddr_in mask_sock = {sizeof(mask_sock), AF_INET, 0, {0}, {0}};
1666b1e0bd39Sthorpej #else
1667b1e0bd39Sthorpej static struct sockaddr_in_new dst_sock = {_SIN_ADDR_SIZE, AF_INET};
1668b1e0bd39Sthorpej static struct sockaddr_in_new mask_sock = {_SIN_ADDR_SIZE, AF_INET};
1669b1e0bd39Sthorpej #endif
1670b1e0bd39Sthorpej 
1671b1e0bd39Sthorpej 
167293daf394Schristos static void
set_need_flash(void)1673b1e0bd39Sthorpej set_need_flash(void)
1674b1e0bd39Sthorpej {
1675b1e0bd39Sthorpej 	if (!need_flash) {
1676b1e0bd39Sthorpej 		need_flash = 1;
1677b1e0bd39Sthorpej 		/* Do not send the flash update immediately.  Wait a little
1678b1e0bd39Sthorpej 		 * while to hear from other routers.
1679b1e0bd39Sthorpej 		 */
1680b1e0bd39Sthorpej 		no_flash.tv_sec = now.tv_sec + MIN_WAITTIME;
1681b1e0bd39Sthorpej 	}
1682b1e0bd39Sthorpej }
1683b1e0bd39Sthorpej 
1684b1e0bd39Sthorpej 
1685b1e0bd39Sthorpej /* Get a particular routing table entry
1686b1e0bd39Sthorpej  */
1687b1e0bd39Sthorpej struct rt_entry *
rtget(naddr dst,naddr mask)1688b1e0bd39Sthorpej rtget(naddr dst, naddr mask)
1689b1e0bd39Sthorpej {
1690b1e0bd39Sthorpej 	struct rt_entry *rt;
1691b1e0bd39Sthorpej 
1692b1e0bd39Sthorpej 	dst_sock.sin_addr.s_addr = dst;
169393daf394Schristos 	mask_sock.sin_addr.s_addr = htonl(mask);
1694b1e0bd39Sthorpej 	masktrim(&mask_sock);
1695b1e0bd39Sthorpej 	rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock,&mask_sock,rhead);
1696b1e0bd39Sthorpej 	if (!rt
1697b1e0bd39Sthorpej 	    || rt->rt_dst != dst
1698b1e0bd39Sthorpej 	    || rt->rt_mask != mask)
1699b1e0bd39Sthorpej 		return 0;
1700b1e0bd39Sthorpej 
1701b1e0bd39Sthorpej 	return rt;
1702b1e0bd39Sthorpej }
1703b1e0bd39Sthorpej 
1704b1e0bd39Sthorpej 
1705b1e0bd39Sthorpej /* Find a route to dst as the kernel would.
1706b1e0bd39Sthorpej  */
1707b1e0bd39Sthorpej struct rt_entry *
rtfind(naddr dst)1708b1e0bd39Sthorpej rtfind(naddr dst)
1709b1e0bd39Sthorpej {
1710b1e0bd39Sthorpej 	dst_sock.sin_addr.s_addr = dst;
1711b1e0bd39Sthorpej 	return (struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead);
1712b1e0bd39Sthorpej }
1713b1e0bd39Sthorpej 
1714b1e0bd39Sthorpej 
1715b1e0bd39Sthorpej /* add a route to the table
1716b1e0bd39Sthorpej  */
1717b1e0bd39Sthorpej void
rtadd(naddr dst,naddr mask,u_int state,struct rt_spare * new)1718b1e0bd39Sthorpej rtadd(naddr	dst,
1719b1e0bd39Sthorpej       naddr	mask,
17206d8ef4dfSthorpej       u_int	state,			/* rt_state for the entry */
17216d8ef4dfSthorpej       struct	rt_spare *new)
1722b1e0bd39Sthorpej {
1723b1e0bd39Sthorpej 	struct rt_entry *rt;
1724b1e0bd39Sthorpej 	naddr smask;
1725b1e0bd39Sthorpej 	int i;
1726b1e0bd39Sthorpej 	struct rt_spare *rts;
1727b1e0bd39Sthorpej 
17284d3fba59Schristos 	rt = (struct rt_entry *)rtmalloc(sizeof (*rt), "rtadd");
17293f50343aSlukem 	memset(rt, 0, sizeof(*rt));
1730b1e0bd39Sthorpej 	for (rts = rt->rt_spares, i = NUM_SPARES; i != 0; i--, rts++)
1731b1e0bd39Sthorpej 		rts->rts_metric = HOPCNT_INFINITY;
1732b1e0bd39Sthorpej 
1733b1e0bd39Sthorpej 	rt->rt_nodes->rn_key = (caddr_t)&rt->rt_dst_sock;
1734b1e0bd39Sthorpej 	rt->rt_dst = dst;
1735b1e0bd39Sthorpej 	rt->rt_dst_sock.sin_family = AF_INET;
1736b1e0bd39Sthorpej #ifdef _HAVE_SIN_LEN
1737b1e0bd39Sthorpej 	rt->rt_dst_sock.sin_len = dst_sock.sin_len;
1738b1e0bd39Sthorpej #endif
1739b1e0bd39Sthorpej 	if (mask != HOST_MASK) {
1740b1e0bd39Sthorpej 		smask = std_mask(dst);
1741b1e0bd39Sthorpej 		if ((smask & ~mask) == 0 && mask > smask)
1742b1e0bd39Sthorpej 			state |= RS_SUBNET;
1743b1e0bd39Sthorpej 	}
174493daf394Schristos 	mask_sock.sin_addr.s_addr = htonl(mask);
1745b1e0bd39Sthorpej 	masktrim(&mask_sock);
1746b1e0bd39Sthorpej 	rt->rt_mask = mask;
1747b1e0bd39Sthorpej 	rt->rt_state = state;
17486d8ef4dfSthorpej 	rt->rt_spares[0] = *new;
1749b1e0bd39Sthorpej 	rt->rt_time = now.tv_sec;
1750b1e0bd39Sthorpej 	rt->rt_poison_metric = HOPCNT_INFINITY;
1751b1e0bd39Sthorpej 	rt->rt_seqno = update_seqno;
1752b1e0bd39Sthorpej 
1753cad376bdSchristos 	if (++total_routes == MAX_ROUTES)
1754cad376bdSchristos 		msglog("have maximum (%d) routes", total_routes);
1755b1e0bd39Sthorpej 	if (TRACEACTIONS)
1756b1e0bd39Sthorpej 		trace_add_del("Add", rt);
1757b1e0bd39Sthorpej 
1758b1e0bd39Sthorpej 	need_kern.tv_sec = now.tv_sec;
1759b1e0bd39Sthorpej 	set_need_flash();
1760b1e0bd39Sthorpej 
1761b1e0bd39Sthorpej 	if (0 == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock,
1762b1e0bd39Sthorpej 				    rhead, rt->rt_nodes)) {
1763756b1291Schristos 		msglog("rnh_addaddr() failed for %s mask=%#lx",
1764756b1291Schristos 		       naddr_ntoa(dst), (u_long)mask);
17653e1606a4Schristos 		free(rt);
1766b1e0bd39Sthorpej 	}
1767b1e0bd39Sthorpej }
1768b1e0bd39Sthorpej 
1769b1e0bd39Sthorpej 
1770b1e0bd39Sthorpej /* notice a changed route
1771b1e0bd39Sthorpej  */
1772b1e0bd39Sthorpej void
rtchange(struct rt_entry * rt,u_int state,struct rt_spare * new,char * label)1773b1e0bd39Sthorpej rtchange(struct rt_entry *rt,
1774b1e0bd39Sthorpej 	 u_int	state,			/* new state bits */
17756d8ef4dfSthorpej 	 struct rt_spare *new,
1776b1e0bd39Sthorpej 	 char	*label)
1777b1e0bd39Sthorpej {
17786d8ef4dfSthorpej 	if (rt->rt_metric != new->rts_metric) {
1779b1e0bd39Sthorpej 		/* Fix the kernel immediately if it seems the route
1780b1e0bd39Sthorpej 		 * has gone bad, since there may be a working route that
1781b1e0bd39Sthorpej 		 * aggregates this route.
1782b1e0bd39Sthorpej 		 */
17836d8ef4dfSthorpej 		if (new->rts_metric == HOPCNT_INFINITY) {
1784b1e0bd39Sthorpej 			need_kern.tv_sec = now.tv_sec;
17856d8ef4dfSthorpej 			if (new->rts_time >= now.tv_sec - EXPIRE_TIME)
17866d8ef4dfSthorpej 				new->rts_time = now.tv_sec - EXPIRE_TIME;
17874d3fba59Schristos 		}
1788b1e0bd39Sthorpej 		rt->rt_seqno = update_seqno;
1789b1e0bd39Sthorpej 		set_need_flash();
1790b1e0bd39Sthorpej 	}
1791b1e0bd39Sthorpej 
17926d8ef4dfSthorpej 	if (rt->rt_gate != new->rts_gate) {
1793b1e0bd39Sthorpej 		need_kern.tv_sec = now.tv_sec;
1794b1e0bd39Sthorpej 		rt->rt_seqno = update_seqno;
1795b1e0bd39Sthorpej 		set_need_flash();
1796b1e0bd39Sthorpej 	}
1797b1e0bd39Sthorpej 
1798b1e0bd39Sthorpej 	state |= (rt->rt_state & RS_SUBNET);
1799b1e0bd39Sthorpej 
18004d3fba59Schristos 	/* Keep various things from deciding ageless routes are stale.
18014d3fba59Schristos 	 */
18026d8ef4dfSthorpej 	if (!AGE_RT(state, new->rts_ifp))
18036d8ef4dfSthorpej 		new->rts_time = now.tv_sec;
18044d3fba59Schristos 
1805b1e0bd39Sthorpej 	if (TRACEACTIONS)
18066d8ef4dfSthorpej 		trace_change(rt, state, new,
1807b1e0bd39Sthorpej 			     label ? label : "Chg   ");
1808b1e0bd39Sthorpej 
1809b1e0bd39Sthorpej 	rt->rt_state = state;
18106d8ef4dfSthorpej 	rt->rt_spares[0] = *new;
1811b1e0bd39Sthorpej }
1812b1e0bd39Sthorpej 
1813b1e0bd39Sthorpej 
1814b1e0bd39Sthorpej /* check for a better route among the spares
1815b1e0bd39Sthorpej  */
1816b1e0bd39Sthorpej static struct rt_spare *
rts_better(struct rt_entry * rt)1817b1e0bd39Sthorpej rts_better(struct rt_entry *rt)
1818b1e0bd39Sthorpej {
1819b1e0bd39Sthorpej 	struct rt_spare *rts, *rts1;
1820b1e0bd39Sthorpej 	int i;
1821b1e0bd39Sthorpej 
1822b1e0bd39Sthorpej 	/* find the best alternative among the spares */
1823b1e0bd39Sthorpej 	rts = rt->rt_spares+1;
1824b1e0bd39Sthorpej 	for (i = NUM_SPARES, rts1 = rts+1; i > 2; i--, rts1++) {
1825b1e0bd39Sthorpej 		if (BETTER_LINK(rt,rts1,rts))
1826b1e0bd39Sthorpej 			rts = rts1;
1827b1e0bd39Sthorpej 	}
1828b1e0bd39Sthorpej 
1829b1e0bd39Sthorpej 	return rts;
1830b1e0bd39Sthorpej }
1831b1e0bd39Sthorpej 
1832b1e0bd39Sthorpej 
1833b1e0bd39Sthorpej /* switch to a backup route
1834b1e0bd39Sthorpej  */
1835b1e0bd39Sthorpej void
rtswitch(struct rt_entry * rt,struct rt_spare * rts)1836b1e0bd39Sthorpej rtswitch(struct rt_entry *rt,
1837b1e0bd39Sthorpej 	 struct rt_spare *rts)
1838b1e0bd39Sthorpej {
1839b1e0bd39Sthorpej 	struct rt_spare swap;
184064e132e7Sitojun 	char label[20];
1841b1e0bd39Sthorpej 
1842b1e0bd39Sthorpej 	/* Do not change permanent routes */
18434d3fba59Schristos 	if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | RS_RDISC
18444d3fba59Schristos 				  | RS_NET_SYN | RS_IF)))
1845b1e0bd39Sthorpej 		return;
1846b1e0bd39Sthorpej 
1847b1e0bd39Sthorpej 	/* find the best alternative among the spares */
1848b1e0bd39Sthorpej 	if (rts == 0)
1849b1e0bd39Sthorpej 		rts = rts_better(rt);
1850b1e0bd39Sthorpej 
1851b1e0bd39Sthorpej 	/* Do not bother if it is not worthwhile.
1852b1e0bd39Sthorpej 	 */
1853b1e0bd39Sthorpej 	if (!BETTER_LINK(rt, rts, rt->rt_spares))
1854b1e0bd39Sthorpej 		return;
1855b1e0bd39Sthorpej 
1856b1e0bd39Sthorpej 	swap = rt->rt_spares[0];
18573fbac68bSitojun 	(void)snprintf(label, sizeof(label), "Use #%d",
18583fbac68bSitojun 	    (int)(rts - rt->rt_spares));
18596d8ef4dfSthorpej 	rtchange(rt, rt->rt_state & ~(RS_NET_SYN | RS_RDISC), rts, label);
1860e7512e5aSchristos 	if (swap.rts_metric == HOPCNT_INFINITY) {
1861e7512e5aSchristos 		*rts = rts_empty;
1862e7512e5aSchristos 	} else {
1863b1e0bd39Sthorpej 		*rts = swap;
1864b1e0bd39Sthorpej 	}
1865e7512e5aSchristos }
1866b1e0bd39Sthorpej 
1867b1e0bd39Sthorpej 
1868b1e0bd39Sthorpej void
rtdelete(struct rt_entry * rt)1869b1e0bd39Sthorpej rtdelete(struct rt_entry *rt)
1870b1e0bd39Sthorpej {
1871b1e0bd39Sthorpej 	struct khash *k;
1872b1e0bd39Sthorpej 
1873b1e0bd39Sthorpej 
1874b1e0bd39Sthorpej 	if (TRACEACTIONS)
1875b1e0bd39Sthorpej 		trace_add_del("Del", rt);
1876b1e0bd39Sthorpej 
1877b1e0bd39Sthorpej 	k = kern_find(rt->rt_dst, rt->rt_mask, 0);
1878b1e0bd39Sthorpej 	if (k != 0) {
1879b1e0bd39Sthorpej 		k->k_state |= KS_DELETE;
1880b1e0bd39Sthorpej 		need_kern.tv_sec = now.tv_sec;
1881b1e0bd39Sthorpej 	}
1882b1e0bd39Sthorpej 
1883b1e0bd39Sthorpej 	dst_sock.sin_addr.s_addr = rt->rt_dst;
188493daf394Schristos 	mask_sock.sin_addr.s_addr = htonl(rt->rt_mask);
1885b1e0bd39Sthorpej 	masktrim(&mask_sock);
1886b1e0bd39Sthorpej 	if (rt != (struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock,
1887b1e0bd39Sthorpej 							rhead)) {
1888b1e0bd39Sthorpej 		msglog("rnh_deladdr() failed");
1889b1e0bd39Sthorpej 	} else {
1890b1e0bd39Sthorpej 		free(rt);
18914d3fba59Schristos 		total_routes--;
1892b1e0bd39Sthorpej 	}
1893b1e0bd39Sthorpej }
1894b1e0bd39Sthorpej 
1895b1e0bd39Sthorpej 
1896e7512e5aSchristos void
rts_delete(struct rt_entry * rt,struct rt_spare * rts)1897e7512e5aSchristos rts_delete(struct rt_entry *rt,
1898e7512e5aSchristos 	   struct rt_spare *rts)
1899e7512e5aSchristos {
19006d8ef4dfSthorpej 	trace_upslot(rt, rts, &rts_empty);
1901e7512e5aSchristos 	*rts = rts_empty;
1902e7512e5aSchristos }
1903e7512e5aSchristos 
1904e7512e5aSchristos 
1905b1e0bd39Sthorpej /* Get rid of a bad route, and try to switch to a replacement.
1906b1e0bd39Sthorpej  */
1907b1e0bd39Sthorpej void
rtbad(struct rt_entry * rt)1908b1e0bd39Sthorpej rtbad(struct rt_entry *rt)
1909b1e0bd39Sthorpej {
19106d8ef4dfSthorpej 	struct rt_spare new;
1911b1e0bd39Sthorpej 
19126d8ef4dfSthorpej 	/* Poison the route */
19136d8ef4dfSthorpej 	new = rt->rt_spares[0];
19146d8ef4dfSthorpej 	new.rts_metric = HOPCNT_INFINITY;
19156d8ef4dfSthorpej 	rtchange(rt, rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC), &new, 0);
1916b1e0bd39Sthorpej 	rtswitch(rt, 0);
1917b1e0bd39Sthorpej }
1918b1e0bd39Sthorpej 
1919b1e0bd39Sthorpej 
1920b1e0bd39Sthorpej /* Junk a RS_NET_SYN or RS_LOCAL route,
1921b1e0bd39Sthorpej  *	unless it is needed by another interface.
1922b1e0bd39Sthorpej  */
1923b1e0bd39Sthorpej void
rtbad_sub(struct rt_entry * rt)1924b1e0bd39Sthorpej rtbad_sub(struct rt_entry *rt)
1925b1e0bd39Sthorpej {
1926b1e0bd39Sthorpej 	struct interface *ifp, *ifp1;
1927b1e0bd39Sthorpej 	struct intnet *intnetp;
1928b1e0bd39Sthorpej 	u_int state;
1929b1e0bd39Sthorpej 
1930b1e0bd39Sthorpej 
1931b1e0bd39Sthorpej 	ifp1 = 0;
1932b1e0bd39Sthorpej 	state = 0;
1933b1e0bd39Sthorpej 
1934b1e0bd39Sthorpej 	if (rt->rt_state & RS_LOCAL) {
1935b1e0bd39Sthorpej 		/* Is this the route through loopback for the interface?
1936b1e0bd39Sthorpej 		 * If so, see if it is used by any other interfaces, such
1937b1e0bd39Sthorpej 		 * as a point-to-point interface with the same local address.
1938b1e0bd39Sthorpej 		 */
1939b1e0bd39Sthorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1940b1e0bd39Sthorpej 			/* Retain it if another interface needs it.
1941b1e0bd39Sthorpej 			 */
1942b1e0bd39Sthorpej 			if (ifp->int_addr == rt->rt_ifp->int_addr) {
1943b1e0bd39Sthorpej 				state |= RS_LOCAL;
1944b1e0bd39Sthorpej 				ifp1 = ifp;
1945b1e0bd39Sthorpej 				break;
1946b1e0bd39Sthorpej 			}
1947b1e0bd39Sthorpej 		}
1948b1e0bd39Sthorpej 
1949b1e0bd39Sthorpej 	}
1950b1e0bd39Sthorpej 
1951b1e0bd39Sthorpej 	if (!(state & RS_LOCAL)) {
1952b1e0bd39Sthorpej 		/* Retain RIPv1 logical network route if there is another
1953b1e0bd39Sthorpej 		 * interface that justifies it.
1954b1e0bd39Sthorpej 		 */
1955b1e0bd39Sthorpej 		if (rt->rt_state & RS_NET_SYN) {
1956b1e0bd39Sthorpej 			for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1957b1e0bd39Sthorpej 				if ((ifp->int_state & IS_NEED_NET_SYN)
1958b1e0bd39Sthorpej 				    && rt->rt_mask == ifp->int_std_mask
1959b1e0bd39Sthorpej 				    && rt->rt_dst == ifp->int_std_addr) {
1960b1e0bd39Sthorpej 					state |= RS_NET_SYN;
1961b1e0bd39Sthorpej 					ifp1 = ifp;
1962b1e0bd39Sthorpej 					break;
1963b1e0bd39Sthorpej 				}
1964b1e0bd39Sthorpej 			}
1965b1e0bd39Sthorpej 		}
1966b1e0bd39Sthorpej 
1967b1e0bd39Sthorpej 		/* or if there is an authority route that needs it. */
1968b1e0bd39Sthorpej 		for (intnetp = intnets;
1969b1e0bd39Sthorpej 		     intnetp != 0;
1970b1e0bd39Sthorpej 		     intnetp = intnetp->intnet_next) {
1971b1e0bd39Sthorpej 			if (intnetp->intnet_addr == rt->rt_dst
1972b1e0bd39Sthorpej 			    && intnetp->intnet_mask == rt->rt_mask) {
1973b1e0bd39Sthorpej 				state |= (RS_NET_SYN | RS_NET_INT);
1974b1e0bd39Sthorpej 				break;
1975b1e0bd39Sthorpej 			}
1976b1e0bd39Sthorpej 		}
1977b1e0bd39Sthorpej 	}
1978b1e0bd39Sthorpej 
1979b1e0bd39Sthorpej 	if (ifp1 != 0 || (state & RS_NET_SYN)) {
19806d8ef4dfSthorpej 		struct rt_spare new = rt->rt_spares[0];
19816d8ef4dfSthorpej 		new.rts_ifp = ifp1;
19826d8ef4dfSthorpej 		rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state),
19836d8ef4dfSthorpej 			 &new, 0);
1984b1e0bd39Sthorpej 	} else {
1985b1e0bd39Sthorpej 		rtbad(rt);
1986b1e0bd39Sthorpej 	}
1987b1e0bd39Sthorpej }
1988b1e0bd39Sthorpej 
1989b1e0bd39Sthorpej 
1990b1e0bd39Sthorpej /* Called while walking the table looking for sick interfaces
1991b1e0bd39Sthorpej  * or after a time change.
1992b1e0bd39Sthorpej  */
1993b1e0bd39Sthorpej /* ARGSUSED */
1994b1e0bd39Sthorpej int
walk_bad(struct radix_node * rn,struct walkarg * argp UNUSED)199594b2d428Schristos walk_bad(struct radix_node *rn,
1996756b1291Schristos 	 struct walkarg *argp UNUSED)
1997b1e0bd39Sthorpej {
1998b1e0bd39Sthorpej #define RT ((struct rt_entry *)rn)
1999b1e0bd39Sthorpej 	struct rt_spare *rts;
2000b1e0bd39Sthorpej 	int i;
2001b1e0bd39Sthorpej 
2002b1e0bd39Sthorpej 
2003b1e0bd39Sthorpej 	/* fix any spare routes through the interface
2004b1e0bd39Sthorpej 	 */
2005b1e0bd39Sthorpej 	rts = RT->rt_spares;
2006b1e0bd39Sthorpej 	for (i = NUM_SPARES; i != 1; i--) {
2007b1e0bd39Sthorpej 		rts++;
2008e7512e5aSchristos 		if (rts->rts_metric < HOPCNT_INFINITY
2009e7512e5aSchristos 		    && (rts->rts_ifp == 0
2010e7512e5aSchristos 			|| (rts->rts_ifp->int_state & IS_BROKE)))
2011e7512e5aSchristos 			rts_delete(RT, rts);
2012b1e0bd39Sthorpej 	}
2013b1e0bd39Sthorpej 
2014b1e0bd39Sthorpej 	/* Deal with the main route
2015b1e0bd39Sthorpej 	 */
2016b1e0bd39Sthorpej 	/* finished if it has been handled before or if its interface is ok
2017b1e0bd39Sthorpej 	 */
2018b1e0bd39Sthorpej 	if (RT->rt_ifp == 0 || !(RT->rt_ifp->int_state & IS_BROKE))
2019b1e0bd39Sthorpej 		return 0;
2020b1e0bd39Sthorpej 
2021b1e0bd39Sthorpej 	/* Bad routes for other than interfaces are easy.
2022b1e0bd39Sthorpej 	 */
2023b1e0bd39Sthorpej 	if (0 == (RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) {
2024b1e0bd39Sthorpej 		rtbad(RT);
2025b1e0bd39Sthorpej 		return 0;
2026b1e0bd39Sthorpej 	}
2027b1e0bd39Sthorpej 
2028b1e0bd39Sthorpej 	rtbad_sub(RT);
2029b1e0bd39Sthorpej 	return 0;
2030b1e0bd39Sthorpej #undef RT
2031b1e0bd39Sthorpej }
2032b1e0bd39Sthorpej 
2033b1e0bd39Sthorpej 
2034b1e0bd39Sthorpej /* Check the age of an individual route.
2035b1e0bd39Sthorpej  */
2036b1e0bd39Sthorpej /* ARGSUSED */
2037b1e0bd39Sthorpej static int
walk_age(struct radix_node * rn,struct walkarg * argp UNUSED)203894b2d428Schristos walk_age(struct radix_node *rn,
2039756b1291Schristos 	   struct walkarg *argp UNUSED)
2040b1e0bd39Sthorpej {
2041b1e0bd39Sthorpej #define RT ((struct rt_entry *)rn)
2042b1e0bd39Sthorpej 	struct interface *ifp;
2043b1e0bd39Sthorpej 	struct rt_spare *rts;
2044b1e0bd39Sthorpej 	int i;
2045b1e0bd39Sthorpej 
2046b1e0bd39Sthorpej 
2047b1e0bd39Sthorpej 	/* age all of the spare routes, including the primary route
2048b1e0bd39Sthorpej 	 * currently in use
2049b1e0bd39Sthorpej 	 */
2050b1e0bd39Sthorpej 	rts = RT->rt_spares;
2051b1e0bd39Sthorpej 	for (i = NUM_SPARES; i != 0; i--, rts++) {
2052b1e0bd39Sthorpej 
2053b1e0bd39Sthorpej 		ifp = rts->rts_ifp;
2054b1e0bd39Sthorpej 		if (i == NUM_SPARES) {
20554d3fba59Schristos 			if (!AGE_RT(RT->rt_state, ifp)) {
2056b1e0bd39Sthorpej 				/* Keep various things from deciding ageless
20574d3fba59Schristos 				 * routes are stale
20584d3fba59Schristos 				 */
2059b1e0bd39Sthorpej 				rts->rts_time = now.tv_sec;
2060b1e0bd39Sthorpej 				continue;
2061b1e0bd39Sthorpej 			}
2062b1e0bd39Sthorpej 
2063b1e0bd39Sthorpej 			/* forget RIP routes after RIP has been turned off.
2064b1e0bd39Sthorpej 			 */
2065b1e0bd39Sthorpej 			if (rip_sock < 0) {
2066b1e0bd39Sthorpej 				rtdelete(RT);
2067b1e0bd39Sthorpej 				return 0;
2068b1e0bd39Sthorpej 			}
2069b1e0bd39Sthorpej 		}
2070b1e0bd39Sthorpej 
2071b1e0bd39Sthorpej 		/* age failing routes
2072b1e0bd39Sthorpej 		 */
2073b1e0bd39Sthorpej 		if (age_bad_gate == rts->rts_gate
2074b1e0bd39Sthorpej 		    && rts->rts_time >= now_stale) {
2075b1e0bd39Sthorpej 			rts->rts_time -= SUPPLY_INTERVAL;
2076b1e0bd39Sthorpej 		}
2077b1e0bd39Sthorpej 
2078b1e0bd39Sthorpej 		/* trash the spare routes when they go bad */
2079b1e0bd39Sthorpej 		if (rts->rts_metric < HOPCNT_INFINITY
20806d8ef4dfSthorpej 		    && now_garbage > rts->rts_time
20816d8ef4dfSthorpej 		    && i != NUM_SPARES)
2082e7512e5aSchristos 			rts_delete(RT, rts);
2083b1e0bd39Sthorpej 	}
2084b1e0bd39Sthorpej 
2085b1e0bd39Sthorpej 
2086b1e0bd39Sthorpej 	/* finished if the active route is still fresh */
2087b1e0bd39Sthorpej 	if (now_stale <= RT->rt_time)
2088b1e0bd39Sthorpej 		return 0;
2089b1e0bd39Sthorpej 
2090b1e0bd39Sthorpej 	/* try to switch to an alternative */
2091b1e0bd39Sthorpej 	rtswitch(RT, 0);
2092b1e0bd39Sthorpej 
2093b1e0bd39Sthorpej 	/* Delete a dead route after it has been publically mourned. */
2094b1e0bd39Sthorpej 	if (now_garbage > RT->rt_time) {
2095b1e0bd39Sthorpej 		rtdelete(RT);
2096b1e0bd39Sthorpej 		return 0;
2097b1e0bd39Sthorpej 	}
2098b1e0bd39Sthorpej 
2099b1e0bd39Sthorpej 	/* Start poisoning a bad route before deleting it. */
21006d8ef4dfSthorpej 	if (now.tv_sec - RT->rt_time > EXPIRE_TIME) {
21016d8ef4dfSthorpej 		struct rt_spare new = RT->rt_spares[0];
21026d8ef4dfSthorpej 		new.rts_metric = HOPCNT_INFINITY;
21036d8ef4dfSthorpej 		rtchange(RT, RT->rt_state, &new, 0);
21046d8ef4dfSthorpej 	}
2105b1e0bd39Sthorpej 	return 0;
2106b1e0bd39Sthorpej }
2107b1e0bd39Sthorpej 
2108b1e0bd39Sthorpej 
2109b1e0bd39Sthorpej /* Watch for dead routes and interfaces.
2110b1e0bd39Sthorpej  */
2111b1e0bd39Sthorpej void
age(naddr bad_gate)2112b1e0bd39Sthorpej age(naddr bad_gate)
2113b1e0bd39Sthorpej {
2114b1e0bd39Sthorpej 	struct interface *ifp;
2115e7512e5aSchristos 	int need_query = 0;
2116b1e0bd39Sthorpej 
2117e7512e5aSchristos 	/* If not listening to RIP, there is no need to age the routes in
2118e7512e5aSchristos 	 * the table.
2119e7512e5aSchristos 	 */
2120e7512e5aSchristos 	age_timer.tv_sec = (now.tv_sec
2121e7512e5aSchristos 			    + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL));
2122b1e0bd39Sthorpej 
2123b1e0bd39Sthorpej 	/* Check for dead IS_REMOTE interfaces by timing their
2124b1e0bd39Sthorpej 	 * transmissions.
2125b1e0bd39Sthorpej 	 */
2126e7512e5aSchristos 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
2127e7512e5aSchristos 		if (!(ifp->int_state & IS_REMOTE))
2128e7512e5aSchristos 			continue;
2129b1e0bd39Sthorpej 
2130e7512e5aSchristos 		/* ignore unreachable remote interfaces */
2131e7512e5aSchristos 		if (!check_remote(ifp))
2132e7512e5aSchristos 			continue;
21336d8ef4dfSthorpej 
2134e7512e5aSchristos 		/* Restore remote interface that has become reachable
2135e7512e5aSchristos 		 */
2136e7512e5aSchristos 		if (ifp->int_state & IS_BROKE)
2137e7512e5aSchristos 			if_ok(ifp, "remote ");
2138e7512e5aSchristos 
2139e7512e5aSchristos 		if (ifp->int_act_time != NEVER
2140e7512e5aSchristos 		    && now.tv_sec - ifp->int_act_time > EXPIRE_TIME) {
2141e7512e5aSchristos 			msglog("remote interface %s to %s timed out after"
2142b935e079Schristos 			       " %lld:%lld",
2143b1e0bd39Sthorpej 			       ifp->int_name,
2144e7512e5aSchristos 			       naddr_ntoa(ifp->int_dstaddr),
2145b935e079Schristos 			       (long long)(now.tv_sec - ifp->int_act_time)/60,
2146b935e079Schristos 			       (long long)(now.tv_sec - ifp->int_act_time)%60);
2147e7512e5aSchristos 			if_sick(ifp);
2148b1e0bd39Sthorpej 		}
2149e7512e5aSchristos 
2150e7512e5aSchristos 		/* If we have not heard from the other router
2151e7512e5aSchristos 		 * recently, ask it.
2152e7512e5aSchristos 		 */
2153e7512e5aSchristos 		if (now.tv_sec >= ifp->int_query_time) {
2154e7512e5aSchristos 			ifp->int_query_time = NEVER;
2155e7512e5aSchristos 			need_query = 1;
2156b1e0bd39Sthorpej 		}
2157b1e0bd39Sthorpej 	}
2158b1e0bd39Sthorpej 
2159b1e0bd39Sthorpej 	/* Age routes. */
2160b1e0bd39Sthorpej 	age_bad_gate = bad_gate;
2161b1e0bd39Sthorpej 	(void)rn_walktree(rhead, walk_age, 0);
2162b1e0bd39Sthorpej 
21636d8ef4dfSthorpej 	/* delete old redirected routes to keep the kernel table small
21646d8ef4dfSthorpej 	 * and prevent blackholes
21656d8ef4dfSthorpej 	 */
21666d8ef4dfSthorpej 	del_redirects(bad_gate, now.tv_sec-STALE_TIME);
21676d8ef4dfSthorpej 
2168b1e0bd39Sthorpej 	/* Update the kernel routing table. */
2169b1e0bd39Sthorpej 	fix_kern();
2170e7512e5aSchristos 
2171e7512e5aSchristos 	/* poke reticent remote gateways */
2172e7512e5aSchristos 	if (need_query)
2173e7512e5aSchristos 		rip_query();
2174b1e0bd39Sthorpej }
2175