1 /* $OpenBSD: tcp_timer.c,v 1.60 2017/10/29 14:56:36 florian 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/ip.h> 48 #include <netinet/in_pcb.h> 49 #include <netinet/ip_var.h> 50 #include <netinet/tcp.h> 51 #include <netinet/tcp_fsm.h> 52 #include <netinet/tcp_timer.h> 53 #include <netinet/tcp_var.h> 54 #include <netinet/ip_icmp.h> 55 #include <netinet/tcp_seq.h> 56 57 int tcp_always_keepalive; 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 109 /* 110 * If tcp_output() wasn't able to transmit the ACK 111 * for whatever reason, it will restart the delayed 112 * ACK callout. 113 */ 114 NET_LOCK(); 115 if (tp->t_flags & TF_DEAD) 116 goto out; 117 tp->t_flags |= TF_ACKNOW; 118 (void) tcp_output(tp); 119 out: 120 NET_UNLOCK(); 121 } 122 123 /* 124 * Tcp protocol timeout routine called every 500 ms. 125 * Updates the timers in all active tcb's and 126 * causes finite state machine actions if timers expire. 127 */ 128 void 129 tcp_slowtimo(void) 130 { 131 NET_LOCK(); 132 133 tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl; 134 tcp_iss += TCP_ISSINCR2/PR_SLOWHZ; /* increment iss */ 135 tcp_now++; /* for timestamps */ 136 137 NET_UNLOCK(); 138 } 139 140 /* 141 * Cancel all timers for TCP tp. 142 */ 143 void 144 tcp_canceltimers(struct tcpcb *tp) 145 { 146 int i; 147 148 for (i = 0; i < TCPT_NTIMERS; i++) 149 TCP_TIMER_DISARM(tp, i); 150 } 151 152 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 153 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; 154 155 int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ 156 157 /* 158 * TCP timer processing. 159 */ 160 161 void tcp_timer_freesack(struct tcpcb *); 162 163 void 164 tcp_timer_freesack(struct tcpcb *tp) 165 { 166 struct sackhole *p, *q; 167 /* 168 * Free SACK holes for 2MSL and REXMT timers. 169 */ 170 q = tp->snd_holes; 171 while (q != NULL) { 172 p = q; 173 q = q->next; 174 pool_put(&sackhl_pool, p); 175 } 176 tp->snd_holes = 0; 177 } 178 179 void 180 tcp_timer_rexmt(void *arg) 181 { 182 struct tcpcb *tp = arg; 183 uint32_t rto; 184 185 NET_LOCK(); 186 if (tp->t_flags & TF_DEAD) 187 goto out; 188 189 if ((tp->t_flags & TF_PMTUD_PEND) && tp->t_inpcb && 190 SEQ_GEQ(tp->t_pmtud_th_seq, tp->snd_una) && 191 SEQ_LT(tp->t_pmtud_th_seq, (int)(tp->snd_una + tp->t_maxseg))) { 192 struct sockaddr_in sin; 193 struct icmp icmp; 194 195 tp->t_flags &= ~TF_PMTUD_PEND; 196 197 /* XXX create fake icmp message with relevant entries */ 198 icmp.icmp_nextmtu = tp->t_pmtud_nextmtu; 199 icmp.icmp_ip.ip_len = tp->t_pmtud_ip_len; 200 icmp.icmp_ip.ip_hl = tp->t_pmtud_ip_hl; 201 icmp.icmp_ip.ip_dst = tp->t_inpcb->inp_faddr; 202 icmp_mtudisc(&icmp, tp->t_inpcb->inp_rtableid); 203 204 /* 205 * Notify all connections to the same peer about 206 * new mss and trigger retransmit. 207 */ 208 bzero(&sin, sizeof(sin)); 209 sin.sin_len = sizeof(sin); 210 sin.sin_family = AF_INET; 211 sin.sin_addr = tp->t_inpcb->inp_faddr; 212 in_pcbnotifyall(&tcbtable, sintosa(&sin), 213 tp->t_inpcb->inp_rtableid, EMSGSIZE, tcp_mtudisc); 214 goto out; 215 } 216 217 tcp_timer_freesack(tp); 218 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 219 tp->t_rxtshift = TCP_MAXRXTSHIFT; 220 tcpstat_inc(tcps_timeoutdrop); 221 (void)tcp_drop(tp, tp->t_softerror ? 222 tp->t_softerror : ETIMEDOUT); 223 goto out; 224 } 225 tcpstat_inc(tcps_rexmttimeo); 226 rto = TCP_REXMTVAL(tp); 227 if (rto < tp->t_rttmin) 228 rto = tp->t_rttmin; 229 TCPT_RANGESET(tp->t_rxtcur, 230 rto * tcp_backoff[tp->t_rxtshift], 231 tp->t_rttmin, TCPTV_REXMTMAX); 232 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 233 234 /* 235 * If we are losing and we are trying path MTU discovery, 236 * try turning it off. This will avoid black holes in 237 * the network which suppress or fail to send "packet 238 * too big" ICMP messages. We should ideally do 239 * lots more sophisticated searching to find the right 240 * value here... 241 */ 242 if (ip_mtudisc && tp->t_inpcb && 243 TCPS_HAVEESTABLISHED(tp->t_state) && 244 tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) { 245 struct inpcb *inp = tp->t_inpcb; 246 struct rtentry *rt = NULL; 247 248 /* No data to send means path mtu is not a problem */ 249 if (!inp->inp_socket->so_snd.sb_cc) 250 goto leave; 251 252 rt = in_pcbrtentry(inp); 253 /* Check if path MTU discovery is disabled already */ 254 if (rt && (rt->rt_flags & RTF_HOST) && 255 (rt->rt_locks & RTV_MTU)) 256 goto leave; 257 258 rt = NULL; 259 switch(tp->pf) { 260 #ifdef INET6 261 case PF_INET6: 262 /* 263 * We can not turn off path MTU for IPv6. 264 * Do nothing for now, maybe lower to 265 * minimum MTU. 266 */ 267 break; 268 #endif 269 case PF_INET: 270 rt = icmp_mtudisc_clone(inp->inp_faddr, 271 inp->inp_rtableid); 272 break; 273 } 274 if (rt != NULL) { 275 /* Disable path MTU discovery */ 276 if ((rt->rt_locks & RTV_MTU) == 0) { 277 rt->rt_locks |= RTV_MTU; 278 in_rtchange(inp, 0); 279 } 280 281 rtfree(rt); 282 } 283 leave: 284 ; 285 } 286 287 /* 288 * If losing, let the lower level know and try for 289 * a better route. Also, if we backed off this far, 290 * our srtt estimate is probably bogus. Clobber it 291 * so we'll take the next rtt measurement as our srtt; 292 * move the current srtt into rttvar to keep the current 293 * retransmit times until then. 294 */ 295 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { 296 in_losing(tp->t_inpcb); 297 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); 298 tp->t_srtt = 0; 299 } 300 tp->snd_nxt = tp->snd_una; 301 /* 302 * Note: We overload snd_last to function also as the 303 * snd_last variable described in RFC 2582 304 */ 305 tp->snd_last = tp->snd_max; 306 /* 307 * If timing a segment in this window, stop the timer. 308 */ 309 tp->t_rtttime = 0; 310 #ifdef TCP_ECN 311 /* 312 * if ECN is enabled, there might be a broken firewall which 313 * blocks ecn packets. fall back to non-ecn. 314 */ 315 if ((tp->t_state == TCPS_SYN_SENT || tp->t_state == TCPS_SYN_RECEIVED) 316 && tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 317 tp->t_flags |= TF_DISABLE_ECN; 318 #endif 319 /* 320 * Close the congestion window down to one segment 321 * (we'll open it by one segment for each ack we get). 322 * Since we probably have a window's worth of unacked 323 * data accumulated, this "slow start" keeps us from 324 * dumping all that data as back-to-back packets (which 325 * might overwhelm an intermediate gateway). 326 * 327 * There are two phases to the opening: Initially we 328 * open by one mss on each ack. This makes the window 329 * size increase exponentially with time. If the 330 * window is larger than the path can handle, this 331 * exponential growth results in dropped packet(s) 332 * almost immediately. To get more time between 333 * drops but still "push" the network to take advantage 334 * of improving conditions, we switch from exponential 335 * to linear window opening at some threshold size. 336 * For a threshold, we use half the current window 337 * size, truncated to a multiple of the mss. 338 * 339 * (the minimum cwnd that will give us exponential 340 * growth is 2 mss. We don't allow the threshold 341 * to go below this.) 342 */ 343 { 344 u_long win = ulmin(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg; 345 if (win < 2) 346 win = 2; 347 tp->snd_cwnd = tp->t_maxseg; 348 tp->snd_ssthresh = win * tp->t_maxseg; 349 tp->t_dupacks = 0; 350 #ifdef TCP_ECN 351 tp->snd_last = tp->snd_max; 352 tp->t_flags |= TF_SEND_CWR; 353 #endif 354 #if 1 /* TCP_ECN */ 355 tcpstat_inc(tcps_cwr_timeout); 356 #endif 357 } 358 (void) tcp_output(tp); 359 360 out: 361 NET_UNLOCK(); 362 } 363 364 void 365 tcp_timer_persist(void *arg) 366 { 367 struct tcpcb *tp = arg; 368 uint32_t rto; 369 370 NET_LOCK(); 371 if ((tp->t_flags & TF_DEAD) || 372 TCP_TIMER_ISARMED(tp, TCPT_REXMT)) { 373 goto out; 374 } 375 tcpstat_inc(tcps_persisttimeo); 376 /* 377 * Hack: if the peer is dead/unreachable, we do not 378 * time out if the window is closed. After a full 379 * backoff, drop the connection if the idle time 380 * (no responses to probes) reaches the maximum 381 * backoff that we would use if retransmitting. 382 */ 383 rto = TCP_REXMTVAL(tp); 384 if (rto < tp->t_rttmin) 385 rto = tp->t_rttmin; 386 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 387 ((tcp_now - tp->t_rcvtime) >= tcp_maxpersistidle || 388 (tcp_now - tp->t_rcvtime) >= rto * tcp_totbackoff)) { 389 tcpstat_inc(tcps_persistdrop); 390 tp = tcp_drop(tp, ETIMEDOUT); 391 goto out; 392 } 393 tcp_setpersist(tp); 394 tp->t_force = 1; 395 (void) tcp_output(tp); 396 tp->t_force = 0; 397 out: 398 NET_UNLOCK(); 399 } 400 401 void 402 tcp_timer_keep(void *arg) 403 { 404 struct tcpcb *tp = arg; 405 406 NET_LOCK(); 407 if (tp->t_flags & TF_DEAD) 408 goto out; 409 410 tcpstat_inc(tcps_keeptimeo); 411 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 412 goto dropit; 413 if ((tcp_always_keepalive || 414 tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) && 415 tp->t_state <= TCPS_CLOSING) { 416 if ((tcp_maxidle > 0) && 417 ((tcp_now - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)) 418 goto dropit; 419 /* 420 * Send a packet designed to force a response 421 * if the peer is up and reachable: 422 * either an ACK if the connection is still alive, 423 * or an RST if the peer has closed the connection 424 * due to timeout or reboot. 425 * Using sequence number tp->snd_una-1 426 * causes the transmitted zero-length segment 427 * to lie outside the receive window; 428 * by the protocol spec, this requires the 429 * correspondent TCP to respond. 430 */ 431 tcpstat_inc(tcps_keepprobe); 432 tcp_respond(tp, mtod(tp->t_template, caddr_t), 433 NULL, tp->rcv_nxt, tp->snd_una - 1, 0, 0); 434 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepintvl); 435 } else 436 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 437 out: 438 NET_UNLOCK(); 439 return; 440 441 dropit: 442 tcpstat_inc(tcps_keepdrops); 443 tp = tcp_drop(tp, ETIMEDOUT); 444 NET_UNLOCK(); 445 } 446 447 void 448 tcp_timer_2msl(void *arg) 449 { 450 struct tcpcb *tp = arg; 451 452 NET_LOCK(); 453 if (tp->t_flags & TF_DEAD) 454 goto out; 455 456 tcp_timer_freesack(tp); 457 458 if (tp->t_state != TCPS_TIME_WAIT && 459 ((tcp_maxidle == 0) || ((tcp_now - tp->t_rcvtime) <= tcp_maxidle))) 460 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_keepintvl); 461 else 462 tp = tcp_close(tp); 463 464 out: 465 NET_UNLOCK(); 466 } 467