1 /*- 2 * Copyright 1994, 1995 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * This code does two things necessary for the enhanced TCP metrics to 32 * function in a useful manner: 33 * 1) It marks all non-host routes as `cloning', thus ensuring that 34 * every actual reference to such a route actually gets turned 35 * into a reference to a host route to the specific destination 36 * requested. 37 * 2) When such routes lose all their references, it arranges for them 38 * to be deleted in some random collection of circumstances, so that 39 * a large quantity of stale routing data is not kept in kernel memory 40 * indefinitely. See in_rtqtimo() below for the exact mechanism. 41 */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/sysctl.h> 50 #include <sys/socket.h> 51 #include <sys/mbuf.h> 52 #include <sys/syslog.h> 53 #include <sys/callout.h> 54 #include <sys/vimage.h> 55 56 #include <net/if.h> 57 #include <net/route.h> 58 #include <net/vnet.h> 59 60 #include <netinet/in.h> 61 #include <netinet/in_var.h> 62 #include <netinet/ip_var.h> 63 #include <netinet/vinet.h> 64 65 extern int in_inithead(void **head, int off); 66 67 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */ 68 69 /* 70 * Do what we need to do when inserting a route. 71 */ 72 static struct radix_node * 73 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 74 struct radix_node *treenodes) 75 { 76 struct rtentry *rt = (struct rtentry *)treenodes; 77 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt); 78 struct radix_node *ret; 79 80 /* 81 * A little bit of help for both IP output and input: 82 * For host routes, we make sure that RTF_BROADCAST 83 * is set for anything that looks like a broadcast address. 84 * This way, we can avoid an expensive call to in_broadcast() 85 * in ip_output() most of the time (because the route passed 86 * to ip_output() is almost always a host route). 87 * 88 * We also do the same for local addresses, with the thought 89 * that this might one day be used to speed up ip_input(). 90 * 91 * We also mark routes to multicast addresses as such, because 92 * it's easy to do and might be useful (but this is much more 93 * dubious since it's so easy to inspect the address). 94 */ 95 if (rt->rt_flags & RTF_HOST) { 96 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) { 97 rt->rt_flags |= RTF_BROADCAST; 98 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr == 99 sin->sin_addr.s_addr) { 100 rt->rt_flags |= RTF_LOCAL; 101 } 102 } 103 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 104 rt->rt_flags |= RTF_MULTICAST; 105 106 if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp) 107 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; 108 109 ret = rn_addroute(v_arg, n_arg, head, treenodes); 110 if (ret == NULL && rt->rt_flags & RTF_HOST) { 111 struct rtentry *rt2; 112 /* 113 * We are trying to add a host route, but can't. 114 * Find out if it is because of an 115 * ARP entry and delete it if so. 116 */ 117 rt2 = in_rtalloc1((struct sockaddr *)sin, 0, 118 RTF_CLONING, rt->rt_fibnum); 119 if (rt2) { 120 if (rt2->rt_flags & RTF_LLINFO && 121 rt2->rt_flags & RTF_HOST && 122 rt2->rt_gateway && 123 rt2->rt_gateway->sa_family == AF_LINK) { 124 rtexpunge(rt2); 125 RTFREE_LOCKED(rt2); 126 ret = rn_addroute(v_arg, n_arg, head, 127 treenodes); 128 } else 129 RTFREE_LOCKED(rt2); 130 } 131 } 132 133 return ret; 134 } 135 136 /* 137 * This code is the inverse of in_clsroute: on first reference, if we 138 * were managing the route, stop doing so and set the expiration timer 139 * back off again. 140 */ 141 static struct radix_node * 142 in_matroute(void *v_arg, struct radix_node_head *head) 143 { 144 struct radix_node *rn = rn_match(v_arg, head); 145 struct rtentry *rt = (struct rtentry *)rn; 146 147 /*XXX locking? */ 148 if (rt && rt->rt_refcnt == 0) { /* this is first reference */ 149 if (rt->rt_flags & RTPRF_OURS) { 150 rt->rt_flags &= ~RTPRF_OURS; 151 rt->rt_rmx.rmx_expire = 0; 152 } 153 } 154 return rn; 155 } 156 157 #ifdef VIMAGE_GLOBALS 158 static int rtq_reallyold; 159 static int rtq_minreallyold; 160 static int rtq_toomany; 161 #endif 162 163 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTEXPIRE, rtexpire, 164 CTLFLAG_RW, rtq_reallyold, 0, 165 "Default expiration time on dynamically learned routes"); 166 167 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTMINEXPIRE, 168 rtminexpire, CTLFLAG_RW, rtq_minreallyold, 0, 169 "Minimum time to attempt to hold onto dynamically learned routes"); 170 171 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTMAXCACHE, 172 rtmaxcache, CTLFLAG_RW, rtq_toomany, 0, 173 "Upper limit on dynamically learned routes"); 174 175 /* 176 * On last reference drop, mark the route as belong to us so that it can be 177 * timed out. 178 */ 179 static void 180 in_clsroute(struct radix_node *rn, struct radix_node_head *head) 181 { 182 INIT_VNET_INET(curvnet); 183 struct rtentry *rt = (struct rtentry *)rn; 184 185 RT_LOCK_ASSERT(rt); 186 187 if (!(rt->rt_flags & RTF_UP)) 188 return; /* prophylactic measures */ 189 190 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) 191 return; 192 193 if (rt->rt_flags & RTPRF_OURS) 194 return; 195 196 if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC))) 197 return; 198 199 /* 200 * If rtq_reallyold is 0, just delete the route without 201 * waiting for a timeout cycle to kill it. 202 */ 203 if (V_rtq_reallyold != 0) { 204 rt->rt_flags |= RTPRF_OURS; 205 rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold; 206 } else { 207 rtexpunge(rt); 208 } 209 } 210 211 struct rtqk_arg { 212 struct radix_node_head *rnh; 213 int draining; 214 int killed; 215 int found; 216 int updating; 217 time_t nextstop; 218 }; 219 220 /* 221 * Get rid of old routes. When draining, this deletes everything, even when 222 * the timeout is not expired yet. When updating, this makes sure that 223 * nothing has a timeout longer than the current value of rtq_reallyold. 224 */ 225 static int 226 in_rtqkill(struct radix_node *rn, void *rock) 227 { 228 INIT_VNET_INET(curvnet); 229 struct rtqk_arg *ap = rock; 230 struct rtentry *rt = (struct rtentry *)rn; 231 int err; 232 233 if (rt->rt_flags & RTPRF_OURS) { 234 ap->found++; 235 236 if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) { 237 if (rt->rt_refcnt > 0) 238 panic("rtqkill route really not free"); 239 240 err = in_rtrequest(RTM_DELETE, 241 (struct sockaddr *)rt_key(rt), 242 rt->rt_gateway, rt_mask(rt), 243 rt->rt_flags, 0, rt->rt_fibnum); 244 if (err) { 245 log(LOG_WARNING, "in_rtqkill: error %d\n", err); 246 } else { 247 ap->killed++; 248 } 249 } else { 250 if (ap->updating && 251 (rt->rt_rmx.rmx_expire - time_uptime > 252 V_rtq_reallyold)) { 253 rt->rt_rmx.rmx_expire = 254 time_uptime + V_rtq_reallyold; 255 } 256 ap->nextstop = lmin(ap->nextstop, 257 rt->rt_rmx.rmx_expire); 258 } 259 } 260 261 return 0; 262 } 263 264 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ 265 #ifdef VIMAGE_GLOBALS 266 static int rtq_timeout; 267 static struct callout rtq_timer; 268 #endif 269 270 static void in_rtqtimo_one(void *rock); 271 272 static void 273 in_rtqtimo(void *rock) 274 { 275 int fibnum; 276 void *newrock; 277 struct timeval atv; 278 279 KASSERT((rock == (void *)V_rt_tables[0][AF_INET]), 280 ("in_rtqtimo: unexpected arg")); 281 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 282 if ((newrock = V_rt_tables[fibnum][AF_INET]) != NULL) 283 in_rtqtimo_one(newrock); 284 } 285 atv.tv_usec = 0; 286 atv.tv_sec = V_rtq_timeout; 287 callout_reset(&V_rtq_timer, tvtohz(&atv), in_rtqtimo, rock); 288 } 289 290 static void 291 in_rtqtimo_one(void *rock) 292 { 293 INIT_VNET_INET(curvnet); 294 struct radix_node_head *rnh = rock; 295 struct rtqk_arg arg; 296 static time_t last_adjusted_timeout = 0; 297 298 arg.found = arg.killed = 0; 299 arg.rnh = rnh; 300 arg.nextstop = time_uptime + V_rtq_timeout; 301 arg.draining = arg.updating = 0; 302 RADIX_NODE_HEAD_LOCK(rnh); 303 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 304 RADIX_NODE_HEAD_UNLOCK(rnh); 305 306 /* 307 * Attempt to be somewhat dynamic about this: 308 * If there are ``too many'' routes sitting around taking up space, 309 * then crank down the timeout, and see if we can't make some more 310 * go away. However, we make sure that we will never adjust more 311 * than once in rtq_timeout seconds, to keep from cranking down too 312 * hard. 313 */ 314 if ((arg.found - arg.killed > V_rtq_toomany) && 315 (time_uptime - last_adjusted_timeout >= V_rtq_timeout) && 316 V_rtq_reallyold > V_rtq_minreallyold) { 317 V_rtq_reallyold = 2 * V_rtq_reallyold / 3; 318 if (V_rtq_reallyold < V_rtq_minreallyold) { 319 V_rtq_reallyold = V_rtq_minreallyold; 320 } 321 322 last_adjusted_timeout = time_uptime; 323 #ifdef DIAGNOSTIC 324 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", 325 V_rtq_reallyold); 326 #endif 327 arg.found = arg.killed = 0; 328 arg.updating = 1; 329 RADIX_NODE_HEAD_LOCK(rnh); 330 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 331 RADIX_NODE_HEAD_UNLOCK(rnh); 332 } 333 334 } 335 336 void 337 in_rtqdrain(void) 338 { 339 VNET_ITERATOR_DECL(vnet_iter); 340 struct radix_node_head *rnh; 341 struct rtqk_arg arg; 342 int fibnum; 343 344 VNET_LIST_RLOCK(); 345 VNET_FOREACH(vnet_iter) { 346 CURVNET_SET(vnet_iter); 347 INIT_VNET_NET(vnet_iter); 348 349 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 350 rnh = V_rt_tables[fibnum][AF_INET]; 351 arg.found = arg.killed = 0; 352 arg.rnh = rnh; 353 arg.nextstop = 0; 354 arg.draining = 1; 355 arg.updating = 0; 356 RADIX_NODE_HEAD_LOCK(rnh); 357 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 358 RADIX_NODE_HEAD_UNLOCK(rnh); 359 } 360 CURVNET_RESTORE(); 361 } 362 VNET_LIST_RUNLOCK(); 363 } 364 365 static int _in_rt_was_here; 366 /* 367 * Initialize our routing tree. 368 */ 369 int 370 in_inithead(void **head, int off) 371 { 372 INIT_VNET_INET(curvnet); 373 struct radix_node_head *rnh; 374 375 /* XXX MRT 376 * This can be called from vfs_export.c too in which case 'off' 377 * will be 0. We know the correct value so just use that and 378 * return directly if it was 0. 379 * This is a hack that replaces an even worse hack on a bad hack 380 * on a bad design. After RELENG_7 this should be fixed but that 381 * will change the ABI, so for now do it this way. 382 */ 383 if (!rn_inithead(head, 32)) 384 return 0; 385 386 if (off == 0) /* XXX MRT see above */ 387 return 1; /* only do the rest for a real routing table */ 388 389 V_rtq_reallyold = 60*60; /* one hour is "really old" */ 390 V_rtq_minreallyold = 10; /* never automatically crank down to less */ 391 V_rtq_toomany = 128; /* 128 cached routes is "too many" */ 392 V_rtq_timeout = RTQ_TIMEOUT; 393 394 rnh = *head; 395 rnh->rnh_addaddr = in_addroute; 396 rnh->rnh_matchaddr = in_matroute; 397 rnh->rnh_close = in_clsroute; 398 if (_in_rt_was_here == 0 ) { 399 callout_init(&V_rtq_timer, CALLOUT_MPSAFE); 400 in_rtqtimo(rnh); /* kick off timeout first time */ 401 _in_rt_was_here = 1; 402 } 403 return 1; 404 } 405 406 /* 407 * This zaps old routes when the interface goes down or interface 408 * address is deleted. In the latter case, it deletes static routes 409 * that point to this address. If we don't do this, we may end up 410 * using the old address in the future. The ones we always want to 411 * get rid of are things like ARP entries, since the user might down 412 * the interface, walk over to a completely different network, and 413 * plug back in. 414 */ 415 struct in_ifadown_arg { 416 struct ifaddr *ifa; 417 int del; 418 }; 419 420 static int 421 in_ifadownkill(struct radix_node *rn, void *xap) 422 { 423 struct in_ifadown_arg *ap = xap; 424 struct rtentry *rt = (struct rtentry *)rn; 425 426 RT_LOCK(rt); 427 if (rt->rt_ifa == ap->ifa && 428 (ap->del || !(rt->rt_flags & RTF_STATIC))) { 429 /* 430 * We need to disable the automatic prune that happens 431 * in this case in rtrequest() because it will blow 432 * away the pointers that rn_walktree() needs in order 433 * continue our descent. We will end up deleting all 434 * the routes that rtrequest() would have in any case, 435 * so that behavior is not needed there. 436 */ 437 rt->rt_flags &= ~RTF_CLONING; 438 rtexpunge(rt); 439 } 440 RT_UNLOCK(rt); 441 return 0; 442 } 443 444 int 445 in_ifadown(struct ifaddr *ifa, int delete) 446 { 447 INIT_VNET_NET(curvnet); 448 struct in_ifadown_arg arg; 449 struct radix_node_head *rnh; 450 int fibnum; 451 452 if (ifa->ifa_addr->sa_family != AF_INET) 453 return 1; 454 455 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 456 rnh = V_rt_tables[fibnum][AF_INET]; 457 arg.ifa = ifa; 458 arg.del = delete; 459 RADIX_NODE_HEAD_LOCK(rnh); 460 rnh->rnh_walktree(rnh, in_ifadownkill, &arg); 461 RADIX_NODE_HEAD_UNLOCK(rnh); 462 ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */ 463 } 464 return 0; 465 } 466 467 /* 468 * inet versions of rt functions. These have fib extensions and 469 * for now will just reference the _fib variants. 470 * eventually this order will be reversed, 471 */ 472 void 473 in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum) 474 { 475 rtalloc_ign_fib(ro, ignflags, fibnum); 476 } 477 478 int 479 in_rtrequest( int req, 480 struct sockaddr *dst, 481 struct sockaddr *gateway, 482 struct sockaddr *netmask, 483 int flags, 484 struct rtentry **ret_nrt, 485 u_int fibnum) 486 { 487 return (rtrequest_fib(req, dst, gateway, netmask, 488 flags, ret_nrt, fibnum)); 489 } 490 491 struct rtentry * 492 in_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 493 { 494 return (rtalloc1_fib(dst, report, ignflags, fibnum)); 495 } 496 497 void 498 in_rtredirect(struct sockaddr *dst, 499 struct sockaddr *gateway, 500 struct sockaddr *netmask, 501 int flags, 502 struct sockaddr *src, 503 u_int fibnum) 504 { 505 rtredirect_fib(dst, gateway, netmask, flags, src, fibnum); 506 } 507 508 void 509 in_rtalloc(struct route *ro, u_int fibnum) 510 { 511 rtalloc_ign_fib(ro, 0UL, fibnum); 512 } 513 514 #if 0 515 int in_rt_getifa(struct rt_addrinfo *, u_int fibnum); 516 int in_rtioctl(u_long, caddr_t, u_int); 517 int in_rtrequest1(int, struct rt_addrinfo *, struct rtentry **, u_int); 518 #endif 519 520 521