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