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 * $FreeBSD$ 30 */ 31 32 /* 33 * This code does two things necessary for the enhanced TCP metrics to 34 * function in a useful manner: 35 * 1) It marks all non-host routes as `cloning', thus ensuring that 36 * every actual reference to such a route actually gets turned 37 * into a reference to a host route to the specific destination 38 * requested. 39 * 2) When such routes lose all their references, it arranges for them 40 * to be deleted in some random collection of circumstances, so that 41 * a large quantity of stale routing data is not kept in kernel memory 42 * indefinitely. See in_rtqtimo() below for the exact mechanism. 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/sysctl.h> 49 #include <sys/socket.h> 50 #include <sys/mbuf.h> 51 #include <sys/syslog.h> 52 #include <sys/callout.h> 53 54 #include <net/if.h> 55 #include <net/route.h> 56 #include <netinet/in.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip_var.h> 59 60 extern int in_inithead(void **head, int off); 61 62 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */ 63 64 /* 65 * Do what we need to do when inserting a route. 66 */ 67 static struct radix_node * 68 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 69 struct radix_node *treenodes) 70 { 71 struct rtentry *rt = (struct rtentry *)treenodes; 72 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt); 73 struct radix_node *ret; 74 75 /* 76 * For IP, all unicast non-host routes are automatically cloning. 77 */ 78 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 79 rt->rt_flags |= RTF_MULTICAST; 80 81 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) 82 rt->rt_flags |= RTF_PRCLONING; 83 84 /* 85 * A little bit of help for both IP output and input: 86 * For host routes, we make sure that RTF_BROADCAST 87 * is set for anything that looks like a broadcast address. 88 * This way, we can avoid an expensive call to in_broadcast() 89 * in ip_output() most of the time (because the route passed 90 * to ip_output() is almost always a host route). 91 * 92 * We also do the same for local addresses, with the thought 93 * that this might one day be used to speed up ip_input(). 94 * 95 * We also mark routes to multicast addresses as such, because 96 * it's easy to do and might be useful (but this is much more 97 * dubious since it's so easy to inspect the address). (This 98 * is done above.) 99 */ 100 if (rt->rt_flags & RTF_HOST) { 101 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) { 102 rt->rt_flags |= RTF_BROADCAST; 103 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr == 104 sin->sin_addr.s_addr) { 105 rt->rt_flags |= RTF_LOCAL; 106 } 107 } 108 109 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) && 110 rt->rt_ifp) 111 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; 112 113 ret = rn_addroute(v_arg, n_arg, head, treenodes); 114 if (ret == NULL && rt->rt_flags & RTF_HOST) { 115 struct rtentry *rt2; 116 /* 117 * We are trying to add a host route, but can't. 118 * Find out if it is because of an 119 * ARP entry and delete it if so. 120 */ 121 rt2 = rtalloc1((struct sockaddr *)sin, 0, 122 RTF_CLONING | RTF_PRCLONING); 123 if (rt2) { 124 if (rt2->rt_flags & RTF_LLINFO && 125 rt2->rt_flags & RTF_HOST && 126 rt2->rt_gateway && 127 rt2->rt_gateway->sa_family == AF_LINK) { 128 rtexpunge(rt2); 129 RTFREE_LOCKED(rt2); 130 ret = rn_addroute(v_arg, n_arg, head, 131 treenodes); 132 } else 133 RTFREE_LOCKED(rt2); 134 } 135 } 136 137 /* 138 * If the new route created successfully, and we are forwarding, 139 * flush any cached routes to avoid using a stale value. 140 */ 141 if (ret != NULL && ipforwarding) 142 ip_forward_cacheinval(); 143 144 return ret; 145 } 146 147 /* 148 * This code is the inverse of in_clsroute: on first reference, if we 149 * were managing the route, stop doing so and set the expiration timer 150 * back off again. 151 */ 152 static struct radix_node * 153 in_matroute(void *v_arg, struct radix_node_head *head) 154 { 155 struct radix_node *rn = rn_match(v_arg, head); 156 struct rtentry *rt = (struct rtentry *)rn; 157 158 /*XXX locking? */ 159 if (rt && rt->rt_refcnt == 0) { /* this is first reference */ 160 if (rt->rt_flags & RTPRF_OURS) { 161 rt->rt_flags &= ~RTPRF_OURS; 162 rt->rt_rmx.rmx_expire = 0; 163 } 164 } 165 return rn; 166 } 167 168 static int rtq_reallyold = 60*60; /* one hour is "really old" */ 169 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW, 170 &rtq_reallyold, 0, "Default expiration time on dynamically learned routes"); 171 172 static int rtq_minreallyold = 10; /* never automatically crank down to less */ 173 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW, 174 &rtq_minreallyold, 0, 175 "Minimum time to attempt to hold onto dynamically learned routes"); 176 177 static int rtq_toomany = 128; /* 128 cached routes is "too many" */ 178 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW, 179 &rtq_toomany, 0, "Upper limit on dynamically learned routes"); 180 181 /* 182 * On last reference drop, mark the route as belong to us so that it can be 183 * timed out. 184 */ 185 static void 186 in_clsroute(struct radix_node *rn, struct radix_node_head *head) 187 { 188 struct rtentry *rt = (struct rtentry *)rn; 189 190 RT_LOCK_ASSERT(rt); 191 192 if (!(rt->rt_flags & RTF_UP)) 193 return; /* prophylactic measures */ 194 195 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) 196 return; 197 198 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED) 199 return; 200 201 /* 202 * If rtq_reallyold is 0, just delete the route without 203 * waiting for a timeout cycle to kill it. 204 */ 205 if (rtq_reallyold != 0) { 206 rt->rt_flags |= RTPRF_OURS; 207 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold; 208 } else { 209 rtexpunge(rt); 210 } 211 } 212 213 struct rtqk_arg { 214 struct radix_node_head *rnh; 215 int draining; 216 int killed; 217 int found; 218 int updating; 219 time_t nextstop; 220 }; 221 222 /* 223 * Get rid of old routes. When draining, this deletes everything, even when 224 * the timeout is not expired yet. When updating, this makes sure that 225 * nothing has a timeout longer than the current value of rtq_reallyold. 226 */ 227 static int 228 in_rtqkill(struct radix_node *rn, void *rock) 229 { 230 struct rtqk_arg *ap = rock; 231 struct rtentry *rt = (struct rtentry *)rn; 232 int err; 233 234 if (rt->rt_flags & RTPRF_OURS) { 235 ap->found++; 236 237 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) { 238 if (rt->rt_refcnt > 0) 239 panic("rtqkill route really not free"); 240 241 err = rtrequest(RTM_DELETE, 242 (struct sockaddr *)rt_key(rt), 243 rt->rt_gateway, rt_mask(rt), 244 rt->rt_flags, 0); 245 if (err) { 246 log(LOG_WARNING, "in_rtqkill: error %d\n", err); 247 } else { 248 ap->killed++; 249 } 250 } else { 251 if (ap->updating && 252 (rt->rt_rmx.rmx_expire - time_second > 253 rtq_reallyold)) { 254 rt->rt_rmx.rmx_expire = 255 time_second + rtq_reallyold; 256 } 257 ap->nextstop = lmin(ap->nextstop, 258 rt->rt_rmx.rmx_expire); 259 } 260 } 261 262 return 0; 263 } 264 265 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ 266 static int rtq_timeout = RTQ_TIMEOUT; 267 static struct callout rtq_timer; 268 269 static void 270 in_rtqtimo(void *rock) 271 { 272 struct radix_node_head *rnh = rock; 273 struct rtqk_arg arg; 274 struct timeval atv; 275 static time_t last_adjusted_timeout = 0; 276 277 arg.found = arg.killed = 0; 278 arg.rnh = rnh; 279 arg.nextstop = time_second + rtq_timeout; 280 arg.draining = arg.updating = 0; 281 RADIX_NODE_HEAD_LOCK(rnh); 282 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 283 RADIX_NODE_HEAD_UNLOCK(rnh); 284 285 /* 286 * Attempt to be somewhat dynamic about this: 287 * If there are ``too many'' routes sitting around taking up space, 288 * then crank down the timeout, and see if we can't make some more 289 * go away. However, we make sure that we will never adjust more 290 * than once in rtq_timeout seconds, to keep from cranking down too 291 * hard. 292 */ 293 if ((arg.found - arg.killed > rtq_toomany) && 294 (time_second - last_adjusted_timeout >= rtq_timeout) && 295 rtq_reallyold > rtq_minreallyold) { 296 rtq_reallyold = 2 * rtq_reallyold / 3; 297 if (rtq_reallyold < rtq_minreallyold) { 298 rtq_reallyold = rtq_minreallyold; 299 } 300 301 last_adjusted_timeout = time_second; 302 #ifdef DIAGNOSTIC 303 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", 304 rtq_reallyold); 305 #endif 306 arg.found = arg.killed = 0; 307 arg.updating = 1; 308 RADIX_NODE_HEAD_LOCK(rnh); 309 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 310 RADIX_NODE_HEAD_UNLOCK(rnh); 311 } 312 313 atv.tv_usec = 0; 314 atv.tv_sec = arg.nextstop - time_second; 315 callout_reset(&rtq_timer, tvtohz(&atv), in_rtqtimo, rock); 316 } 317 318 void 319 in_rtqdrain(void) 320 { 321 struct radix_node_head *rnh = rt_tables[AF_INET]; 322 struct rtqk_arg arg; 323 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 334 /* 335 * Initialize our routing tree. 336 */ 337 int 338 in_inithead(void **head, int off) 339 { 340 struct radix_node_head *rnh; 341 342 if (!rn_inithead(head, off)) 343 return 0; 344 345 if (head != (void **)&rt_tables[AF_INET]) /* BOGUS! */ 346 return 1; /* only do this for the real routing table */ 347 348 rnh = *head; 349 rnh->rnh_addaddr = in_addroute; 350 rnh->rnh_matchaddr = in_matroute; 351 rnh->rnh_close = in_clsroute; 352 callout_init(&rtq_timer, CALLOUT_MPSAFE); 353 in_rtqtimo(rnh); /* kick off timeout first time */ 354 return 1; 355 } 356 357 /* 358 * This zaps old routes when the interface goes down or interface 359 * address is deleted. In the latter case, it deletes static routes 360 * that point to this address. If we don't do this, we may end up 361 * using the old address in the future. The ones we always want to 362 * get rid of are things like ARP entries, since the user might down 363 * the interface, walk over to a completely different network, and 364 * plug back in. 365 */ 366 struct in_ifadown_arg { 367 struct radix_node_head *rnh; 368 struct ifaddr *ifa; 369 int del; 370 }; 371 372 static int 373 in_ifadownkill(struct radix_node *rn, void *xap) 374 { 375 struct in_ifadown_arg *ap = xap; 376 struct rtentry *rt = (struct rtentry *)rn; 377 378 RT_LOCK(rt); 379 if (rt->rt_ifa == ap->ifa && 380 (ap->del || !(rt->rt_flags & RTF_STATIC))) { 381 /* 382 * We need to disable the automatic prune that happens 383 * in this case in rtrequest() because it will blow 384 * away the pointers that rn_walktree() needs in order 385 * continue our descent. We will end up deleting all 386 * the routes that rtrequest() would have in any case, 387 * so that behavior is not needed there. 388 */ 389 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING); 390 rtexpunge(rt); 391 } 392 RT_UNLOCK(rt); 393 return 0; 394 } 395 396 int 397 in_ifadown(struct ifaddr *ifa, int delete) 398 { 399 struct in_ifadown_arg arg; 400 struct radix_node_head *rnh; 401 402 if (ifa->ifa_addr->sa_family != AF_INET) 403 return 1; 404 405 arg.rnh = rnh = rt_tables[AF_INET]; 406 arg.ifa = ifa; 407 arg.del = delete; 408 RADIX_NODE_HEAD_LOCK(rnh); 409 rnh->rnh_walktree(rnh, in_ifadownkill, &arg); 410 RADIX_NODE_HEAD_UNLOCK(rnh); 411 ifa->ifa_flags &= ~IFA_ROUTE; /* XXXlocking? */ 412 return 0; 413 } 414