xref: /freebsd/sys/netinet/tcp_timer.c (revision c4ab59c1a1ced020b821d0fafe2e7508d8527a67)
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 
35fb59c426SYoshinobu Inoue #include "opt_inet6.h"
360cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
370cc12cc5SJoerg Wunsch 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
3998163b98SPoul-Henning Kamp #include <sys/kernel.h>
40c74af4faSBruce Evans #include <sys/lock.h>
4108517d53SMike Silbersack #include <sys/mbuf.h>
42c74af4faSBruce Evans #include <sys/mutex.h>
43c74af4faSBruce Evans #include <sys/protosw.h>
4487aedea4SKip Macy #include <sys/smp.h>
45df8bae1dSRodney W. Grimes #include <sys/socket.h>
46df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
47c74af4faSBruce Evans #include <sys/sysctl.h>
48c74af4faSBruce Evans #include <sys/systm.h>
49e79adb8eSGarrett Wollman 
504b79449eSBjoern A. Zeeb #include <net/if.h>
51df8bae1dSRodney W. Grimes #include <net/route.h>
52530c0060SRobert Watson #include <net/vnet.h>
53df8bae1dSRodney W. Grimes 
54dbc42409SLawrence Stewart #include <netinet/cc.h>
55df8bae1dSRodney W. Grimes #include <netinet/in.h>
56df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
57c74af4faSBruce Evans #include <netinet/in_systm.h>
58fb59c426SYoshinobu Inoue #ifdef INET6
59fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
60fb59c426SYoshinobu Inoue #endif
61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
62df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
63df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
64df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
65df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
66af7a2999SDavid Greenman #ifdef TCPDEBUG
67af7a2999SDavid Greenman #include <netinet/tcp_debug.h>
68af7a2999SDavid Greenman #endif
69df8bae1dSRodney W. Grimes 
709b8b58e0SJonathan Lemon int	tcp_keepinit;
71ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
7241698ebfSTom Rhodes     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
737b40aa32SPaul Traina 
749b8b58e0SJonathan Lemon int	tcp_keepidle;
75ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
7641698ebfSTom Rhodes     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
7798163b98SPoul-Henning Kamp 
789b8b58e0SJonathan Lemon int	tcp_keepintvl;
79ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
8041698ebfSTom Rhodes     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
8198163b98SPoul-Henning Kamp 
829b8b58e0SJonathan Lemon int	tcp_delacktime;
836489fe65SAndre Oppermann SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
846489fe65SAndre Oppermann     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
85ccb4d0c6SJonathan Lemon     "Time before a delayed ACK is sent");
869b8b58e0SJonathan Lemon 
879b8b58e0SJonathan Lemon int	tcp_msl;
88ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
89ccb4d0c6SJonathan Lemon     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
909b8b58e0SJonathan Lemon 
91701bec5aSMatthew Dillon int	tcp_rexmit_min;
92701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
936489fe65SAndre Oppermann     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
946489fe65SAndre Oppermann     "Minimum Retransmission Timeout");
95701bec5aSMatthew Dillon 
96701bec5aSMatthew Dillon int	tcp_rexmit_slop;
97701bec5aSMatthew Dillon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
986489fe65SAndre Oppermann     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
996489fe65SAndre Oppermann     "Retransmission Timer Slop");
100701bec5aSMatthew Dillon 
101c39a614eSRobert Watson static int	always_keepalive = 1;
1023d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
1033d177f46SBill Fumerola     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
10434be9bf3SPoul-Henning Kamp 
1057c72af87SMohan Srinivasan int    tcp_fast_finwait2_recycle = 0;
1067c72af87SMohan Srinivasan SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
1076489fe65SAndre Oppermann     &tcp_fast_finwait2_recycle, 0,
1086489fe65SAndre Oppermann     "Recycle closed FIN_WAIT_2 connections faster");
1097c72af87SMohan Srinivasan 
1107c72af87SMohan Srinivasan int    tcp_finwait2_timeout;
1117c72af87SMohan Srinivasan SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
1126489fe65SAndre Oppermann     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
1137c72af87SMohan Srinivasan 
1149077f387SGleb Smirnoff int	tcp_keepcnt = TCPTV_KEEPCNT;
1159077f387SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
1169077f387SGleb Smirnoff     "Number of keepalive probes to send");
1177c72af87SMohan Srinivasan 
1180312fbe9SPoul-Henning Kamp 	/* max idle probes */
1199b8b58e0SJonathan Lemon int	tcp_maxpersistidle;
120e79adb8eSGarrett Wollman 
12187aedea4SKip Macy static int	per_cpu_timers = 0;
12287aedea4SKip Macy SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
12387aedea4SKip Macy     &per_cpu_timers , 0, "run tcp timers on all cpus");
12487aedea4SKip Macy 
12587aedea4SKip Macy #define	INP_CPU(inp)	(per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
12687aedea4SKip Macy 		((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
12787aedea4SKip Macy 
128df8bae1dSRodney W. Grimes /*
129df8bae1dSRodney W. Grimes  * Tcp protocol timeout routine called every 500 ms.
1309b8b58e0SJonathan Lemon  * Updates timestamps used for TCP
131df8bae1dSRodney W. Grimes  * causes finite state machine actions if timers expire.
132df8bae1dSRodney W. Grimes  */
133df8bae1dSRodney W. Grimes void
134e2f2059fSMike Silbersack tcp_slowtimo(void)
135df8bae1dSRodney W. Grimes {
1368b615593SMarko Zec 	VNET_ITERATOR_DECL(vnet_iter);
13715bd2b43SDavid Greenman 
1385ee847d3SRobert Watson 	VNET_LIST_RLOCK_NOSLEEP();
1398b615593SMarko Zec 	VNET_FOREACH(vnet_iter) {
1408b615593SMarko Zec 		CURVNET_SET(vnet_iter);
141603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
1422104448fSAndre Oppermann 		(void) tcp_tw_2msl_scan(0);
143603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1448b615593SMarko Zec 		CURVNET_RESTORE();
1458b615593SMarko Zec 	}
1465ee847d3SRobert Watson 	VNET_LIST_RUNLOCK_NOSLEEP();
147df8bae1dSRodney W. Grimes }
148df8bae1dSRodney W. Grimes 
1497d42e30cSJonathan Lemon int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
1507d42e30cSJonathan Lemon     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
1517d42e30cSJonathan Lemon 
152df8bae1dSRodney W. Grimes int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
153f058535dSJeffrey Hsu     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
154df8bae1dSRodney W. Grimes 
155f058535dSJeffrey Hsu static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
156e79adb8eSGarrett Wollman 
157623dce13SRobert Watson static int tcp_timer_race;
158623dce13SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, timer_race, CTLFLAG_RD, &tcp_timer_race,
159623dce13SRobert Watson     0, "Count of t_inpcb races on tcp_discardcb");
160623dce13SRobert Watson 
161df8bae1dSRodney W. Grimes /*
162df8bae1dSRodney W. Grimes  * TCP timer processing.
163df8bae1dSRodney W. Grimes  */
16485d94372SRobert Watson 
16585d94372SRobert Watson void
16685d94372SRobert Watson tcp_timer_delack(void *xtp)
167df8bae1dSRodney W. Grimes {
16885d94372SRobert Watson 	struct tcpcb *tp = xtp;
16985d94372SRobert Watson 	struct inpcb *inp;
1708b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
17185d94372SRobert Watson 
17285d94372SRobert Watson 	inp = tp->t_inpcb;
17385d94372SRobert Watson 	/*
17485d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
17585d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
17685d94372SRobert Watson 	 * timers and tcp_discardcb().
17785d94372SRobert Watson 	 *
17885d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_delack: inp == NULL"));
17985d94372SRobert Watson 	 */
18085d94372SRobert Watson 	if (inp == NULL) {
18185d94372SRobert Watson 		tcp_timer_race++;
1828b615593SMarko Zec 		CURVNET_RESTORE();
18385d94372SRobert Watson 		return;
18485d94372SRobert Watson 	}
1858501a69cSRobert Watson 	INP_WLOCK(inp);
186655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_delack) ||
187655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_delack)) {
1888501a69cSRobert Watson 		INP_WUNLOCK(inp);
1898b615593SMarko Zec 		CURVNET_RESTORE();
19085d94372SRobert Watson 		return;
19185d94372SRobert Watson 	}
192e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_delack);
193655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
194655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
195655f934bSMikolaj Golub 		CURVNET_RESTORE();
196655f934bSMikolaj Golub 		return;
197655f934bSMikolaj Golub 	}
198df8bae1dSRodney W. Grimes 
1999b8b58e0SJonathan Lemon 	tp->t_flags |= TF_ACKNOW;
20078b50714SRobert Watson 	TCPSTAT_INC(tcps_delack);
2019b8b58e0SJonathan Lemon 	(void) tcp_output(tp);
2028501a69cSRobert Watson 	INP_WUNLOCK(inp);
2038b615593SMarko Zec 	CURVNET_RESTORE();
2049b8b58e0SJonathan Lemon }
2059b8b58e0SJonathan Lemon 
20685d94372SRobert Watson void
20785d94372SRobert Watson tcp_timer_2msl(void *xtp)
2089b8b58e0SJonathan Lemon {
20985d94372SRobert Watson 	struct tcpcb *tp = xtp;
21085d94372SRobert Watson 	struct inpcb *inp;
2118b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
2129b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2139b8b58e0SJonathan Lemon 	int ostate;
2149b8b58e0SJonathan Lemon 
2159b8b58e0SJonathan Lemon 	ostate = tp->t_state;
2169b8b58e0SJonathan Lemon #endif
217623dce13SRobert Watson 	/*
21885d94372SRobert Watson 	 * XXXRW: Does this actually happen?
21985d94372SRobert Watson 	 */
220603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
22185d94372SRobert Watson 	inp = tp->t_inpcb;
22285d94372SRobert Watson 	/*
22385d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
22485d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
22585d94372SRobert Watson 	 * timers and tcp_discardcb().
22685d94372SRobert Watson 	 *
22785d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_2msl: inp == NULL"));
22885d94372SRobert Watson 	 */
22985d94372SRobert Watson 	if (inp == NULL) {
23085d94372SRobert Watson 		tcp_timer_race++;
231603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2328b615593SMarko Zec 		CURVNET_RESTORE();
23385d94372SRobert Watson 		return;
23485d94372SRobert Watson 	}
2358501a69cSRobert Watson 	INP_WLOCK(inp);
23685d94372SRobert Watson 	tcp_free_sackholes(tp);
237655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_2msl) ||
238e2f2059fSMike Silbersack 	    !callout_active(&tp->t_timers->tt_2msl)) {
2398501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
240603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
2418b615593SMarko Zec 		CURVNET_RESTORE();
24285d94372SRobert Watson 		return;
24385d94372SRobert Watson 	}
244e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_2msl);
245655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
246655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
247655f934bSMikolaj Golub 		INP_INFO_WUNLOCK(&V_tcbinfo);
248655f934bSMikolaj Golub 		CURVNET_RESTORE();
249655f934bSMikolaj Golub 		return;
250655f934bSMikolaj Golub 	}
25185d94372SRobert Watson 	/*
252df8bae1dSRodney W. Grimes 	 * 2 MSL timeout in shutdown went off.  If we're closed but
253df8bae1dSRodney W. Grimes 	 * still waiting for peer to close and connection has been idle
254df8bae1dSRodney W. Grimes 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
255df8bae1dSRodney W. Grimes 	 * control block.  Otherwise, check again in a bit.
2567c72af87SMohan Srinivasan 	 *
2577c72af87SMohan Srinivasan 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
2587c72af87SMohan Srinivasan 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
2597c72af87SMohan Srinivasan 	 * Ignore fact that there were recent incoming segments.
260df8bae1dSRodney W. Grimes 	 */
2617c72af87SMohan Srinivasan 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
26285d94372SRobert Watson 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
2637c72af87SMohan Srinivasan 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
26478b50714SRobert Watson 		TCPSTAT_INC(tcps_finwait2_drops);
26585d94372SRobert Watson 		tp = tcp_close(tp);
2667c72af87SMohan Srinivasan 	} else {
267df8bae1dSRodney W. Grimes 		if (tp->t_state != TCPS_TIME_WAIT &&
2689077f387SGleb Smirnoff 		   ticks - tp->t_rcvtime <= TP_MAXIDLE(tp))
2699077f387SGleb Smirnoff 		       callout_reset_on(&tp->t_timers->tt_2msl,
2709077f387SGleb Smirnoff 			   TP_KEEPINTVL(tp), tcp_timer_2msl, tp, INP_CPU(inp));
271df8bae1dSRodney W. Grimes 	       else
27285d94372SRobert Watson 		       tp = tcp_close(tp);
2737c72af87SMohan Srinivasan        }
274df8bae1dSRodney W. Grimes 
2759b8b58e0SJonathan Lemon #ifdef TCPDEBUG
276586b4a0eSKonstantin Belousov 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
277fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
2789b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
2799b8b58e0SJonathan Lemon #endif
28085d94372SRobert Watson 	if (tp != NULL)
2818501a69cSRobert Watson 		INP_WUNLOCK(inp);
282603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
2838b615593SMarko Zec 	CURVNET_RESTORE();
2849b8b58e0SJonathan Lemon }
2859b8b58e0SJonathan Lemon 
28685d94372SRobert Watson void
28785d94372SRobert Watson tcp_timer_keep(void *xtp)
2889b8b58e0SJonathan Lemon {
28985d94372SRobert Watson 	struct tcpcb *tp = xtp;
29008517d53SMike Silbersack 	struct tcptemp *t_template;
29185d94372SRobert Watson 	struct inpcb *inp;
2928b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
2939b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2949b8b58e0SJonathan Lemon 	int ostate;
2959b8b58e0SJonathan Lemon 
2969b8b58e0SJonathan Lemon 	ostate = tp->t_state;
2979b8b58e0SJonathan Lemon #endif
298603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
29985d94372SRobert Watson 	inp = tp->t_inpcb;
30085d94372SRobert Watson 	/*
30185d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
30285d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
30385d94372SRobert Watson 	 * timers and tcp_discardcb().
30485d94372SRobert Watson 	 *
30585d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_keep: inp == NULL"));
30685d94372SRobert Watson 	 */
30785d94372SRobert Watson 	if (inp == NULL) {
30885d94372SRobert Watson 		tcp_timer_race++;
309603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3108b615593SMarko Zec 		CURVNET_RESTORE();
31185d94372SRobert Watson 		return;
31285d94372SRobert Watson 	}
3138501a69cSRobert Watson 	INP_WLOCK(inp);
314655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_keep) ||
315655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_keep)) {
3168501a69cSRobert Watson 		INP_WUNLOCK(inp);
317603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
3188b615593SMarko Zec 		CURVNET_RESTORE();
31985d94372SRobert Watson 		return;
32085d94372SRobert Watson 	}
321e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_keep);
322655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
323655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
324655f934bSMikolaj Golub 		INP_INFO_WUNLOCK(&V_tcbinfo);
325655f934bSMikolaj Golub 		CURVNET_RESTORE();
326655f934bSMikolaj Golub 		return;
327655f934bSMikolaj Golub 	}
3289b8b58e0SJonathan Lemon 	/*
3299b8b58e0SJonathan Lemon 	 * Keep-alive timer went off; send something
3309b8b58e0SJonathan Lemon 	 * or drop connection if idle for too long.
3319b8b58e0SJonathan Lemon 	 */
33278b50714SRobert Watson 	TCPSTAT_INC(tcps_keeptimeo);
3339b8b58e0SJonathan Lemon 	if (tp->t_state < TCPS_ESTABLISHED)
3349b8b58e0SJonathan Lemon 		goto dropit;
3352a074620SSam Leffler 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
3369b8b58e0SJonathan Lemon 	    tp->t_state <= TCPS_CLOSING) {
3379077f387SGleb Smirnoff 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
3389b8b58e0SJonathan Lemon 			goto dropit;
3399b8b58e0SJonathan Lemon 		/*
3409b8b58e0SJonathan Lemon 		 * Send a packet designed to force a response
3419b8b58e0SJonathan Lemon 		 * if the peer is up and reachable:
3429b8b58e0SJonathan Lemon 		 * either an ACK if the connection is still alive,
3439b8b58e0SJonathan Lemon 		 * or an RST if the peer has closed the connection
3449b8b58e0SJonathan Lemon 		 * due to timeout or reboot.
3459b8b58e0SJonathan Lemon 		 * Using sequence number tp->snd_una-1
3469b8b58e0SJonathan Lemon 		 * causes the transmitted zero-length segment
3479b8b58e0SJonathan Lemon 		 * to lie outside the receive window;
3489b8b58e0SJonathan Lemon 		 * by the protocol spec, this requires the
3499b8b58e0SJonathan Lemon 		 * correspondent TCP to respond.
3509b8b58e0SJonathan Lemon 		 */
35178b50714SRobert Watson 		TCPSTAT_INC(tcps_keepprobe);
35279909384SJonathan Lemon 		t_template = tcpip_maketemplate(inp);
35308517d53SMike Silbersack 		if (t_template) {
35408517d53SMike Silbersack 			tcp_respond(tp, t_template->tt_ipgen,
35508517d53SMike Silbersack 				    &t_template->tt_t, (struct mbuf *)NULL,
3569b8b58e0SJonathan Lemon 				    tp->rcv_nxt, tp->snd_una - 1, 0);
35753640b0eSRobert Watson 			free(t_template, M_TEMP);
35808517d53SMike Silbersack 		}
3599077f387SGleb Smirnoff 		callout_reset_on(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
3609077f387SGleb Smirnoff 		    tcp_timer_keep, tp, INP_CPU(inp));
3614cc20ab1SSeigo Tanimura 	} else
3629077f387SGleb Smirnoff 		callout_reset_on(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
3639077f387SGleb Smirnoff 		    tcp_timer_keep, tp, INP_CPU(inp));
3649b8b58e0SJonathan Lemon 
3659b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3662a074620SSam Leffler 	if (inp->inp_socket->so_options & SO_DEBUG)
367fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
3689b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
3699b8b58e0SJonathan Lemon #endif
3708501a69cSRobert Watson 	INP_WUNLOCK(inp);
371603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
3728b615593SMarko Zec 	CURVNET_RESTORE();
37385d94372SRobert Watson 	return;
3749b8b58e0SJonathan Lemon 
3759b8b58e0SJonathan Lemon dropit:
37678b50714SRobert Watson 	TCPSTAT_INC(tcps_keepdrops);
37785d94372SRobert Watson 	tp = tcp_drop(tp, ETIMEDOUT);
37885d94372SRobert Watson 
37985d94372SRobert Watson #ifdef TCPDEBUG
38085d94372SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
38185d94372SRobert Watson 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
38285d94372SRobert Watson 			  PRU_SLOWTIMO);
38385d94372SRobert Watson #endif
38485d94372SRobert Watson 	if (tp != NULL)
3858501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
386603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
3878b615593SMarko Zec 	CURVNET_RESTORE();
3889b8b58e0SJonathan Lemon }
3899b8b58e0SJonathan Lemon 
39085d94372SRobert Watson void
39185d94372SRobert Watson tcp_timer_persist(void *xtp)
3929b8b58e0SJonathan Lemon {
39385d94372SRobert Watson 	struct tcpcb *tp = xtp;
39485d94372SRobert Watson 	struct inpcb *inp;
3958b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
3969b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3979b8b58e0SJonathan Lemon 	int ostate;
3989b8b58e0SJonathan Lemon 
3999b8b58e0SJonathan Lemon 	ostate = tp->t_state;
4009b8b58e0SJonathan Lemon #endif
401603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
40285d94372SRobert Watson 	inp = tp->t_inpcb;
40385d94372SRobert Watson 	/*
40485d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
40585d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
40685d94372SRobert Watson 	 * timers and tcp_discardcb().
40785d94372SRobert Watson 	 *
40885d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_persist: inp == NULL"));
40985d94372SRobert Watson 	 */
41085d94372SRobert Watson 	if (inp == NULL) {
41185d94372SRobert Watson 		tcp_timer_race++;
412603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
4138b615593SMarko Zec 		CURVNET_RESTORE();
41485d94372SRobert Watson 		return;
41585d94372SRobert Watson 	}
4168501a69cSRobert Watson 	INP_WLOCK(inp);
417655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_persist) ||
418655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_persist)) {
4198501a69cSRobert Watson 		INP_WUNLOCK(inp);
420603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
4218b615593SMarko Zec 		CURVNET_RESTORE();
42285d94372SRobert Watson 		return;
42385d94372SRobert Watson 	}
424e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_persist);
425655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
426655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
427655f934bSMikolaj Golub 		INP_INFO_WUNLOCK(&V_tcbinfo);
428655f934bSMikolaj Golub 		CURVNET_RESTORE();
429655f934bSMikolaj Golub 		return;
430655f934bSMikolaj Golub 	}
4319b8b58e0SJonathan Lemon 	/*
4329b8b58e0SJonathan Lemon 	 * Persistance timer into zero window.
4339b8b58e0SJonathan Lemon 	 * Force a byte to be output, if possible.
4349b8b58e0SJonathan Lemon 	 */
43578b50714SRobert Watson 	TCPSTAT_INC(tcps_persisttimeo);
4369b8b58e0SJonathan Lemon 	/*
4379b8b58e0SJonathan Lemon 	 * Hack: if the peer is dead/unreachable, we do not
4389b8b58e0SJonathan Lemon 	 * time out if the window is closed.  After a full
4399b8b58e0SJonathan Lemon 	 * backoff, drop the connection if the idle time
4409b8b58e0SJonathan Lemon 	 * (no responses to probes) reaches the maximum
4419b8b58e0SJonathan Lemon 	 * backoff that we would use if retransmitting.
4429b8b58e0SJonathan Lemon 	 */
4439b8b58e0SJonathan Lemon 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
4446b0c5521SJohn Baldwin 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
4456b0c5521SJohn Baldwin 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
44678b50714SRobert Watson 		TCPSTAT_INC(tcps_persistdrop);
44785d94372SRobert Watson 		tp = tcp_drop(tp, ETIMEDOUT);
44885d94372SRobert Watson 		goto out;
4499b8b58e0SJonathan Lemon 	}
4509b8b58e0SJonathan Lemon 	tcp_setpersist(tp);
4512cdbfa66SPaul Saab 	tp->t_flags |= TF_FORCEDATA;
4529b8b58e0SJonathan Lemon 	(void) tcp_output(tp);
4532cdbfa66SPaul Saab 	tp->t_flags &= ~TF_FORCEDATA;
4549b8b58e0SJonathan Lemon 
45585d94372SRobert Watson out:
4569b8b58e0SJonathan Lemon #ifdef TCPDEBUG
457ffb761f6SGleb Smirnoff 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
458ffb761f6SGleb Smirnoff 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
4599b8b58e0SJonathan Lemon #endif
46085d94372SRobert Watson 	if (tp != NULL)
4618501a69cSRobert Watson 		INP_WUNLOCK(inp);
462603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
4638b615593SMarko Zec 	CURVNET_RESTORE();
4649b8b58e0SJonathan Lemon }
4659b8b58e0SJonathan Lemon 
46685d94372SRobert Watson void
46785d94372SRobert Watson tcp_timer_rexmt(void * xtp)
4689b8b58e0SJonathan Lemon {
46985d94372SRobert Watson 	struct tcpcb *tp = xtp;
4708b615593SMarko Zec 	CURVNET_SET(tp->t_vnet);
4719b8b58e0SJonathan Lemon 	int rexmt;
47285d94372SRobert Watson 	int headlocked;
47385d94372SRobert Watson 	struct inpcb *inp;
4749b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4759b8b58e0SJonathan Lemon 	int ostate;
4769b8b58e0SJonathan Lemon 
4779b8b58e0SJonathan Lemon 	ostate = tp->t_state;
4789b8b58e0SJonathan Lemon #endif
47987aedea4SKip Macy 	INP_INFO_RLOCK(&V_tcbinfo);
48085d94372SRobert Watson 	inp = tp->t_inpcb;
48185d94372SRobert Watson 	/*
48285d94372SRobert Watson 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
48385d94372SRobert Watson 	 * tear-down mean we need it as a work-around for races between
48485d94372SRobert Watson 	 * timers and tcp_discardcb().
48585d94372SRobert Watson 	 *
48685d94372SRobert Watson 	 * KASSERT(inp != NULL, ("tcp_timer_rexmt: inp == NULL"));
48785d94372SRobert Watson 	 */
48885d94372SRobert Watson 	if (inp == NULL) {
48985d94372SRobert Watson 		tcp_timer_race++;
49087aedea4SKip Macy 		INP_INFO_RUNLOCK(&V_tcbinfo);
4918b615593SMarko Zec 		CURVNET_RESTORE();
49285d94372SRobert Watson 		return;
49385d94372SRobert Watson 	}
4948501a69cSRobert Watson 	INP_WLOCK(inp);
495655f934bSMikolaj Golub 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
496655f934bSMikolaj Golub 	    !callout_active(&tp->t_timers->tt_rexmt)) {
4978501a69cSRobert Watson 		INP_WUNLOCK(inp);
49887aedea4SKip Macy 		INP_INFO_RUNLOCK(&V_tcbinfo);
4998b615593SMarko Zec 		CURVNET_RESTORE();
50085d94372SRobert Watson 		return;
50185d94372SRobert Watson 	}
502e2f2059fSMike Silbersack 	callout_deactivate(&tp->t_timers->tt_rexmt);
503655f934bSMikolaj Golub 	if ((inp->inp_flags & INP_DROPPED) != 0) {
504655f934bSMikolaj Golub 		INP_WUNLOCK(inp);
505655f934bSMikolaj Golub 		INP_INFO_RUNLOCK(&V_tcbinfo);
506655f934bSMikolaj Golub 		CURVNET_RESTORE();
507655f934bSMikolaj Golub 		return;
508655f934bSMikolaj Golub 	}
5096d90faf3SPaul Saab 	tcp_free_sackholes(tp);
510df8bae1dSRodney W. Grimes 	/*
511df8bae1dSRodney W. Grimes 	 * Retransmission timer went off.  Message has not
512df8bae1dSRodney W. Grimes 	 * been acked within retransmit interval.  Back off
513df8bae1dSRodney W. Grimes 	 * to a longer retransmit interval and retransmit one segment.
514df8bae1dSRodney W. Grimes 	 */
515df8bae1dSRodney W. Grimes 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
516df8bae1dSRodney W. Grimes 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
51778b50714SRobert Watson 		TCPSTAT_INC(tcps_timeoutdrop);
51887aedea4SKip Macy 		in_pcbref(inp);
51987aedea4SKip Macy 		INP_INFO_RUNLOCK(&V_tcbinfo);
52087aedea4SKip Macy 		INP_WUNLOCK(inp);
52187aedea4SKip Macy 		INP_INFO_WLOCK(&V_tcbinfo);
52287aedea4SKip Macy 		INP_WLOCK(inp);
523fa046d87SRobert Watson 		if (in_pcbrele_wlocked(inp)) {
52487aedea4SKip Macy 			INP_INFO_WUNLOCK(&V_tcbinfo);
52587aedea4SKip Macy 			CURVNET_RESTORE();
52687aedea4SKip Macy 			return;
52787aedea4SKip Macy 		}
528aa4b09c5SNavdeep Parhar 		if (inp->inp_flags & INP_DROPPED) {
529aa4b09c5SNavdeep Parhar 			INP_WUNLOCK(inp);
530aa4b09c5SNavdeep Parhar 			INP_INFO_WUNLOCK(&V_tcbinfo);
531aa4b09c5SNavdeep Parhar 			CURVNET_RESTORE();
532aa4b09c5SNavdeep Parhar 			return;
533aa4b09c5SNavdeep Parhar 		}
534aa4b09c5SNavdeep Parhar 
53585d94372SRobert Watson 		tp = tcp_drop(tp, tp->t_softerror ?
53685d94372SRobert Watson 			      tp->t_softerror : ETIMEDOUT);
53787aedea4SKip Macy 		headlocked = 1;
53885d94372SRobert Watson 		goto out;
5399b8b58e0SJonathan Lemon 	}
54087aedea4SKip Macy 	INP_INFO_RUNLOCK(&V_tcbinfo);
54185d94372SRobert Watson 	headlocked = 0;
542cf8f04f4SAndre Oppermann 	if (tp->t_state == TCPS_SYN_SENT) {
543cf8f04f4SAndre Oppermann 		/*
544cf8f04f4SAndre Oppermann 		 * If the SYN was retransmitted, indicate CWND to be
545cf8f04f4SAndre Oppermann 		 * limited to 1 segment in cc_conn_init().
546cf8f04f4SAndre Oppermann 		 */
547cf8f04f4SAndre Oppermann 		tp->snd_cwnd = 1;
548cf8f04f4SAndre Oppermann 	} else if (tp->t_rxtshift == 1) {
5499b8b58e0SJonathan Lemon 		/*
5509b8b58e0SJonathan Lemon 		 * first retransmit; record ssthresh and cwnd so they can
5519b8b58e0SJonathan Lemon 		 * be recovered if this turns out to be a "bad" retransmit.
5529b8b58e0SJonathan Lemon 		 * A retransmit is considered "bad" if an ACK for this
5539b8b58e0SJonathan Lemon 		 * segment is received within RTT/2 interval; the assumption
5549b8b58e0SJonathan Lemon 		 * here is that the ACK was already in flight.  See
5559b8b58e0SJonathan Lemon 		 * "On Estimating End-to-End Network Path Properties" by
5569b8b58e0SJonathan Lemon 		 * Allman and Paxson for more details.
5579b8b58e0SJonathan Lemon 		 */
5589b8b58e0SJonathan Lemon 		tp->snd_cwnd_prev = tp->snd_cwnd;
5599b8b58e0SJonathan Lemon 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
5609d11646dSJeffrey Hsu 		tp->snd_recover_prev = tp->snd_recover;
561dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags))
5629d11646dSJeffrey Hsu 			tp->t_flags |= TF_WASFRECOVERY;
5639d11646dSJeffrey Hsu 		else
5649d11646dSJeffrey Hsu 			tp->t_flags &= ~TF_WASFRECOVERY;
565dbc42409SLawrence Stewart 		if (IN_CONGRECOVERY(tp->t_flags))
566dbc42409SLawrence Stewart 			tp->t_flags |= TF_WASCRECOVERY;
567dbc42409SLawrence Stewart 		else
568dbc42409SLawrence Stewart 			tp->t_flags &= ~TF_WASCRECOVERY;
5699b8b58e0SJonathan Lemon 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
570672dc4aeSJohn Baldwin 		tp->t_flags |= TF_PREVVALID;
571672dc4aeSJohn Baldwin 	} else
572672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
57378b50714SRobert Watson 	TCPSTAT_INC(tcps_rexmttimeo);
5747d42e30cSJonathan Lemon 	if (tp->t_state == TCPS_SYN_SENT)
575f4748ef5SAndre Oppermann 		rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
5767d42e30cSJonathan Lemon 	else
577df8bae1dSRodney W. Grimes 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
578df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
579df8bae1dSRodney W. Grimes 		      tp->t_rttmin, TCPTV_REXMTMAX);
580df8bae1dSRodney W. Grimes 	/*
581b5224580SJohn Baldwin 	 * Disable rfc1323 if we haven't got any response to
5827ceb7783SJesper Skriver 	 * our third SYN to work-around some broken terminal servers
5837ceb7783SJesper Skriver 	 * (most of which have hopefully been retired) that have bad VJ
5847ceb7783SJesper Skriver 	 * header compression code which trashes TCP segments containing
5857ceb7783SJesper Skriver 	 * unknown-to-them TCP options.
5867ceb7783SJesper Skriver 	 */
5877ceb7783SJesper Skriver 	if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
588*c4ab59c1SAndre Oppermann 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
5897ceb7783SJesper Skriver 	/*
59097d8d152SAndre Oppermann 	 * If we backed off this far, our srtt estimate is probably bogus.
59197d8d152SAndre Oppermann 	 * Clobber it so we'll take the next rtt measurement as our srtt;
592df8bae1dSRodney W. Grimes 	 * move the current srtt into rttvar to keep the current
593df8bae1dSRodney W. Grimes 	 * retransmit times until then.
594df8bae1dSRodney W. Grimes 	 */
595df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
596fb59c426SYoshinobu Inoue #ifdef INET6
597fb59c426SYoshinobu Inoue 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
598fb59c426SYoshinobu Inoue 			in6_losing(tp->t_inpcb);
599fb59c426SYoshinobu Inoue #endif
600df8bae1dSRodney W. Grimes 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
601df8bae1dSRodney W. Grimes 		tp->t_srtt = 0;
602df8bae1dSRodney W. Grimes 	}
603df8bae1dSRodney W. Grimes 	tp->snd_nxt = tp->snd_una;
6049d11646dSJeffrey Hsu 	tp->snd_recover = tp->snd_max;
60546f58482SJonathan Lemon 	/*
60674b48c1dSAndras Olah 	 * Force a segment to be sent.
60774b48c1dSAndras Olah 	 */
60874b48c1dSAndras Olah 	tp->t_flags |= TF_ACKNOW;
60974b48c1dSAndras Olah 	/*
610df8bae1dSRodney W. Grimes 	 * If timing a segment in this window, stop the timer.
611df8bae1dSRodney W. Grimes 	 */
6129b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
613dbc42409SLawrence Stewart 
614b5af1b88SLawrence Stewart 	cc_cong_signal(tp, NULL, CC_RTO);
615dbc42409SLawrence Stewart 
616df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
617df8bae1dSRodney W. Grimes 
61885d94372SRobert Watson out:
6199b8b58e0SJonathan Lemon #ifdef TCPDEBUG
6201c53f806SRobert Watson 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
621fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
6229b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
623df8bae1dSRodney W. Grimes #endif
62485d94372SRobert Watson 	if (tp != NULL)
6258501a69cSRobert Watson 		INP_WUNLOCK(inp);
62685d94372SRobert Watson 	if (headlocked)
627603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
6288b615593SMarko Zec 	CURVNET_RESTORE();
62985d94372SRobert Watson }
63085d94372SRobert Watson 
63185d94372SRobert Watson void
63285d94372SRobert Watson tcp_timer_activate(struct tcpcb *tp, int timer_type, u_int delta)
63385d94372SRobert Watson {
63485d94372SRobert Watson 	struct callout *t_callout;
63585d94372SRobert Watson 	void *f_callout;
63687aedea4SKip Macy 	struct inpcb *inp = tp->t_inpcb;
63787aedea4SKip Macy 	int cpu = INP_CPU(inp);
63885d94372SRobert Watson 
63909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64009fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
64109fe6320SNavdeep Parhar 		return;
64209fe6320SNavdeep Parhar #endif
64309fe6320SNavdeep Parhar 
64485d94372SRobert Watson 	switch (timer_type) {
64585d94372SRobert Watson 		case TT_DELACK:
646e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
64785d94372SRobert Watson 			f_callout = tcp_timer_delack;
64885d94372SRobert Watson 			break;
64985d94372SRobert Watson 		case TT_REXMT:
650e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
65185d94372SRobert Watson 			f_callout = tcp_timer_rexmt;
65285d94372SRobert Watson 			break;
65385d94372SRobert Watson 		case TT_PERSIST:
654e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
65585d94372SRobert Watson 			f_callout = tcp_timer_persist;
65685d94372SRobert Watson 			break;
65785d94372SRobert Watson 		case TT_KEEP:
658e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
65985d94372SRobert Watson 			f_callout = tcp_timer_keep;
66085d94372SRobert Watson 			break;
66185d94372SRobert Watson 		case TT_2MSL:
662e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
66385d94372SRobert Watson 			f_callout = tcp_timer_2msl;
66485d94372SRobert Watson 			break;
66585d94372SRobert Watson 		default:
66685d94372SRobert Watson 			panic("bad timer_type");
66785d94372SRobert Watson 		}
66885d94372SRobert Watson 	if (delta == 0) {
66985d94372SRobert Watson 		callout_stop(t_callout);
67085d94372SRobert Watson 	} else {
67187aedea4SKip Macy 		callout_reset_on(t_callout, delta, f_callout, tp, cpu);
67285d94372SRobert Watson 	}
67385d94372SRobert Watson }
67485d94372SRobert Watson 
67585d94372SRobert Watson int
67685d94372SRobert Watson tcp_timer_active(struct tcpcb *tp, int timer_type)
67785d94372SRobert Watson {
67885d94372SRobert Watson 	struct callout *t_callout;
67985d94372SRobert Watson 
68085d94372SRobert Watson 	switch (timer_type) {
68185d94372SRobert Watson 		case TT_DELACK:
682e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_delack;
68385d94372SRobert Watson 			break;
68485d94372SRobert Watson 		case TT_REXMT:
685e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_rexmt;
68685d94372SRobert Watson 			break;
68785d94372SRobert Watson 		case TT_PERSIST:
688e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_persist;
68985d94372SRobert Watson 			break;
69085d94372SRobert Watson 		case TT_KEEP:
691e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_keep;
69285d94372SRobert Watson 			break;
69385d94372SRobert Watson 		case TT_2MSL:
694e2f2059fSMike Silbersack 			t_callout = &tp->t_timers->tt_2msl;
69585d94372SRobert Watson 			break;
69685d94372SRobert Watson 		default:
69785d94372SRobert Watson 			panic("bad timer_type");
69885d94372SRobert Watson 		}
69985d94372SRobert Watson 	return callout_active(t_callout);
700df8bae1dSRodney W. Grimes }
701b8614722SMike Silbersack 
702b8614722SMike Silbersack #define	ticks_to_msecs(t)	(1000*(t) / hz)
703b8614722SMike Silbersack 
704b8614722SMike Silbersack void
705b8614722SMike Silbersack tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer, struct xtcp_timer *xtimer)
706b8614722SMike Silbersack {
707b8614722SMike Silbersack 	bzero(xtimer, sizeof(struct xtcp_timer));
708b8614722SMike Silbersack 	if (timer == NULL)
709b8614722SMike Silbersack 		return;
710b8614722SMike Silbersack 	if (callout_active(&timer->tt_delack))
711b8614722SMike Silbersack 		xtimer->tt_delack = ticks_to_msecs(timer->tt_delack.c_time - ticks);
712b8614722SMike Silbersack 	if (callout_active(&timer->tt_rexmt))
713b8614722SMike Silbersack 		xtimer->tt_rexmt = ticks_to_msecs(timer->tt_rexmt.c_time - ticks);
714b8614722SMike Silbersack 	if (callout_active(&timer->tt_persist))
715b8614722SMike Silbersack 		xtimer->tt_persist = ticks_to_msecs(timer->tt_persist.c_time - ticks);
716b8614722SMike Silbersack 	if (callout_active(&timer->tt_keep))
717b8614722SMike Silbersack 		xtimer->tt_keep = ticks_to_msecs(timer->tt_keep.c_time - ticks);
718b8614722SMike Silbersack 	if (callout_active(&timer->tt_2msl))
719b8614722SMike Silbersack 		xtimer->tt_2msl = ticks_to_msecs(timer->tt_2msl.c_time - ticks);
720b8614722SMike Silbersack 	xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);
721b8614722SMike Silbersack }
722