release_sock(sk);
                return err;
        }
+       case TCP_FASTOPEN_KEY: {
+               __u8 key[TCP_FASTOPEN_KEY_LENGTH];
+
+               if (optlen != sizeof(key))
+                       return -EINVAL;
+
+               if (copy_from_user(key, optval, optlen))
+                       return -EFAULT;
+
+               return tcp_fastopen_reset_cipher(net, sk, key, sizeof(key));
+       }
        default:
                /* fallthru */
                break;
                        return -EFAULT;
                return 0;
 
+       case TCP_FASTOPEN_KEY: {
+               __u8 key[TCP_FASTOPEN_KEY_LENGTH];
+               struct tcp_fastopen_context *ctx;
+
+               if (get_user(len, optlen))
+                       return -EFAULT;
+
+               rcu_read_lock();
+               ctx = rcu_dereference(icsk->icsk_accept_queue.fastopenq.ctx);
+               if (ctx)
+                       memcpy(key, ctx->key, sizeof(key));
+               else
+                       len = 0;
+               rcu_read_unlock();
+
+               len = min_t(unsigned int, len, sizeof(key));
+               if (put_user(len, optlen))
+                       return -EFAULT;
+               if (copy_to_user(optval, key, len))
+                       return -EFAULT;
+               return 0;
+       }
        case TCP_THIN_LINEAR_TIMEOUTS:
                val = tp->thin_lto;
                break;
 
         * for a valid cookie, so this is an acceptable risk.
         */
        get_random_bytes(key, sizeof(key));
-       tcp_fastopen_reset_cipher(net, key, sizeof(key));
+       tcp_fastopen_reset_cipher(net, NULL, key, sizeof(key));
 }
 
 static void tcp_fastopen_ctx_free(struct rcu_head *head)
        kfree(ctx);
 }
 
+void tcp_fastopen_destroy_cipher(struct sock *sk)
+{
+       struct tcp_fastopen_context *ctx;
+
+       ctx = rcu_dereference_protected(
+                       inet_csk(sk)->icsk_accept_queue.fastopenq.ctx, 1);
+       if (ctx)
+               call_rcu(&ctx->rcu, tcp_fastopen_ctx_free);
+}
+
 void tcp_fastopen_ctx_destroy(struct net *net)
 {
        struct tcp_fastopen_context *ctxt;
                call_rcu(&ctxt->rcu, tcp_fastopen_ctx_free);
 }
 
-int tcp_fastopen_reset_cipher(struct net *net, void *key, unsigned int len)
+int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
+                             void *key, unsigned int len)
 {
-       int err;
        struct tcp_fastopen_context *ctx, *octx;
+       struct fastopen_queue *q;
+       int err;
 
        ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
        if (!ctx)
        }
        memcpy(ctx->key, key, len);
 
-       spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
 
-       octx = rcu_dereference_protected(net->ipv4.tcp_fastopen_ctx,
-                               lockdep_is_held(&net->ipv4.tcp_fastopen_ctx_lock));
-       rcu_assign_pointer(net->ipv4.tcp_fastopen_ctx, ctx);
-       spin_unlock(&net->ipv4.tcp_fastopen_ctx_lock);
+       if (sk) {
+               q = &inet_csk(sk)->icsk_accept_queue.fastopenq;
+               spin_lock_bh(&q->lock);
+               octx = rcu_dereference_protected(q->ctx,
+                                                lockdep_is_held(&q->lock));
+               rcu_assign_pointer(q->ctx, ctx);
+               spin_unlock_bh(&q->lock);
+       } else {
+               spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
+               octx = rcu_dereference_protected(net->ipv4.tcp_fastopen_ctx,
+                       lockdep_is_held(&net->ipv4.tcp_fastopen_ctx_lock));
+               rcu_assign_pointer(net->ipv4.tcp_fastopen_ctx, ctx);
+               spin_unlock(&net->ipv4.tcp_fastopen_ctx_lock);
+       }
 
        if (octx)
                call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
        return err;
 }
 
-static bool __tcp_fastopen_cookie_gen(struct net *net,
-                                     const void *path,
+static bool __tcp_fastopen_cookie_gen(struct sock *sk, const void *path,
                                      struct tcp_fastopen_cookie *foc)
 {
        struct tcp_fastopen_context *ctx;
        bool ok = false;
 
        rcu_read_lock();
-       ctx = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
+
+       ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
+       if (!ctx)
+               ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
+
        if (ctx) {
                crypto_cipher_encrypt_one(ctx->tfm, foc->val, path);
                foc->len = TCP_FASTOPEN_COOKIE_SIZE;
  *
  * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE.
  */
-static bool tcp_fastopen_cookie_gen(struct net *net,
+static bool tcp_fastopen_cookie_gen(struct sock *sk,
                                    struct request_sock *req,
                                    struct sk_buff *syn,
                                    struct tcp_fastopen_cookie *foc)
                const struct iphdr *iph = ip_hdr(syn);
 
                __be32 path[4] = { iph->saddr, iph->daddr, 0, 0 };
-               return __tcp_fastopen_cookie_gen(net, path, foc);
+               return __tcp_fastopen_cookie_gen(sk, path, foc);
        }
 
 #if IS_ENABLED(CONFIG_IPV6)
                const struct ipv6hdr *ip6h = ipv6_hdr(syn);
                struct tcp_fastopen_cookie tmp;
 
-               if (__tcp_fastopen_cookie_gen(net, &ip6h->saddr, &tmp)) {
+               if (__tcp_fastopen_cookie_gen(sk, &ip6h->saddr, &tmp)) {
                        struct in6_addr *buf = &tmp.addr;
                        int i;
 
                        for (i = 0; i < 4; i++)
                                buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i];
-                       return __tcp_fastopen_cookie_gen(net, buf, foc);
+                       return __tcp_fastopen_cookie_gen(sk, buf, foc);
                }
        }
 #endif
                goto fastopen;
 
        if (foc->len >= 0 &&  /* Client presents or requests a cookie */
-           tcp_fastopen_cookie_gen(sock_net(sk), req, skb, &valid_foc) &&
+           tcp_fastopen_cookie_gen(sk, req, skb, &valid_foc) &&
            foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
            foc->len == valid_foc.len &&
            !memcmp(foc->val, valid_foc.val, foc->len)) {