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