xref: /openbmc/linux/net/ipv4/tcp_fastopen.c (revision 9439ce00)
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 
81222e83d2SHannes Frederic Sowa 	tcp_fastopen_init_key_once(true);
82222e83d2SHannes Frederic Sowa 
8310467163SJerry Chu 	rcu_read_lock();
8410467163SJerry Chu 	ctx = rcu_dereference(tcp_fastopen_ctx);
8510467163SJerry Chu 	if (ctx) {
863a19ce0eSDaniel Lee 		crypto_cipher_encrypt_one(ctx->tfm, foc->val, path);
8710467163SJerry Chu 		foc->len = TCP_FASTOPEN_COOKIE_SIZE;
883a19ce0eSDaniel Lee 		ok = true;
8910467163SJerry Chu 	}
9010467163SJerry Chu 	rcu_read_unlock();
913a19ce0eSDaniel Lee 	return ok;
923a19ce0eSDaniel Lee }
933a19ce0eSDaniel Lee 
943a19ce0eSDaniel Lee /* Generate the fastopen cookie by doing aes128 encryption on both
953a19ce0eSDaniel Lee  * the source and destination addresses. Pad 0s for IPv4 or IPv4-mapped-IPv6
963a19ce0eSDaniel Lee  * addresses. For the longer IPv6 addresses use CBC-MAC.
973a19ce0eSDaniel Lee  *
983a19ce0eSDaniel Lee  * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE.
993a19ce0eSDaniel Lee  */
1003a19ce0eSDaniel Lee static bool tcp_fastopen_cookie_gen(struct request_sock *req,
1013a19ce0eSDaniel Lee 				    struct sk_buff *syn,
1023a19ce0eSDaniel Lee 				    struct tcp_fastopen_cookie *foc)
1033a19ce0eSDaniel Lee {
1043a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET) {
1053a19ce0eSDaniel Lee 		const struct iphdr *iph = ip_hdr(syn);
1063a19ce0eSDaniel Lee 
1073a19ce0eSDaniel Lee 		__be32 path[4] = { iph->saddr, iph->daddr, 0, 0 };
1083a19ce0eSDaniel Lee 		return __tcp_fastopen_cookie_gen(path, foc);
1093a19ce0eSDaniel Lee 	}
1103a19ce0eSDaniel Lee 
1113a19ce0eSDaniel Lee #if IS_ENABLED(CONFIG_IPV6)
1123a19ce0eSDaniel Lee 	if (req->rsk_ops->family == AF_INET6) {
1133a19ce0eSDaniel Lee 		const struct ipv6hdr *ip6h = ipv6_hdr(syn);
1143a19ce0eSDaniel Lee 		struct tcp_fastopen_cookie tmp;
1153a19ce0eSDaniel Lee 
1163a19ce0eSDaniel Lee 		if (__tcp_fastopen_cookie_gen(&ip6h->saddr, &tmp)) {
1173a19ce0eSDaniel Lee 			struct in6_addr *buf = (struct in6_addr *) tmp.val;
11841c91996SLi RongQing 			int i;
1193a19ce0eSDaniel Lee 
1203a19ce0eSDaniel Lee 			for (i = 0; i < 4; i++)
1213a19ce0eSDaniel Lee 				buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i];
1223a19ce0eSDaniel Lee 			return __tcp_fastopen_cookie_gen(buf, foc);
1233a19ce0eSDaniel Lee 		}
1243a19ce0eSDaniel Lee 	}
1253a19ce0eSDaniel Lee #endif
1263a19ce0eSDaniel Lee 	return false;
12710467163SJerry Chu }
1285b7ed089SYuchung Cheng 
129843f4a55SYuchung Cheng static bool tcp_fastopen_create_child(struct sock *sk,
1305b7ed089SYuchung Cheng 				      struct sk_buff *skb,
131843f4a55SYuchung Cheng 				      struct dst_entry *dst,
1325b7ed089SYuchung Cheng 				      struct request_sock *req)
1335b7ed089SYuchung Cheng {
13417846376SDave Jones 	struct tcp_sock *tp;
1355b7ed089SYuchung Cheng 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
1365b7ed089SYuchung Cheng 	struct sock *child;
137ba34e6d9SEric Dumazet 	u32 end_seq;
1385b7ed089SYuchung Cheng 
1395b7ed089SYuchung Cheng 	req->num_retrans = 0;
1405b7ed089SYuchung Cheng 	req->num_timeout = 0;
1415b7ed089SYuchung Cheng 	req->sk = NULL;
1425b7ed089SYuchung Cheng 
1435b7ed089SYuchung Cheng 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
144843f4a55SYuchung Cheng 	if (child == NULL)
145843f4a55SYuchung Cheng 		return false;
1465b7ed089SYuchung Cheng 
1475b7ed089SYuchung Cheng 	spin_lock(&queue->fastopenq->lock);
1485b7ed089SYuchung Cheng 	queue->fastopenq->qlen++;
1495b7ed089SYuchung Cheng 	spin_unlock(&queue->fastopenq->lock);
1505b7ed089SYuchung Cheng 
1515b7ed089SYuchung Cheng 	/* Initialize the child socket. Have to fix some values to take
1525b7ed089SYuchung Cheng 	 * into account the child is a Fast Open socket and is created
1535b7ed089SYuchung Cheng 	 * only out of the bits carried in the SYN packet.
1545b7ed089SYuchung Cheng 	 */
1555b7ed089SYuchung Cheng 	tp = tcp_sk(child);
1565b7ed089SYuchung Cheng 
1575b7ed089SYuchung Cheng 	tp->fastopen_rsk = req;
1589439ce00SEric Dumazet 	tcp_rsk(req)->tfo_listener = true;
1595b7ed089SYuchung Cheng 
1605b7ed089SYuchung Cheng 	/* RFC1323: The window in SYN & SYN/ACK segments is never
1615b7ed089SYuchung Cheng 	 * scaled. So correct it appropriately.
1625b7ed089SYuchung Cheng 	 */
1635b7ed089SYuchung Cheng 	tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
1645b7ed089SYuchung Cheng 
1655b7ed089SYuchung Cheng 	/* Activate the retrans timer so that SYNACK can be retransmitted.
1665b7ed089SYuchung Cheng 	 * The request socket is not added to the SYN table of the parent
1675b7ed089SYuchung Cheng 	 * because it's been added to the accept queue directly.
1685b7ed089SYuchung Cheng 	 */
1695b7ed089SYuchung Cheng 	inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
1705b7ed089SYuchung Cheng 				  TCP_TIMEOUT_INIT, TCP_RTO_MAX);
1715b7ed089SYuchung Cheng 
1725b7ed089SYuchung Cheng 	/* Add the child socket directly into the accept queue */
1735b7ed089SYuchung Cheng 	inet_csk_reqsk_queue_add(sk, req, child);
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 
1825b7ed089SYuchung Cheng 	/* Queue the data carried in the SYN packet. We need to first
1835b7ed089SYuchung Cheng 	 * bump skb's refcnt because the caller will attempt to free it.
184ba34e6d9SEric Dumazet 	 * Note that IPv6 might also have used skb_get() trick
185ba34e6d9SEric Dumazet 	 * in tcp_v6_conn_request() to keep this SYN around (treq->pktopts)
186ba34e6d9SEric Dumazet 	 * So we need to eventually get a clone of the packet,
187ba34e6d9SEric Dumazet 	 * before inserting it in sk_receive_queue.
1885b7ed089SYuchung Cheng 	 *
189843f4a55SYuchung Cheng 	 * XXX (TFO) - we honor a zero-payload TFO request for now,
190843f4a55SYuchung Cheng 	 * (any reason not to?) but no need to queue the skb since
191843f4a55SYuchung Cheng 	 * there is no data. How about SYN+FIN?
1925b7ed089SYuchung Cheng 	 */
193ba34e6d9SEric Dumazet 	end_seq = TCP_SKB_CB(skb)->end_seq;
194ba34e6d9SEric Dumazet 	if (end_seq != TCP_SKB_CB(skb)->seq + 1) {
195ba34e6d9SEric Dumazet 		struct sk_buff *skb2;
196ba34e6d9SEric Dumazet 
197ba34e6d9SEric Dumazet 		if (unlikely(skb_shared(skb)))
198ba34e6d9SEric Dumazet 			skb2 = skb_clone(skb, GFP_ATOMIC);
199ba34e6d9SEric Dumazet 		else
200ba34e6d9SEric Dumazet 			skb2 = skb_get(skb);
201ba34e6d9SEric Dumazet 
202ba34e6d9SEric Dumazet 		if (likely(skb2)) {
203ba34e6d9SEric Dumazet 			skb_dst_drop(skb2);
204ba34e6d9SEric Dumazet 			__skb_pull(skb2, tcp_hdrlen(skb));
205ba34e6d9SEric Dumazet 			skb_set_owner_r(skb2, child);
206ba34e6d9SEric Dumazet 			__skb_queue_tail(&child->sk_receive_queue, skb2);
2075b7ed089SYuchung Cheng 			tp->syn_data_acked = 1;
208ba34e6d9SEric Dumazet 		} else {
209ba34e6d9SEric Dumazet 			end_seq = TCP_SKB_CB(skb)->seq + 1;
2105b7ed089SYuchung Cheng 		}
211ba34e6d9SEric Dumazet 	}
212ba34e6d9SEric Dumazet 	tcp_rsk(req)->rcv_nxt = tp->rcv_nxt = end_seq;
2135b7ed089SYuchung Cheng 	sk->sk_data_ready(sk);
2145b7ed089SYuchung Cheng 	bh_unlock_sock(child);
2155b7ed089SYuchung Cheng 	sock_put(child);
2165b7ed089SYuchung Cheng 	WARN_ON(req->sk == NULL);
217843f4a55SYuchung Cheng 	return true;
2185b7ed089SYuchung Cheng }
2195b7ed089SYuchung Cheng 
2205b7ed089SYuchung Cheng static bool tcp_fastopen_queue_check(struct sock *sk)
2215b7ed089SYuchung Cheng {
2225b7ed089SYuchung Cheng 	struct fastopen_queue *fastopenq;
2235b7ed089SYuchung Cheng 
2245b7ed089SYuchung Cheng 	/* Make sure the listener has enabled fastopen, and we don't
2255b7ed089SYuchung Cheng 	 * exceed the max # of pending TFO requests allowed before trying
2265b7ed089SYuchung Cheng 	 * to validating the cookie in order to avoid burning CPU cycles
2275b7ed089SYuchung Cheng 	 * unnecessarily.
2285b7ed089SYuchung Cheng 	 *
2295b7ed089SYuchung Cheng 	 * XXX (TFO) - The implication of checking the max_qlen before
2305b7ed089SYuchung Cheng 	 * processing a cookie request is that clients can't differentiate
2315b7ed089SYuchung Cheng 	 * between qlen overflow causing Fast Open to be disabled
2325b7ed089SYuchung Cheng 	 * temporarily vs a server not supporting Fast Open at all.
2335b7ed089SYuchung Cheng 	 */
2345b7ed089SYuchung Cheng 	fastopenq = inet_csk(sk)->icsk_accept_queue.fastopenq;
2355b7ed089SYuchung Cheng 	if (fastopenq == NULL || fastopenq->max_qlen == 0)
2365b7ed089SYuchung Cheng 		return false;
2375b7ed089SYuchung Cheng 
2385b7ed089SYuchung Cheng 	if (fastopenq->qlen >= fastopenq->max_qlen) {
2395b7ed089SYuchung Cheng 		struct request_sock *req1;
2405b7ed089SYuchung Cheng 		spin_lock(&fastopenq->lock);
2415b7ed089SYuchung Cheng 		req1 = fastopenq->rskq_rst_head;
2425b7ed089SYuchung Cheng 		if ((req1 == NULL) || time_after(req1->expires, jiffies)) {
2435b7ed089SYuchung Cheng 			spin_unlock(&fastopenq->lock);
2445b7ed089SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
2455b7ed089SYuchung Cheng 					 LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
2465b7ed089SYuchung Cheng 			return false;
2475b7ed089SYuchung Cheng 		}
2485b7ed089SYuchung Cheng 		fastopenq->rskq_rst_head = req1->dl_next;
2495b7ed089SYuchung Cheng 		fastopenq->qlen--;
2505b7ed089SYuchung Cheng 		spin_unlock(&fastopenq->lock);
25113854e5aSEric Dumazet 		reqsk_put(req1);
2525b7ed089SYuchung Cheng 	}
2535b7ed089SYuchung Cheng 	return true;
2545b7ed089SYuchung Cheng }
2555b7ed089SYuchung Cheng 
25689278c9dSYuchung Cheng /* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
25789278c9dSYuchung Cheng  * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
25889278c9dSYuchung Cheng  * cookie request (foc->len == 0).
25989278c9dSYuchung Cheng  */
260843f4a55SYuchung Cheng bool tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
2615b7ed089SYuchung Cheng 		      struct request_sock *req,
262843f4a55SYuchung Cheng 		      struct tcp_fastopen_cookie *foc,
263843f4a55SYuchung Cheng 		      struct dst_entry *dst)
2645b7ed089SYuchung Cheng {
26589278c9dSYuchung Cheng 	struct tcp_fastopen_cookie valid_foc = { .len = -1 };
26689278c9dSYuchung Cheng 	bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
2675b7ed089SYuchung Cheng 
268531c94a9SYuchung Cheng 	if (foc->len == 0) /* Client requests a cookie */
269531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
270531c94a9SYuchung Cheng 
27189278c9dSYuchung Cheng 	if (!((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) &&
27289278c9dSYuchung Cheng 	      (syn_data || foc->len >= 0) &&
27389278c9dSYuchung Cheng 	      tcp_fastopen_queue_check(sk))) {
27489278c9dSYuchung Cheng 		foc->len = -1;
2755b7ed089SYuchung Cheng 		return false;
2765b7ed089SYuchung Cheng 	}
27789278c9dSYuchung Cheng 
27889278c9dSYuchung Cheng 	if (syn_data && (sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD))
27989278c9dSYuchung Cheng 		goto fastopen;
28089278c9dSYuchung Cheng 
281531c94a9SYuchung Cheng 	if (foc->len >= 0 &&  /* Client presents or requests a cookie */
282531c94a9SYuchung Cheng 	    tcp_fastopen_cookie_gen(req, skb, &valid_foc) &&
2833a19ce0eSDaniel Lee 	    foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
28489278c9dSYuchung Cheng 	    foc->len == valid_foc.len &&
28589278c9dSYuchung Cheng 	    !memcmp(foc->val, valid_foc.val, foc->len)) {
286843f4a55SYuchung Cheng 		/* Cookie is valid. Create a (full) child socket to accept
287843f4a55SYuchung Cheng 		 * the data in SYN before returning a SYN-ACK to ack the
288843f4a55SYuchung Cheng 		 * data. If we fail to create the socket, fall back and
289843f4a55SYuchung Cheng 		 * ack the ISN only but includes the same cookie.
290843f4a55SYuchung Cheng 		 *
291843f4a55SYuchung Cheng 		 * Note: Data-less SYN with valid cookie is allowed to send
292843f4a55SYuchung Cheng 		 * data in SYN_RECV state.
293843f4a55SYuchung Cheng 		 */
29489278c9dSYuchung Cheng fastopen:
295843f4a55SYuchung Cheng 		if (tcp_fastopen_create_child(sk, skb, dst, req)) {
29689278c9dSYuchung Cheng 			foc->len = -1;
297843f4a55SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
298843f4a55SYuchung Cheng 					 LINUX_MIB_TCPFASTOPENPASSIVE);
2995b7ed089SYuchung Cheng 			return true;
3005b7ed089SYuchung Cheng 		}
301531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
302531c94a9SYuchung Cheng 	} else if (foc->len > 0) /* Client presents an invalid cookie */
303531c94a9SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
3045b7ed089SYuchung Cheng 
30589278c9dSYuchung Cheng 	*foc = valid_foc;
3065b7ed089SYuchung Cheng 	return false;
3075b7ed089SYuchung Cheng }
308843f4a55SYuchung Cheng EXPORT_SYMBOL(tcp_try_fastopen);
309