2 * Copyright (c) 2015-2017 Patrick Kelsey
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This is an implementation of TCP Fast Open (TFO) [RFC7413]. To include
29 * this code, add the following line to your kernel config:
34 * The generated TFO cookies are the 64-bit output of
35 * SipHash24(key=<16-byte-key>, msg=<client-ip>). Multiple concurrent valid
36 * keys are supported so that time-based rolling cookie invalidation
37 * policies can be implemented in the system. The default number of
38 * concurrent keys is 2. This can be adjusted in the kernel config as
41 * options TCP_RFC7413_MAX_KEYS=<num-keys>
44 * In addition to the facilities defined in RFC7413, this implementation
45 * supports a pre-shared key (PSK) mode of operation in which the TFO server
46 * requires the client to be in posession of a shared secret in order for
47 * the client to be able to successfully open TFO connections with the
48 * server. This is useful, for example, in environments where TFO servers
49 * are exposed to both internal and external clients and only wish to allow
50 * TFO connections from internal clients.
52 * In the PSK mode of operation, the server generates and sends TFO cookies
53 * to requesting clients as usual. However, when validating cookies
54 * received in TFO SYNs from clients, the server requires the
55 * client-supplied cookie to equal SipHash24(key=<16-byte-psk>,
56 * msg=<cookie-sent-to-client>).
58 * Multiple concurrent valid pre-shared keys are supported so that
59 * time-based rolling PSK invalidation policies can be implemented in the
60 * system. The default number of concurrent pre-shared keys is 2. This can
61 * be adjusted in the kernel config as follows:
63 * options TCP_RFC7413_MAX_PSKS=<num-psks>
66 * The following TFO-specific sysctls are defined:
68 * net.inet.tcp.fastopen.acceptany (RW, default 0)
69 * When non-zero, all client-supplied TFO cookies will be considered to
72 * net.inet.tcp.fastopen.autokey (RW, default 120)
73 * When this and net.inet.tcp.fastopen.server_enable are non-zero, a new
74 * key will be automatically generated after this many seconds.
76 * net.inet.tcp.fastopen.ccache_bucket_limit
77 * (RWTUN, default TCP_FASTOPEN_CCACHE_BUCKET_LIMIT_DEFAULT)
78 * The maximum number of entries in a client cookie cache bucket.
80 * net.inet.tcp.fastopen.ccache_buckets
81 * (RDTUN, default TCP_FASTOPEN_CCACHE_BUCKETS_DEFAULT)
82 * The number of client cookie cache buckets.
84 * net.inet.tcp.fastopen.client_enable (RW, default 0)
85 * When zero, no new active (i.e., client) TFO connections can be
86 * created. On the transition from enabled to disabled, the client
87 * cookie cache is cleared and disabled. The transition from enabled to
88 * disabled does not affect any active TFO connections in progress; it
89 * only prevents new ones from being made.
91 * net.inet.tcp.fastopen.keylen (RD)
92 * The key length in bytes.
94 * net.inet.tcp.fastopen.maxkeys (RD)
95 * The maximum number of keys supported.
97 * net.inet.tcp.fastopen.maxpsks (RD)
98 * The maximum number of pre-shared keys supported.
100 * net.inet.tcp.fastopen.numkeys (RD)
101 * The current number of keys installed.
103 * net.inet.tcp.fastopen.numpsks (RD)
104 * The current number of pre-shared keys installed.
106 * net.inet.tcp.fastopen.path_disable_time
107 * (RW, default TCP_FASTOPEN_PATH_DISABLE_TIME_DEFAULT)
108 * When a failure occurs while trying to create a new active (i.e.,
109 * client) TFO connection, new active connections on the same path, as
110 * determined by the tuple {client_ip, server_ip, server_port}, will be
111 * forced to be non-TFO for this many seconds. Note that the path
112 * disable mechanism relies on state stored in client cookie cache
113 * entries, so it is possible for the disable time for a given path to
114 * be reduced if the corresponding client cookie cache entry is reused
115 * due to resource pressure before the disable period has elapsed.
117 * net.inet.tcp.fastopen.psk_enable (RW, default 0)
118 * When non-zero, pre-shared key (PSK) mode is enabled for all TFO
119 * servers. On the transition from enabled to disabled, all installed
120 * pre-shared keys are removed.
122 * net.inet.tcp.fastopen.server_enable (RW, default 0)
123 * When zero, no new passive (i.e., server) TFO connections can be
124 * created. On the transition from enabled to disabled, all installed
125 * keys and pre-shared keys are removed. On the transition from
126 * disabled to enabled, if net.inet.tcp.fastopen.autokey is non-zero and
127 * there are no keys installed, a new key will be generated immediately.
128 * The transition from enabled to disabled does not affect any passive
129 * TFO connections in progress; it only prevents new ones from being
132 * net.inet.tcp.fastopen.setkey (WR)
133 * Install a new key by writing net.inet.tcp.fastopen.keylen bytes to
136 * net.inet.tcp.fastopen.setpsk (WR)
137 * Install a new pre-shared key by writing net.inet.tcp.fastopen.keylen
138 * bytes to this sysctl.
140 * In order for TFO connections to be created via a listen socket, that
141 * socket must have the TCP_FASTOPEN socket option set on it. This option
142 * can be set on the socket either before or after the listen() is invoked.
143 * Clearing this option on a listen socket after it has been set has no
144 * effect on existing TFO connections or TFO connections in progress; it
145 * only prevents new TFO connections from being made.
147 * For passively-created sockets, the TCP_FASTOPEN socket option can be
148 * queried to determine whether the connection was established using TFO.
149 * Note that connections that are established via a TFO SYN, but that fall
150 * back to using a non-TFO SYN|ACK will have the TCP_FASTOPEN socket option
153 * Per the RFC, this implementation limits the number of TFO connections
154 * that can be in the SYN_RECEIVED state on a per listen-socket basis.
155 * Whenever this limit is exceeded, requests for new TFO connections are
156 * serviced as non-TFO requests. Without such a limit, given a valid TFO
157 * cookie, an attacker could keep the listen queue in an overflow condition
158 * using a TFO SYN flood. This implementation sets the limit at half the
159 * configured listen backlog.
163 #include <sys/cdefs.h>
164 __FBSDID("$FreeBSD$");
166 #include "opt_inet.h"
168 #include <sys/param.h>
169 #include <sys/kernel.h>
170 #include <sys/hash.h>
171 #include <sys/limits.h>
172 #include <sys/lock.h>
173 #include <sys/rmlock.h>
174 #include <sys/socket.h>
175 #include <sys/socketvar.h>
176 #include <sys/sysctl.h>
177 #include <sys/systm.h>
179 #include <crypto/siphash/siphash.h>
181 #include <net/vnet.h>
183 #include <netinet/in.h>
184 #include <netinet/in_pcb.h>
185 #include <netinet/tcp_var.h>
186 #include <netinet/tcp_fastopen.h>
189 #define TCP_FASTOPEN_KEY_LEN SIPHASH_KEY_LENGTH
191 #if TCP_FASTOPEN_PSK_LEN != TCP_FASTOPEN_KEY_LEN
192 #error TCP_FASTOPEN_PSK_LEN must be equal to TCP_FASTOPEN_KEY_LEN
196 * Because a PSK-mode setsockopt() uses tcpcb.t_tfo_cookie.client to hold
197 * the PSK until the connect occurs.
199 #if TCP_FASTOPEN_MAX_COOKIE_LEN < TCP_FASTOPEN_PSK_LEN
200 #error TCP_FASTOPEN_MAX_COOKIE_LEN must be >= TCP_FASTOPEN_PSK_LEN
203 #define TCP_FASTOPEN_CCACHE_BUCKET_LIMIT_DEFAULT 16
204 #define TCP_FASTOPEN_CCACHE_BUCKETS_DEFAULT 2048 /* must be power of 2 */
206 #define TCP_FASTOPEN_PATH_DISABLE_TIME_DEFAULT 900 /* seconds */
208 #if !defined(TCP_RFC7413_MAX_KEYS) || (TCP_RFC7413_MAX_KEYS < 1)
209 #define TCP_FASTOPEN_MAX_KEYS 2
211 #define TCP_FASTOPEN_MAX_KEYS TCP_RFC7413_MAX_KEYS
214 #if TCP_FASTOPEN_MAX_KEYS > 10
215 #undef TCP_FASTOPEN_MAX_KEYS
216 #define TCP_FASTOPEN_MAX_KEYS 10
219 #if !defined(TCP_RFC7413_MAX_PSKS) || (TCP_RFC7413_MAX_PSKS < 1)
220 #define TCP_FASTOPEN_MAX_PSKS 2
222 #define TCP_FASTOPEN_MAX_PSKS TCP_RFC7413_MAX_PSKS
225 #if TCP_FASTOPEN_MAX_PSKS > 10
226 #undef TCP_FASTOPEN_MAX_PSKS
227 #define TCP_FASTOPEN_MAX_PSKS 10
230 struct tcp_fastopen_keylist {
232 unsigned int newest_psk;
233 uint8_t key[TCP_FASTOPEN_MAX_KEYS][TCP_FASTOPEN_KEY_LEN];
234 uint8_t psk[TCP_FASTOPEN_MAX_PSKS][TCP_FASTOPEN_KEY_LEN];
237 struct tcp_fastopen_callout {
242 static struct tcp_fastopen_ccache_entry *tcp_fastopen_ccache_lookup(
243 struct in_conninfo *, struct tcp_fastopen_ccache_bucket **);
244 static struct tcp_fastopen_ccache_entry *tcp_fastopen_ccache_create(
245 struct tcp_fastopen_ccache_bucket *, struct in_conninfo *, uint16_t, uint8_t,
247 static void tcp_fastopen_ccache_bucket_trim(struct tcp_fastopen_ccache_bucket *,
249 static void tcp_fastopen_ccache_entry_drop(struct tcp_fastopen_ccache_entry *,
250 struct tcp_fastopen_ccache_bucket *);
252 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, fastopen, CTLFLAG_RW, 0, "TCP Fast Open");
254 static VNET_DEFINE(int, tcp_fastopen_acceptany) = 0;
255 #define V_tcp_fastopen_acceptany VNET(tcp_fastopen_acceptany)
256 SYSCTL_INT(_net_inet_tcp_fastopen, OID_AUTO, acceptany,
257 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_fastopen_acceptany), 0,
258 "Accept any non-empty cookie");
260 static VNET_DEFINE(unsigned int, tcp_fastopen_autokey) = 120;
261 #define V_tcp_fastopen_autokey VNET(tcp_fastopen_autokey)
262 static int sysctl_net_inet_tcp_fastopen_autokey(SYSCTL_HANDLER_ARGS);
263 SYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, autokey,
264 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, NULL, 0,
265 &sysctl_net_inet_tcp_fastopen_autokey, "IU",
266 "Number of seconds between auto-generation of a new key; zero disables");
268 static int sysctl_net_inet_tcp_fastopen_ccache_bucket_limit(SYSCTL_HANDLER_ARGS);
269 SYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, ccache_bucket_limit,
270 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RWTUN, NULL, 0,
271 &sysctl_net_inet_tcp_fastopen_ccache_bucket_limit, "IU",
272 "Max entries per bucket in client cookie cache");
274 static VNET_DEFINE(unsigned int, tcp_fastopen_ccache_buckets) =
275 TCP_FASTOPEN_CCACHE_BUCKETS_DEFAULT;
276 #define V_tcp_fastopen_ccache_buckets VNET(tcp_fastopen_ccache_buckets)
277 SYSCTL_UINT(_net_inet_tcp_fastopen, OID_AUTO, ccache_buckets,
278 CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_fastopen_ccache_buckets), 0,
279 "Client cookie cache number of buckets (power of 2)");
281 VNET_DEFINE(unsigned int, tcp_fastopen_client_enable) = 0;
282 static int sysctl_net_inet_tcp_fastopen_client_enable(SYSCTL_HANDLER_ARGS);
283 SYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, client_enable,
284 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, NULL, 0,
285 &sysctl_net_inet_tcp_fastopen_client_enable, "IU",
286 "Enable/disable TCP Fast Open client functionality");
288 SYSCTL_INT(_net_inet_tcp_fastopen, OID_AUTO, keylen,
289 CTLFLAG_RD, SYSCTL_NULL_INT_PTR, TCP_FASTOPEN_KEY_LEN,
290 "Key length in bytes");
292 SYSCTL_INT(_net_inet_tcp_fastopen, OID_AUTO, maxkeys,
293 CTLFLAG_RD, SYSCTL_NULL_INT_PTR, TCP_FASTOPEN_MAX_KEYS,
294 "Maximum number of keys supported");
296 SYSCTL_INT(_net_inet_tcp_fastopen, OID_AUTO, maxpsks,
297 CTLFLAG_RD, SYSCTL_NULL_INT_PTR, TCP_FASTOPEN_MAX_PSKS,
298 "Maximum number of pre-shared keys supported");
300 static VNET_DEFINE(unsigned int, tcp_fastopen_numkeys) = 0;
301 #define V_tcp_fastopen_numkeys VNET(tcp_fastopen_numkeys)
302 SYSCTL_UINT(_net_inet_tcp_fastopen, OID_AUTO, numkeys,
303 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(tcp_fastopen_numkeys), 0,
304 "Number of keys installed");
306 static VNET_DEFINE(unsigned int, tcp_fastopen_numpsks) = 0;
307 #define V_tcp_fastopen_numpsks VNET(tcp_fastopen_numpsks)
308 SYSCTL_UINT(_net_inet_tcp_fastopen, OID_AUTO, numpsks,
309 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(tcp_fastopen_numpsks), 0,
310 "Number of pre-shared keys installed");
312 static VNET_DEFINE(unsigned int, tcp_fastopen_path_disable_time) =
313 TCP_FASTOPEN_PATH_DISABLE_TIME_DEFAULT;
314 #define V_tcp_fastopen_path_disable_time VNET(tcp_fastopen_path_disable_time)
315 SYSCTL_UINT(_net_inet_tcp_fastopen, OID_AUTO, path_disable_time,
316 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_fastopen_path_disable_time), 0,
317 "Seconds a TFO failure disables a {client_ip, server_ip, server_port} path");
319 static VNET_DEFINE(unsigned int, tcp_fastopen_psk_enable) = 0;
320 #define V_tcp_fastopen_psk_enable VNET(tcp_fastopen_psk_enable)
321 static int sysctl_net_inet_tcp_fastopen_psk_enable(SYSCTL_HANDLER_ARGS);
322 SYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, psk_enable,
323 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, NULL, 0,
324 &sysctl_net_inet_tcp_fastopen_psk_enable, "IU",
325 "Enable/disable TCP Fast Open server pre-shared key mode");
327 VNET_DEFINE(unsigned int, tcp_fastopen_server_enable) = 0;
328 static int sysctl_net_inet_tcp_fastopen_server_enable(SYSCTL_HANDLER_ARGS);
329 SYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, server_enable,
330 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, NULL, 0,
331 &sysctl_net_inet_tcp_fastopen_server_enable, "IU",
332 "Enable/disable TCP Fast Open server functionality");
334 static int sysctl_net_inet_tcp_fastopen_setkey(SYSCTL_HANDLER_ARGS);
335 SYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, setkey,
336 CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_WR, NULL, 0,
337 &sysctl_net_inet_tcp_fastopen_setkey, "",
338 "Install a new key");
340 static int sysctl_net_inet_tcp_fastopen_setpsk(SYSCTL_HANDLER_ARGS);
341 SYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, setpsk,
342 CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_WR, NULL, 0,
343 &sysctl_net_inet_tcp_fastopen_setpsk, "",
344 "Install a new pre-shared key");
346 static VNET_DEFINE(struct rmlock, tcp_fastopen_keylock);
347 #define V_tcp_fastopen_keylock VNET(tcp_fastopen_keylock)
349 #define TCP_FASTOPEN_KEYS_RLOCK(t) rm_rlock(&V_tcp_fastopen_keylock, (t))
350 #define TCP_FASTOPEN_KEYS_RUNLOCK(t) rm_runlock(&V_tcp_fastopen_keylock, (t))
351 #define TCP_FASTOPEN_KEYS_WLOCK() rm_wlock(&V_tcp_fastopen_keylock)
352 #define TCP_FASTOPEN_KEYS_WUNLOCK() rm_wunlock(&V_tcp_fastopen_keylock)
354 static VNET_DEFINE(struct tcp_fastopen_keylist, tcp_fastopen_keys);
355 #define V_tcp_fastopen_keys VNET(tcp_fastopen_keys)
357 static VNET_DEFINE(struct tcp_fastopen_callout, tcp_fastopen_autokey_ctx);
358 #define V_tcp_fastopen_autokey_ctx VNET(tcp_fastopen_autokey_ctx)
360 static VNET_DEFINE(uma_zone_t, counter_zone);
361 #define V_counter_zone VNET(counter_zone)
363 static MALLOC_DEFINE(M_TCP_FASTOPEN_CCACHE, "tfo_ccache", "TFO client cookie cache buckets");
365 static VNET_DEFINE(struct tcp_fastopen_ccache, tcp_fastopen_ccache);
366 #define V_tcp_fastopen_ccache VNET(tcp_fastopen_ccache)
368 #define CCB_LOCK(ccb) mtx_lock(&(ccb)->ccb_mtx)
369 #define CCB_UNLOCK(ccb) mtx_unlock(&(ccb)->ccb_mtx)
370 #define CCB_LOCK_ASSERT(ccb) mtx_assert(&(ccb)->ccb_mtx, MA_OWNED)
374 tcp_fastopen_init(void)
378 V_counter_zone = uma_zcreate("tfo", sizeof(unsigned int),
379 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
380 rm_init(&V_tcp_fastopen_keylock, "tfo_keylock");
381 callout_init_rm(&V_tcp_fastopen_autokey_ctx.c,
382 &V_tcp_fastopen_keylock, 0);
383 V_tcp_fastopen_autokey_ctx.v = curvnet;
384 V_tcp_fastopen_keys.newest = TCP_FASTOPEN_MAX_KEYS - 1;
385 V_tcp_fastopen_keys.newest_psk = TCP_FASTOPEN_MAX_PSKS - 1;
387 /* May already be non-zero if kernel tunable was set */
388 if (V_tcp_fastopen_ccache.bucket_limit == 0)
389 V_tcp_fastopen_ccache.bucket_limit =
390 TCP_FASTOPEN_CCACHE_BUCKET_LIMIT_DEFAULT;
392 /* May already be non-zero if kernel tunable was set */
393 if ((V_tcp_fastopen_ccache_buckets == 0) ||
394 !powerof2(V_tcp_fastopen_ccache_buckets))
395 V_tcp_fastopen_ccache.buckets =
396 TCP_FASTOPEN_CCACHE_BUCKETS_DEFAULT;
398 V_tcp_fastopen_ccache.buckets = V_tcp_fastopen_ccache_buckets;
400 V_tcp_fastopen_ccache.mask = V_tcp_fastopen_ccache.buckets - 1;
401 V_tcp_fastopen_ccache.secret = arc4random();
403 V_tcp_fastopen_ccache.base = malloc(V_tcp_fastopen_ccache.buckets *
404 sizeof(struct tcp_fastopen_ccache_bucket), M_TCP_FASTOPEN_CCACHE,
407 for (i = 0; i < V_tcp_fastopen_ccache.buckets; i++) {
408 TAILQ_INIT(&V_tcp_fastopen_ccache.base[i].ccb_entries);
409 mtx_init(&V_tcp_fastopen_ccache.base[i].ccb_mtx, "tfo_ccache_bucket",
411 V_tcp_fastopen_ccache.base[i].ccb_num_entries = -1; /* bucket disabled */
412 V_tcp_fastopen_ccache.base[i].ccb_ccache = &V_tcp_fastopen_ccache;
416 * Note that while the total number of entries in the cookie cache
417 * is limited by the table management logic to
418 * V_tcp_fastopen_ccache.buckets *
419 * V_tcp_fastopen_ccache.bucket_limit, the total number of items in
420 * this zone can exceed that amount by the number of CPUs in the
421 * system times the maximum number of unallocated items that can be
422 * present in each UMA per-CPU cache for this zone.
424 V_tcp_fastopen_ccache.zone = uma_zcreate("tfo_ccache_entries",
425 sizeof(struct tcp_fastopen_ccache_entry), NULL, NULL, NULL, NULL,
430 tcp_fastopen_destroy(void)
432 struct tcp_fastopen_ccache_bucket *ccb;
435 for (i = 0; i < V_tcp_fastopen_ccache.buckets; i++) {
436 ccb = &V_tcp_fastopen_ccache.base[i];
437 tcp_fastopen_ccache_bucket_trim(ccb, 0);
438 mtx_destroy(&ccb->ccb_mtx);
441 KASSERT(uma_zone_get_cur(V_tcp_fastopen_ccache.zone) == 0,
442 ("%s: TFO ccache zone allocation count not 0", __func__));
443 uma_zdestroy(V_tcp_fastopen_ccache.zone);
444 free(V_tcp_fastopen_ccache.base, M_TCP_FASTOPEN_CCACHE);
446 callout_drain(&V_tcp_fastopen_autokey_ctx.c);
447 rm_destroy(&V_tcp_fastopen_keylock);
448 uma_zdestroy(V_counter_zone);
452 tcp_fastopen_alloc_counter(void)
454 unsigned int *counter;
455 counter = uma_zalloc(V_counter_zone, M_NOWAIT);
462 tcp_fastopen_decrement_counter(unsigned int *counter)
465 uma_zfree(V_counter_zone, counter);
467 atomic_subtract_int(counter, 1);
471 tcp_fastopen_addkey_locked(uint8_t *key)
474 V_tcp_fastopen_keys.newest++;
475 if (V_tcp_fastopen_keys.newest == TCP_FASTOPEN_MAX_KEYS)
476 V_tcp_fastopen_keys.newest = 0;
477 memcpy(V_tcp_fastopen_keys.key[V_tcp_fastopen_keys.newest], key,
478 TCP_FASTOPEN_KEY_LEN);
479 if (V_tcp_fastopen_numkeys < TCP_FASTOPEN_MAX_KEYS)
480 V_tcp_fastopen_numkeys++;
484 tcp_fastopen_addpsk_locked(uint8_t *psk)
487 V_tcp_fastopen_keys.newest_psk++;
488 if (V_tcp_fastopen_keys.newest_psk == TCP_FASTOPEN_MAX_PSKS)
489 V_tcp_fastopen_keys.newest_psk = 0;
490 memcpy(V_tcp_fastopen_keys.psk[V_tcp_fastopen_keys.newest_psk], psk,
491 TCP_FASTOPEN_KEY_LEN);
492 if (V_tcp_fastopen_numpsks < TCP_FASTOPEN_MAX_PSKS)
493 V_tcp_fastopen_numpsks++;
497 tcp_fastopen_autokey_locked(void)
499 uint8_t newkey[TCP_FASTOPEN_KEY_LEN];
501 arc4rand(newkey, TCP_FASTOPEN_KEY_LEN, 0);
502 tcp_fastopen_addkey_locked(newkey);
506 tcp_fastopen_autokey_callout(void *arg)
508 struct tcp_fastopen_callout *ctx = arg;
511 tcp_fastopen_autokey_locked();
512 callout_reset(&ctx->c, V_tcp_fastopen_autokey * hz,
513 tcp_fastopen_autokey_callout, ctx);
519 tcp_fastopen_make_cookie(uint8_t key[SIPHASH_KEY_LENGTH], struct in_conninfo *inc)
524 SipHash24_Init(&ctx);
525 SipHash_SetKey(&ctx, key);
526 switch (inc->inc_flags & INC_ISIPV6) {
529 SipHash_Update(&ctx, &inc->inc_faddr, sizeof(inc->inc_faddr));
534 SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(inc->inc6_faddr));
538 SipHash_Final((u_int8_t *)&siphash, &ctx);
544 tcp_fastopen_make_psk_cookie(uint8_t *psk, uint8_t *cookie, uint8_t cookie_len)
549 SipHash24_Init(&ctx);
550 SipHash_SetKey(&ctx, psk);
551 SipHash_Update(&ctx, cookie, cookie_len);
552 SipHash_Final((u_int8_t *)&psk_cookie, &ctx);
558 tcp_fastopen_find_cookie_match_locked(uint8_t *wire_cookie, uint64_t *cur_cookie)
560 unsigned int i, psk_index;
563 if (V_tcp_fastopen_psk_enable) {
564 psk_index = V_tcp_fastopen_keys.newest_psk;
565 for (i = 0; i < V_tcp_fastopen_numpsks; i++) {
567 tcp_fastopen_make_psk_cookie(
568 V_tcp_fastopen_keys.psk[psk_index],
569 (uint8_t *)cur_cookie,
570 TCP_FASTOPEN_COOKIE_LEN);
572 if (memcmp(wire_cookie, &psk_cookie,
573 TCP_FASTOPEN_COOKIE_LEN) == 0)
577 psk_index = TCP_FASTOPEN_MAX_PSKS - 1;
581 } else if (memcmp(wire_cookie, cur_cookie, TCP_FASTOPEN_COOKIE_LEN) == 0)
589 * -1 the cookie is invalid and no valid cookie is available
590 * 0 the cookie is invalid and the latest cookie has been returned
591 * 1 the cookie is valid and the latest cookie has been returned
594 tcp_fastopen_check_cookie(struct in_conninfo *inc, uint8_t *cookie,
595 unsigned int len, uint64_t *latest_cookie)
597 struct rm_priotracker tracker;
598 unsigned int i, key_index;
602 if (V_tcp_fastopen_acceptany) {
607 TCP_FASTOPEN_KEYS_RLOCK(&tracker);
608 if (len != TCP_FASTOPEN_COOKIE_LEN) {
609 if (V_tcp_fastopen_numkeys > 0) {
611 tcp_fastopen_make_cookie(
612 V_tcp_fastopen_keys.key[V_tcp_fastopen_keys.newest],
621 * Check against each available key, from newest to oldest.
623 key_index = V_tcp_fastopen_keys.newest;
624 for (i = 0; i < V_tcp_fastopen_numkeys; i++) {
626 tcp_fastopen_make_cookie(V_tcp_fastopen_keys.key[key_index],
629 *latest_cookie = cur_cookie;
630 rv = tcp_fastopen_find_cookie_match_locked(cookie, &cur_cookie);
634 key_index = TCP_FASTOPEN_MAX_KEYS - 1;
641 TCP_FASTOPEN_KEYS_RUNLOCK(&tracker);
646 sysctl_net_inet_tcp_fastopen_autokey(SYSCTL_HANDLER_ARGS)
651 new = V_tcp_fastopen_autokey;
652 error = sysctl_handle_int(oidp, &new, 0, req);
653 if (error == 0 && req->newptr) {
654 if (new > (INT_MAX / hz))
657 TCP_FASTOPEN_KEYS_WLOCK();
658 if (V_tcp_fastopen_server_enable) {
659 if (V_tcp_fastopen_autokey && !new)
660 callout_stop(&V_tcp_fastopen_autokey_ctx.c);
662 callout_reset(&V_tcp_fastopen_autokey_ctx.c,
663 new * hz, tcp_fastopen_autokey_callout,
664 &V_tcp_fastopen_autokey_ctx);
666 V_tcp_fastopen_autokey = new;
667 TCP_FASTOPEN_KEYS_WUNLOCK();
674 sysctl_net_inet_tcp_fastopen_psk_enable(SYSCTL_HANDLER_ARGS)
679 new = V_tcp_fastopen_psk_enable;
680 error = sysctl_handle_int(oidp, &new, 0, req);
681 if (error == 0 && req->newptr) {
682 if (V_tcp_fastopen_psk_enable && !new) {
683 /* enabled -> disabled */
684 TCP_FASTOPEN_KEYS_WLOCK();
685 V_tcp_fastopen_numpsks = 0;
686 V_tcp_fastopen_keys.newest_psk =
687 TCP_FASTOPEN_MAX_PSKS - 1;
688 V_tcp_fastopen_psk_enable = 0;
689 TCP_FASTOPEN_KEYS_WUNLOCK();
690 } else if (!V_tcp_fastopen_psk_enable && new) {
691 /* disabled -> enabled */
692 TCP_FASTOPEN_KEYS_WLOCK();
693 V_tcp_fastopen_psk_enable = 1;
694 TCP_FASTOPEN_KEYS_WUNLOCK();
701 sysctl_net_inet_tcp_fastopen_server_enable(SYSCTL_HANDLER_ARGS)
706 new = V_tcp_fastopen_server_enable;
707 error = sysctl_handle_int(oidp, &new, 0, req);
708 if (error == 0 && req->newptr) {
709 if (V_tcp_fastopen_server_enable && !new) {
710 /* enabled -> disabled */
711 TCP_FASTOPEN_KEYS_WLOCK();
712 V_tcp_fastopen_numkeys = 0;
713 V_tcp_fastopen_keys.newest = TCP_FASTOPEN_MAX_KEYS - 1;
714 if (V_tcp_fastopen_autokey)
715 callout_stop(&V_tcp_fastopen_autokey_ctx.c);
716 V_tcp_fastopen_numpsks = 0;
717 V_tcp_fastopen_keys.newest_psk =
718 TCP_FASTOPEN_MAX_PSKS - 1;
719 V_tcp_fastopen_server_enable = 0;
720 TCP_FASTOPEN_KEYS_WUNLOCK();
721 } else if (!V_tcp_fastopen_server_enable && new) {
722 /* disabled -> enabled */
723 TCP_FASTOPEN_KEYS_WLOCK();
724 if (V_tcp_fastopen_autokey &&
725 (V_tcp_fastopen_numkeys == 0)) {
726 tcp_fastopen_autokey_locked();
727 callout_reset(&V_tcp_fastopen_autokey_ctx.c,
728 V_tcp_fastopen_autokey * hz,
729 tcp_fastopen_autokey_callout,
730 &V_tcp_fastopen_autokey_ctx);
732 V_tcp_fastopen_server_enable = 1;
733 TCP_FASTOPEN_KEYS_WUNLOCK();
740 sysctl_net_inet_tcp_fastopen_setkey(SYSCTL_HANDLER_ARGS)
743 uint8_t newkey[TCP_FASTOPEN_KEY_LEN];
745 if (req->oldptr != NULL || req->oldlen != 0)
747 if (req->newptr == NULL)
749 if (req->newlen != sizeof(newkey))
751 error = SYSCTL_IN(req, newkey, sizeof(newkey));
755 TCP_FASTOPEN_KEYS_WLOCK();
756 tcp_fastopen_addkey_locked(newkey);
757 TCP_FASTOPEN_KEYS_WUNLOCK();
763 sysctl_net_inet_tcp_fastopen_setpsk(SYSCTL_HANDLER_ARGS)
766 uint8_t newpsk[TCP_FASTOPEN_KEY_LEN];
768 if (req->oldptr != NULL || req->oldlen != 0)
770 if (req->newptr == NULL)
772 if (req->newlen != sizeof(newpsk))
774 error = SYSCTL_IN(req, newpsk, sizeof(newpsk));
778 TCP_FASTOPEN_KEYS_WLOCK();
779 tcp_fastopen_addpsk_locked(newpsk);
780 TCP_FASTOPEN_KEYS_WUNLOCK();
786 sysctl_net_inet_tcp_fastopen_ccache_bucket_limit(SYSCTL_HANDLER_ARGS)
788 struct tcp_fastopen_ccache_bucket *ccb;
793 new = V_tcp_fastopen_ccache.bucket_limit;
794 error = sysctl_handle_int(oidp, &new, 0, req);
795 if (error == 0 && req->newptr) {
796 if ((new == 0) || (new > INT_MAX))
799 if (new < V_tcp_fastopen_ccache.bucket_limit) {
800 for (i = 0; i < V_tcp_fastopen_ccache.buckets;
802 ccb = &V_tcp_fastopen_ccache.base[i];
803 tcp_fastopen_ccache_bucket_trim(ccb, new);
806 V_tcp_fastopen_ccache.bucket_limit = new;
814 sysctl_net_inet_tcp_fastopen_client_enable(SYSCTL_HANDLER_ARGS)
816 struct tcp_fastopen_ccache_bucket *ccb;
820 new = V_tcp_fastopen_client_enable;
821 error = sysctl_handle_int(oidp, &new, 0, req);
822 if (error == 0 && req->newptr) {
823 if (V_tcp_fastopen_client_enable && !new) {
824 /* enabled -> disabled */
825 for (i = 0; i < V_tcp_fastopen_ccache.buckets; i++) {
826 ccb = &V_tcp_fastopen_ccache.base[i];
827 tcp_fastopen_ccache_bucket_trim(ccb, 0);
829 V_tcp_fastopen_client_enable = 0;
830 } else if (!V_tcp_fastopen_client_enable && new) {
831 /* disabled -> enabled */
832 for (i = 0; i < V_tcp_fastopen_ccache.buckets; i++) {
833 ccb = &V_tcp_fastopen_ccache.base[i];
835 KASSERT(TAILQ_EMPTY(&ccb->ccb_entries),
836 ("%s: ccb->ccb_entries not empty", __func__));
837 KASSERT(ccb->ccb_num_entries == -1,
838 ("%s: ccb->ccb_num_entries %d not -1", __func__,
839 ccb->ccb_num_entries));
840 ccb->ccb_num_entries = 0; /* enable bucket */
843 V_tcp_fastopen_client_enable = 1;
850 tcp_fastopen_connect(struct tcpcb *tp)
853 struct tcp_fastopen_ccache_bucket *ccb;
854 struct tcp_fastopen_ccache_entry *cce;
861 cce = tcp_fastopen_ccache_lookup(&inp->inp_inc, &ccb);
863 if (cce->disable_time == 0) {
864 if ((cce->cookie_len > 0) &&
865 (tp->t_tfo_client_cookie_len ==
866 TCP_FASTOPEN_PSK_LEN)) {
868 tcp_fastopen_make_psk_cookie(
869 tp->t_tfo_cookie.client,
870 cce->cookie, cce->cookie_len);
872 tp->t_tfo_client_cookie_len = cce->cookie_len;
873 memcpy(tp->t_tfo_cookie.client, cce->cookie,
876 server_mss = cce->server_mss;
878 if (tp->t_tfo_client_cookie_len ==
879 TCP_FASTOPEN_PSK_LEN && psk_cookie) {
880 tp->t_tfo_client_cookie_len =
881 TCP_FASTOPEN_COOKIE_LEN;
882 memcpy(tp->t_tfo_cookie.client, &psk_cookie,
883 TCP_FASTOPEN_COOKIE_LEN);
885 tcp_mss(tp, server_mss ? server_mss : -1);
886 tp->snd_wnd = tp->t_maxseg;
889 * The path is disabled. Check the time and
890 * possibly re-enable.
892 now = getsbinuptime();
893 if (now - cce->disable_time >
894 ((sbintime_t)V_tcp_fastopen_path_disable_time << 32)) {
896 * Re-enable path. Force a TFO cookie
897 * request. Forget the old MSS as it may be
898 * bogus now, and we will rediscover it in
901 cce->disable_time = 0;
905 * tp->t_tfo... cookie details are already
906 * zero from the tcpcb init.
910 * Path is disabled, so disable TFO on this
913 tp->t_flags &= ~TF_FASTOPEN;
918 * snd_wnd is irrelevant since we are either forcing
919 * a TFO cookie request or disabling TFO - either
920 * way, no data with the SYN.
925 * A new entry for this path will be created when a SYN|ACK
926 * comes back, or the attempt otherwise fails.
931 * snd_wnd is irrelevant since we are forcing a TFO cookie
938 tcp_fastopen_disable_path(struct tcpcb *tp)
940 struct in_conninfo *inc = &tp->t_inpcb->inp_inc;
941 struct tcp_fastopen_ccache_bucket *ccb;
942 struct tcp_fastopen_ccache_entry *cce;
944 cce = tcp_fastopen_ccache_lookup(inc, &ccb);
949 * Preserve the existing disable time if it is already
952 if (cce->disable_time == 0)
953 cce->disable_time = getsbinuptime();
954 } else /* use invalid cookie len to create disabled entry */
955 tcp_fastopen_ccache_create(ccb, inc, 0,
956 TCP_FASTOPEN_MAX_COOKIE_LEN + 1, NULL);
959 tp->t_flags &= ~TF_FASTOPEN;
963 tcp_fastopen_update_cache(struct tcpcb *tp, uint16_t mss,
964 uint8_t cookie_len, uint8_t *cookie)
966 struct in_conninfo *inc = &tp->t_inpcb->inp_inc;
967 struct tcp_fastopen_ccache_bucket *ccb;
968 struct tcp_fastopen_ccache_entry *cce;
970 cce = tcp_fastopen_ccache_lookup(inc, &ccb);
972 if ((cookie_len >= TCP_FASTOPEN_MIN_COOKIE_LEN) &&
973 (cookie_len <= TCP_FASTOPEN_MAX_COOKIE_LEN) &&
974 ((cookie_len & 0x1) == 0)) {
975 cce->server_mss = mss;
976 cce->cookie_len = cookie_len;
977 memcpy(cce->cookie, cookie, cookie_len);
978 cce->disable_time = 0;
980 /* invalid cookie length, disable entry */
984 * Preserve the existing disable time if it is
987 if (cce->disable_time == 0)
988 cce->disable_time = getsbinuptime();
991 tcp_fastopen_ccache_create(ccb, inc, mss, cookie_len, cookie);
996 static struct tcp_fastopen_ccache_entry *
997 tcp_fastopen_ccache_lookup(struct in_conninfo *inc,
998 struct tcp_fastopen_ccache_bucket **ccbp)
1000 struct tcp_fastopen_ccache_bucket *ccb;
1001 struct tcp_fastopen_ccache_entry *cce;
1005 hash = jenkins_hash32((uint32_t *)&inc->inc_ie.ie_dependladdr, 4,
1006 V_tcp_fastopen_ccache.secret);
1007 hash = jenkins_hash32((uint32_t *)&inc->inc_ie.ie_dependfaddr, 4,
1009 last_word = inc->inc_fport;
1010 hash = jenkins_hash32(&last_word, 1, hash);
1011 ccb = &V_tcp_fastopen_ccache.base[hash & V_tcp_fastopen_ccache.mask];
1016 * Always returns with locked bucket.
1018 TAILQ_FOREACH(cce, &ccb->ccb_entries, cce_link)
1019 if ((!(cce->af == AF_INET6) == !(inc->inc_flags & INC_ISIPV6)) &&
1020 (cce->server_port == inc->inc_ie.ie_fport) &&
1021 (((cce->af == AF_INET) &&
1022 (cce->cce_client_ip.v4.s_addr == inc->inc_laddr.s_addr) &&
1023 (cce->cce_server_ip.v4.s_addr == inc->inc_faddr.s_addr)) ||
1024 ((cce->af == AF_INET6) &&
1025 IN6_ARE_ADDR_EQUAL(&cce->cce_client_ip.v6, &inc->inc6_laddr) &&
1026 IN6_ARE_ADDR_EQUAL(&cce->cce_server_ip.v6, &inc->inc6_faddr))))
1032 static struct tcp_fastopen_ccache_entry *
1033 tcp_fastopen_ccache_create(struct tcp_fastopen_ccache_bucket *ccb,
1034 struct in_conninfo *inc, uint16_t mss, uint8_t cookie_len, uint8_t *cookie)
1036 struct tcp_fastopen_ccache_entry *cce;
1039 * 1. Create a new entry, or
1040 * 2. Reclaim an existing entry, or
1044 CCB_LOCK_ASSERT(ccb);
1047 if (ccb->ccb_num_entries < V_tcp_fastopen_ccache.bucket_limit)
1048 cce = uma_zalloc(V_tcp_fastopen_ccache.zone, M_NOWAIT);
1052 * At bucket limit, or out of memory - reclaim last
1055 cce = TAILQ_LAST(&ccb->ccb_entries, bucket_entries);
1057 /* XXX count this event */
1061 TAILQ_REMOVE(&ccb->ccb_entries, cce, cce_link);
1063 ccb->ccb_num_entries++;
1065 TAILQ_INSERT_HEAD(&ccb->ccb_entries, cce, cce_link);
1066 cce->af = (inc->inc_flags & INC_ISIPV6) ? AF_INET6 : AF_INET;
1067 if (cce->af == AF_INET) {
1068 cce->cce_client_ip.v4 = inc->inc_laddr;
1069 cce->cce_server_ip.v4 = inc->inc_faddr;
1071 cce->cce_client_ip.v6 = inc->inc6_laddr;
1072 cce->cce_server_ip.v6 = inc->inc6_faddr;
1074 cce->server_port = inc->inc_fport;
1075 if ((cookie_len >= TCP_FASTOPEN_MIN_COOKIE_LEN) &&
1076 (cookie_len <= TCP_FASTOPEN_MAX_COOKIE_LEN) &&
1077 ((cookie_len & 0x1) == 0)) {
1078 cce->server_mss = mss;
1079 cce->cookie_len = cookie_len;
1080 memcpy(cce->cookie, cookie, cookie_len);
1081 cce->disable_time = 0;
1083 /* invalid cookie length, disable cce */
1084 cce->server_mss = 0;
1085 cce->cookie_len = 0;
1086 cce->disable_time = getsbinuptime();
1093 tcp_fastopen_ccache_bucket_trim(struct tcp_fastopen_ccache_bucket *ccb,
1096 struct tcp_fastopen_ccache_entry *cce, *cce_tmp;
1097 unsigned int entries;
1101 TAILQ_FOREACH_SAFE(cce, &ccb->ccb_entries, cce_link, cce_tmp) {
1103 if (entries > limit)
1104 tcp_fastopen_ccache_entry_drop(cce, ccb);
1106 KASSERT(ccb->ccb_num_entries <= (int)limit,
1107 ("%s: ccb->ccb_num_entries %d exceeds limit %d", __func__,
1108 ccb->ccb_num_entries, limit));
1110 KASSERT(TAILQ_EMPTY(&ccb->ccb_entries),
1111 ("%s: ccb->ccb_entries not empty", __func__));
1112 ccb->ccb_num_entries = -1; /* disable bucket */
1118 tcp_fastopen_ccache_entry_drop(struct tcp_fastopen_ccache_entry *cce,
1119 struct tcp_fastopen_ccache_bucket *ccb)
1122 CCB_LOCK_ASSERT(ccb);
1124 TAILQ_REMOVE(&ccb->ccb_entries, cce, cce_link);
1125 ccb->ccb_num_entries--;
1126 uma_zfree(V_tcp_fastopen_ccache.zone, cce);