xref: /openbmc/linux/net/ipv4/tcp_fastopen.c (revision 1fba70e5)
1cf80e0e4SHerbert Xu #include <linux/crypto.h>
210467163SJerry Chu #include <linux/err.h>
32100c8d2SYuchung Cheng #include <linux/init.h>
42100c8d2SYuchung Cheng #include <linux/kernel.h>
510467163SJerry Chu #include <linux/list.h>
610467163SJerry Chu #include <linux/tcp.h>
710467163SJerry Chu #include <linux/rcupdate.h>
810467163SJerry Chu #include <linux/rculist.h>
910467163SJerry Chu #include <net/inetpeer.h>
1010467163SJerry Chu #include <net/tcp.h>
112100c8d2SYuchung Cheng 
1243713848SHaishuang Yan void tcp_fastopen_init_key_once(struct net *net)
13222e83d2SHannes Frederic Sowa {
1443713848SHaishuang Yan 	u8 key[TCP_FASTOPEN_KEY_LENGTH];
1543713848SHaishuang Yan 	struct tcp_fastopen_context *ctxt;
1643713848SHaishuang Yan 
1743713848SHaishuang Yan 	rcu_read_lock();
1843713848SHaishuang Yan 	ctxt = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
1943713848SHaishuang Yan 	if (ctxt) {
2043713848SHaishuang Yan 		rcu_read_unlock();
2143713848SHaishuang Yan 		return;
2243713848SHaishuang Yan 	}
2343713848SHaishuang Yan 	rcu_read_unlock();
24222e83d2SHannes Frederic Sowa 
25222e83d2SHannes Frederic Sowa 	/* tcp_fastopen_reset_cipher publishes the new context
26222e83d2SHannes Frederic Sowa 	 * atomically, so we allow this race happening here.
27222e83d2SHannes Frederic Sowa 	 *
28222e83d2SHannes Frederic Sowa 	 * All call sites of tcp_fastopen_cookie_gen also check
29222e83d2SHannes Frederic Sowa 	 * for a valid cookie, so this is an acceptable risk.
30222e83d2SHannes Frederic Sowa 	 */
3143713848SHaishuang Yan 	get_random_bytes(key, sizeof(key));
321fba70e5SYuchung Cheng 	tcp_fastopen_reset_cipher(net, NULL, key, sizeof(key));
33222e83d2SHannes Frederic Sowa }
34222e83d2SHannes Frederic Sowa 
3510467163SJerry Chu static void tcp_fastopen_ctx_free(struct rcu_head *head)
3610467163SJerry Chu {
3710467163SJerry Chu 	struct tcp_fastopen_context *ctx =
3810467163SJerry Chu 	    container_of(head, struct tcp_fastopen_context, rcu);
3910467163SJerry Chu 	crypto_free_cipher(ctx->tfm);
4010467163SJerry Chu 	kfree(ctx);
4110467163SJerry Chu }
4210467163SJerry Chu 
431fba70e5SYuchung Cheng void tcp_fastopen_destroy_cipher(struct sock *sk)
441fba70e5SYuchung Cheng {
451fba70e5SYuchung Cheng 	struct tcp_fastopen_context *ctx;
461fba70e5SYuchung Cheng 
471fba70e5SYuchung Cheng 	ctx = rcu_dereference_protected(
481fba70e5SYuchung Cheng 			inet_csk(sk)->icsk_accept_queue.fastopenq.ctx, 1);
491fba70e5SYuchung Cheng 	if (ctx)
501fba70e5SYuchung Cheng 		call_rcu(&ctx->rcu, tcp_fastopen_ctx_free);
511fba70e5SYuchung Cheng }
521fba70e5SYuchung Cheng 
5343713848SHaishuang Yan void tcp_fastopen_ctx_destroy(struct net *net)
5443713848SHaishuang Yan {
5543713848SHaishuang Yan 	struct tcp_fastopen_context *ctxt;
5643713848SHaishuang Yan 
5743713848SHaishuang Yan 	spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
5843713848SHaishuang Yan 
5943713848SHaishuang Yan 	ctxt = rcu_dereference_protected(net->ipv4.tcp_fastopen_ctx,
6043713848SHaishuang Yan 				lockdep_is_held(&net->ipv4.tcp_fastopen_ctx_lock));
6143713848SHaishuang Yan 	rcu_assign_pointer(net->ipv4.tcp_fastopen_ctx, NULL);
6243713848SHaishuang Yan 	spin_unlock(&net->ipv4.tcp_fastopen_ctx_lock);
6343713848SHaishuang Yan 
6443713848SHaishuang Yan 	if (ctxt)
6543713848SHaishuang Yan 		call_rcu(&ctxt->rcu, tcp_fastopen_ctx_free);
6643713848SHaishuang Yan }
6743713848SHaishuang Yan 
681fba70e5SYuchung Cheng int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
691fba70e5SYuchung Cheng 			      void *key, unsigned int len)
7010467163SJerry Chu {
7110467163SJerry Chu 	struct tcp_fastopen_context *ctx, *octx;
721fba70e5SYuchung Cheng 	struct fastopen_queue *q;
731fba70e5SYuchung Cheng 	int err;
7410467163SJerry Chu 
7510467163SJerry Chu 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
7610467163SJerry Chu 	if (!ctx)
7710467163SJerry Chu 		return -ENOMEM;
7810467163SJerry Chu 	ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
7910467163SJerry Chu 
8010467163SJerry Chu 	if (IS_ERR(ctx->tfm)) {
8110467163SJerry Chu 		err = PTR_ERR(ctx->tfm);
8210467163SJerry Chu error:		kfree(ctx);
8310467163SJerry Chu 		pr_err("TCP: TFO aes cipher alloc error: %d\n", err);
8410467163SJerry Chu 		return err;
8510467163SJerry Chu 	}
8610467163SJerry Chu 	err = crypto_cipher_setkey(ctx->tfm, key, len);
8710467163SJerry Chu 	if (err) {
8810467163SJerry Chu 		pr_err("TCP: TFO cipher key error: %d\n", err);
8910467163SJerry Chu 		crypto_free_cipher(ctx->tfm);
9010467163SJerry Chu 		goto error;
9110467163SJerry Chu 	}
9210467163SJerry Chu 	memcpy(ctx->key, key, len);
9310467163SJerry Chu 
9410467163SJerry Chu 
951fba70e5SYuchung Cheng 	if (sk) {
961fba70e5SYuchung Cheng 		q = &inet_csk(sk)->icsk_accept_queue.fastopenq;
971fba70e5SYuchung Cheng 		spin_lock_bh(&q->lock);
981fba70e5SYuchung Cheng 		octx = rcu_dereference_protected(q->ctx,
991fba70e5SYuchung Cheng 						 lockdep_is_held(&q->lock));
1001fba70e5SYuchung Cheng 		rcu_assign_pointer(q->ctx, ctx);
1011fba70e5SYuchung Cheng 		spin_unlock_bh(&q->lock);
1021fba70e5SYuchung Cheng 	} else {
1031fba70e5SYuchung Cheng 		spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
10443713848SHaishuang Yan 		octx = rcu_dereference_protected(net->ipv4.tcp_fastopen_ctx,
10543713848SHaishuang Yan 			lockdep_is_held(&net->ipv4.tcp_fastopen_ctx_lock));
10643713848SHaishuang Yan 		rcu_assign_pointer(net->ipv4.tcp_fastopen_ctx, ctx);
10743713848SHaishuang Yan 		spin_unlock(&net->ipv4.tcp_fastopen_ctx_lock);
1081fba70e5SYuchung Cheng 	}
10910467163SJerry Chu 
11010467163SJerry Chu 	if (octx)
11110467163SJerry Chu 		call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
11210467163SJerry Chu 	return err;
11310467163SJerry Chu }
11410467163SJerry Chu 
1151fba70e5SYuchung Cheng static bool __tcp_fastopen_cookie_gen(struct sock *sk, const void *path,
116149479d0SYuchung Cheng 				      struct tcp_fastopen_cookie *foc)
11710467163SJerry Chu {
11810467163SJerry Chu 	struct tcp_fastopen_context *ctx;
1193a19ce0eSDaniel Lee 	bool ok = false;
12010467163SJerry Chu 
12110467163SJerry Chu 	rcu_read_lock();
1221fba70e5SYuchung Cheng 
1231fba70e5SYuchung Cheng 	ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
1241fba70e5SYuchung Cheng 	if (!ctx)
1251fba70e5SYuchung Cheng 		ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
1261fba70e5SYuchung Cheng 
12710467163SJerry Chu 	if (ctx) {
1283a19ce0eSDaniel Lee 		crypto_cipher_encrypt_one(ctx->tfm, foc->val, path);
12910467163SJerry Chu 		foc->len = TCP_FASTOPEN_COOKIE_SIZE;
1303a19ce0eSDaniel Lee 		ok = true;
13110467163SJerry Chu 	}
13210467163SJerry Chu 	rcu_read_unlock();
1333a19ce0eSDaniel Lee 	return ok;
1343a19ce0eSDaniel Lee }
1353a19ce0eSDaniel Lee 
1363a19ce0eSDaniel Lee /* Generate the fastopen cookie by doing aes128 encryption on both
1373a19ce0eSDaniel Lee  * the source and destination addresses. Pad 0s for IPv4 or IPv4-mapped-IPv6
1383a19ce0eSDaniel Lee  * addresses. For the longer IPv6 addresses use CBC-MAC.
1393a19ce0eSDaniel Lee  *
1403a19ce0eSDaniel Lee  * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE.
1413a19ce0eSDaniel Lee  */
1421fba70e5SYuchung Cheng static bool tcp_fastopen_cookie_gen(struct sock *sk,
14343713848SHaishuang Yan 				    struct request_sock *req,
1443a19ce0eSDaniel Lee 				    struct sk_buff *syn,
1453a19ce0eSDaniel Lee 				    struct tcp_fastopen_cookie *foc)
1463a19ce0eSDaniel Lee {
1473a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET) {
1483a19ce0eSDaniel Lee 		const struct iphdr *iph = ip_hdr(syn);
1493a19ce0eSDaniel Lee 
1503a19ce0eSDaniel Lee 		__be32 path[4] = { iph->saddr, iph->daddr, 0, 0 };
1511fba70e5SYuchung Cheng 		return __tcp_fastopen_cookie_gen(sk, path, foc);
1523a19ce0eSDaniel Lee 	}
1533a19ce0eSDaniel Lee 
1543a19ce0eSDaniel Lee #if IS_ENABLED(CONFIG_IPV6)
1553a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET6) {
1563a19ce0eSDaniel Lee 		const struct ipv6hdr *ip6h = ipv6_hdr(syn);
1573a19ce0eSDaniel Lee 		struct tcp_fastopen_cookie tmp;
1583a19ce0eSDaniel Lee 
1591fba70e5SYuchung Cheng 		if (__tcp_fastopen_cookie_gen(sk, &ip6h->saddr, &tmp)) {
160003c9410SShannon Nelson 			struct in6_addr *buf = &tmp.addr;
16141c91996SLi RongQing 			int i;
1623a19ce0eSDaniel Lee 
1633a19ce0eSDaniel Lee 			for (i = 0; i < 4; i++)
1643a19ce0eSDaniel Lee 				buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i];
1651fba70e5SYuchung Cheng 			return __tcp_fastopen_cookie_gen(sk, buf, foc);
1663a19ce0eSDaniel Lee 		}
1673a19ce0eSDaniel Lee 	}
1683a19ce0eSDaniel Lee #endif
1693a19ce0eSDaniel Lee 	return false;
17010467163SJerry Chu }
1715b7ed089SYuchung Cheng 
17261d2bcaeSEric Dumazet 
17361d2bcaeSEric Dumazet /* If an incoming SYN or SYNACK frame contains a payload and/or FIN,
17461d2bcaeSEric Dumazet  * queue this additional data / FIN.
17561d2bcaeSEric Dumazet  */
17661d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb)
17761d2bcaeSEric Dumazet {
17861d2bcaeSEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
17961d2bcaeSEric Dumazet 
18061d2bcaeSEric Dumazet 	if (TCP_SKB_CB(skb)->end_seq == tp->rcv_nxt)
18161d2bcaeSEric Dumazet 		return;
18261d2bcaeSEric Dumazet 
18361d2bcaeSEric Dumazet 	skb = skb_clone(skb, GFP_ATOMIC);
18461d2bcaeSEric Dumazet 	if (!skb)
18561d2bcaeSEric Dumazet 		return;
18661d2bcaeSEric Dumazet 
18761d2bcaeSEric Dumazet 	skb_dst_drop(skb);
188a44d6eacSMartin KaFai Lau 	/* segs_in has been initialized to 1 in tcp_create_openreq_child().
189a44d6eacSMartin KaFai Lau 	 * Hence, reset segs_in to 0 before calling tcp_segs_in()
190a44d6eacSMartin KaFai Lau 	 * to avoid double counting.  Also, tcp_segs_in() expects
191a44d6eacSMartin KaFai Lau 	 * skb->len to include the tcp_hdrlen.  Hence, it should
192a44d6eacSMartin KaFai Lau 	 * be called before __skb_pull().
193a44d6eacSMartin KaFai Lau 	 */
194a44d6eacSMartin KaFai Lau 	tp->segs_in = 0;
195a44d6eacSMartin KaFai Lau 	tcp_segs_in(tp, skb);
19661d2bcaeSEric Dumazet 	__skb_pull(skb, tcp_hdrlen(skb));
19776061f63SEric Dumazet 	sk_forced_mem_schedule(sk, skb->truesize);
19861d2bcaeSEric Dumazet 	skb_set_owner_r(skb, sk);
19961d2bcaeSEric Dumazet 
2009d691539SEric Dumazet 	TCP_SKB_CB(skb)->seq++;
2019d691539SEric Dumazet 	TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_SYN;
2029d691539SEric Dumazet 
20361d2bcaeSEric Dumazet 	tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
20461d2bcaeSEric Dumazet 	__skb_queue_tail(&sk->sk_receive_queue, skb);
20561d2bcaeSEric Dumazet 	tp->syn_data_acked = 1;
20661d2bcaeSEric Dumazet 
20761d2bcaeSEric Dumazet 	/* u64_stats_update_begin(&tp->syncp) not needed here,
20861d2bcaeSEric Dumazet 	 * as we certainly are not changing upper 32bit value (0)
20961d2bcaeSEric Dumazet 	 */
21061d2bcaeSEric Dumazet 	tp->bytes_received = skb->len;
211e3e17b77SEric Dumazet 
212e3e17b77SEric Dumazet 	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
213e3e17b77SEric Dumazet 		tcp_fin(sk);
21461d2bcaeSEric Dumazet }
21561d2bcaeSEric Dumazet 
2167c85af88SEric Dumazet static struct sock *tcp_fastopen_create_child(struct sock *sk,
2175b7ed089SYuchung Cheng 					      struct sk_buff *skb,
2185b7ed089SYuchung Cheng 					      struct request_sock *req)
2195b7ed089SYuchung Cheng {
22017846376SDave Jones 	struct tcp_sock *tp;
2215b7ed089SYuchung Cheng 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
2225b7ed089SYuchung Cheng 	struct sock *child;
2235e0724d0SEric Dumazet 	bool own_req;
2245b7ed089SYuchung Cheng 
2255b7ed089SYuchung Cheng 	req->num_retrans = 0;
2265b7ed089SYuchung Cheng 	req->num_timeout = 0;
2275b7ed089SYuchung Cheng 	req->sk = NULL;
2285b7ed089SYuchung Cheng 
2295e0724d0SEric Dumazet 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
2305e0724d0SEric Dumazet 							 NULL, &own_req);
23151456b29SIan Morris 	if (!child)
2327c85af88SEric Dumazet 		return NULL;
2335b7ed089SYuchung Cheng 
2340536fcc0SEric Dumazet 	spin_lock(&queue->fastopenq.lock);
2350536fcc0SEric Dumazet 	queue->fastopenq.qlen++;
2360536fcc0SEric Dumazet 	spin_unlock(&queue->fastopenq.lock);
2375b7ed089SYuchung Cheng 
2385b7ed089SYuchung Cheng 	/* Initialize the child socket. Have to fix some values to take
2395b7ed089SYuchung Cheng 	 * into account the child is a Fast Open socket and is created
2405b7ed089SYuchung Cheng 	 * only out of the bits carried in the SYN packet.
2415b7ed089SYuchung Cheng 	 */
2425b7ed089SYuchung Cheng 	tp = tcp_sk(child);
2435b7ed089SYuchung Cheng 
2445b7ed089SYuchung Cheng 	tp->fastopen_rsk = req;
2459439ce00SEric Dumazet 	tcp_rsk(req)->tfo_listener = true;
2465b7ed089SYuchung Cheng 
2475b7ed089SYuchung Cheng 	/* RFC1323: The window in SYN & SYN/ACK segments is never
2485b7ed089SYuchung Cheng 	 * scaled. So correct it appropriately.
2495b7ed089SYuchung Cheng 	 */
2505b7ed089SYuchung Cheng 	tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
2510dbd7ff3SAlexey Kodanev 	tp->max_window = tp->snd_wnd;
2525b7ed089SYuchung Cheng 
2535b7ed089SYuchung Cheng 	/* Activate the retrans timer so that SYNACK can be retransmitted.
254ca6fb065SEric Dumazet 	 * The request socket is not added to the ehash
2555b7ed089SYuchung Cheng 	 * because it's been added to the accept queue directly.
2565b7ed089SYuchung Cheng 	 */
2575b7ed089SYuchung Cheng 	inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
2585b7ed089SYuchung Cheng 				  TCP_TIMEOUT_INIT, TCP_RTO_MAX);
2595b7ed089SYuchung Cheng 
26041c6d650SReshetova, Elena 	refcount_set(&req->rsk_refcnt, 2);
2615b7ed089SYuchung Cheng 
2625b7ed089SYuchung Cheng 	/* Now finish processing the fastopen child socket. */
26327204aaaSWei Wang 	tcp_init_transfer(child, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB);
2645b7ed089SYuchung Cheng 
26561d2bcaeSEric Dumazet 	tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
266ba34e6d9SEric Dumazet 
26761d2bcaeSEric Dumazet 	tcp_fastopen_add_skb(child, skb);
268d654976cSEric Dumazet 
26961d2bcaeSEric Dumazet 	tcp_rsk(req)->rcv_nxt = tp->rcv_nxt;
27028b346cbSNeal Cardwell 	tp->rcv_wup = tp->rcv_nxt;
2717656d842SEric Dumazet 	/* tcp_conn_request() is sending the SYNACK,
2727656d842SEric Dumazet 	 * and queues the child into listener accept queue.
2737c85af88SEric Dumazet 	 */
2747c85af88SEric Dumazet 	return child;
2755b7ed089SYuchung Cheng }
2765b7ed089SYuchung Cheng 
2775b7ed089SYuchung Cheng static bool tcp_fastopen_queue_check(struct sock *sk)
2785b7ed089SYuchung Cheng {
2795b7ed089SYuchung Cheng 	struct fastopen_queue *fastopenq;
2805b7ed089SYuchung Cheng 
2815b7ed089SYuchung Cheng 	/* Make sure the listener has enabled fastopen, and we don't
2825b7ed089SYuchung Cheng 	 * exceed the max # of pending TFO requests allowed before trying
2835b7ed089SYuchung Cheng 	 * to validating the cookie in order to avoid burning CPU cycles
2845b7ed089SYuchung Cheng 	 * unnecessarily.
2855b7ed089SYuchung Cheng 	 *
2865b7ed089SYuchung Cheng 	 * XXX (TFO) - The implication of checking the max_qlen before
2875b7ed089SYuchung Cheng 	 * processing a cookie request is that clients can't differentiate
2885b7ed089SYuchung Cheng 	 * between qlen overflow causing Fast Open to be disabled
2895b7ed089SYuchung Cheng 	 * temporarily vs a server not supporting Fast Open at all.
2905b7ed089SYuchung Cheng 	 */
2910536fcc0SEric Dumazet 	fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq;
2920536fcc0SEric Dumazet 	if (fastopenq->max_qlen == 0)
2935b7ed089SYuchung Cheng 		return false;
2945b7ed089SYuchung Cheng 
2955b7ed089SYuchung Cheng 	if (fastopenq->qlen >= fastopenq->max_qlen) {
2965b7ed089SYuchung Cheng 		struct request_sock *req1;
2975b7ed089SYuchung Cheng 		spin_lock(&fastopenq->lock);
2985b7ed089SYuchung Cheng 		req1 = fastopenq->rskq_rst_head;
299fa76ce73SEric Dumazet 		if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) {
30002a1d6e7SEric Dumazet 			__NET_INC_STATS(sock_net(sk),
3015b7ed089SYuchung Cheng 					LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
302c10d9310SEric Dumazet 			spin_unlock(&fastopenq->lock);
3035b7ed089SYuchung Cheng 			return false;
3045b7ed089SYuchung Cheng 		}
3055b7ed089SYuchung Cheng 		fastopenq->rskq_rst_head = req1->dl_next;
3065b7ed089SYuchung Cheng 		fastopenq->qlen--;
3075b7ed089SYuchung Cheng 		spin_unlock(&fastopenq->lock);
30813854e5aSEric Dumazet 		reqsk_put(req1);
3095b7ed089SYuchung Cheng 	}
3105b7ed089SYuchung Cheng 	return true;
3115b7ed089SYuchung Cheng }
3125b7ed089SYuchung Cheng 
31389278c9dSYuchung Cheng /* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
31489278c9dSYuchung Cheng  * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
31589278c9dSYuchung Cheng  * cookie request (foc->len == 0).
31689278c9dSYuchung Cheng  */
3177c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
3185b7ed089SYuchung Cheng 			      struct request_sock *req,
31911199369STonghao Zhang 			      struct tcp_fastopen_cookie *foc)
3205b7ed089SYuchung Cheng {
32189278c9dSYuchung Cheng 	bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
322e1cfcbe8SHaishuang Yan 	int tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
323e1cfcbe8SHaishuang Yan 	struct tcp_fastopen_cookie valid_foc = { .len = -1 };
3247c85af88SEric Dumazet 	struct sock *child;
3255b7ed089SYuchung Cheng 
326531c94a9SYuchung Cheng 	if (foc->len == 0) /* Client requests a cookie */
327c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
328531c94a9SYuchung Cheng 
329e1cfcbe8SHaishuang Yan 	if (!((tcp_fastopen & TFO_SERVER_ENABLE) &&
33089278c9dSYuchung Cheng 	      (syn_data || foc->len >= 0) &&
33189278c9dSYuchung Cheng 	      tcp_fastopen_queue_check(sk))) {
33289278c9dSYuchung Cheng 		foc->len = -1;
3337c85af88SEric Dumazet 		return NULL;
3345b7ed089SYuchung Cheng 	}
33589278c9dSYuchung Cheng 
336e1cfcbe8SHaishuang Yan 	if (syn_data && (tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD))
33789278c9dSYuchung Cheng 		goto fastopen;
33889278c9dSYuchung Cheng 
339531c94a9SYuchung Cheng 	if (foc->len >= 0 &&  /* Client presents or requests a cookie */
3401fba70e5SYuchung Cheng 	    tcp_fastopen_cookie_gen(sk, req, skb, &valid_foc) &&
3413a19ce0eSDaniel Lee 	    foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
34289278c9dSYuchung Cheng 	    foc->len == valid_foc.len &&
34389278c9dSYuchung Cheng 	    !memcmp(foc->val, valid_foc.val, foc->len)) {
344843f4a55SYuchung Cheng 		/* Cookie is valid. Create a (full) child socket to accept
345843f4a55SYuchung Cheng 		 * the data in SYN before returning a SYN-ACK to ack the
346843f4a55SYuchung Cheng 		 * data. If we fail to create the socket, fall back and
347843f4a55SYuchung Cheng 		 * ack the ISN only but includes the same cookie.
348843f4a55SYuchung Cheng 		 *
349843f4a55SYuchung Cheng 		 * Note: Data-less SYN with valid cookie is allowed to send
350843f4a55SYuchung Cheng 		 * data in SYN_RECV state.
351843f4a55SYuchung Cheng 		 */
35289278c9dSYuchung Cheng fastopen:
35311199369STonghao Zhang 		child = tcp_fastopen_create_child(sk, skb, req);
3547c85af88SEric Dumazet 		if (child) {
35589278c9dSYuchung Cheng 			foc->len = -1;
356c10d9310SEric Dumazet 			NET_INC_STATS(sock_net(sk),
357843f4a55SYuchung Cheng 				      LINUX_MIB_TCPFASTOPENPASSIVE);
3587c85af88SEric Dumazet 			return child;
3595b7ed089SYuchung Cheng 		}
360c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
361531c94a9SYuchung Cheng 	} else if (foc->len > 0) /* Client presents an invalid cookie */
362c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
3635b7ed089SYuchung Cheng 
3647f9b838bSDaniel Lee 	valid_foc.exp = foc->exp;
36589278c9dSYuchung Cheng 	*foc = valid_foc;
3667c85af88SEric Dumazet 	return NULL;
3675b7ed089SYuchung Cheng }
368065263f4SWei Wang 
369065263f4SWei Wang bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
370065263f4SWei Wang 			       struct tcp_fastopen_cookie *cookie)
371065263f4SWei Wang {
372065263f4SWei Wang 	unsigned long last_syn_loss = 0;
373065263f4SWei Wang 	int syn_loss = 0;
374065263f4SWei Wang 
375065263f4SWei Wang 	tcp_fastopen_cache_get(sk, mss, cookie, &syn_loss, &last_syn_loss);
376065263f4SWei Wang 
377065263f4SWei Wang 	/* Recurring FO SYN losses: no cookie or data in SYN */
378065263f4SWei Wang 	if (syn_loss > 1 &&
379065263f4SWei Wang 	    time_before(jiffies, last_syn_loss + (60*HZ << syn_loss))) {
380065263f4SWei Wang 		cookie->len = -1;
381065263f4SWei Wang 		return false;
382065263f4SWei Wang 	}
383cf1ef3f0SWei Wang 
384cf1ef3f0SWei Wang 	/* Firewall blackhole issue check */
385cf1ef3f0SWei Wang 	if (tcp_fastopen_active_should_disable(sk)) {
386cf1ef3f0SWei Wang 		cookie->len = -1;
387cf1ef3f0SWei Wang 		return false;
388cf1ef3f0SWei Wang 	}
389cf1ef3f0SWei Wang 
390e1cfcbe8SHaishuang Yan 	if (sock_net(sk)->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_NO_COOKIE) {
391065263f4SWei Wang 		cookie->len = -1;
392065263f4SWei Wang 		return true;
393065263f4SWei Wang 	}
394065263f4SWei Wang 	return cookie->len > 0;
395065263f4SWei Wang }
39619f6d3f3SWei Wang 
39719f6d3f3SWei Wang /* This function checks if we want to defer sending SYN until the first
39819f6d3f3SWei Wang  * write().  We defer under the following conditions:
39919f6d3f3SWei Wang  * 1. fastopen_connect sockopt is set
40019f6d3f3SWei Wang  * 2. we have a valid cookie
40119f6d3f3SWei Wang  * Return value: return true if we want to defer until application writes data
40219f6d3f3SWei Wang  *               return false if we want to send out SYN immediately
40319f6d3f3SWei Wang  */
40419f6d3f3SWei Wang bool tcp_fastopen_defer_connect(struct sock *sk, int *err)
40519f6d3f3SWei Wang {
40619f6d3f3SWei Wang 	struct tcp_fastopen_cookie cookie = { .len = 0 };
40719f6d3f3SWei Wang 	struct tcp_sock *tp = tcp_sk(sk);
40819f6d3f3SWei Wang 	u16 mss;
40919f6d3f3SWei Wang 
41019f6d3f3SWei Wang 	if (tp->fastopen_connect && !tp->fastopen_req) {
41119f6d3f3SWei Wang 		if (tcp_fastopen_cookie_check(sk, &mss, &cookie)) {
41219f6d3f3SWei Wang 			inet_sk(sk)->defer_connect = 1;
41319f6d3f3SWei Wang 			return true;
41419f6d3f3SWei Wang 		}
41519f6d3f3SWei Wang 
41619f6d3f3SWei Wang 		/* Alloc fastopen_req in order for FO option to be included
41719f6d3f3SWei Wang 		 * in SYN
41819f6d3f3SWei Wang 		 */
41919f6d3f3SWei Wang 		tp->fastopen_req = kzalloc(sizeof(*tp->fastopen_req),
42019f6d3f3SWei Wang 					   sk->sk_allocation);
42119f6d3f3SWei Wang 		if (tp->fastopen_req)
42219f6d3f3SWei Wang 			tp->fastopen_req->cookie = cookie;
42319f6d3f3SWei Wang 		else
42419f6d3f3SWei Wang 			*err = -ENOBUFS;
42519f6d3f3SWei Wang 	}
42619f6d3f3SWei Wang 	return false;
42719f6d3f3SWei Wang }
42819f6d3f3SWei Wang EXPORT_SYMBOL(tcp_fastopen_defer_connect);
429cf1ef3f0SWei Wang 
430cf1ef3f0SWei Wang /*
431cf1ef3f0SWei Wang  * The following code block is to deal with middle box issues with TFO:
432cf1ef3f0SWei Wang  * Middlebox firewall issues can potentially cause server's data being
433cf1ef3f0SWei Wang  * blackholed after a successful 3WHS using TFO.
434cf1ef3f0SWei Wang  * The proposed solution is to disable active TFO globally under the
435cf1ef3f0SWei Wang  * following circumstances:
436cf1ef3f0SWei Wang  *   1. client side TFO socket receives out of order FIN
437cf1ef3f0SWei Wang  *   2. client side TFO socket receives out of order RST
438cf1ef3f0SWei Wang  * We disable active side TFO globally for 1hr at first. Then if it
439cf1ef3f0SWei Wang  * happens again, we disable it for 2h, then 4h, 8h, ...
440cf1ef3f0SWei Wang  * And we reset the timeout back to 1hr when we see a successful active
441cf1ef3f0SWei Wang  * TFO connection with data exchanges.
442cf1ef3f0SWei Wang  */
443cf1ef3f0SWei Wang 
444cf1ef3f0SWei Wang /* Disable active TFO and record current jiffies and
445cf1ef3f0SWei Wang  * tfo_active_disable_times
446cf1ef3f0SWei Wang  */
44746c2fa39SWei Wang void tcp_fastopen_active_disable(struct sock *sk)
448cf1ef3f0SWei Wang {
4493733be14SHaishuang Yan 	struct net *net = sock_net(sk);
450cf1ef3f0SWei Wang 
4513733be14SHaishuang Yan 	atomic_inc(&net->ipv4.tfo_active_disable_times);
4523733be14SHaishuang Yan 	net->ipv4.tfo_active_disable_stamp = jiffies;
4533733be14SHaishuang Yan 	NET_INC_STATS(net, LINUX_MIB_TCPFASTOPENBLACKHOLE);
454cf1ef3f0SWei Wang }
455cf1ef3f0SWei Wang 
456cf1ef3f0SWei Wang /* Calculate timeout for tfo active disable
457cf1ef3f0SWei Wang  * Return true if we are still in the active TFO disable period
458cf1ef3f0SWei Wang  * Return false if timeout already expired and we should use active TFO
459cf1ef3f0SWei Wang  */
460cf1ef3f0SWei Wang bool tcp_fastopen_active_should_disable(struct sock *sk)
461cf1ef3f0SWei Wang {
4623733be14SHaishuang Yan 	unsigned int tfo_bh_timeout = sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout;
4633733be14SHaishuang Yan 	int tfo_da_times = atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times);
464cf1ef3f0SWei Wang 	unsigned long timeout;
4653733be14SHaishuang Yan 	int multiplier;
466cf1ef3f0SWei Wang 
467cf1ef3f0SWei Wang 	if (!tfo_da_times)
468cf1ef3f0SWei Wang 		return false;
469cf1ef3f0SWei Wang 
470cf1ef3f0SWei Wang 	/* Limit timout to max: 2^6 * initial timeout */
471cf1ef3f0SWei Wang 	multiplier = 1 << min(tfo_da_times - 1, 6);
4723733be14SHaishuang Yan 	timeout = multiplier * tfo_bh_timeout * HZ;
4733733be14SHaishuang Yan 	if (time_before(jiffies, sock_net(sk)->ipv4.tfo_active_disable_stamp + timeout))
474cf1ef3f0SWei Wang 		return true;
475cf1ef3f0SWei Wang 
476cf1ef3f0SWei Wang 	/* Mark check bit so we can check for successful active TFO
477cf1ef3f0SWei Wang 	 * condition and reset tfo_active_disable_times
478cf1ef3f0SWei Wang 	 */
479cf1ef3f0SWei Wang 	tcp_sk(sk)->syn_fastopen_ch = 1;
480cf1ef3f0SWei Wang 	return false;
481cf1ef3f0SWei Wang }
482cf1ef3f0SWei Wang 
483cf1ef3f0SWei Wang /* Disable active TFO if FIN is the only packet in the ofo queue
484cf1ef3f0SWei Wang  * and no data is received.
485cf1ef3f0SWei Wang  * Also check if we can reset tfo_active_disable_times if data is
486cf1ef3f0SWei Wang  * received successfully on a marked active TFO sockets opened on
487cf1ef3f0SWei Wang  * a non-loopback interface
488cf1ef3f0SWei Wang  */
489cf1ef3f0SWei Wang void tcp_fastopen_active_disable_ofo_check(struct sock *sk)
490cf1ef3f0SWei Wang {
491cf1ef3f0SWei Wang 	struct tcp_sock *tp = tcp_sk(sk);
492cf1ef3f0SWei Wang 	struct dst_entry *dst;
49318a4c0eaSEric Dumazet 	struct sk_buff *skb;
494cf1ef3f0SWei Wang 
495cf1ef3f0SWei Wang 	if (!tp->syn_fastopen)
496cf1ef3f0SWei Wang 		return;
497cf1ef3f0SWei Wang 
498cf1ef3f0SWei Wang 	if (!tp->data_segs_in) {
49918a4c0eaSEric Dumazet 		skb = skb_rb_first(&tp->out_of_order_queue);
50018a4c0eaSEric Dumazet 		if (skb && !skb_rb_next(skb)) {
501cf1ef3f0SWei Wang 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
50246c2fa39SWei Wang 				tcp_fastopen_active_disable(sk);
503cf1ef3f0SWei Wang 				return;
504cf1ef3f0SWei Wang 			}
505cf1ef3f0SWei Wang 		}
506cf1ef3f0SWei Wang 	} else if (tp->syn_fastopen_ch &&
5073733be14SHaishuang Yan 		   atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times)) {
508cf1ef3f0SWei Wang 		dst = sk_dst_get(sk);
509cf1ef3f0SWei Wang 		if (!(dst && dst->dev && (dst->dev->flags & IFF_LOOPBACK)))
5103733be14SHaishuang Yan 			atomic_set(&sock_net(sk)->ipv4.tfo_active_disable_times, 0);
511cf1ef3f0SWei Wang 		dst_release(dst);
512cf1ef3f0SWei Wang 	}
513cf1ef3f0SWei Wang }
514