xref: /linux/net/ipv4/tcp_fastopen.c (revision 480274787d7e3458bc5a7cfbbbe07033984ad711)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cf80e0e4SHerbert Xu #include <linux/crypto.h>
310467163SJerry Chu #include <linux/err.h>
42100c8d2SYuchung Cheng #include <linux/init.h>
52100c8d2SYuchung Cheng #include <linux/kernel.h>
610467163SJerry Chu #include <linux/list.h>
710467163SJerry Chu #include <linux/tcp.h>
810467163SJerry Chu #include <linux/rcupdate.h>
910467163SJerry Chu #include <linux/rculist.h>
1010467163SJerry Chu #include <net/inetpeer.h>
1110467163SJerry Chu #include <net/tcp.h>
122100c8d2SYuchung Cheng 
1343713848SHaishuang Yan void tcp_fastopen_init_key_once(struct net *net)
14222e83d2SHannes Frederic Sowa {
1543713848SHaishuang Yan 	u8 key[TCP_FASTOPEN_KEY_LENGTH];
1643713848SHaishuang Yan 	struct tcp_fastopen_context *ctxt;
1743713848SHaishuang Yan 
1843713848SHaishuang Yan 	rcu_read_lock();
1943713848SHaishuang Yan 	ctxt = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
2043713848SHaishuang Yan 	if (ctxt) {
2143713848SHaishuang Yan 		rcu_read_unlock();
2243713848SHaishuang Yan 		return;
2343713848SHaishuang Yan 	}
2443713848SHaishuang Yan 	rcu_read_unlock();
25222e83d2SHannes Frederic Sowa 
26222e83d2SHannes Frederic Sowa 	/* tcp_fastopen_reset_cipher publishes the new context
27222e83d2SHannes Frederic Sowa 	 * atomically, so we allow this race happening here.
28222e83d2SHannes Frederic Sowa 	 *
29222e83d2SHannes Frederic Sowa 	 * All call sites of tcp_fastopen_cookie_gen also check
30222e83d2SHannes Frederic Sowa 	 * for a valid cookie, so this is an acceptable risk.
31222e83d2SHannes Frederic Sowa 	 */
3243713848SHaishuang Yan 	get_random_bytes(key, sizeof(key));
33438ac880SArd Biesheuvel 	tcp_fastopen_reset_cipher(net, NULL, key, NULL);
34222e83d2SHannes Frederic Sowa }
35222e83d2SHannes Frederic Sowa 
3610467163SJerry Chu static void tcp_fastopen_ctx_free(struct rcu_head *head)
3710467163SJerry Chu {
3810467163SJerry Chu 	struct tcp_fastopen_context *ctx =
3910467163SJerry Chu 	    container_of(head, struct tcp_fastopen_context, rcu);
409092a76dSJason Baron 
41c681edaeSArd Biesheuvel 	kzfree(ctx);
4210467163SJerry Chu }
4310467163SJerry Chu 
441fba70e5SYuchung Cheng void tcp_fastopen_destroy_cipher(struct sock *sk)
451fba70e5SYuchung Cheng {
461fba70e5SYuchung Cheng 	struct tcp_fastopen_context *ctx;
471fba70e5SYuchung Cheng 
481fba70e5SYuchung Cheng 	ctx = rcu_dereference_protected(
491fba70e5SYuchung Cheng 			inet_csk(sk)->icsk_accept_queue.fastopenq.ctx, 1);
501fba70e5SYuchung Cheng 	if (ctx)
511fba70e5SYuchung Cheng 		call_rcu(&ctx->rcu, tcp_fastopen_ctx_free);
521fba70e5SYuchung Cheng }
531fba70e5SYuchung Cheng 
5443713848SHaishuang Yan void tcp_fastopen_ctx_destroy(struct net *net)
5543713848SHaishuang Yan {
5643713848SHaishuang Yan 	struct tcp_fastopen_context *ctxt;
5743713848SHaishuang Yan 
5843713848SHaishuang Yan 	spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
5943713848SHaishuang Yan 
6043713848SHaishuang Yan 	ctxt = rcu_dereference_protected(net->ipv4.tcp_fastopen_ctx,
6143713848SHaishuang Yan 				lockdep_is_held(&net->ipv4.tcp_fastopen_ctx_lock));
6243713848SHaishuang Yan 	rcu_assign_pointer(net->ipv4.tcp_fastopen_ctx, NULL);
6343713848SHaishuang Yan 	spin_unlock(&net->ipv4.tcp_fastopen_ctx_lock);
6443713848SHaishuang Yan 
6543713848SHaishuang Yan 	if (ctxt)
6643713848SHaishuang Yan 		call_rcu(&ctxt->rcu, tcp_fastopen_ctx_free);
6743713848SHaishuang Yan }
6843713848SHaishuang Yan 
691fba70e5SYuchung Cheng int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
70438ac880SArd Biesheuvel 			      void *primary_key, void *backup_key)
7110467163SJerry Chu {
7210467163SJerry Chu 	struct tcp_fastopen_context *ctx, *octx;
731fba70e5SYuchung Cheng 	struct fastopen_queue *q;
749092a76dSJason Baron 	int err = 0;
7510467163SJerry Chu 
76c681edaeSArd Biesheuvel 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
77c681edaeSArd Biesheuvel 	if (!ctx) {
78c681edaeSArd Biesheuvel 		err = -ENOMEM;
799092a76dSJason Baron 		goto out;
8010467163SJerry Chu 	}
81c681edaeSArd Biesheuvel 
82438ac880SArd Biesheuvel 	ctx->key[0].key[0] = get_unaligned_le64(primary_key);
83438ac880SArd Biesheuvel 	ctx->key[0].key[1] = get_unaligned_le64(primary_key + 8);
84c681edaeSArd Biesheuvel 	if (backup_key) {
85438ac880SArd Biesheuvel 		ctx->key[1].key[0] = get_unaligned_le64(backup_key);
86438ac880SArd Biesheuvel 		ctx->key[1].key[1] = get_unaligned_le64(backup_key + 8);
87c681edaeSArd Biesheuvel 		ctx->num = 2;
88c681edaeSArd Biesheuvel 	} else {
89c681edaeSArd Biesheuvel 		ctx->num = 1;
90c681edaeSArd Biesheuvel 	}
91c681edaeSArd Biesheuvel 
929eba9353SEric Dumazet 	spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
931fba70e5SYuchung Cheng 	if (sk) {
941fba70e5SYuchung Cheng 		q = &inet_csk(sk)->icsk_accept_queue.fastopenq;
951fba70e5SYuchung Cheng 		octx = rcu_dereference_protected(q->ctx,
969eba9353SEric Dumazet 			lockdep_is_held(&net->ipv4.tcp_fastopen_ctx_lock));
971fba70e5SYuchung Cheng 		rcu_assign_pointer(q->ctx, ctx);
981fba70e5SYuchung Cheng 	} else {
9943713848SHaishuang Yan 		octx = rcu_dereference_protected(net->ipv4.tcp_fastopen_ctx,
10043713848SHaishuang Yan 			lockdep_is_held(&net->ipv4.tcp_fastopen_ctx_lock));
10143713848SHaishuang Yan 		rcu_assign_pointer(net->ipv4.tcp_fastopen_ctx, ctx);
1021fba70e5SYuchung Cheng 	}
1039eba9353SEric Dumazet 	spin_unlock(&net->ipv4.tcp_fastopen_ctx_lock);
10410467163SJerry Chu 
10510467163SJerry Chu 	if (octx)
10610467163SJerry Chu 		call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
1079092a76dSJason Baron out:
10810467163SJerry Chu 	return err;
10910467163SJerry Chu }
11010467163SJerry Chu 
111483642e5SChristoph Paasch static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req,
112483642e5SChristoph Paasch 					     struct sk_buff *syn,
113438ac880SArd Biesheuvel 					     const siphash_key_t *key,
114149479d0SYuchung Cheng 					     struct tcp_fastopen_cookie *foc)
11510467163SJerry Chu {
116c681edaeSArd Biesheuvel 	BUILD_BUG_ON(TCP_FASTOPEN_COOKIE_SIZE != sizeof(u64));
117c681edaeSArd Biesheuvel 
118483642e5SChristoph Paasch 	if (req->rsk_ops->family == AF_INET) {
119483642e5SChristoph Paasch 		const struct iphdr *iph = ip_hdr(syn);
12010467163SJerry Chu 
121438ac880SArd Biesheuvel 		foc->val[0] = cpu_to_le64(siphash(&iph->saddr,
122c681edaeSArd Biesheuvel 					  sizeof(iph->saddr) +
123c681edaeSArd Biesheuvel 					  sizeof(iph->daddr),
124438ac880SArd Biesheuvel 					  key));
12510467163SJerry Chu 		foc->len = TCP_FASTOPEN_COOKIE_SIZE;
126483642e5SChristoph Paasch 		return true;
12710467163SJerry Chu 	}
128483642e5SChristoph Paasch #if IS_ENABLED(CONFIG_IPV6)
129483642e5SChristoph Paasch 	if (req->rsk_ops->family == AF_INET6) {
130483642e5SChristoph Paasch 		const struct ipv6hdr *ip6h = ipv6_hdr(syn);
131483642e5SChristoph Paasch 
132438ac880SArd Biesheuvel 		foc->val[0] = cpu_to_le64(siphash(&ip6h->saddr,
133c681edaeSArd Biesheuvel 					  sizeof(ip6h->saddr) +
134c681edaeSArd Biesheuvel 					  sizeof(ip6h->daddr),
135438ac880SArd Biesheuvel 					  key));
136483642e5SChristoph Paasch 		foc->len = TCP_FASTOPEN_COOKIE_SIZE;
137483642e5SChristoph Paasch 		return true;
138483642e5SChristoph Paasch 	}
139483642e5SChristoph Paasch #endif
140483642e5SChristoph Paasch 	return false;
1413a19ce0eSDaniel Lee }
1423a19ce0eSDaniel Lee 
143c681edaeSArd Biesheuvel /* Generate the fastopen cookie by applying SipHash to both the source and
144c681edaeSArd Biesheuvel  * destination addresses.
1453a19ce0eSDaniel Lee  */
1469092a76dSJason Baron static void tcp_fastopen_cookie_gen(struct sock *sk,
14743713848SHaishuang Yan 				    struct request_sock *req,
1483a19ce0eSDaniel Lee 				    struct sk_buff *syn,
1493a19ce0eSDaniel Lee 				    struct tcp_fastopen_cookie *foc)
1503a19ce0eSDaniel Lee {
151483642e5SChristoph Paasch 	struct tcp_fastopen_context *ctx;
1523a19ce0eSDaniel Lee 
153483642e5SChristoph Paasch 	rcu_read_lock();
1549092a76dSJason Baron 	ctx = tcp_fastopen_get_ctx(sk);
155483642e5SChristoph Paasch 	if (ctx)
156438ac880SArd Biesheuvel 		__tcp_fastopen_cookie_gen_cipher(req, syn, &ctx->key[0], foc);
157483642e5SChristoph Paasch 	rcu_read_unlock();
15810467163SJerry Chu }
1595b7ed089SYuchung Cheng 
16061d2bcaeSEric Dumazet /* If an incoming SYN or SYNACK frame contains a payload and/or FIN,
16161d2bcaeSEric Dumazet  * queue this additional data / FIN.
16261d2bcaeSEric Dumazet  */
16361d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb)
16461d2bcaeSEric Dumazet {
16561d2bcaeSEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
16661d2bcaeSEric Dumazet 
16761d2bcaeSEric Dumazet 	if (TCP_SKB_CB(skb)->end_seq == tp->rcv_nxt)
16861d2bcaeSEric Dumazet 		return;
16961d2bcaeSEric Dumazet 
17061d2bcaeSEric Dumazet 	skb = skb_clone(skb, GFP_ATOMIC);
17161d2bcaeSEric Dumazet 	if (!skb)
17261d2bcaeSEric Dumazet 		return;
17361d2bcaeSEric Dumazet 
17461d2bcaeSEric Dumazet 	skb_dst_drop(skb);
175a44d6eacSMartin KaFai Lau 	/* segs_in has been initialized to 1 in tcp_create_openreq_child().
176a44d6eacSMartin KaFai Lau 	 * Hence, reset segs_in to 0 before calling tcp_segs_in()
177a44d6eacSMartin KaFai Lau 	 * to avoid double counting.  Also, tcp_segs_in() expects
178a44d6eacSMartin KaFai Lau 	 * skb->len to include the tcp_hdrlen.  Hence, it should
179a44d6eacSMartin KaFai Lau 	 * be called before __skb_pull().
180a44d6eacSMartin KaFai Lau 	 */
181a44d6eacSMartin KaFai Lau 	tp->segs_in = 0;
182a44d6eacSMartin KaFai Lau 	tcp_segs_in(tp, skb);
18361d2bcaeSEric Dumazet 	__skb_pull(skb, tcp_hdrlen(skb));
18476061f63SEric Dumazet 	sk_forced_mem_schedule(sk, skb->truesize);
18561d2bcaeSEric Dumazet 	skb_set_owner_r(skb, sk);
18661d2bcaeSEric Dumazet 
1879d691539SEric Dumazet 	TCP_SKB_CB(skb)->seq++;
1889d691539SEric Dumazet 	TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_SYN;
1899d691539SEric Dumazet 
19061d2bcaeSEric Dumazet 	tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
19161d2bcaeSEric Dumazet 	__skb_queue_tail(&sk->sk_receive_queue, skb);
19261d2bcaeSEric Dumazet 	tp->syn_data_acked = 1;
19361d2bcaeSEric Dumazet 
19461d2bcaeSEric Dumazet 	/* u64_stats_update_begin(&tp->syncp) not needed here,
19561d2bcaeSEric Dumazet 	 * as we certainly are not changing upper 32bit value (0)
19661d2bcaeSEric Dumazet 	 */
19761d2bcaeSEric Dumazet 	tp->bytes_received = skb->len;
198e3e17b77SEric Dumazet 
199e3e17b77SEric Dumazet 	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
200e3e17b77SEric Dumazet 		tcp_fin(sk);
20161d2bcaeSEric Dumazet }
20261d2bcaeSEric Dumazet 
2039092a76dSJason Baron /* returns 0 - no key match, 1 for primary, 2 for backup */
2049092a76dSJason Baron static int tcp_fastopen_cookie_gen_check(struct sock *sk,
2059092a76dSJason Baron 					 struct request_sock *req,
2069092a76dSJason Baron 					 struct sk_buff *syn,
2079092a76dSJason Baron 					 struct tcp_fastopen_cookie *orig,
2089092a76dSJason Baron 					 struct tcp_fastopen_cookie *valid_foc)
2099092a76dSJason Baron {
2109092a76dSJason Baron 	struct tcp_fastopen_cookie search_foc = { .len = -1 };
2119092a76dSJason Baron 	struct tcp_fastopen_cookie *foc = valid_foc;
2129092a76dSJason Baron 	struct tcp_fastopen_context *ctx;
2139092a76dSJason Baron 	int i, ret = 0;
2149092a76dSJason Baron 
2159092a76dSJason Baron 	rcu_read_lock();
2169092a76dSJason Baron 	ctx = tcp_fastopen_get_ctx(sk);
2179092a76dSJason Baron 	if (!ctx)
2189092a76dSJason Baron 		goto out;
2199092a76dSJason Baron 	for (i = 0; i < tcp_fastopen_context_len(ctx); i++) {
220438ac880SArd Biesheuvel 		__tcp_fastopen_cookie_gen_cipher(req, syn, &ctx->key[i], foc);
2219092a76dSJason Baron 		if (tcp_fastopen_cookie_match(foc, orig)) {
2229092a76dSJason Baron 			ret = i + 1;
2239092a76dSJason Baron 			goto out;
2249092a76dSJason Baron 		}
2259092a76dSJason Baron 		foc = &search_foc;
2269092a76dSJason Baron 	}
2279092a76dSJason Baron out:
2289092a76dSJason Baron 	rcu_read_unlock();
2299092a76dSJason Baron 	return ret;
2309092a76dSJason Baron }
2319092a76dSJason Baron 
2327c85af88SEric Dumazet static struct sock *tcp_fastopen_create_child(struct sock *sk,
2335b7ed089SYuchung Cheng 					      struct sk_buff *skb,
2345b7ed089SYuchung Cheng 					      struct request_sock *req)
2355b7ed089SYuchung Cheng {
23617846376SDave Jones 	struct tcp_sock *tp;
2375b7ed089SYuchung Cheng 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
2385b7ed089SYuchung Cheng 	struct sock *child;
2395e0724d0SEric Dumazet 	bool own_req;
2405b7ed089SYuchung Cheng 
2415e0724d0SEric Dumazet 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
2425e0724d0SEric Dumazet 							 NULL, &own_req);
24351456b29SIan Morris 	if (!child)
2447c85af88SEric Dumazet 		return NULL;
2455b7ed089SYuchung Cheng 
2460536fcc0SEric Dumazet 	spin_lock(&queue->fastopenq.lock);
2470536fcc0SEric Dumazet 	queue->fastopenq.qlen++;
2480536fcc0SEric Dumazet 	spin_unlock(&queue->fastopenq.lock);
2495b7ed089SYuchung Cheng 
2505b7ed089SYuchung Cheng 	/* Initialize the child socket. Have to fix some values to take
2515b7ed089SYuchung Cheng 	 * into account the child is a Fast Open socket and is created
2525b7ed089SYuchung Cheng 	 * only out of the bits carried in the SYN packet.
2535b7ed089SYuchung Cheng 	 */
2545b7ed089SYuchung Cheng 	tp = tcp_sk(child);
2555b7ed089SYuchung Cheng 
256d983ea6fSEric Dumazet 	rcu_assign_pointer(tp->fastopen_rsk, req);
2579439ce00SEric Dumazet 	tcp_rsk(req)->tfo_listener = true;
2585b7ed089SYuchung Cheng 
2595b7ed089SYuchung Cheng 	/* RFC1323: The window in SYN & SYN/ACK segments is never
2605b7ed089SYuchung Cheng 	 * scaled. So correct it appropriately.
2615b7ed089SYuchung Cheng 	 */
2625b7ed089SYuchung Cheng 	tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
2630dbd7ff3SAlexey Kodanev 	tp->max_window = tp->snd_wnd;
2645b7ed089SYuchung Cheng 
2655b7ed089SYuchung Cheng 	/* Activate the retrans timer so that SYNACK can be retransmitted.
266ca6fb065SEric Dumazet 	 * The request socket is not added to the ehash
2675b7ed089SYuchung Cheng 	 * because it's been added to the accept queue directly.
2685b7ed089SYuchung Cheng 	 */
2695b7ed089SYuchung Cheng 	inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
2705b7ed089SYuchung Cheng 				  TCP_TIMEOUT_INIT, TCP_RTO_MAX);
2715b7ed089SYuchung Cheng 
27241c6d650SReshetova, Elena 	refcount_set(&req->rsk_refcnt, 2);
2735b7ed089SYuchung Cheng 
2745b7ed089SYuchung Cheng 	/* Now finish processing the fastopen child socket. */
27527204aaaSWei Wang 	tcp_init_transfer(child, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB);
2765b7ed089SYuchung Cheng 
27761d2bcaeSEric Dumazet 	tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
278ba34e6d9SEric Dumazet 
27961d2bcaeSEric Dumazet 	tcp_fastopen_add_skb(child, skb);
280d654976cSEric Dumazet 
28161d2bcaeSEric Dumazet 	tcp_rsk(req)->rcv_nxt = tp->rcv_nxt;
28228b346cbSNeal Cardwell 	tp->rcv_wup = tp->rcv_nxt;
2837656d842SEric Dumazet 	/* tcp_conn_request() is sending the SYNACK,
2847656d842SEric Dumazet 	 * and queues the child into listener accept queue.
2857c85af88SEric Dumazet 	 */
2867c85af88SEric Dumazet 	return child;
2875b7ed089SYuchung Cheng }
2885b7ed089SYuchung Cheng 
2895b7ed089SYuchung Cheng static bool tcp_fastopen_queue_check(struct sock *sk)
2905b7ed089SYuchung Cheng {
2915b7ed089SYuchung Cheng 	struct fastopen_queue *fastopenq;
2925b7ed089SYuchung Cheng 
2935b7ed089SYuchung Cheng 	/* Make sure the listener has enabled fastopen, and we don't
2945b7ed089SYuchung Cheng 	 * exceed the max # of pending TFO requests allowed before trying
2955b7ed089SYuchung Cheng 	 * to validating the cookie in order to avoid burning CPU cycles
2965b7ed089SYuchung Cheng 	 * unnecessarily.
2975b7ed089SYuchung Cheng 	 *
2985b7ed089SYuchung Cheng 	 * XXX (TFO) - The implication of checking the max_qlen before
2995b7ed089SYuchung Cheng 	 * processing a cookie request is that clients can't differentiate
3005b7ed089SYuchung Cheng 	 * between qlen overflow causing Fast Open to be disabled
3015b7ed089SYuchung Cheng 	 * temporarily vs a server not supporting Fast Open at all.
3025b7ed089SYuchung Cheng 	 */
3030536fcc0SEric Dumazet 	fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq;
3040536fcc0SEric Dumazet 	if (fastopenq->max_qlen == 0)
3055b7ed089SYuchung Cheng 		return false;
3065b7ed089SYuchung Cheng 
3075b7ed089SYuchung Cheng 	if (fastopenq->qlen >= fastopenq->max_qlen) {
3085b7ed089SYuchung Cheng 		struct request_sock *req1;
3095b7ed089SYuchung Cheng 		spin_lock(&fastopenq->lock);
3105b7ed089SYuchung Cheng 		req1 = fastopenq->rskq_rst_head;
311fa76ce73SEric Dumazet 		if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) {
31202a1d6e7SEric Dumazet 			__NET_INC_STATS(sock_net(sk),
3135b7ed089SYuchung Cheng 					LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
314c10d9310SEric Dumazet 			spin_unlock(&fastopenq->lock);
3155b7ed089SYuchung Cheng 			return false;
3165b7ed089SYuchung Cheng 		}
3175b7ed089SYuchung Cheng 		fastopenq->rskq_rst_head = req1->dl_next;
3185b7ed089SYuchung Cheng 		fastopenq->qlen--;
3195b7ed089SYuchung Cheng 		spin_unlock(&fastopenq->lock);
32013854e5aSEric Dumazet 		reqsk_put(req1);
3215b7ed089SYuchung Cheng 	}
3225b7ed089SYuchung Cheng 	return true;
3235b7ed089SYuchung Cheng }
3245b7ed089SYuchung Cheng 
32571c02379SChristoph Paasch static bool tcp_fastopen_no_cookie(const struct sock *sk,
32671c02379SChristoph Paasch 				   const struct dst_entry *dst,
32771c02379SChristoph Paasch 				   int flag)
32871c02379SChristoph Paasch {
32971c02379SChristoph Paasch 	return (sock_net(sk)->ipv4.sysctl_tcp_fastopen & flag) ||
33071c02379SChristoph Paasch 	       tcp_sk(sk)->fastopen_no_cookie ||
33171c02379SChristoph Paasch 	       (dst && dst_metric(dst, RTAX_FASTOPEN_NO_COOKIE));
33271c02379SChristoph Paasch }
33371c02379SChristoph Paasch 
33489278c9dSYuchung Cheng /* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
33589278c9dSYuchung Cheng  * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
33689278c9dSYuchung Cheng  * cookie request (foc->len == 0).
33789278c9dSYuchung Cheng  */
3387c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
3395b7ed089SYuchung Cheng 			      struct request_sock *req,
34071c02379SChristoph Paasch 			      struct tcp_fastopen_cookie *foc,
34171c02379SChristoph Paasch 			      const struct dst_entry *dst)
3425b7ed089SYuchung Cheng {
34389278c9dSYuchung Cheng 	bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
344e1cfcbe8SHaishuang Yan 	int tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
345e1cfcbe8SHaishuang Yan 	struct tcp_fastopen_cookie valid_foc = { .len = -1 };
3467c85af88SEric Dumazet 	struct sock *child;
3479092a76dSJason Baron 	int ret = 0;
3485b7ed089SYuchung Cheng 
349531c94a9SYuchung Cheng 	if (foc->len == 0) /* Client requests a cookie */
350c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
351531c94a9SYuchung Cheng 
352e1cfcbe8SHaishuang Yan 	if (!((tcp_fastopen & TFO_SERVER_ENABLE) &&
35389278c9dSYuchung Cheng 	      (syn_data || foc->len >= 0) &&
35489278c9dSYuchung Cheng 	      tcp_fastopen_queue_check(sk))) {
35589278c9dSYuchung Cheng 		foc->len = -1;
3567c85af88SEric Dumazet 		return NULL;
3575b7ed089SYuchung Cheng 	}
35889278c9dSYuchung Cheng 
35971c02379SChristoph Paasch 	if (syn_data &&
36071c02379SChristoph Paasch 	    tcp_fastopen_no_cookie(sk, dst, TFO_SERVER_COOKIE_NOT_REQD))
36189278c9dSYuchung Cheng 		goto fastopen;
36289278c9dSYuchung Cheng 
3639092a76dSJason Baron 	if (foc->len == 0) {
3649092a76dSJason Baron 		/* Client requests a cookie. */
3659092a76dSJason Baron 		tcp_fastopen_cookie_gen(sk, req, skb, &valid_foc);
3669092a76dSJason Baron 	} else if (foc->len > 0) {
3679092a76dSJason Baron 		ret = tcp_fastopen_cookie_gen_check(sk, req, skb, foc,
3689092a76dSJason Baron 						    &valid_foc);
3699092a76dSJason Baron 		if (!ret) {
3709092a76dSJason Baron 			NET_INC_STATS(sock_net(sk),
3719092a76dSJason Baron 				      LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
3729092a76dSJason Baron 		} else {
3739092a76dSJason Baron 			/* Cookie is valid. Create a (full) child socket to
3749092a76dSJason Baron 			 * accept the data in SYN before returning a SYN-ACK to
3759092a76dSJason Baron 			 * ack the data. If we fail to create the socket, fall
3769092a76dSJason Baron 			 * back and ack the ISN only but includes the same
3779092a76dSJason Baron 			 * cookie.
378843f4a55SYuchung Cheng 			 *
3799092a76dSJason Baron 			 * Note: Data-less SYN with valid cookie is allowed to
3809092a76dSJason Baron 			 * send data in SYN_RECV state.
381843f4a55SYuchung Cheng 			 */
38289278c9dSYuchung Cheng fastopen:
38311199369STonghao Zhang 			child = tcp_fastopen_create_child(sk, skb, req);
3847c85af88SEric Dumazet 			if (child) {
3859092a76dSJason Baron 				if (ret == 2) {
3869092a76dSJason Baron 					valid_foc.exp = foc->exp;
3879092a76dSJason Baron 					*foc = valid_foc;
3889092a76dSJason Baron 					NET_INC_STATS(sock_net(sk),
3899092a76dSJason Baron 						      LINUX_MIB_TCPFASTOPENPASSIVEALTKEY);
3909092a76dSJason Baron 				} else {
39189278c9dSYuchung Cheng 					foc->len = -1;
3929092a76dSJason Baron 				}
393c10d9310SEric Dumazet 				NET_INC_STATS(sock_net(sk),
394843f4a55SYuchung Cheng 					      LINUX_MIB_TCPFASTOPENPASSIVE);
3957c85af88SEric Dumazet 				return child;
3965b7ed089SYuchung Cheng 			}
3979092a76dSJason Baron 			NET_INC_STATS(sock_net(sk),
3989092a76dSJason Baron 				      LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
3999092a76dSJason Baron 		}
4009092a76dSJason Baron 	}
4017f9b838bSDaniel Lee 	valid_foc.exp = foc->exp;
40289278c9dSYuchung Cheng 	*foc = valid_foc;
4037c85af88SEric Dumazet 	return NULL;
4045b7ed089SYuchung Cheng }
405065263f4SWei Wang 
406065263f4SWei Wang bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
407065263f4SWei Wang 			       struct tcp_fastopen_cookie *cookie)
408065263f4SWei Wang {
40971c02379SChristoph Paasch 	const struct dst_entry *dst;
410065263f4SWei Wang 
4117268586bSYuchung Cheng 	tcp_fastopen_cache_get(sk, mss, cookie);
412cf1ef3f0SWei Wang 
413cf1ef3f0SWei Wang 	/* Firewall blackhole issue check */
414cf1ef3f0SWei Wang 	if (tcp_fastopen_active_should_disable(sk)) {
415cf1ef3f0SWei Wang 		cookie->len = -1;
416cf1ef3f0SWei Wang 		return false;
417cf1ef3f0SWei Wang 	}
418cf1ef3f0SWei Wang 
41971c02379SChristoph Paasch 	dst = __sk_dst_get(sk);
42071c02379SChristoph Paasch 
42171c02379SChristoph Paasch 	if (tcp_fastopen_no_cookie(sk, dst, TFO_CLIENT_NO_COOKIE)) {
422065263f4SWei Wang 		cookie->len = -1;
423065263f4SWei Wang 		return true;
424065263f4SWei Wang 	}
425*48027478SJason Baron 	if (cookie->len > 0)
426*48027478SJason Baron 		return true;
427*48027478SJason Baron 	tcp_sk(sk)->fastopen_client_fail = TFO_COOKIE_UNAVAILABLE;
428*48027478SJason Baron 	return false;
429065263f4SWei Wang }
43019f6d3f3SWei Wang 
43119f6d3f3SWei Wang /* This function checks if we want to defer sending SYN until the first
43219f6d3f3SWei Wang  * write().  We defer under the following conditions:
43319f6d3f3SWei Wang  * 1. fastopen_connect sockopt is set
43419f6d3f3SWei Wang  * 2. we have a valid cookie
43519f6d3f3SWei Wang  * Return value: return true if we want to defer until application writes data
43619f6d3f3SWei Wang  *               return false if we want to send out SYN immediately
43719f6d3f3SWei Wang  */
43819f6d3f3SWei Wang bool tcp_fastopen_defer_connect(struct sock *sk, int *err)
43919f6d3f3SWei Wang {
44019f6d3f3SWei Wang 	struct tcp_fastopen_cookie cookie = { .len = 0 };
44119f6d3f3SWei Wang 	struct tcp_sock *tp = tcp_sk(sk);
44219f6d3f3SWei Wang 	u16 mss;
44319f6d3f3SWei Wang 
44419f6d3f3SWei Wang 	if (tp->fastopen_connect && !tp->fastopen_req) {
44519f6d3f3SWei Wang 		if (tcp_fastopen_cookie_check(sk, &mss, &cookie)) {
44619f6d3f3SWei Wang 			inet_sk(sk)->defer_connect = 1;
44719f6d3f3SWei Wang 			return true;
44819f6d3f3SWei Wang 		}
44919f6d3f3SWei Wang 
45019f6d3f3SWei Wang 		/* Alloc fastopen_req in order for FO option to be included
45119f6d3f3SWei Wang 		 * in SYN
45219f6d3f3SWei Wang 		 */
45319f6d3f3SWei Wang 		tp->fastopen_req = kzalloc(sizeof(*tp->fastopen_req),
45419f6d3f3SWei Wang 					   sk->sk_allocation);
45519f6d3f3SWei Wang 		if (tp->fastopen_req)
45619f6d3f3SWei Wang 			tp->fastopen_req->cookie = cookie;
45719f6d3f3SWei Wang 		else
45819f6d3f3SWei Wang 			*err = -ENOBUFS;
45919f6d3f3SWei Wang 	}
46019f6d3f3SWei Wang 	return false;
46119f6d3f3SWei Wang }
46219f6d3f3SWei Wang EXPORT_SYMBOL(tcp_fastopen_defer_connect);
463cf1ef3f0SWei Wang 
464cf1ef3f0SWei Wang /*
465cf1ef3f0SWei Wang  * The following code block is to deal with middle box issues with TFO:
466cf1ef3f0SWei Wang  * Middlebox firewall issues can potentially cause server's data being
467cf1ef3f0SWei Wang  * blackholed after a successful 3WHS using TFO.
468cf1ef3f0SWei Wang  * The proposed solution is to disable active TFO globally under the
469cf1ef3f0SWei Wang  * following circumstances:
470cf1ef3f0SWei Wang  *   1. client side TFO socket receives out of order FIN
471cf1ef3f0SWei Wang  *   2. client side TFO socket receives out of order RST
4727268586bSYuchung Cheng  *   3. client side TFO socket has timed out three times consecutively during
4737268586bSYuchung Cheng  *      or after handshake
474cf1ef3f0SWei Wang  * We disable active side TFO globally for 1hr at first. Then if it
475cf1ef3f0SWei Wang  * happens again, we disable it for 2h, then 4h, 8h, ...
476cf1ef3f0SWei Wang  * And we reset the timeout back to 1hr when we see a successful active
477cf1ef3f0SWei Wang  * TFO connection with data exchanges.
478cf1ef3f0SWei Wang  */
479cf1ef3f0SWei Wang 
480cf1ef3f0SWei Wang /* Disable active TFO and record current jiffies and
481cf1ef3f0SWei Wang  * tfo_active_disable_times
482cf1ef3f0SWei Wang  */
48346c2fa39SWei Wang void tcp_fastopen_active_disable(struct sock *sk)
484cf1ef3f0SWei Wang {
4853733be14SHaishuang Yan 	struct net *net = sock_net(sk);
486cf1ef3f0SWei Wang 
4873733be14SHaishuang Yan 	atomic_inc(&net->ipv4.tfo_active_disable_times);
4883733be14SHaishuang Yan 	net->ipv4.tfo_active_disable_stamp = jiffies;
4893733be14SHaishuang Yan 	NET_INC_STATS(net, LINUX_MIB_TCPFASTOPENBLACKHOLE);
490cf1ef3f0SWei Wang }
491cf1ef3f0SWei Wang 
492cf1ef3f0SWei Wang /* Calculate timeout for tfo active disable
493cf1ef3f0SWei Wang  * Return true if we are still in the active TFO disable period
494cf1ef3f0SWei Wang  * Return false if timeout already expired and we should use active TFO
495cf1ef3f0SWei Wang  */
496cf1ef3f0SWei Wang bool tcp_fastopen_active_should_disable(struct sock *sk)
497cf1ef3f0SWei Wang {
4983733be14SHaishuang Yan 	unsigned int tfo_bh_timeout = sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout;
4993733be14SHaishuang Yan 	int tfo_da_times = atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times);
500cf1ef3f0SWei Wang 	unsigned long timeout;
5013733be14SHaishuang Yan 	int multiplier;
502cf1ef3f0SWei Wang 
503cf1ef3f0SWei Wang 	if (!tfo_da_times)
504cf1ef3f0SWei Wang 		return false;
505cf1ef3f0SWei Wang 
506cf1ef3f0SWei Wang 	/* Limit timout to max: 2^6 * initial timeout */
507cf1ef3f0SWei Wang 	multiplier = 1 << min(tfo_da_times - 1, 6);
5083733be14SHaishuang Yan 	timeout = multiplier * tfo_bh_timeout * HZ;
5093733be14SHaishuang Yan 	if (time_before(jiffies, sock_net(sk)->ipv4.tfo_active_disable_stamp + timeout))
510cf1ef3f0SWei Wang 		return true;
511cf1ef3f0SWei Wang 
512cf1ef3f0SWei Wang 	/* Mark check bit so we can check for successful active TFO
513cf1ef3f0SWei Wang 	 * condition and reset tfo_active_disable_times
514cf1ef3f0SWei Wang 	 */
515cf1ef3f0SWei Wang 	tcp_sk(sk)->syn_fastopen_ch = 1;
516cf1ef3f0SWei Wang 	return false;
517cf1ef3f0SWei Wang }
518cf1ef3f0SWei Wang 
519cf1ef3f0SWei Wang /* Disable active TFO if FIN is the only packet in the ofo queue
520cf1ef3f0SWei Wang  * and no data is received.
521cf1ef3f0SWei Wang  * Also check if we can reset tfo_active_disable_times if data is
522cf1ef3f0SWei Wang  * received successfully on a marked active TFO sockets opened on
523cf1ef3f0SWei Wang  * a non-loopback interface
524cf1ef3f0SWei Wang  */
525cf1ef3f0SWei Wang void tcp_fastopen_active_disable_ofo_check(struct sock *sk)
526cf1ef3f0SWei Wang {
527cf1ef3f0SWei Wang 	struct tcp_sock *tp = tcp_sk(sk);
528cf1ef3f0SWei Wang 	struct dst_entry *dst;
52918a4c0eaSEric Dumazet 	struct sk_buff *skb;
530cf1ef3f0SWei Wang 
531cf1ef3f0SWei Wang 	if (!tp->syn_fastopen)
532cf1ef3f0SWei Wang 		return;
533cf1ef3f0SWei Wang 
534cf1ef3f0SWei Wang 	if (!tp->data_segs_in) {
53518a4c0eaSEric Dumazet 		skb = skb_rb_first(&tp->out_of_order_queue);
53618a4c0eaSEric Dumazet 		if (skb && !skb_rb_next(skb)) {
537cf1ef3f0SWei Wang 			if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
53846c2fa39SWei Wang 				tcp_fastopen_active_disable(sk);
539cf1ef3f0SWei Wang 				return;
540cf1ef3f0SWei Wang 			}
541cf1ef3f0SWei Wang 		}
542cf1ef3f0SWei Wang 	} else if (tp->syn_fastopen_ch &&
5433733be14SHaishuang Yan 		   atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times)) {
544cf1ef3f0SWei Wang 		dst = sk_dst_get(sk);
545cf1ef3f0SWei Wang 		if (!(dst && dst->dev && (dst->dev->flags & IFF_LOOPBACK)))
5463733be14SHaishuang Yan 			atomic_set(&sock_net(sk)->ipv4.tfo_active_disable_times, 0);
547cf1ef3f0SWei Wang 		dst_release(dst);
548cf1ef3f0SWei Wang 	}
549cf1ef3f0SWei Wang }
5507268586bSYuchung Cheng 
5517268586bSYuchung Cheng void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired)
5527268586bSYuchung Cheng {
5537268586bSYuchung Cheng 	u32 timeouts = inet_csk(sk)->icsk_retransmits;
5547268586bSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
5557268586bSYuchung Cheng 
5567268586bSYuchung Cheng 	/* Broken middle-boxes may black-hole Fast Open connection during or
5577268586bSYuchung Cheng 	 * even after the handshake. Be extremely conservative and pause
5587268586bSYuchung Cheng 	 * Fast Open globally after hitting the third consecutive timeout or
5597268586bSYuchung Cheng 	 * exceeding the configured timeout limit.
5607268586bSYuchung Cheng 	 */
5617268586bSYuchung Cheng 	if ((tp->syn_fastopen || tp->syn_data || tp->syn_data_acked) &&
5627268586bSYuchung Cheng 	    (timeouts == 2 || (timeouts < 2 && expired))) {
5637268586bSYuchung Cheng 		tcp_fastopen_active_disable(sk);
5647268586bSYuchung Cheng 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVEFAIL);
5657268586bSYuchung Cheng 	}
5667268586bSYuchung Cheng }
567