xref: /linux-6.15/include/net/request_sock.h (revision 70a217f1)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
22e6599cbSArnaldo Carvalho de Melo /*
32e6599cbSArnaldo Carvalho de Melo  * NET		Generic infrastructure for Network protocols.
42e6599cbSArnaldo Carvalho de Melo  *
52e6599cbSArnaldo Carvalho de Melo  *		Definitions for request_sock
62e6599cbSArnaldo Carvalho de Melo  *
72e6599cbSArnaldo Carvalho de Melo  * Authors:	Arnaldo Carvalho de Melo <[email protected]>
82e6599cbSArnaldo Carvalho de Melo  *
92e6599cbSArnaldo Carvalho de Melo  * 		From code originally in include/net/tcp.h
102e6599cbSArnaldo Carvalho de Melo  */
112e6599cbSArnaldo Carvalho de Melo #ifndef _REQUEST_SOCK_H
122e6599cbSArnaldo Carvalho de Melo #define _REQUEST_SOCK_H
132e6599cbSArnaldo Carvalho de Melo 
142e6599cbSArnaldo Carvalho de Melo #include <linux/slab.h>
150e87506fSArnaldo Carvalho de Melo #include <linux/spinlock.h>
162e6599cbSArnaldo Carvalho de Melo #include <linux/types.h>
17547b792cSIlpo Järvinen #include <linux/bug.h>
1841c6d650SReshetova, Elena #include <linux/refcount.h>
190e87506fSArnaldo Carvalho de Melo 
202e6599cbSArnaldo Carvalho de Melo #include <net/sock.h>
212e6599cbSArnaldo Carvalho de Melo 
2260236fddSArnaldo Carvalho de Melo struct request_sock;
232e6599cbSArnaldo Carvalho de Melo struct sk_buff;
242e6599cbSArnaldo Carvalho de Melo struct dst_entry;
252e6599cbSArnaldo Carvalho de Melo struct proto;
262e6599cbSArnaldo Carvalho de Melo 
2760236fddSArnaldo Carvalho de Melo struct request_sock_ops {
282e6599cbSArnaldo Carvalho de Melo 	int		family;
29417ccf6bSAlexey Dobriyan 	unsigned int	obj_size;
30e18b890bSChristoph Lameter 	struct kmem_cache	*slab;
317e56b5d6SCatalin Marinas 	char		*slab_name;
32ea3bea3aSEric Dumazet 	int		(*rtx_syn_ack)(const struct sock *sk,
331a2c6181SChristoph Paasch 				       struct request_sock *req);
34a00e7444SEric Dumazet 	void		(*send_ack)(const struct sock *sk, struct sk_buff *skb,
3560236fddSArnaldo Carvalho de Melo 				    struct request_sock *req);
36a00e7444SEric Dumazet 	void		(*send_reset)(const struct sock *sk,
37cfb6eeb4SYOSHIFUJI Hideaki 				      struct sk_buff *skb);
3860236fddSArnaldo Carvalho de Melo 	void		(*destructor)(struct request_sock *req);
3942cb80a2SEric Dumazet 	void		(*syn_ack_timeout)(const struct request_sock *req);
402e6599cbSArnaldo Carvalho de Melo };
412e6599cbSArnaldo Carvalho de Melo 
421b70e977SEric Dumazet int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req);
43e6c022a4SEric Dumazet 
44*70a217f1SMartin KaFai Lau struct saved_syn {
45*70a217f1SMartin KaFai Lau 	u32 network_hdrlen;
46*70a217f1SMartin KaFai Lau 	u32 tcp_hdrlen;
47*70a217f1SMartin KaFai Lau 	u8 data[];
48*70a217f1SMartin KaFai Lau };
49*70a217f1SMartin KaFai Lau 
5060236fddSArnaldo Carvalho de Melo /* struct request_sock - mini sock to represent a connection request
512e6599cbSArnaldo Carvalho de Melo  */
5260236fddSArnaldo Carvalho de Melo struct request_sock {
53634fb979SEric Dumazet 	struct sock_common		__req_common;
541e2e0117SEric Dumazet #define rsk_refcnt			__req_common.skc_refcnt
5552452c54SEric Dumazet #define rsk_hash			__req_common.skc_hash
568e5eb54dSEric Dumazet #define rsk_listener			__req_common.skc_listener
57ed53d0abSEric Dumazet #define rsk_window_clamp		__req_common.skc_window_clamp
58ed53d0abSEric Dumazet #define rsk_rcv_wnd			__req_common.skc_rcv_wnd
591e2e0117SEric Dumazet 
603fb62c5dSEric Dumazet 	struct request_sock		*dl_next;
612e6599cbSArnaldo Carvalho de Melo 	u16				mss;
62e6c022a4SEric Dumazet 	u8				num_retrans; /* number of retransmits */
63f8ace8d9SFlorian Westphal 	u8				syncookie:1; /* syncookie: encode tcpopts in timestamp */
64e6c022a4SEric Dumazet 	u8				num_timeout:7; /* number of timeouts */
652e6599cbSArnaldo Carvalho de Melo 	u32				ts_recent;
66fa76ce73SEric Dumazet 	struct timer_list		rsk_timer;
6772a3effaSEric Dumazet 	const struct request_sock_ops	*rsk_ops;
682e6599cbSArnaldo Carvalho de Melo 	struct sock			*sk;
69*70a217f1SMartin KaFai Lau 	struct saved_syn		*saved_syn;
704237c75cSVenkat Yekkirala 	u32				secid;
716b877699SVenkat Yekkirala 	u32				peer_secid;
722e6599cbSArnaldo Carvalho de Melo };
732e6599cbSArnaldo Carvalho de Melo 
74b1f0a0e9SFlorian Westphal static inline struct request_sock *inet_reqsk(const struct sock *sk)
75b267cdd1SEric Dumazet {
76b267cdd1SEric Dumazet 	return (struct request_sock *)sk;
77b267cdd1SEric Dumazet }
78b267cdd1SEric Dumazet 
79b267cdd1SEric Dumazet static inline struct sock *req_to_sk(struct request_sock *req)
80b267cdd1SEric Dumazet {
81b267cdd1SEric Dumazet 	return (struct sock *)req;
82b267cdd1SEric Dumazet }
83b267cdd1SEric Dumazet 
844e9a578eSEric Dumazet static inline struct request_sock *
85a1a5344dSEric Dumazet reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener,
86a1a5344dSEric Dumazet 	    bool attach_listener)
872e6599cbSArnaldo Carvalho de Melo {
88e96f78abSEric Dumazet 	struct request_sock *req;
89e96f78abSEric Dumazet 
90e96f78abSEric Dumazet 	req = kmem_cache_alloc(ops->slab, GFP_ATOMIC | __GFP_NOWARN);
913a5d1c0eSEric Dumazet 	if (!req)
923a5d1c0eSEric Dumazet 		return NULL;
93a1a5344dSEric Dumazet 	req->rsk_listener = NULL;
943a5d1c0eSEric Dumazet 	if (attach_listener) {
9541c6d650SReshetova, Elena 		if (unlikely(!refcount_inc_not_zero(&sk_listener->sk_refcnt))) {
963a5d1c0eSEric Dumazet 			kmem_cache_free(ops->slab, req);
973a5d1c0eSEric Dumazet 			return NULL;
98a1a5344dSEric Dumazet 		}
993a5d1c0eSEric Dumazet 		req->rsk_listener = sk_listener;
1003a5d1c0eSEric Dumazet 	}
1013a5d1c0eSEric Dumazet 	req->rsk_ops = ops;
102b267cdd1SEric Dumazet 	req_to_sk(req)->sk_prot = sk_listener->sk_prot;
103b267cdd1SEric Dumazet 	sk_node_init(&req_to_sk(req)->sk_node);
104004a5d01SEric Dumazet 	sk_tx_queue_clear(req_to_sk(req));
105cd8ae852SEric Dumazet 	req->saved_syn = NULL;
10685f9aa75SEric Dumazet 	req->num_timeout = 0;
10785f9aa75SEric Dumazet 	req->num_retrans = 0;
10885f9aa75SEric Dumazet 	req->sk = NULL;
10941c6d650SReshetova, Elena 	refcount_set(&req->rsk_refcnt, 0);
1103a5d1c0eSEric Dumazet 
1112e6599cbSArnaldo Carvalho de Melo 	return req;
1122e6599cbSArnaldo Carvalho de Melo }
1132e6599cbSArnaldo Carvalho de Melo 
1149403cf23SGuillaume Nault static inline void __reqsk_free(struct request_sock *req)
1152e6599cbSArnaldo Carvalho de Melo {
11660236fddSArnaldo Carvalho de Melo 	req->rsk_ops->destructor(req);
1174e9a578eSEric Dumazet 	if (req->rsk_listener)
1184e9a578eSEric Dumazet 		sock_put(req->rsk_listener);
119cd8ae852SEric Dumazet 	kfree(req->saved_syn);
12013854e5aSEric Dumazet 	kmem_cache_free(req->rsk_ops->slab, req);
1212e6599cbSArnaldo Carvalho de Melo }
1222e6599cbSArnaldo Carvalho de Melo 
1239403cf23SGuillaume Nault static inline void reqsk_free(struct request_sock *req)
1249403cf23SGuillaume Nault {
1259403cf23SGuillaume Nault 	WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0);
1269403cf23SGuillaume Nault 	__reqsk_free(req);
1279403cf23SGuillaume Nault }
1289403cf23SGuillaume Nault 
1291e2e0117SEric Dumazet static inline void reqsk_put(struct request_sock *req)
1301e2e0117SEric Dumazet {
13141c6d650SReshetova, Elena 	if (refcount_dec_and_test(&req->rsk_refcnt))
1321e2e0117SEric Dumazet 		reqsk_free(req);
1331e2e0117SEric Dumazet }
1340e87506fSArnaldo Carvalho de Melo 
13510467163SJerry Chu /*
13610467163SJerry Chu  * For a TCP Fast Open listener -
13710467163SJerry Chu  *	lock - protects the access to all the reqsk, which is co-owned by
13810467163SJerry Chu  *		the listener and the child socket.
13910467163SJerry Chu  *	qlen - pending TFO requests (still in TCP_SYN_RECV).
14010467163SJerry Chu  *	max_qlen - max TFO reqs allowed before TFO is disabled.
14110467163SJerry Chu  *
14210467163SJerry Chu  *	XXX (TFO) - ideally these fields can be made as part of "listen_sock"
14310467163SJerry Chu  *	structure above. But there is some implementation difficulty due to
14410467163SJerry Chu  *	listen_sock being part of request_sock_queue hence will be freed when
14510467163SJerry Chu  *	a listener is stopped. But TFO related fields may continue to be
14610467163SJerry Chu  *	accessed even after a listener is closed, until its sk_refcnt drops
14710467163SJerry Chu  *	to 0 implying no more outstanding TFO reqs. One solution is to keep
14810467163SJerry Chu  *	listen_opt around until	sk_refcnt drops to 0. But there is some other
14910467163SJerry Chu  *	complexity that needs to be resolved. E.g., a listener can be disabled
15010467163SJerry Chu  *	temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
15110467163SJerry Chu  */
15210467163SJerry Chu struct fastopen_queue {
15310467163SJerry Chu 	struct request_sock	*rskq_rst_head; /* Keep track of past TFO */
15410467163SJerry Chu 	struct request_sock	*rskq_rst_tail; /* requests that caused RST.
15510467163SJerry Chu 						 * This is part of the defense
15610467163SJerry Chu 						 * against spoofing attack.
15710467163SJerry Chu 						 */
15810467163SJerry Chu 	spinlock_t	lock;
15910467163SJerry Chu 	int		qlen;		/* # of pending (TCP_SYN_RECV) reqs */
16010467163SJerry Chu 	int		max_qlen;	/* != 0 iff TFO is currently enabled */
1611fba70e5SYuchung Cheng 
1621fba70e5SYuchung Cheng 	struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */
16310467163SJerry Chu };
16410467163SJerry Chu 
1650e87506fSArnaldo Carvalho de Melo /** struct request_sock_queue - queue of request_socks
1660e87506fSArnaldo Carvalho de Melo  *
1670e87506fSArnaldo Carvalho de Melo  * @rskq_accept_head - FIFO head of established children
1680e87506fSArnaldo Carvalho de Melo  * @rskq_accept_tail - FIFO tail of established children
169295f7324SArnaldo Carvalho de Melo  * @rskq_defer_accept - User waits for some data after accept()
1700e87506fSArnaldo Carvalho de Melo  *
1710e87506fSArnaldo Carvalho de Melo  */
1720e87506fSArnaldo Carvalho de Melo struct request_sock_queue {
173fff1f300SEric Dumazet 	spinlock_t		rskq_lock;
174fff1f300SEric Dumazet 	u8			rskq_defer_accept;
175ef547f2aSEric Dumazet 
1768d2675f1SEric Dumazet 	u32			synflood_warned;
177aac065c5SEric Dumazet 	atomic_t		qlen;
178aac065c5SEric Dumazet 	atomic_t		young;
179aac065c5SEric Dumazet 
1800e87506fSArnaldo Carvalho de Melo 	struct request_sock	*rskq_accept_head;
1810e87506fSArnaldo Carvalho de Melo 	struct request_sock	*rskq_accept_tail;
1820536fcc0SEric Dumazet 	struct fastopen_queue	fastopenq;  /* Check max_qlen != 0 to determine
1830536fcc0SEric Dumazet 					     * if TFO is enabled.
18410467163SJerry Chu 					     */
1850e87506fSArnaldo Carvalho de Melo };
1860e87506fSArnaldo Carvalho de Melo 
187ef547f2aSEric Dumazet void reqsk_queue_alloc(struct request_sock_queue *queue);
1880e87506fSArnaldo Carvalho de Melo 
189c0f4502aSJoe Perches void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
190c0f4502aSJoe Perches 			   bool reset);
19183e3609eSArnaldo Carvalho de Melo 
192fff1f300SEric Dumazet static inline bool reqsk_queue_empty(const struct request_sock_queue *queue)
1930e87506fSArnaldo Carvalho de Melo {
19460b173caSEric Dumazet 	return READ_ONCE(queue->rskq_accept_head) == NULL;
1950e87506fSArnaldo Carvalho de Melo }
1960e87506fSArnaldo Carvalho de Melo 
197fff1f300SEric Dumazet static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue,
198fff1f300SEric Dumazet 						      struct sock *parent)
1990e87506fSArnaldo Carvalho de Melo {
200fff1f300SEric Dumazet 	struct request_sock *req;
2010e87506fSArnaldo Carvalho de Melo 
202fff1f300SEric Dumazet 	spin_lock_bh(&queue->rskq_lock);
203fff1f300SEric Dumazet 	req = queue->rskq_accept_head;
204fff1f300SEric Dumazet 	if (req) {
205fff1f300SEric Dumazet 		sk_acceptq_removed(parent);
20660b173caSEric Dumazet 		WRITE_ONCE(queue->rskq_accept_head, req->dl_next);
2070e87506fSArnaldo Carvalho de Melo 		if (queue->rskq_accept_head == NULL)
2080e87506fSArnaldo Carvalho de Melo 			queue->rskq_accept_tail = NULL;
209fff1f300SEric Dumazet 	}
210fff1f300SEric Dumazet 	spin_unlock_bh(&queue->rskq_lock);
2110e87506fSArnaldo Carvalho de Melo 	return req;
2120e87506fSArnaldo Carvalho de Melo }
2130e87506fSArnaldo Carvalho de Melo 
214fa76ce73SEric Dumazet static inline void reqsk_queue_removed(struct request_sock_queue *queue,
215fa76ce73SEric Dumazet 				       const struct request_sock *req)
2160e87506fSArnaldo Carvalho de Melo {
217e6c022a4SEric Dumazet 	if (req->num_timeout == 0)
218aac065c5SEric Dumazet 		atomic_dec(&queue->young);
219aac065c5SEric Dumazet 	atomic_dec(&queue->qlen);
2200e87506fSArnaldo Carvalho de Melo }
2210e87506fSArnaldo Carvalho de Melo 
222fa76ce73SEric Dumazet static inline void reqsk_queue_added(struct request_sock_queue *queue)
2230e87506fSArnaldo Carvalho de Melo {
224aac065c5SEric Dumazet 	atomic_inc(&queue->young);
225aac065c5SEric Dumazet 	atomic_inc(&queue->qlen);
2260e87506fSArnaldo Carvalho de Melo }
2270e87506fSArnaldo Carvalho de Melo 
228463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len(const struct request_sock_queue *queue)
2290e87506fSArnaldo Carvalho de Melo {
230aac065c5SEric Dumazet 	return atomic_read(&queue->qlen);
2310e87506fSArnaldo Carvalho de Melo }
2320e87506fSArnaldo Carvalho de Melo 
233463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
2340e87506fSArnaldo Carvalho de Melo {
235aac065c5SEric Dumazet 	return atomic_read(&queue->young);
2360e87506fSArnaldo Carvalho de Melo }
2370e87506fSArnaldo Carvalho de Melo 
2382e6599cbSArnaldo Carvalho de Melo #endif /* _REQUEST_SOCK_H */
239