1292706Spkelsey/*-
2292706Spkelsey * Copyright (c) 2015 Patrick Kelsey
3292706Spkelsey * All rights reserved.
4292706Spkelsey *
5292706Spkelsey * Redistribution and use in source and binary forms, with or without
6292706Spkelsey * modification, are permitted provided that the following conditions
7292706Spkelsey * are met:
8292706Spkelsey * 1. Redistributions of source code must retain the above copyright
9292706Spkelsey *    notice, this list of conditions and the following disclaimer.
10292706Spkelsey * 2. Redistributions in binary form must reproduce the above copyright
11292706Spkelsey *    notice, this list of conditions and the following disclaimer in the
12292706Spkelsey *    documentation and/or other materials provided with the distribution.
13292706Spkelsey *
14292706Spkelsey * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15292706Spkelsey * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16292706Spkelsey * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17292706Spkelsey * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18292706Spkelsey * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19292706Spkelsey * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20292706Spkelsey * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21292706Spkelsey * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22292706Spkelsey * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23292706Spkelsey * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24292706Spkelsey * SUCH DAMAGE.
25292706Spkelsey */
26292706Spkelsey
27292706Spkelsey/*
28292706Spkelsey * This is a server-side implementation of TCP Fast Open (TFO) [RFC7413].
29292706Spkelsey *
30292706Spkelsey * This implementation is currently considered to be experimental and is not
31292706Spkelsey * included in kernel builds by default.  To include this code, add the
32292706Spkelsey * following line to your kernel config:
33292706Spkelsey *
34292706Spkelsey * options TCP_RFC7413
35292706Spkelsey *
36292706Spkelsey * The generated TFO cookies are the 64-bit output of
37292706Spkelsey * SipHash24(<16-byte-key><client-ip>).  Multiple concurrent valid keys are
38292706Spkelsey * supported so that time-based rolling cookie invalidation policies can be
39292706Spkelsey * implemented in the system.  The default number of concurrent keys is 2.
40292706Spkelsey * This can be adjusted in the kernel config as follows:
41292706Spkelsey *
42292706Spkelsey * options TCP_RFC7413_MAX_KEYS=<num-keys>
43292706Spkelsey *
44292706Spkelsey *
45292706Spkelsey * The following TFO-specific sysctls are defined:
46292706Spkelsey *
47292706Spkelsey * net.inet.tcp.fastopen.acceptany (RW, default 0)
48292706Spkelsey *     When non-zero, all client-supplied TFO cookies will be considered to
49292706Spkelsey *     be valid.
50292706Spkelsey *
51292706Spkelsey * net.inet.tcp.fastopen.autokey (RW, default 120)
52292706Spkelsey *     When this and net.inet.tcp.fastopen.enabled are non-zero, a new key
53292706Spkelsey *     will be automatically generated after this many seconds.
54292706Spkelsey *
55292706Spkelsey * net.inet.tcp.fastopen.enabled (RW, default 0)
56292706Spkelsey *     When zero, no new TFO connections can be created.  On the transition
57292706Spkelsey *     from enabled to disabled, all installed keys are removed.  On the
58292706Spkelsey *     transition from disabled to enabled, if net.inet.tcp.fastopen.autokey
59292706Spkelsey *     is non-zero and there are no keys installed, a new key will be
60292706Spkelsey *     generated immediately.  The transition from enabled to disabled does
61292706Spkelsey *     not affect any TFO connections in progress; it only prevents new ones
62292706Spkelsey *     from being made.
63292706Spkelsey *
64292706Spkelsey * net.inet.tcp.fastopen.keylen (RO)
65292706Spkelsey *     The key length in bytes.
66292706Spkelsey *
67292706Spkelsey * net.inet.tcp.fastopen.maxkeys (RO)
68292706Spkelsey *     The maximum number of keys supported.
69292706Spkelsey *
70292706Spkelsey * net.inet.tcp.fastopen.numkeys (RO)
71292706Spkelsey *     The current number of keys installed.
72292706Spkelsey *
73292706Spkelsey * net.inet.tcp.fastopen.setkey (WO)
74292706Spkelsey *     Install a new key by writing net.inet.tcp.fastopen.keylen bytes to this
75292706Spkelsey *     sysctl.
76292706Spkelsey *
77292706Spkelsey *
78292706Spkelsey * In order for TFO connections to be created via a listen socket, that
79292706Spkelsey * socket must have the TCP_FASTOPEN socket option set on it.  This option
80292706Spkelsey * can be set on the socket either before or after the listen() is invoked.
81292706Spkelsey * Clearing this option on a listen socket after it has been set has no
82292706Spkelsey * effect on existing TFO connections or TFO connections in progress; it
83292706Spkelsey * only prevents new TFO connections from being made.
84292706Spkelsey *
85292706Spkelsey * For passively-created sockets, the TCP_FASTOPEN socket option can be
86292706Spkelsey * queried to determine whether the connection was established using TFO.
87292706Spkelsey * Note that connections that are established via a TFO SYN, but that fall
88292706Spkelsey * back to using a non-TFO SYN|ACK will have the TCP_FASTOPEN socket option
89292706Spkelsey * set.
90292706Spkelsey *
91292706Spkelsey * Per the RFC, this implementation limits the number of TFO connections
92292706Spkelsey * that can be in the SYN_RECEIVED state on a per listen-socket basis.
93292706Spkelsey * Whenever this limit is exceeded, requests for new TFO connections are
94292706Spkelsey * serviced as non-TFO requests.  Without such a limit, given a valid TFO
95292706Spkelsey * cookie, an attacker could keep the listen queue in an overflow condition
96292706Spkelsey * using a TFO SYN flood.  This implementation sets the limit at half the
97292706Spkelsey * configured listen backlog.
98292706Spkelsey *
99292706Spkelsey */
100292706Spkelsey
101292706Spkelsey#include <sys/cdefs.h>
102292706Spkelsey__FBSDID("$FreeBSD$");
103292706Spkelsey
104292706Spkelsey#include "opt_inet.h"
105292706Spkelsey
106292706Spkelsey#include <sys/param.h>
107292706Spkelsey#include <sys/kernel.h>
108292706Spkelsey#include <sys/limits.h>
109292706Spkelsey#include <sys/lock.h>
110292706Spkelsey#include <sys/rmlock.h>
111292706Spkelsey#include <sys/socketvar.h>
112292706Spkelsey#include <sys/sysctl.h>
113292706Spkelsey#include <sys/systm.h>
114292706Spkelsey
115292706Spkelsey#include <crypto/siphash/siphash.h>
116292706Spkelsey
117292706Spkelsey#include <net/vnet.h>
118292706Spkelsey
119292706Spkelsey#include <netinet/in.h>
120292706Spkelsey#include <netinet/in_pcb.h>
121292706Spkelsey#include <netinet/tcp_fastopen.h>
122292706Spkelsey#include <netinet/tcp_var.h>
123292706Spkelsey
124292706Spkelsey
125292706Spkelsey#define	TCP_FASTOPEN_KEY_LEN	SIPHASH_KEY_LENGTH
126292706Spkelsey
127292706Spkelsey#if !defined(TCP_RFC7413_MAX_KEYS) || (TCP_RFC7413_MAX_KEYS < 1)
128292706Spkelsey#define	TCP_FASTOPEN_MAX_KEYS	2
129292706Spkelsey#else
130292706Spkelsey#define	TCP_FASTOPEN_MAX_KEYS	TCP_RFC7413_MAX_KEYS
131292706Spkelsey#endif
132292706Spkelsey
133292706Spkelseystruct tcp_fastopen_keylist {
134292706Spkelsey	unsigned int newest;
135292706Spkelsey	uint8_t key[TCP_FASTOPEN_MAX_KEYS][TCP_FASTOPEN_KEY_LEN];
136292706Spkelsey};
137292706Spkelsey
138292706Spkelseystruct tcp_fastopen_callout {
139292706Spkelsey	struct callout c;
140292706Spkelsey	struct vnet *v;
141292706Spkelsey};
142292706Spkelsey
143292706SpkelseySYSCTL_NODE(_net_inet_tcp, OID_AUTO, fastopen, CTLFLAG_RW, 0, "TCP Fast Open");
144292706Spkelsey
145292706Spkelseystatic VNET_DEFINE(int, tcp_fastopen_acceptany) = 0;
146292706Spkelsey#define	V_tcp_fastopen_acceptany	VNET(tcp_fastopen_acceptany)
147292706SpkelseySYSCTL_INT(_net_inet_tcp_fastopen, OID_AUTO, acceptany,
148292706Spkelsey    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_fastopen_acceptany), 0,
149292706Spkelsey    "Accept any non-empty cookie");
150292706Spkelsey
151292706Spkelseystatic VNET_DEFINE(unsigned int, tcp_fastopen_autokey) = 120;
152292706Spkelsey#define	V_tcp_fastopen_autokey	VNET(tcp_fastopen_autokey)
153292706Spkelseystatic int sysctl_net_inet_tcp_fastopen_autokey(SYSCTL_HANDLER_ARGS);
154292706SpkelseySYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, autokey,
155292706Spkelsey    CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, NULL, 0,
156292706Spkelsey    &sysctl_net_inet_tcp_fastopen_autokey, "IU",
157292706Spkelsey    "Number of seconds between auto-generation of a new key; zero disables");
158292706Spkelsey
159292706SpkelseyVNET_DEFINE(unsigned int, tcp_fastopen_enabled) = 0;
160292706Spkelseystatic int sysctl_net_inet_tcp_fastopen_enabled(SYSCTL_HANDLER_ARGS);
161292706SpkelseySYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, enabled,
162292706Spkelsey    CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, NULL, 0,
163292706Spkelsey    &sysctl_net_inet_tcp_fastopen_enabled, "IU",
164292706Spkelsey    "Enable/disable TCP Fast Open processing");
165292706Spkelsey
166292706SpkelseySYSCTL_INT(_net_inet_tcp_fastopen, OID_AUTO, keylen,
167292706Spkelsey    CTLFLAG_RD, SYSCTL_NULL_INT_PTR, TCP_FASTOPEN_KEY_LEN,
168292706Spkelsey    "Key length in bytes");
169292706Spkelsey
170292706SpkelseySYSCTL_INT(_net_inet_tcp_fastopen, OID_AUTO, maxkeys,
171292706Spkelsey    CTLFLAG_RD, SYSCTL_NULL_INT_PTR, TCP_FASTOPEN_MAX_KEYS,
172292706Spkelsey    "Maximum number of keys supported");
173292706Spkelsey
174292706Spkelseystatic VNET_DEFINE(unsigned int, tcp_fastopen_numkeys) = 0;
175292706Spkelsey#define	V_tcp_fastopen_numkeys	VNET(tcp_fastopen_numkeys)
176292706SpkelseySYSCTL_UINT(_net_inet_tcp_fastopen, OID_AUTO, numkeys,
177292706Spkelsey    CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(tcp_fastopen_numkeys), 0,
178292706Spkelsey    "Number of keys installed");
179292706Spkelsey
180292706Spkelseystatic int sysctl_net_inet_tcp_fastopen_setkey(SYSCTL_HANDLER_ARGS);
181292706SpkelseySYSCTL_PROC(_net_inet_tcp_fastopen, OID_AUTO, setkey,
182292706Spkelsey    CTLFLAG_VNET | CTLTYPE_OPAQUE | CTLFLAG_WR, NULL, 0,
183292706Spkelsey    &sysctl_net_inet_tcp_fastopen_setkey, "",
184292706Spkelsey    "Install a new key");
185292706Spkelsey
186292706Spkelseystatic VNET_DEFINE(struct rmlock, tcp_fastopen_keylock);
187292706Spkelsey#define	V_tcp_fastopen_keylock	VNET(tcp_fastopen_keylock)
188292706Spkelsey
189292706Spkelsey#define TCP_FASTOPEN_KEYS_RLOCK(t)	rm_rlock(&V_tcp_fastopen_keylock, (t))
190292706Spkelsey#define TCP_FASTOPEN_KEYS_RUNLOCK(t)	rm_runlock(&V_tcp_fastopen_keylock, (t))
191292706Spkelsey#define TCP_FASTOPEN_KEYS_WLOCK()	rm_wlock(&V_tcp_fastopen_keylock)
192292706Spkelsey#define TCP_FASTOPEN_KEYS_WUNLOCK()	rm_wunlock(&V_tcp_fastopen_keylock)
193292706Spkelsey
194292706Spkelseystatic VNET_DEFINE(struct tcp_fastopen_keylist, tcp_fastopen_keys);
195292706Spkelsey#define V_tcp_fastopen_keys	VNET(tcp_fastopen_keys)
196292706Spkelsey
197292706Spkelseystatic VNET_DEFINE(struct tcp_fastopen_callout, tcp_fastopen_autokey_ctx);
198292706Spkelsey#define V_tcp_fastopen_autokey_ctx	VNET(tcp_fastopen_autokey_ctx)
199292706Spkelsey
200292706Spkelseystatic VNET_DEFINE(uma_zone_t, counter_zone);
201292706Spkelsey#define	V_counter_zone			VNET(counter_zone)
202292706Spkelsey
203292706Spkelseyvoid
204292706Spkelseytcp_fastopen_init(void)
205292706Spkelsey{
206292706Spkelsey	V_counter_zone = uma_zcreate("tfo", sizeof(unsigned int),
207292706Spkelsey	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
208292706Spkelsey	rm_init(&V_tcp_fastopen_keylock, "tfo_keylock");
209292706Spkelsey	callout_init_rm(&V_tcp_fastopen_autokey_ctx.c,
210292706Spkelsey	    &V_tcp_fastopen_keylock, 0);
211292706Spkelsey	V_tcp_fastopen_keys.newest = TCP_FASTOPEN_MAX_KEYS - 1;
212292706Spkelsey}
213292706Spkelsey
214292706Spkelseyvoid
215292706Spkelseytcp_fastopen_destroy(void)
216292706Spkelsey{
217292706Spkelsey	callout_drain(&V_tcp_fastopen_autokey_ctx.c);
218292706Spkelsey	rm_destroy(&V_tcp_fastopen_keylock);
219292706Spkelsey	uma_zdestroy(V_counter_zone);
220292706Spkelsey}
221292706Spkelsey
222292706Spkelseyunsigned int *
223292706Spkelseytcp_fastopen_alloc_counter(void)
224292706Spkelsey{
225292706Spkelsey	unsigned int *counter;
226292706Spkelsey	counter = uma_zalloc(V_counter_zone, M_NOWAIT);
227292706Spkelsey	if (counter)
228292706Spkelsey		*counter = 1;
229292706Spkelsey	return (counter);
230292706Spkelsey}
231292706Spkelsey
232292706Spkelseyvoid
233292706Spkelseytcp_fastopen_decrement_counter(unsigned int *counter)
234292706Spkelsey{
235292706Spkelsey	if (*counter == 1)
236292706Spkelsey		uma_zfree(V_counter_zone, counter);
237292706Spkelsey	else
238292706Spkelsey		atomic_subtract_int(counter, 1);
239292706Spkelsey}
240292706Spkelsey
241292706Spkelseystatic void
242292706Spkelseytcp_fastopen_addkey_locked(uint8_t *key)
243292706Spkelsey{
244292706Spkelsey
245292706Spkelsey	V_tcp_fastopen_keys.newest++;
246292706Spkelsey	if (V_tcp_fastopen_keys.newest == TCP_FASTOPEN_MAX_KEYS)
247292706Spkelsey		V_tcp_fastopen_keys.newest = 0;
248292706Spkelsey	memcpy(V_tcp_fastopen_keys.key[V_tcp_fastopen_keys.newest], key,
249292706Spkelsey	    TCP_FASTOPEN_KEY_LEN);
250292706Spkelsey	if (V_tcp_fastopen_numkeys < TCP_FASTOPEN_MAX_KEYS)
251292706Spkelsey		V_tcp_fastopen_numkeys++;
252292706Spkelsey}
253292706Spkelsey
254292706Spkelseystatic void
255292706Spkelseytcp_fastopen_autokey_locked(void)
256292706Spkelsey{
257292706Spkelsey	uint8_t newkey[TCP_FASTOPEN_KEY_LEN];
258292706Spkelsey
259292706Spkelsey	arc4rand(newkey, TCP_FASTOPEN_KEY_LEN, 0);
260292706Spkelsey	tcp_fastopen_addkey_locked(newkey);
261292706Spkelsey}
262292706Spkelsey
263292706Spkelseystatic void
264292706Spkelseytcp_fastopen_autokey_callout(void *arg)
265292706Spkelsey{
266292706Spkelsey	struct tcp_fastopen_callout *ctx = arg;
267292706Spkelsey
268292706Spkelsey	CURVNET_SET(ctx->v);
269292706Spkelsey	tcp_fastopen_autokey_locked();
270292706Spkelsey	callout_reset(&ctx->c, V_tcp_fastopen_autokey * hz,
271292706Spkelsey		      tcp_fastopen_autokey_callout, ctx);
272292706Spkelsey	CURVNET_RESTORE();
273292706Spkelsey}
274292706Spkelsey
275292706Spkelsey
276292706Spkelseystatic uint64_t
277292706Spkelseytcp_fastopen_make_cookie(uint8_t key[SIPHASH_KEY_LENGTH], struct in_conninfo *inc)
278292706Spkelsey{
279292706Spkelsey	SIPHASH_CTX ctx;
280292706Spkelsey	uint64_t siphash;
281292706Spkelsey
282292706Spkelsey	SipHash24_Init(&ctx);
283292706Spkelsey	SipHash_SetKey(&ctx, key);
284292706Spkelsey	switch (inc->inc_flags & INC_ISIPV6) {
285292706Spkelsey#ifdef INET
286292706Spkelsey	case 0:
287292706Spkelsey		SipHash_Update(&ctx, &inc->inc_faddr, sizeof(inc->inc_faddr));
288292706Spkelsey		break;
289292706Spkelsey#endif
290292706Spkelsey#ifdef INET6
291292706Spkelsey	case INC_ISIPV6:
292292706Spkelsey		SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(inc->inc6_faddr));
293292706Spkelsey		break;
294292706Spkelsey#endif
295292706Spkelsey	}
296292706Spkelsey	SipHash_Final((u_int8_t *)&siphash, &ctx);
297292706Spkelsey
298292706Spkelsey	return (siphash);
299292706Spkelsey}
300292706Spkelsey
301292706Spkelsey
302292706Spkelsey/*
303292706Spkelsey * Return values:
304292706Spkelsey *	-1	the cookie is invalid and no valid cookie is available
305292706Spkelsey *	 0	the cookie is invalid and the latest cookie has been returned
306292706Spkelsey *	 1	the cookie is valid and the latest cookie has been returned
307292706Spkelsey */
308292706Spkelseyint
309292706Spkelseytcp_fastopen_check_cookie(struct in_conninfo *inc, uint8_t *cookie,
310292706Spkelsey    unsigned int len, uint64_t *latest_cookie)
311292706Spkelsey{
312292706Spkelsey	struct rm_priotracker tracker;
313292706Spkelsey	unsigned int i, key_index;
314292706Spkelsey	uint64_t cur_cookie;
315292706Spkelsey
316292706Spkelsey	if (V_tcp_fastopen_acceptany) {
317292706Spkelsey		*latest_cookie = 0;
318292706Spkelsey		return (1);
319292706Spkelsey	}
320292706Spkelsey
321292706Spkelsey	if (len != TCP_FASTOPEN_COOKIE_LEN) {
322292706Spkelsey		if (V_tcp_fastopen_numkeys > 0) {
323292706Spkelsey			*latest_cookie =
324292706Spkelsey			    tcp_fastopen_make_cookie(
325292706Spkelsey				V_tcp_fastopen_keys.key[V_tcp_fastopen_keys.newest],
326292706Spkelsey				inc);
327292706Spkelsey			return (0);
328292706Spkelsey		}
329292706Spkelsey 		return (-1);
330292706Spkelsey	}
331292706Spkelsey
332292706Spkelsey	/*
333292706Spkelsey	 * Check against each available key, from newest to oldest.
334292706Spkelsey	 */
335292706Spkelsey	TCP_FASTOPEN_KEYS_RLOCK(&tracker);
336292706Spkelsey	key_index = V_tcp_fastopen_keys.newest;
337292706Spkelsey	for (i = 0; i < V_tcp_fastopen_numkeys; i++) {
338292706Spkelsey		cur_cookie =
339292706Spkelsey		    tcp_fastopen_make_cookie(V_tcp_fastopen_keys.key[key_index],
340292706Spkelsey			inc);
341292706Spkelsey		if (i == 0)
342292706Spkelsey			*latest_cookie = cur_cookie;
343292706Spkelsey		if (memcmp(cookie, &cur_cookie, TCP_FASTOPEN_COOKIE_LEN) == 0) {
344292706Spkelsey			TCP_FASTOPEN_KEYS_RUNLOCK(&tracker);
345292706Spkelsey			return (1);
346292706Spkelsey		}
347292706Spkelsey		if (key_index == 0)
348292706Spkelsey			key_index = TCP_FASTOPEN_MAX_KEYS - 1;
349292706Spkelsey		else
350292706Spkelsey			key_index--;
351292706Spkelsey	}
352292706Spkelsey	TCP_FASTOPEN_KEYS_RUNLOCK(&tracker);
353292706Spkelsey
354292706Spkelsey	return (0);
355292706Spkelsey}
356292706Spkelsey
357292706Spkelseystatic int
358292706Spkelseysysctl_net_inet_tcp_fastopen_autokey(SYSCTL_HANDLER_ARGS)
359292706Spkelsey{
360292706Spkelsey	int error;
361292706Spkelsey	unsigned int new;
362292706Spkelsey
363292706Spkelsey	new = V_tcp_fastopen_autokey;
364292706Spkelsey	error = sysctl_handle_int(oidp, &new, 0, req);
365292706Spkelsey	if (error == 0 && req->newptr) {
366292706Spkelsey		if (new > (INT_MAX / hz))
367292706Spkelsey			return (EINVAL);
368292706Spkelsey
369292706Spkelsey		TCP_FASTOPEN_KEYS_WLOCK();
370292706Spkelsey		if (V_tcp_fastopen_enabled) {
371292706Spkelsey			if (V_tcp_fastopen_autokey && !new)
372292706Spkelsey				callout_stop(&V_tcp_fastopen_autokey_ctx.c);
373292706Spkelsey			else if (new)
374292706Spkelsey				callout_reset(&V_tcp_fastopen_autokey_ctx.c,
375292706Spkelsey				    new * hz, tcp_fastopen_autokey_callout,
376292706Spkelsey				    &V_tcp_fastopen_autokey_ctx);
377292706Spkelsey		}
378292706Spkelsey		V_tcp_fastopen_autokey = new;
379292706Spkelsey		TCP_FASTOPEN_KEYS_WUNLOCK();
380292706Spkelsey	}
381292706Spkelsey
382292706Spkelsey	return (error);
383292706Spkelsey}
384292706Spkelsey
385292706Spkelseystatic int
386292706Spkelseysysctl_net_inet_tcp_fastopen_enabled(SYSCTL_HANDLER_ARGS)
387292706Spkelsey{
388292706Spkelsey	int error;
389292706Spkelsey	unsigned int new;
390292706Spkelsey
391292706Spkelsey	new = V_tcp_fastopen_enabled;
392292706Spkelsey	error = sysctl_handle_int(oidp, &new, 0, req);
393292706Spkelsey	if (error == 0 && req->newptr) {
394292706Spkelsey		if (V_tcp_fastopen_enabled && !new) {
395292706Spkelsey			/* enabled -> disabled */
396292706Spkelsey			TCP_FASTOPEN_KEYS_WLOCK();
397292706Spkelsey			V_tcp_fastopen_numkeys = 0;
398292706Spkelsey			V_tcp_fastopen_keys.newest = TCP_FASTOPEN_MAX_KEYS - 1;
399292706Spkelsey			if (V_tcp_fastopen_autokey)
400292706Spkelsey				callout_stop(&V_tcp_fastopen_autokey_ctx.c);
401292706Spkelsey			V_tcp_fastopen_enabled = 0;
402292706Spkelsey			TCP_FASTOPEN_KEYS_WUNLOCK();
403292706Spkelsey		} else if (!V_tcp_fastopen_enabled && new) {
404292706Spkelsey			/* disabled -> enabled */
405292706Spkelsey			TCP_FASTOPEN_KEYS_WLOCK();
406292706Spkelsey			if (V_tcp_fastopen_autokey &&
407292706Spkelsey			    (V_tcp_fastopen_numkeys == 0)) {
408292706Spkelsey				tcp_fastopen_autokey_locked();
409292706Spkelsey				callout_reset(&V_tcp_fastopen_autokey_ctx.c,
410292706Spkelsey				    V_tcp_fastopen_autokey * hz,
411292706Spkelsey				    tcp_fastopen_autokey_callout,
412292706Spkelsey				    &V_tcp_fastopen_autokey_ctx);
413292706Spkelsey			}
414292706Spkelsey			V_tcp_fastopen_enabled = 1;
415292706Spkelsey			TCP_FASTOPEN_KEYS_WUNLOCK();
416292706Spkelsey		}
417292706Spkelsey	}
418292706Spkelsey	return (error);
419292706Spkelsey}
420292706Spkelsey
421292706Spkelseystatic int
422292706Spkelseysysctl_net_inet_tcp_fastopen_setkey(SYSCTL_HANDLER_ARGS)
423292706Spkelsey{
424292706Spkelsey	int error;
425292706Spkelsey	uint8_t newkey[TCP_FASTOPEN_KEY_LEN];
426292706Spkelsey
427292706Spkelsey	if (req->oldptr != NULL || req->oldlen != 0)
428292706Spkelsey		return (EINVAL);
429292706Spkelsey	if (req->newptr == NULL)
430292706Spkelsey		return (EPERM);
431292706Spkelsey	if (req->newlen != sizeof(newkey))
432292706Spkelsey		return (EINVAL);
433292706Spkelsey	error = SYSCTL_IN(req, newkey, sizeof(newkey));
434292706Spkelsey	if (error)
435292706Spkelsey		return (error);
436292706Spkelsey
437292706Spkelsey	TCP_FASTOPEN_KEYS_WLOCK();
438292706Spkelsey	tcp_fastopen_addkey_locked(newkey);
439292706Spkelsey	TCP_FASTOPEN_KEYS_WUNLOCK();
440292706Spkelsey
441292706Spkelsey	return (0);
442292706Spkelsey}
443