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 * $Id: in_rmx.c,v 1.18 1995/11/09 20:22:59 phk Exp $ 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/queue.h> 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <sys/mbuf.h> 53 #include <sys/syslog.h> 54 55 #include <net/if.h> 56 #include <net/route.h> 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/in_var.h> 60 61 #include <netinet/ip.h> 62 #include <netinet/ip_var.h> 63 64 #include <netinet/tcp.h> 65 #include <netinet/tcp_seq.h> 66 #include <netinet/tcp_timer.h> 67 #include <netinet/tcp_var.h> 68 #ifndef MTUDISC 69 #include <netinet/tcpip.h> 70 #endif /* not MTUDISC */ 71 72 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */ 73 74 /* 75 * Do what we need to do when inserting a route. 76 */ 77 static struct radix_node * 78 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 79 struct radix_node *treenodes) 80 { 81 struct rtentry *rt = (struct rtentry *)treenodes; 82 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt); 83 84 /* 85 * For IP, all unicast non-host routes are automatically cloning. 86 */ 87 if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING))) { 88 if(!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 89 rt->rt_flags |= RTF_PRCLONING; 90 } 91 } 92 93 /* 94 * We also specify a send and receive pipe size for every 95 * route added, to help TCP a bit. TCP doesn't actually 96 * want a true pipe size, which would be prohibitive in memory 97 * costs and is hard to compute anyway; it simply uses these 98 * values to size its buffers. So, we fill them in with the 99 * same values that TCP would have used anyway, and allow the 100 * installing program or the link layer to override these values 101 * as it sees fit. This will hopefully allow TCP more 102 * opportunities to save its ssthresh value. 103 */ 104 if (!rt->rt_rmx.rmx_sendpipe && !(rt->rt_rmx.rmx_locks & RTV_SPIPE)) 105 rt->rt_rmx.rmx_sendpipe = tcp_sendspace; 106 107 if (!rt->rt_rmx.rmx_recvpipe && !(rt->rt_rmx.rmx_locks & RTV_RPIPE)) 108 rt->rt_rmx.rmx_recvpipe = tcp_recvspace; 109 110 #ifndef MTUDISC 111 /* 112 * Finally, set an MTU, again duplicating logic in TCP. 113 * The in_localaddr() business will go away when we have 114 * proper PMTU discovery. 115 */ 116 #endif /* not MTUDISC */ 117 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) 118 && rt->rt_ifp) 119 #ifndef MTUDISC 120 rt->rt_rmx.rmx_mtu = (in_localaddr(sin->sin_addr) 121 ? rt->rt_ifp->if_mtu 122 : tcp_mssdflt + sizeof(struct tcpiphdr)); 123 #else /* MTUDISC */ 124 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; 125 #endif /* MTUDISC */ 126 127 return rn_addroute(v_arg, n_arg, head, treenodes); 128 } 129 130 /* 131 * This code is the inverse of in_clsroute: on first reference, if we 132 * were managing the route, stop doing so and set the expiration timer 133 * back off again. 134 */ 135 static struct radix_node * 136 in_matroute(void *v_arg, struct radix_node_head *head) 137 { 138 struct radix_node *rn = rn_match(v_arg, head); 139 struct rtentry *rt = (struct rtentry *)rn; 140 141 if(rt && rt->rt_refcnt == 0) { /* this is first reference */ 142 if(rt->rt_flags & RTPRF_OURS) { 143 rt->rt_flags &= ~RTPRF_OURS; 144 rt->rt_rmx.rmx_expire = 0; 145 } 146 } 147 return rn; 148 } 149 150 static int rtq_reallyold = 60*60; 151 /* one hour is ``really old'' */ 152 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, 153 CTLFLAG_RW, &rtq_reallyold , 0, ""); 154 155 static int rtq_minreallyold = 10; 156 /* never automatically crank down to less */ 157 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, 158 CTLFLAG_RW, &rtq_minreallyold , 0, ""); 159 160 static int rtq_toomany = 128; 161 /* 128 cached routes is ``too many'' */ 162 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, 163 CTLFLAG_RW, &rtq_toomany , 0, ""); 164 165 166 /* 167 * On last reference drop, mark the route as belong to us so that it can be 168 * timed out. 169 */ 170 static void 171 in_clsroute(struct radix_node *rn, struct radix_node_head *head) 172 { 173 struct rtentry *rt = (struct rtentry *)rn; 174 175 if(!(rt->rt_flags & RTF_UP)) 176 return; /* prophylactic measures */ 177 178 if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) 179 return; 180 181 if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) 182 != RTF_WASCLONED) 183 return; 184 185 /* 186 * As requested by David Greenman: 187 * If rtq_reallyold is 0, just delete the route without 188 * waiting for a timeout cycle to kill it. 189 */ 190 if(rtq_reallyold != 0) { 191 rt->rt_flags |= RTPRF_OURS; 192 rt->rt_rmx.rmx_expire = time.tv_sec + rtq_reallyold; 193 } else { 194 rtrequest(RTM_DELETE, 195 (struct sockaddr *)rt_key(rt), 196 rt->rt_gateway, rt_mask(rt), 197 rt->rt_flags, 0); 198 } 199 } 200 201 struct rtqk_arg { 202 struct radix_node_head *rnh; 203 int draining; 204 int killed; 205 int found; 206 int updating; 207 time_t nextstop; 208 }; 209 210 /* 211 * Get rid of old routes. When draining, this deletes everything, even when 212 * the timeout is not expired yet. When updating, this makes sure that 213 * nothing has a timeout longer than the current value of rtq_reallyold. 214 */ 215 static int 216 in_rtqkill(struct radix_node *rn, void *rock) 217 { 218 struct rtqk_arg *ap = rock; 219 struct rtentry *rt = (struct rtentry *)rn; 220 int err; 221 222 if(rt->rt_flags & RTPRF_OURS) { 223 ap->found++; 224 225 if(ap->draining || rt->rt_rmx.rmx_expire <= time.tv_sec) { 226 if(rt->rt_refcnt > 0) 227 panic("rtqkill route really not free"); 228 229 err = rtrequest(RTM_DELETE, 230 (struct sockaddr *)rt_key(rt), 231 rt->rt_gateway, rt_mask(rt), 232 rt->rt_flags, 0); 233 if(err) { 234 log(LOG_WARNING, "in_rtqkill: error %d\n", err); 235 } else { 236 ap->killed++; 237 } 238 } else { 239 if(ap->updating 240 && (rt->rt_rmx.rmx_expire - time.tv_sec 241 > rtq_reallyold)) { 242 rt->rt_rmx.rmx_expire = time.tv_sec 243 + rtq_reallyold; 244 } 245 ap->nextstop = lmin(ap->nextstop, 246 rt->rt_rmx.rmx_expire); 247 } 248 } 249 250 return 0; 251 } 252 253 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ 254 static int rtq_timeout = RTQ_TIMEOUT; 255 256 static void 257 in_rtqtimo(void *rock) 258 { 259 struct radix_node_head *rnh = rock; 260 struct rtqk_arg arg; 261 struct timeval atv; 262 static time_t last_adjusted_timeout = 0; 263 int s; 264 265 arg.found = arg.killed = 0; 266 arg.rnh = rnh; 267 arg.nextstop = time.tv_sec + rtq_timeout; 268 arg.draining = arg.updating = 0; 269 s = splnet(); 270 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 271 splx(s); 272 273 /* 274 * Attempt to be somewhat dynamic about this: 275 * If there are ``too many'' routes sitting around taking up space, 276 * then crank down the timeout, and see if we can't make some more 277 * go away. However, we make sure that we will never adjust more 278 * than once in rtq_timeout seconds, to keep from cranking down too 279 * hard. 280 */ 281 if((arg.found - arg.killed > rtq_toomany) 282 && (time.tv_sec - last_adjusted_timeout >= rtq_timeout) 283 && rtq_reallyold > rtq_minreallyold) { 284 rtq_reallyold = 2*rtq_reallyold / 3; 285 if(rtq_reallyold < rtq_minreallyold) { 286 rtq_reallyold = rtq_minreallyold; 287 } 288 289 last_adjusted_timeout = time.tv_sec; 290 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", 291 rtq_reallyold); 292 arg.found = arg.killed = 0; 293 arg.updating = 1; 294 s = splnet(); 295 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 296 splx(s); 297 } 298 299 atv.tv_usec = 0; 300 atv.tv_sec = arg.nextstop; 301 timeout(in_rtqtimo, rock, hzto(&atv)); 302 } 303 304 #ifdef never_used 305 static void 306 in_rtqdrain(void) 307 { 308 struct radix_node_head *rnh = rt_tables[AF_INET]; 309 struct rtqk_arg arg; 310 int s; 311 arg.found = arg.killed = 0; 312 arg.rnh = rnh; 313 arg.nextstop = 0; 314 arg.draining = 1; 315 arg.updating = 0; 316 s = splnet(); 317 rnh->rnh_walktree(rnh, in_rtqkill, &arg); 318 splx(s); 319 } 320 #endif 321 322 /* 323 * Initialize our routing tree. 324 */ 325 int 326 in_inithead(void **head, int off) 327 { 328 struct radix_node_head *rnh; 329 330 if(!rn_inithead(head, off)) 331 return 0; 332 333 if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */ 334 return 1; /* only do this for the real routing table */ 335 336 rnh = *head; 337 rnh->rnh_addaddr = in_addroute; 338 rnh->rnh_matchaddr = in_matroute; 339 rnh->rnh_close = in_clsroute; 340 in_rtqtimo(rnh); /* kick off timeout first time */ 341 return 1; 342 } 343 344