1 /* $OpenBSD: tcp_timer.c,v 1.43 2009/06/05 00:05:22 claudio Exp $ */ 2 /* $NetBSD: tcp_timer.c,v 1.14 1996/02/13 23:44:09 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)tcp_timer.c 8.1 (Berkeley) 6/10/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/mbuf.h> 38 #include <sys/socket.h> 39 #include <sys/socketvar.h> 40 #include <sys/protosw.h> 41 #include <sys/kernel.h> 42 #include <sys/pool.h> 43 44 #include <net/route.h> 45 46 #include <netinet/in.h> 47 #include <netinet/in_systm.h> 48 #include <netinet/ip.h> 49 #include <netinet/in_pcb.h> 50 #include <netinet/ip_var.h> 51 #include <netinet/tcp.h> 52 #include <netinet/tcp_fsm.h> 53 #include <netinet/tcp_timer.h> 54 #include <netinet/tcp_var.h> 55 #include <netinet/ip_icmp.h> 56 #include <netinet/tcp_seq.h> 57 58 int tcp_keepidle; 59 int tcp_keepintvl; 60 int tcp_maxpersistidle; /* max idle time in persist */ 61 int tcp_maxidle; 62 63 /* 64 * Time to delay the ACK. This is initialized in tcp_init(), unless 65 * its patched. 66 */ 67 int tcp_delack_ticks; 68 69 void tcp_timer_rexmt(void *); 70 void tcp_timer_persist(void *); 71 void tcp_timer_keep(void *); 72 void tcp_timer_2msl(void *); 73 74 const tcp_timer_func_t tcp_timer_funcs[TCPT_NTIMERS] = { 75 tcp_timer_rexmt, 76 tcp_timer_persist, 77 tcp_timer_keep, 78 tcp_timer_2msl, 79 }; 80 81 /* 82 * Timer state initialization, called from tcp_init(). 83 */ 84 void 85 tcp_timer_init(void) 86 { 87 88 if (tcp_keepidle == 0) 89 tcp_keepidle = TCPTV_KEEP_IDLE; 90 91 if (tcp_keepintvl == 0) 92 tcp_keepintvl = TCPTV_KEEPINTVL; 93 94 if (tcp_maxpersistidle == 0) 95 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 96 97 if (tcp_delack_ticks == 0) 98 tcp_delack_ticks = TCP_DELACK_TICKS; 99 } 100 101 /* 102 * Callout to process delayed ACKs for a TCPCB. 103 */ 104 void 105 tcp_delack(void *arg) 106 { 107 struct tcpcb *tp = arg; 108 int s; 109 110 /* 111 * If tcp_output() wasn't able to transmit the ACK 112 * for whatever reason, it will restart the delayed 113 * ACK callout. 114 */ 115 116 s = splsoftnet(); 117 if (tp->t_flags & TF_DEAD) { 118 splx(s); 119 return; 120 } 121 tp->t_flags |= TF_ACKNOW; 122 (void) tcp_output(tp); 123 splx(s); 124 } 125 126 /* 127 * Tcp protocol timeout routine called every 500 ms. 128 * Updates the timers in all active tcb's and 129 * causes finite state machine actions if timers expire. 130 */ 131 void 132 tcp_slowtimo() 133 { 134 int s; 135 136 s = splsoftnet(); 137 tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl; 138 tcp_iss += TCP_ISSINCR2/PR_SLOWHZ; /* increment iss */ 139 tcp_now++; /* for timestamps */ 140 splx(s); 141 } 142 143 /* 144 * Cancel all timers for TCP tp. 145 */ 146 void 147 tcp_canceltimers(tp) 148 struct tcpcb *tp; 149 { 150 int i; 151 152 for (i = 0; i < TCPT_NTIMERS; i++) 153 TCP_TIMER_DISARM(tp, i); 154 } 155 156 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 157 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 158 159 int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 160 161 /* 162 * TCP timer processing. 163 */ 164 165 #ifdef TCP_SACK 166 void tcp_timer_freesack(struct tcpcb *); 167 168 void 169 tcp_timer_freesack(struct tcpcb *tp) 170 { 171 struct sackhole *p, *q; 172 /* 173 * Free SACK holes for 2MSL and REXMT timers. 174 */ 175 q = tp->snd_holes; 176 while (q != NULL) { 177 p = q; 178 q = q->next; 179 pool_put(&sackhl_pool, p); 180 } 181 tp->snd_holes = 0; 182 #ifdef TCP_FACK 183 tp->snd_fack = tp->snd_una; 184 tp->retran_data = 0; 185 tp->snd_awnd = 0; 186 #endif /* TCP_FACK */ 187 } 188 #endif /* TCP_SACK */ 189 190 void 191 tcp_timer_rexmt(void *arg) 192 { 193 struct tcpcb *tp = arg; 194 uint32_t rto; 195 int s; 196 197 s = splsoftnet(); 198 if (tp->t_flags & TF_DEAD) { 199 splx(s); 200 return; 201 } 202 203 if ((tp->t_flags & TF_PMTUD_PEND) && tp->t_inpcb && 204 SEQ_GEQ(tp->t_pmtud_th_seq, tp->snd_una) && 205 SEQ_LT(tp->t_pmtud_th_seq, (int)(tp->snd_una + tp->t_maxseg))) { 206 extern struct sockaddr_in icmpsrc; 207 struct icmp icmp; 208 209 tp->t_flags &= ~TF_PMTUD_PEND; 210 211 /* XXX create fake icmp message with relevant entries */ 212 icmp.icmp_nextmtu = tp->t_pmtud_nextmtu; 213 icmp.icmp_ip.ip_len = tp->t_pmtud_ip_len; 214 icmp.icmp_ip.ip_hl = tp->t_pmtud_ip_hl; 215 icmpsrc.sin_addr = tp->t_inpcb->inp_faddr; 216 icmp_mtudisc(&icmp, tp->t_inpcb->inp_rdomain); 217 218 /* 219 * Notify all connections to the same peer about 220 * new mss and trigger retransmit. 221 */ 222 in_pcbnotifyall(&tcbtable, sintosa(&icmpsrc), EMSGSIZE, 223 tcp_mtudisc); 224 splx(s); 225 return; 226 } 227 228 #ifdef TCP_SACK 229 tcp_timer_freesack(tp); 230 #endif 231 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 232 tp->t_rxtshift = TCP_MAXRXTSHIFT; 233 tcpstat.tcps_timeoutdrop++; 234 (void)tcp_drop(tp, tp->t_softerror ? 235 tp->t_softerror : ETIMEDOUT); 236 goto out; 237 } 238 tcpstat.tcps_rexmttimeo++; 239 rto = TCP_REXMTVAL(tp); 240 if (rto < tp->t_rttmin) 241 rto = tp->t_rttmin; 242 TCPT_RANGESET(tp->t_rxtcur, 243 rto * tcp_backoff[tp->t_rxtshift], 244 tp->t_rttmin, TCPTV_REXMTMAX); 245 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 246 247 /* 248 * If we are losing and we are trying path MTU discovery, 249 * try turning it off. This will avoid black holes in 250 * the network which suppress or fail to send "packet 251 * too big" ICMP messages. We should ideally do 252 * lots more sophisticated searching to find the right 253 * value here... 254 */ 255 if (ip_mtudisc && tp->t_inpcb && 256 TCPS_HAVEESTABLISHED(tp->t_state) && 257 tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) { 258 struct inpcb *inp = tp->t_inpcb; 259 struct rtentry *rt = NULL; 260 struct sockaddr_in sin; 261 262 /* No data to send means path mtu is not a problem */ 263 if (!inp->inp_socket->so_snd.sb_cc) 264 goto leave; 265 266 rt = in_pcbrtentry(inp); 267 /* Check if path MTU discovery is disabled already */ 268 if (rt && (rt->rt_flags & RTF_HOST) && 269 (rt->rt_rmx.rmx_locks & RTV_MTU)) 270 goto leave; 271 272 rt = NULL; 273 switch(tp->pf) { 274 #ifdef INET6 275 case PF_INET6: 276 /* 277 * We can not turn off path MTU for IPv6. 278 * Do nothing for now, maybe lower to 279 * minimum MTU. 280 */ 281 break; 282 #endif 283 case PF_INET: 284 bzero(&sin, sizeof(struct sockaddr_in)); 285 sin.sin_family = AF_INET; 286 sin.sin_len = sizeof(struct sockaddr_in); 287 sin.sin_addr = inp->inp_faddr; 288 rt = icmp_mtudisc_clone(sintosa(&sin), 289 inp->inp_rdomain); 290 break; 291 } 292 if (rt != NULL) { 293 /* Disable path MTU discovery */ 294 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) { 295 rt->rt_rmx.rmx_locks |= RTV_MTU; 296 in_rtchange(inp, 0); 297 } 298 299 rtfree(rt); 300 } 301 leave: 302 ; 303 } 304 305 /* 306 * If losing, let the lower level know and try for 307 * a better route. Also, if we backed off this far, 308 * our srtt estimate is probably bogus. Clobber it 309 * so we'll take the next rtt measurement as our srtt; 310 * move the current srtt into rttvar to keep the current 311 * retransmit times until then. 312 */ 313 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 314 in_losing(tp->t_inpcb); 315 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 316 tp->t_srtt = 0; 317 } 318 tp->snd_nxt = tp->snd_una; 319 #if defined(TCP_SACK) 320 /* 321 * Note: We overload snd_last to function also as the 322 * snd_last variable described in RFC 2582 323 */ 324 tp->snd_last = tp->snd_max; 325 #endif /* TCP_SACK */ 326 /* 327 * If timing a segment in this window, stop the timer. 328 */ 329 tp->t_rtttime = 0; 330 #ifdef TCP_ECN 331 /* 332 * if ECN is enabled, there might be a broken firewall which 333 * blocks ecn packets. fall back to non-ecn. 334 */ 335 if ((tp->t_state == TCPS_SYN_SENT || tp->t_state == TCPS_SYN_RECEIVED) 336 && tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 337 tp->t_flags |= TF_DISABLE_ECN; 338 #endif 339 /* 340 * Close the congestion window down to one segment 341 * (we'll open it by one segment for each ack we get). 342 * Since we probably have a window's worth of unacked 343 * data accumulated, this "slow start" keeps us from 344 * dumping all that data as back-to-back packets (which 345 * might overwhelm an intermediate gateway). 346 * 347 * There are two phases to the opening: Initially we 348 * open by one mss on each ack. This makes the window 349 * size increase exponentially with time. If the 350 * window is larger than the path can handle, this 351 * exponential growth results in dropped packet(s) 352 * almost immediately. To get more time between 353 * drops but still "push" the network to take advantage 354 * of improving conditions, we switch from exponential 355 * to linear window opening at some threshold size. 356 * For a threshold, we use half the current window 357 * size, truncated to a multiple of the mss. 358 * 359 * (the minimum cwnd that will give us exponential 360 * growth is 2 mss. We don't allow the threshold 361 * to go below this.) 362 */ 363 { 364 u_long win = ulmin(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 365 if (win < 2) 366 win = 2; 367 tp->snd_cwnd = tp->t_maxseg; 368 tp->snd_ssthresh = win * tp->t_maxseg; 369 tp->t_dupacks = 0; 370 #ifdef TCP_ECN 371 tp->snd_last = tp->snd_max; 372 tp->t_flags |= TF_SEND_CWR; 373 #endif 374 #if 1 /* TCP_ECN */ 375 tcpstat.tcps_cwr_timeout++; 376 #endif 377 } 378 (void) tcp_output(tp); 379 380 out: 381 splx(s); 382 } 383 384 void 385 tcp_timer_persist(void *arg) 386 { 387 struct tcpcb *tp = arg; 388 uint32_t rto; 389 int s; 390 391 s = splsoftnet(); 392 if ((tp->t_flags & TF_DEAD) || 393 TCP_TIMER_ISARMED(tp, TCPT_REXMT)) { 394 splx(s); 395 return; 396 } 397 tcpstat.tcps_persisttimeo++; 398 /* 399 * Hack: if the peer is dead/unreachable, we do not 400 * time out if the window is closed. After a full 401 * backoff, drop the connection if the idle time 402 * (no responses to probes) reaches the maximum 403 * backoff that we would use if retransmitting. 404 */ 405 rto = TCP_REXMTVAL(tp); 406 if (rto < tp->t_rttmin) 407 rto = tp->t_rttmin; 408 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 409 ((tcp_now - tp->t_rcvtime) >= tcp_maxpersistidle || 410 (tcp_now - tp->t_rcvtime) >= rto * tcp_totbackoff)) { 411 tcpstat.tcps_persistdrop++; 412 tp = tcp_drop(tp, ETIMEDOUT); 413 goto out; 414 } 415 tcp_setpersist(tp); 416 tp->t_force = 1; 417 (void) tcp_output(tp); 418 tp->t_force = 0; 419 out: 420 splx(s); 421 } 422 423 void 424 tcp_timer_keep(void *arg) 425 { 426 struct tcpcb *tp = arg; 427 int s; 428 429 s = splsoftnet(); 430 if (tp->t_flags & TF_DEAD) { 431 splx(s); 432 return; 433 } 434 435 tcpstat.tcps_keeptimeo++; 436 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 437 goto dropit; 438 if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE && 439 tp->t_state <= TCPS_CLOSING) { 440 if ((tcp_maxidle > 0) && 441 ((tcp_now - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)) 442 goto dropit; 443 /* 444 * Send a packet designed to force a response 445 * if the peer is up and reachable: 446 * either an ACK if the connection is still alive, 447 * or an RST if the peer has closed the connection 448 * due to timeout or reboot. 449 * Using sequence number tp->snd_una-1 450 * causes the transmitted zero-length segment 451 * to lie outside the receive window; 452 * by the protocol spec, this requires the 453 * correspondent TCP to respond. 454 */ 455 tcpstat.tcps_keepprobe++; 456 tcp_respond(tp, mtod(tp->t_template, caddr_t), 457 NULL, tp->rcv_nxt, tp->snd_una - 1, 0); 458 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepintvl); 459 } else 460 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 461 462 splx(s); 463 return; 464 465 dropit: 466 tcpstat.tcps_keepdrops++; 467 tp = tcp_drop(tp, ETIMEDOUT); 468 469 splx(s); 470 } 471 472 void 473 tcp_timer_2msl(void *arg) 474 { 475 struct tcpcb *tp = arg; 476 int s; 477 478 s = splsoftnet(); 479 if (tp->t_flags & TF_DEAD) { 480 splx(s); 481 return; 482 } 483 484 #ifdef TCP_SACK 485 tcp_timer_freesack(tp); 486 #endif 487 488 if (tp->t_state != TCPS_TIME_WAIT && 489 ((tcp_maxidle == 0) || ((tcp_now - tp->t_rcvtime) <= tcp_maxidle))) 490 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_keepintvl); 491 else 492 tp = tcp_close(tp); 493 494 splx(s); 495 } 496