xref: /freebsd/sys/netinet/tcp_timer.c (revision 0999766ddfa4602637fc7ffb0148dc0b33870533)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31e79adb8eSGarrett Wollman  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
37825fd1e4SNavdeep Parhar #include "opt_inet.h"
38fb59c426SYoshinobu Inoue #include "opt_inet6.h"
390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
40883831c6SAdrian Chadd #include "opt_rss.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
4398163b98SPoul-Henning Kamp #include <sys/kernel.h>
44c74af4faSBruce Evans #include <sys/lock.h>
4508517d53SMike Silbersack #include <sys/mbuf.h>
46c74af4faSBruce Evans #include <sys/mutex.h>
47c74af4faSBruce Evans #include <sys/protosw.h>
4887aedea4SKip Macy #include <sys/smp.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51c74af4faSBruce Evans #include <sys/sysctl.h>
52c74af4faSBruce Evans #include <sys/systm.h>
53e79adb8eSGarrett Wollman 
544b79449eSBjoern A. Zeeb #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56b2bdc62aSAdrian Chadd #include <net/rss_config.h>
57530c0060SRobert Watson #include <net/vnet.h>
58883831c6SAdrian Chadd #include <net/netisr.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <netinet/in.h>
615d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
63883831c6SAdrian Chadd #include <netinet/in_rss.h>
64c74af4faSBruce Evans #include <netinet/in_systm.h>
65fb59c426SYoshinobu Inoue #ifdef INET6
66fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
67fb59c426SYoshinobu Inoue #endif
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
692de3e790SGleb Smirnoff #include <netinet/tcp.h>
70df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
712529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
72df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
7489e560f4SRandall Stewart #include <netinet/tcp_seq.h>
754644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
76f6f6703fSSean Bruno #ifdef INET6
77f6f6703fSSean Bruno #include <netinet6/tcp6_var.h>
78f6f6703fSSean Bruno #endif
79df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
80af7a2999SDavid Greenman #ifdef TCPDEBUG
81af7a2999SDavid Greenman #include <netinet/tcp_debug.h>
82af7a2999SDavid Greenman #endif
83df8bae1dSRodney W. Grimes 
840645c604SHiren Panchasara int    tcp_persmin;
850645c604SHiren Panchasara SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmin, CTLTYPE_INT|CTLFLAG_RW,
860645c604SHiren Panchasara     &tcp_persmin, 0, sysctl_msec_to_ticks, "I", "minimum persistence interval");
870645c604SHiren Panchasara 
880645c604SHiren Panchasara int    tcp_persmax;
890645c604SHiren Panchasara SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax, CTLTYPE_INT|CTLFLAG_RW,
900645c604SHiren Panchasara     &tcp_persmax, 0, sysctl_msec_to_ticks, "I", "maximum persistence interval");
910645c604SHiren Panchasara 
929b8b58e0SJonathan Lemon int	tcp_keepinit;
93ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
9441698ebfSTom Rhodes     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
957b40aa32SPaul Traina 
969b8b58e0SJonathan Lemon int	tcp_keepidle;
97ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
9841698ebfSTom Rhodes     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
9998163b98SPoul-Henning Kamp 
1009b8b58e0SJonathan Lemon int	tcp_keepintvl;
101ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
10241698ebfSTom Rhodes     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
10398163b98SPoul-Henning Kamp 
1049b8b58e0SJonathan Lemon int	tcp_delacktime;
1056489fe65SAndre Oppermann SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
1066489fe65SAndre Oppermann     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
107ccb4d0c6SJonathan Lemon     "Time before a delayed ACK is sent");
1089b8b58e0SJonathan Lemon 
1099b8b58e0SJonathan Lemon int	tcp_msl;
110ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
111ccb4d0c6SJonathan Lemon     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
1129b8b58e0SJonathan Lemon 
113*0999766dSMichael Tuexen int	tcp_rexmit_initial;
114*0999766dSMichael Tuexen SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_initial, CTLTYPE_INT|CTLFLAG_RW,
115*0999766dSMichael Tuexen     &tcp_rexmit_initial, 0, sysctl_msec_to_ticks, "I",
116*0999766dSMichael Tuexen     "Initial Retransmission Timeout");
117*0999766dSMichael Tuexen 
118701bec5aSMatthew Dillon int	tcp_rexmit_min;
119701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
1206489fe65SAndre Oppermann     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
1216489fe65SAndre Oppermann     "Minimum Retransmission Timeout");
122701bec5aSMatthew Dillon 
123701bec5aSMatthew Dillon int	tcp_rexmit_slop;
124701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
1256489fe65SAndre Oppermann     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
1266489fe65SAndre Oppermann     "Retransmission Timer Slop");
127701bec5aSMatthew Dillon 
128f1798531SJohn Baldwin int	tcp_always_keepalive = 1;
1293d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
130f1798531SJohn Baldwin     &tcp_always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
13134be9bf3SPoul-Henning Kamp 
1327c72af87SMohan Srinivasan int    tcp_fast_finwait2_recycle = 0;
1337c72af87SMohan Srinivasan SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
1346489fe65SAndre Oppermann     &tcp_fast_finwait2_recycle, 0,
1356489fe65SAndre Oppermann     "Recycle closed FIN_WAIT_2 connections faster");
1367c72af87SMohan Srinivasan 
1377c72af87SMohan Srinivasan int    tcp_finwait2_timeout;
1387c72af87SMohan Srinivasan SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
1396489fe65SAndre Oppermann     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
1407c72af87SMohan Srinivasan 
1419077f387SGleb Smirnoff int	tcp_keepcnt = TCPTV_KEEPCNT;
1429077f387SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
1439077f387SGleb Smirnoff     "Number of keepalive probes to send");
1447c72af87SMohan Srinivasan 
1450312fbe9SPoul-Henning Kamp 	/* max idle probes */
1469b8b58e0SJonathan Lemon int	tcp_maxpersistidle;
147e79adb8eSGarrett Wollman 
14889e560f4SRandall Stewart int	tcp_rexmit_drop_options = 0;
1496c0ef895SJohn Baldwin SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
1506c0ef895SJohn Baldwin     &tcp_rexmit_drop_options, 0,
1516c0ef895SJohn Baldwin     "Drop TCP options from 3rd and later retransmitted SYN");
1526c0ef895SJohn Baldwin 
153e29c55e4SGleb Smirnoff VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
154f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
155f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
156f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
157f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection Enabled");
158f6f6703fSSean Bruno 
159f6f6703fSSean Bruno #ifdef INET
160e29c55e4SGleb Smirnoff VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
161f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
162f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
163f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
164f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection lowered MSS");
165f6f6703fSSean Bruno #endif
166f6f6703fSSean Bruno 
167f6f6703fSSean Bruno #ifdef INET6
168e29c55e4SGleb Smirnoff VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
169f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
170f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
171f6f6703fSSean Bruno     &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
172f6f6703fSSean Bruno     "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
173f6f6703fSSean Bruno #endif
174f6f6703fSSean Bruno 
1758f7e75cbSAdrian Chadd #ifdef	RSS
1768f7e75cbSAdrian Chadd static int	per_cpu_timers = 1;
1778f7e75cbSAdrian Chadd #else
17887aedea4SKip Macy static int	per_cpu_timers = 0;
1798f7e75cbSAdrian Chadd #endif
18087aedea4SKip Macy SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
18187aedea4SKip Macy     &per_cpu_timers , 0, "run tcp timers on all cpus");
18287aedea4SKip Macy 
183883831c6SAdrian Chadd /*
184883831c6SAdrian Chadd  * Map the given inp to a CPU id.
185883831c6SAdrian Chadd  *
186883831c6SAdrian Chadd  * This queries RSS if it's compiled in, else it defaults to the current
187883831c6SAdrian Chadd  * CPU ID.
188883831c6SAdrian Chadd  */
18989e560f4SRandall Stewart inline int
190883831c6SAdrian Chadd inp_to_cpuid(struct inpcb *inp)
191883831c6SAdrian Chadd {
192883831c6SAdrian Chadd 	u_int cpuid;
193883831c6SAdrian Chadd 
194883831c6SAdrian Chadd #ifdef	RSS
195883831c6SAdrian Chadd 	if (per_cpu_timers) {
196883831c6SAdrian Chadd 		cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
197883831c6SAdrian Chadd 		if (cpuid == NETISR_CPUID_NONE)
198883831c6SAdrian Chadd 			return (curcpu);	/* XXX */
199883831c6SAdrian Chadd 		else
200883831c6SAdrian Chadd 			return (cpuid);
201883831c6SAdrian Chadd 	}
202883831c6SAdrian Chadd #else
203883831c6SAdrian Chadd 	/* Legacy, pre-RSS behaviour */
204883831c6SAdrian Chadd 	if (per_cpu_timers) {
205883831c6SAdrian Chadd 		/*
206883831c6SAdrian Chadd 		 * We don't have a flowid -> cpuid mapping, so cheat and
207883831c6SAdrian Chadd 		 * just map unknown cpuids to curcpu.  Not the best, but
208883831c6SAdrian Chadd 		 * apparently better than defaulting to swi 0.
209883831c6SAdrian Chadd 		 */
210883831c6SAdrian Chadd 		cpuid = inp->inp_flowid % (mp_maxid + 1);
211883831c6SAdrian Chadd 		if (! CPU_ABSENT(cpuid))
212883831c6SAdrian Chadd 			return (cpuid);
213883831c6SAdrian Chadd 		return (curcpu);
214883831c6SAdrian Chadd 	}
215883831c6SAdrian Chadd #endif
216883831c6SAdrian Chadd 	/* Default for RSS and non-RSS - cpuid 0 */
217883831c6SAdrian Chadd 	else {
218883831c6SAdrian Chadd 		return (0);
219883831c6SAdrian Chadd 	}
220883831c6SAdrian Chadd }
22187aedea4SKip Macy 
222df8bae1dSRodney W. Grimes /*
223df8bae1dSRodney W. Grimes  * Tcp protocol timeout routine called every 500 ms.
2249b8b58e0SJonathan Lemon  * Updates timestamps used for TCP
225df8bae1dSRodney W. Grimes  * causes finite state machine actions if timers expire.
226df8bae1dSRodney W. Grimes  */
227df8bae1dSRodney W. Grimes void
228e2f2059fSMike Silbersack tcp_slowtimo(void)
229df8bae1dSRodney W. Grimes {
2308b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
23115bd2b43SDavid Greenman 
2325ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
2338b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
2348b615593SMarko Zec 		CURVNET_SET(vnet_iter);
235cea40c48SJulien Charbon 		(void) tcp_tw_2msl_scan(0);
2368b615593SMarko Zec 		CURVNET_RESTORE();
2378b615593SMarko Zec 	}
2385ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
239df8bae1dSRodney W. Grimes }
240df8bae1dSRodney W. Grimes 
241df8bae1dSRodney W. Grimes int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
242f058535dSJeffrey Hsu     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
243df8bae1dSRodney W. Grimes 
24489e560f4SRandall Stewart int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
245e79adb8eSGarrett Wollman 
246df8bae1dSRodney W. Grimes /*
247df8bae1dSRodney W. Grimes  * TCP timer processing.
248df8bae1dSRodney W. Grimes  */
24985d94372SRobert Watson 
25085d94372SRobert Watson void
25185d94372SRobert Watson tcp_timer_delack(void *xtp)
252df8bae1dSRodney W. Grimes {
25385d94372SRobert Watson 	struct tcpcb *tp = xtp;
25485d94372SRobert Watson 	struct inpcb *inp;
2558b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
25685d94372SRobert Watson 
25785d94372SRobert Watson 	inp = tp->t_inpcb;
2585571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
2598501a69cSRobert Watson 	INP_WLOCK(inp);
260655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_delack) ||
261655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_delack)) {
2628501a69cSRobert Watson 		INP_WUNLOCK(inp);
2638b615593SMarko Zec 		CURVNET_RESTORE();
26485d94372SRobert Watson 		return;
26585d94372SRobert Watson 	}
266e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_delack);
267655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
268655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
269655f934bSMikolaj Golub 		CURVNET_RESTORE();
270655f934bSMikolaj Golub 		return;
271655f934bSMikolaj Golub 	}
2729b8b58e0SJonathan Lemon 	tp->t_flags |= TF_ACKNOW;
27378b50714SRobert Watson 	TCPSTAT_INC(tcps_delack);
27455bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
2758501a69cSRobert Watson 	INP_WUNLOCK(inp);
2768b615593SMarko Zec 	CURVNET_RESTORE();
2779b8b58e0SJonathan Lemon }
2789b8b58e0SJonathan Lemon 
279b07fef50SRandall Stewart void
280b07fef50SRandall Stewart tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp)
281b07fef50SRandall Stewart {
2826573d758SMatt Macy 	if (inp && tp != NULL)
283b07fef50SRandall Stewart 		INP_WUNLOCK(inp);
284b07fef50SRandall Stewart }
285b07fef50SRandall Stewart 
28685d94372SRobert Watson void
28785d94372SRobert Watson tcp_timer_2msl(void *xtp)
2889b8b58e0SJonathan Lemon {
28985d94372SRobert Watson 	struct tcpcb *tp = xtp;
29085d94372SRobert Watson 	struct inpcb *inp;
2916573d758SMatt Macy 	struct epoch_tracker et;
2928b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
2939b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2949b8b58e0SJonathan Lemon 	int ostate;
2959b8b58e0SJonathan Lemon 
2969b8b58e0SJonathan Lemon 	ostate = tp->t_state;
2979b8b58e0SJonathan Lemon #endif
29885d94372SRobert Watson 	inp = tp->t_inpcb;
2995571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
3008501a69cSRobert Watson 	INP_WLOCK(inp);
30185d94372SRobert Watson 	tcp_free_sackholes(tp);
302655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_2msl) ||
303e2f2059fSMike Silbersack 	    !callout_active(&tp->t_timers->tt_2msl)) {
3048501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3058b615593SMarko Zec 		CURVNET_RESTORE();
30685d94372SRobert Watson 		return;
30785d94372SRobert Watson 	}
308e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_2msl);
309655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
310655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
311655f934bSMikolaj Golub 		CURVNET_RESTORE();
312655f934bSMikolaj Golub 		return;
313655f934bSMikolaj Golub 	}
3145571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
3155571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
31685d94372SRobert Watson 	/*
317df8bae1dSRodney W. Grimes 	 * 2 MSL timeout in shutdown went off.  If we're closed but
318df8bae1dSRodney W. Grimes 	 * still waiting for peer to close and connection has been idle
31931a7749dSJulien Charbon 	 * too long delete connection control block.  Otherwise, check
32031a7749dSJulien Charbon 	 * again in a bit.
32131a7749dSJulien Charbon 	 *
32231a7749dSJulien Charbon 	 * If in TIME_WAIT state just ignore as this timeout is handled in
32331a7749dSJulien Charbon 	 * tcp_tw_2msl_scan().
3247c72af87SMohan Srinivasan 	 *
3257c72af87SMohan Srinivasan 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
3267c72af87SMohan Srinivasan 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
3277c72af87SMohan Srinivasan 	 * Ignore fact that there were recent incoming segments.
328df8bae1dSRodney W. Grimes 	 */
32931a7749dSJulien Charbon 	if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
33031a7749dSJulien Charbon 		INP_WUNLOCK(inp);
33131a7749dSJulien Charbon 		CURVNET_RESTORE();
33231a7749dSJulien Charbon 		return;
33331a7749dSJulien Charbon 	}
3347c72af87SMohan Srinivasan 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
33585d94372SRobert Watson 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
3367c72af87SMohan Srinivasan 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
33778b50714SRobert Watson 		TCPSTAT_INC(tcps_finwait2_drops);
3386573d758SMatt Macy 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
339b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
340b07fef50SRandall Stewart 			goto out;
341b07fef50SRandall Stewart 		}
3426573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, et);
34385d94372SRobert Watson 		tp = tcp_close(tp);
3446573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
345b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
346b07fef50SRandall Stewart 		goto out;
3477c72af87SMohan Srinivasan 	} else {
348d6de19acSJulien Charbon 		if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
349b07fef50SRandall Stewart 			callout_reset(&tp->t_timers->tt_2msl,
350b07fef50SRandall Stewart 				      TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
351b07fef50SRandall Stewart 		} else {
3526573d758SMatt Macy 			if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
353b07fef50SRandall Stewart 				tcp_inpinfo_lock_del(inp, tp);
354b07fef50SRandall Stewart 				goto out;
355d6de19acSJulien Charbon 			}
3566573d758SMatt Macy 			INP_INFO_RLOCK_ET(&V_tcbinfo, et);
35785d94372SRobert Watson 			tp = tcp_close(tp);
3586573d758SMatt Macy 			INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
359b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
360b07fef50SRandall Stewart 			goto out;
361b07fef50SRandall Stewart 		}
3627c72af87SMohan Srinivasan 	}
363df8bae1dSRodney W. Grimes 
3649b8b58e0SJonathan Lemon #ifdef TCPDEBUG
365586b4a0eSKonstantin Belousov 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
366fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
3679b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
3689b8b58e0SJonathan Lemon #endif
3695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
3705d06879aSGeorge V. Neville-Neil 
37185d94372SRobert Watson 	if (tp != NULL)
3728501a69cSRobert Watson 		INP_WUNLOCK(inp);
373b07fef50SRandall Stewart out:
3748b615593SMarko Zec 	CURVNET_RESTORE();
3759b8b58e0SJonathan Lemon }
3769b8b58e0SJonathan Lemon 
37785d94372SRobert Watson void
37885d94372SRobert Watson tcp_timer_keep(void *xtp)
3799b8b58e0SJonathan Lemon {
38085d94372SRobert Watson 	struct tcpcb *tp = xtp;
38108517d53SMike Silbersack 	struct tcptemp *t_template;
38285d94372SRobert Watson 	struct inpcb *inp;
3836573d758SMatt Macy 	struct epoch_tracker et;
3848b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
3859b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3869b8b58e0SJonathan Lemon 	int ostate;
3879b8b58e0SJonathan Lemon 
3889b8b58e0SJonathan Lemon 	ostate = tp->t_state;
3899b8b58e0SJonathan Lemon #endif
39085d94372SRobert Watson 	inp = tp->t_inpcb;
3915571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
3928501a69cSRobert Watson 	INP_WLOCK(inp);
393655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_keep) ||
394655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_keep)) {
3958501a69cSRobert Watson 		INP_WUNLOCK(inp);
3968b615593SMarko Zec 		CURVNET_RESTORE();
39785d94372SRobert Watson 		return;
39885d94372SRobert Watson 	}
399e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_keep);
400655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
401655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
402655f934bSMikolaj Golub 		CURVNET_RESTORE();
403655f934bSMikolaj Golub 		return;
404655f934bSMikolaj Golub 	}
4055571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
4065571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
4076d172f58SJonathan T. Looney 
4086d172f58SJonathan T. Looney 	/*
4096d172f58SJonathan T. Looney 	 * Because we don't regularly reset the keepalive callout in
4106d172f58SJonathan T. Looney 	 * the ESTABLISHED state, it may be that we don't actually need
4116d172f58SJonathan T. Looney 	 * to send a keepalive yet. If that occurs, schedule another
4126d172f58SJonathan T. Looney 	 * call for the next time the keepalive timer might expire.
4136d172f58SJonathan T. Looney 	 */
4146d172f58SJonathan T. Looney 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
4156d172f58SJonathan T. Looney 		u_int idletime;
4166d172f58SJonathan T. Looney 
4176d172f58SJonathan T. Looney 		idletime = ticks - tp->t_rcvtime;
4186d172f58SJonathan T. Looney 		if (idletime < TP_KEEPIDLE(tp)) {
4196d172f58SJonathan T. Looney 			callout_reset(&tp->t_timers->tt_keep,
4206d172f58SJonathan T. Looney 			    TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp);
4216d172f58SJonathan T. Looney 			INP_WUNLOCK(inp);
4226d172f58SJonathan T. Looney 			CURVNET_RESTORE();
4236d172f58SJonathan T. Looney 			return;
4246d172f58SJonathan T. Looney 		}
4256d172f58SJonathan T. Looney 	}
4266d172f58SJonathan T. Looney 
4279b8b58e0SJonathan Lemon 	/*
4289b8b58e0SJonathan Lemon 	 * Keep-alive timer went off; send something
4299b8b58e0SJonathan Lemon 	 * or drop connection if idle for too long.
4309b8b58e0SJonathan Lemon 	 */
43178b50714SRobert Watson 	TCPSTAT_INC(tcps_keeptimeo);
4329b8b58e0SJonathan Lemon 	if (tp->t_state < TCPS_ESTABLISHED)
4339b8b58e0SJonathan Lemon 		goto dropit;
434f1798531SJohn Baldwin 	if ((tcp_always_keepalive ||
435f1798531SJohn Baldwin 	    inp->inp_socket->so_options & SO_KEEPALIVE) &&
4369b8b58e0SJonathan Lemon 	    tp->t_state <= TCPS_CLOSING) {
4379077f387SGleb Smirnoff 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
4389b8b58e0SJonathan Lemon 			goto dropit;
4399b8b58e0SJonathan Lemon 		/*
4409b8b58e0SJonathan Lemon 		 * Send a packet designed to force a response
4419b8b58e0SJonathan Lemon 		 * if the peer is up and reachable:
4429b8b58e0SJonathan Lemon 		 * either an ACK if the connection is still alive,
4439b8b58e0SJonathan Lemon 		 * or an RST if the peer has closed the connection
4449b8b58e0SJonathan Lemon 		 * due to timeout or reboot.
4459b8b58e0SJonathan Lemon 		 * Using sequence number tp->snd_una-1
4469b8b58e0SJonathan Lemon 		 * causes the transmitted zero-length segment
4479b8b58e0SJonathan Lemon 		 * to lie outside the receive window;
4489b8b58e0SJonathan Lemon 		 * by the protocol spec, this requires the
4499b8b58e0SJonathan Lemon 		 * correspondent TCP to respond.
4509b8b58e0SJonathan Lemon 		 */
45178b50714SRobert Watson 		TCPSTAT_INC(tcps_keepprobe);
45279909384SJonathan Lemon 		t_template = tcpip_maketemplate(inp);
45308517d53SMike Silbersack 		if (t_template) {
45408517d53SMike Silbersack 			tcp_respond(tp, t_template->tt_ipgen,
45508517d53SMike Silbersack 				    &t_template->tt_t, (struct mbuf *)NULL,
4569b8b58e0SJonathan Lemon 				    tp->rcv_nxt, tp->snd_una - 1, 0);
45753640b0eSRobert Watson 			free(t_template, M_TEMP);
45808517d53SMike Silbersack 		}
459b07fef50SRandall Stewart 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
460b07fef50SRandall Stewart 			      tcp_timer_keep, tp);
461b07fef50SRandall Stewart 	} else
462b07fef50SRandall Stewart 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
463b07fef50SRandall Stewart 			      tcp_timer_keep, tp);
4649b8b58e0SJonathan Lemon 
4659b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4662a074620SSam Leffler 	if (inp->inp_socket->so_options & SO_DEBUG)
467fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
4689b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
4699b8b58e0SJonathan Lemon #endif
4705d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
4718501a69cSRobert Watson 	INP_WUNLOCK(inp);
4728b615593SMarko Zec 	CURVNET_RESTORE();
47385d94372SRobert Watson 	return;
4749b8b58e0SJonathan Lemon 
4759b8b58e0SJonathan Lemon dropit:
47678b50714SRobert Watson 	TCPSTAT_INC(tcps_keepdrops);
4776573d758SMatt Macy 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
478b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
479b07fef50SRandall Stewart 		goto out;
480b07fef50SRandall Stewart 	}
4816573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
48285d94372SRobert Watson 	tp = tcp_drop(tp, ETIMEDOUT);
48385d94372SRobert Watson 
48485d94372SRobert Watson #ifdef TCPDEBUG
48585d94372SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
48685d94372SRobert Watson 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
48785d94372SRobert Watson 			  PRU_SLOWTIMO);
48885d94372SRobert Watson #endif
4895d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
4906573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
491b07fef50SRandall Stewart 	tcp_inpinfo_lock_del(inp, tp);
492b07fef50SRandall Stewart  out:
4938b615593SMarko Zec 	CURVNET_RESTORE();
4949b8b58e0SJonathan Lemon }
4959b8b58e0SJonathan Lemon 
49685d94372SRobert Watson void
49785d94372SRobert Watson tcp_timer_persist(void *xtp)
4989b8b58e0SJonathan Lemon {
49985d94372SRobert Watson 	struct tcpcb *tp = xtp;
50085d94372SRobert Watson 	struct inpcb *inp;
5016573d758SMatt Macy 	struct epoch_tracker et;
5028b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
5039b8b58e0SJonathan Lemon #ifdef TCPDEBUG
5049b8b58e0SJonathan Lemon 	int ostate;
5059b8b58e0SJonathan Lemon 
5069b8b58e0SJonathan Lemon 	ostate = tp->t_state;
5079b8b58e0SJonathan Lemon #endif
50885d94372SRobert Watson 	inp = tp->t_inpcb;
5095571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
5108501a69cSRobert Watson 	INP_WLOCK(inp);
511655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_persist) ||
512655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_persist)) {
5138501a69cSRobert Watson 		INP_WUNLOCK(inp);
5148b615593SMarko Zec 		CURVNET_RESTORE();
51585d94372SRobert Watson 		return;
51685d94372SRobert Watson 	}
517e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_persist);
518655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
519655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
520655f934bSMikolaj Golub 		CURVNET_RESTORE();
521655f934bSMikolaj Golub 		return;
522655f934bSMikolaj Golub 	}
5235571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
5245571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
5259b8b58e0SJonathan Lemon 	/*
526a4641f4eSPedro F. Giffuni 	 * Persistence timer into zero window.
5279b8b58e0SJonathan Lemon 	 * Force a byte to be output, if possible.
5289b8b58e0SJonathan Lemon 	 */
52978b50714SRobert Watson 	TCPSTAT_INC(tcps_persisttimeo);
5309b8b58e0SJonathan Lemon 	/*
5319b8b58e0SJonathan Lemon 	 * Hack: if the peer is dead/unreachable, we do not
5329b8b58e0SJonathan Lemon 	 * time out if the window is closed.  After a full
5339b8b58e0SJonathan Lemon 	 * backoff, drop the connection if the idle time
5349b8b58e0SJonathan Lemon 	 * (no responses to probes) reaches the maximum
5359b8b58e0SJonathan Lemon 	 * backoff that we would use if retransmitting.
5369b8b58e0SJonathan Lemon 	 */
5379b8b58e0SJonathan Lemon 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
5386b0c5521SJohn Baldwin 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
5396b0c5521SJohn Baldwin 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
54078b50714SRobert Watson 		TCPSTAT_INC(tcps_persistdrop);
5416573d758SMatt Macy 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
542b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
543b07fef50SRandall Stewart 			goto out;
544b07fef50SRandall Stewart 		}
5456573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, et);
54685d94372SRobert Watson 		tp = tcp_drop(tp, ETIMEDOUT);
5476573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
548b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
54985d94372SRobert Watson 		goto out;
5509b8b58e0SJonathan Lemon 	}
551322181c9SAndre Oppermann 	/*
552322181c9SAndre Oppermann 	 * If the user has closed the socket then drop a persisting
553322181c9SAndre Oppermann 	 * connection after a much reduced timeout.
554322181c9SAndre Oppermann 	 */
555322181c9SAndre Oppermann 	if (tp->t_state > TCPS_CLOSE_WAIT &&
556322181c9SAndre Oppermann 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
557322181c9SAndre Oppermann 		TCPSTAT_INC(tcps_persistdrop);
5586573d758SMatt Macy 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
559b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
560b07fef50SRandall Stewart 			goto out;
561b07fef50SRandall Stewart 		}
5626573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, et);
563322181c9SAndre Oppermann 		tp = tcp_drop(tp, ETIMEDOUT);
5646573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
565b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
566322181c9SAndre Oppermann 		goto out;
567322181c9SAndre Oppermann 	}
5689b8b58e0SJonathan Lemon 	tcp_setpersist(tp);
5692cdbfa66SPaul Saab 	tp->t_flags |= TF_FORCEDATA;
57055bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
5712cdbfa66SPaul Saab 	tp->t_flags &= ~TF_FORCEDATA;
5729b8b58e0SJonathan Lemon 
5739b8b58e0SJonathan Lemon #ifdef TCPDEBUG
574ffb761f6SGleb Smirnoff 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
575ffb761f6SGleb Smirnoff 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
5769b8b58e0SJonathan Lemon #endif
5775d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
5788501a69cSRobert Watson 	INP_WUNLOCK(inp);
579b07fef50SRandall Stewart out:
5808b615593SMarko Zec 	CURVNET_RESTORE();
5819b8b58e0SJonathan Lemon }
5829b8b58e0SJonathan Lemon 
58385d94372SRobert Watson void
58485d94372SRobert Watson tcp_timer_rexmt(void * xtp)
5859b8b58e0SJonathan Lemon {
58685d94372SRobert Watson 	struct tcpcb *tp = xtp;
5878b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
5889b8b58e0SJonathan Lemon 	int rexmt;
58985d94372SRobert Watson 	struct inpcb *inp;
5906573d758SMatt Macy 	struct epoch_tracker et;
5919b8b58e0SJonathan Lemon #ifdef TCPDEBUG
5929b8b58e0SJonathan Lemon 	int ostate;
5939b8b58e0SJonathan Lemon 
5949b8b58e0SJonathan Lemon 	ostate = tp->t_state;
5959b8b58e0SJonathan Lemon #endif
59685d94372SRobert Watson 	inp = tp->t_inpcb;
5975571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
5988501a69cSRobert Watson 	INP_WLOCK(inp);
599655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
600655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_rexmt)) {
6018501a69cSRobert Watson 		INP_WUNLOCK(inp);
6028b615593SMarko Zec 		CURVNET_RESTORE();
60385d94372SRobert Watson 		return;
60485d94372SRobert Watson 	}
605e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_rexmt);
606655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
607655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
608655f934bSMikolaj Golub 		CURVNET_RESTORE();
609655f934bSMikolaj Golub 		return;
610655f934bSMikolaj Golub 	}
6115571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
6125571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
6136d90faf3SPaul Saab 	tcp_free_sackholes(tp);
6142529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL, NULL, NULL, TCP_LOG_RTO, 0, 0, NULL, false);
6155105a92cSRandall Stewart 	if (tp->t_fb->tfb_tcp_rexmit_tmr) {
6165105a92cSRandall Stewart 		/* The stack has a timer action too. */
6175105a92cSRandall Stewart 		(*tp->t_fb->tfb_tcp_rexmit_tmr)(tp);
6185105a92cSRandall Stewart 	}
619df8bae1dSRodney W. Grimes 	/*
620df8bae1dSRodney W. Grimes 	 * Retransmission timer went off.  Message has not
621df8bae1dSRodney W. Grimes 	 * been acked within retransmit interval.  Back off
622df8bae1dSRodney W. Grimes 	 * to a longer retransmit interval and retransmit one segment.
623df8bae1dSRodney W. Grimes 	 */
624df8bae1dSRodney W. Grimes 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
625df8bae1dSRodney W. Grimes 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
62678b50714SRobert Watson 		TCPSTAT_INC(tcps_timeoutdrop);
6276573d758SMatt Macy 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
628b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
62985d94372SRobert Watson 			goto out;
6309b8b58e0SJonathan Lemon 		}
6316573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, et);
6324c6a1090SMichael Tuexen 		tp = tcp_drop(tp, ETIMEDOUT);
6336573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
634b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
635b07fef50SRandall Stewart 		goto out;
636b07fef50SRandall Stewart 	}
637cf8f04f4SAndre Oppermann 	if (tp->t_state == TCPS_SYN_SENT) {
638cf8f04f4SAndre Oppermann 		/*
639cf8f04f4SAndre Oppermann 		 * If the SYN was retransmitted, indicate CWND to be
640cf8f04f4SAndre Oppermann 		 * limited to 1 segment in cc_conn_init().
641cf8f04f4SAndre Oppermann 		 */
642cf8f04f4SAndre Oppermann 		tp->snd_cwnd = 1;
643cf8f04f4SAndre Oppermann 	} else if (tp->t_rxtshift == 1) {
6449b8b58e0SJonathan Lemon 		/*
6459b8b58e0SJonathan Lemon 		 * first retransmit; record ssthresh and cwnd so they can
6469b8b58e0SJonathan Lemon 		 * be recovered if this turns out to be a "bad" retransmit.
6479b8b58e0SJonathan Lemon 		 * A retransmit is considered "bad" if an ACK for this
6489b8b58e0SJonathan Lemon 		 * segment is received within RTT/2 interval; the assumption
6499b8b58e0SJonathan Lemon 		 * here is that the ACK was already in flight.  See
6509b8b58e0SJonathan Lemon 		 * "On Estimating End-to-End Network Path Properties" by
6519b8b58e0SJonathan Lemon 		 * Allman and Paxson for more details.
6529b8b58e0SJonathan Lemon 		 */
6539b8b58e0SJonathan Lemon 		tp->snd_cwnd_prev = tp->snd_cwnd;
6549b8b58e0SJonathan Lemon 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
6559d11646dSJeffrey Hsu 		tp->snd_recover_prev = tp->snd_recover;
656dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags))
6579d11646dSJeffrey Hsu 			tp->t_flags |= TF_WASFRECOVERY;
6589d11646dSJeffrey Hsu 		else
6599d11646dSJeffrey Hsu 			tp->t_flags &= ~TF_WASFRECOVERY;
660dbc42409SLawrence Stewart 		if (IN_CONGRECOVERY(tp->t_flags))
661dbc42409SLawrence Stewart 			tp->t_flags |= TF_WASCRECOVERY;
662dbc42409SLawrence Stewart 		else
663dbc42409SLawrence Stewart 			tp->t_flags &= ~TF_WASCRECOVERY;
66410d20c84SMatt Macy 		if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
6659b8b58e0SJonathan Lemon 			tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
66610d20c84SMatt Macy 		/* In the event that we've negotiated timestamps
66710d20c84SMatt Macy 		 * badrxtwin will be set to the value that we set
66810d20c84SMatt Macy 		 * the retransmitted packet's to_tsval to by tcp_output
66910d20c84SMatt Macy 		 */
670672dc4aeSJohn Baldwin 		tp->t_flags |= TF_PREVVALID;
671672dc4aeSJohn Baldwin 	} else
672672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
67378b50714SRobert Watson 	TCPSTAT_INC(tcps_rexmttimeo);
674281a0fd4SPatrick Kelsey 	if ((tp->t_state == TCPS_SYN_SENT) ||
675281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
676*0999766dSMichael Tuexen 		rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift];
6777d42e30cSJonathan Lemon 	else
678df8bae1dSRodney W. Grimes 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
679df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
680df8bae1dSRodney W. Grimes 		      tp->t_rttmin, TCPTV_REXMTMAX);
681f6f6703fSSean Bruno 
682882ac53eSSean Bruno 	/*
683882ac53eSSean Bruno 	 * We enter the path for PLMTUD if connection is established or, if
684882ac53eSSean Bruno 	 * connection is FIN_WAIT_1 status, reason for the last is that if
685882ac53eSSean Bruno 	 * amount of data we send is very small, we could send it in couple of
686882ac53eSSean Bruno 	 * packets and process straight to FIN. In that case we won't catch
687882ac53eSSean Bruno 	 * ESTABLISHED state.
688882ac53eSSean Bruno 	 */
689882ac53eSSean Bruno 	if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
690882ac53eSSean Bruno 	    || (tp->t_state == TCPS_FIN_WAIT_1))) {
691f6f6703fSSean Bruno #ifdef INET6
692f6f6703fSSean Bruno 		int isipv6;
693f6f6703fSSean Bruno #endif
694f6f6703fSSean Bruno 
695adf43a92SHiren Panchasara 		/*
696adf43a92SHiren Panchasara 		 * Idea here is that at each stage of mtu probe (usually, 1448
697adf43a92SHiren Panchasara 		 * -> 1188 -> 524) should be given 2 chances to recover before
698adf43a92SHiren Panchasara 		 *  further clamping down. 'tp->t_rxtshift % 2 == 0' should
699adf43a92SHiren Panchasara 		 *  take care of that.
700adf43a92SHiren Panchasara 		 */
701f6f6703fSSean Bruno 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
702f6f6703fSSean Bruno 		    (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
7033d5af7a1SMichael Tuexen 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift < 6 &&
7043d5af7a1SMichael Tuexen 		    tp->t_rxtshift % 2 == 0)) {
705f6f6703fSSean Bruno 			/*
706f6f6703fSSean Bruno 			 * Enter Path MTU Black-hole Detection mechanism:
707f6f6703fSSean Bruno 			 * - Disable Path MTU Discovery (IP "DF" bit).
708f6f6703fSSean Bruno 			 * - Reduce MTU to lower value than what we
709f6f6703fSSean Bruno 			 *   negotiated with peer.
710f6f6703fSSean Bruno 			 */
7113d5af7a1SMichael Tuexen 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
712f6f6703fSSean Bruno 				/* Record that we may have found a black hole. */
713f6f6703fSSean Bruno 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
714f6f6703fSSean Bruno 				/* Keep track of previous MSS. */
7150c39d38dSGleb Smirnoff 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
7163d5af7a1SMichael Tuexen 			}
717f6f6703fSSean Bruno 
718f6f6703fSSean Bruno 			/*
719f6f6703fSSean Bruno 			 * Reduce the MSS to blackhole value or to the default
720f6f6703fSSean Bruno 			 * in an attempt to retransmit.
721f6f6703fSSean Bruno 			 */
722f6f6703fSSean Bruno #ifdef INET6
723f6f6703fSSean Bruno 			isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
724f6f6703fSSean Bruno 			if (isipv6 &&
7250c39d38dSGleb Smirnoff 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
726f6f6703fSSean Bruno 				/* Use the sysctl tuneable blackhole MSS. */
7270c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
72832a04bb8SSean Bruno 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
729f6f6703fSSean Bruno 			} else if (isipv6) {
730f6f6703fSSean Bruno 				/* Use the default MSS. */
7310c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_v6mssdflt;
732f6f6703fSSean Bruno 				/*
733f6f6703fSSean Bruno 				 * Disable Path MTU Discovery when we switch to
734f6f6703fSSean Bruno 				 * minmss.
735f6f6703fSSean Bruno 				 */
736f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
73732a04bb8SSean Bruno 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
738f6f6703fSSean Bruno 			}
739f6f6703fSSean Bruno #endif
740f6f6703fSSean Bruno #if defined(INET6) && defined(INET)
741f6f6703fSSean Bruno 			else
742f6f6703fSSean Bruno #endif
743f6f6703fSSean Bruno #ifdef INET
7440c39d38dSGleb Smirnoff 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
745f6f6703fSSean Bruno 				/* Use the sysctl tuneable blackhole MSS. */
7460c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
74732a04bb8SSean Bruno 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
748f6f6703fSSean Bruno 			} else {
749f6f6703fSSean Bruno 				/* Use the default MSS. */
7500c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_mssdflt;
751f6f6703fSSean Bruno 				/*
752f6f6703fSSean Bruno 				 * Disable Path MTU Discovery when we switch to
753f6f6703fSSean Bruno 				 * minmss.
754f6f6703fSSean Bruno 				 */
755f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
75632a04bb8SSean Bruno 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
757f6f6703fSSean Bruno 			}
758f6f6703fSSean Bruno #endif
759f6f6703fSSean Bruno 			/*
760f6f6703fSSean Bruno 			 * Reset the slow-start flight size
761f6f6703fSSean Bruno 			 * as it may depend on the new MSS.
762f6f6703fSSean Bruno 			 */
763f6f6703fSSean Bruno 			if (CC_ALGO(tp)->conn_init != NULL)
764f6f6703fSSean Bruno 				CC_ALGO(tp)->conn_init(tp->ccv);
765f6f6703fSSean Bruno 		} else {
766f6f6703fSSean Bruno 			/*
767f6f6703fSSean Bruno 			 * If further retransmissions are still unsuccessful
768f6f6703fSSean Bruno 			 * with a lowered MTU, maybe this isn't a blackhole and
769f6f6703fSSean Bruno 			 * we restore the previous MSS and blackhole detection
770f6f6703fSSean Bruno 			 * flags.
771adf43a92SHiren Panchasara 			 * The limit '6' is determined by giving each probe
772adf43a92SHiren Panchasara 			 * stage (1448, 1188, 524) 2 chances to recover.
773f6f6703fSSean Bruno 			 */
774f6f6703fSSean Bruno 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
7753d5af7a1SMichael Tuexen 			    (tp->t_rxtshift >= 6)) {
776f6f6703fSSean Bruno 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
777f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
7780c39d38dSGleb Smirnoff 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
77932a04bb8SSean Bruno 				TCPSTAT_INC(tcps_pmtud_blackhole_failed);
780f6f6703fSSean Bruno 				/*
781f6f6703fSSean Bruno 				 * Reset the slow-start flight size as it
782f6f6703fSSean Bruno 				 * may depend on the new MSS.
783f6f6703fSSean Bruno 				 */
784f6f6703fSSean Bruno 				if (CC_ALGO(tp)->conn_init != NULL)
785f6f6703fSSean Bruno 					CC_ALGO(tp)->conn_init(tp->ccv);
786f6f6703fSSean Bruno 			}
787f6f6703fSSean Bruno 		}
788f6f6703fSSean Bruno 	}
789f6f6703fSSean Bruno 
790df8bae1dSRodney W. Grimes 	/*
79177339e1cSAndre Oppermann 	 * Disable RFC1323 and SACK if we haven't got any response to
7927ceb7783SJesper Skriver 	 * our third SYN to work-around some broken terminal servers
7937ceb7783SJesper Skriver 	 * (most of which have hopefully been retired) that have bad VJ
7947ceb7783SJesper Skriver 	 * header compression code which trashes TCP segments containing
7957ceb7783SJesper Skriver 	 * unknown-to-them TCP options.
7967ceb7783SJesper Skriver 	 */
7976c0ef895SJohn Baldwin 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
7986c0ef895SJohn Baldwin 	    (tp->t_rxtshift == 3))
799c4ab59c1SAndre Oppermann 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
8007ceb7783SJesper Skriver 	/*
8015ede40dcSRyan Stone 	 * If we backed off this far, notify the L3 protocol that we're having
8025ede40dcSRyan Stone 	 * connection problems.
803df8bae1dSRodney W. Grimes 	 */
8045ede40dcSRyan Stone 	if (tp->t_rxtshift > TCP_RTT_INVALIDATE) {
805fb59c426SYoshinobu Inoue #ifdef INET6
806fb59c426SYoshinobu Inoue 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
807fb59c426SYoshinobu Inoue 			in6_losing(tp->t_inpcb);
80884cc0778SGeorge V. Neville-Neil 		else
809fb59c426SYoshinobu Inoue #endif
81084cc0778SGeorge V. Neville-Neil 			in_losing(tp->t_inpcb);
811df8bae1dSRodney W. Grimes 	}
812df8bae1dSRodney W. Grimes 	tp->snd_nxt = tp->snd_una;
8139d11646dSJeffrey Hsu 	tp->snd_recover = tp->snd_max;
81446f58482SJonathan Lemon 	/*
81574b48c1dSAndras Olah 	 * Force a segment to be sent.
81674b48c1dSAndras Olah 	 */
81774b48c1dSAndras Olah 	tp->t_flags |= TF_ACKNOW;
81874b48c1dSAndras Olah 	/*
819df8bae1dSRodney W. Grimes 	 * If timing a segment in this window, stop the timer.
820df8bae1dSRodney W. Grimes 	 */
8219b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
822dbc42409SLawrence Stewart 
823b5af1b88SLawrence Stewart 	cc_cong_signal(tp, NULL, CC_RTO);
824dbc42409SLawrence Stewart 
82555bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
826df8bae1dSRodney W. Grimes 
8279b8b58e0SJonathan Lemon #ifdef TCPDEBUG
8281c53f806SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
829fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
8309b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
831df8bae1dSRodney W. Grimes #endif
8325d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
8338501a69cSRobert Watson 	INP_WUNLOCK(inp);
834b07fef50SRandall Stewart out:
8358b615593SMarko Zec 	CURVNET_RESTORE();
83685d94372SRobert Watson }
83785d94372SRobert Watson 
83885d94372SRobert Watson void
8395571f9cfSJulien Charbon tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
84085d94372SRobert Watson {
84185d94372SRobert Watson 	struct callout *t_callout;
84218832f1fSJulien Charbon 	timeout_t *f_callout;
84387aedea4SKip Macy 	struct inpcb *inp = tp->t_inpcb;
844883831c6SAdrian Chadd 	int cpu = inp_to_cpuid(inp);
84585d94372SRobert Watson 
84609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
84709fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
84809fe6320SNavdeep Parhar 		return;
84909fe6320SNavdeep Parhar #endif
85009fe6320SNavdeep Parhar 
8515571f9cfSJulien Charbon 	if (tp->t_timers->tt_flags & TT_STOPPED)
8525571f9cfSJulien Charbon 		return;
8535571f9cfSJulien Charbon 
85485d94372SRobert Watson 	switch (timer_type) {
85585d94372SRobert Watson 		case TT_DELACK:
856e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
85785d94372SRobert Watson 			f_callout = tcp_timer_delack;
85885d94372SRobert Watson 			break;
85985d94372SRobert Watson 		case TT_REXMT:
860e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
86185d94372SRobert Watson 			f_callout = tcp_timer_rexmt;
86285d94372SRobert Watson 			break;
86385d94372SRobert Watson 		case TT_PERSIST:
864e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
86585d94372SRobert Watson 			f_callout = tcp_timer_persist;
86685d94372SRobert Watson 			break;
86785d94372SRobert Watson 		case TT_KEEP:
868e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
86985d94372SRobert Watson 			f_callout = tcp_timer_keep;
87085d94372SRobert Watson 			break;
87185d94372SRobert Watson 		case TT_2MSL:
872e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
87385d94372SRobert Watson 			f_callout = tcp_timer_2msl;
87485d94372SRobert Watson 			break;
87585d94372SRobert Watson 		default:
87655bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_activate) {
87755bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
87855bceb1eSRandall Stewart 				return;
87955bceb1eSRandall Stewart 			}
88003374917SJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
88185d94372SRobert Watson 		}
88285d94372SRobert Watson 	if (delta == 0) {
883b07fef50SRandall Stewart 		callout_stop(t_callout);
88485d94372SRobert Watson 	} else {
88587aedea4SKip Macy 		callout_reset_on(t_callout, delta, f_callout, tp, cpu);
88685d94372SRobert Watson 	}
88785d94372SRobert Watson }
88885d94372SRobert Watson 
88985d94372SRobert Watson int
8905571f9cfSJulien Charbon tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
89185d94372SRobert Watson {
89285d94372SRobert Watson 	struct callout *t_callout;
89385d94372SRobert Watson 
89485d94372SRobert Watson 	switch (timer_type) {
89585d94372SRobert Watson 		case TT_DELACK:
896e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
89785d94372SRobert Watson 			break;
89885d94372SRobert Watson 		case TT_REXMT:
899e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
90085d94372SRobert Watson 			break;
90185d94372SRobert Watson 		case TT_PERSIST:
902e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
90385d94372SRobert Watson 			break;
90485d94372SRobert Watson 		case TT_KEEP:
905e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
90685d94372SRobert Watson 			break;
90785d94372SRobert Watson 		case TT_2MSL:
908e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
90985d94372SRobert Watson 			break;
91085d94372SRobert Watson 		default:
91155bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_active) {
91255bceb1eSRandall Stewart 				return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
91355bceb1eSRandall Stewart 			}
91403374917SJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
91585d94372SRobert Watson 		}
91685d94372SRobert Watson 	return callout_active(t_callout);
917df8bae1dSRodney W. Grimes }
918b8614722SMike Silbersack 
91989e560f4SRandall Stewart /*
92089e560f4SRandall Stewart  * Stop the timer from running, and apply a flag
92189e560f4SRandall Stewart  * against the timer_flags that will force the
92289e560f4SRandall Stewart  * timer never to run. The flag is needed to assure
92389e560f4SRandall Stewart  * a race does not leave it running and cause
92489e560f4SRandall Stewart  * the timer to possibly restart itself (keep and persist
92589e560f4SRandall Stewart  * especially do this).
92689e560f4SRandall Stewart  */
92789e560f4SRandall Stewart int
92889e560f4SRandall Stewart tcp_timer_suspend(struct tcpcb *tp, uint32_t timer_type)
92989e560f4SRandall Stewart {
93089e560f4SRandall Stewart 	struct callout *t_callout;
93189e560f4SRandall Stewart 	uint32_t t_flags;
93289e560f4SRandall Stewart 
93389e560f4SRandall Stewart 	switch (timer_type) {
93489e560f4SRandall Stewart 		case TT_DELACK:
93589e560f4SRandall Stewart 			t_flags = TT_DELACK_SUS;
93689e560f4SRandall Stewart 			t_callout = &tp->t_timers->tt_delack;
93789e560f4SRandall Stewart 			break;
93889e560f4SRandall Stewart 		case TT_REXMT:
93989e560f4SRandall Stewart 			t_flags = TT_REXMT_SUS;
94089e560f4SRandall Stewart 			t_callout = &tp->t_timers->tt_rexmt;
94189e560f4SRandall Stewart 			break;
94289e560f4SRandall Stewart 		case TT_PERSIST:
94389e560f4SRandall Stewart 			t_flags = TT_PERSIST_SUS;
94489e560f4SRandall Stewart 			t_callout = &tp->t_timers->tt_persist;
94589e560f4SRandall Stewart 			break;
94689e560f4SRandall Stewart 		case TT_KEEP:
94789e560f4SRandall Stewart 			t_flags = TT_KEEP_SUS;
94889e560f4SRandall Stewart 			t_callout = &tp->t_timers->tt_keep;
94989e560f4SRandall Stewart 			break;
95089e560f4SRandall Stewart 		case TT_2MSL:
95189e560f4SRandall Stewart 			t_flags = TT_2MSL_SUS;
95289e560f4SRandall Stewart 			t_callout = &tp->t_timers->tt_2msl;
95389e560f4SRandall Stewart 			break;
95489e560f4SRandall Stewart 		default:
95589e560f4SRandall Stewart 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
95689e560f4SRandall Stewart 	}
95789e560f4SRandall Stewart 	tp->t_timers->tt_flags |= t_flags;
95889e560f4SRandall Stewart 	return (callout_stop(t_callout));
95989e560f4SRandall Stewart }
96089e560f4SRandall Stewart 
96189e560f4SRandall Stewart void
96289e560f4SRandall Stewart tcp_timers_unsuspend(struct tcpcb *tp, uint32_t timer_type)
96389e560f4SRandall Stewart {
96489e560f4SRandall Stewart 	switch (timer_type) {
96589e560f4SRandall Stewart 		case TT_DELACK:
96689e560f4SRandall Stewart 			if (tp->t_timers->tt_flags & TT_DELACK_SUS) {
96789e560f4SRandall Stewart 				tp->t_timers->tt_flags &= ~TT_DELACK_SUS;
96889e560f4SRandall Stewart 				if (tp->t_flags & TF_DELACK) {
96989e560f4SRandall Stewart 					/* Delayed ack timer should be up activate a timer */
97089e560f4SRandall Stewart 					tp->t_flags &= ~TF_DELACK;
97189e560f4SRandall Stewart 					tcp_timer_activate(tp, TT_DELACK,
97289e560f4SRandall Stewart 					    tcp_delacktime);
97389e560f4SRandall Stewart 				}
97489e560f4SRandall Stewart 			}
97589e560f4SRandall Stewart 			break;
97689e560f4SRandall Stewart 		case TT_REXMT:
97789e560f4SRandall Stewart 			if (tp->t_timers->tt_flags & TT_REXMT_SUS) {
97889e560f4SRandall Stewart 				tp->t_timers->tt_flags &= ~TT_REXMT_SUS;
97989e560f4SRandall Stewart 				if (SEQ_GT(tp->snd_max, tp->snd_una) &&
98089e560f4SRandall Stewart 				    (tcp_timer_active((tp), TT_PERSIST) == 0) &&
98189e560f4SRandall Stewart 				    tp->snd_wnd) {
98289e560f4SRandall Stewart 					/* We have outstanding data activate a timer */
98389e560f4SRandall Stewart 					tcp_timer_activate(tp, TT_REXMT,
98489e560f4SRandall Stewart                                             tp->t_rxtcur);
98589e560f4SRandall Stewart 				}
98689e560f4SRandall Stewart 			}
98789e560f4SRandall Stewart 			break;
98889e560f4SRandall Stewart 		case TT_PERSIST:
98989e560f4SRandall Stewart 			if (tp->t_timers->tt_flags & TT_PERSIST_SUS) {
99089e560f4SRandall Stewart 				tp->t_timers->tt_flags &= ~TT_PERSIST_SUS;
99189e560f4SRandall Stewart 				if (tp->snd_wnd == 0) {
99289e560f4SRandall Stewart 					/* Activate the persists timer */
99389e560f4SRandall Stewart 					tp->t_rxtshift = 0;
99489e560f4SRandall Stewart 					tcp_setpersist(tp);
99589e560f4SRandall Stewart 				}
99689e560f4SRandall Stewart 			}
99789e560f4SRandall Stewart 			break;
99889e560f4SRandall Stewart 		case TT_KEEP:
99989e560f4SRandall Stewart 			if (tp->t_timers->tt_flags & TT_KEEP_SUS) {
100089e560f4SRandall Stewart 				tp->t_timers->tt_flags &= ~TT_KEEP_SUS;
100189e560f4SRandall Stewart 				tcp_timer_activate(tp, TT_KEEP,
100289e560f4SRandall Stewart 					    TCPS_HAVEESTABLISHED(tp->t_state) ?
100389e560f4SRandall Stewart 					    TP_KEEPIDLE(tp) : TP_KEEPINIT(tp));
100489e560f4SRandall Stewart 			}
100589e560f4SRandall Stewart 			break;
100689e560f4SRandall Stewart 		case TT_2MSL:
100789e560f4SRandall Stewart 			if (tp->t_timers->tt_flags &= TT_2MSL_SUS) {
100889e560f4SRandall Stewart 				tp->t_timers->tt_flags &= ~TT_2MSL_SUS;
100989e560f4SRandall Stewart 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
101089e560f4SRandall Stewart 				    ((tp->t_inpcb->inp_socket == NULL) ||
101189e560f4SRandall Stewart 				     (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE))) {
101289e560f4SRandall Stewart 					/* Star the 2MSL timer */
101389e560f4SRandall Stewart 					tcp_timer_activate(tp, TT_2MSL,
101489e560f4SRandall Stewart 					    (tcp_fast_finwait2_recycle) ?
101589e560f4SRandall Stewart 					    tcp_finwait2_timeout : TP_MAXIDLE(tp));
101689e560f4SRandall Stewart 				}
101789e560f4SRandall Stewart 			}
101889e560f4SRandall Stewart 			break;
101989e560f4SRandall Stewart 		default:
102089e560f4SRandall Stewart 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
102189e560f4SRandall Stewart 	}
102289e560f4SRandall Stewart }
102389e560f4SRandall Stewart 
10245571f9cfSJulien Charbon void
10255571f9cfSJulien Charbon tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
10265571f9cfSJulien Charbon {
10275571f9cfSJulien Charbon 	struct callout *t_callout;
10285571f9cfSJulien Charbon 
10295571f9cfSJulien Charbon 	tp->t_timers->tt_flags |= TT_STOPPED;
10305571f9cfSJulien Charbon 	switch (timer_type) {
10315571f9cfSJulien Charbon 		case TT_DELACK:
10325571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_delack;
10335571f9cfSJulien Charbon 			break;
10345571f9cfSJulien Charbon 		case TT_REXMT:
10355571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_rexmt;
10365571f9cfSJulien Charbon 			break;
10375571f9cfSJulien Charbon 		case TT_PERSIST:
10385571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_persist;
10395571f9cfSJulien Charbon 			break;
10405571f9cfSJulien Charbon 		case TT_KEEP:
10415571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_keep;
10425571f9cfSJulien Charbon 			break;
10435571f9cfSJulien Charbon 		case TT_2MSL:
10445571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_2msl;
10455571f9cfSJulien Charbon 			break;
10465571f9cfSJulien Charbon 		default:
104755bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_stop) {
104855bceb1eSRandall Stewart 				/*
104955bceb1eSRandall Stewart 				 * XXXrrs we need to look at this with the
105055bceb1eSRandall Stewart 				 * stop case below (flags).
105155bceb1eSRandall Stewart 				 */
105255bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
105355bceb1eSRandall Stewart 				return;
105455bceb1eSRandall Stewart 			}
10555571f9cfSJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
10565571f9cfSJulien Charbon 		}
10575571f9cfSJulien Charbon 
1058e5ad6456SRandall Stewart 	if (callout_async_drain(t_callout, tcp_timer_discard) == 0) {
10595571f9cfSJulien Charbon 		/*
10605571f9cfSJulien Charbon 		 * Can't stop the callout, defer tcpcb actual deletion
1061e5ad6456SRandall Stewart 		 * to the last one. We do this using the async drain
1062e5ad6456SRandall Stewart 		 * function and incrementing the count in
10635571f9cfSJulien Charbon 		 */
1064e5ad6456SRandall Stewart 		tp->t_timers->tt_draincnt++;
10655571f9cfSJulien Charbon 	}
10665571f9cfSJulien Charbon }
1067