xref: /linux/include/net/request_sock.h (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * NET		Generic infrastructure for Network protocols.
4  *
5  *		Definitions for request_sock
6  *
7  * Authors:	Arnaldo Carvalho de Melo <acme@conectiva.com.br>
8  *
9  * 		From code originally in include/net/tcp.h
10  */
11 #ifndef _REQUEST_SOCK_H
12 #define _REQUEST_SOCK_H
13 
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/types.h>
17 #include <linux/bug.h>
18 #include <linux/refcount.h>
19 
20 #include <net/sock.h>
21 #include <net/rstreason.h>
22 
23 struct request_sock;
24 struct sk_buff;
25 struct dst_entry;
26 struct proto;
27 
28 struct request_sock_ops {
29 	int		family;
30 	unsigned int	obj_size;
31 	struct kmem_cache	*slab;
32 	char		*slab_name;
33 	void		(*send_ack)(const struct sock *sk, struct sk_buff *skb,
34 				    struct request_sock *req);
35 	void		(*send_reset)(const struct sock *sk,
36 				      struct sk_buff *skb,
37 				      enum sk_rst_reason reason);
38 	void		(*destructor)(struct request_sock *req);
39 };
40 
41 struct saved_syn {
42 	struct rcu_head rcu;
43 	u32 mac_hdrlen;
44 	u32 network_hdrlen;
45 	u32 tcp_hdrlen;
46 	u8 data[];
47 };
48 
49 /* struct request_sock - mini sock to represent a connection request
50  */
51 struct request_sock {
52 	struct sock_common		__req_common;
53 #define rsk_refcnt			__req_common.skc_refcnt
54 #define rsk_hash			__req_common.skc_hash
55 #define rsk_listener			__req_common.skc_listener
56 #define rsk_window_clamp		__req_common.skc_window_clamp
57 #define rsk_rcv_wnd			__req_common.skc_rcv_wnd
58 
59 	struct request_sock		*dl_next;
60 	u16				mss;
61 	u8				num_retrans; /* number of retransmits */
62 	u8				syncookie:1; /* True if
63 						      * 1) tcpopts needs to be encoded in
64 						      *    TS of SYN+ACK
65 						      * 2) ACK is validated by BPF kfunc.
66 						      */
67 	u8				num_timeout:7; /* number of timeouts */
68 	u32				ts_recent;
69 	struct timer_list		rsk_timer;
70 	const struct request_sock_ops	*rsk_ops;
71 	struct sock			*sk;
72 	struct saved_syn __rcu		*saved_syn;
73 	u32				secid;
74 	u32				peer_secid;
75 	u32				timeout;
76 };
77 
78 static inline struct request_sock *inet_reqsk(const struct sock *sk)
79 {
80 	return (struct request_sock *)sk;
81 }
82 
83 static inline struct sock *req_to_sk(struct request_sock *req)
84 {
85 	return (struct sock *)req;
86 }
87 
88 /**
89  * skb_steal_sock - steal a socket from an sk_buff
90  * @skb: sk_buff to steal the socket from
91  * @refcounted: is set to true if the socket is reference-counted
92  * @prefetched: is set to true if the socket was assigned from bpf
93  */
94 static inline struct sock *skb_steal_sock(struct sk_buff *skb,
95 					  bool *refcounted, bool *prefetched)
96 {
97 	struct sock *sk = skb->sk;
98 
99 	if (!sk) {
100 		*prefetched = false;
101 		*refcounted = false;
102 		return NULL;
103 	}
104 
105 	*prefetched = skb_sk_is_prefetched(skb);
106 	if (*prefetched) {
107 #if IS_ENABLED(CONFIG_SYN_COOKIES)
108 		if (sk->sk_state == TCP_NEW_SYN_RECV && inet_reqsk(sk)->syncookie) {
109 			struct request_sock *req = inet_reqsk(sk);
110 
111 			*refcounted = false;
112 			sk = req->rsk_listener;
113 			req->rsk_listener = NULL;
114 			return sk;
115 		}
116 #endif
117 		*refcounted = sk_is_refcounted(sk);
118 	} else {
119 		*refcounted = true;
120 	}
121 
122 	skb->destructor = NULL;
123 	skb->sk = NULL;
124 	return sk;
125 }
126 
127 void __reqsk_free(struct request_sock *req);
128 
129 static inline void reqsk_free(struct request_sock *req)
130 {
131 	DEBUG_NET_WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0);
132 	__reqsk_free(req);
133 }
134 
135 static inline void reqsk_put(struct request_sock *req)
136 {
137 	if (refcount_dec_and_test(&req->rsk_refcnt))
138 		__reqsk_free(req);
139 }
140 
141 /*
142  * For a TCP Fast Open listener -
143  *	lock - protects the access to all the reqsk, which is co-owned by
144  *		the listener and the child socket.
145  *	qlen - pending TFO requests (still in TCP_SYN_RECV).
146  *	max_qlen - max TFO reqs allowed before TFO is disabled.
147  *
148  *	XXX (TFO) - ideally these fields can be made as part of "listen_sock"
149  *	structure above. But there is some implementation difficulty due to
150  *	listen_sock being part of request_sock_queue hence will be freed when
151  *	a listener is stopped. But TFO related fields may continue to be
152  *	accessed even after a listener is closed, until its sk_refcnt drops
153  *	to 0 implying no more outstanding TFO reqs. One solution is to keep
154  *	listen_opt around until	sk_refcnt drops to 0. But there is some other
155  *	complexity that needs to be resolved. E.g., a listener can be disabled
156  *	temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
157  */
158 struct fastopen_queue {
159 	struct request_sock	*rskq_rst_head; /* Keep track of past TFO */
160 	struct request_sock	*rskq_rst_tail; /* requests that caused RST.
161 						 * This is part of the defense
162 						 * against spoofing attack.
163 						 */
164 	spinlock_t	lock;
165 	int		qlen;		/* # of pending (TCP_SYN_RECV) reqs */
166 	int		max_qlen;	/* != 0 iff TFO is currently enabled */
167 
168 	struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */
169 };
170 
171 /** struct request_sock_queue - queue of request_socks
172  *
173  * @rskq_accept_head - FIFO head of established children
174  * @rskq_accept_tail - FIFO tail of established children
175  * @rskq_defer_accept - User waits for some data after accept()
176  *
177  */
178 struct request_sock_queue {
179 	spinlock_t		rskq_lock;
180 	u8			rskq_defer_accept;
181 	u8			synflood_warned;
182 
183 	atomic_t		qlen;
184 	atomic_t		young;
185 
186 	struct request_sock	*rskq_accept_head;
187 	struct request_sock	*rskq_accept_tail;
188 	struct fastopen_queue	fastopenq;  /* Check max_qlen != 0 to determine
189 					     * if TFO is enabled.
190 					     */
191 };
192 
193 void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
194 			   bool reset);
195 
196 static inline bool reqsk_queue_empty(const struct request_sock_queue *queue)
197 {
198 	return READ_ONCE(queue->rskq_accept_head) == NULL;
199 }
200 
201 static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue,
202 						      struct sock *parent)
203 {
204 	struct request_sock *req;
205 
206 	spin_lock_bh(&queue->rskq_lock);
207 	req = queue->rskq_accept_head;
208 	if (req) {
209 		sk_acceptq_removed(parent);
210 		WRITE_ONCE(queue->rskq_accept_head, req->dl_next);
211 		if (queue->rskq_accept_head == NULL)
212 			queue->rskq_accept_tail = NULL;
213 	}
214 	spin_unlock_bh(&queue->rskq_lock);
215 	return req;
216 }
217 
218 static inline void reqsk_queue_removed(struct request_sock_queue *queue,
219 				       const struct request_sock *req)
220 {
221 	if (req->num_timeout == 0)
222 		atomic_dec(&queue->young);
223 	atomic_dec(&queue->qlen);
224 }
225 
226 static inline void reqsk_queue_added(struct request_sock_queue *queue)
227 {
228 	atomic_inc(&queue->young);
229 	atomic_inc(&queue->qlen);
230 }
231 
232 static inline int reqsk_queue_len(const struct request_sock_queue *queue)
233 {
234 	return atomic_read(&queue->qlen);
235 }
236 
237 static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
238 {
239 	return atomic_read(&queue->young);
240 }
241 
242 /* RFC 7323 2.3 Using the Window Scale Option
243  *  The window field (SEG.WND) of every outgoing segment, with the
244  *  exception of <SYN> segments, MUST be right-shifted by
245  *  Rcv.Wind.Shift bits.
246  *
247  * This means the SEG.WND carried in SYNACK can not exceed 65535.
248  * We use this property to harden TCP stack while in NEW_SYN_RECV state.
249  */
250 static inline u32 tcp_synack_window(const struct request_sock *req)
251 {
252 	return min(req->rsk_rcv_wnd, 65535U);
253 }
254 #endif /* _REQUEST_SOCK_H */
255