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