xref: /linux-6.15/include/net/request_sock.h (revision 6be49dea)
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>
21*6be49deaSJason Xing #include <net/rstreason.h>
222e6599cbSArnaldo Carvalho de Melo 
2360236fddSArnaldo Carvalho de Melo struct request_sock;
242e6599cbSArnaldo Carvalho de Melo struct sk_buff;
252e6599cbSArnaldo Carvalho de Melo struct dst_entry;
262e6599cbSArnaldo Carvalho de Melo struct proto;
272e6599cbSArnaldo Carvalho de Melo 
2860236fddSArnaldo Carvalho de Melo struct request_sock_ops {
292e6599cbSArnaldo Carvalho de Melo 	int		family;
30417ccf6bSAlexey Dobriyan 	unsigned int	obj_size;
31e18b890bSChristoph Lameter 	struct kmem_cache	*slab;
327e56b5d6SCatalin Marinas 	char		*slab_name;
33ea3bea3aSEric Dumazet 	int		(*rtx_syn_ack)(const struct sock *sk,
341a2c6181SChristoph Paasch 				       struct request_sock *req);
35a00e7444SEric Dumazet 	void		(*send_ack)(const struct sock *sk, struct sk_buff *skb,
3660236fddSArnaldo Carvalho de Melo 				    struct request_sock *req);
37a00e7444SEric Dumazet 	void		(*send_reset)(const struct sock *sk,
38*6be49deaSJason Xing 				      struct sk_buff *skb,
39*6be49deaSJason Xing 				      enum sk_rst_reason reason);
4060236fddSArnaldo Carvalho de Melo 	void		(*destructor)(struct request_sock *req);
4142cb80a2SEric Dumazet 	void		(*syn_ack_timeout)(const struct request_sock *req);
422e6599cbSArnaldo Carvalho de Melo };
432e6599cbSArnaldo Carvalho de Melo 
441b70e977SEric Dumazet int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req);
45e6c022a4SEric Dumazet 
4670a217f1SMartin KaFai Lau struct saved_syn {
47267cf9faSMartin KaFai Lau 	u32 mac_hdrlen;
4870a217f1SMartin KaFai Lau 	u32 network_hdrlen;
4970a217f1SMartin KaFai Lau 	u32 tcp_hdrlen;
5070a217f1SMartin KaFai Lau 	u8 data[];
5170a217f1SMartin KaFai Lau };
5270a217f1SMartin KaFai Lau 
5360236fddSArnaldo Carvalho de Melo /* struct request_sock - mini sock to represent a connection request
542e6599cbSArnaldo Carvalho de Melo  */
5560236fddSArnaldo Carvalho de Melo struct request_sock {
56634fb979SEric Dumazet 	struct sock_common		__req_common;
571e2e0117SEric Dumazet #define rsk_refcnt			__req_common.skc_refcnt
5852452c54SEric Dumazet #define rsk_hash			__req_common.skc_hash
598e5eb54dSEric Dumazet #define rsk_listener			__req_common.skc_listener
60ed53d0abSEric Dumazet #define rsk_window_clamp		__req_common.skc_window_clamp
61ed53d0abSEric Dumazet #define rsk_rcv_wnd			__req_common.skc_rcv_wnd
621e2e0117SEric Dumazet 
633fb62c5dSEric Dumazet 	struct request_sock		*dl_next;
642e6599cbSArnaldo Carvalho de Melo 	u16				mss;
65e6c022a4SEric Dumazet 	u8				num_retrans; /* number of retransmits */
66956c0d61SKuniyuki Iwashima 	u8				syncookie:1; /* True if
67956c0d61SKuniyuki Iwashima 						      * 1) tcpopts needs to be encoded in
68956c0d61SKuniyuki Iwashima 						      *    TS of SYN+ACK
69956c0d61SKuniyuki Iwashima 						      * 2) ACK is validated by BPF kfunc.
70956c0d61SKuniyuki Iwashima 						      */
71e6c022a4SEric Dumazet 	u8				num_timeout:7; /* number of timeouts */
722e6599cbSArnaldo Carvalho de Melo 	u32				ts_recent;
73fa76ce73SEric Dumazet 	struct timer_list		rsk_timer;
7472a3effaSEric Dumazet 	const struct request_sock_ops	*rsk_ops;
752e6599cbSArnaldo Carvalho de Melo 	struct sock			*sk;
7670a217f1SMartin KaFai Lau 	struct saved_syn		*saved_syn;
774237c75cSVenkat Yekkirala 	u32				secid;
786b877699SVenkat Yekkirala 	u32				peer_secid;
795903123fSAkhmat Karakotov 	u32				timeout;
802e6599cbSArnaldo Carvalho de Melo };
812e6599cbSArnaldo Carvalho de Melo 
82b1f0a0e9SFlorian Westphal static inline struct request_sock *inet_reqsk(const struct sock *sk)
83b267cdd1SEric Dumazet {
84b267cdd1SEric Dumazet 	return (struct request_sock *)sk;
85b267cdd1SEric Dumazet }
86b267cdd1SEric Dumazet 
87b267cdd1SEric Dumazet static inline struct sock *req_to_sk(struct request_sock *req)
88b267cdd1SEric Dumazet {
89b267cdd1SEric Dumazet 	return (struct sock *)req;
90b267cdd1SEric Dumazet }
91b267cdd1SEric Dumazet 
9295e752b5SKuniyuki Iwashima /**
9395e752b5SKuniyuki Iwashima  * skb_steal_sock - steal a socket from an sk_buff
9495e752b5SKuniyuki Iwashima  * @skb: sk_buff to steal the socket from
9595e752b5SKuniyuki Iwashima  * @refcounted: is set to true if the socket is reference-counted
9695e752b5SKuniyuki Iwashima  * @prefetched: is set to true if the socket was assigned from bpf
9795e752b5SKuniyuki Iwashima  */
9895e752b5SKuniyuki Iwashima static inline struct sock *skb_steal_sock(struct sk_buff *skb,
9995e752b5SKuniyuki Iwashima 					  bool *refcounted, bool *prefetched)
10095e752b5SKuniyuki Iwashima {
10195e752b5SKuniyuki Iwashima 	struct sock *sk = skb->sk;
10295e752b5SKuniyuki Iwashima 
10395e752b5SKuniyuki Iwashima 	if (!sk) {
10495e752b5SKuniyuki Iwashima 		*prefetched = false;
10595e752b5SKuniyuki Iwashima 		*refcounted = false;
10695e752b5SKuniyuki Iwashima 		return NULL;
10795e752b5SKuniyuki Iwashima 	}
10895e752b5SKuniyuki Iwashima 
10995e752b5SKuniyuki Iwashima 	*prefetched = skb_sk_is_prefetched(skb);
1108b5ac68fSKuniyuki Iwashima 	if (*prefetched) {
1118b5ac68fSKuniyuki Iwashima #if IS_ENABLED(CONFIG_SYN_COOKIES)
1128b5ac68fSKuniyuki Iwashima 		if (sk->sk_state == TCP_NEW_SYN_RECV && inet_reqsk(sk)->syncookie) {
1138b5ac68fSKuniyuki Iwashima 			struct request_sock *req = inet_reqsk(sk);
1148b5ac68fSKuniyuki Iwashima 
1158b5ac68fSKuniyuki Iwashima 			*refcounted = false;
1168b5ac68fSKuniyuki Iwashima 			sk = req->rsk_listener;
1178b5ac68fSKuniyuki Iwashima 			req->rsk_listener = NULL;
1188b5ac68fSKuniyuki Iwashima 			return sk;
1198b5ac68fSKuniyuki Iwashima 		}
1208b5ac68fSKuniyuki Iwashima #endif
12195e752b5SKuniyuki Iwashima 		*refcounted = sk_is_refcounted(sk);
1228b5ac68fSKuniyuki Iwashima 	} else {
12395e752b5SKuniyuki Iwashima 		*refcounted = true;
1248b5ac68fSKuniyuki Iwashima 	}
12595e752b5SKuniyuki Iwashima 
12695e752b5SKuniyuki Iwashima 	skb->destructor = NULL;
12795e752b5SKuniyuki Iwashima 	skb->sk = NULL;
12895e752b5SKuniyuki Iwashima 	return sk;
12995e752b5SKuniyuki Iwashima }
13095e752b5SKuniyuki Iwashima 
1314e9a578eSEric Dumazet static inline struct request_sock *
132a1a5344dSEric Dumazet reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener,
133a1a5344dSEric Dumazet 	    bool attach_listener)
1342e6599cbSArnaldo Carvalho de Melo {
135e96f78abSEric Dumazet 	struct request_sock *req;
136e96f78abSEric Dumazet 
137e96f78abSEric Dumazet 	req = kmem_cache_alloc(ops->slab, GFP_ATOMIC | __GFP_NOWARN);
1383a5d1c0eSEric Dumazet 	if (!req)
1393a5d1c0eSEric Dumazet 		return NULL;
140a1a5344dSEric Dumazet 	req->rsk_listener = NULL;
1413a5d1c0eSEric Dumazet 	if (attach_listener) {
14241c6d650SReshetova, Elena 		if (unlikely(!refcount_inc_not_zero(&sk_listener->sk_refcnt))) {
1433a5d1c0eSEric Dumazet 			kmem_cache_free(ops->slab, req);
1443a5d1c0eSEric Dumazet 			return NULL;
145a1a5344dSEric Dumazet 		}
1463a5d1c0eSEric Dumazet 		req->rsk_listener = sk_listener;
1473a5d1c0eSEric Dumazet 	}
1483a5d1c0eSEric Dumazet 	req->rsk_ops = ops;
149b267cdd1SEric Dumazet 	req_to_sk(req)->sk_prot = sk_listener->sk_prot;
150b267cdd1SEric Dumazet 	sk_node_init(&req_to_sk(req)->sk_node);
151004a5d01SEric Dumazet 	sk_tx_queue_clear(req_to_sk(req));
152cd8ae852SEric Dumazet 	req->saved_syn = NULL;
153956c0d61SKuniyuki Iwashima 	req->syncookie = 0;
1545903123fSAkhmat Karakotov 	req->timeout = 0;
15585f9aa75SEric Dumazet 	req->num_timeout = 0;
15685f9aa75SEric Dumazet 	req->num_retrans = 0;
15785f9aa75SEric Dumazet 	req->sk = NULL;
15841c6d650SReshetova, Elena 	refcount_set(&req->rsk_refcnt, 0);
1593a5d1c0eSEric Dumazet 
1602e6599cbSArnaldo Carvalho de Melo 	return req;
1612e6599cbSArnaldo Carvalho de Melo }
1622e6599cbSArnaldo Carvalho de Melo 
1639403cf23SGuillaume Nault static inline void __reqsk_free(struct request_sock *req)
1642e6599cbSArnaldo Carvalho de Melo {
16560236fddSArnaldo Carvalho de Melo 	req->rsk_ops->destructor(req);
1664e9a578eSEric Dumazet 	if (req->rsk_listener)
1674e9a578eSEric Dumazet 		sock_put(req->rsk_listener);
168cd8ae852SEric Dumazet 	kfree(req->saved_syn);
16913854e5aSEric Dumazet 	kmem_cache_free(req->rsk_ops->slab, req);
1702e6599cbSArnaldo Carvalho de Melo }
1712e6599cbSArnaldo Carvalho de Melo 
1729403cf23SGuillaume Nault static inline void reqsk_free(struct request_sock *req)
1739403cf23SGuillaume Nault {
1749403cf23SGuillaume Nault 	WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0);
1759403cf23SGuillaume Nault 	__reqsk_free(req);
1769403cf23SGuillaume Nault }
1779403cf23SGuillaume Nault 
1781e2e0117SEric Dumazet static inline void reqsk_put(struct request_sock *req)
1791e2e0117SEric Dumazet {
18041c6d650SReshetova, Elena 	if (refcount_dec_and_test(&req->rsk_refcnt))
1811e2e0117SEric Dumazet 		reqsk_free(req);
1821e2e0117SEric Dumazet }
1830e87506fSArnaldo Carvalho de Melo 
18410467163SJerry Chu /*
18510467163SJerry Chu  * For a TCP Fast Open listener -
18610467163SJerry Chu  *	lock - protects the access to all the reqsk, which is co-owned by
18710467163SJerry Chu  *		the listener and the child socket.
18810467163SJerry Chu  *	qlen - pending TFO requests (still in TCP_SYN_RECV).
18910467163SJerry Chu  *	max_qlen - max TFO reqs allowed before TFO is disabled.
19010467163SJerry Chu  *
19110467163SJerry Chu  *	XXX (TFO) - ideally these fields can be made as part of "listen_sock"
19210467163SJerry Chu  *	structure above. But there is some implementation difficulty due to
19310467163SJerry Chu  *	listen_sock being part of request_sock_queue hence will be freed when
19410467163SJerry Chu  *	a listener is stopped. But TFO related fields may continue to be
19510467163SJerry Chu  *	accessed even after a listener is closed, until its sk_refcnt drops
19610467163SJerry Chu  *	to 0 implying no more outstanding TFO reqs. One solution is to keep
19710467163SJerry Chu  *	listen_opt around until	sk_refcnt drops to 0. But there is some other
19810467163SJerry Chu  *	complexity that needs to be resolved. E.g., a listener can be disabled
19910467163SJerry Chu  *	temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
20010467163SJerry Chu  */
20110467163SJerry Chu struct fastopen_queue {
20210467163SJerry Chu 	struct request_sock	*rskq_rst_head; /* Keep track of past TFO */
20310467163SJerry Chu 	struct request_sock	*rskq_rst_tail; /* requests that caused RST.
20410467163SJerry Chu 						 * This is part of the defense
20510467163SJerry Chu 						 * against spoofing attack.
20610467163SJerry Chu 						 */
20710467163SJerry Chu 	spinlock_t	lock;
20810467163SJerry Chu 	int		qlen;		/* # of pending (TCP_SYN_RECV) reqs */
20910467163SJerry Chu 	int		max_qlen;	/* != 0 iff TFO is currently enabled */
2101fba70e5SYuchung Cheng 
2111fba70e5SYuchung Cheng 	struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */
21210467163SJerry Chu };
21310467163SJerry Chu 
2140e87506fSArnaldo Carvalho de Melo /** struct request_sock_queue - queue of request_socks
2150e87506fSArnaldo Carvalho de Melo  *
2160e87506fSArnaldo Carvalho de Melo  * @rskq_accept_head - FIFO head of established children
2170e87506fSArnaldo Carvalho de Melo  * @rskq_accept_tail - FIFO tail of established children
218295f7324SArnaldo Carvalho de Melo  * @rskq_defer_accept - User waits for some data after accept()
2190e87506fSArnaldo Carvalho de Melo  *
2200e87506fSArnaldo Carvalho de Melo  */
2210e87506fSArnaldo Carvalho de Melo struct request_sock_queue {
222fff1f300SEric Dumazet 	spinlock_t		rskq_lock;
223fff1f300SEric Dumazet 	u8			rskq_defer_accept;
224ef547f2aSEric Dumazet 
2258d2675f1SEric Dumazet 	u32			synflood_warned;
226aac065c5SEric Dumazet 	atomic_t		qlen;
227aac065c5SEric Dumazet 	atomic_t		young;
228aac065c5SEric Dumazet 
2290e87506fSArnaldo Carvalho de Melo 	struct request_sock	*rskq_accept_head;
2300e87506fSArnaldo Carvalho de Melo 	struct request_sock	*rskq_accept_tail;
2310536fcc0SEric Dumazet 	struct fastopen_queue	fastopenq;  /* Check max_qlen != 0 to determine
2320536fcc0SEric Dumazet 					     * if TFO is enabled.
23310467163SJerry Chu 					     */
2340e87506fSArnaldo Carvalho de Melo };
2350e87506fSArnaldo Carvalho de Melo 
236ef547f2aSEric Dumazet void reqsk_queue_alloc(struct request_sock_queue *queue);
2370e87506fSArnaldo Carvalho de Melo 
238c0f4502aSJoe Perches void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
239c0f4502aSJoe Perches 			   bool reset);
24083e3609eSArnaldo Carvalho de Melo 
241fff1f300SEric Dumazet static inline bool reqsk_queue_empty(const struct request_sock_queue *queue)
2420e87506fSArnaldo Carvalho de Melo {
24360b173caSEric Dumazet 	return READ_ONCE(queue->rskq_accept_head) == NULL;
2440e87506fSArnaldo Carvalho de Melo }
2450e87506fSArnaldo Carvalho de Melo 
246fff1f300SEric Dumazet static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue,
247fff1f300SEric Dumazet 						      struct sock *parent)
2480e87506fSArnaldo Carvalho de Melo {
249fff1f300SEric Dumazet 	struct request_sock *req;
2500e87506fSArnaldo Carvalho de Melo 
251fff1f300SEric Dumazet 	spin_lock_bh(&queue->rskq_lock);
252fff1f300SEric Dumazet 	req = queue->rskq_accept_head;
253fff1f300SEric Dumazet 	if (req) {
254fff1f300SEric Dumazet 		sk_acceptq_removed(parent);
25560b173caSEric Dumazet 		WRITE_ONCE(queue->rskq_accept_head, req->dl_next);
2560e87506fSArnaldo Carvalho de Melo 		if (queue->rskq_accept_head == NULL)
2570e87506fSArnaldo Carvalho de Melo 			queue->rskq_accept_tail = NULL;
258fff1f300SEric Dumazet 	}
259fff1f300SEric Dumazet 	spin_unlock_bh(&queue->rskq_lock);
2600e87506fSArnaldo Carvalho de Melo 	return req;
2610e87506fSArnaldo Carvalho de Melo }
2620e87506fSArnaldo Carvalho de Melo 
263fa76ce73SEric Dumazet static inline void reqsk_queue_removed(struct request_sock_queue *queue,
264fa76ce73SEric Dumazet 				       const struct request_sock *req)
2650e87506fSArnaldo Carvalho de Melo {
266e6c022a4SEric Dumazet 	if (req->num_timeout == 0)
267aac065c5SEric Dumazet 		atomic_dec(&queue->young);
268aac065c5SEric Dumazet 	atomic_dec(&queue->qlen);
2690e87506fSArnaldo Carvalho de Melo }
2700e87506fSArnaldo Carvalho de Melo 
271fa76ce73SEric Dumazet static inline void reqsk_queue_added(struct request_sock_queue *queue)
2720e87506fSArnaldo Carvalho de Melo {
273aac065c5SEric Dumazet 	atomic_inc(&queue->young);
274aac065c5SEric Dumazet 	atomic_inc(&queue->qlen);
2750e87506fSArnaldo Carvalho de Melo }
2760e87506fSArnaldo Carvalho de Melo 
277463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len(const struct request_sock_queue *queue)
2780e87506fSArnaldo Carvalho de Melo {
279aac065c5SEric Dumazet 	return atomic_read(&queue->qlen);
2800e87506fSArnaldo Carvalho de Melo }
2810e87506fSArnaldo Carvalho de Melo 
282463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
2830e87506fSArnaldo Carvalho de Melo {
284aac065c5SEric Dumazet 	return atomic_read(&queue->young);
2850e87506fSArnaldo Carvalho de Melo }
2860e87506fSArnaldo Carvalho de Melo 
2872e6599cbSArnaldo Carvalho de Melo #endif /* _REQUEST_SOCK_H */
288