xref: /openbmc/linux/net/ipv4/tcp_fastopen.c (revision ca6fb065)
110467163SJerry Chu #include <linux/err.h>
22100c8d2SYuchung Cheng #include <linux/init.h>
32100c8d2SYuchung Cheng #include <linux/kernel.h>
410467163SJerry Chu #include <linux/list.h>
510467163SJerry Chu #include <linux/tcp.h>
610467163SJerry Chu #include <linux/rcupdate.h>
710467163SJerry Chu #include <linux/rculist.h>
810467163SJerry Chu #include <net/inetpeer.h>
910467163SJerry Chu #include <net/tcp.h>
102100c8d2SYuchung Cheng 
110d41cca4SYuchung Cheng int sysctl_tcp_fastopen __read_mostly = TFO_CLIENT_ENABLE;
1210467163SJerry Chu 
1310467163SJerry Chu struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
1410467163SJerry Chu 
1510467163SJerry Chu static DEFINE_SPINLOCK(tcp_fastopen_ctx_lock);
1610467163SJerry Chu 
17222e83d2SHannes Frederic Sowa void tcp_fastopen_init_key_once(bool publish)
18222e83d2SHannes Frederic Sowa {
19222e83d2SHannes Frederic Sowa 	static u8 key[TCP_FASTOPEN_KEY_LENGTH];
20222e83d2SHannes Frederic Sowa 
21222e83d2SHannes Frederic Sowa 	/* tcp_fastopen_reset_cipher publishes the new context
22222e83d2SHannes Frederic Sowa 	 * atomically, so we allow this race happening here.
23222e83d2SHannes Frederic Sowa 	 *
24222e83d2SHannes Frederic Sowa 	 * All call sites of tcp_fastopen_cookie_gen also check
25222e83d2SHannes Frederic Sowa 	 * for a valid cookie, so this is an acceptable risk.
26222e83d2SHannes Frederic Sowa 	 */
27222e83d2SHannes Frederic Sowa 	if (net_get_random_once(key, sizeof(key)) && publish)
28222e83d2SHannes Frederic Sowa 		tcp_fastopen_reset_cipher(key, sizeof(key));
29222e83d2SHannes Frederic Sowa }
30222e83d2SHannes Frederic Sowa 
3110467163SJerry Chu static void tcp_fastopen_ctx_free(struct rcu_head *head)
3210467163SJerry Chu {
3310467163SJerry Chu 	struct tcp_fastopen_context *ctx =
3410467163SJerry Chu 	    container_of(head, struct tcp_fastopen_context, rcu);
3510467163SJerry Chu 	crypto_free_cipher(ctx->tfm);
3610467163SJerry Chu 	kfree(ctx);
3710467163SJerry Chu }
3810467163SJerry Chu 
3910467163SJerry Chu int tcp_fastopen_reset_cipher(void *key, unsigned int len)
4010467163SJerry Chu {
4110467163SJerry Chu 	int err;
4210467163SJerry Chu 	struct tcp_fastopen_context *ctx, *octx;
4310467163SJerry Chu 
4410467163SJerry Chu 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
4510467163SJerry Chu 	if (!ctx)
4610467163SJerry Chu 		return -ENOMEM;
4710467163SJerry Chu 	ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
4810467163SJerry Chu 
4910467163SJerry Chu 	if (IS_ERR(ctx->tfm)) {
5010467163SJerry Chu 		err = PTR_ERR(ctx->tfm);
5110467163SJerry Chu error:		kfree(ctx);
5210467163SJerry Chu 		pr_err("TCP: TFO aes cipher alloc error: %d\n", err);
5310467163SJerry Chu 		return err;
5410467163SJerry Chu 	}
5510467163SJerry Chu 	err = crypto_cipher_setkey(ctx->tfm, key, len);
5610467163SJerry Chu 	if (err) {
5710467163SJerry Chu 		pr_err("TCP: TFO cipher key error: %d\n", err);
5810467163SJerry Chu 		crypto_free_cipher(ctx->tfm);
5910467163SJerry Chu 		goto error;
6010467163SJerry Chu 	}
6110467163SJerry Chu 	memcpy(ctx->key, key, len);
6210467163SJerry Chu 
6310467163SJerry Chu 	spin_lock(&tcp_fastopen_ctx_lock);
6410467163SJerry Chu 
6510467163SJerry Chu 	octx = rcu_dereference_protected(tcp_fastopen_ctx,
6610467163SJerry Chu 				lockdep_is_held(&tcp_fastopen_ctx_lock));
6710467163SJerry Chu 	rcu_assign_pointer(tcp_fastopen_ctx, ctx);
6810467163SJerry Chu 	spin_unlock(&tcp_fastopen_ctx_lock);
6910467163SJerry Chu 
7010467163SJerry Chu 	if (octx)
7110467163SJerry Chu 		call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
7210467163SJerry Chu 	return err;
7310467163SJerry Chu }
7410467163SJerry Chu 
753a19ce0eSDaniel Lee static bool __tcp_fastopen_cookie_gen(const void *path,
76149479d0SYuchung Cheng 				      struct tcp_fastopen_cookie *foc)
7710467163SJerry Chu {
7810467163SJerry Chu 	struct tcp_fastopen_context *ctx;
793a19ce0eSDaniel Lee 	bool ok = false;
8010467163SJerry Chu 
8110467163SJerry Chu 	rcu_read_lock();
8210467163SJerry Chu 	ctx = rcu_dereference(tcp_fastopen_ctx);
8310467163SJerry Chu 	if (ctx) {
843a19ce0eSDaniel Lee 		crypto_cipher_encrypt_one(ctx->tfm, foc->val, path);
8510467163SJerry Chu 		foc->len = TCP_FASTOPEN_COOKIE_SIZE;
863a19ce0eSDaniel Lee 		ok = true;
8710467163SJerry Chu 	}
8810467163SJerry Chu 	rcu_read_unlock();
893a19ce0eSDaniel Lee 	return ok;
903a19ce0eSDaniel Lee }
913a19ce0eSDaniel Lee 
923a19ce0eSDaniel Lee /* Generate the fastopen cookie by doing aes128 encryption on both
933a19ce0eSDaniel Lee  * the source and destination addresses. Pad 0s for IPv4 or IPv4-mapped-IPv6
943a19ce0eSDaniel Lee  * addresses. For the longer IPv6 addresses use CBC-MAC.
953a19ce0eSDaniel Lee  *
963a19ce0eSDaniel Lee  * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE.
973a19ce0eSDaniel Lee  */
983a19ce0eSDaniel Lee static bool tcp_fastopen_cookie_gen(struct request_sock *req,
993a19ce0eSDaniel Lee 				    struct sk_buff *syn,
1003a19ce0eSDaniel Lee 				    struct tcp_fastopen_cookie *foc)
1013a19ce0eSDaniel Lee {
1023a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET) {
1033a19ce0eSDaniel Lee 		const struct iphdr *iph = ip_hdr(syn);
1043a19ce0eSDaniel Lee 
1053a19ce0eSDaniel Lee 		__be32 path[4] = { iph->saddr, iph->daddr, 0, 0 };
1063a19ce0eSDaniel Lee 		return __tcp_fastopen_cookie_gen(path, foc);
1073a19ce0eSDaniel Lee 	}
1083a19ce0eSDaniel Lee 
1093a19ce0eSDaniel Lee #if IS_ENABLED(CONFIG_IPV6)
1103a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET6) {
1113a19ce0eSDaniel Lee 		const struct ipv6hdr *ip6h = ipv6_hdr(syn);
1123a19ce0eSDaniel Lee 		struct tcp_fastopen_cookie tmp;
1133a19ce0eSDaniel Lee 
1143a19ce0eSDaniel Lee 		if (__tcp_fastopen_cookie_gen(&ip6h->saddr, &tmp)) {
1153a19ce0eSDaniel Lee 			struct in6_addr *buf = (struct in6_addr *) tmp.val;
11641c91996SLi RongQing 			int i;
1173a19ce0eSDaniel Lee 
1183a19ce0eSDaniel Lee 			for (i = 0; i < 4; i++)
1193a19ce0eSDaniel Lee 				buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i];
1203a19ce0eSDaniel Lee 			return __tcp_fastopen_cookie_gen(buf, foc);
1213a19ce0eSDaniel Lee 		}
1223a19ce0eSDaniel Lee 	}
1233a19ce0eSDaniel Lee #endif
1243a19ce0eSDaniel Lee 	return false;
12510467163SJerry Chu }
1265b7ed089SYuchung Cheng 
1277c85af88SEric Dumazet static struct sock *tcp_fastopen_create_child(struct sock *sk,
1285b7ed089SYuchung Cheng 					      struct sk_buff *skb,
129843f4a55SYuchung Cheng 					      struct dst_entry *dst,
1305b7ed089SYuchung Cheng 					      struct request_sock *req)
1315b7ed089SYuchung Cheng {
13217846376SDave Jones 	struct tcp_sock *tp;
1335b7ed089SYuchung Cheng 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
1345b7ed089SYuchung Cheng 	struct sock *child;
135ba34e6d9SEric Dumazet 	u32 end_seq;
1365b7ed089SYuchung Cheng 
1375b7ed089SYuchung Cheng 	req->num_retrans = 0;
1385b7ed089SYuchung Cheng 	req->num_timeout = 0;
1395b7ed089SYuchung Cheng 	req->sk = NULL;
1405b7ed089SYuchung Cheng 
1415b7ed089SYuchung Cheng 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
14251456b29SIan Morris 	if (!child)
1437c85af88SEric Dumazet 		return NULL;
1445b7ed089SYuchung Cheng 
1450536fcc0SEric Dumazet 	spin_lock(&queue->fastopenq.lock);
1460536fcc0SEric Dumazet 	queue->fastopenq.qlen++;
1470536fcc0SEric Dumazet 	spin_unlock(&queue->fastopenq.lock);
1485b7ed089SYuchung Cheng 
1495b7ed089SYuchung Cheng 	/* Initialize the child socket. Have to fix some values to take
1505b7ed089SYuchung Cheng 	 * into account the child is a Fast Open socket and is created
1515b7ed089SYuchung Cheng 	 * only out of the bits carried in the SYN packet.
1525b7ed089SYuchung Cheng 	 */
1535b7ed089SYuchung Cheng 	tp = tcp_sk(child);
1545b7ed089SYuchung Cheng 
1555b7ed089SYuchung Cheng 	tp->fastopen_rsk = req;
1569439ce00SEric Dumazet 	tcp_rsk(req)->tfo_listener = true;
1575b7ed089SYuchung Cheng 
1585b7ed089SYuchung Cheng 	/* RFC1323: The window in SYN & SYN/ACK segments is never
1595b7ed089SYuchung Cheng 	 * scaled. So correct it appropriately.
1605b7ed089SYuchung Cheng 	 */
1615b7ed089SYuchung Cheng 	tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
1625b7ed089SYuchung Cheng 
1635b7ed089SYuchung Cheng 	/* Activate the retrans timer so that SYNACK can be retransmitted.
164ca6fb065SEric Dumazet 	 * The request socket is not added to the ehash
1655b7ed089SYuchung Cheng 	 * because it's been added to the accept queue directly.
1665b7ed089SYuchung Cheng 	 */
1675b7ed089SYuchung Cheng 	inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
1685b7ed089SYuchung Cheng 				  TCP_TIMEOUT_INIT, TCP_RTO_MAX);
1695b7ed089SYuchung Cheng 
170ca6fb065SEric Dumazet 	atomic_set(&req->rsk_refcnt, 2);
1715b7ed089SYuchung Cheng 	/* Add the child socket directly into the accept queue */
1725b7ed089SYuchung Cheng 	inet_csk_reqsk_queue_add(sk, req, child);
1735b7ed089SYuchung Cheng 
1745b7ed089SYuchung Cheng 	/* Now finish processing the fastopen child socket. */
1755b7ed089SYuchung Cheng 	inet_csk(child)->icsk_af_ops->rebuild_header(child);
1765b7ed089SYuchung Cheng 	tcp_init_congestion_control(child);
1775b7ed089SYuchung Cheng 	tcp_mtup_init(child);
1785b7ed089SYuchung Cheng 	tcp_init_metrics(child);
1795b7ed089SYuchung Cheng 	tcp_init_buffer_space(child);
1805b7ed089SYuchung Cheng 
1815b7ed089SYuchung Cheng 	/* Queue the data carried in the SYN packet. We need to first
1825b7ed089SYuchung Cheng 	 * bump skb's refcnt because the caller will attempt to free it.
183ba34e6d9SEric Dumazet 	 * Note that IPv6 might also have used skb_get() trick
184ba34e6d9SEric Dumazet 	 * in tcp_v6_conn_request() to keep this SYN around (treq->pktopts)
185ba34e6d9SEric Dumazet 	 * So we need to eventually get a clone of the packet,
186ba34e6d9SEric Dumazet 	 * before inserting it in sk_receive_queue.
1875b7ed089SYuchung Cheng 	 *
188843f4a55SYuchung Cheng 	 * XXX (TFO) - we honor a zero-payload TFO request for now,
189843f4a55SYuchung Cheng 	 * (any reason not to?) but no need to queue the skb since
190843f4a55SYuchung Cheng 	 * there is no data. How about SYN+FIN?
1915b7ed089SYuchung Cheng 	 */
192ba34e6d9SEric Dumazet 	end_seq = TCP_SKB_CB(skb)->end_seq;
193ba34e6d9SEric Dumazet 	if (end_seq != TCP_SKB_CB(skb)->seq + 1) {
194ba34e6d9SEric Dumazet 		struct sk_buff *skb2;
195ba34e6d9SEric Dumazet 
196ba34e6d9SEric Dumazet 		if (unlikely(skb_shared(skb)))
197ba34e6d9SEric Dumazet 			skb2 = skb_clone(skb, GFP_ATOMIC);
198ba34e6d9SEric Dumazet 		else
199ba34e6d9SEric Dumazet 			skb2 = skb_get(skb);
200ba34e6d9SEric Dumazet 
201ba34e6d9SEric Dumazet 		if (likely(skb2)) {
202ba34e6d9SEric Dumazet 			skb_dst_drop(skb2);
203ba34e6d9SEric Dumazet 			__skb_pull(skb2, tcp_hdrlen(skb));
204ba34e6d9SEric Dumazet 			skb_set_owner_r(skb2, child);
205ba34e6d9SEric Dumazet 			__skb_queue_tail(&child->sk_receive_queue, skb2);
2065b7ed089SYuchung Cheng 			tp->syn_data_acked = 1;
207d654976cSEric Dumazet 
208d654976cSEric Dumazet 			/* u64_stats_update_begin(&tp->syncp) not needed here,
209d654976cSEric Dumazet 			 * as we certainly are not changing upper 32bit value (0)
210d654976cSEric Dumazet 			 */
211bdd1f9edSEric Dumazet 			tp->bytes_received = end_seq - TCP_SKB_CB(skb)->seq - 1;
212ba34e6d9SEric Dumazet 		} else {
213ba34e6d9SEric Dumazet 			end_seq = TCP_SKB_CB(skb)->seq + 1;
2145b7ed089SYuchung Cheng 		}
215ba34e6d9SEric Dumazet 	}
216ba34e6d9SEric Dumazet 	tcp_rsk(req)->rcv_nxt = tp->rcv_nxt = end_seq;
2175b7ed089SYuchung Cheng 	sk->sk_data_ready(sk);
2185b7ed089SYuchung Cheng 	bh_unlock_sock(child);
2197c85af88SEric Dumazet 	/* Note: sock_put(child) will be done by tcp_conn_request()
2207c85af88SEric Dumazet 	 * after SYNACK packet is sent.
2217c85af88SEric Dumazet 	 */
22251456b29SIan Morris 	WARN_ON(!req->sk);
2237c85af88SEric Dumazet 	return child;
2245b7ed089SYuchung Cheng }
2255b7ed089SYuchung Cheng 
2265b7ed089SYuchung Cheng static bool tcp_fastopen_queue_check(struct sock *sk)
2275b7ed089SYuchung Cheng {
2285b7ed089SYuchung Cheng 	struct fastopen_queue *fastopenq;
2295b7ed089SYuchung Cheng 
2305b7ed089SYuchung Cheng 	/* Make sure the listener has enabled fastopen, and we don't
2315b7ed089SYuchung Cheng 	 * exceed the max # of pending TFO requests allowed before trying
2325b7ed089SYuchung Cheng 	 * to validating the cookie in order to avoid burning CPU cycles
2335b7ed089SYuchung Cheng 	 * unnecessarily.
2345b7ed089SYuchung Cheng 	 *
2355b7ed089SYuchung Cheng 	 * XXX (TFO) - The implication of checking the max_qlen before
2365b7ed089SYuchung Cheng 	 * processing a cookie request is that clients can't differentiate
2375b7ed089SYuchung Cheng 	 * between qlen overflow causing Fast Open to be disabled
2385b7ed089SYuchung Cheng 	 * temporarily vs a server not supporting Fast Open at all.
2395b7ed089SYuchung Cheng 	 */
2400536fcc0SEric Dumazet 	fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq;
2410536fcc0SEric Dumazet 	if (fastopenq->max_qlen == 0)
2425b7ed089SYuchung Cheng 		return false;
2435b7ed089SYuchung Cheng 
2445b7ed089SYuchung Cheng 	if (fastopenq->qlen >= fastopenq->max_qlen) {
2455b7ed089SYuchung Cheng 		struct request_sock *req1;
2465b7ed089SYuchung Cheng 		spin_lock(&fastopenq->lock);
2475b7ed089SYuchung Cheng 		req1 = fastopenq->rskq_rst_head;
248fa76ce73SEric Dumazet 		if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) {
2495b7ed089SYuchung Cheng 			spin_unlock(&fastopenq->lock);
2505b7ed089SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
2515b7ed089SYuchung Cheng 					 LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
2525b7ed089SYuchung Cheng 			return false;
2535b7ed089SYuchung Cheng 		}
2545b7ed089SYuchung Cheng 		fastopenq->rskq_rst_head = req1->dl_next;
2555b7ed089SYuchung Cheng 		fastopenq->qlen--;
2565b7ed089SYuchung Cheng 		spin_unlock(&fastopenq->lock);
25713854e5aSEric Dumazet 		reqsk_put(req1);
2585b7ed089SYuchung Cheng 	}
2595b7ed089SYuchung Cheng 	return true;
2605b7ed089SYuchung Cheng }
2615b7ed089SYuchung Cheng 
26289278c9dSYuchung Cheng /* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
26389278c9dSYuchung Cheng  * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
26489278c9dSYuchung Cheng  * cookie request (foc->len == 0).
26589278c9dSYuchung Cheng  */
2667c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
2675b7ed089SYuchung Cheng 			      struct request_sock *req,
268843f4a55SYuchung Cheng 			      struct tcp_fastopen_cookie *foc,
269843f4a55SYuchung Cheng 			      struct dst_entry *dst)
2705b7ed089SYuchung Cheng {
27189278c9dSYuchung Cheng 	struct tcp_fastopen_cookie valid_foc = { .len = -1 };
27289278c9dSYuchung Cheng 	bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
2737c85af88SEric Dumazet 	struct sock *child;
2745b7ed089SYuchung Cheng 
275531c94a9SYuchung Cheng 	if (foc->len == 0) /* Client requests a cookie */
276531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
277531c94a9SYuchung Cheng 
27889278c9dSYuchung Cheng 	if (!((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) &&
27989278c9dSYuchung Cheng 	      (syn_data || foc->len >= 0) &&
28089278c9dSYuchung Cheng 	      tcp_fastopen_queue_check(sk))) {
28189278c9dSYuchung Cheng 		foc->len = -1;
2827c85af88SEric Dumazet 		return NULL;
2835b7ed089SYuchung Cheng 	}
28489278c9dSYuchung Cheng 
28589278c9dSYuchung Cheng 	if (syn_data && (sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD))
28689278c9dSYuchung Cheng 		goto fastopen;
28789278c9dSYuchung Cheng 
288531c94a9SYuchung Cheng 	if (foc->len >= 0 &&  /* Client presents or requests a cookie */
289531c94a9SYuchung Cheng 	    tcp_fastopen_cookie_gen(req, skb, &valid_foc) &&
2903a19ce0eSDaniel Lee 	    foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
29189278c9dSYuchung Cheng 	    foc->len == valid_foc.len &&
29289278c9dSYuchung Cheng 	    !memcmp(foc->val, valid_foc.val, foc->len)) {
293843f4a55SYuchung Cheng 		/* Cookie is valid. Create a (full) child socket to accept
294843f4a55SYuchung Cheng 		 * the data in SYN before returning a SYN-ACK to ack the
295843f4a55SYuchung Cheng 		 * data. If we fail to create the socket, fall back and
296843f4a55SYuchung Cheng 		 * ack the ISN only but includes the same cookie.
297843f4a55SYuchung Cheng 		 *
298843f4a55SYuchung Cheng 		 * Note: Data-less SYN with valid cookie is allowed to send
299843f4a55SYuchung Cheng 		 * data in SYN_RECV state.
300843f4a55SYuchung Cheng 		 */
30189278c9dSYuchung Cheng fastopen:
3027c85af88SEric Dumazet 		child = tcp_fastopen_create_child(sk, skb, dst, req);
3037c85af88SEric Dumazet 		if (child) {
30489278c9dSYuchung Cheng 			foc->len = -1;
305843f4a55SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
306843f4a55SYuchung Cheng 					 LINUX_MIB_TCPFASTOPENPASSIVE);
3077c85af88SEric Dumazet 			return child;
3085b7ed089SYuchung Cheng 		}
309531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
310531c94a9SYuchung Cheng 	} else if (foc->len > 0) /* Client presents an invalid cookie */
311531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
3125b7ed089SYuchung Cheng 
3137f9b838bSDaniel Lee 	valid_foc.exp = foc->exp;
31489278c9dSYuchung Cheng 	*foc = valid_foc;
3157c85af88SEric Dumazet 	return NULL;
3165b7ed089SYuchung Cheng }
317