in6_rmx.c revision 185571
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: head/sys/netinet6/in6_rmx.c 185571 2008-12-02 21:37:28Z bz $"); 77 78#include <sys/param.h> 79#include <sys/systm.h> 80#include <sys/kernel.h> 81#include <sys/sysctl.h> 82#include <sys/queue.h> 83#include <sys/socket.h> 84#include <sys/socketvar.h> 85#include <sys/mbuf.h> 86#include <sys/syslog.h> 87#include <sys/callout.h> 88#include <sys/vimage.h> 89 90#include <net/if.h> 91#include <net/route.h> 92#include <net/vnet.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#include <netinet6/vinet6.h> 104 105#include <netinet/tcp.h> 106#include <netinet/tcp_seq.h> 107#include <netinet/tcp_timer.h> 108#include <netinet/tcp_var.h> 109 110extern int in6_inithead(void **head, int off); 111 112#define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */ 113 114/* 115 * Do what we need to do when inserting a route. 116 */ 117static struct radix_node * 118in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 119 struct radix_node *treenodes) 120{ 121 struct rtentry *rt = (struct rtentry *)treenodes; 122 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt); 123 struct radix_node *ret; 124 125 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 126 rt->rt_flags |= RTF_MULTICAST; 127 128 /* 129 * A little bit of help for both IPv6 output and input: 130 * For local addresses, we make sure that RTF_LOCAL is set, 131 * with the thought that this might one day be used to speed up 132 * ip_input(). 133 * 134 * We also mark routes to multicast addresses as such, because 135 * it's easy to do and might be useful (but this is much more 136 * dubious since it's so easy to inspect the address). (This 137 * is done above.) 138 * 139 * XXX 140 * should elaborate the code. 141 */ 142 if (rt->rt_flags & RTF_HOST) { 143 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr) 144 ->sin6_addr, 145 &sin6->sin6_addr)) { 146 rt->rt_flags |= RTF_LOCAL; 147 } 148 } 149 150 if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp) 151 rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp); 152 153 ret = rn_addroute(v_arg, n_arg, head, treenodes); 154 if (ret == NULL && rt->rt_flags & RTF_HOST) { 155 struct rtentry *rt2; 156 /* 157 * We are trying to add a host route, but can't. 158 * Find out if it is because of an 159 * ARP entry and delete it if so. 160 */ 161 rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING); 162 if (rt2) { 163 if (rt2->rt_flags & RTF_LLINFO && 164 rt2->rt_flags & RTF_HOST && 165 rt2->rt_gateway && 166 rt2->rt_gateway->sa_family == AF_LINK) { 167 rtexpunge(rt2); 168 RTFREE_LOCKED(rt2); 169 ret = rn_addroute(v_arg, n_arg, head, 170 treenodes); 171 } else 172 RTFREE_LOCKED(rt2); 173 } 174 } else if (ret == NULL && rt->rt_flags & RTF_CLONING) { 175 struct rtentry *rt2; 176 /* 177 * We are trying to add a net route, but can't. 178 * The following case should be allowed, so we'll make a 179 * special check for this: 180 * Two IPv6 addresses with the same prefix is assigned 181 * to a single interrface. 182 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1) 183 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2) 184 * In this case, (*1) and (*2) want to add the same 185 * net route entry, 3ffe:0501:: -> if0. 186 * This case should not raise an error. 187 */ 188 rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING); 189 if (rt2) { 190 if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY)) 191 == RTF_CLONING 192 && rt2->rt_gateway 193 && rt2->rt_gateway->sa_family == AF_LINK 194 && rt2->rt_ifp == rt->rt_ifp) { 195 ret = rt2->rt_nodes; 196 } 197 RTFREE_LOCKED(rt2); 198 } 199 } 200 return ret; 201} 202 203/* 204 * This code is the inverse of in6_clsroute: on first reference, if we 205 * were managing the route, stop doing so and set the expiration timer 206 * back off again. 207 */ 208static struct radix_node * 209in6_matroute(void *v_arg, struct radix_node_head *head) 210{ 211 struct radix_node *rn = rn_match(v_arg, head); 212 struct rtentry *rt = (struct rtentry *)rn; 213 214 if (rt && rt->rt_refcnt == 0) { /* this is first reference */ 215 if (rt->rt_flags & RTPRF_OURS) { 216 rt->rt_flags &= ~RTPRF_OURS; 217 rt->rt_rmx.rmx_expire = 0; 218 } 219 } 220 return rn; 221} 222 223SYSCTL_DECL(_net_inet6_ip6); 224 225#ifdef VIMAGE_GLOBALS 226static int rtq_reallyold6; 227static int rtq_minreallyold6; 228static int rtq_toomany6; 229#endif 230 231SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTEXPIRE, 232 rtexpire, CTLFLAG_RW, rtq_reallyold6 , 0, ""); 233 234SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMINEXPIRE, 235 rtminexpire, CTLFLAG_RW, rtq_minreallyold6 , 0, ""); 236 237SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMAXCACHE, 238 rtmaxcache, CTLFLAG_RW, rtq_toomany6 , 0, ""); 239 240 241/* 242 * On last reference drop, mark the route as belong to us so that it can be 243 * timed out. 244 */ 245static void 246in6_clsroute(struct radix_node *rn, struct radix_node_head *head) 247{ 248 INIT_VNET_INET6(curvnet); 249 struct rtentry *rt = (struct rtentry *)rn; 250 251 RT_LOCK_ASSERT(rt); 252 253 if (!(rt->rt_flags & RTF_UP)) 254 return; /* prophylactic measures */ 255 256 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) 257 return; 258 259 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED) 260 return; 261 262 /* 263 * As requested by David Greenman: 264 * If rtq_reallyold6 is 0, just delete the route without 265 * waiting for a timeout cycle to kill it. 266 */ 267 if (V_rtq_reallyold6 != 0) { 268 rt->rt_flags |= RTPRF_OURS; 269 rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold6; 270 } else { 271 rtexpunge(rt); 272 } 273} 274 275struct rtqk_arg { 276 struct radix_node_head *rnh; 277 int mode; 278 int updating; 279 int draining; 280 int killed; 281 int found; 282 time_t nextstop; 283}; 284 285/* 286 * Get rid of old routes. When draining, this deletes everything, even when 287 * the timeout is not expired yet. When updating, this makes sure that 288 * nothing has a timeout longer than the current value of rtq_reallyold6. 289 */ 290static int 291in6_rtqkill(struct radix_node *rn, void *rock) 292{ 293 INIT_VNET_INET6(curvnet); 294 struct rtqk_arg *ap = rock; 295 struct rtentry *rt = (struct rtentry *)rn; 296 int err; 297 298 if (rt->rt_flags & RTPRF_OURS) { 299 ap->found++; 300 301 if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) { 302 if (rt->rt_refcnt > 0) 303 panic("rtqkill route really not free"); 304 305 err = rtrequest(RTM_DELETE, 306 (struct sockaddr *)rt_key(rt), 307 rt->rt_gateway, rt_mask(rt), 308 rt->rt_flags, 0); 309 if (err) { 310 log(LOG_WARNING, "in6_rtqkill: error %d", err); 311 } else { 312 ap->killed++; 313 } 314 } else { 315 if (ap->updating 316 && (rt->rt_rmx.rmx_expire - time_uptime 317 > V_rtq_reallyold6)) { 318 rt->rt_rmx.rmx_expire = time_uptime 319 + V_rtq_reallyold6; 320 } 321 ap->nextstop = lmin(ap->nextstop, 322 rt->rt_rmx.rmx_expire); 323 } 324 } 325 326 return 0; 327} 328 329#define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ 330#ifdef VIMAGE_GLOBALS 331static int rtq_timeout6; 332static struct callout rtq_timer6; 333#endif 334 335static void 336in6_rtqtimo(void *rock) 337{ 338 CURVNET_SET_QUIET((struct vnet *) rock); 339 INIT_VNET_NET((struct vnet *) rock); 340 INIT_VNET_INET6((struct vnet *) rock); 341 struct radix_node_head *rnh = rock; 342 struct rtqk_arg arg; 343 struct timeval atv; 344 static time_t last_adjusted_timeout = 0; 345 346 arg.found = arg.killed = 0; 347 arg.rnh = rnh; 348 arg.nextstop = time_uptime + V_rtq_timeout6; 349 arg.draining = arg.updating = 0; 350 RADIX_NODE_HEAD_LOCK(rnh); 351 rnh->rnh_walktree(rnh, in6_rtqkill, &arg); 352 RADIX_NODE_HEAD_UNLOCK(rnh); 353 354 /* 355 * Attempt to be somewhat dynamic about this: 356 * If there are ``too many'' routes sitting around taking up space, 357 * then crank down the timeout, and see if we can't make some more 358 * go away. However, we make sure that we will never adjust more 359 * than once in rtq_timeout6 seconds, to keep from cranking down too 360 * hard. 361 */ 362 if ((arg.found - arg.killed > V_rtq_toomany6) 363 && (time_uptime - last_adjusted_timeout >= V_rtq_timeout6) 364 && V_rtq_reallyold6 > V_rtq_minreallyold6) { 365 V_rtq_reallyold6 = 2*V_rtq_reallyold6 / 3; 366 if (V_rtq_reallyold6 < V_rtq_minreallyold6) { 367 V_rtq_reallyold6 = V_rtq_minreallyold6; 368 } 369 370 last_adjusted_timeout = time_uptime; 371#ifdef DIAGNOSTIC 372 log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold6 to %d", 373 V_rtq_reallyold6); 374#endif 375 arg.found = arg.killed = 0; 376 arg.updating = 1; 377 RADIX_NODE_HEAD_LOCK(rnh); 378 rnh->rnh_walktree(rnh, in6_rtqkill, &arg); 379 RADIX_NODE_HEAD_UNLOCK(rnh); 380 } 381 382 atv.tv_usec = 0; 383 atv.tv_sec = arg.nextstop - time_uptime; 384 callout_reset(&V_rtq_timer6, tvtohz(&atv), in6_rtqtimo, rock); 385 CURVNET_RESTORE(); 386} 387 388/* 389 * Age old PMTUs. 390 */ 391struct mtuex_arg { 392 struct radix_node_head *rnh; 393 time_t nextstop; 394}; 395#ifdef VIMAGE_GLOBALS 396static struct callout rtq_mtutimer; 397#endif 398 399static int 400in6_mtuexpire(struct radix_node *rn, void *rock) 401{ 402 struct rtentry *rt = (struct rtentry *)rn; 403 struct mtuex_arg *ap = rock; 404 405 /* sanity */ 406 if (!rt) 407 panic("rt == NULL in in6_mtuexpire"); 408 409 if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) { 410 if (rt->rt_rmx.rmx_expire <= time_uptime) { 411 rt->rt_flags |= RTF_PROBEMTU; 412 } else { 413 ap->nextstop = lmin(ap->nextstop, 414 rt->rt_rmx.rmx_expire); 415 } 416 } 417 418 return 0; 419} 420 421#define MTUTIMO_DEFAULT (60*1) 422 423static void 424in6_mtutimo(void *rock) 425{ 426 CURVNET_SET_QUIET((struct vnet *) rock); 427 INIT_VNET_NET((struct vnet *) rock); 428 INIT_VNET_INET6((struct vnet *) rock); 429 struct radix_node_head *rnh = rock; 430 struct mtuex_arg arg; 431 struct timeval atv; 432 433 arg.rnh = rnh; 434 arg.nextstop = time_uptime + MTUTIMO_DEFAULT; 435 RADIX_NODE_HEAD_LOCK(rnh); 436 rnh->rnh_walktree(rnh, in6_mtuexpire, &arg); 437 RADIX_NODE_HEAD_UNLOCK(rnh); 438 439 atv.tv_usec = 0; 440 atv.tv_sec = arg.nextstop - time_uptime; 441 if (atv.tv_sec < 0) { 442 printf("invalid mtu expiration time on routing table\n"); 443 arg.nextstop = time_uptime + 30; /* last resort */ 444 atv.tv_sec = 30; 445 } 446 callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock); 447 CURVNET_RESTORE(); 448} 449 450#if 0 451void 452in6_rtqdrain(void) 453{ 454 INIT_VNET_NET(curvnet); 455 struct radix_node_head *rnh = V_rt_tables[AF_INET6]; 456 struct rtqk_arg arg; 457 458 arg.found = arg.killed = 0; 459 arg.rnh = rnh; 460 arg.nextstop = 0; 461 arg.draining = 1; 462 arg.updating = 0; 463 RADIX_NODE_HEAD_LOCK(rnh); 464 rnh->rnh_walktree(rnh, in6_rtqkill, &arg); 465 RADIX_NODE_HEAD_UNLOCK(rnh); 466} 467#endif 468 469/* 470 * Initialize our routing tree. 471 * XXX MRT When off == 0, we are being called from vfs_export.c 472 * so just set up their table and leave. (we know what the correct 473 * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd 474 * see also comments in in_inithead() vfs_export.c and domain.h 475 */ 476int 477in6_inithead(void **head, int off) 478{ 479 INIT_VNET_INET6(curvnet); 480 struct radix_node_head *rnh; 481 482 if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3)) 483 return 0; /* See above */ 484 485 if (off == 0) /* See above */ 486 return 1; /* only do the rest for the real thing */ 487 488 V_rtq_reallyold6 = 60*60; /* one hour is ``really old'' */ 489 V_rtq_minreallyold6 = 10; /* never automatically crank down to less */ 490 V_rtq_toomany6 = 128; /* 128 cached routes is ``too many'' */ 491 V_rtq_timeout6 = RTQ_TIMEOUT; 492 493 rnh = *head; 494 rnh->rnh_addaddr = in6_addroute; 495 rnh->rnh_matchaddr = in6_matroute; 496 rnh->rnh_close = in6_clsroute; 497 callout_init(&V_rtq_timer6, CALLOUT_MPSAFE); 498 in6_rtqtimo(rnh); /* kick off timeout first time */ 499 callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE); 500 in6_mtutimo(rnh); /* kick off timeout first time */ 501 return 1; 502} 503