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