xref: /freebsd/sys/netinet/tcp_timer.c (revision be181ee2a28aa2b4b0e76684bce9f673ef668874)
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 /*
315  * Call tcp_close() from a callout context.
316  */
317 static void
318 tcp_timer_close(struct tcpcb *tp)
319 {
320 	struct epoch_tracker et;
321 	struct inpcb *inp = tp->t_inpcb;
322 
323 	INP_WLOCK_ASSERT(inp);
324 
325 	NET_EPOCH_ENTER(et);
326 	tp = tcp_close(tp);
327 	NET_EPOCH_EXIT(et);
328 	if (tp != NULL)
329 		INP_WUNLOCK(inp);
330 }
331 
332 /*
333  * Call tcp_drop() from a callout context.
334  */
335 static void
336 tcp_timer_drop(struct tcpcb *tp)
337 {
338 	struct epoch_tracker et;
339 	struct inpcb *inp = tp->t_inpcb;
340 
341 	INP_WLOCK_ASSERT(inp);
342 
343 	NET_EPOCH_ENTER(et);
344 	tp = tcp_drop(tp, ETIMEDOUT);
345 	NET_EPOCH_EXIT(et);
346 	if (tp != NULL)
347 		INP_WUNLOCK(inp);
348 }
349 
350 void
351 tcp_timer_2msl(void *xtp)
352 {
353 	struct tcpcb *tp = xtp;
354 	struct inpcb *inp;
355 	CURVNET_SET(tp->t_vnet);
356 #ifdef TCPDEBUG
357 	int ostate;
358 
359 	ostate = tp->t_state;
360 #endif
361 	inp = tp->t_inpcb;
362 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
363 	INP_WLOCK(inp);
364 	tcp_log_end_status(tp, TCP_EI_STATUS_2MSL);
365 	tcp_free_sackholes(tp);
366 	if (callout_pending(&tp->t_timers->tt_2msl) ||
367 	    !callout_active(&tp->t_timers->tt_2msl)) {
368 		INP_WUNLOCK(tp->t_inpcb);
369 		CURVNET_RESTORE();
370 		return;
371 	}
372 	callout_deactivate(&tp->t_timers->tt_2msl);
373 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
374 		INP_WUNLOCK(inp);
375 		CURVNET_RESTORE();
376 		return;
377 	}
378 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
379 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
380 	/*
381 	 * 2 MSL timeout in shutdown went off.  If we're closed but
382 	 * still waiting for peer to close and connection has been idle
383 	 * too long delete connection control block.  Otherwise, check
384 	 * again in a bit.
385 	 *
386 	 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
387 	 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
388 	 * Ignore fact that there were recent incoming segments.
389 	 */
390 	if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
391 	    tp->t_inpcb && tp->t_inpcb->inp_socket &&
392 	    (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
393 		TCPSTAT_INC(tcps_finwait2_drops);
394 		tcp_timer_close(tp);
395 		CURVNET_RESTORE();
396 		return;
397 	} else {
398 		if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
399 			callout_reset(&tp->t_timers->tt_2msl,
400 				      TP_KEEPINTVL(tp), tcp_timer_2msl, tp);
401 		} else {
402 			tcp_timer_close(tp);
403 			CURVNET_RESTORE();
404 			return;
405 		}
406 	}
407 
408 #ifdef TCPDEBUG
409 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
410 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
411 			  PRU_SLOWTIMO);
412 #endif
413 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
414 
415 	INP_WUNLOCK(inp);
416 	CURVNET_RESTORE();
417 }
418 
419 void
420 tcp_timer_keep(void *xtp)
421 {
422 	struct tcpcb *tp = xtp;
423 	struct tcptemp *t_template;
424 	struct inpcb *inp;
425 	struct epoch_tracker et;
426 	CURVNET_SET(tp->t_vnet);
427 #ifdef TCPDEBUG
428 	int ostate;
429 
430 	ostate = tp->t_state;
431 #endif
432 	inp = tp->t_inpcb;
433 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
434 	INP_WLOCK(inp);
435 	if (callout_pending(&tp->t_timers->tt_keep) ||
436 	    !callout_active(&tp->t_timers->tt_keep)) {
437 		INP_WUNLOCK(inp);
438 		CURVNET_RESTORE();
439 		return;
440 	}
441 	callout_deactivate(&tp->t_timers->tt_keep);
442 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
443 		INP_WUNLOCK(inp);
444 		CURVNET_RESTORE();
445 		return;
446 	}
447 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
448 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
449 
450 	/*
451 	 * Because we don't regularly reset the keepalive callout in
452 	 * the ESTABLISHED state, it may be that we don't actually need
453 	 * to send a keepalive yet. If that occurs, schedule another
454 	 * call for the next time the keepalive timer might expire.
455 	 */
456 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
457 		u_int idletime;
458 
459 		idletime = ticks - tp->t_rcvtime;
460 		if (idletime < TP_KEEPIDLE(tp)) {
461 			callout_reset(&tp->t_timers->tt_keep,
462 			    TP_KEEPIDLE(tp) - idletime, tcp_timer_keep, tp);
463 			INP_WUNLOCK(inp);
464 			CURVNET_RESTORE();
465 			return;
466 		}
467 	}
468 
469 	/*
470 	 * Keep-alive timer went off; send something
471 	 * or drop connection if idle for too long.
472 	 */
473 	TCPSTAT_INC(tcps_keeptimeo);
474 	if (tp->t_state < TCPS_ESTABLISHED)
475 		goto dropit;
476 	if ((V_tcp_always_keepalive ||
477 	    inp->inp_socket->so_options & SO_KEEPALIVE) &&
478 	    tp->t_state <= TCPS_CLOSING) {
479 		if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
480 			goto dropit;
481 		/*
482 		 * Send a packet designed to force a response
483 		 * if the peer is up and reachable:
484 		 * either an ACK if the connection is still alive,
485 		 * or an RST if the peer has closed the connection
486 		 * due to timeout or reboot.
487 		 * Using sequence number tp->snd_una-1
488 		 * causes the transmitted zero-length segment
489 		 * to lie outside the receive window;
490 		 * by the protocol spec, this requires the
491 		 * correspondent TCP to respond.
492 		 */
493 		TCPSTAT_INC(tcps_keepprobe);
494 		t_template = tcpip_maketemplate(inp);
495 		if (t_template) {
496 			NET_EPOCH_ENTER(et);
497 			tcp_respond(tp, t_template->tt_ipgen,
498 				    &t_template->tt_t, (struct mbuf *)NULL,
499 				    tp->rcv_nxt, tp->snd_una - 1, 0);
500 			NET_EPOCH_EXIT(et);
501 			free(t_template, M_TEMP);
502 		}
503 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
504 			      tcp_timer_keep, tp);
505 	} else
506 		callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
507 			      tcp_timer_keep, tp);
508 
509 #ifdef TCPDEBUG
510 	if (inp->inp_socket->so_options & SO_DEBUG)
511 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
512 			  PRU_SLOWTIMO);
513 #endif
514 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
515 	INP_WUNLOCK(inp);
516 	CURVNET_RESTORE();
517 	return;
518 
519 dropit:
520 	TCPSTAT_INC(tcps_keepdrops);
521 	NET_EPOCH_ENTER(et);
522 	tcp_log_end_status(tp, TCP_EI_STATUS_KEEP_MAX);
523 	tp = tcp_drop(tp, ETIMEDOUT);
524 
525 #ifdef TCPDEBUG
526 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
527 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
528 			  PRU_SLOWTIMO);
529 #endif
530 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
531 	NET_EPOCH_EXIT(et);
532 	if (tp != NULL)
533 		INP_WUNLOCK(inp);
534 	CURVNET_RESTORE();
535 }
536 
537 /*
538  * Has this session exceeded the maximum time without seeing a substantive
539  * acknowledgement? If so, return true; otherwise false.
540  */
541 static bool
542 tcp_maxunacktime_check(struct tcpcb *tp)
543 {
544 
545 	/* Are we tracking this timer for this session? */
546 	if (TP_MAXUNACKTIME(tp) == 0)
547 		return false;
548 
549 	/* Do we have a current measurement. */
550 	if (tp->t_acktime == 0)
551 		return false;
552 
553 	/* Are we within the acceptable range? */
554 	if (TSTMP_GT(TP_MAXUNACKTIME(tp) + tp->t_acktime, (u_int)ticks))
555 		return false;
556 
557 	/* We exceeded the timer. */
558 	TCPSTAT_INC(tcps_progdrops);
559 	return true;
560 }
561 
562 void
563 tcp_timer_persist(void *xtp)
564 {
565 	struct tcpcb *tp = xtp;
566 	struct inpcb *inp;
567 	struct epoch_tracker et;
568 	bool progdrop;
569 	int outrv;
570 	CURVNET_SET(tp->t_vnet);
571 #ifdef TCPDEBUG
572 	int ostate;
573 
574 	ostate = tp->t_state;
575 #endif
576 	inp = tp->t_inpcb;
577 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
578 	INP_WLOCK(inp);
579 	if (callout_pending(&tp->t_timers->tt_persist) ||
580 	    !callout_active(&tp->t_timers->tt_persist)) {
581 		INP_WUNLOCK(inp);
582 		CURVNET_RESTORE();
583 		return;
584 	}
585 	callout_deactivate(&tp->t_timers->tt_persist);
586 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
587 		INP_WUNLOCK(inp);
588 		CURVNET_RESTORE();
589 		return;
590 	}
591 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
592 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
593 	/*
594 	 * Persistence timer into zero window.
595 	 * Force a byte to be output, if possible.
596 	 */
597 	TCPSTAT_INC(tcps_persisttimeo);
598 	/*
599 	 * Hack: if the peer is dead/unreachable, we do not
600 	 * time out if the window is closed.  After a full
601 	 * backoff, drop the connection if the idle time
602 	 * (no responses to probes) reaches the maximum
603 	 * backoff that we would use if retransmitting.
604 	 * Also, drop the connection if we haven't been making
605 	 * progress.
606 	 */
607 	progdrop = tcp_maxunacktime_check(tp);
608 	if (progdrop || (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
609 	    (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
610 	     ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff))) {
611 		if (!progdrop)
612 			TCPSTAT_INC(tcps_persistdrop);
613 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
614 		tcp_timer_drop(tp);
615 		CURVNET_RESTORE();
616 		return;
617 	}
618 	/*
619 	 * If the user has closed the socket then drop a persisting
620 	 * connection after a much reduced timeout.
621 	 */
622 	if (tp->t_state > TCPS_CLOSE_WAIT &&
623 	    (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
624 		TCPSTAT_INC(tcps_persistdrop);
625 		tcp_log_end_status(tp, TCP_EI_STATUS_PERSIST_MAX);
626 		tcp_timer_drop(tp);
627 		CURVNET_RESTORE();
628 		return;
629 	}
630 	tcp_setpersist(tp);
631 	tp->t_flags |= TF_FORCEDATA;
632 	NET_EPOCH_ENTER(et);
633 	outrv = tcp_output_nodrop(tp);
634 	tp->t_flags &= ~TF_FORCEDATA;
635 
636 #ifdef TCPDEBUG
637 	if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
638 		tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
639 #endif
640 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
641 	(void) tcp_unlock_or_drop(tp, outrv);
642 	NET_EPOCH_EXIT(et);
643 	CURVNET_RESTORE();
644 }
645 
646 void
647 tcp_timer_rexmt(void * xtp)
648 {
649 	struct tcpcb *tp = xtp;
650 	CURVNET_SET(tp->t_vnet);
651 	int rexmt, outrv;
652 	struct inpcb *inp;
653 	struct epoch_tracker et;
654 	bool isipv6;
655 #ifdef TCPDEBUG
656 	int ostate;
657 
658 	ostate = tp->t_state;
659 #endif
660 	inp = tp->t_inpcb;
661 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
662 	INP_WLOCK(inp);
663 	if (callout_pending(&tp->t_timers->tt_rexmt) ||
664 	    !callout_active(&tp->t_timers->tt_rexmt)) {
665 		INP_WUNLOCK(inp);
666 		CURVNET_RESTORE();
667 		return;
668 	}
669 	callout_deactivate(&tp->t_timers->tt_rexmt);
670 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
671 		INP_WUNLOCK(inp);
672 		CURVNET_RESTORE();
673 		return;
674 	}
675 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
676 		("%s: tp %p tcpcb can't be stopped here", __func__, tp));
677 	tcp_free_sackholes(tp);
678 	TCP_LOG_EVENT(tp, NULL, NULL, NULL, TCP_LOG_RTO, 0, 0, NULL, false);
679 	if (tp->t_fb->tfb_tcp_rexmit_tmr) {
680 		/* The stack has a timer action too. */
681 		(*tp->t_fb->tfb_tcp_rexmit_tmr)(tp);
682 	}
683 	/*
684 	 * Retransmission timer went off.  Message has not
685 	 * been acked within retransmit interval.  Back off
686 	 * to a longer retransmit interval and retransmit one segment.
687 	 *
688 	 * If we've either exceeded the maximum number of retransmissions,
689 	 * or we've gone long enough without making progress, then drop
690 	 * the session.
691 	 */
692 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT || tcp_maxunacktime_check(tp)) {
693 		if (tp->t_rxtshift > TCP_MAXRXTSHIFT)
694 			TCPSTAT_INC(tcps_timeoutdrop);
695 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
696 		tcp_log_end_status(tp, TCP_EI_STATUS_RETRAN);
697 		tcp_timer_drop(tp);
698 		CURVNET_RESTORE();
699 		return;
700 	}
701 	if (tp->t_state == TCPS_SYN_SENT) {
702 		/*
703 		 * If the SYN was retransmitted, indicate CWND to be
704 		 * limited to 1 segment in cc_conn_init().
705 		 */
706 		tp->snd_cwnd = 1;
707 	} else if (tp->t_rxtshift == 1) {
708 		/*
709 		 * first retransmit; record ssthresh and cwnd so they can
710 		 * be recovered if this turns out to be a "bad" retransmit.
711 		 * A retransmit is considered "bad" if an ACK for this
712 		 * segment is received within RTT/2 interval; the assumption
713 		 * here is that the ACK was already in flight.  See
714 		 * "On Estimating End-to-End Network Path Properties" by
715 		 * Allman and Paxson for more details.
716 		 */
717 		tp->snd_cwnd_prev = tp->snd_cwnd;
718 		tp->snd_ssthresh_prev = tp->snd_ssthresh;
719 		tp->snd_recover_prev = tp->snd_recover;
720 		if (IN_FASTRECOVERY(tp->t_flags))
721 			tp->t_flags |= TF_WASFRECOVERY;
722 		else
723 			tp->t_flags &= ~TF_WASFRECOVERY;
724 		if (IN_CONGRECOVERY(tp->t_flags))
725 			tp->t_flags |= TF_WASCRECOVERY;
726 		else
727 			tp->t_flags &= ~TF_WASCRECOVERY;
728 		if ((tp->t_flags & TF_RCVD_TSTMP) == 0)
729 			tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
730 		/* In the event that we've negotiated timestamps
731 		 * badrxtwin will be set to the value that we set
732 		 * the retransmitted packet's to_tsval to by tcp_output
733 		 */
734 		tp->t_flags |= TF_PREVVALID;
735 	} else
736 		tp->t_flags &= ~TF_PREVVALID;
737 	TCPSTAT_INC(tcps_rexmttimeo);
738 	if ((tp->t_state == TCPS_SYN_SENT) ||
739 	    (tp->t_state == TCPS_SYN_RECEIVED))
740 		rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift];
741 	else
742 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
743 	TCPT_RANGESET(tp->t_rxtcur, rexmt,
744 		      tp->t_rttmin, TCPTV_REXMTMAX);
745 
746 	/*
747 	 * We enter the path for PLMTUD if connection is established or, if
748 	 * connection is FIN_WAIT_1 status, reason for the last is that if
749 	 * amount of data we send is very small, we could send it in couple of
750 	 * packets and process straight to FIN. In that case we won't catch
751 	 * ESTABLISHED state.
752 	 */
753 #ifdef INET6
754 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? true : false;
755 #else
756 	isipv6 = false;
757 #endif
758 	if (((V_tcp_pmtud_blackhole_detect == 1) ||
759 	    (V_tcp_pmtud_blackhole_detect == 2 && !isipv6) ||
760 	    (V_tcp_pmtud_blackhole_detect == 3 && isipv6)) &&
761 	    ((tp->t_state == TCPS_ESTABLISHED) ||
762 	    (tp->t_state == TCPS_FIN_WAIT_1))) {
763 		if (tp->t_rxtshift == 1) {
764 			/*
765 			 * We enter blackhole detection after the first
766 			 * unsuccessful timer based retransmission.
767 			 * Then we reduce up to two times the MSS, each
768 			 * candidate giving two tries of retransmissions.
769 			 * But we give a candidate only two tries, if it
770 			 * actually reduces the MSS.
771 			 */
772 			tp->t_blackhole_enter = 2;
773 			tp->t_blackhole_exit = tp->t_blackhole_enter;
774 			if (isipv6) {
775 #ifdef INET6
776 				if (tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss)
777 					tp->t_blackhole_exit += 2;
778 				if (tp->t_maxseg > V_tcp_v6mssdflt &&
779 				    V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt)
780 					tp->t_blackhole_exit += 2;
781 #endif
782 			} else {
783 #ifdef INET
784 				if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss)
785 					tp->t_blackhole_exit += 2;
786 				if (tp->t_maxseg > V_tcp_mssdflt &&
787 				    V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt)
788 					tp->t_blackhole_exit += 2;
789 #endif
790 			}
791 		}
792 		if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
793 		    (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
794 		    (tp->t_rxtshift >= tp->t_blackhole_enter &&
795 		    tp->t_rxtshift < tp->t_blackhole_exit &&
796 		    (tp->t_rxtshift - tp->t_blackhole_enter) % 2 == 0)) {
797 			/*
798 			 * Enter Path MTU Black-hole Detection mechanism:
799 			 * - Disable Path MTU Discovery (IP "DF" bit).
800 			 * - Reduce MTU to lower value than what we
801 			 *   negotiated with peer.
802 			 */
803 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) == 0) {
804 				/* Record that we may have found a black hole. */
805 				tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
806 				/* Keep track of previous MSS. */
807 				tp->t_pmtud_saved_maxseg = tp->t_maxseg;
808 			}
809 
810 			/*
811 			 * Reduce the MSS to blackhole value or to the default
812 			 * in an attempt to retransmit.
813 			 */
814 #ifdef INET6
815 			if (isipv6 &&
816 			    tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss &&
817 			    V_tcp_v6pmtud_blackhole_mss > V_tcp_v6mssdflt) {
818 				/* Use the sysctl tuneable blackhole MSS. */
819 				tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
820 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
821 			} else if (isipv6) {
822 				/* Use the default MSS. */
823 				tp->t_maxseg = V_tcp_v6mssdflt;
824 				/*
825 				 * Disable Path MTU Discovery when we switch to
826 				 * minmss.
827 				 */
828 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
829 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
830 			}
831 #endif
832 #if defined(INET6) && defined(INET)
833 			else
834 #endif
835 #ifdef INET
836 			if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss &&
837 			    V_tcp_pmtud_blackhole_mss > V_tcp_mssdflt) {
838 				/* Use the sysctl tuneable blackhole MSS. */
839 				tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
840 				TCPSTAT_INC(tcps_pmtud_blackhole_activated);
841 			} else {
842 				/* Use the default MSS. */
843 				tp->t_maxseg = V_tcp_mssdflt;
844 				/*
845 				 * Disable Path MTU Discovery when we switch to
846 				 * minmss.
847 				 */
848 				tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
849 				TCPSTAT_INC(tcps_pmtud_blackhole_activated_min_mss);
850 			}
851 #endif
852 			/*
853 			 * Reset the slow-start flight size
854 			 * as it may depend on the new MSS.
855 			 */
856 			if (CC_ALGO(tp)->conn_init != NULL)
857 				CC_ALGO(tp)->conn_init(tp->ccv);
858 		} else {
859 			/*
860 			 * If further retransmissions are still unsuccessful
861 			 * with a lowered MTU, maybe this isn't a blackhole and
862 			 * we restore the previous MSS and blackhole detection
863 			 * flags.
864 			 */
865 			if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
866 			    (tp->t_rxtshift >= tp->t_blackhole_exit)) {
867 				tp->t_flags2 |= TF2_PLPMTU_PMTUD;
868 				tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
869 				tp->t_maxseg = tp->t_pmtud_saved_maxseg;
870 				TCPSTAT_INC(tcps_pmtud_blackhole_failed);
871 				/*
872 				 * Reset the slow-start flight size as it
873 				 * may depend on the new MSS.
874 				 */
875 				if (CC_ALGO(tp)->conn_init != NULL)
876 					CC_ALGO(tp)->conn_init(tp->ccv);
877 			}
878 		}
879 	}
880 
881 	/*
882 	 * Disable RFC1323 and SACK if we haven't got any response to
883 	 * our third SYN to work-around some broken terminal servers
884 	 * (most of which have hopefully been retired) that have bad VJ
885 	 * header compression code which trashes TCP segments containing
886 	 * unknown-to-them TCP options.
887 	 */
888 	if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
889 	    (tp->t_rxtshift == 3))
890 		tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
891 	/*
892 	 * If we backed off this far, notify the L3 protocol that we're having
893 	 * connection problems.
894 	 */
895 	if (tp->t_rxtshift > TCP_RTT_INVALIDATE) {
896 #ifdef INET6
897 		if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
898 			in6_losing(tp->t_inpcb);
899 		else
900 #endif
901 			in_losing(tp->t_inpcb);
902 	}
903 	tp->snd_nxt = tp->snd_una;
904 	tp->snd_recover = tp->snd_max;
905 	/*
906 	 * Force a segment to be sent.
907 	 */
908 	tp->t_flags |= TF_ACKNOW;
909 	/*
910 	 * If timing a segment in this window, stop the timer.
911 	 */
912 	tp->t_rtttime = 0;
913 
914 	cc_cong_signal(tp, NULL, CC_RTO);
915 	NET_EPOCH_ENTER(et);
916 	outrv = tcp_output_nodrop(tp);
917 #ifdef TCPDEBUG
918 	if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
919 		tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
920 			  PRU_SLOWTIMO);
921 #endif
922 	TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
923 	(void) tcp_unlock_or_drop(tp, outrv);
924 	NET_EPOCH_EXIT(et);
925 	CURVNET_RESTORE();
926 }
927 
928 void
929 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
930 {
931 	struct callout *t_callout;
932 	callout_func_t *f_callout;
933 	struct inpcb *inp = tp->t_inpcb;
934 	int cpu = inp_to_cpuid(inp);
935 
936 #ifdef TCP_OFFLOAD
937 	if (tp->t_flags & TF_TOE)
938 		return;
939 #endif
940 
941 	if (tp->t_timers->tt_flags & TT_STOPPED)
942 		return;
943 
944 	switch (timer_type) {
945 		case TT_DELACK:
946 			t_callout = &tp->t_timers->tt_delack;
947 			f_callout = tcp_timer_delack;
948 			break;
949 		case TT_REXMT:
950 			t_callout = &tp->t_timers->tt_rexmt;
951 			f_callout = tcp_timer_rexmt;
952 			break;
953 		case TT_PERSIST:
954 			t_callout = &tp->t_timers->tt_persist;
955 			f_callout = tcp_timer_persist;
956 			break;
957 		case TT_KEEP:
958 			t_callout = &tp->t_timers->tt_keep;
959 			f_callout = tcp_timer_keep;
960 			break;
961 		case TT_2MSL:
962 			t_callout = &tp->t_timers->tt_2msl;
963 			f_callout = tcp_timer_2msl;
964 			break;
965 		default:
966 			if (tp->t_fb->tfb_tcp_timer_activate) {
967 				tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
968 				return;
969 			}
970 			panic("tp %p bad timer_type %#x", tp, timer_type);
971 		}
972 	if (delta == 0) {
973 		callout_stop(t_callout);
974 	} else {
975 		callout_reset_on(t_callout, delta, f_callout, tp, cpu);
976 	}
977 }
978 
979 int
980 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
981 {
982 	struct callout *t_callout;
983 
984 	switch (timer_type) {
985 		case TT_DELACK:
986 			t_callout = &tp->t_timers->tt_delack;
987 			break;
988 		case TT_REXMT:
989 			t_callout = &tp->t_timers->tt_rexmt;
990 			break;
991 		case TT_PERSIST:
992 			t_callout = &tp->t_timers->tt_persist;
993 			break;
994 		case TT_KEEP:
995 			t_callout = &tp->t_timers->tt_keep;
996 			break;
997 		case TT_2MSL:
998 			t_callout = &tp->t_timers->tt_2msl;
999 			break;
1000 		default:
1001 			if (tp->t_fb->tfb_tcp_timer_active) {
1002 				return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
1003 			}
1004 			panic("tp %p bad timer_type %#x", tp, timer_type);
1005 		}
1006 	return callout_active(t_callout);
1007 }
1008 
1009 /*
1010  * Stop the timer from running, and apply a flag
1011  * against the timer_flags that will force the
1012  * timer never to run. The flag is needed to assure
1013  * a race does not leave it running and cause
1014  * the timer to possibly restart itself (keep and persist
1015  * especially do this).
1016  */
1017 int
1018 tcp_timer_suspend(struct tcpcb *tp, uint32_t timer_type)
1019 {
1020 	struct callout *t_callout;
1021 	uint32_t t_flags;
1022 
1023 	switch (timer_type) {
1024 		case TT_DELACK:
1025 			t_flags = TT_DELACK_SUS;
1026 			t_callout = &tp->t_timers->tt_delack;
1027 			break;
1028 		case TT_REXMT:
1029 			t_flags = TT_REXMT_SUS;
1030 			t_callout = &tp->t_timers->tt_rexmt;
1031 			break;
1032 		case TT_PERSIST:
1033 			t_flags = TT_PERSIST_SUS;
1034 			t_callout = &tp->t_timers->tt_persist;
1035 			break;
1036 		case TT_KEEP:
1037 			t_flags = TT_KEEP_SUS;
1038 			t_callout = &tp->t_timers->tt_keep;
1039 			break;
1040 		case TT_2MSL:
1041 			t_flags = TT_2MSL_SUS;
1042 			t_callout = &tp->t_timers->tt_2msl;
1043 			break;
1044 		default:
1045 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
1046 	}
1047 	tp->t_timers->tt_flags |= t_flags;
1048 	return (callout_stop(t_callout));
1049 }
1050 
1051 void
1052 tcp_timers_unsuspend(struct tcpcb *tp, uint32_t timer_type)
1053 {
1054 	switch (timer_type) {
1055 		case TT_DELACK:
1056 			if (tp->t_timers->tt_flags & TT_DELACK_SUS) {
1057 				tp->t_timers->tt_flags &= ~TT_DELACK_SUS;
1058 				if (tp->t_flags & TF_DELACK) {
1059 					/* Delayed ack timer should be up activate a timer */
1060 					tp->t_flags &= ~TF_DELACK;
1061 					tcp_timer_activate(tp, TT_DELACK,
1062 					    tcp_delacktime);
1063 				}
1064 			}
1065 			break;
1066 		case TT_REXMT:
1067 			if (tp->t_timers->tt_flags & TT_REXMT_SUS) {
1068 				tp->t_timers->tt_flags &= ~TT_REXMT_SUS;
1069 				if (SEQ_GT(tp->snd_max, tp->snd_una) &&
1070 				    (tcp_timer_active((tp), TT_PERSIST) == 0) &&
1071 				    tp->snd_wnd) {
1072 					/* We have outstanding data activate a timer */
1073 					tcp_timer_activate(tp, TT_REXMT,
1074                                             tp->t_rxtcur);
1075 				}
1076 			}
1077 			break;
1078 		case TT_PERSIST:
1079 			if (tp->t_timers->tt_flags & TT_PERSIST_SUS) {
1080 				tp->t_timers->tt_flags &= ~TT_PERSIST_SUS;
1081 				if (tp->snd_wnd == 0) {
1082 					/* Activate the persists timer */
1083 					tp->t_rxtshift = 0;
1084 					tcp_setpersist(tp);
1085 				}
1086 			}
1087 			break;
1088 		case TT_KEEP:
1089 			if (tp->t_timers->tt_flags & TT_KEEP_SUS) {
1090 				tp->t_timers->tt_flags &= ~TT_KEEP_SUS;
1091 				tcp_timer_activate(tp, TT_KEEP,
1092 					    TCPS_HAVEESTABLISHED(tp->t_state) ?
1093 					    TP_KEEPIDLE(tp) : TP_KEEPINIT(tp));
1094 			}
1095 			break;
1096 		case TT_2MSL:
1097 			if (tp->t_timers->tt_flags &= TT_2MSL_SUS) {
1098 				tp->t_timers->tt_flags &= ~TT_2MSL_SUS;
1099 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1100 				    ((tp->t_inpcb->inp_socket == NULL) ||
1101 				     (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE))) {
1102 					/* Star the 2MSL timer */
1103 					tcp_timer_activate(tp, TT_2MSL,
1104 					    (tcp_fast_finwait2_recycle) ?
1105 					    tcp_finwait2_timeout : TP_MAXIDLE(tp));
1106 				}
1107 			}
1108 			break;
1109 		default:
1110 			panic("tp:%p bad timer_type 0x%x", tp, timer_type);
1111 	}
1112 }
1113 
1114 static void
1115 tcp_timer_discard(void *ptp)
1116 {
1117 	struct inpcb *inp;
1118 	struct tcpcb *tp;
1119 	struct epoch_tracker et;
1120 
1121 	tp = (struct tcpcb *)ptp;
1122 	CURVNET_SET(tp->t_vnet);
1123 	NET_EPOCH_ENTER(et);
1124 	inp = tp->t_inpcb;
1125 	KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL",
1126 		__func__, tp));
1127 	INP_WLOCK(inp);
1128 	KASSERT((tp->t_timers->tt_flags & TT_STOPPED) != 0,
1129 		("%s: tcpcb has to be stopped here", __func__));
1130 	if (--tp->t_timers->tt_draincnt > 0 ||
1131 	    tcp_freecb(tp) == false)
1132 		INP_WUNLOCK(inp);
1133 	NET_EPOCH_EXIT(et);
1134 	CURVNET_RESTORE();
1135 }
1136 
1137 void
1138 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
1139 {
1140 	struct callout *t_callout;
1141 
1142 	tp->t_timers->tt_flags |= TT_STOPPED;
1143 	switch (timer_type) {
1144 		case TT_DELACK:
1145 			t_callout = &tp->t_timers->tt_delack;
1146 			break;
1147 		case TT_REXMT:
1148 			t_callout = &tp->t_timers->tt_rexmt;
1149 			break;
1150 		case TT_PERSIST:
1151 			t_callout = &tp->t_timers->tt_persist;
1152 			break;
1153 		case TT_KEEP:
1154 			t_callout = &tp->t_timers->tt_keep;
1155 			break;
1156 		case TT_2MSL:
1157 			t_callout = &tp->t_timers->tt_2msl;
1158 			break;
1159 		default:
1160 			if (tp->t_fb->tfb_tcp_timer_stop) {
1161 				/*
1162 				 * XXXrrs we need to look at this with the
1163 				 * stop case below (flags).
1164 				 */
1165 				tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
1166 				return;
1167 			}
1168 			panic("tp %p bad timer_type %#x", tp, timer_type);
1169 		}
1170 
1171 	if (callout_async_drain(t_callout, tcp_timer_discard) == 0) {
1172 		/*
1173 		 * Can't stop the callout, defer tcpcb actual deletion
1174 		 * to the last one. We do this using the async drain
1175 		 * function and incrementing the count in
1176 		 */
1177 		tp->t_timers->tt_draincnt++;
1178 	}
1179 }
1180