xref: /freebsd/sys/netinet/tcp_timer.c (revision 281a0fd4f9dd2b96396b634e282cd136b12724fc)
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"
38883831c6SAdrian 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>
54b2bdc62aSAdrian Chadd #include <net/rss_config.h>
55530c0060SRobert Watson #include <net/vnet.h>
56883831c6SAdrian Chadd #include <net/netisr.h>
57df8bae1dSRodney W. Grimes 
58dbc42409SLawrence Stewart #include <netinet/cc.h>
59df8bae1dSRodney W. Grimes #include <netinet/in.h>
605d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
62883831c6SAdrian Chadd #include <netinet/in_rss.h>
63c74af4faSBruce Evans #include <netinet/in_systm.h>
64fb59c426SYoshinobu Inoue #ifdef INET6
65fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
66fb59c426SYoshinobu Inoue #endif
67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
69df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
70df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
71f6f6703fSSean Bruno #ifdef INET6
72f6f6703fSSean Bruno #include <netinet6/tcp6_var.h>
73f6f6703fSSean Bruno #endif
74df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
75af7a2999SDavid Greenman #ifdef TCPDEBUG
76af7a2999SDavid Greenman #include <netinet/tcp_debug.h>
77af7a2999SDavid Greenman #endif
78df8bae1dSRodney W. Grimes 
799b8b58e0SJonathan Lemon int	tcp_keepinit;
80ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
8141698ebfSTom Rhodes     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
827b40aa32SPaul Traina 
839b8b58e0SJonathan Lemon int	tcp_keepidle;
84ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
8541698ebfSTom Rhodes     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
8698163b98SPoul-Henning Kamp 
879b8b58e0SJonathan Lemon int	tcp_keepintvl;
88ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
8941698ebfSTom Rhodes     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
9098163b98SPoul-Henning Kamp 
919b8b58e0SJonathan Lemon int	tcp_delacktime;
926489fe65SAndre Oppermann SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
936489fe65SAndre Oppermann     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
94ccb4d0c6SJonathan Lemon     "Time before a delayed ACK is sent");
959b8b58e0SJonathan Lemon 
969b8b58e0SJonathan Lemon int	tcp_msl;
97ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
98ccb4d0c6SJonathan Lemon     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
999b8b58e0SJonathan Lemon 
100701bec5aSMatthew Dillon int	tcp_rexmit_min;
101701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
1026489fe65SAndre Oppermann     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
1036489fe65SAndre Oppermann     "Minimum Retransmission Timeout");
104701bec5aSMatthew Dillon 
105701bec5aSMatthew Dillon int	tcp_rexmit_slop;
106701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
1076489fe65SAndre Oppermann     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
1086489fe65SAndre Oppermann     "Retransmission Timer Slop");
109701bec5aSMatthew Dillon 
110c39a614eSRobert Watson static int	always_keepalive = 1;
1113d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
1123d177f46SBill Fumerola     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
11334be9bf3SPoul-Henning Kamp 
1147c72af87SMohan Srinivasan int    tcp_fast_finwait2_recycle = 0;
1157c72af87SMohan Srinivasan SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
1166489fe65SAndre Oppermann     &tcp_fast_finwait2_recycle, 0,
1176489fe65SAndre Oppermann     "Recycle closed FIN_WAIT_2 connections faster");
1187c72af87SMohan Srinivasan 
1197c72af87SMohan Srinivasan int    tcp_finwait2_timeout;
1207c72af87SMohan Srinivasan SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
1216489fe65SAndre Oppermann     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
1227c72af87SMohan Srinivasan 
1239077f387SGleb Smirnoff int	tcp_keepcnt = TCPTV_KEEPCNT;
1249077f387SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
1259077f387SGleb Smirnoff     "Number of keepalive probes to send");
1267c72af87SMohan Srinivasan 
1270312fbe9SPoul-Henning Kamp 	/* max idle probes */
1289b8b58e0SJonathan Lemon int	tcp_maxpersistidle;
129e79adb8eSGarrett Wollman 
1306c0ef895SJohn Baldwin static int	tcp_rexmit_drop_options = 0;
1316c0ef895SJohn Baldwin SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
1326c0ef895SJohn Baldwin     &tcp_rexmit_drop_options, 0,
1336c0ef895SJohn Baldwin     "Drop TCP options from 3rd and later retransmitted SYN");
1346c0ef895SJohn Baldwin 
135f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
136f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_detect	VNET(tcp_pmtud_blackhole_detect)
137f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
138f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
139f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
140f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection Enabled");
141f6f6703fSSean Bruno 
142f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_activated);
143f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_activated \
144f6f6703fSSean Bruno     VNET(tcp_pmtud_blackhole_activated)
145f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated,
146f0188618SHans Petter Selasky     CTLFLAG_RD|CTLFLAG_VNET,
147f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
148f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection, Activation Count");
149f6f6703fSSean Bruno 
150f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_activated_min_mss);
151f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_activated_min_mss \
152f6f6703fSSean Bruno     VNET(tcp_pmtud_blackhole_activated_min_mss)
153f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated_min_mss,
154f0188618SHans Petter Selasky     CTLFLAG_RD|CTLFLAG_VNET,
155f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
156f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
157f6f6703fSSean Bruno 
158f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_failed);
159f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_failed	VNET(tcp_pmtud_blackhole_failed)
160f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_failed,
161f0188618SHans Petter Selasky     CTLFLAG_RD|CTLFLAG_VNET,
162f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
163f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection, Failure Count");
164f6f6703fSSean Bruno 
165f6f6703fSSean Bruno #ifdef INET
166f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
167f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_mss	VNET(tcp_pmtud_blackhole_mss)
168f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
169f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
170f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
171f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection lowered MSS");
172f6f6703fSSean Bruno #endif
173f6f6703fSSean Bruno 
174f6f6703fSSean Bruno #ifdef INET6
175f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
176f6f6703fSSean Bruno #define	V_tcp_v6pmtud_blackhole_mss	VNET(tcp_v6pmtud_blackhole_mss)
177f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
178f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
179f6f6703fSSean Bruno     &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
180f6f6703fSSean Bruno     "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
181f6f6703fSSean Bruno #endif
182f6f6703fSSean Bruno 
1838f7e75cbSAdrian Chadd #ifdef	RSS
1848f7e75cbSAdrian Chadd static int	per_cpu_timers = 1;
1858f7e75cbSAdrian Chadd #else
18687aedea4SKip Macy static int	per_cpu_timers = 0;
1878f7e75cbSAdrian Chadd #endif
18887aedea4SKip Macy SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
18987aedea4SKip Macy     &per_cpu_timers , 0, "run tcp timers on all cpus");
19087aedea4SKip Macy 
191883831c6SAdrian Chadd #if 0
19287aedea4SKip Macy #define	INP_CPU(inp)	(per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
19387aedea4SKip Macy 		((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
194883831c6SAdrian Chadd #endif
195883831c6SAdrian Chadd 
196883831c6SAdrian Chadd /*
197883831c6SAdrian Chadd  * Map the given inp to a CPU id.
198883831c6SAdrian Chadd  *
199883831c6SAdrian Chadd  * This queries RSS if it's compiled in, else it defaults to the current
200883831c6SAdrian Chadd  * CPU ID.
201883831c6SAdrian Chadd  */
202883831c6SAdrian Chadd static inline int
203883831c6SAdrian Chadd inp_to_cpuid(struct inpcb *inp)
204883831c6SAdrian Chadd {
205883831c6SAdrian Chadd 	u_int cpuid;
206883831c6SAdrian Chadd 
207883831c6SAdrian Chadd #ifdef	RSS
208883831c6SAdrian Chadd 	if (per_cpu_timers) {
209883831c6SAdrian Chadd 		cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
210883831c6SAdrian Chadd 		if (cpuid == NETISR_CPUID_NONE)
211883831c6SAdrian Chadd 			return (curcpu);	/* XXX */
212883831c6SAdrian Chadd 		else
213883831c6SAdrian Chadd 			return (cpuid);
214883831c6SAdrian Chadd 	}
215883831c6SAdrian Chadd #else
216883831c6SAdrian Chadd 	/* Legacy, pre-RSS behaviour */
217883831c6SAdrian Chadd 	if (per_cpu_timers) {
218883831c6SAdrian Chadd 		/*
219883831c6SAdrian Chadd 		 * We don't have a flowid -> cpuid mapping, so cheat and
220883831c6SAdrian Chadd 		 * just map unknown cpuids to curcpu.  Not the best, but
221883831c6SAdrian Chadd 		 * apparently better than defaulting to swi 0.
222883831c6SAdrian Chadd 		 */
223883831c6SAdrian Chadd 		cpuid = inp->inp_flowid % (mp_maxid + 1);
224883831c6SAdrian Chadd 		if (! CPU_ABSENT(cpuid))
225883831c6SAdrian Chadd 			return (cpuid);
226883831c6SAdrian Chadd 		return (curcpu);
227883831c6SAdrian Chadd 	}
228883831c6SAdrian Chadd #endif
229883831c6SAdrian Chadd 	/* Default for RSS and non-RSS - cpuid 0 */
230883831c6SAdrian Chadd 	else {
231883831c6SAdrian Chadd 		return (0);
232883831c6SAdrian Chadd 	}
233883831c6SAdrian Chadd }
23487aedea4SKip Macy 
235df8bae1dSRodney W. Grimes /*
236df8bae1dSRodney W. Grimes  * Tcp protocol timeout routine called every 500 ms.
2379b8b58e0SJonathan Lemon  * Updates timestamps used for TCP
238df8bae1dSRodney W. Grimes  * causes finite state machine actions if timers expire.
239df8bae1dSRodney W. Grimes  */
240df8bae1dSRodney W. Grimes void
241e2f2059fSMike Silbersack tcp_slowtimo(void)
242df8bae1dSRodney W. Grimes {
2438b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
24415bd2b43SDavid Greenman 
2455ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
2468b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
2478b615593SMarko Zec 		CURVNET_SET(vnet_iter);
248cea40c48SJulien Charbon 		(void) tcp_tw_2msl_scan(0);
2498b615593SMarko Zec 		CURVNET_RESTORE();
2508b615593SMarko Zec 	}
2515ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
252df8bae1dSRodney W. Grimes }
253df8bae1dSRodney W. Grimes 
2547d42e30cSJonathan Lemon int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
2557d42e30cSJonathan Lemon     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
2567d42e30cSJonathan Lemon 
257df8bae1dSRodney W. Grimes int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
258f058535dSJeffrey Hsu     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
259df8bae1dSRodney W. Grimes 
260f058535dSJeffrey Hsu static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
261e79adb8eSGarrett Wollman 
262df8bae1dSRodney W. Grimes /*
263df8bae1dSRodney W. Grimes  * TCP timer processing.
264df8bae1dSRodney W. Grimes  */
26585d94372SRobert Watson 
26685d94372SRobert Watson void
26785d94372SRobert Watson tcp_timer_delack(void *xtp)
268df8bae1dSRodney W. Grimes {
26985d94372SRobert Watson 	struct tcpcb *tp = xtp;
27085d94372SRobert Watson 	struct inpcb *inp;
2718b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
27285d94372SRobert Watson 
27385d94372SRobert Watson 	inp = tp->t_inpcb;
2745571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
2758501a69cSRobert Watson 	INP_WLOCK(inp);
276655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_delack) ||
277655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_delack)) {
2788501a69cSRobert Watson 		INP_WUNLOCK(inp);
2798b615593SMarko Zec 		CURVNET_RESTORE();
28085d94372SRobert Watson 		return;
28185d94372SRobert Watson 	}
282e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_delack);
283655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
284655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
285655f934bSMikolaj Golub 		CURVNET_RESTORE();
286655f934bSMikolaj Golub 		return;
287655f934bSMikolaj Golub 	}
2885571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
2895571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
2905571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_DELACK) != 0,
2915571f9cfSJulien Charbon 		("%s: tp %p delack callout should be running", __func__, tp));
292df8bae1dSRodney W. Grimes 
2939b8b58e0SJonathan Lemon 	tp->t_flags |= TF_ACKNOW;
29478b50714SRobert Watson 	TCPSTAT_INC(tcps_delack);
29555bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
2968501a69cSRobert Watson 	INP_WUNLOCK(inp);
2978b615593SMarko Zec 	CURVNET_RESTORE();
2989b8b58e0SJonathan Lemon }
2999b8b58e0SJonathan Lemon 
30085d94372SRobert Watson void
30185d94372SRobert Watson tcp_timer_2msl(void *xtp)
3029b8b58e0SJonathan Lemon {
30385d94372SRobert Watson 	struct tcpcb *tp = xtp;
30485d94372SRobert Watson 	struct inpcb *inp;
3058b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
3069b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3079b8b58e0SJonathan Lemon 	int ostate;
3089b8b58e0SJonathan Lemon 
3099b8b58e0SJonathan Lemon 	ostate = tp->t_state;
3109b8b58e0SJonathan Lemon #endif
311ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
31285d94372SRobert Watson 	inp = tp->t_inpcb;
3135571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
3148501a69cSRobert Watson 	INP_WLOCK(inp);
31585d94372SRobert Watson 	tcp_free_sackholes(tp);
316655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_2msl) ||
317e2f2059fSMike Silbersack 	    !callout_active(&tp->t_timers->tt_2msl)) {
3188501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
319ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3208b615593SMarko Zec 		CURVNET_RESTORE();
32185d94372SRobert Watson 		return;
32285d94372SRobert Watson 	}
323e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_2msl);
324655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
325655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
326ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
327655f934bSMikolaj Golub 		CURVNET_RESTORE();
328655f934bSMikolaj Golub 		return;
329655f934bSMikolaj Golub 	}
3305571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
3315571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
3325571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_2MSL) != 0,
3335571f9cfSJulien Charbon 		("%s: tp %p 2msl callout should be running", __func__, tp));
33485d94372SRobert Watson 	/*
335df8bae1dSRodney W. Grimes 	 * 2 MSL timeout in shutdown went off.  If we're closed but
336df8bae1dSRodney W. Grimes 	 * still waiting for peer to close and connection has been idle
33731a7749dSJulien Charbon 	 * too long delete connection control block.  Otherwise, check
33831a7749dSJulien Charbon 	 * again in a bit.
33931a7749dSJulien Charbon 	 *
34031a7749dSJulien Charbon 	 * If in TIME_WAIT state just ignore as this timeout is handled in
34131a7749dSJulien Charbon 	 * tcp_tw_2msl_scan().
3427c72af87SMohan Srinivasan 	 *
3437c72af87SMohan Srinivasan 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
3447c72af87SMohan Srinivasan 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
3457c72af87SMohan Srinivasan 	 * Ignore fact that there were recent incoming segments.
346df8bae1dSRodney W. Grimes 	 */
34731a7749dSJulien Charbon 	if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
34831a7749dSJulien Charbon 		INP_WUNLOCK(inp);
34931a7749dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
35031a7749dSJulien Charbon 		CURVNET_RESTORE();
35131a7749dSJulien Charbon 		return;
35231a7749dSJulien Charbon 	}
3537c72af87SMohan Srinivasan 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
35485d94372SRobert Watson 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
3557c72af87SMohan Srinivasan 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
35678b50714SRobert Watson 		TCPSTAT_INC(tcps_finwait2_drops);
35785d94372SRobert Watson 		tp = tcp_close(tp);
3587c72af87SMohan Srinivasan 	} else {
359d6de19acSJulien Charbon 		if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
360d6de19acSJulien Charbon 			if (!callout_reset(&tp->t_timers->tt_2msl,
361d6de19acSJulien Charbon 			   TP_KEEPINTVL(tp), tcp_timer_2msl, tp)) {
362d6de19acSJulien Charbon 				tp->t_timers->tt_flags &= ~TT_2MSL_RST;
363d6de19acSJulien Charbon 			}
364d6de19acSJulien Charbon 		} else
36585d94372SRobert Watson 		       tp = tcp_close(tp);
3667c72af87SMohan Srinivasan        }
367df8bae1dSRodney W. Grimes 
3689b8b58e0SJonathan Lemon #ifdef TCPDEBUG
369586b4a0eSKonstantin Belousov 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
370fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
3719b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
3729b8b58e0SJonathan Lemon #endif
3735d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
3745d06879aSGeorge V. Neville-Neil 
37585d94372SRobert Watson 	if (tp != NULL)
3768501a69cSRobert Watson 		INP_WUNLOCK(inp);
377ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
3788b615593SMarko Zec 	CURVNET_RESTORE();
3799b8b58e0SJonathan Lemon }
3809b8b58e0SJonathan Lemon 
38185d94372SRobert Watson void
38285d94372SRobert Watson tcp_timer_keep(void *xtp)
3839b8b58e0SJonathan Lemon {
38485d94372SRobert Watson 	struct tcpcb *tp = xtp;
38508517d53SMike Silbersack 	struct tcptemp *t_template;
38685d94372SRobert Watson 	struct inpcb *inp;
3878b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
3889b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3899b8b58e0SJonathan Lemon 	int ostate;
3909b8b58e0SJonathan Lemon 
3919b8b58e0SJonathan Lemon 	ostate = tp->t_state;
3929b8b58e0SJonathan Lemon #endif
393ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
39485d94372SRobert Watson 	inp = tp->t_inpcb;
3955571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
3968501a69cSRobert Watson 	INP_WLOCK(inp);
397655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_keep) ||
398655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_keep)) {
3998501a69cSRobert Watson 		INP_WUNLOCK(inp);
400ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
4018b615593SMarko Zec 		CURVNET_RESTORE();
40285d94372SRobert Watson 		return;
40385d94372SRobert Watson 	}
404e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_keep);
405655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
406655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
407ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
408655f934bSMikolaj Golub 		CURVNET_RESTORE();
409655f934bSMikolaj Golub 		return;
410655f934bSMikolaj Golub 	}
4115571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
4125571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
4135571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_KEEP) != 0,
4145571f9cfSJulien Charbon 		("%s: tp %p keep callout should be running", __func__, tp));
4159b8b58e0SJonathan Lemon 	/*
4169b8b58e0SJonathan Lemon 	 * Keep-alive timer went off; send something
4179b8b58e0SJonathan Lemon 	 * or drop connection if idle for too long.
4189b8b58e0SJonathan Lemon 	 */
41978b50714SRobert Watson 	TCPSTAT_INC(tcps_keeptimeo);
4209b8b58e0SJonathan Lemon 	if (tp->t_state < TCPS_ESTABLISHED)
4219b8b58e0SJonathan Lemon 		goto dropit;
4222a074620SSam Leffler 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
4239b8b58e0SJonathan Lemon 	    tp->t_state <= TCPS_CLOSING) {
4249077f387SGleb Smirnoff 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
4259b8b58e0SJonathan Lemon 			goto dropit;
4269b8b58e0SJonathan Lemon 		/*
4279b8b58e0SJonathan Lemon 		 * Send a packet designed to force a response
4289b8b58e0SJonathan Lemon 		 * if the peer is up and reachable:
4299b8b58e0SJonathan Lemon 		 * either an ACK if the connection is still alive,
4309b8b58e0SJonathan Lemon 		 * or an RST if the peer has closed the connection
4319b8b58e0SJonathan Lemon 		 * due to timeout or reboot.
4329b8b58e0SJonathan Lemon 		 * Using sequence number tp->snd_una-1
4339b8b58e0SJonathan Lemon 		 * causes the transmitted zero-length segment
4349b8b58e0SJonathan Lemon 		 * to lie outside the receive window;
4359b8b58e0SJonathan Lemon 		 * by the protocol spec, this requires the
4369b8b58e0SJonathan Lemon 		 * correspondent TCP to respond.
4379b8b58e0SJonathan Lemon 		 */
43878b50714SRobert Watson 		TCPSTAT_INC(tcps_keepprobe);
43979909384SJonathan Lemon 		t_template = tcpip_maketemplate(inp);
44008517d53SMike Silbersack 		if (t_template) {
44108517d53SMike Silbersack 			tcp_respond(tp, t_template->tt_ipgen,
44208517d53SMike Silbersack 				    &t_template->tt_t, (struct mbuf *)NULL,
4439b8b58e0SJonathan Lemon 				    tp->rcv_nxt, tp->snd_una - 1, 0);
44453640b0eSRobert Watson 			free(t_template, M_TEMP);
44508517d53SMike Silbersack 		}
446d6de19acSJulien Charbon 		if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
447d6de19acSJulien Charbon 		    tcp_timer_keep, tp)) {
448d6de19acSJulien Charbon 			tp->t_timers->tt_flags &= ~TT_KEEP_RST;
449d6de19acSJulien Charbon 		}
450d6de19acSJulien Charbon 	} else if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
451d6de19acSJulien Charbon 		    tcp_timer_keep, tp)) {
452d6de19acSJulien Charbon 			tp->t_timers->tt_flags &= ~TT_KEEP_RST;
453d6de19acSJulien Charbon 		}
4549b8b58e0SJonathan Lemon 
4559b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4562a074620SSam Leffler 	if (inp->inp_socket->so_options & SO_DEBUG)
457fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
4589b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
4599b8b58e0SJonathan Lemon #endif
4605d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
4618501a69cSRobert Watson 	INP_WUNLOCK(inp);
462ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
4638b615593SMarko Zec 	CURVNET_RESTORE();
46485d94372SRobert Watson 	return;
4659b8b58e0SJonathan Lemon 
4669b8b58e0SJonathan Lemon dropit:
46778b50714SRobert Watson 	TCPSTAT_INC(tcps_keepdrops);
46885d94372SRobert Watson 	tp = tcp_drop(tp, ETIMEDOUT);
46985d94372SRobert Watson 
47085d94372SRobert Watson #ifdef TCPDEBUG
47185d94372SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
47285d94372SRobert Watson 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
47385d94372SRobert Watson 			  PRU_SLOWTIMO);
47485d94372SRobert Watson #endif
4755d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
47685d94372SRobert Watson 	if (tp != NULL)
4778501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
478ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
4798b615593SMarko Zec 	CURVNET_RESTORE();
4809b8b58e0SJonathan Lemon }
4819b8b58e0SJonathan Lemon 
48285d94372SRobert Watson void
48385d94372SRobert Watson tcp_timer_persist(void *xtp)
4849b8b58e0SJonathan Lemon {
48585d94372SRobert Watson 	struct tcpcb *tp = xtp;
48685d94372SRobert Watson 	struct inpcb *inp;
4878b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
4889b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4899b8b58e0SJonathan Lemon 	int ostate;
4909b8b58e0SJonathan Lemon 
4919b8b58e0SJonathan Lemon 	ostate = tp->t_state;
4929b8b58e0SJonathan Lemon #endif
493ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
49485d94372SRobert Watson 	inp = tp->t_inpcb;
4955571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
4968501a69cSRobert Watson 	INP_WLOCK(inp);
497655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_persist) ||
498655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_persist)) {
4998501a69cSRobert Watson 		INP_WUNLOCK(inp);
500ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
5018b615593SMarko Zec 		CURVNET_RESTORE();
50285d94372SRobert Watson 		return;
50385d94372SRobert Watson 	}
504e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_persist);
505655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
506655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
507ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
508655f934bSMikolaj Golub 		CURVNET_RESTORE();
509655f934bSMikolaj Golub 		return;
510655f934bSMikolaj Golub 	}
5115571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
5125571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
5135571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_PERSIST) != 0,
5145571f9cfSJulien Charbon 		("%s: tp %p persist callout should be running", __func__, tp));
5159b8b58e0SJonathan Lemon 	/*
5169b8b58e0SJonathan Lemon 	 * Persistance timer into zero window.
5179b8b58e0SJonathan Lemon 	 * Force a byte to be output, if possible.
5189b8b58e0SJonathan Lemon 	 */
51978b50714SRobert Watson 	TCPSTAT_INC(tcps_persisttimeo);
5209b8b58e0SJonathan Lemon 	/*
5219b8b58e0SJonathan Lemon 	 * Hack: if the peer is dead/unreachable, we do not
5229b8b58e0SJonathan Lemon 	 * time out if the window is closed.  After a full
5239b8b58e0SJonathan Lemon 	 * backoff, drop the connection if the idle time
5249b8b58e0SJonathan Lemon 	 * (no responses to probes) reaches the maximum
5259b8b58e0SJonathan Lemon 	 * backoff that we would use if retransmitting.
5269b8b58e0SJonathan Lemon 	 */
5279b8b58e0SJonathan Lemon 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
5286b0c5521SJohn Baldwin 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
5296b0c5521SJohn Baldwin 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
53078b50714SRobert Watson 		TCPSTAT_INC(tcps_persistdrop);
53185d94372SRobert Watson 		tp = tcp_drop(tp, ETIMEDOUT);
53285d94372SRobert Watson 		goto out;
5339b8b58e0SJonathan Lemon 	}
534322181c9SAndre Oppermann 	/*
535322181c9SAndre Oppermann 	 * If the user has closed the socket then drop a persisting
536322181c9SAndre Oppermann 	 * connection after a much reduced timeout.
537322181c9SAndre Oppermann 	 */
538322181c9SAndre Oppermann 	if (tp->t_state > TCPS_CLOSE_WAIT &&
539322181c9SAndre Oppermann 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
540322181c9SAndre Oppermann 		TCPSTAT_INC(tcps_persistdrop);
541322181c9SAndre Oppermann 		tp = tcp_drop(tp, ETIMEDOUT);
542322181c9SAndre Oppermann 		goto out;
543322181c9SAndre Oppermann 	}
5449b8b58e0SJonathan Lemon 	tcp_setpersist(tp);
5452cdbfa66SPaul Saab 	tp->t_flags |= TF_FORCEDATA;
54655bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
5472cdbfa66SPaul Saab 	tp->t_flags &= ~TF_FORCEDATA;
5489b8b58e0SJonathan Lemon 
54985d94372SRobert Watson out:
5509b8b58e0SJonathan Lemon #ifdef TCPDEBUG
551ffb761f6SGleb Smirnoff 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
552ffb761f6SGleb Smirnoff 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
5539b8b58e0SJonathan Lemon #endif
5545d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
55585d94372SRobert Watson 	if (tp != NULL)
5568501a69cSRobert Watson 		INP_WUNLOCK(inp);
557ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
5588b615593SMarko Zec 	CURVNET_RESTORE();
5599b8b58e0SJonathan Lemon }
5609b8b58e0SJonathan Lemon 
56185d94372SRobert Watson void
56285d94372SRobert Watson tcp_timer_rexmt(void * xtp)
5639b8b58e0SJonathan Lemon {
56485d94372SRobert Watson 	struct tcpcb *tp = xtp;
5658b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
5669b8b58e0SJonathan Lemon 	int rexmt;
56785d94372SRobert Watson 	int headlocked;
56885d94372SRobert Watson 	struct inpcb *inp;
5699b8b58e0SJonathan Lemon #ifdef TCPDEBUG
5709b8b58e0SJonathan Lemon 	int ostate;
5719b8b58e0SJonathan Lemon 
5729b8b58e0SJonathan Lemon 	ostate = tp->t_state;
5739b8b58e0SJonathan Lemon #endif
574f6f6703fSSean Bruno 
57587aedea4SKip Macy 	INP_INFO_RLOCK(&V_tcbinfo);
57685d94372SRobert Watson 	inp = tp->t_inpcb;
5775571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
5788501a69cSRobert Watson 	INP_WLOCK(inp);
579655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
580655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_rexmt)) {
5818501a69cSRobert Watson 		INP_WUNLOCK(inp);
58287aedea4SKip Macy 		INP_INFO_RUNLOCK(&V_tcbinfo);
5838b615593SMarko Zec 		CURVNET_RESTORE();
58485d94372SRobert Watson 		return;
58585d94372SRobert Watson 	}
586e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_rexmt);
587655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
588655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
589655f934bSMikolaj Golub 		INP_INFO_RUNLOCK(&V_tcbinfo);
590655f934bSMikolaj Golub 		CURVNET_RESTORE();
591655f934bSMikolaj Golub 		return;
592655f934bSMikolaj Golub 	}
5935571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
5945571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
5955571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_REXMT) != 0,
5965571f9cfSJulien Charbon 		("%s: tp %p rexmt callout should be running", __func__, tp));
5976d90faf3SPaul Saab 	tcp_free_sackholes(tp);
598df8bae1dSRodney W. Grimes 	/*
599df8bae1dSRodney W. Grimes 	 * Retransmission timer went off.  Message has not
600df8bae1dSRodney W. Grimes 	 * been acked within retransmit interval.  Back off
601df8bae1dSRodney W. Grimes 	 * to a longer retransmit interval and retransmit one segment.
602df8bae1dSRodney W. Grimes 	 */
603df8bae1dSRodney W. Grimes 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
604df8bae1dSRodney W. Grimes 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
60578b50714SRobert Watson 		TCPSTAT_INC(tcps_timeoutdrop);
606aa4b09c5SNavdeep Parhar 
60785d94372SRobert Watson 		tp = tcp_drop(tp, tp->t_softerror ?
60885d94372SRobert Watson 			      tp->t_softerror : ETIMEDOUT);
60987aedea4SKip Macy 		headlocked = 1;
61085d94372SRobert Watson 		goto out;
6119b8b58e0SJonathan Lemon 	}
61287aedea4SKip Macy 	INP_INFO_RUNLOCK(&V_tcbinfo);
61385d94372SRobert Watson 	headlocked = 0;
614cf8f04f4SAndre Oppermann 	if (tp->t_state == TCPS_SYN_SENT) {
615cf8f04f4SAndre Oppermann 		/*
616cf8f04f4SAndre Oppermann 		 * If the SYN was retransmitted, indicate CWND to be
617cf8f04f4SAndre Oppermann 		 * limited to 1 segment in cc_conn_init().
618cf8f04f4SAndre Oppermann 		 */
619cf8f04f4SAndre Oppermann 		tp->snd_cwnd = 1;
620cf8f04f4SAndre Oppermann 	} else if (tp->t_rxtshift == 1) {
6219b8b58e0SJonathan Lemon 		/*
6229b8b58e0SJonathan Lemon 		 * first retransmit; record ssthresh and cwnd so they can
6239b8b58e0SJonathan Lemon 		 * be recovered if this turns out to be a "bad" retransmit.
6249b8b58e0SJonathan Lemon 		 * A retransmit is considered "bad" if an ACK for this
6259b8b58e0SJonathan Lemon 		 * segment is received within RTT/2 interval; the assumption
6269b8b58e0SJonathan Lemon 		 * here is that the ACK was already in flight.  See
6279b8b58e0SJonathan Lemon 		 * "On Estimating End-to-End Network Path Properties" by
6289b8b58e0SJonathan Lemon 		 * Allman and Paxson for more details.
6299b8b58e0SJonathan Lemon 		 */
6309b8b58e0SJonathan Lemon 		tp->snd_cwnd_prev = tp->snd_cwnd;
6319b8b58e0SJonathan Lemon 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
6329d11646dSJeffrey Hsu 		tp->snd_recover_prev = tp->snd_recover;
633dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags))
6349d11646dSJeffrey Hsu 			tp->t_flags |= TF_WASFRECOVERY;
6359d11646dSJeffrey Hsu 		else
6369d11646dSJeffrey Hsu 			tp->t_flags &= ~TF_WASFRECOVERY;
637dbc42409SLawrence Stewart 		if (IN_CONGRECOVERY(tp->t_flags))
638dbc42409SLawrence Stewart 			tp->t_flags |= TF_WASCRECOVERY;
639dbc42409SLawrence Stewart 		else
640dbc42409SLawrence Stewart 			tp->t_flags &= ~TF_WASCRECOVERY;
6419b8b58e0SJonathan Lemon 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
642672dc4aeSJohn Baldwin 		tp->t_flags |= TF_PREVVALID;
643672dc4aeSJohn Baldwin 	} else
644672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
64578b50714SRobert Watson 	TCPSTAT_INC(tcps_rexmttimeo);
646*281a0fd4SPatrick Kelsey 	if ((tp->t_state == TCPS_SYN_SENT) ||
647*281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
648f4748ef5SAndre Oppermann 		rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
6497d42e30cSJonathan Lemon 	else
650df8bae1dSRodney W. Grimes 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
651df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
652df8bae1dSRodney W. Grimes 		      tp->t_rttmin, TCPTV_REXMTMAX);
653f6f6703fSSean Bruno 
654882ac53eSSean Bruno 	/*
655882ac53eSSean Bruno 	 * We enter the path for PLMTUD if connection is established or, if
656882ac53eSSean Bruno 	 * connection is FIN_WAIT_1 status, reason for the last is that if
657882ac53eSSean Bruno 	 * amount of data we send is very small, we could send it in couple of
658882ac53eSSean Bruno 	 * packets and process straight to FIN. In that case we won't catch
659882ac53eSSean Bruno 	 * ESTABLISHED state.
660882ac53eSSean Bruno 	 */
661882ac53eSSean Bruno 	if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
662882ac53eSSean Bruno 	    || (tp->t_state == TCPS_FIN_WAIT_1))) {
663f6f6703fSSean Bruno 		int optlen;
664f6f6703fSSean Bruno #ifdef INET6
665f6f6703fSSean Bruno 		int isipv6;
666f6f6703fSSean Bruno #endif
667f6f6703fSSean Bruno 
668adf43a92SHiren Panchasara 		/*
669adf43a92SHiren Panchasara 		 * Idea here is that at each stage of mtu probe (usually, 1448
670adf43a92SHiren Panchasara 		 * -> 1188 -> 524) should be given 2 chances to recover before
671adf43a92SHiren Panchasara 		 *  further clamping down. 'tp->t_rxtshift % 2 == 0' should
672adf43a92SHiren Panchasara 		 *  take care of that.
673adf43a92SHiren Panchasara 		 */
674f6f6703fSSean Bruno 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
675f6f6703fSSean Bruno 		    (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
676adf43a92SHiren Panchasara 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift % 2 == 0)) {
677f6f6703fSSean Bruno 			/*
678f6f6703fSSean Bruno 			 * Enter Path MTU Black-hole Detection mechanism:
679f6f6703fSSean Bruno 			 * - Disable Path MTU Discovery (IP "DF" bit).
680f6f6703fSSean Bruno 			 * - Reduce MTU to lower value than what we
681f6f6703fSSean Bruno 			 *   negotiated with peer.
682f6f6703fSSean Bruno 			 */
683f6f6703fSSean Bruno 			/* Record that we may have found a black hole. */
684f6f6703fSSean Bruno 			tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
685f6f6703fSSean Bruno 
686f6f6703fSSean Bruno 			/* Keep track of previous MSS. */
687f6f6703fSSean Bruno 			optlen = tp->t_maxopd - tp->t_maxseg;
688f6f6703fSSean Bruno 			tp->t_pmtud_saved_maxopd = tp->t_maxopd;
689f6f6703fSSean Bruno 
690f6f6703fSSean Bruno 			/*
691f6f6703fSSean Bruno 			 * Reduce the MSS to blackhole value or to the default
692f6f6703fSSean Bruno 			 * in an attempt to retransmit.
693f6f6703fSSean Bruno 			 */
694f6f6703fSSean Bruno #ifdef INET6
695f6f6703fSSean Bruno 			isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
696f6f6703fSSean Bruno 			if (isipv6 &&
697f6f6703fSSean Bruno 			    tp->t_maxopd > V_tcp_v6pmtud_blackhole_mss) {
698f6f6703fSSean Bruno 				/* Use the sysctl tuneable blackhole MSS. */
699f6f6703fSSean Bruno 				tp->t_maxopd = V_tcp_v6pmtud_blackhole_mss;
700f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated++;
701f6f6703fSSean Bruno 			} else if (isipv6) {
702f6f6703fSSean Bruno 				/* Use the default MSS. */
703f6f6703fSSean Bruno 				tp->t_maxopd = V_tcp_v6mssdflt;
704f6f6703fSSean Bruno 				/*
705f6f6703fSSean Bruno 				 * Disable Path MTU Discovery when we switch to
706f6f6703fSSean Bruno 				 * minmss.
707f6f6703fSSean Bruno 				 */
708f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
709f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated_min_mss++;
710f6f6703fSSean Bruno 			}
711f6f6703fSSean Bruno #endif
712f6f6703fSSean Bruno #if defined(INET6) && defined(INET)
713f6f6703fSSean Bruno 			else
714f6f6703fSSean Bruno #endif
715f6f6703fSSean Bruno #ifdef INET
716f6f6703fSSean Bruno 			if (tp->t_maxopd > V_tcp_pmtud_blackhole_mss) {
717f6f6703fSSean Bruno 				/* Use the sysctl tuneable blackhole MSS. */
718f6f6703fSSean Bruno 				tp->t_maxopd = V_tcp_pmtud_blackhole_mss;
719f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated++;
720f6f6703fSSean Bruno 			} else {
721f6f6703fSSean Bruno 				/* Use the default MSS. */
722f6f6703fSSean Bruno 				tp->t_maxopd = V_tcp_mssdflt;
723f6f6703fSSean Bruno 				/*
724f6f6703fSSean Bruno 				 * Disable Path MTU Discovery when we switch to
725f6f6703fSSean Bruno 				 * minmss.
726f6f6703fSSean Bruno 				 */
727f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
728f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated_min_mss++;
729f6f6703fSSean Bruno 			}
730f6f6703fSSean Bruno #endif
731f6f6703fSSean Bruno 			tp->t_maxseg = tp->t_maxopd - optlen;
732f6f6703fSSean Bruno 			/*
733f6f6703fSSean Bruno 			 * Reset the slow-start flight size
734f6f6703fSSean Bruno 			 * as it may depend on the new MSS.
735f6f6703fSSean Bruno 			 */
736f6f6703fSSean Bruno 			if (CC_ALGO(tp)->conn_init != NULL)
737f6f6703fSSean Bruno 				CC_ALGO(tp)->conn_init(tp->ccv);
738f6f6703fSSean Bruno 		} else {
739f6f6703fSSean Bruno 			/*
740f6f6703fSSean Bruno 			 * If further retransmissions are still unsuccessful
741f6f6703fSSean Bruno 			 * with a lowered MTU, maybe this isn't a blackhole and
742f6f6703fSSean Bruno 			 * we restore the previous MSS and blackhole detection
743f6f6703fSSean Bruno 			 * flags.
744adf43a92SHiren Panchasara 			 * The limit '6' is determined by giving each probe
745adf43a92SHiren Panchasara 			 * stage (1448, 1188, 524) 2 chances to recover.
746f6f6703fSSean Bruno 			 */
747f6f6703fSSean Bruno 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
748adf43a92SHiren Panchasara 			    (tp->t_rxtshift > 6)) {
749f6f6703fSSean Bruno 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
750f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
751f6f6703fSSean Bruno 				optlen = tp->t_maxopd - tp->t_maxseg;
752f6f6703fSSean Bruno 				tp->t_maxopd = tp->t_pmtud_saved_maxopd;
753f6f6703fSSean Bruno 				tp->t_maxseg = tp->t_maxopd - optlen;
754f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_failed++;
755f6f6703fSSean Bruno 				/*
756f6f6703fSSean Bruno 				 * Reset the slow-start flight size as it
757f6f6703fSSean Bruno 				 * may depend on the new MSS.
758f6f6703fSSean Bruno 				 */
759f6f6703fSSean Bruno 				if (CC_ALGO(tp)->conn_init != NULL)
760f6f6703fSSean Bruno 					CC_ALGO(tp)->conn_init(tp->ccv);
761f6f6703fSSean Bruno 			}
762f6f6703fSSean Bruno 		}
763f6f6703fSSean Bruno 	}
764f6f6703fSSean Bruno 
765df8bae1dSRodney W. Grimes 	/*
76677339e1cSAndre Oppermann 	 * Disable RFC1323 and SACK if we haven't got any response to
7677ceb7783SJesper Skriver 	 * our third SYN to work-around some broken terminal servers
7687ceb7783SJesper Skriver 	 * (most of which have hopefully been retired) that have bad VJ
7697ceb7783SJesper Skriver 	 * header compression code which trashes TCP segments containing
7707ceb7783SJesper Skriver 	 * unknown-to-them TCP options.
7717ceb7783SJesper Skriver 	 */
7726c0ef895SJohn Baldwin 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
7736c0ef895SJohn Baldwin 	    (tp->t_rxtshift == 3))
774c4ab59c1SAndre Oppermann 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
7757ceb7783SJesper Skriver 	/*
77697d8d152SAndre Oppermann 	 * If we backed off this far, our srtt estimate is probably bogus.
77797d8d152SAndre Oppermann 	 * Clobber it so we'll take the next rtt measurement as our srtt;
778df8bae1dSRodney W. Grimes 	 * move the current srtt into rttvar to keep the current
779df8bae1dSRodney W. Grimes 	 * retransmit times until then.
780df8bae1dSRodney W. Grimes 	 */
781df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
782fb59c426SYoshinobu Inoue #ifdef INET6
783fb59c426SYoshinobu Inoue 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
784fb59c426SYoshinobu Inoue 			in6_losing(tp->t_inpcb);
785fb59c426SYoshinobu Inoue #endif
786df8bae1dSRodney W. Grimes 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
787df8bae1dSRodney W. Grimes 		tp->t_srtt = 0;
788df8bae1dSRodney W. Grimes 	}
789df8bae1dSRodney W. Grimes 	tp->snd_nxt = tp->snd_una;
7909d11646dSJeffrey Hsu 	tp->snd_recover = tp->snd_max;
79146f58482SJonathan Lemon 	/*
79274b48c1dSAndras Olah 	 * Force a segment to be sent.
79374b48c1dSAndras Olah 	 */
79474b48c1dSAndras Olah 	tp->t_flags |= TF_ACKNOW;
79574b48c1dSAndras Olah 	/*
796df8bae1dSRodney W. Grimes 	 * If timing a segment in this window, stop the timer.
797df8bae1dSRodney W. Grimes 	 */
7989b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
799dbc42409SLawrence Stewart 
800b5af1b88SLawrence Stewart 	cc_cong_signal(tp, NULL, CC_RTO);
801dbc42409SLawrence Stewart 
80255bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
803df8bae1dSRodney W. Grimes 
80485d94372SRobert Watson out:
8059b8b58e0SJonathan Lemon #ifdef TCPDEBUG
8061c53f806SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
807fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
8089b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
809df8bae1dSRodney W. Grimes #endif
8105d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
81185d94372SRobert Watson 	if (tp != NULL)
8128501a69cSRobert Watson 		INP_WUNLOCK(inp);
81385d94372SRobert Watson 	if (headlocked)
814ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
8158b615593SMarko Zec 	CURVNET_RESTORE();
81685d94372SRobert Watson }
81785d94372SRobert Watson 
81885d94372SRobert Watson void
8195571f9cfSJulien Charbon tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
82085d94372SRobert Watson {
82185d94372SRobert Watson 	struct callout *t_callout;
82218832f1fSJulien Charbon 	timeout_t *f_callout;
82387aedea4SKip Macy 	struct inpcb *inp = tp->t_inpcb;
824883831c6SAdrian Chadd 	int cpu = inp_to_cpuid(inp);
825d6de19acSJulien Charbon 	uint32_t f_reset;
82685d94372SRobert Watson 
82709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
82809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
82909fe6320SNavdeep Parhar 		return;
83009fe6320SNavdeep Parhar #endif
83109fe6320SNavdeep Parhar 
8325571f9cfSJulien Charbon 	if (tp->t_timers->tt_flags & TT_STOPPED)
8335571f9cfSJulien Charbon 		return;
8345571f9cfSJulien Charbon 
83585d94372SRobert Watson 	switch (timer_type) {
83685d94372SRobert Watson 		case TT_DELACK:
837e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
83885d94372SRobert Watson 			f_callout = tcp_timer_delack;
839d6de19acSJulien Charbon 			f_reset = TT_DELACK_RST;
84085d94372SRobert Watson 			break;
84185d94372SRobert Watson 		case TT_REXMT:
842e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
84385d94372SRobert Watson 			f_callout = tcp_timer_rexmt;
844d6de19acSJulien Charbon 			f_reset = TT_REXMT_RST;
84585d94372SRobert Watson 			break;
84685d94372SRobert Watson 		case TT_PERSIST:
847e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
84885d94372SRobert Watson 			f_callout = tcp_timer_persist;
849d6de19acSJulien Charbon 			f_reset = TT_PERSIST_RST;
85085d94372SRobert Watson 			break;
85185d94372SRobert Watson 		case TT_KEEP:
852e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
85385d94372SRobert Watson 			f_callout = tcp_timer_keep;
854d6de19acSJulien Charbon 			f_reset = TT_KEEP_RST;
85585d94372SRobert Watson 			break;
85685d94372SRobert Watson 		case TT_2MSL:
857e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
85885d94372SRobert Watson 			f_callout = tcp_timer_2msl;
859d6de19acSJulien Charbon 			f_reset = TT_2MSL_RST;
86085d94372SRobert Watson 			break;
86185d94372SRobert Watson 		default:
86255bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_activate) {
86355bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
86455bceb1eSRandall Stewart 				return;
86555bceb1eSRandall Stewart 			}
86603374917SJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
86785d94372SRobert Watson 		}
86885d94372SRobert Watson 	if (delta == 0) {
8695571f9cfSJulien Charbon 		if ((tp->t_timers->tt_flags & timer_type) &&
8707c4676ddSRandall Stewart 		    (callout_stop(t_callout) > 0) &&
871d6de19acSJulien Charbon 		    (tp->t_timers->tt_flags & f_reset)) {
872d6de19acSJulien Charbon 			tp->t_timers->tt_flags &= ~(timer_type | f_reset);
8735571f9cfSJulien Charbon 		}
87485d94372SRobert Watson 	} else {
8755571f9cfSJulien Charbon 		if ((tp->t_timers->tt_flags & timer_type) == 0) {
876d6de19acSJulien Charbon 			tp->t_timers->tt_flags |= (timer_type | f_reset);
87787aedea4SKip Macy 			callout_reset_on(t_callout, delta, f_callout, tp, cpu);
8785571f9cfSJulien Charbon 		} else {
8795571f9cfSJulien Charbon 			/* Reset already running callout on the same CPU. */
880d6de19acSJulien Charbon 			if (!callout_reset(t_callout, delta, f_callout, tp)) {
881d6de19acSJulien Charbon 				/*
882d6de19acSJulien Charbon 				 * Callout not cancelled, consider it as not
883d6de19acSJulien Charbon 				 * properly restarted. */
884d6de19acSJulien Charbon 				tp->t_timers->tt_flags &= ~f_reset;
885d6de19acSJulien Charbon 			}
8865571f9cfSJulien Charbon 		}
88785d94372SRobert Watson 	}
88885d94372SRobert Watson }
88985d94372SRobert Watson 
89085d94372SRobert Watson int
8915571f9cfSJulien Charbon tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
89285d94372SRobert Watson {
89385d94372SRobert Watson 	struct callout *t_callout;
89485d94372SRobert Watson 
89585d94372SRobert Watson 	switch (timer_type) {
89685d94372SRobert Watson 		case TT_DELACK:
897e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
89885d94372SRobert Watson 			break;
89985d94372SRobert Watson 		case TT_REXMT:
900e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
90185d94372SRobert Watson 			break;
90285d94372SRobert Watson 		case TT_PERSIST:
903e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
90485d94372SRobert Watson 			break;
90585d94372SRobert Watson 		case TT_KEEP:
906e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
90785d94372SRobert Watson 			break;
90885d94372SRobert Watson 		case TT_2MSL:
909e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
91085d94372SRobert Watson 			break;
91185d94372SRobert Watson 		default:
91255bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_active) {
91355bceb1eSRandall Stewart 				return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
91455bceb1eSRandall Stewart 			}
91503374917SJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
91685d94372SRobert Watson 		}
91785d94372SRobert Watson 	return callout_active(t_callout);
918df8bae1dSRodney W. Grimes }
919b8614722SMike Silbersack 
9205571f9cfSJulien Charbon void
9215571f9cfSJulien Charbon tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
9225571f9cfSJulien Charbon {
9235571f9cfSJulien Charbon 	struct callout *t_callout;
9245571f9cfSJulien Charbon 	timeout_t *f_callout;
925d6de19acSJulien Charbon 	uint32_t f_reset;
9265571f9cfSJulien Charbon 
9275571f9cfSJulien Charbon 	tp->t_timers->tt_flags |= TT_STOPPED;
9285571f9cfSJulien Charbon 
9295571f9cfSJulien Charbon 	switch (timer_type) {
9305571f9cfSJulien Charbon 		case TT_DELACK:
9315571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_delack;
9325571f9cfSJulien Charbon 			f_callout = tcp_timer_delack_discard;
933d6de19acSJulien Charbon 			f_reset = TT_DELACK_RST;
9345571f9cfSJulien Charbon 			break;
9355571f9cfSJulien Charbon 		case TT_REXMT:
9365571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_rexmt;
9375571f9cfSJulien Charbon 			f_callout = tcp_timer_rexmt_discard;
938d6de19acSJulien Charbon 			f_reset = TT_REXMT_RST;
9395571f9cfSJulien Charbon 			break;
9405571f9cfSJulien Charbon 		case TT_PERSIST:
9415571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_persist;
9425571f9cfSJulien Charbon 			f_callout = tcp_timer_persist_discard;
943d6de19acSJulien Charbon 			f_reset = TT_PERSIST_RST;
9445571f9cfSJulien Charbon 			break;
9455571f9cfSJulien Charbon 		case TT_KEEP:
9465571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_keep;
9475571f9cfSJulien Charbon 			f_callout = tcp_timer_keep_discard;
948d6de19acSJulien Charbon 			f_reset = TT_KEEP_RST;
9495571f9cfSJulien Charbon 			break;
9505571f9cfSJulien Charbon 		case TT_2MSL:
9515571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_2msl;
9525571f9cfSJulien Charbon 			f_callout = tcp_timer_2msl_discard;
953d6de19acSJulien Charbon 			f_reset = TT_2MSL_RST;
9545571f9cfSJulien Charbon 			break;
9555571f9cfSJulien Charbon 		default:
95655bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_stop) {
95755bceb1eSRandall Stewart 				/*
95855bceb1eSRandall Stewart 				 * XXXrrs we need to look at this with the
95955bceb1eSRandall Stewart 				 * stop case below (flags).
96055bceb1eSRandall Stewart 				 */
96155bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
96255bceb1eSRandall Stewart 				return;
96355bceb1eSRandall Stewart 			}
9645571f9cfSJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
9655571f9cfSJulien Charbon 		}
9665571f9cfSJulien Charbon 
9675571f9cfSJulien Charbon 	if (tp->t_timers->tt_flags & timer_type) {
9687c4676ddSRandall Stewart 		if ((callout_stop(t_callout) > 0) &&
969d6de19acSJulien Charbon 		    (tp->t_timers->tt_flags & f_reset)) {
970d6de19acSJulien Charbon 			tp->t_timers->tt_flags &= ~(timer_type | f_reset);
9715571f9cfSJulien Charbon 		} else {
9725571f9cfSJulien Charbon 			/*
9735571f9cfSJulien Charbon 			 * Can't stop the callout, defer tcpcb actual deletion
9745571f9cfSJulien Charbon 			 * to the last tcp timer discard callout.
9755571f9cfSJulien Charbon 			 * The TT_STOPPED flag will ensure that no tcp timer
9765571f9cfSJulien Charbon 			 * callouts can be restarted on our behalf, and
9775571f9cfSJulien Charbon 			 * past this point currently running callouts waiting
9785571f9cfSJulien Charbon 			 * on inp lock will return right away after the
9795571f9cfSJulien Charbon 			 * classical check for callout reset/stop events:
9805571f9cfSJulien Charbon 			 * callout_pending() || !callout_active()
9815571f9cfSJulien Charbon 			 */
9825571f9cfSJulien Charbon 			callout_reset(t_callout, 1, f_callout, tp);
9835571f9cfSJulien Charbon 		}
9845571f9cfSJulien Charbon 	}
9855571f9cfSJulien Charbon }
9865571f9cfSJulien Charbon 
987b8614722SMike Silbersack #define	ticks_to_msecs(t)	(1000*(t) / hz)
988b8614722SMike Silbersack 
989b8614722SMike Silbersack void
9905b999a6bSDavide Italiano tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
9915b999a6bSDavide Italiano     struct xtcp_timer *xtimer)
992b8614722SMike Silbersack {
9935b999a6bSDavide Italiano 	sbintime_t now;
9945b999a6bSDavide Italiano 
9955b999a6bSDavide Italiano 	bzero(xtimer, sizeof(*xtimer));
996b8614722SMike Silbersack 	if (timer == NULL)
997b8614722SMike Silbersack 		return;
9985b999a6bSDavide Italiano 	now = getsbinuptime();
999b8614722SMike Silbersack 	if (callout_active(&timer->tt_delack))
10005b999a6bSDavide Italiano 		xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
1001b8614722SMike Silbersack 	if (callout_active(&timer->tt_rexmt))
10025b999a6bSDavide Italiano 		xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
1003b8614722SMike Silbersack 	if (callout_active(&timer->tt_persist))
10045b999a6bSDavide Italiano 		xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
1005b8614722SMike Silbersack 	if (callout_active(&timer->tt_keep))
10065b999a6bSDavide Italiano 		xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
1007b8614722SMike Silbersack 	if (callout_active(&timer->tt_2msl))
10085b999a6bSDavide Italiano 		xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
1009b8614722SMike Silbersack 	xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);
1010b8614722SMike Silbersack }
1011