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 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/sysctl.h> 37 #include <sys/socket.h> 38 #include <sys/mbuf.h> 39 #include <sys/syslog.h> 40 #include <sys/callout.h> 41 42 #include <net/if.h> 43 #include <net/if_var.h> 44 #include <net/route.h> 45 #include <net/vnet.h> 46 47 #include <netinet/in.h> 48 #include <netinet/in_var.h> 49 #include <netinet/ip.h> 50 #include <netinet/ip_icmp.h> 51 #include <netinet/ip_var.h> 52 53 extern int in_inithead(void **head, int off); 54 #ifdef VIMAGE 55 extern int in_detachhead(void **head, int off); 56 #endif 57 58 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */ 59 60 /* 61 * Do what we need to do when inserting a route. 62 */ 63 static struct radix_node * 64 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 65 struct radix_node *treenodes) 66 { 67 struct rtentry *rt = (struct rtentry *)treenodes; 68 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt); 69 70 RADIX_NODE_HEAD_WLOCK_ASSERT(head); 71 /* 72 * A little bit of help for both IP output and input: 73 * For host routes, we make sure that RTF_BROADCAST 74 * is set for anything that looks like a broadcast address. 75 * This way, we can avoid an expensive call to in_broadcast() 76 * in ip_output() most of the time (because the route passed 77 * to ip_output() is almost always a host route). 78 * 79 * We also do the same for local addresses, with the thought 80 * that this might one day be used to speed up ip_input(). 81 * 82 * We also mark routes to multicast addresses as such, because 83 * it's easy to do and might be useful (but this is much more 84 * dubious since it's so easy to inspect the address). 85 */ 86 if (rt->rt_flags & RTF_HOST) { 87 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) { 88 rt->rt_flags |= RTF_BROADCAST; 89 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr == 90 sin->sin_addr.s_addr) { 91 rt->rt_flags |= RTF_LOCAL; 92 } 93 } 94 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 95 rt->rt_flags |= RTF_MULTICAST; 96 97 if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp) 98 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; 99 100 return (rn_addroute(v_arg, n_arg, head, treenodes)); 101 } 102 103 /* 104 * This code is the inverse of in_clsroute: on first reference, if we 105 * were managing the route, stop doing so and set the expiration timer 106 * back off again. 107 */ 108 static struct radix_node * 109 in_matroute(void *v_arg, struct radix_node_head *head) 110 { 111 struct radix_node *rn = rn_match(v_arg, head); 112 struct rtentry *rt = (struct rtentry *)rn; 113 114 if (rt) { 115 RT_LOCK(rt); 116 if (rt->rt_flags & RTPRF_OURS) { 117 rt->rt_flags &= ~RTPRF_OURS; 118 rt->rt_rmx.rmx_expire = 0; 119 } 120 RT_UNLOCK(rt); 121 } 122 return rn; 123 } 124 125 static VNET_DEFINE(int, rtq_reallyold) = 60*60; /* one hour is "really old" */ 126 #define V_rtq_reallyold VNET(rtq_reallyold) 127 SYSCTL_VNET_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW, 128 &VNET_NAME(rtq_reallyold), 0, 129 "Default expiration time on dynamically learned routes"); 130 131 /* never automatically crank down to less */ 132 static VNET_DEFINE(int, rtq_minreallyold) = 10; 133 #define V_rtq_minreallyold VNET(rtq_minreallyold) 134 SYSCTL_VNET_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW, 135 &VNET_NAME(rtq_minreallyold), 0, 136 "Minimum time to attempt to hold onto dynamically learned routes"); 137 138 /* 128 cached routes is "too many" */ 139 static VNET_DEFINE(int, rtq_toomany) = 128; 140 #define V_rtq_toomany VNET(rtq_toomany) 141 SYSCTL_VNET_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW, 142 &VNET_NAME(rtq_toomany), 0, 143 "Upper limit on dynamically learned routes"); 144 145 /* 146 * On last reference drop, mark the route as belong to us so that it can be 147 * timed out. 148 */ 149 static void 150 in_clsroute(struct radix_node *rn, struct radix_node_head *head) 151 { 152 struct rtentry *rt = (struct rtentry *)rn; 153 154 RT_LOCK_ASSERT(rt); 155 156 if (!(rt->rt_flags & RTF_UP)) 157 return; /* prophylactic measures */ 158 159 if (rt->rt_flags & RTPRF_OURS) 160 return; 161 162 if (!(rt->rt_flags & RTF_DYNAMIC)) 163 return; 164 165 /* 166 * If rtq_reallyold is 0, just delete the route without 167 * waiting for a timeout cycle to kill it. 168 */ 169 if (V_rtq_reallyold != 0) { 170 rt->rt_flags |= RTPRF_OURS; 171 rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold; 172 } else { 173 rtexpunge(rt); 174 } 175 } 176 177 struct rtqk_arg { 178 struct radix_node_head *rnh; 179 int draining; 180 int killed; 181 int found; 182 int updating; 183 time_t nextstop; 184 }; 185 186 /* 187 * Get rid of old routes. When draining, this deletes everything, even when 188 * the timeout is not expired yet. When updating, this makes sure that 189 * nothing has a timeout longer than the current value of rtq_reallyold. 190 */ 191 static int 192 in_rtqkill(struct radix_node *rn, void *rock) 193 { 194 struct rtqk_arg *ap = rock; 195 struct rtentry *rt = (struct rtentry *)rn; 196 int err; 197 198 RADIX_NODE_HEAD_WLOCK_ASSERT(ap->rnh); 199 200 if (rt->rt_flags & RTPRF_OURS) { 201 ap->found++; 202 203 if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) { 204 if (rt->rt_refcnt > 0) 205 panic("rtqkill route really not free"); 206 207 err = in_rtrequest(RTM_DELETE, 208 (struct sockaddr *)rt_key(rt), 209 rt->rt_gateway, rt_mask(rt), 210 rt->rt_flags | RTF_RNH_LOCKED, 0, 211 rt->rt_fibnum); 212 if (err) { 213 log(LOG_WARNING, "in_rtqkill: error %d\n", err); 214 } else { 215 ap->killed++; 216 } 217 } else { 218 if (ap->updating && 219 (rt->rt_rmx.rmx_expire - time_uptime > 220 V_rtq_reallyold)) { 221 rt->rt_rmx.rmx_expire = 222 time_uptime + V_rtq_reallyold; 223 } 224 ap->nextstop = lmin(ap->nextstop, 225 rt->rt_rmx.rmx_expire); 226 } 227 } 228 229 return 0; 230 } 231 232 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ 233 static VNET_DEFINE(int, rtq_timeout) = RTQ_TIMEOUT; 234 static VNET_DEFINE(struct callout, rtq_timer); 235 236 #define V_rtq_timeout VNET(rtq_timeout) 237 #define V_rtq_timer VNET(rtq_timer) 238 239 static void in_rtqtimo_one(void *rock); 240 241 static void 242 in_rtqtimo(void *rock) 243 { 244 CURVNET_SET((struct vnet *) rock); 245 int fibnum; 246 void *newrock; 247 struct timeval atv; 248 249 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 250 newrock = rt_tables_get_rnh(fibnum, AF_INET); 251 if (newrock != NULL) 252 in_rtqtimo_one(newrock); 253 } 254 atv.tv_usec = 0; 255 atv.tv_sec = V_rtq_timeout; 256 callout_reset(&V_rtq_timer, tvtohz(&atv), in_rtqtimo, rock); 257 CURVNET_RESTORE(); 258 } 259 260 static void 261 in_rtqtimo_one(void *rock) 262 { 263 struct radix_node_head *rnh = rock; 264 struct rtqk_arg arg; 265 static time_t last_adjusted_timeout = 0; 266 267 arg.found = arg.killed = 0; 268 arg.rnh = rnh; 269 arg.nextstop = time_uptime + V_rtq_timeout; 270 arg.draining = arg.updating = 0; 271 RADIX_NODE_HEAD_LOCK(rnh); 272 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 273 RADIX_NODE_HEAD_UNLOCK(rnh); 274 275 /* 276 * Attempt to be somewhat dynamic about this: 277 * If there are ``too many'' routes sitting around taking up space, 278 * then crank down the timeout, and see if we can't make some more 279 * go away. However, we make sure that we will never adjust more 280 * than once in rtq_timeout seconds, to keep from cranking down too 281 * hard. 282 */ 283 if ((arg.found - arg.killed > V_rtq_toomany) && 284 (time_uptime - last_adjusted_timeout >= V_rtq_timeout) && 285 V_rtq_reallyold > V_rtq_minreallyold) { 286 V_rtq_reallyold = 2 * V_rtq_reallyold / 3; 287 if (V_rtq_reallyold < V_rtq_minreallyold) { 288 V_rtq_reallyold = V_rtq_minreallyold; 289 } 290 291 last_adjusted_timeout = time_uptime; 292 #ifdef DIAGNOSTIC 293 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", 294 V_rtq_reallyold); 295 #endif 296 arg.found = arg.killed = 0; 297 arg.updating = 1; 298 RADIX_NODE_HEAD_LOCK(rnh); 299 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 300 RADIX_NODE_HEAD_UNLOCK(rnh); 301 } 302 303 } 304 305 void 306 in_rtqdrain(void) 307 { 308 VNET_ITERATOR_DECL(vnet_iter); 309 struct radix_node_head *rnh; 310 struct rtqk_arg arg; 311 int fibnum; 312 313 VNET_LIST_RLOCK_NOSLEEP(); 314 VNET_FOREACH(vnet_iter) { 315 CURVNET_SET(vnet_iter); 316 317 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 318 rnh = rt_tables_get_rnh(fibnum, AF_INET); 319 arg.found = arg.killed = 0; 320 arg.rnh = rnh; 321 arg.nextstop = 0; 322 arg.draining = 1; 323 arg.updating = 0; 324 RADIX_NODE_HEAD_LOCK(rnh); 325 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 326 RADIX_NODE_HEAD_UNLOCK(rnh); 327 } 328 CURVNET_RESTORE(); 329 } 330 VNET_LIST_RUNLOCK_NOSLEEP(); 331 } 332 333 void 334 in_setmatchfunc(struct radix_node_head *rnh, int val) 335 { 336 337 rnh->rnh_matchaddr = (val != 0) ? rn_match : in_matroute; 338 } 339 340 static int _in_rt_was_here; 341 /* 342 * Initialize our routing tree. 343 */ 344 int 345 in_inithead(void **head, int off) 346 { 347 struct radix_node_head *rnh; 348 349 /* XXX MRT 350 * This can be called from vfs_export.c too in which case 'off' 351 * will be 0. We know the correct value so just use that and 352 * return directly if it was 0. 353 * This is a hack that replaces an even worse hack on a bad hack 354 * on a bad design. After RELENG_7 this should be fixed but that 355 * will change the ABI, so for now do it this way. 356 */ 357 if (!rn_inithead(head, 32)) 358 return 0; 359 360 if (off == 0) /* XXX MRT see above */ 361 return 1; /* only do the rest for a real routing table */ 362 363 rnh = *head; 364 rnh->rnh_addaddr = in_addroute; 365 in_setmatchfunc(rnh, V_drop_redirect); 366 rnh->rnh_close = in_clsroute; 367 if (_in_rt_was_here == 0 ) { 368 callout_init(&V_rtq_timer, CALLOUT_MPSAFE); 369 callout_reset(&V_rtq_timer, 1, in_rtqtimo, curvnet); 370 _in_rt_was_here = 1; 371 } 372 return 1; 373 } 374 375 #ifdef VIMAGE 376 int 377 in_detachhead(void **head, int off) 378 { 379 380 callout_drain(&V_rtq_timer); 381 return (1); 382 } 383 #endif 384 385 /* 386 * This zaps old routes when the interface goes down or interface 387 * address is deleted. In the latter case, it deletes static routes 388 * that point to this address. If we don't do this, we may end up 389 * using the old address in the future. The ones we always want to 390 * get rid of are things like ARP entries, since the user might down 391 * the interface, walk over to a completely different network, and 392 * plug back in. 393 */ 394 struct in_ifadown_arg { 395 struct ifaddr *ifa; 396 int del; 397 }; 398 399 static int 400 in_ifadownkill(struct radix_node *rn, void *xap) 401 { 402 struct in_ifadown_arg *ap = xap; 403 struct rtentry *rt = (struct rtentry *)rn; 404 405 RT_LOCK(rt); 406 if (rt->rt_ifa == ap->ifa && 407 (ap->del || !(rt->rt_flags & RTF_STATIC))) { 408 /* 409 * Aquire a reference so that it can later be freed 410 * as the refcount would be 0 here in case of at least 411 * ap->del. 412 */ 413 RT_ADDREF(rt); 414 /* 415 * Disconnect it from the tree and permit protocols 416 * to cleanup. 417 */ 418 rtexpunge(rt); 419 /* 420 * At this point it is an rttrash node, and in case 421 * the above is the only reference we must free it. 422 * If we do not noone will have a pointer and the 423 * rtentry will be leaked forever. 424 * In case someone else holds a reference, we are 425 * fine as we only decrement the refcount. In that 426 * case if the other entity calls RT_REMREF, we 427 * will still be leaking but at least we tried. 428 */ 429 RTFREE_LOCKED(rt); 430 return (0); 431 } 432 RT_UNLOCK(rt); 433 return 0; 434 } 435 436 void 437 in_ifadown(struct ifaddr *ifa, int delete) 438 { 439 struct in_ifadown_arg arg; 440 struct radix_node_head *rnh; 441 int fibnum; 442 443 KASSERT(ifa->ifa_addr->sa_family == AF_INET, 444 ("%s: wrong family", __func__)); 445 446 for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) { 447 rnh = rt_tables_get_rnh(fibnum, AF_INET); 448 arg.ifa = ifa; 449 arg.del = delete; 450 RADIX_NODE_HEAD_LOCK(rnh); 451 rnh->rnh_walktree(rnh, in_ifadownkill, &arg); 452 RADIX_NODE_HEAD_UNLOCK(rnh); 453 ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */ 454 } 455 } 456 457 /* 458 * inet versions of rt functions. These have fib extensions and 459 * for now will just reference the _fib variants. 460 * eventually this order will be reversed, 461 */ 462 void 463 in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum) 464 { 465 rtalloc_ign_fib(ro, ignflags, fibnum); 466 } 467 468 int 469 in_rtrequest( int req, 470 struct sockaddr *dst, 471 struct sockaddr *gateway, 472 struct sockaddr *netmask, 473 int flags, 474 struct rtentry **ret_nrt, 475 u_int fibnum) 476 { 477 return (rtrequest_fib(req, dst, gateway, netmask, 478 flags, ret_nrt, fibnum)); 479 } 480 481 struct rtentry * 482 in_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 483 { 484 return (rtalloc1_fib(dst, report, ignflags, fibnum)); 485 } 486 487 void 488 in_rtredirect(struct sockaddr *dst, 489 struct sockaddr *gateway, 490 struct sockaddr *netmask, 491 int flags, 492 struct sockaddr *src, 493 u_int fibnum) 494 { 495 rtredirect_fib(dst, gateway, netmask, flags, src, fibnum); 496 } 497 498 void 499 in_rtalloc(struct route *ro, u_int fibnum) 500 { 501 rtalloc_ign_fib(ro, 0UL, fibnum); 502 } 503 504 #if 0 505 int in_rt_getifa(struct rt_addrinfo *, u_int fibnum); 506 int in_rtioctl(u_long, caddr_t, u_int); 507 int in_rtrequest1(int, struct rt_addrinfo *, struct rtentry **, u_int); 508 #endif 509 510 511