xref: /openbmc/linux/net/ipv4/tcp_fastopen.c (revision 003c9410)
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 
120d41cca4SYuchung Cheng int sysctl_tcp_fastopen __read_mostly = TFO_CLIENT_ENABLE;
1310467163SJerry Chu 
1410467163SJerry Chu struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
1510467163SJerry Chu 
1610467163SJerry Chu static DEFINE_SPINLOCK(tcp_fastopen_ctx_lock);
1710467163SJerry Chu 
18222e83d2SHannes Frederic Sowa void tcp_fastopen_init_key_once(bool publish)
19222e83d2SHannes Frederic Sowa {
20222e83d2SHannes Frederic Sowa 	static u8 key[TCP_FASTOPEN_KEY_LENGTH];
21222e83d2SHannes Frederic Sowa 
22222e83d2SHannes Frederic Sowa 	/* tcp_fastopen_reset_cipher publishes the new context
23222e83d2SHannes Frederic Sowa 	 * atomically, so we allow this race happening here.
24222e83d2SHannes Frederic Sowa 	 *
25222e83d2SHannes Frederic Sowa 	 * All call sites of tcp_fastopen_cookie_gen also check
26222e83d2SHannes Frederic Sowa 	 * for a valid cookie, so this is an acceptable risk.
27222e83d2SHannes Frederic Sowa 	 */
28222e83d2SHannes Frederic Sowa 	if (net_get_random_once(key, sizeof(key)) && publish)
29222e83d2SHannes Frederic Sowa 		tcp_fastopen_reset_cipher(key, sizeof(key));
30222e83d2SHannes Frederic Sowa }
31222e83d2SHannes Frederic Sowa 
3210467163SJerry Chu static void tcp_fastopen_ctx_free(struct rcu_head *head)
3310467163SJerry Chu {
3410467163SJerry Chu 	struct tcp_fastopen_context *ctx =
3510467163SJerry Chu 	    container_of(head, struct tcp_fastopen_context, rcu);
3610467163SJerry Chu 	crypto_free_cipher(ctx->tfm);
3710467163SJerry Chu 	kfree(ctx);
3810467163SJerry Chu }
3910467163SJerry Chu 
4010467163SJerry Chu int tcp_fastopen_reset_cipher(void *key, unsigned int len)
4110467163SJerry Chu {
4210467163SJerry Chu 	int err;
4310467163SJerry Chu 	struct tcp_fastopen_context *ctx, *octx;
4410467163SJerry Chu 
4510467163SJerry Chu 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
4610467163SJerry Chu 	if (!ctx)
4710467163SJerry Chu 		return -ENOMEM;
4810467163SJerry Chu 	ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
4910467163SJerry Chu 
5010467163SJerry Chu 	if (IS_ERR(ctx->tfm)) {
5110467163SJerry Chu 		err = PTR_ERR(ctx->tfm);
5210467163SJerry Chu error:		kfree(ctx);
5310467163SJerry Chu 		pr_err("TCP: TFO aes cipher alloc error: %d\n", err);
5410467163SJerry Chu 		return err;
5510467163SJerry Chu 	}
5610467163SJerry Chu 	err = crypto_cipher_setkey(ctx->tfm, key, len);
5710467163SJerry Chu 	if (err) {
5810467163SJerry Chu 		pr_err("TCP: TFO cipher key error: %d\n", err);
5910467163SJerry Chu 		crypto_free_cipher(ctx->tfm);
6010467163SJerry Chu 		goto error;
6110467163SJerry Chu 	}
6210467163SJerry Chu 	memcpy(ctx->key, key, len);
6310467163SJerry Chu 
6410467163SJerry Chu 	spin_lock(&tcp_fastopen_ctx_lock);
6510467163SJerry Chu 
6610467163SJerry Chu 	octx = rcu_dereference_protected(tcp_fastopen_ctx,
6710467163SJerry Chu 				lockdep_is_held(&tcp_fastopen_ctx_lock));
6810467163SJerry Chu 	rcu_assign_pointer(tcp_fastopen_ctx, ctx);
6910467163SJerry Chu 	spin_unlock(&tcp_fastopen_ctx_lock);
7010467163SJerry Chu 
7110467163SJerry Chu 	if (octx)
7210467163SJerry Chu 		call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
7310467163SJerry Chu 	return err;
7410467163SJerry Chu }
7510467163SJerry Chu 
763a19ce0eSDaniel Lee static bool __tcp_fastopen_cookie_gen(const void *path,
77149479d0SYuchung Cheng 				      struct tcp_fastopen_cookie *foc)
7810467163SJerry Chu {
7910467163SJerry Chu 	struct tcp_fastopen_context *ctx;
803a19ce0eSDaniel Lee 	bool ok = false;
8110467163SJerry Chu 
8210467163SJerry Chu 	rcu_read_lock();
8310467163SJerry Chu 	ctx = rcu_dereference(tcp_fastopen_ctx);
8410467163SJerry Chu 	if (ctx) {
853a19ce0eSDaniel Lee 		crypto_cipher_encrypt_one(ctx->tfm, foc->val, path);
8610467163SJerry Chu 		foc->len = TCP_FASTOPEN_COOKIE_SIZE;
873a19ce0eSDaniel Lee 		ok = true;
8810467163SJerry Chu 	}
8910467163SJerry Chu 	rcu_read_unlock();
903a19ce0eSDaniel Lee 	return ok;
913a19ce0eSDaniel Lee }
923a19ce0eSDaniel Lee 
933a19ce0eSDaniel Lee /* Generate the fastopen cookie by doing aes128 encryption on both
943a19ce0eSDaniel Lee  * the source and destination addresses. Pad 0s for IPv4 or IPv4-mapped-IPv6
953a19ce0eSDaniel Lee  * addresses. For the longer IPv6 addresses use CBC-MAC.
963a19ce0eSDaniel Lee  *
973a19ce0eSDaniel Lee  * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE.
983a19ce0eSDaniel Lee  */
993a19ce0eSDaniel Lee static bool tcp_fastopen_cookie_gen(struct request_sock *req,
1003a19ce0eSDaniel Lee 				    struct sk_buff *syn,
1013a19ce0eSDaniel Lee 				    struct tcp_fastopen_cookie *foc)
1023a19ce0eSDaniel Lee {
1033a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET) {
1043a19ce0eSDaniel Lee 		const struct iphdr *iph = ip_hdr(syn);
1053a19ce0eSDaniel Lee 
1063a19ce0eSDaniel Lee 		__be32 path[4] = { iph->saddr, iph->daddr, 0, 0 };
1073a19ce0eSDaniel Lee 		return __tcp_fastopen_cookie_gen(path, foc);
1083a19ce0eSDaniel Lee 	}
1093a19ce0eSDaniel Lee 
1103a19ce0eSDaniel Lee #if IS_ENABLED(CONFIG_IPV6)
1113a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET6) {
1123a19ce0eSDaniel Lee 		const struct ipv6hdr *ip6h = ipv6_hdr(syn);
1133a19ce0eSDaniel Lee 		struct tcp_fastopen_cookie tmp;
1143a19ce0eSDaniel Lee 
1153a19ce0eSDaniel Lee 		if (__tcp_fastopen_cookie_gen(&ip6h->saddr, &tmp)) {
116003c9410SShannon Nelson 			struct in6_addr *buf = &tmp.addr;
11741c91996SLi RongQing 			int i;
1183a19ce0eSDaniel Lee 
1193a19ce0eSDaniel Lee 			for (i = 0; i < 4; i++)
1203a19ce0eSDaniel Lee 				buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i];
1213a19ce0eSDaniel Lee 			return __tcp_fastopen_cookie_gen(buf, foc);
1223a19ce0eSDaniel Lee 		}
1233a19ce0eSDaniel Lee 	}
1243a19ce0eSDaniel Lee #endif
1253a19ce0eSDaniel Lee 	return false;
12610467163SJerry Chu }
1275b7ed089SYuchung Cheng 
12861d2bcaeSEric Dumazet 
12961d2bcaeSEric Dumazet /* If an incoming SYN or SYNACK frame contains a payload and/or FIN,
13061d2bcaeSEric Dumazet  * queue this additional data / FIN.
13161d2bcaeSEric Dumazet  */
13261d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb)
13361d2bcaeSEric Dumazet {
13461d2bcaeSEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
13561d2bcaeSEric Dumazet 
13661d2bcaeSEric Dumazet 	if (TCP_SKB_CB(skb)->end_seq == tp->rcv_nxt)
13761d2bcaeSEric Dumazet 		return;
13861d2bcaeSEric Dumazet 
13961d2bcaeSEric Dumazet 	skb = skb_clone(skb, GFP_ATOMIC);
14061d2bcaeSEric Dumazet 	if (!skb)
14161d2bcaeSEric Dumazet 		return;
14261d2bcaeSEric Dumazet 
14361d2bcaeSEric Dumazet 	skb_dst_drop(skb);
144a44d6eacSMartin KaFai Lau 	/* segs_in has been initialized to 1 in tcp_create_openreq_child().
145a44d6eacSMartin KaFai Lau 	 * Hence, reset segs_in to 0 before calling tcp_segs_in()
146a44d6eacSMartin KaFai Lau 	 * to avoid double counting.  Also, tcp_segs_in() expects
147a44d6eacSMartin KaFai Lau 	 * skb->len to include the tcp_hdrlen.  Hence, it should
148a44d6eacSMartin KaFai Lau 	 * be called before __skb_pull().
149a44d6eacSMartin KaFai Lau 	 */
150a44d6eacSMartin KaFai Lau 	tp->segs_in = 0;
151a44d6eacSMartin KaFai Lau 	tcp_segs_in(tp, skb);
15261d2bcaeSEric Dumazet 	__skb_pull(skb, tcp_hdrlen(skb));
15376061f63SEric Dumazet 	sk_forced_mem_schedule(sk, skb->truesize);
15461d2bcaeSEric Dumazet 	skb_set_owner_r(skb, sk);
15561d2bcaeSEric Dumazet 
1569d691539SEric Dumazet 	TCP_SKB_CB(skb)->seq++;
1579d691539SEric Dumazet 	TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_SYN;
1589d691539SEric Dumazet 
15961d2bcaeSEric Dumazet 	tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
16061d2bcaeSEric Dumazet 	__skb_queue_tail(&sk->sk_receive_queue, skb);
16161d2bcaeSEric Dumazet 	tp->syn_data_acked = 1;
16261d2bcaeSEric Dumazet 
16361d2bcaeSEric Dumazet 	/* u64_stats_update_begin(&tp->syncp) not needed here,
16461d2bcaeSEric Dumazet 	 * as we certainly are not changing upper 32bit value (0)
16561d2bcaeSEric Dumazet 	 */
16661d2bcaeSEric Dumazet 	tp->bytes_received = skb->len;
167e3e17b77SEric Dumazet 
168e3e17b77SEric Dumazet 	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
169e3e17b77SEric Dumazet 		tcp_fin(sk);
17061d2bcaeSEric Dumazet }
17161d2bcaeSEric Dumazet 
1727c85af88SEric Dumazet static struct sock *tcp_fastopen_create_child(struct sock *sk,
1735b7ed089SYuchung Cheng 					      struct sk_buff *skb,
174843f4a55SYuchung Cheng 					      struct dst_entry *dst,
1755b7ed089SYuchung Cheng 					      struct request_sock *req)
1765b7ed089SYuchung Cheng {
17717846376SDave Jones 	struct tcp_sock *tp;
1785b7ed089SYuchung Cheng 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
1795b7ed089SYuchung Cheng 	struct sock *child;
1805e0724d0SEric Dumazet 	bool own_req;
1815b7ed089SYuchung Cheng 
1825b7ed089SYuchung Cheng 	req->num_retrans = 0;
1835b7ed089SYuchung Cheng 	req->num_timeout = 0;
1845b7ed089SYuchung Cheng 	req->sk = NULL;
1855b7ed089SYuchung Cheng 
1865e0724d0SEric Dumazet 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
1875e0724d0SEric Dumazet 							 NULL, &own_req);
18851456b29SIan Morris 	if (!child)
1897c85af88SEric Dumazet 		return NULL;
1905b7ed089SYuchung Cheng 
1910536fcc0SEric Dumazet 	spin_lock(&queue->fastopenq.lock);
1920536fcc0SEric Dumazet 	queue->fastopenq.qlen++;
1930536fcc0SEric Dumazet 	spin_unlock(&queue->fastopenq.lock);
1945b7ed089SYuchung Cheng 
1955b7ed089SYuchung Cheng 	/* Initialize the child socket. Have to fix some values to take
1965b7ed089SYuchung Cheng 	 * into account the child is a Fast Open socket and is created
1975b7ed089SYuchung Cheng 	 * only out of the bits carried in the SYN packet.
1985b7ed089SYuchung Cheng 	 */
1995b7ed089SYuchung Cheng 	tp = tcp_sk(child);
2005b7ed089SYuchung Cheng 
2015b7ed089SYuchung Cheng 	tp->fastopen_rsk = req;
2029439ce00SEric Dumazet 	tcp_rsk(req)->tfo_listener = true;
2035b7ed089SYuchung Cheng 
2045b7ed089SYuchung Cheng 	/* RFC1323: The window in SYN & SYN/ACK segments is never
2055b7ed089SYuchung Cheng 	 * scaled. So correct it appropriately.
2065b7ed089SYuchung Cheng 	 */
2075b7ed089SYuchung Cheng 	tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
2085b7ed089SYuchung Cheng 
2095b7ed089SYuchung Cheng 	/* Activate the retrans timer so that SYNACK can be retransmitted.
210ca6fb065SEric Dumazet 	 * The request socket is not added to the ehash
2115b7ed089SYuchung Cheng 	 * because it's been added to the accept queue directly.
2125b7ed089SYuchung Cheng 	 */
2135b7ed089SYuchung Cheng 	inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
2145b7ed089SYuchung Cheng 				  TCP_TIMEOUT_INIT, TCP_RTO_MAX);
2155b7ed089SYuchung Cheng 
216ca6fb065SEric Dumazet 	atomic_set(&req->rsk_refcnt, 2);
2175b7ed089SYuchung Cheng 
2185b7ed089SYuchung Cheng 	/* Now finish processing the fastopen child socket. */
2195b7ed089SYuchung Cheng 	inet_csk(child)->icsk_af_ops->rebuild_header(child);
2205b7ed089SYuchung Cheng 	tcp_init_congestion_control(child);
2215b7ed089SYuchung Cheng 	tcp_mtup_init(child);
2225b7ed089SYuchung Cheng 	tcp_init_metrics(child);
2235b7ed089SYuchung Cheng 	tcp_init_buffer_space(child);
2245b7ed089SYuchung Cheng 
22561d2bcaeSEric Dumazet 	tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
226ba34e6d9SEric Dumazet 
22761d2bcaeSEric Dumazet 	tcp_fastopen_add_skb(child, skb);
228d654976cSEric Dumazet 
22961d2bcaeSEric Dumazet 	tcp_rsk(req)->rcv_nxt = tp->rcv_nxt;
23028b346cbSNeal Cardwell 	tp->rcv_wup = tp->rcv_nxt;
2317656d842SEric Dumazet 	/* tcp_conn_request() is sending the SYNACK,
2327656d842SEric Dumazet 	 * and queues the child into listener accept queue.
2337c85af88SEric Dumazet 	 */
2347c85af88SEric Dumazet 	return child;
2355b7ed089SYuchung Cheng }
2365b7ed089SYuchung Cheng 
2375b7ed089SYuchung Cheng static bool tcp_fastopen_queue_check(struct sock *sk)
2385b7ed089SYuchung Cheng {
2395b7ed089SYuchung Cheng 	struct fastopen_queue *fastopenq;
2405b7ed089SYuchung Cheng 
2415b7ed089SYuchung Cheng 	/* Make sure the listener has enabled fastopen, and we don't
2425b7ed089SYuchung Cheng 	 * exceed the max # of pending TFO requests allowed before trying
2435b7ed089SYuchung Cheng 	 * to validating the cookie in order to avoid burning CPU cycles
2445b7ed089SYuchung Cheng 	 * unnecessarily.
2455b7ed089SYuchung Cheng 	 *
2465b7ed089SYuchung Cheng 	 * XXX (TFO) - The implication of checking the max_qlen before
2475b7ed089SYuchung Cheng 	 * processing a cookie request is that clients can't differentiate
2485b7ed089SYuchung Cheng 	 * between qlen overflow causing Fast Open to be disabled
2495b7ed089SYuchung Cheng 	 * temporarily vs a server not supporting Fast Open at all.
2505b7ed089SYuchung Cheng 	 */
2510536fcc0SEric Dumazet 	fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq;
2520536fcc0SEric Dumazet 	if (fastopenq->max_qlen == 0)
2535b7ed089SYuchung Cheng 		return false;
2545b7ed089SYuchung Cheng 
2555b7ed089SYuchung Cheng 	if (fastopenq->qlen >= fastopenq->max_qlen) {
2565b7ed089SYuchung Cheng 		struct request_sock *req1;
2575b7ed089SYuchung Cheng 		spin_lock(&fastopenq->lock);
2585b7ed089SYuchung Cheng 		req1 = fastopenq->rskq_rst_head;
259fa76ce73SEric Dumazet 		if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) {
26002a1d6e7SEric Dumazet 			__NET_INC_STATS(sock_net(sk),
2615b7ed089SYuchung Cheng 					LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
262c10d9310SEric Dumazet 			spin_unlock(&fastopenq->lock);
2635b7ed089SYuchung Cheng 			return false;
2645b7ed089SYuchung Cheng 		}
2655b7ed089SYuchung Cheng 		fastopenq->rskq_rst_head = req1->dl_next;
2665b7ed089SYuchung Cheng 		fastopenq->qlen--;
2675b7ed089SYuchung Cheng 		spin_unlock(&fastopenq->lock);
26813854e5aSEric Dumazet 		reqsk_put(req1);
2695b7ed089SYuchung Cheng 	}
2705b7ed089SYuchung Cheng 	return true;
2715b7ed089SYuchung Cheng }
2725b7ed089SYuchung Cheng 
27389278c9dSYuchung Cheng /* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
27489278c9dSYuchung Cheng  * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
27589278c9dSYuchung Cheng  * cookie request (foc->len == 0).
27689278c9dSYuchung Cheng  */
2777c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
2785b7ed089SYuchung Cheng 			      struct request_sock *req,
279843f4a55SYuchung Cheng 			      struct tcp_fastopen_cookie *foc,
280843f4a55SYuchung Cheng 			      struct dst_entry *dst)
2815b7ed089SYuchung Cheng {
28289278c9dSYuchung Cheng 	struct tcp_fastopen_cookie valid_foc = { .len = -1 };
28389278c9dSYuchung Cheng 	bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
2847c85af88SEric Dumazet 	struct sock *child;
2855b7ed089SYuchung Cheng 
286531c94a9SYuchung Cheng 	if (foc->len == 0) /* Client requests a cookie */
287c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
288531c94a9SYuchung Cheng 
28989278c9dSYuchung Cheng 	if (!((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) &&
29089278c9dSYuchung Cheng 	      (syn_data || foc->len >= 0) &&
29189278c9dSYuchung Cheng 	      tcp_fastopen_queue_check(sk))) {
29289278c9dSYuchung Cheng 		foc->len = -1;
2937c85af88SEric Dumazet 		return NULL;
2945b7ed089SYuchung Cheng 	}
29589278c9dSYuchung Cheng 
29689278c9dSYuchung Cheng 	if (syn_data && (sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD))
29789278c9dSYuchung Cheng 		goto fastopen;
29889278c9dSYuchung Cheng 
299531c94a9SYuchung Cheng 	if (foc->len >= 0 &&  /* Client presents or requests a cookie */
300531c94a9SYuchung Cheng 	    tcp_fastopen_cookie_gen(req, skb, &valid_foc) &&
3013a19ce0eSDaniel Lee 	    foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
30289278c9dSYuchung Cheng 	    foc->len == valid_foc.len &&
30389278c9dSYuchung Cheng 	    !memcmp(foc->val, valid_foc.val, foc->len)) {
304843f4a55SYuchung Cheng 		/* Cookie is valid. Create a (full) child socket to accept
305843f4a55SYuchung Cheng 		 * the data in SYN before returning a SYN-ACK to ack the
306843f4a55SYuchung Cheng 		 * data. If we fail to create the socket, fall back and
307843f4a55SYuchung Cheng 		 * ack the ISN only but includes the same cookie.
308843f4a55SYuchung Cheng 		 *
309843f4a55SYuchung Cheng 		 * Note: Data-less SYN with valid cookie is allowed to send
310843f4a55SYuchung Cheng 		 * data in SYN_RECV state.
311843f4a55SYuchung Cheng 		 */
31289278c9dSYuchung Cheng fastopen:
3137c85af88SEric Dumazet 		child = tcp_fastopen_create_child(sk, skb, dst, req);
3147c85af88SEric Dumazet 		if (child) {
31589278c9dSYuchung Cheng 			foc->len = -1;
316c10d9310SEric Dumazet 			NET_INC_STATS(sock_net(sk),
317843f4a55SYuchung Cheng 				      LINUX_MIB_TCPFASTOPENPASSIVE);
3187c85af88SEric Dumazet 			return child;
3195b7ed089SYuchung Cheng 		}
320c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
321531c94a9SYuchung Cheng 	} else if (foc->len > 0) /* Client presents an invalid cookie */
322c10d9310SEric Dumazet 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
3235b7ed089SYuchung Cheng 
3247f9b838bSDaniel Lee 	valid_foc.exp = foc->exp;
32589278c9dSYuchung Cheng 	*foc = valid_foc;
3267c85af88SEric Dumazet 	return NULL;
3275b7ed089SYuchung Cheng }
328