table.c revision 1.15 1 1.15 lukem /* $NetBSD: table.c,v 1.15 2001/11/02 05:30:57 lukem Exp $ */
2 1.2 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1983, 1988, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.8 christos * must display the following acknowledgment:
17 1.1 thorpej * This product includes software developed by the University of
18 1.1 thorpej * California, Berkeley and its contributors.
19 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
20 1.1 thorpej * may be used to endorse or promote products derived from this software
21 1.1 thorpej * without specific prior written permission.
22 1.1 thorpej *
23 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 thorpej * SUCH DAMAGE.
34 1.1 thorpej */
35 1.1 thorpej
36 1.14 christos #include "defs.h"
37 1.14 christos
38 1.14 christos #ifdef __NetBSD__
39 1.15 lukem __RCSID("$NetBSD: table.c,v 1.15 2001/11/02 05:30:57 lukem Exp $");
40 1.14 christos #elif defined(__FreeBSD__)
41 1.14 christos __RCSID("$FreeBSD$");
42 1.14 christos #else
43 1.14 christos __RCSID("Revision: 2.23 ");
44 1.14 christos #ident "Revision: 2.23 "
45 1.2 thorpej #endif
46 1.1 thorpej
47 1.1 thorpej static struct rt_spare *rts_better(struct rt_entry *);
48 1.7 thorpej static struct rt_spare rts_empty = {0,0,0,HOPCNT_INFINITY,0,0,0};
49 1.14 christos static void set_need_flash(void);
50 1.5 lukem #ifdef _HAVE_SIN_LEN
51 1.14 christos static void masktrim(struct sockaddr_in *ap);
52 1.5 lukem #else
53 1.14 christos static void masktrim(struct sockaddr_in_new *ap);
54 1.5 lukem #endif
55 1.5 lukem
56 1.13 christos
57 1.1 thorpej struct radix_node_head *rhead; /* root of the radix tree */
58 1.1 thorpej
59 1.1 thorpej int need_flash = 1; /* flash update needed
60 1.1 thorpej * start =1 to suppress the 1st
61 1.1 thorpej */
62 1.1 thorpej
63 1.1 thorpej struct timeval age_timer; /* next check of old routes */
64 1.1 thorpej struct timeval need_kern = { /* need to update kernel table */
65 1.14 christos EPOCH+MIN_WAITTIME-1, 0
66 1.1 thorpej };
67 1.1 thorpej
68 1.1 thorpej int stopint;
69 1.1 thorpej
70 1.3 christos int total_routes;
71 1.3 christos
72 1.4 christos /* zap any old routes through this gateway */
73 1.1 thorpej naddr age_bad_gate;
74 1.1 thorpej
75 1.1 thorpej
76 1.1 thorpej /* It is desirable to "aggregate" routes, to combine differing routes of
77 1.1 thorpej * the same metric and next hop into a common route with a smaller netmask
78 1.1 thorpej * or to suppress redundant routes, routes that add no information to
79 1.1 thorpej * routes with smaller netmasks.
80 1.1 thorpej *
81 1.1 thorpej * A route is redundant if and only if any and all routes with smaller
82 1.1 thorpej * but matching netmasks and nets are the same. Since routes are
83 1.1 thorpej * kept sorted in the radix tree, redundant routes always come second.
84 1.1 thorpej *
85 1.1 thorpej * There are two kinds of aggregations. First, two routes of the same bit
86 1.1 thorpej * mask and differing only in the least significant bit of the network
87 1.1 thorpej * number can be combined into a single route with a coarser mask.
88 1.1 thorpej *
89 1.1 thorpej * Second, a route can be suppressed in favor of another route with a more
90 1.1 thorpej * coarse mask provided no incompatible routes with intermediate masks
91 1.1 thorpej * are present. The second kind of aggregation involves suppressing routes.
92 1.1 thorpej * A route must not be suppressed if an incompatible route exists with
93 1.1 thorpej * an intermediate mask, since the suppressed route would be covered
94 1.1 thorpej * by the intermediate.
95 1.1 thorpej *
96 1.1 thorpej * This code relies on the radix tree walk encountering routes
97 1.1 thorpej * sorted first by address, with the smallest address first.
98 1.1 thorpej */
99 1.1 thorpej
100 1.1 thorpej struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest, *ag_finest;
101 1.1 thorpej
102 1.1 thorpej /* #define DEBUG_AG */
103 1.1 thorpej #ifdef DEBUG_AG
104 1.1 thorpej #define CHECK_AG() {int acnt = 0; struct ag_info *cag; \
105 1.1 thorpej for (cag = ag_avail; cag != 0; cag = cag->ag_fine) \
106 1.1 thorpej acnt++; \
107 1.1 thorpej for (cag = ag_corsest; cag != 0; cag = cag->ag_fine) \
108 1.1 thorpej acnt++; \
109 1.1 thorpej if (acnt != NUM_AG_SLOTS) { \
110 1.1 thorpej (void)fflush(stderr); \
111 1.1 thorpej abort(); \
112 1.1 thorpej } \
113 1.1 thorpej }
114 1.1 thorpej #else
115 1.1 thorpej #define CHECK_AG()
116 1.1 thorpej #endif
117 1.1 thorpej
118 1.1 thorpej
119 1.1 thorpej /* Output the contents of an aggregation table slot.
120 1.1 thorpej * This function must always be immediately followed with the deletion
121 1.1 thorpej * of the target slot.
122 1.1 thorpej */
123 1.1 thorpej static void
124 1.1 thorpej ag_out(struct ag_info *ag,
125 1.1 thorpej void (*out)(struct ag_info *))
126 1.1 thorpej {
127 1.1 thorpej struct ag_info *ag_cors;
128 1.1 thorpej naddr bit;
129 1.1 thorpej
130 1.1 thorpej
131 1.7 thorpej /* Forget it if this route should not be output for split-horizon. */
132 1.7 thorpej if (ag->ag_state & AGS_SPLIT_HZ)
133 1.7 thorpej return;
134 1.7 thorpej
135 1.1 thorpej /* If we output both the even and odd twins, then the immediate parent,
136 1.1 thorpej * if it is present, is redundant, unless the parent manages to
137 1.1 thorpej * aggregate into something coarser.
138 1.1 thorpej * On successive calls, this code detects the even and odd twins,
139 1.1 thorpej * and marks the parent.
140 1.1 thorpej *
141 1.1 thorpej * Note that the order in which the radix tree code emits routes
142 1.1 thorpej * ensures that the twins are seen before the parent is emitted.
143 1.1 thorpej */
144 1.1 thorpej ag_cors = ag->ag_cors;
145 1.1 thorpej if (ag_cors != 0
146 1.1 thorpej && ag_cors->ag_mask == ag->ag_mask<<1
147 1.1 thorpej && ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) {
148 1.1 thorpej ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h)
149 1.1 thorpej ? AGS_REDUN0
150 1.1 thorpej : AGS_REDUN1);
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej /* Skip it if this route is itself redundant.
154 1.1 thorpej *
155 1.1 thorpej * It is ok to change the contents of the slot here, since it is
156 1.1 thorpej * always deleted next.
157 1.1 thorpej */
158 1.1 thorpej if (ag->ag_state & AGS_REDUN0) {
159 1.1 thorpej if (ag->ag_state & AGS_REDUN1)
160 1.7 thorpej return; /* quit if fully redundant */
161 1.7 thorpej /* make it finer if it is half-redundant */
162 1.1 thorpej bit = (-ag->ag_mask) >> 1;
163 1.1 thorpej ag->ag_dst_h |= bit;
164 1.1 thorpej ag->ag_mask |= bit;
165 1.1 thorpej
166 1.1 thorpej } else if (ag->ag_state & AGS_REDUN1) {
167 1.7 thorpej /* make it finer if it is half-redundant */
168 1.1 thorpej bit = (-ag->ag_mask) >> 1;
169 1.1 thorpej ag->ag_mask |= bit;
170 1.1 thorpej }
171 1.1 thorpej out(ag);
172 1.1 thorpej }
173 1.1 thorpej
174 1.1 thorpej
175 1.1 thorpej static void
176 1.1 thorpej ag_del(struct ag_info *ag)
177 1.1 thorpej {
178 1.1 thorpej CHECK_AG();
179 1.1 thorpej
180 1.1 thorpej if (ag->ag_cors == 0)
181 1.1 thorpej ag_corsest = ag->ag_fine;
182 1.1 thorpej else
183 1.1 thorpej ag->ag_cors->ag_fine = ag->ag_fine;
184 1.1 thorpej
185 1.1 thorpej if (ag->ag_fine == 0)
186 1.1 thorpej ag_finest = ag->ag_cors;
187 1.1 thorpej else
188 1.1 thorpej ag->ag_fine->ag_cors = ag->ag_cors;
189 1.1 thorpej
190 1.1 thorpej ag->ag_fine = ag_avail;
191 1.1 thorpej ag_avail = ag;
192 1.1 thorpej
193 1.1 thorpej CHECK_AG();
194 1.1 thorpej }
195 1.1 thorpej
196 1.1 thorpej
197 1.8 christos /* Flush routes waiting for aggregation.
198 1.1 thorpej * This must not suppress a route unless it is known that among all
199 1.1 thorpej * routes with coarser masks that match it, the one with the longest
200 1.1 thorpej * mask is appropriate. This is ensured by scanning the routes
201 1.8 christos * in lexical order, and with the most restrictive mask first
202 1.1 thorpej * among routes to the same destination.
203 1.1 thorpej */
204 1.1 thorpej void
205 1.1 thorpej ag_flush(naddr lim_dst_h, /* flush routes to here */
206 1.1 thorpej naddr lim_mask, /* matching this mask */
207 1.1 thorpej void (*out)(struct ag_info *))
208 1.1 thorpej {
209 1.1 thorpej struct ag_info *ag, *ag_cors;
210 1.1 thorpej naddr dst_h;
211 1.1 thorpej
212 1.1 thorpej
213 1.1 thorpej for (ag = ag_finest;
214 1.1 thorpej ag != 0 && ag->ag_mask >= lim_mask;
215 1.1 thorpej ag = ag_cors) {
216 1.1 thorpej ag_cors = ag->ag_cors;
217 1.1 thorpej
218 1.1 thorpej /* work on only the specified routes */
219 1.1 thorpej dst_h = ag->ag_dst_h;
220 1.1 thorpej if ((dst_h & lim_mask) != lim_dst_h)
221 1.1 thorpej continue;
222 1.1 thorpej
223 1.1 thorpej if (!(ag->ag_state & AGS_SUPPRESS))
224 1.1 thorpej ag_out(ag, out);
225 1.1 thorpej
226 1.1 thorpej else for ( ; ; ag_cors = ag_cors->ag_cors) {
227 1.1 thorpej /* Look for a route that can suppress the
228 1.1 thorpej * current route */
229 1.1 thorpej if (ag_cors == 0) {
230 1.1 thorpej /* failed, so output it and look for
231 1.1 thorpej * another route to work on
232 1.1 thorpej */
233 1.1 thorpej ag_out(ag, out);
234 1.1 thorpej break;
235 1.1 thorpej }
236 1.1 thorpej
237 1.1 thorpej if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) {
238 1.1 thorpej /* We found a route with a coarser mask that
239 1.1 thorpej * aggregates the current target.
240 1.1 thorpej *
241 1.1 thorpej * If it has a different next hop, it
242 1.1 thorpej * cannot replace the target, so output
243 1.1 thorpej * the target.
244 1.1 thorpej */
245 1.1 thorpej if (ag->ag_gate != ag_cors->ag_gate
246 1.1 thorpej && !(ag->ag_state & AGS_FINE_GATE)
247 1.1 thorpej && !(ag_cors->ag_state & AGS_CORS_GATE)) {
248 1.1 thorpej ag_out(ag, out);
249 1.1 thorpej break;
250 1.1 thorpej }
251 1.1 thorpej
252 1.1 thorpej /* If the coarse route has a good enough
253 1.1 thorpej * metric, it suppresses the target.
254 1.7 thorpej * If the suppressed target was redundant,
255 1.7 thorpej * then mark the suppressor redundant.
256 1.1 thorpej */
257 1.1 thorpej if (ag_cors->ag_pref <= ag->ag_pref) {
258 1.1 thorpej if (ag_cors->ag_seqno > ag->ag_seqno)
259 1.1 thorpej ag_cors->ag_seqno = ag->ag_seqno;
260 1.1 thorpej if (AG_IS_REDUN(ag->ag_state)
261 1.1 thorpej && ag_cors->ag_mask==ag->ag_mask<<1) {
262 1.1 thorpej if (ag_cors->ag_dst_h == dst_h)
263 1.1 thorpej ag_cors->ag_state |= AGS_REDUN0;
264 1.1 thorpej else
265 1.1 thorpej ag_cors->ag_state |= AGS_REDUN1;
266 1.1 thorpej }
267 1.1 thorpej if (ag->ag_tag != ag_cors->ag_tag)
268 1.1 thorpej ag_cors->ag_tag = 0;
269 1.1 thorpej if (ag->ag_nhop != ag_cors->ag_nhop)
270 1.1 thorpej ag_cors->ag_nhop = 0;
271 1.1 thorpej break;
272 1.1 thorpej }
273 1.1 thorpej }
274 1.1 thorpej }
275 1.1 thorpej
276 1.1 thorpej /* That route has either been output or suppressed */
277 1.1 thorpej ag_cors = ag->ag_cors;
278 1.1 thorpej ag_del(ag);
279 1.1 thorpej }
280 1.1 thorpej
281 1.1 thorpej CHECK_AG();
282 1.1 thorpej }
283 1.1 thorpej
284 1.1 thorpej
285 1.1 thorpej /* Try to aggregate a route with previous routes.
286 1.1 thorpej */
287 1.1 thorpej void
288 1.1 thorpej ag_check(naddr dst,
289 1.1 thorpej naddr mask,
290 1.1 thorpej naddr gate,
291 1.1 thorpej naddr nhop,
292 1.1 thorpej char metric,
293 1.1 thorpej char pref,
294 1.1 thorpej u_int seqno,
295 1.1 thorpej u_short tag,
296 1.1 thorpej u_short state,
297 1.1 thorpej void (*out)(struct ag_info *)) /* output using this */
298 1.1 thorpej {
299 1.1 thorpej struct ag_info *ag, *nag, *ag_cors;
300 1.1 thorpej naddr xaddr;
301 1.1 thorpej int x;
302 1.1 thorpej
303 1.1 thorpej NTOHL(dst);
304 1.1 thorpej
305 1.1 thorpej /* Punt non-contiguous subnet masks.
306 1.1 thorpej *
307 1.1 thorpej * (X & -X) contains a single bit if and only if X is a power of 2.
308 1.1 thorpej * (X + (X & -X)) == 0 if and only if X is a power of 2.
309 1.1 thorpej */
310 1.1 thorpej if ((mask & -mask) + mask != 0) {
311 1.1 thorpej struct ag_info nc_ag;
312 1.1 thorpej
313 1.1 thorpej nc_ag.ag_dst_h = dst;
314 1.1 thorpej nc_ag.ag_mask = mask;
315 1.1 thorpej nc_ag.ag_gate = gate;
316 1.1 thorpej nc_ag.ag_nhop = nhop;
317 1.1 thorpej nc_ag.ag_metric = metric;
318 1.1 thorpej nc_ag.ag_pref = pref;
319 1.1 thorpej nc_ag.ag_tag = tag;
320 1.1 thorpej nc_ag.ag_state = state;
321 1.1 thorpej nc_ag.ag_seqno = seqno;
322 1.1 thorpej out(&nc_ag);
323 1.1 thorpej return;
324 1.1 thorpej }
325 1.1 thorpej
326 1.1 thorpej /* Search for the right slot in the aggregation table.
327 1.1 thorpej */
328 1.1 thorpej ag_cors = 0;
329 1.1 thorpej ag = ag_corsest;
330 1.1 thorpej while (ag != 0) {
331 1.1 thorpej if (ag->ag_mask >= mask)
332 1.1 thorpej break;
333 1.1 thorpej
334 1.1 thorpej /* Suppress old routes (i.e. combine with compatible routes
335 1.1 thorpej * with coarser masks) as we look for the right slot in the
336 1.1 thorpej * aggregation table for the new route.
337 1.1 thorpej * A route to an address less than the current destination
338 1.1 thorpej * will not be affected by the current route or any route
339 1.1 thorpej * seen hereafter. That means it is safe to suppress it.
340 1.8 christos * This check keeps poor routes (e.g. with large hop counts)
341 1.8 christos * from preventing suppression of finer routes.
342 1.1 thorpej */
343 1.1 thorpej if (ag_cors != 0
344 1.1 thorpej && ag->ag_dst_h < dst
345 1.1 thorpej && (ag->ag_state & AGS_SUPPRESS)
346 1.1 thorpej && ag_cors->ag_pref <= ag->ag_pref
347 1.1 thorpej && (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h
348 1.1 thorpej && (ag_cors->ag_gate == ag->ag_gate
349 1.1 thorpej || (ag->ag_state & AGS_FINE_GATE)
350 1.1 thorpej || (ag_cors->ag_state & AGS_CORS_GATE))) {
351 1.1 thorpej if (ag_cors->ag_seqno > ag->ag_seqno)
352 1.1 thorpej ag_cors->ag_seqno = ag->ag_seqno;
353 1.7 thorpej /* If the suppressed target was redundant,
354 1.7 thorpej * then mark the suppressor redundant.
355 1.7 thorpej */
356 1.1 thorpej if (AG_IS_REDUN(ag->ag_state)
357 1.13 christos && ag_cors->ag_mask == ag->ag_mask<<1) {
358 1.1 thorpej if (ag_cors->ag_dst_h == dst)
359 1.1 thorpej ag_cors->ag_state |= AGS_REDUN0;
360 1.1 thorpej else
361 1.1 thorpej ag_cors->ag_state |= AGS_REDUN1;
362 1.1 thorpej }
363 1.1 thorpej if (ag->ag_tag != ag_cors->ag_tag)
364 1.1 thorpej ag_cors->ag_tag = 0;
365 1.1 thorpej if (ag->ag_nhop != ag_cors->ag_nhop)
366 1.1 thorpej ag_cors->ag_nhop = 0;
367 1.1 thorpej ag_del(ag);
368 1.1 thorpej CHECK_AG();
369 1.1 thorpej } else {
370 1.1 thorpej ag_cors = ag;
371 1.1 thorpej }
372 1.1 thorpej ag = ag_cors->ag_fine;
373 1.1 thorpej }
374 1.1 thorpej
375 1.1 thorpej /* If we find the even/odd twin of the new route, and if the
376 1.1 thorpej * masks and so forth are equal, we can aggregate them.
377 1.1 thorpej * We can probably promote one of the pair.
378 1.1 thorpej *
379 1.1 thorpej * Since the routes are encountered in lexical order,
380 1.1 thorpej * the new route must be odd. However, the second or later
381 1.1 thorpej * times around this loop, it could be the even twin promoted
382 1.1 thorpej * from the even/odd pair of twins of the finer route.
383 1.1 thorpej */
384 1.1 thorpej while (ag != 0
385 1.1 thorpej && ag->ag_mask == mask
386 1.1 thorpej && ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) {
387 1.1 thorpej
388 1.1 thorpej /* Here we know the target route and the route in the current
389 1.1 thorpej * slot have the same netmasks and differ by at most the
390 1.1 thorpej * last bit. They are either for the same destination, or
391 1.1 thorpej * for an even/odd pair of destinations.
392 1.1 thorpej */
393 1.1 thorpej if (ag->ag_dst_h == dst) {
394 1.1 thorpej /* We have two routes to the same destination.
395 1.1 thorpej * Routes are encountered in lexical order, so a
396 1.1 thorpej * route is never promoted until the parent route is
397 1.1 thorpej * already present. So we know that the new route is
398 1.7 thorpej * a promoted (or aggregated) pair and the route
399 1.7 thorpej * already in the slot is the explicit route.
400 1.1 thorpej *
401 1.1 thorpej * Prefer the best route if their metrics differ,
402 1.7 thorpej * or the aggregated one if not, following a sort
403 1.1 thorpej * of longest-match rule.
404 1.1 thorpej */
405 1.1 thorpej if (pref <= ag->ag_pref) {
406 1.1 thorpej ag->ag_gate = gate;
407 1.1 thorpej ag->ag_nhop = nhop;
408 1.1 thorpej ag->ag_tag = tag;
409 1.1 thorpej ag->ag_metric = metric;
410 1.1 thorpej ag->ag_pref = pref;
411 1.1 thorpej x = ag->ag_state;
412 1.1 thorpej ag->ag_state = state;
413 1.1 thorpej state = x;
414 1.1 thorpej }
415 1.1 thorpej
416 1.1 thorpej /* The sequence number controls flash updating,
417 1.1 thorpej * and should be the smaller of the two.
418 1.1 thorpej */
419 1.1 thorpej if (ag->ag_seqno > seqno)
420 1.1 thorpej ag->ag_seqno = seqno;
421 1.1 thorpej
422 1.7 thorpej /* Some bits are set if they are set on either route,
423 1.7 thorpej * except when the route is for an interface.
424 1.7 thorpej */
425 1.7 thorpej if (!(ag->ag_state & AGS_IF))
426 1.7 thorpej ag->ag_state |= (state & (AGS_AGGREGATE_EITHER
427 1.7 thorpej | AGS_REDUN0
428 1.7 thorpej | AGS_REDUN1));
429 1.1 thorpej return;
430 1.1 thorpej }
431 1.1 thorpej
432 1.1 thorpej /* If one of the routes can be promoted and the other can
433 1.1 thorpej * be suppressed, it may be possible to combine them or
434 1.1 thorpej * worthwhile to promote one.
435 1.1 thorpej *
436 1.7 thorpej * Any route that can be promoted is always
437 1.1 thorpej * marked to be eligible to be suppressed.
438 1.1 thorpej */
439 1.7 thorpej if (!((state & AGS_AGGREGATE)
440 1.1 thorpej && (ag->ag_state & AGS_SUPPRESS))
441 1.7 thorpej && !((ag->ag_state & AGS_AGGREGATE)
442 1.1 thorpej && (state & AGS_SUPPRESS)))
443 1.1 thorpej break;
444 1.1 thorpej
445 1.1 thorpej /* A pair of even/odd twin routes can be combined
446 1.1 thorpej * if either is redundant, or if they are via the
447 1.1 thorpej * same gateway and have the same metric.
448 1.1 thorpej */
449 1.1 thorpej if (AG_IS_REDUN(ag->ag_state)
450 1.1 thorpej || AG_IS_REDUN(state)
451 1.1 thorpej || (ag->ag_gate == gate
452 1.1 thorpej && ag->ag_pref == pref
453 1.7 thorpej && (state & ag->ag_state & AGS_AGGREGATE) != 0)) {
454 1.1 thorpej
455 1.1 thorpej /* We have both the even and odd pairs.
456 1.1 thorpej * Since the routes are encountered in order,
457 1.1 thorpej * the route in the slot must be the even twin.
458 1.1 thorpej *
459 1.7 thorpej * Combine and promote (aggregate) the pair of routes.
460 1.1 thorpej */
461 1.1 thorpej if (seqno > ag->ag_seqno)
462 1.1 thorpej seqno = ag->ag_seqno;
463 1.1 thorpej if (!AG_IS_REDUN(state))
464 1.1 thorpej state &= ~AGS_REDUN1;
465 1.1 thorpej if (AG_IS_REDUN(ag->ag_state))
466 1.1 thorpej state |= AGS_REDUN0;
467 1.1 thorpej else
468 1.1 thorpej state &= ~AGS_REDUN0;
469 1.7 thorpej state |= (ag->ag_state & AGS_AGGREGATE_EITHER);
470 1.1 thorpej if (ag->ag_tag != tag)
471 1.1 thorpej tag = 0;
472 1.1 thorpej if (ag->ag_nhop != nhop)
473 1.1 thorpej nhop = 0;
474 1.1 thorpej
475 1.1 thorpej /* Get rid of the even twin that was already
476 1.1 thorpej * in the slot.
477 1.1 thorpej */
478 1.1 thorpej ag_del(ag);
479 1.1 thorpej
480 1.1 thorpej } else if (ag->ag_pref >= pref
481 1.7 thorpej && (ag->ag_state & AGS_AGGREGATE)) {
482 1.1 thorpej /* If we cannot combine the pair, maybe the route
483 1.1 thorpej * with the worse metric can be promoted.
484 1.1 thorpej *
485 1.1 thorpej * Promote the old, even twin, by giving its slot
486 1.1 thorpej * in the table to the new, odd twin.
487 1.1 thorpej */
488 1.1 thorpej ag->ag_dst_h = dst;
489 1.1 thorpej
490 1.1 thorpej xaddr = ag->ag_gate;
491 1.1 thorpej ag->ag_gate = gate;
492 1.1 thorpej gate = xaddr;
493 1.1 thorpej
494 1.1 thorpej xaddr = ag->ag_nhop;
495 1.1 thorpej ag->ag_nhop = nhop;
496 1.1 thorpej nhop = xaddr;
497 1.1 thorpej
498 1.1 thorpej x = ag->ag_tag;
499 1.1 thorpej ag->ag_tag = tag;
500 1.1 thorpej tag = x;
501 1.1 thorpej
502 1.7 thorpej /* The promoted route is even-redundant only if the
503 1.7 thorpej * even twin was fully redundant. It is not
504 1.7 thorpej * odd-redundant because the odd-twin will still be
505 1.7 thorpej * in the table.
506 1.7 thorpej */
507 1.1 thorpej x = ag->ag_state;
508 1.7 thorpej if (!AG_IS_REDUN(x))
509 1.7 thorpej x &= ~AGS_REDUN0;
510 1.7 thorpej x &= ~AGS_REDUN1;
511 1.1 thorpej ag->ag_state = state;
512 1.1 thorpej state = x;
513 1.1 thorpej
514 1.1 thorpej x = ag->ag_metric;
515 1.1 thorpej ag->ag_metric = metric;
516 1.1 thorpej metric = x;
517 1.1 thorpej
518 1.1 thorpej x = ag->ag_pref;
519 1.1 thorpej ag->ag_pref = pref;
520 1.1 thorpej pref = x;
521 1.1 thorpej
522 1.7 thorpej /* take the newest sequence number */
523 1.1 thorpej if (seqno >= ag->ag_seqno)
524 1.1 thorpej seqno = ag->ag_seqno;
525 1.1 thorpej else
526 1.1 thorpej ag->ag_seqno = seqno;
527 1.1 thorpej
528 1.1 thorpej } else {
529 1.7 thorpej if (!(state & AGS_AGGREGATE))
530 1.1 thorpej break; /* cannot promote either twin */
531 1.1 thorpej
532 1.7 thorpej /* Promote the new, odd twin by shaving its
533 1.1 thorpej * mask and address.
534 1.7 thorpej * The promoted route is odd-redundant only if the
535 1.7 thorpej * odd twin was fully redundant. It is not
536 1.7 thorpej * even-redundant because the even twin is still in
537 1.7 thorpej * the table.
538 1.1 thorpej */
539 1.7 thorpej if (!AG_IS_REDUN(state))
540 1.7 thorpej state &= ~AGS_REDUN1;
541 1.7 thorpej state &= ~AGS_REDUN0;
542 1.1 thorpej if (seqno > ag->ag_seqno)
543 1.1 thorpej seqno = ag->ag_seqno;
544 1.1 thorpej else
545 1.1 thorpej ag->ag_seqno = seqno;
546 1.1 thorpej }
547 1.1 thorpej
548 1.1 thorpej mask <<= 1;
549 1.1 thorpej dst &= mask;
550 1.1 thorpej
551 1.1 thorpej if (ag_cors == 0) {
552 1.1 thorpej ag = ag_corsest;
553 1.1 thorpej break;
554 1.1 thorpej }
555 1.1 thorpej ag = ag_cors;
556 1.1 thorpej ag_cors = ag->ag_cors;
557 1.1 thorpej }
558 1.1 thorpej
559 1.1 thorpej /* When we can no longer promote and combine routes,
560 1.1 thorpej * flush the old route in the target slot. Also flush
561 1.1 thorpej * any finer routes that we know will never be aggregated by
562 1.1 thorpej * the new route.
563 1.1 thorpej *
564 1.1 thorpej * In case we moved toward coarser masks,
565 1.1 thorpej * get back where we belong
566 1.1 thorpej */
567 1.1 thorpej if (ag != 0
568 1.1 thorpej && ag->ag_mask < mask) {
569 1.1 thorpej ag_cors = ag;
570 1.1 thorpej ag = ag->ag_fine;
571 1.1 thorpej }
572 1.1 thorpej
573 1.1 thorpej /* Empty the target slot
574 1.1 thorpej */
575 1.1 thorpej if (ag != 0 && ag->ag_mask == mask) {
576 1.1 thorpej ag_flush(ag->ag_dst_h, ag->ag_mask, out);
577 1.1 thorpej ag = (ag_cors == 0) ? ag_corsest : ag_cors->ag_fine;
578 1.1 thorpej }
579 1.1 thorpej
580 1.1 thorpej #ifdef DEBUG_AG
581 1.1 thorpej (void)fflush(stderr);
582 1.1 thorpej if (ag == 0 && ag_cors != ag_finest)
583 1.1 thorpej abort();
584 1.1 thorpej if (ag_cors == 0 && ag != ag_corsest)
585 1.1 thorpej abort();
586 1.1 thorpej if (ag != 0 && ag->ag_cors != ag_cors)
587 1.1 thorpej abort();
588 1.1 thorpej if (ag_cors != 0 && ag_cors->ag_fine != ag)
589 1.1 thorpej abort();
590 1.1 thorpej CHECK_AG();
591 1.1 thorpej #endif
592 1.1 thorpej
593 1.1 thorpej /* Save the new route on the end of the table.
594 1.1 thorpej */
595 1.1 thorpej nag = ag_avail;
596 1.1 thorpej ag_avail = nag->ag_fine;
597 1.1 thorpej
598 1.1 thorpej nag->ag_dst_h = dst;
599 1.1 thorpej nag->ag_mask = mask;
600 1.1 thorpej nag->ag_gate = gate;
601 1.1 thorpej nag->ag_nhop = nhop;
602 1.1 thorpej nag->ag_metric = metric;
603 1.1 thorpej nag->ag_pref = pref;
604 1.1 thorpej nag->ag_tag = tag;
605 1.1 thorpej nag->ag_state = state;
606 1.1 thorpej nag->ag_seqno = seqno;
607 1.1 thorpej
608 1.1 thorpej nag->ag_fine = ag;
609 1.1 thorpej if (ag != 0)
610 1.1 thorpej ag->ag_cors = nag;
611 1.1 thorpej else
612 1.1 thorpej ag_finest = nag;
613 1.1 thorpej nag->ag_cors = ag_cors;
614 1.1 thorpej if (ag_cors == 0)
615 1.1 thorpej ag_corsest = nag;
616 1.1 thorpej else
617 1.1 thorpej ag_cors->ag_fine = nag;
618 1.1 thorpej CHECK_AG();
619 1.1 thorpej }
620 1.1 thorpej
621 1.1 thorpej
622 1.9 christos static const char *
623 1.1 thorpej rtm_type_name(u_char type)
624 1.1 thorpej {
625 1.9 christos static const char *rtm_types[] = {
626 1.1 thorpej "RTM_ADD",
627 1.1 thorpej "RTM_DELETE",
628 1.1 thorpej "RTM_CHANGE",
629 1.1 thorpej "RTM_GET",
630 1.1 thorpej "RTM_LOSING",
631 1.1 thorpej "RTM_REDIRECT",
632 1.1 thorpej "RTM_MISS",
633 1.1 thorpej "RTM_LOCK",
634 1.1 thorpej "RTM_OLDADD",
635 1.1 thorpej "RTM_OLDDEL",
636 1.1 thorpej "RTM_RESOLVE",
637 1.1 thorpej "RTM_NEWADDR",
638 1.1 thorpej "RTM_DELADDR",
639 1.11 bouyer #ifdef RTM_OIFINFO
640 1.11 bouyer "RTM_OIFINFO",
641 1.11 bouyer #endif
642 1.14 christos "RTM_IFINFO",
643 1.14 christos "RTM_NEWMADDR",
644 1.14 christos "RTM_DELMADDR"
645 1.1 thorpej };
646 1.14 christos #define NEW_RTM_PAT "RTM type %#x"
647 1.14 christos static char name0[sizeof(NEW_RTM_PAT)+2];
648 1.1 thorpej
649 1.1 thorpej
650 1.1 thorpej if (type > sizeof(rtm_types)/sizeof(rtm_types[0])
651 1.1 thorpej || type == 0) {
652 1.1 thorpej sprintf(name0, "RTM type %#x", type);
653 1.1 thorpej return name0;
654 1.1 thorpej } else {
655 1.1 thorpej return rtm_types[type-1];
656 1.1 thorpej }
657 1.14 christos #undef NEW_RTM_PAT
658 1.1 thorpej }
659 1.1 thorpej
660 1.1 thorpej
661 1.1 thorpej /* Trim a mask in a sockaddr
662 1.1 thorpej * Produce a length of 0 for an address of 0.
663 1.1 thorpej * Otherwise produce the index of the first zero byte.
664 1.1 thorpej */
665 1.1 thorpej void
666 1.1 thorpej #ifdef _HAVE_SIN_LEN
667 1.1 thorpej masktrim(struct sockaddr_in *ap)
668 1.1 thorpej #else
669 1.1 thorpej masktrim(struct sockaddr_in_new *ap)
670 1.1 thorpej #endif
671 1.1 thorpej {
672 1.5 lukem char *cp;
673 1.1 thorpej
674 1.1 thorpej if (ap->sin_addr.s_addr == 0) {
675 1.1 thorpej ap->sin_len = 0;
676 1.1 thorpej return;
677 1.1 thorpej }
678 1.1 thorpej cp = (char *)(&ap->sin_addr.s_addr+1);
679 1.1 thorpej while (*--cp == 0)
680 1.1 thorpej continue;
681 1.1 thorpej ap->sin_len = cp - (char*)ap + 1;
682 1.1 thorpej }
683 1.1 thorpej
684 1.1 thorpej
685 1.1 thorpej /* Tell the kernel to add, delete or change a route
686 1.1 thorpej */
687 1.1 thorpej static void
688 1.1 thorpej rtioctl(int action, /* RTM_DELETE, etc */
689 1.1 thorpej naddr dst,
690 1.1 thorpej naddr gate,
691 1.1 thorpej naddr mask,
692 1.1 thorpej int metric,
693 1.1 thorpej int flags)
694 1.1 thorpej {
695 1.1 thorpej struct {
696 1.1 thorpej struct rt_msghdr w_rtm;
697 1.1 thorpej struct sockaddr_in w_dst;
698 1.1 thorpej struct sockaddr_in w_gate;
699 1.1 thorpej #ifdef _HAVE_SA_LEN
700 1.1 thorpej struct sockaddr_in w_mask;
701 1.1 thorpej #else
702 1.1 thorpej struct sockaddr_in_new w_mask;
703 1.1 thorpej #endif
704 1.1 thorpej } w;
705 1.1 thorpej long cc;
706 1.4 christos # define PAT " %-10s %s metric=%d flags=%#x"
707 1.4 christos # define ARGS rtm_type_name(action), rtname(dst,mask,gate), metric, flags
708 1.1 thorpej
709 1.1 thorpej again:
710 1.5 lukem memset(&w, 0, sizeof(w));
711 1.1 thorpej w.w_rtm.rtm_msglen = sizeof(w);
712 1.1 thorpej w.w_rtm.rtm_version = RTM_VERSION;
713 1.1 thorpej w.w_rtm.rtm_type = action;
714 1.1 thorpej w.w_rtm.rtm_flags = flags;
715 1.1 thorpej w.w_rtm.rtm_seq = ++rt_sock_seqno;
716 1.1 thorpej w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
717 1.7 thorpej if (metric != 0 || action == RTM_CHANGE) {
718 1.1 thorpej w.w_rtm.rtm_rmx.rmx_hopcount = metric;
719 1.1 thorpej w.w_rtm.rtm_inits |= RTV_HOPCOUNT;
720 1.1 thorpej }
721 1.1 thorpej w.w_dst.sin_family = AF_INET;
722 1.1 thorpej w.w_dst.sin_addr.s_addr = dst;
723 1.1 thorpej w.w_gate.sin_family = AF_INET;
724 1.1 thorpej w.w_gate.sin_addr.s_addr = gate;
725 1.1 thorpej #ifdef _HAVE_SA_LEN
726 1.1 thorpej w.w_dst.sin_len = sizeof(w.w_dst);
727 1.1 thorpej w.w_gate.sin_len = sizeof(w.w_gate);
728 1.1 thorpej #endif
729 1.1 thorpej if (mask == HOST_MASK) {
730 1.1 thorpej w.w_rtm.rtm_flags |= RTF_HOST;
731 1.1 thorpej w.w_rtm.rtm_msglen -= sizeof(w.w_mask);
732 1.1 thorpej } else {
733 1.1 thorpej w.w_rtm.rtm_addrs |= RTA_NETMASK;
734 1.1 thorpej w.w_mask.sin_addr.s_addr = htonl(mask);
735 1.1 thorpej #ifdef _HAVE_SA_LEN
736 1.1 thorpej masktrim(&w.w_mask);
737 1.1 thorpej if (w.w_mask.sin_len == 0)
738 1.1 thorpej w.w_mask.sin_len = sizeof(long);
739 1.1 thorpej w.w_rtm.rtm_msglen -= (sizeof(w.w_mask) - w.w_mask.sin_len);
740 1.1 thorpej #endif
741 1.1 thorpej }
742 1.3 christos
743 1.1 thorpej #ifndef NO_INSTALL
744 1.1 thorpej cc = write(rt_sock, &w, w.w_rtm.rtm_msglen);
745 1.1 thorpej if (cc < 0) {
746 1.1 thorpej if (errno == ESRCH
747 1.1 thorpej && (action == RTM_CHANGE || action == RTM_DELETE)) {
748 1.4 christos trace_act("route disappeared before" PAT, ARGS);
749 1.1 thorpej if (action == RTM_CHANGE) {
750 1.1 thorpej action = RTM_ADD;
751 1.1 thorpej goto again;
752 1.1 thorpej }
753 1.1 thorpej return;
754 1.1 thorpej }
755 1.7 thorpej msglog("write(rt_sock)" PAT ": %s", ARGS, strerror(errno));
756 1.4 christos return;
757 1.4 christos } else if (cc != w.w_rtm.rtm_msglen) {
758 1.9 christos msglog("write(rt_sock) wrote %ld instead of %d for" PAT,
759 1.4 christos cc, w.w_rtm.rtm_msglen, ARGS);
760 1.4 christos return;
761 1.1 thorpej }
762 1.1 thorpej #endif
763 1.4 christos if (TRACEKERNEL)
764 1.7 thorpej trace_misc("write kernel" PAT, ARGS);
765 1.4 christos #undef PAT
766 1.4 christos #undef ARGS
767 1.1 thorpej }
768 1.1 thorpej
769 1.1 thorpej
770 1.1 thorpej #define KHASH_SIZE 71 /* should be prime */
771 1.1 thorpej #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
772 1.1 thorpej static struct khash {
773 1.1 thorpej struct khash *k_next;
774 1.1 thorpej naddr k_dst;
775 1.1 thorpej naddr k_mask;
776 1.1 thorpej naddr k_gate;
777 1.1 thorpej short k_metric;
778 1.1 thorpej u_short k_state;
779 1.1 thorpej #define KS_NEW 0x001
780 1.7 thorpej #define KS_DELETE 0x002 /* need to delete the route */
781 1.1 thorpej #define KS_ADD 0x004 /* add to the kernel */
782 1.1 thorpej #define KS_CHANGE 0x008 /* tell kernel to change the route */
783 1.1 thorpej #define KS_DEL_ADD 0x010 /* delete & add to change the kernel */
784 1.1 thorpej #define KS_STATIC 0x020 /* Static flag in kernel */
785 1.1 thorpej #define KS_GATEWAY 0x040 /* G flag in kernel */
786 1.1 thorpej #define KS_DYNAMIC 0x080 /* result of redirect */
787 1.7 thorpej #define KS_DELETED 0x100 /* already deleted from kernel */
788 1.7 thorpej #define KS_CHECK 0x200
789 1.1 thorpej time_t k_keep;
790 1.1 thorpej #define K_KEEP_LIM 30
791 1.4 christos time_t k_redirect_time; /* when redirected route 1st seen */
792 1.1 thorpej } *khash_bins[KHASH_SIZE];
793 1.1 thorpej
794 1.1 thorpej
795 1.1 thorpej static struct khash*
796 1.1 thorpej kern_find(naddr dst, naddr mask, struct khash ***ppk)
797 1.1 thorpej {
798 1.1 thorpej struct khash *k, **pk;
799 1.1 thorpej
800 1.1 thorpej for (pk = &KHASH(dst,mask); (k = *pk) != 0; pk = &k->k_next) {
801 1.1 thorpej if (k->k_dst == dst && k->k_mask == mask)
802 1.1 thorpej break;
803 1.1 thorpej }
804 1.1 thorpej if (ppk != 0)
805 1.1 thorpej *ppk = pk;
806 1.1 thorpej return k;
807 1.1 thorpej }
808 1.1 thorpej
809 1.1 thorpej
810 1.1 thorpej static struct khash*
811 1.1 thorpej kern_add(naddr dst, naddr mask)
812 1.1 thorpej {
813 1.1 thorpej struct khash *k, **pk;
814 1.1 thorpej
815 1.1 thorpej k = kern_find(dst, mask, &pk);
816 1.1 thorpej if (k != 0)
817 1.1 thorpej return k;
818 1.1 thorpej
819 1.7 thorpej k = (struct khash *)rtmalloc(sizeof(*k), "kern_add");
820 1.1 thorpej
821 1.5 lukem memset(k, 0, sizeof(*k));
822 1.1 thorpej k->k_dst = dst;
823 1.1 thorpej k->k_mask = mask;
824 1.1 thorpej k->k_state = KS_NEW;
825 1.1 thorpej k->k_keep = now.tv_sec;
826 1.1 thorpej *pk = k;
827 1.1 thorpej
828 1.1 thorpej return k;
829 1.1 thorpej }
830 1.1 thorpej
831 1.1 thorpej
832 1.3 christos /* If a kernel route has a non-zero metric, check that it is still in the
833 1.3 christos * daemon table, and not deleted by interfaces coming and going.
834 1.1 thorpej */
835 1.1 thorpej static void
836 1.1 thorpej kern_check_static(struct khash *k,
837 1.1 thorpej struct interface *ifp)
838 1.1 thorpej {
839 1.1 thorpej struct rt_entry *rt;
840 1.7 thorpej struct rt_spare new;
841 1.1 thorpej
842 1.1 thorpej if (k->k_metric == 0)
843 1.1 thorpej return;
844 1.1 thorpej
845 1.8 christos memset(&new, 0, sizeof(new));
846 1.7 thorpej new.rts_ifp = ifp;
847 1.7 thorpej new.rts_gate = k->k_gate;
848 1.7 thorpej new.rts_router = (ifp != 0) ? ifp->int_addr : loopaddr;
849 1.7 thorpej new.rts_metric = k->k_metric;
850 1.7 thorpej new.rts_time = now.tv_sec;
851 1.1 thorpej
852 1.1 thorpej rt = rtget(k->k_dst, k->k_mask);
853 1.1 thorpej if (rt != 0) {
854 1.1 thorpej if (!(rt->rt_state & RS_STATIC))
855 1.7 thorpej rtchange(rt, rt->rt_state | RS_STATIC, &new, 0);
856 1.1 thorpej } else {
857 1.7 thorpej rtadd(k->k_dst, k->k_mask, RS_STATIC, &new);
858 1.1 thorpej }
859 1.1 thorpej }
860 1.1 thorpej
861 1.1 thorpej
862 1.4 christos /* operate on a kernel entry
863 1.4 christos */
864 1.4 christos static void
865 1.4 christos kern_ioctl(struct khash *k,
866 1.4 christos int action, /* RTM_DELETE, etc */
867 1.4 christos int flags)
868 1.4 christos
869 1.4 christos {
870 1.4 christos switch (action) {
871 1.4 christos case RTM_DELETE:
872 1.4 christos k->k_state &= ~KS_DYNAMIC;
873 1.4 christos if (k->k_state & KS_DELETED)
874 1.4 christos return;
875 1.4 christos k->k_state |= KS_DELETED;
876 1.4 christos break;
877 1.4 christos case RTM_ADD:
878 1.4 christos k->k_state &= ~KS_DELETED;
879 1.4 christos break;
880 1.4 christos case RTM_CHANGE:
881 1.4 christos if (k->k_state & KS_DELETED) {
882 1.4 christos action = RTM_ADD;
883 1.4 christos k->k_state &= ~KS_DELETED;
884 1.4 christos }
885 1.4 christos break;
886 1.4 christos }
887 1.4 christos
888 1.4 christos rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_metric, flags);
889 1.4 christos }
890 1.4 christos
891 1.4 christos
892 1.1 thorpej /* add a route the kernel told us
893 1.1 thorpej */
894 1.1 thorpej static void
895 1.1 thorpej rtm_add(struct rt_msghdr *rtm,
896 1.1 thorpej struct rt_addrinfo *info,
897 1.1 thorpej time_t keep)
898 1.1 thorpej {
899 1.1 thorpej struct khash *k;
900 1.1 thorpej struct interface *ifp;
901 1.1 thorpej naddr mask;
902 1.1 thorpej
903 1.1 thorpej
904 1.1 thorpej if (rtm->rtm_flags & RTF_HOST) {
905 1.1 thorpej mask = HOST_MASK;
906 1.1 thorpej } else if (INFO_MASK(info) != 0) {
907 1.1 thorpej mask = ntohl(S_ADDR(INFO_MASK(info)));
908 1.1 thorpej } else {
909 1.4 christos msglog("ignore %s without mask", rtm_type_name(rtm->rtm_type));
910 1.1 thorpej return;
911 1.1 thorpej }
912 1.1 thorpej
913 1.1 thorpej k = kern_add(S_ADDR(INFO_DST(info)), mask);
914 1.1 thorpej if (k->k_state & KS_NEW)
915 1.1 thorpej k->k_keep = now.tv_sec+keep;
916 1.7 thorpej if (INFO_GATE(info) == 0) {
917 1.7 thorpej trace_act("note %s without gateway",
918 1.7 thorpej rtm_type_name(rtm->rtm_type));
919 1.7 thorpej k->k_metric = HOPCNT_INFINITY;
920 1.7 thorpej } else if (INFO_GATE(info)->sa_family != AF_INET) {
921 1.7 thorpej trace_act("note %s with gateway AF=%d",
922 1.7 thorpej rtm_type_name(rtm->rtm_type),
923 1.7 thorpej INFO_GATE(info)->sa_family);
924 1.7 thorpej k->k_metric = HOPCNT_INFINITY;
925 1.7 thorpej } else {
926 1.7 thorpej k->k_gate = S_ADDR(INFO_GATE(info));
927 1.7 thorpej k->k_metric = rtm->rtm_rmx.rmx_hopcount;
928 1.7 thorpej if (k->k_metric < 0)
929 1.7 thorpej k->k_metric = 0;
930 1.7 thorpej else if (k->k_metric > HOPCNT_INFINITY-1)
931 1.7 thorpej k->k_metric = HOPCNT_INFINITY-1;
932 1.7 thorpej }
933 1.7 thorpej k->k_state &= ~(KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD
934 1.7 thorpej | KS_DELETED | KS_GATEWAY | KS_STATIC
935 1.7 thorpej | KS_NEW | KS_CHECK);
936 1.1 thorpej if (rtm->rtm_flags & RTF_GATEWAY)
937 1.1 thorpej k->k_state |= KS_GATEWAY;
938 1.1 thorpej if (rtm->rtm_flags & RTF_STATIC)
939 1.1 thorpej k->k_state |= KS_STATIC;
940 1.3 christos
941 1.3 christos if (0 != (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED))) {
942 1.4 christos if (INFO_AUTHOR(info) != 0
943 1.4 christos && INFO_AUTHOR(info)->sa_family == AF_INET)
944 1.4 christos ifp = iflookup(S_ADDR(INFO_AUTHOR(info)));
945 1.4 christos else
946 1.4 christos ifp = 0;
947 1.4 christos if (supplier
948 1.4 christos && (ifp == 0 || !(ifp->int_state & IS_REDIRECT_OK))) {
949 1.3 christos /* Routers are not supposed to listen to redirects,
950 1.4 christos * so delete it if it came via an unknown interface
951 1.4 christos * or the interface does not have special permission.
952 1.3 christos */
953 1.3 christos k->k_state &= ~KS_DYNAMIC;
954 1.3 christos k->k_state |= KS_DELETE;
955 1.3 christos LIM_SEC(need_kern, 0);
956 1.4 christos trace_act("mark for deletion redirected %s --> %s"
957 1.4 christos " via %s",
958 1.1 thorpej addrname(k->k_dst, k->k_mask, 0),
959 1.4 christos naddr_ntoa(k->k_gate),
960 1.4 christos ifp ? ifp->int_name : "unknown interface");
961 1.3 christos } else {
962 1.3 christos k->k_state |= KS_DYNAMIC;
963 1.3 christos k->k_redirect_time = now.tv_sec;
964 1.4 christos trace_act("accept redirected %s --> %s via %s",
965 1.4 christos addrname(k->k_dst, k->k_mask, 0),
966 1.4 christos naddr_ntoa(k->k_gate),
967 1.4 christos ifp ? ifp->int_name : "unknown interface");
968 1.1 thorpej }
969 1.3 christos return;
970 1.1 thorpej }
971 1.1 thorpej
972 1.3 christos /* If it is not a static route, quit until the next comparison
973 1.3 christos * between the kernel and daemon tables, when it will be deleted.
974 1.1 thorpej */
975 1.1 thorpej if (!(k->k_state & KS_STATIC)) {
976 1.1 thorpej k->k_state |= KS_DELETE;
977 1.1 thorpej LIM_SEC(need_kern, k->k_keep);
978 1.1 thorpej return;
979 1.1 thorpej }
980 1.1 thorpej
981 1.1 thorpej /* Put static routes with real metrics into the daemon table so
982 1.1 thorpej * they can be advertised.
983 1.1 thorpej *
984 1.4 christos * Find the interface toward the gateway.
985 1.1 thorpej */
986 1.1 thorpej ifp = iflookup(k->k_gate);
987 1.4 christos if (ifp == 0)
988 1.4 christos msglog("static route %s --> %s impossibly lacks ifp",
989 1.4 christos addrname(S_ADDR(INFO_DST(info)), mask, 0),
990 1.4 christos naddr_ntoa(k->k_gate));
991 1.1 thorpej
992 1.1 thorpej kern_check_static(k, ifp);
993 1.1 thorpej }
994 1.1 thorpej
995 1.1 thorpej
996 1.1 thorpej /* deal with packet loss
997 1.1 thorpej */
998 1.1 thorpej static void
999 1.1 thorpej rtm_lose(struct rt_msghdr *rtm,
1000 1.1 thorpej struct rt_addrinfo *info)
1001 1.1 thorpej {
1002 1.1 thorpej if (INFO_GATE(info) == 0
1003 1.1 thorpej || INFO_GATE(info)->sa_family != AF_INET) {
1004 1.4 christos trace_act("ignore %s without gateway",
1005 1.4 christos rtm_type_name(rtm->rtm_type));
1006 1.1 thorpej return;
1007 1.1 thorpej }
1008 1.1 thorpej
1009 1.7 thorpej if (rdisc_ok)
1010 1.1 thorpej rdisc_age(S_ADDR(INFO_GATE(info)));
1011 1.7 thorpej age(S_ADDR(INFO_GATE(info)));
1012 1.7 thorpej }
1013 1.7 thorpej
1014 1.7 thorpej
1015 1.7 thorpej /* Make the gateway slot of an info structure point to something
1016 1.7 thorpej * useful. If it is not already useful, but it specifies an interface,
1017 1.7 thorpej * then fill in the sockaddr_in provided and point it there.
1018 1.7 thorpej */
1019 1.7 thorpej static int
1020 1.7 thorpej get_info_gate(struct sockaddr **sap,
1021 1.15 lukem struct sockaddr_in *rsin)
1022 1.7 thorpej {
1023 1.7 thorpej struct sockaddr_dl *sdl = (struct sockaddr_dl *)*sap;
1024 1.7 thorpej struct interface *ifp;
1025 1.7 thorpej
1026 1.7 thorpej if (sdl == 0)
1027 1.7 thorpej return 0;
1028 1.7 thorpej if ((sdl)->sdl_family == AF_INET)
1029 1.7 thorpej return 1;
1030 1.7 thorpej if ((sdl)->sdl_family != AF_LINK)
1031 1.7 thorpej return 0;
1032 1.7 thorpej
1033 1.7 thorpej ifp = ifwithindex(sdl->sdl_index, 1);
1034 1.7 thorpej if (ifp == 0)
1035 1.7 thorpej return 0;
1036 1.1 thorpej
1037 1.15 lukem rsin->sin_addr.s_addr = ifp->int_addr;
1038 1.7 thorpej #ifdef _HAVE_SA_LEN
1039 1.15 lukem rsin->sin_len = sizeof(*rsin);
1040 1.7 thorpej #endif
1041 1.15 lukem rsin->sin_family = AF_INET;
1042 1.15 lukem *sap = (struct sockaddr*)rsin;
1043 1.7 thorpej
1044 1.7 thorpej return 1;
1045 1.1 thorpej }
1046 1.1 thorpej
1047 1.1 thorpej
1048 1.1 thorpej /* Clean the kernel table by copying it to the daemon image.
1049 1.1 thorpej * Eventually the daemon will delete any extra routes.
1050 1.1 thorpej */
1051 1.1 thorpej void
1052 1.1 thorpej flush_kern(void)
1053 1.1 thorpej {
1054 1.7 thorpej static char *sysctl_buf;
1055 1.7 thorpej static size_t sysctl_buf_size = 0;
1056 1.1 thorpej size_t needed;
1057 1.1 thorpej int mib[6];
1058 1.7 thorpej char *next, *lim;
1059 1.1 thorpej struct rt_msghdr *rtm;
1060 1.7 thorpej struct sockaddr_in gate_sin;
1061 1.1 thorpej struct rt_addrinfo info;
1062 1.7 thorpej int i;
1063 1.7 thorpej struct khash *k;
1064 1.1 thorpej
1065 1.1 thorpej
1066 1.7 thorpej for (i = 0; i < KHASH_SIZE; i++) {
1067 1.7 thorpej for (k = khash_bins[i]; k != 0; k = k->k_next) {
1068 1.7 thorpej k->k_state |= KS_CHECK;
1069 1.7 thorpej }
1070 1.7 thorpej }
1071 1.7 thorpej
1072 1.1 thorpej mib[0] = CTL_NET;
1073 1.1 thorpej mib[1] = PF_ROUTE;
1074 1.1 thorpej mib[2] = 0; /* protocol */
1075 1.1 thorpej mib[3] = 0; /* wildcard address family */
1076 1.1 thorpej mib[4] = NET_RT_DUMP;
1077 1.1 thorpej mib[5] = 0; /* no flags */
1078 1.7 thorpej for (;;) {
1079 1.7 thorpej if ((needed = sysctl_buf_size) != 0) {
1080 1.7 thorpej if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
1081 1.7 thorpej break;
1082 1.7 thorpej if (errno != ENOMEM && errno != EFAULT)
1083 1.7 thorpej BADERR(1,"flush_kern: sysctl(RT_DUMP)");
1084 1.7 thorpej free(sysctl_buf);
1085 1.7 thorpej needed = 0;
1086 1.7 thorpej }
1087 1.7 thorpej if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
1088 1.7 thorpej BADERR(1,"flush_kern: sysctl(RT_DUMP) estimate");
1089 1.7 thorpej /* Kludge around the habit of some systems, such as
1090 1.7 thorpej * BSD/OS 3.1, to not admit how many routes are in the
1091 1.7 thorpej * kernel, or at least to be quite wrong.
1092 1.7 thorpej */
1093 1.7 thorpej needed += 50*(sizeof(*rtm)+5*sizeof(struct sockaddr));
1094 1.7 thorpej sysctl_buf = rtmalloc(sysctl_buf_size = needed,
1095 1.7 thorpej "flush_kern sysctl(RT_DUMP)");
1096 1.1 thorpej }
1097 1.7 thorpej
1098 1.7 thorpej lim = sysctl_buf + needed;
1099 1.7 thorpej for (next = sysctl_buf; next < lim; next += rtm->rtm_msglen) {
1100 1.1 thorpej rtm = (struct rt_msghdr *)next;
1101 1.7 thorpej if (rtm->rtm_msglen == 0) {
1102 1.7 thorpej msglog("zero length kernel route at "
1103 1.10 ross " %#lx in buffer %#lx before %#lx",
1104 1.13 christos (u_long)rtm, (u_long)sysctl_buf, (u_long)lim);
1105 1.7 thorpej break;
1106 1.7 thorpej }
1107 1.1 thorpej
1108 1.1 thorpej rt_xaddrs(&info,
1109 1.1 thorpej (struct sockaddr *)(rtm+1),
1110 1.1 thorpej (struct sockaddr *)(next + rtm->rtm_msglen),
1111 1.1 thorpej rtm->rtm_addrs);
1112 1.1 thorpej
1113 1.1 thorpej if (INFO_DST(&info) == 0
1114 1.1 thorpej || INFO_DST(&info)->sa_family != AF_INET)
1115 1.1 thorpej continue;
1116 1.1 thorpej
1117 1.1 thorpej /* ignore ARP table entries on systems with a merged route
1118 1.1 thorpej * and ARP table.
1119 1.1 thorpej */
1120 1.1 thorpej if (rtm->rtm_flags & RTF_LLINFO)
1121 1.1 thorpej continue;
1122 1.1 thorpej
1123 1.1 thorpej /* ignore multicast addresses
1124 1.1 thorpej */
1125 1.1 thorpej if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info)))))
1126 1.1 thorpej continue;
1127 1.1 thorpej
1128 1.7 thorpej if (!get_info_gate(&INFO_GATE(&info), &gate_sin))
1129 1.7 thorpej continue;
1130 1.7 thorpej
1131 1.1 thorpej /* Note static routes and interface routes, and also
1132 1.1 thorpej * preload the image of the kernel table so that
1133 1.1 thorpej * we can later clean it, as well as avoid making
1134 1.1 thorpej * unneeded changes. Keep the old kernel routes for a
1135 1.1 thorpej * few seconds to allow a RIP or router-discovery
1136 1.1 thorpej * response to be heard.
1137 1.1 thorpej */
1138 1.1 thorpej rtm_add(rtm,&info,MIN_WAITTIME);
1139 1.1 thorpej }
1140 1.7 thorpej
1141 1.7 thorpej for (i = 0; i < KHASH_SIZE; i++) {
1142 1.7 thorpej for (k = khash_bins[i]; k != 0; k = k->k_next) {
1143 1.7 thorpej if (k->k_state & KS_CHECK) {
1144 1.7 thorpej msglog("%s --> %s disappeared from kernel",
1145 1.7 thorpej addrname(k->k_dst, k->k_mask, 0),
1146 1.7 thorpej naddr_ntoa(k->k_gate));
1147 1.7 thorpej del_static(k->k_dst, k->k_mask, k->k_gate, 1);
1148 1.7 thorpej }
1149 1.7 thorpej }
1150 1.7 thorpej }
1151 1.1 thorpej }
1152 1.1 thorpej
1153 1.1 thorpej
1154 1.1 thorpej /* Listen to announcements from the kernel
1155 1.1 thorpej */
1156 1.1 thorpej void
1157 1.1 thorpej read_rt(void)
1158 1.1 thorpej {
1159 1.1 thorpej long cc;
1160 1.1 thorpej struct interface *ifp;
1161 1.7 thorpej struct sockaddr_in gate_sin;
1162 1.7 thorpej naddr mask, gate;
1163 1.1 thorpej union {
1164 1.1 thorpej struct {
1165 1.1 thorpej struct rt_msghdr rtm;
1166 1.1 thorpej struct sockaddr addrs[RTAX_MAX];
1167 1.1 thorpej } r;
1168 1.1 thorpej struct if_msghdr ifm;
1169 1.1 thorpej } m;
1170 1.1 thorpej char str[100], *strp;
1171 1.1 thorpej struct rt_addrinfo info;
1172 1.1 thorpej
1173 1.1 thorpej
1174 1.1 thorpej for (;;) {
1175 1.1 thorpej cc = read(rt_sock, &m, sizeof(m));
1176 1.1 thorpej if (cc <= 0) {
1177 1.1 thorpej if (cc < 0 && errno != EWOULDBLOCK)
1178 1.1 thorpej LOGERR("read(rt_sock)");
1179 1.1 thorpej return;
1180 1.1 thorpej }
1181 1.1 thorpej
1182 1.1 thorpej if (m.r.rtm.rtm_version != RTM_VERSION) {
1183 1.1 thorpej msglog("bogus routing message version %d",
1184 1.1 thorpej m.r.rtm.rtm_version);
1185 1.1 thorpej continue;
1186 1.1 thorpej }
1187 1.1 thorpej
1188 1.1 thorpej /* Ignore our own results.
1189 1.1 thorpej */
1190 1.1 thorpej if (m.r.rtm.rtm_type <= RTM_CHANGE
1191 1.1 thorpej && m.r.rtm.rtm_pid == mypid) {
1192 1.1 thorpej static int complained = 0;
1193 1.1 thorpej if (!complained) {
1194 1.1 thorpej msglog("receiving our own change messages");
1195 1.1 thorpej complained = 1;
1196 1.1 thorpej }
1197 1.1 thorpej continue;
1198 1.1 thorpej }
1199 1.1 thorpej
1200 1.1 thorpej if (m.r.rtm.rtm_type == RTM_IFINFO
1201 1.1 thorpej || m.r.rtm.rtm_type == RTM_NEWADDR
1202 1.1 thorpej || m.r.rtm.rtm_type == RTM_DELADDR) {
1203 1.4 christos ifp = ifwithindex(m.ifm.ifm_index,
1204 1.4 christos m.r.rtm.rtm_type != RTM_DELADDR);
1205 1.1 thorpej if (ifp == 0)
1206 1.1 thorpej trace_act("note %s with flags %#x"
1207 1.7 thorpej " for unknown interface index #%d",
1208 1.1 thorpej rtm_type_name(m.r.rtm.rtm_type),
1209 1.1 thorpej m.ifm.ifm_flags,
1210 1.1 thorpej m.ifm.ifm_index);
1211 1.1 thorpej else
1212 1.4 christos trace_act("note %s with flags %#x for %s",
1213 1.1 thorpej rtm_type_name(m.r.rtm.rtm_type),
1214 1.1 thorpej m.ifm.ifm_flags,
1215 1.1 thorpej ifp->int_name);
1216 1.1 thorpej
1217 1.1 thorpej /* After being informed of a change to an interface,
1218 1.1 thorpej * check them all now if the check would otherwise
1219 1.1 thorpej * be a long time from now, if the interface is
1220 1.1 thorpej * not known, or if the interface has been turned
1221 1.1 thorpej * off or on.
1222 1.1 thorpej */
1223 1.1 thorpej if (ifinit_timer.tv_sec-now.tv_sec>=CHECK_BAD_INTERVAL
1224 1.1 thorpej || ifp == 0
1225 1.1 thorpej || ((ifp->int_if_flags ^ m.ifm.ifm_flags)
1226 1.7 thorpej & IFF_UP) != 0)
1227 1.1 thorpej ifinit_timer.tv_sec = now.tv_sec;
1228 1.1 thorpej continue;
1229 1.1 thorpej }
1230 1.11 bouyer #ifdef RTM_OIFINFO
1231 1.11 bouyer if (m.r.rtm.rtm_type == RTM_OIFINFO) {
1232 1.11 bouyer continue; /* ignore compat message */
1233 1.11 bouyer }
1234 1.11 bouyer #endif
1235 1.1 thorpej
1236 1.1 thorpej strcpy(str, rtm_type_name(m.r.rtm.rtm_type));
1237 1.1 thorpej strp = &str[strlen(str)];
1238 1.1 thorpej if (m.r.rtm.rtm_type <= RTM_CHANGE)
1239 1.1 thorpej strp += sprintf(strp," from pid %d",m.r.rtm.rtm_pid);
1240 1.1 thorpej
1241 1.1 thorpej rt_xaddrs(&info, m.r.addrs, &m.r.addrs[RTAX_MAX],
1242 1.1 thorpej m.r.rtm.rtm_addrs);
1243 1.1 thorpej
1244 1.1 thorpej if (INFO_DST(&info) == 0) {
1245 1.4 christos trace_act("ignore %s without dst", str);
1246 1.1 thorpej continue;
1247 1.1 thorpej }
1248 1.1 thorpej
1249 1.1 thorpej if (INFO_DST(&info)->sa_family != AF_INET) {
1250 1.4 christos trace_act("ignore %s for AF %d", str,
1251 1.1 thorpej INFO_DST(&info)->sa_family);
1252 1.1 thorpej continue;
1253 1.1 thorpej }
1254 1.1 thorpej
1255 1.1 thorpej mask = ((INFO_MASK(&info) != 0)
1256 1.1 thorpej ? ntohl(S_ADDR(INFO_MASK(&info)))
1257 1.1 thorpej : (m.r.rtm.rtm_flags & RTF_HOST)
1258 1.1 thorpej ? HOST_MASK
1259 1.1 thorpej : std_mask(S_ADDR(INFO_DST(&info))));
1260 1.1 thorpej
1261 1.1 thorpej strp += sprintf(strp, ": %s",
1262 1.1 thorpej addrname(S_ADDR(INFO_DST(&info)), mask, 0));
1263 1.1 thorpej
1264 1.1 thorpej if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) {
1265 1.4 christos trace_act("ignore multicast %s", str);
1266 1.1 thorpej continue;
1267 1.1 thorpej }
1268 1.14 christos
1269 1.7 thorpej if (m.r.rtm.rtm_flags & RTF_LLINFO) {
1270 1.7 thorpej trace_act("ignore ARP %s", str);
1271 1.7 thorpej continue;
1272 1.7 thorpej }
1273 1.1 thorpej
1274 1.7 thorpej if (get_info_gate(&INFO_GATE(&info), &gate_sin)) {
1275 1.7 thorpej gate = S_ADDR(INFO_GATE(&info));
1276 1.7 thorpej strp += sprintf(strp, " --> %s", naddr_ntoa(gate));
1277 1.7 thorpej } else {
1278 1.7 thorpej gate = 0;
1279 1.7 thorpej }
1280 1.1 thorpej
1281 1.1 thorpej if (INFO_AUTHOR(&info) != 0)
1282 1.1 thorpej strp += sprintf(strp, " by authority of %s",
1283 1.1 thorpej saddr_ntoa(INFO_AUTHOR(&info)));
1284 1.1 thorpej
1285 1.1 thorpej switch (m.r.rtm.rtm_type) {
1286 1.1 thorpej case RTM_ADD:
1287 1.1 thorpej case RTM_CHANGE:
1288 1.1 thorpej case RTM_REDIRECT:
1289 1.1 thorpej if (m.r.rtm.rtm_errno != 0) {
1290 1.4 christos trace_act("ignore %s with \"%s\" error",
1291 1.1 thorpej str, strerror(m.r.rtm.rtm_errno));
1292 1.1 thorpej } else {
1293 1.4 christos trace_act("%s", str);
1294 1.1 thorpej rtm_add(&m.r.rtm,&info,0);
1295 1.1 thorpej }
1296 1.1 thorpej break;
1297 1.1 thorpej
1298 1.1 thorpej case RTM_DELETE:
1299 1.7 thorpej if (m.r.rtm.rtm_errno != 0
1300 1.7 thorpej && m.r.rtm.rtm_errno != ESRCH) {
1301 1.4 christos trace_act("ignore %s with \"%s\" error",
1302 1.1 thorpej str, strerror(m.r.rtm.rtm_errno));
1303 1.1 thorpej } else {
1304 1.4 christos trace_act("%s", str);
1305 1.7 thorpej del_static(S_ADDR(INFO_DST(&info)), mask,
1306 1.7 thorpej gate, 1);
1307 1.1 thorpej }
1308 1.1 thorpej break;
1309 1.1 thorpej
1310 1.1 thorpej case RTM_LOSING:
1311 1.4 christos trace_act("%s", str);
1312 1.1 thorpej rtm_lose(&m.r.rtm,&info);
1313 1.1 thorpej break;
1314 1.1 thorpej
1315 1.1 thorpej default:
1316 1.4 christos trace_act("ignore %s", str);
1317 1.1 thorpej break;
1318 1.1 thorpej }
1319 1.1 thorpej }
1320 1.1 thorpej }
1321 1.1 thorpej
1322 1.1 thorpej
1323 1.1 thorpej /* after aggregating, note routes that belong in the kernel
1324 1.1 thorpej */
1325 1.1 thorpej static void
1326 1.1 thorpej kern_out(struct ag_info *ag)
1327 1.1 thorpej {
1328 1.1 thorpej struct khash *k;
1329 1.1 thorpej
1330 1.1 thorpej
1331 1.1 thorpej /* Do not install bad routes if they are not already present.
1332 1.1 thorpej * This includes routes that had RS_NET_SYN for interfaces that
1333 1.1 thorpej * recently died.
1334 1.1 thorpej */
1335 1.1 thorpej if (ag->ag_metric == HOPCNT_INFINITY) {
1336 1.1 thorpej k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 0);
1337 1.1 thorpej if (k == 0)
1338 1.1 thorpej return;
1339 1.1 thorpej } else {
1340 1.1 thorpej k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask);
1341 1.1 thorpej }
1342 1.1 thorpej
1343 1.1 thorpej if (k->k_state & KS_NEW) {
1344 1.1 thorpej /* will need to add new entry to the kernel table */
1345 1.1 thorpej k->k_state = KS_ADD;
1346 1.1 thorpej if (ag->ag_state & AGS_GATEWAY)
1347 1.1 thorpej k->k_state |= KS_GATEWAY;
1348 1.1 thorpej k->k_gate = ag->ag_gate;
1349 1.1 thorpej k->k_metric = ag->ag_metric;
1350 1.1 thorpej return;
1351 1.1 thorpej }
1352 1.1 thorpej
1353 1.1 thorpej if (k->k_state & KS_STATIC)
1354 1.1 thorpej return;
1355 1.1 thorpej
1356 1.1 thorpej /* modify existing kernel entry if necessary */
1357 1.1 thorpej if (k->k_gate != ag->ag_gate
1358 1.1 thorpej || k->k_metric != ag->ag_metric) {
1359 1.7 thorpej /* Must delete bad interface routes etc. to change them. */
1360 1.7 thorpej if (k->k_metric == HOPCNT_INFINITY)
1361 1.7 thorpej k->k_state |= KS_DEL_ADD;
1362 1.1 thorpej k->k_gate = ag->ag_gate;
1363 1.1 thorpej k->k_metric = ag->ag_metric;
1364 1.1 thorpej k->k_state |= KS_CHANGE;
1365 1.1 thorpej }
1366 1.1 thorpej
1367 1.7 thorpej /* If the daemon thinks the route should exist, forget
1368 1.7 thorpej * about any redirections.
1369 1.7 thorpej * If the daemon thinks the route should exist, eventually
1370 1.7 thorpej * override manual intervention by the operator.
1371 1.7 thorpej */
1372 1.7 thorpej if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) {
1373 1.1 thorpej k->k_state &= ~KS_DYNAMIC;
1374 1.1 thorpej k->k_state |= (KS_ADD | KS_DEL_ADD);
1375 1.1 thorpej }
1376 1.1 thorpej
1377 1.1 thorpej if ((k->k_state & KS_GATEWAY)
1378 1.1 thorpej && !(ag->ag_state & AGS_GATEWAY)) {
1379 1.1 thorpej k->k_state &= ~KS_GATEWAY;
1380 1.1 thorpej k->k_state |= (KS_ADD | KS_DEL_ADD);
1381 1.1 thorpej } else if (!(k->k_state & KS_GATEWAY)
1382 1.1 thorpej && (ag->ag_state & AGS_GATEWAY)) {
1383 1.1 thorpej k->k_state |= KS_GATEWAY;
1384 1.1 thorpej k->k_state |= (KS_ADD | KS_DEL_ADD);
1385 1.1 thorpej }
1386 1.1 thorpej
1387 1.3 christos /* Deleting-and-adding is necessary to change aspects of a route.
1388 1.3 christos * Just delete instead of deleting and then adding a bad route.
1389 1.1 thorpej * Otherwise, we want to keep the route in the kernel.
1390 1.1 thorpej */
1391 1.1 thorpej if (k->k_metric == HOPCNT_INFINITY
1392 1.1 thorpej && (k->k_state & KS_DEL_ADD))
1393 1.1 thorpej k->k_state |= KS_DELETE;
1394 1.1 thorpej else
1395 1.1 thorpej k->k_state &= ~KS_DELETE;
1396 1.1 thorpej #undef RT
1397 1.1 thorpej }
1398 1.1 thorpej
1399 1.1 thorpej
1400 1.1 thorpej /* ARGSUSED */
1401 1.1 thorpej static int
1402 1.8 christos walk_kern(struct radix_node *rn,
1403 1.9 christos struct walkarg *argp UNUSED)
1404 1.1 thorpej {
1405 1.1 thorpej #define RT ((struct rt_entry *)rn)
1406 1.1 thorpej char metric, pref;
1407 1.1 thorpej u_int ags = 0;
1408 1.1 thorpej
1409 1.1 thorpej
1410 1.1 thorpej /* Do not install synthetic routes */
1411 1.1 thorpej if (RT->rt_state & RS_NET_SYN)
1412 1.1 thorpej return 0;
1413 1.1 thorpej
1414 1.1 thorpej if (!(RT->rt_state & RS_IF)) {
1415 1.7 thorpej /* This is an ordinary route, not for an interface.
1416 1.7 thorpej */
1417 1.7 thorpej
1418 1.7 thorpej /* aggregate, ordinary good routes without regard to
1419 1.7 thorpej * their metric
1420 1.7 thorpej */
1421 1.7 thorpej pref = 1;
1422 1.7 thorpej ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1423 1.7 thorpej
1424 1.7 thorpej /* Do not install host routes directly to hosts, to avoid
1425 1.7 thorpej * interfering with ARP entries in the kernel table.
1426 1.7 thorpej */
1427 1.7 thorpej if (RT_ISHOST(RT)
1428 1.7 thorpej && ntohl(RT->rt_dst) == RT->rt_gate)
1429 1.7 thorpej return 0;
1430 1.1 thorpej
1431 1.1 thorpej } else {
1432 1.7 thorpej /* This is an interface route.
1433 1.7 thorpej * Do not install routes for "external" remote interfaces.
1434 1.1 thorpej */
1435 1.1 thorpej if (RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_EXTERNAL))
1436 1.1 thorpej return 0;
1437 1.1 thorpej
1438 1.7 thorpej /* Interfaces should override received routes.
1439 1.7 thorpej */
1440 1.7 thorpej pref = 0;
1441 1.7 thorpej ags |= (AGS_IF | AGS_CORS_GATE);
1442 1.1 thorpej
1443 1.1 thorpej /* If it is not an interface, or an alias for an interface,
1444 1.1 thorpej * it must be a "gateway."
1445 1.1 thorpej *
1446 1.1 thorpej * If it is a "remote" interface, it is also a "gateway" to
1447 1.1 thorpej * the kernel if is not a alias.
1448 1.1 thorpej */
1449 1.1 thorpej if (RT->rt_ifp == 0
1450 1.4 christos || (RT->rt_ifp->int_state & IS_REMOTE))
1451 1.7 thorpej ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1452 1.1 thorpej }
1453 1.1 thorpej
1454 1.7 thorpej /* If RIP is off and IRDP is on, let the route to the discovered
1455 1.7 thorpej * route suppress any RIP routes. Eventually the RIP routes
1456 1.7 thorpej * will time-out and be deleted. This reaches the steady-state
1457 1.7 thorpej * quicker.
1458 1.7 thorpej */
1459 1.7 thorpej if ((RT->rt_state & RS_RDISC) && rip_sock < 0)
1460 1.1 thorpej ags |= AGS_CORS_GATE;
1461 1.1 thorpej
1462 1.1 thorpej metric = RT->rt_metric;
1463 1.1 thorpej if (metric == HOPCNT_INFINITY) {
1464 1.1 thorpej /* if the route is dead, so try hard to aggregate. */
1465 1.1 thorpej pref = HOPCNT_INFINITY;
1466 1.1 thorpej ags |= (AGS_FINE_GATE | AGS_SUPPRESS);
1467 1.7 thorpej ags &= ~(AGS_IF | AGS_CORS_GATE);
1468 1.1 thorpej }
1469 1.1 thorpej
1470 1.1 thorpej ag_check(RT->rt_dst, RT->rt_mask, RT->rt_gate, 0,
1471 1.1 thorpej metric,pref, 0, 0, ags, kern_out);
1472 1.1 thorpej return 0;
1473 1.1 thorpej #undef RT
1474 1.1 thorpej }
1475 1.1 thorpej
1476 1.1 thorpej
1477 1.1 thorpej /* Update the kernel table to match the daemon table.
1478 1.1 thorpej */
1479 1.1 thorpej static void
1480 1.1 thorpej fix_kern(void)
1481 1.1 thorpej {
1482 1.4 christos int i;
1483 1.1 thorpej struct khash *k, **pk;
1484 1.1 thorpej
1485 1.1 thorpej
1486 1.1 thorpej need_kern = age_timer;
1487 1.1 thorpej
1488 1.1 thorpej /* Walk daemon table, updating the copy of the kernel table.
1489 1.1 thorpej */
1490 1.1 thorpej (void)rn_walktree(rhead, walk_kern, 0);
1491 1.1 thorpej ag_flush(0,0,kern_out);
1492 1.1 thorpej
1493 1.1 thorpej for (i = 0; i < KHASH_SIZE; i++) {
1494 1.1 thorpej for (pk = &khash_bins[i]; (k = *pk) != 0; ) {
1495 1.1 thorpej /* Do not touch static routes */
1496 1.1 thorpej if (k->k_state & KS_STATIC) {
1497 1.1 thorpej kern_check_static(k,0);
1498 1.1 thorpej pk = &k->k_next;
1499 1.1 thorpej continue;
1500 1.1 thorpej }
1501 1.1 thorpej
1502 1.1 thorpej /* check hold on routes deleted by the operator */
1503 1.1 thorpej if (k->k_keep > now.tv_sec) {
1504 1.7 thorpej /* ensure we check when the hold is over */
1505 1.1 thorpej LIM_SEC(need_kern, k->k_keep);
1506 1.7 thorpej /* mark for the next cycle */
1507 1.1 thorpej k->k_state |= KS_DELETE;
1508 1.1 thorpej pk = &k->k_next;
1509 1.1 thorpej continue;
1510 1.1 thorpej }
1511 1.1 thorpej
1512 1.4 christos if ((k->k_state & KS_DELETE)
1513 1.4 christos && !(k->k_state & KS_DYNAMIC)) {
1514 1.4 christos kern_ioctl(k, RTM_DELETE, 0);
1515 1.1 thorpej *pk = k->k_next;
1516 1.1 thorpej free(k);
1517 1.1 thorpej continue;
1518 1.1 thorpej }
1519 1.1 thorpej
1520 1.4 christos if (k->k_state & KS_DEL_ADD)
1521 1.4 christos kern_ioctl(k, RTM_DELETE, 0);
1522 1.3 christos
1523 1.4 christos if (k->k_state & KS_ADD) {
1524 1.4 christos kern_ioctl(k, RTM_ADD,
1525 1.4 christos ((0 != (k->k_state & (KS_GATEWAY
1526 1.4 christos | KS_DYNAMIC)))
1527 1.4 christos ? RTF_GATEWAY : 0));
1528 1.4 christos } else if (k->k_state & KS_CHANGE) {
1529 1.4 christos kern_ioctl(k, RTM_CHANGE,
1530 1.4 christos ((0 != (k->k_state & (KS_GATEWAY
1531 1.4 christos | KS_DYNAMIC)))
1532 1.4 christos ? RTF_GATEWAY : 0));
1533 1.1 thorpej }
1534 1.4 christos k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD);
1535 1.1 thorpej
1536 1.1 thorpej /* Mark this route to be deleted in the next cycle.
1537 1.1 thorpej * This deletes routes that disappear from the
1538 1.1 thorpej * daemon table, since the normal aging code
1539 1.1 thorpej * will clear the bit for routes that have not
1540 1.1 thorpej * disappeared from the daemon table.
1541 1.1 thorpej */
1542 1.1 thorpej k->k_state |= KS_DELETE;
1543 1.1 thorpej pk = &k->k_next;
1544 1.1 thorpej }
1545 1.1 thorpej }
1546 1.1 thorpej }
1547 1.1 thorpej
1548 1.1 thorpej
1549 1.1 thorpej /* Delete a static route in the image of the kernel table.
1550 1.1 thorpej */
1551 1.1 thorpej void
1552 1.1 thorpej del_static(naddr dst,
1553 1.1 thorpej naddr mask,
1554 1.7 thorpej naddr gate,
1555 1.1 thorpej int gone)
1556 1.1 thorpej {
1557 1.1 thorpej struct khash *k;
1558 1.1 thorpej struct rt_entry *rt;
1559 1.1 thorpej
1560 1.1 thorpej /* Just mark it in the table to be deleted next time the kernel
1561 1.1 thorpej * table is updated.
1562 1.1 thorpej * If it has already been deleted, mark it as such, and set its
1563 1.1 thorpej * keep-timer so that it will not be deleted again for a while.
1564 1.1 thorpej * This lets the operator delete a route added by the daemon
1565 1.1 thorpej * and add a replacement.
1566 1.1 thorpej */
1567 1.1 thorpej k = kern_find(dst, mask, 0);
1568 1.7 thorpej if (k != 0 && (gate == 0 || k->k_gate == gate)) {
1569 1.7 thorpej k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK);
1570 1.1 thorpej k->k_state |= KS_DELETE;
1571 1.1 thorpej if (gone) {
1572 1.1 thorpej k->k_state |= KS_DELETED;
1573 1.1 thorpej k->k_keep = now.tv_sec + K_KEEP_LIM;
1574 1.1 thorpej }
1575 1.1 thorpej }
1576 1.1 thorpej
1577 1.1 thorpej rt = rtget(dst, mask);
1578 1.1 thorpej if (rt != 0 && (rt->rt_state & RS_STATIC))
1579 1.1 thorpej rtbad(rt);
1580 1.1 thorpej }
1581 1.1 thorpej
1582 1.1 thorpej
1583 1.3 christos /* Delete all routes generated from ICMP Redirects that use a given gateway,
1584 1.3 christos * as well as old redirected routes.
1585 1.1 thorpej */
1586 1.1 thorpej void
1587 1.1 thorpej del_redirects(naddr bad_gate,
1588 1.1 thorpej time_t old)
1589 1.1 thorpej {
1590 1.1 thorpej int i;
1591 1.1 thorpej struct khash *k;
1592 1.1 thorpej
1593 1.1 thorpej
1594 1.1 thorpej for (i = 0; i < KHASH_SIZE; i++) {
1595 1.1 thorpej for (k = khash_bins[i]; k != 0; k = k->k_next) {
1596 1.1 thorpej if (!(k->k_state & KS_DYNAMIC)
1597 1.1 thorpej || (k->k_state & KS_STATIC))
1598 1.1 thorpej continue;
1599 1.1 thorpej
1600 1.1 thorpej if (k->k_gate != bad_gate
1601 1.1 thorpej && k->k_redirect_time > old
1602 1.1 thorpej && !supplier)
1603 1.1 thorpej continue;
1604 1.1 thorpej
1605 1.1 thorpej k->k_state |= KS_DELETE;
1606 1.3 christos k->k_state &= ~KS_DYNAMIC;
1607 1.1 thorpej need_kern.tv_sec = now.tv_sec;
1608 1.4 christos trace_act("mark redirected %s --> %s for deletion",
1609 1.1 thorpej addrname(k->k_dst, k->k_mask, 0),
1610 1.1 thorpej naddr_ntoa(k->k_gate));
1611 1.1 thorpej }
1612 1.1 thorpej }
1613 1.1 thorpej }
1614 1.1 thorpej
1615 1.1 thorpej
1616 1.1 thorpej /* Start the daemon tables.
1617 1.1 thorpej */
1618 1.9 christos extern int max_keylen;
1619 1.9 christos
1620 1.1 thorpej void
1621 1.1 thorpej rtinit(void)
1622 1.1 thorpej {
1623 1.1 thorpej int i;
1624 1.1 thorpej struct ag_info *ag;
1625 1.1 thorpej
1626 1.1 thorpej /* Initialize the radix trees */
1627 1.1 thorpej max_keylen = sizeof(struct sockaddr_in);
1628 1.1 thorpej rn_init();
1629 1.1 thorpej rn_inithead((void**)&rhead, 32);
1630 1.1 thorpej
1631 1.1 thorpej /* mark all of the slots in the table free */
1632 1.1 thorpej ag_avail = ag_slots;
1633 1.1 thorpej for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) {
1634 1.1 thorpej ag->ag_fine = ag+1;
1635 1.1 thorpej ag++;
1636 1.1 thorpej }
1637 1.1 thorpej }
1638 1.1 thorpej
1639 1.1 thorpej
1640 1.1 thorpej #ifdef _HAVE_SIN_LEN
1641 1.14 christos static struct sockaddr_in dst_sock = {sizeof(dst_sock), AF_INET, 0, {0}, {0}};
1642 1.14 christos static struct sockaddr_in mask_sock = {sizeof(mask_sock), AF_INET, 0, {0}, {0}};
1643 1.1 thorpej #else
1644 1.1 thorpej static struct sockaddr_in_new dst_sock = {_SIN_ADDR_SIZE, AF_INET};
1645 1.1 thorpej static struct sockaddr_in_new mask_sock = {_SIN_ADDR_SIZE, AF_INET};
1646 1.1 thorpej #endif
1647 1.1 thorpej
1648 1.1 thorpej
1649 1.13 christos static void
1650 1.1 thorpej set_need_flash(void)
1651 1.1 thorpej {
1652 1.1 thorpej if (!need_flash) {
1653 1.1 thorpej need_flash = 1;
1654 1.1 thorpej /* Do not send the flash update immediately. Wait a little
1655 1.1 thorpej * while to hear from other routers.
1656 1.1 thorpej */
1657 1.1 thorpej no_flash.tv_sec = now.tv_sec + MIN_WAITTIME;
1658 1.1 thorpej }
1659 1.1 thorpej }
1660 1.1 thorpej
1661 1.1 thorpej
1662 1.1 thorpej /* Get a particular routing table entry
1663 1.1 thorpej */
1664 1.1 thorpej struct rt_entry *
1665 1.1 thorpej rtget(naddr dst, naddr mask)
1666 1.1 thorpej {
1667 1.1 thorpej struct rt_entry *rt;
1668 1.1 thorpej
1669 1.1 thorpej dst_sock.sin_addr.s_addr = dst;
1670 1.13 christos mask_sock.sin_addr.s_addr = htonl(mask);
1671 1.1 thorpej masktrim(&mask_sock);
1672 1.1 thorpej rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock,&mask_sock,rhead);
1673 1.1 thorpej if (!rt
1674 1.1 thorpej || rt->rt_dst != dst
1675 1.1 thorpej || rt->rt_mask != mask)
1676 1.1 thorpej return 0;
1677 1.1 thorpej
1678 1.1 thorpej return rt;
1679 1.1 thorpej }
1680 1.1 thorpej
1681 1.1 thorpej
1682 1.1 thorpej /* Find a route to dst as the kernel would.
1683 1.1 thorpej */
1684 1.1 thorpej struct rt_entry *
1685 1.1 thorpej rtfind(naddr dst)
1686 1.1 thorpej {
1687 1.1 thorpej dst_sock.sin_addr.s_addr = dst;
1688 1.1 thorpej return (struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead);
1689 1.1 thorpej }
1690 1.1 thorpej
1691 1.1 thorpej
1692 1.1 thorpej /* add a route to the table
1693 1.1 thorpej */
1694 1.1 thorpej void
1695 1.1 thorpej rtadd(naddr dst,
1696 1.1 thorpej naddr mask,
1697 1.7 thorpej u_int state, /* rt_state for the entry */
1698 1.7 thorpej struct rt_spare *new)
1699 1.1 thorpej {
1700 1.1 thorpej struct rt_entry *rt;
1701 1.1 thorpej naddr smask;
1702 1.1 thorpej int i;
1703 1.1 thorpej struct rt_spare *rts;
1704 1.1 thorpej
1705 1.3 christos rt = (struct rt_entry *)rtmalloc(sizeof (*rt), "rtadd");
1706 1.5 lukem memset(rt, 0, sizeof(*rt));
1707 1.1 thorpej for (rts = rt->rt_spares, i = NUM_SPARES; i != 0; i--, rts++)
1708 1.1 thorpej rts->rts_metric = HOPCNT_INFINITY;
1709 1.1 thorpej
1710 1.1 thorpej rt->rt_nodes->rn_key = (caddr_t)&rt->rt_dst_sock;
1711 1.1 thorpej rt->rt_dst = dst;
1712 1.1 thorpej rt->rt_dst_sock.sin_family = AF_INET;
1713 1.1 thorpej #ifdef _HAVE_SIN_LEN
1714 1.1 thorpej rt->rt_dst_sock.sin_len = dst_sock.sin_len;
1715 1.1 thorpej #endif
1716 1.1 thorpej if (mask != HOST_MASK) {
1717 1.1 thorpej smask = std_mask(dst);
1718 1.1 thorpej if ((smask & ~mask) == 0 && mask > smask)
1719 1.1 thorpej state |= RS_SUBNET;
1720 1.1 thorpej }
1721 1.13 christos mask_sock.sin_addr.s_addr = htonl(mask);
1722 1.1 thorpej masktrim(&mask_sock);
1723 1.1 thorpej rt->rt_mask = mask;
1724 1.1 thorpej rt->rt_state = state;
1725 1.7 thorpej rt->rt_spares[0] = *new;
1726 1.1 thorpej rt->rt_time = now.tv_sec;
1727 1.1 thorpej rt->rt_poison_metric = HOPCNT_INFINITY;
1728 1.1 thorpej rt->rt_seqno = update_seqno;
1729 1.1 thorpej
1730 1.3 christos if (++total_routes == MAX_ROUTES)
1731 1.3 christos msglog("have maximum (%d) routes", total_routes);
1732 1.1 thorpej if (TRACEACTIONS)
1733 1.1 thorpej trace_add_del("Add", rt);
1734 1.1 thorpej
1735 1.1 thorpej need_kern.tv_sec = now.tv_sec;
1736 1.1 thorpej set_need_flash();
1737 1.1 thorpej
1738 1.1 thorpej if (0 == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock,
1739 1.1 thorpej rhead, rt->rt_nodes)) {
1740 1.9 christos msglog("rnh_addaddr() failed for %s mask=%#lx",
1741 1.9 christos naddr_ntoa(dst), (u_long)mask);
1742 1.12 christos free(rt);
1743 1.1 thorpej }
1744 1.1 thorpej }
1745 1.1 thorpej
1746 1.1 thorpej
1747 1.1 thorpej /* notice a changed route
1748 1.1 thorpej */
1749 1.1 thorpej void
1750 1.1 thorpej rtchange(struct rt_entry *rt,
1751 1.1 thorpej u_int state, /* new state bits */
1752 1.7 thorpej struct rt_spare *new,
1753 1.1 thorpej char *label)
1754 1.1 thorpej {
1755 1.7 thorpej if (rt->rt_metric != new->rts_metric) {
1756 1.1 thorpej /* Fix the kernel immediately if it seems the route
1757 1.1 thorpej * has gone bad, since there may be a working route that
1758 1.1 thorpej * aggregates this route.
1759 1.1 thorpej */
1760 1.7 thorpej if (new->rts_metric == HOPCNT_INFINITY) {
1761 1.1 thorpej need_kern.tv_sec = now.tv_sec;
1762 1.7 thorpej if (new->rts_time >= now.tv_sec - EXPIRE_TIME)
1763 1.7 thorpej new->rts_time = now.tv_sec - EXPIRE_TIME;
1764 1.3 christos }
1765 1.1 thorpej rt->rt_seqno = update_seqno;
1766 1.1 thorpej set_need_flash();
1767 1.1 thorpej }
1768 1.1 thorpej
1769 1.7 thorpej if (rt->rt_gate != new->rts_gate) {
1770 1.1 thorpej need_kern.tv_sec = now.tv_sec;
1771 1.1 thorpej rt->rt_seqno = update_seqno;
1772 1.1 thorpej set_need_flash();
1773 1.1 thorpej }
1774 1.1 thorpej
1775 1.1 thorpej state |= (rt->rt_state & RS_SUBNET);
1776 1.1 thorpej
1777 1.3 christos /* Keep various things from deciding ageless routes are stale.
1778 1.3 christos */
1779 1.7 thorpej if (!AGE_RT(state, new->rts_ifp))
1780 1.7 thorpej new->rts_time = now.tv_sec;
1781 1.3 christos
1782 1.1 thorpej if (TRACEACTIONS)
1783 1.7 thorpej trace_change(rt, state, new,
1784 1.1 thorpej label ? label : "Chg ");
1785 1.1 thorpej
1786 1.1 thorpej rt->rt_state = state;
1787 1.7 thorpej rt->rt_spares[0] = *new;
1788 1.1 thorpej }
1789 1.1 thorpej
1790 1.1 thorpej
1791 1.1 thorpej /* check for a better route among the spares
1792 1.1 thorpej */
1793 1.1 thorpej static struct rt_spare *
1794 1.1 thorpej rts_better(struct rt_entry *rt)
1795 1.1 thorpej {
1796 1.1 thorpej struct rt_spare *rts, *rts1;
1797 1.1 thorpej int i;
1798 1.1 thorpej
1799 1.1 thorpej /* find the best alternative among the spares */
1800 1.1 thorpej rts = rt->rt_spares+1;
1801 1.1 thorpej for (i = NUM_SPARES, rts1 = rts+1; i > 2; i--, rts1++) {
1802 1.1 thorpej if (BETTER_LINK(rt,rts1,rts))
1803 1.1 thorpej rts = rts1;
1804 1.1 thorpej }
1805 1.1 thorpej
1806 1.1 thorpej return rts;
1807 1.1 thorpej }
1808 1.1 thorpej
1809 1.1 thorpej
1810 1.1 thorpej /* switch to a backup route
1811 1.1 thorpej */
1812 1.1 thorpej void
1813 1.1 thorpej rtswitch(struct rt_entry *rt,
1814 1.1 thorpej struct rt_spare *rts)
1815 1.1 thorpej {
1816 1.1 thorpej struct rt_spare swap;
1817 1.1 thorpej char label[10];
1818 1.1 thorpej
1819 1.1 thorpej
1820 1.1 thorpej /* Do not change permanent routes */
1821 1.3 christos if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | RS_RDISC
1822 1.3 christos | RS_NET_SYN | RS_IF)))
1823 1.1 thorpej return;
1824 1.1 thorpej
1825 1.1 thorpej /* find the best alternative among the spares */
1826 1.1 thorpej if (rts == 0)
1827 1.1 thorpej rts = rts_better(rt);
1828 1.1 thorpej
1829 1.1 thorpej /* Do not bother if it is not worthwhile.
1830 1.1 thorpej */
1831 1.1 thorpej if (!BETTER_LINK(rt, rts, rt->rt_spares))
1832 1.1 thorpej return;
1833 1.1 thorpej
1834 1.1 thorpej swap = rt->rt_spares[0];
1835 1.6 mrg (void)sprintf(label, "Use #%d", (int)(rts - rt->rt_spares));
1836 1.7 thorpej rtchange(rt, rt->rt_state & ~(RS_NET_SYN | RS_RDISC), rts, label);
1837 1.4 christos if (swap.rts_metric == HOPCNT_INFINITY) {
1838 1.4 christos *rts = rts_empty;
1839 1.4 christos } else {
1840 1.4 christos *rts = swap;
1841 1.4 christos }
1842 1.1 thorpej }
1843 1.1 thorpej
1844 1.1 thorpej
1845 1.1 thorpej void
1846 1.1 thorpej rtdelete(struct rt_entry *rt)
1847 1.1 thorpej {
1848 1.1 thorpej struct khash *k;
1849 1.1 thorpej
1850 1.1 thorpej
1851 1.1 thorpej if (TRACEACTIONS)
1852 1.1 thorpej trace_add_del("Del", rt);
1853 1.1 thorpej
1854 1.1 thorpej k = kern_find(rt->rt_dst, rt->rt_mask, 0);
1855 1.1 thorpej if (k != 0) {
1856 1.1 thorpej k->k_state |= KS_DELETE;
1857 1.1 thorpej need_kern.tv_sec = now.tv_sec;
1858 1.1 thorpej }
1859 1.1 thorpej
1860 1.1 thorpej dst_sock.sin_addr.s_addr = rt->rt_dst;
1861 1.13 christos mask_sock.sin_addr.s_addr = htonl(rt->rt_mask);
1862 1.1 thorpej masktrim(&mask_sock);
1863 1.1 thorpej if (rt != (struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock,
1864 1.1 thorpej rhead)) {
1865 1.1 thorpej msglog("rnh_deladdr() failed");
1866 1.1 thorpej } else {
1867 1.1 thorpej free(rt);
1868 1.3 christos total_routes--;
1869 1.1 thorpej }
1870 1.1 thorpej }
1871 1.1 thorpej
1872 1.1 thorpej
1873 1.4 christos void
1874 1.4 christos rts_delete(struct rt_entry *rt,
1875 1.4 christos struct rt_spare *rts)
1876 1.4 christos {
1877 1.7 thorpej trace_upslot(rt, rts, &rts_empty);
1878 1.4 christos *rts = rts_empty;
1879 1.4 christos }
1880 1.4 christos
1881 1.4 christos
1882 1.1 thorpej /* Get rid of a bad route, and try to switch to a replacement.
1883 1.1 thorpej */
1884 1.1 thorpej void
1885 1.1 thorpej rtbad(struct rt_entry *rt)
1886 1.1 thorpej {
1887 1.7 thorpej struct rt_spare new;
1888 1.7 thorpej
1889 1.1 thorpej /* Poison the route */
1890 1.7 thorpej new = rt->rt_spares[0];
1891 1.7 thorpej new.rts_metric = HOPCNT_INFINITY;
1892 1.7 thorpej rtchange(rt, rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC), &new, 0);
1893 1.1 thorpej rtswitch(rt, 0);
1894 1.1 thorpej }
1895 1.1 thorpej
1896 1.1 thorpej
1897 1.1 thorpej /* Junk a RS_NET_SYN or RS_LOCAL route,
1898 1.1 thorpej * unless it is needed by another interface.
1899 1.1 thorpej */
1900 1.1 thorpej void
1901 1.1 thorpej rtbad_sub(struct rt_entry *rt)
1902 1.1 thorpej {
1903 1.1 thorpej struct interface *ifp, *ifp1;
1904 1.1 thorpej struct intnet *intnetp;
1905 1.1 thorpej u_int state;
1906 1.1 thorpej
1907 1.1 thorpej
1908 1.1 thorpej ifp1 = 0;
1909 1.1 thorpej state = 0;
1910 1.1 thorpej
1911 1.1 thorpej if (rt->rt_state & RS_LOCAL) {
1912 1.1 thorpej /* Is this the route through loopback for the interface?
1913 1.1 thorpej * If so, see if it is used by any other interfaces, such
1914 1.1 thorpej * as a point-to-point interface with the same local address.
1915 1.1 thorpej */
1916 1.1 thorpej for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1917 1.1 thorpej /* Retain it if another interface needs it.
1918 1.1 thorpej */
1919 1.1 thorpej if (ifp->int_addr == rt->rt_ifp->int_addr) {
1920 1.1 thorpej state |= RS_LOCAL;
1921 1.1 thorpej ifp1 = ifp;
1922 1.1 thorpej break;
1923 1.1 thorpej }
1924 1.1 thorpej }
1925 1.1 thorpej
1926 1.1 thorpej }
1927 1.1 thorpej
1928 1.1 thorpej if (!(state & RS_LOCAL)) {
1929 1.1 thorpej /* Retain RIPv1 logical network route if there is another
1930 1.1 thorpej * interface that justifies it.
1931 1.1 thorpej */
1932 1.1 thorpej if (rt->rt_state & RS_NET_SYN) {
1933 1.1 thorpej for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1934 1.1 thorpej if ((ifp->int_state & IS_NEED_NET_SYN)
1935 1.1 thorpej && rt->rt_mask == ifp->int_std_mask
1936 1.1 thorpej && rt->rt_dst == ifp->int_std_addr) {
1937 1.1 thorpej state |= RS_NET_SYN;
1938 1.1 thorpej ifp1 = ifp;
1939 1.1 thorpej break;
1940 1.1 thorpej }
1941 1.1 thorpej }
1942 1.1 thorpej }
1943 1.1 thorpej
1944 1.1 thorpej /* or if there is an authority route that needs it. */
1945 1.1 thorpej for (intnetp = intnets;
1946 1.1 thorpej intnetp != 0;
1947 1.1 thorpej intnetp = intnetp->intnet_next) {
1948 1.1 thorpej if (intnetp->intnet_addr == rt->rt_dst
1949 1.1 thorpej && intnetp->intnet_mask == rt->rt_mask) {
1950 1.1 thorpej state |= (RS_NET_SYN | RS_NET_INT);
1951 1.1 thorpej break;
1952 1.1 thorpej }
1953 1.1 thorpej }
1954 1.1 thorpej }
1955 1.1 thorpej
1956 1.1 thorpej if (ifp1 != 0 || (state & RS_NET_SYN)) {
1957 1.7 thorpej struct rt_spare new = rt->rt_spares[0];
1958 1.7 thorpej new.rts_ifp = ifp1;
1959 1.7 thorpej rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state),
1960 1.7 thorpej &new, 0);
1961 1.1 thorpej } else {
1962 1.1 thorpej rtbad(rt);
1963 1.1 thorpej }
1964 1.1 thorpej }
1965 1.1 thorpej
1966 1.1 thorpej
1967 1.1 thorpej /* Called while walking the table looking for sick interfaces
1968 1.1 thorpej * or after a time change.
1969 1.1 thorpej */
1970 1.1 thorpej /* ARGSUSED */
1971 1.1 thorpej int
1972 1.8 christos walk_bad(struct radix_node *rn,
1973 1.9 christos struct walkarg *argp UNUSED)
1974 1.1 thorpej {
1975 1.1 thorpej #define RT ((struct rt_entry *)rn)
1976 1.1 thorpej struct rt_spare *rts;
1977 1.1 thorpej int i;
1978 1.1 thorpej
1979 1.1 thorpej
1980 1.1 thorpej /* fix any spare routes through the interface
1981 1.1 thorpej */
1982 1.1 thorpej rts = RT->rt_spares;
1983 1.1 thorpej for (i = NUM_SPARES; i != 1; i--) {
1984 1.1 thorpej rts++;
1985 1.4 christos if (rts->rts_metric < HOPCNT_INFINITY
1986 1.4 christos && (rts->rts_ifp == 0
1987 1.4 christos || (rts->rts_ifp->int_state & IS_BROKE)))
1988 1.4 christos rts_delete(RT, rts);
1989 1.1 thorpej }
1990 1.1 thorpej
1991 1.1 thorpej /* Deal with the main route
1992 1.1 thorpej */
1993 1.1 thorpej /* finished if it has been handled before or if its interface is ok
1994 1.1 thorpej */
1995 1.1 thorpej if (RT->rt_ifp == 0 || !(RT->rt_ifp->int_state & IS_BROKE))
1996 1.1 thorpej return 0;
1997 1.1 thorpej
1998 1.1 thorpej /* Bad routes for other than interfaces are easy.
1999 1.1 thorpej */
2000 1.1 thorpej if (0 == (RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) {
2001 1.1 thorpej rtbad(RT);
2002 1.1 thorpej return 0;
2003 1.1 thorpej }
2004 1.1 thorpej
2005 1.1 thorpej rtbad_sub(RT);
2006 1.1 thorpej return 0;
2007 1.1 thorpej #undef RT
2008 1.1 thorpej }
2009 1.1 thorpej
2010 1.1 thorpej
2011 1.1 thorpej /* Check the age of an individual route.
2012 1.1 thorpej */
2013 1.1 thorpej /* ARGSUSED */
2014 1.1 thorpej static int
2015 1.8 christos walk_age(struct radix_node *rn,
2016 1.9 christos struct walkarg *argp UNUSED)
2017 1.1 thorpej {
2018 1.1 thorpej #define RT ((struct rt_entry *)rn)
2019 1.1 thorpej struct interface *ifp;
2020 1.1 thorpej struct rt_spare *rts;
2021 1.1 thorpej int i;
2022 1.1 thorpej
2023 1.1 thorpej
2024 1.1 thorpej /* age all of the spare routes, including the primary route
2025 1.1 thorpej * currently in use
2026 1.1 thorpej */
2027 1.1 thorpej rts = RT->rt_spares;
2028 1.1 thorpej for (i = NUM_SPARES; i != 0; i--, rts++) {
2029 1.1 thorpej
2030 1.1 thorpej ifp = rts->rts_ifp;
2031 1.1 thorpej if (i == NUM_SPARES) {
2032 1.3 christos if (!AGE_RT(RT->rt_state, ifp)) {
2033 1.1 thorpej /* Keep various things from deciding ageless
2034 1.3 christos * routes are stale
2035 1.3 christos */
2036 1.1 thorpej rts->rts_time = now.tv_sec;
2037 1.1 thorpej continue;
2038 1.1 thorpej }
2039 1.1 thorpej
2040 1.1 thorpej /* forget RIP routes after RIP has been turned off.
2041 1.1 thorpej */
2042 1.1 thorpej if (rip_sock < 0) {
2043 1.1 thorpej rtdelete(RT);
2044 1.1 thorpej return 0;
2045 1.1 thorpej }
2046 1.1 thorpej }
2047 1.1 thorpej
2048 1.1 thorpej /* age failing routes
2049 1.1 thorpej */
2050 1.1 thorpej if (age_bad_gate == rts->rts_gate
2051 1.1 thorpej && rts->rts_time >= now_stale) {
2052 1.1 thorpej rts->rts_time -= SUPPLY_INTERVAL;
2053 1.1 thorpej }
2054 1.1 thorpej
2055 1.1 thorpej /* trash the spare routes when they go bad */
2056 1.1 thorpej if (rts->rts_metric < HOPCNT_INFINITY
2057 1.7 thorpej && now_garbage > rts->rts_time
2058 1.7 thorpej && i != NUM_SPARES)
2059 1.4 christos rts_delete(RT, rts);
2060 1.1 thorpej }
2061 1.1 thorpej
2062 1.1 thorpej
2063 1.1 thorpej /* finished if the active route is still fresh */
2064 1.1 thorpej if (now_stale <= RT->rt_time)
2065 1.1 thorpej return 0;
2066 1.1 thorpej
2067 1.1 thorpej /* try to switch to an alternative */
2068 1.1 thorpej rtswitch(RT, 0);
2069 1.1 thorpej
2070 1.1 thorpej /* Delete a dead route after it has been publically mourned. */
2071 1.1 thorpej if (now_garbage > RT->rt_time) {
2072 1.1 thorpej rtdelete(RT);
2073 1.1 thorpej return 0;
2074 1.1 thorpej }
2075 1.1 thorpej
2076 1.1 thorpej /* Start poisoning a bad route before deleting it. */
2077 1.7 thorpej if (now.tv_sec - RT->rt_time > EXPIRE_TIME) {
2078 1.7 thorpej struct rt_spare new = RT->rt_spares[0];
2079 1.7 thorpej new.rts_metric = HOPCNT_INFINITY;
2080 1.7 thorpej rtchange(RT, RT->rt_state, &new, 0);
2081 1.7 thorpej }
2082 1.1 thorpej return 0;
2083 1.1 thorpej }
2084 1.1 thorpej
2085 1.1 thorpej
2086 1.1 thorpej /* Watch for dead routes and interfaces.
2087 1.1 thorpej */
2088 1.1 thorpej void
2089 1.1 thorpej age(naddr bad_gate)
2090 1.1 thorpej {
2091 1.1 thorpej struct interface *ifp;
2092 1.4 christos int need_query = 0;
2093 1.1 thorpej
2094 1.4 christos /* If not listening to RIP, there is no need to age the routes in
2095 1.4 christos * the table.
2096 1.4 christos */
2097 1.4 christos age_timer.tv_sec = (now.tv_sec
2098 1.4 christos + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL));
2099 1.4 christos
2100 1.4 christos /* Check for dead IS_REMOTE interfaces by timing their
2101 1.4 christos * transmissions.
2102 1.4 christos */
2103 1.4 christos for (ifp = ifnet; ifp; ifp = ifp->int_next) {
2104 1.4 christos if (!(ifp->int_state & IS_REMOTE))
2105 1.4 christos continue;
2106 1.4 christos
2107 1.4 christos /* ignore unreachable remote interfaces */
2108 1.4 christos if (!check_remote(ifp))
2109 1.4 christos continue;
2110 1.7 thorpej
2111 1.4 christos /* Restore remote interface that has become reachable
2112 1.4 christos */
2113 1.4 christos if (ifp->int_state & IS_BROKE)
2114 1.4 christos if_ok(ifp, "remote ");
2115 1.1 thorpej
2116 1.4 christos if (ifp->int_act_time != NEVER
2117 1.4 christos && now.tv_sec - ifp->int_act_time > EXPIRE_TIME) {
2118 1.4 christos msglog("remote interface %s to %s timed out after"
2119 1.9 christos " %ld:%ld",
2120 1.4 christos ifp->int_name,
2121 1.4 christos naddr_ntoa(ifp->int_dstaddr),
2122 1.4 christos (now.tv_sec - ifp->int_act_time)/60,
2123 1.4 christos (now.tv_sec - ifp->int_act_time)%60);
2124 1.4 christos if_sick(ifp);
2125 1.4 christos }
2126 1.1 thorpej
2127 1.4 christos /* If we have not heard from the other router
2128 1.4 christos * recently, ask it.
2129 1.1 thorpej */
2130 1.4 christos if (now.tv_sec >= ifp->int_query_time) {
2131 1.4 christos ifp->int_query_time = NEVER;
2132 1.4 christos need_query = 1;
2133 1.1 thorpej }
2134 1.1 thorpej }
2135 1.1 thorpej
2136 1.1 thorpej /* Age routes. */
2137 1.1 thorpej age_bad_gate = bad_gate;
2138 1.1 thorpej (void)rn_walktree(rhead, walk_age, 0);
2139 1.7 thorpej
2140 1.7 thorpej /* delete old redirected routes to keep the kernel table small
2141 1.7 thorpej * and prevent blackholes
2142 1.7 thorpej */
2143 1.7 thorpej del_redirects(bad_gate, now.tv_sec-STALE_TIME);
2144 1.1 thorpej
2145 1.1 thorpej /* Update the kernel routing table. */
2146 1.1 thorpej fix_kern();
2147 1.4 christos
2148 1.4 christos /* poke reticent remote gateways */
2149 1.4 christos if (need_query)
2150 1.4 christos rip_query();
2151 1.1 thorpej }
2152