1 /* $FreeBSD: releng/5.4/sys/netinet6/in6_rmx.c 141090 2005-01-31 23:27:04Z imp $ */
2 /* $KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $ */
3
4 /*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright 1994, 1995 Massachusetts Institute of Technology
35 *
36 * Permission to use, copy, modify, and distribute this software and
37 * its documentation for any purpose and without fee is hereby
38 * granted, provided that both the above copyright notice and this
39 * permission notice appear in all copies, that both the above
40 * copyright notice and this permission notice appear in all
41 * supporting documentation, and that the name of M.I.T. not be used
42 * in advertising or publicity pertaining to distribution of the
43 * software without specific, written prior permission. M.I.T. makes
44 * no representations about the suitability of this software for any
45 * purpose. It is provided "as is" without express or implied
46 * warranty.
47 *
48 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
49 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
50 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
51 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
52 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
55 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
58 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 */
62
63 /*
64 * This code does two things necessary for the enhanced TCP metrics to
65 * function in a useful manner:
66 * 1) It marks all non-host routes as `cloning', thus ensuring that
67 * every actual reference to such a route actually gets turned
68 * into a reference to a host route to the specific destination
69 * requested.
70 * 2) When such routes lose all their references, it arranges for them
71 * to be deleted in some random collection of circumstances, so that
72 * a large quantity of stale routing data is not kept in kernel memory
73 * indefinitely. See in6_rtqtimo() below for the exact mechanism.
74 */
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/queue.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/mbuf.h>
84 #include <sys/syslog.h>
85 #include <sys/callout.h>
86
87 #include <net/if.h>
88 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/in_var.h>
92
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95
96 #include <netinet/icmp6.h>
97 #include <netinet6/nd6.h>
98
99 #include <netinet/tcp.h>
100 #include <netinet/tcp_seq.h>
101 #include <netinet/tcp_timer.h>
102 #include <netinet/tcp_var.h>
103
104 extern int in6_inithead __P((void **head, int off));
105
106 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
107
108 /*
109 * Do what we need to do when inserting a route.
110 */
111 static struct radix_node *
112 in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
113 struct radix_node *treenodes)
114 {
115 struct rtentry *rt = (struct rtentry *)treenodes;
116 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
117 struct radix_node *ret;
118
119 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
120 rt->rt_flags |= RTF_MULTICAST;
121
122 /*
123 * A little bit of help for both IPv6 output and input:
124 * For local addresses, we make sure that RTF_LOCAL is set,
125 * with the thought that this might one day be used to speed up
126 * ip_input().
127 *
128 * We also mark routes to multicast addresses as such, because
129 * it's easy to do and might be useful (but this is much more
130 * dubious since it's so easy to inspect the address). (This
131 * is done above.)
132 *
133 * XXX
134 * should elaborate the code.
135 */
136 if (rt->rt_flags & RTF_HOST) {
137 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
138 ->sin6_addr,
139 &sin6->sin6_addr)) {
140 rt->rt_flags |= RTF_LOCAL;
141 }
142 }
143
144 if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
145 rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
146
147 ret = rn_addroute(v_arg, n_arg, head, treenodes);
148 if (ret == NULL && rt->rt_flags & RTF_HOST) {
149 struct rtentry *rt2;
150 /*
151 * We are trying to add a host route, but can't.
152 * Find out if it is because of an
153 * ARP entry and delete it if so.
154 */
155 rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
156 if (rt2) {
157 if (rt2->rt_flags & RTF_LLINFO &&
158 rt2->rt_flags & RTF_HOST &&
159 rt2->rt_gateway &&
160 rt2->rt_gateway->sa_family == AF_LINK) {
161 rtexpunge(rt2);
162 RTFREE_LOCKED(rt2);
163 ret = rn_addroute(v_arg, n_arg, head,
164 treenodes);
165 } else
166 RTFREE_LOCKED(rt2);
167 }
168 } else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
169 struct rtentry *rt2;
170 /*
171 * We are trying to add a net route, but can't.
172 * The following case should be allowed, so we'll make a
173 * special check for this:
174 * Two IPv6 addresses with the same prefix is assigned
175 * to a single interrface.
176 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
177 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
178 * In this case, (*1) and (*2) want to add the same
179 * net route entry, 3ffe:0501:: -> if0.
180 * This case should not raise an error.
181 */
182 rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
183 if (rt2) {
184 if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
185 == RTF_CLONING
186 && rt2->rt_gateway
187 && rt2->rt_gateway->sa_family == AF_LINK
188 && rt2->rt_ifp == rt->rt_ifp) {
189 ret = rt2->rt_nodes;
190 }
191 RTFREE_LOCKED(rt2);
192 }
193 }
194 return ret;
195 }
196
197 /*
198 * This code is the inverse of in6_clsroute: on first reference, if we
199 * were managing the route, stop doing so and set the expiration timer
200 * back off again.
201 */
202 static struct radix_node *
203 in6_matroute(void *v_arg, struct radix_node_head *head)
204 {
205 struct radix_node *rn = rn_match(v_arg, head);
206 struct rtentry *rt = (struct rtentry *)rn;
207
208 if (rt && rt->rt_refcnt == 0) { /* this is first reference */
209 if (rt->rt_flags & RTPRF_OURS) {
210 rt->rt_flags &= ~RTPRF_OURS;
211 rt->rt_rmx.rmx_expire = 0;
212 }
213 }
214 return rn;
215 }
216
217 SYSCTL_DECL(_net_inet6_ip6);
218
219 static int rtq_reallyold = 60*60;
220 /* one hour is ``really old'' */
221 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
222 CTLFLAG_RW, &rtq_reallyold , 0, "");
223
224 static int rtq_minreallyold = 10;
225 /* never automatically crank down to less */
226 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
227 CTLFLAG_RW, &rtq_minreallyold , 0, "");
228
229 static int rtq_toomany = 128;
230 /* 128 cached routes is ``too many'' */
231 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
232 CTLFLAG_RW, &rtq_toomany , 0, "");
233
234
235 /*
236 * On last reference drop, mark the route as belong to us so that it can be
237 * timed out.
238 */
239 static void
240 in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
241 {
242 struct rtentry *rt = (struct rtentry *)rn;
243
244 RT_LOCK_ASSERT(rt);
245
246 if (!(rt->rt_flags & RTF_UP))
247 return; /* prophylactic measures */
248
249 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
250 return;
251
252 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
253 return;
254
255 /*
256 * As requested by David Greenman:
257 * If rtq_reallyold is 0, just delete the route without
258 * waiting for a timeout cycle to kill it.
259 */
260 if (rtq_reallyold != 0) {
261 rt->rt_flags |= RTPRF_OURS;
262 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
263 } else {
264 rtexpunge(rt);
265 }
266 }
267
268 struct rtqk_arg {
269 struct radix_node_head *rnh;
270 int mode;
271 int updating;
272 int draining;
273 int killed;
274 int found;
275 time_t nextstop;
276 };
277
278 /*
279 * Get rid of old routes. When draining, this deletes everything, even when
280 * the timeout is not expired yet. When updating, this makes sure that
281 * nothing has a timeout longer than the current value of rtq_reallyold.
282 */
283 static int
284 in6_rtqkill(struct radix_node *rn, void *rock)
285 {
286 struct rtqk_arg *ap = rock;
287 struct rtentry *rt = (struct rtentry *)rn;
288 int err;
289
290 if (rt->rt_flags & RTPRF_OURS) {
291 ap->found++;
292
293 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
294 if (rt->rt_refcnt > 0)
295 panic("rtqkill route really not free");
296
297 err = rtrequest(RTM_DELETE,
298 (struct sockaddr *)rt_key(rt),
299 rt->rt_gateway, rt_mask(rt),
300 rt->rt_flags, 0);
301 if (err) {
302 log(LOG_WARNING, "in6_rtqkill: error %d", err);
303 } else {
304 ap->killed++;
305 }
306 } else {
307 if (ap->updating
308 && (rt->rt_rmx.rmx_expire - time_second
309 > rtq_reallyold)) {
310 rt->rt_rmx.rmx_expire = time_second
311 + rtq_reallyold;
312 }
313 ap->nextstop = lmin(ap->nextstop,
314 rt->rt_rmx.rmx_expire);
315 }
316 }
317
318 return 0;
319 }
320
321 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
322 static int rtq_timeout = RTQ_TIMEOUT;
323 static struct callout rtq_timer;
324
325 static void
326 in6_rtqtimo(void *rock)
327 {
328 struct radix_node_head *rnh = rock;
329 struct rtqk_arg arg;
330 struct timeval atv;
331 static time_t last_adjusted_timeout = 0;
332
333 arg.found = arg.killed = 0;
334 arg.rnh = rnh;
335 arg.nextstop = time_second + rtq_timeout;
336 arg.draining = arg.updating = 0;
337 RADIX_NODE_HEAD_LOCK(rnh);
338 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
339 RADIX_NODE_HEAD_UNLOCK(rnh);
340
341 /*
342 * Attempt to be somewhat dynamic about this:
343 * If there are ``too many'' routes sitting around taking up space,
344 * then crank down the timeout, and see if we can't make some more
345 * go away. However, we make sure that we will never adjust more
346 * than once in rtq_timeout seconds, to keep from cranking down too
347 * hard.
348 */
349 if ((arg.found - arg.killed > rtq_toomany)
350 && (time_second - last_adjusted_timeout >= rtq_timeout)
351 && rtq_reallyold > rtq_minreallyold) {
352 rtq_reallyold = 2*rtq_reallyold / 3;
353 if (rtq_reallyold < rtq_minreallyold) {
354 rtq_reallyold = rtq_minreallyold;
355 }
356
357 last_adjusted_timeout = time_second;
358 #ifdef DIAGNOSTIC
359 log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
360 rtq_reallyold);
361 #endif
362 arg.found = arg.killed = 0;
363 arg.updating = 1;
364 RADIX_NODE_HEAD_LOCK(rnh);
365 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
366 RADIX_NODE_HEAD_UNLOCK(rnh);
367 }
368
369 atv.tv_usec = 0;
370 atv.tv_sec = arg.nextstop;
371 callout_reset(&rtq_timer, tvtohz(&atv), in6_rtqtimo, rock);
372 }
373
374 /*
375 * Age old PMTUs.
376 */
377 struct mtuex_arg {
378 struct radix_node_head *rnh;
379 time_t nextstop;
380 };
381 static struct callout rtq_mtutimer;
382
383 static int
384 in6_mtuexpire(struct radix_node *rn, void *rock)
385 {
386 struct rtentry *rt = (struct rtentry *)rn;
387 struct mtuex_arg *ap = rock;
388
389 /* sanity */
390 if (!rt)
391 panic("rt == NULL in in6_mtuexpire");
392
393 if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
394 if (rt->rt_rmx.rmx_expire <= time_second) {
395 rt->rt_flags |= RTF_PROBEMTU;
396 } else {
397 ap->nextstop = lmin(ap->nextstop,
398 rt->rt_rmx.rmx_expire);
399 }
400 }
401
402 return 0;
403 }
404
405 #define MTUTIMO_DEFAULT (60*1)
406
407 static void
408 in6_mtutimo(void *rock)
409 {
410 struct radix_node_head *rnh = rock;
411 struct mtuex_arg arg;
412 struct timeval atv;
413
414 arg.rnh = rnh;
415 arg.nextstop = time_second + MTUTIMO_DEFAULT;
416 RADIX_NODE_HEAD_LOCK(rnh);
417 rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
418 RADIX_NODE_HEAD_UNLOCK(rnh);
419
420 atv.tv_usec = 0;
421 atv.tv_sec = arg.nextstop;
422 if (atv.tv_sec < time_second) {
423 printf("invalid mtu expiration time on routing table\n");
424 arg.nextstop = time_second + 30; /* last resort */
425 }
426 callout_reset(&rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
427 }
428
429 #if 0
430 void
431 in6_rtqdrain()
432 {
433 struct radix_node_head *rnh = rt_tables[AF_INET6];
434 struct rtqk_arg arg;
435
436 arg.found = arg.killed = 0;
437 arg.rnh = rnh;
438 arg.nextstop = 0;
439 arg.draining = 1;
440 arg.updating = 0;
441 RADIX_NODE_HEAD_LOCK(rnh);
442 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
443 RADIX_NODE_HEAD_UNLOCK(rnh);
444 }
445 #endif
446
447 /*
448 * Initialize our routing tree.
449 */
450 int
451 in6_inithead(void **head, int off)
452 {
453 struct radix_node_head *rnh;
454
455 if (!rn_inithead(head, off))
456 return 0;
457
458 if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
459 return 1; /* only do this for the real routing table */
460
461 rnh = *head;
462 rnh->rnh_addaddr = in6_addroute;
463 rnh->rnh_matchaddr = in6_matroute;
464 rnh->rnh_close = in6_clsroute;
465 callout_init(&rtq_timer, CALLOUT_MPSAFE);
466 in6_rtqtimo(rnh); /* kick off timeout first time */
467 callout_init(&rtq_mtutimer, CALLOUT_MPSAFE);
468 in6_mtutimo(rnh); /* kick off timeout first time */
469 return 1;
470 }
Cache object: b8b3f827f0393d364a4155bab4b81964
|