xref: /freebsd/sys/netinet/tcp_timer.c (revision fb59c426ff89244d934aaed59855310b98f7a071)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33e79adb8eSGarrett Wollman  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
3792252381SEivind Eklund #include "opt_compat.h"
38fb59c426SYoshinobu Inoue #include "opt_inet6.h"
390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
400cc12cc5SJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
4398163b98SPoul-Henning Kamp #include <sys/kernel.h>
4498163b98SPoul-Henning Kamp #include <sys/sysctl.h>
45df8bae1dSRodney W. Grimes #include <sys/socket.h>
46df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48df8bae1dSRodney W. Grimes 
49e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
50e79adb8eSGarrett Wollman 
51df8bae1dSRodney W. Grimes #include <net/route.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes #include <netinet/in.h>
54df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
56fb59c426SYoshinobu Inoue #ifdef INET6
57fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
58fb59c426SYoshinobu Inoue #endif
59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
60df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
61df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
62df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.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 
70ccb4d0c6SJonathan Lemon static int
71ccb4d0c6SJonathan Lemon sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
72ccb4d0c6SJonathan Lemon {
73ccb4d0c6SJonathan Lemon 	int error, s, tt;
74ccb4d0c6SJonathan Lemon 
75ccb4d0c6SJonathan Lemon 	tt = *(int *)oidp->oid_arg1;
76ccb4d0c6SJonathan Lemon 	s = tt * 1000 / hz;
77ccb4d0c6SJonathan Lemon 
789fc2bcf6SJonathan Lemon 	error = sysctl_handle_int(oidp, &s, 0, req);
79ccb4d0c6SJonathan Lemon 	if (error || !req->newptr)
80ccb4d0c6SJonathan Lemon 		return (error);
81ccb4d0c6SJonathan Lemon 
82ccb4d0c6SJonathan Lemon 	tt = s * hz / 1000;
839fc2bcf6SJonathan Lemon 	if (tt < 1)
849fc2bcf6SJonathan Lemon 		return (EINVAL);
85ccb4d0c6SJonathan Lemon 
86ccb4d0c6SJonathan Lemon 	*(int *)oidp->oid_arg1 = tt;
87ccb4d0c6SJonathan Lemon         return (0);
88ccb4d0c6SJonathan Lemon }
89ccb4d0c6SJonathan Lemon 
909b8b58e0SJonathan Lemon int	tcp_keepinit;
91ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
92ccb4d0c6SJonathan Lemon     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
937b40aa32SPaul Traina 
949b8b58e0SJonathan Lemon int	tcp_keepidle;
95ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
96ccb4d0c6SJonathan Lemon     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
9798163b98SPoul-Henning Kamp 
989b8b58e0SJonathan Lemon int	tcp_keepintvl;
99ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
100ccb4d0c6SJonathan Lemon     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
10198163b98SPoul-Henning Kamp 
1029b8b58e0SJonathan Lemon int	tcp_delacktime;
103ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
104ccb4d0c6SJonathan Lemon     CTLTYPE_INT|CTLFLAG_RW, &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
105ccb4d0c6SJonathan Lemon     "Time before a delayed ACK is sent");
1069b8b58e0SJonathan Lemon 
1079b8b58e0SJonathan Lemon int	tcp_msl;
108ccb4d0c6SJonathan Lemon SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
109ccb4d0c6SJonathan Lemon     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
1109b8b58e0SJonathan Lemon 
11134be9bf3SPoul-Henning Kamp static int	always_keepalive = 0;
1123d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
1133d177f46SBill Fumerola     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
11434be9bf3SPoul-Henning Kamp 
1150312fbe9SPoul-Henning Kamp static int	tcp_keepcnt = TCPTV_KEEPCNT;
1160312fbe9SPoul-Henning Kamp 	/* max idle probes */
1179b8b58e0SJonathan Lemon int	tcp_maxpersistidle;
1180312fbe9SPoul-Henning Kamp 	/* max idle time in persist */
119df8bae1dSRodney W. Grimes int	tcp_maxidle;
120e79adb8eSGarrett Wollman 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * Tcp protocol timeout routine called every 500 ms.
1239b8b58e0SJonathan Lemon  * Updates timestamps used for TCP
124df8bae1dSRodney W. Grimes  * causes finite state machine actions if timers expire.
125df8bae1dSRodney W. Grimes  */
126df8bae1dSRodney W. Grimes void
127df8bae1dSRodney W. Grimes tcp_slowtimo()
128df8bae1dSRodney W. Grimes {
12915bd2b43SDavid Greenman 	int s;
13015bd2b43SDavid Greenman 
13115bd2b43SDavid Greenman 	s = splnet();
132df8bae1dSRodney W. Grimes 
133e79adb8eSGarrett Wollman 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
13415bd2b43SDavid Greenman 
135df8bae1dSRodney W. Grimes 	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/* increment iss */
136df8bae1dSRodney W. Grimes #ifdef TCP_COMPAT_42
137df8bae1dSRodney W. Grimes 	if ((int)tcp_iss < 0)
138e79adb8eSGarrett Wollman 		tcp_iss = TCP_ISSINCR;			/* XXX */
139df8bae1dSRodney W. Grimes #endif
140df8bae1dSRodney W. Grimes 	splx(s);
141df8bae1dSRodney W. Grimes }
142df8bae1dSRodney W. Grimes 
143df8bae1dSRodney W. Grimes /*
144df8bae1dSRodney W. Grimes  * Cancel all timers for TCP tp.
145df8bae1dSRodney W. Grimes  */
146df8bae1dSRodney W. Grimes void
147df8bae1dSRodney W. Grimes tcp_canceltimers(tp)
148df8bae1dSRodney W. Grimes 	struct tcpcb *tp;
149df8bae1dSRodney W. Grimes {
1509b8b58e0SJonathan Lemon 	callout_stop(tp->tt_2msl);
1519b8b58e0SJonathan Lemon 	callout_stop(tp->tt_persist);
1529b8b58e0SJonathan Lemon 	callout_stop(tp->tt_keep);
1539b8b58e0SJonathan Lemon 	callout_stop(tp->tt_rexmt);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
157df8bae1dSRodney W. Grimes     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
158df8bae1dSRodney W. Grimes 
1590312fbe9SPoul-Henning Kamp static int tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
160e79adb8eSGarrett Wollman 
161df8bae1dSRodney W. Grimes /*
162df8bae1dSRodney W. Grimes  * TCP timer processing.
163df8bae1dSRodney W. Grimes  */
1649b8b58e0SJonathan Lemon void
1659b8b58e0SJonathan Lemon tcp_timer_delack(xtp)
1669b8b58e0SJonathan Lemon 	void *xtp;
167df8bae1dSRodney W. Grimes {
1689b8b58e0SJonathan Lemon 	struct tcpcb *tp = xtp;
1699b8b58e0SJonathan Lemon 	int s;
170df8bae1dSRodney W. Grimes 
1719b8b58e0SJonathan Lemon 	s = splnet();
1729b8b58e0SJonathan Lemon 	if (callout_pending(tp->tt_delack)) {
1739b8b58e0SJonathan Lemon 		splx(s);
1749b8b58e0SJonathan Lemon 		return;
1759b8b58e0SJonathan Lemon 	}
1769b8b58e0SJonathan Lemon 	callout_deactivate(tp->tt_delack);
177df8bae1dSRodney W. Grimes 
1789b8b58e0SJonathan Lemon 	tp->t_flags |= TF_ACKNOW;
1799b8b58e0SJonathan Lemon 	tcpstat.tcps_delack++;
1809b8b58e0SJonathan Lemon 	(void) tcp_output(tp);
1819b8b58e0SJonathan Lemon 	splx(s);
1829b8b58e0SJonathan Lemon }
1839b8b58e0SJonathan Lemon 
1849b8b58e0SJonathan Lemon void
1859b8b58e0SJonathan Lemon tcp_timer_2msl(xtp)
1869b8b58e0SJonathan Lemon 	void *xtp;
1879b8b58e0SJonathan Lemon {
1889b8b58e0SJonathan Lemon 	struct tcpcb *tp = xtp;
1899b8b58e0SJonathan Lemon 	int s;
1909b8b58e0SJonathan Lemon #ifdef TCPDEBUG
1919b8b58e0SJonathan Lemon 	int ostate;
1929b8b58e0SJonathan Lemon 
1939b8b58e0SJonathan Lemon 	ostate = tp->t_state;
1949b8b58e0SJonathan Lemon #endif
1959b8b58e0SJonathan Lemon 	s = splnet();
1969b8b58e0SJonathan Lemon 	if (callout_pending(tp->tt_2msl)) {
1979b8b58e0SJonathan Lemon 		splx(s);
1989b8b58e0SJonathan Lemon 		return;
1999b8b58e0SJonathan Lemon 	}
2009b8b58e0SJonathan Lemon 	callout_deactivate(tp->tt_2msl);
201df8bae1dSRodney W. Grimes 	/*
202df8bae1dSRodney W. Grimes 	 * 2 MSL timeout in shutdown went off.  If we're closed but
203df8bae1dSRodney W. Grimes 	 * still waiting for peer to close and connection has been idle
204df8bae1dSRodney W. Grimes 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
205df8bae1dSRodney W. Grimes 	 * control block.  Otherwise, check again in a bit.
206df8bae1dSRodney W. Grimes 	 */
207df8bae1dSRodney W. Grimes 	if (tp->t_state != TCPS_TIME_WAIT &&
2089b8b58e0SJonathan Lemon 	    (ticks - tp->t_rcvtime) <= tcp_maxidle)
2099b8b58e0SJonathan Lemon 		callout_reset(tp->tt_2msl, tcp_keepintvl,
2109b8b58e0SJonathan Lemon 			      tcp_timer_2msl, tp);
211df8bae1dSRodney W. Grimes 	else
212df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
213df8bae1dSRodney W. Grimes 
2149b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2159b8b58e0SJonathan Lemon 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
216fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
2179b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
2189b8b58e0SJonathan Lemon #endif
2199b8b58e0SJonathan Lemon 	splx(s);
2209b8b58e0SJonathan Lemon }
2219b8b58e0SJonathan Lemon 
2229b8b58e0SJonathan Lemon void
2239b8b58e0SJonathan Lemon tcp_timer_keep(xtp)
2249b8b58e0SJonathan Lemon 	void *xtp;
2259b8b58e0SJonathan Lemon {
2269b8b58e0SJonathan Lemon 	struct tcpcb *tp = xtp;
2279b8b58e0SJonathan Lemon 	int s;
2289b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2299b8b58e0SJonathan Lemon 	int ostate;
2309b8b58e0SJonathan Lemon 
2319b8b58e0SJonathan Lemon 	ostate = tp->t_state;
2329b8b58e0SJonathan Lemon #endif
2339b8b58e0SJonathan Lemon 	s = splnet();
2349b8b58e0SJonathan Lemon 	if (callout_pending(tp->tt_keep)) {
2359b8b58e0SJonathan Lemon 		splx(s);
2369b8b58e0SJonathan Lemon 		return;
2379b8b58e0SJonathan Lemon 	}
2389b8b58e0SJonathan Lemon 	callout_deactivate(tp->tt_keep);
2399b8b58e0SJonathan Lemon 	/*
2409b8b58e0SJonathan Lemon 	 * Keep-alive timer went off; send something
2419b8b58e0SJonathan Lemon 	 * or drop connection if idle for too long.
2429b8b58e0SJonathan Lemon 	 */
2439b8b58e0SJonathan Lemon 	tcpstat.tcps_keeptimeo++;
2449b8b58e0SJonathan Lemon 	if (tp->t_state < TCPS_ESTABLISHED)
2459b8b58e0SJonathan Lemon 		goto dropit;
2469b8b58e0SJonathan Lemon 	if ((always_keepalive ||
2479b8b58e0SJonathan Lemon 	     tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
2489b8b58e0SJonathan Lemon 	    tp->t_state <= TCPS_CLOSING) {
2499b8b58e0SJonathan Lemon 		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
2509b8b58e0SJonathan Lemon 			goto dropit;
2519b8b58e0SJonathan Lemon 		/*
2529b8b58e0SJonathan Lemon 		 * Send a packet designed to force a response
2539b8b58e0SJonathan Lemon 		 * if the peer is up and reachable:
2549b8b58e0SJonathan Lemon 		 * either an ACK if the connection is still alive,
2559b8b58e0SJonathan Lemon 		 * or an RST if the peer has closed the connection
2569b8b58e0SJonathan Lemon 		 * due to timeout or reboot.
2579b8b58e0SJonathan Lemon 		 * Using sequence number tp->snd_una-1
2589b8b58e0SJonathan Lemon 		 * causes the transmitted zero-length segment
2599b8b58e0SJonathan Lemon 		 * to lie outside the receive window;
2609b8b58e0SJonathan Lemon 		 * by the protocol spec, this requires the
2619b8b58e0SJonathan Lemon 		 * correspondent TCP to respond.
2629b8b58e0SJonathan Lemon 		 */
2639b8b58e0SJonathan Lemon 		tcpstat.tcps_keepprobe++;
2649b8b58e0SJonathan Lemon #ifdef TCP_COMPAT_42
2659b8b58e0SJonathan Lemon 		/*
2669b8b58e0SJonathan Lemon 		 * The keepalive packet must have nonzero length
2679b8b58e0SJonathan Lemon 		 * to get a 4.2 host to respond.
2689b8b58e0SJonathan Lemon 		 */
269fb59c426SYoshinobu Inoue 		tcp_respond(tp, tp->t_template->tt_ipgen,
270fb59c426SYoshinobu Inoue 			    &tp->t_template->tt_t, (struct mbuf *)NULL,
2719b8b58e0SJonathan Lemon 			    tp->rcv_nxt - 1, tp->snd_una - 1, 0);
2729b8b58e0SJonathan Lemon #else
273fb59c426SYoshinobu Inoue 		tcp_respond(tp, tp->t_template->tt_ipgen,
274fb59c426SYoshinobu Inoue 			    &tp->t_template->tt_t, (struct mbuf *)NULL,
2759b8b58e0SJonathan Lemon 			    tp->rcv_nxt, tp->snd_una - 1, 0);
2769b8b58e0SJonathan Lemon #endif
2779b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
2789b8b58e0SJonathan Lemon 	} else
2799b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
2809b8b58e0SJonathan Lemon 
2819b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2829b8b58e0SJonathan Lemon 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
283fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
2849b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
2859b8b58e0SJonathan Lemon #endif
2869b8b58e0SJonathan Lemon 	splx(s);
2879b8b58e0SJonathan Lemon 	return;
2889b8b58e0SJonathan Lemon 
2899b8b58e0SJonathan Lemon dropit:
2909b8b58e0SJonathan Lemon 	tcpstat.tcps_keepdrops++;
2919b8b58e0SJonathan Lemon 	tp = tcp_drop(tp, ETIMEDOUT);
2929b8b58e0SJonathan Lemon 
2939b8b58e0SJonathan Lemon #ifdef TCPDEBUG
2949b8b58e0SJonathan Lemon 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
295fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
2969b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
2979b8b58e0SJonathan Lemon #endif
2989b8b58e0SJonathan Lemon 	splx(s);
2999b8b58e0SJonathan Lemon }
3009b8b58e0SJonathan Lemon 
3019b8b58e0SJonathan Lemon void
3029b8b58e0SJonathan Lemon tcp_timer_persist(xtp)
3039b8b58e0SJonathan Lemon 	void *xtp;
3049b8b58e0SJonathan Lemon {
3059b8b58e0SJonathan Lemon 	struct tcpcb *tp = xtp;
3069b8b58e0SJonathan Lemon 	int s;
3079b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3089b8b58e0SJonathan Lemon 	int ostate;
3099b8b58e0SJonathan Lemon 
3109b8b58e0SJonathan Lemon 	ostate = tp->t_state;
3119b8b58e0SJonathan Lemon #endif
3129b8b58e0SJonathan Lemon 	s = splnet();
3139b8b58e0SJonathan Lemon 	if (callout_pending(tp->tt_persist)) {
3149b8b58e0SJonathan Lemon 		splx(s);
3159b8b58e0SJonathan Lemon 		return;
3169b8b58e0SJonathan Lemon 	}
3179b8b58e0SJonathan Lemon 	callout_deactivate(tp->tt_persist);
3189b8b58e0SJonathan Lemon 	/*
3199b8b58e0SJonathan Lemon 	 * Persistance timer into zero window.
3209b8b58e0SJonathan Lemon 	 * Force a byte to be output, if possible.
3219b8b58e0SJonathan Lemon 	 */
3229b8b58e0SJonathan Lemon 	tcpstat.tcps_persisttimeo++;
3239b8b58e0SJonathan Lemon 	/*
3249b8b58e0SJonathan Lemon 	 * Hack: if the peer is dead/unreachable, we do not
3259b8b58e0SJonathan Lemon 	 * time out if the window is closed.  After a full
3269b8b58e0SJonathan Lemon 	 * backoff, drop the connection if the idle time
3279b8b58e0SJonathan Lemon 	 * (no responses to probes) reaches the maximum
3289b8b58e0SJonathan Lemon 	 * backoff that we would use if retransmitting.
3299b8b58e0SJonathan Lemon 	 */
3309b8b58e0SJonathan Lemon 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
3319b8b58e0SJonathan Lemon 	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
3329b8b58e0SJonathan Lemon 	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
3339b8b58e0SJonathan Lemon 		tcpstat.tcps_persistdrop++;
3349b8b58e0SJonathan Lemon 		tp = tcp_drop(tp, ETIMEDOUT);
3359b8b58e0SJonathan Lemon 		goto out;
3369b8b58e0SJonathan Lemon 	}
3379b8b58e0SJonathan Lemon 	tcp_setpersist(tp);
3389b8b58e0SJonathan Lemon 	tp->t_force = 1;
3399b8b58e0SJonathan Lemon 	(void) tcp_output(tp);
3409b8b58e0SJonathan Lemon 	tp->t_force = 0;
3419b8b58e0SJonathan Lemon 
3429b8b58e0SJonathan Lemon out:
3439b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3449b8b58e0SJonathan Lemon 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
345fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
3469b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
3479b8b58e0SJonathan Lemon #endif
3489b8b58e0SJonathan Lemon 	splx(s);
3499b8b58e0SJonathan Lemon }
3509b8b58e0SJonathan Lemon 
3519b8b58e0SJonathan Lemon void
3529b8b58e0SJonathan Lemon tcp_timer_rexmt(xtp)
3539b8b58e0SJonathan Lemon 	void *xtp;
3549b8b58e0SJonathan Lemon {
3559b8b58e0SJonathan Lemon 	struct tcpcb *tp = xtp;
3569b8b58e0SJonathan Lemon 	int s;
3579b8b58e0SJonathan Lemon 	int rexmt;
3589b8b58e0SJonathan Lemon #ifdef TCPDEBUG
3599b8b58e0SJonathan Lemon 	int ostate;
3609b8b58e0SJonathan Lemon 
3619b8b58e0SJonathan Lemon 	ostate = tp->t_state;
3629b8b58e0SJonathan Lemon #endif
3639b8b58e0SJonathan Lemon 	s = splnet();
3649b8b58e0SJonathan Lemon 	if (callout_pending(tp->tt_rexmt)) {
3659b8b58e0SJonathan Lemon 		splx(s);
3669b8b58e0SJonathan Lemon 		return;
3679b8b58e0SJonathan Lemon 	}
3689b8b58e0SJonathan Lemon 	callout_deactivate(tp->tt_rexmt);
369df8bae1dSRodney W. Grimes 	/*
370df8bae1dSRodney W. Grimes 	 * Retransmission timer went off.  Message has not
371df8bae1dSRodney W. Grimes 	 * been acked within retransmit interval.  Back off
372df8bae1dSRodney W. Grimes 	 * to a longer retransmit interval and retransmit one segment.
373df8bae1dSRodney W. Grimes 	 */
374df8bae1dSRodney W. Grimes 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
375df8bae1dSRodney W. Grimes 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
376df8bae1dSRodney W. Grimes 		tcpstat.tcps_timeoutdrop++;
377df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, tp->t_softerror ?
378df8bae1dSRodney W. Grimes 			      tp->t_softerror : ETIMEDOUT);
3799b8b58e0SJonathan Lemon 		goto out;
3809b8b58e0SJonathan Lemon 	}
3819b8b58e0SJonathan Lemon 	if (tp->t_rxtshift == 1) {
3829b8b58e0SJonathan Lemon 		/*
3839b8b58e0SJonathan Lemon 		 * first retransmit; record ssthresh and cwnd so they can
3849b8b58e0SJonathan Lemon 	 	 * be recovered if this turns out to be a "bad" retransmit.
3859b8b58e0SJonathan Lemon 		 * A retransmit is considered "bad" if an ACK for this
3869b8b58e0SJonathan Lemon 		 * segment is received within RTT/2 interval; the assumption
3879b8b58e0SJonathan Lemon 		 * here is that the ACK was already in flight.  See
3889b8b58e0SJonathan Lemon 		 * "On Estimating End-to-End Network Path Properties" by
3899b8b58e0SJonathan Lemon 		 * Allman and Paxson for more details.
3909b8b58e0SJonathan Lemon 		 */
3919b8b58e0SJonathan Lemon 		tp->snd_cwnd_prev = tp->snd_cwnd;
3929b8b58e0SJonathan Lemon 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
3939b8b58e0SJonathan Lemon 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes 	tcpstat.tcps_rexmttimeo++;
396df8bae1dSRodney W. Grimes 	rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
397df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
398df8bae1dSRodney W. Grimes 		      tp->t_rttmin, TCPTV_REXMTMAX);
399df8bae1dSRodney W. Grimes 	/*
400df8bae1dSRodney W. Grimes 	 * If losing, let the lower level know and try for
401df8bae1dSRodney W. Grimes 	 * a better route.  Also, if we backed off this far,
402df8bae1dSRodney W. Grimes 	 * our srtt estimate is probably bogus.  Clobber it
403df8bae1dSRodney W. Grimes 	 * so we'll take the next rtt measurement as our srtt;
404df8bae1dSRodney W. Grimes 	 * move the current srtt into rttvar to keep the current
405df8bae1dSRodney W. Grimes 	 * retransmit times until then.
406df8bae1dSRodney W. Grimes 	 */
407df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
408fb59c426SYoshinobu Inoue #ifdef INET6
409fb59c426SYoshinobu Inoue 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
410fb59c426SYoshinobu Inoue 			in6_losing(tp->t_inpcb);
411fb59c426SYoshinobu Inoue 		else
412fb59c426SYoshinobu Inoue #endif
413df8bae1dSRodney W. Grimes 		in_losing(tp->t_inpcb);
414df8bae1dSRodney W. Grimes 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
415df8bae1dSRodney W. Grimes 		tp->t_srtt = 0;
416df8bae1dSRodney W. Grimes 	}
417df8bae1dSRodney W. Grimes 	tp->snd_nxt = tp->snd_una;
418a0292f23SGarrett Wollman 	/*
41974b48c1dSAndras Olah 	 * Force a segment to be sent.
42074b48c1dSAndras Olah 	 */
42174b48c1dSAndras Olah 	tp->t_flags |= TF_ACKNOW;
42274b48c1dSAndras Olah 	/*
423df8bae1dSRodney W. Grimes 	 * If timing a segment in this window, stop the timer.
424df8bae1dSRodney W. Grimes 	 */
4259b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
426df8bae1dSRodney W. Grimes 	/*
427df8bae1dSRodney W. Grimes 	 * Close the congestion window down to one segment
428df8bae1dSRodney W. Grimes 	 * (we'll open it by one segment for each ack we get).
429df8bae1dSRodney W. Grimes 	 * Since we probably have a window's worth of unacked
430df8bae1dSRodney W. Grimes 	 * data accumulated, this "slow start" keeps us from
431df8bae1dSRodney W. Grimes 	 * dumping all that data as back-to-back packets (which
432df8bae1dSRodney W. Grimes 	 * might overwhelm an intermediate gateway).
433df8bae1dSRodney W. Grimes 	 *
434df8bae1dSRodney W. Grimes 	 * There are two phases to the opening: Initially we
435df8bae1dSRodney W. Grimes 	 * open by one mss on each ack.  This makes the window
436df8bae1dSRodney W. Grimes 	 * size increase exponentially with time.  If the
437df8bae1dSRodney W. Grimes 	 * window is larger than the path can handle, this
438df8bae1dSRodney W. Grimes 	 * exponential growth results in dropped packet(s)
439df8bae1dSRodney W. Grimes 	 * almost immediately.  To get more time between
440df8bae1dSRodney W. Grimes 	 * drops but still "push" the network to take advantage
441df8bae1dSRodney W. Grimes 	 * of improving conditions, we switch from exponential
442df8bae1dSRodney W. Grimes 	 * to linear window opening at some threshhold size.
443df8bae1dSRodney W. Grimes 	 * For a threshhold, we use half the current window
444df8bae1dSRodney W. Grimes 	 * size, truncated to a multiple of the mss.
445df8bae1dSRodney W. Grimes 	 *
446df8bae1dSRodney W. Grimes 	 * (the minimum cwnd that will give us exponential
447df8bae1dSRodney W. Grimes 	 * growth is 2 mss.  We don't allow the threshhold
448df8bae1dSRodney W. Grimes 	 * to go below this.)
449df8bae1dSRodney W. Grimes 	 */
450df8bae1dSRodney W. Grimes 	{
451df8bae1dSRodney W. Grimes 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
452df8bae1dSRodney W. Grimes 		if (win < 2)
453df8bae1dSRodney W. Grimes 			win = 2;
454df8bae1dSRodney W. Grimes 		tp->snd_cwnd = tp->t_maxseg;
455df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = win * tp->t_maxseg;
456df8bae1dSRodney W. Grimes 		tp->t_dupacks = 0;
457df8bae1dSRodney W. Grimes 	}
458df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
459df8bae1dSRodney W. Grimes 
4609b8b58e0SJonathan Lemon out:
4619b8b58e0SJonathan Lemon #ifdef TCPDEBUG
4629b8b58e0SJonathan Lemon 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
463fb59c426SYoshinobu Inoue 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
4649b8b58e0SJonathan Lemon 			  PRU_SLOWTIMO);
465df8bae1dSRodney W. Grimes #endif
4669b8b58e0SJonathan Lemon 	splx(s);
467df8bae1dSRodney W. Grimes }
468