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 void (*syn_ack_timeout)(const struct request_sock *req); 40 }; 41 42 struct saved_syn { 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 *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 static inline void __reqsk_free(struct request_sock *req) 128 { 129 req->rsk_ops->destructor(req); 130 if (req->rsk_listener) 131 sock_put(req->rsk_listener); 132 kfree(req->saved_syn); 133 kmem_cache_free(req->rsk_ops->slab, req); 134 } 135 136 static inline void reqsk_free(struct request_sock *req) 137 { 138 DEBUG_NET_WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0); 139 __reqsk_free(req); 140 } 141 142 static inline void reqsk_put(struct request_sock *req) 143 { 144 if (refcount_dec_and_test(&req->rsk_refcnt)) 145 __reqsk_free(req); 146 } 147 148 /* 149 * For a TCP Fast Open listener - 150 * lock - protects the access to all the reqsk, which is co-owned by 151 * the listener and the child socket. 152 * qlen - pending TFO requests (still in TCP_SYN_RECV). 153 * max_qlen - max TFO reqs allowed before TFO is disabled. 154 * 155 * XXX (TFO) - ideally these fields can be made as part of "listen_sock" 156 * structure above. But there is some implementation difficulty due to 157 * listen_sock being part of request_sock_queue hence will be freed when 158 * a listener is stopped. But TFO related fields may continue to be 159 * accessed even after a listener is closed, until its sk_refcnt drops 160 * to 0 implying no more outstanding TFO reqs. One solution is to keep 161 * listen_opt around until sk_refcnt drops to 0. But there is some other 162 * complexity that needs to be resolved. E.g., a listener can be disabled 163 * temporarily through shutdown()->tcp_disconnect(), and re-enabled later. 164 */ 165 struct fastopen_queue { 166 struct request_sock *rskq_rst_head; /* Keep track of past TFO */ 167 struct request_sock *rskq_rst_tail; /* requests that caused RST. 168 * This is part of the defense 169 * against spoofing attack. 170 */ 171 spinlock_t lock; 172 int qlen; /* # of pending (TCP_SYN_RECV) reqs */ 173 int max_qlen; /* != 0 iff TFO is currently enabled */ 174 175 struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */ 176 }; 177 178 /** struct request_sock_queue - queue of request_socks 179 * 180 * @rskq_accept_head - FIFO head of established children 181 * @rskq_accept_tail - FIFO tail of established children 182 * @rskq_defer_accept - User waits for some data after accept() 183 * 184 */ 185 struct request_sock_queue { 186 spinlock_t rskq_lock; 187 u8 rskq_defer_accept; 188 189 u32 synflood_warned; 190 atomic_t qlen; 191 atomic_t young; 192 193 struct request_sock *rskq_accept_head; 194 struct request_sock *rskq_accept_tail; 195 struct fastopen_queue fastopenq; /* Check max_qlen != 0 to determine 196 * if TFO is enabled. 197 */ 198 }; 199 200 void reqsk_queue_alloc(struct request_sock_queue *queue); 201 202 void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req, 203 bool reset); 204 205 static inline bool reqsk_queue_empty(const struct request_sock_queue *queue) 206 { 207 return READ_ONCE(queue->rskq_accept_head) == NULL; 208 } 209 210 static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue, 211 struct sock *parent) 212 { 213 struct request_sock *req; 214 215 spin_lock_bh(&queue->rskq_lock); 216 req = queue->rskq_accept_head; 217 if (req) { 218 sk_acceptq_removed(parent); 219 WRITE_ONCE(queue->rskq_accept_head, req->dl_next); 220 if (queue->rskq_accept_head == NULL) 221 queue->rskq_accept_tail = NULL; 222 } 223 spin_unlock_bh(&queue->rskq_lock); 224 return req; 225 } 226 227 static inline void reqsk_queue_removed(struct request_sock_queue *queue, 228 const struct request_sock *req) 229 { 230 if (req->num_timeout == 0) 231 atomic_dec(&queue->young); 232 atomic_dec(&queue->qlen); 233 } 234 235 static inline void reqsk_queue_added(struct request_sock_queue *queue) 236 { 237 atomic_inc(&queue->young); 238 atomic_inc(&queue->qlen); 239 } 240 241 static inline int reqsk_queue_len(const struct request_sock_queue *queue) 242 { 243 return atomic_read(&queue->qlen); 244 } 245 246 static inline int reqsk_queue_len_young(const struct request_sock_queue *queue) 247 { 248 return atomic_read(&queue->young); 249 } 250 251 /* RFC 7323 2.3 Using the Window Scale Option 252 * The window field (SEG.WND) of every outgoing segment, with the 253 * exception of <SYN> segments, MUST be right-shifted by 254 * Rcv.Wind.Shift bits. 255 * 256 * This means the SEG.WND carried in SYNACK can not exceed 65535. 257 * We use this property to harden TCP stack while in NEW_SYN_RECV state. 258 */ 259 static inline u32 tcp_synack_window(const struct request_sock *req) 260 { 261 return min(req->rsk_rcv_wnd, 65535U); 262 } 263 #endif /* _REQUEST_SOCK_H */ 264