xref: /freebsd/sys/netinet/tcp_timer.c (revision d30a1689f5b37e78ea189232a8b94a7011dc0dc8)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_tcpdebug.h"
40 #include "opt_rss.h"
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mbuf.h>
46 #include <sys/mutex.h>
47 #include <sys/protosw.h>
48 #include <sys/smp.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 
54 #include <net/if.h>
55 #include <net/route.h>
56 #include <net/rss_config.h>
57 #include <net/vnet.h>
58 #include <net/netisr.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/in_kdtrace.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_rss.h>
64 #include <netinet/in_systm.h>
65 #ifdef INET6
66 #include <netinet6/in6_pcb.h>
67 #endif
68 #include <netinet/ip_var.h>
69 #include <netinet/tcp.h>
70 #include <netinet/tcp_fsm.h>
71 #include <netinet/tcp_log_buf.h>
72 #include <netinet/tcp_timer.h>
73 #include <netinet/tcp_var.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/cc/cc.h>
76 #ifdef INET6
77 #include <netinet6/tcp6_var.h>
78 #endif
79 #include <netinet/tcpip.h>
80 #include <netinet/tcp_debug.h>
81 
82 int    tcp_persmin;
83 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmin,
84     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
85     &tcp_persmin, 0, sysctl_msec_to_ticks, "I",
86     "minimum persistence interval");
87 
88 int    tcp_persmax;
89 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, persmax,
90     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
91     &tcp_persmax, 0, sysctl_msec_to_ticks, "I",
92     "maximum persistence interval");
93 
94 int	tcp_keepinit;
95 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit,
96     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
97     &tcp_keepinit, 0, sysctl_msec_to_ticks, "I",
98     "time to establish connection");
99 
100 int	tcp_keepidle;
101 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle,
102     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
103     &tcp_keepidle, 0, sysctl_msec_to_ticks, "I",
104     "time before keepalive probes begin");
105 
106 int	tcp_keepintvl;
107 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl,
108     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
109     &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I",
110     "time between keepalive probes");
111 
112 int	tcp_delacktime;
113 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime,
114     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
115     &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
116     "Time before a delayed ACK is sent");
117 
118 VNET_DEFINE(int, tcp_msl);
119 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl,
120     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET,
121     &VNET_NAME(tcp_msl), 0, sysctl_msec_to_ticks, "I",
122     "Maximum segment lifetime");
123 
124 int	tcp_rexmit_initial;
125 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_initial,
126    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
127     &tcp_rexmit_initial, 0, sysctl_msec_to_ticks, "I",
128     "Initial Retransmission Timeout");
129 
130 int	tcp_rexmit_min;
131 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min,
132     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
133     &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
134     "Minimum Retransmission Timeout");
135 
136 int	tcp_rexmit_slop;
137 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop,
138     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
139     &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
140     "Retransmission Timer Slop");
141 
142 VNET_DEFINE(int, tcp_always_keepalive) = 1;
143 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_VNET|CTLFLAG_RW,
144     &VNET_NAME(tcp_always_keepalive) , 0,
145     "Assume SO_KEEPALIVE on all TCP connections");
146 
147 int    tcp_fast_finwait2_recycle = 0;
148 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
149     &tcp_fast_finwait2_recycle, 0,
150     "Recycle closed FIN_WAIT_2 connections faster");
151 
152 int    tcp_finwait2_timeout;
153 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout,
154     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
155     &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I",
156     "FIN-WAIT2 timeout");
157 
158 int	tcp_keepcnt = TCPTV_KEEPCNT;
159 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
160     "Number of keepalive probes to send");
161 
162 	/* max idle probes */
163 int	tcp_maxpersistidle;
164 
165 int	tcp_rexmit_drop_options = 0;
166 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
167     &tcp_rexmit_drop_options, 0,
168     "Drop TCP options from 3rd and later retransmitted SYN");
169 
170 int	tcp_maxunacktime = TCPTV_MAXUNACKTIME;
171 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxunacktime,
172     CTLTYPE_INT|CTLFLAG_RW | CTLFLAG_NEEDGIANT,
173     &tcp_maxunacktime, 0, sysctl_msec_to_ticks, "I",
174     "Maximum time (in ms) that a session can linger without making progress");
175 
176 VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
177 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
178     CTLFLAG_RW|CTLFLAG_VNET,
179     &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
180     "Path MTU Discovery Black Hole Detection Enabled");
181 
182 #ifdef INET
183 VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
184 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
185     CTLFLAG_RW|CTLFLAG_VNET,
186     &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
187     "Path MTU Discovery Black Hole Detection lowered MSS");
188 #endif
189 
190 #ifdef INET6
191 VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
192 SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
193     CTLFLAG_RW|CTLFLAG_VNET,
194     &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
195     "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
196 #endif
197 
198 #ifdef	RSS
199 static int	per_cpu_timers = 1;
200 #else
201 static int	per_cpu_timers = 0;
202 #endif
203 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
204     &per_cpu_timers , 0, "run tcp timers on all cpus");
205 
206 /*
207  * Map the given inp to a CPU id.
208  *
209  * This queries RSS if it's compiled in, else it defaults to the current
210  * CPU ID.
211  */
212 inline int
213 inp_to_cpuid(struct inpcb *inp)
214 {
215 	u_int cpuid;
216 
217 	if (per_cpu_timers) {
218 #ifdef	RSS
219 		cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
220 		if (cpuid == NETISR_CPUID_NONE)
221 			return (curcpu);	/* XXX */
222 		else
223 			return (cpuid);
224 #endif
225 		/*
226 		 * We don't have a flowid -> cpuid mapping, so cheat and
227 		 * just map unknown cpuids to curcpu.  Not the best, but
228 		 * apparently better than defaulting to swi 0.
229 		 */
230 		cpuid = inp->inp_flowid % (mp_maxid + 1);
231 		if (! CPU_ABSENT(cpuid))
232 			return (cpuid);
233 		return (curcpu);
234 	} else {
235 		return (0);
236 	}
237 }
238 
239 /*
240  * Legacy TCP global callout routine called every 500 ms.
241  * Used to cleanup timewait states, which lack their own callouts.
242  */
243 static struct callout tcpslow_callout;
244 static void
245 tcp_slowtimo(void *arg __unused)
246 {
247 	struct epoch_tracker et;
248 	VNET_ITERATOR_DECL(vnet_iter);
249 
250 	NET_EPOCH_ENTER(et);
251 	VNET_LIST_RLOCK_NOSLEEP();
252 	VNET_FOREACH(vnet_iter) {
253 		CURVNET_SET(vnet_iter);
254 		(void) tcp_tw_2msl_scan(0);
255 		CURVNET_RESTORE();
256 	}
257 	VNET_LIST_RUNLOCK_NOSLEEP();
258 	NET_EPOCH_EXIT(et);
259 
260 	callout_reset_sbt(&tcpslow_callout, SBT_1MS * 500, SBT_1MS * 10,
261 	    tcp_slowtimo, NULL, 0);
262 }
263 
264 static void
265 tcp_slowtimo_init(void *arg __unused)
266 {
267 
268         callout_init(&tcpslow_callout, 1);
269 	callout_reset_sbt(&tcpslow_callout, SBT_1MS * 500, SBT_1MS * 10,
270 	    tcp_slowtimo, NULL, 0);
271 }
272 SYSINIT(tcp_timer, SI_SUB_VNET_DONE, SI_ORDER_ANY, tcp_slowtimo_init, NULL);
273 
274 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
275     { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
276 
277 int tcp_totbackoff = 2559;	/* sum of tcp_backoff[] */
278 
279 /*
280  * TCP timer processing.
281  */
282 
283 void
284 tcp_timer_delack(void *xtp)
285 {
286 	struct epoch_tracker et;
287 	struct tcpcb *tp = xtp;
288 	struct inpcb *inp;
289 	CURVNET_SET(tp->t_vnet);
290 
291 	inp = tp->t_inpcb;
292 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
293 	INP_WLOCK(inp);
294 	if (callout_pending(&tp->t_timers->tt_delack) ||
295 	    !callout_active(&tp->t_timers->tt_delack)) {
296 		INP_WUNLOCK(inp);
297 		CURVNET_RESTORE();
298 		return;
299 	}
300 	callout_deactivate(&tp->t_timers->tt_delack);
301 	if ((inp->inp_flags & INP_DROPPED) != 0) {
302 		INP_WUNLOCK(inp);
303 		CURVNET_RESTORE();
304 		return;
305 	}
306 	tp->t_flags |= TF_ACKNOW;
307 	TCPSTAT_INC(tcps_delack);
308 	NET_EPOCH_ENTER(et);
309 	(void) tcp_output_unlock(tp);
310 	NET_EPOCH_EXIT(et);
311 	CURVNET_RESTORE();
312 }
313 
314 void
315 tcp_inpinfo_lock_del(struct inpcb *inp, struct tcpcb *tp)
316 {
317 	if (inp && tp != NULL)
318 		INP_WUNLOCK(inp);
319 }
320 
321 void
322 tcp_timer_2msl(void *xtp)
323 {
324 	struct tcpcb *tp = xtp;
325 	struct inpcb *inp;
326 	struct epoch_tracker et;
327 	CURVNET_SET(tp->t_vnet);
328 #ifdef TCPDEBUG
329 	int ostate;
330 
331 	ostate = tp->t_state;
332 #endif
333 	inp = tp->t_inpcb;
334 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
335 	INP_WLOCK(inp);
336 	tcp_log_end_status(tp, TCP_EI_STATUS_2MSL);
337 	tcp_free_sackholes(tp);
338 	if (callout_pending(&tp->t_timers->tt_2msl) ||
339 	    !callout_active(&tp->t_timers->tt_2msl)) {
340 		INP_WUNLOCK(tp->t_inpcb);
341 		CURVNET_RESTORE();
342 		return;
343 	}
344 	callout_deactivate(&tp->t_timers->tt_2msl);
345 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
346 		INP_WUNLOCK(inp);
347 		CURVNET_RESTORE();
348 		return;
349 	}
350 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
351 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
352 	/*
353 	 * 2 MSL timeout in shutdown went off.  If we're closed but
354 	 * still waiting for peer to close and connection has been idle
355 	 * too long delete connection control block.  Otherwise, check
356 	 * again in a bit.
357 	 *
358 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
359 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
360 	 * Ignore fact that there were recent incoming segments.
361 	 */
362 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
363 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
364 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
365 		TCPSTAT_INC(tcps_finwait2_drops);
366 		NET_EPOCH_ENTER(et);
367 		tp = tcp_close(tp);
368 		NET_EPOCH_EXIT(et);
369 		tcp_inpinfo_lock_del(inp, tp);
370 		goto out;
371 	} else {
372 		if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
373 			callout_reset(&tp->t_timers->tt_2msl,
374 				      TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
375 		} else {
376 			NET_EPOCH_ENTER(et);
377 			tp = tcp_close(tp);
378 			NET_EPOCH_EXIT(et);
379 			tcp_inpinfo_lock_del(inp, tp);
380 			goto out;
381 		}
382 	}
383 
384 #ifdef TCPDEBUG
385 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
386 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
387 			  PRU_SLOWTIMO);
388 #endif
389 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
390 
391 	if (tp != NULL)
392 		INP_WUNLOCK(inp);
393 out:
394 	CURVNET_RESTORE();
395 }
396 
397 void
398 tcp_timer_keep(void *xtp)
399 {
400 	struct tcpcb *tp = xtp;
401 	struct tcptemp *t_template;
402 	struct inpcb *inp;
403 	struct epoch_tracker et;
404 	CURVNET_SET(tp->t_vnet);
405 #ifdef TCPDEBUG
406 	int ostate;
407 
408 	ostate = tp->t_state;
409 #endif
410 	inp = tp->t_inpcb;
411 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
412 	INP_WLOCK(inp);
413 	if (callout_pending(&tp->t_timers->tt_keep) ||
414 	    !callout_active(&tp->t_timers->tt_keep)) {
415 		INP_WUNLOCK(inp);
416 		CURVNET_RESTORE();
417 		return;
418 	}
419 	callout_deactivate(&tp->t_timers->tt_keep);
420 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
421 		INP_WUNLOCK(inp);
422 		CURVNET_RESTORE();
423 		return;
424 	}
425 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
426 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
427 
428 	/*
429 	 * Because we don't regularly reset the keepalive callout in
430 	 * the ESTABLISHED state, it may be that we don't actually need
431 	 * to send a keepalive yet. If that occurs, schedule another
432 	 * call for the next time the keepalive timer might expire.
433 	 */
434 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
435 		u_int idletime;
436 
437 		idletime = ticks - tp->t_rcvtime;
438 		if (idletime < TP_KEEPIDLE(tp)) {
439 			callout_reset(&tp->t_timers->tt_keep,
440 			    TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp);
441 			INP_WUNLOCK(inp);
442 			CURVNET_RESTORE();
443 			return;
444 		}
445 	}
446 
447 	/*
448 	 * Keep-alive timer went off; send something
449 	 * or drop connection if idle for too long.
450 	 */
451 	TCPSTAT_INC(tcps_keeptimeo);
452 	if (tp->t_state < TCPS_ESTABLISHED)
453 		goto dropit;
454 	if ((V_tcp_always_keepalive ||
455 	    inp->inp_socket->so_options & SO_KEEPALIVE) &&
456 	    tp->t_state <= TCPS_CLOSING) {
457 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
458 			goto dropit;
459 		/*
460 		 * Send a packet designed to force a response
461 		 * if the peer is up and reachable:
462 		 * either an ACK if the connection is still alive,
463 		 * or an RST if the peer has closed the connection
464 		 * due to timeout or reboot.
465 		 * Using sequence number tp->snd_una-1
466 		 * causes the transmitted zero-length segment
467 		 * to lie outside the receive window;
468 		 * by the protocol spec, this requires the
469 		 * correspondent TCP to respond.
470 		 */
471 		TCPSTAT_INC(tcps_keepprobe);
472 		t_template = tcpip_maketemplate(inp);
473 		if (t_template) {
474 			NET_EPOCH_ENTER(et);
475 			tcp_respond(tp, t_template->tt_ipgen,
476 				    &t_template->tt_t, (struct mbuf *)NULL,
477 				    tp->rcv_nxt, tp->snd_una - 1, 0);
478 			NET_EPOCH_EXIT(et);
479 			free(t_template, M_TEMP);
480 		}
481 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
482 			      tcp_timer_keep, tp);
483 	} else
484 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
485 			      tcp_timer_keep, tp);
486 
487 #ifdef TCPDEBUG
488 	if (inp->inp_socket->so_options & SO_DEBUG)
489 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
490 			  PRU_SLOWTIMO);
491 #endif
492 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
493 	INP_WUNLOCK(inp);
494 	CURVNET_RESTORE();
495 	return;
496 
497 dropit:
498 	TCPSTAT_INC(tcps_keepdrops);
499 	NET_EPOCH_ENTER(et);
500 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
501 	tp = tcp_drop(tp, ETIMEDOUT);
502 
503 #ifdef TCPDEBUG
504 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
505 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
506 			  PRU_SLOWTIMO);
507 #endif
508 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
509 	NET_EPOCH_EXIT(et);
510 	tcp_inpinfo_lock_del(inp, tp);
511 	CURVNET_RESTORE();
512 }
513 
514 /*
515  * Has this session exceeded the maximum time without seeing a substantive
516  * acknowledgement? If so, return true; otherwise false.
517  */
518 static bool
519 tcp_maxunacktime_check(struct tcpcb *tp)
520 {
521 
522 	/* Are we tracking this timer for this session? */
523 	if (TP_MAXUNACKTIME(tp) == 0)
524 		return false;
525 
526 	/* Do we have a current measurement. */
527 	if (tp->t_acktime == 0)
528 		return false;
529 
530 	/* Are we within the acceptable range? */
531 	if (TSTMP_GT(TP_MAXUNACKTIME(tp) + tp->t_acktime, (u_int)ticks))
532 		return false;
533 
534 	/* We exceeded the timer. */
535 	TCPSTAT_INC(tcps_progdrops);
536 	return true;
537 }
538 
539 void
540 tcp_timer_persist(void *xtp)
541 {
542 	struct tcpcb *tp = xtp;
543 	struct inpcb *inp;
544 	struct epoch_tracker et;
545 	bool progdrop;
546 	int outrv;
547 	CURVNET_SET(tp->t_vnet);
548 #ifdef TCPDEBUG
549 	int ostate;
550 
551 	ostate = tp->t_state;
552 #endif
553 	inp = tp->t_inpcb;
554 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
555 	INP_WLOCK(inp);
556 	if (callout_pending(&tp->t_timers->tt_persist) ||
557 	    !callout_active(&tp->t_timers->tt_persist)) {
558 		INP_WUNLOCK(inp);
559 		CURVNET_RESTORE();
560 		return;
561 	}
562 	callout_deactivate(&tp->t_timers->tt_persist);
563 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
564 		INP_WUNLOCK(inp);
565 		CURVNET_RESTORE();
566 		return;
567 	}
568 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
569 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
570 	/*
571 	 * Persistence timer into zero window.
572 	 * Force a byte to be output, if possible.
573 	 */
574 	TCPSTAT_INC(tcps_persisttimeo);
575 	/*
576 	 * Hack: if the peer is dead/unreachable, we do not
577 	 * time out if the window is closed.  After a full
578 	 * backoff, drop the connection if the idle time
579 	 * (no responses to probes) reaches the maximum
580 	 * backoff that we would use if retransmitting.
581 	 * Also, drop the connection if we haven't been making
582 	 * progress.
583 	 */
584 	progdrop = tcp_maxunacktime_check(tp);
585 	if (progdrop || (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
586 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
587 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff))) {
588 		if (!progdrop)
589 			TCPSTAT_INC(tcps_persistdrop);
590 		NET_EPOCH_ENTER(et);
591 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
592 		tp = tcp_drop(tp, ETIMEDOUT);
593 		NET_EPOCH_EXIT(et);
594 		tcp_inpinfo_lock_del(inp, tp);
595 		goto out;
596 	}
597 	/*
598 	 * If the user has closed the socket then drop a persisting
599 	 * connection after a much reduced timeout.
600 	 */
601 	if (tp->t_state > TCPS_CLOSE_WAIT &&
602 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
603 		TCPSTAT_INC(tcps_persistdrop);
604 		NET_EPOCH_ENTER(et);
605 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
606 		tp = tcp_drop(tp, ETIMEDOUT);
607 		NET_EPOCH_EXIT(et);
608 		tcp_inpinfo_lock_del(inp, tp);
609 		goto out;
610 	}
611 	tcp_setpersist(tp);
612 	tp->t_flags |= TF_FORCEDATA;
613 	NET_EPOCH_ENTER(et);
614 	outrv = tcp_output_nodrop(tp);
615 	tp->t_flags &= ~TF_FORCEDATA;
616 
617 #ifdef TCPDEBUG
618 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
619 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
620 #endif
621 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
622 	(void) tcp_unlock_or_drop(tp, outrv);
623 	NET_EPOCH_EXIT(et);
624 out:
625 	CURVNET_RESTORE();
626 }
627 
628 void
629 tcp_timer_rexmt(void * xtp)
630 {
631 	struct tcpcb *tp = xtp;
632 	CURVNET_SET(tp->t_vnet);
633 	int rexmt, outrv;
634 	struct inpcb *inp;
635 	struct epoch_tracker et;
636 	bool isipv6;
637 #ifdef TCPDEBUG
638 	int ostate;
639 
640 	ostate = tp->t_state;
641 #endif
642 	inp = tp->t_inpcb;
643 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
644 	INP_WLOCK(inp);
645 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
646 	    !callout_active(&tp->t_timers->tt_rexmt)) {
647 		INP_WUNLOCK(inp);
648 		CURVNET_RESTORE();
649 		return;
650 	}
651 	callout_deactivate(&tp->t_timers->tt_rexmt);
652 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
653 		INP_WUNLOCK(inp);
654 		CURVNET_RESTORE();
655 		return;
656 	}
657 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
658 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
659 	tcp_free_sackholes(tp);
660 	TCP_LOG_EVENT(tp, NULL, NULL, NULL, TCP_LOG_RTO, 0, 0, NULL, false);
661 	if (tp->t_fb->tfb_tcp_rexmit_tmr) {
662 		/* The stack has a timer action too. */
663 		(*tp->t_fb->tfb_tcp_rexmit_tmr)(tp);
664 	}
665 	/*
666 	 * Retransmission timer went off.  Message has not
667 	 * been acked within retransmit interval.  Back off
668 	 * to a longer retransmit interval and retransmit one segment.
669 	 *
670 	 * If we've either exceeded the maximum number of retransmissions,
671 	 * or we've gone long enough without making progress, then drop
672 	 * the session.
673 	 */
674 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT || tcp_maxunacktime_check(tp)) {
675 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT)
676 			TCPSTAT_INC(tcps_timeoutdrop);
677 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
678 		NET_EPOCH_ENTER(et);
679 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
680 		tp = tcp_drop(tp, ETIMEDOUT);
681 		NET_EPOCH_EXIT(et);
682 		tcp_inpinfo_lock_del(inp, tp);
683 		goto out;
684 	}
685 	if (tp->t_state == TCPS_SYN_SENT) {
686 		/*
687 		 * If the SYN was retransmitted, indicate CWND to be
688 		 * limited to 1 segment in cc_conn_init().
689 		 */
690 		tp->snd_cwnd = 1;
691 	} else if (tp->t_rxtshift == 1) {
692 		/*
693 		 * first retransmit; record ssthresh and cwnd so they can
694 		 * be recovered if this turns out to be a "bad" retransmit.
695 		 * A retransmit is considered "bad" if an ACK for this
696 		 * segment is received within RTT/2 interval; the assumption
697 		 * here is that the ACK was already in flight.  See
698 		 * "On Estimating End-to-End Network Path Properties" by
699 		 * Allman and Paxson for more details.
700 		 */
701 		tp->snd_cwnd_prev = tp->snd_cwnd;
702 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
703 		tp->snd_recover_prev = tp->snd_recover;
704 		if (IN_FASTRECOVERY(tp->t_flags))
705 			tp->t_flags |= TF_WASFRECOVERY;
706 		else
707 			tp->t_flags &= ~TF_WASFRECOVERY;
708 		if (IN_CONGRECOVERY(tp->t_flags))
709 			tp->t_flags |= TF_WASCRECOVERY;
710 		else
711 			tp->t_flags &= ~TF_WASCRECOVERY;
712 		if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
713 			tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
714 		/* In the event that we've negotiated timestamps
715 		 * badrxtwin will be set to the value that we set
716 		 * the retransmitted packet's to_tsval to by tcp_output
717 		 */
718 		tp->t_flags |= TF_PREVVALID;
719 	} else
720 		tp->t_flags &= ~TF_PREVVALID;
721 	TCPSTAT_INC(tcps_rexmttimeo);
722 	if ((tp->t_state == TCPS_SYN_SENT) ||
723 	    (tp->t_state == TCPS_SYN_RECEIVED))
724 		rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift];
725 	else
726 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
727 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
728 		      tp->t_rttmin, TCPTV_REXMTMAX);
729 
730 	/*
731 	 * We enter the path for PLMTUD if connection is established or, if
732 	 * connection is FIN_WAIT_1 status, reason for the last is that if
733 	 * amount of data we send is very small, we could send it in couple of
734 	 * packets and process straight to FIN. In that case we won't catch
735 	 * ESTABLISHED state.
736 	 */
737 #ifdef INET6
738 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
739 #else
740 	isipv6 = false;
741 #endif
742 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
743 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
744 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
745 	    ((tp->t_state == TCPS_ESTABLISHED) ||
746 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
747 		if (tp->t_rxtshift == 1) {
748 			/*
749 			 * We enter blackhole detection after the first
750 			 * unsuccessful timer based retransmission.
751 			 * Then we reduce up to two times the MSS, each
752 			 * candidate giving two tries of retransmissions.
753 			 * But we give a candidate only two tries, if it
754 			 * actually reduces the MSS.
755 			 */
756 			tp->t_blackhole_enter = 2;
757 			tp->t_blackhole_exit = tp->t_blackhole_enter;
758 			if (isipv6) {
759 #ifdef INET6
760 				if (tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss)
761 					tp->t_blackhole_exit += 2;
762 				if (tp->t_maxseg > V_tcp_v6mssdflt &&
763 				    V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt)
764 					tp->t_blackhole_exit += 2;
765 #endif
766 			} else {
767 #ifdef INET
768 				if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss)
769 					tp->t_blackhole_exit += 2;
770 				if (tp->t_maxseg > V_tcp_mssdflt &&
771 				    V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt)
772 					tp->t_blackhole_exit += 2;
773 #endif
774 			}
775 		}
776 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
777 		    (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
778 		    (tp->t_rxtshift >= tp->t_blackhole_enter &&
779 		    tp->t_rxtshift < tp->t_blackhole_exit &&
780 		    (tp->t_rxtshift - tp->t_blackhole_enter) % 2 == 0)) {
781 			/*
782 			 * Enter Path MTU Black-hole Detection mechanism:
783 			 * - Disable Path MTU Discovery (IP "DF" bit).
784 			 * - Reduce MTU to lower value than what we
785 			 *   negotiated with peer.
786 			 */
787 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
788 				/* Record that we may have found a black hole. */
789 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
790 				/* Keep track of previous MSS. */
791 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
792 			}
793 
794 			/*
795 			 * Reduce the MSS to blackhole value or to the default
796 			 * in an attempt to retransmit.
797 			 */
798 #ifdef INET6
799 			if (isipv6 &&
800 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss &&
801 			    V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt) {
802 				/* Use the sysctl tuneable blackhole MSS. */
803 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
804 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
805 			} else if (isipv6) {
806 				/* Use the default MSS. */
807 				tp->t_maxseg = V_tcp_v6mssdflt;
808 				/*
809 				 * Disable Path MTU Discovery when we switch to
810 				 * minmss.
811 				 */
812 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
813 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
814 			}
815 #endif
816 #if defined(INET6) && defined(INET)
817 			else
818 #endif
819 #ifdef INET
820 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss &&
821 			    V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt) {
822 				/* Use the sysctl tuneable blackhole MSS. */
823 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
824 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
825 			} else {
826 				/* Use the default MSS. */
827 				tp->t_maxseg = V_tcp_mssdflt;
828 				/*
829 				 * Disable Path MTU Discovery when we switch to
830 				 * minmss.
831 				 */
832 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
833 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
834 			}
835 #endif
836 			/*
837 			 * Reset the slow-start flight size
838 			 * as it may depend on the new MSS.
839 			 */
840 			if (CC_ALGO(tp)->conn_init != NULL)
841 				CC_ALGO(tp)->conn_init(tp->ccv);
842 		} else {
843 			/*
844 			 * If further retransmissions are still unsuccessful
845 			 * with a lowered MTU, maybe this isn't a blackhole and
846 			 * we restore the previous MSS and blackhole detection
847 			 * flags.
848 			 */
849 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
850 			    (tp->t_rxtshift >= tp->t_blackhole_exit)) {
851 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
852 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
853 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
854 				TCPSTAT_INC(tcps_pmtud_blackhole_failed);
855 				/*
856 				 * Reset the slow-start flight size as it
857 				 * may depend on the new MSS.
858 				 */
859 				if (CC_ALGO(tp)->conn_init != NULL)
860 					CC_ALGO(tp)->conn_init(tp->ccv);
861 			}
862 		}
863 	}
864 
865 	/*
866 	 * Disable RFC1323 and SACK if we haven't got any response to
867 	 * our third SYN to work-around some broken terminal servers
868 	 * (most of which have hopefully been retired) that have bad VJ
869 	 * header compression code which trashes TCP segments containing
870 	 * unknown-to-them TCP options.
871 	 */
872 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
873 	    (tp->t_rxtshift == 3))
874 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
875 	/*
876 	 * If we backed off this far, notify the L3 protocol that we're having
877 	 * connection problems.
878 	 */
879 	if (tp->t_rxtshift > TCP_RTT_INVALIDATE) {
880 #ifdef INET6
881 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
882 			in6_losing(tp->t_inpcb);
883 		else
884 #endif
885 			in_losing(tp->t_inpcb);
886 	}
887 	tp->snd_nxt = tp->snd_una;
888 	tp->snd_recover = tp->snd_max;
889 	/*
890 	 * Force a segment to be sent.
891 	 */
892 	tp->t_flags |= TF_ACKNOW;
893 	/*
894 	 * If timing a segment in this window, stop the timer.
895 	 */
896 	tp->t_rtttime = 0;
897 
898 	cc_cong_signal(tp, NULL, CC_RTO);
899 	NET_EPOCH_ENTER(et);
900 	outrv = tcp_output_nodrop(tp);
901 #ifdef TCPDEBUG
902 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
903 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
904 			  PRU_SLOWTIMO);
905 #endif
906 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
907 	(void) tcp_unlock_or_drop(tp, outrv);
908 	NET_EPOCH_EXIT(et);
909 out:
910 	CURVNET_RESTORE();
911 }
912 
913 void
914 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
915 {
916 	struct callout *t_callout;
917 	callout_func_t *f_callout;
918 	struct inpcb *inp = tp->t_inpcb;
919 	int cpu = inp_to_cpuid(inp);
920 
921 #ifdef TCP_OFFLOAD
922 	if (tp->t_flags & TF_TOE)
923 		return;
924 #endif
925 
926 	if (tp->t_timers->tt_flags & TT_STOPPED)
927 		return;
928 
929 	switch (timer_type) {
930 		case TT_DELACK:
931 			t_callout = &tp->t_timers->tt_delack;
932 			f_callout = tcp_timer_delack;
933 			break;
934 		case TT_REXMT:
935 			t_callout = &tp->t_timers->tt_rexmt;
936 			f_callout = tcp_timer_rexmt;
937 			break;
938 		case TT_PERSIST:
939 			t_callout = &tp->t_timers->tt_persist;
940 			f_callout = tcp_timer_persist;
941 			break;
942 		case TT_KEEP:
943 			t_callout = &tp->t_timers->tt_keep;
944 			f_callout = tcp_timer_keep;
945 			break;
946 		case TT_2MSL:
947 			t_callout = &tp->t_timers->tt_2msl;
948 			f_callout = tcp_timer_2msl;
949 			break;
950 		default:
951 			if (tp->t_fb->tfb_tcp_timer_activate) {
952 				tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
953 				return;
954 			}
955 			panic("tp %p bad timer_type %#x", tp, timer_type);
956 		}
957 	if (delta == 0) {
958 		callout_stop(t_callout);
959 	} else {
960 		callout_reset_on(t_callout, delta, f_callout, tp, cpu);
961 	}
962 }
963 
964 int
965 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
966 {
967 	struct callout *t_callout;
968 
969 	switch (timer_type) {
970 		case TT_DELACK:
971 			t_callout = &tp->t_timers->tt_delack;
972 			break;
973 		case TT_REXMT:
974 			t_callout = &tp->t_timers->tt_rexmt;
975 			break;
976 		case TT_PERSIST:
977 			t_callout = &tp->t_timers->tt_persist;
978 			break;
979 		case TT_KEEP:
980 			t_callout = &tp->t_timers->tt_keep;
981 			break;
982 		case TT_2MSL:
983 			t_callout = &tp->t_timers->tt_2msl;
984 			break;
985 		default:
986 			if (tp->t_fb->tfb_tcp_timer_active) {
987 				return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
988 			}
989 			panic("tp %p bad timer_type %#x", tp, timer_type);
990 		}
991 	return callout_active(t_callout);
992 }
993 
994 /*
995  * Stop the timer from running, and apply a flag
996  * against the timer_flags that will force the
997  * timer never to run. The flag is needed to assure
998  * a race does not leave it running and cause
999  * the timer to possibly restart itself (keep and persist
1000  * especially do this).
1001  */
1002 int
1003 tcp_timer_suspend(struct tcpcb *tp, uint32_t timer_type)
1004 {
1005 	struct callout *t_callout;
1006 	uint32_t t_flags;
1007 
1008 	switch (timer_type) {
1009 		case TT_DELACK:
1010 			t_flags = TT_DELACK_SUS;
1011 			t_callout = &tp->t_timers->tt_delack;
1012 			break;
1013 		case TT_REXMT:
1014 			t_flags = TT_REXMT_SUS;
1015 			t_callout = &tp->t_timers->tt_rexmt;
1016 			break;
1017 		case TT_PERSIST:
1018 			t_flags = TT_PERSIST_SUS;
1019 			t_callout = &tp->t_timers->tt_persist;
1020 			break;
1021 		case TT_KEEP:
1022 			t_flags = TT_KEEP_SUS;
1023 			t_callout = &tp->t_timers->tt_keep;
1024 			break;
1025 		case TT_2MSL:
1026 			t_flags = TT_2MSL_SUS;
1027 			t_callout = &tp->t_timers->tt_2msl;
1028 			break;
1029 		default:
1030 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
1031 	}
1032 	tp->t_timers->tt_flags |= t_flags;
1033 	return (callout_stop(t_callout));
1034 }
1035 
1036 void
1037 tcp_timers_unsuspend(struct tcpcb *tp, uint32_t timer_type)
1038 {
1039 	switch (timer_type) {
1040 		case TT_DELACK:
1041 			if (tp->t_timers->tt_flags & TT_DELACK_SUS) {
1042 				tp->t_timers->tt_flags &= ~TT_DELACK_SUS;
1043 				if (tp->t_flags & TF_DELACK) {
1044 					/* Delayed ack timer should be up activate a timer */
1045 					tp->t_flags &= ~TF_DELACK;
1046 					tcp_timer_activate(tp, TT_DELACK,
1047 					    tcp_delacktime);
1048 				}
1049 			}
1050 			break;
1051 		case TT_REXMT:
1052 			if (tp->t_timers->tt_flags & TT_REXMT_SUS) {
1053 				tp->t_timers->tt_flags &= ~TT_REXMT_SUS;
1054 				if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1055 				    (tcp_timer_active((tp), TT_PERSIST) == 0) &&
1056 				    tp->snd_wnd) {
1057 					/* We have outstanding data activate a timer */
1058 					tcp_timer_activate(tp, TT_REXMT,
1059                                             tp->t_rxtcur);
1060 				}
1061 			}
1062 			break;
1063 		case TT_PERSIST:
1064 			if (tp->t_timers->tt_flags & TT_PERSIST_SUS) {
1065 				tp->t_timers->tt_flags &= ~TT_PERSIST_SUS;
1066 				if (tp->snd_wnd == 0) {
1067 					/* Activate the persists timer */
1068 					tp->t_rxtshift = 0;
1069 					tcp_setpersist(tp);
1070 				}
1071 			}
1072 			break;
1073 		case TT_KEEP:
1074 			if (tp->t_timers->tt_flags & TT_KEEP_SUS) {
1075 				tp->t_timers->tt_flags &= ~TT_KEEP_SUS;
1076 				tcp_timer_activate(tp, TT_KEEP,
1077 					    TCPS_HAVEESTABLISHED(tp->t_state) ?
1078 					    TP_KEEPIDLE(tp) : TP_KEEPINIT(tp));
1079 			}
1080 			break;
1081 		case TT_2MSL:
1082 			if (tp->t_timers->tt_flags &= TT_2MSL_SUS) {
1083 				tp->t_timers->tt_flags &= ~TT_2MSL_SUS;
1084 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1085 				    ((tp->t_inpcb->inp_socket == NULL) ||
1086 				     (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE))) {
1087 					/* Star the 2MSL timer */
1088 					tcp_timer_activate(tp, TT_2MSL,
1089 					    (tcp_fast_finwait2_recycle) ?
1090 					    tcp_finwait2_timeout : TP_MAXIDLE(tp));
1091 				}
1092 			}
1093 			break;
1094 		default:
1095 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
1096 	}
1097 }
1098 
1099 static void
1100 tcp_timer_discard(void *ptp)
1101 {
1102 	struct inpcb *inp;
1103 	struct tcpcb *tp;
1104 	struct epoch_tracker et;
1105 
1106 	tp = (struct tcpcb *)ptp;
1107 	CURVNET_SET(tp->t_vnet);
1108 	NET_EPOCH_ENTER(et);
1109 	inp = tp->t_inpcb;
1110 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL",
1111 		__func__, tp));
1112 	INP_WLOCK(inp);
1113 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) != 0,
1114 		("%s: tcpcb has to be stopped here", __func__));
1115 	if (--tp->t_timers->tt_draincnt > 0 ||
1116 	    tcp_freecb(tp) == false)
1117 		INP_WUNLOCK(inp);
1118 	NET_EPOCH_EXIT(et);
1119 	CURVNET_RESTORE();
1120 }
1121 
1122 void
1123 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
1124 {
1125 	struct callout *t_callout;
1126 
1127 	tp->t_timers->tt_flags |= TT_STOPPED;
1128 	switch (timer_type) {
1129 		case TT_DELACK:
1130 			t_callout = &tp->t_timers->tt_delack;
1131 			break;
1132 		case TT_REXMT:
1133 			t_callout = &tp->t_timers->tt_rexmt;
1134 			break;
1135 		case TT_PERSIST:
1136 			t_callout = &tp->t_timers->tt_persist;
1137 			break;
1138 		case TT_KEEP:
1139 			t_callout = &tp->t_timers->tt_keep;
1140 			break;
1141 		case TT_2MSL:
1142 			t_callout = &tp->t_timers->tt_2msl;
1143 			break;
1144 		default:
1145 			if (tp->t_fb->tfb_tcp_timer_stop) {
1146 				/*
1147 				 * XXXrrs we need to look at this with the
1148 				 * stop case below (flags).
1149 				 */
1150 				tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
1151 				return;
1152 			}
1153 			panic("tp %p bad timer_type %#x", tp, timer_type);
1154 		}
1155 
1156 	if (callout_async_drain(t_callout, tcp_timer_discard) == 0) {
1157 		/*
1158 		 * Can't stop the callout, defer tcpcb actual deletion
1159 		 * to the last one. We do this using the async drain
1160 		 * function and incrementing the count in
1161 		 */
1162 		tp->t_timers->tt_draincnt++;
1163 	}
1164 }
1165