xref: /freebsd/sys/netinet/tcp_timer.c (revision 6d172f58a23a478ce4ffe5a14b08e0a884545dbb)
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 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
595d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
61883831c6SAdrian Chadd #include <netinet/in_rss.h>
62c74af4faSBruce Evans #include <netinet/in_systm.h>
63fb59c426SYoshinobu Inoue #ifdef INET6
64fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
65fb59c426SYoshinobu Inoue #endif
66df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
672de3e790SGleb Smirnoff #include <netinet/tcp.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>
714644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
72f6f6703fSSean Bruno #ifdef INET6
73f6f6703fSSean Bruno #include <netinet6/tcp6_var.h>
74f6f6703fSSean Bruno #endif
75df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
76af7a2999SDavid Greenman #ifdef TCPDEBUG
77af7a2999SDavid Greenman #include <netinet/tcp_debug.h>
78af7a2999SDavid Greenman #endif
79df8bae1dSRodney W. Grimes 
800645c604SHiren Panchasara int    tcp_persmin;
810645c604SHiren Panchasara SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmin, CTLTYPE_INT|CTLFLAG_RW,
820645c604SHiren Panchasara     &tcp_persmin, 0, sysctl_msec_to_ticks, "I", "minimum persistence interval");
830645c604SHiren Panchasara 
840645c604SHiren Panchasara int    tcp_persmax;
850645c604SHiren Panchasara SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax, CTLTYPE_INT|CTLFLAG_RW,
860645c604SHiren Panchasara     &tcp_persmax, 0, sysctl_msec_to_ticks, "I", "maximum persistence interval");
870645c604SHiren Panchasara 
889b8b58e0SJonathan Lemon int	tcp_keepinit;
89ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
9041698ebfSTom Rhodes     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
917b40aa32SPaul Traina 
929b8b58e0SJonathan Lemon int	tcp_keepidle;
93ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
9441698ebfSTom Rhodes     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
9598163b98SPoul-Henning Kamp 
969b8b58e0SJonathan Lemon int	tcp_keepintvl;
97ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
9841698ebfSTom Rhodes     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
9998163b98SPoul-Henning Kamp 
1009b8b58e0SJonathan Lemon int	tcp_delacktime;
1016489fe65SAndre Oppermann SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
1026489fe65SAndre Oppermann     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
103ccb4d0c6SJonathan Lemon     "Time before a delayed ACK is sent");
1049b8b58e0SJonathan Lemon 
1059b8b58e0SJonathan Lemon int	tcp_msl;
106ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
107ccb4d0c6SJonathan Lemon     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
1089b8b58e0SJonathan Lemon 
109701bec5aSMatthew Dillon int	tcp_rexmit_min;
110701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
1116489fe65SAndre Oppermann     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
1126489fe65SAndre Oppermann     "Minimum Retransmission Timeout");
113701bec5aSMatthew Dillon 
114701bec5aSMatthew Dillon int	tcp_rexmit_slop;
115701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
1166489fe65SAndre Oppermann     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
1176489fe65SAndre Oppermann     "Retransmission Timer Slop");
118701bec5aSMatthew Dillon 
119c39a614eSRobert Watson static int	always_keepalive = 1;
1203d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
1213d177f46SBill Fumerola     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
12234be9bf3SPoul-Henning Kamp 
1237c72af87SMohan Srinivasan int    tcp_fast_finwait2_recycle = 0;
1247c72af87SMohan Srinivasan SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
1256489fe65SAndre Oppermann     &tcp_fast_finwait2_recycle, 0,
1266489fe65SAndre Oppermann     "Recycle closed FIN_WAIT_2 connections faster");
1277c72af87SMohan Srinivasan 
1287c72af87SMohan Srinivasan int    tcp_finwait2_timeout;
1297c72af87SMohan Srinivasan SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
1306489fe65SAndre Oppermann     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
1317c72af87SMohan Srinivasan 
1329077f387SGleb Smirnoff int	tcp_keepcnt = TCPTV_KEEPCNT;
1339077f387SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
1349077f387SGleb Smirnoff     "Number of keepalive probes to send");
1357c72af87SMohan Srinivasan 
1360312fbe9SPoul-Henning Kamp 	/* max idle probes */
1379b8b58e0SJonathan Lemon int	tcp_maxpersistidle;
138e79adb8eSGarrett Wollman 
1396c0ef895SJohn Baldwin static int	tcp_rexmit_drop_options = 0;
1406c0ef895SJohn Baldwin SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
1416c0ef895SJohn Baldwin     &tcp_rexmit_drop_options, 0,
1426c0ef895SJohn Baldwin     "Drop TCP options from 3rd and later retransmitted SYN");
1436c0ef895SJohn Baldwin 
144f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
145f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_detect	VNET(tcp_pmtud_blackhole_detect)
146f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
147f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
148f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
149f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection Enabled");
150f6f6703fSSean Bruno 
151f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_activated);
152f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_activated \
153f6f6703fSSean Bruno     VNET(tcp_pmtud_blackhole_activated)
154f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated,
155f0188618SHans Petter Selasky     CTLFLAG_RD|CTLFLAG_VNET,
156f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
157f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection, Activation Count");
158f6f6703fSSean Bruno 
159f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_activated_min_mss);
160f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_activated_min_mss \
161f6f6703fSSean Bruno     VNET(tcp_pmtud_blackhole_activated_min_mss)
162f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated_min_mss,
163f0188618SHans Petter Selasky     CTLFLAG_RD|CTLFLAG_VNET,
164f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
165f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
166f6f6703fSSean Bruno 
167f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_failed);
168f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_failed	VNET(tcp_pmtud_blackhole_failed)
169f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_failed,
170f0188618SHans Petter Selasky     CTLFLAG_RD|CTLFLAG_VNET,
171f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
172f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection, Failure Count");
173f6f6703fSSean Bruno 
174f6f6703fSSean Bruno #ifdef INET
175f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
176f6f6703fSSean Bruno #define	V_tcp_pmtud_blackhole_mss	VNET(tcp_pmtud_blackhole_mss)
177f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
178f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
179f6f6703fSSean Bruno     &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
180f6f6703fSSean Bruno     "Path MTU Discovery Black Hole Detection lowered MSS");
181f6f6703fSSean Bruno #endif
182f6f6703fSSean Bruno 
183f6f6703fSSean Bruno #ifdef INET6
184f6f6703fSSean Bruno static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
185f6f6703fSSean Bruno #define	V_tcp_v6pmtud_blackhole_mss	VNET(tcp_v6pmtud_blackhole_mss)
186f6f6703fSSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
187f0188618SHans Petter Selasky     CTLFLAG_RW|CTLFLAG_VNET,
188f6f6703fSSean Bruno     &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
189f6f6703fSSean Bruno     "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
190f6f6703fSSean Bruno #endif
191f6f6703fSSean Bruno 
1928f7e75cbSAdrian Chadd #ifdef	RSS
1938f7e75cbSAdrian Chadd static int	per_cpu_timers = 1;
1948f7e75cbSAdrian Chadd #else
19587aedea4SKip Macy static int	per_cpu_timers = 0;
1968f7e75cbSAdrian Chadd #endif
19787aedea4SKip Macy SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
19887aedea4SKip Macy     &per_cpu_timers , 0, "run tcp timers on all cpus");
19987aedea4SKip Macy 
200883831c6SAdrian Chadd #if 0
20187aedea4SKip Macy #define	INP_CPU(inp)	(per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
20287aedea4SKip Macy 		((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
203883831c6SAdrian Chadd #endif
204883831c6SAdrian Chadd 
205883831c6SAdrian Chadd /*
206883831c6SAdrian Chadd  * Map the given inp to a CPU id.
207883831c6SAdrian Chadd  *
208883831c6SAdrian Chadd  * This queries RSS if it's compiled in, else it defaults to the current
209883831c6SAdrian Chadd  * CPU ID.
210883831c6SAdrian Chadd  */
211883831c6SAdrian Chadd static inline int
212883831c6SAdrian Chadd inp_to_cpuid(struct inpcb *inp)
213883831c6SAdrian Chadd {
214883831c6SAdrian Chadd 	u_int cpuid;
215883831c6SAdrian Chadd 
216883831c6SAdrian Chadd #ifdef	RSS
217883831c6SAdrian Chadd 	if (per_cpu_timers) {
218883831c6SAdrian Chadd 		cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
219883831c6SAdrian Chadd 		if (cpuid == NETISR_CPUID_NONE)
220883831c6SAdrian Chadd 			return (curcpu);	/* XXX */
221883831c6SAdrian Chadd 		else
222883831c6SAdrian Chadd 			return (cpuid);
223883831c6SAdrian Chadd 	}
224883831c6SAdrian Chadd #else
225883831c6SAdrian Chadd 	/* Legacy, pre-RSS behaviour */
226883831c6SAdrian Chadd 	if (per_cpu_timers) {
227883831c6SAdrian Chadd 		/*
228883831c6SAdrian Chadd 		 * We don't have a flowid -> cpuid mapping, so cheat and
229883831c6SAdrian Chadd 		 * just map unknown cpuids to curcpu.  Not the best, but
230883831c6SAdrian Chadd 		 * apparently better than defaulting to swi 0.
231883831c6SAdrian Chadd 		 */
232883831c6SAdrian Chadd 		cpuid = inp->inp_flowid % (mp_maxid + 1);
233883831c6SAdrian Chadd 		if (! CPU_ABSENT(cpuid))
234883831c6SAdrian Chadd 			return (cpuid);
235883831c6SAdrian Chadd 		return (curcpu);
236883831c6SAdrian Chadd 	}
237883831c6SAdrian Chadd #endif
238883831c6SAdrian Chadd 	/* Default for RSS and non-RSS - cpuid 0 */
239883831c6SAdrian Chadd 	else {
240883831c6SAdrian Chadd 		return (0);
241883831c6SAdrian Chadd 	}
242883831c6SAdrian Chadd }
24387aedea4SKip Macy 
244df8bae1dSRodney W. Grimes /*
245df8bae1dSRodney W. Grimes  * Tcp protocol timeout routine called every 500 ms.
2469b8b58e0SJonathan Lemon  * Updates timestamps used for TCP
247df8bae1dSRodney W. Grimes  * causes finite state machine actions if timers expire.
248df8bae1dSRodney W. Grimes  */
249df8bae1dSRodney W. Grimes void
250e2f2059fSMike Silbersack tcp_slowtimo(void)
251df8bae1dSRodney W. Grimes {
2528b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
25315bd2b43SDavid Greenman 
2545ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
2558b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
2568b615593SMarko Zec 		CURVNET_SET(vnet_iter);
257cea40c48SJulien Charbon 		(void) tcp_tw_2msl_scan(0);
2588b615593SMarko Zec 		CURVNET_RESTORE();
2598b615593SMarko Zec 	}
2605ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
261df8bae1dSRodney W. Grimes }
262df8bae1dSRodney W. Grimes 
2637d42e30cSJonathan Lemon int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
2647d42e30cSJonathan Lemon     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
2657d42e30cSJonathan Lemon 
266df8bae1dSRodney W. Grimes int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
267f058535dSJeffrey Hsu     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
268df8bae1dSRodney W. Grimes 
269f058535dSJeffrey Hsu static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
270e79adb8eSGarrett Wollman 
271df8bae1dSRodney W. Grimes /*
272df8bae1dSRodney W. Grimes  * TCP timer processing.
273df8bae1dSRodney W. Grimes  */
27485d94372SRobert Watson 
27585d94372SRobert Watson void
27685d94372SRobert Watson tcp_timer_delack(void *xtp)
277df8bae1dSRodney W. Grimes {
27885d94372SRobert Watson 	struct tcpcb *tp = xtp;
27985d94372SRobert Watson 	struct inpcb *inp;
2808b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
28185d94372SRobert Watson 
28285d94372SRobert Watson 	inp = tp->t_inpcb;
2835571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
2848501a69cSRobert Watson 	INP_WLOCK(inp);
285655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_delack) ||
286655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_delack)) {
2878501a69cSRobert Watson 		INP_WUNLOCK(inp);
2888b615593SMarko Zec 		CURVNET_RESTORE();
28985d94372SRobert Watson 		return;
29085d94372SRobert Watson 	}
291e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_delack);
292655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
293655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
294655f934bSMikolaj Golub 		CURVNET_RESTORE();
295655f934bSMikolaj Golub 		return;
296655f934bSMikolaj Golub 	}
2979b8b58e0SJonathan Lemon 	tp->t_flags |= TF_ACKNOW;
29878b50714SRobert Watson 	TCPSTAT_INC(tcps_delack);
29955bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
3008501a69cSRobert Watson 	INP_WUNLOCK(inp);
3018b615593SMarko Zec 	CURVNET_RESTORE();
3029b8b58e0SJonathan Lemon }
3039b8b58e0SJonathan Lemon 
3040fa047b9SRandall Stewart /*
3050fa047b9SRandall Stewart  * When a timer wants to remove a TCB it must
3060fa047b9SRandall Stewart  * hold the INP_INFO_RLOCK(). The timer function
3070fa047b9SRandall Stewart  * should only have grabbed the INP_WLOCK() when
3080fa047b9SRandall Stewart  * it entered. To safely switch to holding both the
3090fa047b9SRandall Stewart  * INP_INFO_RLOCK() and the INP_WLOCK() we must first
310eadd00f8SRandall Stewart  * grab a reference on the inp, which will hold the inp
311eadd00f8SRandall Stewart  * so that it can't be removed. We then unlock the INP_WLOCK(),
312eadd00f8SRandall Stewart  * and grab the INP_INFO_RLOCK() lock. Once we have the INP_INFO_RLOCK()
313eadd00f8SRandall Stewart  * we proceed again to get the INP_WLOCK() (this preserves proper
314eadd00f8SRandall Stewart  * lock order). After acquiring the INP_WLOCK we must check if someone
315eadd00f8SRandall Stewart  * else deleted the pcb i.e. the inp_flags check.
316eadd00f8SRandall Stewart  * If so we return 1 otherwise we return 0.
3170fa047b9SRandall Stewart  *
318eadd00f8SRandall Stewart  * No matter what the tcp_inpinfo_lock_add() function
3190fa047b9SRandall Stewart  * returns the caller must afterwards call tcp_inpinfo_lock_del()
3200fa047b9SRandall Stewart  * to drop the locks and reference properly.
3210fa047b9SRandall Stewart  */
3220fa047b9SRandall Stewart 
323b07fef50SRandall Stewart int
324b07fef50SRandall Stewart tcp_inpinfo_lock_add(struct inpcb *inp)
325b07fef50SRandall Stewart {
326b07fef50SRandall Stewart 	in_pcbref(inp);
327b07fef50SRandall Stewart 	INP_WUNLOCK(inp);
328b07fef50SRandall Stewart 	INP_INFO_RLOCK(&V_tcbinfo);
329b07fef50SRandall Stewart 	INP_WLOCK(inp);
330b07fef50SRandall Stewart 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
331b07fef50SRandall Stewart 		return(1);
332b07fef50SRandall Stewart 	}
333b07fef50SRandall Stewart 	return(0);
334b07fef50SRandall Stewart 
335b07fef50SRandall Stewart }
336b07fef50SRandall Stewart 
337b07fef50SRandall Stewart void
338b07fef50SRandall Stewart tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp)
339b07fef50SRandall Stewart {
340b07fef50SRandall Stewart 	INP_INFO_RUNLOCK(&V_tcbinfo);
341b07fef50SRandall Stewart 	if (inp && (tp == NULL)) {
342b07fef50SRandall Stewart 		/*
343b07fef50SRandall Stewart 		 * If tcp_close/drop() gets called and tp
344b07fef50SRandall Stewart 		 * returns NULL, then the function dropped
345b07fef50SRandall Stewart 		 * the inp lock, we hold a reference keeping
346b07fef50SRandall Stewart 		 * this around, so we must re-aquire the
347b07fef50SRandall Stewart 		 * INP_WLOCK() in order to proceed with
348b07fef50SRandall Stewart 		 * our dropping the inp reference.
349b07fef50SRandall Stewart 		 */
350b07fef50SRandall Stewart 		INP_WLOCK(inp);
351b07fef50SRandall Stewart 	}
352b07fef50SRandall Stewart 	if (inp && in_pcbrele_wlocked(inp) == 0)
353b07fef50SRandall Stewart 		INP_WUNLOCK(inp);
354b07fef50SRandall Stewart }
355b07fef50SRandall Stewart 
35685d94372SRobert Watson void
35785d94372SRobert Watson tcp_timer_2msl(void *xtp)
3589b8b58e0SJonathan Lemon {
35985d94372SRobert Watson 	struct tcpcb *tp = xtp;
36085d94372SRobert Watson 	struct inpcb *inp;
3618b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
3629b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3639b8b58e0SJonathan Lemon 	int ostate;
3649b8b58e0SJonathan Lemon 
3659b8b58e0SJonathan Lemon 	ostate = tp->t_state;
3669b8b58e0SJonathan Lemon #endif
36785d94372SRobert Watson 	inp = tp->t_inpcb;
3685571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
3698501a69cSRobert Watson 	INP_WLOCK(inp);
37085d94372SRobert Watson 	tcp_free_sackholes(tp);
371655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_2msl) ||
372e2f2059fSMike Silbersack 	    !callout_active(&tp->t_timers->tt_2msl)) {
3738501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3748b615593SMarko Zec 		CURVNET_RESTORE();
37585d94372SRobert Watson 		return;
37685d94372SRobert Watson 	}
377e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_2msl);
378655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
379655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
380655f934bSMikolaj Golub 		CURVNET_RESTORE();
381655f934bSMikolaj Golub 		return;
382655f934bSMikolaj Golub 	}
3835571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
3845571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
38585d94372SRobert Watson 	/*
386df8bae1dSRodney W. Grimes 	 * 2 MSL timeout in shutdown went off.  If we're closed but
387df8bae1dSRodney W. Grimes 	 * still waiting for peer to close and connection has been idle
38831a7749dSJulien Charbon 	 * too long delete connection control block.  Otherwise, check
38931a7749dSJulien Charbon 	 * again in a bit.
39031a7749dSJulien Charbon 	 *
39131a7749dSJulien Charbon 	 * If in TIME_WAIT state just ignore as this timeout is handled in
39231a7749dSJulien Charbon 	 * tcp_tw_2msl_scan().
3937c72af87SMohan Srinivasan 	 *
3947c72af87SMohan Srinivasan 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
3957c72af87SMohan Srinivasan 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
3967c72af87SMohan Srinivasan 	 * Ignore fact that there were recent incoming segments.
397df8bae1dSRodney W. Grimes 	 */
39831a7749dSJulien Charbon 	if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
39931a7749dSJulien Charbon 		INP_WUNLOCK(inp);
40031a7749dSJulien Charbon 		CURVNET_RESTORE();
40131a7749dSJulien Charbon 		return;
40231a7749dSJulien Charbon 	}
4037c72af87SMohan Srinivasan 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
40485d94372SRobert Watson 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
4057c72af87SMohan Srinivasan 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
40678b50714SRobert Watson 		TCPSTAT_INC(tcps_finwait2_drops);
407b07fef50SRandall Stewart 		if (tcp_inpinfo_lock_add(inp)) {
408b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
409b07fef50SRandall Stewart 			goto out;
410b07fef50SRandall Stewart 		}
41185d94372SRobert Watson 		tp = tcp_close(tp);
412b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
413b07fef50SRandall Stewart 		goto out;
4147c72af87SMohan Srinivasan 	} else {
415d6de19acSJulien Charbon 		if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
416b07fef50SRandall Stewart 			callout_reset(&tp->t_timers->tt_2msl,
417b07fef50SRandall Stewart 				      TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
418b07fef50SRandall Stewart 		} else {
419b07fef50SRandall Stewart 			if (tcp_inpinfo_lock_add(inp)) {
420b07fef50SRandall Stewart 				tcp_inpinfo_lock_del(inp, tp);
421b07fef50SRandall Stewart 				goto out;
422d6de19acSJulien Charbon 			}
42385d94372SRobert Watson 			tp = tcp_close(tp);
424b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
425b07fef50SRandall Stewart 			goto out;
426b07fef50SRandall Stewart 		}
4277c72af87SMohan Srinivasan        }
428df8bae1dSRodney W. Grimes 
4299b8b58e0SJonathan Lemon #ifdef TCPDEBUG
430586b4a0eSKonstantin Belousov 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
431fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
4329b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
4339b8b58e0SJonathan Lemon #endif
4345d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
4355d06879aSGeorge V. Neville-Neil 
43685d94372SRobert Watson 	if (tp != NULL)
4378501a69cSRobert Watson 		INP_WUNLOCK(inp);
438b07fef50SRandall Stewart out:
4398b615593SMarko Zec 	CURVNET_RESTORE();
4409b8b58e0SJonathan Lemon }
4419b8b58e0SJonathan Lemon 
44285d94372SRobert Watson void
44385d94372SRobert Watson tcp_timer_keep(void *xtp)
4449b8b58e0SJonathan Lemon {
44585d94372SRobert Watson 	struct tcpcb *tp = xtp;
44608517d53SMike Silbersack 	struct tcptemp *t_template;
44785d94372SRobert Watson 	struct inpcb *inp;
4488b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
4499b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4509b8b58e0SJonathan Lemon 	int ostate;
4519b8b58e0SJonathan Lemon 
4529b8b58e0SJonathan Lemon 	ostate = tp->t_state;
4539b8b58e0SJonathan Lemon #endif
45485d94372SRobert Watson 	inp = tp->t_inpcb;
4555571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
4568501a69cSRobert Watson 	INP_WLOCK(inp);
457655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_keep) ||
458655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_keep)) {
4598501a69cSRobert Watson 		INP_WUNLOCK(inp);
4608b615593SMarko Zec 		CURVNET_RESTORE();
46185d94372SRobert Watson 		return;
46285d94372SRobert Watson 	}
463e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_keep);
464655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
465655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
466655f934bSMikolaj Golub 		CURVNET_RESTORE();
467655f934bSMikolaj Golub 		return;
468655f934bSMikolaj Golub 	}
4695571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
4705571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
471*6d172f58SJonathan T. Looney 
472*6d172f58SJonathan T. Looney 	/*
473*6d172f58SJonathan T. Looney 	 * Because we don't regularly reset the keepalive callout in
474*6d172f58SJonathan T. Looney 	 * the ESTABLISHED state, it may be that we don't actually need
475*6d172f58SJonathan T. Looney 	 * to send a keepalive yet. If that occurs, schedule another
476*6d172f58SJonathan T. Looney 	 * call for the next time the keepalive timer might expire.
477*6d172f58SJonathan T. Looney 	 */
478*6d172f58SJonathan T. Looney 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
479*6d172f58SJonathan T. Looney 		u_int idletime;
480*6d172f58SJonathan T. Looney 
481*6d172f58SJonathan T. Looney 		idletime = ticks - tp->t_rcvtime;
482*6d172f58SJonathan T. Looney 		if (idletime < TP_KEEPIDLE(tp)) {
483*6d172f58SJonathan T. Looney 			callout_reset(&tp->t_timers->tt_keep,
484*6d172f58SJonathan T. Looney 			    TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp);
485*6d172f58SJonathan T. Looney 			INP_WUNLOCK(inp);
486*6d172f58SJonathan T. Looney 			CURVNET_RESTORE();
487*6d172f58SJonathan T. Looney 			return;
488*6d172f58SJonathan T. Looney 		}
489*6d172f58SJonathan T. Looney 	}
490*6d172f58SJonathan T. Looney 
4919b8b58e0SJonathan Lemon 	/*
4929b8b58e0SJonathan Lemon 	 * Keep-alive timer went off; send something
4939b8b58e0SJonathan Lemon 	 * or drop connection if idle for too long.
4949b8b58e0SJonathan Lemon 	 */
49578b50714SRobert Watson 	TCPSTAT_INC(tcps_keeptimeo);
4969b8b58e0SJonathan Lemon 	if (tp->t_state < TCPS_ESTABLISHED)
4979b8b58e0SJonathan Lemon 		goto dropit;
4982a074620SSam Leffler 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
4999b8b58e0SJonathan Lemon 	    tp->t_state <= TCPS_CLOSING) {
5009077f387SGleb Smirnoff 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
5019b8b58e0SJonathan Lemon 			goto dropit;
5029b8b58e0SJonathan Lemon 		/*
5039b8b58e0SJonathan Lemon 		 * Send a packet designed to force a response
5049b8b58e0SJonathan Lemon 		 * if the peer is up and reachable:
5059b8b58e0SJonathan Lemon 		 * either an ACK if the connection is still alive,
5069b8b58e0SJonathan Lemon 		 * or an RST if the peer has closed the connection
5079b8b58e0SJonathan Lemon 		 * due to timeout or reboot.
5089b8b58e0SJonathan Lemon 		 * Using sequence number tp->snd_una-1
5099b8b58e0SJonathan Lemon 		 * causes the transmitted zero-length segment
5109b8b58e0SJonathan Lemon 		 * to lie outside the receive window;
5119b8b58e0SJonathan Lemon 		 * by the protocol spec, this requires the
5129b8b58e0SJonathan Lemon 		 * correspondent TCP to respond.
5139b8b58e0SJonathan Lemon 		 */
51478b50714SRobert Watson 		TCPSTAT_INC(tcps_keepprobe);
51579909384SJonathan Lemon 		t_template = tcpip_maketemplate(inp);
51608517d53SMike Silbersack 		if (t_template) {
51708517d53SMike Silbersack 			tcp_respond(tp, t_template->tt_ipgen,
51808517d53SMike Silbersack 				    &t_template->tt_t, (struct mbuf *)NULL,
5199b8b58e0SJonathan Lemon 				    tp->rcv_nxt, tp->snd_una - 1, 0);
52053640b0eSRobert Watson 			free(t_template, M_TEMP);
52108517d53SMike Silbersack 		}
522b07fef50SRandall Stewart 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
523b07fef50SRandall Stewart 			      tcp_timer_keep, tp);
524b07fef50SRandall Stewart 	} else
525b07fef50SRandall Stewart 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
526b07fef50SRandall Stewart 			      tcp_timer_keep, tp);
5279b8b58e0SJonathan Lemon 
5289b8b58e0SJonathan Lemon #ifdef TCPDEBUG
5292a074620SSam Leffler 	if (inp->inp_socket->so_options & SO_DEBUG)
530fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
5319b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
5329b8b58e0SJonathan Lemon #endif
5335d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
5348501a69cSRobert Watson 	INP_WUNLOCK(inp);
5358b615593SMarko Zec 	CURVNET_RESTORE();
53685d94372SRobert Watson 	return;
5379b8b58e0SJonathan Lemon 
5389b8b58e0SJonathan Lemon dropit:
53978b50714SRobert Watson 	TCPSTAT_INC(tcps_keepdrops);
540b07fef50SRandall Stewart 
541b07fef50SRandall Stewart 	if (tcp_inpinfo_lock_add(inp)) {
542b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
543b07fef50SRandall Stewart 		goto out;
544b07fef50SRandall Stewart 	}
54585d94372SRobert Watson 	tp = tcp_drop(tp, ETIMEDOUT);
54685d94372SRobert Watson 
54785d94372SRobert Watson #ifdef TCPDEBUG
54885d94372SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
54985d94372SRobert Watson 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
55085d94372SRobert Watson 			  PRU_SLOWTIMO);
55185d94372SRobert Watson #endif
5525d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
553b07fef50SRandall Stewart 	tcp_inpinfo_lock_del(inp, tp);
554b07fef50SRandall Stewart out:
5558b615593SMarko Zec 	CURVNET_RESTORE();
5569b8b58e0SJonathan Lemon }
5579b8b58e0SJonathan Lemon 
55885d94372SRobert Watson void
55985d94372SRobert Watson tcp_timer_persist(void *xtp)
5609b8b58e0SJonathan Lemon {
56185d94372SRobert Watson 	struct tcpcb *tp = xtp;
56285d94372SRobert Watson 	struct inpcb *inp;
5638b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
5649b8b58e0SJonathan Lemon #ifdef TCPDEBUG
5659b8b58e0SJonathan Lemon 	int ostate;
5669b8b58e0SJonathan Lemon 
5679b8b58e0SJonathan Lemon 	ostate = tp->t_state;
5689b8b58e0SJonathan Lemon #endif
56985d94372SRobert Watson 	inp = tp->t_inpcb;
5705571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
5718501a69cSRobert Watson 	INP_WLOCK(inp);
572655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_persist) ||
573655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_persist)) {
5748501a69cSRobert Watson 		INP_WUNLOCK(inp);
5758b615593SMarko Zec 		CURVNET_RESTORE();
57685d94372SRobert Watson 		return;
57785d94372SRobert Watson 	}
578e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_persist);
579655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
580655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
581655f934bSMikolaj Golub 		CURVNET_RESTORE();
582655f934bSMikolaj Golub 		return;
583655f934bSMikolaj Golub 	}
5845571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
5855571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
5869b8b58e0SJonathan Lemon 	/*
587a4641f4eSPedro F. Giffuni 	 * Persistence timer into zero window.
5889b8b58e0SJonathan Lemon 	 * Force a byte to be output, if possible.
5899b8b58e0SJonathan Lemon 	 */
59078b50714SRobert Watson 	TCPSTAT_INC(tcps_persisttimeo);
5919b8b58e0SJonathan Lemon 	/*
5929b8b58e0SJonathan Lemon 	 * Hack: if the peer is dead/unreachable, we do not
5939b8b58e0SJonathan Lemon 	 * time out if the window is closed.  After a full
5949b8b58e0SJonathan Lemon 	 * backoff, drop the connection if the idle time
5959b8b58e0SJonathan Lemon 	 * (no responses to probes) reaches the maximum
5969b8b58e0SJonathan Lemon 	 * backoff that we would use if retransmitting.
5979b8b58e0SJonathan Lemon 	 */
5989b8b58e0SJonathan Lemon 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
5996b0c5521SJohn Baldwin 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
6006b0c5521SJohn Baldwin 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
60178b50714SRobert Watson 		TCPSTAT_INC(tcps_persistdrop);
602b07fef50SRandall Stewart 		if (tcp_inpinfo_lock_add(inp)) {
603b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
604b07fef50SRandall Stewart 			goto out;
605b07fef50SRandall Stewart 		}
60685d94372SRobert Watson 		tp = tcp_drop(tp, ETIMEDOUT);
607b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
60885d94372SRobert Watson 		goto out;
6099b8b58e0SJonathan Lemon 	}
610322181c9SAndre Oppermann 	/*
611322181c9SAndre Oppermann 	 * If the user has closed the socket then drop a persisting
612322181c9SAndre Oppermann 	 * connection after a much reduced timeout.
613322181c9SAndre Oppermann 	 */
614322181c9SAndre Oppermann 	if (tp->t_state > TCPS_CLOSE_WAIT &&
615322181c9SAndre Oppermann 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
616322181c9SAndre Oppermann 		TCPSTAT_INC(tcps_persistdrop);
617b07fef50SRandall Stewart 		if (tcp_inpinfo_lock_add(inp)) {
618b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
619b07fef50SRandall Stewart 			goto out;
620b07fef50SRandall Stewart 		}
621322181c9SAndre Oppermann 		tp = tcp_drop(tp, ETIMEDOUT);
622b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
623322181c9SAndre Oppermann 		goto out;
624322181c9SAndre Oppermann 	}
6259b8b58e0SJonathan Lemon 	tcp_setpersist(tp);
6262cdbfa66SPaul Saab 	tp->t_flags |= TF_FORCEDATA;
62755bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
6282cdbfa66SPaul Saab 	tp->t_flags &= ~TF_FORCEDATA;
6299b8b58e0SJonathan Lemon 
6309b8b58e0SJonathan Lemon #ifdef TCPDEBUG
631ffb761f6SGleb Smirnoff 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
632ffb761f6SGleb Smirnoff 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
6339b8b58e0SJonathan Lemon #endif
6345d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
6358501a69cSRobert Watson 	INP_WUNLOCK(inp);
636b07fef50SRandall Stewart out:
6378b615593SMarko Zec 	CURVNET_RESTORE();
6389b8b58e0SJonathan Lemon }
6399b8b58e0SJonathan Lemon 
64085d94372SRobert Watson void
64185d94372SRobert Watson tcp_timer_rexmt(void * xtp)
6429b8b58e0SJonathan Lemon {
64385d94372SRobert Watson 	struct tcpcb *tp = xtp;
6448b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
6459b8b58e0SJonathan Lemon 	int rexmt;
64685d94372SRobert Watson 	struct inpcb *inp;
6479b8b58e0SJonathan Lemon #ifdef TCPDEBUG
6489b8b58e0SJonathan Lemon 	int ostate;
6499b8b58e0SJonathan Lemon 
6509b8b58e0SJonathan Lemon 	ostate = tp->t_state;
6519b8b58e0SJonathan Lemon #endif
65285d94372SRobert Watson 	inp = tp->t_inpcb;
6535571f9cfSJulien Charbon 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
6548501a69cSRobert Watson 	INP_WLOCK(inp);
655655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
656655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_rexmt)) {
6578501a69cSRobert Watson 		INP_WUNLOCK(inp);
6588b615593SMarko Zec 		CURVNET_RESTORE();
65985d94372SRobert Watson 		return;
66085d94372SRobert Watson 	}
661e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_rexmt);
662655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
663655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
664655f934bSMikolaj Golub 		CURVNET_RESTORE();
665655f934bSMikolaj Golub 		return;
666655f934bSMikolaj Golub 	}
6675571f9cfSJulien Charbon 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
6685571f9cfSJulien Charbon 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
6696d90faf3SPaul Saab 	tcp_free_sackholes(tp);
6705105a92cSRandall Stewart 	if (tp->t_fb->tfb_tcp_rexmit_tmr) {
6715105a92cSRandall Stewart 		/* The stack has a timer action too. */
6725105a92cSRandall Stewart 		(*tp->t_fb->tfb_tcp_rexmit_tmr)(tp);
6735105a92cSRandall Stewart 	}
674df8bae1dSRodney W. Grimes 	/*
675df8bae1dSRodney W. Grimes 	 * Retransmission timer went off.  Message has not
676df8bae1dSRodney W. Grimes 	 * been acked within retransmit interval.  Back off
677df8bae1dSRodney W. Grimes 	 * to a longer retransmit interval and retransmit one segment.
678df8bae1dSRodney W. Grimes 	 */
679df8bae1dSRodney W. Grimes 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
680df8bae1dSRodney W. Grimes 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
68178b50714SRobert Watson 		TCPSTAT_INC(tcps_timeoutdrop);
682b07fef50SRandall Stewart 		if (tcp_inpinfo_lock_add(inp)) {
683b07fef50SRandall Stewart 			tcp_inpinfo_lock_del(inp, tp);
68485d94372SRobert Watson 			goto out;
6859b8b58e0SJonathan Lemon 		}
686b07fef50SRandall Stewart 		tp = tcp_drop(tp, tp->t_softerror ?
687b07fef50SRandall Stewart 			      tp->t_softerror : ETIMEDOUT);
688b07fef50SRandall Stewart 		tcp_inpinfo_lock_del(inp, tp);
689b07fef50SRandall Stewart 		goto out;
690b07fef50SRandall Stewart 	}
691cf8f04f4SAndre Oppermann 	if (tp->t_state == TCPS_SYN_SENT) {
692cf8f04f4SAndre Oppermann 		/*
693cf8f04f4SAndre Oppermann 		 * If the SYN was retransmitted, indicate CWND to be
694cf8f04f4SAndre Oppermann 		 * limited to 1 segment in cc_conn_init().
695cf8f04f4SAndre Oppermann 		 */
696cf8f04f4SAndre Oppermann 		tp->snd_cwnd = 1;
697cf8f04f4SAndre Oppermann 	} else if (tp->t_rxtshift == 1) {
6989b8b58e0SJonathan Lemon 		/*
6999b8b58e0SJonathan Lemon 		 * first retransmit; record ssthresh and cwnd so they can
7009b8b58e0SJonathan Lemon 		 * be recovered if this turns out to be a "bad" retransmit.
7019b8b58e0SJonathan Lemon 		 * A retransmit is considered "bad" if an ACK for this
7029b8b58e0SJonathan Lemon 		 * segment is received within RTT/2 interval; the assumption
7039b8b58e0SJonathan Lemon 		 * here is that the ACK was already in flight.  See
7049b8b58e0SJonathan Lemon 		 * "On Estimating End-to-End Network Path Properties" by
7059b8b58e0SJonathan Lemon 		 * Allman and Paxson for more details.
7069b8b58e0SJonathan Lemon 		 */
7079b8b58e0SJonathan Lemon 		tp->snd_cwnd_prev = tp->snd_cwnd;
7089b8b58e0SJonathan Lemon 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
7099d11646dSJeffrey Hsu 		tp->snd_recover_prev = tp->snd_recover;
710dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags))
7119d11646dSJeffrey Hsu 			tp->t_flags |= TF_WASFRECOVERY;
7129d11646dSJeffrey Hsu 		else
7139d11646dSJeffrey Hsu 			tp->t_flags &= ~TF_WASFRECOVERY;
714dbc42409SLawrence Stewart 		if (IN_CONGRECOVERY(tp->t_flags))
715dbc42409SLawrence Stewart 			tp->t_flags |= TF_WASCRECOVERY;
716dbc42409SLawrence Stewart 		else
717dbc42409SLawrence Stewart 			tp->t_flags &= ~TF_WASCRECOVERY;
7189b8b58e0SJonathan Lemon 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
719672dc4aeSJohn Baldwin 		tp->t_flags |= TF_PREVVALID;
720672dc4aeSJohn Baldwin 	} else
721672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
72278b50714SRobert Watson 	TCPSTAT_INC(tcps_rexmttimeo);
723281a0fd4SPatrick Kelsey 	if ((tp->t_state == TCPS_SYN_SENT) ||
724281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
725f4748ef5SAndre Oppermann 		rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
7267d42e30cSJonathan Lemon 	else
727df8bae1dSRodney W. Grimes 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
728df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
729df8bae1dSRodney W. Grimes 		      tp->t_rttmin, TCPTV_REXMTMAX);
730f6f6703fSSean Bruno 
731882ac53eSSean Bruno 	/*
732882ac53eSSean Bruno 	 * We enter the path for PLMTUD if connection is established or, if
733882ac53eSSean Bruno 	 * connection is FIN_WAIT_1 status, reason for the last is that if
734882ac53eSSean Bruno 	 * amount of data we send is very small, we could send it in couple of
735882ac53eSSean Bruno 	 * packets and process straight to FIN. In that case we won't catch
736882ac53eSSean Bruno 	 * ESTABLISHED state.
737882ac53eSSean Bruno 	 */
738882ac53eSSean Bruno 	if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
739882ac53eSSean Bruno 	    || (tp->t_state == TCPS_FIN_WAIT_1))) {
740f6f6703fSSean Bruno #ifdef INET6
741f6f6703fSSean Bruno 		int isipv6;
742f6f6703fSSean Bruno #endif
743f6f6703fSSean Bruno 
744adf43a92SHiren Panchasara 		/*
745adf43a92SHiren Panchasara 		 * Idea here is that at each stage of mtu probe (usually, 1448
746adf43a92SHiren Panchasara 		 * -> 1188 -> 524) should be given 2 chances to recover before
747adf43a92SHiren Panchasara 		 *  further clamping down. 'tp->t_rxtshift % 2 == 0' should
748adf43a92SHiren Panchasara 		 *  take care of that.
749adf43a92SHiren Panchasara 		 */
750f6f6703fSSean Bruno 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
751f6f6703fSSean Bruno 		    (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
752adf43a92SHiren Panchasara 		    (tp->t_rxtshift >= 2 && tp->t_rxtshift % 2 == 0)) {
753f6f6703fSSean Bruno 			/*
754f6f6703fSSean Bruno 			 * Enter Path MTU Black-hole Detection mechanism:
755f6f6703fSSean Bruno 			 * - Disable Path MTU Discovery (IP "DF" bit).
756f6f6703fSSean Bruno 			 * - Reduce MTU to lower value than what we
757f6f6703fSSean Bruno 			 *   negotiated with peer.
758f6f6703fSSean Bruno 			 */
759f6f6703fSSean Bruno 			/* Record that we may have found a black hole. */
760f6f6703fSSean Bruno 			tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
761f6f6703fSSean Bruno 
762f6f6703fSSean Bruno 			/* Keep track of previous MSS. */
7630c39d38dSGleb Smirnoff 			tp->t_pmtud_saved_maxseg = tp->t_maxseg;
764f6f6703fSSean Bruno 
765f6f6703fSSean Bruno 			/*
766f6f6703fSSean Bruno 			 * Reduce the MSS to blackhole value or to the default
767f6f6703fSSean Bruno 			 * in an attempt to retransmit.
768f6f6703fSSean Bruno 			 */
769f6f6703fSSean Bruno #ifdef INET6
770f6f6703fSSean Bruno 			isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
771f6f6703fSSean Bruno 			if (isipv6 &&
7720c39d38dSGleb Smirnoff 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
773f6f6703fSSean Bruno 				/* Use the sysctl tuneable blackhole MSS. */
7740c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
775f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated++;
776f6f6703fSSean Bruno 			} else if (isipv6) {
777f6f6703fSSean Bruno 				/* Use the default MSS. */
7780c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_v6mssdflt;
779f6f6703fSSean Bruno 				/*
780f6f6703fSSean Bruno 				 * Disable Path MTU Discovery when we switch to
781f6f6703fSSean Bruno 				 * minmss.
782f6f6703fSSean Bruno 				 */
783f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
784f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated_min_mss++;
785f6f6703fSSean Bruno 			}
786f6f6703fSSean Bruno #endif
787f6f6703fSSean Bruno #if defined(INET6) && defined(INET)
788f6f6703fSSean Bruno 			else
789f6f6703fSSean Bruno #endif
790f6f6703fSSean Bruno #ifdef INET
7910c39d38dSGleb Smirnoff 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
792f6f6703fSSean Bruno 				/* Use the sysctl tuneable blackhole MSS. */
7930c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
794f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated++;
795f6f6703fSSean Bruno 			} else {
796f6f6703fSSean Bruno 				/* Use the default MSS. */
7970c39d38dSGleb Smirnoff 				tp->t_maxseg = V_tcp_mssdflt;
798f6f6703fSSean Bruno 				/*
799f6f6703fSSean Bruno 				 * Disable Path MTU Discovery when we switch to
800f6f6703fSSean Bruno 				 * minmss.
801f6f6703fSSean Bruno 				 */
802f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
803f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_activated_min_mss++;
804f6f6703fSSean Bruno 			}
805f6f6703fSSean Bruno #endif
806f6f6703fSSean Bruno 			/*
807f6f6703fSSean Bruno 			 * Reset the slow-start flight size
808f6f6703fSSean Bruno 			 * as it may depend on the new MSS.
809f6f6703fSSean Bruno 			 */
810f6f6703fSSean Bruno 			if (CC_ALGO(tp)->conn_init != NULL)
811f6f6703fSSean Bruno 				CC_ALGO(tp)->conn_init(tp->ccv);
812f6f6703fSSean Bruno 		} else {
813f6f6703fSSean Bruno 			/*
814f6f6703fSSean Bruno 			 * If further retransmissions are still unsuccessful
815f6f6703fSSean Bruno 			 * with a lowered MTU, maybe this isn't a blackhole and
816f6f6703fSSean Bruno 			 * we restore the previous MSS and blackhole detection
817f6f6703fSSean Bruno 			 * flags.
818adf43a92SHiren Panchasara 			 * The limit '6' is determined by giving each probe
819adf43a92SHiren Panchasara 			 * stage (1448, 1188, 524) 2 chances to recover.
820f6f6703fSSean Bruno 			 */
821f6f6703fSSean Bruno 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
822adf43a92SHiren Panchasara 			    (tp->t_rxtshift > 6)) {
823f6f6703fSSean Bruno 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
824f6f6703fSSean Bruno 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
8250c39d38dSGleb Smirnoff 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
826f6f6703fSSean Bruno 				V_tcp_pmtud_blackhole_failed++;
827f6f6703fSSean Bruno 				/*
828f6f6703fSSean Bruno 				 * Reset the slow-start flight size as it
829f6f6703fSSean Bruno 				 * may depend on the new MSS.
830f6f6703fSSean Bruno 				 */
831f6f6703fSSean Bruno 				if (CC_ALGO(tp)->conn_init != NULL)
832f6f6703fSSean Bruno 					CC_ALGO(tp)->conn_init(tp->ccv);
833f6f6703fSSean Bruno 			}
834f6f6703fSSean Bruno 		}
835f6f6703fSSean Bruno 	}
836f6f6703fSSean Bruno 
837df8bae1dSRodney W. Grimes 	/*
83877339e1cSAndre Oppermann 	 * Disable RFC1323 and SACK if we haven't got any response to
8397ceb7783SJesper Skriver 	 * our third SYN to work-around some broken terminal servers
8407ceb7783SJesper Skriver 	 * (most of which have hopefully been retired) that have bad VJ
8417ceb7783SJesper Skriver 	 * header compression code which trashes TCP segments containing
8427ceb7783SJesper Skriver 	 * unknown-to-them TCP options.
8437ceb7783SJesper Skriver 	 */
8446c0ef895SJohn Baldwin 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
8456c0ef895SJohn Baldwin 	    (tp->t_rxtshift == 3))
846c4ab59c1SAndre Oppermann 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
8477ceb7783SJesper Skriver 	/*
84897d8d152SAndre Oppermann 	 * If we backed off this far, our srtt estimate is probably bogus.
84997d8d152SAndre Oppermann 	 * Clobber it so we'll take the next rtt measurement as our srtt;
850df8bae1dSRodney W. Grimes 	 * move the current srtt into rttvar to keep the current
851df8bae1dSRodney W. Grimes 	 * retransmit times until then.
852df8bae1dSRodney W. Grimes 	 */
853df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
854fb59c426SYoshinobu Inoue #ifdef INET6
855fb59c426SYoshinobu Inoue 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
856fb59c426SYoshinobu Inoue 			in6_losing(tp->t_inpcb);
85784cc0778SGeorge V. Neville-Neil 		else
858fb59c426SYoshinobu Inoue #endif
85984cc0778SGeorge V. Neville-Neil 			in_losing(tp->t_inpcb);
860df8bae1dSRodney W. Grimes 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
861df8bae1dSRodney W. Grimes 		tp->t_srtt = 0;
862df8bae1dSRodney W. Grimes 	}
863df8bae1dSRodney W. Grimes 	tp->snd_nxt = tp->snd_una;
8649d11646dSJeffrey Hsu 	tp->snd_recover = tp->snd_max;
86546f58482SJonathan Lemon 	/*
86674b48c1dSAndras Olah 	 * Force a segment to be sent.
86774b48c1dSAndras Olah 	 */
86874b48c1dSAndras Olah 	tp->t_flags |= TF_ACKNOW;
86974b48c1dSAndras Olah 	/*
870df8bae1dSRodney W. Grimes 	 * If timing a segment in this window, stop the timer.
871df8bae1dSRodney W. Grimes 	 */
8729b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
873dbc42409SLawrence Stewart 
874b5af1b88SLawrence Stewart 	cc_cong_signal(tp, NULL, CC_RTO);
875dbc42409SLawrence Stewart 
87655bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
877df8bae1dSRodney W. Grimes 
8789b8b58e0SJonathan Lemon #ifdef TCPDEBUG
8791c53f806SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
880fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
8819b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
882df8bae1dSRodney W. Grimes #endif
8835d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
8848501a69cSRobert Watson 	INP_WUNLOCK(inp);
885b07fef50SRandall Stewart out:
8868b615593SMarko Zec 	CURVNET_RESTORE();
88785d94372SRobert Watson }
88885d94372SRobert Watson 
88985d94372SRobert Watson void
8905571f9cfSJulien Charbon tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
89185d94372SRobert Watson {
89285d94372SRobert Watson 	struct callout *t_callout;
89318832f1fSJulien Charbon 	timeout_t *f_callout;
89487aedea4SKip Macy 	struct inpcb *inp = tp->t_inpcb;
895883831c6SAdrian Chadd 	int cpu = inp_to_cpuid(inp);
89685d94372SRobert Watson 
89709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
89809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
89909fe6320SNavdeep Parhar 		return;
90009fe6320SNavdeep Parhar #endif
90109fe6320SNavdeep Parhar 
9025571f9cfSJulien Charbon 	if (tp->t_timers->tt_flags & TT_STOPPED)
9035571f9cfSJulien Charbon 		return;
9045571f9cfSJulien Charbon 
90585d94372SRobert Watson 	switch (timer_type) {
90685d94372SRobert Watson 		case TT_DELACK:
907e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
90885d94372SRobert Watson 			f_callout = tcp_timer_delack;
90985d94372SRobert Watson 			break;
91085d94372SRobert Watson 		case TT_REXMT:
911e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
91285d94372SRobert Watson 			f_callout = tcp_timer_rexmt;
91385d94372SRobert Watson 			break;
91485d94372SRobert Watson 		case TT_PERSIST:
915e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
91685d94372SRobert Watson 			f_callout = tcp_timer_persist;
91785d94372SRobert Watson 			break;
91885d94372SRobert Watson 		case TT_KEEP:
919e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
92085d94372SRobert Watson 			f_callout = tcp_timer_keep;
92185d94372SRobert Watson 			break;
92285d94372SRobert Watson 		case TT_2MSL:
923e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
92485d94372SRobert Watson 			f_callout = tcp_timer_2msl;
92585d94372SRobert Watson 			break;
92685d94372SRobert Watson 		default:
92755bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_activate) {
92855bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
92955bceb1eSRandall Stewart 				return;
93055bceb1eSRandall Stewart 			}
93103374917SJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
93285d94372SRobert Watson 		}
93385d94372SRobert Watson 	if (delta == 0) {
934b07fef50SRandall Stewart 		callout_stop(t_callout);
93585d94372SRobert Watson 	} else {
93687aedea4SKip Macy 		callout_reset_on(t_callout, delta, f_callout, tp, cpu);
93785d94372SRobert Watson 	}
93885d94372SRobert Watson }
93985d94372SRobert Watson 
94085d94372SRobert Watson int
9415571f9cfSJulien Charbon tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
94285d94372SRobert Watson {
94385d94372SRobert Watson 	struct callout *t_callout;
94485d94372SRobert Watson 
94585d94372SRobert Watson 	switch (timer_type) {
94685d94372SRobert Watson 		case TT_DELACK:
947e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
94885d94372SRobert Watson 			break;
94985d94372SRobert Watson 		case TT_REXMT:
950e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
95185d94372SRobert Watson 			break;
95285d94372SRobert Watson 		case TT_PERSIST:
953e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
95485d94372SRobert Watson 			break;
95585d94372SRobert Watson 		case TT_KEEP:
956e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
95785d94372SRobert Watson 			break;
95885d94372SRobert Watson 		case TT_2MSL:
959e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
96085d94372SRobert Watson 			break;
96185d94372SRobert Watson 		default:
96255bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_active) {
96355bceb1eSRandall Stewart 				return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
96455bceb1eSRandall Stewart 			}
96503374917SJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
96685d94372SRobert Watson 		}
96785d94372SRobert Watson 	return callout_active(t_callout);
968df8bae1dSRodney W. Grimes }
969b8614722SMike Silbersack 
9705571f9cfSJulien Charbon void
9715571f9cfSJulien Charbon tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
9725571f9cfSJulien Charbon {
9735571f9cfSJulien Charbon 	struct callout *t_callout;
9745571f9cfSJulien Charbon 
9755571f9cfSJulien Charbon 	tp->t_timers->tt_flags |= TT_STOPPED;
9765571f9cfSJulien Charbon 	switch (timer_type) {
9775571f9cfSJulien Charbon 		case TT_DELACK:
9785571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_delack;
9795571f9cfSJulien Charbon 			break;
9805571f9cfSJulien Charbon 		case TT_REXMT:
9815571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_rexmt;
9825571f9cfSJulien Charbon 			break;
9835571f9cfSJulien Charbon 		case TT_PERSIST:
9845571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_persist;
9855571f9cfSJulien Charbon 			break;
9865571f9cfSJulien Charbon 		case TT_KEEP:
9875571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_keep;
9885571f9cfSJulien Charbon 			break;
9895571f9cfSJulien Charbon 		case TT_2MSL:
9905571f9cfSJulien Charbon 			t_callout = &tp->t_timers->tt_2msl;
9915571f9cfSJulien Charbon 			break;
9925571f9cfSJulien Charbon 		default:
99355bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_timer_stop) {
99455bceb1eSRandall Stewart 				/*
99555bceb1eSRandall Stewart 				 * XXXrrs we need to look at this with the
99655bceb1eSRandall Stewart 				 * stop case below (flags).
99755bceb1eSRandall Stewart 				 */
99855bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
99955bceb1eSRandall Stewart 				return;
100055bceb1eSRandall Stewart 			}
10015571f9cfSJulien Charbon 			panic("tp %p bad timer_type %#x", tp, timer_type);
10025571f9cfSJulien Charbon 		}
10035571f9cfSJulien Charbon 
1004e5ad6456SRandall Stewart 	if (callout_async_drain(t_callout, tcp_timer_discard) == 0) {
10055571f9cfSJulien Charbon 		/*
10065571f9cfSJulien Charbon 		 * Can't stop the callout, defer tcpcb actual deletion
1007e5ad6456SRandall Stewart 		 * to the last one. We do this using the async drain
1008e5ad6456SRandall Stewart 		 * function and incrementing the count in
10095571f9cfSJulien Charbon 		 */
1010e5ad6456SRandall Stewart 		tp->t_timers->tt_draincnt++;
10115571f9cfSJulien Charbon 	}
10125571f9cfSJulien Charbon }
10135571f9cfSJulien Charbon 
1014b8614722SMike Silbersack #define	ticks_to_msecs(t)	(1000*(t) / hz)
1015b8614722SMike Silbersack 
1016b8614722SMike Silbersack void
10175b999a6bSDavide Italiano tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
10185b999a6bSDavide Italiano     struct xtcp_timer *xtimer)
1019b8614722SMike Silbersack {
10205b999a6bSDavide Italiano 	sbintime_t now;
10215b999a6bSDavide Italiano 
10225b999a6bSDavide Italiano 	bzero(xtimer, sizeof(*xtimer));
1023b8614722SMike Silbersack 	if (timer == NULL)
1024b8614722SMike Silbersack 		return;
10255b999a6bSDavide Italiano 	now = getsbinuptime();
1026b8614722SMike Silbersack 	if (callout_active(&timer->tt_delack))
10275b999a6bSDavide Italiano 		xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
1028b8614722SMike Silbersack 	if (callout_active(&timer->tt_rexmt))
10295b999a6bSDavide Italiano 		xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
1030b8614722SMike Silbersack 	if (callout_active(&timer->tt_persist))
10315b999a6bSDavide Italiano 		xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
1032b8614722SMike Silbersack 	if (callout_active(&timer->tt_keep))
10335b999a6bSDavide Italiano 		xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
1034b8614722SMike Silbersack 	if (callout_active(&timer->tt_2msl))
10355b999a6bSDavide Italiano 		xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
1036b8614722SMike Silbersack 	xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);
1037b8614722SMike Silbersack }
1038