xref: /openbmc/linux/net/ipv4/tcp_fastopen.c (revision cf80e0e4)
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)) {
1163a19ce0eSDaniel Lee 			struct in6_addr *buf = (struct in6_addr *) tmp.val;
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 
1287c85af88SEric Dumazet static struct sock *tcp_fastopen_create_child(struct sock *sk,
1295b7ed089SYuchung Cheng 					      struct sk_buff *skb,
130843f4a55SYuchung Cheng 					      struct dst_entry *dst,
1315b7ed089SYuchung Cheng 					      struct request_sock *req)
1325b7ed089SYuchung Cheng {
13317846376SDave Jones 	struct tcp_sock *tp;
1345b7ed089SYuchung Cheng 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
1355b7ed089SYuchung Cheng 	struct sock *child;
136ba34e6d9SEric Dumazet 	u32 end_seq;
1375e0724d0SEric Dumazet 	bool own_req;
1385b7ed089SYuchung Cheng 
1395b7ed089SYuchung Cheng 	req->num_retrans = 0;
1405b7ed089SYuchung Cheng 	req->num_timeout = 0;
1415b7ed089SYuchung Cheng 	req->sk = NULL;
1425b7ed089SYuchung Cheng 
1435e0724d0SEric Dumazet 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
1445e0724d0SEric Dumazet 							 NULL, &own_req);
14551456b29SIan Morris 	if (!child)
1467c85af88SEric Dumazet 		return NULL;
1475b7ed089SYuchung Cheng 
1480536fcc0SEric Dumazet 	spin_lock(&queue->fastopenq.lock);
1490536fcc0SEric Dumazet 	queue->fastopenq.qlen++;
1500536fcc0SEric Dumazet 	spin_unlock(&queue->fastopenq.lock);
1515b7ed089SYuchung Cheng 
1525b7ed089SYuchung Cheng 	/* Initialize the child socket. Have to fix some values to take
1535b7ed089SYuchung Cheng 	 * into account the child is a Fast Open socket and is created
1545b7ed089SYuchung Cheng 	 * only out of the bits carried in the SYN packet.
1555b7ed089SYuchung Cheng 	 */
1565b7ed089SYuchung Cheng 	tp = tcp_sk(child);
1575b7ed089SYuchung Cheng 
1585b7ed089SYuchung Cheng 	tp->fastopen_rsk = req;
1599439ce00SEric Dumazet 	tcp_rsk(req)->tfo_listener = true;
1605b7ed089SYuchung Cheng 
1615b7ed089SYuchung Cheng 	/* RFC1323: The window in SYN & SYN/ACK segments is never
1625b7ed089SYuchung Cheng 	 * scaled. So correct it appropriately.
1635b7ed089SYuchung Cheng 	 */
1645b7ed089SYuchung Cheng 	tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
1655b7ed089SYuchung Cheng 
1665b7ed089SYuchung Cheng 	/* Activate the retrans timer so that SYNACK can be retransmitted.
167ca6fb065SEric Dumazet 	 * The request socket is not added to the ehash
1685b7ed089SYuchung Cheng 	 * because it's been added to the accept queue directly.
1695b7ed089SYuchung Cheng 	 */
1705b7ed089SYuchung Cheng 	inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
1715b7ed089SYuchung Cheng 				  TCP_TIMEOUT_INIT, TCP_RTO_MAX);
1725b7ed089SYuchung Cheng 
173ca6fb065SEric Dumazet 	atomic_set(&req->rsk_refcnt, 2);
1745b7ed089SYuchung Cheng 
1755b7ed089SYuchung Cheng 	/* Now finish processing the fastopen child socket. */
1765b7ed089SYuchung Cheng 	inet_csk(child)->icsk_af_ops->rebuild_header(child);
1775b7ed089SYuchung Cheng 	tcp_init_congestion_control(child);
1785b7ed089SYuchung Cheng 	tcp_mtup_init(child);
1795b7ed089SYuchung Cheng 	tcp_init_metrics(child);
1805b7ed089SYuchung Cheng 	tcp_init_buffer_space(child);
1815b7ed089SYuchung Cheng 
1827656d842SEric Dumazet 	/* Queue the data carried in the SYN packet.
1837656d842SEric Dumazet 	 * We used to play tricky games with skb_get().
1847656d842SEric Dumazet 	 * With lockless listener, it is a dead end.
1857656d842SEric Dumazet 	 * Do not think about it.
1865b7ed089SYuchung Cheng 	 *
187843f4a55SYuchung Cheng 	 * XXX (TFO) - we honor a zero-payload TFO request for now,
188843f4a55SYuchung Cheng 	 * (any reason not to?) but no need to queue the skb since
189843f4a55SYuchung Cheng 	 * there is no data. How about SYN+FIN?
1905b7ed089SYuchung Cheng 	 */
191ba34e6d9SEric Dumazet 	end_seq = TCP_SKB_CB(skb)->end_seq;
192ba34e6d9SEric Dumazet 	if (end_seq != TCP_SKB_CB(skb)->seq + 1) {
1937656d842SEric Dumazet 		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
194ba34e6d9SEric Dumazet 
195ba34e6d9SEric Dumazet 		if (likely(skb2)) {
196ba34e6d9SEric Dumazet 			skb_dst_drop(skb2);
197ba34e6d9SEric Dumazet 			__skb_pull(skb2, tcp_hdrlen(skb));
198ba34e6d9SEric Dumazet 			skb_set_owner_r(skb2, child);
199ba34e6d9SEric Dumazet 			__skb_queue_tail(&child->sk_receive_queue, skb2);
2005b7ed089SYuchung Cheng 			tp->syn_data_acked = 1;
201d654976cSEric Dumazet 
202d654976cSEric Dumazet 			/* u64_stats_update_begin(&tp->syncp) not needed here,
203d654976cSEric Dumazet 			 * as we certainly are not changing upper 32bit value (0)
204d654976cSEric Dumazet 			 */
205bdd1f9edSEric Dumazet 			tp->bytes_received = end_seq - TCP_SKB_CB(skb)->seq - 1;
206ba34e6d9SEric Dumazet 		} else {
207ba34e6d9SEric Dumazet 			end_seq = TCP_SKB_CB(skb)->seq + 1;
2085b7ed089SYuchung Cheng 		}
209ba34e6d9SEric Dumazet 	}
210ba34e6d9SEric Dumazet 	tcp_rsk(req)->rcv_nxt = tp->rcv_nxt = end_seq;
2117656d842SEric Dumazet 	/* tcp_conn_request() is sending the SYNACK,
2127656d842SEric Dumazet 	 * and queues the child into listener accept queue.
2137c85af88SEric Dumazet 	 */
2147c85af88SEric Dumazet 	return child;
2155b7ed089SYuchung Cheng }
2165b7ed089SYuchung Cheng 
2175b7ed089SYuchung Cheng static bool tcp_fastopen_queue_check(struct sock *sk)
2185b7ed089SYuchung Cheng {
2195b7ed089SYuchung Cheng 	struct fastopen_queue *fastopenq;
2205b7ed089SYuchung Cheng 
2215b7ed089SYuchung Cheng 	/* Make sure the listener has enabled fastopen, and we don't
2225b7ed089SYuchung Cheng 	 * exceed the max # of pending TFO requests allowed before trying
2235b7ed089SYuchung Cheng 	 * to validating the cookie in order to avoid burning CPU cycles
2245b7ed089SYuchung Cheng 	 * unnecessarily.
2255b7ed089SYuchung Cheng 	 *
2265b7ed089SYuchung Cheng 	 * XXX (TFO) - The implication of checking the max_qlen before
2275b7ed089SYuchung Cheng 	 * processing a cookie request is that clients can't differentiate
2285b7ed089SYuchung Cheng 	 * between qlen overflow causing Fast Open to be disabled
2295b7ed089SYuchung Cheng 	 * temporarily vs a server not supporting Fast Open at all.
2305b7ed089SYuchung Cheng 	 */
2310536fcc0SEric Dumazet 	fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq;
2320536fcc0SEric Dumazet 	if (fastopenq->max_qlen == 0)
2335b7ed089SYuchung Cheng 		return false;
2345b7ed089SYuchung Cheng 
2355b7ed089SYuchung Cheng 	if (fastopenq->qlen >= fastopenq->max_qlen) {
2365b7ed089SYuchung Cheng 		struct request_sock *req1;
2375b7ed089SYuchung Cheng 		spin_lock(&fastopenq->lock);
2385b7ed089SYuchung Cheng 		req1 = fastopenq->rskq_rst_head;
239fa76ce73SEric Dumazet 		if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) {
2405b7ed089SYuchung Cheng 			spin_unlock(&fastopenq->lock);
2415b7ed089SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
2425b7ed089SYuchung Cheng 					 LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
2435b7ed089SYuchung Cheng 			return false;
2445b7ed089SYuchung Cheng 		}
2455b7ed089SYuchung Cheng 		fastopenq->rskq_rst_head = req1->dl_next;
2465b7ed089SYuchung Cheng 		fastopenq->qlen--;
2475b7ed089SYuchung Cheng 		spin_unlock(&fastopenq->lock);
24813854e5aSEric Dumazet 		reqsk_put(req1);
2495b7ed089SYuchung Cheng 	}
2505b7ed089SYuchung Cheng 	return true;
2515b7ed089SYuchung Cheng }
2525b7ed089SYuchung Cheng 
25389278c9dSYuchung Cheng /* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
25489278c9dSYuchung Cheng  * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
25589278c9dSYuchung Cheng  * cookie request (foc->len == 0).
25689278c9dSYuchung Cheng  */
2577c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
2585b7ed089SYuchung Cheng 			      struct request_sock *req,
259843f4a55SYuchung Cheng 			      struct tcp_fastopen_cookie *foc,
260843f4a55SYuchung Cheng 			      struct dst_entry *dst)
2615b7ed089SYuchung Cheng {
26289278c9dSYuchung Cheng 	struct tcp_fastopen_cookie valid_foc = { .len = -1 };
26389278c9dSYuchung Cheng 	bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
2647c85af88SEric Dumazet 	struct sock *child;
2655b7ed089SYuchung Cheng 
266531c94a9SYuchung Cheng 	if (foc->len == 0) /* Client requests a cookie */
267531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
268531c94a9SYuchung Cheng 
26989278c9dSYuchung Cheng 	if (!((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) &&
27089278c9dSYuchung Cheng 	      (syn_data || foc->len >= 0) &&
27189278c9dSYuchung Cheng 	      tcp_fastopen_queue_check(sk))) {
27289278c9dSYuchung Cheng 		foc->len = -1;
2737c85af88SEric Dumazet 		return NULL;
2745b7ed089SYuchung Cheng 	}
27589278c9dSYuchung Cheng 
27689278c9dSYuchung Cheng 	if (syn_data && (sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD))
27789278c9dSYuchung Cheng 		goto fastopen;
27889278c9dSYuchung Cheng 
279531c94a9SYuchung Cheng 	if (foc->len >= 0 &&  /* Client presents or requests a cookie */
280531c94a9SYuchung Cheng 	    tcp_fastopen_cookie_gen(req, skb, &valid_foc) &&
2813a19ce0eSDaniel Lee 	    foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
28289278c9dSYuchung Cheng 	    foc->len == valid_foc.len &&
28389278c9dSYuchung Cheng 	    !memcmp(foc->val, valid_foc.val, foc->len)) {
284843f4a55SYuchung Cheng 		/* Cookie is valid. Create a (full) child socket to accept
285843f4a55SYuchung Cheng 		 * the data in SYN before returning a SYN-ACK to ack the
286843f4a55SYuchung Cheng 		 * data. If we fail to create the socket, fall back and
287843f4a55SYuchung Cheng 		 * ack the ISN only but includes the same cookie.
288843f4a55SYuchung Cheng 		 *
289843f4a55SYuchung Cheng 		 * Note: Data-less SYN with valid cookie is allowed to send
290843f4a55SYuchung Cheng 		 * data in SYN_RECV state.
291843f4a55SYuchung Cheng 		 */
29289278c9dSYuchung Cheng fastopen:
2937c85af88SEric Dumazet 		child = tcp_fastopen_create_child(sk, skb, dst, req);
2947c85af88SEric Dumazet 		if (child) {
29589278c9dSYuchung Cheng 			foc->len = -1;
296843f4a55SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
297843f4a55SYuchung Cheng 					 LINUX_MIB_TCPFASTOPENPASSIVE);
2987c85af88SEric Dumazet 			return child;
2995b7ed089SYuchung Cheng 		}
300531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
301531c94a9SYuchung Cheng 	} else if (foc->len > 0) /* Client presents an invalid cookie */
302531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
3035b7ed089SYuchung Cheng 
3047f9b838bSDaniel Lee 	valid_foc.exp = foc->exp;
30589278c9dSYuchung Cheng 	*foc = valid_foc;
3067c85af88SEric Dumazet 	return NULL;
3075b7ed089SYuchung Cheng }
308