12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20e87506fSArnaldo Carvalho de Melo /*
30e87506fSArnaldo Carvalho de Melo * NET Generic infrastructure for Network protocols.
40e87506fSArnaldo Carvalho de Melo *
50e87506fSArnaldo Carvalho de Melo * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
60e87506fSArnaldo Carvalho de Melo *
70e87506fSArnaldo Carvalho de Melo * From code originally in include/net/tcp.h
80e87506fSArnaldo Carvalho de Melo */
90e87506fSArnaldo Carvalho de Melo
100e87506fSArnaldo Carvalho de Melo #include <linux/module.h>
110e87506fSArnaldo Carvalho de Melo #include <linux/random.h>
120e87506fSArnaldo Carvalho de Melo #include <linux/slab.h>
130e87506fSArnaldo Carvalho de Melo #include <linux/string.h>
148336886fSJerry Chu #include <linux/tcp.h>
1572a3effaSEric Dumazet #include <linux/vmalloc.h>
160e87506fSArnaldo Carvalho de Melo
170e87506fSArnaldo Carvalho de Melo #include <net/request_sock.h>
180e87506fSArnaldo Carvalho de Melo
19e52c1f17SDavid S. Miller /*
20e52c1f17SDavid S. Miller * Maximum number of SYN_RECV sockets in queue per LISTEN socket.
21e52c1f17SDavid S. Miller * One SYN_RECV socket costs about 80bytes on a 32bit machine.
22e52c1f17SDavid S. Miller * It would be better to replace it with a global counter for all sockets
23e52c1f17SDavid S. Miller * but then some measure against one socket starving all other sockets
24e52c1f17SDavid S. Miller * would be needed.
25e52c1f17SDavid S. Miller *
2699b53bddSPeter Pan(潘卫平) * The minimum value of it is 128. Experiments with real servers show that
27e52c1f17SDavid S. Miller * it is absolutely not enough even at 100conn/sec. 256 cures most
2899b53bddSPeter Pan(潘卫平) * of problems.
2999b53bddSPeter Pan(潘卫平) * This value is adjusted to 128 for low memory machines,
3099b53bddSPeter Pan(潘卫平) * and it will increase in proportion to the memory of machine.
3172a3effaSEric Dumazet * Note : Dont forget somaxconn that may limit backlog too.
32e52c1f17SDavid S. Miller */
33e52c1f17SDavid S. Miller
reqsk_queue_alloc(struct request_sock_queue * queue)34ef547f2aSEric Dumazet void reqsk_queue_alloc(struct request_sock_queue *queue)
350e87506fSArnaldo Carvalho de Melo {
360536fcc0SEric Dumazet queue->fastopenq.rskq_rst_head = NULL;
370536fcc0SEric Dumazet queue->fastopenq.rskq_rst_tail = NULL;
380536fcc0SEric Dumazet queue->fastopenq.qlen = 0;
390536fcc0SEric Dumazet
403eb4801dSNorbert Kiesel queue->rskq_accept_head = NULL;
4183e3609eSArnaldo Carvalho de Melo }
4283e3609eSArnaldo Carvalho de Melo
438336886fSJerry Chu /*
448336886fSJerry Chu * This function is called to set a Fast Open socket's "fastopen_rsk" field
458336886fSJerry Chu * to NULL when a TFO socket no longer needs to access the request_sock.
468336886fSJerry Chu * This happens only after 3WHS has been either completed or aborted (e.g.,
478336886fSJerry Chu * RST is received).
488336886fSJerry Chu *
498336886fSJerry Chu * Before TFO, a child socket is created only after 3WHS is completed,
508336886fSJerry Chu * hence it never needs to access the request_sock. things get a lot more
518336886fSJerry Chu * complex with TFO. A child socket, accepted or not, has to access its
528336886fSJerry Chu * request_sock for 3WHS processing, e.g., to retransmit SYN-ACK pkts,
538336886fSJerry Chu * until 3WHS is either completed or aborted. Afterwards the req will stay
548336886fSJerry Chu * until either the child socket is accepted, or in the rare case when the
558336886fSJerry Chu * listener is closed before the child is accepted.
568336886fSJerry Chu *
578336886fSJerry Chu * In short, a request socket is only freed after BOTH 3WHS has completed
588336886fSJerry Chu * (or aborted) and the child socket has been accepted (or listener closed).
598336886fSJerry Chu * When a child socket is accepted, its corresponding req->sk is set to
608336886fSJerry Chu * NULL since it's no longer needed. More importantly, "req->sk == NULL"
618336886fSJerry Chu * will be used by the code below to determine if a child socket has been
628336886fSJerry Chu * accepted or not, and the check is protected by the fastopenq->lock
638336886fSJerry Chu * described below.
648336886fSJerry Chu *
658336886fSJerry Chu * Note that fastopen_rsk is only accessed from the child socket's context
668336886fSJerry Chu * with its socket lock held. But a request_sock (req) can be accessed by
678336886fSJerry Chu * both its child socket through fastopen_rsk, and a listener socket through
688336886fSJerry Chu * icsk_accept_queue.rskq_accept_head. To protect the access a simple spin
698336886fSJerry Chu * lock per listener "icsk->icsk_accept_queue.fastopenq->lock" is created.
708336886fSJerry Chu * only in the rare case when both the listener and the child locks are held,
718336886fSJerry Chu * e.g., in inet_csk_listen_stop() do we not need to acquire the lock.
728336886fSJerry Chu * The lock also protects other fields such as fastopenq->qlen, which is
738336886fSJerry Chu * decremented by this function when fastopen_rsk is no longer needed.
748336886fSJerry Chu *
758336886fSJerry Chu * Note that another solution was to simply use the existing socket lock
768336886fSJerry Chu * from the listener. But first socket lock is difficult to use. It is not
778336886fSJerry Chu * a simple spin lock - one must consider sock_owned_by_user() and arrange
788336886fSJerry Chu * to use sk_add_backlog() stuff. But what really makes it infeasible is the
798336886fSJerry Chu * locking hierarchy violation. E.g., inet_csk_listen_stop() may try to
808336886fSJerry Chu * acquire a child's lock while holding listener's socket lock. A corner
818336886fSJerry Chu * case might also exist in tcp_v4_hnd_req() that will trigger this locking
828336886fSJerry Chu * order.
838336886fSJerry Chu *
849439ce00SEric Dumazet * This function also sets "treq->tfo_listener" to false.
859439ce00SEric Dumazet * treq->tfo_listener is used by the listener so it is protected by the
868336886fSJerry Chu * fastopenq->lock in this function.
878336886fSJerry Chu */
reqsk_fastopen_remove(struct sock * sk,struct request_sock * req,bool reset)888336886fSJerry Chu void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
898336886fSJerry Chu bool reset)
908336886fSJerry Chu {
919439ce00SEric Dumazet struct sock *lsk = req->rsk_listener;
929439ce00SEric Dumazet struct fastopen_queue *fastopenq;
939439ce00SEric Dumazet
940536fcc0SEric Dumazet fastopenq = &inet_csk(lsk)->icsk_accept_queue.fastopenq;
958336886fSJerry Chu
96*d983ea6fSEric Dumazet RCU_INIT_POINTER(tcp_sk(sk)->fastopen_rsk, NULL);
978336886fSJerry Chu spin_lock_bh(&fastopenq->lock);
988336886fSJerry Chu fastopenq->qlen--;
999439ce00SEric Dumazet tcp_rsk(req)->tfo_listener = false;
1008336886fSJerry Chu if (req->sk) /* the child socket hasn't been accepted yet */
1018336886fSJerry Chu goto out;
1028336886fSJerry Chu
1038336886fSJerry Chu if (!reset || lsk->sk_state != TCP_LISTEN) {
1048336886fSJerry Chu /* If the listener has been closed don't bother with the
1058336886fSJerry Chu * special RST handling below.
1068336886fSJerry Chu */
1078336886fSJerry Chu spin_unlock_bh(&fastopenq->lock);
10813854e5aSEric Dumazet reqsk_put(req);
1098336886fSJerry Chu return;
1108336886fSJerry Chu }
1118336886fSJerry Chu /* Wait for 60secs before removing a req that has triggered RST.
1128336886fSJerry Chu * This is a simple defense against TFO spoofing attack - by
1138336886fSJerry Chu * counting the req against fastopen.max_qlen, and disabling
1148336886fSJerry Chu * TFO when the qlen exceeds max_qlen.
1158336886fSJerry Chu *
1168336886fSJerry Chu * For more details see CoNext'11 "TCP Fast Open" paper.
1178336886fSJerry Chu */
118fa76ce73SEric Dumazet req->rsk_timer.expires = jiffies + 60*HZ;
1198336886fSJerry Chu if (fastopenq->rskq_rst_head == NULL)
1208336886fSJerry Chu fastopenq->rskq_rst_head = req;
1218336886fSJerry Chu else
1228336886fSJerry Chu fastopenq->rskq_rst_tail->dl_next = req;
1238336886fSJerry Chu
1248336886fSJerry Chu req->dl_next = NULL;
1258336886fSJerry Chu fastopenq->rskq_rst_tail = req;
1268336886fSJerry Chu fastopenq->qlen++;
1278336886fSJerry Chu out:
1288336886fSJerry Chu spin_unlock_bh(&fastopenq->lock);
1298336886fSJerry Chu }
130