xref: /xnu-8020.101.4/bsd/netinet/tcp_cache.c (revision e7776783b89a353188416a9a346c6cdb4928faad)
1 /*
2  * Copyright (c) 2015-2021 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 /* TCP-cache to store and retrieve TCP-related information */
30 
31 #include <net/flowhash.h>
32 #include <net/route.h>
33 #include <net/necp.h>
34 #include <netinet/in_pcb.h>
35 #include <netinet/mptcp.h>
36 #include <netinet/mptcp_var.h>
37 #include <netinet/tcp_cache.h>
38 #include <netinet/tcp_seq.h>
39 #include <netinet/tcp_var.h>
40 #include <kern/locks.h>
41 #include <sys/queue.h>
42 #include <dev/random/randomdev.h>
43 
44 typedef union {
45 	struct in_addr addr;
46 	struct in6_addr addr6;
47 } in_4_6_addr;
48 
49 struct tcp_heuristic_key {
50 	union {
51 		uint8_t thk_net_signature[IFNET_SIGNATURELEN];
52 		in_4_6_addr thk_ip;
53 	};
54 	sa_family_t     thk_family;
55 };
56 
57 struct tcp_heuristic {
58 	SLIST_ENTRY(tcp_heuristic) list;
59 
60 	uint32_t        th_last_access;
61 
62 	struct tcp_heuristic_key        th_key;
63 
64 	char            th_val_start[0]; /* Marker for memsetting to 0 */
65 
66 	uint8_t         th_tfo_data_loss; /* The number of times a SYN+data has been lost */
67 	uint8_t         th_tfo_req_loss; /* The number of times a SYN+cookie-req has been lost */
68 	uint8_t         th_tfo_data_rst; /* The number of times a SYN+data has received a RST */
69 	uint8_t         th_tfo_req_rst; /* The number of times a SYN+cookie-req has received a RST */
70 	uint8_t         th_mptcp_loss; /* The number of times a SYN+MP_CAPABLE has been lost */
71 	uint8_t         th_mptcp_success; /* The number of times MPTCP-negotiation has been successful */
72 	uint8_t         th_ecn_loss; /* The number of times a SYN+ecn has been lost */
73 	uint8_t         th_ecn_aggressive; /* The number of times we did an aggressive fallback */
74 	uint8_t         th_ecn_droprst; /* The number of times ECN connections received a RST after first data pkt */
75 	uint8_t         th_ecn_droprxmt; /* The number of times ECN connection is dropped after multiple retransmits */
76 	uint8_t         th_ecn_synrst;  /* number of times RST was received in response to an ECN enabled SYN */
77 	uint32_t        th_tfo_enabled_time; /* The moment when we reenabled TFO after backing off */
78 	uint32_t        th_tfo_backoff_until; /* Time until when we should not try out TFO */
79 	uint32_t        th_tfo_backoff; /* Current backoff timer */
80 	uint32_t        th_mptcp_backoff; /* Time until when we should not try out MPTCP */
81 	uint32_t        th_ecn_backoff; /* Time until when we should not try out ECN */
82 
83 	uint8_t         th_tfo_in_backoff:1, /* Are we avoiding TFO due to the backoff timer? */
84 	    th_mptcp_in_backoff:1,             /* Are we avoiding MPTCP due to the backoff timer? */
85 	    th_mptcp_heuristic_disabled:1;             /* Are heuristics disabled? */
86 
87 	char            th_val_end[0]; /* Marker for memsetting to 0 */
88 };
89 
90 struct tcp_heuristics_head {
91 	SLIST_HEAD(tcp_heur_bucket, tcp_heuristic) tcp_heuristics;
92 
93 	/* Per-hashbucket lock to avoid lock-contention */
94 	lck_mtx_t       thh_mtx;
95 };
96 
97 struct tcp_cache_key {
98 	sa_family_t     tck_family;
99 
100 	struct tcp_heuristic_key tck_src;
101 	in_4_6_addr tck_dst;
102 };
103 
104 #define MPTCP_VERSION_SUPPORTED 1
105 #define MPTCP_VERSION_UNSUPPORTED -1
106 #define MPTCP_VERSION_SUPPORTED_UNKNOWN 0
107 struct tcp_cache {
108 	SLIST_ENTRY(tcp_cache) list;
109 
110 	uint32_t       tc_last_access;
111 	uint32_t       tc_mptcp_version_discovery_refresh; /* Time until when we should do version discovery again */
112 
113 	struct tcp_cache_key tc_key;
114 
115 	uint8_t        tc_tfo_cookie[TFO_COOKIE_LEN_MAX];
116 	uint8_t        tc_tfo_cookie_len;
117 
118 	int8_t         tc_mptcp_v0_support; /* -1 unsupported, 0 unknown, 1 supported */
119 	int8_t         tc_mptcp_v1_support; /* -1 unsupported, 0 unknown, 1 supported */
120 	uint8_t        tc_mptcp_v0_failed_count;
121 	uint8_t        tc_mptcp_v1_failed_count;
122 };
123 
124 struct tcp_cache_head {
125 	SLIST_HEAD(tcp_cache_bucket, tcp_cache) tcp_caches;
126 
127 	/* Per-hashbucket lock to avoid lock-contention */
128 	lck_mtx_t       tch_mtx;
129 };
130 
131 struct tcp_cache_key_src {
132 	struct ifnet *ifp;
133 	in_4_6_addr laddr;
134 	in_4_6_addr faddr;
135 	int af;
136 };
137 
138 static uint32_t tcp_cache_hash_seed;
139 
140 size_t tcp_cache_size;
141 
142 /*
143  * The maximum depth of the hash-bucket. This way we limit the tcp_cache to
144  * TCP_CACHE_BUCKET_SIZE * tcp_cache_size and have "natural" garbage collection
145  */
146 #define TCP_CACHE_BUCKET_SIZE 5
147 
148 static struct tcp_cache_head *tcp_cache;
149 
150 static LCK_ATTR_DECLARE(tcp_cache_mtx_attr, 0, 0);
151 static LCK_GRP_DECLARE(tcp_cache_mtx_grp, "tcpcache");
152 
153 static struct tcp_heuristics_head *tcp_heuristics;
154 
155 static LCK_ATTR_DECLARE(tcp_heuristic_mtx_attr, 0, 0);
156 static LCK_GRP_DECLARE(tcp_heuristic_mtx_grp, "tcpheuristic");
157 
158 static uint32_t tcp_backoff_maximum = 65536;
159 
160 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, backoff_maximum, CTLFLAG_RW | CTLFLAG_LOCKED,
161     &tcp_backoff_maximum, 0, "Maximum time for which we won't try TFO");
162 
163 static uint32_t tcp_ecn_timeout = 60;
164 
165 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, ecn_timeout, CTLFLAG_RW | CTLFLAG_LOCKED,
166     &tcp_ecn_timeout, 60, "Initial minutes to wait before re-trying ECN");
167 
168 static int disable_tcp_heuristics = 0;
169 SYSCTL_INT(_net_inet_tcp, OID_AUTO, disable_tcp_heuristics, CTLFLAG_RW | CTLFLAG_LOCKED,
170     &disable_tcp_heuristics, 0, "Set to 1, to disable all TCP heuristics (TFO, ECN, MPTCP)");
171 
172 
173 static uint32_t
tcp_min_to_hz(uint32_t minutes)174 tcp_min_to_hz(uint32_t minutes)
175 {
176 	if (minutes > 65536) {
177 		return (uint32_t)65536 * 60 * TCP_RETRANSHZ;
178 	}
179 
180 	return minutes * 60 * TCP_RETRANSHZ;
181 }
182 
183 /*
184  * This number is coupled with tcp_ecn_timeout, because we want to prevent
185  * integer overflow. Need to find an unexpensive way to prevent integer overflow
186  * while still allowing a dynamic sysctl.
187  */
188 #define TCP_CACHE_OVERFLOW_PROTECT      9
189 
190 /* Number of SYN-losses we accept */
191 #define TFO_MAX_COOKIE_LOSS     2
192 #define ECN_MAX_SYN_LOSS        2
193 #define MPTCP_MAX_SYN_LOSS      2
194 #define MPTCP_SUCCESS_TRIGGER   10
195 #define MPTCP_VERSION_MAX_FAIL  2
196 #define ECN_MAX_DROPRST         1
197 #define ECN_MAX_DROPRXMT        4
198 #define ECN_MAX_SYNRST          4
199 
200 /* Flags for setting/unsetting loss-heuristics, limited to 4 bytes */
201 #define TCPCACHE_F_TFO_REQ      0x01
202 #define TCPCACHE_F_TFO_DATA     0x02
203 #define TCPCACHE_F_ECN          0x04
204 #define TCPCACHE_F_MPTCP        0x08
205 #define TCPCACHE_F_ECN_DROPRST  0x10
206 #define TCPCACHE_F_ECN_DROPRXMT 0x20
207 #define TCPCACHE_F_TFO_REQ_RST  0x40
208 #define TCPCACHE_F_TFO_DATA_RST 0x80
209 #define TCPCACHE_F_ECN_SYNRST   0x100
210 
211 /* Always retry ECN after backing off to this level for some heuristics */
212 #define ECN_RETRY_LIMIT 9
213 
214 #define TCP_CACHE_INC_IFNET_STAT(_ifp_, _af_, _stat_) { \
215 	if ((_ifp_) != NULL) { \
216 	        if ((_af_) == AF_INET6) { \
217 	                (_ifp_)->if_ipv6_stat->_stat_++;\
218 	        } else { \
219 	                (_ifp_)->if_ipv4_stat->_stat_++;\
220 	        }\
221 	}\
222 }
223 
224 /*
225  * Round up to next higher power-of 2.  See "Bit Twiddling Hacks".
226  *
227  * Might be worth moving this to a library so that others
228  * (e.g., scale_to_powerof2()) can use this as well instead of a while-loop.
229  */
230 static uint32_t
tcp_cache_roundup2(uint32_t a)231 tcp_cache_roundup2(uint32_t a)
232 {
233 	a--;
234 	a |= a >> 1;
235 	a |= a >> 2;
236 	a |= a >> 4;
237 	a |= a >> 8;
238 	a |= a >> 16;
239 	a++;
240 
241 	return a;
242 }
243 
244 static void
tcp_cache_hash_src(struct tcp_cache_key_src * tcks,struct tcp_heuristic_key * key)245 tcp_cache_hash_src(struct tcp_cache_key_src *tcks, struct tcp_heuristic_key *key)
246 {
247 	struct ifnet *ifp = tcks->ifp;
248 	uint8_t len = sizeof(key->thk_net_signature);
249 	uint16_t flags;
250 
251 	if (tcks->af == AF_INET6) {
252 		int ret;
253 
254 		key->thk_family = AF_INET6;
255 		ret = ifnet_get_netsignature(ifp, AF_INET6, &len, &flags,
256 		    key->thk_net_signature);
257 
258 		/*
259 		 * ifnet_get_netsignature only returns EINVAL if ifn is NULL
260 		 * (we made sure that in the other cases it does not). So,
261 		 * in this case we should take the connection's address.
262 		 */
263 		if (ret == ENOENT || ret == EINVAL) {
264 			memcpy(&key->thk_ip.addr6, &tcks->laddr.addr6, sizeof(struct in6_addr));
265 		}
266 	} else {
267 		int ret;
268 
269 		key->thk_family = AF_INET;
270 		ret = ifnet_get_netsignature(ifp, AF_INET, &len, &flags,
271 		    key->thk_net_signature);
272 
273 		/*
274 		 * ifnet_get_netsignature only returns EINVAL if ifn is NULL
275 		 * (we made sure that in the other cases it does not). So,
276 		 * in this case we should take the connection's address.
277 		 */
278 		if (ret == ENOENT || ret == EINVAL) {
279 			memcpy(&key->thk_ip.addr, &tcks->laddr.addr, sizeof(struct in_addr));
280 		}
281 	}
282 }
283 
284 static uint16_t
tcp_cache_hash(struct tcp_cache_key_src * tcks,struct tcp_cache_key * key)285 tcp_cache_hash(struct tcp_cache_key_src *tcks, struct tcp_cache_key *key)
286 {
287 	uint32_t hash;
288 
289 	bzero(key, sizeof(struct tcp_cache_key));
290 
291 	tcp_cache_hash_src(tcks, &key->tck_src);
292 
293 	if (tcks->af == AF_INET6) {
294 		key->tck_family = AF_INET6;
295 		memcpy(&key->tck_dst.addr6, &tcks->faddr.addr6,
296 		    sizeof(struct in6_addr));
297 	} else {
298 		key->tck_family = AF_INET;
299 		memcpy(&key->tck_dst.addr, &tcks->faddr.addr,
300 		    sizeof(struct in_addr));
301 	}
302 
303 	hash = net_flowhash(key, sizeof(struct tcp_cache_key),
304 	    tcp_cache_hash_seed);
305 
306 	return (uint16_t)(hash & (tcp_cache_size - 1));
307 }
308 
309 static void
tcp_cache_unlock(struct tcp_cache_head * head)310 tcp_cache_unlock(struct tcp_cache_head *head)
311 {
312 	lck_mtx_unlock(&head->tch_mtx);
313 }
314 
315 /*
316  * Make sure that everything that happens after tcp_getcache_with_lock()
317  * is short enough to justify that you hold the per-bucket lock!!!
318  *
319  * Otherwise, better build another lookup-function that does not hold the
320  * lock and you copy out the bits and bytes.
321  *
322  * That's why we provide the head as a "return"-pointer so that the caller
323  * can give it back to use for tcp_cache_unlock().
324  */
325 static struct tcp_cache *
tcp_getcache_with_lock(struct tcp_cache_key_src * tcks,int create,struct tcp_cache_head ** headarg)326 tcp_getcache_with_lock(struct tcp_cache_key_src *tcks,
327     int create, struct tcp_cache_head **headarg)
328 {
329 	struct tcp_cache *tpcache = NULL;
330 	struct tcp_cache_head *head;
331 	struct tcp_cache_key key;
332 	uint16_t hash;
333 	int i = 0;
334 
335 	hash = tcp_cache_hash(tcks, &key);
336 	head = &tcp_cache[hash];
337 
338 	lck_mtx_lock(&head->tch_mtx);
339 
340 	/*** First step: Look for the tcp_cache in our bucket ***/
341 	SLIST_FOREACH(tpcache, &head->tcp_caches, list) {
342 		if (memcmp(&tpcache->tc_key, &key, sizeof(key)) == 0) {
343 			break;
344 		}
345 
346 		i++;
347 	}
348 
349 	/*** Second step: If it's not there, create/recycle it ***/
350 	if ((tpcache == NULL) && create) {
351 		if (i >= TCP_CACHE_BUCKET_SIZE) {
352 			struct tcp_cache *oldest_cache = NULL;
353 			uint32_t max_age = 0;
354 
355 			/* Look for the oldest tcp_cache in the bucket */
356 			SLIST_FOREACH(tpcache, &head->tcp_caches, list) {
357 				uint32_t age = tcp_now - tpcache->tc_last_access;
358 				if (age > max_age) {
359 					max_age = age;
360 					oldest_cache = tpcache;
361 				}
362 			}
363 			VERIFY(oldest_cache != NULL);
364 
365 			tpcache = oldest_cache;
366 
367 			/* We recycle, thus let's indicate that there is no cookie */
368 			tpcache->tc_tfo_cookie_len = 0;
369 		} else {
370 			/* Create a new cache and add it to the list */
371 			tpcache = kalloc_type(struct tcp_cache, Z_NOWAIT | Z_ZERO);
372 			if (tpcache == NULL) {
373 				os_log_error(OS_LOG_DEFAULT, "%s could not allocate cache", __func__);
374 				goto out_null;
375 			}
376 
377 			SLIST_INSERT_HEAD(&head->tcp_caches, tpcache, list);
378 		}
379 
380 		memcpy(&tpcache->tc_key, &key, sizeof(key));
381 	}
382 
383 	if (tpcache == NULL) {
384 		goto out_null;
385 	}
386 
387 	/* Update timestamp for garbage collection purposes */
388 	tpcache->tc_last_access = tcp_now;
389 	*headarg = head;
390 
391 	return tpcache;
392 
393 out_null:
394 	tcp_cache_unlock(head);
395 	return NULL;
396 }
397 
398 static void
tcp_cache_key_src_create(struct tcpcb * tp,struct tcp_cache_key_src * tcks)399 tcp_cache_key_src_create(struct tcpcb *tp, struct tcp_cache_key_src *tcks)
400 {
401 	struct inpcb *inp = tp->t_inpcb;
402 	memset(tcks, 0, sizeof(*tcks));
403 
404 	tcks->ifp = inp->inp_last_outifp;
405 
406 	if (inp->inp_vflag & INP_IPV6) {
407 		memcpy(&tcks->laddr.addr6, &inp->in6p_laddr, sizeof(struct in6_addr));
408 		memcpy(&tcks->faddr.addr6, &inp->in6p_faddr, sizeof(struct in6_addr));
409 		tcks->af = AF_INET6;
410 	} else {
411 		memcpy(&tcks->laddr.addr, &inp->inp_laddr, sizeof(struct in_addr));
412 		memcpy(&tcks->faddr.addr, &inp->inp_faddr, sizeof(struct in_addr));
413 		tcks->af = AF_INET;
414 	}
415 
416 	return;
417 }
418 
419 static void
mptcp_version_cache_key_src_init(struct sockaddr * dst,struct tcp_cache_key_src * tcks)420 mptcp_version_cache_key_src_init(struct sockaddr *dst, struct tcp_cache_key_src *tcks)
421 {
422 	memset(tcks, 0, sizeof(*tcks));
423 
424 	if (dst->sa_family == AF_INET) {
425 		memcpy(&tcks->faddr.addr, &SIN(dst)->sin_addr, sizeof(struct in_addr));
426 		tcks->af = AF_INET;
427 	} else {
428 		memcpy(&tcks->faddr.addr6, &SIN6(dst)->sin6_addr, sizeof(struct in6_addr));
429 		tcks->af = AF_INET6;
430 	}
431 
432 	return;
433 }
434 
435 static void
tcp_cache_set_cookie_common(struct tcp_cache_key_src * tcks,u_char * cookie,uint8_t len)436 tcp_cache_set_cookie_common(struct tcp_cache_key_src *tcks, u_char *cookie, uint8_t len)
437 {
438 	struct tcp_cache_head *head;
439 	struct tcp_cache *tpcache;
440 
441 	/* Call lookup/create function */
442 	tpcache = tcp_getcache_with_lock(tcks, 1, &head);
443 	if (tpcache == NULL) {
444 		return;
445 	}
446 
447 	tpcache->tc_tfo_cookie_len = len > TFO_COOKIE_LEN_MAX ?
448 	    TFO_COOKIE_LEN_MAX : len;
449 	memcpy(tpcache->tc_tfo_cookie, cookie, tpcache->tc_tfo_cookie_len);
450 
451 	tcp_cache_unlock(head);
452 }
453 
454 void
tcp_cache_set_cookie(struct tcpcb * tp,u_char * cookie,uint8_t len)455 tcp_cache_set_cookie(struct tcpcb *tp, u_char *cookie, uint8_t len)
456 {
457 	struct tcp_cache_key_src tcks;
458 
459 	tcp_cache_key_src_create(tp, &tcks);
460 	tcp_cache_set_cookie_common(&tcks, cookie, len);
461 }
462 
463 static int
tcp_cache_get_cookie_common(struct tcp_cache_key_src * tcks,u_char * cookie,uint8_t * len)464 tcp_cache_get_cookie_common(struct tcp_cache_key_src *tcks, u_char *cookie, uint8_t *len)
465 {
466 	struct tcp_cache_head *head;
467 	struct tcp_cache *tpcache;
468 
469 	/* Call lookup/create function */
470 	tpcache = tcp_getcache_with_lock(tcks, 1, &head);
471 	if (tpcache == NULL) {
472 		return 0;
473 	}
474 
475 	if (tpcache->tc_tfo_cookie_len == 0) {
476 		tcp_cache_unlock(head);
477 		return 0;
478 	}
479 
480 	/*
481 	 * Not enough space - this should never happen as it has been checked
482 	 * in tcp_tfo_check. So, fail here!
483 	 */
484 	VERIFY(tpcache->tc_tfo_cookie_len <= *len);
485 
486 	memcpy(cookie, tpcache->tc_tfo_cookie, tpcache->tc_tfo_cookie_len);
487 	*len = tpcache->tc_tfo_cookie_len;
488 
489 	tcp_cache_unlock(head);
490 
491 	return 1;
492 }
493 
494 /*
495  * Get the cookie related to 'tp', and copy it into 'cookie', provided that len
496  * is big enough (len designates the available memory.
497  * Upon return, 'len' is set to the cookie's length.
498  *
499  * Returns 0 if we should request a cookie.
500  * Returns 1 if the cookie has been found and written.
501  */
502 int
tcp_cache_get_cookie(struct tcpcb * tp,u_char * cookie,uint8_t * len)503 tcp_cache_get_cookie(struct tcpcb *tp, u_char *cookie, uint8_t *len)
504 {
505 	struct tcp_cache_key_src tcks;
506 
507 	tcp_cache_key_src_create(tp, &tcks);
508 	return tcp_cache_get_cookie_common(&tcks, cookie, len);
509 }
510 
511 static unsigned int
tcp_cache_get_cookie_len_common(struct tcp_cache_key_src * tcks)512 tcp_cache_get_cookie_len_common(struct tcp_cache_key_src *tcks)
513 {
514 	struct tcp_cache_head *head;
515 	struct tcp_cache *tpcache;
516 	unsigned int cookie_len;
517 
518 	/* Call lookup/create function */
519 	tpcache = tcp_getcache_with_lock(tcks, 1, &head);
520 	if (tpcache == NULL) {
521 		return 0;
522 	}
523 
524 	cookie_len = tpcache->tc_tfo_cookie_len;
525 
526 	tcp_cache_unlock(head);
527 
528 	return cookie_len;
529 }
530 
531 unsigned int
tcp_cache_get_cookie_len(struct tcpcb * tp)532 tcp_cache_get_cookie_len(struct tcpcb *tp)
533 {
534 	struct tcp_cache_key_src tcks;
535 
536 	tcp_cache_key_src_create(tp, &tcks);
537 	return tcp_cache_get_cookie_len_common(&tcks);
538 }
539 
540 /*
541  * @return:
542  *         0	MPTCP_VERSION_0
543  *         1	MPTCP_VERSION_1
544  */
545 uint8_t
tcp_cache_get_mptcp_version(struct sockaddr * dst)546 tcp_cache_get_mptcp_version(struct sockaddr *dst)
547 {
548 	struct tcp_cache_key_src tcks;
549 	mptcp_version_cache_key_src_init(dst, &tcks);
550 	uint8_t version = (uint8_t) mptcp_preferred_version;
551 
552 	struct tcp_cache_head *head;
553 	struct tcp_cache *tpcache;
554 
555 	/* Call lookup/create function */
556 	tpcache = tcp_getcache_with_lock(&tcks, 1, &head);
557 	if (tpcache == NULL) {
558 		return version;
559 	}
560 
561 	if (tpcache->tc_mptcp_v1_support == MPTCP_VERSION_UNSUPPORTED &&
562 	    tpcache->tc_mptcp_v0_support != MPTCP_VERSION_UNSUPPORTED) {
563 		version =  MPTCP_VERSION_0;
564 	}
565 	if (tpcache->tc_mptcp_v0_support == MPTCP_VERSION_UNSUPPORTED &&
566 	    tpcache->tc_mptcp_v1_support != MPTCP_VERSION_UNSUPPORTED) {
567 		version =  MPTCP_VERSION_1;
568 	}
569 
570 	tcp_cache_unlock(head);
571 	return version;
572 }
573 
574 void
tcp_cache_update_mptcp_version(struct tcpcb * tp,boolean_t succeeded)575 tcp_cache_update_mptcp_version(struct tcpcb *tp, boolean_t succeeded)
576 {
577 	struct mptcb *mp_tp = tptomptp(tp);
578 	uint8_t version = mp_tp->mpt_version;
579 	int8_t record = succeeded ? MPTCP_VERSION_SUPPORTED : MPTCP_VERSION_UNSUPPORTED;
580 
581 	struct tcp_cache_key_src tcks;
582 
583 	struct inpcb *inp = tp->t_inpcb;
584 	if (inp->inp_vflag & INP_IPV6) {
585 		struct sockaddr_in6 dst = {
586 			.sin6_len = sizeof(struct sockaddr_in6),
587 			.sin6_family = AF_INET6,
588 			.sin6_addr = inp->in6p_faddr,
589 		};
590 		mptcp_version_cache_key_src_init((struct sockaddr *)&dst, &tcks);
591 	} else {
592 		struct sockaddr_in dst = {
593 			.sin_len = sizeof(struct sockaddr_in),
594 			.sin_family = AF_INET,
595 			.sin_addr = inp->inp_faddr,
596 		};
597 		mptcp_version_cache_key_src_init((struct sockaddr *)&dst, &tcks);
598 	}
599 
600 	struct tcp_cache_head *head;
601 	struct tcp_cache *tpcache;
602 	/* Call lookup/create function */
603 	tpcache = tcp_getcache_with_lock(&tcks, 1, &head);
604 	if (tpcache == NULL) {
605 		return;
606 	}
607 
608 	if (version == MPTCP_VERSION_0) {
609 		if (tpcache->tc_mptcp_v0_support == MPTCP_VERSION_SUPPORTED_UNKNOWN) {
610 			tpcache->tc_mptcp_v0_support = record;
611 		} else {
612 			if (succeeded) {
613 				tpcache->tc_mptcp_v0_failed_count = 0;
614 				tpcache->tc_mptcp_v0_support = MPTCP_VERSION_SUPPORTED;
615 			} else {
616 				tpcache->tc_mptcp_v0_failed_count += 1;
617 				// flip the record, so the other version is attempted the next time
618 				if (tpcache->tc_mptcp_v0_failed_count >= MPTCP_VERSION_MAX_FAIL) {
619 					tpcache->tc_mptcp_v0_failed_count = 0;
620 					tpcache->tc_mptcp_v1_failed_count = 0;
621 					tpcache->tc_mptcp_v0_support = MPTCP_VERSION_UNSUPPORTED;
622 					tpcache->tc_mptcp_v1_support = MPTCP_VERSION_SUPPORTED_UNKNOWN;
623 				}
624 			}
625 		}
626 	}
627 
628 	if (version == MPTCP_VERSION_1) {
629 		if (tpcache->tc_mptcp_v1_support == MPTCP_VERSION_SUPPORTED_UNKNOWN) {
630 			tpcache->tc_mptcp_v1_support = record;
631 		} else {
632 			if (succeeded) {
633 				tpcache->tc_mptcp_v1_failed_count = 0;
634 				tpcache->tc_mptcp_v1_support = MPTCP_VERSION_SUPPORTED;
635 			} else {
636 				tpcache->tc_mptcp_v1_failed_count += 1;
637 				// flip the record, so the other version is attempted the next time
638 				if (tpcache->tc_mptcp_v1_failed_count >= MPTCP_VERSION_MAX_FAIL) {
639 					tpcache->tc_mptcp_v0_failed_count = 0;
640 					tpcache->tc_mptcp_v1_failed_count = 0;
641 					tpcache->tc_mptcp_v1_support = MPTCP_VERSION_UNSUPPORTED;
642 					tpcache->tc_mptcp_v0_support = MPTCP_VERSION_SUPPORTED_UNKNOWN;
643 				}
644 			}
645 		}
646 	}
647 
648 	tcp_cache_unlock(head);
649 }
650 
651 static uint16_t
tcp_heuristics_hash(struct tcp_cache_key_src * tcks,struct tcp_heuristic_key * key)652 tcp_heuristics_hash(struct tcp_cache_key_src *tcks, struct tcp_heuristic_key *key)
653 {
654 	uint32_t hash;
655 
656 	bzero(key, sizeof(struct tcp_heuristic_key));
657 
658 	tcp_cache_hash_src(tcks, key);
659 
660 	hash = net_flowhash(key, sizeof(struct tcp_heuristic_key),
661 	    tcp_cache_hash_seed);
662 
663 	return (uint16_t)(hash & (tcp_cache_size - 1));
664 }
665 
666 static void
tcp_heuristic_unlock(struct tcp_heuristics_head * head)667 tcp_heuristic_unlock(struct tcp_heuristics_head *head)
668 {
669 	lck_mtx_unlock(&head->thh_mtx);
670 }
671 
672 /*
673  * Make sure that everything that happens after tcp_getheuristic_with_lock()
674  * is short enough to justify that you hold the per-bucket lock!!!
675  *
676  * Otherwise, better build another lookup-function that does not hold the
677  * lock and you copy out the bits and bytes.
678  *
679  * That's why we provide the head as a "return"-pointer so that the caller
680  * can give it back to use for tcp_heur_unlock().
681  *
682  *
683  * ToDo - way too much code-duplication. We should create an interface to handle
684  * bucketized hashtables with recycling of the oldest element.
685  */
686 static struct tcp_heuristic *
tcp_getheuristic_with_lock(struct tcp_cache_key_src * tcks,int create,struct tcp_heuristics_head ** headarg)687 tcp_getheuristic_with_lock(struct tcp_cache_key_src *tcks,
688     int create, struct tcp_heuristics_head **headarg)
689 {
690 	struct tcp_heuristic *tpheur = NULL;
691 	struct tcp_heuristics_head *head;
692 	struct tcp_heuristic_key key;
693 	uint16_t hash;
694 	int i = 0;
695 
696 	hash = tcp_heuristics_hash(tcks, &key);
697 	head = &tcp_heuristics[hash];
698 
699 	lck_mtx_lock(&head->thh_mtx);
700 
701 	/*** First step: Look for the tcp_heur in our bucket ***/
702 	SLIST_FOREACH(tpheur, &head->tcp_heuristics, list) {
703 		if (memcmp(&tpheur->th_key, &key, sizeof(key)) == 0) {
704 			break;
705 		}
706 
707 		i++;
708 	}
709 
710 	/*** Second step: If it's not there, create/recycle it ***/
711 	if ((tpheur == NULL) && create) {
712 		if (i >= TCP_CACHE_BUCKET_SIZE) {
713 			struct tcp_heuristic *oldest_heur = NULL;
714 			uint32_t max_age = 0;
715 
716 			/* Look for the oldest tcp_heur in the bucket */
717 			SLIST_FOREACH(tpheur, &head->tcp_heuristics, list) {
718 				uint32_t age = tcp_now - tpheur->th_last_access;
719 				if (age > max_age) {
720 					max_age = age;
721 					oldest_heur = tpheur;
722 				}
723 			}
724 			VERIFY(oldest_heur != NULL);
725 
726 			tpheur = oldest_heur;
727 
728 			/* We recycle - set everything to 0 */
729 			bzero(tpheur->th_val_start,
730 			    tpheur->th_val_end - tpheur->th_val_start);
731 		} else {
732 			/* Create a new heuristic and add it to the list */
733 			tpheur = kalloc_type(struct tcp_heuristic, Z_NOWAIT | Z_ZERO);
734 			if (tpheur == NULL) {
735 				os_log_error(OS_LOG_DEFAULT, "%s could not allocate cache", __func__);
736 				goto out_null;
737 			}
738 
739 			SLIST_INSERT_HEAD(&head->tcp_heuristics, tpheur, list);
740 		}
741 
742 		/*
743 		 * Set to tcp_now, to make sure it won't be > than tcp_now in the
744 		 * near future.
745 		 */
746 		tpheur->th_ecn_backoff = tcp_now;
747 		tpheur->th_tfo_backoff_until = tcp_now;
748 		tpheur->th_mptcp_backoff = tcp_now;
749 		tpheur->th_tfo_backoff = tcp_min_to_hz(tcp_ecn_timeout);
750 
751 		memcpy(&tpheur->th_key, &key, sizeof(key));
752 	}
753 
754 	if (tpheur == NULL) {
755 		goto out_null;
756 	}
757 
758 	/* Update timestamp for garbage collection purposes */
759 	tpheur->th_last_access = tcp_now;
760 	*headarg = head;
761 
762 	return tpheur;
763 
764 out_null:
765 	tcp_heuristic_unlock(head);
766 	return NULL;
767 }
768 
769 static void
tcp_heuristic_reset_counters(struct tcp_cache_key_src * tcks,uint8_t flags)770 tcp_heuristic_reset_counters(struct tcp_cache_key_src *tcks, uint8_t flags)
771 {
772 	struct tcp_heuristics_head *head;
773 	struct tcp_heuristic *tpheur;
774 
775 	/*
776 	 * Always create heuristics here because MPTCP needs to write success
777 	 * into it. Thus, we always end up creating them.
778 	 */
779 	tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
780 	if (tpheur == NULL) {
781 		return;
782 	}
783 
784 	if (flags & TCPCACHE_F_TFO_DATA) {
785 		if (tpheur->th_tfo_data_loss >= TFO_MAX_COOKIE_LOSS) {
786 			os_log(OS_LOG_DEFAULT, "%s: Resetting TFO-data loss to 0 from %u on heur %lx\n",
787 			    __func__, tpheur->th_tfo_data_loss, (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
788 		}
789 		tpheur->th_tfo_data_loss = 0;
790 	}
791 
792 	if (flags & TCPCACHE_F_TFO_REQ) {
793 		if (tpheur->th_tfo_req_loss >= TFO_MAX_COOKIE_LOSS) {
794 			os_log(OS_LOG_DEFAULT, "%s: Resetting TFO-req loss to 0 from %u on heur %lx\n",
795 			    __func__, tpheur->th_tfo_req_loss, (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
796 		}
797 		tpheur->th_tfo_req_loss = 0;
798 	}
799 
800 	if (flags & TCPCACHE_F_TFO_DATA_RST) {
801 		if (tpheur->th_tfo_data_rst >= TFO_MAX_COOKIE_LOSS) {
802 			os_log(OS_LOG_DEFAULT, "%s: Resetting TFO-data RST to 0 from %u on heur %lx\n",
803 			    __func__, tpheur->th_tfo_data_rst, (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
804 		}
805 		tpheur->th_tfo_data_rst = 0;
806 	}
807 
808 	if (flags & TCPCACHE_F_TFO_REQ_RST) {
809 		if (tpheur->th_tfo_req_rst >= TFO_MAX_COOKIE_LOSS) {
810 			os_log(OS_LOG_DEFAULT, "%s: Resetting TFO-req RST to 0 from %u on heur %lx\n",
811 			    __func__, tpheur->th_tfo_req_rst, (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
812 		}
813 		tpheur->th_tfo_req_rst = 0;
814 	}
815 
816 	if (flags & TCPCACHE_F_ECN) {
817 		if (tpheur->th_ecn_loss >= ECN_MAX_SYN_LOSS || tpheur->th_ecn_synrst >= ECN_MAX_SYNRST) {
818 			os_log(OS_LOG_DEFAULT, "%s: Resetting ECN-loss to 0 from %u and synrst from %u on heur %lx\n",
819 			    __func__, tpheur->th_ecn_loss, tpheur->th_ecn_synrst, (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
820 		}
821 		tpheur->th_ecn_loss = 0;
822 		tpheur->th_ecn_synrst = 0;
823 	}
824 
825 	if (flags & TCPCACHE_F_MPTCP) {
826 		tpheur->th_mptcp_loss = 0;
827 		if (tpheur->th_mptcp_success < MPTCP_SUCCESS_TRIGGER) {
828 			tpheur->th_mptcp_success++;
829 
830 			if (tpheur->th_mptcp_success == MPTCP_SUCCESS_TRIGGER) {
831 				os_log(mptcp_log_handle, "%s disabling heuristics for 12 hours", __func__);
832 				tpheur->th_mptcp_heuristic_disabled = 1;
833 				/* Disable heuristics for 12 hours */
834 				tpheur->th_mptcp_backoff = tcp_now + tcp_min_to_hz(tcp_ecn_timeout * 12);
835 			}
836 		}
837 	}
838 
839 	tcp_heuristic_unlock(head);
840 }
841 
842 void
tcp_heuristic_tfo_success(struct tcpcb * tp)843 tcp_heuristic_tfo_success(struct tcpcb *tp)
844 {
845 	struct tcp_cache_key_src tcks;
846 	uint8_t flag = 0;
847 
848 	tcp_cache_key_src_create(tp, &tcks);
849 
850 	if (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) {
851 		flag = (TCPCACHE_F_TFO_DATA | TCPCACHE_F_TFO_REQ |
852 		    TCPCACHE_F_TFO_DATA_RST | TCPCACHE_F_TFO_REQ_RST);
853 	}
854 	if (tp->t_tfo_stats & TFO_S_COOKIE_REQ) {
855 		flag = (TCPCACHE_F_TFO_REQ | TCPCACHE_F_TFO_REQ_RST);
856 	}
857 
858 	tcp_heuristic_reset_counters(&tcks, flag);
859 }
860 
861 void
tcp_heuristic_mptcp_success(struct tcpcb * tp)862 tcp_heuristic_mptcp_success(struct tcpcb *tp)
863 {
864 	struct tcp_cache_key_src tcks;
865 
866 	tcp_cache_key_src_create(tp, &tcks);
867 	tcp_heuristic_reset_counters(&tcks, TCPCACHE_F_MPTCP);
868 }
869 
870 void
tcp_heuristic_ecn_success(struct tcpcb * tp)871 tcp_heuristic_ecn_success(struct tcpcb *tp)
872 {
873 	struct tcp_cache_key_src tcks;
874 
875 	tcp_cache_key_src_create(tp, &tcks);
876 	tcp_heuristic_reset_counters(&tcks, TCPCACHE_F_ECN);
877 }
878 
879 static void
__tcp_heuristic_tfo_middlebox_common(struct tcp_heuristic * tpheur)880 __tcp_heuristic_tfo_middlebox_common(struct tcp_heuristic *tpheur)
881 {
882 	if (tpheur->th_tfo_in_backoff) {
883 		return;
884 	}
885 
886 	tpheur->th_tfo_in_backoff = 1;
887 
888 	if (tpheur->th_tfo_enabled_time) {
889 		uint32_t old_backoff = tpheur->th_tfo_backoff;
890 
891 		tpheur->th_tfo_backoff -= (tcp_now - tpheur->th_tfo_enabled_time);
892 		if (tpheur->th_tfo_backoff > old_backoff) {
893 			tpheur->th_tfo_backoff = tcp_min_to_hz(tcp_ecn_timeout);
894 		}
895 	}
896 
897 	tpheur->th_tfo_backoff_until = tcp_now + tpheur->th_tfo_backoff;
898 
899 	/* Then, increase the backoff time */
900 	tpheur->th_tfo_backoff *= 2;
901 
902 	if (tpheur->th_tfo_backoff > tcp_min_to_hz(tcp_backoff_maximum)) {
903 		tpheur->th_tfo_backoff = tcp_min_to_hz(tcp_ecn_timeout);
904 	}
905 
906 	os_log(OS_LOG_DEFAULT, "%s disable TFO until %u now %u on %lx\n", __func__,
907 	    tpheur->th_tfo_backoff_until, tcp_now, (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
908 }
909 
910 static void
tcp_heuristic_tfo_middlebox_common(struct tcp_cache_key_src * tcks)911 tcp_heuristic_tfo_middlebox_common(struct tcp_cache_key_src *tcks)
912 {
913 	struct tcp_heuristics_head *head;
914 	struct tcp_heuristic *tpheur;
915 
916 	tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
917 	if (tpheur == NULL) {
918 		return;
919 	}
920 
921 	__tcp_heuristic_tfo_middlebox_common(tpheur);
922 
923 	tcp_heuristic_unlock(head);
924 }
925 
926 static void
tcp_heuristic_inc_counters(struct tcp_cache_key_src * tcks,uint32_t flags)927 tcp_heuristic_inc_counters(struct tcp_cache_key_src *tcks,
928     uint32_t flags)
929 {
930 	struct tcp_heuristics_head *head;
931 	struct tcp_heuristic *tpheur;
932 
933 	tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
934 	if (tpheur == NULL) {
935 		return;
936 	}
937 
938 	/* Limit to prevent integer-overflow during exponential backoff */
939 	if ((flags & TCPCACHE_F_TFO_DATA) && tpheur->th_tfo_data_loss < TCP_CACHE_OVERFLOW_PROTECT) {
940 		tpheur->th_tfo_data_loss++;
941 
942 		if (tpheur->th_tfo_data_loss >= TFO_MAX_COOKIE_LOSS) {
943 			__tcp_heuristic_tfo_middlebox_common(tpheur);
944 		}
945 	}
946 
947 	if ((flags & TCPCACHE_F_TFO_REQ) && tpheur->th_tfo_req_loss < TCP_CACHE_OVERFLOW_PROTECT) {
948 		tpheur->th_tfo_req_loss++;
949 
950 		if (tpheur->th_tfo_req_loss >= TFO_MAX_COOKIE_LOSS) {
951 			__tcp_heuristic_tfo_middlebox_common(tpheur);
952 		}
953 	}
954 
955 	if ((flags & TCPCACHE_F_TFO_DATA_RST) && tpheur->th_tfo_data_rst < TCP_CACHE_OVERFLOW_PROTECT) {
956 		tpheur->th_tfo_data_rst++;
957 
958 		if (tpheur->th_tfo_data_rst >= TFO_MAX_COOKIE_LOSS) {
959 			__tcp_heuristic_tfo_middlebox_common(tpheur);
960 		}
961 	}
962 
963 	if ((flags & TCPCACHE_F_TFO_REQ_RST) && tpheur->th_tfo_req_rst < TCP_CACHE_OVERFLOW_PROTECT) {
964 		tpheur->th_tfo_req_rst++;
965 
966 		if (tpheur->th_tfo_req_rst >= TFO_MAX_COOKIE_LOSS) {
967 			__tcp_heuristic_tfo_middlebox_common(tpheur);
968 		}
969 	}
970 
971 	if ((flags & TCPCACHE_F_ECN) &&
972 	    tpheur->th_ecn_loss < TCP_CACHE_OVERFLOW_PROTECT &&
973 	    TSTMP_LEQ(tpheur->th_ecn_backoff, tcp_now)) {
974 		tpheur->th_ecn_loss++;
975 		if (tpheur->th_ecn_loss >= ECN_MAX_SYN_LOSS) {
976 			tcpstat.tcps_ecn_fallback_synloss++;
977 			TCP_CACHE_INC_IFNET_STAT(tcks->ifp, tcks->af, ecn_fallback_synloss);
978 			tpheur->th_ecn_backoff = tcp_now +
979 			    (tcp_min_to_hz(tcp_ecn_timeout) <<
980 			    (tpheur->th_ecn_loss - ECN_MAX_SYN_LOSS));
981 
982 			os_log(OS_LOG_DEFAULT, "%s disable ECN until %u now %u on %lx for SYN-loss\n",
983 			    __func__, tpheur->th_ecn_backoff, tcp_now,
984 			    (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
985 		}
986 	}
987 
988 	if ((flags & TCPCACHE_F_MPTCP) &&
989 	    tpheur->th_mptcp_loss < TCP_CACHE_OVERFLOW_PROTECT &&
990 	    tpheur->th_mptcp_heuristic_disabled == 0) {
991 		tpheur->th_mptcp_loss++;
992 		if (tpheur->th_mptcp_loss >= MPTCP_MAX_SYN_LOSS) {
993 			/*
994 			 * Yes, we take tcp_ecn_timeout, to avoid adding yet
995 			 * another sysctl that is just used for testing.
996 			 */
997 			tpheur->th_mptcp_backoff = tcp_now +
998 			    (tcp_min_to_hz(tcp_ecn_timeout) <<
999 			    (tpheur->th_mptcp_loss - MPTCP_MAX_SYN_LOSS));
1000 			tpheur->th_mptcp_in_backoff = 1;
1001 
1002 			os_log(OS_LOG_DEFAULT, "%s disable MPTCP until %u now %u on %lx\n",
1003 			    __func__, tpheur->th_mptcp_backoff, tcp_now,
1004 			    (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
1005 		}
1006 	}
1007 
1008 	if ((flags & TCPCACHE_F_ECN_DROPRST) &&
1009 	    tpheur->th_ecn_droprst < TCP_CACHE_OVERFLOW_PROTECT &&
1010 	    TSTMP_LEQ(tpheur->th_ecn_backoff, tcp_now)) {
1011 		tpheur->th_ecn_droprst++;
1012 		if (tpheur->th_ecn_droprst >= ECN_MAX_DROPRST) {
1013 			tcpstat.tcps_ecn_fallback_droprst++;
1014 			TCP_CACHE_INC_IFNET_STAT(tcks->ifp, tcks->af,
1015 			    ecn_fallback_droprst);
1016 			tpheur->th_ecn_backoff = tcp_now +
1017 			    (tcp_min_to_hz(tcp_ecn_timeout) <<
1018 			    (tpheur->th_ecn_droprst - ECN_MAX_DROPRST));
1019 
1020 			os_log(OS_LOG_DEFAULT, "%s disable ECN until %u now %u on %lx for drop-RST\n",
1021 			    __func__, tpheur->th_ecn_backoff, tcp_now,
1022 			    (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
1023 		}
1024 	}
1025 
1026 	if ((flags & TCPCACHE_F_ECN_DROPRXMT) &&
1027 	    tpheur->th_ecn_droprxmt < TCP_CACHE_OVERFLOW_PROTECT &&
1028 	    TSTMP_LEQ(tpheur->th_ecn_backoff, tcp_now)) {
1029 		tpheur->th_ecn_droprxmt++;
1030 		if (tpheur->th_ecn_droprxmt >= ECN_MAX_DROPRXMT) {
1031 			tcpstat.tcps_ecn_fallback_droprxmt++;
1032 			TCP_CACHE_INC_IFNET_STAT(tcks->ifp, tcks->af,
1033 			    ecn_fallback_droprxmt);
1034 			tpheur->th_ecn_backoff = tcp_now +
1035 			    (tcp_min_to_hz(tcp_ecn_timeout) <<
1036 			    (tpheur->th_ecn_droprxmt - ECN_MAX_DROPRXMT));
1037 
1038 			os_log(OS_LOG_DEFAULT, "%s disable ECN until %u now %u on %lx for drop-Rxmit\n",
1039 			    __func__, tpheur->th_ecn_backoff, tcp_now,
1040 			    (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
1041 		}
1042 	}
1043 	if ((flags & TCPCACHE_F_ECN_SYNRST) &&
1044 	    tpheur->th_ecn_synrst < TCP_CACHE_OVERFLOW_PROTECT) {
1045 		tpheur->th_ecn_synrst++;
1046 		if (tpheur->th_ecn_synrst >= ECN_MAX_SYNRST) {
1047 			tcpstat.tcps_ecn_fallback_synrst++;
1048 			TCP_CACHE_INC_IFNET_STAT(tcks->ifp, tcks->af,
1049 			    ecn_fallback_synrst);
1050 			tpheur->th_ecn_backoff = tcp_now +
1051 			    (tcp_min_to_hz(tcp_ecn_timeout) <<
1052 			    (tpheur->th_ecn_synrst - ECN_MAX_SYNRST));
1053 
1054 			os_log(OS_LOG_DEFAULT, "%s disable ECN until %u now %u on %lx for SYN-RST\n",
1055 			    __func__, tpheur->th_ecn_backoff, tcp_now,
1056 			    (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
1057 		}
1058 	}
1059 	tcp_heuristic_unlock(head);
1060 }
1061 
1062 void
tcp_heuristic_tfo_loss(struct tcpcb * tp)1063 tcp_heuristic_tfo_loss(struct tcpcb *tp)
1064 {
1065 	struct tcp_cache_key_src tcks;
1066 	uint32_t flag = 0;
1067 
1068 	if (symptoms_is_wifi_lossy() &&
1069 	    IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) {
1070 		return;
1071 	}
1072 
1073 	tcp_cache_key_src_create(tp, &tcks);
1074 
1075 	if (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) {
1076 		flag = (TCPCACHE_F_TFO_DATA | TCPCACHE_F_TFO_REQ);
1077 	}
1078 	if (tp->t_tfo_stats & TFO_S_COOKIE_REQ) {
1079 		flag = TCPCACHE_F_TFO_REQ;
1080 	}
1081 
1082 	tcp_heuristic_inc_counters(&tcks, flag);
1083 }
1084 
1085 void
tcp_heuristic_tfo_rst(struct tcpcb * tp)1086 tcp_heuristic_tfo_rst(struct tcpcb *tp)
1087 {
1088 	struct tcp_cache_key_src tcks;
1089 	uint32_t flag = 0;
1090 
1091 	tcp_cache_key_src_create(tp, &tcks);
1092 
1093 	if (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) {
1094 		flag = (TCPCACHE_F_TFO_DATA_RST | TCPCACHE_F_TFO_REQ_RST);
1095 	}
1096 	if (tp->t_tfo_stats & TFO_S_COOKIE_REQ) {
1097 		flag = TCPCACHE_F_TFO_REQ_RST;
1098 	}
1099 
1100 	tcp_heuristic_inc_counters(&tcks, flag);
1101 }
1102 
1103 void
tcp_heuristic_mptcp_loss(struct tcpcb * tp)1104 tcp_heuristic_mptcp_loss(struct tcpcb *tp)
1105 {
1106 	struct tcp_cache_key_src tcks;
1107 
1108 	if (symptoms_is_wifi_lossy() &&
1109 	    IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) {
1110 		return;
1111 	}
1112 
1113 	tcp_cache_key_src_create(tp, &tcks);
1114 
1115 	tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_MPTCP);
1116 }
1117 
1118 void
tcp_heuristic_ecn_loss(struct tcpcb * tp)1119 tcp_heuristic_ecn_loss(struct tcpcb *tp)
1120 {
1121 	struct tcp_cache_key_src tcks;
1122 
1123 	if (symptoms_is_wifi_lossy() &&
1124 	    IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) {
1125 		return;
1126 	}
1127 
1128 	tcp_cache_key_src_create(tp, &tcks);
1129 
1130 	tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN);
1131 }
1132 
1133 void
tcp_heuristic_ecn_droprst(struct tcpcb * tp)1134 tcp_heuristic_ecn_droprst(struct tcpcb *tp)
1135 {
1136 	struct tcp_cache_key_src tcks;
1137 
1138 	tcp_cache_key_src_create(tp, &tcks);
1139 
1140 	tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_DROPRST);
1141 }
1142 
1143 void
tcp_heuristic_ecn_droprxmt(struct tcpcb * tp)1144 tcp_heuristic_ecn_droprxmt(struct tcpcb *tp)
1145 {
1146 	struct tcp_cache_key_src tcks;
1147 
1148 	tcp_cache_key_src_create(tp, &tcks);
1149 
1150 	tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_DROPRXMT);
1151 }
1152 
1153 void
tcp_heuristic_ecn_synrst(struct tcpcb * tp)1154 tcp_heuristic_ecn_synrst(struct tcpcb *tp)
1155 {
1156 	struct tcp_cache_key_src tcks;
1157 
1158 	tcp_cache_key_src_create(tp, &tcks);
1159 
1160 	tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_SYNRST);
1161 }
1162 
1163 void
tcp_heuristic_tfo_middlebox(struct tcpcb * tp)1164 tcp_heuristic_tfo_middlebox(struct tcpcb *tp)
1165 {
1166 	struct tcp_cache_key_src tcks;
1167 
1168 	tp->t_tfo_flags |= TFO_F_HEURISTIC_DONE;
1169 
1170 	tcp_cache_key_src_create(tp, &tcks);
1171 	tcp_heuristic_tfo_middlebox_common(&tcks);
1172 }
1173 
1174 static void
tcp_heuristic_ecn_aggressive_common(struct tcp_cache_key_src * tcks)1175 tcp_heuristic_ecn_aggressive_common(struct tcp_cache_key_src *tcks)
1176 {
1177 	struct tcp_heuristics_head *head;
1178 	struct tcp_heuristic *tpheur;
1179 
1180 	tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
1181 	if (tpheur == NULL) {
1182 		return;
1183 	}
1184 
1185 	if (TSTMP_GT(tpheur->th_ecn_backoff, tcp_now)) {
1186 		/* We are already in aggressive mode */
1187 		tcp_heuristic_unlock(head);
1188 		return;
1189 	}
1190 
1191 	/* Must be done before, otherwise we will start off with expo-backoff */
1192 	tpheur->th_ecn_backoff = tcp_now +
1193 	    (tcp_min_to_hz(tcp_ecn_timeout) << (tpheur->th_ecn_aggressive));
1194 
1195 	/*
1196 	 * Ugly way to prevent integer overflow... limit to prevent in
1197 	 * overflow during exp. backoff.
1198 	 */
1199 	if (tpheur->th_ecn_aggressive < TCP_CACHE_OVERFLOW_PROTECT) {
1200 		tpheur->th_ecn_aggressive++;
1201 	}
1202 
1203 	tcp_heuristic_unlock(head);
1204 
1205 	os_log(OS_LOG_DEFAULT, "%s disable ECN until %u now %u on %lx\n", __func__,
1206 	    tpheur->th_ecn_backoff, tcp_now, (unsigned long)VM_KERNEL_ADDRPERM(tpheur));
1207 }
1208 
1209 void
tcp_heuristic_ecn_aggressive(struct tcpcb * tp)1210 tcp_heuristic_ecn_aggressive(struct tcpcb *tp)
1211 {
1212 	struct tcp_cache_key_src tcks;
1213 
1214 	tcp_cache_key_src_create(tp, &tcks);
1215 	tcp_heuristic_ecn_aggressive_common(&tcks);
1216 }
1217 
1218 static boolean_t
tcp_heuristic_do_tfo_common(struct tcp_cache_key_src * tcks)1219 tcp_heuristic_do_tfo_common(struct tcp_cache_key_src *tcks)
1220 {
1221 	struct tcp_heuristics_head *head;
1222 	struct tcp_heuristic *tpheur;
1223 
1224 	if (disable_tcp_heuristics) {
1225 		return TRUE;
1226 	}
1227 
1228 	/* Get the tcp-heuristic. */
1229 	tpheur = tcp_getheuristic_with_lock(tcks, 0, &head);
1230 	if (tpheur == NULL) {
1231 		return TRUE;
1232 	}
1233 
1234 	if (tpheur->th_tfo_in_backoff == 0) {
1235 		goto tfo_ok;
1236 	}
1237 
1238 	if (TSTMP_GT(tcp_now, tpheur->th_tfo_backoff_until)) {
1239 		tpheur->th_tfo_in_backoff = 0;
1240 		tpheur->th_tfo_enabled_time = tcp_now;
1241 
1242 		goto tfo_ok;
1243 	}
1244 
1245 	tcp_heuristic_unlock(head);
1246 	return FALSE;
1247 
1248 tfo_ok:
1249 	tcp_heuristic_unlock(head);
1250 	return TRUE;
1251 }
1252 
1253 boolean_t
tcp_heuristic_do_tfo(struct tcpcb * tp)1254 tcp_heuristic_do_tfo(struct tcpcb *tp)
1255 {
1256 	struct tcp_cache_key_src tcks;
1257 
1258 	tcp_cache_key_src_create(tp, &tcks);
1259 	if (tcp_heuristic_do_tfo_common(&tcks)) {
1260 		return TRUE;
1261 	}
1262 
1263 	return FALSE;
1264 }
1265 /*
1266  * @return:
1267  *         0	Enable MPTCP (we are still discovering middleboxes)
1268  *         -1	Enable MPTCP (heuristics have been temporarily disabled)
1269  *         1	Disable MPTCP
1270  */
1271 int
tcp_heuristic_do_mptcp(struct tcpcb * tp)1272 tcp_heuristic_do_mptcp(struct tcpcb *tp)
1273 {
1274 	struct tcp_cache_key_src tcks;
1275 	struct tcp_heuristics_head *head = NULL;
1276 	struct tcp_heuristic *tpheur;
1277 	int ret = 0;
1278 
1279 	if (disable_tcp_heuristics ||
1280 	    (tptomptp(tp)->mpt_mpte->mpte_flags & MPTE_FORCE_ENABLE)) {
1281 		return 0;
1282 	}
1283 
1284 	tcp_cache_key_src_create(tp, &tcks);
1285 
1286 	/* Get the tcp-heuristic. */
1287 	tpheur = tcp_getheuristic_with_lock(&tcks, 0, &head);
1288 	if (tpheur == NULL) {
1289 		return 0;
1290 	}
1291 
1292 	if (tpheur->th_mptcp_in_backoff == 0 ||
1293 	    tpheur->th_mptcp_heuristic_disabled == 1) {
1294 		goto mptcp_ok;
1295 	}
1296 
1297 	if (TSTMP_GT(tpheur->th_mptcp_backoff, tcp_now)) {
1298 		goto fallback;
1299 	}
1300 
1301 	tpheur->th_mptcp_in_backoff = 0;
1302 
1303 mptcp_ok:
1304 	if (tpheur->th_mptcp_heuristic_disabled) {
1305 		ret = -1;
1306 
1307 		if (TSTMP_GT(tcp_now, tpheur->th_mptcp_backoff)) {
1308 			tpheur->th_mptcp_heuristic_disabled = 0;
1309 			tpheur->th_mptcp_success = 0;
1310 		}
1311 	}
1312 
1313 	tcp_heuristic_unlock(head);
1314 	return ret;
1315 
1316 fallback:
1317 	if (head) {
1318 		tcp_heuristic_unlock(head);
1319 	}
1320 
1321 	if (tptomptp(tp)->mpt_mpte->mpte_flags & MPTE_FIRSTPARTY) {
1322 		tcpstat.tcps_mptcp_fp_heuristic_fallback++;
1323 	} else {
1324 		tcpstat.tcps_mptcp_heuristic_fallback++;
1325 	}
1326 
1327 	return 1;
1328 }
1329 
1330 static boolean_t
tcp_heuristic_do_ecn_common(struct tcp_cache_key_src * tcks)1331 tcp_heuristic_do_ecn_common(struct tcp_cache_key_src *tcks)
1332 {
1333 	struct tcp_heuristics_head *head;
1334 	struct tcp_heuristic *tpheur;
1335 	boolean_t ret = TRUE;
1336 
1337 	if (disable_tcp_heuristics) {
1338 		return TRUE;
1339 	}
1340 
1341 	/* Get the tcp-heuristic. */
1342 	tpheur = tcp_getheuristic_with_lock(tcks, 0, &head);
1343 	if (tpheur == NULL) {
1344 		return ret;
1345 	}
1346 
1347 	if (TSTMP_GT(tpheur->th_ecn_backoff, tcp_now)) {
1348 		ret = FALSE;
1349 	} else {
1350 		/* Reset the following counters to start re-evaluating */
1351 		if (tpheur->th_ecn_droprst >= ECN_RETRY_LIMIT) {
1352 			tpheur->th_ecn_droprst = 0;
1353 		}
1354 		if (tpheur->th_ecn_droprxmt >= ECN_RETRY_LIMIT) {
1355 			tpheur->th_ecn_droprxmt = 0;
1356 		}
1357 		if (tpheur->th_ecn_synrst >= ECN_RETRY_LIMIT) {
1358 			tpheur->th_ecn_synrst = 0;
1359 		}
1360 
1361 		/* Make sure it follows along */
1362 		tpheur->th_ecn_backoff = tcp_now;
1363 	}
1364 
1365 	tcp_heuristic_unlock(head);
1366 
1367 	return ret;
1368 }
1369 
1370 boolean_t
tcp_heuristic_do_ecn(struct tcpcb * tp)1371 tcp_heuristic_do_ecn(struct tcpcb *tp)
1372 {
1373 	struct tcp_cache_key_src tcks;
1374 
1375 	tcp_cache_key_src_create(tp, &tcks);
1376 	return tcp_heuristic_do_ecn_common(&tcks);
1377 }
1378 
1379 boolean_t
tcp_heuristic_do_ecn_with_address(struct ifnet * ifp,union sockaddr_in_4_6 * local_address)1380 tcp_heuristic_do_ecn_with_address(struct ifnet *ifp,
1381     union sockaddr_in_4_6 *local_address)
1382 {
1383 	struct tcp_cache_key_src tcks;
1384 
1385 	memset(&tcks, 0, sizeof(tcks));
1386 	tcks.ifp = ifp;
1387 
1388 	calculate_tcp_clock();
1389 
1390 	if (local_address->sa.sa_family == AF_INET6) {
1391 		memcpy(&tcks.laddr.addr6, &local_address->sin6.sin6_addr, sizeof(struct in6_addr));
1392 		tcks.af = AF_INET6;
1393 	} else if (local_address->sa.sa_family == AF_INET) {
1394 		memcpy(&tcks.laddr.addr, &local_address->sin.sin_addr, sizeof(struct in_addr));
1395 		tcks.af = AF_INET;
1396 	}
1397 
1398 	return tcp_heuristic_do_ecn_common(&tcks);
1399 }
1400 
1401 void
tcp_heuristics_ecn_update(struct necp_tcp_ecn_cache * necp_buffer,struct ifnet * ifp,union sockaddr_in_4_6 * local_address)1402 tcp_heuristics_ecn_update(struct necp_tcp_ecn_cache *necp_buffer,
1403     struct ifnet *ifp, union sockaddr_in_4_6 *local_address)
1404 {
1405 	struct tcp_cache_key_src tcks;
1406 
1407 	memset(&tcks, 0, sizeof(tcks));
1408 	tcks.ifp = ifp;
1409 
1410 	calculate_tcp_clock();
1411 
1412 	if (local_address->sa.sa_family == AF_INET6) {
1413 		memcpy(&tcks.laddr.addr6, &local_address->sin6.sin6_addr, sizeof(struct in6_addr));
1414 		tcks.af = AF_INET6;
1415 	} else if (local_address->sa.sa_family == AF_INET) {
1416 		memcpy(&tcks.laddr.addr, &local_address->sin.sin_addr, sizeof(struct in_addr));
1417 		tcks.af = AF_INET;
1418 	}
1419 
1420 	if (necp_buffer->necp_tcp_ecn_heuristics_success) {
1421 		tcp_heuristic_reset_counters(&tcks, TCPCACHE_F_ECN);
1422 	} else if (necp_buffer->necp_tcp_ecn_heuristics_loss) {
1423 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN);
1424 	} else if (necp_buffer->necp_tcp_ecn_heuristics_drop_rst) {
1425 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_DROPRST);
1426 	} else if (necp_buffer->necp_tcp_ecn_heuristics_drop_rxmt) {
1427 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_DROPRXMT);
1428 	} else if (necp_buffer->necp_tcp_ecn_heuristics_syn_rst) {
1429 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_SYNRST);
1430 	} else if (necp_buffer->necp_tcp_ecn_heuristics_aggressive) {
1431 		tcp_heuristic_ecn_aggressive_common(&tcks);
1432 	}
1433 
1434 	return;
1435 }
1436 
1437 boolean_t
tcp_heuristic_do_tfo_with_address(struct ifnet * ifp,union sockaddr_in_4_6 * local_address,union sockaddr_in_4_6 * remote_address,uint8_t * cookie,uint8_t * cookie_len)1438 tcp_heuristic_do_tfo_with_address(struct ifnet *ifp,
1439     union sockaddr_in_4_6 *local_address, union sockaddr_in_4_6 *remote_address,
1440     uint8_t *cookie, uint8_t *cookie_len)
1441 {
1442 	struct tcp_cache_key_src tcks;
1443 
1444 	memset(&tcks, 0, sizeof(tcks));
1445 	tcks.ifp = ifp;
1446 
1447 	calculate_tcp_clock();
1448 
1449 	if (remote_address->sa.sa_family == AF_INET6) {
1450 		memcpy(&tcks.laddr.addr6, &local_address->sin6.sin6_addr, sizeof(struct in6_addr));
1451 		memcpy(&tcks.faddr.addr6, &remote_address->sin6.sin6_addr, sizeof(struct in6_addr));
1452 		tcks.af = AF_INET6;
1453 	} else if (remote_address->sa.sa_family == AF_INET) {
1454 		memcpy(&tcks.laddr.addr, &local_address->sin.sin_addr, sizeof(struct in_addr));
1455 		memcpy(&tcks.faddr.addr, &remote_address->sin.sin_addr, sizeof(struct in_addr));
1456 		tcks.af = AF_INET;
1457 	}
1458 
1459 	if (tcp_heuristic_do_tfo_common(&tcks)) {
1460 		if (!tcp_cache_get_cookie_common(&tcks, cookie, cookie_len)) {
1461 			*cookie_len = 0;
1462 		}
1463 		return TRUE;
1464 	}
1465 
1466 	return FALSE;
1467 }
1468 
1469 void
tcp_heuristics_tfo_update(struct necp_tcp_tfo_cache * necp_buffer,struct ifnet * ifp,union sockaddr_in_4_6 * local_address,union sockaddr_in_4_6 * remote_address)1470 tcp_heuristics_tfo_update(struct necp_tcp_tfo_cache *necp_buffer,
1471     struct ifnet *ifp, union sockaddr_in_4_6 *local_address,
1472     union sockaddr_in_4_6 *remote_address)
1473 {
1474 	struct tcp_cache_key_src tcks;
1475 
1476 	memset(&tcks, 0, sizeof(tcks));
1477 	tcks.ifp = ifp;
1478 
1479 	calculate_tcp_clock();
1480 
1481 	if (remote_address->sa.sa_family == AF_INET6) {
1482 		memcpy(&tcks.laddr.addr6, &local_address->sin6.sin6_addr, sizeof(struct in6_addr));
1483 		memcpy(&tcks.faddr.addr6, &remote_address->sin6.sin6_addr, sizeof(struct in6_addr));
1484 		tcks.af = AF_INET6;
1485 	} else if (remote_address->sa.sa_family == AF_INET) {
1486 		memcpy(&tcks.laddr.addr, &local_address->sin.sin_addr, sizeof(struct in_addr));
1487 		memcpy(&tcks.faddr.addr, &remote_address->sin.sin_addr, sizeof(struct in_addr));
1488 		tcks.af = AF_INET;
1489 	}
1490 
1491 	if (necp_buffer->necp_tcp_tfo_heuristics_success) {
1492 		tcp_heuristic_reset_counters(&tcks, TCPCACHE_F_TFO_REQ | TCPCACHE_F_TFO_DATA |
1493 		    TCPCACHE_F_TFO_REQ_RST | TCPCACHE_F_TFO_DATA_RST);
1494 	}
1495 
1496 	if (necp_buffer->necp_tcp_tfo_heuristics_success_req) {
1497 		tcp_heuristic_reset_counters(&tcks, TCPCACHE_F_TFO_REQ | TCPCACHE_F_TFO_REQ_RST);
1498 	}
1499 
1500 	if (necp_buffer->necp_tcp_tfo_heuristics_loss) {
1501 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_TFO_REQ | TCPCACHE_F_TFO_DATA);
1502 	}
1503 
1504 	if (necp_buffer->necp_tcp_tfo_heuristics_loss_req) {
1505 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_TFO_REQ);
1506 	}
1507 
1508 	if (necp_buffer->necp_tcp_tfo_heuristics_rst_data) {
1509 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_TFO_REQ_RST | TCPCACHE_F_TFO_DATA_RST);
1510 	}
1511 
1512 	if (necp_buffer->necp_tcp_tfo_heuristics_rst_req) {
1513 		tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_TFO_REQ_RST);
1514 	}
1515 
1516 	if (necp_buffer->necp_tcp_tfo_heuristics_middlebox) {
1517 		tcp_heuristic_tfo_middlebox_common(&tcks);
1518 	}
1519 
1520 	if (necp_buffer->necp_tcp_tfo_cookie_len != 0) {
1521 		tcp_cache_set_cookie_common(&tcks,
1522 		    necp_buffer->necp_tcp_tfo_cookie, necp_buffer->necp_tcp_tfo_cookie_len);
1523 	}
1524 
1525 	return;
1526 }
1527 
1528 static void
sysctl_cleartfocache(void)1529 sysctl_cleartfocache(void)
1530 {
1531 	int i;
1532 
1533 	for (i = 0; i < tcp_cache_size; i++) {
1534 		struct tcp_cache_head *head = &tcp_cache[i];
1535 		struct tcp_cache *tpcache, *tmp;
1536 		struct tcp_heuristics_head *hhead = &tcp_heuristics[i];
1537 		struct tcp_heuristic *tpheur, *htmp;
1538 
1539 		lck_mtx_lock(&head->tch_mtx);
1540 		SLIST_FOREACH_SAFE(tpcache, &head->tcp_caches, list, tmp) {
1541 			SLIST_REMOVE(&head->tcp_caches, tpcache, tcp_cache, list);
1542 			kfree_type(struct tcp_cache, tpcache);
1543 		}
1544 		lck_mtx_unlock(&head->tch_mtx);
1545 
1546 		lck_mtx_lock(&hhead->thh_mtx);
1547 		SLIST_FOREACH_SAFE(tpheur, &hhead->tcp_heuristics, list, htmp) {
1548 			SLIST_REMOVE(&hhead->tcp_heuristics, tpheur, tcp_heuristic, list);
1549 			kfree_type(struct tcp_heuristic, tpheur);
1550 		}
1551 		lck_mtx_unlock(&hhead->thh_mtx);
1552 	}
1553 }
1554 
1555 /* This sysctl is useful for testing purposes only */
1556 static int tcpcleartfo = 0;
1557 
1558 static int sysctl_cleartfo SYSCTL_HANDLER_ARGS
1559 {
1560 #pragma unused(arg1, arg2)
1561 	int error = 0, val, oldval = tcpcleartfo;
1562 
1563 	val = oldval;
1564 	error = sysctl_handle_int(oidp, &val, 0, req);
1565 	if (error || !req->newptr) {
1566 		if (error) {
1567 			os_log_error(OS_LOG_DEFAULT, "%s could not parse int: %d", __func__, error);
1568 		}
1569 		return error;
1570 	}
1571 
1572 	/*
1573 	 * The actual value does not matter. If the value is set, it triggers
1574 	 * the clearing of the TFO cache. If a future implementation does not
1575 	 * use the route entry to hold the TFO cache, replace the route sysctl.
1576 	 */
1577 
1578 	if (val != oldval) {
1579 		sysctl_cleartfocache();
1580 	}
1581 
1582 	tcpcleartfo = val;
1583 
1584 	return error;
1585 }
1586 
1587 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, clear_tfocache, CTLTYPE_INT | CTLFLAG_RW |
1588     CTLFLAG_LOCKED, &tcpcleartfo, 0, &sysctl_cleartfo, "I",
1589     "Toggle to clear the TFO destination based heuristic cache");
1590 
1591 void
tcp_cache_init(void)1592 tcp_cache_init(void)
1593 {
1594 	uint64_t sane_size_meg = sane_size / 1024 / 1024;
1595 
1596 	/*
1597 	 * On machines with <100MB of memory this will result in a (full) cache-size
1598 	 * of 32 entries, thus 32 * 5 * 64bytes = 10KB. (about 0.01 %)
1599 	 * On machines with > 4GB of memory, we have a cache-size of 1024 entries,
1600 	 * thus about 327KB.
1601 	 *
1602 	 * Side-note: we convert to uint32_t. If sane_size is more than
1603 	 * 16000 TB, we loose precision. But, who cares? :)
1604 	 */
1605 	tcp_cache_size = tcp_cache_roundup2((uint32_t)(sane_size_meg >> 2));
1606 	if (tcp_cache_size < 32) {
1607 		tcp_cache_size = 32;
1608 	} else if (tcp_cache_size > 1024) {
1609 		tcp_cache_size = 1024;
1610 	}
1611 
1612 	tcp_cache = zalloc_permanent(sizeof(struct tcp_cache_head) * tcp_cache_size,
1613 	    ZALIGN(struct tcp_cache_head));
1614 
1615 	tcp_heuristics = zalloc_permanent(sizeof(struct tcp_heuristics_head) * tcp_cache_size,
1616 	    ZALIGN(struct tcp_heuristics_head));
1617 
1618 	for (int i = 0; i < tcp_cache_size; i++) {
1619 		lck_mtx_init(&tcp_cache[i].tch_mtx, &tcp_cache_mtx_grp,
1620 		    &tcp_cache_mtx_attr);
1621 		SLIST_INIT(&tcp_cache[i].tcp_caches);
1622 
1623 		lck_mtx_init(&tcp_heuristics[i].thh_mtx, &tcp_heuristic_mtx_grp,
1624 		    &tcp_heuristic_mtx_attr);
1625 		SLIST_INIT(&tcp_heuristics[i].tcp_heuristics);
1626 	}
1627 
1628 	tcp_cache_hash_seed = RandomULong();
1629 }
1630