1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * NET Generic infrastructure for Network protocols.
5 * Definitions for request_sock
7 * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
9 * From code originally in include/net/tcp.h
11 #ifndef _REQUEST_SOCK_H
12 #define _REQUEST_SOCK_H
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>
21 #include <net/rstreason.h>
28 struct request_sock_ops {
30 unsigned int obj_size;
31 struct kmem_cache *slab;
33 int (*rtx_syn_ack)(const struct sock *sk,
34 struct request_sock *req);
35 void (*send_ack)(const struct sock *sk, struct sk_buff *skb,
36 struct request_sock *req);
37 void (*send_reset)(const struct sock *sk,
39 enum sk_rst_reason reason);
40 void (*destructor)(struct request_sock *req);
41 void (*syn_ack_timeout)(const struct request_sock *req);
44 int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req);
53 /* struct request_sock - mini sock to represent a connection request
56 struct sock_common __req_common;
57 #define rsk_refcnt __req_common.skc_refcnt
58 #define rsk_hash __req_common.skc_hash
59 #define rsk_listener __req_common.skc_listener
60 #define rsk_window_clamp __req_common.skc_window_clamp
61 #define rsk_rcv_wnd __req_common.skc_rcv_wnd
63 struct request_sock *dl_next;
65 u8 num_retrans; /* number of retransmits */
66 u8 syncookie:1; /* True if
67 * 1) tcpopts needs to be encoded in
69 * 2) ACK is validated by BPF kfunc.
71 u8 num_timeout:7; /* number of timeouts */
73 struct timer_list rsk_timer;
74 const struct request_sock_ops *rsk_ops;
76 struct saved_syn *saved_syn;
82 static inline struct request_sock *inet_reqsk(const struct sock *sk)
84 return (struct request_sock *)sk;
87 static inline struct sock *req_to_sk(struct request_sock *req)
89 return (struct sock *)req;
93 * skb_steal_sock - steal a socket from an sk_buff
94 * @skb: sk_buff to steal the socket from
95 * @refcounted: is set to true if the socket is reference-counted
96 * @prefetched: is set to true if the socket was assigned from bpf
98 static inline struct sock *skb_steal_sock(struct sk_buff *skb,
99 bool *refcounted, bool *prefetched)
101 struct sock *sk = skb->sk;
109 *prefetched = skb_sk_is_prefetched(skb);
111 #if IS_ENABLED(CONFIG_SYN_COOKIES)
112 if (sk->sk_state == TCP_NEW_SYN_RECV && inet_reqsk(sk)->syncookie) {
113 struct request_sock *req = inet_reqsk(sk);
116 sk = req->rsk_listener;
117 req->rsk_listener = NULL;
121 *refcounted = sk_is_refcounted(sk);
126 skb->destructor = NULL;
131 static inline struct request_sock *
132 reqsk_alloc_noprof(const struct request_sock_ops *ops, struct sock *sk_listener,
133 bool attach_listener)
135 struct request_sock *req;
137 req = kmem_cache_alloc_noprof(ops->slab, GFP_ATOMIC | __GFP_NOWARN);
140 req->rsk_listener = NULL;
141 if (attach_listener) {
142 if (unlikely(!refcount_inc_not_zero(&sk_listener->sk_refcnt))) {
143 kmem_cache_free(ops->slab, req);
146 req->rsk_listener = sk_listener;
149 req_to_sk(req)->sk_prot = sk_listener->sk_prot;
150 sk_node_init(&req_to_sk(req)->sk_node);
151 sk_tx_queue_clear(req_to_sk(req));
152 req->saved_syn = NULL;
155 req->num_timeout = 0;
156 req->num_retrans = 0;
158 refcount_set(&req->rsk_refcnt, 0);
162 #define reqsk_alloc(...) alloc_hooks(reqsk_alloc_noprof(__VA_ARGS__))
164 static inline void __reqsk_free(struct request_sock *req)
166 req->rsk_ops->destructor(req);
167 if (req->rsk_listener)
168 sock_put(req->rsk_listener);
169 kfree(req->saved_syn);
170 kmem_cache_free(req->rsk_ops->slab, req);
173 static inline void reqsk_free(struct request_sock *req)
175 WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0);
179 static inline void reqsk_put(struct request_sock *req)
181 if (refcount_dec_and_test(&req->rsk_refcnt))
186 * For a TCP Fast Open listener -
187 * lock - protects the access to all the reqsk, which is co-owned by
188 * the listener and the child socket.
189 * qlen - pending TFO requests (still in TCP_SYN_RECV).
190 * max_qlen - max TFO reqs allowed before TFO is disabled.
192 * XXX (TFO) - ideally these fields can be made as part of "listen_sock"
193 * structure above. But there is some implementation difficulty due to
194 * listen_sock being part of request_sock_queue hence will be freed when
195 * a listener is stopped. But TFO related fields may continue to be
196 * accessed even after a listener is closed, until its sk_refcnt drops
197 * to 0 implying no more outstanding TFO reqs. One solution is to keep
198 * listen_opt around until sk_refcnt drops to 0. But there is some other
199 * complexity that needs to be resolved. E.g., a listener can be disabled
200 * temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
202 struct fastopen_queue {
203 struct request_sock *rskq_rst_head; /* Keep track of past TFO */
204 struct request_sock *rskq_rst_tail; /* requests that caused RST.
205 * This is part of the defense
206 * against spoofing attack.
209 int qlen; /* # of pending (TCP_SYN_RECV) reqs */
210 int max_qlen; /* != 0 iff TFO is currently enabled */
212 struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */
215 /** struct request_sock_queue - queue of request_socks
217 * @rskq_accept_head - FIFO head of established children
218 * @rskq_accept_tail - FIFO tail of established children
219 * @rskq_defer_accept - User waits for some data after accept()
222 struct request_sock_queue {
223 spinlock_t rskq_lock;
224 u8 rskq_defer_accept;
230 struct request_sock *rskq_accept_head;
231 struct request_sock *rskq_accept_tail;
232 struct fastopen_queue fastopenq; /* Check max_qlen != 0 to determine
237 void reqsk_queue_alloc(struct request_sock_queue *queue);
239 void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
242 static inline bool reqsk_queue_empty(const struct request_sock_queue *queue)
244 return READ_ONCE(queue->rskq_accept_head) == NULL;
247 static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue,
250 struct request_sock *req;
252 spin_lock_bh(&queue->rskq_lock);
253 req = queue->rskq_accept_head;
255 sk_acceptq_removed(parent);
256 WRITE_ONCE(queue->rskq_accept_head, req->dl_next);
257 if (queue->rskq_accept_head == NULL)
258 queue->rskq_accept_tail = NULL;
260 spin_unlock_bh(&queue->rskq_lock);
264 static inline void reqsk_queue_removed(struct request_sock_queue *queue,
265 const struct request_sock *req)
267 if (req->num_timeout == 0)
268 atomic_dec(&queue->young);
269 atomic_dec(&queue->qlen);
272 static inline void reqsk_queue_added(struct request_sock_queue *queue)
274 atomic_inc(&queue->young);
275 atomic_inc(&queue->qlen);
278 static inline int reqsk_queue_len(const struct request_sock_queue *queue)
280 return atomic_read(&queue->qlen);
283 static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
285 return atomic_read(&queue->young);
288 #endif /* _REQUEST_SOCK_H */