162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * NET Generic infrastructure for Network protocols. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br> 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * From code originally in include/net/tcp.h 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/module.h> 1162306a36Sopenharmony_ci#include <linux/random.h> 1262306a36Sopenharmony_ci#include <linux/slab.h> 1362306a36Sopenharmony_ci#include <linux/string.h> 1462306a36Sopenharmony_ci#include <linux/tcp.h> 1562306a36Sopenharmony_ci#include <linux/vmalloc.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#include <net/request_sock.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci/* 2062306a36Sopenharmony_ci * Maximum number of SYN_RECV sockets in queue per LISTEN socket. 2162306a36Sopenharmony_ci * One SYN_RECV socket costs about 80bytes on a 32bit machine. 2262306a36Sopenharmony_ci * It would be better to replace it with a global counter for all sockets 2362306a36Sopenharmony_ci * but then some measure against one socket starving all other sockets 2462306a36Sopenharmony_ci * would be needed. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * The minimum value of it is 128. Experiments with real servers show that 2762306a36Sopenharmony_ci * it is absolutely not enough even at 100conn/sec. 256 cures most 2862306a36Sopenharmony_ci * of problems. 2962306a36Sopenharmony_ci * This value is adjusted to 128 for low memory machines, 3062306a36Sopenharmony_ci * and it will increase in proportion to the memory of machine. 3162306a36Sopenharmony_ci * Note : Dont forget somaxconn that may limit backlog too. 3262306a36Sopenharmony_ci */ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_civoid reqsk_queue_alloc(struct request_sock_queue *queue) 3562306a36Sopenharmony_ci{ 3662306a36Sopenharmony_ci queue->fastopenq.rskq_rst_head = NULL; 3762306a36Sopenharmony_ci queue->fastopenq.rskq_rst_tail = NULL; 3862306a36Sopenharmony_ci queue->fastopenq.qlen = 0; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci queue->rskq_accept_head = NULL; 4162306a36Sopenharmony_ci} 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci/* 4462306a36Sopenharmony_ci * This function is called to set a Fast Open socket's "fastopen_rsk" field 4562306a36Sopenharmony_ci * to NULL when a TFO socket no longer needs to access the request_sock. 4662306a36Sopenharmony_ci * This happens only after 3WHS has been either completed or aborted (e.g., 4762306a36Sopenharmony_ci * RST is received). 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * Before TFO, a child socket is created only after 3WHS is completed, 5062306a36Sopenharmony_ci * hence it never needs to access the request_sock. things get a lot more 5162306a36Sopenharmony_ci * complex with TFO. A child socket, accepted or not, has to access its 5262306a36Sopenharmony_ci * request_sock for 3WHS processing, e.g., to retransmit SYN-ACK pkts, 5362306a36Sopenharmony_ci * until 3WHS is either completed or aborted. Afterwards the req will stay 5462306a36Sopenharmony_ci * until either the child socket is accepted, or in the rare case when the 5562306a36Sopenharmony_ci * listener is closed before the child is accepted. 5662306a36Sopenharmony_ci * 5762306a36Sopenharmony_ci * In short, a request socket is only freed after BOTH 3WHS has completed 5862306a36Sopenharmony_ci * (or aborted) and the child socket has been accepted (or listener closed). 5962306a36Sopenharmony_ci * When a child socket is accepted, its corresponding req->sk is set to 6062306a36Sopenharmony_ci * NULL since it's no longer needed. More importantly, "req->sk == NULL" 6162306a36Sopenharmony_ci * will be used by the code below to determine if a child socket has been 6262306a36Sopenharmony_ci * accepted or not, and the check is protected by the fastopenq->lock 6362306a36Sopenharmony_ci * described below. 6462306a36Sopenharmony_ci * 6562306a36Sopenharmony_ci * Note that fastopen_rsk is only accessed from the child socket's context 6662306a36Sopenharmony_ci * with its socket lock held. But a request_sock (req) can be accessed by 6762306a36Sopenharmony_ci * both its child socket through fastopen_rsk, and a listener socket through 6862306a36Sopenharmony_ci * icsk_accept_queue.rskq_accept_head. To protect the access a simple spin 6962306a36Sopenharmony_ci * lock per listener "icsk->icsk_accept_queue.fastopenq->lock" is created. 7062306a36Sopenharmony_ci * only in the rare case when both the listener and the child locks are held, 7162306a36Sopenharmony_ci * e.g., in inet_csk_listen_stop() do we not need to acquire the lock. 7262306a36Sopenharmony_ci * The lock also protects other fields such as fastopenq->qlen, which is 7362306a36Sopenharmony_ci * decremented by this function when fastopen_rsk is no longer needed. 7462306a36Sopenharmony_ci * 7562306a36Sopenharmony_ci * Note that another solution was to simply use the existing socket lock 7662306a36Sopenharmony_ci * from the listener. But first socket lock is difficult to use. It is not 7762306a36Sopenharmony_ci * a simple spin lock - one must consider sock_owned_by_user() and arrange 7862306a36Sopenharmony_ci * to use sk_add_backlog() stuff. But what really makes it infeasible is the 7962306a36Sopenharmony_ci * locking hierarchy violation. E.g., inet_csk_listen_stop() may try to 8062306a36Sopenharmony_ci * acquire a child's lock while holding listener's socket lock. A corner 8162306a36Sopenharmony_ci * case might also exist in tcp_v4_hnd_req() that will trigger this locking 8262306a36Sopenharmony_ci * order. 8362306a36Sopenharmony_ci * 8462306a36Sopenharmony_ci * This function also sets "treq->tfo_listener" to false. 8562306a36Sopenharmony_ci * treq->tfo_listener is used by the listener so it is protected by the 8662306a36Sopenharmony_ci * fastopenq->lock in this function. 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_civoid reqsk_fastopen_remove(struct sock *sk, struct request_sock *req, 8962306a36Sopenharmony_ci bool reset) 9062306a36Sopenharmony_ci{ 9162306a36Sopenharmony_ci struct sock *lsk = req->rsk_listener; 9262306a36Sopenharmony_ci struct fastopen_queue *fastopenq; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci fastopenq = &inet_csk(lsk)->icsk_accept_queue.fastopenq; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci RCU_INIT_POINTER(tcp_sk(sk)->fastopen_rsk, NULL); 9762306a36Sopenharmony_ci spin_lock_bh(&fastopenq->lock); 9862306a36Sopenharmony_ci fastopenq->qlen--; 9962306a36Sopenharmony_ci tcp_rsk(req)->tfo_listener = false; 10062306a36Sopenharmony_ci if (req->sk) /* the child socket hasn't been accepted yet */ 10162306a36Sopenharmony_ci goto out; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci if (!reset || lsk->sk_state != TCP_LISTEN) { 10462306a36Sopenharmony_ci /* If the listener has been closed don't bother with the 10562306a36Sopenharmony_ci * special RST handling below. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci spin_unlock_bh(&fastopenq->lock); 10862306a36Sopenharmony_ci reqsk_put(req); 10962306a36Sopenharmony_ci return; 11062306a36Sopenharmony_ci } 11162306a36Sopenharmony_ci /* Wait for 60secs before removing a req that has triggered RST. 11262306a36Sopenharmony_ci * This is a simple defense against TFO spoofing attack - by 11362306a36Sopenharmony_ci * counting the req against fastopen.max_qlen, and disabling 11462306a36Sopenharmony_ci * TFO when the qlen exceeds max_qlen. 11562306a36Sopenharmony_ci * 11662306a36Sopenharmony_ci * For more details see CoNext'11 "TCP Fast Open" paper. 11762306a36Sopenharmony_ci */ 11862306a36Sopenharmony_ci req->rsk_timer.expires = jiffies + 60*HZ; 11962306a36Sopenharmony_ci if (fastopenq->rskq_rst_head == NULL) 12062306a36Sopenharmony_ci fastopenq->rskq_rst_head = req; 12162306a36Sopenharmony_ci else 12262306a36Sopenharmony_ci fastopenq->rskq_rst_tail->dl_next = req; 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci req->dl_next = NULL; 12562306a36Sopenharmony_ci fastopenq->rskq_rst_tail = req; 12662306a36Sopenharmony_ci fastopenq->qlen++; 12762306a36Sopenharmony_ciout: 12862306a36Sopenharmony_ci spin_unlock_bh(&fastopenq->lock); 12962306a36Sopenharmony_ci} 130