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