]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_timer.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_timer.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
2d21ac55
A
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.
39037602 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39037602 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
9bccf70c 61 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.11 2001/08/22 00:59:12 silby Exp $
1c79356b
A
62 */
63
1c79356b
A
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
9bccf70c 68#include <sys/mbuf.h>
1c79356b
A
69#include <sys/sysctl.h>
70#include <sys/socket.h>
71#include <sys/socketvar.h>
72#include <sys/protosw.h>
b0d623f7 73#include <sys/domain.h>
6d2010ae
A
74#include <sys/mcache.h>
75#include <sys/queue.h>
91447636 76#include <kern/locks.h>
0a7de745 77#include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
39236c6e 78#include <mach/boolean.h>
1c79356b
A
79
80#include <net/route.h>
316670eb 81#include <net/if_var.h>
fe8ab488 82#include <net/ntstat.h>
1c79356b
A
83
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
1c79356b 86#include <netinet/in_pcb.h>
cb323159 87#include <netinet/in_var.h>
9bccf70c 88#include <netinet6/in6_pcb.h>
1c79356b
A
89#include <netinet/ip_var.h>
90#include <netinet/tcp.h>
3e170ce0 91#include <netinet/tcp_cache.h>
1c79356b
A
92#include <netinet/tcp_fsm.h>
93#include <netinet/tcp_seq.h>
94#include <netinet/tcp_timer.h>
95#include <netinet/tcp_var.h>
6d2010ae 96#include <netinet/tcp_cc.h>
b0d623f7 97#include <netinet6/tcp6_var.h>
1c79356b
A
98#include <netinet/tcpip.h>
99#if TCPDEBUG
100#include <netinet/tcp_debug.h>
101#endif
cb323159
A
102#include <netinet/tcp_log.h>
103
1c79356b 104#include <sys/kdebug.h>
6d2010ae 105#include <mach/sdt.h>
39236c6e 106#include <netinet/mptcp_var.h>
1c79356b 107
fe8ab488 108/* Max number of times a stretch ack can be delayed on a connection */
0a7de745 109#define TCP_STRETCHACK_DELAY_THRESHOLD 5
fe8ab488 110
3e170ce0
A
111/*
112 * If the host processor has been sleeping for too long, this is the threshold
113 * used to avoid sending stale retransmissions.
114 */
0a7de745 115#define TCP_SLEEP_TOO_LONG (10 * 60 * 1000) /* 10 minutes in ms */
3e170ce0 116
39236c6e
A
117/* tcp timer list */
118struct tcptimerlist tcp_timer_list;
119
120/* List of pcbs in timewait state, protected by tcbinfo's ipi_lock */
121struct tcptailq tcp_tw_tailq;
122
9bccf70c
A
123static int
124sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
125{
5ba3f43e 126#pragma unused(arg2)
4ba76501
A
127 int error, temp;
128 long s, tt;
1c79356b 129
5ba3f43e 130 tt = *(int *)arg1;
4ba76501
A
131 s = tt * 1000 / TCP_RETRANSHZ;
132 if (tt < 0 || s > INT_MAX) {
cb323159
A
133 return EINVAL;
134 }
4ba76501 135 temp = (int)s;
1c79356b 136
4ba76501 137 error = sysctl_handle_int(oidp, &temp, 0, req);
0a7de745
A
138 if (error || !req->newptr) {
139 return error;
140 }
9bccf70c 141
ea3f0419 142 tt = temp * TCP_RETRANSHZ / 1000;
4ba76501 143 if (tt < 1 || tt > INT_MAX) {
0a7de745
A
144 return EINVAL;
145 }
9bccf70c 146
4ba76501 147 *(int *)arg1 = (int)tt;
5ba3f43e 148 SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2, *(int*)arg1);
0a7de745 149 return 0;
9bccf70c 150}
1c79356b 151
5ba3f43e 152#if SYSCTL_SKMEM
0a7de745 153int tcp_keepinit = TCPTV_KEEP_INIT;
5ba3f43e
A
154SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit,
155 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
156 &tcp_keepinit, offsetof(skmem_sysctl, tcp.keepinit),
0a7de745 157 sysctl_msec_to_ticks, "I", "");
5ba3f43e 158
0a7de745 159int tcp_keepidle = TCPTV_KEEP_IDLE;
5ba3f43e
A
160SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle,
161 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
162 &tcp_keepidle, offsetof(skmem_sysctl, tcp.keepidle),
0a7de745 163 sysctl_msec_to_ticks, "I", "");
5ba3f43e 164
0a7de745 165int tcp_keepintvl = TCPTV_KEEPINTVL;
5ba3f43e
A
166SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl,
167 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
168 &tcp_keepintvl, offsetof(skmem_sysctl, tcp.keepintvl),
0a7de745 169 sysctl_msec_to_ticks, "I", "");
5ba3f43e
A
170
171SYSCTL_SKMEM_TCP_INT(OID_AUTO, keepcnt,
172 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
173 int, tcp_keepcnt, TCPTV_KEEPCNT, "number of times to repeat keepalive");
174
0a7de745 175int tcp_msl = TCPTV_MSL;
5ba3f43e
A
176SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl,
177 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
178 &tcp_msl, offsetof(skmem_sysctl, tcp.msl),
0a7de745 179 sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
5ba3f43e
A
180#else /* SYSCTL_SKMEM */
181int tcp_keepinit;
fe8ab488
A
182SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit,
183 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
9bccf70c
A
184 &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", "");
185
5ba3f43e 186int tcp_keepidle;
fe8ab488
A
187SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle,
188 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
9bccf70c
A
189 &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", "");
190
5ba3f43e 191int tcp_keepintvl;
fe8ab488
A
192SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl,
193 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
9bccf70c
A
194 &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", "");
195
5ba3f43e 196int tcp_keepcnt;
fe8ab488
A
197SYSCTL_INT(_net_inet_tcp, OID_AUTO, keepcnt,
198 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
199 &tcp_keepcnt, 0, "number of times to repeat keepalive");
39236c6e 200
5ba3f43e 201int tcp_msl;
fe8ab488
A
202SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl,
203 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
9bccf70c 204 &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime");
5ba3f43e 205#endif /* SYSCTL_SKMEM */
1c79356b 206
39037602 207/*
fe8ab488
A
208 * Avoid DoS via TCP Robustness in Persist Condition
209 * (see http://www.ietf.org/id/draft-ananth-tcpm-persist-02.txt)
210 * by allowing a system wide maximum persistence timeout value when in
211 * Zero Window Probe mode.
212 *
213 * Expressed in milliseconds to be consistent without timeout related
214 * values, the TCP socket option is in seconds.
6d2010ae 215 */
5ba3f43e
A
216#if SYSCTL_SKMEM
217u_int32_t tcp_max_persist_timeout = 0;
218SYSCTL_PROC(_net_inet_tcp, OID_AUTO, max_persist_timeout,
219 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
220 &tcp_max_persist_timeout, offsetof(skmem_sysctl, tcp.max_persist_timeout),
0a7de745 221 sysctl_msec_to_ticks, "I", "Maximum persistence timeout for ZWP");
5ba3f43e 222#else /* SYSCTL_SKMEM */
6d2010ae 223u_int32_t tcp_max_persist_timeout = 0;
fe8ab488
A
224SYSCTL_PROC(_net_inet_tcp, OID_AUTO, max_persist_timeout,
225 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
39037602 226 &tcp_max_persist_timeout, 0, sysctl_msec_to_ticks, "I",
fe8ab488 227 "Maximum persistence timeout for ZWP");
5ba3f43e 228#endif /* SYSCTL_SKMEM */
6d2010ae 229
5ba3f43e
A
230SYSCTL_SKMEM_TCP_INT(OID_AUTO, always_keepalive,
231 CTLFLAG_RW | CTLFLAG_LOCKED, static int, always_keepalive, 0,
0a7de745 232 "Assume SO_KEEPALIVE on all TCP connections");
1c79356b 233
fe8ab488
A
234/*
235 * This parameter determines how long the timer list will stay in fast or
39037602 236 * quick mode even though all connections are idle. In this state, the
fe8ab488 237 * timer will run more frequently anticipating new data.
6d2010ae 238 */
5ba3f43e
A
239SYSCTL_SKMEM_TCP_INT(OID_AUTO, timer_fastmode_idlemax,
240 CTLFLAG_RW | CTLFLAG_LOCKED, int, timer_fastmode_idlemax,
0a7de745 241 TCP_FASTMODE_IDLERUN_MAX, "Maximum idle generations in fast mode");
6d2010ae 242
b0d623f7
A
243/*
244 * See tcp_syn_backoff[] for interval values between SYN retransmits;
245 * the value set below defines the number of retransmits, before we
246 * disable the timestamp and window scaling options during subsequent
247 * SYN retransmits. Setting it to 0 disables the dropping off of those
248 * two options.
249 */
5ba3f43e
A
250SYSCTL_SKMEM_TCP_INT(OID_AUTO, broken_peer_syn_rexmit_thres,
251 CTLFLAG_RW | CTLFLAG_LOCKED, static int, tcp_broken_peer_syn_rxmit_thres,
0a7de745 252 10, "Number of retransmitted SYNs before disabling RFC 1323 "
fe8ab488 253 "options on local connections");
39236c6e 254
6d2010ae 255static int tcp_timer_advanced = 0;
fe8ab488
A
256SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_timer_advanced,
257 CTLFLAG_RD | CTLFLAG_LOCKED, &tcp_timer_advanced, 0,
258 "Number of times one of the timers was advanced");
6d2010ae
A
259
260static int tcp_resched_timerlist = 0;
fe8ab488 261SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_resched_timerlist,
39037602 262 CTLFLAG_RD | CTLFLAG_LOCKED, &tcp_resched_timerlist, 0,
6d2010ae
A
263 "Number of times timer list was rescheduled as part of processing a packet");
264
5ba3f43e
A
265SYSCTL_SKMEM_TCP_INT(OID_AUTO, pmtud_blackhole_detection,
266 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_pmtud_black_hole_detect, 1,
fe8ab488 267 "Path MTU Discovery Black Hole Detection");
b0d623f7 268
5ba3f43e
A
269SYSCTL_SKMEM_TCP_INT(OID_AUTO, pmtud_blackhole_mss,
270 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_pmtud_black_hole_mss, 1200,
fe8ab488 271 "Path MTU Discovery Black Hole Detection lowered MSS");
b0d623f7 272
cb323159
A
273#if (DEBUG || DEVELOPMENT)
274int tcp_probe_if_fix_port = 0;
275SYSCTL_INT(_net_inet_tcp, OID_AUTO, probe_if_fix_port,
276 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
277 &tcp_probe_if_fix_port, 0, "");
278#endif /* (DEBUG || DEVELOPMENT) */
279
39037602
A
280static u_int32_t tcp_mss_rec_medium = 1200;
281static u_int32_t tcp_mss_rec_low = 512;
282
0a7de745 283#define TCP_REPORT_STATS_INTERVAL 43200 /* 12 hours, in seconds */
3e170ce0 284int tcp_report_stats_interval = TCP_REPORT_STATS_INTERVAL;
3e170ce0 285
39236c6e
A
286/* performed garbage collection of "used" sockets */
287static boolean_t tcp_gc_done = FALSE;
288
fe8ab488 289/* max idle probes */
0a7de745 290int tcp_maxpersistidle = TCPTV_KEEP_IDLE;
1c79356b 291
fe8ab488
A
292/*
293 * TCP delack timer is set to 100 ms. Since the processing of timer list
294 * in fast mode will happen no faster than 100 ms, the delayed ack timer
295 * will fire some where between 100 and 200 ms.
6d2010ae 296 */
0a7de745 297int tcp_delack = TCP_RETRANSHZ / 10;
6d2010ae 298
39236c6e
A
299#if MPTCP
300/*
301 * MP_JOIN retransmission of 3rd ACK will be every 500 msecs without backoff
302 */
0a7de745 303int tcp_jack_rxmt = TCP_RETRANSHZ / 2;
39236c6e 304#endif /* MPTCP */
1c79356b 305
3e170ce0
A
306static boolean_t tcp_itimer_done = FALSE;
307
6d2010ae
A
308static void tcp_remove_timer(struct tcpcb *tp);
309static void tcp_sched_timerlist(uint32_t offset);
3e170ce0
A
310static u_int32_t tcp_run_conn_timer(struct tcpcb *tp, u_int16_t *mode,
311 u_int16_t probe_if_index);
6d2010ae 312static inline void tcp_set_lotimer_index(struct tcpcb *);
fe8ab488 313__private_extern__ void tcp_remove_from_time_wait(struct inpcb *inp);
39037602 314static inline void tcp_update_mss_core(struct tcpcb *tp, struct ifnet *ifp);
fe8ab488 315__private_extern__ void tcp_report_stats(void);
6d2010ae 316
0a7de745 317static u_int64_t tcp_last_report_time;
3e170ce0
A
318
319/*
320 * Structure to store previously reported stats so that we can send
321 * incremental changes in each report interval.
322 */
323struct tcp_last_report_stats {
0a7de745
A
324 u_int32_t tcps_connattempt;
325 u_int32_t tcps_accepts;
326 u_int32_t tcps_ecn_client_setup;
327 u_int32_t tcps_ecn_server_setup;
328 u_int32_t tcps_ecn_client_success;
329 u_int32_t tcps_ecn_server_success;
330 u_int32_t tcps_ecn_not_supported;
331 u_int32_t tcps_ecn_lost_syn;
332 u_int32_t tcps_ecn_lost_synack;
333 u_int32_t tcps_ecn_recv_ce;
334 u_int32_t tcps_ecn_recv_ece;
335 u_int32_t tcps_ecn_sent_ece;
336 u_int32_t tcps_ecn_conn_recv_ce;
337 u_int32_t tcps_ecn_conn_recv_ece;
338 u_int32_t tcps_ecn_conn_plnoce;
339 u_int32_t tcps_ecn_conn_pl_ce;
340 u_int32_t tcps_ecn_conn_nopl_ce;
341 u_int32_t tcps_ecn_fallback_synloss;
342 u_int32_t tcps_ecn_fallback_reorder;
343 u_int32_t tcps_ecn_fallback_ce;
3e170ce0
A
344
345 /* TFO-related statistics */
0a7de745
A
346 u_int32_t tcps_tfo_syn_data_rcv;
347 u_int32_t tcps_tfo_cookie_req_rcv;
348 u_int32_t tcps_tfo_cookie_sent;
349 u_int32_t tcps_tfo_cookie_invalid;
350 u_int32_t tcps_tfo_cookie_req;
351 u_int32_t tcps_tfo_cookie_rcv;
352 u_int32_t tcps_tfo_syn_data_sent;
353 u_int32_t tcps_tfo_syn_data_acked;
354 u_int32_t tcps_tfo_syn_loss;
355 u_int32_t tcps_tfo_blackhole;
356 u_int32_t tcps_tfo_cookie_wrong;
357 u_int32_t tcps_tfo_no_cookie_rcv;
358 u_int32_t tcps_tfo_heuristics_disable;
359 u_int32_t tcps_tfo_sndblackhole;
5ba3f43e
A
360
361 /* MPTCP-related statistics */
0a7de745
A
362 u_int32_t tcps_mptcp_handover_attempt;
363 u_int32_t tcps_mptcp_interactive_attempt;
364 u_int32_t tcps_mptcp_aggregate_attempt;
365 u_int32_t tcps_mptcp_fp_handover_attempt;
366 u_int32_t tcps_mptcp_fp_interactive_attempt;
367 u_int32_t tcps_mptcp_fp_aggregate_attempt;
368 u_int32_t tcps_mptcp_heuristic_fallback;
369 u_int32_t tcps_mptcp_fp_heuristic_fallback;
370 u_int32_t tcps_mptcp_handover_success_wifi;
371 u_int32_t tcps_mptcp_handover_success_cell;
372 u_int32_t tcps_mptcp_interactive_success;
373 u_int32_t tcps_mptcp_aggregate_success;
374 u_int32_t tcps_mptcp_fp_handover_success_wifi;
375 u_int32_t tcps_mptcp_fp_handover_success_cell;
376 u_int32_t tcps_mptcp_fp_interactive_success;
377 u_int32_t tcps_mptcp_fp_aggregate_success;
378 u_int32_t tcps_mptcp_handover_cell_from_wifi;
379 u_int32_t tcps_mptcp_handover_wifi_from_cell;
380 u_int32_t tcps_mptcp_interactive_cell_from_wifi;
381 u_int64_t tcps_mptcp_handover_cell_bytes;
382 u_int64_t tcps_mptcp_interactive_cell_bytes;
383 u_int64_t tcps_mptcp_aggregate_cell_bytes;
384 u_int64_t tcps_mptcp_handover_all_bytes;
385 u_int64_t tcps_mptcp_interactive_all_bytes;
386 u_int64_t tcps_mptcp_aggregate_all_bytes;
387 u_int32_t tcps_mptcp_back_to_wifi;
388 u_int32_t tcps_mptcp_wifi_proxy;
389 u_int32_t tcps_mptcp_cell_proxy;
390 u_int32_t tcps_mptcp_triggered_cell;
3e170ce0
A
391};
392
fe8ab488 393
6d2010ae
A
394/* Returns true if the timer is on the timer list */
395#define TIMER_IS_ON_LIST(tp) ((tp)->t_flags & TF_TIMER_ONLIST)
396
8a3053a0 397/* Run the TCP timerlist atleast once every hour */
fe8ab488 398#define TCP_TIMERLIST_MAX_OFFSET (60 * 60 * TCP_RETRANSHZ)
6d2010ae 399
2d21ac55 400
fe8ab488 401static void add_to_time_wait_locked(struct tcpcb *tp, uint32_t delay);
39236c6e 402static boolean_t tcp_garbage_collect(struct inpcb *, int);
1c79356b 403
39037602
A
404#define TIMERENTRY_TO_TP(te) ((struct tcpcb *)((uintptr_t)te - offsetof(struct tcpcb, tentry.le.le_next)))
405
0a7de745
A
406#define VERIFY_NEXT_LINK(elm, field) do { \
407 if (LIST_NEXT((elm),field) != NULL && \
408 LIST_NEXT((elm),field)->field.le_prev != \
409 &((elm)->field.le_next)) \
410 panic("Bad link elm %p next->prev != elm", (elm)); \
39037602
A
411} while(0)
412
0a7de745
A
413#define VERIFY_PREV_LINK(elm, field) do { \
414 if (*(elm)->field.le_prev != (elm)) \
415 panic("Bad link elm %p prev->next != elm", (elm)); \
39037602
A
416} while(0)
417
418#define TCP_SET_TIMER_MODE(mode, i) do { \
419 if (IS_TIMER_HZ_10MS(i)) \
0a7de745 420 (mode) |= TCP_TIMERLIST_10MS_MODE; \
39037602 421 else if (IS_TIMER_HZ_100MS(i)) \
0a7de745 422 (mode) |= TCP_TIMERLIST_100MS_MODE; \
39037602 423 else \
0a7de745 424 (mode) |= TCP_TIMERLIST_500MS_MODE; \
39037602
A
425} while(0)
426
427#if (DEVELOPMENT || DEBUG)
428SYSCTL_UINT(_net_inet_tcp, OID_AUTO, mss_rec_medium,
429 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_mss_rec_medium, 0,
430 "Medium MSS based on recommendation in link status report");
431SYSCTL_UINT(_net_inet_tcp, OID_AUTO, mss_rec_low,
432 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_mss_rec_low, 0,
433 "Low MSS based on recommendation in link status report");
434
435static int32_t tcp_change_mss_recommended = 0;
436static int
437sysctl_change_mss_recommended SYSCTL_HANDLER_ARGS
438{
439#pragma unused(oidp, arg1, arg2)
440 int i, err = 0, changed = 0;
441 struct ifnet *ifp;
442 struct if_link_status ifsr;
443 struct if_cellular_status_v1 *new_cell_sr;
444 err = sysctl_io_number(req, tcp_change_mss_recommended,
0a7de745 445 sizeof(int32_t), &i, &changed);
39037602 446 if (changed) {
f427ee49
A
447 if (i < 0 || i > UINT16_MAX) {
448 return EINVAL;
449 }
39037602
A
450 ifnet_head_lock_shared();
451 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
452 if (IFNET_IS_CELLULAR(ifp)) {
0a7de745 453 bzero(&ifsr, sizeof(ifsr));
39037602
A
454 new_cell_sr = &ifsr.ifsr_u.ifsr_cell.if_cell_u.if_status_v1;
455 ifsr.ifsr_version = IF_CELLULAR_STATUS_REPORT_CURRENT_VERSION;
456 ifsr.ifsr_len = sizeof(*new_cell_sr);
457
458 /* Set MSS recommended */
459 new_cell_sr->valid_bitmask |= IF_CELL_UL_MSS_RECOMMENDED_VALID;
f427ee49 460 new_cell_sr->mss_recommended = (uint16_t)i;
0a7de745 461 err = ifnet_link_status_report(ifp, new_cell_sr, sizeof(new_cell_sr));
39037602
A
462 if (err == 0) {
463 tcp_change_mss_recommended = i;
464 } else {
465 break;
466 }
467 }
468 }
469 ifnet_head_done();
470 }
0a7de745 471 return err;
39037602
A
472}
473
474SYSCTL_PROC(_net_inet_tcp, OID_AUTO, change_mss_recommended,
475 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_change_mss_recommended,
476 0, sysctl_change_mss_recommended, "IU", "Change MSS recommended");
477
478SYSCTL_INT(_net_inet_tcp, OID_AUTO, report_stats_interval,
479 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_report_stats_interval, 0,
480 "Report stats interval");
481#endif /* (DEVELOPMENT || DEBUG) */
482
483/*
484 * Macro to compare two timers. If there is a reset of the sign bit,
485 * it is safe to assume that the timer has wrapped around. By doing
486 * signed comparision, we take care of wrap around such that the value
487 * with the sign bit reset is actually ahead of the other.
488 */
489inline int32_t
0a7de745
A
490timer_diff(uint32_t t1, uint32_t toff1, uint32_t t2, uint32_t toff2)
491{
39037602 492 return (int32_t)((t1 + toff1) - (t2 + toff2));
cb323159 493}
39037602 494
39236c6e
A
495/*
496 * Add to tcp timewait list, delay is given in milliseconds.
497 */
498static void
499add_to_time_wait_locked(struct tcpcb *tp, uint32_t delay)
1c79356b 500{
39236c6e
A
501 struct inpcbinfo *pcbinfo = &tcbinfo;
502 struct inpcb *inp = tp->t_inpcb;
6d2010ae 503 uint32_t timer;
1c79356b 504
39236c6e 505 /* pcb list should be locked when we get here */
5ba3f43e 506 LCK_RW_ASSERT(pcbinfo->ipi_lock, LCK_RW_ASSERT_EXCLUSIVE);
1c79356b 507
39236c6e
A
508 /* We may get here multiple times, so check */
509 if (!(inp->inp_flags2 & INP2_TIMEWAIT)) {
510 pcbinfo->ipi_twcount++;
511 inp->inp_flags2 |= INP2_TIMEWAIT;
39037602 512
39236c6e
A
513 /* Remove from global inp list */
514 LIST_REMOVE(inp, inp_list);
515 } else {
516 TAILQ_REMOVE(&tcp_tw_tailq, tp, t_twentry);
517 }
2d21ac55 518
39236c6e
A
519 /* Compute the time at which this socket can be closed */
520 timer = tcp_now + delay;
39037602 521
39236c6e 522 /* We will use the TCPT_2MSL timer for tracking this delay */
2d21ac55 523
0a7de745 524 if (TIMER_IS_ON_LIST(tp)) {
39236c6e 525 tcp_remove_timer(tp);
0a7de745 526 }
39236c6e 527 tp->t_timer[TCPT_2MSL] = timer;
1c79356b 528
39236c6e 529 TAILQ_INSERT_TAIL(&tcp_tw_tailq, tp, t_twentry);
1c79356b
A
530}
531
39236c6e
A
532void
533add_to_time_wait(struct tcpcb *tp, uint32_t delay)
91447636 534{
39236c6e 535 struct inpcbinfo *pcbinfo = &tcbinfo;
0a7de745 536 if (tp->t_inpcb->inp_socket->so_options & SO_NOWAKEFROMSLEEP) {
fe8ab488 537 socket_post_kev_msg_closed(tp->t_inpcb->inp_socket);
0a7de745 538 }
39236c6e 539
f427ee49
A
540 tcp_del_fsw_flow(tp);
541
3e170ce0
A
542 /* 19182803: Notify nstat that connection is closing before waiting. */
543 nstat_pcb_detach(tp->t_inpcb);
544
39236c6e 545 if (!lck_rw_try_lock_exclusive(pcbinfo->ipi_lock)) {
5ba3f43e 546 socket_unlock(tp->t_inpcb->inp_socket, 0);
39236c6e 547 lck_rw_lock_exclusive(pcbinfo->ipi_lock);
5ba3f43e 548 socket_lock(tp->t_inpcb->inp_socket, 0);
91447636 549 }
6d2010ae 550 add_to_time_wait_locked(tp, delay);
39236c6e
A
551 lck_rw_done(pcbinfo->ipi_lock);
552
553 inpcb_gc_sched(pcbinfo, INPCB_TIMER_LAZY);
91447636 554}
1c79356b 555
39236c6e
A
556/* If this is on time wait queue, remove it. */
557void
558tcp_remove_from_time_wait(struct inpcb *inp)
559{
560 struct tcpcb *tp = intotcpcb(inp);
0a7de745 561 if (inp->inp_flags2 & INP2_TIMEWAIT) {
39236c6e 562 TAILQ_REMOVE(&tcp_tw_tailq, tp, t_twentry);
0a7de745 563 }
39236c6e
A
564}
565
566static boolean_t
b0d623f7 567tcp_garbage_collect(struct inpcb *inp, int istimewait)
2d21ac55 568{
39236c6e 569 boolean_t active = FALSE;
5ba3f43e 570 struct socket *so, *mp_so = NULL;
2d21ac55
A
571 struct tcpcb *tp;
572
b0d623f7
A
573 so = inp->inp_socket;
574 tp = intotcpcb(inp);
2d21ac55 575
5ba3f43e
A
576 if (so->so_flags & SOF_MP_SUBFLOW) {
577 mp_so = mptetoso(tptomptp(tp)->mpt_mpte);
578 if (!socket_try_lock(mp_so)) {
579 mp_so = NULL;
580 active = TRUE;
581 goto out;
582 }
cb323159
A
583 if (mpsotomppcb(mp_so)->mpp_inside > 0) {
584 os_log(mptcp_log_handle, "%s - %lx: Still inside %d usecount %d\n", __func__,
585 (unsigned long)VM_KERNEL_ADDRPERM(mpsotompte(mp_so)),
586 mpsotomppcb(mp_so)->mpp_inside,
587 mp_so->so_usecount);
588 socket_unlock(mp_so, 0);
589 mp_so = NULL;
590 active = TRUE;
591 goto out;
592 }
593 /* We call socket_unlock with refcount further below */
5ba3f43e 594 mp_so->so_usecount++;
cb323159 595 tptomptp(tp)->mpt_mpte->mpte_mppcb->mpp_inside++;
5ba3f43e
A
596 }
597
b0d623f7
A
598 /*
599 * Skip if still in use or busy; it would have been more efficient
600 * if we were to test so_usecount against 0, but this isn't possible
601 * due to the current implementation of tcp_dropdropablreq() where
602 * overflow sockets that are eligible for garbage collection have
603 * their usecounts set to 1.
604 */
5ba3f43e
A
605 if (!lck_mtx_try_lock_spin(&inp->inpcb_mtx)) {
606 active = TRUE;
607 goto out;
608 }
2d21ac55 609
b0d623f7
A
610 /* Check again under the lock */
611 if (so->so_usecount > 1) {
0a7de745 612 if (inp->inp_wantcnt == WNT_STOPUSING) {
39236c6e 613 active = TRUE;
0a7de745 614 }
6d2010ae 615 lck_mtx_unlock(&inp->inpcb_mtx);
5ba3f43e 616 goto out;
39236c6e
A
617 }
618
5ba3f43e
A
619 if (istimewait && TSTMP_GEQ(tcp_now, tp->t_timer[TCPT_2MSL]) &&
620 tp->t_state != TCPS_CLOSED) {
39236c6e
A
621 /* Become a regular mutex */
622 lck_mtx_convert_spin(&inp->inpcb_mtx);
623 tcp_close(tp);
b0d623f7 624 }
2d21ac55 625
b0d623f7
A
626 /*
627 * Overflowed socket dropped from the listening queue? Do this
628 * only if we are called to clean up the time wait slots, since
629 * tcp_dropdropablreq() considers a socket to have been fully
630 * dropped after add_to_time_wait() is finished.
39236c6e
A
631 * Also handle the case of connections getting closed by the peer
632 * while in the queue as seen with rdar://6422317
633 *
b0d623f7 634 */
39236c6e 635 if (so->so_usecount == 1 &&
b0d623f7 636 ((istimewait && (so->so_flags & SOF_OVERFLOW)) ||
39236c6e
A
637 ((tp != NULL) && (tp->t_state == TCPS_CLOSED) &&
638 (so->so_head != NULL) &&
0a7de745
A
639 ((so->so_state & (SS_INCOMP | SS_CANTSENDMORE | SS_CANTRCVMORE)) ==
640 (SS_INCOMP | SS_CANTSENDMORE | SS_CANTRCVMORE))))) {
b0d623f7
A
641 if (inp->inp_state != INPCB_STATE_DEAD) {
642 /* Become a regular mutex */
6d2010ae 643 lck_mtx_convert_spin(&inp->inpcb_mtx);
0a7de745 644 if (SOCK_CHECK_DOM(so, PF_INET6)) {
b0d623f7 645 in6_pcbdetach(inp);
f427ee49
A
646 } else {
647 in_pcbdetach(inp);
648 }
2d21ac55 649 }
d190cdc3 650 VERIFY(so->so_usecount > 0);
b0d623f7 651 so->so_usecount--;
0a7de745 652 if (inp->inp_wantcnt == WNT_STOPUSING) {
39236c6e 653 active = TRUE;
0a7de745 654 }
6d2010ae 655 lck_mtx_unlock(&inp->inpcb_mtx);
5ba3f43e 656 goto out;
b0d623f7 657 } else if (inp->inp_wantcnt != WNT_STOPUSING) {
6d2010ae 658 lck_mtx_unlock(&inp->inpcb_mtx);
5ba3f43e
A
659 active = FALSE;
660 goto out;
b0d623f7 661 }
2d21ac55 662
b0d623f7 663 /*
39037602
A
664 * We get here because the PCB is no longer searchable
665 * (WNT_STOPUSING); detach (if needed) and dispose if it is dead
666 * (usecount is 0). This covers all cases, including overflow
667 * sockets and those that are considered as "embryonic",
668 * i.e. created by sonewconn() in TCP input path, and have
39236c6e 669 * not yet been committed. For the former, we reduce the usecount
39037602 670 * to 0 as done by the code above. For the latter, the usecount
39236c6e 671 * would have reduced to 0 as part calling soabort() when the
b0d623f7
A
672 * socket is dropped at the end of tcp_input().
673 */
674 if (so->so_usecount == 0) {
6d2010ae 675 DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp,
0a7de745 676 struct tcpcb *, tp, int32_t, TCPS_CLOSED);
b0d623f7 677 /* Become a regular mutex */
6d2010ae 678 lck_mtx_convert_spin(&inp->inpcb_mtx);
39236c6e
A
679
680 /*
39037602 681 * If this tp still happens to be on the timer list,
ebb1b9f4
A
682 * take it out
683 */
684 if (TIMER_IS_ON_LIST(tp)) {
685 tcp_remove_timer(tp);
686 }
687
b0d623f7 688 if (inp->inp_state != INPCB_STATE_DEAD) {
0a7de745 689 if (SOCK_CHECK_DOM(so, PF_INET6)) {
b0d623f7 690 in6_pcbdetach(inp);
f427ee49
A
691 } else {
692 in_pcbdetach(inp);
693 }
2d21ac55 694 }
5ba3f43e
A
695
696 if (mp_so) {
697 mptcp_subflow_del(tptomptp(tp)->mpt_mpte, tp->t_mpsub);
698
699 /* so is now unlinked from mp_so - let's drop the lock */
700 socket_unlock(mp_so, 1);
701 mp_so = NULL;
702 }
703
b0d623f7 704 in_pcbdispose(inp);
5ba3f43e
A
705 active = FALSE;
706 goto out;
b0d623f7 707 }
39236c6e
A
708
709 lck_mtx_unlock(&inp->inpcb_mtx);
5ba3f43e
A
710 active = TRUE;
711
712out:
0a7de745 713 if (mp_so) {
5ba3f43e 714 socket_unlock(mp_so, 1);
0a7de745 715 }
5ba3f43e 716
0a7de745 717 return active;
2d21ac55
A
718}
719
39236c6e
A
720/*
721 * TCP garbage collector callback (inpcb_timer_func_t).
722 *
723 * Returns the number of pcbs that will need to be gc-ed soon,
724 * returnining > 0 will keep timer active.
725 */
1c79356b 726void
39236c6e 727tcp_gc(struct inpcbinfo *ipi)
1c79356b 728{
4a3eedf9 729 struct inpcb *inp, *nxt;
39236c6e 730 struct tcpcb *tw_tp, *tw_ntp;
1c79356b
A
731#if TCPDEBUG
732 int ostate;
733#endif
b0d623f7 734#if KDEBUG
2d21ac55 735 static int tws_checked = 0;
b0d623f7 736#endif
2d21ac55 737
39236c6e 738 KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_START, 0, 0, 0, 0, 0);
1c79356b 739
39236c6e
A
740 /*
741 * Update tcp_now here as it may get used while
742 * processing the slow timer.
743 */
6d2010ae 744 calculate_tcp_clock();
8ad349bb 745
39236c6e
A
746 /*
747 * Garbage collect socket/tcpcb: We need to acquire the list lock
6d2010ae 748 * exclusively to do this
2d21ac55
A
749 */
750
39236c6e
A
751 if (lck_rw_try_lock_exclusive(ipi->ipi_lock) == FALSE) {
752 /* don't sweat it this time; cleanup was done last time */
753 if (tcp_gc_done == TRUE) {
2d21ac55 754 tcp_gc_done = FALSE;
39236c6e
A
755 KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_END,
756 tws_checked, cur_tw_slot, 0, 0, 0);
757 /* Lock upgrade failed, give up this round */
758 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
759 return;
2d21ac55 760 }
39236c6e
A
761 /* Upgrade failed, lost lock now take it again exclusive */
762 lck_rw_lock_exclusive(ipi->ipi_lock);
2d21ac55
A
763 }
764 tcp_gc_done = TRUE;
1c79356b 765
39236c6e 766 LIST_FOREACH_SAFE(inp, &tcb, inp_list, nxt) {
0a7de745 767 if (tcp_garbage_collect(inp, 0)) {
39236c6e 768 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
0a7de745 769 }
39236c6e 770 }
2d21ac55 771
39236c6e
A
772 /* Now cleanup the time wait ones */
773 TAILQ_FOREACH_SAFE(tw_tp, &tcp_tw_tailq, t_twentry, tw_ntp) {
774 /*
39037602 775 * We check the timestamp here without holding the
39236c6e
A
776 * socket lock for better performance. If there are
777 * any pcbs in time-wait, the timer will get rescheduled.
778 * Hence some error in this check can be tolerated.
15129b1c
A
779 *
780 * Sometimes a socket on time-wait queue can be closed if
781 * 2MSL timer expired but the application still has a
39037602 782 * usecount on it.
39236c6e 783 */
39037602 784 if (tw_tp->t_state == TCPS_CLOSED ||
15129b1c 785 TSTMP_GEQ(tcp_now, tw_tp->t_timer[TCPT_2MSL])) {
0a7de745 786 if (tcp_garbage_collect(tw_tp->t_inpcb, 1)) {
39236c6e 787 atomic_add_32(&ipi->ipi_gc_req.intimer_lazy, 1);
0a7de745 788 }
2d21ac55 789 }
91447636
A
790 }
791
39236c6e
A
792 /* take into account pcbs that are still in time_wait_slots */
793 atomic_add_32(&ipi->ipi_gc_req.intimer_lazy, ipi->ipi_twcount);
91447636 794
39236c6e 795 lck_rw_done(ipi->ipi_lock);
1c79356b 796
39236c6e 797 /* Clean up the socache while we are here */
0a7de745 798 if (so_cache_timer()) {
39236c6e 799 atomic_add_32(&ipi->ipi_gc_req.intimer_lazy, 1);
0a7de745 800 }
91447636 801
39236c6e
A
802 KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_END, tws_checked,
803 cur_tw_slot, 0, 0, 0);
804
805 return;
1c79356b
A
806}
807
808/*
809 * Cancel all timers for TCP tp.
810 */
811void
39037602 812tcp_canceltimers(struct tcpcb *tp)
1c79356b 813{
39037602 814 int i;
1c79356b 815
6d2010ae 816 tcp_remove_timer(tp);
0a7de745 817 for (i = 0; i < TCPT_NTIMERS; i++) {
1c79356b 818 tp->t_timer[i] = 0;
0a7de745 819 }
6d2010ae
A
820 tp->tentry.timer_start = tcp_now;
821 tp->tentry.index = TCPT_NONE;
1c79356b
A
822}
823
0a7de745
A
824int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] =
825{ 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
9bccf70c 826
0a7de745
A
827int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
828{ 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
1c79356b 829
0a7de745 830static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */
1c79356b 831
39037602
A
832void
833tcp_rexmt_save_state(struct tcpcb *tp)
39236c6e
A
834{
835 u_int32_t fsize;
836 if (TSTMP_SUPPORTED(tp)) {
837 /*
39037602 838 * Since timestamps are supported on the connection,
39236c6e
A
839 * we can do recovery as described in rfc 4015.
840 */
841 fsize = tp->snd_max - tp->snd_una;
842 tp->snd_ssthresh_prev = max(fsize, tp->snd_ssthresh);
843 tp->snd_recover_prev = tp->snd_recover;
844 } else {
845 /*
846 * Timestamp option is not supported on this connection.
847 * Record ssthresh and cwnd so they can
848 * be recovered if this turns out to be a "bad" retransmit.
39037602 849 * A retransmit is considered "bad" if an ACK for this
39236c6e 850 * segment is received within RTT/2 interval; the assumption
39037602 851 * here is that the ACK was already in flight. See
39236c6e
A
852 * "On Estimating End-to-End Network Path Properties" by
853 * Allman and Paxson for more details.
854 */
855 tp->snd_cwnd_prev = tp->snd_cwnd;
856 tp->snd_ssthresh_prev = tp->snd_ssthresh;
857 tp->snd_recover_prev = tp->snd_recover;
0a7de745 858 if (IN_FASTRECOVERY(tp)) {
39236c6e 859 tp->t_flags |= TF_WASFRECOVERY;
0a7de745 860 } else {
39236c6e 861 tp->t_flags &= ~TF_WASFRECOVERY;
0a7de745 862 }
39236c6e
A
863 }
864 tp->t_srtt_prev = (tp->t_srtt >> TCP_RTT_SHIFT) + 2;
865 tp->t_rttvar_prev = (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
866 tp->t_flagsext &= ~(TF_RECOMPUTE_RTT);
867}
868
fe8ab488
A
869/*
870 * Revert to the older segment size if there is an indication that PMTU
871 * blackhole detection was not needed.
872 */
39037602
A
873void
874tcp_pmtud_revert_segment_size(struct tcpcb *tp)
fe8ab488
A
875{
876 int32_t optlen;
877
878 VERIFY(tp->t_pmtud_saved_maxopd > 0);
39037602
A
879 tp->t_flags |= TF_PMTUD;
880 tp->t_flags &= ~TF_BLACKHOLE;
fe8ab488
A
881 optlen = tp->t_maxopd - tp->t_maxseg;
882 tp->t_maxopd = tp->t_pmtud_saved_maxopd;
883 tp->t_maxseg = tp->t_maxopd - optlen;
a39ff7e2 884
fe8ab488 885 /*
39037602 886 * Reset the slow-start flight size as it
fe8ab488
A
887 * may depend on the new MSS
888 */
0a7de745 889 if (CC_ALGO(tp)->cwnd_init != NULL) {
fe8ab488 890 CC_ALGO(tp)->cwnd_init(tp);
0a7de745 891 }
fe8ab488
A
892 tp->t_pmtud_start_ts = 0;
893 tcpstat.tcps_pmtudbh_reverted++;
5ba3f43e
A
894
895 /* change MSS according to recommendation, if there was one */
896 tcp_update_mss_locked(tp->t_inpcb->inp_socket, NULL);
fe8ab488
A
897}
898
f427ee49
A
899static uint32_t
900tcp_pmtud_black_holed_next_mss(struct tcpcb *tp)
901{
902 /* Reduce the MSS to intermediary value */
903 if (tp->t_maxopd > tcp_pmtud_black_hole_mss) {
904 return tcp_pmtud_black_hole_mss;
905 } else {
906 if (tp->t_inpcb->inp_vflag & INP_IPV4) {
907 return tcp_mssdflt;
908 } else {
909 return tcp_v6mssdflt;
910 }
911 }
912}
913
1c79356b
A
914/*
915 * TCP timer processing.
916 */
917struct tcpcb *
39037602 918tcp_timers(struct tcpcb *tp, int timer)
1c79356b 919{
fe8ab488 920 int32_t rexmt, optlen = 0, idle_time = 0;
316670eb 921 struct socket *so;
9bccf70c 922 struct tcptemp *t_template;
55e303ae
A
923#if TCPDEBUG
924 int ostate;
925#endif
3e170ce0 926 u_int64_t accsleep_ms;
f427ee49 927 u_int64_t last_sleep_ms = 0;
1c79356b 928
316670eb 929 so = tp->t_inpcb->inp_socket;
6d2010ae 930 idle_time = tcp_now - tp->t_rcvtime;
9bccf70c 931
1c79356b 932 switch (timer) {
1c79356b
A
933 /*
934 * 2 MSL timeout in shutdown went off. If we're closed but
935 * still waiting for peer to close and connection has been idle
2d21ac55
A
936 * too long, or if 2MSL time is up from TIME_WAIT or FIN_WAIT_2,
937 * delete connection control block.
938 * Otherwise, (this case shouldn't happen) check again in a bit
939 * we keep the socket in the main list in that case.
1c79356b
A
940 */
941 case TCPT_2MSL:
8ad349bb 942 tcp_free_sackholes(tp);
1c79356b 943 if (tp->t_state != TCPS_TIME_WAIT &&
2d21ac55 944 tp->t_state != TCPS_FIN_WAIT_2 &&
39236c6e 945 ((idle_time > 0) && (idle_time < TCP_CONN_MAXIDLE(tp)))) {
39037602 946 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
0a7de745 947 (u_int32_t)TCP_CONN_KEEPINTVL(tp));
39236c6e 948 } else {
1c79356b 949 tp = tcp_close(tp);
0a7de745 950 return tp;
91447636 951 }
1c79356b
A
952 break;
953
954 /*
955 * Retransmission timer went off. Message has not
956 * been acked within retransmit interval. Back off
957 * to a longer retransmit interval and retransmit one segment.
958 */
959 case TCPT_REXMT:
39037602
A
960 absolutetime_to_nanoseconds(mach_absolutetime_asleep,
961 &accsleep_ms);
962 accsleep_ms = accsleep_ms / 1000000UL;
0a7de745 963 if (accsleep_ms > tp->t_accsleep_ms) {
3e170ce0 964 last_sleep_ms = accsleep_ms - tp->t_accsleep_ms;
0a7de745 965 }
fe8ab488
A
966 /*
967 * Drop a connection in the retransmit timer
968 * 1. If we have retransmitted more than TCP_MAXRXTSHIFT
969 * times
970 * 2. If the time spent in this retransmission episode is
971 * more than the time limit set with TCP_RXT_CONNDROPTIME
972 * socket option
973 * 3. If TCP_RXT_FINDROP socket option was set and
974 * we have already retransmitted the FIN 3 times without
975 * receiving an ack
6d2010ae
A
976 */
977 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT ||
3e170ce0
A
978 (tp->t_rxt_conndroptime > 0 && tp->t_rxtstart > 0 &&
979 (tcp_now - tp->t_rxtstart) >= tp->t_rxt_conndroptime) ||
980 ((tp->t_flagsext & TF_RXTFINDROP) != 0 &&
981 (tp->t_flags & TF_SENTFIN) != 0 && tp->t_rxtshift >= 4) ||
982 (tp->t_rxtshift > 4 && last_sleep_ms >= TCP_SLEEP_TOO_LONG)) {
5ba3f43e
A
983 if (tp->t_state == TCPS_ESTABLISHED &&
984 tp->t_rxt_minimum_timeout > 0) {
985 /*
986 * Avoid dropping a connection if minimum
987 * timeout is set and that time did not
988 * pass. We will retry sending
989 * retransmissions at the maximum interval
990 */
991 if (TSTMP_LT(tcp_now, (tp->t_rxtstart +
992 tp->t_rxt_minimum_timeout))) {
993 tp->t_rxtshift = TCP_MAXRXTSHIFT - 1;
994 goto retransmit_packet;
995 }
996 }
6d2010ae
A
997 if ((tp->t_flagsext & TF_RXTFINDROP) != 0) {
998 tcpstat.tcps_rxtfindrop++;
3e170ce0
A
999 } else if (last_sleep_ms >= TCP_SLEEP_TOO_LONG) {
1000 tcpstat.tcps_drop_after_sleep++;
6d2010ae
A
1001 } else {
1002 tcpstat.tcps_timeoutdrop++;
1003 }
4bd07ac2
A
1004 if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) {
1005 if (TCP_ECN_ENABLED(tp)) {
1006 INP_INC_IFNET_STAT(tp->t_inpcb,
1007 ecn_on.rxmit_drop);
1008 } else {
1009 INP_INC_IFNET_STAT(tp->t_inpcb,
1010 ecn_off.rxmit_drop);
1011 }
1012 }
1c79356b 1013 tp->t_rxtshift = TCP_MAXRXTSHIFT;
39037602 1014 soevent(so,
0a7de745 1015 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_TIMEOUT));
39037602
A
1016
1017 if (TCP_ECN_ENABLED(tp) &&
0a7de745 1018 tp->t_state == TCPS_ESTABLISHED) {
39037602 1019 tcp_heuristic_ecn_droprxmt(tp);
0a7de745 1020 }
39037602 1021
1c79356b
A
1022 tp = tcp_drop(tp, tp->t_softerror ?
1023 tp->t_softerror : ETIMEDOUT);
316670eb 1024
1c79356b
A
1025 break;
1026 }
5ba3f43e 1027retransmit_packet:
39236c6e 1028 tcpstat.tcps_rexmttimeo++;
3e170ce0 1029 tp->t_accsleep_ms = accsleep_ms;
6d2010ae 1030
39037602 1031 if (tp->t_rxtshift == 1 &&
0a7de745 1032 tp->t_state == TCPS_ESTABLISHED) {
39236c6e
A
1033 /* Set the time at which retransmission started. */
1034 tp->t_rxtstart = tcp_now;
1035
39037602 1036 /*
39236c6e
A
1037 * if this is the first retransmit timeout, save
1038 * the state so that we can recover if the timeout
1039 * is spurious.
39037602 1040 */
39236c6e 1041 tcp_rexmt_save_state(tp);
cb323159 1042 tcp_ccdbg_trace(tp, NULL, TCP_CC_FIRST_REXMT);
39236c6e
A
1043 }
1044#if MPTCP
fe8ab488 1045 if ((tp->t_rxtshift >= mptcp_fail_thresh) &&
39236c6e 1046 (tp->t_state == TCPS_ESTABLISHED) &&
0a7de745 1047 (tp->t_mpflags & TMPF_MPTCP_TRUE)) {
39236c6e 1048 mptcp_act_on_txfail(so);
0a7de745 1049 }
39236c6e 1050
cb323159
A
1051 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1052 (so->so_flags & SOF_MP_SUBFLOW)) {
5ba3f43e
A
1053 struct mptses *mpte = tptomptp(tp)->mpt_mpte;
1054
c3c9b80d
A
1055 if (mpte->mpte_svctype == MPTCP_SVCTYPE_HANDOVER ||
1056 mpte->mpte_svctype == MPTCP_SVCTYPE_PURE_HANDOVER) {
cb323159
A
1057 mptcp_check_subflows_and_add(mpte);
1058 }
5ba3f43e 1059 }
39236c6e
A
1060#endif /* MPTCP */
1061
1062 if (tp->t_adaptive_wtimo > 0 &&
0a7de745
A
1063 tp->t_rxtshift > tp->t_adaptive_wtimo &&
1064 TCPS_HAVEESTABLISHED(tp->t_state)) {
39236c6e
A
1065 /* Send an event to the application */
1066 soevent(so,
0a7de745
A
1067 (SO_FILT_HINT_LOCKED |
1068 SO_FILT_HINT_ADAPTIVE_WTIMO));
9bccf70c 1069 }
316670eb 1070
fe8ab488
A
1071 /*
1072 * If this is a retransmit timeout after PTO, the PTO
1073 * was not effective
1074 */
1075 if (tp->t_flagsext & TF_SENT_TLPROBE) {
1076 tp->t_flagsext &= ~(TF_SENT_TLPROBE);
1077 tcpstat.tcps_rto_after_pto++;
1078 }
1079
1080 if (tp->t_flagsext & TF_DELAY_RECOVERY) {
1081 /*
1082 * Retransmit timer fired before entering recovery
1083 * on a connection with packet re-ordering. This
1084 * suggests that the reordering metrics computed
1085 * are not accurate.
1086 */
1087 tp->t_reorderwin = 0;
1088 tp->t_timer[TCPT_DELAYFR] = 0;
1089 tp->t_flagsext &= ~(TF_DELAY_RECOVERY);
1090 }
1091
cb323159
A
1092 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
1093 tp->t_state == TCPS_SYN_RECEIVED) {
3e170ce0 1094 tcp_disable_tfo(tp);
0a7de745 1095 }
3e170ce0 1096
cb323159
A
1097 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
1098 !(tp->t_tfo_flags & TFO_F_HEURISTIC_DONE) &&
5ba3f43e 1099 (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) &&
3e170ce0
A
1100 !(tp->t_tfo_flags & TFO_F_NO_SNDPROBING) &&
1101 ((tp->t_state != TCPS_SYN_SENT && tp->t_rxtshift > 1) ||
0a7de745 1102 tp->t_rxtshift > 4)) {
3e170ce0
A
1103 /*
1104 * For regular retransmissions, a first one is being
1105 * done for tail-loss probe.
1106 * Thus, if rxtshift > 1, this means we have sent the segment
1107 * a total of 3 times.
1108 *
1109 * If we are in SYN-SENT state, then there is no tail-loss
1110 * probe thus we have to let rxtshift go up to 3.
1111 */
1112 tcp_heuristic_tfo_middlebox(tp);
1113
1114 so->so_error = ENODATA;
cb323159
A
1115 soevent(so,
1116 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_MP_SUB_ERROR));
3e170ce0
A
1117 sorwakeup(so);
1118 sowwakeup(so);
39037602
A
1119
1120 tp->t_tfo_stats |= TFO_S_SEND_BLACKHOLE;
1121 tcpstat.tcps_tfo_sndblackhole++;
3e170ce0
A
1122 }
1123
cb323159
A
1124 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
1125 !(tp->t_tfo_flags & TFO_F_HEURISTIC_DONE) &&
5ba3f43e 1126 (tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED) &&
5c9f4661 1127 tp->t_rxtshift > 3) {
5ba3f43e
A
1128 if (TSTMP_GT(tp->t_sndtime - 10 * TCP_RETRANSHZ, tp->t_rcvtime)) {
1129 tcp_heuristic_tfo_middlebox(tp);
1130
1131 so->so_error = ENODATA;
cb323159
A
1132 soevent(so,
1133 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_MP_SUB_ERROR));
5ba3f43e
A
1134 sorwakeup(so);
1135 sowwakeup(so);
1136 }
1137 }
1138
39236c6e 1139 if (tp->t_state == TCPS_SYN_SENT) {
9bccf70c 1140 rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift];
39236c6e 1141 tp->t_stat.synrxtshift = tp->t_rxtshift;
cb323159 1142 tp->t_stat.rxmitsyns++;
3e170ce0
A
1143
1144 /* When retransmitting, disable TFO */
5ba3f43e 1145 if (tfo_enabled(tp) &&
cb323159
A
1146 !(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE)) {
1147 tcp_disable_tfo(tp);
3e170ce0 1148 tp->t_tfo_flags |= TFO_F_SYN_LOSS;
3e170ce0 1149 }
fe8ab488 1150 } else {
9bccf70c 1151 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
fe8ab488
A
1152 }
1153
490019cf 1154 TCPT_RANGESET(tp->t_rxtcur, rexmt, tp->t_rttmin, TCPTV_REXMTMAX,
0a7de745 1155 TCP_ADD_REXMTSLOP(tp));
6d2010ae 1156 tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp, tp->t_rxtcur);
9bccf70c 1157
cb323159
A
1158 TCP_LOG_RTT_INFO(tp);
1159
0a7de745 1160 if (INP_WAIT_FOR_IF_FEEDBACK(tp->t_inpcb)) {
316670eb 1161 goto fc_output;
0a7de745 1162 }
316670eb
A
1163
1164 tcp_free_sackholes(tp);
9bccf70c 1165 /*
fe8ab488 1166 * Check for potential Path MTU Discovery Black Hole
b0d623f7 1167 */
fe8ab488 1168 if (tcp_pmtud_black_hole_detect &&
0a7de745
A
1169 !(tp->t_flagsext & TF_NOBLACKHOLE_DETECTION) &&
1170 (tp->t_state == TCPS_ESTABLISHED)) {
3e170ce0 1171 if ((tp->t_flags & TF_PMTUD) &&
f427ee49 1172 tp->t_pmtud_lastseg_size > tcp_pmtud_black_holed_next_mss(tp) &&
3e170ce0 1173 tp->t_rxtshift == 2) {
39037602 1174 /*
b0d623f7
A
1175 * Enter Path MTU Black-hole Detection mechanism:
1176 * - Disable Path MTU Discovery (IP "DF" bit).
fe8ab488
A
1177 * - Reduce MTU to lower value than what we
1178 * negotiated with the peer.
b0d623f7 1179 */
39236c6e
A
1180 /* Disable Path MTU Discovery for now */
1181 tp->t_flags &= ~TF_PMTUD;
1182 /* Record that we may have found a black hole */
1183 tp->t_flags |= TF_BLACKHOLE;
b0d623f7 1184 optlen = tp->t_maxopd - tp->t_maxseg;
39236c6e
A
1185 /* Keep track of previous MSS */
1186 tp->t_pmtud_saved_maxopd = tp->t_maxopd;
fe8ab488 1187 tp->t_pmtud_start_ts = tcp_now;
0a7de745 1188 if (tp->t_pmtud_start_ts == 0) {
fe8ab488 1189 tp->t_pmtud_start_ts++;
0a7de745 1190 }
39236c6e 1191 /* Reduce the MSS to intermediary value */
f427ee49 1192 tp->t_maxopd = tcp_pmtud_black_holed_next_mss(tp);
b0d623f7 1193 tp->t_maxseg = tp->t_maxopd - optlen;
6d2010ae
A
1194
1195 /*
d9a64523 1196 * Reset the slow-start flight size
39236c6e 1197 * as it may depend on the new MSS
d9a64523 1198 */
0a7de745 1199 if (CC_ALGO(tp)->cwnd_init != NULL) {
6d2010ae 1200 CC_ALGO(tp)->cwnd_init(tp);
0a7de745 1201 }
39037602 1202 tp->snd_cwnd = tp->t_maxseg;
b0d623f7
A
1203 }
1204 /*
fe8ab488
A
1205 * If further retransmissions are still
1206 * unsuccessful with a lowered MTU, maybe this
1207 * isn't a Black Hole and we restore the previous
1208 * MSS and blackhole detection flags.
b0d623f7
A
1209 */
1210 else {
fe8ab488
A
1211 if ((tp->t_flags & TF_BLACKHOLE) &&
1212 (tp->t_rxtshift > 4)) {
1213 tcp_pmtud_revert_segment_size(tp);
39037602 1214 tp->snd_cwnd = tp->t_maxseg;
b0d623f7
A
1215 }
1216 }
1217 }
1218
1219
1220 /*
fe8ab488
A
1221 * Disable rfc1323 and rfc1644 if we haven't got any
1222 * response to our SYN (after we reach the threshold)
1223 * to work-around some broken terminal servers (most of
1224 * which have hopefully been retired) that have bad VJ
1225 * header compression code which trashes TCP segments
1226 * containing unknown-to-them TCP options.
39236c6e 1227 * Do this only on non-local connections.
9bccf70c 1228 */
39236c6e 1229 if (tp->t_state == TCPS_SYN_SENT &&
0a7de745
A
1230 tp->t_rxtshift == tcp_broken_peer_syn_rxmit_thres) {
1231 tp->t_flags &= ~(TF_REQ_SCALE | TF_REQ_TSTMP | TF_REQ_CC);
1232 }
316670eb 1233
1c79356b
A
1234 /*
1235 * If losing, let the lower level know and try for
1236 * a better route. Also, if we backed off this far,
1237 * our srtt estimate is probably bogus. Clobber it
1238 * so we'll take the next rtt measurement as our srtt;
1239 * move the current srtt into rttvar to keep the current
1240 * retransmit times until then.
1241 */
1242 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
f427ee49 1243 if (!(tp->t_inpcb->inp_vflag & INP_IPV4)) {
1c79356b 1244 in6_losing(tp->t_inpcb);
f427ee49
A
1245 } else {
1246 in_losing(tp->t_inpcb);
1247 }
1c79356b
A
1248 tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
1249 tp->t_srtt = 0;
1250 }
1251 tp->snd_nxt = tp->snd_una;
9bccf70c
A
1252 /*
1253 * Note: We overload snd_recover to function also as the
1254 * snd_last variable described in RFC 2582
1255 */
1256 tp->snd_recover = tp->snd_max;
1c79356b
A
1257 /*
1258 * Force a segment to be sent.
1259 */
1260 tp->t_flags |= TF_ACKNOW;
fe8ab488
A
1261
1262 /* If timing a segment in this window, stop the timer */
9bccf70c 1263 tp->t_rtttime = 0;
6d2010ae 1264
0a7de745 1265 if (!IN_FASTRECOVERY(tp) && tp->t_rxtshift == 1) {
fe8ab488 1266 tcpstat.tcps_tailloss_rto++;
0a7de745 1267 }
fe8ab488 1268
39236c6e 1269
fe8ab488
A
1270 /*
1271 * RFC 5681 says: when a TCP sender detects segment loss
39236c6e
A
1272 * using retransmit timer and the given segment has already
1273 * been retransmitted by way of the retransmission timer at
1274 * least once, the value of ssthresh is held constant
1275 */
39037602 1276 if (tp->t_rxtshift == 1 &&
3e170ce0 1277 CC_ALGO(tp)->after_timeout != NULL) {
6d2010ae 1278 CC_ALGO(tp)->after_timeout(tp);
3e170ce0
A
1279 /*
1280 * CWR notifications are to be sent on new data
1281 * right after Fast Retransmits and ECE
1282 * notification receipts.
1283 */
0a7de745 1284 if (TCP_ECN_ENABLED(tp)) {
3e170ce0 1285 tp->ecn_flags |= TE_SENDCWR;
0a7de745 1286 }
3e170ce0 1287 }
6d2010ae 1288
fe8ab488 1289 EXIT_FASTRECOVERY(tp);
6d2010ae 1290
3e170ce0
A
1291 /* Exit cwnd non validated phase */
1292 tp->t_flagsext &= ~TF_CWND_NONVALIDATED;
1293
1294
316670eb 1295fc_output:
fe8ab488 1296 tcp_ccdbg_trace(tp, NULL, TCP_CC_REXMT_TIMEOUT);
6d2010ae 1297
1c79356b
A
1298 (void) tcp_output(tp);
1299 break;
1300
1301 /*
1302 * Persistance timer into zero window.
1303 * Force a byte to be output, if possible.
1304 */
1305 case TCPT_PERSIST:
1306 tcpstat.tcps_persisttimeo++;
1307 /*
1308 * Hack: if the peer is dead/unreachable, we do not
1309 * time out if the window is closed. After a full
1310 * backoff, drop the connection if the idle time
1311 * (no responses to probes) reaches the maximum
1312 * backoff that we would use if retransmitting.
39037602
A
1313 *
1314 * Drop the connection if we reached the maximum allowed time for
1315 * Zero Window Probes without a non-zero update from the peer.
6d2010ae 1316 * See rdar://5805356
1c79356b 1317 */
6d2010ae
A
1318 if ((tp->t_rxtshift == TCP_MAXRXTSHIFT &&
1319 (idle_time >= tcp_maxpersistidle ||
39037602
A
1320 idle_time >= TCP_REXMTVAL(tp) * tcp_totbackoff)) ||
1321 ((tp->t_persist_stop != 0) &&
0a7de745 1322 TSTMP_LEQ(tp->t_persist_stop, tcp_now))) {
1c79356b 1323 tcpstat.tcps_persistdrop++;
316670eb 1324 soevent(so,
0a7de745 1325 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_TIMEOUT));
1c79356b 1326 tp = tcp_drop(tp, ETIMEDOUT);
1c79356b
A
1327 break;
1328 }
1329 tcp_setpersist(tp);
fe8ab488 1330 tp->t_flagsext |= TF_FORCE;
1c79356b 1331 (void) tcp_output(tp);
fe8ab488 1332 tp->t_flagsext &= ~TF_FORCE;
1c79356b
A
1333 break;
1334
1335 /*
1336 * Keep-alive timer went off; send something
1337 * or drop connection if idle for too long.
1338 */
1339 case TCPT_KEEP:
f427ee49
A
1340#if FLOW_DIVERT
1341 if (tp->t_inpcb->inp_socket->so_flags & SOF_FLOW_DIVERT) {
1342 break;
1343 }
1344#endif /* FLOW_DIVERT */
1345
1c79356b 1346 tcpstat.tcps_keeptimeo++;
39236c6e
A
1347#if MPTCP
1348 /*
1349 * Regular TCP connections do not send keepalives after closing
1350 * MPTCP must not also, after sending Data FINs.
1351 */
5ba3f43e 1352 struct mptcb *mp_tp = tptomptp(tp);
fe8ab488
A
1353 if ((tp->t_mpflags & TMPF_MPTCP_TRUE) &&
1354 (tp->t_state > TCPS_ESTABLISHED)) {
39236c6e
A
1355 goto dropit;
1356 } else if (mp_tp != NULL) {
0a7de745 1357 if ((mptcp_ok_to_keepalive(mp_tp) == 0)) {
39236c6e 1358 goto dropit;
0a7de745 1359 }
39236c6e
A
1360 }
1361#endif /* MPTCP */
0a7de745 1362 if (tp->t_state < TCPS_ESTABLISHED) {
1c79356b 1363 goto dropit;
0a7de745 1364 }
1c79356b 1365 if ((always_keepalive ||
39236c6e 1366 (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) ||
3e170ce0
A
1367 (tp->t_flagsext & TF_DETECT_READSTALL) ||
1368 (tp->t_tfo_probe_state == TFO_PROBE_PROBING)) &&
2d21ac55 1369 (tp->t_state <= TCPS_CLOSING || tp->t_state == TCPS_FIN_WAIT_2)) {
0a7de745 1370 if (idle_time >= TCP_CONN_KEEPIDLE(tp) + TCP_CONN_MAXIDLE(tp)) {
1c79356b 1371 goto dropit;
0a7de745 1372 }
1c79356b
A
1373 /*
1374 * Send a packet designed to force a response
1375 * if the peer is up and reachable:
1376 * either an ACK if the connection is still alive,
1377 * or an RST if the peer has closed the connection
1378 * due to timeout or reboot.
1379 * Using sequence number tp->snd_una-1
1380 * causes the transmitted zero-length segment
1381 * to lie outside the receive window;
1382 * by the protocol spec, this requires the
1383 * correspondent TCP to respond.
1384 */
1385 tcpstat.tcps_keepprobe++;
9bccf70c
A
1386 t_template = tcp_maketemplate(tp);
1387 if (t_template) {
fe8ab488
A
1388 struct inpcb *inp = tp->t_inpcb;
1389 struct tcp_respond_args tra;
c910b4d9 1390
fe8ab488
A
1391 bzero(&tra, sizeof(tra));
1392 tra.nocell = INP_NO_CELLULAR(inp);
1393 tra.noexpensive = INP_NO_EXPENSIVE(inp);
cb323159 1394 tra.noconstrained = INP_NO_CONSTRAINED(inp);
fe8ab488 1395 tra.awdl_unrestricted = INP_AWDL_UNRESTRICTED(inp);
39037602 1396 tra.intcoproc_allowed = INP_INTCOPROC_ALLOWED(inp);
cb323159 1397 tra.keep_alive = 1;
0a7de745 1398 if (tp->t_inpcb->inp_flags & INP_BOUND_IF) {
fe8ab488 1399 tra.ifscope = tp->t_inpcb->inp_boundifp->if_index;
0a7de745 1400 } else {
fe8ab488 1401 tra.ifscope = IFSCOPE_NONE;
0a7de745 1402 }
9bccf70c
A
1403 tcp_respond(tp, t_template->tt_ipgen,
1404 &t_template->tt_t, (struct mbuf *)NULL,
fe8ab488 1405 tp->rcv_nxt, tp->snd_una - 1, 0, &tra);
9bccf70c 1406 (void) m_free(dtom(t_template));
0a7de745 1407 if (tp->t_flagsext & TF_DETECT_READSTALL) {
39236c6e 1408 tp->t_rtimo_probes++;
0a7de745 1409 }
39236c6e 1410 }
cb323159
A
1411
1412 TCP_LOG_KEEP_ALIVE(tp, idle_time);
1413
39236c6e 1414 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
3e170ce0 1415 TCP_CONN_KEEPINTVL(tp));
39236c6e
A
1416 } else {
1417 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
3e170ce0 1418 TCP_CONN_KEEPIDLE(tp));
39236c6e
A
1419 }
1420 if (tp->t_flagsext & TF_DETECT_READSTALL) {
3e170ce0
A
1421 struct ifnet *outifp = tp->t_inpcb->inp_last_outifp;
1422 bool reenable_probe = false;
39037602 1423 /*
39236c6e 1424 * The keep alive packets sent to detect a read
39037602 1425 * stall did not get a response from the
39236c6e
A
1426 * peer. Generate more keep-alives to confirm this.
1427 * If the number of probes sent reaches the limit,
1428 * generate an event.
1429 */
3e170ce0
A
1430 if (tp->t_adaptive_rtimo > 0) {
1431 if (tp->t_rtimo_probes > tp->t_adaptive_rtimo) {
1432 /* Generate an event */
1433 soevent(so,
1434 (SO_FILT_HINT_LOCKED |
1435 SO_FILT_HINT_ADAPTIVE_RTIMO));
1436 tcp_keepalive_reset(tp);
1437 } else {
1438 reenable_probe = true;
1439 }
1440 } else if (outifp != NULL &&
1441 (outifp->if_eflags & IFEF_PROBE_CONNECTIVITY) &&
1442 tp->t_rtimo_probes <= TCP_CONNECTIVITY_PROBES_MAX) {
1443 reenable_probe = true;
39236c6e 1444 } else {
3e170ce0
A
1445 tp->t_flagsext &= ~TF_DETECT_READSTALL;
1446 }
1447 if (reenable_probe) {
1448 int ind = min(tp->t_rtimo_probes,
1449 TCP_MAXRXTSHIFT);
39236c6e 1450 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(
0a7de745 1451 tp, tcp_backoff[ind] * TCP_REXMTVAL(tp));
9bccf70c 1452 }
39236c6e 1453 }
3e170ce0
A
1454 if (tp->t_tfo_probe_state == TFO_PROBE_PROBING) {
1455 int ind;
1456
1457 tp->t_tfo_probes++;
1458 ind = min(tp->t_tfo_probes, TCP_MAXRXTSHIFT);
1459
1460 /*
1461 * We take the minimum among the time set by true
1462 * keepalive (see above) and the backoff'd RTO. That
1463 * way we backoff in case of packet-loss but will never
1464 * timeout slower than regular keepalive due to the
1465 * backing off.
1466 */
1467 tp->t_timer[TCPT_KEEP] = min(OFFSET_FROM_START(
0a7de745 1468 tp, tcp_backoff[ind] * TCP_REXMTVAL(tp)),
3e170ce0 1469 tp->t_timer[TCPT_KEEP]);
cb323159
A
1470 } else if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
1471 !(tp->t_tfo_flags & TFO_F_HEURISTIC_DONE) &&
0a7de745 1472 tp->t_tfo_probe_state == TFO_PROBE_WAIT_DATA) {
3e170ce0
A
1473 /* Still no data! Let's assume a TFO-error and err out... */
1474 tcp_heuristic_tfo_middlebox(tp);
1475
1476 so->so_error = ENODATA;
cb323159
A
1477 soevent(so,
1478 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_MP_SUB_ERROR));
3e170ce0 1479 sorwakeup(so);
39037602 1480 tp->t_tfo_stats |= TFO_S_RECV_BLACKHOLE;
3e170ce0
A
1481 tcpstat.tcps_tfo_blackhole++;
1482 }
6d2010ae
A
1483 break;
1484 case TCPT_DELACK:
1485 if (tcp_delack_enabled && (tp->t_flags & TF_DELACK)) {
1486 tp->t_flags &= ~TF_DELACK;
1487 tp->t_timer[TCPT_DELACK] = 0;
1488 tp->t_flags |= TF_ACKNOW;
1489
fe8ab488
A
1490 /*
1491 * If delayed ack timer fired while stretching
1492 * acks, count the number of times the streaming
39037602 1493 * detection was not correct. If this exceeds a
fe8ab488
A
1494 * threshold, disable strech ack on this
1495 * connection
1496 *
1497 * Also, go back to acking every other packet.
6d2010ae 1498 */
fe8ab488
A
1499 if ((tp->t_flags & TF_STRETCHACK)) {
1500 if (tp->t_unacksegs > 1 &&
0a7de745 1501 tp->t_unacksegs < maxseg_unacked) {
fe8ab488 1502 tp->t_stretchack_delayed++;
0a7de745 1503 }
fe8ab488
A
1504
1505 if (tp->t_stretchack_delayed >
0a7de745 1506 TCP_STRETCHACK_DELAY_THRESHOLD) {
fe8ab488
A
1507 tp->t_flagsext |= TF_DISABLE_STRETCHACK;
1508 /*
1509 * Note the time at which stretch
1510 * ack was disabled automatically
1511 */
1512 tp->rcv_nostrack_ts = tcp_now;
1513 tcpstat.tcps_nostretchack++;
1514 tp->t_stretchack_delayed = 0;
39037602 1515 tp->rcv_nostrack_pkts = 0;
fe8ab488 1516 }
6d2010ae 1517 tcp_reset_stretch_ack(tp);
fe8ab488 1518 }
f427ee49 1519 tp->t_forced_acks = TCP_FORCED_ACKS_COUNT;
6d2010ae 1520
fe8ab488
A
1521 /*
1522 * If we are measuring inter packet arrival jitter
1523 * for throttling a connection, this delayed ack
1524 * might be the reason for accumulating some
1525 * jitter. So let's restart the measurement.
316670eb
A
1526 */
1527 CLEAR_IAJ_STATE(tp);
1528
6d2010ae 1529 tcpstat.tcps_delack++;
f427ee49 1530 tp->t_stat.delayed_acks_sent++;
6d2010ae
A
1531 (void) tcp_output(tp);
1532 }
1c79356b 1533 break;
9bccf70c 1534
39236c6e
A
1535#if MPTCP
1536 case TCPT_JACK_RXMT:
1537 if ((tp->t_state == TCPS_ESTABLISHED) &&
1538 (tp->t_mpflags & TMPF_PREESTABLISHED) &&
1539 (tp->t_mpflags & TMPF_JOINED_FLOW)) {
1540 if (++tp->t_mprxtshift > TCP_MAXRXTSHIFT) {
1541 tcpstat.tcps_timeoutdrop++;
39037602 1542 soevent(so,
0a7de745 1543 (SO_FILT_HINT_LOCKED |
39236c6e
A
1544 SO_FILT_HINT_TIMEOUT));
1545 tp = tcp_drop(tp, tp->t_softerror ?
5ba3f43e 1546 tp->t_softerror : ETIMEDOUT);
39236c6e
A
1547 break;
1548 }
1549 tcpstat.tcps_join_rxmts++;
5ba3f43e 1550 tp->t_mpflags |= TMPF_SND_JACK;
39236c6e
A
1551 tp->t_flags |= TF_ACKNOW;
1552
1553 /*
39037602 1554 * No backoff is implemented for simplicity for this
39236c6e
A
1555 * corner case.
1556 */
1557 (void) tcp_output(tp);
1558 }
1559 break;
94ff46dc
A
1560 case TCPT_CELLICON:
1561 {
1562 struct mptses *mpte = tptomptp(tp)->mpt_mpte;
1563
1564 tp->t_timer[TCPT_CELLICON] = 0;
1565
1566 if (mpte->mpte_cellicon_increments == 0) {
1567 /* Cell-icon not set by this connection */
1568 break;
1569 }
1570
1571 if (TSTMP_LT(mpte->mpte_last_cellicon_set + MPTCP_CELLICON_TOGGLE_RATE, tcp_now)) {
1572 mptcp_unset_cellicon(mpte, NULL, 1);
1573 }
1574
1575 if (mpte->mpte_cellicon_increments) {
1576 tp->t_timer[TCPT_CELLICON] = OFFSET_FROM_START(tp, MPTCP_CELLICON_TOGGLE_RATE);
1577 }
1578
1579 break;
1580 }
39236c6e
A
1581#endif /* MPTCP */
1582
fe8ab488
A
1583 case TCPT_PTO:
1584 {
cb323159 1585 int32_t ret = 0;
fe8ab488 1586
cb323159
A
1587 if (!(tp->t_flagsext & TF_IF_PROBING)) {
1588 tp->t_flagsext &= ~(TF_SENT_TLPROBE);
1589 }
fe8ab488
A
1590 /*
1591 * Check if the connection is in the right state to
1592 * send a probe
1593 */
cb323159
A
1594 if ((tp->t_state != TCPS_ESTABLISHED ||
1595 tp->t_rxtshift > 0 ||
d9a64523
A
1596 tp->snd_max == tp->snd_una ||
1597 !SACK_ENABLED(tp) ||
f427ee49 1598 (tcp_do_better_lr != 1 && !TAILQ_EMPTY(&tp->snd_holes)) ||
cb323159
A
1599 IN_FASTRECOVERY(tp)) &&
1600 !(tp->t_flagsext & TF_IF_PROBING)) {
fe8ab488 1601 break;
0a7de745 1602 }
fe8ab488 1603
3e170ce0 1604 /*
cb323159
A
1605 * When the interface state is changed explicitly reset the retransmission
1606 * timer state for both SYN and data packets because we do not want to
1607 * wait unnecessarily or timeout too quickly if the link characteristics
1608 * have changed drastically
3e170ce0 1609 */
cb323159
A
1610 if (tp->t_flagsext & TF_IF_PROBING) {
1611 tp->t_rxtshift = 0;
1612 if (tp->t_state == TCPS_SYN_SENT) {
1613 tp->t_stat.synrxtshift = tp->t_rxtshift;
1614 }
1615 /*
1616 * Reset to the the default RTO
1617 */
1618 tp->t_srtt = TCPTV_SRTTBASE;
1619 tp->t_rttvar =
1620 ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
1621 tp->t_rttmin = tp->t_flags & TF_LOCAL ? tcp_TCPTV_MIN :
1622 TCPTV_REXMTMIN;
1623 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1624 tp->t_rttmin, TCPTV_REXMTMAX, TCP_ADD_REXMTSLOP(tp));
1625 TCP_LOG_RTT_INFO(tp);
1626 }
1627
1628 if (tp->t_state == TCPS_SYN_SENT) {
1629 /*
1630 * The PTO for SYN_SENT reinitializes TCP as if it was a fresh
1631 * connection attempt
1632 */
1633 tp->snd_nxt = tp->snd_una;
1634 /*
1635 * Note: We overload snd_recover to function also as the
1636 * snd_last variable described in RFC 2582
1637 */
1638 tp->snd_recover = tp->snd_max;
1639 /*
1640 * Force a segment to be sent.
1641 */
1642 tp->t_flags |= TF_ACKNOW;
1643
1644 /* If timing a segment in this window, stop the timer */
1645 tp->t_rtttime = 0;
3e170ce0 1646 } else {
cb323159
A
1647 int32_t snd_len;
1648
1649 /*
1650 * If there is no new data to send or if the
1651 * connection is limited by receive window then
1652 * retransmit the last segment, otherwise send
1653 * new data.
1654 */
1655 snd_len = min(so->so_snd.sb_cc, tp->snd_wnd)
1656 - (tp->snd_max - tp->snd_una);
1657 if (snd_len > 0) {
1658 tp->snd_nxt = tp->snd_max;
1659 } else {
1660 snd_len = min((tp->snd_max - tp->snd_una),
1661 tp->t_maxseg);
1662 tp->snd_nxt = tp->snd_max - snd_len;
1663 }
3e170ce0
A
1664 }
1665
fe8ab488 1666 tcpstat.tcps_pto++;
cb323159 1667 if (tp->t_flagsext & TF_IF_PROBING) {
3e170ce0 1668 tcpstat.tcps_probe_if++;
0a7de745 1669 }
fe8ab488
A
1670
1671 /* If timing a segment in this window, stop the timer */
1672 tp->t_rtttime = 0;
cb323159
A
1673 /* Note that tail loss probe is being sent. Exclude IF probe */
1674 if (!(tp->t_flagsext & TF_IF_PROBING)) {
1675 tp->t_flagsext |= TF_SENT_TLPROBE;
1676 tp->t_tlpstart = tcp_now;
1677 }
fe8ab488
A
1678
1679 tp->snd_cwnd += tp->t_maxseg;
d9a64523
A
1680 /*
1681 * When tail-loss-probe fires, we reset the RTO timer, because
1682 * a probe just got sent, so we are good to push out the timer.
1683 *
1684 * Set to 0 to ensure that tcp_output() will reschedule it
1685 */
1686 tp->t_timer[TCPT_REXMT] = 0;
cb323159
A
1687 ret = tcp_output(tp);
1688
1689#if (DEBUG || DEVELOPMENT)
1690 if ((tp->t_flagsext & TF_IF_PROBING) &&
1691 ((IFNET_IS_COMPANION_LINK(tp->t_inpcb->inp_last_outifp)) ||
1692 tp->t_state == TCPS_SYN_SENT)) {
1693 if (ret == 0 && tcp_probe_if_fix_port > 0 &&
1694 tcp_probe_if_fix_port <= IPPORT_HILASTAUTO) {
1695 tp->t_timer[TCPT_REXMT] = 0;
1696 tcp_set_lotimer_index(tp);
1697 }
d9a64523 1698
cb323159
A
1699 os_log(OS_LOG_DEFAULT,
1700 "%s: sent %s probe for %u > %u on interface %s"
1701 " (%u) %s(%d)",
1702 __func__,
1703 tp->t_state == TCPS_SYN_SENT ? "SYN" : "data",
1704 ntohs(tp->t_inpcb->inp_lport),
1705 ntohs(tp->t_inpcb->inp_fport),
1706 if_name(tp->t_inpcb->inp_last_outifp),
1707 tp->t_inpcb->inp_last_outifp->if_index,
1708 ret == 0 ? "succeeded" :"failed", ret);
1709 }
1710#endif /* DEBUG || DEVELOPMENT */
1711
1712 /*
1713 * When the connection is not idle, make sure the retransmission timer
1714 * is armed because it was set to zero above
1715 */
1716 if ((tp->t_timer[TCPT_REXMT] == 0 || tp->t_timer[TCPT_PERSIST] == 0) &&
1717 (tp->t_inpcb->inp_socket->so_snd.sb_cc != 0 || tp->t_state == TCPS_SYN_SENT ||
1718 tp->t_state == TCPS_SYN_RECEIVED)) {
1719 tp->t_timer[TCPT_REXMT] =
1720 OFFSET_FROM_START(tp, tp->t_rxtcur);
1721
1722 os_log(OS_LOG_DEFAULT,
1723 "%s: tcp_output() returned %u with retransmission timer disabled "
1724 "for %u > %u in state %d, reset timer to %d",
1725 __func__, ret,
1726 ntohs(tp->t_inpcb->inp_lport),
1727 ntohs(tp->t_inpcb->inp_fport),
1728 tp->t_state,
1729 tp->t_timer[TCPT_REXMT]);
1730
1731 tcp_check_timer_state(tp);
1732 }
fe8ab488
A
1733 tp->snd_cwnd -= tp->t_maxseg;
1734
cb323159
A
1735 if (!(tp->t_flagsext & TF_IF_PROBING)) {
1736 tp->t_tlphighrxt = tp->snd_nxt;
1737 }
fe8ab488
A
1738 break;
1739 }
1740 case TCPT_DELAYFR:
1741 tp->t_flagsext &= ~TF_DELAY_RECOVERY;
1742
1743 /*
1744 * Don't do anything if one of the following is true:
1745 * - the connection is already in recovery
1746 * - sequence until snd_recover has been acknowledged.
1747 * - retransmit timeout has fired
1748 */
1749 if (IN_FASTRECOVERY(tp) ||
1750 SEQ_GEQ(tp->snd_una, tp->snd_recover) ||
0a7de745 1751 tp->t_rxtshift > 0) {
fe8ab488 1752 break;
0a7de745 1753 }
fe8ab488
A
1754
1755 VERIFY(SACK_ENABLED(tp));
3e170ce0
A
1756 tcp_rexmt_save_state(tp);
1757 if (CC_ALGO(tp)->pre_fr != NULL) {
fe8ab488 1758 CC_ALGO(tp)->pre_fr(tp);
0a7de745 1759 if (TCP_ECN_ENABLED(tp)) {
3e170ce0 1760 tp->ecn_flags |= TE_SENDCWR;
0a7de745 1761 }
3e170ce0 1762 }
fe8ab488 1763 ENTER_FASTRECOVERY(tp);
fe8ab488
A
1764
1765 tp->t_timer[TCPT_REXMT] = 0;
1766 tcpstat.tcps_sack_recovery_episode++;
4bd07ac2 1767 tp->t_sack_recovery_episode++;
fe8ab488
A
1768 tp->sack_newdata = tp->snd_nxt;
1769 tp->snd_cwnd = tp->t_maxseg;
1770 tcp_ccdbg_trace(tp, NULL, TCP_CC_ENTER_FASTRECOVERY);
1771 (void) tcp_output(tp);
1772 break;
0a7de745 1773dropit:
1c79356b 1774 tcpstat.tcps_keepdrops++;
316670eb 1775 soevent(so,
0a7de745 1776 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_TIMEOUT));
1c79356b 1777 tp = tcp_drop(tp, ETIMEDOUT);
1c79356b
A
1778 break;
1779 }
fe8ab488 1780#if TCPDEBUG
0a7de745 1781 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) {
fe8ab488 1782 tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0,
0a7de745
A
1783 PRU_SLOWTIMO);
1784 }
fe8ab488 1785#endif
0a7de745 1786 return tp;
1c79356b 1787}
6d2010ae
A
1788
1789/* Remove a timer entry from timer list */
1790void
1791tcp_remove_timer(struct tcpcb *tp)
1792{
1793 struct tcptimerlist *listp = &tcp_timer_list;
1794
5ba3f43e 1795 socket_lock_assert_owned(tp->t_inpcb->inp_socket);
6d2010ae
A
1796 if (!(TIMER_IS_ON_LIST(tp))) {
1797 return;
1798 }
1799 lck_mtx_lock(listp->mtx);
39037602 1800
6d2010ae
A
1801 /* Check if pcb is on timer list again after acquiring the lock */
1802 if (!(TIMER_IS_ON_LIST(tp))) {
1803 lck_mtx_unlock(listp->mtx);
1804 return;
1805 }
39037602 1806
0a7de745 1807 if (listp->next_te != NULL && listp->next_te == &tp->tentry) {
6d2010ae 1808 listp->next_te = LIST_NEXT(&tp->tentry, le);
0a7de745 1809 }
6d2010ae
A
1810
1811 LIST_REMOVE(&tp->tentry, le);
1812 tp->t_flags &= ~(TF_TIMER_ONLIST);
1813
1814 listp->entries--;
6d2010ae
A
1815
1816 tp->tentry.le.le_next = NULL;
1817 tp->tentry.le.le_prev = NULL;
ebb1b9f4 1818 lck_mtx_unlock(listp->mtx);
6d2010ae
A
1819}
1820
fe8ab488
A
1821/*
1822 * Function to check if the timerlist needs to be rescheduled to run
6d2010ae
A
1823 * the timer entry correctly. Basically, this is to check if we can avoid
1824 * taking the list lock.
1825 */
1826
1827static boolean_t
fe8ab488
A
1828need_to_resched_timerlist(u_int32_t runtime, u_int16_t mode)
1829{
6d2010ae
A
1830 struct tcptimerlist *listp = &tcp_timer_list;
1831 int32_t diff;
6d2010ae 1832
fe8ab488
A
1833 /*
1834 * If the list is being processed then the state of the list is
1835 * in flux. In this case always acquire the lock and set the state
1836 * correctly.
6d2010ae 1837 */
0a7de745
A
1838 if (listp->running) {
1839 return TRUE;
1840 }
8a3053a0 1841
0a7de745
A
1842 if (!listp->scheduled) {
1843 return TRUE;
1844 }
6d2010ae
A
1845
1846 diff = timer_diff(listp->runtime, 0, runtime, 0);
1847 if (diff <= 0) {
1848 /* The list is going to run before this timer */
0a7de745 1849 return FALSE;
6d2010ae 1850 } else {
fe8ab488 1851 if (mode & TCP_TIMERLIST_10MS_MODE) {
0a7de745
A
1852 if (diff <= TCP_TIMER_10MS_QUANTUM) {
1853 return FALSE;
1854 }
fe8ab488 1855 } else if (mode & TCP_TIMERLIST_100MS_MODE) {
0a7de745
A
1856 if (diff <= TCP_TIMER_100MS_QUANTUM) {
1857 return FALSE;
1858 }
6d2010ae 1859 } else {
0a7de745
A
1860 if (diff <= TCP_TIMER_500MS_QUANTUM) {
1861 return FALSE;
1862 }
6d2010ae
A
1863 }
1864 }
0a7de745 1865 return TRUE;
6d2010ae
A
1866}
1867
1868void
39037602 1869tcp_sched_timerlist(uint32_t offset)
6d2010ae 1870{
6d2010ae
A
1871 uint64_t deadline = 0;
1872 struct tcptimerlist *listp = &tcp_timer_list;
1873
5ba3f43e 1874 LCK_MTX_ASSERT(listp->mtx, LCK_MTX_ASSERT_OWNED);
6d2010ae 1875
8a3053a0 1876 offset = min(offset, TCP_TIMERLIST_MAX_OFFSET);
6d2010ae 1877 listp->runtime = tcp_now + offset;
5ba3f43e 1878 listp->schedtime = tcp_now;
fe8ab488 1879 if (listp->runtime == 0) {
8a3053a0 1880 listp->runtime++;
fe8ab488
A
1881 offset++;
1882 }
6d2010ae 1883
fe8ab488 1884 clock_interval_to_deadline(offset, USEC_PER_SEC, &deadline);
6d2010ae
A
1885
1886 thread_call_enter_delayed(listp->call, deadline);
8a3053a0 1887 listp->scheduled = TRUE;
6d2010ae
A
1888}
1889
fe8ab488
A
1890/*
1891 * Function to run the timers for a connection.
6d2010ae 1892 *
39037602 1893 * Returns the offset of next timer to be run for this connection which
6d2010ae 1894 * can be used to reschedule the timerlist.
fe8ab488
A
1895 *
1896 * te_mode is an out parameter that indicates the modes of active
1897 * timers for this connection.
6d2010ae 1898 */
fe8ab488 1899u_int32_t
3e170ce0 1900tcp_run_conn_timer(struct tcpcb *tp, u_int16_t *te_mode,
0a7de745 1901 u_int16_t probe_if_index)
3e170ce0 1902{
fe8ab488
A
1903 struct socket *so;
1904 u_int16_t i = 0, index = TCPT_NONE, lo_index = TCPT_NONE;
1905 u_int32_t timer_val, offset = 0, lo_timer = 0;
6d2010ae
A
1906 int32_t diff;
1907 boolean_t needtorun[TCPT_NTIMERS];
1908 int count = 0;
1909
fe8ab488
A
1910 VERIFY(tp != NULL);
1911 bzero(needtorun, sizeof(needtorun));
1912 *te_mode = 0;
6d2010ae 1913
5ba3f43e 1914 socket_lock(tp->t_inpcb->inp_socket, 1);
6d2010ae 1915
fe8ab488 1916 so = tp->t_inpcb->inp_socket;
39037602 1917 /* Release the want count on inp */
fe8ab488 1918 if (in_pcb_checkstate(tp->t_inpcb, WNT_RELEASE, 1)
0a7de745 1919 == WNT_STOPUSING) {
6d2010ae
A
1920 if (TIMER_IS_ON_LIST(tp)) {
1921 tcp_remove_timer(tp);
1922 }
1923
39037602 1924 /* Looks like the TCP connection got closed while we
6d2010ae
A
1925 * were waiting for the lock.. Done
1926 */
1927 goto done;
1928 }
1929
3e170ce0
A
1930 /*
1931 * If this connection is over an interface that needs to
1932 * be probed, send probe packets to reinitiate communication.
1933 */
cb323159
A
1934 if (TCP_IF_STATE_CHANGED(tp, probe_if_index)) {
1935 tp->t_flagsext |= TF_IF_PROBING;
3e170ce0
A
1936 tcp_timers(tp, TCPT_PTO);
1937 tp->t_timer[TCPT_PTO] = 0;
cb323159 1938 tp->t_flagsext &= ~TF_IF_PROBING;
3e170ce0
A
1939 }
1940
fe8ab488
A
1941 /*
1942 * Since the timer thread needs to wait for tcp lock, it may race
1943 * with another thread that can cancel or reschedule the timer
1944 * that is about to run. Check if we need to run anything.
1945 */
0a7de745 1946 if ((index = tp->tentry.index) == TCPT_NONE) {
6d2010ae 1947 goto done;
0a7de745 1948 }
39037602 1949
8a3053a0 1950 timer_val = tp->t_timer[index];
6d2010ae
A
1951
1952 diff = timer_diff(tp->tentry.runtime, 0, tcp_now, 0);
1953 if (diff > 0) {
1954 if (tp->tentry.index != TCPT_NONE) {
1955 offset = diff;
fe8ab488 1956 *(te_mode) = tp->tentry.mode;
6d2010ae
A
1957 }
1958 goto done;
1959 }
1960
1961 tp->t_timer[index] = 0;
1962 if (timer_val > 0) {
1963 tp = tcp_timers(tp, index);
0a7de745 1964 if (tp == NULL) {
6d2010ae 1965 goto done;
0a7de745 1966 }
6d2010ae 1967 }
39037602 1968
fe8ab488
A
1969 /*
1970 * Check if there are any other timers that need to be run.
1971 * While doing it, adjust the timer values wrt tcp_now.
6d2010ae 1972 */
fe8ab488 1973 tp->tentry.mode = 0;
6d2010ae
A
1974 for (i = 0; i < TCPT_NTIMERS; ++i) {
1975 if (tp->t_timer[i] != 0) {
fe8ab488 1976 diff = timer_diff(tp->tentry.timer_start,
0a7de745 1977 tp->t_timer[i], tcp_now, 0);
6d2010ae 1978 if (diff <= 0) {
6d2010ae
A
1979 needtorun[i] = TRUE;
1980 count++;
1981 } else {
1982 tp->t_timer[i] = diff;
1983 needtorun[i] = FALSE;
1984 if (lo_timer == 0 || diff < lo_timer) {
1985 lo_timer = diff;
1986 lo_index = i;
1987 }
fe8ab488 1988 TCP_SET_TIMER_MODE(tp->tentry.mode, i);
6d2010ae
A
1989 }
1990 }
1991 }
39037602 1992
6d2010ae
A
1993 tp->tentry.timer_start = tcp_now;
1994 tp->tentry.index = lo_index;
fe8ab488
A
1995 VERIFY(tp->tentry.index == TCPT_NONE || tp->tentry.mode > 0);
1996
1997 if (tp->tentry.index != TCPT_NONE) {
1998 tp->tentry.runtime = tp->tentry.timer_start +
0a7de745
A
1999 tp->t_timer[tp->tentry.index];
2000 if (tp->tentry.runtime == 0) {
8a3053a0 2001 tp->tentry.runtime++;
0a7de745 2002 }
6d2010ae
A
2003 }
2004
2005 if (count > 0) {
fe8ab488 2006 /* run any other timers outstanding at this time. */
6d2010ae
A
2007 for (i = 0; i < TCPT_NTIMERS; ++i) {
2008 if (needtorun[i]) {
2009 tp->t_timer[i] = 0;
2010 tp = tcp_timers(tp, i);
8a3053a0
A
2011 if (tp == NULL) {
2012 offset = 0;
fe8ab488 2013 *(te_mode) = 0;
6d2010ae 2014 goto done;
8a3053a0 2015 }
6d2010ae
A
2016 }
2017 }
2018 tcp_set_lotimer_index(tp);
2019 }
2020
2021 if (tp->tentry.index < TCPT_NONE) {
2022 offset = tp->t_timer[tp->tentry.index];
fe8ab488 2023 *(te_mode) = tp->tentry.mode;
6d2010ae
A
2024 }
2025
2026done:
2027 if (tp != NULL && tp->tentry.index == TCPT_NONE) {
2028 tcp_remove_timer(tp);
8a3053a0 2029 offset = 0;
6d2010ae 2030 }
fe8ab488 2031
5ba3f43e 2032 socket_unlock(so, 1);
0a7de745 2033 return offset;
6d2010ae
A
2034}
2035
2036void
39037602
A
2037tcp_run_timerlist(void * arg1, void * arg2)
2038{
6d2010ae 2039#pragma unused(arg1, arg2)
6d2010ae
A
2040 struct tcptimerentry *te, *next_te;
2041 struct tcptimerlist *listp = &tcp_timer_list;
2042 struct tcpcb *tp;
fe8ab488 2043 uint32_t next_timer = 0; /* offset of the next timer on the list */
0a7de745 2044 u_int16_t te_mode = 0; /* modes of all active timers in a tcpcb */
fe8ab488 2045 u_int16_t list_mode = 0; /* cumulative of modes of all tcpcbs */
6d2010ae 2046 uint32_t active_count = 0;
6d2010ae
A
2047
2048 calculate_tcp_clock();
2049
2050 lck_mtx_lock(listp->mtx);
2051
5ba3f43e
A
2052 int32_t drift = tcp_now - listp->runtime;
2053 if (drift <= 1) {
2054 tcpstat.tcps_timer_drift_le_1_ms++;
2055 } else if (drift <= 10) {
2056 tcpstat.tcps_timer_drift_le_10_ms++;
2057 } else if (drift <= 20) {
2058 tcpstat.tcps_timer_drift_le_20_ms++;
2059 } else if (drift <= 50) {
2060 tcpstat.tcps_timer_drift_le_50_ms++;
2061 } else if (drift <= 100) {
2062 tcpstat.tcps_timer_drift_le_100_ms++;
2063 } else if (drift <= 200) {
2064 tcpstat.tcps_timer_drift_le_200_ms++;
2065 } else if (drift <= 500) {
2066 tcpstat.tcps_timer_drift_le_500_ms++;
2067 } else if (drift <= 1000) {
2068 tcpstat.tcps_timer_drift_le_1000_ms++;
2069 } else {
2070 tcpstat.tcps_timer_drift_gt_1000_ms++;
2071 }
2072
6d2010ae 2073 listp->running = TRUE;
39037602 2074
6d2010ae
A
2075 LIST_FOREACH_SAFE(te, &listp->lhead, le, next_te) {
2076 uint32_t offset = 0;
2077 uint32_t runtime = te->runtime;
cb323159
A
2078
2079 tp = TIMERENTRY_TO_TP(te);
2080
2081 /*
2082 * An interface probe may need to happen before the previously scheduled runtime
2083 */
2084 if (te->index < TCPT_NONE && TSTMP_GT(runtime, tcp_now) &&
2085 !TCP_IF_STATE_CHANGED(tp, listp->probe_if_index)) {
6d2010ae
A
2086 offset = timer_diff(runtime, 0, tcp_now, 0);
2087 if (next_timer == 0 || offset < next_timer) {
2088 next_timer = offset;
2089 }
fe8ab488 2090 list_mode |= te->mode;
6d2010ae
A
2091 continue;
2092 }
6d2010ae 2093
fe8ab488
A
2094 /*
2095 * Acquire an inp wantcnt on the inpcb so that the socket
2096 * won't get detached even if tcp_close is called
6d2010ae 2097 */
fe8ab488
A
2098 if (in_pcb_checkstate(tp->t_inpcb, WNT_ACQUIRE, 0)
2099 == WNT_STOPUSING) {
2100 /*
2101 * Some how this pcb went into dead state while
2102 * on the timer list, just take it off the list.
2103 * Since the timer list entry pointers are
39037602 2104 * protected by the timer list lock, we can
fe8ab488 2105 * do it here without the socket lock.
6d2010ae
A
2106 */
2107 if (TIMER_IS_ON_LIST(tp)) {
2108 tp->t_flags &= ~(TF_TIMER_ONLIST);
2109 LIST_REMOVE(&tp->tentry, le);
2110 listp->entries--;
2111
2112 tp->tentry.le.le_next = NULL;
2113 tp->tentry.le.le_prev = NULL;
2114 }
2115 continue;
2116 }
fe8ab488 2117 active_count++;
6d2010ae 2118
fe8ab488
A
2119 /*
2120 * Store the next timerentry pointer before releasing the
2121 * list lock. If that entry has to be removed when we
2122 * release the lock, this pointer will be updated to the
2123 * element after that.
6d2010ae 2124 */
39037602 2125 listp->next_te = next_te;
6d2010ae
A
2126
2127 VERIFY_NEXT_LINK(&tp->tentry, le);
2128 VERIFY_PREV_LINK(&tp->tentry, le);
2129
2130 lck_mtx_unlock(listp->mtx);
2131
3e170ce0
A
2132 offset = tcp_run_conn_timer(tp, &te_mode,
2133 listp->probe_if_index);
39037602 2134
6d2010ae
A
2135 lck_mtx_lock(listp->mtx);
2136
2137 next_te = listp->next_te;
2138 listp->next_te = NULL;
2139
fe8ab488
A
2140 if (offset > 0 && te_mode != 0) {
2141 list_mode |= te_mode;
6d2010ae 2142
0a7de745 2143 if (next_timer == 0 || offset < next_timer) {
fe8ab488 2144 next_timer = offset;
0a7de745 2145 }
6d2010ae
A
2146 }
2147 }
2148
2149 if (!LIST_EMPTY(&listp->lhead)) {
f427ee49 2150 uint32_t next_mode = 0;
fe8ab488 2151 if ((list_mode & TCP_TIMERLIST_10MS_MODE) ||
0a7de745 2152 (listp->pref_mode & TCP_TIMERLIST_10MS_MODE)) {
fe8ab488 2153 next_mode = TCP_TIMERLIST_10MS_MODE;
0a7de745
A
2154 } else if ((list_mode & TCP_TIMERLIST_100MS_MODE) ||
2155 (listp->pref_mode & TCP_TIMERLIST_100MS_MODE)) {
fe8ab488 2156 next_mode = TCP_TIMERLIST_100MS_MODE;
0a7de745 2157 } else {
fe8ab488 2158 next_mode = TCP_TIMERLIST_500MS_MODE;
0a7de745 2159 }
6d2010ae 2160
fe8ab488
A
2161 if (next_mode != TCP_TIMERLIST_500MS_MODE) {
2162 listp->idleruns = 0;
6d2010ae 2163 } else {
fe8ab488
A
2164 /*
2165 * the next required mode is slow mode, but if
2166 * the last one was a faster mode and we did not
2167 * have enough idle runs, repeat the last mode.
2168 *
2169 * We try to keep the timer list in fast mode for
2170 * some idle time in expectation of new data.
2171 */
2172 if (listp->mode != next_mode &&
2173 listp->idleruns < timer_fastmode_idlemax) {
2174 listp->idleruns++;
2175 next_mode = listp->mode;
2176 next_timer = TCP_TIMER_100MS_QUANTUM;
2177 } else {
2178 listp->idleruns = 0;
2179 }
6d2010ae 2180 }
fe8ab488 2181 listp->mode = next_mode;
0a7de745 2182 if (listp->pref_offset != 0) {
fe8ab488 2183 next_timer = min(listp->pref_offset, next_timer);
0a7de745 2184 }
6d2010ae 2185
0a7de745 2186 if (listp->mode == TCP_TIMERLIST_500MS_MODE) {
fe8ab488 2187 next_timer = max(next_timer,
0a7de745
A
2188 TCP_TIMER_500MS_QUANTUM);
2189 }
6d2010ae
A
2190
2191 tcp_sched_timerlist(next_timer);
2192 } else {
8a3053a0
A
2193 /*
2194 * No need to reschedule this timer, but always run
2195 * periodically at a much higher granularity.
2196 */
2197 tcp_sched_timerlist(TCP_TIMERLIST_MAX_OFFSET);
6d2010ae
A
2198 }
2199
2200 listp->running = FALSE;
2201 listp->pref_mode = 0;
2202 listp->pref_offset = 0;
3e170ce0 2203 listp->probe_if_index = 0;
6d2010ae
A
2204
2205 lck_mtx_unlock(listp->mtx);
2206}
2207
fe8ab488 2208/*
3e170ce0 2209 * Function to check if the timerlist needs to be rescheduled to run this
fe8ab488
A
2210 * connection's timers correctly.
2211 */
39037602
A
2212void
2213tcp_sched_timers(struct tcpcb *tp)
6d2010ae
A
2214{
2215 struct tcptimerentry *te = &tp->tentry;
fe8ab488
A
2216 u_int16_t index = te->index;
2217 u_int16_t mode = te->mode;
6d2010ae 2218 struct tcptimerlist *listp = &tcp_timer_list;
8a3053a0 2219 int32_t offset = 0;
fe8ab488 2220 boolean_t list_locked = FALSE;
6d2010ae
A
2221
2222 if (tp->t_inpcb->inp_state == INPCB_STATE_DEAD) {
2223 /* Just return without adding the dead pcb to the list */
2224 if (TIMER_IS_ON_LIST(tp)) {
2225 tcp_remove_timer(tp);
2226 }
2227 return;
2228 }
2229
2230 if (index == TCPT_NONE) {
fe8ab488 2231 /* Nothing to run */
6d2010ae
A
2232 tcp_remove_timer(tp);
2233 return;
2234 }
2235
fe8ab488
A
2236 /*
2237 * compute the offset at which the next timer for this connection
2238 * has to run.
2239 */
8a3053a0
A
2240 offset = timer_diff(te->runtime, 0, tcp_now, 0);
2241 if (offset <= 0) {
6d2010ae
A
2242 offset = 1;
2243 tcp_timer_advanced++;
2244 }
6d2010ae
A
2245
2246 if (!TIMER_IS_ON_LIST(tp)) {
2247 if (!list_locked) {
2248 lck_mtx_lock(listp->mtx);
fe8ab488 2249 list_locked = TRUE;
6d2010ae
A
2250 }
2251
813fb2f6
A
2252 if (!TIMER_IS_ON_LIST(tp)) {
2253 LIST_INSERT_HEAD(&listp->lhead, te, le);
2254 tp->t_flags |= TF_TIMER_ONLIST;
6d2010ae 2255
813fb2f6 2256 listp->entries++;
0a7de745 2257 if (listp->entries > listp->maxentries) {
813fb2f6 2258 listp->maxentries = listp->entries;
0a7de745 2259 }
6d2010ae 2260
813fb2f6 2261 /* if the list is not scheduled, just schedule it */
0a7de745 2262 if (!listp->scheduled) {
813fb2f6 2263 goto schedule;
0a7de745 2264 }
813fb2f6 2265 }
6d2010ae
A
2266 }
2267
fe8ab488
A
2268 /*
2269 * Timer entry is currently on the list, check if the list needs
2270 * to be rescheduled.
2271 */
2272 if (need_to_resched_timerlist(te->runtime, mode)) {
6d2010ae 2273 tcp_resched_timerlist++;
39037602 2274
6d2010ae
A
2275 if (!list_locked) {
2276 lck_mtx_lock(listp->mtx);
fe8ab488 2277 list_locked = TRUE;
6d2010ae
A
2278 }
2279
2280 VERIFY_NEXT_LINK(te, le);
2281 VERIFY_PREV_LINK(te, le);
2282
2283 if (listp->running) {
fe8ab488
A
2284 listp->pref_mode |= mode;
2285 if (listp->pref_offset == 0 ||
0a7de745 2286 offset < listp->pref_offset) {
6d2010ae
A
2287 listp->pref_offset = offset;
2288 }
2289 } else {
8a3053a0 2290 /*
fe8ab488
A
2291 * The list could have got rescheduled while
2292 * this thread was waiting for the lock
8a3053a0
A
2293 */
2294 if (listp->scheduled) {
2295 int32_t diff;
2296 diff = timer_diff(listp->runtime, 0,
2297 tcp_now, offset);
0a7de745 2298 if (diff <= 0) {
8a3053a0 2299 goto done;
0a7de745 2300 } else {
8a3053a0 2301 goto schedule;
0a7de745 2302 }
6d2010ae
A
2303 } else {
2304 goto schedule;
2305 }
2306 }
2307 }
2308 goto done;
2309
2310schedule:
fe8ab488
A
2311 /*
2312 * Since a connection with timers is getting scheduled, the timer
2313 * list moves from idle to active state and that is why idlegen is
2314 * reset
2315 */
2316 if (mode & TCP_TIMERLIST_10MS_MODE) {
2317 listp->mode = TCP_TIMERLIST_10MS_MODE;
2318 listp->idleruns = 0;
2319 offset = min(offset, TCP_TIMER_10MS_QUANTUM);
2320 } else if (mode & TCP_TIMERLIST_100MS_MODE) {
0a7de745 2321 if (listp->mode > TCP_TIMERLIST_100MS_MODE) {
fe8ab488 2322 listp->mode = TCP_TIMERLIST_100MS_MODE;
0a7de745 2323 }
fe8ab488
A
2324 listp->idleruns = 0;
2325 offset = min(offset, TCP_TIMER_100MS_QUANTUM);
6d2010ae
A
2326 }
2327 tcp_sched_timerlist(offset);
2328
2329done:
0a7de745 2330 if (list_locked) {
6d2010ae 2331 lck_mtx_unlock(listp->mtx);
0a7de745 2332 }
6d2010ae
A
2333
2334 return;
2335}
39037602 2336
fe8ab488 2337static inline void
3e170ce0
A
2338tcp_set_lotimer_index(struct tcpcb *tp)
2339{
fe8ab488 2340 uint16_t i, lo_index = TCPT_NONE, mode = 0;
6d2010ae
A
2341 uint32_t lo_timer = 0;
2342 for (i = 0; i < TCPT_NTIMERS; ++i) {
fe8ab488
A
2343 if (tp->t_timer[i] != 0) {
2344 TCP_SET_TIMER_MODE(mode, i);
2345 if (lo_timer == 0 || tp->t_timer[i] < lo_timer) {
2346 lo_timer = tp->t_timer[i];
2347 lo_index = i;
2348 }
6d2010ae
A
2349 }
2350 }
2351 tp->tentry.index = lo_index;
fe8ab488
A
2352 tp->tentry.mode = mode;
2353 VERIFY(tp->tentry.index == TCPT_NONE || tp->tentry.mode > 0);
2354
2355 if (tp->tentry.index != TCPT_NONE) {
39037602 2356 tp->tentry.runtime = tp->tentry.timer_start
fe8ab488 2357 + tp->t_timer[tp->tentry.index];
0a7de745 2358 if (tp->tentry.runtime == 0) {
8a3053a0 2359 tp->tentry.runtime++;
0a7de745 2360 }
6d2010ae
A
2361 }
2362}
2363
2364void
3e170ce0
A
2365tcp_check_timer_state(struct tcpcb *tp)
2366{
5ba3f43e 2367 socket_lock_assert_owned(tp->t_inpcb->inp_socket);
6d2010ae 2368
0a7de745 2369 if (tp->t_inpcb->inp_flags2 & INP2_TIMEWAIT) {
8a3053a0 2370 return;
0a7de745 2371 }
8a3053a0 2372
6d2010ae
A
2373 tcp_set_lotimer_index(tp);
2374
2375 tcp_sched_timers(tp);
2376 return;
2377}
fe8ab488 2378
3e170ce0
A
2379static inline void
2380tcp_cumulative_stat(u_int32_t cur, u_int32_t *prev, u_int32_t *dest)
2381{
2382 /* handle wrap around */
2383 int32_t diff = (int32_t) (cur - *prev);
0a7de745 2384 if (diff > 0) {
3e170ce0 2385 *dest = diff;
0a7de745 2386 } else {
3e170ce0 2387 *dest = 0;
0a7de745 2388 }
3e170ce0
A
2389 *prev = cur;
2390 return;
2391}
2392
5ba3f43e
A
2393static inline void
2394tcp_cumulative_stat64(u_int64_t cur, u_int64_t *prev, u_int64_t *dest)
2395{
2396 /* handle wrap around */
2397 int64_t diff = (int64_t) (cur - *prev);
0a7de745 2398 if (diff > 0) {
5ba3f43e 2399 *dest = diff;
0a7de745 2400 } else {
5ba3f43e 2401 *dest = 0;
0a7de745 2402 }
5ba3f43e
A
2403 *prev = cur;
2404 return;
2405}
2406
fe8ab488
A
2407__private_extern__ void
2408tcp_report_stats(void)
2409{
2410 struct nstat_sysinfo_data data;
2411 struct sockaddr_in dst;
2412 struct sockaddr_in6 dst6;
2413 struct rtentry *rt = NULL;
3e170ce0 2414 static struct tcp_last_report_stats prev;
39037602 2415 u_int64_t var, uptime;
fe8ab488 2416
0a7de745 2417#define stat data.u.tcp_stats
fe8ab488 2418 if (((uptime = net_uptime()) - tcp_last_report_time) <
0a7de745 2419 tcp_report_stats_interval) {
fe8ab488 2420 return;
0a7de745 2421 }
fe8ab488
A
2422
2423 tcp_last_report_time = uptime;
2424
2425 bzero(&data, sizeof(data));
2426 data.flags = NSTAT_SYSINFO_TCP_STATS;
2427
2428 bzero(&dst, sizeof(dst));
2429 dst.sin_len = sizeof(dst);
2430 dst.sin_family = AF_INET;
2431
2432 /* ipv4 avg rtt */
2433 lck_mtx_lock(rnh_lock);
2434 rt = rt_lookup(TRUE, (struct sockaddr *)&dst, NULL,
0a7de745 2435 rt_tables[AF_INET], IFSCOPE_NONE);
fe8ab488
A
2436 lck_mtx_unlock(rnh_lock);
2437 if (rt != NULL) {
2438 RT_LOCK(rt);
2439 if (rt_primary_default(rt, rt_key(rt)) &&
0a7de745 2440 rt->rt_stats != NULL) {
fe8ab488
A
2441 stat.ipv4_avgrtt = rt->rt_stats->nstat_avg_rtt;
2442 }
2443 RT_UNLOCK(rt);
2444 rtfree(rt);
2445 rt = NULL;
2446 }
2447
2448 /* ipv6 avg rtt */
2449 bzero(&dst6, sizeof(dst6));
2450 dst6.sin6_len = sizeof(dst6);
2451 dst6.sin6_family = AF_INET6;
2452
2453 lck_mtx_lock(rnh_lock);
0a7de745
A
2454 rt = rt_lookup(TRUE, (struct sockaddr *)&dst6, NULL,
2455 rt_tables[AF_INET6], IFSCOPE_NONE);
fe8ab488
A
2456 lck_mtx_unlock(rnh_lock);
2457 if (rt != NULL) {
2458 RT_LOCK(rt);
2459 if (rt_primary_default(rt, rt_key(rt)) &&
0a7de745 2460 rt->rt_stats != NULL) {
fe8ab488
A
2461 stat.ipv6_avgrtt = rt->rt_stats->nstat_avg_rtt;
2462 }
2463 RT_UNLOCK(rt);
2464 rtfree(rt);
2465 rt = NULL;
2466 }
2467
2468 /* send packet loss rate, shift by 10 for precision */
2469 if (tcpstat.tcps_sndpack > 0 && tcpstat.tcps_sndrexmitpack > 0) {
2470 var = tcpstat.tcps_sndrexmitpack << 10;
f427ee49 2471 stat.send_plr = (uint32_t)((var * 100) / tcpstat.tcps_sndpack);
fe8ab488
A
2472 }
2473
2474 /* recv packet loss rate, shift by 10 for precision */
2475 if (tcpstat.tcps_rcvpack > 0 && tcpstat.tcps_recovered_pkts > 0) {
2476 var = tcpstat.tcps_recovered_pkts << 10;
f427ee49 2477 stat.recv_plr = (uint32_t)((var * 100) / tcpstat.tcps_rcvpack);
fe8ab488
A
2478 }
2479
2480 /* RTO after tail loss, shift by 10 for precision */
39037602 2481 if (tcpstat.tcps_sndrexmitpack > 0
fe8ab488
A
2482 && tcpstat.tcps_tailloss_rto > 0) {
2483 var = tcpstat.tcps_tailloss_rto << 10;
2484 stat.send_tlrto_rate =
f427ee49 2485 (uint32_t)((var * 100) / tcpstat.tcps_sndrexmitpack);
fe8ab488 2486 }
39037602 2487
fe8ab488
A
2488 /* packet reordering */
2489 if (tcpstat.tcps_sndpack > 0 && tcpstat.tcps_reordered_pkts > 0) {
2490 var = tcpstat.tcps_reordered_pkts << 10;
2491 stat.send_reorder_rate =
f427ee49 2492 (uint32_t)((var * 100) / tcpstat.tcps_sndpack);
fe8ab488
A
2493 }
2494
0a7de745 2495 if (tcp_ecn_outbound == 1) {
3e170ce0 2496 stat.ecn_client_enabled = 1;
0a7de745
A
2497 }
2498 if (tcp_ecn_inbound == 1) {
3e170ce0 2499 stat.ecn_server_enabled = 1;
0a7de745 2500 }
3e170ce0
A
2501 tcp_cumulative_stat(tcpstat.tcps_connattempt,
2502 &prev.tcps_connattempt, &stat.connection_attempts);
2503 tcp_cumulative_stat(tcpstat.tcps_accepts,
2504 &prev.tcps_accepts, &stat.connection_accepts);
2505 tcp_cumulative_stat(tcpstat.tcps_ecn_client_setup,
2506 &prev.tcps_ecn_client_setup, &stat.ecn_client_setup);
2507 tcp_cumulative_stat(tcpstat.tcps_ecn_server_setup,
2508 &prev.tcps_ecn_server_setup, &stat.ecn_server_setup);
2509 tcp_cumulative_stat(tcpstat.tcps_ecn_client_success,
2510 &prev.tcps_ecn_client_success, &stat.ecn_client_success);
2511 tcp_cumulative_stat(tcpstat.tcps_ecn_server_success,
2512 &prev.tcps_ecn_server_success, &stat.ecn_server_success);
2513 tcp_cumulative_stat(tcpstat.tcps_ecn_not_supported,
2514 &prev.tcps_ecn_not_supported, &stat.ecn_not_supported);
2515 tcp_cumulative_stat(tcpstat.tcps_ecn_lost_syn,
2516 &prev.tcps_ecn_lost_syn, &stat.ecn_lost_syn);
2517 tcp_cumulative_stat(tcpstat.tcps_ecn_lost_synack,
2518 &prev.tcps_ecn_lost_synack, &stat.ecn_lost_synack);
2519 tcp_cumulative_stat(tcpstat.tcps_ecn_recv_ce,
2520 &prev.tcps_ecn_recv_ce, &stat.ecn_recv_ce);
2521 tcp_cumulative_stat(tcpstat.tcps_ecn_recv_ece,
2522 &prev.tcps_ecn_recv_ece, &stat.ecn_recv_ece);
2523 tcp_cumulative_stat(tcpstat.tcps_ecn_recv_ece,
2524 &prev.tcps_ecn_recv_ece, &stat.ecn_recv_ece);
2525 tcp_cumulative_stat(tcpstat.tcps_ecn_sent_ece,
2526 &prev.tcps_ecn_sent_ece, &stat.ecn_sent_ece);
2527 tcp_cumulative_stat(tcpstat.tcps_ecn_sent_ece,
2528 &prev.tcps_ecn_sent_ece, &stat.ecn_sent_ece);
2529 tcp_cumulative_stat(tcpstat.tcps_ecn_conn_recv_ce,
2530 &prev.tcps_ecn_conn_recv_ce, &stat.ecn_conn_recv_ce);
2531 tcp_cumulative_stat(tcpstat.tcps_ecn_conn_recv_ece,
2532 &prev.tcps_ecn_conn_recv_ece, &stat.ecn_conn_recv_ece);
2533 tcp_cumulative_stat(tcpstat.tcps_ecn_conn_plnoce,
2534 &prev.tcps_ecn_conn_plnoce, &stat.ecn_conn_plnoce);
2535 tcp_cumulative_stat(tcpstat.tcps_ecn_conn_pl_ce,
2536 &prev.tcps_ecn_conn_pl_ce, &stat.ecn_conn_pl_ce);
2537 tcp_cumulative_stat(tcpstat.tcps_ecn_conn_nopl_ce,
2538 &prev.tcps_ecn_conn_nopl_ce, &stat.ecn_conn_nopl_ce);
4bd07ac2
A
2539 tcp_cumulative_stat(tcpstat.tcps_ecn_fallback_synloss,
2540 &prev.tcps_ecn_fallback_synloss, &stat.ecn_fallback_synloss);
2541 tcp_cumulative_stat(tcpstat.tcps_ecn_fallback_reorder,
2542 &prev.tcps_ecn_fallback_reorder, &stat.ecn_fallback_reorder);
2543 tcp_cumulative_stat(tcpstat.tcps_ecn_fallback_ce,
2544 &prev.tcps_ecn_fallback_ce, &stat.ecn_fallback_ce);
3e170ce0
A
2545 tcp_cumulative_stat(tcpstat.tcps_tfo_syn_data_rcv,
2546 &prev.tcps_tfo_syn_data_rcv, &stat.tfo_syn_data_rcv);
2547 tcp_cumulative_stat(tcpstat.tcps_tfo_cookie_req_rcv,
2548 &prev.tcps_tfo_cookie_req_rcv, &stat.tfo_cookie_req_rcv);
2549 tcp_cumulative_stat(tcpstat.tcps_tfo_cookie_sent,
2550 &prev.tcps_tfo_cookie_sent, &stat.tfo_cookie_sent);
2551 tcp_cumulative_stat(tcpstat.tcps_tfo_cookie_invalid,
2552 &prev.tcps_tfo_cookie_invalid, &stat.tfo_cookie_invalid);
2553 tcp_cumulative_stat(tcpstat.tcps_tfo_cookie_req,
2554 &prev.tcps_tfo_cookie_req, &stat.tfo_cookie_req);
2555 tcp_cumulative_stat(tcpstat.tcps_tfo_cookie_rcv,
2556 &prev.tcps_tfo_cookie_rcv, &stat.tfo_cookie_rcv);
2557 tcp_cumulative_stat(tcpstat.tcps_tfo_syn_data_sent,
2558 &prev.tcps_tfo_syn_data_sent, &stat.tfo_syn_data_sent);
2559 tcp_cumulative_stat(tcpstat.tcps_tfo_syn_data_acked,
2560 &prev.tcps_tfo_syn_data_acked, &stat.tfo_syn_data_acked);
2561 tcp_cumulative_stat(tcpstat.tcps_tfo_syn_loss,
2562 &prev.tcps_tfo_syn_loss, &stat.tfo_syn_loss);
2563 tcp_cumulative_stat(tcpstat.tcps_tfo_blackhole,
2564 &prev.tcps_tfo_blackhole, &stat.tfo_blackhole);
39037602
A
2565 tcp_cumulative_stat(tcpstat.tcps_tfo_cookie_wrong,
2566 &prev.tcps_tfo_cookie_wrong, &stat.tfo_cookie_wrong);
2567 tcp_cumulative_stat(tcpstat.tcps_tfo_no_cookie_rcv,
2568 &prev.tcps_tfo_no_cookie_rcv, &stat.tfo_no_cookie_rcv);
2569 tcp_cumulative_stat(tcpstat.tcps_tfo_heuristics_disable,
2570 &prev.tcps_tfo_heuristics_disable, &stat.tfo_heuristics_disable);
2571 tcp_cumulative_stat(tcpstat.tcps_tfo_sndblackhole,
2572 &prev.tcps_tfo_sndblackhole, &stat.tfo_sndblackhole);
2573
2574
5ba3f43e 2575 tcp_cumulative_stat(tcpstat.tcps_mptcp_handover_attempt,
0a7de745 2576 &prev.tcps_mptcp_handover_attempt, &stat.mptcp_handover_attempt);
5ba3f43e 2577 tcp_cumulative_stat(tcpstat.tcps_mptcp_interactive_attempt,
0a7de745 2578 &prev.tcps_mptcp_interactive_attempt, &stat.mptcp_interactive_attempt);
5ba3f43e 2579 tcp_cumulative_stat(tcpstat.tcps_mptcp_aggregate_attempt,
0a7de745 2580 &prev.tcps_mptcp_aggregate_attempt, &stat.mptcp_aggregate_attempt);
5ba3f43e 2581 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_handover_attempt,
0a7de745 2582 &prev.tcps_mptcp_fp_handover_attempt, &stat.mptcp_fp_handover_attempt);
5ba3f43e 2583 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_interactive_attempt,
0a7de745 2584 &prev.tcps_mptcp_fp_interactive_attempt, &stat.mptcp_fp_interactive_attempt);
5ba3f43e 2585 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_aggregate_attempt,
0a7de745 2586 &prev.tcps_mptcp_fp_aggregate_attempt, &stat.mptcp_fp_aggregate_attempt);
5ba3f43e 2587 tcp_cumulative_stat(tcpstat.tcps_mptcp_heuristic_fallback,
0a7de745 2588 &prev.tcps_mptcp_heuristic_fallback, &stat.mptcp_heuristic_fallback);
5ba3f43e 2589 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_heuristic_fallback,
0a7de745 2590 &prev.tcps_mptcp_fp_heuristic_fallback, &stat.mptcp_fp_heuristic_fallback);
5ba3f43e 2591 tcp_cumulative_stat(tcpstat.tcps_mptcp_handover_success_wifi,
0a7de745 2592 &prev.tcps_mptcp_handover_success_wifi, &stat.mptcp_handover_success_wifi);
5ba3f43e 2593 tcp_cumulative_stat(tcpstat.tcps_mptcp_handover_success_cell,
0a7de745 2594 &prev.tcps_mptcp_handover_success_cell, &stat.mptcp_handover_success_cell);
5ba3f43e 2595 tcp_cumulative_stat(tcpstat.tcps_mptcp_interactive_success,
0a7de745 2596 &prev.tcps_mptcp_interactive_success, &stat.mptcp_interactive_success);
5ba3f43e 2597 tcp_cumulative_stat(tcpstat.tcps_mptcp_aggregate_success,
0a7de745 2598 &prev.tcps_mptcp_aggregate_success, &stat.mptcp_aggregate_success);
5ba3f43e 2599 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_handover_success_wifi,
0a7de745 2600 &prev.tcps_mptcp_fp_handover_success_wifi, &stat.mptcp_fp_handover_success_wifi);
5ba3f43e 2601 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_handover_success_cell,
0a7de745 2602 &prev.tcps_mptcp_fp_handover_success_cell, &stat.mptcp_fp_handover_success_cell);
5ba3f43e 2603 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_interactive_success,
0a7de745 2604 &prev.tcps_mptcp_fp_interactive_success, &stat.mptcp_fp_interactive_success);
5ba3f43e 2605 tcp_cumulative_stat(tcpstat.tcps_mptcp_fp_aggregate_success,
0a7de745 2606 &prev.tcps_mptcp_fp_aggregate_success, &stat.mptcp_fp_aggregate_success);
5ba3f43e 2607 tcp_cumulative_stat(tcpstat.tcps_mptcp_handover_cell_from_wifi,
0a7de745 2608 &prev.tcps_mptcp_handover_cell_from_wifi, &stat.mptcp_handover_cell_from_wifi);
5ba3f43e 2609 tcp_cumulative_stat(tcpstat.tcps_mptcp_handover_wifi_from_cell,
0a7de745 2610 &prev.tcps_mptcp_handover_wifi_from_cell, &stat.mptcp_handover_wifi_from_cell);
5ba3f43e 2611 tcp_cumulative_stat(tcpstat.tcps_mptcp_interactive_cell_from_wifi,
0a7de745 2612 &prev.tcps_mptcp_interactive_cell_from_wifi, &stat.mptcp_interactive_cell_from_wifi);
5ba3f43e 2613 tcp_cumulative_stat64(tcpstat.tcps_mptcp_handover_cell_bytes,
0a7de745 2614 &prev.tcps_mptcp_handover_cell_bytes, &stat.mptcp_handover_cell_bytes);
5ba3f43e 2615 tcp_cumulative_stat64(tcpstat.tcps_mptcp_interactive_cell_bytes,
0a7de745 2616 &prev.tcps_mptcp_interactive_cell_bytes, &stat.mptcp_interactive_cell_bytes);
5ba3f43e 2617 tcp_cumulative_stat64(tcpstat.tcps_mptcp_aggregate_cell_bytes,
0a7de745 2618 &prev.tcps_mptcp_aggregate_cell_bytes, &stat.mptcp_aggregate_cell_bytes);
5ba3f43e 2619 tcp_cumulative_stat64(tcpstat.tcps_mptcp_handover_all_bytes,
0a7de745 2620 &prev.tcps_mptcp_handover_all_bytes, &stat.mptcp_handover_all_bytes);
5ba3f43e 2621 tcp_cumulative_stat64(tcpstat.tcps_mptcp_interactive_all_bytes,
0a7de745 2622 &prev.tcps_mptcp_interactive_all_bytes, &stat.mptcp_interactive_all_bytes);
5ba3f43e 2623 tcp_cumulative_stat64(tcpstat.tcps_mptcp_aggregate_all_bytes,
0a7de745 2624 &prev.tcps_mptcp_aggregate_all_bytes, &stat.mptcp_aggregate_all_bytes);
5ba3f43e 2625 tcp_cumulative_stat(tcpstat.tcps_mptcp_back_to_wifi,
0a7de745 2626 &prev.tcps_mptcp_back_to_wifi, &stat.mptcp_back_to_wifi);
5ba3f43e 2627 tcp_cumulative_stat(tcpstat.tcps_mptcp_wifi_proxy,
0a7de745 2628 &prev.tcps_mptcp_wifi_proxy, &stat.mptcp_wifi_proxy);
5ba3f43e 2629 tcp_cumulative_stat(tcpstat.tcps_mptcp_cell_proxy,
0a7de745 2630 &prev.tcps_mptcp_cell_proxy, &stat.mptcp_cell_proxy);
d9a64523
A
2631 tcp_cumulative_stat(tcpstat.tcps_mptcp_triggered_cell,
2632 &prev.tcps_mptcp_triggered_cell, &stat.mptcp_triggered_cell);
3e170ce0 2633
fe8ab488
A
2634 nstat_sysinfo_send_data(&data);
2635
0a7de745 2636#undef stat
fe8ab488 2637}
3e170ce0
A
2638
2639void
2640tcp_interface_send_probe(u_int16_t probe_if_index)
2641{
2642 int32_t offset = 0;
2643 struct tcptimerlist *listp = &tcp_timer_list;
2644
2645 /* Make sure TCP clock is up to date */
2646 calculate_tcp_clock();
2647
2648 lck_mtx_lock(listp->mtx);
cb323159 2649 if (listp->probe_if_index > 0 && listp->probe_if_index != probe_if_index) {
3e170ce0 2650 tcpstat.tcps_probe_if_conflict++;
cb323159
A
2651 os_log(OS_LOG_DEFAULT,
2652 "%s: probe_if_index %u conflicts with %u, tcps_probe_if_conflict %u\n",
2653 __func__, probe_if_index, listp->probe_if_index,
2654 tcpstat.tcps_probe_if_conflict);
3e170ce0
A
2655 goto done;
2656 }
2657
2658 listp->probe_if_index = probe_if_index;
0a7de745 2659 if (listp->running) {
cb323159
A
2660 os_log(OS_LOG_DEFAULT, "%s: timer list already running for if_index %u\n",
2661 __func__, probe_if_index);
3e170ce0 2662 goto done;
0a7de745 2663 }
3e170ce0
A
2664
2665 /*
2666 * Reschedule the timerlist to run within the next 10ms, which is
2667 * the fastest that we can do.
2668 */
2669 offset = TCP_TIMER_10MS_QUANTUM;
2670 if (listp->scheduled) {
2671 int32_t diff;
2672 diff = timer_diff(listp->runtime, 0, tcp_now, offset);
2673 if (diff <= 0) {
2674 /* The timer will fire sooner than what's needed */
cb323159
A
2675 os_log(OS_LOG_DEFAULT,
2676 "%s: timer will fire sooner than needed for if_index %u\n",
2677 __func__, probe_if_index);
3e170ce0
A
2678 goto done;
2679 }
2680 }
2681 listp->mode = TCP_TIMERLIST_10MS_MODE;
2682 listp->idleruns = 0;
2683
2684 tcp_sched_timerlist(offset);
2685
2686done:
2687 lck_mtx_unlock(listp->mtx);
2688 return;
2689}
2690
2691/*
2692 * Enable read probes on this connection, if:
2693 * - it is in established state
2694 * - doesn't have any data outstanding
2695 * - the outgoing ifp matches
2696 * - we have not already sent any read probes
2697 */
2698static void
2699tcp_enable_read_probe(struct tcpcb *tp, struct ifnet *ifp)
2700{
2701 if (tp->t_state == TCPS_ESTABLISHED &&
2702 tp->snd_max == tp->snd_una &&
2703 tp->t_inpcb->inp_last_outifp == ifp &&
2704 !(tp->t_flagsext & TF_DETECT_READSTALL) &&
2705 tp->t_rtimo_probes == 0) {
2706 tp->t_flagsext |= TF_DETECT_READSTALL;
2707 tp->t_rtimo_probes = 0;
2708 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
2709 TCP_TIMER_10MS_QUANTUM);
2710 if (tp->tentry.index == TCPT_NONE) {
2711 tp->tentry.index = TCPT_KEEP;
2712 tp->tentry.runtime = tcp_now +
2713 TCP_TIMER_10MS_QUANTUM;
2714 } else {
2715 int32_t diff = 0;
2716
2717 /* Reset runtime to be in next 10ms */
2718 diff = timer_diff(tp->tentry.runtime, 0,
2719 tcp_now, TCP_TIMER_10MS_QUANTUM);
2720 if (diff > 0) {
2721 tp->tentry.index = TCPT_KEEP;
2722 tp->tentry.runtime = tcp_now +
2723 TCP_TIMER_10MS_QUANTUM;
0a7de745 2724 if (tp->tentry.runtime == 0) {
3e170ce0 2725 tp->tentry.runtime++;
0a7de745 2726 }
3e170ce0
A
2727 }
2728 }
2729 }
2730}
2731
2732/*
2733 * Disable read probe and reset the keep alive timer
2734 */
2735static void
2736tcp_disable_read_probe(struct tcpcb *tp)
2737{
2738 if (tp->t_adaptive_rtimo == 0 &&
2739 ((tp->t_flagsext & TF_DETECT_READSTALL) ||
2740 tp->t_rtimo_probes > 0)) {
2741 tcp_keepalive_reset(tp);
5ba3f43e 2742
0a7de745 2743 if (tp->t_mpsub) {
5ba3f43e 2744 mptcp_reset_keepalive(tp);
0a7de745 2745 }
3e170ce0
A
2746 }
2747}
2748
2749/*
2750 * Reschedule the tcp timerlist in the next 10ms to re-enable read/write
2751 * probes on connections going over a particular interface.
2752 */
2753void
2754tcp_probe_connectivity(struct ifnet *ifp, u_int32_t enable)
2755{
2756 int32_t offset;
2757 struct tcptimerlist *listp = &tcp_timer_list;
2758 struct inpcbinfo *pcbinfo = &tcbinfo;
2759 struct inpcb *inp, *nxt;
2760
0a7de745 2761 if (ifp == NULL) {
3e170ce0 2762 return;
0a7de745 2763 }
3e170ce0
A
2764
2765 /* update clock */
2766 calculate_tcp_clock();
2767
2768 /*
2769 * Enable keep alive timer on all connections that are
2770 * active/established on this interface.
2771 */
2772 lck_rw_lock_shared(pcbinfo->ipi_lock);
2773
2774 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, nxt) {
2775 struct tcpcb *tp = NULL;
2776 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
0a7de745 2777 WNT_STOPUSING) {
3e170ce0 2778 continue;
0a7de745 2779 }
3e170ce0
A
2780
2781 /* Acquire lock to look at the state of the connection */
5ba3f43e 2782 socket_lock(inp->inp_socket, 1);
3e170ce0
A
2783
2784 /* Release the want count */
490019cf
A
2785 if (inp->inp_ppcb == NULL ||
2786 (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING)) {
5ba3f43e 2787 socket_unlock(inp->inp_socket, 1);
3e170ce0
A
2788 continue;
2789 }
3e170ce0 2790 tp = intotcpcb(inp);
0a7de745 2791 if (enable) {
3e170ce0 2792 tcp_enable_read_probe(tp, ifp);
0a7de745 2793 } else {
3e170ce0 2794 tcp_disable_read_probe(tp);
0a7de745 2795 }
3e170ce0 2796
5ba3f43e 2797 socket_unlock(inp->inp_socket, 1);
3e170ce0
A
2798 }
2799 lck_rw_done(pcbinfo->ipi_lock);
2800
2801 lck_mtx_lock(listp->mtx);
2802 if (listp->running) {
2803 listp->pref_mode |= TCP_TIMERLIST_10MS_MODE;
2804 goto done;
2805 }
2806
2807 /* Reschedule within the next 10ms */
2808 offset = TCP_TIMER_10MS_QUANTUM;
2809 if (listp->scheduled) {
2810 int32_t diff;
2811 diff = timer_diff(listp->runtime, 0, tcp_now, offset);
2812 if (diff <= 0) {
2813 /* The timer will fire sooner than what's needed */
2814 goto done;
2815 }
2816 }
2817 listp->mode = TCP_TIMERLIST_10MS_MODE;
2818 listp->idleruns = 0;
2819
2820 tcp_sched_timerlist(offset);
2821done:
2822 lck_mtx_unlock(listp->mtx);
2823 return;
2824}
2825
39037602
A
2826inline void
2827tcp_update_mss_core(struct tcpcb *tp, struct ifnet *ifp)
2828{
2829 struct if_cellular_status_v1 *ifsr;
2830 u_int32_t optlen;
2831 ifsr = &ifp->if_link_status->ifsr_u.ifsr_cell.if_cell_u.if_status_v1;
2832 if (ifsr->valid_bitmask & IF_CELL_UL_MSS_RECOMMENDED_VALID) {
2833 optlen = tp->t_maxopd - tp->t_maxseg;
2834
2835 if (ifsr->mss_recommended ==
2836 IF_CELL_UL_MSS_RECOMMENDED_NONE &&
2837 tp->t_cached_maxopd > 0 &&
2838 tp->t_maxopd < tp->t_cached_maxopd) {
2839 tp->t_maxopd = tp->t_cached_maxopd;
2840 tcpstat.tcps_mss_to_default++;
2841 } else if (ifsr->mss_recommended ==
2842 IF_CELL_UL_MSS_RECOMMENDED_MEDIUM &&
2843 tp->t_maxopd > tcp_mss_rec_medium) {
2844 tp->t_cached_maxopd = tp->t_maxopd;
2845 tp->t_maxopd = tcp_mss_rec_medium;
2846 tcpstat.tcps_mss_to_medium++;
2847 } else if (ifsr->mss_recommended ==
2848 IF_CELL_UL_MSS_RECOMMENDED_LOW &&
2849 tp->t_maxopd > tcp_mss_rec_low) {
2850 tp->t_cached_maxopd = tp->t_maxopd;
2851 tp->t_maxopd = tcp_mss_rec_low;
2852 tcpstat.tcps_mss_to_low++;
2853 }
2854 tp->t_maxseg = tp->t_maxopd - optlen;
2855
2856 /*
2857 * clear the cached value if it is same as the current
2858 */
0a7de745 2859 if (tp->t_maxopd == tp->t_cached_maxopd) {
39037602 2860 tp->t_cached_maxopd = 0;
0a7de745 2861 }
39037602
A
2862 }
2863}
2864
2865void
2866tcp_update_mss_locked(struct socket *so, struct ifnet *ifp)
2867{
2868 struct inpcb *inp = sotoinpcb(so);
2869 struct tcpcb *tp = intotcpcb(inp);
2870
0a7de745 2871 if (ifp == NULL && (ifp = inp->inp_last_outifp) == NULL) {
39037602 2872 return;
0a7de745 2873 }
39037602 2874
39037602
A
2875 if (!IFNET_IS_CELLULAR(ifp)) {
2876 /*
2877 * This optimization is implemented for cellular
2878 * networks only
2879 */
2880 return;
2881 }
0a7de745 2882 if (tp->t_state <= TCPS_CLOSE_WAIT) {
39037602
A
2883 /*
2884 * If the connection is currently doing or has done PMTU
2885 * blackhole detection, do not change the MSS
2886 */
0a7de745 2887 if (tp->t_flags & TF_BLACKHOLE) {
39037602 2888 return;
0a7de745
A
2889 }
2890 if (ifp->if_link_status == NULL) {
39037602 2891 return;
0a7de745 2892 }
39037602
A
2893 tcp_update_mss_core(tp, ifp);
2894 }
2895}
2896
3e170ce0
A
2897void
2898tcp_itimer(struct inpcbinfo *ipi)
2899{
2900 struct inpcb *inp, *nxt;
2901
2902 if (lck_rw_try_lock_exclusive(ipi->ipi_lock) == FALSE) {
2903 if (tcp_itimer_done == TRUE) {
2904 tcp_itimer_done = FALSE;
2905 atomic_add_32(&ipi->ipi_timer_req.intimer_fast, 1);
2906 return;
2907 }
2908 /* Upgrade failed, lost lock now take it again exclusive */
2909 lck_rw_lock_exclusive(ipi->ipi_lock);
2910 }
2911 tcp_itimer_done = TRUE;
2912
2913 LIST_FOREACH_SAFE(inp, &tcb, inp_list, nxt) {
2914 struct socket *so;
5ba3f43e 2915 struct ifnet *ifp;
3e170ce0 2916
39037602 2917 if (inp->inp_ppcb == NULL ||
0a7de745 2918 in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) {
3e170ce0 2919 continue;
0a7de745 2920 }
3e170ce0 2921 so = inp->inp_socket;
5ba3f43e
A
2922 ifp = inp->inp_last_outifp;
2923 socket_lock(so, 1);
3e170ce0 2924 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
5ba3f43e 2925 socket_unlock(so, 1);
3e170ce0
A
2926 continue;
2927 }
2928 so_check_extended_bk_idle_time(so);
39037602
A
2929 if (ipi->ipi_flags & INPCBINFO_UPDATE_MSS) {
2930 tcp_update_mss_locked(so, NULL);
2931 }
5ba3f43e
A
2932 socket_unlock(so, 1);
2933
2934 /*
2935 * Defunct all system-initiated background sockets if the
2936 * socket is using the cellular interface and the interface
2937 * has its LQM set to abort.
2938 */
2939 if ((ipi->ipi_flags & INPCBINFO_HANDLE_LQM_ABORT) &&
2940 IS_SO_TC_BACKGROUNDSYSTEM(so->so_traffic_class) &&
2941 ifp != NULL && IFNET_IS_CELLULAR(ifp) &&
2942 (ifp->if_interface_state.valid_bitmask &
2943 IF_INTERFACE_STATE_LQM_STATE_VALID) &&
2944 ifp->if_interface_state.lqm_state ==
2945 IFNET_LQM_THRESH_ABORT) {
2946 socket_defunct(current_proc(), so,
2947 SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL);
2948 }
3e170ce0
A
2949 }
2950
5ba3f43e 2951 ipi->ipi_flags &= ~(INPCBINFO_UPDATE_MSS | INPCBINFO_HANDLE_LQM_ABORT);
3e170ce0
A
2952 lck_rw_done(ipi->ipi_lock);
2953}