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