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 4470a217f1SMartin KaFai Lau struct saved_syn { 45267cf9faSMartin KaFai Lau u32 mac_hdrlen; 4670a217f1SMartin KaFai Lau u32 network_hdrlen; 4770a217f1SMartin KaFai Lau u32 tcp_hdrlen; 4870a217f1SMartin KaFai Lau u8 data[]; 4970a217f1SMartin KaFai Lau }; 5070a217f1SMartin KaFai Lau 5160236fddSArnaldo Carvalho de Melo /* struct request_sock - mini sock to represent a connection request 522e6599cbSArnaldo Carvalho de Melo */ 5360236fddSArnaldo Carvalho de Melo struct request_sock { 54634fb979SEric Dumazet struct sock_common __req_common; 551e2e0117SEric Dumazet #define rsk_refcnt __req_common.skc_refcnt 5652452c54SEric Dumazet #define rsk_hash __req_common.skc_hash 578e5eb54dSEric Dumazet #define rsk_listener __req_common.skc_listener 58ed53d0abSEric Dumazet #define rsk_window_clamp __req_common.skc_window_clamp 59ed53d0abSEric Dumazet #define rsk_rcv_wnd __req_common.skc_rcv_wnd 601e2e0117SEric Dumazet 613fb62c5dSEric Dumazet struct request_sock *dl_next; 622e6599cbSArnaldo Carvalho de Melo u16 mss; 63e6c022a4SEric Dumazet u8 num_retrans; /* number of retransmits */ 64f8ace8d9SFlorian Westphal u8 syncookie:1; /* syncookie: encode tcpopts in timestamp */ 65e6c022a4SEric Dumazet u8 num_timeout:7; /* number of timeouts */ 662e6599cbSArnaldo Carvalho de Melo u32 ts_recent; 67fa76ce73SEric Dumazet struct timer_list rsk_timer; 6872a3effaSEric Dumazet const struct request_sock_ops *rsk_ops; 692e6599cbSArnaldo Carvalho de Melo struct sock *sk; 7070a217f1SMartin KaFai Lau struct saved_syn *saved_syn; 714237c75cSVenkat Yekkirala u32 secid; 726b877699SVenkat Yekkirala u32 peer_secid; 735903123fSAkhmat Karakotov u32 timeout; 742e6599cbSArnaldo Carvalho de Melo }; 752e6599cbSArnaldo Carvalho de Melo 76b1f0a0e9SFlorian Westphal static inline struct request_sock *inet_reqsk(const struct sock *sk) 77b267cdd1SEric Dumazet { 78b267cdd1SEric Dumazet return (struct request_sock *)sk; 79b267cdd1SEric Dumazet } 80b267cdd1SEric Dumazet 81b267cdd1SEric Dumazet static inline struct sock *req_to_sk(struct request_sock *req) 82b267cdd1SEric Dumazet { 83b267cdd1SEric Dumazet return (struct sock *)req; 84b267cdd1SEric Dumazet } 85b267cdd1SEric Dumazet 8695e752b5SKuniyuki Iwashima /** 8795e752b5SKuniyuki Iwashima * skb_steal_sock - steal a socket from an sk_buff 8895e752b5SKuniyuki Iwashima * @skb: sk_buff to steal the socket from 8995e752b5SKuniyuki Iwashima * @refcounted: is set to true if the socket is reference-counted 9095e752b5SKuniyuki Iwashima * @prefetched: is set to true if the socket was assigned from bpf 9195e752b5SKuniyuki Iwashima */ 9295e752b5SKuniyuki Iwashima static inline struct sock *skb_steal_sock(struct sk_buff *skb, 9395e752b5SKuniyuki Iwashima bool *refcounted, bool *prefetched) 9495e752b5SKuniyuki Iwashima { 9595e752b5SKuniyuki Iwashima struct sock *sk = skb->sk; 9695e752b5SKuniyuki Iwashima 9795e752b5SKuniyuki Iwashima if (!sk) { 9895e752b5SKuniyuki Iwashima *prefetched = false; 9995e752b5SKuniyuki Iwashima *refcounted = false; 10095e752b5SKuniyuki Iwashima return NULL; 10195e752b5SKuniyuki Iwashima } 10295e752b5SKuniyuki Iwashima 10395e752b5SKuniyuki Iwashima *prefetched = skb_sk_is_prefetched(skb); 104*8b5ac68fSKuniyuki Iwashima if (*prefetched) { 105*8b5ac68fSKuniyuki Iwashima #if IS_ENABLED(CONFIG_SYN_COOKIES) 106*8b5ac68fSKuniyuki Iwashima if (sk->sk_state == TCP_NEW_SYN_RECV && inet_reqsk(sk)->syncookie) { 107*8b5ac68fSKuniyuki Iwashima struct request_sock *req = inet_reqsk(sk); 108*8b5ac68fSKuniyuki Iwashima 109*8b5ac68fSKuniyuki Iwashima *refcounted = false; 110*8b5ac68fSKuniyuki Iwashima sk = req->rsk_listener; 111*8b5ac68fSKuniyuki Iwashima req->rsk_listener = NULL; 112*8b5ac68fSKuniyuki Iwashima return sk; 113*8b5ac68fSKuniyuki Iwashima } 114*8b5ac68fSKuniyuki Iwashima #endif 11595e752b5SKuniyuki Iwashima *refcounted = sk_is_refcounted(sk); 116*8b5ac68fSKuniyuki Iwashima } else { 11795e752b5SKuniyuki Iwashima *refcounted = true; 118*8b5ac68fSKuniyuki Iwashima } 11995e752b5SKuniyuki Iwashima 12095e752b5SKuniyuki Iwashima skb->destructor = NULL; 12195e752b5SKuniyuki Iwashima skb->sk = NULL; 12295e752b5SKuniyuki Iwashima return sk; 12395e752b5SKuniyuki Iwashima } 12495e752b5SKuniyuki Iwashima 1254e9a578eSEric Dumazet static inline struct request_sock * 126a1a5344dSEric Dumazet reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener, 127a1a5344dSEric Dumazet bool attach_listener) 1282e6599cbSArnaldo Carvalho de Melo { 129e96f78abSEric Dumazet struct request_sock *req; 130e96f78abSEric Dumazet 131e96f78abSEric Dumazet req = kmem_cache_alloc(ops->slab, GFP_ATOMIC | __GFP_NOWARN); 1323a5d1c0eSEric Dumazet if (!req) 1333a5d1c0eSEric Dumazet return NULL; 134a1a5344dSEric Dumazet req->rsk_listener = NULL; 1353a5d1c0eSEric Dumazet if (attach_listener) { 13641c6d650SReshetova, Elena if (unlikely(!refcount_inc_not_zero(&sk_listener->sk_refcnt))) { 1373a5d1c0eSEric Dumazet kmem_cache_free(ops->slab, req); 1383a5d1c0eSEric Dumazet return NULL; 139a1a5344dSEric Dumazet } 1403a5d1c0eSEric Dumazet req->rsk_listener = sk_listener; 1413a5d1c0eSEric Dumazet } 1423a5d1c0eSEric Dumazet req->rsk_ops = ops; 143b267cdd1SEric Dumazet req_to_sk(req)->sk_prot = sk_listener->sk_prot; 144b267cdd1SEric Dumazet sk_node_init(&req_to_sk(req)->sk_node); 145004a5d01SEric Dumazet sk_tx_queue_clear(req_to_sk(req)); 146cd8ae852SEric Dumazet req->saved_syn = NULL; 1475903123fSAkhmat Karakotov req->timeout = 0; 14885f9aa75SEric Dumazet req->num_timeout = 0; 14985f9aa75SEric Dumazet req->num_retrans = 0; 15085f9aa75SEric Dumazet req->sk = NULL; 15141c6d650SReshetova, Elena refcount_set(&req->rsk_refcnt, 0); 1523a5d1c0eSEric Dumazet 1532e6599cbSArnaldo Carvalho de Melo return req; 1542e6599cbSArnaldo Carvalho de Melo } 1552e6599cbSArnaldo Carvalho de Melo 1569403cf23SGuillaume Nault static inline void __reqsk_free(struct request_sock *req) 1572e6599cbSArnaldo Carvalho de Melo { 15860236fddSArnaldo Carvalho de Melo req->rsk_ops->destructor(req); 1594e9a578eSEric Dumazet if (req->rsk_listener) 1604e9a578eSEric Dumazet sock_put(req->rsk_listener); 161cd8ae852SEric Dumazet kfree(req->saved_syn); 16213854e5aSEric Dumazet kmem_cache_free(req->rsk_ops->slab, req); 1632e6599cbSArnaldo Carvalho de Melo } 1642e6599cbSArnaldo Carvalho de Melo 1659403cf23SGuillaume Nault static inline void reqsk_free(struct request_sock *req) 1669403cf23SGuillaume Nault { 1679403cf23SGuillaume Nault WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0); 1689403cf23SGuillaume Nault __reqsk_free(req); 1699403cf23SGuillaume Nault } 1709403cf23SGuillaume Nault 1711e2e0117SEric Dumazet static inline void reqsk_put(struct request_sock *req) 1721e2e0117SEric Dumazet { 17341c6d650SReshetova, Elena if (refcount_dec_and_test(&req->rsk_refcnt)) 1741e2e0117SEric Dumazet reqsk_free(req); 1751e2e0117SEric Dumazet } 1760e87506fSArnaldo Carvalho de Melo 17710467163SJerry Chu /* 17810467163SJerry Chu * For a TCP Fast Open listener - 17910467163SJerry Chu * lock - protects the access to all the reqsk, which is co-owned by 18010467163SJerry Chu * the listener and the child socket. 18110467163SJerry Chu * qlen - pending TFO requests (still in TCP_SYN_RECV). 18210467163SJerry Chu * max_qlen - max TFO reqs allowed before TFO is disabled. 18310467163SJerry Chu * 18410467163SJerry Chu * XXX (TFO) - ideally these fields can be made as part of "listen_sock" 18510467163SJerry Chu * structure above. But there is some implementation difficulty due to 18610467163SJerry Chu * listen_sock being part of request_sock_queue hence will be freed when 18710467163SJerry Chu * a listener is stopped. But TFO related fields may continue to be 18810467163SJerry Chu * accessed even after a listener is closed, until its sk_refcnt drops 18910467163SJerry Chu * to 0 implying no more outstanding TFO reqs. One solution is to keep 19010467163SJerry Chu * listen_opt around until sk_refcnt drops to 0. But there is some other 19110467163SJerry Chu * complexity that needs to be resolved. E.g., a listener can be disabled 19210467163SJerry Chu * temporarily through shutdown()->tcp_disconnect(), and re-enabled later. 19310467163SJerry Chu */ 19410467163SJerry Chu struct fastopen_queue { 19510467163SJerry Chu struct request_sock *rskq_rst_head; /* Keep track of past TFO */ 19610467163SJerry Chu struct request_sock *rskq_rst_tail; /* requests that caused RST. 19710467163SJerry Chu * This is part of the defense 19810467163SJerry Chu * against spoofing attack. 19910467163SJerry Chu */ 20010467163SJerry Chu spinlock_t lock; 20110467163SJerry Chu int qlen; /* # of pending (TCP_SYN_RECV) reqs */ 20210467163SJerry Chu int max_qlen; /* != 0 iff TFO is currently enabled */ 2031fba70e5SYuchung Cheng 2041fba70e5SYuchung Cheng struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */ 20510467163SJerry Chu }; 20610467163SJerry Chu 2070e87506fSArnaldo Carvalho de Melo /** struct request_sock_queue - queue of request_socks 2080e87506fSArnaldo Carvalho de Melo * 2090e87506fSArnaldo Carvalho de Melo * @rskq_accept_head - FIFO head of established children 2100e87506fSArnaldo Carvalho de Melo * @rskq_accept_tail - FIFO tail of established children 211295f7324SArnaldo Carvalho de Melo * @rskq_defer_accept - User waits for some data after accept() 2120e87506fSArnaldo Carvalho de Melo * 2130e87506fSArnaldo Carvalho de Melo */ 2140e87506fSArnaldo Carvalho de Melo struct request_sock_queue { 215fff1f300SEric Dumazet spinlock_t rskq_lock; 216fff1f300SEric Dumazet u8 rskq_defer_accept; 217ef547f2aSEric Dumazet 2188d2675f1SEric Dumazet u32 synflood_warned; 219aac065c5SEric Dumazet atomic_t qlen; 220aac065c5SEric Dumazet atomic_t young; 221aac065c5SEric Dumazet 2220e87506fSArnaldo Carvalho de Melo struct request_sock *rskq_accept_head; 2230e87506fSArnaldo Carvalho de Melo struct request_sock *rskq_accept_tail; 2240536fcc0SEric Dumazet struct fastopen_queue fastopenq; /* Check max_qlen != 0 to determine 2250536fcc0SEric Dumazet * if TFO is enabled. 22610467163SJerry Chu */ 2270e87506fSArnaldo Carvalho de Melo }; 2280e87506fSArnaldo Carvalho de Melo 229ef547f2aSEric Dumazet void reqsk_queue_alloc(struct request_sock_queue *queue); 2300e87506fSArnaldo Carvalho de Melo 231c0f4502aSJoe Perches void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req, 232c0f4502aSJoe Perches bool reset); 23383e3609eSArnaldo Carvalho de Melo 234fff1f300SEric Dumazet static inline bool reqsk_queue_empty(const struct request_sock_queue *queue) 2350e87506fSArnaldo Carvalho de Melo { 23660b173caSEric Dumazet return READ_ONCE(queue->rskq_accept_head) == NULL; 2370e87506fSArnaldo Carvalho de Melo } 2380e87506fSArnaldo Carvalho de Melo 239fff1f300SEric Dumazet static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue, 240fff1f300SEric Dumazet struct sock *parent) 2410e87506fSArnaldo Carvalho de Melo { 242fff1f300SEric Dumazet struct request_sock *req; 2430e87506fSArnaldo Carvalho de Melo 244fff1f300SEric Dumazet spin_lock_bh(&queue->rskq_lock); 245fff1f300SEric Dumazet req = queue->rskq_accept_head; 246fff1f300SEric Dumazet if (req) { 247fff1f300SEric Dumazet sk_acceptq_removed(parent); 24860b173caSEric Dumazet WRITE_ONCE(queue->rskq_accept_head, req->dl_next); 2490e87506fSArnaldo Carvalho de Melo if (queue->rskq_accept_head == NULL) 2500e87506fSArnaldo Carvalho de Melo queue->rskq_accept_tail = NULL; 251fff1f300SEric Dumazet } 252fff1f300SEric Dumazet spin_unlock_bh(&queue->rskq_lock); 2530e87506fSArnaldo Carvalho de Melo return req; 2540e87506fSArnaldo Carvalho de Melo } 2550e87506fSArnaldo Carvalho de Melo 256fa76ce73SEric Dumazet static inline void reqsk_queue_removed(struct request_sock_queue *queue, 257fa76ce73SEric Dumazet const struct request_sock *req) 2580e87506fSArnaldo Carvalho de Melo { 259e6c022a4SEric Dumazet if (req->num_timeout == 0) 260aac065c5SEric Dumazet atomic_dec(&queue->young); 261aac065c5SEric Dumazet atomic_dec(&queue->qlen); 2620e87506fSArnaldo Carvalho de Melo } 2630e87506fSArnaldo Carvalho de Melo 264fa76ce73SEric Dumazet static inline void reqsk_queue_added(struct request_sock_queue *queue) 2650e87506fSArnaldo Carvalho de Melo { 266aac065c5SEric Dumazet atomic_inc(&queue->young); 267aac065c5SEric Dumazet atomic_inc(&queue->qlen); 2680e87506fSArnaldo Carvalho de Melo } 2690e87506fSArnaldo Carvalho de Melo 270463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len(const struct request_sock_queue *queue) 2710e87506fSArnaldo Carvalho de Melo { 272aac065c5SEric Dumazet return atomic_read(&queue->qlen); 2730e87506fSArnaldo Carvalho de Melo } 2740e87506fSArnaldo Carvalho de Melo 275463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len_young(const struct request_sock_queue *queue) 2760e87506fSArnaldo Carvalho de Melo { 277aac065c5SEric Dumazet return atomic_read(&queue->young); 2780e87506fSArnaldo Carvalho de Melo } 2790e87506fSArnaldo Carvalho de Melo 2802e6599cbSArnaldo Carvalho de Melo #endif /* _REQUEST_SOCK_H */ 281