]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_cache.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_cache.c
1 /*
2 * Copyright (c) 2015-2017 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_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>
42
43 typedef union {
44 struct in_addr addr;
45 struct in6_addr addr6;
46 } in_4_6_addr;
47
48 struct tcp_heuristic_key {
49 union {
50 uint8_t thk_net_signature[IFNET_SIGNATURELEN];
51 in_4_6_addr thk_ip;
52 };
53 sa_family_t thk_family;
54 };
55
56 struct tcp_heuristic {
57 SLIST_ENTRY(tcp_heuristic) list;
58
59 uint32_t th_last_access;
60
61 struct tcp_heuristic_key th_key;
62
63 char th_val_start[0]; /* Marker for memsetting to 0 */
64
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 */
81
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? */
85
86 char th_val_end[0]; /* Marker for memsetting to 0 */
87 };
88
89 struct tcp_heuristics_head {
90 SLIST_HEAD(tcp_heur_bucket, tcp_heuristic) tcp_heuristics;
91
92 /* Per-hashbucket lock to avoid lock-contention */
93 lck_mtx_t thh_mtx;
94 };
95
96 struct tcp_cache_key {
97 sa_family_t tck_family;
98
99 struct tcp_heuristic_key tck_src;
100 in_4_6_addr tck_dst;
101 };
102
103 struct tcp_cache {
104 SLIST_ENTRY(tcp_cache) list;
105
106 u_int32_t tc_last_access;
107
108 struct tcp_cache_key tc_key;
109
110 u_int8_t tc_tfo_cookie[TFO_COOKIE_LEN_MAX];
111 u_int8_t tc_tfo_cookie_len;
112 };
113
114 struct tcp_cache_head {
115 SLIST_HEAD(tcp_cache_bucket, tcp_cache) tcp_caches;
116
117 /* Per-hashbucket lock to avoid lock-contention */
118 lck_mtx_t tch_mtx;
119 };
120
121 struct tcp_cache_key_src {
122 struct ifnet *ifp;
123 in_4_6_addr laddr;
124 in_4_6_addr faddr;
125 int af;
126 };
127
128 static u_int32_t tcp_cache_hash_seed;
129
130 size_t tcp_cache_size;
131
132 /*
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
135 */
136 #define TCP_CACHE_BUCKET_SIZE 5
137
138 static struct tcp_cache_head *tcp_cache;
139
140 decl_lck_mtx_data(, tcp_cache_mtx);
141
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;
145
146 static struct tcp_heuristics_head *tcp_heuristics;
147
148 decl_lck_mtx_data(, tcp_heuristics_mtx);
149
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;
153
154 static uint32_t tcp_backoff_maximum = 65536;
155
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");
158
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");
161
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)");
164
165 static uint32_t
166 tcp_min_to_hz(uint32_t minutes)
167 {
168 if (minutes > 65536) {
169 return (uint32_t)65536 * 60 * TCP_RETRANSHZ;
170 }
171
172 return minutes * 60 * TCP_RETRANSHZ;
173 }
174
175 /*
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.
179 */
180 #define TCP_CACHE_OVERFLOW_PROTECT 9
181
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
190
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
201
202 /* Always retry ECN after backing off to this level for some heuristics */
203 #define ECN_RETRY_LIMIT 9
204
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_++;\
209 } else { \
210 (_ifp_)->if_ipv4_stat->_stat_++;\
211 }\
212 }\
213 }
214
215 /*
216 * Round up to next higher power-of 2. See "Bit Twiddling Hacks".
217 *
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.
220 */
221 static u_int32_t
222 tcp_cache_roundup2(u_int32_t a)
223 {
224 a--;
225 a |= a >> 1;
226 a |= a >> 2;
227 a |= a >> 4;
228 a |= a >> 8;
229 a |= a >> 16;
230 a++;
231
232 return a;
233 }
234
235 static void
236 tcp_cache_hash_src(struct tcp_cache_key_src *tcks, struct tcp_heuristic_key *key)
237 {
238 struct ifnet *ifp = tcks->ifp;
239 uint8_t len = sizeof(key->thk_net_signature);
240 uint16_t flags;
241
242 if (tcks->af == AF_INET6) {
243 int ret;
244
245 key->thk_family = AF_INET6;
246 ret = ifnet_get_netsignature(ifp, AF_INET6, &len, &flags,
247 key->thk_net_signature);
248
249 /*
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.
253 */
254 if (ret == ENOENT || ret == EINVAL) {
255 memcpy(&key->thk_ip.addr6, &tcks->laddr.addr6, sizeof(struct in6_addr));
256 }
257 } else {
258 int ret;
259
260 key->thk_family = AF_INET;
261 ret = ifnet_get_netsignature(ifp, AF_INET, &len, &flags,
262 key->thk_net_signature);
263
264 /*
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.
268 */
269 if (ret == ENOENT || ret == EINVAL) {
270 memcpy(&key->thk_ip.addr, &tcks->laddr.addr, sizeof(struct in_addr));
271 }
272 }
273 }
274
275 static u_int16_t
276 tcp_cache_hash(struct tcp_cache_key_src *tcks, struct tcp_cache_key *key)
277 {
278 u_int32_t hash;
279
280 bzero(key, sizeof(struct tcp_cache_key));
281
282 tcp_cache_hash_src(tcks, &key->tck_src);
283
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));
288 } else {
289 key->tck_family = AF_INET;
290 memcpy(&key->tck_dst.addr, &tcks->faddr.addr,
291 sizeof(struct in_addr));
292 }
293
294 hash = net_flowhash(key, sizeof(struct tcp_cache_key),
295 tcp_cache_hash_seed);
296
297 return hash & (tcp_cache_size - 1);
298 }
299
300 static void
301 tcp_cache_unlock(struct tcp_cache_head *head)
302 {
303 lck_mtx_unlock(&head->tch_mtx);
304 }
305
306 /*
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!!!
309 *
310 * Otherwise, better build another lookup-function that does not hold the
311 * lock and you copy out the bits and bytes.
312 *
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().
315 */
316 static struct tcp_cache *
317 tcp_getcache_with_lock(struct tcp_cache_key_src *tcks,
318 int create, struct tcp_cache_head **headarg)
319 {
320 struct tcp_cache *tpcache = NULL;
321 struct tcp_cache_head *head;
322 struct tcp_cache_key key;
323 u_int16_t hash;
324 int i = 0;
325
326 hash = tcp_cache_hash(tcks, &key);
327 head = &tcp_cache[hash];
328
329 lck_mtx_lock(&head->tch_mtx);
330
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) {
334 break;
335 }
336
337 i++;
338 }
339
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;
345
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;
349 if (age > max_age) {
350 max_age = age;
351 oldest_cache = tpcache;
352 }
353 }
354 VERIFY(oldest_cache != NULL);
355
356 tpcache = oldest_cache;
357
358 /* We recycle, thus let's indicate that there is no cookie */
359 tpcache->tc_tfo_cookie_len = 0;
360 } else {
361 /* Create a new cache and add it to the list */
362 tpcache = _MALLOC(sizeof(struct tcp_cache), M_TEMP,
363 M_NOWAIT | M_ZERO);
364 if (tpcache == NULL) {
365 goto out_null;
366 }
367
368 SLIST_INSERT_HEAD(&head->tcp_caches, tpcache, list);
369 }
370
371 memcpy(&tpcache->tc_key, &key, sizeof(key));
372 }
373
374 if (tpcache == NULL) {
375 goto out_null;
376 }
377
378 /* Update timestamp for garbage collection purposes */
379 tpcache->tc_last_access = tcp_now;
380 *headarg = head;
381
382 return tpcache;
383
384 out_null:
385 tcp_cache_unlock(head);
386 return NULL;
387 }
388
389 static void
390 tcp_cache_key_src_create(struct tcpcb *tp, struct tcp_cache_key_src *tcks)
391 {
392 struct inpcb *inp = tp->t_inpcb;
393 memset(tcks, 0, sizeof(*tcks));
394
395 tcks->ifp = inp->inp_last_outifp;
396
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));
400 tcks->af = AF_INET6;
401 } else {
402 memcpy(&tcks->laddr.addr, &inp->inp_laddr, sizeof(struct in_addr));
403 memcpy(&tcks->faddr.addr, &inp->inp_faddr, sizeof(struct in_addr));
404 tcks->af = AF_INET;
405 }
406
407 return;
408 }
409
410 static void
411 tcp_cache_set_cookie_common(struct tcp_cache_key_src *tcks, u_char *cookie, u_int8_t len)
412 {
413 struct tcp_cache_head *head;
414 struct tcp_cache *tpcache;
415
416 /* Call lookup/create function */
417 tpcache = tcp_getcache_with_lock(tcks, 1, &head);
418 if (tpcache == NULL) {
419 return;
420 }
421
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);
425
426 tcp_cache_unlock(head);
427 }
428
429 void
430 tcp_cache_set_cookie(struct tcpcb *tp, u_char *cookie, u_int8_t len)
431 {
432 struct tcp_cache_key_src tcks;
433
434 tcp_cache_key_src_create(tp, &tcks);
435 tcp_cache_set_cookie_common(&tcks, cookie, len);
436 }
437
438 static int
439 tcp_cache_get_cookie_common(struct tcp_cache_key_src *tcks, u_char *cookie, u_int8_t *len)
440 {
441 struct tcp_cache_head *head;
442 struct tcp_cache *tpcache;
443
444 /* Call lookup/create function */
445 tpcache = tcp_getcache_with_lock(tcks, 1, &head);
446 if (tpcache == NULL) {
447 return 0;
448 }
449
450 if (tpcache->tc_tfo_cookie_len == 0) {
451 tcp_cache_unlock(head);
452 return 0;
453 }
454
455 /*
456 * Not enough space - this should never happen as it has been checked
457 * in tcp_tfo_check. So, fail here!
458 */
459 VERIFY(tpcache->tc_tfo_cookie_len <= *len);
460
461 memcpy(cookie, tpcache->tc_tfo_cookie, tpcache->tc_tfo_cookie_len);
462 *len = tpcache->tc_tfo_cookie_len;
463
464 tcp_cache_unlock(head);
465
466 return 1;
467 }
468
469 /*
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.
473 *
474 * Returns 0 if we should request a cookie.
475 * Returns 1 if the cookie has been found and written.
476 */
477 int
478 tcp_cache_get_cookie(struct tcpcb *tp, u_char *cookie, u_int8_t *len)
479 {
480 struct tcp_cache_key_src tcks;
481
482 tcp_cache_key_src_create(tp, &tcks);
483 return tcp_cache_get_cookie_common(&tcks, cookie, len);
484 }
485
486 static unsigned int
487 tcp_cache_get_cookie_len_common(struct tcp_cache_key_src *tcks)
488 {
489 struct tcp_cache_head *head;
490 struct tcp_cache *tpcache;
491 unsigned int cookie_len;
492
493 /* Call lookup/create function */
494 tpcache = tcp_getcache_with_lock(tcks, 1, &head);
495 if (tpcache == NULL) {
496 return 0;
497 }
498
499 cookie_len = tpcache->tc_tfo_cookie_len;
500
501 tcp_cache_unlock(head);
502
503 return cookie_len;
504 }
505
506 unsigned int
507 tcp_cache_get_cookie_len(struct tcpcb *tp)
508 {
509 struct tcp_cache_key_src tcks;
510
511 tcp_cache_key_src_create(tp, &tcks);
512 return tcp_cache_get_cookie_len_common(&tcks);
513 }
514
515 static u_int16_t
516 tcp_heuristics_hash(struct tcp_cache_key_src *tcks, struct tcp_heuristic_key *key)
517 {
518 u_int32_t hash;
519
520 bzero(key, sizeof(struct tcp_heuristic_key));
521
522 tcp_cache_hash_src(tcks, key);
523
524 hash = net_flowhash(key, sizeof(struct tcp_heuristic_key),
525 tcp_cache_hash_seed);
526
527 return hash & (tcp_cache_size - 1);
528 }
529
530 static void
531 tcp_heuristic_unlock(struct tcp_heuristics_head *head)
532 {
533 lck_mtx_unlock(&head->thh_mtx);
534 }
535
536 /*
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!!!
539 *
540 * Otherwise, better build another lookup-function that does not hold the
541 * lock and you copy out the bits and bytes.
542 *
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().
545 *
546 *
547 * ToDo - way too much code-duplication. We should create an interface to handle
548 * bucketized hashtables with recycling of the oldest element.
549 */
550 static struct tcp_heuristic *
551 tcp_getheuristic_with_lock(struct tcp_cache_key_src *tcks,
552 int create, struct tcp_heuristics_head **headarg)
553 {
554 struct tcp_heuristic *tpheur = NULL;
555 struct tcp_heuristics_head *head;
556 struct tcp_heuristic_key key;
557 u_int16_t hash;
558 int i = 0;
559
560 hash = tcp_heuristics_hash(tcks, &key);
561 head = &tcp_heuristics[hash];
562
563 lck_mtx_lock(&head->thh_mtx);
564
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) {
568 break;
569 }
570
571 i++;
572 }
573
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;
579
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;
583 if (age > max_age) {
584 max_age = age;
585 oldest_heur = tpheur;
586 }
587 }
588 VERIFY(oldest_heur != NULL);
589
590 tpheur = oldest_heur;
591
592 /* We recycle - set everything to 0 */
593 bzero(tpheur->th_val_start,
594 tpheur->th_val_end - tpheur->th_val_start);
595 } else {
596 /* Create a new heuristic and add it to the list */
597 tpheur = _MALLOC(sizeof(struct tcp_heuristic), M_TEMP,
598 M_NOWAIT | M_ZERO);
599 if (tpheur == NULL) {
600 goto out_null;
601 }
602
603 SLIST_INSERT_HEAD(&head->tcp_heuristics, tpheur, list);
604 }
605
606 /*
607 * Set to tcp_now, to make sure it won't be > than tcp_now in the
608 * near future.
609 */
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);
614
615 memcpy(&tpheur->th_key, &key, sizeof(key));
616 }
617
618 if (tpheur == NULL) {
619 goto out_null;
620 }
621
622 /* Update timestamp for garbage collection purposes */
623 tpheur->th_last_access = tcp_now;
624 *headarg = head;
625
626 return tpheur;
627
628 out_null:
629 tcp_heuristic_unlock(head);
630 return NULL;
631 }
632
633 static void
634 tcp_heuristic_reset_counters(struct tcp_cache_key_src *tcks, u_int8_t flags)
635 {
636 struct tcp_heuristics_head *head;
637 struct tcp_heuristic *tpheur;
638
639 /*
640 * Always create heuristics here because MPTCP needs to write success
641 * into it. Thus, we always end up creating them.
642 */
643 tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
644 if (tpheur == NULL) {
645 return;
646 }
647
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));
652 }
653 tpheur->th_tfo_data_loss = 0;
654 }
655
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));
660 }
661 tpheur->th_tfo_req_loss = 0;
662 }
663
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));
668 }
669 tpheur->th_tfo_data_rst = 0;
670 }
671
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));
676 }
677 tpheur->th_tfo_req_rst = 0;
678 }
679
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));
684 }
685 tpheur->th_ecn_loss = 0;
686 tpheur->th_ecn_synrst = 0;
687 }
688
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++;
693
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);
699 }
700 }
701 }
702
703 tcp_heuristic_unlock(head);
704 }
705
706 void
707 tcp_heuristic_tfo_success(struct tcpcb *tp)
708 {
709 struct tcp_cache_key_src tcks;
710 uint8_t flag = 0;
711
712 tcp_cache_key_src_create(tp, &tcks);
713
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);
717 }
718 if (tp->t_tfo_stats & TFO_S_COOKIE_REQ) {
719 flag = (TCPCACHE_F_TFO_REQ | TCPCACHE_F_TFO_REQ_RST);
720 }
721
722 tcp_heuristic_reset_counters(&tcks, flag);
723 }
724
725 void
726 tcp_heuristic_mptcp_success(struct tcpcb *tp)
727 {
728 struct tcp_cache_key_src tcks;
729
730 tcp_cache_key_src_create(tp, &tcks);
731 tcp_heuristic_reset_counters(&tcks, TCPCACHE_F_MPTCP);
732 }
733
734 void
735 tcp_heuristic_ecn_success(struct tcpcb *tp)
736 {
737 struct tcp_cache_key_src tcks;
738
739 tcp_cache_key_src_create(tp, &tcks);
740 tcp_heuristic_reset_counters(&tcks, TCPCACHE_F_ECN);
741 }
742
743 static void
744 __tcp_heuristic_tfo_middlebox_common(struct tcp_heuristic *tpheur)
745 {
746 if (tpheur->th_tfo_in_backoff) {
747 return;
748 }
749
750 tpheur->th_tfo_in_backoff = 1;
751
752 if (tpheur->th_tfo_enabled_time) {
753 uint32_t old_backoff = tpheur->th_tfo_backoff;
754
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);
758 }
759 }
760
761 tpheur->th_tfo_backoff_until = tcp_now + tpheur->th_tfo_backoff;
762
763 /* Then, increase the backoff time */
764 tpheur->th_tfo_backoff *= 2;
765
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);
768 }
769
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));
772 }
773
774 static void
775 tcp_heuristic_tfo_middlebox_common(struct tcp_cache_key_src *tcks)
776 {
777 struct tcp_heuristics_head *head;
778 struct tcp_heuristic *tpheur;
779
780 tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
781 if (tpheur == NULL) {
782 return;
783 }
784
785 __tcp_heuristic_tfo_middlebox_common(tpheur);
786
787 tcp_heuristic_unlock(head);
788 }
789
790 static void
791 tcp_heuristic_inc_counters(struct tcp_cache_key_src *tcks,
792 u_int32_t flags)
793 {
794 struct tcp_heuristics_head *head;
795 struct tcp_heuristic *tpheur;
796
797 tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
798 if (tpheur == NULL) {
799 return;
800 }
801
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++;
805
806 if (tpheur->th_tfo_data_loss >= TFO_MAX_COOKIE_LOSS) {
807 __tcp_heuristic_tfo_middlebox_common(tpheur);
808 }
809 }
810
811 if ((flags & TCPCACHE_F_TFO_REQ) && tpheur->th_tfo_req_loss < TCP_CACHE_OVERFLOW_PROTECT) {
812 tpheur->th_tfo_req_loss++;
813
814 if (tpheur->th_tfo_req_loss >= TFO_MAX_COOKIE_LOSS) {
815 __tcp_heuristic_tfo_middlebox_common(tpheur);
816 }
817 }
818
819 if ((flags & TCPCACHE_F_TFO_DATA_RST) && tpheur->th_tfo_data_rst < TCP_CACHE_OVERFLOW_PROTECT) {
820 tpheur->th_tfo_data_rst++;
821
822 if (tpheur->th_tfo_data_rst >= TFO_MAX_COOKIE_LOSS) {
823 __tcp_heuristic_tfo_middlebox_common(tpheur);
824 }
825 }
826
827 if ((flags & TCPCACHE_F_TFO_REQ_RST) && tpheur->th_tfo_req_rst < TCP_CACHE_OVERFLOW_PROTECT) {
828 tpheur->th_tfo_req_rst++;
829
830 if (tpheur->th_tfo_req_rst >= TFO_MAX_COOKIE_LOSS) {
831 __tcp_heuristic_tfo_middlebox_common(tpheur);
832 }
833 }
834
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));
845
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));
849 }
850 }
851
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) {
857 /*
858 * Yes, we take tcp_ecn_timeout, to avoid adding yet
859 * another sysctl that is just used for testing.
860 */
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;
865
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));
869 }
870 }
871
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));
883
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));
887 }
888 }
889
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));
901
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));
905 }
906 }
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));
917
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));
921 }
922 }
923 tcp_heuristic_unlock(head);
924 }
925
926 void
927 tcp_heuristic_tfo_loss(struct tcpcb *tp)
928 {
929 struct tcp_cache_key_src tcks;
930 uint32_t flag = 0;
931
932 if (symptoms_is_wifi_lossy() &&
933 IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) {
934 return;
935 }
936
937 tcp_cache_key_src_create(tp, &tcks);
938
939 if (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) {
940 flag = (TCPCACHE_F_TFO_DATA | TCPCACHE_F_TFO_REQ);
941 }
942 if (tp->t_tfo_stats & TFO_S_COOKIE_REQ) {
943 flag = TCPCACHE_F_TFO_REQ;
944 }
945
946 tcp_heuristic_inc_counters(&tcks, flag);
947 }
948
949 void
950 tcp_heuristic_tfo_rst(struct tcpcb *tp)
951 {
952 struct tcp_cache_key_src tcks;
953 uint32_t flag = 0;
954
955 tcp_cache_key_src_create(tp, &tcks);
956
957 if (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) {
958 flag = (TCPCACHE_F_TFO_DATA_RST | TCPCACHE_F_TFO_REQ_RST);
959 }
960 if (tp->t_tfo_stats & TFO_S_COOKIE_REQ) {
961 flag = TCPCACHE_F_TFO_REQ_RST;
962 }
963
964 tcp_heuristic_inc_counters(&tcks, flag);
965 }
966
967 void
968 tcp_heuristic_mptcp_loss(struct tcpcb *tp)
969 {
970 struct tcp_cache_key_src tcks;
971
972 if (symptoms_is_wifi_lossy() &&
973 IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) {
974 return;
975 }
976
977 tcp_cache_key_src_create(tp, &tcks);
978
979 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_MPTCP);
980 }
981
982 void
983 tcp_heuristic_ecn_loss(struct tcpcb *tp)
984 {
985 struct tcp_cache_key_src tcks;
986
987 if (symptoms_is_wifi_lossy() &&
988 IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) {
989 return;
990 }
991
992 tcp_cache_key_src_create(tp, &tcks);
993
994 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN);
995 }
996
997 void
998 tcp_heuristic_ecn_droprst(struct tcpcb *tp)
999 {
1000 struct tcp_cache_key_src tcks;
1001
1002 tcp_cache_key_src_create(tp, &tcks);
1003
1004 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_DROPRST);
1005 }
1006
1007 void
1008 tcp_heuristic_ecn_droprxmt(struct tcpcb *tp)
1009 {
1010 struct tcp_cache_key_src tcks;
1011
1012 tcp_cache_key_src_create(tp, &tcks);
1013
1014 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_DROPRXMT);
1015 }
1016
1017 void
1018 tcp_heuristic_ecn_synrst(struct tcpcb *tp)
1019 {
1020 struct tcp_cache_key_src tcks;
1021
1022 tcp_cache_key_src_create(tp, &tcks);
1023
1024 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_ECN_SYNRST);
1025 }
1026
1027 void
1028 tcp_heuristic_tfo_middlebox(struct tcpcb *tp)
1029 {
1030 struct tcp_cache_key_src tcks;
1031
1032 tp->t_tfo_flags |= TFO_F_HEURISTIC_DONE;
1033
1034 tcp_cache_key_src_create(tp, &tcks);
1035 tcp_heuristic_tfo_middlebox_common(&tcks);
1036 }
1037
1038 static void
1039 tcp_heuristic_ecn_aggressive_common(struct tcp_cache_key_src *tcks)
1040 {
1041 struct tcp_heuristics_head *head;
1042 struct tcp_heuristic *tpheur;
1043
1044 tpheur = tcp_getheuristic_with_lock(tcks, 1, &head);
1045 if (tpheur == NULL) {
1046 return;
1047 }
1048
1049 if (TSTMP_GT(tpheur->th_ecn_backoff, tcp_now)) {
1050 /* We are already in aggressive mode */
1051 tcp_heuristic_unlock(head);
1052 return;
1053 }
1054
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));
1058
1059 /*
1060 * Ugly way to prevent integer overflow... limit to prevent in
1061 * overflow during exp. backoff.
1062 */
1063 if (tpheur->th_ecn_aggressive < TCP_CACHE_OVERFLOW_PROTECT) {
1064 tpheur->th_ecn_aggressive++;
1065 }
1066
1067 tcp_heuristic_unlock(head);
1068
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));
1071 }
1072
1073 void
1074 tcp_heuristic_ecn_aggressive(struct tcpcb *tp)
1075 {
1076 struct tcp_cache_key_src tcks;
1077
1078 tcp_cache_key_src_create(tp, &tcks);
1079 tcp_heuristic_ecn_aggressive_common(&tcks);
1080 }
1081
1082 static boolean_t
1083 tcp_heuristic_do_tfo_common(struct tcp_cache_key_src *tcks)
1084 {
1085 struct tcp_heuristics_head *head;
1086 struct tcp_heuristic *tpheur;
1087
1088 if (disable_tcp_heuristics) {
1089 return TRUE;
1090 }
1091
1092 /* Get the tcp-heuristic. */
1093 tpheur = tcp_getheuristic_with_lock(tcks, 0, &head);
1094 if (tpheur == NULL) {
1095 return TRUE;
1096 }
1097
1098 if (tpheur->th_tfo_in_backoff == 0) {
1099 goto tfo_ok;
1100 }
1101
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;
1105
1106 goto tfo_ok;
1107 }
1108
1109 tcp_heuristic_unlock(head);
1110 return FALSE;
1111
1112 tfo_ok:
1113 tcp_heuristic_unlock(head);
1114 return TRUE;
1115 }
1116
1117 boolean_t
1118 tcp_heuristic_do_tfo(struct tcpcb *tp)
1119 {
1120 struct tcp_cache_key_src tcks;
1121
1122 tcp_cache_key_src_create(tp, &tcks);
1123 if (tcp_heuristic_do_tfo_common(&tcks)) {
1124 return TRUE;
1125 }
1126
1127 return FALSE;
1128 }
1129 /*
1130 * @return:
1131 * 0 Enable MPTCP (we are still discovering middleboxes)
1132 * -1 Enable MPTCP (heuristics have been temporarily disabled)
1133 * 1 Disable MPTCP
1134 */
1135 int
1136 tcp_heuristic_do_mptcp(struct tcpcb *tp)
1137 {
1138 struct tcp_cache_key_src tcks;
1139 struct tcp_heuristics_head *head = NULL;
1140 struct tcp_heuristic *tpheur;
1141 int ret = 0;
1142
1143 if (disable_tcp_heuristics ||
1144 (tptomptp(tp)->mpt_mpte->mpte_flags & MPTE_FORCE_ENABLE)) {
1145 return 0;
1146 }
1147
1148 tcp_cache_key_src_create(tp, &tcks);
1149
1150 /* Get the tcp-heuristic. */
1151 tpheur = tcp_getheuristic_with_lock(&tcks, 0, &head);
1152 if (tpheur == NULL) {
1153 return 0;
1154 }
1155
1156 if (tpheur->th_mptcp_in_backoff == 0 ||
1157 tpheur->th_mptcp_heuristic_disabled == 1) {
1158 goto mptcp_ok;
1159 }
1160
1161 if (TSTMP_GT(tpheur->th_mptcp_backoff, tcp_now)) {
1162 goto fallback;
1163 }
1164
1165 tpheur->th_mptcp_in_backoff = 0;
1166
1167 mptcp_ok:
1168 if (tpheur->th_mptcp_heuristic_disabled) {
1169 ret = -1;
1170
1171 if (TSTMP_GT(tcp_now, tpheur->th_mptcp_backoff)) {
1172 tpheur->th_mptcp_heuristic_disabled = 0;
1173 tpheur->th_mptcp_success = 0;
1174 }
1175 }
1176
1177 tcp_heuristic_unlock(head);
1178 return ret;
1179
1180 fallback:
1181 if (head) {
1182 tcp_heuristic_unlock(head);
1183 }
1184
1185 if (tptomptp(tp)->mpt_mpte->mpte_flags & MPTE_FIRSTPARTY) {
1186 tcpstat.tcps_mptcp_fp_heuristic_fallback++;
1187 } else {
1188 tcpstat.tcps_mptcp_heuristic_fallback++;
1189 }
1190
1191 return 1;
1192 }
1193
1194 static boolean_t
1195 tcp_heuristic_do_ecn_common(struct tcp_cache_key_src *tcks)
1196 {
1197 struct tcp_heuristics_head *head;
1198 struct tcp_heuristic *tpheur;
1199 boolean_t ret = TRUE;
1200
1201 if (disable_tcp_heuristics) {
1202 return TRUE;
1203 }
1204
1205 /* Get the tcp-heuristic. */
1206 tpheur = tcp_getheuristic_with_lock(tcks, 0, &head);
1207 if (tpheur == NULL) {
1208 return ret;
1209 }
1210
1211 if (TSTMP_GT(tpheur->th_ecn_backoff, tcp_now)) {
1212 ret = FALSE;
1213 } else {
1214 /* Reset the following counters to start re-evaluating */
1215 if (tpheur->th_ecn_droprst >= ECN_RETRY_LIMIT) {
1216 tpheur->th_ecn_droprst = 0;
1217 }
1218 if (tpheur->th_ecn_droprxmt >= ECN_RETRY_LIMIT) {
1219 tpheur->th_ecn_droprxmt = 0;
1220 }
1221 if (tpheur->th_ecn_synrst >= ECN_RETRY_LIMIT) {
1222 tpheur->th_ecn_synrst = 0;
1223 }
1224
1225 /* Make sure it follows along */
1226 tpheur->th_ecn_backoff = tcp_now;
1227 }
1228
1229 tcp_heuristic_unlock(head);
1230
1231 return ret;
1232 }
1233
1234 boolean_t
1235 tcp_heuristic_do_ecn(struct tcpcb *tp)
1236 {
1237 struct tcp_cache_key_src tcks;
1238
1239 tcp_cache_key_src_create(tp, &tcks);
1240 return tcp_heuristic_do_ecn_common(&tcks);
1241 }
1242
1243 boolean_t
1244 tcp_heuristic_do_ecn_with_address(struct ifnet *ifp,
1245 union sockaddr_in_4_6 *local_address)
1246 {
1247 struct tcp_cache_key_src tcks;
1248
1249 memset(&tcks, 0, sizeof(tcks));
1250 tcks.ifp = ifp;
1251
1252 calculate_tcp_clock();
1253
1254 if (local_address->sa.sa_family == AF_INET6) {
1255 memcpy(&tcks.laddr.addr6, &local_address->sin6.sin6_addr, sizeof(struct in6_addr));
1256 tcks.af = AF_INET6;
1257 } else if (local_address->sa.sa_family == AF_INET) {
1258 memcpy(&tcks.laddr.addr, &local_address->sin.sin_addr, sizeof(struct in_addr));
1259 tcks.af = AF_INET;
1260 }
1261
1262 return tcp_heuristic_do_ecn_common(&tcks);
1263 }
1264
1265 void
1266 tcp_heuristics_ecn_update(struct necp_tcp_ecn_cache *necp_buffer,
1267 struct ifnet *ifp, union sockaddr_in_4_6 *local_address)
1268 {
1269 struct tcp_cache_key_src tcks;
1270
1271 memset(&tcks, 0, sizeof(tcks));
1272 tcks.ifp = ifp;
1273
1274 calculate_tcp_clock();
1275
1276 if (local_address->sa.sa_family == AF_INET6) {
1277 memcpy(&tcks.laddr.addr6, &local_address->sin6.sin6_addr, sizeof(struct in6_addr));
1278 tcks.af = AF_INET6;
1279 } else if (local_address->sa.sa_family == AF_INET) {
1280 memcpy(&tcks.laddr.addr, &local_address->sin.sin_addr, sizeof(struct in_addr));
1281 tcks.af = AF_INET;
1282 }
1283
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);
1296 }
1297
1298 return;
1299 }
1300
1301 boolean_t
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)
1305 {
1306 struct tcp_cache_key_src tcks;
1307
1308 memset(&tcks, 0, sizeof(tcks));
1309 tcks.ifp = ifp;
1310
1311 calculate_tcp_clock();
1312
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));
1316 tcks.af = AF_INET6;
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));
1320 tcks.af = AF_INET;
1321 }
1322
1323 if (tcp_heuristic_do_tfo_common(&tcks)) {
1324 if (!tcp_cache_get_cookie_common(&tcks, cookie, cookie_len)) {
1325 *cookie_len = 0;
1326 }
1327 return TRUE;
1328 }
1329
1330 return FALSE;
1331 }
1332
1333 void
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)
1337 {
1338 struct tcp_cache_key_src tcks;
1339
1340 memset(&tcks, 0, sizeof(tcks));
1341 tcks.ifp = ifp;
1342
1343 calculate_tcp_clock();
1344
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));
1348 tcks.af = AF_INET6;
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));
1352 tcks.af = AF_INET;
1353 }
1354
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);
1358 }
1359
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);
1362 }
1363
1364 if (necp_buffer->necp_tcp_tfo_heuristics_loss) {
1365 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_TFO_REQ | TCPCACHE_F_TFO_DATA);
1366 }
1367
1368 if (necp_buffer->necp_tcp_tfo_heuristics_loss_req) {
1369 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_TFO_REQ);
1370 }
1371
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);
1374 }
1375
1376 if (necp_buffer->necp_tcp_tfo_heuristics_rst_req) {
1377 tcp_heuristic_inc_counters(&tcks, TCPCACHE_F_TFO_REQ_RST);
1378 }
1379
1380 if (necp_buffer->necp_tcp_tfo_heuristics_middlebox) {
1381 tcp_heuristic_tfo_middlebox_common(&tcks);
1382 }
1383
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);
1387 }
1388
1389 return;
1390 }
1391
1392 static void
1393 sysctl_cleartfocache(void)
1394 {
1395 int i;
1396
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;
1402
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);
1407 }
1408 lck_mtx_unlock(&head->tch_mtx);
1409
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);
1414 }
1415 lck_mtx_unlock(&hhead->thh_mtx);
1416 }
1417 }
1418
1419 /* This sysctl is useful for testing purposes only */
1420 static int tcpcleartfo = 0;
1421
1422 static int sysctl_cleartfo SYSCTL_HANDLER_ARGS
1423 {
1424 #pragma unused(arg1, arg2)
1425 int error = 0, val, oldval = tcpcleartfo;
1426
1427 val = oldval;
1428 error = sysctl_handle_int(oidp, &val, 0, req);
1429 if (error || !req->newptr) {
1430 return error;
1431 }
1432
1433 /*
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.
1437 */
1438
1439 if (val != oldval) {
1440 sysctl_cleartfocache();
1441 }
1442
1443 tcpcleartfo = val;
1444
1445 return error;
1446 }
1447
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");
1451
1452 void
1453 tcp_cache_init(void)
1454 {
1455 uint64_t sane_size_meg = sane_size / 1024 / 1024;
1456 int i;
1457
1458 /*
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,
1462 * thus about 327KB.
1463 *
1464 * Side-note: we convert to u_int32_t. If sane_size is more than
1465 * 16000 TB, we loose precision. But, who cares? :)
1466 */
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;
1472 }
1473
1474 tcp_cache = _MALLOC(sizeof(struct tcp_cache_head) * tcp_cache_size,
1475 M_TEMP, M_ZERO);
1476 if (tcp_cache == NULL) {
1477 panic("Allocating tcp_cache failed at boot-time!");
1478 }
1479
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();
1483
1484 tcp_heuristics = _MALLOC(sizeof(struct tcp_heuristics_head) * tcp_cache_size,
1485 M_TEMP, M_ZERO);
1486 if (tcp_heuristics == NULL) {
1487 panic("Allocating tcp_heuristic failed at boot-time!");
1488 }
1489
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();
1493
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);
1498
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);
1502 }
1503
1504 tcp_cache_hash_seed = RandomULong();
1505 }