2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
39 #include "opt_tcpdebug.h"
42 #include <sys/param.h>
43 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
55 #include <net/route.h>
56 #include <net/rss_config.h>
58 #include <net/netisr.h>
60 #include <netinet/in.h>
61 #include <netinet/in_kdtrace.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_rss.h>
64 #include <netinet/in_systm.h>
66 #include <netinet6/in6_pcb.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/tcp.h>
70 #include <netinet/tcp_fsm.h>
71 #include <netinet/tcp_log_buf.h>
72 #include <netinet/tcp_timer.h>
73 #include <netinet/tcp_var.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/cc/cc.h>
77 #include <netinet6/tcp6_var.h>
79 #include <netinet/tcpip.h>
81 #include <netinet/tcp_debug.h>
85 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmin,
86 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
87 &tcp_persmin, 0, sysctl_msec_to_ticks, "I",
88 "minimum persistence interval");
91 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax,
92 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
93 &tcp_persmax, 0, sysctl_msec_to_ticks, "I",
94 "maximum persistence interval");
97 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit,
98 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
99 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I",
100 "time to establish connection");
103 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle,
104 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
105 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I",
106 "time before keepalive probes begin");
109 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl,
110 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
111 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I",
112 "time between keepalive probes");
115 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
116 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
117 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
118 "Time before a delayed ACK is sent");
120 VNET_DEFINE(int, tcp_msl);
121 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl,
122 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET,
123 &VNET_NAME(tcp_msl), 0, sysctl_msec_to_ticks, "I",
124 "Maximum segment lifetime");
126 int tcp_rexmit_initial;
127 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_initial,
128 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
129 &tcp_rexmit_initial, 0, sysctl_msec_to_ticks, "I",
130 "Initial Retransmission Timeout");
133 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min,
134 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
135 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
136 "Minimum Retransmission Timeout");
139 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop,
140 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
141 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
142 "Retransmission Timer Slop");
144 VNET_DEFINE(int, tcp_always_keepalive) = 1;
145 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_VNET|CTLFLAG_RW,
146 &VNET_NAME(tcp_always_keepalive) , 0,
147 "Assume SO_KEEPALIVE on all TCP connections");
149 int tcp_fast_finwait2_recycle = 0;
150 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
151 &tcp_fast_finwait2_recycle, 0,
152 "Recycle closed FIN_WAIT_2 connections faster");
154 int tcp_finwait2_timeout;
155 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout,
156 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
157 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I",
158 "FIN-WAIT2 timeout");
160 int tcp_keepcnt = TCPTV_KEEPCNT;
161 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
162 "Number of keepalive probes to send");
164 /* max idle probes */
165 int tcp_maxpersistidle;
167 int tcp_rexmit_drop_options = 0;
168 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
169 &tcp_rexmit_drop_options, 0,
170 "Drop TCP options from 3rd and later retransmitted SYN");
172 VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
173 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
174 CTLFLAG_RW|CTLFLAG_VNET,
175 &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
176 "Path MTU Discovery Black Hole Detection Enabled");
179 VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
180 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
181 CTLFLAG_RW|CTLFLAG_VNET,
182 &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
183 "Path MTU Discovery Black Hole Detection lowered MSS");
187 VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
188 SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
189 CTLFLAG_RW|CTLFLAG_VNET,
190 &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
191 "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
195 static int per_cpu_timers = 1;
197 static int per_cpu_timers = 0;
199 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
200 &per_cpu_timers , 0, "run tcp timers on all cpus");
203 * Map the given inp to a CPU id.
205 * This queries RSS if it's compiled in, else it defaults to the current
209 inp_to_cpuid(struct inpcb *inp)
213 if (per_cpu_timers) {
215 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
216 if (cpuid == NETISR_CPUID_NONE)
217 return (curcpu); /* XXX */
222 * We don't have a flowid -> cpuid mapping, so cheat and
223 * just map unknown cpuids to curcpu. Not the best, but
224 * apparently better than defaulting to swi 0.
226 cpuid = inp->inp_flowid % (mp_maxid + 1);
227 if (! CPU_ABSENT(cpuid))
236 * Tcp protocol timeout routine called every 500 ms.
237 * Updates timestamps used for TCP
238 * causes finite state machine actions if timers expire.
243 VNET_ITERATOR_DECL(vnet_iter);
245 VNET_LIST_RLOCK_NOSLEEP();
246 VNET_FOREACH(vnet_iter) {
247 CURVNET_SET(vnet_iter);
248 (void) tcp_tw_2msl_scan(0);
251 VNET_LIST_RUNLOCK_NOSLEEP();
254 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
255 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
257 int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
260 * TCP timer processing.
264 tcp_timer_delack(void *xtp)
266 struct epoch_tracker et;
267 struct tcpcb *tp = xtp;
269 CURVNET_SET(tp->t_vnet);
272 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
274 if (callout_pending(&tp->t_timers->tt_delack) ||
275 !callout_active(&tp->t_timers->tt_delack)) {
280 callout_deactivate(&tp->t_timers->tt_delack);
281 if ((inp->inp_flags & INP_DROPPED) != 0) {
286 tp->t_flags |= TF_ACKNOW;
287 TCPSTAT_INC(tcps_delack);
289 (void) tcp_output_unlock(tp);
295 tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp)
297 if (inp && tp != NULL)
302 tcp_timer_2msl(void *xtp)
304 struct tcpcb *tp = xtp;
306 struct epoch_tracker et;
307 CURVNET_SET(tp->t_vnet);
311 ostate = tp->t_state;
314 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
316 tcp_free_sackholes(tp);
317 if (callout_pending(&tp->t_timers->tt_2msl) ||
318 !callout_active(&tp->t_timers->tt_2msl)) {
319 INP_WUNLOCK(tp->t_inpcb);
323 callout_deactivate(&tp->t_timers->tt_2msl);
324 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
329 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
330 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
332 * 2 MSL timeout in shutdown went off. If we're closed but
333 * still waiting for peer to close and connection has been idle
334 * too long delete connection control block. Otherwise, check
337 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
338 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
339 * Ignore fact that there were recent incoming segments.
341 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
342 tp->t_inpcb && tp->t_inpcb->inp_socket &&
343 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
344 TCPSTAT_INC(tcps_finwait2_drops);
348 tcp_inpinfo_lock_del(inp, tp);
351 if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
352 callout_reset(&tp->t_timers->tt_2msl,
353 TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
358 tcp_inpinfo_lock_del(inp, tp);
364 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
365 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
368 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
377 tcp_timer_keep(void *xtp)
379 struct tcpcb *tp = xtp;
380 struct tcptemp *t_template;
382 struct epoch_tracker et;
383 CURVNET_SET(tp->t_vnet);
387 ostate = tp->t_state;
390 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
392 if (callout_pending(&tp->t_timers->tt_keep) ||
393 !callout_active(&tp->t_timers->tt_keep)) {
398 callout_deactivate(&tp->t_timers->tt_keep);
399 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
404 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
405 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
408 * Because we don't regularly reset the keepalive callout in
409 * the ESTABLISHED state, it may be that we don't actually need
410 * to send a keepalive yet. If that occurs, schedule another
411 * call for the next time the keepalive timer might expire.
413 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
416 idletime = ticks - tp->t_rcvtime;
417 if (idletime < TP_KEEPIDLE(tp)) {
418 callout_reset(&tp->t_timers->tt_keep,
419 TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp);
427 * Keep-alive timer went off; send something
428 * or drop connection if idle for too long.
430 TCPSTAT_INC(tcps_keeptimeo);
431 if (tp->t_state < TCPS_ESTABLISHED)
433 if ((V_tcp_always_keepalive ||
434 inp->inp_socket->so_options & SO_KEEPALIVE) &&
435 tp->t_state <= TCPS_CLOSING) {
436 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
439 * Send a packet designed to force a response
440 * if the peer is up and reachable:
441 * either an ACK if the connection is still alive,
442 * or an RST if the peer has closed the connection
443 * due to timeout or reboot.
444 * Using sequence number tp->snd_una-1
445 * causes the transmitted zero-length segment
446 * to lie outside the receive window;
447 * by the protocol spec, this requires the
448 * correspondent TCP to respond.
450 TCPSTAT_INC(tcps_keepprobe);
451 t_template = tcpip_maketemplate(inp);
454 tcp_respond(tp, t_template->tt_ipgen,
455 &t_template->tt_t, (struct mbuf *)NULL,
456 tp->rcv_nxt, tp->snd_una - 1, 0);
458 free(t_template, M_TEMP);
460 callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
463 callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
467 if (inp->inp_socket->so_options & SO_DEBUG)
468 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
471 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
477 TCPSTAT_INC(tcps_keepdrops);
479 tp = tcp_drop(tp, ETIMEDOUT);
482 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
483 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
486 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
488 tcp_inpinfo_lock_del(inp, tp);
493 tcp_timer_persist(void *xtp)
495 struct tcpcb *tp = xtp;
497 struct epoch_tracker et;
499 CURVNET_SET(tp->t_vnet);
503 ostate = tp->t_state;
506 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
508 if (callout_pending(&tp->t_timers->tt_persist) ||
509 !callout_active(&tp->t_timers->tt_persist)) {
514 callout_deactivate(&tp->t_timers->tt_persist);
515 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
520 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
521 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
523 * Persistence timer into zero window.
524 * Force a byte to be output, if possible.
526 TCPSTAT_INC(tcps_persisttimeo);
528 * Hack: if the peer is dead/unreachable, we do not
529 * time out if the window is closed. After a full
530 * backoff, drop the connection if the idle time
531 * (no responses to probes) reaches the maximum
532 * backoff that we would use if retransmitting.
534 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
535 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
536 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
537 TCPSTAT_INC(tcps_persistdrop);
539 tp = tcp_drop(tp, ETIMEDOUT);
541 tcp_inpinfo_lock_del(inp, tp);
545 * If the user has closed the socket then drop a persisting
546 * connection after a much reduced timeout.
548 if (tp->t_state > TCPS_CLOSE_WAIT &&
549 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
550 TCPSTAT_INC(tcps_persistdrop);
552 tp = tcp_drop(tp, ETIMEDOUT);
554 tcp_inpinfo_lock_del(inp, tp);
558 tp->t_flags |= TF_FORCEDATA;
560 outrv = tcp_output_nodrop(tp);
561 tp->t_flags &= ~TF_FORCEDATA;
564 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
565 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
567 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
568 (void) tcp_unlock_or_drop(tp, outrv);
575 tcp_timer_rexmt(void * xtp)
577 struct tcpcb *tp = xtp;
578 CURVNET_SET(tp->t_vnet);
581 struct epoch_tracker et;
586 ostate = tp->t_state;
589 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
591 if (callout_pending(&tp->t_timers->tt_rexmt) ||
592 !callout_active(&tp->t_timers->tt_rexmt)) {
597 callout_deactivate(&tp->t_timers->tt_rexmt);
598 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
603 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
604 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
605 tcp_free_sackholes(tp);
606 TCP_LOG_EVENT(tp, NULL, NULL, NULL, TCP_LOG_RTO, 0, 0, NULL, false);
607 if (tp->t_fb->tfb_tcp_rexmit_tmr) {
608 /* The stack has a timer action too. */
609 (*tp->t_fb->tfb_tcp_rexmit_tmr)(tp);
612 * Retransmission timer went off. Message has not
613 * been acked within retransmit interval. Back off
614 * to a longer retransmit interval and retransmit one segment.
616 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
617 tp->t_rxtshift = TCP_MAXRXTSHIFT;
618 TCPSTAT_INC(tcps_timeoutdrop);
620 tp = tcp_drop(tp, ETIMEDOUT);
622 tcp_inpinfo_lock_del(inp, tp);
625 if (tp->t_state == TCPS_SYN_SENT) {
627 * If the SYN was retransmitted, indicate CWND to be
628 * limited to 1 segment in cc_conn_init().
631 } else if (tp->t_rxtshift == 1) {
633 * first retransmit; record ssthresh and cwnd so they can
634 * be recovered if this turns out to be a "bad" retransmit.
635 * A retransmit is considered "bad" if an ACK for this
636 * segment is received within RTT/2 interval; the assumption
637 * here is that the ACK was already in flight. See
638 * "On Estimating End-to-End Network Path Properties" by
639 * Allman and Paxson for more details.
641 tp->snd_cwnd_prev = tp->snd_cwnd;
642 tp->snd_ssthresh_prev = tp->snd_ssthresh;
643 tp->snd_recover_prev = tp->snd_recover;
644 if (IN_FASTRECOVERY(tp->t_flags))
645 tp->t_flags |= TF_WASFRECOVERY;
647 tp->t_flags &= ~TF_WASFRECOVERY;
648 if (IN_CONGRECOVERY(tp->t_flags))
649 tp->t_flags |= TF_WASCRECOVERY;
651 tp->t_flags &= ~TF_WASCRECOVERY;
652 if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
653 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
654 /* In the event that we've negotiated timestamps
655 * badrxtwin will be set to the value that we set
656 * the retransmitted packet's to_tsval to by tcp_output
658 tp->t_flags |= TF_PREVVALID;
660 tp->t_flags &= ~TF_PREVVALID;
661 TCPSTAT_INC(tcps_rexmttimeo);
662 if ((tp->t_state == TCPS_SYN_SENT) ||
663 (tp->t_state == TCPS_SYN_RECEIVED))
664 rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift];
666 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
667 TCPT_RANGESET(tp->t_rxtcur, rexmt,
668 tp->t_rttmin, TCPTV_REXMTMAX);
671 * We enter the path for PLMTUD if connection is established or, if
672 * connection is FIN_WAIT_1 status, reason for the last is that if
673 * amount of data we send is very small, we could send it in couple of
674 * packets and process straight to FIN. In that case we won't catch
678 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
682 if (((V_tcp_pmtud_blackhole_detect == 1) ||
683 (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
684 (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
685 ((tp->t_state == TCPS_ESTABLISHED) ||
686 (tp->t_state == TCPS_FIN_WAIT_1))) {
687 if (tp->t_rxtshift == 1) {
689 * We enter blackhole detection after the first
690 * unsuccessful timer based retransmission.
691 * Then we reduce up to two times the MSS, each
692 * candidate giving two tries of retransmissions.
693 * But we give a candidate only two tries, if it
694 * actually reduces the MSS.
696 tp->t_blackhole_enter = 2;
697 tp->t_blackhole_exit = tp->t_blackhole_enter;
700 if (tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss)
701 tp->t_blackhole_exit += 2;
702 if (tp->t_maxseg > V_tcp_v6mssdflt &&
703 V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt)
704 tp->t_blackhole_exit += 2;
708 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss)
709 tp->t_blackhole_exit += 2;
710 if (tp->t_maxseg > V_tcp_mssdflt &&
711 V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt)
712 tp->t_blackhole_exit += 2;
716 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
717 (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
718 (tp->t_rxtshift >= tp->t_blackhole_enter &&
719 tp->t_rxtshift < tp->t_blackhole_exit &&
720 (tp->t_rxtshift - tp->t_blackhole_enter) % 2 == 0)) {
722 * Enter Path MTU Black-hole Detection mechanism:
723 * - Disable Path MTU Discovery (IP "DF" bit).
724 * - Reduce MTU to lower value than what we
725 * negotiated with peer.
727 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
728 /* Record that we may have found a black hole. */
729 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
730 /* Keep track of previous MSS. */
731 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
735 * Reduce the MSS to blackhole value or to the default
736 * in an attempt to retransmit.
740 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss &&
741 V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt) {
742 /* Use the sysctl tuneable blackhole MSS. */
743 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
744 TCPSTAT_INC(tcps_pmtud_blackhole_activated);
746 /* Use the default MSS. */
747 tp->t_maxseg = V_tcp_v6mssdflt;
749 * Disable Path MTU Discovery when we switch to
752 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
753 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
756 #if defined(INET6) && defined(INET)
760 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss &&
761 V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt) {
762 /* Use the sysctl tuneable blackhole MSS. */
763 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
764 TCPSTAT_INC(tcps_pmtud_blackhole_activated);
766 /* Use the default MSS. */
767 tp->t_maxseg = V_tcp_mssdflt;
769 * Disable Path MTU Discovery when we switch to
772 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
773 TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
777 * Reset the slow-start flight size
778 * as it may depend on the new MSS.
780 if (CC_ALGO(tp)->conn_init != NULL)
781 CC_ALGO(tp)->conn_init(tp->ccv);
784 * If further retransmissions are still unsuccessful
785 * with a lowered MTU, maybe this isn't a blackhole and
786 * we restore the previous MSS and blackhole detection
789 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
790 (tp->t_rxtshift >= tp->t_blackhole_exit)) {
791 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
792 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
793 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
794 TCPSTAT_INC(tcps_pmtud_blackhole_failed);
796 * Reset the slow-start flight size as it
797 * may depend on the new MSS.
799 if (CC_ALGO(tp)->conn_init != NULL)
800 CC_ALGO(tp)->conn_init(tp->ccv);
806 * Disable RFC1323 and SACK if we haven't got any response to
807 * our third SYN to work-around some broken terminal servers
808 * (most of which have hopefully been retired) that have bad VJ
809 * header compression code which trashes TCP segments containing
810 * unknown-to-them TCP options.
812 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
813 (tp->t_rxtshift == 3))
814 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
816 * If we backed off this far, notify the L3 protocol that we're having
817 * connection problems.
819 if (tp->t_rxtshift > TCP_RTT_INVALIDATE) {
821 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
822 in6_losing(tp->t_inpcb);
825 in_losing(tp->t_inpcb);
827 tp->snd_nxt = tp->snd_una;
828 tp->snd_recover = tp->snd_max;
830 * Force a segment to be sent.
832 tp->t_flags |= TF_ACKNOW;
834 * If timing a segment in this window, stop the timer.
838 cc_cong_signal(tp, NULL, CC_RTO);
840 outrv = tcp_output_nodrop(tp);
842 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
843 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
846 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
847 (void) tcp_unlock_or_drop(tp, outrv);
854 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
856 struct callout *t_callout;
857 callout_func_t *f_callout;
858 struct inpcb *inp = tp->t_inpcb;
859 int cpu = inp_to_cpuid(inp);
862 if (tp->t_flags & TF_TOE)
866 if (tp->t_timers->tt_flags & TT_STOPPED)
869 switch (timer_type) {
871 t_callout = &tp->t_timers->tt_delack;
872 f_callout = tcp_timer_delack;
875 t_callout = &tp->t_timers->tt_rexmt;
876 f_callout = tcp_timer_rexmt;
879 t_callout = &tp->t_timers->tt_persist;
880 f_callout = tcp_timer_persist;
883 t_callout = &tp->t_timers->tt_keep;
884 f_callout = tcp_timer_keep;
887 t_callout = &tp->t_timers->tt_2msl;
888 f_callout = tcp_timer_2msl;
891 if (tp->t_fb->tfb_tcp_timer_activate) {
892 tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
895 panic("tp %p bad timer_type %#x", tp, timer_type);
898 callout_stop(t_callout);
900 callout_reset_on(t_callout, delta, f_callout, tp, cpu);
905 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
907 struct callout *t_callout;
909 switch (timer_type) {
911 t_callout = &tp->t_timers->tt_delack;
914 t_callout = &tp->t_timers->tt_rexmt;
917 t_callout = &tp->t_timers->tt_persist;
920 t_callout = &tp->t_timers->tt_keep;
923 t_callout = &tp->t_timers->tt_2msl;
926 if (tp->t_fb->tfb_tcp_timer_active) {
927 return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
929 panic("tp %p bad timer_type %#x", tp, timer_type);
931 return callout_active(t_callout);
935 * Stop the timer from running, and apply a flag
936 * against the timer_flags that will force the
937 * timer never to run. The flag is needed to assure
938 * a race does not leave it running and cause
939 * the timer to possibly restart itself (keep and persist
940 * especially do this).
943 tcp_timer_suspend(struct tcpcb *tp, uint32_t timer_type)
945 struct callout *t_callout;
948 switch (timer_type) {
950 t_flags = TT_DELACK_SUS;
951 t_callout = &tp->t_timers->tt_delack;
954 t_flags = TT_REXMT_SUS;
955 t_callout = &tp->t_timers->tt_rexmt;
958 t_flags = TT_PERSIST_SUS;
959 t_callout = &tp->t_timers->tt_persist;
962 t_flags = TT_KEEP_SUS;
963 t_callout = &tp->t_timers->tt_keep;
966 t_flags = TT_2MSL_SUS;
967 t_callout = &tp->t_timers->tt_2msl;
970 panic("tp:%p bad timer_type 0x%x", tp, timer_type);
972 tp->t_timers->tt_flags |= t_flags;
973 return (callout_stop(t_callout));
977 tcp_timers_unsuspend(struct tcpcb *tp, uint32_t timer_type)
979 switch (timer_type) {
981 if (tp->t_timers->tt_flags & TT_DELACK_SUS) {
982 tp->t_timers->tt_flags &= ~TT_DELACK_SUS;
983 if (tp->t_flags & TF_DELACK) {
984 /* Delayed ack timer should be up activate a timer */
985 tp->t_flags &= ~TF_DELACK;
986 tcp_timer_activate(tp, TT_DELACK,
992 if (tp->t_timers->tt_flags & TT_REXMT_SUS) {
993 tp->t_timers->tt_flags &= ~TT_REXMT_SUS;
994 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
995 (tcp_timer_active((tp), TT_PERSIST) == 0) &&
997 /* We have outstanding data activate a timer */
998 tcp_timer_activate(tp, TT_REXMT,
1004 if (tp->t_timers->tt_flags & TT_PERSIST_SUS) {
1005 tp->t_timers->tt_flags &= ~TT_PERSIST_SUS;
1006 if (tp->snd_wnd == 0) {
1007 /* Activate the persists timer */
1014 if (tp->t_timers->tt_flags & TT_KEEP_SUS) {
1015 tp->t_timers->tt_flags &= ~TT_KEEP_SUS;
1016 tcp_timer_activate(tp, TT_KEEP,
1017 TCPS_HAVEESTABLISHED(tp->t_state) ?
1018 TP_KEEPIDLE(tp) : TP_KEEPINIT(tp));
1022 if (tp->t_timers->tt_flags &= TT_2MSL_SUS) {
1023 tp->t_timers->tt_flags &= ~TT_2MSL_SUS;
1024 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1025 ((tp->t_inpcb->inp_socket == NULL) ||
1026 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE))) {
1027 /* Star the 2MSL timer */
1028 tcp_timer_activate(tp, TT_2MSL,
1029 (tcp_fast_finwait2_recycle) ?
1030 tcp_finwait2_timeout : TP_MAXIDLE(tp));
1035 panic("tp:%p bad timer_type 0x%x", tp, timer_type);
1040 tcp_timer_discard(void *ptp)
1044 struct epoch_tracker et;
1046 tp = (struct tcpcb *)ptp;
1047 CURVNET_SET(tp->t_vnet);
1048 NET_EPOCH_ENTER(et);
1050 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL",
1053 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) != 0,
1054 ("%s: tcpcb has to be stopped here", __func__));
1055 if (--tp->t_timers->tt_draincnt > 0 ||
1056 tcp_freecb(tp) == false)
1063 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
1065 struct callout *t_callout;
1067 tp->t_timers->tt_flags |= TT_STOPPED;
1068 switch (timer_type) {
1070 t_callout = &tp->t_timers->tt_delack;
1073 t_callout = &tp->t_timers->tt_rexmt;
1076 t_callout = &tp->t_timers->tt_persist;
1079 t_callout = &tp->t_timers->tt_keep;
1082 t_callout = &tp->t_timers->tt_2msl;
1085 if (tp->t_fb->tfb_tcp_timer_stop) {
1087 * XXXrrs we need to look at this with the
1088 * stop case below (flags).
1090 tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
1093 panic("tp %p bad timer_type %#x", tp, timer_type);
1096 if (callout_async_drain(t_callout, tcp_timer_discard) == 0) {
1098 * Can't stop the callout, defer tcpcb actual deletion
1099 * to the last one. We do this using the async drain
1100 * function and incrementing the count in
1102 tp->t_timers->tt_draincnt++;