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