xref: /freebsd/sys/netinet/tcp_timer.c (revision 883831c675caf07e334bf083d0e4e64bc8ad04ab)
1c398230bSWarner Losh /*-
2e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29e79adb8eSGarrett Wollman  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
35825fd1e4SNavdeep Parhar #include "opt_inet.h"
36fb59c426SYoshinobu Inoue #include "opt_inet6.h"
370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
38*883831c6SAdrian Chadd #include "opt_rss.h"
390cc12cc5SJoerg Wunsch 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
4198163b98SPoul-Henning Kamp #include <sys/kernel.h>
42c74af4faSBruce Evans #include <sys/lock.h>
4308517d53SMike Silbersack #include <sys/mbuf.h>
44c74af4faSBruce Evans #include <sys/mutex.h>
45c74af4faSBruce Evans #include <sys/protosw.h>
4687aedea4SKip Macy #include <sys/smp.h>
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
49c74af4faSBruce Evans #include <sys/sysctl.h>
50c74af4faSBruce Evans #include <sys/systm.h>
51e79adb8eSGarrett Wollman 
524b79449eSBjoern A. Zeeb #include <net/if.h>
53df8bae1dSRodney W. Grimes #include <net/route.h>
54530c0060SRobert Watson #include <net/vnet.h>
55*883831c6SAdrian Chadd #include <net/netisr.h>
56df8bae1dSRodney W. Grimes 
57dbc42409SLawrence Stewart #include <netinet/cc.h>
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
60*883831c6SAdrian Chadd #include <netinet/in_rss.h>
61c74af4faSBruce Evans #include <netinet/in_systm.h>
62fb59c426SYoshinobu Inoue #ifdef INET6
63fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
64fb59c426SYoshinobu Inoue #endif
65df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
67df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
68df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
70af7a2999SDavid Greenman #ifdef TCPDEBUG
71af7a2999SDavid Greenman #include <netinet/tcp_debug.h>
72af7a2999SDavid Greenman #endif
73df8bae1dSRodney W. Grimes 
749b8b58e0SJonathan Lemon int	tcp_keepinit;
75ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
7641698ebfSTom Rhodes     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
777b40aa32SPaul Traina 
789b8b58e0SJonathan Lemon int	tcp_keepidle;
79ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
8041698ebfSTom Rhodes     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
8198163b98SPoul-Henning Kamp 
829b8b58e0SJonathan Lemon int	tcp_keepintvl;
83ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
8441698ebfSTom Rhodes     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
8598163b98SPoul-Henning Kamp 
869b8b58e0SJonathan Lemon int	tcp_delacktime;
876489fe65SAndre Oppermann SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
886489fe65SAndre Oppermann     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
89ccb4d0c6SJonathan Lemon     "Time before a delayed ACK is sent");
909b8b58e0SJonathan Lemon 
919b8b58e0SJonathan Lemon int	tcp_msl;
92ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
93ccb4d0c6SJonathan Lemon     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
949b8b58e0SJonathan Lemon 
95701bec5aSMatthew Dillon int	tcp_rexmit_min;
96701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
976489fe65SAndre Oppermann     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
986489fe65SAndre Oppermann     "Minimum Retransmission Timeout");
99701bec5aSMatthew Dillon 
100701bec5aSMatthew Dillon int	tcp_rexmit_slop;
101701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
1026489fe65SAndre Oppermann     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
1036489fe65SAndre Oppermann     "Retransmission Timer Slop");
104701bec5aSMatthew Dillon 
105c39a614eSRobert Watson static int	always_keepalive = 1;
1063d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
1073d177f46SBill Fumerola     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
10834be9bf3SPoul-Henning Kamp 
1097c72af87SMohan Srinivasan int    tcp_fast_finwait2_recycle = 0;
1107c72af87SMohan Srinivasan SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
1116489fe65SAndre Oppermann     &tcp_fast_finwait2_recycle, 0,
1126489fe65SAndre Oppermann     "Recycle closed FIN_WAIT_2 connections faster");
1137c72af87SMohan Srinivasan 
1147c72af87SMohan Srinivasan int    tcp_finwait2_timeout;
1157c72af87SMohan Srinivasan SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
1166489fe65SAndre Oppermann     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
1177c72af87SMohan Srinivasan 
1189077f387SGleb Smirnoff int	tcp_keepcnt = TCPTV_KEEPCNT;
1199077f387SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
1209077f387SGleb Smirnoff     "Number of keepalive probes to send");
1217c72af87SMohan Srinivasan 
1220312fbe9SPoul-Henning Kamp 	/* max idle probes */
1239b8b58e0SJonathan Lemon int	tcp_maxpersistidle;
124e79adb8eSGarrett Wollman 
1256c0ef895SJohn Baldwin static int	tcp_rexmit_drop_options = 0;
1266c0ef895SJohn Baldwin SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
1276c0ef895SJohn Baldwin     &tcp_rexmit_drop_options, 0,
1286c0ef895SJohn Baldwin     "Drop TCP options from 3rd and later retransmitted SYN");
1296c0ef895SJohn Baldwin 
13087aedea4SKip Macy static int	per_cpu_timers = 0;
13187aedea4SKip Macy SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
13287aedea4SKip Macy     &per_cpu_timers , 0, "run tcp timers on all cpus");
13387aedea4SKip Macy 
134*883831c6SAdrian Chadd #if 0
13587aedea4SKip Macy #define	INP_CPU(inp)	(per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
13687aedea4SKip Macy 		((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
137*883831c6SAdrian Chadd #endif
138*883831c6SAdrian Chadd 
139*883831c6SAdrian Chadd /*
140*883831c6SAdrian Chadd  * Map the given inp to a CPU id.
141*883831c6SAdrian Chadd  *
142*883831c6SAdrian Chadd  * This queries RSS if it's compiled in, else it defaults to the current
143*883831c6SAdrian Chadd  * CPU ID.
144*883831c6SAdrian Chadd  */
145*883831c6SAdrian Chadd static inline int
146*883831c6SAdrian Chadd inp_to_cpuid(struct inpcb *inp)
147*883831c6SAdrian Chadd {
148*883831c6SAdrian Chadd 	u_int cpuid;
149*883831c6SAdrian Chadd 
150*883831c6SAdrian Chadd #ifdef	RSS
151*883831c6SAdrian Chadd 	if (per_cpu_timers) {
152*883831c6SAdrian Chadd 		cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
153*883831c6SAdrian Chadd 		if (cpuid == NETISR_CPUID_NONE)
154*883831c6SAdrian Chadd 			return (curcpu);	/* XXX */
155*883831c6SAdrian Chadd 		else
156*883831c6SAdrian Chadd 			return (cpuid);
157*883831c6SAdrian Chadd 	}
158*883831c6SAdrian Chadd #else
159*883831c6SAdrian Chadd 	/* Legacy, pre-RSS behaviour */
160*883831c6SAdrian Chadd 	if (per_cpu_timers) {
161*883831c6SAdrian Chadd 		/*
162*883831c6SAdrian Chadd 		 * We don't have a flowid -> cpuid mapping, so cheat and
163*883831c6SAdrian Chadd 		 * just map unknown cpuids to curcpu.  Not the best, but
164*883831c6SAdrian Chadd 		 * apparently better than defaulting to swi 0.
165*883831c6SAdrian Chadd 		 */
166*883831c6SAdrian Chadd 		cpuid = inp->inp_flowid % (mp_maxid + 1);
167*883831c6SAdrian Chadd 		if (! CPU_ABSENT(cpuid))
168*883831c6SAdrian Chadd 			return (cpuid);
169*883831c6SAdrian Chadd 		return (curcpu);
170*883831c6SAdrian Chadd 	}
171*883831c6SAdrian Chadd #endif
172*883831c6SAdrian Chadd 	/* Default for RSS and non-RSS - cpuid 0 */
173*883831c6SAdrian Chadd 	else {
174*883831c6SAdrian Chadd 		return (0);
175*883831c6SAdrian Chadd 	}
176*883831c6SAdrian Chadd }
17787aedea4SKip Macy 
178df8bae1dSRodney W. Grimes /*
179df8bae1dSRodney W. Grimes  * Tcp protocol timeout routine called every 500 ms.
1809b8b58e0SJonathan Lemon  * Updates timestamps used for TCP
181df8bae1dSRodney W. Grimes  * causes finite state machine actions if timers expire.
182df8bae1dSRodney W. Grimes  */
183df8bae1dSRodney W. Grimes void
184e2f2059fSMike Silbersack tcp_slowtimo(void)
185df8bae1dSRodney W. Grimes {
1868b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
18715bd2b43SDavid Greenman 
1885ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
1898b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
1908b615593SMarko Zec 		CURVNET_SET(vnet_iter);
19166eefb1eSJohn Baldwin 		tcp_tw_2msl_scan();
1928b615593SMarko Zec 		CURVNET_RESTORE();
1938b615593SMarko Zec 	}
1945ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
195df8bae1dSRodney W. Grimes }
196df8bae1dSRodney W. Grimes 
1977d42e30cSJonathan Lemon int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
1987d42e30cSJonathan Lemon     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
1997d42e30cSJonathan Lemon 
200df8bae1dSRodney W. Grimes int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
201f058535dSJeffrey Hsu     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
202df8bae1dSRodney W. Grimes 
203f058535dSJeffrey Hsu static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
204e79adb8eSGarrett Wollman 
205623dce13SRobert Watson static int tcp_timer_race;
206623dce13SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, timer_race, CTLFLAG_RD, &tcp_timer_race,
207623dce13SRobert Watson     0, "Count of t_inpcb races on tcp_discardcb");
208623dce13SRobert Watson 
209df8bae1dSRodney W. Grimes /*
210df8bae1dSRodney W. Grimes  * TCP timer processing.
211df8bae1dSRodney W. Grimes  */
21285d94372SRobert Watson 
21385d94372SRobert Watson void
21485d94372SRobert Watson tcp_timer_delack(void *xtp)
215df8bae1dSRodney W. Grimes {
21685d94372SRobert Watson 	struct tcpcb *tp = xtp;
21785d94372SRobert Watson 	struct inpcb *inp;
2188b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
21985d94372SRobert Watson 
22085d94372SRobert Watson 	inp = tp->t_inpcb;
22185d94372SRobert Watson 	/*
22285d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
22385d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
22485d94372SRobert Watson 	 * timers and tcp_discardcb().
22585d94372SRobert Watson 	 *
22685d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_delack: inp == NULL"));
22785d94372SRobert Watson 	 */
22885d94372SRobert Watson 	if (inp == NULL) {
22985d94372SRobert Watson 		tcp_timer_race++;
2308b615593SMarko Zec 		CURVNET_RESTORE();
23185d94372SRobert Watson 		return;
23285d94372SRobert Watson 	}
2338501a69cSRobert Watson 	INP_WLOCK(inp);
234655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_delack) ||
235655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_delack)) {
2368501a69cSRobert Watson 		INP_WUNLOCK(inp);
2378b615593SMarko Zec 		CURVNET_RESTORE();
23885d94372SRobert Watson 		return;
23985d94372SRobert Watson 	}
240e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_delack);
241655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
242655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
243655f934bSMikolaj Golub 		CURVNET_RESTORE();
244655f934bSMikolaj Golub 		return;
245655f934bSMikolaj Golub 	}
246df8bae1dSRodney W. Grimes 
2479b8b58e0SJonathan Lemon 	tp->t_flags |= TF_ACKNOW;
24878b50714SRobert Watson 	TCPSTAT_INC(tcps_delack);
2499b8b58e0SJonathan Lemon 	(void) tcp_output(tp);
2508501a69cSRobert Watson 	INP_WUNLOCK(inp);
2518b615593SMarko Zec 	CURVNET_RESTORE();
2529b8b58e0SJonathan Lemon }
2539b8b58e0SJonathan Lemon 
25485d94372SRobert Watson void
25585d94372SRobert Watson tcp_timer_2msl(void *xtp)
2569b8b58e0SJonathan Lemon {
25785d94372SRobert Watson 	struct tcpcb *tp = xtp;
25885d94372SRobert Watson 	struct inpcb *inp;
2598b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
2609b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2619b8b58e0SJonathan Lemon 	int ostate;
2629b8b58e0SJonathan Lemon 
2639b8b58e0SJonathan Lemon 	ostate = tp->t_state;
2649b8b58e0SJonathan Lemon #endif
265623dce13SRobert Watson 	/*
26685d94372SRobert Watson 	 * XXXRW: Does this actually happen?
26785d94372SRobert Watson 	 */
268603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
26985d94372SRobert Watson 	inp = tp->t_inpcb;
27085d94372SRobert Watson 	/*
27185d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
27285d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
27385d94372SRobert Watson 	 * timers and tcp_discardcb().
27485d94372SRobert Watson 	 *
27585d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_2msl: inp == NULL"));
27685d94372SRobert Watson 	 */
27785d94372SRobert Watson 	if (inp == NULL) {
27885d94372SRobert Watson 		tcp_timer_race++;
279603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2808b615593SMarko Zec 		CURVNET_RESTORE();
28185d94372SRobert Watson 		return;
28285d94372SRobert Watson 	}
2838501a69cSRobert Watson 	INP_WLOCK(inp);
28485d94372SRobert Watson 	tcp_free_sackholes(tp);
285655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_2msl) ||
286e2f2059fSMike Silbersack 	    !callout_active(&tp->t_timers->tt_2msl)) {
2878501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
288603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2898b615593SMarko Zec 		CURVNET_RESTORE();
29085d94372SRobert Watson 		return;
29185d94372SRobert Watson 	}
292e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_2msl);
293655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
294655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
295655f934bSMikolaj Golub 		INP_INFO_WUNLOCK(&V_tcbinfo);
296655f934bSMikolaj Golub 		CURVNET_RESTORE();
297655f934bSMikolaj Golub 		return;
298655f934bSMikolaj Golub 	}
29985d94372SRobert Watson 	/*
300df8bae1dSRodney W. Grimes 	 * 2 MSL timeout in shutdown went off.  If we're closed but
301df8bae1dSRodney W. Grimes 	 * still waiting for peer to close and connection has been idle
302df8bae1dSRodney W. Grimes 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
303df8bae1dSRodney W. Grimes 	 * control block.  Otherwise, check again in a bit.
3047c72af87SMohan Srinivasan 	 *
3057c72af87SMohan Srinivasan 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
3067c72af87SMohan Srinivasan 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
3077c72af87SMohan Srinivasan 	 * Ignore fact that there were recent incoming segments.
308df8bae1dSRodney W. Grimes 	 */
3097c72af87SMohan Srinivasan 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
31085d94372SRobert Watson 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
3117c72af87SMohan Srinivasan 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
31278b50714SRobert Watson 		TCPSTAT_INC(tcps_finwait2_drops);
31385d94372SRobert Watson 		tp = tcp_close(tp);
3147c72af87SMohan Srinivasan 	} else {
315df8bae1dSRodney W. Grimes 		if (tp->t_state != TCPS_TIME_WAIT &&
3169077f387SGleb Smirnoff 		   ticks - tp->t_rcvtime <= TP_MAXIDLE(tp))
3179077f387SGleb Smirnoff 		       callout_reset_on(&tp->t_timers->tt_2msl,
318*883831c6SAdrian Chadd 			   TP_KEEPINTVL(tp), tcp_timer_2msl, tp,
319*883831c6SAdrian Chadd 			   inp_to_cpuid(inp));
320df8bae1dSRodney W. Grimes 	       else
32185d94372SRobert Watson 		       tp = tcp_close(tp);
3227c72af87SMohan Srinivasan        }
323df8bae1dSRodney W. Grimes 
3249b8b58e0SJonathan Lemon #ifdef TCPDEBUG
325586b4a0eSKonstantin Belousov 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
326fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
3279b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
3289b8b58e0SJonathan Lemon #endif
32985d94372SRobert Watson 	if (tp != NULL)
3308501a69cSRobert Watson 		INP_WUNLOCK(inp);
331603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
3328b615593SMarko Zec 	CURVNET_RESTORE();
3339b8b58e0SJonathan Lemon }
3349b8b58e0SJonathan Lemon 
33585d94372SRobert Watson void
33685d94372SRobert Watson tcp_timer_keep(void *xtp)
3379b8b58e0SJonathan Lemon {
33885d94372SRobert Watson 	struct tcpcb *tp = xtp;
33908517d53SMike Silbersack 	struct tcptemp *t_template;
34085d94372SRobert Watson 	struct inpcb *inp;
3418b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
3429b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3439b8b58e0SJonathan Lemon 	int ostate;
3449b8b58e0SJonathan Lemon 
3459b8b58e0SJonathan Lemon 	ostate = tp->t_state;
3469b8b58e0SJonathan Lemon #endif
347603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
34885d94372SRobert Watson 	inp = tp->t_inpcb;
34985d94372SRobert Watson 	/*
35085d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
35185d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
35285d94372SRobert Watson 	 * timers and tcp_discardcb().
35385d94372SRobert Watson 	 *
35485d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_keep: inp == NULL"));
35585d94372SRobert Watson 	 */
35685d94372SRobert Watson 	if (inp == NULL) {
35785d94372SRobert Watson 		tcp_timer_race++;
358603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3598b615593SMarko Zec 		CURVNET_RESTORE();
36085d94372SRobert Watson 		return;
36185d94372SRobert Watson 	}
3628501a69cSRobert Watson 	INP_WLOCK(inp);
363655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_keep) ||
364655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_keep)) {
3658501a69cSRobert Watson 		INP_WUNLOCK(inp);
366603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3678b615593SMarko Zec 		CURVNET_RESTORE();
36885d94372SRobert Watson 		return;
36985d94372SRobert Watson 	}
370e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_keep);
371655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
372655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
373655f934bSMikolaj Golub 		INP_INFO_WUNLOCK(&V_tcbinfo);
374655f934bSMikolaj Golub 		CURVNET_RESTORE();
375655f934bSMikolaj Golub 		return;
376655f934bSMikolaj Golub 	}
3779b8b58e0SJonathan Lemon 	/*
3789b8b58e0SJonathan Lemon 	 * Keep-alive timer went off; send something
3799b8b58e0SJonathan Lemon 	 * or drop connection if idle for too long.
3809b8b58e0SJonathan Lemon 	 */
38178b50714SRobert Watson 	TCPSTAT_INC(tcps_keeptimeo);
3829b8b58e0SJonathan Lemon 	if (tp->t_state < TCPS_ESTABLISHED)
3839b8b58e0SJonathan Lemon 		goto dropit;
3842a074620SSam Leffler 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
3859b8b58e0SJonathan Lemon 	    tp->t_state <= TCPS_CLOSING) {
3869077f387SGleb Smirnoff 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
3879b8b58e0SJonathan Lemon 			goto dropit;
3889b8b58e0SJonathan Lemon 		/*
3899b8b58e0SJonathan Lemon 		 * Send a packet designed to force a response
3909b8b58e0SJonathan Lemon 		 * if the peer is up and reachable:
3919b8b58e0SJonathan Lemon 		 * either an ACK if the connection is still alive,
3929b8b58e0SJonathan Lemon 		 * or an RST if the peer has closed the connection
3939b8b58e0SJonathan Lemon 		 * due to timeout or reboot.
3949b8b58e0SJonathan Lemon 		 * Using sequence number tp->snd_una-1
3959b8b58e0SJonathan Lemon 		 * causes the transmitted zero-length segment
3969b8b58e0SJonathan Lemon 		 * to lie outside the receive window;
3979b8b58e0SJonathan Lemon 		 * by the protocol spec, this requires the
3989b8b58e0SJonathan Lemon 		 * correspondent TCP to respond.
3999b8b58e0SJonathan Lemon 		 */
40078b50714SRobert Watson 		TCPSTAT_INC(tcps_keepprobe);
40179909384SJonathan Lemon 		t_template = tcpip_maketemplate(inp);
40208517d53SMike Silbersack 		if (t_template) {
40308517d53SMike Silbersack 			tcp_respond(tp, t_template->tt_ipgen,
40408517d53SMike Silbersack 				    &t_template->tt_t, (struct mbuf *)NULL,
4059b8b58e0SJonathan Lemon 				    tp->rcv_nxt, tp->snd_una - 1, 0);
40653640b0eSRobert Watson 			free(t_template, M_TEMP);
40708517d53SMike Silbersack 		}
4089077f387SGleb Smirnoff 		callout_reset_on(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
409*883831c6SAdrian Chadd 		    tcp_timer_keep, tp, inp_to_cpuid(inp));
4104cc20ab1SSeigo Tanimura 	} else
4119077f387SGleb Smirnoff 		callout_reset_on(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
412*883831c6SAdrian Chadd 		    tcp_timer_keep, tp, inp_to_cpuid(inp));
4139b8b58e0SJonathan Lemon 
4149b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4152a074620SSam Leffler 	if (inp->inp_socket->so_options & SO_DEBUG)
416fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
4179b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
4189b8b58e0SJonathan Lemon #endif
4198501a69cSRobert Watson 	INP_WUNLOCK(inp);
420603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
4218b615593SMarko Zec 	CURVNET_RESTORE();
42285d94372SRobert Watson 	return;
4239b8b58e0SJonathan Lemon 
4249b8b58e0SJonathan Lemon dropit:
42578b50714SRobert Watson 	TCPSTAT_INC(tcps_keepdrops);
42685d94372SRobert Watson 	tp = tcp_drop(tp, ETIMEDOUT);
42785d94372SRobert Watson 
42885d94372SRobert Watson #ifdef TCPDEBUG
42985d94372SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
43085d94372SRobert Watson 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
43185d94372SRobert Watson 			  PRU_SLOWTIMO);
43285d94372SRobert Watson #endif
43385d94372SRobert Watson 	if (tp != NULL)
4348501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
435603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
4368b615593SMarko Zec 	CURVNET_RESTORE();
4379b8b58e0SJonathan Lemon }
4389b8b58e0SJonathan Lemon 
43985d94372SRobert Watson void
44085d94372SRobert Watson tcp_timer_persist(void *xtp)
4419b8b58e0SJonathan Lemon {
44285d94372SRobert Watson 	struct tcpcb *tp = xtp;
44385d94372SRobert Watson 	struct inpcb *inp;
4448b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
4459b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4469b8b58e0SJonathan Lemon 	int ostate;
4479b8b58e0SJonathan Lemon 
4489b8b58e0SJonathan Lemon 	ostate = tp->t_state;
4499b8b58e0SJonathan Lemon #endif
450603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
45185d94372SRobert Watson 	inp = tp->t_inpcb;
45285d94372SRobert Watson 	/*
45385d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
45485d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
45585d94372SRobert Watson 	 * timers and tcp_discardcb().
45685d94372SRobert Watson 	 *
45785d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_persist: inp == NULL"));
45885d94372SRobert Watson 	 */
45985d94372SRobert Watson 	if (inp == NULL) {
46085d94372SRobert Watson 		tcp_timer_race++;
461603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
4628b615593SMarko Zec 		CURVNET_RESTORE();
46385d94372SRobert Watson 		return;
46485d94372SRobert Watson 	}
4658501a69cSRobert Watson 	INP_WLOCK(inp);
466655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_persist) ||
467655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_persist)) {
4688501a69cSRobert Watson 		INP_WUNLOCK(inp);
469603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
4708b615593SMarko Zec 		CURVNET_RESTORE();
47185d94372SRobert Watson 		return;
47285d94372SRobert Watson 	}
473e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_persist);
474655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
475655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
476655f934bSMikolaj Golub 		INP_INFO_WUNLOCK(&V_tcbinfo);
477655f934bSMikolaj Golub 		CURVNET_RESTORE();
478655f934bSMikolaj Golub 		return;
479655f934bSMikolaj Golub 	}
4809b8b58e0SJonathan Lemon 	/*
4819b8b58e0SJonathan Lemon 	 * Persistance timer into zero window.
4829b8b58e0SJonathan Lemon 	 * Force a byte to be output, if possible.
4839b8b58e0SJonathan Lemon 	 */
48478b50714SRobert Watson 	TCPSTAT_INC(tcps_persisttimeo);
4859b8b58e0SJonathan Lemon 	/*
4869b8b58e0SJonathan Lemon 	 * Hack: if the peer is dead/unreachable, we do not
4879b8b58e0SJonathan Lemon 	 * time out if the window is closed.  After a full
4889b8b58e0SJonathan Lemon 	 * backoff, drop the connection if the idle time
4899b8b58e0SJonathan Lemon 	 * (no responses to probes) reaches the maximum
4909b8b58e0SJonathan Lemon 	 * backoff that we would use if retransmitting.
4919b8b58e0SJonathan Lemon 	 */
4929b8b58e0SJonathan Lemon 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
4936b0c5521SJohn Baldwin 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
4946b0c5521SJohn Baldwin 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
49578b50714SRobert Watson 		TCPSTAT_INC(tcps_persistdrop);
49685d94372SRobert Watson 		tp = tcp_drop(tp, ETIMEDOUT);
49785d94372SRobert Watson 		goto out;
4989b8b58e0SJonathan Lemon 	}
499322181c9SAndre Oppermann 	/*
500322181c9SAndre Oppermann 	 * If the user has closed the socket then drop a persisting
501322181c9SAndre Oppermann 	 * connection after a much reduced timeout.
502322181c9SAndre Oppermann 	 */
503322181c9SAndre Oppermann 	if (tp->t_state > TCPS_CLOSE_WAIT &&
504322181c9SAndre Oppermann 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
505322181c9SAndre Oppermann 		TCPSTAT_INC(tcps_persistdrop);
506322181c9SAndre Oppermann 		tp = tcp_drop(tp, ETIMEDOUT);
507322181c9SAndre Oppermann 		goto out;
508322181c9SAndre Oppermann 	}
5099b8b58e0SJonathan Lemon 	tcp_setpersist(tp);
5102cdbfa66SPaul Saab 	tp->t_flags |= TF_FORCEDATA;
5119b8b58e0SJonathan Lemon 	(void) tcp_output(tp);
5122cdbfa66SPaul Saab 	tp->t_flags &= ~TF_FORCEDATA;
5139b8b58e0SJonathan Lemon 
51485d94372SRobert Watson out:
5159b8b58e0SJonathan Lemon #ifdef TCPDEBUG
516ffb761f6SGleb Smirnoff 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
517ffb761f6SGleb Smirnoff 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
5189b8b58e0SJonathan Lemon #endif
51985d94372SRobert Watson 	if (tp != NULL)
5208501a69cSRobert Watson 		INP_WUNLOCK(inp);
521603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
5228b615593SMarko Zec 	CURVNET_RESTORE();
5239b8b58e0SJonathan Lemon }
5249b8b58e0SJonathan Lemon 
52585d94372SRobert Watson void
52685d94372SRobert Watson tcp_timer_rexmt(void * xtp)
5279b8b58e0SJonathan Lemon {
52885d94372SRobert Watson 	struct tcpcb *tp = xtp;
5298b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
5309b8b58e0SJonathan Lemon 	int rexmt;
53185d94372SRobert Watson 	int headlocked;
53285d94372SRobert Watson 	struct inpcb *inp;
5339b8b58e0SJonathan Lemon #ifdef TCPDEBUG
5349b8b58e0SJonathan Lemon 	int ostate;
5359b8b58e0SJonathan Lemon 
5369b8b58e0SJonathan Lemon 	ostate = tp->t_state;
5379b8b58e0SJonathan Lemon #endif
53887aedea4SKip Macy 	INP_INFO_RLOCK(&V_tcbinfo);
53985d94372SRobert Watson 	inp = tp->t_inpcb;
54085d94372SRobert Watson 	/*
54185d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
54285d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
54385d94372SRobert Watson 	 * timers and tcp_discardcb().
54485d94372SRobert Watson 	 *
54585d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_rexmt: inp == NULL"));
54685d94372SRobert Watson 	 */
54785d94372SRobert Watson 	if (inp == NULL) {
54885d94372SRobert Watson 		tcp_timer_race++;
54987aedea4SKip Macy 		INP_INFO_RUNLOCK(&V_tcbinfo);
5508b615593SMarko Zec 		CURVNET_RESTORE();
55185d94372SRobert Watson 		return;
55285d94372SRobert Watson 	}
5538501a69cSRobert Watson 	INP_WLOCK(inp);
554655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
555655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_rexmt)) {
5568501a69cSRobert Watson 		INP_WUNLOCK(inp);
55787aedea4SKip Macy 		INP_INFO_RUNLOCK(&V_tcbinfo);
5588b615593SMarko Zec 		CURVNET_RESTORE();
55985d94372SRobert Watson 		return;
56085d94372SRobert Watson 	}
561e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_rexmt);
562655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
563655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
564655f934bSMikolaj Golub 		INP_INFO_RUNLOCK(&V_tcbinfo);
565655f934bSMikolaj Golub 		CURVNET_RESTORE();
566655f934bSMikolaj Golub 		return;
567655f934bSMikolaj Golub 	}
5686d90faf3SPaul Saab 	tcp_free_sackholes(tp);
569df8bae1dSRodney W. Grimes 	/*
570df8bae1dSRodney W. Grimes 	 * Retransmission timer went off.  Message has not
571df8bae1dSRodney W. Grimes 	 * been acked within retransmit interval.  Back off
572df8bae1dSRodney W. Grimes 	 * to a longer retransmit interval and retransmit one segment.
573df8bae1dSRodney W. Grimes 	 */
574df8bae1dSRodney W. Grimes 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
575df8bae1dSRodney W. Grimes 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
57678b50714SRobert Watson 		TCPSTAT_INC(tcps_timeoutdrop);
57787aedea4SKip Macy 		in_pcbref(inp);
57887aedea4SKip Macy 		INP_INFO_RUNLOCK(&V_tcbinfo);
57987aedea4SKip Macy 		INP_WUNLOCK(inp);
58087aedea4SKip Macy 		INP_INFO_WLOCK(&V_tcbinfo);
58187aedea4SKip Macy 		INP_WLOCK(inp);
582fa046d87SRobert Watson 		if (in_pcbrele_wlocked(inp)) {
58387aedea4SKip Macy 			INP_INFO_WUNLOCK(&V_tcbinfo);
58487aedea4SKip Macy 			CURVNET_RESTORE();
58587aedea4SKip Macy 			return;
58687aedea4SKip Macy 		}
587aa4b09c5SNavdeep Parhar 		if (inp->inp_flags & INP_DROPPED) {
588aa4b09c5SNavdeep Parhar 			INP_WUNLOCK(inp);
589aa4b09c5SNavdeep Parhar 			INP_INFO_WUNLOCK(&V_tcbinfo);
590aa4b09c5SNavdeep Parhar 			CURVNET_RESTORE();
591aa4b09c5SNavdeep Parhar 			return;
592aa4b09c5SNavdeep Parhar 		}
593aa4b09c5SNavdeep Parhar 
59485d94372SRobert Watson 		tp = tcp_drop(tp, tp->t_softerror ?
59585d94372SRobert Watson 			      tp->t_softerror : ETIMEDOUT);
59687aedea4SKip Macy 		headlocked = 1;
59785d94372SRobert Watson 		goto out;
5989b8b58e0SJonathan Lemon 	}
59987aedea4SKip Macy 	INP_INFO_RUNLOCK(&V_tcbinfo);
60085d94372SRobert Watson 	headlocked = 0;
601cf8f04f4SAndre Oppermann 	if (tp->t_state == TCPS_SYN_SENT) {
602cf8f04f4SAndre Oppermann 		/*
603cf8f04f4SAndre Oppermann 		 * If the SYN was retransmitted, indicate CWND to be
604cf8f04f4SAndre Oppermann 		 * limited to 1 segment in cc_conn_init().
605cf8f04f4SAndre Oppermann 		 */
606cf8f04f4SAndre Oppermann 		tp->snd_cwnd = 1;
607cf8f04f4SAndre Oppermann 	} else if (tp->t_rxtshift == 1) {
6089b8b58e0SJonathan Lemon 		/*
6099b8b58e0SJonathan Lemon 		 * first retransmit; record ssthresh and cwnd so they can
6109b8b58e0SJonathan Lemon 		 * be recovered if this turns out to be a "bad" retransmit.
6119b8b58e0SJonathan Lemon 		 * A retransmit is considered "bad" if an ACK for this
6129b8b58e0SJonathan Lemon 		 * segment is received within RTT/2 interval; the assumption
6139b8b58e0SJonathan Lemon 		 * here is that the ACK was already in flight.  See
6149b8b58e0SJonathan Lemon 		 * "On Estimating End-to-End Network Path Properties" by
6159b8b58e0SJonathan Lemon 		 * Allman and Paxson for more details.
6169b8b58e0SJonathan Lemon 		 */
6179b8b58e0SJonathan Lemon 		tp->snd_cwnd_prev = tp->snd_cwnd;
6189b8b58e0SJonathan Lemon 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
6199d11646dSJeffrey Hsu 		tp->snd_recover_prev = tp->snd_recover;
620dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags))
6219d11646dSJeffrey Hsu 			tp->t_flags |= TF_WASFRECOVERY;
6229d11646dSJeffrey Hsu 		else
6239d11646dSJeffrey Hsu 			tp->t_flags &= ~TF_WASFRECOVERY;
624dbc42409SLawrence Stewart 		if (IN_CONGRECOVERY(tp->t_flags))
625dbc42409SLawrence Stewart 			tp->t_flags |= TF_WASCRECOVERY;
626dbc42409SLawrence Stewart 		else
627dbc42409SLawrence Stewart 			tp->t_flags &= ~TF_WASCRECOVERY;
6289b8b58e0SJonathan Lemon 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
629672dc4aeSJohn Baldwin 		tp->t_flags |= TF_PREVVALID;
630672dc4aeSJohn Baldwin 	} else
631672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
63278b50714SRobert Watson 	TCPSTAT_INC(tcps_rexmttimeo);
6337d42e30cSJonathan Lemon 	if (tp->t_state == TCPS_SYN_SENT)
634f4748ef5SAndre Oppermann 		rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
6357d42e30cSJonathan Lemon 	else
636df8bae1dSRodney W. Grimes 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
637df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
638df8bae1dSRodney W. Grimes 		      tp->t_rttmin, TCPTV_REXMTMAX);
639df8bae1dSRodney W. Grimes 	/*
64077339e1cSAndre Oppermann 	 * Disable RFC1323 and SACK if we haven't got any response to
6417ceb7783SJesper Skriver 	 * our third SYN to work-around some broken terminal servers
6427ceb7783SJesper Skriver 	 * (most of which have hopefully been retired) that have bad VJ
6437ceb7783SJesper Skriver 	 * header compression code which trashes TCP segments containing
6447ceb7783SJesper Skriver 	 * unknown-to-them TCP options.
6457ceb7783SJesper Skriver 	 */
6466c0ef895SJohn Baldwin 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
6476c0ef895SJohn Baldwin 	    (tp->t_rxtshift == 3))
648c4ab59c1SAndre Oppermann 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
6497ceb7783SJesper Skriver 	/*
65097d8d152SAndre Oppermann 	 * If we backed off this far, our srtt estimate is probably bogus.
65197d8d152SAndre Oppermann 	 * Clobber it so we'll take the next rtt measurement as our srtt;
652df8bae1dSRodney W. Grimes 	 * move the current srtt into rttvar to keep the current
653df8bae1dSRodney W. Grimes 	 * retransmit times until then.
654df8bae1dSRodney W. Grimes 	 */
655df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
656fb59c426SYoshinobu Inoue #ifdef INET6
657fb59c426SYoshinobu Inoue 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
658fb59c426SYoshinobu Inoue 			in6_losing(tp->t_inpcb);
659fb59c426SYoshinobu Inoue #endif
660df8bae1dSRodney W. Grimes 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
661df8bae1dSRodney W. Grimes 		tp->t_srtt = 0;
662df8bae1dSRodney W. Grimes 	}
663df8bae1dSRodney W. Grimes 	tp->snd_nxt = tp->snd_una;
6649d11646dSJeffrey Hsu 	tp->snd_recover = tp->snd_max;
66546f58482SJonathan Lemon 	/*
66674b48c1dSAndras Olah 	 * Force a segment to be sent.
66774b48c1dSAndras Olah 	 */
66874b48c1dSAndras Olah 	tp->t_flags |= TF_ACKNOW;
66974b48c1dSAndras Olah 	/*
670df8bae1dSRodney W. Grimes 	 * If timing a segment in this window, stop the timer.
671df8bae1dSRodney W. Grimes 	 */
6729b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
673dbc42409SLawrence Stewart 
674b5af1b88SLawrence Stewart 	cc_cong_signal(tp, NULL, CC_RTO);
675dbc42409SLawrence Stewart 
676df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
677df8bae1dSRodney W. Grimes 
67885d94372SRobert Watson out:
6799b8b58e0SJonathan Lemon #ifdef TCPDEBUG
6801c53f806SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
681fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
6829b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
683df8bae1dSRodney W. Grimes #endif
68485d94372SRobert Watson 	if (tp != NULL)
6858501a69cSRobert Watson 		INP_WUNLOCK(inp);
68685d94372SRobert Watson 	if (headlocked)
687603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
6888b615593SMarko Zec 	CURVNET_RESTORE();
68985d94372SRobert Watson }
69085d94372SRobert Watson 
69185d94372SRobert Watson void
69285d94372SRobert Watson tcp_timer_activate(struct tcpcb *tp, int timer_type, u_int delta)
69385d94372SRobert Watson {
69485d94372SRobert Watson 	struct callout *t_callout;
69585d94372SRobert Watson 	void *f_callout;
69687aedea4SKip Macy 	struct inpcb *inp = tp->t_inpcb;
697*883831c6SAdrian Chadd 	int cpu = inp_to_cpuid(inp);
69885d94372SRobert Watson 
69909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
70009fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
70109fe6320SNavdeep Parhar 		return;
70209fe6320SNavdeep Parhar #endif
70309fe6320SNavdeep Parhar 
70485d94372SRobert Watson 	switch (timer_type) {
70585d94372SRobert Watson 		case TT_DELACK:
706e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
70785d94372SRobert Watson 			f_callout = tcp_timer_delack;
70885d94372SRobert Watson 			break;
70985d94372SRobert Watson 		case TT_REXMT:
710e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
71185d94372SRobert Watson 			f_callout = tcp_timer_rexmt;
71285d94372SRobert Watson 			break;
71385d94372SRobert Watson 		case TT_PERSIST:
714e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
71585d94372SRobert Watson 			f_callout = tcp_timer_persist;
71685d94372SRobert Watson 			break;
71785d94372SRobert Watson 		case TT_KEEP:
718e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
71985d94372SRobert Watson 			f_callout = tcp_timer_keep;
72085d94372SRobert Watson 			break;
72185d94372SRobert Watson 		case TT_2MSL:
722e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
72385d94372SRobert Watson 			f_callout = tcp_timer_2msl;
72485d94372SRobert Watson 			break;
72585d94372SRobert Watson 		default:
72685d94372SRobert Watson 			panic("bad timer_type");
72785d94372SRobert Watson 		}
72885d94372SRobert Watson 	if (delta == 0) {
72985d94372SRobert Watson 		callout_stop(t_callout);
73085d94372SRobert Watson 	} else {
73187aedea4SKip Macy 		callout_reset_on(t_callout, delta, f_callout, tp, cpu);
73285d94372SRobert Watson 	}
73385d94372SRobert Watson }
73485d94372SRobert Watson 
73585d94372SRobert Watson int
73685d94372SRobert Watson tcp_timer_active(struct tcpcb *tp, int timer_type)
73785d94372SRobert Watson {
73885d94372SRobert Watson 	struct callout *t_callout;
73985d94372SRobert Watson 
74085d94372SRobert Watson 	switch (timer_type) {
74185d94372SRobert Watson 		case TT_DELACK:
742e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
74385d94372SRobert Watson 			break;
74485d94372SRobert Watson 		case TT_REXMT:
745e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
74685d94372SRobert Watson 			break;
74785d94372SRobert Watson 		case TT_PERSIST:
748e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
74985d94372SRobert Watson 			break;
75085d94372SRobert Watson 		case TT_KEEP:
751e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
75285d94372SRobert Watson 			break;
75385d94372SRobert Watson 		case TT_2MSL:
754e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
75585d94372SRobert Watson 			break;
75685d94372SRobert Watson 		default:
75785d94372SRobert Watson 			panic("bad timer_type");
75885d94372SRobert Watson 		}
75985d94372SRobert Watson 	return callout_active(t_callout);
760df8bae1dSRodney W. Grimes }
761b8614722SMike Silbersack 
762b8614722SMike Silbersack #define	ticks_to_msecs(t)	(1000*(t) / hz)
763b8614722SMike Silbersack 
764b8614722SMike Silbersack void
7655b999a6bSDavide Italiano tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
7665b999a6bSDavide Italiano     struct xtcp_timer *xtimer)
767b8614722SMike Silbersack {
7685b999a6bSDavide Italiano 	sbintime_t now;
7695b999a6bSDavide Italiano 
7705b999a6bSDavide Italiano 	bzero(xtimer, sizeof(*xtimer));
771b8614722SMike Silbersack 	if (timer == NULL)
772b8614722SMike Silbersack 		return;
7735b999a6bSDavide Italiano 	now = getsbinuptime();
774b8614722SMike Silbersack 	if (callout_active(&timer->tt_delack))
7755b999a6bSDavide Italiano 		xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
776b8614722SMike Silbersack 	if (callout_active(&timer->tt_rexmt))
7775b999a6bSDavide Italiano 		xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
778b8614722SMike Silbersack 	if (callout_active(&timer->tt_persist))
7795b999a6bSDavide Italiano 		xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
780b8614722SMike Silbersack 	if (callout_active(&timer->tt_keep))
7815b999a6bSDavide Italiano 		xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
782b8614722SMike Silbersack 	if (callout_active(&timer->tt_2msl))
7835b999a6bSDavide Italiano 		xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
784b8614722SMike Silbersack 	xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);
785b8614722SMike Silbersack }
786