1 /*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
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/cc.h>
59 #include <netinet/in.h>
60 #include <netinet/in_kdtrace.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/in_rss.h>
63 #include <netinet/in_systm.h>
64 #ifdef INET6
65 #include <netinet6/in6_pcb.h>
66 #endif
67 #include <netinet/ip_var.h>
68 #include <netinet/tcp_fsm.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #ifdef INET6
72 #include <netinet6/tcp6_var.h>
73 #endif
74 #include <netinet/tcpip.h>
75 #ifdef TCPDEBUG
76 #include <netinet/tcp_debug.h>
77 #endif
78
79 int tcp_keepinit;
80 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, CTLTYPE_INT|CTLFLAG_RW,
81 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "time to establish connection");
82
83 int tcp_keepidle;
84 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, CTLTYPE_INT|CTLFLAG_RW,
85 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "time before keepalive probes begin");
86
87 int tcp_keepintvl;
88 SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, CTLTYPE_INT|CTLFLAG_RW,
89 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "time between keepalive probes");
90
91 int tcp_delacktime;
92 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DELACKTIME, delacktime, CTLTYPE_INT|CTLFLAG_RW,
93 &tcp_delacktime, 0, sysctl_msec_to_ticks, "I",
94 "Time before a delayed ACK is sent");
95
96 int tcp_msl;
97 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, CTLTYPE_INT|CTLFLAG_RW,
98 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
99
100 int tcp_rexmit_min;
101 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_min, CTLTYPE_INT|CTLFLAG_RW,
102 &tcp_rexmit_min, 0, sysctl_msec_to_ticks, "I",
103 "Minimum Retransmission Timeout");
104
105 int tcp_rexmit_slop;
106 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmit_slop, CTLTYPE_INT|CTLFLAG_RW,
107 &tcp_rexmit_slop, 0, sysctl_msec_to_ticks, "I",
108 "Retransmission Timer Slop");
109
110 static int always_keepalive = 1;
111 SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, CTLFLAG_RW,
112 &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections");
113
114 int tcp_fast_finwait2_recycle = 0;
115 SYSCTL_INT(_net_inet_tcp, OID_AUTO, fast_finwait2_recycle, CTLFLAG_RW,
116 &tcp_fast_finwait2_recycle, 0,
117 "Recycle closed FIN_WAIT_2 connections faster");
118
119 int tcp_finwait2_timeout;
120 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, finwait2_timeout, CTLTYPE_INT|CTLFLAG_RW,
121 &tcp_finwait2_timeout, 0, sysctl_msec_to_ticks, "I", "FIN-WAIT2 timeout");
122
123 int tcp_keepcnt = TCPTV_KEEPCNT;
124 SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt, CTLFLAG_RW, &tcp_keepcnt, 0,
125 "Number of keepalive probes to send");
126
127 /* max idle probes */
128 int tcp_maxpersistidle;
129
130 static int tcp_rexmit_drop_options = 0;
131 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rexmit_drop_options, CTLFLAG_RW,
132 &tcp_rexmit_drop_options, 0,
133 "Drop TCP options from 3rd and later retransmitted SYN");
134
135 static VNET_DEFINE(int, tcp_pmtud_blackhole_detect);
136 #define V_tcp_pmtud_blackhole_detect VNET(tcp_pmtud_blackhole_detect)
137 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_detection,
138 CTLFLAG_RW|CTLFLAG_VNET,
139 &VNET_NAME(tcp_pmtud_blackhole_detect), 0,
140 "Path MTU Discovery Black Hole Detection Enabled");
141
142 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated);
143 #define V_tcp_pmtud_blackhole_activated \
144 VNET(tcp_pmtud_blackhole_activated)
145 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated,
146 CTLFLAG_RD|CTLFLAG_VNET,
147 &VNET_NAME(tcp_pmtud_blackhole_activated), 0,
148 "Path MTU Discovery Black Hole Detection, Activation Count");
149
150 static VNET_DEFINE(int, tcp_pmtud_blackhole_activated_min_mss);
151 #define V_tcp_pmtud_blackhole_activated_min_mss \
152 VNET(tcp_pmtud_blackhole_activated_min_mss)
153 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_activated_min_mss,
154 CTLFLAG_RD|CTLFLAG_VNET,
155 &VNET_NAME(tcp_pmtud_blackhole_activated_min_mss), 0,
156 "Path MTU Discovery Black Hole Detection, Activation Count at min MSS");
157
158 static VNET_DEFINE(int, tcp_pmtud_blackhole_failed);
159 #define V_tcp_pmtud_blackhole_failed VNET(tcp_pmtud_blackhole_failed)
160 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_failed,
161 CTLFLAG_RD|CTLFLAG_VNET,
162 &VNET_NAME(tcp_pmtud_blackhole_failed), 0,
163 "Path MTU Discovery Black Hole Detection, Failure Count");
164
165 #ifdef INET
166 static VNET_DEFINE(int, tcp_pmtud_blackhole_mss) = 1200;
167 #define V_tcp_pmtud_blackhole_mss VNET(tcp_pmtud_blackhole_mss)
168 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pmtud_blackhole_mss,
169 CTLFLAG_RW|CTLFLAG_VNET,
170 &VNET_NAME(tcp_pmtud_blackhole_mss), 0,
171 "Path MTU Discovery Black Hole Detection lowered MSS");
172 #endif
173
174 #ifdef INET6
175 static VNET_DEFINE(int, tcp_v6pmtud_blackhole_mss) = 1220;
176 #define V_tcp_v6pmtud_blackhole_mss VNET(tcp_v6pmtud_blackhole_mss)
177 SYSCTL_INT(_net_inet_tcp, OID_AUTO, v6pmtud_blackhole_mss,
178 CTLFLAG_RW|CTLFLAG_VNET,
179 &VNET_NAME(tcp_v6pmtud_blackhole_mss), 0,
180 "Path MTU Discovery IPv6 Black Hole Detection lowered MSS");
181 #endif
182
183 #ifdef RSS
184 static int per_cpu_timers = 1;
185 #else
186 static int per_cpu_timers = 0;
187 #endif
188 SYSCTL_INT(_net_inet_tcp, OID_AUTO, per_cpu_timers, CTLFLAG_RW,
189 &per_cpu_timers , 0, "run tcp timers on all cpus");
190
191 #if 0
192 #define INP_CPU(inp) (per_cpu_timers ? (!CPU_ABSENT(((inp)->inp_flowid % (mp_maxid+1))) ? \
193 ((inp)->inp_flowid % (mp_maxid+1)) : curcpu) : 0)
194 #endif
195
196 /*
197 * Map the given inp to a CPU id.
198 *
199 * This queries RSS if it's compiled in, else it defaults to the current
200 * CPU ID.
201 */
202 static inline int
inp_to_cpuid(struct inpcb * inp)203 inp_to_cpuid(struct inpcb *inp)
204 {
205 u_int cpuid;
206
207 #ifdef RSS
208 if (per_cpu_timers) {
209 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
210 if (cpuid == NETISR_CPUID_NONE)
211 return (curcpu); /* XXX */
212 else
213 return (cpuid);
214 }
215 #else
216 /* Legacy, pre-RSS behaviour */
217 if (per_cpu_timers) {
218 /*
219 * We don't have a flowid -> cpuid mapping, so cheat and
220 * just map unknown cpuids to curcpu. Not the best, but
221 * apparently better than defaulting to swi 0.
222 */
223 cpuid = inp->inp_flowid % (mp_maxid + 1);
224 if (! CPU_ABSENT(cpuid))
225 return (cpuid);
226 return (curcpu);
227 }
228 #endif
229 /* Default for RSS and non-RSS - cpuid 0 */
230 else {
231 return (0);
232 }
233 }
234
235 /*
236 * Tcp protocol timeout routine called every 500 ms.
237 * Updates timestamps used for TCP
238 * causes finite state machine actions if timers expire.
239 */
240 void
tcp_slowtimo(void)241 tcp_slowtimo(void)
242 {
243 VNET_ITERATOR_DECL(vnet_iter);
244
245 VNET_LIST_RLOCK_NOSLEEP();
246 VNET_FOREACH(vnet_iter) {
247 CURVNET_SET(vnet_iter);
248 (void) tcp_tw_2msl_scan(0);
249 CURVNET_RESTORE();
250 }
251 VNET_LIST_RUNLOCK_NOSLEEP();
252 }
253
254 int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
255 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
256
257 int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
258 { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 512, 512, 512 };
259
260 static int tcp_totbackoff = 2559; /* sum of tcp_backoff[] */
261
262 /*
263 * TCP timer processing.
264 */
265
266 void
tcp_timer_delack(void * xtp)267 tcp_timer_delack(void *xtp)
268 {
269 struct tcpcb *tp = xtp;
270 struct inpcb *inp;
271 CURVNET_SET(tp->t_vnet);
272
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 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
289 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
290 KASSERT((tp->t_timers->tt_flags & TT_DELACK) != 0,
291 ("%s: tp %p delack callout should be running", __func__, tp));
292
293 tp->t_flags |= TF_ACKNOW;
294 TCPSTAT_INC(tcps_delack);
295 (void) tp->t_fb->tfb_tcp_output(tp);
296 INP_WUNLOCK(inp);
297 CURVNET_RESTORE();
298 }
299
300 void
tcp_timer_2msl(void * xtp)301 tcp_timer_2msl(void *xtp)
302 {
303 struct tcpcb *tp = xtp;
304 struct inpcb *inp;
305 CURVNET_SET(tp->t_vnet);
306 #ifdef TCPDEBUG
307 int ostate;
308
309 ostate = tp->t_state;
310 #endif
311 INP_INFO_RLOCK(&V_tcbinfo);
312 inp = tp->t_inpcb;
313 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
314 INP_WLOCK(inp);
315 tcp_free_sackholes(tp);
316 if (callout_pending(&tp->t_timers->tt_2msl) ||
317 !callout_active(&tp->t_timers->tt_2msl)) {
318 INP_WUNLOCK(tp->t_inpcb);
319 INP_INFO_RUNLOCK(&V_tcbinfo);
320 CURVNET_RESTORE();
321 return;
322 }
323 callout_deactivate(&tp->t_timers->tt_2msl);
324 if ((inp->inp_flags & INP_DROPPED) != 0) {
325 INP_WUNLOCK(inp);
326 INP_INFO_RUNLOCK(&V_tcbinfo);
327 CURVNET_RESTORE();
328 return;
329 }
330 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
331 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
332 KASSERT((tp->t_timers->tt_flags & TT_2MSL) != 0,
333 ("%s: tp %p 2msl callout should be running", __func__, tp));
334 /*
335 * 2 MSL timeout in shutdown went off. If we're closed but
336 * still waiting for peer to close and connection has been idle
337 * too long delete connection control block. Otherwise, check
338 * again in a bit.
339 *
340 * If in TIME_WAIT state just ignore as this timeout is handled in
341 * tcp_tw_2msl_scan().
342 *
343 * If fastrecycle of FIN_WAIT_2, in FIN_WAIT_2 and receiver has closed,
344 * there's no point in hanging onto FIN_WAIT_2 socket. Just close it.
345 * Ignore fact that there were recent incoming segments.
346 */
347 if ((inp->inp_flags & INP_TIMEWAIT) != 0) {
348 INP_WUNLOCK(inp);
349 INP_INFO_RUNLOCK(&V_tcbinfo);
350 CURVNET_RESTORE();
351 return;
352 }
353 if (tcp_fast_finwait2_recycle && tp->t_state == TCPS_FIN_WAIT_2 &&
354 tp->t_inpcb && tp->t_inpcb->inp_socket &&
355 (tp->t_inpcb->inp_socket->so_rcv.sb_state & SBS_CANTRCVMORE)) {
356 TCPSTAT_INC(tcps_finwait2_drops);
357 tp = tcp_close(tp);
358 } else {
359 if (ticks - tp->t_rcvtime <= TP_MAXIDLE(tp)) {
360 if (!callout_reset(&tp->t_timers->tt_2msl,
361 TP_KEEPINTVL(tp), tcp_timer_2msl, tp)) {
362 tp->t_timers->tt_flags &= ~TT_2MSL_RST;
363 }
364 } else
365 tp = tcp_close(tp);
366 }
367
368 #ifdef TCPDEBUG
369 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
370 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
371 PRU_SLOWTIMO);
372 #endif
373 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
374
375 if (tp != NULL)
376 INP_WUNLOCK(inp);
377 INP_INFO_RUNLOCK(&V_tcbinfo);
378 CURVNET_RESTORE();
379 }
380
381 void
tcp_timer_keep(void * xtp)382 tcp_timer_keep(void *xtp)
383 {
384 struct tcpcb *tp = xtp;
385 struct tcptemp *t_template;
386 struct inpcb *inp;
387 CURVNET_SET(tp->t_vnet);
388 #ifdef TCPDEBUG
389 int ostate;
390
391 ostate = tp->t_state;
392 #endif
393 INP_INFO_RLOCK(&V_tcbinfo);
394 inp = tp->t_inpcb;
395 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
396 INP_WLOCK(inp);
397 if (callout_pending(&tp->t_timers->tt_keep) ||
398 !callout_active(&tp->t_timers->tt_keep)) {
399 INP_WUNLOCK(inp);
400 INP_INFO_RUNLOCK(&V_tcbinfo);
401 CURVNET_RESTORE();
402 return;
403 }
404 callout_deactivate(&tp->t_timers->tt_keep);
405 if ((inp->inp_flags & INP_DROPPED) != 0) {
406 INP_WUNLOCK(inp);
407 INP_INFO_RUNLOCK(&V_tcbinfo);
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 KASSERT((tp->t_timers->tt_flags & TT_KEEP) != 0,
414 ("%s: tp %p keep callout should be running", __func__, tp));
415 /*
416 * Keep-alive timer went off; send something
417 * or drop connection if idle for too long.
418 */
419 TCPSTAT_INC(tcps_keeptimeo);
420 if (tp->t_state < TCPS_ESTABLISHED)
421 goto dropit;
422 if ((always_keepalive || inp->inp_socket->so_options & SO_KEEPALIVE) &&
423 tp->t_state <= TCPS_CLOSING) {
424 if (ticks - tp->t_rcvtime >= TP_KEEPIDLE(tp) + TP_MAXIDLE(tp))
425 goto dropit;
426 /*
427 * Send a packet designed to force a response
428 * if the peer is up and reachable:
429 * either an ACK if the connection is still alive,
430 * or an RST if the peer has closed the connection
431 * due to timeout or reboot.
432 * Using sequence number tp->snd_una-1
433 * causes the transmitted zero-length segment
434 * to lie outside the receive window;
435 * by the protocol spec, this requires the
436 * correspondent TCP to respond.
437 */
438 TCPSTAT_INC(tcps_keepprobe);
439 t_template = tcpip_maketemplate(inp);
440 if (t_template) {
441 tcp_respond(tp, t_template->tt_ipgen,
442 &t_template->tt_t, (struct mbuf *)NULL,
443 tp->rcv_nxt, tp->snd_una - 1, 0);
444 free(t_template, M_TEMP);
445 }
446 if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPINTVL(tp),
447 tcp_timer_keep, tp)) {
448 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
449 }
450 } else if (!callout_reset(&tp->t_timers->tt_keep, TP_KEEPIDLE(tp),
451 tcp_timer_keep, tp)) {
452 tp->t_timers->tt_flags &= ~TT_KEEP_RST;
453 }
454
455 #ifdef TCPDEBUG
456 if (inp->inp_socket->so_options & SO_DEBUG)
457 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
458 PRU_SLOWTIMO);
459 #endif
460 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
461 INP_WUNLOCK(inp);
462 INP_INFO_RUNLOCK(&V_tcbinfo);
463 CURVNET_RESTORE();
464 return;
465
466 dropit:
467 TCPSTAT_INC(tcps_keepdrops);
468 tp = tcp_drop(tp, ETIMEDOUT);
469
470 #ifdef TCPDEBUG
471 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
472 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
473 PRU_SLOWTIMO);
474 #endif
475 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
476 if (tp != NULL)
477 INP_WUNLOCK(tp->t_inpcb);
478 INP_INFO_RUNLOCK(&V_tcbinfo);
479 CURVNET_RESTORE();
480 }
481
482 void
tcp_timer_persist(void * xtp)483 tcp_timer_persist(void *xtp)
484 {
485 struct tcpcb *tp = xtp;
486 struct inpcb *inp;
487 CURVNET_SET(tp->t_vnet);
488 #ifdef TCPDEBUG
489 int ostate;
490
491 ostate = tp->t_state;
492 #endif
493 INP_INFO_RLOCK(&V_tcbinfo);
494 inp = tp->t_inpcb;
495 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
496 INP_WLOCK(inp);
497 if (callout_pending(&tp->t_timers->tt_persist) ||
498 !callout_active(&tp->t_timers->tt_persist)) {
499 INP_WUNLOCK(inp);
500 INP_INFO_RUNLOCK(&V_tcbinfo);
501 CURVNET_RESTORE();
502 return;
503 }
504 callout_deactivate(&tp->t_timers->tt_persist);
505 if ((inp->inp_flags & INP_DROPPED) != 0) {
506 INP_WUNLOCK(inp);
507 INP_INFO_RUNLOCK(&V_tcbinfo);
508 CURVNET_RESTORE();
509 return;
510 }
511 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
512 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
513 KASSERT((tp->t_timers->tt_flags & TT_PERSIST) != 0,
514 ("%s: tp %p persist callout should be running", __func__, tp));
515 /*
516 * Persistance timer into zero window.
517 * Force a byte to be output, if possible.
518 */
519 TCPSTAT_INC(tcps_persisttimeo);
520 /*
521 * Hack: if the peer is dead/unreachable, we do not
522 * time out if the window is closed. After a full
523 * backoff, drop the connection if the idle time
524 * (no responses to probes) reaches the maximum
525 * backoff that we would use if retransmitting.
526 */
527 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
528 (ticks - tp->t_rcvtime >= tcp_maxpersistidle ||
529 ticks - tp->t_rcvtime >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {
530 TCPSTAT_INC(tcps_persistdrop);
531 tp = tcp_drop(tp, ETIMEDOUT);
532 goto out;
533 }
534 /*
535 * If the user has closed the socket then drop a persisting
536 * connection after a much reduced timeout.
537 */
538 if (tp->t_state > TCPS_CLOSE_WAIT &&
539 (ticks - tp->t_rcvtime) >= TCPTV_PERSMAX) {
540 TCPSTAT_INC(tcps_persistdrop);
541 tp = tcp_drop(tp, ETIMEDOUT);
542 goto out;
543 }
544 tcp_setpersist(tp);
545 tp->t_flags |= TF_FORCEDATA;
546 (void) tp->t_fb->tfb_tcp_output(tp);
547 tp->t_flags &= ~TF_FORCEDATA;
548
549 out:
550 #ifdef TCPDEBUG
551 if (tp != NULL && tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
552 tcp_trace(TA_USER, ostate, tp, NULL, NULL, PRU_SLOWTIMO);
553 #endif
554 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
555 if (tp != NULL)
556 INP_WUNLOCK(inp);
557 INP_INFO_RUNLOCK(&V_tcbinfo);
558 CURVNET_RESTORE();
559 }
560
561 void
tcp_timer_rexmt(void * xtp)562 tcp_timer_rexmt(void * xtp)
563 {
564 struct tcpcb *tp = xtp;
565 CURVNET_SET(tp->t_vnet);
566 int rexmt;
567 int headlocked;
568 struct inpcb *inp;
569 #ifdef TCPDEBUG
570 int ostate;
571
572 ostate = tp->t_state;
573 #endif
574
575 INP_INFO_RLOCK(&V_tcbinfo);
576 inp = tp->t_inpcb;
577 KASSERT(inp != NULL, ("%s: tp %p tp->t_inpcb == NULL", __func__, tp));
578 INP_WLOCK(inp);
579 if (callout_pending(&tp->t_timers->tt_rexmt) ||
580 !callout_active(&tp->t_timers->tt_rexmt)) {
581 INP_WUNLOCK(inp);
582 INP_INFO_RUNLOCK(&V_tcbinfo);
583 CURVNET_RESTORE();
584 return;
585 }
586 callout_deactivate(&tp->t_timers->tt_rexmt);
587 if ((inp->inp_flags & INP_DROPPED) != 0) {
588 INP_WUNLOCK(inp);
589 INP_INFO_RUNLOCK(&V_tcbinfo);
590 CURVNET_RESTORE();
591 return;
592 }
593 KASSERT((tp->t_timers->tt_flags & TT_STOPPED) == 0,
594 ("%s: tp %p tcpcb can't be stopped here", __func__, tp));
595 KASSERT((tp->t_timers->tt_flags & TT_REXMT) != 0,
596 ("%s: tp %p rexmt callout should be running", __func__, tp));
597 tcp_free_sackholes(tp);
598 /*
599 * Retransmission timer went off. Message has not
600 * been acked within retransmit interval. Back off
601 * to a longer retransmit interval and retransmit one segment.
602 */
603 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
604 tp->t_rxtshift = TCP_MAXRXTSHIFT;
605 TCPSTAT_INC(tcps_timeoutdrop);
606
607 tp = tcp_drop(tp, tp->t_softerror ?
608 tp->t_softerror : ETIMEDOUT);
609 headlocked = 1;
610 goto out;
611 }
612 INP_INFO_RUNLOCK(&V_tcbinfo);
613 headlocked = 0;
614 if (tp->t_state == TCPS_SYN_SENT) {
615 /*
616 * If the SYN was retransmitted, indicate CWND to be
617 * limited to 1 segment in cc_conn_init().
618 */
619 tp->snd_cwnd = 1;
620 } else if (tp->t_rxtshift == 1) {
621 /*
622 * first retransmit; record ssthresh and cwnd so they can
623 * be recovered if this turns out to be a "bad" retransmit.
624 * A retransmit is considered "bad" if an ACK for this
625 * segment is received within RTT/2 interval; the assumption
626 * here is that the ACK was already in flight. See
627 * "On Estimating End-to-End Network Path Properties" by
628 * Allman and Paxson for more details.
629 */
630 tp->snd_cwnd_prev = tp->snd_cwnd;
631 tp->snd_ssthresh_prev = tp->snd_ssthresh;
632 tp->snd_recover_prev = tp->snd_recover;
633 if (IN_FASTRECOVERY(tp->t_flags))
634 tp->t_flags |= TF_WASFRECOVERY;
635 else
636 tp->t_flags &= ~TF_WASFRECOVERY;
637 if (IN_CONGRECOVERY(tp->t_flags))
638 tp->t_flags |= TF_WASCRECOVERY;
639 else
640 tp->t_flags &= ~TF_WASCRECOVERY;
641 tp->t_badrxtwin = ticks + (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
642 tp->t_flags |= TF_PREVVALID;
643 } else
644 tp->t_flags &= ~TF_PREVVALID;
645 TCPSTAT_INC(tcps_rexmttimeo);
646 if ((tp->t_state == TCPS_SYN_SENT) ||
647 (tp->t_state == TCPS_SYN_RECEIVED))
648 rexmt = TCPTV_RTOBASE * tcp_syn_backoff[tp->t_rxtshift];
649 else
650 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
651 TCPT_RANGESET(tp->t_rxtcur, rexmt,
652 tp->t_rttmin, TCPTV_REXMTMAX);
653
654 /*
655 * We enter the path for PLMTUD if connection is established or, if
656 * connection is FIN_WAIT_1 status, reason for the last is that if
657 * amount of data we send is very small, we could send it in couple of
658 * packets and process straight to FIN. In that case we won't catch
659 * ESTABLISHED state.
660 */
661 if (V_tcp_pmtud_blackhole_detect && (((tp->t_state == TCPS_ESTABLISHED))
662 || (tp->t_state == TCPS_FIN_WAIT_1))) {
663 #ifdef INET6
664 int isipv6;
665 #endif
666
667 /*
668 * Idea here is that at each stage of mtu probe (usually, 1448
669 * -> 1188 -> 524) should be given 2 chances to recover before
670 * further clamping down. 'tp->t_rxtshift % 2 == 0' should
671 * take care of that.
672 */
673 if (((tp->t_flags2 & (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) ==
674 (TF2_PLPMTU_PMTUD|TF2_PLPMTU_MAXSEGSNT)) &&
675 (tp->t_rxtshift >= 2 && tp->t_rxtshift % 2 == 0)) {
676 /*
677 * Enter Path MTU Black-hole Detection mechanism:
678 * - Disable Path MTU Discovery (IP "DF" bit).
679 * - Reduce MTU to lower value than what we
680 * negotiated with peer.
681 */
682 /* Record that we may have found a black hole. */
683 tp->t_flags2 |= TF2_PLPMTU_BLACKHOLE;
684
685 /* Keep track of previous MSS. */
686 tp->t_pmtud_saved_maxseg = tp->t_maxseg;
687
688 /*
689 * Reduce the MSS to blackhole value or to the default
690 * in an attempt to retransmit.
691 */
692 #ifdef INET6
693 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) ? 1 : 0;
694 if (isipv6 &&
695 tp->t_maxseg > V_tcp_v6pmtud_blackhole_mss) {
696 /* Use the sysctl tuneable blackhole MSS. */
697 tp->t_maxseg = V_tcp_v6pmtud_blackhole_mss;
698 V_tcp_pmtud_blackhole_activated++;
699 } else if (isipv6) {
700 /* Use the default MSS. */
701 tp->t_maxseg = V_tcp_v6mssdflt;
702 /*
703 * Disable Path MTU Discovery when we switch to
704 * minmss.
705 */
706 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
707 V_tcp_pmtud_blackhole_activated_min_mss++;
708 }
709 #endif
710 #if defined(INET6) && defined(INET)
711 else
712 #endif
713 #ifdef INET
714 if (tp->t_maxseg > V_tcp_pmtud_blackhole_mss) {
715 /* Use the sysctl tuneable blackhole MSS. */
716 tp->t_maxseg = V_tcp_pmtud_blackhole_mss;
717 V_tcp_pmtud_blackhole_activated++;
718 } else {
719 /* Use the default MSS. */
720 tp->t_maxseg = V_tcp_mssdflt;
721 /*
722 * Disable Path MTU Discovery when we switch to
723 * minmss.
724 */
725 tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
726 V_tcp_pmtud_blackhole_activated_min_mss++;
727 }
728 #endif
729 /*
730 * Reset the slow-start flight size
731 * as it may depend on the new MSS.
732 */
733 if (CC_ALGO(tp)->conn_init != NULL)
734 CC_ALGO(tp)->conn_init(tp->ccv);
735 } else {
736 /*
737 * If further retransmissions are still unsuccessful
738 * with a lowered MTU, maybe this isn't a blackhole and
739 * we restore the previous MSS and blackhole detection
740 * flags.
741 * The limit '6' is determined by giving each probe
742 * stage (1448, 1188, 524) 2 chances to recover.
743 */
744 if ((tp->t_flags2 & TF2_PLPMTU_BLACKHOLE) &&
745 (tp->t_rxtshift > 6)) {
746 tp->t_flags2 |= TF2_PLPMTU_PMTUD;
747 tp->t_flags2 &= ~TF2_PLPMTU_BLACKHOLE;
748 tp->t_maxseg = tp->t_pmtud_saved_maxseg;
749 V_tcp_pmtud_blackhole_failed++;
750 /*
751 * Reset the slow-start flight size as it
752 * may depend on the new MSS.
753 */
754 if (CC_ALGO(tp)->conn_init != NULL)
755 CC_ALGO(tp)->conn_init(tp->ccv);
756 }
757 }
758 }
759
760 /*
761 * Disable RFC1323 and SACK if we haven't got any response to
762 * our third SYN to work-around some broken terminal servers
763 * (most of which have hopefully been retired) that have bad VJ
764 * header compression code which trashes TCP segments containing
765 * unknown-to-them TCP options.
766 */
767 if (tcp_rexmit_drop_options && (tp->t_state == TCPS_SYN_SENT) &&
768 (tp->t_rxtshift == 3))
769 tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_SACK_PERMIT);
770 /*
771 * If we backed off this far, our srtt estimate is probably bogus.
772 * Clobber it so we'll take the next rtt measurement as our srtt;
773 * move the current srtt into rttvar to keep the current
774 * retransmit times until then.
775 */
776 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
777 #ifdef INET6
778 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
779 in6_losing(tp->t_inpcb);
780 #endif
781 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
782 tp->t_srtt = 0;
783 }
784 tp->snd_nxt = tp->snd_una;
785 tp->snd_recover = tp->snd_max;
786 /*
787 * Force a segment to be sent.
788 */
789 tp->t_flags |= TF_ACKNOW;
790 /*
791 * If timing a segment in this window, stop the timer.
792 */
793 tp->t_rtttime = 0;
794
795 cc_cong_signal(tp, NULL, CC_RTO);
796
797 (void) tp->t_fb->tfb_tcp_output(tp);
798
799 out:
800 #ifdef TCPDEBUG
801 if (tp != NULL && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
802 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
803 PRU_SLOWTIMO);
804 #endif
805 TCP_PROBE2(debug__user, tp, PRU_SLOWTIMO);
806 if (tp != NULL)
807 INP_WUNLOCK(inp);
808 if (headlocked)
809 INP_INFO_RUNLOCK(&V_tcbinfo);
810 CURVNET_RESTORE();
811 }
812
813 void
tcp_timer_activate(struct tcpcb * tp,uint32_t timer_type,u_int delta)814 tcp_timer_activate(struct tcpcb *tp, uint32_t timer_type, u_int delta)
815 {
816 struct callout *t_callout;
817 timeout_t *f_callout;
818 struct inpcb *inp = tp->t_inpcb;
819 int cpu = inp_to_cpuid(inp);
820 uint32_t f_reset;
821
822 #ifdef TCP_OFFLOAD
823 if (tp->t_flags & TF_TOE)
824 return;
825 #endif
826
827 if (tp->t_timers->tt_flags & TT_STOPPED)
828 return;
829
830 switch (timer_type) {
831 case TT_DELACK:
832 t_callout = &tp->t_timers->tt_delack;
833 f_callout = tcp_timer_delack;
834 f_reset = TT_DELACK_RST;
835 break;
836 case TT_REXMT:
837 t_callout = &tp->t_timers->tt_rexmt;
838 f_callout = tcp_timer_rexmt;
839 f_reset = TT_REXMT_RST;
840 break;
841 case TT_PERSIST:
842 t_callout = &tp->t_timers->tt_persist;
843 f_callout = tcp_timer_persist;
844 f_reset = TT_PERSIST_RST;
845 break;
846 case TT_KEEP:
847 t_callout = &tp->t_timers->tt_keep;
848 f_callout = tcp_timer_keep;
849 f_reset = TT_KEEP_RST;
850 break;
851 case TT_2MSL:
852 t_callout = &tp->t_timers->tt_2msl;
853 f_callout = tcp_timer_2msl;
854 f_reset = TT_2MSL_RST;
855 break;
856 default:
857 if (tp->t_fb->tfb_tcp_timer_activate) {
858 tp->t_fb->tfb_tcp_timer_activate(tp, timer_type, delta);
859 return;
860 }
861 panic("tp %p bad timer_type %#x", tp, timer_type);
862 }
863 if (delta == 0) {
864 if ((tp->t_timers->tt_flags & timer_type) &&
865 (callout_stop(t_callout) > 0) &&
866 (tp->t_timers->tt_flags & f_reset)) {
867 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
868 }
869 } else {
870 if ((tp->t_timers->tt_flags & timer_type) == 0) {
871 tp->t_timers->tt_flags |= (timer_type | f_reset);
872 callout_reset_on(t_callout, delta, f_callout, tp, cpu);
873 } else {
874 /* Reset already running callout on the same CPU. */
875 if (!callout_reset(t_callout, delta, f_callout, tp)) {
876 /*
877 * Callout not cancelled, consider it as not
878 * properly restarted. */
879 tp->t_timers->tt_flags &= ~f_reset;
880 }
881 }
882 }
883 }
884
885 int
tcp_timer_active(struct tcpcb * tp,uint32_t timer_type)886 tcp_timer_active(struct tcpcb *tp, uint32_t timer_type)
887 {
888 struct callout *t_callout;
889
890 switch (timer_type) {
891 case TT_DELACK:
892 t_callout = &tp->t_timers->tt_delack;
893 break;
894 case TT_REXMT:
895 t_callout = &tp->t_timers->tt_rexmt;
896 break;
897 case TT_PERSIST:
898 t_callout = &tp->t_timers->tt_persist;
899 break;
900 case TT_KEEP:
901 t_callout = &tp->t_timers->tt_keep;
902 break;
903 case TT_2MSL:
904 t_callout = &tp->t_timers->tt_2msl;
905 break;
906 default:
907 if (tp->t_fb->tfb_tcp_timer_active) {
908 return(tp->t_fb->tfb_tcp_timer_active(tp, timer_type));
909 }
910 panic("tp %p bad timer_type %#x", tp, timer_type);
911 }
912 return callout_active(t_callout);
913 }
914
915 void
tcp_timer_stop(struct tcpcb * tp,uint32_t timer_type)916 tcp_timer_stop(struct tcpcb *tp, uint32_t timer_type)
917 {
918 struct callout *t_callout;
919 timeout_t *f_callout;
920 uint32_t f_reset;
921
922 tp->t_timers->tt_flags |= TT_STOPPED;
923
924 switch (timer_type) {
925 case TT_DELACK:
926 t_callout = &tp->t_timers->tt_delack;
927 f_callout = tcp_timer_delack_discard;
928 f_reset = TT_DELACK_RST;
929 break;
930 case TT_REXMT:
931 t_callout = &tp->t_timers->tt_rexmt;
932 f_callout = tcp_timer_rexmt_discard;
933 f_reset = TT_REXMT_RST;
934 break;
935 case TT_PERSIST:
936 t_callout = &tp->t_timers->tt_persist;
937 f_callout = tcp_timer_persist_discard;
938 f_reset = TT_PERSIST_RST;
939 break;
940 case TT_KEEP:
941 t_callout = &tp->t_timers->tt_keep;
942 f_callout = tcp_timer_keep_discard;
943 f_reset = TT_KEEP_RST;
944 break;
945 case TT_2MSL:
946 t_callout = &tp->t_timers->tt_2msl;
947 f_callout = tcp_timer_2msl_discard;
948 f_reset = TT_2MSL_RST;
949 break;
950 default:
951 if (tp->t_fb->tfb_tcp_timer_stop) {
952 /*
953 * XXXrrs we need to look at this with the
954 * stop case below (flags).
955 */
956 tp->t_fb->tfb_tcp_timer_stop(tp, timer_type);
957 return;
958 }
959 panic("tp %p bad timer_type %#x", tp, timer_type);
960 }
961
962 if (tp->t_timers->tt_flags & timer_type) {
963 if ((callout_stop(t_callout) > 0) &&
964 (tp->t_timers->tt_flags & f_reset)) {
965 tp->t_timers->tt_flags &= ~(timer_type | f_reset);
966 } else {
967 /*
968 * Can't stop the callout, defer tcpcb actual deletion
969 * to the last tcp timer discard callout.
970 * The TT_STOPPED flag will ensure that no tcp timer
971 * callouts can be restarted on our behalf, and
972 * past this point currently running callouts waiting
973 * on inp lock will return right away after the
974 * classical check for callout reset/stop events:
975 * callout_pending() || !callout_active()
976 */
977 callout_reset(t_callout, 1, f_callout, tp);
978 }
979 }
980 }
981
982 #define ticks_to_msecs(t) (1000*(t) / hz)
983
984 void
tcp_timer_to_xtimer(struct tcpcb * tp,struct tcp_timer * timer,struct xtcp_timer * xtimer)985 tcp_timer_to_xtimer(struct tcpcb *tp, struct tcp_timer *timer,
986 struct xtcp_timer *xtimer)
987 {
988 sbintime_t now;
989
990 bzero(xtimer, sizeof(*xtimer));
991 if (timer == NULL)
992 return;
993 now = getsbinuptime();
994 if (callout_active(&timer->tt_delack))
995 xtimer->tt_delack = (timer->tt_delack.c_time - now) / SBT_1MS;
996 if (callout_active(&timer->tt_rexmt))
997 xtimer->tt_rexmt = (timer->tt_rexmt.c_time - now) / SBT_1MS;
998 if (callout_active(&timer->tt_persist))
999 xtimer->tt_persist = (timer->tt_persist.c_time - now) / SBT_1MS;
1000 if (callout_active(&timer->tt_keep))
1001 xtimer->tt_keep = (timer->tt_keep.c_time - now) / SBT_1MS;
1002 if (callout_active(&timer->tt_2msl))
1003 xtimer->tt_2msl = (timer->tt_2msl.c_time - now) / SBT_1MS;
1004 xtimer->t_rcvtime = ticks_to_msecs(ticks - tp->t_rcvtime);
1005 }
1006