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