1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95 34 * $FreeBSD$ 35 */ 36 37 #include "opt_inet6.h" 38 #include "opt_tcpdebug.h" 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/mbuf.h> 44 #include <sys/mutex.h> 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_pcb.h> 55 #include <netinet/in_systm.h> 56 #ifdef INET6 57 #include <netinet6/in6_pcb.h> 58 #endif 59 #include <netinet/ip_var.h> 60 #include <netinet/tcp.h> 61 #include <netinet/tcp_fsm.h> 62 #include <netinet/tcp_timer.h> 63 #include <netinet/tcp_var.h> 64 #include <netinet/tcpip.h> 65 #ifdef TCPDEBUG 66 #include <netinet/tcp_debug.h> 67 #endif 68 69 static int 70 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 71 { 72 int error, s, tt; 73 74 tt = *(int *)oidp->oid_arg1; 75 s = (int)((int64_t)tt * 1000 / hz); 76 77 error = sysctl_handle_int(oidp, &s, 0, req); 78 if (error || !req->newptr) 79 return (error); 80 81 tt = (int)((int64_t)s * hz / 1000); 82 if (tt < 1) 83 return (EINVAL); 84 85 *(int *)oidp->oid_arg1 = tt; 86 return (0); 87 } 88 89 int tcp_keepinit; 90 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW, 91 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", ""); 92 93 int tcp_keepidle; 94 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW, 95 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", ""); 96 97 int tcp_keepintvl; 98 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW, 99 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", ""); 100 101 int tcp_delacktime; 102 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, 103 CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I", 104 "Time before a delayed ACK is sent"); 105 106 int tcp_msl; 107 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW, 108 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime"); 109 110 int tcp_rexmit_min; 111 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW, 112 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", "Minimum Retransmission Timeout"); 113 114 int tcp_rexmit_slop; 115 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW, 116 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I", "Retransmission Timer Slop"); 117 118 static int always_keepalive = 1; 119 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW, 120 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections"); 121 122 static int tcp_keepcnt = TCPTV_KEEPCNT; 123 /* max idle probes */ 124 int tcp_maxpersistidle; 125 /* max idle time in persist */ 126 int tcp_maxidle; 127 128 /* 129 * Tcp protocol timeout routine called every 500 ms. 130 * Updates timestamps used for TCP 131 * causes finite state machine actions if timers expire. 132 */ 133 void 134 tcp_slowtimo() 135 { 136 int s; 137 138 s = splnet(); 139 140 tcp_maxidle = tcp_keepcnt * tcp_keepintvl; 141 142 splx(s); 143 } 144 145 /* 146 * Cancel all timers for TCP tp. 147 */ 148 void 149 tcp_canceltimers(tp) 150 struct tcpcb *tp; 151 { 152 callout_stop(tp->tt_2msl); 153 callout_stop(tp->tt_persist); 154 callout_stop(tp->tt_keep); 155 callout_stop(tp->tt_rexmt); 156 } 157 158 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] = 159 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 }; 160 161 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 162 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 163 164 static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 165 166 /* 167 * TCP timer processing. 168 */ 169 170 void 171 tcp_timer_delack(xtp) 172 void *xtp; 173 { 174 struct tcpcb *tp = xtp; 175 int s; 176 struct inpcb *inp; 177 178 s = splnet(); 179 INP_INFO_RLOCK(&tcbinfo); 180 inp = tp->t_inpcb; 181 if (!inp) { 182 INP_INFO_RUNLOCK(&tcbinfo); 183 splx(s); 184 return; 185 } 186 INP_LOCK(inp); 187 INP_INFO_RUNLOCK(&tcbinfo); 188 if (callout_pending(tp->tt_delack) || !callout_active(tp->tt_delack)) { 189 INP_UNLOCK(inp); 190 splx(s); 191 return; 192 } 193 callout_deactivate(tp->tt_delack); 194 195 tp->t_flags |= TF_ACKNOW; 196 tcpstat.tcps_delack++; 197 (void) tcp_output(tp); 198 INP_UNLOCK(inp); 199 splx(s); 200 } 201 202 void 203 tcp_timer_2msl(xtp) 204 void *xtp; 205 { 206 struct tcpcb *tp = xtp; 207 int s; 208 struct inpcb *inp; 209 #ifdef TCPDEBUG 210 int ostate; 211 212 ostate = tp->t_state; 213 #endif 214 s = splnet(); 215 INP_INFO_WLOCK(&tcbinfo); 216 inp = tp->t_inpcb; 217 if (!inp) { 218 INP_INFO_WUNLOCK(&tcbinfo); 219 splx(s); 220 return; 221 } 222 INP_LOCK(inp); 223 if (callout_pending(tp->tt_2msl) || !callout_active(tp->tt_2msl)) { 224 INP_UNLOCK(tp->t_inpcb); 225 INP_INFO_WUNLOCK(&tcbinfo); 226 splx(s); 227 return; 228 } 229 callout_deactivate(tp->tt_2msl); 230 /* 231 * 2 MSL timeout in shutdown went off. If we're closed but 232 * still waiting for peer to close and connection has been idle 233 * too long, or if 2MSL time is up from TIME_WAIT, delete connection 234 * control block. Otherwise, check again in a bit. 235 */ 236 if (tp->t_state != TCPS_TIME_WAIT && 237 (ticks - tp->t_rcvtime) <= tcp_maxidle) 238 callout_reset(tp->tt_2msl, tcp_keepintvl, 239 tcp_timer_2msl, tp); 240 else 241 tp = tcp_close(tp); 242 243 #ifdef TCPDEBUG 244 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 245 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 246 PRU_SLOWTIMO); 247 #endif 248 if (tp) 249 INP_UNLOCK(inp); 250 INP_INFO_WUNLOCK(&tcbinfo); 251 splx(s); 252 } 253 254 void 255 tcp_timer_2msl_tw(xtw) 256 void *xtw; 257 { 258 struct tcptw *tw = xtw; 259 int s; 260 261 s = splnet(); 262 INP_INFO_WLOCK(&tcbinfo); 263 if (tw->tw_inpcb == NULL) { 264 INP_INFO_WUNLOCK(&tcbinfo); 265 splx(s); 266 return; 267 } 268 INP_LOCK(tw->tw_inpcb); 269 if (callout_pending(tw->tt_2msl) || !callout_active(tw->tt_2msl)) { 270 INP_UNLOCK(tw->tw_inpcb); 271 INP_INFO_WUNLOCK(&tcbinfo); 272 splx(s); 273 return; 274 } 275 callout_deactivate(tw->tt_2msl); 276 tcp_twclose(tw); 277 INP_INFO_WUNLOCK(&tcbinfo); 278 splx(s); 279 } 280 281 void 282 tcp_timer_keep(xtp) 283 void *xtp; 284 { 285 struct tcpcb *tp = xtp; 286 struct tcptemp *t_template; 287 int s; 288 struct inpcb *inp; 289 #ifdef TCPDEBUG 290 int ostate; 291 292 ostate = tp->t_state; 293 #endif 294 s = splnet(); 295 INP_INFO_WLOCK(&tcbinfo); 296 inp = tp->t_inpcb; 297 if (!inp) { 298 INP_INFO_WUNLOCK(&tcbinfo); 299 splx(s); 300 return; 301 } 302 INP_LOCK(inp); 303 if (callout_pending(tp->tt_keep) || !callout_active(tp->tt_keep)) { 304 INP_UNLOCK(inp); 305 INP_INFO_WUNLOCK(&tcbinfo); 306 splx(s); 307 return; 308 } 309 callout_deactivate(tp->tt_keep); 310 /* 311 * Keep-alive timer went off; send something 312 * or drop connection if idle for too long. 313 */ 314 tcpstat.tcps_keeptimeo++; 315 if (tp->t_state < TCPS_ESTABLISHED) 316 goto dropit; 317 if ((always_keepalive || 318 tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) && 319 tp->t_state <= TCPS_CLOSING) { 320 if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle) 321 goto dropit; 322 /* 323 * Send a packet designed to force a response 324 * if the peer is up and reachable: 325 * either an ACK if the connection is still alive, 326 * or an RST if the peer has closed the connection 327 * due to timeout or reboot. 328 * Using sequence number tp->snd_una-1 329 * causes the transmitted zero-length segment 330 * to lie outside the receive window; 331 * by the protocol spec, this requires the 332 * correspondent TCP to respond. 333 */ 334 tcpstat.tcps_keepprobe++; 335 t_template = tcpip_maketemplate(inp); 336 if (t_template) { 337 tcp_respond(tp, t_template->tt_ipgen, 338 &t_template->tt_t, (struct mbuf *)NULL, 339 tp->rcv_nxt, tp->snd_una - 1, 0); 340 (void) m_free(dtom(t_template)); 341 } 342 callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp); 343 } else 344 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 345 346 #ifdef TCPDEBUG 347 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 348 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 349 PRU_SLOWTIMO); 350 #endif 351 INP_UNLOCK(inp); 352 INP_INFO_WUNLOCK(&tcbinfo); 353 splx(s); 354 return; 355 356 dropit: 357 tcpstat.tcps_keepdrops++; 358 tp = tcp_drop(tp, ETIMEDOUT); 359 360 #ifdef TCPDEBUG 361 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 362 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 363 PRU_SLOWTIMO); 364 #endif 365 if (tp) 366 INP_UNLOCK(tp->t_inpcb); 367 INP_INFO_WUNLOCK(&tcbinfo); 368 splx(s); 369 } 370 371 void 372 tcp_timer_persist(xtp) 373 void *xtp; 374 { 375 struct tcpcb *tp = xtp; 376 int s; 377 struct inpcb *inp; 378 #ifdef TCPDEBUG 379 int ostate; 380 381 ostate = tp->t_state; 382 #endif 383 s = splnet(); 384 INP_INFO_WLOCK(&tcbinfo); 385 inp = tp->t_inpcb; 386 if (!inp) { 387 INP_INFO_WUNLOCK(&tcbinfo); 388 splx(s); 389 return; 390 } 391 INP_LOCK(inp); 392 if (callout_pending(tp->tt_persist) || !callout_active(tp->tt_persist)){ 393 INP_UNLOCK(inp); 394 INP_INFO_WUNLOCK(&tcbinfo); 395 splx(s); 396 return; 397 } 398 callout_deactivate(tp->tt_persist); 399 /* 400 * Persistance timer into zero window. 401 * Force a byte to be output, if possible. 402 */ 403 tcpstat.tcps_persisttimeo++; 404 /* 405 * Hack: if the peer is dead/unreachable, we do not 406 * time out if the window is closed. After a full 407 * backoff, drop the connection if the idle time 408 * (no responses to probes) reaches the maximum 409 * backoff that we would use if retransmitting. 410 */ 411 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 412 ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle || 413 (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 414 tcpstat.tcps_persistdrop++; 415 tp = tcp_drop(tp, ETIMEDOUT); 416 goto out; 417 } 418 tcp_setpersist(tp); 419 tp->t_force = 1; 420 (void) tcp_output(tp); 421 tp->t_force = 0; 422 423 out: 424 #ifdef TCPDEBUG 425 if (tp && tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 426 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 427 PRU_SLOWTIMO); 428 #endif 429 if (tp) 430 INP_UNLOCK(inp); 431 INP_INFO_WUNLOCK(&tcbinfo); 432 splx(s); 433 } 434 435 void 436 tcp_timer_rexmt(xtp) 437 void *xtp; 438 { 439 struct tcpcb *tp = xtp; 440 int s; 441 int rexmt; 442 int headlocked; 443 struct inpcb *inp; 444 #ifdef TCPDEBUG 445 int ostate; 446 447 ostate = tp->t_state; 448 #endif 449 s = splnet(); 450 INP_INFO_WLOCK(&tcbinfo); 451 headlocked = 1; 452 inp = tp->t_inpcb; 453 if (!inp) { 454 INP_INFO_WUNLOCK(&tcbinfo); 455 splx(s); 456 return; 457 } 458 INP_LOCK(inp); 459 if (callout_pending(tp->tt_rexmt) || !callout_active(tp->tt_rexmt)) { 460 INP_UNLOCK(inp); 461 INP_INFO_WUNLOCK(&tcbinfo); 462 splx(s); 463 return; 464 } 465 callout_deactivate(tp->tt_rexmt); 466 /* 467 * Retransmission timer went off. Message has not 468 * been acked within retransmit interval. Back off 469 * to a longer retransmit interval and retransmit one segment. 470 */ 471 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 472 tp->t_rxtshift = TCP_MAXRXTSHIFT; 473 tcpstat.tcps_timeoutdrop++; 474 tp = tcp_drop(tp, tp->t_softerror ? 475 tp->t_softerror : ETIMEDOUT); 476 goto out; 477 } 478 INP_INFO_WUNLOCK(&tcbinfo); 479 headlocked = 0; 480 if (tp->t_rxtshift == 1) { 481 /* 482 * first retransmit; record ssthresh and cwnd so they can 483 * be recovered if this turns out to be a "bad" retransmit. 484 * A retransmit is considered "bad" if an ACK for this 485 * segment is received within RTT/2 interval; the assumption 486 * here is that the ACK was already in flight. See 487 * "On Estimating End-to-End Network Path Properties" by 488 * Allman and Paxson for more details. 489 */ 490 tp->snd_cwnd_prev = tp->snd_cwnd; 491 tp->snd_ssthresh_prev = tp->snd_ssthresh; 492 tp->snd_high_prev = tp->snd_high; 493 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 494 } 495 tcpstat.tcps_rexmttimeo++; 496 if (tp->t_state == TCPS_SYN_SENT) 497 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift]; 498 else 499 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 500 TCPT_RANGESET(tp->t_rxtcur, rexmt, 501 tp->t_rttmin, TCPTV_REXMTMAX); 502 /* 503 * Disable rfc1323 and rfc1644 if we havn't got any response to 504 * our third SYN to work-around some broken terminal servers 505 * (most of which have hopefully been retired) that have bad VJ 506 * header compression code which trashes TCP segments containing 507 * unknown-to-them TCP options. 508 */ 509 if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3)) 510 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC); 511 /* 512 * If losing, let the lower level know and try for 513 * a better route. Also, if we backed off this far, 514 * our srtt estimate is probably bogus. Clobber it 515 * so we'll take the next rtt measurement as our srtt; 516 * move the current srtt into rttvar to keep the current 517 * retransmit times until then. 518 */ 519 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 520 #ifdef INET6 521 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 522 in6_losing(tp->t_inpcb); 523 else 524 #endif 525 in_losing(tp->t_inpcb); 526 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 527 tp->t_srtt = 0; 528 } 529 tp->snd_nxt = tp->snd_una; 530 tp->snd_high = tp->snd_max; 531 /* 532 * Force a segment to be sent. 533 */ 534 tp->t_flags |= TF_ACKNOW; 535 /* 536 * If timing a segment in this window, stop the timer. 537 */ 538 tp->t_rtttime = 0; 539 /* 540 * Close the congestion window down to one segment 541 * (we'll open it by one segment for each ack we get). 542 * Since we probably have a window's worth of unacked 543 * data accumulated, this "slow start" keeps us from 544 * dumping all that data as back-to-back packets (which 545 * might overwhelm an intermediate gateway). 546 * 547 * There are two phases to the opening: Initially we 548 * open by one mss on each ack. This makes the window 549 * size increase exponentially with time. If the 550 * window is larger than the path can handle, this 551 * exponential growth results in dropped packet(s) 552 * almost immediately. To get more time between 553 * drops but still "push" the network to take advantage 554 * of improving conditions, we switch from exponential 555 * to linear window opening at some threshhold size. 556 * For a threshhold, we use half the current window 557 * size, truncated to a multiple of the mss. 558 * 559 * (the minimum cwnd that will give us exponential 560 * growth is 2 mss. We don't allow the threshhold 561 * to go below this.) 562 */ 563 { 564 u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 565 if (win < 2) 566 win = 2; 567 tp->snd_cwnd = tp->t_maxseg; 568 tp->snd_ssthresh = win * tp->t_maxseg; 569 tp->t_dupacks = 0; 570 } 571 (void) tcp_output(tp); 572 573 out: 574 #ifdef TCPDEBUG 575 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 576 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 577 PRU_SLOWTIMO); 578 #endif 579 if (tp) 580 INP_UNLOCK(inp); 581 if (headlocked) 582 INP_INFO_WUNLOCK(&tcbinfo); 583 splx(s); 584 } 585