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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 #include "opt_tcpdebug.h" 38 #include "opt_rss.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/smp.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sysctl.h> 50 #include <sys/systm.h> 51 52 #include <net/if.h> 53 #include <net/route.h> 54 #include <net/rss_config.h> 55 #include <net/vnet.h> 56 #include <net/netisr.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_kdtrace.h> 60 #include <netinet/in_pcb.h> 61 #include <netinet/in_rss.h> 62 #include <netinet/in_systm.h> 63 #ifdef INET6 64 #include <netinet6/in6_pcb.h> 65 #endif 66 #include <netinet/ip_var.h> 67 #include <netinet/tcp.h> 68 #include <netinet/tcp_fsm.h> 69 #include <netinet/tcp_timer.h> 70 #include <netinet/tcp_var.h> 71 #include <netinet/cc/cc.h> 72 #ifdef INET6 73 #include <netinet6/tcp6_var.h> 74 #endif 75 #include <netinet/tcpip.h> 76 #ifdef TCPDEBUG 77 #include <netinet/tcp_debug.h> 78 #endif 79 80 int tcp_persmin; 81 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmin, CTLTYPE_INT|CTLFLAG_RW, 82 &tcp_persmin, 0, sysctl_msec_to_ticks, "I", "minimum persistence interval"); 83 84 int tcp_persmax; 85 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax, CTLTYPE_INT|CTLFLAG_RW, 86 &tcp_persmax, 0, sysctl_msec_to_ticks, "I", "maximum persistence interval"); 87 88 int tcp_keepinit; 89 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW, 90 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection"); 91 92 int tcp_keepidle; 93 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW, 94 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin"); 95 96 int tcp_keepintvl; 97 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW, 98 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes"); 99 100 int tcp_delacktime; 101 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW, 102 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I", 103 "Time before a delayed ACK is sent"); 104 105 int tcp_msl; 106 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW, 107 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime"); 108 109 int tcp_rexmit_min; 110 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW, 111 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I", 112 "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", 117 "Retransmission Timer Slop"); 118 119 static int always_keepalive = 1; 120 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW, 121 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections"); 122 123 int tcp_fast_finwait2_recycle = 0; 124 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW, 125 &tcp_fast_finwait2_recycle, 0, 126 "Recycle closed FIN_WAIT_2 connections faster"); 127 128 int tcp_finwait2_timeout; 129 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW, 130 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout"); 131 132 int tcp_keepcnt = TCPTV_KEEPCNT; 133 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0, 134 "Number of keepalive probes to send"); 135 136 /* max idle probes */ 137 int tcp_maxpersistidle; 138 139 static int tcp_rexmit_drop_options = 0; 140 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW, 141 &tcp_rexmit_drop_options, 0, 142 "Drop TCP options from 3rd and later retransmitted SYN"); 143 144 static VNET_DEFINE(int, tcp_pmtud_blackhole_detect); 145 #define V_tcp_pmtud_blackhole_detect VNET(tcp_pmtud_blackhole_detect) 146 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection, 147 CTLFLAG_RW|CTLFLAG_VNET, 148 &VNET_NAME(tcp_pmtud_blackhole_detect), 0, 149 "Path MTU Discovery Black Hole Detection Enabled"); 150 151 #ifdef INET 152 static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200; 153 #define V_tcp_pmtud_blackhole_mss VNET(tcp_pmtud_blackhole_mss) 154 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss, 155 CTLFLAG_RW|CTLFLAG_VNET, 156 &VNET_NAME(tcp_pmtud_blackhole_mss), 0, 157 "Path MTU Discovery Black Hole Detection lowered MSS"); 158 #endif 159 160 #ifdef INET6 161 static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220; 162 #define V_tcp_v6pmtud_blackhole_mss VNET(tcp_v6pmtud_blackhole_mss) 163 SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss, 164 CTLFLAG_RW|CTLFLAG_VNET, 165 &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0, 166 "Path MTU Discovery IPv6 Black Hole Detection lowered MSS"); 167 #endif 168 169 #ifdef RSS 170 static int per_cpu_timers = 1; 171 #else 172 static int per_cpu_timers = 0; 173 #endif 174 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW, 175 &per_cpu_timers , 0, "run tcp timers on all cpus"); 176 177 #if 0 178 #define INP_CPU(inp) (per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \ 179 ((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0) 180 #endif 181 182 /* 183 * Map the given inp to a CPU id. 184 * 185 * This queries RSS if it's compiled in, else it defaults to the current 186 * CPU ID. 187 */ 188 static inline int 189 inp_to_cpuid(struct inpcb *inp) 190 { 191 u_int cpuid; 192 193 #ifdef RSS 194 if (per_cpu_timers) { 195 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype); 196 if (cpuid == NETISR_CPUID_NONE) 197 return (curcpu); /* XXX */ 198 else 199 return (cpuid); 200 } 201 #else 202 /* Legacy, pre-RSS behaviour */ 203 if (per_cpu_timers) { 204 /* 205 * We don't have a flowid -> cpuid mapping, so cheat and 206 * just map unknown cpuids to curcpu. Not the best, but 207 * apparently better than defaulting to swi 0. 208 */ 209 cpuid = inp->inp_flowid % (mp_maxid + 1); 210 if (! CPU_ABSENT(cpuid)) 211 return (cpuid); 212 return (curcpu); 213 } 214 #endif 215 /* Default for RSS and non-RSS - cpuid 0 */ 216 else { 217 return (0); 218 } 219 } 220 221 /* 222 * Tcp protocol timeout routine called every 500 ms. 223 * Updates timestamps used for TCP 224 * causes finite state machine actions if timers expire. 225 */ 226 void 227 tcp_slowtimo(void) 228 { 229 VNET_ITERATOR_DECL(vnet_iter); 230 231 VNET_LIST_RLOCK_NOSLEEP(); 232 VNET_FOREACH(vnet_iter) { 233 CURVNET_SET(vnet_iter); 234 (void) tcp_tw_2msl_scan(0); 235 CURVNET_RESTORE(); 236 } 237 VNET_LIST_RUNLOCK_NOSLEEP(); 238 } 239 240 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] = 241 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 }; 242 243 int tcp_backoff[TCP_MAXRXTSHIFT + 1] = 244 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 }; 245 246 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */ 247 248 /* 249 * TCP timer processing. 250 */ 251 252 void 253 tcp_timer_delack(void *xtp) 254 { 255 struct tcpcb *tp = xtp; 256 struct inpcb *inp; 257 CURVNET_SET(tp->t_vnet); 258 259 inp = tp->t_inpcb; 260 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp)); 261 INP_WLOCK(inp); 262 if (callout_pending(&tp->t_timers->tt_delack) || 263 !callout_active(&tp->t_timers->tt_delack)) { 264 INP_WUNLOCK(inp); 265 CURVNET_RESTORE(); 266 return; 267 } 268 callout_deactivate(&tp->t_timers->tt_delack); 269 if ((inp->inp_flags & INP_DROPPED) != 0) { 270 INP_WUNLOCK(inp); 271 CURVNET_RESTORE(); 272 return; 273 } 274 tp->t_flags |= TF_ACKNOW; 275 TCPSTAT_INC(tcps_delack); 276 (void) tp->t_fb->tfb_tcp_output(tp); 277 INP_WUNLOCK(inp); 278 CURVNET_RESTORE(); 279 } 280 281 /* 282 * When a timer wants to remove a TCB it must 283 * hold the INP_INFO_RLOCK(). The timer function 284 * should only have grabbed the INP_WLOCK() when 285 * it entered. To safely switch to holding both the 286 * INP_INFO_RLOCK() and the INP_WLOCK() we must first 287 * grab a reference on the inp, which will hold the inp 288 * so that it can't be removed. We then unlock the INP_WLOCK(), 289 * and grab the INP_INFO_RLOCK() lock. Once we have the INP_INFO_RLOCK() 290 * we proceed again to get the INP_WLOCK() (this preserves proper 291 * lock order). After acquiring the INP_WLOCK we must check if someone 292 * else deleted the pcb i.e. the inp_flags check. 293 * If so we return 1 otherwise we return 0. 294 * 295 * No matter what the tcp_inpinfo_lock_add() function 296 * returns the caller must afterwards call tcp_inpinfo_lock_del() 297 * to drop the locks and reference properly. 298 */ 299 300 int 301 tcp_inpinfo_lock_add(struct inpcb *inp) 302 { 303 in_pcbref(inp); 304 INP_WUNLOCK(inp); 305 INP_INFO_RLOCK(&V_tcbinfo); 306 INP_WLOCK(inp); 307 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 308 return(1); 309 } 310 return(0); 311 312 } 313 314 void 315 tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp) 316 { 317 INP_INFO_RUNLOCK(&V_tcbinfo); 318 if (inp && (tp == NULL)) { 319 /* 320 * If tcp_close/drop() gets called and tp 321 * returns NULL, then the function dropped 322 * the inp lock, we hold a reference keeping 323 * this around, so we must re-aquire the 324 * INP_WLOCK() in order to proceed with 325 * our dropping the inp reference. 326 */ 327 INP_WLOCK(inp); 328 } 329 if (inp && in_pcbrele_wlocked(inp) == 0) 330 INP_WUNLOCK(inp); 331 } 332 333 void 334 tcp_timer_2msl(void *xtp) 335 { 336 struct tcpcb *tp = xtp; 337 struct inpcb *inp; 338 CURVNET_SET(tp->t_vnet); 339 #ifdef TCPDEBUG 340 int ostate; 341 342 ostate = tp->t_state; 343 #endif 344 inp = tp->t_inpcb; 345 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp)); 346 INP_WLOCK(inp); 347 tcp_free_sackholes(tp); 348 if (callout_pending(&tp->t_timers->tt_2msl) || 349 !callout_active(&tp->t_timers->tt_2msl)) { 350 INP_WUNLOCK(tp->t_inpcb); 351 CURVNET_RESTORE(); 352 return; 353 } 354 callout_deactivate(&tp->t_timers->tt_2msl); 355 if ((inp->inp_flags & INP_DROPPED) != 0) { 356 INP_WUNLOCK(inp); 357 CURVNET_RESTORE(); 358 return; 359 } 360 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0, 361 ("%s: tp %p tcpcb can't be stopped here", __func__, tp)); 362 /* 363 * 2 MSL timeout in shutdown went off. If we're closed but 364 * still waiting for peer to close and connection has been idle 365 * too long delete connection control block. Otherwise, check 366 * again in a bit. 367 * 368 * If in TIME_WAIT state just ignore as this timeout is handled in 369 * tcp_tw_2msl_scan(). 370 * 371 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed, 372 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it. 373 * Ignore fact that there were recent incoming segments. 374 */ 375 if ((inp->inp_flags & INP_TIMEWAIT) != 0) { 376 INP_WUNLOCK(inp); 377 CURVNET_RESTORE(); 378 return; 379 } 380 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 && 381 tp->t_inpcb && tp->t_inpcb->inp_socket && 382 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) { 383 TCPSTAT_INC(tcps_finwait2_drops); 384 if (tcp_inpinfo_lock_add(inp)) { 385 tcp_inpinfo_lock_del(inp, tp); 386 goto out; 387 } 388 tp = tcp_close(tp); 389 tcp_inpinfo_lock_del(inp, tp); 390 goto out; 391 } else { 392 if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) { 393 callout_reset(&tp->t_timers->tt_2msl, 394 TP_KEEPINTVL(tp), tcp_timer_2msl, tp); 395 } else { 396 if (tcp_inpinfo_lock_add(inp)) { 397 tcp_inpinfo_lock_del(inp, tp); 398 goto out; 399 } 400 tp = tcp_close(tp); 401 tcp_inpinfo_lock_del(inp, tp); 402 goto out; 403 } 404 } 405 406 #ifdef TCPDEBUG 407 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 408 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 409 PRU_SLOWTIMO); 410 #endif 411 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO); 412 413 if (tp != NULL) 414 INP_WUNLOCK(inp); 415 out: 416 CURVNET_RESTORE(); 417 } 418 419 void 420 tcp_timer_keep(void *xtp) 421 { 422 struct tcpcb *tp = xtp; 423 struct tcptemp *t_template; 424 struct inpcb *inp; 425 CURVNET_SET(tp->t_vnet); 426 #ifdef TCPDEBUG 427 int ostate; 428 429 ostate = tp->t_state; 430 #endif 431 inp = tp->t_inpcb; 432 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp)); 433 INP_WLOCK(inp); 434 if (callout_pending(&tp->t_timers->tt_keep) || 435 !callout_active(&tp->t_timers->tt_keep)) { 436 INP_WUNLOCK(inp); 437 CURVNET_RESTORE(); 438 return; 439 } 440 callout_deactivate(&tp->t_timers->tt_keep); 441 if ((inp->inp_flags & INP_DROPPED) != 0) { 442 INP_WUNLOCK(inp); 443 CURVNET_RESTORE(); 444 return; 445 } 446 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0, 447 ("%s: tp %p tcpcb can't be stopped here", __func__, tp)); 448 449 /* 450 * Because we don't regularly reset the keepalive callout in 451 * the ESTABLISHED state, it may be that we don't actually need 452 * to send a keepalive yet. If that occurs, schedule another 453 * call for the next time the keepalive timer might expire. 454 */ 455 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 456 u_int idletime; 457 458 idletime = ticks - tp->t_rcvtime; 459 if (idletime < TP_KEEPIDLE(tp)) { 460 callout_reset(&tp->t_timers->tt_keep, 461 TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp); 462 INP_WUNLOCK(inp); 463 CURVNET_RESTORE(); 464 return; 465 } 466 } 467 468 /* 469 * Keep-alive timer went off; send something 470 * or drop connection if idle for too long. 471 */ 472 TCPSTAT_INC(tcps_keeptimeo); 473 if (tp->t_state < TCPS_ESTABLISHED) 474 goto dropit; 475 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) && 476 tp->t_state <= TCPS_CLOSING) { 477 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp)) 478 goto dropit; 479 /* 480 * Send a packet designed to force a response 481 * if the peer is up and reachable: 482 * either an ACK if the connection is still alive, 483 * or an RST if the peer has closed the connection 484 * due to timeout or reboot. 485 * Using sequence number tp->snd_una-1 486 * causes the transmitted zero-length segment 487 * to lie outside the receive window; 488 * by the protocol spec, this requires the 489 * correspondent TCP to respond. 490 */ 491 TCPSTAT_INC(tcps_keepprobe); 492 t_template = tcpip_maketemplate(inp); 493 if (t_template) { 494 tcp_respond(tp, t_template->tt_ipgen, 495 &t_template->tt_t, (struct mbuf *)NULL, 496 tp->rcv_nxt, tp->snd_una - 1, 0); 497 free(t_template, M_TEMP); 498 } 499 callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp), 500 tcp_timer_keep, tp); 501 } else 502 callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp), 503 tcp_timer_keep, tp); 504 505 #ifdef TCPDEBUG 506 if (inp->inp_socket->so_options & SO_DEBUG) 507 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 508 PRU_SLOWTIMO); 509 #endif 510 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO); 511 INP_WUNLOCK(inp); 512 CURVNET_RESTORE(); 513 return; 514 515 dropit: 516 TCPSTAT_INC(tcps_keepdrops); 517 518 if (tcp_inpinfo_lock_add(inp)) { 519 tcp_inpinfo_lock_del(inp, tp); 520 goto out; 521 } 522 tp = tcp_drop(tp, ETIMEDOUT); 523 524 #ifdef TCPDEBUG 525 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 526 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 527 PRU_SLOWTIMO); 528 #endif 529 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO); 530 tcp_inpinfo_lock_del(inp, tp); 531 out: 532 CURVNET_RESTORE(); 533 } 534 535 void 536 tcp_timer_persist(void *xtp) 537 { 538 struct tcpcb *tp = xtp; 539 struct inpcb *inp; 540 CURVNET_SET(tp->t_vnet); 541 #ifdef TCPDEBUG 542 int ostate; 543 544 ostate = tp->t_state; 545 #endif 546 inp = tp->t_inpcb; 547 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp)); 548 INP_WLOCK(inp); 549 if (callout_pending(&tp->t_timers->tt_persist) || 550 !callout_active(&tp->t_timers->tt_persist)) { 551 INP_WUNLOCK(inp); 552 CURVNET_RESTORE(); 553 return; 554 } 555 callout_deactivate(&tp->t_timers->tt_persist); 556 if ((inp->inp_flags & INP_DROPPED) != 0) { 557 INP_WUNLOCK(inp); 558 CURVNET_RESTORE(); 559 return; 560 } 561 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0, 562 ("%s: tp %p tcpcb can't be stopped here", __func__, tp)); 563 /* 564 * Persistence timer into zero window. 565 * Force a byte to be output, if possible. 566 */ 567 TCPSTAT_INC(tcps_persisttimeo); 568 /* 569 * Hack: if the peer is dead/unreachable, we do not 570 * time out if the window is closed. After a full 571 * backoff, drop the connection if the idle time 572 * (no responses to probes) reaches the maximum 573 * backoff that we would use if retransmitting. 574 */ 575 if (tp->t_rxtshift == TCP_MAXRXTSHIFT && 576 (ticks - tp->t_rcvtime >= tcp_maxpersistidle || 577 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) { 578 TCPSTAT_INC(tcps_persistdrop); 579 if (tcp_inpinfo_lock_add(inp)) { 580 tcp_inpinfo_lock_del(inp, tp); 581 goto out; 582 } 583 tp = tcp_drop(tp, ETIMEDOUT); 584 tcp_inpinfo_lock_del(inp, tp); 585 goto out; 586 } 587 /* 588 * If the user has closed the socket then drop a persisting 589 * connection after a much reduced timeout. 590 */ 591 if (tp->t_state > TCPS_CLOSE_WAIT && 592 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) { 593 TCPSTAT_INC(tcps_persistdrop); 594 if (tcp_inpinfo_lock_add(inp)) { 595 tcp_inpinfo_lock_del(inp, tp); 596 goto out; 597 } 598 tp = tcp_drop(tp, ETIMEDOUT); 599 tcp_inpinfo_lock_del(inp, tp); 600 goto out; 601 } 602 tcp_setpersist(tp); 603 tp->t_flags |= TF_FORCEDATA; 604 (void) tp->t_fb->tfb_tcp_output(tp); 605 tp->t_flags &= ~TF_FORCEDATA; 606 607 #ifdef TCPDEBUG 608 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 609 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO); 610 #endif 611 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO); 612 INP_WUNLOCK(inp); 613 out: 614 CURVNET_RESTORE(); 615 } 616 617 void 618 tcp_timer_rexmt(void * xtp) 619 { 620 struct tcpcb *tp = xtp; 621 CURVNET_SET(tp->t_vnet); 622 int rexmt; 623 struct inpcb *inp; 624 #ifdef TCPDEBUG 625 int ostate; 626 627 ostate = tp->t_state; 628 #endif 629 inp = tp->t_inpcb; 630 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp)); 631 INP_WLOCK(inp); 632 if (callout_pending(&tp->t_timers->tt_rexmt) || 633 !callout_active(&tp->t_timers->tt_rexmt)) { 634 INP_WUNLOCK(inp); 635 CURVNET_RESTORE(); 636 return; 637 } 638 callout_deactivate(&tp->t_timers->tt_rexmt); 639 if ((inp->inp_flags & INP_DROPPED) != 0) { 640 INP_WUNLOCK(inp); 641 CURVNET_RESTORE(); 642 return; 643 } 644 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0, 645 ("%s: tp %p tcpcb can't be stopped here", __func__, tp)); 646 tcp_free_sackholes(tp); 647 if (tp->t_fb->tfb_tcp_rexmit_tmr) { 648 /* The stack has a timer action too. */ 649 (*tp->t_fb->tfb_tcp_rexmit_tmr)(tp); 650 } 651 /* 652 * Retransmission timer went off. Message has not 653 * been acked within retransmit interval. Back off 654 * to a longer retransmit interval and retransmit one segment. 655 */ 656 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { 657 tp->t_rxtshift = TCP_MAXRXTSHIFT; 658 TCPSTAT_INC(tcps_timeoutdrop); 659 if (tcp_inpinfo_lock_add(inp)) { 660 tcp_inpinfo_lock_del(inp, tp); 661 goto out; 662 } 663 tp = tcp_drop(tp, tp->t_softerror ? 664 tp->t_softerror : ETIMEDOUT); 665 tcp_inpinfo_lock_del(inp, tp); 666 goto out; 667 } 668 if (tp->t_state == TCPS_SYN_SENT) { 669 /* 670 * If the SYN was retransmitted, indicate CWND to be 671 * limited to 1 segment in cc_conn_init(). 672 */ 673 tp->snd_cwnd = 1; 674 } else if (tp->t_rxtshift == 1) { 675 /* 676 * first retransmit; record ssthresh and cwnd so they can 677 * be recovered if this turns out to be a "bad" retransmit. 678 * A retransmit is considered "bad" if an ACK for this 679 * segment is received within RTT/2 interval; the assumption 680 * here is that the ACK was already in flight. See 681 * "On Estimating End-to-End Network Path Properties" by 682 * Allman and Paxson for more details. 683 */ 684 tp->snd_cwnd_prev = tp->snd_cwnd; 685 tp->snd_ssthresh_prev = tp->snd_ssthresh; 686 tp->snd_recover_prev = tp->snd_recover; 687 if (IN_FASTRECOVERY(tp->t_flags)) 688 tp->t_flags |= TF_WASFRECOVERY; 689 else 690 tp->t_flags &= ~TF_WASFRECOVERY; 691 if (IN_CONGRECOVERY(tp->t_flags)) 692 tp->t_flags |= TF_WASCRECOVERY; 693 else 694 tp->t_flags &= ~TF_WASCRECOVERY; 695 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); 696 tp->t_flags |= TF_PREVVALID; 697 } else 698 tp->t_flags &= ~TF_PREVVALID; 699 TCPSTAT_INC(tcps_rexmttimeo); 700 if ((tp->t_state == TCPS_SYN_SENT) || 701 (tp->t_state == TCPS_SYN_RECEIVED)) 702 rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift]; 703 else 704 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 705 TCPT_RANGESET(tp->t_rxtcur, rexmt, 706 tp->t_rttmin, TCPTV_REXMTMAX); 707 708 /* 709 * We enter the path for PLMTUD if connection is established or, if 710 * connection is FIN_WAIT_1 status, reason for the last is that if 711 * amount of data we send is very small, we could send it in couple of 712 * packets and process straight to FIN. In that case we won't catch 713 * ESTABLISHED state. 714 */ 715 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED)) 716 || (tp->t_state == TCPS_FIN_WAIT_1))) { 717 #ifdef INET6 718 int isipv6; 719 #endif 720 721 /* 722 * Idea here is that at each stage of mtu probe (usually, 1448 723 * -> 1188 -> 524) should be given 2 chances to recover before 724 * further clamping down. 'tp->t_rxtshift % 2 == 0' should 725 * take care of that. 726 */ 727 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) == 728 (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) && 729 (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 && 730 tp->t_rxtshift % 2 == 0)) { 731 /* 732 * Enter Path MTU Black-hole Detection mechanism: 733 * - Disable Path MTU Discovery (IP "DF" bit). 734 * - Reduce MTU to lower value than what we 735 * negotiated with peer. 736 */ 737 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) { 738 /* Record that we may have found a black hole. */ 739 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE; 740 /* Keep track of previous MSS. */ 741 tp->t_pmtud_saved_maxseg = tp->t_maxseg; 742 } 743 744 /* 745 * Reduce the MSS to blackhole value or to the default 746 * in an attempt to retransmit. 747 */ 748 #ifdef INET6 749 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0; 750 if (isipv6 && 751 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) { 752 /* Use the sysctl tuneable blackhole MSS. */ 753 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss; 754 TCPSTAT_INC(tcps_pmtud_blackhole_activated); 755 } else if (isipv6) { 756 /* Use the default MSS. */ 757 tp->t_maxseg = V_tcp_v6mssdflt; 758 /* 759 * Disable Path MTU Discovery when we switch to 760 * minmss. 761 */ 762 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 763 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 764 } 765 #endif 766 #if defined(INET6) && defined(INET) 767 else 768 #endif 769 #ifdef INET 770 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) { 771 /* Use the sysctl tuneable blackhole MSS. */ 772 tp->t_maxseg = V_tcp_pmtud_blackhole_mss; 773 TCPSTAT_INC(tcps_pmtud_blackhole_activated); 774 } else { 775 /* Use the default MSS. */ 776 tp->t_maxseg = V_tcp_mssdflt; 777 /* 778 * Disable Path MTU Discovery when we switch to 779 * minmss. 780 */ 781 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 782 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss); 783 } 784 #endif 785 /* 786 * Reset the slow-start flight size 787 * as it may depend on the new MSS. 788 */ 789 if (CC_ALGO(tp)->conn_init != NULL) 790 CC_ALGO(tp)->conn_init(tp->ccv); 791 } else { 792 /* 793 * If further retransmissions are still unsuccessful 794 * with a lowered MTU, maybe this isn't a blackhole and 795 * we restore the previous MSS and blackhole detection 796 * flags. 797 * The limit '6' is determined by giving each probe 798 * stage (1448, 1188, 524) 2 chances to recover. 799 */ 800 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) && 801 (tp->t_rxtshift >= 6)) { 802 tp->t_flags2 |= TF2_PLPMTU_PMTUD; 803 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE; 804 tp->t_maxseg = tp->t_pmtud_saved_maxseg; 805 TCPSTAT_INC(tcps_pmtud_blackhole_failed); 806 /* 807 * Reset the slow-start flight size as it 808 * may depend on the new MSS. 809 */ 810 if (CC_ALGO(tp)->conn_init != NULL) 811 CC_ALGO(tp)->conn_init(tp->ccv); 812 } 813 } 814 } 815 816 /* 817 * Disable RFC1323 and SACK if we haven't got any response to 818 * our third SYN to work-around some broken terminal servers 819 * (most of which have hopefully been retired) that have bad VJ 820 * header compression code which trashes TCP segments containing 821 * unknown-to-them TCP options. 822 */ 823 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) && 824 (tp->t_rxtshift == 3)) 825 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT); 826 /* 827 * If we backed off this far, notify the L3 protocol that we're having 828 * connection problems. 829 */ 830 if (tp->t_rxtshift > TCP_RTT_INVALIDATE) { 831 #ifdef INET6 832 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 833 in6_losing(tp->t_inpcb); 834 else 835 #endif 836 in_losing(tp->t_inpcb); 837 } 838 tp->snd_nxt = tp->snd_una; 839 tp->snd_recover = tp->snd_max; 840 /* 841 * Force a segment to be sent. 842 */ 843 tp->t_flags |= TF_ACKNOW; 844 /* 845 * If timing a segment in this window, stop the timer. 846 */ 847 tp->t_rtttime = 0; 848 849 cc_cong_signal(tp, NULL, CC_RTO); 850 851 (void) tp->t_fb->tfb_tcp_output(tp); 852 853 #ifdef TCPDEBUG 854 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 855 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, 856 PRU_SLOWTIMO); 857 #endif 858 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO); 859 INP_WUNLOCK(inp); 860 out: 861 CURVNET_RESTORE(); 862 } 863 864 void 865 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta) 866 { 867 struct callout *t_callout; 868 timeout_t *f_callout; 869 struct inpcb *inp = tp->t_inpcb; 870 int cpu = inp_to_cpuid(inp); 871 872 #ifdef TCP_OFFLOAD 873 if (tp->t_flags & TF_TOE) 874 return; 875 #endif 876 877 if (tp->t_timers->tt_flags & TT_STOPPED) 878 return; 879 880 switch (timer_type) { 881 case TT_DELACK: 882 t_callout = &tp->t_timers->tt_delack; 883 f_callout = tcp_timer_delack; 884 break; 885 case TT_REXMT: 886 t_callout = &tp->t_timers->tt_rexmt; 887 f_callout = tcp_timer_rexmt; 888 break; 889 case TT_PERSIST: 890 t_callout = &tp->t_timers->tt_persist; 891 f_callout = tcp_timer_persist; 892 break; 893 case TT_KEEP: 894 t_callout = &tp->t_timers->tt_keep; 895 f_callout = tcp_timer_keep; 896 break; 897 case TT_2MSL: 898 t_callout = &tp->t_timers->tt_2msl; 899 f_callout = tcp_timer_2msl; 900 break; 901 default: 902 if (tp->t_fb->tfb_tcp_timer_activate) { 903 tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta); 904 return; 905 } 906 panic("tp %p bad timer_type %#x", tp, timer_type); 907 } 908 if (delta == 0) { 909 callout_stop(t_callout); 910 } else { 911 callout_reset_on(t_callout, delta, f_callout, tp, cpu); 912 } 913 } 914 915 int 916 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type) 917 { 918 struct callout *t_callout; 919 920 switch (timer_type) { 921 case TT_DELACK: 922 t_callout = &tp->t_timers->tt_delack; 923 break; 924 case TT_REXMT: 925 t_callout = &tp->t_timers->tt_rexmt; 926 break; 927 case TT_PERSIST: 928 t_callout = &tp->t_timers->tt_persist; 929 break; 930 case TT_KEEP: 931 t_callout = &tp->t_timers->tt_keep; 932 break; 933 case TT_2MSL: 934 t_callout = &tp->t_timers->tt_2msl; 935 break; 936 default: 937 if (tp->t_fb->tfb_tcp_timer_active) { 938 return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type)); 939 } 940 panic("tp %p bad timer_type %#x", tp, timer_type); 941 } 942 return callout_active(t_callout); 943 } 944 945 void 946 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type) 947 { 948 struct callout *t_callout; 949 950 tp->t_timers->tt_flags |= TT_STOPPED; 951 switch (timer_type) { 952 case TT_DELACK: 953 t_callout = &tp->t_timers->tt_delack; 954 break; 955 case TT_REXMT: 956 t_callout = &tp->t_timers->tt_rexmt; 957 break; 958 case TT_PERSIST: 959 t_callout = &tp->t_timers->tt_persist; 960 break; 961 case TT_KEEP: 962 t_callout = &tp->t_timers->tt_keep; 963 break; 964 case TT_2MSL: 965 t_callout = &tp->t_timers->tt_2msl; 966 break; 967 default: 968 if (tp->t_fb->tfb_tcp_timer_stop) { 969 /* 970 * XXXrrs we need to look at this with the 971 * stop case below (flags). 972 */ 973 tp->t_fb->tfb_tcp_timer_stop(tp, timer_type); 974 return; 975 } 976 panic("tp %p bad timer_type %#x", tp, timer_type); 977 } 978 979 if (callout_async_drain(t_callout, tcp_timer_discard) == 0) { 980 /* 981 * Can't stop the callout, defer tcpcb actual deletion 982 * to the last one. We do this using the async drain 983 * function and incrementing the count in 984 */ 985 tp->t_timers->tt_draincnt++; 986 } 987 } 988