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 86*95e752b5SKuniyuki Iwashima /** 87*95e752b5SKuniyuki Iwashima * skb_steal_sock - steal a socket from an sk_buff 88*95e752b5SKuniyuki Iwashima * @skb: sk_buff to steal the socket from 89*95e752b5SKuniyuki Iwashima * @refcounted: is set to true if the socket is reference-counted 90*95e752b5SKuniyuki Iwashima * @prefetched: is set to true if the socket was assigned from bpf 91*95e752b5SKuniyuki Iwashima */ 92*95e752b5SKuniyuki Iwashima static inline struct sock *skb_steal_sock(struct sk_buff *skb, 93*95e752b5SKuniyuki Iwashima bool *refcounted, bool *prefetched) 94*95e752b5SKuniyuki Iwashima { 95*95e752b5SKuniyuki Iwashima struct sock *sk = skb->sk; 96*95e752b5SKuniyuki Iwashima 97*95e752b5SKuniyuki Iwashima if (!sk) { 98*95e752b5SKuniyuki Iwashima *prefetched = false; 99*95e752b5SKuniyuki Iwashima *refcounted = false; 100*95e752b5SKuniyuki Iwashima return NULL; 101*95e752b5SKuniyuki Iwashima } 102*95e752b5SKuniyuki Iwashima 103*95e752b5SKuniyuki Iwashima *prefetched = skb_sk_is_prefetched(skb); 104*95e752b5SKuniyuki Iwashima if (*prefetched) 105*95e752b5SKuniyuki Iwashima *refcounted = sk_is_refcounted(sk); 106*95e752b5SKuniyuki Iwashima else 107*95e752b5SKuniyuki Iwashima *refcounted = true; 108*95e752b5SKuniyuki Iwashima 109*95e752b5SKuniyuki Iwashima skb->destructor = NULL; 110*95e752b5SKuniyuki Iwashima skb->sk = NULL; 111*95e752b5SKuniyuki Iwashima return sk; 112*95e752b5SKuniyuki Iwashima } 113*95e752b5SKuniyuki Iwashima 1144e9a578eSEric Dumazet static inline struct request_sock * 115a1a5344dSEric Dumazet reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener, 116a1a5344dSEric Dumazet bool attach_listener) 1172e6599cbSArnaldo Carvalho de Melo { 118e96f78abSEric Dumazet struct request_sock *req; 119e96f78abSEric Dumazet 120e96f78abSEric Dumazet req = kmem_cache_alloc(ops->slab, GFP_ATOMIC | __GFP_NOWARN); 1213a5d1c0eSEric Dumazet if (!req) 1223a5d1c0eSEric Dumazet return NULL; 123a1a5344dSEric Dumazet req->rsk_listener = NULL; 1243a5d1c0eSEric Dumazet if (attach_listener) { 12541c6d650SReshetova, Elena if (unlikely(!refcount_inc_not_zero(&sk_listener->sk_refcnt))) { 1263a5d1c0eSEric Dumazet kmem_cache_free(ops->slab, req); 1273a5d1c0eSEric Dumazet return NULL; 128a1a5344dSEric Dumazet } 1293a5d1c0eSEric Dumazet req->rsk_listener = sk_listener; 1303a5d1c0eSEric Dumazet } 1313a5d1c0eSEric Dumazet req->rsk_ops = ops; 132b267cdd1SEric Dumazet req_to_sk(req)->sk_prot = sk_listener->sk_prot; 133b267cdd1SEric Dumazet sk_node_init(&req_to_sk(req)->sk_node); 134004a5d01SEric Dumazet sk_tx_queue_clear(req_to_sk(req)); 135cd8ae852SEric Dumazet req->saved_syn = NULL; 1365903123fSAkhmat Karakotov req->timeout = 0; 13785f9aa75SEric Dumazet req->num_timeout = 0; 13885f9aa75SEric Dumazet req->num_retrans = 0; 13985f9aa75SEric Dumazet req->sk = NULL; 14041c6d650SReshetova, Elena refcount_set(&req->rsk_refcnt, 0); 1413a5d1c0eSEric Dumazet 1422e6599cbSArnaldo Carvalho de Melo return req; 1432e6599cbSArnaldo Carvalho de Melo } 1442e6599cbSArnaldo Carvalho de Melo 1459403cf23SGuillaume Nault static inline void __reqsk_free(struct request_sock *req) 1462e6599cbSArnaldo Carvalho de Melo { 14760236fddSArnaldo Carvalho de Melo req->rsk_ops->destructor(req); 1484e9a578eSEric Dumazet if (req->rsk_listener) 1494e9a578eSEric Dumazet sock_put(req->rsk_listener); 150cd8ae852SEric Dumazet kfree(req->saved_syn); 15113854e5aSEric Dumazet kmem_cache_free(req->rsk_ops->slab, req); 1522e6599cbSArnaldo Carvalho de Melo } 1532e6599cbSArnaldo Carvalho de Melo 1549403cf23SGuillaume Nault static inline void reqsk_free(struct request_sock *req) 1559403cf23SGuillaume Nault { 1569403cf23SGuillaume Nault WARN_ON_ONCE(refcount_read(&req->rsk_refcnt) != 0); 1579403cf23SGuillaume Nault __reqsk_free(req); 1589403cf23SGuillaume Nault } 1599403cf23SGuillaume Nault 1601e2e0117SEric Dumazet static inline void reqsk_put(struct request_sock *req) 1611e2e0117SEric Dumazet { 16241c6d650SReshetova, Elena if (refcount_dec_and_test(&req->rsk_refcnt)) 1631e2e0117SEric Dumazet reqsk_free(req); 1641e2e0117SEric Dumazet } 1650e87506fSArnaldo Carvalho de Melo 16610467163SJerry Chu /* 16710467163SJerry Chu * For a TCP Fast Open listener - 16810467163SJerry Chu * lock - protects the access to all the reqsk, which is co-owned by 16910467163SJerry Chu * the listener and the child socket. 17010467163SJerry Chu * qlen - pending TFO requests (still in TCP_SYN_RECV). 17110467163SJerry Chu * max_qlen - max TFO reqs allowed before TFO is disabled. 17210467163SJerry Chu * 17310467163SJerry Chu * XXX (TFO) - ideally these fields can be made as part of "listen_sock" 17410467163SJerry Chu * structure above. But there is some implementation difficulty due to 17510467163SJerry Chu * listen_sock being part of request_sock_queue hence will be freed when 17610467163SJerry Chu * a listener is stopped. But TFO related fields may continue to be 17710467163SJerry Chu * accessed even after a listener is closed, until its sk_refcnt drops 17810467163SJerry Chu * to 0 implying no more outstanding TFO reqs. One solution is to keep 17910467163SJerry Chu * listen_opt around until sk_refcnt drops to 0. But there is some other 18010467163SJerry Chu * complexity that needs to be resolved. E.g., a listener can be disabled 18110467163SJerry Chu * temporarily through shutdown()->tcp_disconnect(), and re-enabled later. 18210467163SJerry Chu */ 18310467163SJerry Chu struct fastopen_queue { 18410467163SJerry Chu struct request_sock *rskq_rst_head; /* Keep track of past TFO */ 18510467163SJerry Chu struct request_sock *rskq_rst_tail; /* requests that caused RST. 18610467163SJerry Chu * This is part of the defense 18710467163SJerry Chu * against spoofing attack. 18810467163SJerry Chu */ 18910467163SJerry Chu spinlock_t lock; 19010467163SJerry Chu int qlen; /* # of pending (TCP_SYN_RECV) reqs */ 19110467163SJerry Chu int max_qlen; /* != 0 iff TFO is currently enabled */ 1921fba70e5SYuchung Cheng 1931fba70e5SYuchung Cheng struct tcp_fastopen_context __rcu *ctx; /* cipher context for cookie */ 19410467163SJerry Chu }; 19510467163SJerry Chu 1960e87506fSArnaldo Carvalho de Melo /** struct request_sock_queue - queue of request_socks 1970e87506fSArnaldo Carvalho de Melo * 1980e87506fSArnaldo Carvalho de Melo * @rskq_accept_head - FIFO head of established children 1990e87506fSArnaldo Carvalho de Melo * @rskq_accept_tail - FIFO tail of established children 200295f7324SArnaldo Carvalho de Melo * @rskq_defer_accept - User waits for some data after accept() 2010e87506fSArnaldo Carvalho de Melo * 2020e87506fSArnaldo Carvalho de Melo */ 2030e87506fSArnaldo Carvalho de Melo struct request_sock_queue { 204fff1f300SEric Dumazet spinlock_t rskq_lock; 205fff1f300SEric Dumazet u8 rskq_defer_accept; 206ef547f2aSEric Dumazet 2078d2675f1SEric Dumazet u32 synflood_warned; 208aac065c5SEric Dumazet atomic_t qlen; 209aac065c5SEric Dumazet atomic_t young; 210aac065c5SEric Dumazet 2110e87506fSArnaldo Carvalho de Melo struct request_sock *rskq_accept_head; 2120e87506fSArnaldo Carvalho de Melo struct request_sock *rskq_accept_tail; 2130536fcc0SEric Dumazet struct fastopen_queue fastopenq; /* Check max_qlen != 0 to determine 2140536fcc0SEric Dumazet * if TFO is enabled. 21510467163SJerry Chu */ 2160e87506fSArnaldo Carvalho de Melo }; 2170e87506fSArnaldo Carvalho de Melo 218ef547f2aSEric Dumazet void reqsk_queue_alloc(struct request_sock_queue *queue); 2190e87506fSArnaldo Carvalho de Melo 220c0f4502aSJoe Perches void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req, 221c0f4502aSJoe Perches bool reset); 22283e3609eSArnaldo Carvalho de Melo 223fff1f300SEric Dumazet static inline bool reqsk_queue_empty(const struct request_sock_queue *queue) 2240e87506fSArnaldo Carvalho de Melo { 22560b173caSEric Dumazet return READ_ONCE(queue->rskq_accept_head) == NULL; 2260e87506fSArnaldo Carvalho de Melo } 2270e87506fSArnaldo Carvalho de Melo 228fff1f300SEric Dumazet static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue, 229fff1f300SEric Dumazet struct sock *parent) 2300e87506fSArnaldo Carvalho de Melo { 231fff1f300SEric Dumazet struct request_sock *req; 2320e87506fSArnaldo Carvalho de Melo 233fff1f300SEric Dumazet spin_lock_bh(&queue->rskq_lock); 234fff1f300SEric Dumazet req = queue->rskq_accept_head; 235fff1f300SEric Dumazet if (req) { 236fff1f300SEric Dumazet sk_acceptq_removed(parent); 23760b173caSEric Dumazet WRITE_ONCE(queue->rskq_accept_head, req->dl_next); 2380e87506fSArnaldo Carvalho de Melo if (queue->rskq_accept_head == NULL) 2390e87506fSArnaldo Carvalho de Melo queue->rskq_accept_tail = NULL; 240fff1f300SEric Dumazet } 241fff1f300SEric Dumazet spin_unlock_bh(&queue->rskq_lock); 2420e87506fSArnaldo Carvalho de Melo return req; 2430e87506fSArnaldo Carvalho de Melo } 2440e87506fSArnaldo Carvalho de Melo 245fa76ce73SEric Dumazet static inline void reqsk_queue_removed(struct request_sock_queue *queue, 246fa76ce73SEric Dumazet const struct request_sock *req) 2470e87506fSArnaldo Carvalho de Melo { 248e6c022a4SEric Dumazet if (req->num_timeout == 0) 249aac065c5SEric Dumazet atomic_dec(&queue->young); 250aac065c5SEric Dumazet atomic_dec(&queue->qlen); 2510e87506fSArnaldo Carvalho de Melo } 2520e87506fSArnaldo Carvalho de Melo 253fa76ce73SEric Dumazet static inline void reqsk_queue_added(struct request_sock_queue *queue) 2540e87506fSArnaldo Carvalho de Melo { 255aac065c5SEric Dumazet atomic_inc(&queue->young); 256aac065c5SEric Dumazet atomic_inc(&queue->qlen); 2570e87506fSArnaldo Carvalho de Melo } 2580e87506fSArnaldo Carvalho de Melo 259463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len(const struct request_sock_queue *queue) 2600e87506fSArnaldo Carvalho de Melo { 261aac065c5SEric Dumazet return atomic_read(&queue->qlen); 2620e87506fSArnaldo Carvalho de Melo } 2630e87506fSArnaldo Carvalho de Melo 264463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len_young(const struct request_sock_queue *queue) 2650e87506fSArnaldo Carvalho de Melo { 266aac065c5SEric Dumazet return atomic_read(&queue->young); 2670e87506fSArnaldo Carvalho de Melo } 2680e87506fSArnaldo Carvalho de Melo 2692e6599cbSArnaldo Carvalho de Melo #endif /* _REQUEST_SOCK_H */ 270