xref: /freebsd/sys/netinet/tcp_timer.c (revision bfe691b2f75de2224c7ceb304ebcdef2b42d4179)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_inet6.h"
34 #include "opt_tcpdebug.h"
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/mutex.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 
47 #include <net/route.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/in_pcb.h>
51 #include <netinet/in_systm.h>
52 #ifdef INET6
53 #include <netinet6/in6_pcb.h>
54 #endif
55 #include <netinet/ip_var.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcp_fsm.h>
58 #include <netinet/tcp_timer.h>
59 #include <netinet/tcp_var.h>
60 #include <netinet/tcpip.h>
61 #ifdef TCPDEBUG
62 #include <netinet/tcp_debug.h>
63 #endif
64 
65 int	tcp_keepinit;
66 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
67     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
68 
69 int	tcp_keepidle;
70 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
71     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
72 
73 int	tcp_keepintvl;
74 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
75     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
76 
77 int	tcp_delacktime;
78 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
79     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
80     "Time before a delayed ACK is sent");
81 
82 int	tcp_msl;
83 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
84     &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
85 
86 int	tcp_rexmit_min;
87 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
88     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
89     "Minimum Retransmission Timeout");
90 
91 int	tcp_rexmit_slop;
92 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
93     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
94     "Retransmission Timer Slop");
95 
96 static int	always_keepalive = 1;
97 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
98     &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
99 
100 int    tcp_fast_finwait2_recycle = 0;
101 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
102     &tcp_fast_finwait2_recycle, 0,
103     "Recycle closed FIN_WAIT_2 connections faster");
104 
105 int    tcp_finwait2_timeout;
106 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
107     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
108 
109 
110 static int	tcp_keepcnt = TCPTV_KEEPCNT;
111 	/* max idle probes */
112 int	tcp_maxpersistidle;
113 	/* max idle time in persist */
114 int	tcp_maxidle;
115 
116 /*
117  * Tcp protocol timeout routine called every 500 ms.
118  * Updates timestamps used for TCP
119  * causes finite state machine actions if timers expire.
120  */
121 void
122 tcp_slowtimo()
123 {
124 
125 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
126 	INP_INFO_WLOCK(&tcbinfo);
127 	(void) tcp_timer_2msl_tw(0);
128 	INP_INFO_WUNLOCK(&tcbinfo);
129 }
130 
131 int	tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
132     { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
133 
134 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
135     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
136 
137 static int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
138 
139 static int tcp_timer_race;
140 SYSCTL_INT(_net_inet_tcp, OID_AUTO, timer_race, CTLFLAG_RD, &tcp_timer_race,
141     0, "Count of t_inpcb races on tcp_discardcb");
142 
143 /*
144  * TCP timer processing.
145  */
146 
147 void
148 tcp_timer_delack(void *xtp)
149 {
150 	struct tcpcb *tp = xtp;
151 	struct inpcb *inp;
152 
153 	INP_INFO_RLOCK(&tcbinfo);
154 	inp = tp->t_inpcb;
155 	/*
156 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
157 	 * tear-down mean we need it as a work-around for races between
158 	 * timers and tcp_discardcb().
159 	 *
160 	 * KASSERT(inp != NULL, ("tcp_timer_delack: inp == NULL"));
161 	 */
162 	if (inp == NULL) {
163 		tcp_timer_race++;
164 		INP_INFO_RUNLOCK(&tcbinfo);
165 		return;
166 	}
167 	INP_LOCK(inp);
168 	INP_INFO_RUNLOCK(&tcbinfo);
169 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_delack)
170 	    || !callout_active(tp->tt_delack)) {
171 		INP_UNLOCK(inp);
172 		return;
173 	}
174 	callout_deactivate(tp->tt_delack);
175 
176 	tp->t_flags |= TF_ACKNOW;
177 	tcpstat.tcps_delack++;
178 	(void) tcp_output(tp);
179 	INP_UNLOCK(inp);
180 }
181 
182 void
183 tcp_timer_2msl(void *xtp)
184 {
185 	struct tcpcb *tp = xtp;
186 	struct inpcb *inp;
187 #ifdef TCPDEBUG
188 	int ostate;
189 
190 	ostate = tp->t_state;
191 #endif
192 	/*
193 	 * XXXRW: Does this actually happen?
194 	 */
195 	INP_INFO_WLOCK(&tcbinfo);
196 	inp = tp->t_inpcb;
197 	/*
198 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
199 	 * tear-down mean we need it as a work-around for races between
200 	 * timers and tcp_discardcb().
201 	 *
202 	 * KASSERT(inp != NULL, ("tcp_timer_2msl: inp == NULL"));
203 	 */
204 	if (inp == NULL) {
205 		tcp_timer_race++;
206 		INP_INFO_WUNLOCK(&tcbinfo);
207 		return;
208 	}
209 	INP_LOCK(inp);
210 	tcp_free_sackholes(tp);
211 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_2msl) ||
212 	    !callout_active(tp->tt_2msl)) {
213 		INP_UNLOCK(tp->t_inpcb);
214 		INP_INFO_WUNLOCK(&tcbinfo);
215 		return;
216 	}
217 	callout_deactivate(tp->tt_2msl);
218 	/*
219 	 * 2 MSL timeout in shutdown went off.  If we're closed but
220 	 * still waiting for peer to close and connection has been idle
221 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
222 	 * control block.  Otherwise, check again in a bit.
223 	 *
224 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
225 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
226 	 * Ignore fact that there were recent incoming segments.
227 	 */
228 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
229 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
230 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
231 		tcpstat.tcps_finwait2_drops++;
232 		tp = tcp_close(tp);
233 	} else {
234 		if (tp->t_state != TCPS_TIME_WAIT &&
235 		   (ticks - tp->t_rcvtime) <= tcp_maxidle)
236 		       callout_reset(tp->tt_2msl, tcp_keepintvl,
237 				     tcp_timer_2msl, tp);
238 	       else
239 		       tp = tcp_close(tp);
240        }
241 
242 #ifdef TCPDEBUG
243 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
244 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
245 			  PRU_SLOWTIMO);
246 #endif
247 	if (tp != NULL)
248 		INP_UNLOCK(inp);
249 	INP_INFO_WUNLOCK(&tcbinfo);
250 }
251 
252 /*
253  * The timed wait queue contains references to each of the TCP sessions
254  * currently in the TIME_WAIT state.  The queue pointers, including the
255  * queue pointers in each tcptw structure, are protected using the global
256  * tcbinfo lock, which must be held over queue iteration and modification.
257  */
258 static TAILQ_HEAD(, tcptw)	twq_2msl;
259 
260 void
261 tcp_timer_init(void)
262 {
263 
264 	TAILQ_INIT(&twq_2msl);
265 }
266 
267 void
268 tcp_timer_2msl_reset(struct tcptw *tw, int rearm)
269 {
270 
271 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
272 	INP_LOCK_ASSERT(tw->tw_inpcb);
273 	if (rearm)
274 		TAILQ_REMOVE(&twq_2msl, tw, tw_2msl);
275 	tw->tw_time = ticks + 2 * tcp_msl;
276 	TAILQ_INSERT_TAIL(&twq_2msl, tw, tw_2msl);
277 }
278 
279 void
280 tcp_timer_2msl_stop(struct tcptw *tw)
281 {
282 
283 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
284 	TAILQ_REMOVE(&twq_2msl, tw, tw_2msl);
285 }
286 
287 struct tcptw *
288 tcp_timer_2msl_tw(int reuse)
289 {
290 	struct tcptw *tw;
291 
292 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
293 	for (;;) {
294 		tw = TAILQ_FIRST(&twq_2msl);
295 		if (tw == NULL || (!reuse && tw->tw_time > ticks))
296 			break;
297 		INP_LOCK(tw->tw_inpcb);
298 		tcp_twclose(tw, reuse);
299 		if (reuse)
300 			return (tw);
301 	}
302 	return (NULL);
303 }
304 
305 void
306 tcp_timer_keep(void *xtp)
307 {
308 	struct tcpcb *tp = xtp;
309 	struct tcptemp *t_template;
310 	struct inpcb *inp;
311 #ifdef TCPDEBUG
312 	int ostate;
313 
314 	ostate = tp->t_state;
315 #endif
316 	INP_INFO_WLOCK(&tcbinfo);
317 	inp = tp->t_inpcb;
318 	/*
319 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
320 	 * tear-down mean we need it as a work-around for races between
321 	 * timers and tcp_discardcb().
322 	 *
323 	 * KASSERT(inp != NULL, ("tcp_timer_keep: inp == NULL"));
324 	 */
325 	if (inp == NULL) {
326 		tcp_timer_race++;
327 		INP_INFO_WUNLOCK(&tcbinfo);
328 		return;
329 	}
330 	INP_LOCK(inp);
331 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_keep)
332 	    || !callout_active(tp->tt_keep)) {
333 		INP_UNLOCK(inp);
334 		INP_INFO_WUNLOCK(&tcbinfo);
335 		return;
336 	}
337 	callout_deactivate(tp->tt_keep);
338 	/*
339 	 * Keep-alive timer went off; send something
340 	 * or drop connection if idle for too long.
341 	 */
342 	tcpstat.tcps_keeptimeo++;
343 	if (tp->t_state < TCPS_ESTABLISHED)
344 		goto dropit;
345 	if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
346 	    tp->t_state <= TCPS_CLOSING) {
347 		if ((ticks - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle)
348 			goto dropit;
349 		/*
350 		 * Send a packet designed to force a response
351 		 * if the peer is up and reachable:
352 		 * either an ACK if the connection is still alive,
353 		 * or an RST if the peer has closed the connection
354 		 * due to timeout or reboot.
355 		 * Using sequence number tp->snd_una-1
356 		 * causes the transmitted zero-length segment
357 		 * to lie outside the receive window;
358 		 * by the protocol spec, this requires the
359 		 * correspondent TCP to respond.
360 		 */
361 		tcpstat.tcps_keepprobe++;
362 		t_template = tcpip_maketemplate(inp);
363 		if (t_template) {
364 			tcp_respond(tp, t_template->tt_ipgen,
365 				    &t_template->tt_t, (struct mbuf *)NULL,
366 				    tp->rcv_nxt, tp->snd_una - 1, 0);
367 			(void) m_free(dtom(t_template));
368 		}
369 		callout_reset(tp->tt_keep, tcp_keepintvl, tcp_timer_keep, tp);
370 	} else
371 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
372 
373 #ifdef TCPDEBUG
374 	if (inp->inp_socket->so_options & SO_DEBUG)
375 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
376 			  PRU_SLOWTIMO);
377 #endif
378 	INP_UNLOCK(inp);
379 	INP_INFO_WUNLOCK(&tcbinfo);
380 	return;
381 
382 dropit:
383 	tcpstat.tcps_keepdrops++;
384 	tp = tcp_drop(tp, ETIMEDOUT);
385 
386 #ifdef TCPDEBUG
387 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
388 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
389 			  PRU_SLOWTIMO);
390 #endif
391 	if (tp != NULL)
392 		INP_UNLOCK(tp->t_inpcb);
393 	INP_INFO_WUNLOCK(&tcbinfo);
394 }
395 
396 void
397 tcp_timer_persist(void *xtp)
398 {
399 	struct tcpcb *tp = xtp;
400 	struct inpcb *inp;
401 #ifdef TCPDEBUG
402 	int ostate;
403 
404 	ostate = tp->t_state;
405 #endif
406 	INP_INFO_WLOCK(&tcbinfo);
407 	inp = tp->t_inpcb;
408 	/*
409 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
410 	 * tear-down mean we need it as a work-around for races between
411 	 * timers and tcp_discardcb().
412 	 *
413 	 * KASSERT(inp != NULL, ("tcp_timer_persist: inp == NULL"));
414 	 */
415 	if (inp == NULL) {
416 		tcp_timer_race++;
417 		INP_INFO_WUNLOCK(&tcbinfo);
418 		return;
419 	}
420 	INP_LOCK(inp);
421 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_persist)
422 	    || !callout_active(tp->tt_persist)) {
423 		INP_UNLOCK(inp);
424 		INP_INFO_WUNLOCK(&tcbinfo);
425 		return;
426 	}
427 	callout_deactivate(tp->tt_persist);
428 	/*
429 	 * Persistance timer into zero window.
430 	 * Force a byte to be output, if possible.
431 	 */
432 	tcpstat.tcps_persisttimeo++;
433 	/*
434 	 * Hack: if the peer is dead/unreachable, we do not
435 	 * time out if the window is closed.  After a full
436 	 * backoff, drop the connection if the idle time
437 	 * (no responses to probes) reaches the maximum
438 	 * backoff that we would use if retransmitting.
439 	 */
440 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
441 	    ((ticks - tp->t_rcvtime) >= tcp_maxpersistidle ||
442 	     (ticks - tp->t_rcvtime) >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
443 		tcpstat.tcps_persistdrop++;
444 		tp = tcp_drop(tp, ETIMEDOUT);
445 		goto out;
446 	}
447 	tcp_setpersist(tp);
448 	tp->t_flags |= TF_FORCEDATA;
449 	(void) tcp_output(tp);
450 	tp->t_flags &= ~TF_FORCEDATA;
451 
452 out:
453 #ifdef TCPDEBUG
454 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
455 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
456 #endif
457 	if (tp != NULL)
458 		INP_UNLOCK(inp);
459 	INP_INFO_WUNLOCK(&tcbinfo);
460 }
461 
462 void
463 tcp_timer_rexmt(void * xtp)
464 {
465 	struct tcpcb *tp = xtp;
466 	int rexmt;
467 	int headlocked;
468 	struct inpcb *inp;
469 #ifdef TCPDEBUG
470 	int ostate;
471 
472 	ostate = tp->t_state;
473 #endif
474 	INP_INFO_WLOCK(&tcbinfo);
475 	headlocked = 1;
476 	inp = tp->t_inpcb;
477 	/*
478 	 * XXXRW: While this assert is in fact correct, bugs in the tcpcb
479 	 * tear-down mean we need it as a work-around for races between
480 	 * timers and tcp_discardcb().
481 	 *
482 	 * KASSERT(inp != NULL, ("tcp_timer_rexmt: inp == NULL"));
483 	 */
484 	if (inp == NULL) {
485 		tcp_timer_race++;
486 		INP_INFO_WUNLOCK(&tcbinfo);
487 		return;
488 	}
489 	INP_LOCK(inp);
490 	if ((inp->inp_vflag & INP_DROPPED) || callout_pending(tp->tt_rexmt)
491 	    || !callout_active(tp->tt_rexmt)) {
492 		INP_UNLOCK(inp);
493 		INP_INFO_WUNLOCK(&tcbinfo);
494 		return;
495 	}
496 	callout_deactivate(tp->tt_rexmt);
497 	tcp_free_sackholes(tp);
498 	/*
499 	 * Retransmission timer went off.  Message has not
500 	 * been acked within retransmit interval.  Back off
501 	 * to a longer retransmit interval and retransmit one segment.
502 	 */
503 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
504 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
505 		tcpstat.tcps_timeoutdrop++;
506 		tp = tcp_drop(tp, tp->t_softerror ?
507 			      tp->t_softerror : ETIMEDOUT);
508 		goto out;
509 	}
510 	INP_INFO_WUNLOCK(&tcbinfo);
511 	headlocked = 0;
512 	if (tp->t_rxtshift == 1) {
513 		/*
514 		 * first retransmit; record ssthresh and cwnd so they can
515 		 * be recovered if this turns out to be a "bad" retransmit.
516 		 * A retransmit is considered "bad" if an ACK for this
517 		 * segment is received within RTT/2 interval; the assumption
518 		 * here is that the ACK was already in flight.  See
519 		 * "On Estimating End-to-End Network Path Properties" by
520 		 * Allman and Paxson for more details.
521 		 */
522 		tp->snd_cwnd_prev = tp->snd_cwnd;
523 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
524 		tp->snd_recover_prev = tp->snd_recover;
525 		if (IN_FASTRECOVERY(tp))
526 		  tp->t_flags |= TF_WASFRECOVERY;
527 		else
528 		  tp->t_flags &= ~TF_WASFRECOVERY;
529 		tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
530 	}
531 	tcpstat.tcps_rexmttimeo++;
532 	if (tp->t_state == TCPS_SYN_SENT)
533 		rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
534 	else
535 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
536 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
537 		      tp->t_rttmin, TCPTV_REXMTMAX);
538 	/*
539 	 * Disable rfc1323 if we havn't got any response to
540 	 * our third SYN to work-around some broken terminal servers
541 	 * (most of which have hopefully been retired) that have bad VJ
542 	 * header compression code which trashes TCP segments containing
543 	 * unknown-to-them TCP options.
544 	 */
545 	if ((tp->t_state == TCPS_SYN_SENT) && (tp->t_rxtshift == 3))
546 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP);
547 	/*
548 	 * If we backed off this far, our srtt estimate is probably bogus.
549 	 * Clobber it so we'll take the next rtt measurement as our srtt;
550 	 * move the current srtt into rttvar to keep the current
551 	 * retransmit times until then.
552 	 */
553 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
554 #ifdef INET6
555 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
556 			in6_losing(tp->t_inpcb);
557 		else
558 #endif
559 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
560 		tp->t_srtt = 0;
561 	}
562 	tp->snd_nxt = tp->snd_una;
563 	tp->snd_recover = tp->snd_max;
564 	/*
565 	 * Force a segment to be sent.
566 	 */
567 	tp->t_flags |= TF_ACKNOW;
568 	/*
569 	 * If timing a segment in this window, stop the timer.
570 	 */
571 	tp->t_rtttime = 0;
572 	/*
573 	 * Close the congestion window down to one segment
574 	 * (we'll open it by one segment for each ack we get).
575 	 * Since we probably have a window's worth of unacked
576 	 * data accumulated, this "slow start" keeps us from
577 	 * dumping all that data as back-to-back packets (which
578 	 * might overwhelm an intermediate gateway).
579 	 *
580 	 * There are two phases to the opening: Initially we
581 	 * open by one mss on each ack.  This makes the window
582 	 * size increase exponentially with time.  If the
583 	 * window is larger than the path can handle, this
584 	 * exponential growth results in dropped packet(s)
585 	 * almost immediately.  To get more time between
586 	 * drops but still "push" the network to take advantage
587 	 * of improving conditions, we switch from exponential
588 	 * to linear window opening at some threshhold size.
589 	 * For a threshhold, we use half the current window
590 	 * size, truncated to a multiple of the mss.
591 	 *
592 	 * (the minimum cwnd that will give us exponential
593 	 * growth is 2 mss.  We don't allow the threshhold
594 	 * to go below this.)
595 	 */
596 	{
597 		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
598 		if (win < 2)
599 			win = 2;
600 		tp->snd_cwnd = tp->t_maxseg;
601 		tp->snd_ssthresh = win * tp->t_maxseg;
602 		tp->t_dupacks = 0;
603 	}
604 	EXIT_FASTRECOVERY(tp);
605 	(void) tcp_output(tp);
606 
607 out:
608 #ifdef TCPDEBUG
609 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
610 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
611 			  PRU_SLOWTIMO);
612 #endif
613 	if (tp != NULL)
614 		INP_UNLOCK(inp);
615 	if (headlocked)
616 		INP_INFO_WUNLOCK(&tcbinfo);
617 }
618