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 12761d2bcaeSEric Dumazet 12861d2bcaeSEric Dumazet /* If an incoming SYN or SYNACK frame contains a payload and/or FIN, 12961d2bcaeSEric Dumazet * queue this additional data / FIN. 13061d2bcaeSEric Dumazet */ 13161d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb) 13261d2bcaeSEric Dumazet { 13361d2bcaeSEric Dumazet struct tcp_sock *tp = tcp_sk(sk); 13461d2bcaeSEric Dumazet 13561d2bcaeSEric Dumazet if (TCP_SKB_CB(skb)->end_seq == tp->rcv_nxt) 13661d2bcaeSEric Dumazet return; 13761d2bcaeSEric Dumazet 13861d2bcaeSEric Dumazet skb = skb_clone(skb, GFP_ATOMIC); 13961d2bcaeSEric Dumazet if (!skb) 14061d2bcaeSEric Dumazet return; 14161d2bcaeSEric Dumazet 14261d2bcaeSEric Dumazet skb_dst_drop(skb); 143a44d6eacSMartin KaFai Lau /* segs_in has been initialized to 1 in tcp_create_openreq_child(). 144a44d6eacSMartin KaFai Lau * Hence, reset segs_in to 0 before calling tcp_segs_in() 145a44d6eacSMartin KaFai Lau * to avoid double counting. Also, tcp_segs_in() expects 146a44d6eacSMartin KaFai Lau * skb->len to include the tcp_hdrlen. Hence, it should 147a44d6eacSMartin KaFai Lau * be called before __skb_pull(). 148a44d6eacSMartin KaFai Lau */ 149a44d6eacSMartin KaFai Lau tp->segs_in = 0; 150a44d6eacSMartin KaFai Lau tcp_segs_in(tp, skb); 15161d2bcaeSEric Dumazet __skb_pull(skb, tcp_hdrlen(skb)); 15261d2bcaeSEric Dumazet skb_set_owner_r(skb, sk); 15361d2bcaeSEric Dumazet 1549d691539SEric Dumazet TCP_SKB_CB(skb)->seq++; 1559d691539SEric Dumazet TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_SYN; 1569d691539SEric Dumazet 15761d2bcaeSEric Dumazet tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 15861d2bcaeSEric Dumazet __skb_queue_tail(&sk->sk_receive_queue, skb); 15961d2bcaeSEric Dumazet tp->syn_data_acked = 1; 16061d2bcaeSEric Dumazet 16161d2bcaeSEric Dumazet /* u64_stats_update_begin(&tp->syncp) not needed here, 16261d2bcaeSEric Dumazet * as we certainly are not changing upper 32bit value (0) 16361d2bcaeSEric Dumazet */ 16461d2bcaeSEric Dumazet tp->bytes_received = skb->len; 165e3e17b77SEric Dumazet 166e3e17b77SEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 167e3e17b77SEric Dumazet tcp_fin(sk); 16861d2bcaeSEric Dumazet } 16961d2bcaeSEric Dumazet 1707c85af88SEric Dumazet static struct sock *tcp_fastopen_create_child(struct sock *sk, 1715b7ed089SYuchung Cheng struct sk_buff *skb, 172843f4a55SYuchung Cheng struct dst_entry *dst, 1735b7ed089SYuchung Cheng struct request_sock *req) 1745b7ed089SYuchung Cheng { 17517846376SDave Jones struct tcp_sock *tp; 1765b7ed089SYuchung Cheng struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue; 1775b7ed089SYuchung Cheng struct sock *child; 1785e0724d0SEric Dumazet bool own_req; 1795b7ed089SYuchung Cheng 1805b7ed089SYuchung Cheng req->num_retrans = 0; 1815b7ed089SYuchung Cheng req->num_timeout = 0; 1825b7ed089SYuchung Cheng req->sk = NULL; 1835b7ed089SYuchung Cheng 1845e0724d0SEric Dumazet child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL, 1855e0724d0SEric Dumazet NULL, &own_req); 18651456b29SIan Morris if (!child) 1877c85af88SEric Dumazet return NULL; 1885b7ed089SYuchung Cheng 1890536fcc0SEric Dumazet spin_lock(&queue->fastopenq.lock); 1900536fcc0SEric Dumazet queue->fastopenq.qlen++; 1910536fcc0SEric Dumazet spin_unlock(&queue->fastopenq.lock); 1925b7ed089SYuchung Cheng 1935b7ed089SYuchung Cheng /* Initialize the child socket. Have to fix some values to take 1945b7ed089SYuchung Cheng * into account the child is a Fast Open socket and is created 1955b7ed089SYuchung Cheng * only out of the bits carried in the SYN packet. 1965b7ed089SYuchung Cheng */ 1975b7ed089SYuchung Cheng tp = tcp_sk(child); 1985b7ed089SYuchung Cheng 1995b7ed089SYuchung Cheng tp->fastopen_rsk = req; 2009439ce00SEric Dumazet tcp_rsk(req)->tfo_listener = true; 2015b7ed089SYuchung Cheng 2025b7ed089SYuchung Cheng /* RFC1323: The window in SYN & SYN/ACK segments is never 2035b7ed089SYuchung Cheng * scaled. So correct it appropriately. 2045b7ed089SYuchung Cheng */ 2055b7ed089SYuchung Cheng tp->snd_wnd = ntohs(tcp_hdr(skb)->window); 2065b7ed089SYuchung Cheng 2075b7ed089SYuchung Cheng /* Activate the retrans timer so that SYNACK can be retransmitted. 208ca6fb065SEric Dumazet * The request socket is not added to the ehash 2095b7ed089SYuchung Cheng * because it's been added to the accept queue directly. 2105b7ed089SYuchung Cheng */ 2115b7ed089SYuchung Cheng inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS, 2125b7ed089SYuchung Cheng TCP_TIMEOUT_INIT, TCP_RTO_MAX); 2135b7ed089SYuchung Cheng 214ca6fb065SEric Dumazet atomic_set(&req->rsk_refcnt, 2); 2155b7ed089SYuchung Cheng 2165b7ed089SYuchung Cheng /* Now finish processing the fastopen child socket. */ 2175b7ed089SYuchung Cheng inet_csk(child)->icsk_af_ops->rebuild_header(child); 2185b7ed089SYuchung Cheng tcp_init_congestion_control(child); 2195b7ed089SYuchung Cheng tcp_mtup_init(child); 2205b7ed089SYuchung Cheng tcp_init_metrics(child); 2215b7ed089SYuchung Cheng tcp_init_buffer_space(child); 2225b7ed089SYuchung Cheng 22361d2bcaeSEric Dumazet tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; 224ba34e6d9SEric Dumazet 22561d2bcaeSEric Dumazet tcp_fastopen_add_skb(child, skb); 226d654976cSEric Dumazet 22761d2bcaeSEric Dumazet tcp_rsk(req)->rcv_nxt = tp->rcv_nxt; 2287656d842SEric Dumazet /* tcp_conn_request() is sending the SYNACK, 2297656d842SEric Dumazet * and queues the child into listener accept queue. 2307c85af88SEric Dumazet */ 2317c85af88SEric Dumazet return child; 2325b7ed089SYuchung Cheng } 2335b7ed089SYuchung Cheng 2345b7ed089SYuchung Cheng static bool tcp_fastopen_queue_check(struct sock *sk) 2355b7ed089SYuchung Cheng { 2365b7ed089SYuchung Cheng struct fastopen_queue *fastopenq; 2375b7ed089SYuchung Cheng 2385b7ed089SYuchung Cheng /* Make sure the listener has enabled fastopen, and we don't 2395b7ed089SYuchung Cheng * exceed the max # of pending TFO requests allowed before trying 2405b7ed089SYuchung Cheng * to validating the cookie in order to avoid burning CPU cycles 2415b7ed089SYuchung Cheng * unnecessarily. 2425b7ed089SYuchung Cheng * 2435b7ed089SYuchung Cheng * XXX (TFO) - The implication of checking the max_qlen before 2445b7ed089SYuchung Cheng * processing a cookie request is that clients can't differentiate 2455b7ed089SYuchung Cheng * between qlen overflow causing Fast Open to be disabled 2465b7ed089SYuchung Cheng * temporarily vs a server not supporting Fast Open at all. 2475b7ed089SYuchung Cheng */ 2480536fcc0SEric Dumazet fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq; 2490536fcc0SEric Dumazet if (fastopenq->max_qlen == 0) 2505b7ed089SYuchung Cheng return false; 2515b7ed089SYuchung Cheng 2525b7ed089SYuchung Cheng if (fastopenq->qlen >= fastopenq->max_qlen) { 2535b7ed089SYuchung Cheng struct request_sock *req1; 2545b7ed089SYuchung Cheng spin_lock(&fastopenq->lock); 2555b7ed089SYuchung Cheng req1 = fastopenq->rskq_rst_head; 256fa76ce73SEric Dumazet if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) { 2575b7ed089SYuchung Cheng spin_unlock(&fastopenq->lock); 2585b7ed089SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), 2595b7ed089SYuchung Cheng LINUX_MIB_TCPFASTOPENLISTENOVERFLOW); 2605b7ed089SYuchung Cheng return false; 2615b7ed089SYuchung Cheng } 2625b7ed089SYuchung Cheng fastopenq->rskq_rst_head = req1->dl_next; 2635b7ed089SYuchung Cheng fastopenq->qlen--; 2645b7ed089SYuchung Cheng spin_unlock(&fastopenq->lock); 26513854e5aSEric Dumazet reqsk_put(req1); 2665b7ed089SYuchung Cheng } 2675b7ed089SYuchung Cheng return true; 2685b7ed089SYuchung Cheng } 2695b7ed089SYuchung Cheng 27089278c9dSYuchung Cheng /* Returns true if we should perform Fast Open on the SYN. The cookie (foc) 27189278c9dSYuchung Cheng * may be updated and return the client in the SYN-ACK later. E.g., Fast Open 27289278c9dSYuchung Cheng * cookie request (foc->len == 0). 27389278c9dSYuchung Cheng */ 2747c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb, 2755b7ed089SYuchung Cheng struct request_sock *req, 276843f4a55SYuchung Cheng struct tcp_fastopen_cookie *foc, 277843f4a55SYuchung Cheng struct dst_entry *dst) 2785b7ed089SYuchung Cheng { 27989278c9dSYuchung Cheng struct tcp_fastopen_cookie valid_foc = { .len = -1 }; 28089278c9dSYuchung Cheng bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1; 2817c85af88SEric Dumazet struct sock *child; 2825b7ed089SYuchung Cheng 283531c94a9SYuchung Cheng if (foc->len == 0) /* Client requests a cookie */ 284531c94a9SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD); 285531c94a9SYuchung Cheng 28689278c9dSYuchung Cheng if (!((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) && 28789278c9dSYuchung Cheng (syn_data || foc->len >= 0) && 28889278c9dSYuchung Cheng tcp_fastopen_queue_check(sk))) { 28989278c9dSYuchung Cheng foc->len = -1; 2907c85af88SEric Dumazet return NULL; 2915b7ed089SYuchung Cheng } 29289278c9dSYuchung Cheng 29389278c9dSYuchung Cheng if (syn_data && (sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD)) 29489278c9dSYuchung Cheng goto fastopen; 29589278c9dSYuchung Cheng 296531c94a9SYuchung Cheng if (foc->len >= 0 && /* Client presents or requests a cookie */ 297531c94a9SYuchung Cheng tcp_fastopen_cookie_gen(req, skb, &valid_foc) && 2983a19ce0eSDaniel Lee foc->len == TCP_FASTOPEN_COOKIE_SIZE && 29989278c9dSYuchung Cheng foc->len == valid_foc.len && 30089278c9dSYuchung Cheng !memcmp(foc->val, valid_foc.val, foc->len)) { 301843f4a55SYuchung Cheng /* Cookie is valid. Create a (full) child socket to accept 302843f4a55SYuchung Cheng * the data in SYN before returning a SYN-ACK to ack the 303843f4a55SYuchung Cheng * data. If we fail to create the socket, fall back and 304843f4a55SYuchung Cheng * ack the ISN only but includes the same cookie. 305843f4a55SYuchung Cheng * 306843f4a55SYuchung Cheng * Note: Data-less SYN with valid cookie is allowed to send 307843f4a55SYuchung Cheng * data in SYN_RECV state. 308843f4a55SYuchung Cheng */ 30989278c9dSYuchung Cheng fastopen: 3107c85af88SEric Dumazet child = tcp_fastopen_create_child(sk, skb, dst, req); 3117c85af88SEric Dumazet if (child) { 31289278c9dSYuchung Cheng foc->len = -1; 313843f4a55SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), 314843f4a55SYuchung Cheng LINUX_MIB_TCPFASTOPENPASSIVE); 3157c85af88SEric Dumazet return child; 3165b7ed089SYuchung Cheng } 317531c94a9SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL); 318531c94a9SYuchung Cheng } else if (foc->len > 0) /* Client presents an invalid cookie */ 319531c94a9SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL); 3205b7ed089SYuchung Cheng 3217f9b838bSDaniel Lee valid_foc.exp = foc->exp; 32289278c9dSYuchung Cheng *foc = valid_foc; 3237c85af88SEric Dumazet return NULL; 3245b7ed089SYuchung Cheng } 325