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 79 RADIX_NODE_HEAD_WLOCK_ASSERT(head); 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 return (rn_addroute(v_arg, n_arg, head, treenodes)); 110 } 111 112 /* 113 * This code is the inverse of in_clsroute: on first reference, if we 114 * were managing the route, stop doing so and set the expiration timer 115 * back off again. 116 */ 117 static struct radix_node * 118 in_matroute(void *v_arg, struct radix_node_head *head) 119 { 120 struct radix_node *rn = rn_match(v_arg, head); 121 struct rtentry *rt = (struct rtentry *)rn; 122 123 /*XXX locking? */ 124 if (rt && rt->rt_refcnt == 0) { /* this is first reference */ 125 if (rt->rt_flags & RTPRF_OURS) { 126 rt->rt_flags &= ~RTPRF_OURS; 127 rt->rt_rmx.rmx_expire = 0; 128 } 129 } 130 return rn; 131 } 132 133 #ifdef VIMAGE_GLOBALS 134 static int rtq_reallyold; 135 static int rtq_minreallyold; 136 static int rtq_toomany; 137 #endif 138 139 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTEXPIRE, rtexpire, 140 CTLFLAG_RW, rtq_reallyold, 0, 141 "Default expiration time on dynamically learned routes"); 142 143 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTMINEXPIRE, 144 rtminexpire, CTLFLAG_RW, rtq_minreallyold, 0, 145 "Minimum time to attempt to hold onto dynamically learned routes"); 146 147 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_RTMAXCACHE, 148 rtmaxcache, CTLFLAG_RW, rtq_toomany, 0, 149 "Upper limit on dynamically learned routes"); 150 151 /* 152 * On last reference drop, mark the route as belong to us so that it can be 153 * timed out. 154 */ 155 static void 156 in_clsroute(struct radix_node *rn, struct radix_node_head *head) 157 { 158 INIT_VNET_INET(curvnet); 159 struct rtentry *rt = (struct rtentry *)rn; 160 161 RT_LOCK_ASSERT(rt); 162 163 if (!(rt->rt_flags & RTF_UP)) 164 return; /* prophylactic measures */ 165 166 if (rt->rt_flags & RTPRF_OURS) 167 return; 168 169 if (!(rt->rt_flags & RTF_DYNAMIC)) 170 return; 171 172 /* 173 * If rtq_reallyold is 0, just delete the route without 174 * waiting for a timeout cycle to kill it. 175 */ 176 if (V_rtq_reallyold != 0) { 177 rt->rt_flags |= RTPRF_OURS; 178 rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold; 179 } else { 180 rtexpunge(rt); 181 } 182 } 183 184 struct rtqk_arg { 185 struct radix_node_head *rnh; 186 int draining; 187 int killed; 188 int found; 189 int updating; 190 time_t nextstop; 191 }; 192 193 /* 194 * Get rid of old routes. When draining, this deletes everything, even when 195 * the timeout is not expired yet. When updating, this makes sure that 196 * nothing has a timeout longer than the current value of rtq_reallyold. 197 */ 198 static int 199 in_rtqkill(struct radix_node *rn, void *rock) 200 { 201 INIT_VNET_INET(curvnet); 202 struct rtqk_arg *ap = rock; 203 struct rtentry *rt = (struct rtentry *)rn; 204 int err; 205 206 if (rt->rt_flags & RTPRF_OURS) { 207 ap->found++; 208 209 if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) { 210 if (rt->rt_refcnt > 0) 211 panic("rtqkill route really not free"); 212 213 err = in_rtrequest(RTM_DELETE, 214 (struct sockaddr *)rt_key(rt), 215 rt->rt_gateway, rt_mask(rt), 216 rt->rt_flags, 0, rt->rt_fibnum); 217 if (err) { 218 log(LOG_WARNING, "in_rtqkill: error %d\n", err); 219 } else { 220 ap->killed++; 221 } 222 } else { 223 if (ap->updating && 224 (rt->rt_rmx.rmx_expire - time_uptime > 225 V_rtq_reallyold)) { 226 rt->rt_rmx.rmx_expire = 227 time_uptime + V_rtq_reallyold; 228 } 229 ap->nextstop = lmin(ap->nextstop, 230 rt->rt_rmx.rmx_expire); 231 } 232 } 233 234 return 0; 235 } 236 237 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ 238 #ifdef VIMAGE_GLOBALS 239 static int rtq_timeout; 240 static struct callout rtq_timer; 241 #endif 242 243 static void in_rtqtimo_one(void *rock); 244 245 static void 246 in_rtqtimo(void *rock) 247 { 248 int fibnum; 249 void *newrock; 250 struct timeval atv; 251 252 KASSERT((rock == (void *)V_rt_tables[0][AF_INET]), 253 ("in_rtqtimo: unexpected arg")); 254 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 255 if ((newrock = V_rt_tables[fibnum][AF_INET]) != NULL) 256 in_rtqtimo_one(newrock); 257 } 258 atv.tv_usec = 0; 259 atv.tv_sec = V_rtq_timeout; 260 callout_reset(&V_rtq_timer, tvtohz(&atv), in_rtqtimo, rock); 261 } 262 263 static void 264 in_rtqtimo_one(void *rock) 265 { 266 INIT_VNET_INET(curvnet); 267 struct radix_node_head *rnh = rock; 268 struct rtqk_arg arg; 269 static time_t last_adjusted_timeout = 0; 270 271 arg.found = arg.killed = 0; 272 arg.rnh = rnh; 273 arg.nextstop = time_uptime + V_rtq_timeout; 274 arg.draining = arg.updating = 0; 275 RADIX_NODE_HEAD_LOCK(rnh); 276 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 277 RADIX_NODE_HEAD_UNLOCK(rnh); 278 279 /* 280 * Attempt to be somewhat dynamic about this: 281 * If there are ``too many'' routes sitting around taking up space, 282 * then crank down the timeout, and see if we can't make some more 283 * go away. However, we make sure that we will never adjust more 284 * than once in rtq_timeout seconds, to keep from cranking down too 285 * hard. 286 */ 287 if ((arg.found - arg.killed > V_rtq_toomany) && 288 (time_uptime - last_adjusted_timeout >= V_rtq_timeout) && 289 V_rtq_reallyold > V_rtq_minreallyold) { 290 V_rtq_reallyold = 2 * V_rtq_reallyold / 3; 291 if (V_rtq_reallyold < V_rtq_minreallyold) { 292 V_rtq_reallyold = V_rtq_minreallyold; 293 } 294 295 last_adjusted_timeout = time_uptime; 296 #ifdef DIAGNOSTIC 297 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", 298 V_rtq_reallyold); 299 #endif 300 arg.found = arg.killed = 0; 301 arg.updating = 1; 302 RADIX_NODE_HEAD_LOCK(rnh); 303 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 304 RADIX_NODE_HEAD_UNLOCK(rnh); 305 } 306 307 } 308 309 void 310 in_rtqdrain(void) 311 { 312 VNET_ITERATOR_DECL(vnet_iter); 313 struct radix_node_head *rnh; 314 struct rtqk_arg arg; 315 int fibnum; 316 317 VNET_LIST_RLOCK(); 318 VNET_FOREACH(vnet_iter) { 319 CURVNET_SET(vnet_iter); 320 INIT_VNET_NET(vnet_iter); 321 322 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 323 rnh = V_rt_tables[fibnum][AF_INET]; 324 arg.found = arg.killed = 0; 325 arg.rnh = rnh; 326 arg.nextstop = 0; 327 arg.draining = 1; 328 arg.updating = 0; 329 RADIX_NODE_HEAD_LOCK(rnh); 330 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 331 RADIX_NODE_HEAD_UNLOCK(rnh); 332 } 333 CURVNET_RESTORE(); 334 } 335 VNET_LIST_RUNLOCK(); 336 } 337 338 static int _in_rt_was_here; 339 /* 340 * Initialize our routing tree. 341 */ 342 int 343 in_inithead(void **head, int off) 344 { 345 INIT_VNET_INET(curvnet); 346 struct radix_node_head *rnh; 347 348 /* XXX MRT 349 * This can be called from vfs_export.c too in which case 'off' 350 * will be 0. We know the correct value so just use that and 351 * return directly if it was 0. 352 * This is a hack that replaces an even worse hack on a bad hack 353 * on a bad design. After RELENG_7 this should be fixed but that 354 * will change the ABI, so for now do it this way. 355 */ 356 if (!rn_inithead(head, 32)) 357 return 0; 358 359 if (off == 0) /* XXX MRT see above */ 360 return 1; /* only do the rest for a real routing table */ 361 362 V_rtq_reallyold = 60*60; /* one hour is "really old" */ 363 V_rtq_minreallyold = 10; /* never automatically crank down to less */ 364 V_rtq_toomany = 128; /* 128 cached routes is "too many" */ 365 V_rtq_timeout = RTQ_TIMEOUT; 366 367 rnh = *head; 368 rnh->rnh_addaddr = in_addroute; 369 rnh->rnh_matchaddr = in_matroute; 370 rnh->rnh_close = in_clsroute; 371 if (_in_rt_was_here == 0 ) { 372 callout_init(&V_rtq_timer, CALLOUT_MPSAFE); 373 in_rtqtimo(rnh); /* kick off timeout first time */ 374 _in_rt_was_here = 1; 375 } 376 return 1; 377 } 378 379 /* 380 * This zaps old routes when the interface goes down or interface 381 * address is deleted. In the latter case, it deletes static routes 382 * that point to this address. If we don't do this, we may end up 383 * using the old address in the future. The ones we always want to 384 * get rid of are things like ARP entries, since the user might down 385 * the interface, walk over to a completely different network, and 386 * plug back in. 387 */ 388 struct in_ifadown_arg { 389 struct ifaddr *ifa; 390 int del; 391 }; 392 393 static int 394 in_ifadownkill(struct radix_node *rn, void *xap) 395 { 396 struct in_ifadown_arg *ap = xap; 397 struct rtentry *rt = (struct rtentry *)rn; 398 399 RT_LOCK(rt); 400 if (rt->rt_ifa == ap->ifa && 401 (ap->del || !(rt->rt_flags & RTF_STATIC))) { 402 /* 403 * We need to disable the automatic prune that happens 404 * in this case in rtrequest() because it will blow 405 * away the pointers that rn_walktree() needs in order 406 * continue our descent. We will end up deleting all 407 * the routes that rtrequest() would have in any case, 408 * so that behavior is not needed there. 409 */ 410 rtexpunge(rt); 411 } 412 RT_UNLOCK(rt); 413 return 0; 414 } 415 416 int 417 in_ifadown(struct ifaddr *ifa, int delete) 418 { 419 INIT_VNET_NET(curvnet); 420 struct in_ifadown_arg arg; 421 struct radix_node_head *rnh; 422 int fibnum; 423 424 if (ifa->ifa_addr->sa_family != AF_INET) 425 return 1; 426 427 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 428 rnh = V_rt_tables[fibnum][AF_INET]; 429 arg.ifa = ifa; 430 arg.del = delete; 431 RADIX_NODE_HEAD_LOCK(rnh); 432 rnh->rnh_walktree(rnh, in_ifadownkill, &arg); 433 RADIX_NODE_HEAD_UNLOCK(rnh); 434 ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */ 435 } 436 return 0; 437 } 438 439 /* 440 * inet versions of rt functions. These have fib extensions and 441 * for now will just reference the _fib variants. 442 * eventually this order will be reversed, 443 */ 444 void 445 in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum) 446 { 447 rtalloc_ign_fib(ro, ignflags, fibnum); 448 } 449 450 int 451 in_rtrequest( int req, 452 struct sockaddr *dst, 453 struct sockaddr *gateway, 454 struct sockaddr *netmask, 455 int flags, 456 struct rtentry **ret_nrt, 457 u_int fibnum) 458 { 459 return (rtrequest_fib(req, dst, gateway, netmask, 460 flags, ret_nrt, fibnum)); 461 } 462 463 struct rtentry * 464 in_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 465 { 466 return (rtalloc1_fib(dst, report, ignflags, fibnum)); 467 } 468 469 void 470 in_rtredirect(struct sockaddr *dst, 471 struct sockaddr *gateway, 472 struct sockaddr *netmask, 473 int flags, 474 struct sockaddr *src, 475 u_int fibnum) 476 { 477 rtredirect_fib(dst, gateway, netmask, flags, src, fibnum); 478 } 479 480 void 481 in_rtalloc(struct route *ro, u_int fibnum) 482 { 483 rtalloc_ign_fib(ro, 0UL, fibnum); 484 } 485 486 #if 0 487 int in_rt_getifa(struct rt_addrinfo *, u_int fibnum); 488 int in_rtioctl(u_long, caddr_t, u_int); 489 int in_rtrequest1(int, struct rt_addrinfo *, struct rtentry **, u_int); 490 #endif 491 492 493