2 * Copyright (c) 2015-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* TCP-cache to store and retrieve TCP-related information */
31 #include <net/flowhash.h>
32 #include <net/route.h>
34 #include <netinet/in_pcb.h>
35 #include <netinet/mptcp_var.h>
36 #include <netinet/tcp_cache.h>
37 #include <netinet/tcp_seq.h>
38 #include <netinet/tcp_var.h>
39 #include <kern/locks.h>
40 #include <sys/queue.h>
41 #include <dev/random/randomdev.h>
45 struct in6_addr addr6
;
48 struct tcp_heuristic_key
{
50 uint8_t thk_net_signature
[IFNET_SIGNATURELEN
];
53 sa_family_t thk_family
;
56 struct tcp_heuristic
{
57 SLIST_ENTRY(tcp_heuristic
) list
;
59 uint32_t th_last_access
;
61 struct tcp_heuristic_key th_key
;
63 char th_val_start
[0]; /* Marker for memsetting to 0 */
65 uint8_t th_tfo_data_loss
; /* The number of times a SYN+data has been lost */
66 uint8_t th_tfo_req_loss
; /* The number of times a SYN+cookie-req has been lost */
67 uint8_t th_tfo_data_rst
; /* The number of times a SYN+data has received a RST */
68 uint8_t th_tfo_req_rst
; /* The number of times a SYN+cookie-req has received a RST */
69 uint8_t th_mptcp_loss
; /* The number of times a SYN+MP_CAPABLE has been lost */
70 uint8_t th_mptcp_success
; /* The number of times MPTCP-negotiation has been successful */
71 uint8_t th_ecn_loss
; /* The number of times a SYN+ecn has been lost */
72 uint8_t th_ecn_aggressive
; /* The number of times we did an aggressive fallback */
73 uint8_t th_ecn_droprst
; /* The number of times ECN connections received a RST after first data pkt */
74 uint8_t th_ecn_droprxmt
; /* The number of times ECN connection is dropped after multiple retransmits */
75 uint8_t th_ecn_synrst
; /* number of times RST was received in response to an ECN enabled SYN */
76 uint32_t th_tfo_enabled_time
; /* The moment when we reenabled TFO after backing off */
77 uint32_t th_tfo_backoff_until
; /* Time until when we should not try out TFO */
78 uint32_t th_tfo_backoff
; /* Current backoff timer */
79 uint32_t th_mptcp_backoff
; /* Time until when we should not try out MPTCP */
80 uint32_t th_ecn_backoff
; /* Time until when we should not try out ECN */
82 uint8_t th_tfo_in_backoff
:1, /* Are we avoiding TFO due to the backoff timer? */
83 th_mptcp_in_backoff
:1, /* Are we avoiding MPTCP due to the backoff timer? */
84 th_mptcp_heuristic_disabled
:1; /* Are heuristics disabled? */
86 char th_val_end
[0]; /* Marker for memsetting to 0 */
89 struct tcp_heuristics_head
{
90 SLIST_HEAD(tcp_heur_bucket
, tcp_heuristic
) tcp_heuristics
;
92 /* Per-hashbucket lock to avoid lock-contention */
96 struct tcp_cache_key
{
97 sa_family_t tck_family
;
99 struct tcp_heuristic_key tck_src
;
104 SLIST_ENTRY(tcp_cache
) list
;
106 u_int32_t tc_last_access
;
108 struct tcp_cache_key tc_key
;
110 u_int8_t tc_tfo_cookie
[TFO_COOKIE_LEN_MAX
];
111 u_int8_t tc_tfo_cookie_len
;
114 struct tcp_cache_head
{
115 SLIST_HEAD(tcp_cache_bucket
, tcp_cache
) tcp_caches
;
117 /* Per-hashbucket lock to avoid lock-contention */
121 struct tcp_cache_key_src
{
128 static u_int32_t tcp_cache_hash_seed
;
130 size_t tcp_cache_size
;
133 * The maximum depth of the hash-bucket. This way we limit the tcp_cache to
134 * TCP_CACHE_BUCKET_SIZE * tcp_cache_size and have "natural" garbage collection
136 #define TCP_CACHE_BUCKET_SIZE 5
138 static struct tcp_cache_head
*tcp_cache
;
140 decl_lck_mtx_data(, tcp_cache_mtx
);
142 static lck_attr_t
*tcp_cache_mtx_attr
;
143 static lck_grp_t
*tcp_cache_mtx_grp
;
144 static lck_grp_attr_t
*tcp_cache_mtx_grp_attr
;
146 static struct tcp_heuristics_head
*tcp_heuristics
;
148 decl_lck_mtx_data(, tcp_heuristics_mtx
);
150 static lck_attr_t
*tcp_heuristic_mtx_attr
;
151 static lck_grp_t
*tcp_heuristic_mtx_grp
;
152 static lck_grp_attr_t
*tcp_heuristic_mtx_grp_attr
;
154 static uint32_t tcp_backoff_maximum
= 65536;
156 SYSCTL_UINT(_net_inet_tcp
, OID_AUTO
, backoff_maximum
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
157 &tcp_backoff_maximum
, 0, "Maximum time for which we won't try TFO");
159 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, ecn_timeout
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
160 static int, tcp_ecn_timeout
, 60, "Initial minutes to wait before re-trying ECN");
162 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, disable_tcp_heuristics
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
163 static int, disable_tcp_heuristics
, 0, "Set to 1, to disable all TCP heuristics (TFO, ECN, MPTCP)");
166 tcp_min_to_hz(uint32_t minutes
)
168 if (minutes
> 65536) {
169 return (uint32_t)65536 * 60 * TCP_RETRANSHZ
;
172 return minutes
* 60 * TCP_RETRANSHZ
;
176 * This number is coupled with tcp_ecn_timeout, because we want to prevent
177 * integer overflow. Need to find an unexpensive way to prevent integer overflow
178 * while still allowing a dynamic sysctl.
180 #define TCP_CACHE_OVERFLOW_PROTECT 9
182 /* Number of SYN-losses we accept */
183 #define TFO_MAX_COOKIE_LOSS 2
184 #define ECN_MAX_SYN_LOSS 2
185 #define MPTCP_MAX_SYN_LOSS 2
186 #define MPTCP_SUCCESS_TRIGGER 10
187 #define ECN_MAX_DROPRST 1
188 #define ECN_MAX_DROPRXMT 4
189 #define ECN_MAX_SYNRST 4
191 /* Flags for setting/unsetting loss-heuristics, limited to 4 bytes */
192 #define TCPCACHE_F_TFO_REQ 0x01
193 #define TCPCACHE_F_TFO_DATA 0x02
194 #define TCPCACHE_F_ECN 0x04
195 #define TCPCACHE_F_MPTCP 0x08
196 #define TCPCACHE_F_ECN_DROPRST 0x10
197 #define TCPCACHE_F_ECN_DROPRXMT 0x20
198 #define TCPCACHE_F_TFO_REQ_RST 0x40
199 #define TCPCACHE_F_TFO_DATA_RST 0x80
200 #define TCPCACHE_F_ECN_SYNRST 0x100
202 /* Always retry ECN after backing off to this level for some heuristics */
203 #define ECN_RETRY_LIMIT 9
205 #define TCP_CACHE_INC_IFNET_STAT(_ifp_, _af_, _stat_) { \
206 if ((_ifp_) != NULL) { \
207 if ((_af_) == AF_INET6) { \
208 (_ifp_)->if_ipv6_stat->_stat_++;\
210 (_ifp_)->if_ipv4_stat->_stat_++;\
216 * Round up to next higher power-of 2. See "Bit Twiddling Hacks".
218 * Might be worth moving this to a library so that others
219 * (e.g., scale_to_powerof2()) can use this as well instead of a while-loop.
222 tcp_cache_roundup2(u_int32_t a
)
236 tcp_cache_hash_src(struct tcp_cache_key_src
*tcks
, struct tcp_heuristic_key
*key
)
238 struct ifnet
*ifp
= tcks
->ifp
;
239 uint8_t len
= sizeof(key
->thk_net_signature
);
242 if (tcks
->af
== AF_INET6
) {
245 key
->thk_family
= AF_INET6
;
246 ret
= ifnet_get_netsignature(ifp
, AF_INET6
, &len
, &flags
,
247 key
->thk_net_signature
);
250 * ifnet_get_netsignature only returns EINVAL if ifn is NULL
251 * (we made sure that in the other cases it does not). So,
252 * in this case we should take the connection's address.
254 if (ret
== ENOENT
|| ret
== EINVAL
) {
255 memcpy(&key
->thk_ip
.addr6
, &tcks
->laddr
.addr6
, sizeof(struct in6_addr
));
260 key
->thk_family
= AF_INET
;
261 ret
= ifnet_get_netsignature(ifp
, AF_INET
, &len
, &flags
,
262 key
->thk_net_signature
);
265 * ifnet_get_netsignature only returns EINVAL if ifn is NULL
266 * (we made sure that in the other cases it does not). So,
267 * in this case we should take the connection's address.
269 if (ret
== ENOENT
|| ret
== EINVAL
) {
270 memcpy(&key
->thk_ip
.addr
, &tcks
->laddr
.addr
, sizeof(struct in_addr
));
276 tcp_cache_hash(struct tcp_cache_key_src
*tcks
, struct tcp_cache_key
*key
)
280 bzero(key
, sizeof(struct tcp_cache_key
));
282 tcp_cache_hash_src(tcks
, &key
->tck_src
);
284 if (tcks
->af
== AF_INET6
) {
285 key
->tck_family
= AF_INET6
;
286 memcpy(&key
->tck_dst
.addr6
, &tcks
->faddr
.addr6
,
287 sizeof(struct in6_addr
));
289 key
->tck_family
= AF_INET
;
290 memcpy(&key
->tck_dst
.addr
, &tcks
->faddr
.addr
,
291 sizeof(struct in_addr
));
294 hash
= net_flowhash(key
, sizeof(struct tcp_cache_key
),
295 tcp_cache_hash_seed
);
297 return hash
& (tcp_cache_size
- 1);
301 tcp_cache_unlock(struct tcp_cache_head
*head
)
303 lck_mtx_unlock(&head
->tch_mtx
);
307 * Make sure that everything that happens after tcp_getcache_with_lock()
308 * is short enough to justify that you hold the per-bucket lock!!!
310 * Otherwise, better build another lookup-function that does not hold the
311 * lock and you copy out the bits and bytes.
313 * That's why we provide the head as a "return"-pointer so that the caller
314 * can give it back to use for tcp_cache_unlock().
316 static struct tcp_cache
*
317 tcp_getcache_with_lock(struct tcp_cache_key_src
*tcks
,
318 int create
, struct tcp_cache_head
**headarg
)
320 struct tcp_cache
*tpcache
= NULL
;
321 struct tcp_cache_head
*head
;
322 struct tcp_cache_key key
;
326 hash
= tcp_cache_hash(tcks
, &key
);
327 head
= &tcp_cache
[hash
];
329 lck_mtx_lock(&head
->tch_mtx
);
331 /*** First step: Look for the tcp_cache in our bucket ***/
332 SLIST_FOREACH(tpcache
, &head
->tcp_caches
, list
) {
333 if (memcmp(&tpcache
->tc_key
, &key
, sizeof(key
)) == 0) {
340 /*** Second step: If it's not there, create/recycle it ***/
341 if ((tpcache
== NULL
) && create
) {
342 if (i
>= TCP_CACHE_BUCKET_SIZE
) {
343 struct tcp_cache
*oldest_cache
= NULL
;
344 u_int32_t max_age
= 0;
346 /* Look for the oldest tcp_cache in the bucket */
347 SLIST_FOREACH(tpcache
, &head
->tcp_caches
, list
) {
348 u_int32_t age
= tcp_now
- tpcache
->tc_last_access
;
351 oldest_cache
= tpcache
;
354 VERIFY(oldest_cache
!= NULL
);
356 tpcache
= oldest_cache
;
358 /* We recycle, thus let's indicate that there is no cookie */
359 tpcache
->tc_tfo_cookie_len
= 0;
361 /* Create a new cache and add it to the list */
362 tpcache
= _MALLOC(sizeof(struct tcp_cache
), M_TEMP
,
364 if (tpcache
== NULL
) {
368 SLIST_INSERT_HEAD(&head
->tcp_caches
, tpcache
, list
);
371 memcpy(&tpcache
->tc_key
, &key
, sizeof(key
));
374 if (tpcache
== NULL
) {
378 /* Update timestamp for garbage collection purposes */
379 tpcache
->tc_last_access
= tcp_now
;
385 tcp_cache_unlock(head
);
390 tcp_cache_key_src_create(struct tcpcb
*tp
, struct tcp_cache_key_src
*tcks
)
392 struct inpcb
*inp
= tp
->t_inpcb
;
393 memset(tcks
, 0, sizeof(*tcks
));
395 tcks
->ifp
= inp
->inp_last_outifp
;
397 if (inp
->inp_vflag
& INP_IPV6
) {
398 memcpy(&tcks
->laddr
.addr6
, &inp
->in6p_laddr
, sizeof(struct in6_addr
));
399 memcpy(&tcks
->faddr
.addr6
, &inp
->in6p_faddr
, sizeof(struct in6_addr
));
402 memcpy(&tcks
->laddr
.addr
, &inp
->inp_laddr
, sizeof(struct in_addr
));
403 memcpy(&tcks
->faddr
.addr
, &inp
->inp_faddr
, sizeof(struct in_addr
));
411 tcp_cache_set_cookie_common(struct tcp_cache_key_src
*tcks
, u_char
*cookie
, u_int8_t len
)
413 struct tcp_cache_head
*head
;
414 struct tcp_cache
*tpcache
;
416 /* Call lookup/create function */
417 tpcache
= tcp_getcache_with_lock(tcks
, 1, &head
);
418 if (tpcache
== NULL
) {
422 tpcache
->tc_tfo_cookie_len
= len
> TFO_COOKIE_LEN_MAX
?
423 TFO_COOKIE_LEN_MAX
: len
;
424 memcpy(tpcache
->tc_tfo_cookie
, cookie
, tpcache
->tc_tfo_cookie_len
);
426 tcp_cache_unlock(head
);
430 tcp_cache_set_cookie(struct tcpcb
*tp
, u_char
*cookie
, u_int8_t len
)
432 struct tcp_cache_key_src tcks
;
434 tcp_cache_key_src_create(tp
, &tcks
);
435 tcp_cache_set_cookie_common(&tcks
, cookie
, len
);
439 tcp_cache_get_cookie_common(struct tcp_cache_key_src
*tcks
, u_char
*cookie
, u_int8_t
*len
)
441 struct tcp_cache_head
*head
;
442 struct tcp_cache
*tpcache
;
444 /* Call lookup/create function */
445 tpcache
= tcp_getcache_with_lock(tcks
, 1, &head
);
446 if (tpcache
== NULL
) {
450 if (tpcache
->tc_tfo_cookie_len
== 0) {
451 tcp_cache_unlock(head
);
456 * Not enough space - this should never happen as it has been checked
457 * in tcp_tfo_check. So, fail here!
459 VERIFY(tpcache
->tc_tfo_cookie_len
<= *len
);
461 memcpy(cookie
, tpcache
->tc_tfo_cookie
, tpcache
->tc_tfo_cookie_len
);
462 *len
= tpcache
->tc_tfo_cookie_len
;
464 tcp_cache_unlock(head
);
470 * Get the cookie related to 'tp', and copy it into 'cookie', provided that len
471 * is big enough (len designates the available memory.
472 * Upon return, 'len' is set to the cookie's length.
474 * Returns 0 if we should request a cookie.
475 * Returns 1 if the cookie has been found and written.
478 tcp_cache_get_cookie(struct tcpcb
*tp
, u_char
*cookie
, u_int8_t
*len
)
480 struct tcp_cache_key_src tcks
;
482 tcp_cache_key_src_create(tp
, &tcks
);
483 return tcp_cache_get_cookie_common(&tcks
, cookie
, len
);
487 tcp_cache_get_cookie_len_common(struct tcp_cache_key_src
*tcks
)
489 struct tcp_cache_head
*head
;
490 struct tcp_cache
*tpcache
;
491 unsigned int cookie_len
;
493 /* Call lookup/create function */
494 tpcache
= tcp_getcache_with_lock(tcks
, 1, &head
);
495 if (tpcache
== NULL
) {
499 cookie_len
= tpcache
->tc_tfo_cookie_len
;
501 tcp_cache_unlock(head
);
507 tcp_cache_get_cookie_len(struct tcpcb
*tp
)
509 struct tcp_cache_key_src tcks
;
511 tcp_cache_key_src_create(tp
, &tcks
);
512 return tcp_cache_get_cookie_len_common(&tcks
);
516 tcp_heuristics_hash(struct tcp_cache_key_src
*tcks
, struct tcp_heuristic_key
*key
)
520 bzero(key
, sizeof(struct tcp_heuristic_key
));
522 tcp_cache_hash_src(tcks
, key
);
524 hash
= net_flowhash(key
, sizeof(struct tcp_heuristic_key
),
525 tcp_cache_hash_seed
);
527 return hash
& (tcp_cache_size
- 1);
531 tcp_heuristic_unlock(struct tcp_heuristics_head
*head
)
533 lck_mtx_unlock(&head
->thh_mtx
);
537 * Make sure that everything that happens after tcp_getheuristic_with_lock()
538 * is short enough to justify that you hold the per-bucket lock!!!
540 * Otherwise, better build another lookup-function that does not hold the
541 * lock and you copy out the bits and bytes.
543 * That's why we provide the head as a "return"-pointer so that the caller
544 * can give it back to use for tcp_heur_unlock().
547 * ToDo - way too much code-duplication. We should create an interface to handle
548 * bucketized hashtables with recycling of the oldest element.
550 static struct tcp_heuristic
*
551 tcp_getheuristic_with_lock(struct tcp_cache_key_src
*tcks
,
552 int create
, struct tcp_heuristics_head
**headarg
)
554 struct tcp_heuristic
*tpheur
= NULL
;
555 struct tcp_heuristics_head
*head
;
556 struct tcp_heuristic_key key
;
560 hash
= tcp_heuristics_hash(tcks
, &key
);
561 head
= &tcp_heuristics
[hash
];
563 lck_mtx_lock(&head
->thh_mtx
);
565 /*** First step: Look for the tcp_heur in our bucket ***/
566 SLIST_FOREACH(tpheur
, &head
->tcp_heuristics
, list
) {
567 if (memcmp(&tpheur
->th_key
, &key
, sizeof(key
)) == 0) {
574 /*** Second step: If it's not there, create/recycle it ***/
575 if ((tpheur
== NULL
) && create
) {
576 if (i
>= TCP_CACHE_BUCKET_SIZE
) {
577 struct tcp_heuristic
*oldest_heur
= NULL
;
578 u_int32_t max_age
= 0;
580 /* Look for the oldest tcp_heur in the bucket */
581 SLIST_FOREACH(tpheur
, &head
->tcp_heuristics
, list
) {
582 u_int32_t age
= tcp_now
- tpheur
->th_last_access
;
585 oldest_heur
= tpheur
;
588 VERIFY(oldest_heur
!= NULL
);
590 tpheur
= oldest_heur
;
592 /* We recycle - set everything to 0 */
593 bzero(tpheur
->th_val_start
,
594 tpheur
->th_val_end
- tpheur
->th_val_start
);
596 /* Create a new heuristic and add it to the list */
597 tpheur
= _MALLOC(sizeof(struct tcp_heuristic
), M_TEMP
,
599 if (tpheur
== NULL
) {
603 SLIST_INSERT_HEAD(&head
->tcp_heuristics
, tpheur
, list
);
607 * Set to tcp_now, to make sure it won't be > than tcp_now in the
610 tpheur
->th_ecn_backoff
= tcp_now
;
611 tpheur
->th_tfo_backoff_until
= tcp_now
;
612 tpheur
->th_mptcp_backoff
= tcp_now
;
613 tpheur
->th_tfo_backoff
= tcp_min_to_hz(tcp_ecn_timeout
);
615 memcpy(&tpheur
->th_key
, &key
, sizeof(key
));
618 if (tpheur
== NULL
) {
622 /* Update timestamp for garbage collection purposes */
623 tpheur
->th_last_access
= tcp_now
;
629 tcp_heuristic_unlock(head
);
634 tcp_heuristic_reset_counters(struct tcp_cache_key_src
*tcks
, u_int8_t flags
)
636 struct tcp_heuristics_head
*head
;
637 struct tcp_heuristic
*tpheur
;
640 * Always create heuristics here because MPTCP needs to write success
641 * into it. Thus, we always end up creating them.
643 tpheur
= tcp_getheuristic_with_lock(tcks
, 1, &head
);
644 if (tpheur
== NULL
) {
648 if (flags
& TCPCACHE_F_TFO_DATA
) {
649 if (tpheur
->th_tfo_data_loss
>= TFO_MAX_COOKIE_LOSS
) {
650 os_log(OS_LOG_DEFAULT
, "%s: Resetting TFO-data loss to 0 from %u on heur %lx\n",
651 __func__
, tpheur
->th_tfo_data_loss
, (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
653 tpheur
->th_tfo_data_loss
= 0;
656 if (flags
& TCPCACHE_F_TFO_REQ
) {
657 if (tpheur
->th_tfo_req_loss
>= TFO_MAX_COOKIE_LOSS
) {
658 os_log(OS_LOG_DEFAULT
, "%s: Resetting TFO-req loss to 0 from %u on heur %lx\n",
659 __func__
, tpheur
->th_tfo_req_loss
, (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
661 tpheur
->th_tfo_req_loss
= 0;
664 if (flags
& TCPCACHE_F_TFO_DATA_RST
) {
665 if (tpheur
->th_tfo_data_rst
>= TFO_MAX_COOKIE_LOSS
) {
666 os_log(OS_LOG_DEFAULT
, "%s: Resetting TFO-data RST to 0 from %u on heur %lx\n",
667 __func__
, tpheur
->th_tfo_data_rst
, (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
669 tpheur
->th_tfo_data_rst
= 0;
672 if (flags
& TCPCACHE_F_TFO_REQ_RST
) {
673 if (tpheur
->th_tfo_req_rst
>= TFO_MAX_COOKIE_LOSS
) {
674 os_log(OS_LOG_DEFAULT
, "%s: Resetting TFO-req RST to 0 from %u on heur %lx\n",
675 __func__
, tpheur
->th_tfo_req_rst
, (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
677 tpheur
->th_tfo_req_rst
= 0;
680 if (flags
& TCPCACHE_F_ECN
) {
681 if (tpheur
->th_ecn_loss
>= ECN_MAX_SYN_LOSS
|| tpheur
->th_ecn_synrst
>= ECN_MAX_SYNRST
) {
682 os_log(OS_LOG_DEFAULT
, "%s: Resetting ECN-loss to 0 from %u and synrst from %u on heur %lx\n",
683 __func__
, tpheur
->th_ecn_loss
, tpheur
->th_ecn_synrst
, (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
685 tpheur
->th_ecn_loss
= 0;
686 tpheur
->th_ecn_synrst
= 0;
689 if (flags
& TCPCACHE_F_MPTCP
) {
690 tpheur
->th_mptcp_loss
= 0;
691 if (tpheur
->th_mptcp_success
< MPTCP_SUCCESS_TRIGGER
) {
692 tpheur
->th_mptcp_success
++;
694 if (tpheur
->th_mptcp_success
== MPTCP_SUCCESS_TRIGGER
) {
695 os_log(mptcp_log_handle
, "%s disabling heuristics for 12 hours", __func__
);
696 tpheur
->th_mptcp_heuristic_disabled
= 1;
697 /* Disable heuristics for 12 hours */
698 tpheur
->th_mptcp_backoff
= tcp_now
+ tcp_min_to_hz(tcp_ecn_timeout
* 12);
703 tcp_heuristic_unlock(head
);
707 tcp_heuristic_tfo_success(struct tcpcb
*tp
)
709 struct tcp_cache_key_src tcks
;
712 tcp_cache_key_src_create(tp
, &tcks
);
714 if (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) {
715 flag
= (TCPCACHE_F_TFO_DATA
| TCPCACHE_F_TFO_REQ
|
716 TCPCACHE_F_TFO_DATA_RST
| TCPCACHE_F_TFO_REQ_RST
);
718 if (tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
) {
719 flag
= (TCPCACHE_F_TFO_REQ
| TCPCACHE_F_TFO_REQ_RST
);
722 tcp_heuristic_reset_counters(&tcks
, flag
);
726 tcp_heuristic_mptcp_success(struct tcpcb
*tp
)
728 struct tcp_cache_key_src tcks
;
730 tcp_cache_key_src_create(tp
, &tcks
);
731 tcp_heuristic_reset_counters(&tcks
, TCPCACHE_F_MPTCP
);
735 tcp_heuristic_ecn_success(struct tcpcb
*tp
)
737 struct tcp_cache_key_src tcks
;
739 tcp_cache_key_src_create(tp
, &tcks
);
740 tcp_heuristic_reset_counters(&tcks
, TCPCACHE_F_ECN
);
744 __tcp_heuristic_tfo_middlebox_common(struct tcp_heuristic
*tpheur
)
746 if (tpheur
->th_tfo_in_backoff
) {
750 tpheur
->th_tfo_in_backoff
= 1;
752 if (tpheur
->th_tfo_enabled_time
) {
753 uint32_t old_backoff
= tpheur
->th_tfo_backoff
;
755 tpheur
->th_tfo_backoff
-= (tcp_now
- tpheur
->th_tfo_enabled_time
);
756 if (tpheur
->th_tfo_backoff
> old_backoff
) {
757 tpheur
->th_tfo_backoff
= tcp_min_to_hz(tcp_ecn_timeout
);
761 tpheur
->th_tfo_backoff_until
= tcp_now
+ tpheur
->th_tfo_backoff
;
763 /* Then, increase the backoff time */
764 tpheur
->th_tfo_backoff
*= 2;
766 if (tpheur
->th_tfo_backoff
> tcp_min_to_hz(tcp_backoff_maximum
)) {
767 tpheur
->th_tfo_backoff
= tcp_min_to_hz(tcp_ecn_timeout
);
770 os_log(OS_LOG_DEFAULT
, "%s disable TFO until %u now %u on %lx\n", __func__
,
771 tpheur
->th_tfo_backoff_until
, tcp_now
, (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
775 tcp_heuristic_tfo_middlebox_common(struct tcp_cache_key_src
*tcks
)
777 struct tcp_heuristics_head
*head
;
778 struct tcp_heuristic
*tpheur
;
780 tpheur
= tcp_getheuristic_with_lock(tcks
, 1, &head
);
781 if (tpheur
== NULL
) {
785 __tcp_heuristic_tfo_middlebox_common(tpheur
);
787 tcp_heuristic_unlock(head
);
791 tcp_heuristic_inc_counters(struct tcp_cache_key_src
*tcks
,
794 struct tcp_heuristics_head
*head
;
795 struct tcp_heuristic
*tpheur
;
797 tpheur
= tcp_getheuristic_with_lock(tcks
, 1, &head
);
798 if (tpheur
== NULL
) {
802 /* Limit to prevent integer-overflow during exponential backoff */
803 if ((flags
& TCPCACHE_F_TFO_DATA
) && tpheur
->th_tfo_data_loss
< TCP_CACHE_OVERFLOW_PROTECT
) {
804 tpheur
->th_tfo_data_loss
++;
806 if (tpheur
->th_tfo_data_loss
>= TFO_MAX_COOKIE_LOSS
) {
807 __tcp_heuristic_tfo_middlebox_common(tpheur
);
811 if ((flags
& TCPCACHE_F_TFO_REQ
) && tpheur
->th_tfo_req_loss
< TCP_CACHE_OVERFLOW_PROTECT
) {
812 tpheur
->th_tfo_req_loss
++;
814 if (tpheur
->th_tfo_req_loss
>= TFO_MAX_COOKIE_LOSS
) {
815 __tcp_heuristic_tfo_middlebox_common(tpheur
);
819 if ((flags
& TCPCACHE_F_TFO_DATA_RST
) && tpheur
->th_tfo_data_rst
< TCP_CACHE_OVERFLOW_PROTECT
) {
820 tpheur
->th_tfo_data_rst
++;
822 if (tpheur
->th_tfo_data_rst
>= TFO_MAX_COOKIE_LOSS
) {
823 __tcp_heuristic_tfo_middlebox_common(tpheur
);
827 if ((flags
& TCPCACHE_F_TFO_REQ_RST
) && tpheur
->th_tfo_req_rst
< TCP_CACHE_OVERFLOW_PROTECT
) {
828 tpheur
->th_tfo_req_rst
++;
830 if (tpheur
->th_tfo_req_rst
>= TFO_MAX_COOKIE_LOSS
) {
831 __tcp_heuristic_tfo_middlebox_common(tpheur
);
835 if ((flags
& TCPCACHE_F_ECN
) &&
836 tpheur
->th_ecn_loss
< TCP_CACHE_OVERFLOW_PROTECT
&&
837 TSTMP_LEQ(tpheur
->th_ecn_backoff
, tcp_now
)) {
838 tpheur
->th_ecn_loss
++;
839 if (tpheur
->th_ecn_loss
>= ECN_MAX_SYN_LOSS
) {
840 tcpstat
.tcps_ecn_fallback_synloss
++;
841 TCP_CACHE_INC_IFNET_STAT(tcks
->ifp
, tcks
->af
, ecn_fallback_synloss
);
842 tpheur
->th_ecn_backoff
= tcp_now
+
843 (tcp_min_to_hz(tcp_ecn_timeout
) <<
844 (tpheur
->th_ecn_loss
- ECN_MAX_SYN_LOSS
));
846 os_log(OS_LOG_DEFAULT
, "%s disable ECN until %u now %u on %lx for SYN-loss\n",
847 __func__
, tpheur
->th_ecn_backoff
, tcp_now
,
848 (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
852 if ((flags
& TCPCACHE_F_MPTCP
) &&
853 tpheur
->th_mptcp_loss
< TCP_CACHE_OVERFLOW_PROTECT
&&
854 tpheur
->th_mptcp_heuristic_disabled
== 0) {
855 tpheur
->th_mptcp_loss
++;
856 if (tpheur
->th_mptcp_loss
>= MPTCP_MAX_SYN_LOSS
) {
858 * Yes, we take tcp_ecn_timeout, to avoid adding yet
859 * another sysctl that is just used for testing.
861 tpheur
->th_mptcp_backoff
= tcp_now
+
862 (tcp_min_to_hz(tcp_ecn_timeout
) <<
863 (tpheur
->th_mptcp_loss
- MPTCP_MAX_SYN_LOSS
));
864 tpheur
->th_mptcp_in_backoff
= 1;
866 os_log(OS_LOG_DEFAULT
, "%s disable MPTCP until %u now %u on %lx\n",
867 __func__
, tpheur
->th_mptcp_backoff
, tcp_now
,
868 (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
872 if ((flags
& TCPCACHE_F_ECN_DROPRST
) &&
873 tpheur
->th_ecn_droprst
< TCP_CACHE_OVERFLOW_PROTECT
&&
874 TSTMP_LEQ(tpheur
->th_ecn_backoff
, tcp_now
)) {
875 tpheur
->th_ecn_droprst
++;
876 if (tpheur
->th_ecn_droprst
>= ECN_MAX_DROPRST
) {
877 tcpstat
.tcps_ecn_fallback_droprst
++;
878 TCP_CACHE_INC_IFNET_STAT(tcks
->ifp
, tcks
->af
,
879 ecn_fallback_droprst
);
880 tpheur
->th_ecn_backoff
= tcp_now
+
881 (tcp_min_to_hz(tcp_ecn_timeout
) <<
882 (tpheur
->th_ecn_droprst
- ECN_MAX_DROPRST
));
884 os_log(OS_LOG_DEFAULT
, "%s disable ECN until %u now %u on %lx for drop-RST\n",
885 __func__
, tpheur
->th_ecn_backoff
, tcp_now
,
886 (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
890 if ((flags
& TCPCACHE_F_ECN_DROPRXMT
) &&
891 tpheur
->th_ecn_droprxmt
< TCP_CACHE_OVERFLOW_PROTECT
&&
892 TSTMP_LEQ(tpheur
->th_ecn_backoff
, tcp_now
)) {
893 tpheur
->th_ecn_droprxmt
++;
894 if (tpheur
->th_ecn_droprxmt
>= ECN_MAX_DROPRXMT
) {
895 tcpstat
.tcps_ecn_fallback_droprxmt
++;
896 TCP_CACHE_INC_IFNET_STAT(tcks
->ifp
, tcks
->af
,
897 ecn_fallback_droprxmt
);
898 tpheur
->th_ecn_backoff
= tcp_now
+
899 (tcp_min_to_hz(tcp_ecn_timeout
) <<
900 (tpheur
->th_ecn_droprxmt
- ECN_MAX_DROPRXMT
));
902 os_log(OS_LOG_DEFAULT
, "%s disable ECN until %u now %u on %lx for drop-Rxmit\n",
903 __func__
, tpheur
->th_ecn_backoff
, tcp_now
,
904 (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
907 if ((flags
& TCPCACHE_F_ECN_SYNRST
) &&
908 tpheur
->th_ecn_synrst
< TCP_CACHE_OVERFLOW_PROTECT
) {
909 tpheur
->th_ecn_synrst
++;
910 if (tpheur
->th_ecn_synrst
>= ECN_MAX_SYNRST
) {
911 tcpstat
.tcps_ecn_fallback_synrst
++;
912 TCP_CACHE_INC_IFNET_STAT(tcks
->ifp
, tcks
->af
,
913 ecn_fallback_synrst
);
914 tpheur
->th_ecn_backoff
= tcp_now
+
915 (tcp_min_to_hz(tcp_ecn_timeout
) <<
916 (tpheur
->th_ecn_synrst
- ECN_MAX_SYNRST
));
918 os_log(OS_LOG_DEFAULT
, "%s disable ECN until %u now %u on %lx for SYN-RST\n",
919 __func__
, tpheur
->th_ecn_backoff
, tcp_now
,
920 (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
923 tcp_heuristic_unlock(head
);
927 tcp_heuristic_tfo_loss(struct tcpcb
*tp
)
929 struct tcp_cache_key_src tcks
;
932 if (symptoms_is_wifi_lossy() &&
933 IFNET_IS_WIFI(tp
->t_inpcb
->inp_last_outifp
)) {
937 tcp_cache_key_src_create(tp
, &tcks
);
939 if (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) {
940 flag
= (TCPCACHE_F_TFO_DATA
| TCPCACHE_F_TFO_REQ
);
942 if (tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
) {
943 flag
= TCPCACHE_F_TFO_REQ
;
946 tcp_heuristic_inc_counters(&tcks
, flag
);
950 tcp_heuristic_tfo_rst(struct tcpcb
*tp
)
952 struct tcp_cache_key_src tcks
;
955 tcp_cache_key_src_create(tp
, &tcks
);
957 if (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) {
958 flag
= (TCPCACHE_F_TFO_DATA_RST
| TCPCACHE_F_TFO_REQ_RST
);
960 if (tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
) {
961 flag
= TCPCACHE_F_TFO_REQ_RST
;
964 tcp_heuristic_inc_counters(&tcks
, flag
);
968 tcp_heuristic_mptcp_loss(struct tcpcb
*tp
)
970 struct tcp_cache_key_src tcks
;
972 if (symptoms_is_wifi_lossy() &&
973 IFNET_IS_WIFI(tp
->t_inpcb
->inp_last_outifp
)) {
977 tcp_cache_key_src_create(tp
, &tcks
);
979 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_MPTCP
);
983 tcp_heuristic_ecn_loss(struct tcpcb
*tp
)
985 struct tcp_cache_key_src tcks
;
987 if (symptoms_is_wifi_lossy() &&
988 IFNET_IS_WIFI(tp
->t_inpcb
->inp_last_outifp
)) {
992 tcp_cache_key_src_create(tp
, &tcks
);
994 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN
);
998 tcp_heuristic_ecn_droprst(struct tcpcb
*tp
)
1000 struct tcp_cache_key_src tcks
;
1002 tcp_cache_key_src_create(tp
, &tcks
);
1004 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN_DROPRST
);
1008 tcp_heuristic_ecn_droprxmt(struct tcpcb
*tp
)
1010 struct tcp_cache_key_src tcks
;
1012 tcp_cache_key_src_create(tp
, &tcks
);
1014 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN_DROPRXMT
);
1018 tcp_heuristic_ecn_synrst(struct tcpcb
*tp
)
1020 struct tcp_cache_key_src tcks
;
1022 tcp_cache_key_src_create(tp
, &tcks
);
1024 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN_SYNRST
);
1028 tcp_heuristic_tfo_middlebox(struct tcpcb
*tp
)
1030 struct tcp_cache_key_src tcks
;
1032 tp
->t_tfo_flags
|= TFO_F_HEURISTIC_DONE
;
1034 tcp_cache_key_src_create(tp
, &tcks
);
1035 tcp_heuristic_tfo_middlebox_common(&tcks
);
1039 tcp_heuristic_ecn_aggressive_common(struct tcp_cache_key_src
*tcks
)
1041 struct tcp_heuristics_head
*head
;
1042 struct tcp_heuristic
*tpheur
;
1044 tpheur
= tcp_getheuristic_with_lock(tcks
, 1, &head
);
1045 if (tpheur
== NULL
) {
1049 if (TSTMP_GT(tpheur
->th_ecn_backoff
, tcp_now
)) {
1050 /* We are already in aggressive mode */
1051 tcp_heuristic_unlock(head
);
1055 /* Must be done before, otherwise we will start off with expo-backoff */
1056 tpheur
->th_ecn_backoff
= tcp_now
+
1057 (tcp_min_to_hz(tcp_ecn_timeout
) << (tpheur
->th_ecn_aggressive
));
1060 * Ugly way to prevent integer overflow... limit to prevent in
1061 * overflow during exp. backoff.
1063 if (tpheur
->th_ecn_aggressive
< TCP_CACHE_OVERFLOW_PROTECT
) {
1064 tpheur
->th_ecn_aggressive
++;
1067 tcp_heuristic_unlock(head
);
1069 os_log(OS_LOG_DEFAULT
, "%s disable ECN until %u now %u on %lx\n", __func__
,
1070 tpheur
->th_ecn_backoff
, tcp_now
, (unsigned long)VM_KERNEL_ADDRPERM(tpheur
));
1074 tcp_heuristic_ecn_aggressive(struct tcpcb
*tp
)
1076 struct tcp_cache_key_src tcks
;
1078 tcp_cache_key_src_create(tp
, &tcks
);
1079 tcp_heuristic_ecn_aggressive_common(&tcks
);
1083 tcp_heuristic_do_tfo_common(struct tcp_cache_key_src
*tcks
)
1085 struct tcp_heuristics_head
*head
;
1086 struct tcp_heuristic
*tpheur
;
1088 if (disable_tcp_heuristics
) {
1092 /* Get the tcp-heuristic. */
1093 tpheur
= tcp_getheuristic_with_lock(tcks
, 0, &head
);
1094 if (tpheur
== NULL
) {
1098 if (tpheur
->th_tfo_in_backoff
== 0) {
1102 if (TSTMP_GT(tcp_now
, tpheur
->th_tfo_backoff_until
)) {
1103 tpheur
->th_tfo_in_backoff
= 0;
1104 tpheur
->th_tfo_enabled_time
= tcp_now
;
1109 tcp_heuristic_unlock(head
);
1113 tcp_heuristic_unlock(head
);
1118 tcp_heuristic_do_tfo(struct tcpcb
*tp
)
1120 struct tcp_cache_key_src tcks
;
1122 tcp_cache_key_src_create(tp
, &tcks
);
1123 if (tcp_heuristic_do_tfo_common(&tcks
)) {
1131 * 0 Enable MPTCP (we are still discovering middleboxes)
1132 * -1 Enable MPTCP (heuristics have been temporarily disabled)
1136 tcp_heuristic_do_mptcp(struct tcpcb
*tp
)
1138 struct tcp_cache_key_src tcks
;
1139 struct tcp_heuristics_head
*head
= NULL
;
1140 struct tcp_heuristic
*tpheur
;
1143 if (disable_tcp_heuristics
||
1144 (tptomptp(tp
)->mpt_mpte
->mpte_flags
& MPTE_FORCE_ENABLE
)) {
1148 tcp_cache_key_src_create(tp
, &tcks
);
1150 /* Get the tcp-heuristic. */
1151 tpheur
= tcp_getheuristic_with_lock(&tcks
, 0, &head
);
1152 if (tpheur
== NULL
) {
1156 if (tpheur
->th_mptcp_in_backoff
== 0 ||
1157 tpheur
->th_mptcp_heuristic_disabled
== 1) {
1161 if (TSTMP_GT(tpheur
->th_mptcp_backoff
, tcp_now
)) {
1165 tpheur
->th_mptcp_in_backoff
= 0;
1168 if (tpheur
->th_mptcp_heuristic_disabled
) {
1171 if (TSTMP_GT(tcp_now
, tpheur
->th_mptcp_backoff
)) {
1172 tpheur
->th_mptcp_heuristic_disabled
= 0;
1173 tpheur
->th_mptcp_success
= 0;
1177 tcp_heuristic_unlock(head
);
1182 tcp_heuristic_unlock(head
);
1185 if (tptomptp(tp
)->mpt_mpte
->mpte_flags
& MPTE_FIRSTPARTY
) {
1186 tcpstat
.tcps_mptcp_fp_heuristic_fallback
++;
1188 tcpstat
.tcps_mptcp_heuristic_fallback
++;
1195 tcp_heuristic_do_ecn_common(struct tcp_cache_key_src
*tcks
)
1197 struct tcp_heuristics_head
*head
;
1198 struct tcp_heuristic
*tpheur
;
1199 boolean_t ret
= TRUE
;
1201 if (disable_tcp_heuristics
) {
1205 /* Get the tcp-heuristic. */
1206 tpheur
= tcp_getheuristic_with_lock(tcks
, 0, &head
);
1207 if (tpheur
== NULL
) {
1211 if (TSTMP_GT(tpheur
->th_ecn_backoff
, tcp_now
)) {
1214 /* Reset the following counters to start re-evaluating */
1215 if (tpheur
->th_ecn_droprst
>= ECN_RETRY_LIMIT
) {
1216 tpheur
->th_ecn_droprst
= 0;
1218 if (tpheur
->th_ecn_droprxmt
>= ECN_RETRY_LIMIT
) {
1219 tpheur
->th_ecn_droprxmt
= 0;
1221 if (tpheur
->th_ecn_synrst
>= ECN_RETRY_LIMIT
) {
1222 tpheur
->th_ecn_synrst
= 0;
1225 /* Make sure it follows along */
1226 tpheur
->th_ecn_backoff
= tcp_now
;
1229 tcp_heuristic_unlock(head
);
1235 tcp_heuristic_do_ecn(struct tcpcb
*tp
)
1237 struct tcp_cache_key_src tcks
;
1239 tcp_cache_key_src_create(tp
, &tcks
);
1240 return tcp_heuristic_do_ecn_common(&tcks
);
1244 tcp_heuristic_do_ecn_with_address(struct ifnet
*ifp
,
1245 union sockaddr_in_4_6
*local_address
)
1247 struct tcp_cache_key_src tcks
;
1249 memset(&tcks
, 0, sizeof(tcks
));
1252 calculate_tcp_clock();
1254 if (local_address
->sa
.sa_family
== AF_INET6
) {
1255 memcpy(&tcks
.laddr
.addr6
, &local_address
->sin6
.sin6_addr
, sizeof(struct in6_addr
));
1257 } else if (local_address
->sa
.sa_family
== AF_INET
) {
1258 memcpy(&tcks
.laddr
.addr
, &local_address
->sin
.sin_addr
, sizeof(struct in_addr
));
1262 return tcp_heuristic_do_ecn_common(&tcks
);
1266 tcp_heuristics_ecn_update(struct necp_tcp_ecn_cache
*necp_buffer
,
1267 struct ifnet
*ifp
, union sockaddr_in_4_6
*local_address
)
1269 struct tcp_cache_key_src tcks
;
1271 memset(&tcks
, 0, sizeof(tcks
));
1274 calculate_tcp_clock();
1276 if (local_address
->sa
.sa_family
== AF_INET6
) {
1277 memcpy(&tcks
.laddr
.addr6
, &local_address
->sin6
.sin6_addr
, sizeof(struct in6_addr
));
1279 } else if (local_address
->sa
.sa_family
== AF_INET
) {
1280 memcpy(&tcks
.laddr
.addr
, &local_address
->sin
.sin_addr
, sizeof(struct in_addr
));
1284 if (necp_buffer
->necp_tcp_ecn_heuristics_success
) {
1285 tcp_heuristic_reset_counters(&tcks
, TCPCACHE_F_ECN
);
1286 } else if (necp_buffer
->necp_tcp_ecn_heuristics_loss
) {
1287 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN
);
1288 } else if (necp_buffer
->necp_tcp_ecn_heuristics_drop_rst
) {
1289 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN_DROPRST
);
1290 } else if (necp_buffer
->necp_tcp_ecn_heuristics_drop_rxmt
) {
1291 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN_DROPRXMT
);
1292 } else if (necp_buffer
->necp_tcp_ecn_heuristics_syn_rst
) {
1293 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_ECN_SYNRST
);
1294 } else if (necp_buffer
->necp_tcp_ecn_heuristics_aggressive
) {
1295 tcp_heuristic_ecn_aggressive_common(&tcks
);
1302 tcp_heuristic_do_tfo_with_address(struct ifnet
*ifp
,
1303 union sockaddr_in_4_6
*local_address
, union sockaddr_in_4_6
*remote_address
,
1304 u_int8_t
*cookie
, u_int8_t
*cookie_len
)
1306 struct tcp_cache_key_src tcks
;
1308 memset(&tcks
, 0, sizeof(tcks
));
1311 calculate_tcp_clock();
1313 if (remote_address
->sa
.sa_family
== AF_INET6
) {
1314 memcpy(&tcks
.laddr
.addr6
, &local_address
->sin6
.sin6_addr
, sizeof(struct in6_addr
));
1315 memcpy(&tcks
.faddr
.addr6
, &remote_address
->sin6
.sin6_addr
, sizeof(struct in6_addr
));
1317 } else if (remote_address
->sa
.sa_family
== AF_INET
) {
1318 memcpy(&tcks
.laddr
.addr
, &local_address
->sin
.sin_addr
, sizeof(struct in_addr
));
1319 memcpy(&tcks
.faddr
.addr
, &remote_address
->sin
.sin_addr
, sizeof(struct in_addr
));
1323 if (tcp_heuristic_do_tfo_common(&tcks
)) {
1324 if (!tcp_cache_get_cookie_common(&tcks
, cookie
, cookie_len
)) {
1334 tcp_heuristics_tfo_update(struct necp_tcp_tfo_cache
*necp_buffer
,
1335 struct ifnet
*ifp
, union sockaddr_in_4_6
*local_address
,
1336 union sockaddr_in_4_6
*remote_address
)
1338 struct tcp_cache_key_src tcks
;
1340 memset(&tcks
, 0, sizeof(tcks
));
1343 calculate_tcp_clock();
1345 if (remote_address
->sa
.sa_family
== AF_INET6
) {
1346 memcpy(&tcks
.laddr
.addr6
, &local_address
->sin6
.sin6_addr
, sizeof(struct in6_addr
));
1347 memcpy(&tcks
.faddr
.addr6
, &remote_address
->sin6
.sin6_addr
, sizeof(struct in6_addr
));
1349 } else if (remote_address
->sa
.sa_family
== AF_INET
) {
1350 memcpy(&tcks
.laddr
.addr
, &local_address
->sin
.sin_addr
, sizeof(struct in_addr
));
1351 memcpy(&tcks
.faddr
.addr
, &remote_address
->sin
.sin_addr
, sizeof(struct in_addr
));
1355 if (necp_buffer
->necp_tcp_tfo_heuristics_success
) {
1356 tcp_heuristic_reset_counters(&tcks
, TCPCACHE_F_TFO_REQ
| TCPCACHE_F_TFO_DATA
|
1357 TCPCACHE_F_TFO_REQ_RST
| TCPCACHE_F_TFO_DATA_RST
);
1360 if (necp_buffer
->necp_tcp_tfo_heuristics_success_req
) {
1361 tcp_heuristic_reset_counters(&tcks
, TCPCACHE_F_TFO_REQ
| TCPCACHE_F_TFO_REQ_RST
);
1364 if (necp_buffer
->necp_tcp_tfo_heuristics_loss
) {
1365 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_TFO_REQ
| TCPCACHE_F_TFO_DATA
);
1368 if (necp_buffer
->necp_tcp_tfo_heuristics_loss_req
) {
1369 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_TFO_REQ
);
1372 if (necp_buffer
->necp_tcp_tfo_heuristics_rst_data
) {
1373 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_TFO_REQ_RST
| TCPCACHE_F_TFO_DATA_RST
);
1376 if (necp_buffer
->necp_tcp_tfo_heuristics_rst_req
) {
1377 tcp_heuristic_inc_counters(&tcks
, TCPCACHE_F_TFO_REQ_RST
);
1380 if (necp_buffer
->necp_tcp_tfo_heuristics_middlebox
) {
1381 tcp_heuristic_tfo_middlebox_common(&tcks
);
1384 if (necp_buffer
->necp_tcp_tfo_cookie_len
!= 0) {
1385 tcp_cache_set_cookie_common(&tcks
,
1386 necp_buffer
->necp_tcp_tfo_cookie
, necp_buffer
->necp_tcp_tfo_cookie_len
);
1393 sysctl_cleartfocache(void)
1397 for (i
= 0; i
< tcp_cache_size
; i
++) {
1398 struct tcp_cache_head
*head
= &tcp_cache
[i
];
1399 struct tcp_cache
*tpcache
, *tmp
;
1400 struct tcp_heuristics_head
*hhead
= &tcp_heuristics
[i
];
1401 struct tcp_heuristic
*tpheur
, *htmp
;
1403 lck_mtx_lock(&head
->tch_mtx
);
1404 SLIST_FOREACH_SAFE(tpcache
, &head
->tcp_caches
, list
, tmp
) {
1405 SLIST_REMOVE(&head
->tcp_caches
, tpcache
, tcp_cache
, list
);
1406 _FREE(tpcache
, M_TEMP
);
1408 lck_mtx_unlock(&head
->tch_mtx
);
1410 lck_mtx_lock(&hhead
->thh_mtx
);
1411 SLIST_FOREACH_SAFE(tpheur
, &hhead
->tcp_heuristics
, list
, htmp
) {
1412 SLIST_REMOVE(&hhead
->tcp_heuristics
, tpheur
, tcp_heuristic
, list
);
1413 _FREE(tpheur
, M_TEMP
);
1415 lck_mtx_unlock(&hhead
->thh_mtx
);
1419 /* This sysctl is useful for testing purposes only */
1420 static int tcpcleartfo
= 0;
1422 static int sysctl_cleartfo SYSCTL_HANDLER_ARGS
1424 #pragma unused(arg1, arg2)
1425 int error
= 0, val
, oldval
= tcpcleartfo
;
1428 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
1429 if (error
|| !req
->newptr
) {
1434 * The actual value does not matter. If the value is set, it triggers
1435 * the clearing of the TFO cache. If a future implementation does not
1436 * use the route entry to hold the TFO cache, replace the route sysctl.
1439 if (val
!= oldval
) {
1440 sysctl_cleartfocache();
1448 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, clear_tfocache
, CTLTYPE_INT
| CTLFLAG_RW
|
1449 CTLFLAG_LOCKED
, &tcpcleartfo
, 0, &sysctl_cleartfo
, "I",
1450 "Toggle to clear the TFO destination based heuristic cache");
1453 tcp_cache_init(void)
1455 uint64_t sane_size_meg
= sane_size
/ 1024 / 1024;
1459 * On machines with <100MB of memory this will result in a (full) cache-size
1460 * of 32 entries, thus 32 * 5 * 64bytes = 10KB. (about 0.01 %)
1461 * On machines with > 4GB of memory, we have a cache-size of 1024 entries,
1464 * Side-note: we convert to u_int32_t. If sane_size is more than
1465 * 16000 TB, we loose precision. But, who cares? :)
1467 tcp_cache_size
= tcp_cache_roundup2((u_int32_t
)(sane_size_meg
>> 2));
1468 if (tcp_cache_size
< 32) {
1469 tcp_cache_size
= 32;
1470 } else if (tcp_cache_size
> 1024) {
1471 tcp_cache_size
= 1024;
1474 tcp_cache
= _MALLOC(sizeof(struct tcp_cache_head
) * tcp_cache_size
,
1476 if (tcp_cache
== NULL
) {
1477 panic("Allocating tcp_cache failed at boot-time!");
1480 tcp_cache_mtx_grp_attr
= lck_grp_attr_alloc_init();
1481 tcp_cache_mtx_grp
= lck_grp_alloc_init("tcpcache", tcp_cache_mtx_grp_attr
);
1482 tcp_cache_mtx_attr
= lck_attr_alloc_init();
1484 tcp_heuristics
= _MALLOC(sizeof(struct tcp_heuristics_head
) * tcp_cache_size
,
1486 if (tcp_heuristics
== NULL
) {
1487 panic("Allocating tcp_heuristic failed at boot-time!");
1490 tcp_heuristic_mtx_grp_attr
= lck_grp_attr_alloc_init();
1491 tcp_heuristic_mtx_grp
= lck_grp_alloc_init("tcpheuristic", tcp_heuristic_mtx_grp_attr
);
1492 tcp_heuristic_mtx_attr
= lck_attr_alloc_init();
1494 for (i
= 0; i
< tcp_cache_size
; i
++) {
1495 lck_mtx_init(&tcp_cache
[i
].tch_mtx
, tcp_cache_mtx_grp
,
1496 tcp_cache_mtx_attr
);
1497 SLIST_INIT(&tcp_cache
[i
].tcp_caches
);
1499 lck_mtx_init(&tcp_heuristics
[i
].thh_mtx
, tcp_heuristic_mtx_grp
,
1500 tcp_heuristic_mtx_attr
);
1501 SLIST_INIT(&tcp_heuristics
[i
].tcp_heuristics
);
1504 tcp_cache_hash_seed
= RandomULong();