configfs: fix race between dentry put and lookup
[linux-2.6.git] / net / ipv4 / tcp_fastopen.c
blobf195d9316e55d3d7ef6dc5285606ea9de0f52a51
1 #include <linux/err.h>
2 #include <linux/init.h>
3 #include <linux/kernel.h>
4 #include <linux/list.h>
5 #include <linux/tcp.h>
6 #include <linux/rcupdate.h>
7 #include <linux/rculist.h>
8 #include <net/inetpeer.h>
9 #include <net/tcp.h>
11 int sysctl_tcp_fastopen __read_mostly = TFO_CLIENT_ENABLE;
13 struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
15 static DEFINE_SPINLOCK(tcp_fastopen_ctx_lock);
17 void tcp_fastopen_init_key_once(bool publish)
19 static u8 key[TCP_FASTOPEN_KEY_LENGTH];
21 /* tcp_fastopen_reset_cipher publishes the new context
22 * atomically, so we allow this race happening here.
24 * All call sites of tcp_fastopen_cookie_gen also check
25 * for a valid cookie, so this is an acceptable risk.
27 if (net_get_random_once(key, sizeof(key)) && publish)
28 tcp_fastopen_reset_cipher(key, sizeof(key));
31 static void tcp_fastopen_ctx_free(struct rcu_head *head)
33 struct tcp_fastopen_context *ctx =
34 container_of(head, struct tcp_fastopen_context, rcu);
35 crypto_free_cipher(ctx->tfm);
36 kfree(ctx);
39 int tcp_fastopen_reset_cipher(void *key, unsigned int len)
41 int err;
42 struct tcp_fastopen_context *ctx, *octx;
44 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
45 if (!ctx)
46 return -ENOMEM;
47 ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
49 if (IS_ERR(ctx->tfm)) {
50 err = PTR_ERR(ctx->tfm);
51 error: kfree(ctx);
52 pr_err("TCP: TFO aes cipher alloc error: %d\n", err);
53 return err;
55 err = crypto_cipher_setkey(ctx->tfm, key, len);
56 if (err) {
57 pr_err("TCP: TFO cipher key error: %d\n", err);
58 crypto_free_cipher(ctx->tfm);
59 goto error;
61 memcpy(ctx->key, key, len);
63 spin_lock(&tcp_fastopen_ctx_lock);
65 octx = rcu_dereference_protected(tcp_fastopen_ctx,
66 lockdep_is_held(&tcp_fastopen_ctx_lock));
67 rcu_assign_pointer(tcp_fastopen_ctx, ctx);
68 spin_unlock(&tcp_fastopen_ctx_lock);
70 if (octx)
71 call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
72 return err;
75 /* Computes the fastopen cookie for the IP path.
76 * The path is a 128 bits long (pad with zeros for IPv4).
78 * The caller must check foc->len to determine if a valid cookie
79 * has been generated successfully.
81 void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
82 struct tcp_fastopen_cookie *foc)
84 __be32 path[4] = { src, dst, 0, 0 };
85 struct tcp_fastopen_context *ctx;
87 tcp_fastopen_init_key_once(true);
89 rcu_read_lock();
90 ctx = rcu_dereference(tcp_fastopen_ctx);
91 if (ctx) {
92 crypto_cipher_encrypt_one(ctx->tfm, foc->val, (__u8 *)path);
93 foc->len = TCP_FASTOPEN_COOKIE_SIZE;
95 rcu_read_unlock();