2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.11 2001/08/22 00:59:12 silby Exp $
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
69 #include <sys/sysctl.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/protosw.h>
73 #include <sys/domain.h>
74 #include <kern/locks.h>
76 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
78 #include <net/route.h>
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/in_pcb.h>
84 #include <netinet6/in6_pcb.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/tcp.h>
88 #include <netinet/tcp_fsm.h>
89 #include <netinet/tcp_seq.h>
90 #include <netinet/tcp_timer.h>
91 #include <netinet/tcp_var.h>
93 #include <netinet6/tcp6_var.h>
95 #include <netinet/tcpip.h>
97 #include <netinet/tcp_debug.h>
99 #include <sys/kdebug.h>
101 extern void postevent(struct socket
*, struct sockbuf
*,
103 #define DBG_FNC_TCP_FAST NETDBG_CODE(DBG_NETTCP, (5 << 8))
104 #define DBG_FNC_TCP_SLOW NETDBG_CODE(DBG_NETTCP, (5 << 8) | 1)
106 static int background_io_trigger
= 5;
107 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, background_io_trigger
, CTLFLAG_RW
,
108 &background_io_trigger
, 0, "Background IO Trigger Setting");
118 sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
120 #pragma unused(arg1, arg2)
123 tt
= *(int *)oidp
->oid_arg1
;
124 s
= tt
* 1000 / TCP_RETRANSHZ
;;
126 error
= sysctl_handle_int(oidp
, &s
, 0, req
);
127 if (error
|| !req
->newptr
)
130 tt
= s
* TCP_RETRANSHZ
/ 1000;
134 *(int *)oidp
->oid_arg1
= tt
;
139 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINIT
, keepinit
, CTLTYPE_INT
|CTLFLAG_RW
,
140 &tcp_keepinit
, 0, sysctl_msec_to_ticks
, "I", "");
143 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPIDLE
, keepidle
, CTLTYPE_INT
|CTLFLAG_RW
,
144 &tcp_keepidle
, 0, sysctl_msec_to_ticks
, "I", "");
147 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINTVL
, keepintvl
, CTLTYPE_INT
|CTLFLAG_RW
,
148 &tcp_keepintvl
, 0, sysctl_msec_to_ticks
, "I", "");
151 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, msl
, CTLTYPE_INT
|CTLFLAG_RW
,
152 &tcp_msl
, 0, sysctl_msec_to_ticks
, "I", "Maximum segment lifetime");
154 static int always_keepalive
= 0;
155 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, always_keepalive
, CTLFLAG_RW
,
156 &always_keepalive
, 0, "Assume SO_KEEPALIVE on all TCP connections");
159 * See tcp_syn_backoff[] for interval values between SYN retransmits;
160 * the value set below defines the number of retransmits, before we
161 * disable the timestamp and window scaling options during subsequent
162 * SYN retransmits. Setting it to 0 disables the dropping off of those
165 static int tcp_broken_peer_syn_rxmit_thres
= 7;
166 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, broken_peer_syn_rxmit_thres
, CTLFLAG_RW
,
167 &tcp_broken_peer_syn_rxmit_thres
, 0, "Number of retransmitted SYNs before "
168 "TCP disables rfc1323 and rfc1644 during the rest of attempts");
170 int tcp_pmtud_black_hole_detect
= 1 ;
171 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pmtud_blackhole_detection
, CTLFLAG_RW
,
172 &tcp_pmtud_black_hole_detect
, 0, "Path MTU Discovery Black Hole Detection");
174 int tcp_pmtud_black_hole_mss
= 1200 ;
175 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pmtud_blackhole_mss
, CTLFLAG_RW
,
176 &tcp_pmtud_black_hole_mss
, 0, "Path MTU Discovery Black Hole Detection lowered MSS");
178 static int tcp_keepcnt
= TCPTV_KEEPCNT
;
179 static int tcp_gc_done
= FALSE
; /* perfromed garbage collection of "used" sockets */
180 /* max idle probes */
181 int tcp_maxpersistidle
;
182 /* max idle time in persist */
185 struct inpcbhead time_wait_slots
[N_TIME_WAIT_SLOTS
];
188 u_int32_t
*delack_bitmask
;
190 void add_to_time_wait_locked(struct tcpcb
*tp
);
191 void add_to_time_wait(struct tcpcb
*tp
) ;
193 static void tcp_garbage_collect(struct inpcb
*, int);
195 void add_to_time_wait_locked(struct tcpcb
*tp
)
198 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
200 /* pcb list should be locked when we get here */
201 lck_rw_assert(pcbinfo
->mtx
, LCK_RW_ASSERT_EXCLUSIVE
);
203 LIST_REMOVE(tp
->t_inpcb
, inp_list
);
205 if (tp
->t_timer
[TCPT_2MSL
] <= 0)
206 tp
->t_timer
[TCPT_2MSL
] = 1;
209 * Because we're pulling this pcb out of the main TCP pcb list,
210 * we need to recalculate the TCPT_2MSL timer value for tcp_slowtimo
211 * higher timer granularity.
214 tp
->t_timer
[TCPT_2MSL
] = (tp
->t_timer
[TCPT_2MSL
] / TCP_RETRANSHZ
) * PR_SLOWHZ
;
215 tp
->t_rcvtime
= (tp
->t_rcvtime
/ TCP_RETRANSHZ
) * PR_SLOWHZ
;
217 tp
->t_rcvtime
+= tp
->t_timer
[TCPT_2MSL
] & (N_TIME_WAIT_SLOTS
- 1);
219 tw_slot
= (tp
->t_timer
[TCPT_2MSL
] & (N_TIME_WAIT_SLOTS
- 1)) + cur_tw_slot
;
220 if (tw_slot
>= N_TIME_WAIT_SLOTS
)
221 tw_slot
-= N_TIME_WAIT_SLOTS
;
223 LIST_INSERT_HEAD(&time_wait_slots
[tw_slot
], tp
->t_inpcb
, inp_list
);
226 void add_to_time_wait(struct tcpcb
*tp
)
228 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
230 if (!lck_rw_try_lock_exclusive(pcbinfo
->mtx
)) {
231 tcp_unlock(tp
->t_inpcb
->inp_socket
, 0, 0);
232 lck_rw_lock_exclusive(pcbinfo
->mtx
);
233 tcp_lock(tp
->t_inpcb
->inp_socket
, 0, 0);
235 add_to_time_wait_locked(tp
);
236 lck_rw_done(pcbinfo
->mtx
);
243 * Fast timeout routine for processing delayed acks
246 tcp_fasttimo(void *arg
)
250 register struct tcpcb
*tp
;
257 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
261 KERNEL_DEBUG(DBG_FNC_TCP_FAST
| DBG_FUNC_START
, 0,0,0,0,0);
264 lck_rw_lock_shared(pcbinfo
->mtx
);
266 /* Walk the list of valid tcpcbs and send ACKS on the ones with DELACK bit set */
268 LIST_FOREACH(inp
, &tcb
, inp_list
) {
270 so
= inp
->inp_socket
;
272 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
277 if ((in_pcb_checkstate(inp
, WNT_RELEASE
,1) == WNT_STOPUSING
) && so
->so_usecount
== 1) {
278 tcp_unlock(so
, 1, 0);
284 if (tp
== 0 || tp
->t_state
== TCPS_LISTEN
) {
285 tcp_unlock(so
, 1, 0);
290 /* Only run the retransmit timer in that case */
291 if (tp
->t_timer
[0] && --tp
->t_timer
[0] == 0) {
292 tp
= tcp_timers(tp
, 0);
297 /* TCP pcb timers following the tcp_now clock rate */
305 * Process delayed acks (if enabled) according to PR_FASTHZ, not the retrans timer
308 if (tcp_delack_enabled
&& (tcp_now
% (TCP_RETRANSHZ
/PR_FASTHZ
)) && tp
->t_flags
& TF_DELACK
) {
310 tp
->t_flags
&= ~TF_DELACK
;
311 tp
->t_flags
|= TF_ACKNOW
;
312 tcpstat
.tcps_delack
++;
314 (void) tcp_output(tp
);
317 tcp_unlock(so
, 1, 0);
319 KERNEL_DEBUG(DBG_FNC_TCP_FAST
| DBG_FUNC_END
, delack_done
, 0, tcpstat
.tcps_delack
,0,0);
320 lck_rw_done(pcbinfo
->mtx
);
323 timeout(tcp_fasttimo
, 0, hz
/TCP_RETRANSHZ
);
327 tcp_garbage_collect(struct inpcb
*inp
, int istimewait
)
332 so
= inp
->inp_socket
;
336 * Skip if still in use or busy; it would have been more efficient
337 * if we were to test so_usecount against 0, but this isn't possible
338 * due to the current implementation of tcp_dropdropablreq() where
339 * overflow sockets that are eligible for garbage collection have
340 * their usecounts set to 1.
342 if (so
->so_usecount
> 1 || !lck_mtx_try_lock_spin(inp
->inpcb_mtx
))
345 /* Check again under the lock */
346 if (so
->so_usecount
> 1) {
347 lck_mtx_unlock(inp
->inpcb_mtx
);
352 * Overflowed socket dropped from the listening queue? Do this
353 * only if we are called to clean up the time wait slots, since
354 * tcp_dropdropablreq() considers a socket to have been fully
355 * dropped after add_to_time_wait() is finished.
356 * Also handle the case of connections getting closed by the peer while in the queue as
357 * seen with rdar://6422317
360 if (so
->so_usecount
== 1 &&
361 ((istimewait
&& (so
->so_flags
& SOF_OVERFLOW
)) ||
362 ((tp
!= NULL
) && (tp
->t_state
== TCPS_CLOSED
) && (so
->so_head
!= NULL
)
363 && ((so
->so_state
& (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) ==
364 (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
))))) {
366 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
367 /* Become a regular mutex */
368 lck_mtx_convert_spin(inp
->inpcb_mtx
);
370 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
377 lck_mtx_unlock(inp
->inpcb_mtx
);
379 } else if (inp
->inp_wantcnt
!= WNT_STOPUSING
) {
380 lck_mtx_unlock(inp
->inpcb_mtx
);
385 * We get here because the PCB is no longer searchable (WNT_STOPUSING);
386 * detach (if needed) and dispose if it is dead (usecount is 0). This
387 * covers all cases, including overflow sockets and those that are
388 * considered as "embryonic", i.e. created by sonewconn() in TCP input
389 * path, and have not yet been committed. For the former, we reduce
390 * the usecount to 0 as done by the code above. For the latter, the
391 * usecount would have reduced to 0 as part calling soabort() when the
392 * socket is dropped at the end of tcp_input().
394 if (so
->so_usecount
== 0) {
395 /* Become a regular mutex */
396 lck_mtx_convert_spin(inp
->inpcb_mtx
);
397 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
399 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
407 lck_mtx_unlock(inp
->inpcb_mtx
);
411 static int bg_cnt
= 0;
412 #define BG_COUNTER_MAX 3
417 struct inpcb
*inp
, *nxt
;
426 static int tws_checked
= 0;
429 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
431 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_START
, 0,0,0,0,0);
433 tcp_maxidle
= tcp_keepcnt
* tcp_keepintvl
;
435 lck_rw_lock_shared(pcbinfo
->mtx
);
439 LIST_FOREACH(inp
, &tcb
, inp_list
) {
441 so
= inp
->inp_socket
;
443 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
448 if ((in_pcb_checkstate(inp
, WNT_RELEASE
,1) == WNT_STOPUSING
) && so
->so_usecount
== 1) {
449 tcp_unlock(so
, 1, 0);
453 if (tp
== 0 || tp
->t_state
== TCPS_LISTEN
) {
454 tcp_unlock(so
, 1, 0);
460 if (tp
== 0 || tp
->t_state
== TCPS_LISTEN
)
464 if (so
->so_traffic_mgt_flags
& TRAFFIC_MGT_SO_BACKGROUND
&&
465 bg_cnt
> BG_COUNTER_MAX
) {
466 u_int32_t curr_recvtotal
= tcpstat
.tcps_rcvtotal
;
467 u_int32_t curr_bg_recvtotal
= tcpstat
.tcps_bg_rcvtotal
;
468 u_int32_t bg_recvdiff
= curr_bg_recvtotal
- tp
->bg_recv_snapshot
;
469 u_int32_t tot_recvdiff
= curr_recvtotal
- tp
->tot_recv_snapshot
;
470 u_int32_t fg_recv_change
= tot_recvdiff
- bg_recvdiff
;
471 u_int32_t recv_change
;
473 if (!(so
->so_traffic_mgt_flags
& TRAFFIC_MGT_SO_BG_SUPPRESSED
)) {
475 recv_change
= (fg_recv_change
* 100) / tot_recvdiff
;
479 if (recv_change
> background_io_trigger
) {
480 so
->so_traffic_mgt_flags
|= TRAFFIC_MGT_SO_BG_SUPPRESSED
;
483 tp
->tot_recv_snapshot
= curr_recvtotal
;
484 tp
->bg_recv_snapshot
= curr_bg_recvtotal
;
487 // this allows for bg traffic to subside before we start measuring total traffic change
489 recv_change
= (bg_recvdiff
* 100) / tot_recvdiff
;
493 if (recv_change
< background_io_trigger
) {
494 // Draconian for now: if there is any change at all, keep suppressed
496 so
->so_traffic_mgt_flags
&= ~TRAFFIC_MGT_SO_BG_SUPPRESSED
;
498 (void) tcp_output(tp
); // open window
502 tp
->tot_recv_snapshot
= curr_recvtotal
;
503 tp
->bg_recv_snapshot
= curr_bg_recvtotal
;
506 #endif /* TRAFFIC_MGT */
508 for (i
= 1; i
< TCPT_NTIMERS
; i
++) {
509 if (tp
->t_timer
[i
] != 0) {
510 tp
->t_timer
[i
] -= TCP_RETRANSHZ
/PR_SLOWHZ
;
511 if (tp
->t_timer
[i
] <= 0) {
513 ostate
= tp
->t_state
;
516 tp
->t_timer
[i
] = 0; /* account for granularity change between tcp_now and slowtimo */
517 tp
= tcp_timers(tp
, i
);
521 if (tp
->t_inpcb
->inp_socket
->so_options
523 tcp_trace(TA_USER
, ostate
, tp
,
532 tcp_unlock(so
, 1, 0);
538 /* Second part of tcp_slowtimo: garbage collect socket/tcpcb
539 * We need to acquire the list lock exclusively to do this
542 if (lck_rw_lock_shared_to_exclusive(pcbinfo
->mtx
) == FALSE
) {
543 if (tcp_gc_done
== TRUE
) { /* don't sweat it this time. cleanup was done last time */
545 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_END
, tws_checked
, cur_tw_slot
,0,0,0);
546 return; /* Upgrade failed and lost lock - give up this time. */
548 lck_rw_lock_exclusive(pcbinfo
->mtx
); /* Upgrade failed, lost lock now take it again exclusive */
553 * Process the items in the current time-wait slot
558 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_NONE
, tws_checked
,0,0,0,0);
560 LIST_FOREACH(inp
, &time_wait_slots
[cur_tw_slot
], inp_list
) {
565 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
568 tcp_lock(inp
->inp_socket
, 1, 0);
570 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
)
574 if (tp
== NULL
) /* tp already closed, remove from list */
577 if (tp
->t_timer
[TCPT_2MSL
] >= N_TIME_WAIT_SLOTS
) {
578 tp
->t_timer
[TCPT_2MSL
] -= N_TIME_WAIT_SLOTS
;
579 tp
->t_rcvtime
+= N_TIME_WAIT_SLOTS
;
582 tp
->t_timer
[TCPT_2MSL
] = 0;
584 if (tp
->t_timer
[TCPT_2MSL
] == 0) {
586 /* That pcb is ready for a close */
587 tcp_free_sackholes(tp
);
591 tcp_unlock(inp
->inp_socket
, 1, 0);
595 LIST_FOREACH_SAFE(inp
, &tcb
, inp_list
, nxt
) {
596 tcp_garbage_collect(inp
, 0);
599 /* Now cleanup the time wait ones */
600 LIST_FOREACH_SAFE(inp
, &time_wait_slots
[cur_tw_slot
], inp_list
, nxt
) {
601 tcp_garbage_collect(inp
, 1);
604 if (++cur_tw_slot
>= N_TIME_WAIT_SLOTS
)
607 lck_rw_done(pcbinfo
->mtx
);
608 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_END
, tws_checked
, cur_tw_slot
,0,0,0);
612 * Cancel all timers for TCP tp.
620 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
624 int tcp_syn_backoff
[TCP_MAXRXTSHIFT
+ 1] =
625 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
627 int tcp_backoff
[TCP_MAXRXTSHIFT
+ 1] =
628 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
630 static int tcp_totbackoff
= 511; /* sum of tcp_backoff[] */
633 * TCP timer processing.
636 tcp_timers(tp
, timer
)
637 register struct tcpcb
*tp
;
641 struct socket
*so_tmp
;
642 struct tcptemp
*t_template
;
650 int isipv6
= (tp
->t_inpcb
->inp_vflag
& INP_IPV4
) == 0;
653 so_tmp
= tp
->t_inpcb
->inp_socket
;
658 * 2 MSL timeout in shutdown went off. If we're closed but
659 * still waiting for peer to close and connection has been idle
660 * too long, or if 2MSL time is up from TIME_WAIT or FIN_WAIT_2,
661 * delete connection control block.
662 * Otherwise, (this case shouldn't happen) check again in a bit
663 * we keep the socket in the main list in that case.
666 tcp_free_sackholes(tp
);
667 if (tp
->t_state
!= TCPS_TIME_WAIT
&&
668 tp
->t_state
!= TCPS_FIN_WAIT_2
&&
669 tp
->t_rcvtime
< tcp_maxidle
) {
670 tp
->t_timer
[TCPT_2MSL
] = (u_int32_t
)tcp_keepintvl
;
679 * Retransmission timer went off. Message has not
680 * been acked within retransmit interval. Back off
681 * to a longer retransmit interval and retransmit one segment.
684 tcp_free_sackholes(tp
);
685 if (++tp
->t_rxtshift
> TCP_MAXRXTSHIFT
) {
686 tp
->t_rxtshift
= TCP_MAXRXTSHIFT
;
687 tcpstat
.tcps_timeoutdrop
++;
688 tp
= tcp_drop(tp
, tp
->t_softerror
?
689 tp
->t_softerror
: ETIMEDOUT
);
690 postevent(so_tmp
, 0, EV_TIMEOUT
);
694 if (tp
->t_rxtshift
== 1) {
696 * first retransmit; record ssthresh and cwnd so they can
697 * be recovered if this turns out to be a "bad" retransmit.
698 * A retransmit is considered "bad" if an ACK for this
699 * segment is received within RTT/2 interval; the assumption
700 * here is that the ACK was already in flight. See
701 * "On Estimating End-to-End Network Path Properties" by
702 * Allman and Paxson for more details.
704 tp
->snd_cwnd_prev
= tp
->snd_cwnd
;
705 tp
->snd_ssthresh_prev
= tp
->snd_ssthresh
;
706 tp
->snd_recover_prev
= tp
->snd_recover
;
707 if (IN_FASTRECOVERY(tp
))
708 tp
->t_flags
|= TF_WASFRECOVERY
;
710 tp
->t_flags
&= ~TF_WASFRECOVERY
;
711 tp
->t_badrxtwin
= tcp_now
+ (tp
->t_srtt
>> (TCP_RTT_SHIFT
));
713 tcpstat
.tcps_rexmttimeo
++;
714 if (tp
->t_state
== TCPS_SYN_SENT
)
715 rexmt
= TCP_REXMTVAL(tp
) * tcp_syn_backoff
[tp
->t_rxtshift
];
717 rexmt
= TCP_REXMTVAL(tp
) * tcp_backoff
[tp
->t_rxtshift
];
718 TCPT_RANGESET(tp
->t_rxtcur
, rexmt
,
719 tp
->t_rttmin
, TCPTV_REXMTMAX
);
720 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
723 * Check for potential Path MTU Discovery Black Hole
726 if (tcp_pmtud_black_hole_detect
&& (tp
->t_state
== TCPS_ESTABLISHED
)) {
727 if (((tp
->t_flags
& (TF_PMTUD
|TF_MAXSEGSNT
)) == (TF_PMTUD
|TF_MAXSEGSNT
)) && (tp
->t_rxtshift
== 2)) {
729 * Enter Path MTU Black-hole Detection mechanism:
730 * - Disable Path MTU Discovery (IP "DF" bit).
731 * - Reduce MTU to lower value than what we negociated with peer.
734 tp
->t_flags
&= ~TF_PMTUD
; /* Disable Path MTU Discovery for now */
735 tp
->t_flags
|= TF_BLACKHOLE
; /* Record that we may have found a black hole */
736 optlen
= tp
->t_maxopd
- tp
->t_maxseg
;
737 tp
->t_pmtud_saved_maxopd
= tp
->t_maxopd
; /* Keep track of previous MSS */
738 if (tp
->t_maxopd
> tcp_pmtud_black_hole_mss
)
739 tp
->t_maxopd
= tcp_pmtud_black_hole_mss
; /* Reduce the MSS to intermediary value */
741 tp
->t_maxopd
= /* use the default MSS */
743 isipv6
? tcp_v6mssdflt
:
747 tp
->t_maxseg
= tp
->t_maxopd
- optlen
;
750 * If further retransmissions are still unsuccessful with a lowered MTU,
751 * maybe this isn't a Black Hole and we restore the previous MSS and
752 * blackhole detection flags.
756 if ((tp
->t_flags
& TF_BLACKHOLE
) && (tp
->t_rxtshift
> 4)) {
757 tp
->t_flags
|= TF_PMTUD
;
758 tp
->t_flags
&= ~TF_BLACKHOLE
;
759 optlen
= tp
->t_maxopd
- tp
->t_maxseg
;
760 tp
->t_maxopd
= tp
->t_pmtud_saved_maxopd
;
761 tp
->t_maxseg
= tp
->t_maxopd
- optlen
;
768 * Disable rfc1323 and rfc1644 if we haven't got any response to
769 * our SYN (after we reach the threshold) to work-around some
770 * broken terminal servers (most of which have hopefully been
771 * retired) that have bad VJ header compression code which
772 * trashes TCP segments containing unknown-to-them TCP options.
774 if ((tp
->t_state
== TCPS_SYN_SENT
) &&
775 (tp
->t_rxtshift
== tcp_broken_peer_syn_rxmit_thres
))
776 tp
->t_flags
&= ~(TF_REQ_SCALE
|TF_REQ_TSTMP
|TF_REQ_CC
);
778 * If losing, let the lower level know and try for
779 * a better route. Also, if we backed off this far,
780 * our srtt estimate is probably bogus. Clobber it
781 * so we'll take the next rtt measurement as our srtt;
782 * move the current srtt into rttvar to keep the current
783 * retransmit times until then.
785 if (tp
->t_rxtshift
> TCP_MAXRXTSHIFT
/ 4) {
788 in6_losing(tp
->t_inpcb
);
791 in_losing(tp
->t_inpcb
);
792 tp
->t_rttvar
+= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
795 tp
->snd_nxt
= tp
->snd_una
;
797 * Note: We overload snd_recover to function also as the
798 * snd_last variable described in RFC 2582
800 tp
->snd_recover
= tp
->snd_max
;
802 * Force a segment to be sent.
804 tp
->t_flags
|= TF_ACKNOW
;
806 * If timing a segment in this window, stop the timer.
810 * Close the congestion window down to one segment
811 * (we'll open it by one segment for each ack we get).
812 * Since we probably have a window's worth of unacked
813 * data accumulated, this "slow start" keeps us from
814 * dumping all that data as back-to-back packets (which
815 * might overwhelm an intermediate gateway).
817 * There are two phases to the opening: Initially we
818 * open by one mss on each ack. This makes the window
819 * size increase exponentially with time. If the
820 * window is larger than the path can handle, this
821 * exponential growth results in dropped packet(s)
822 * almost immediately. To get more time between
823 * drops but still "push" the network to take advantage
824 * of improving conditions, we switch from exponential
825 * to linear window opening at some threshhold size.
826 * For a threshhold, we use half the current window
827 * size, truncated to a multiple of the mss.
829 * (the minimum cwnd that will give us exponential
830 * growth is 2 mss. We don't allow the threshhold
833 if (tp
->t_state
>= TCPS_ESTABLISHED
) {
834 u_int win
= min(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 / tp
->t_maxseg
;
837 tp
->snd_cwnd
= tp
->t_maxseg
;
838 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
839 tp
->t_bytes_acked
= 0;
843 EXIT_FASTRECOVERY(tp
);
844 (void) tcp_output(tp
);
848 * Persistance timer into zero window.
849 * Force a byte to be output, if possible.
852 tcpstat
.tcps_persisttimeo
++;
854 * Hack: if the peer is dead/unreachable, we do not
855 * time out if the window is closed. After a full
856 * backoff, drop the connection if the idle time
857 * (no responses to probes) reaches the maximum
858 * backoff that we would use if retransmitting.
860 if (tp
->t_rxtshift
== TCP_MAXRXTSHIFT
&&
861 (tp
->t_rcvtime
>= tcp_maxpersistidle
||
862 tp
->t_rcvtime
>= TCP_REXMTVAL(tp
) * tcp_totbackoff
)) {
863 tcpstat
.tcps_persistdrop
++;
864 so_tmp
= tp
->t_inpcb
->inp_socket
;
865 tp
= tcp_drop(tp
, ETIMEDOUT
);
866 postevent(so_tmp
, 0, EV_TIMEOUT
);
872 (void) tcp_output(tp
);
877 * Keep-alive timer went off; send something
878 * or drop connection if idle for too long.
881 tcpstat
.tcps_keeptimeo
++;
882 if (tp
->t_state
< TCPS_ESTABLISHED
)
884 if ((always_keepalive
||
885 tp
->t_inpcb
->inp_socket
->so_options
& SO_KEEPALIVE
) &&
886 (tp
->t_state
<= TCPS_CLOSING
|| tp
->t_state
== TCPS_FIN_WAIT_2
)) {
887 if (tp
->t_rcvtime
>= TCP_KEEPIDLE(tp
) + (u_int32_t
)tcp_maxidle
)
890 * Send a packet designed to force a response
891 * if the peer is up and reachable:
892 * either an ACK if the connection is still alive,
893 * or an RST if the peer has closed the connection
894 * due to timeout or reboot.
895 * Using sequence number tp->snd_una-1
896 * causes the transmitted zero-length segment
897 * to lie outside the receive window;
898 * by the protocol spec, this requires the
899 * correspondent TCP to respond.
901 tcpstat
.tcps_keepprobe
++;
902 t_template
= tcp_maketemplate(tp
);
904 unsigned int ifscope
;
906 if (tp
->t_inpcb
->inp_flags
& INP_BOUND_IF
)
907 ifscope
= tp
->t_inpcb
->inp_boundif
;
909 ifscope
= IFSCOPE_NONE
;
911 tcp_respond(tp
, t_template
->tt_ipgen
,
912 &t_template
->tt_t
, (struct mbuf
*)NULL
,
913 tp
->rcv_nxt
, tp
->snd_una
- 1, 0, ifscope
);
914 (void) m_free(dtom(t_template
));
916 tp
->t_timer
[TCPT_KEEP
] = tcp_keepintvl
;
918 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
);
922 if (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)
923 tcp_trace(TA_USER
, ostate
, tp
, (void *)0, (struct tcphdr
*)0,
927 tcpstat
.tcps_keepdrops
++;
928 tp
= tcp_drop(tp
, ETIMEDOUT
);
929 postevent(so_tmp
, 0, EV_TIMEOUT
);