12e6599cbSArnaldo Carvalho de Melo /* 22e6599cbSArnaldo Carvalho de Melo * NET Generic infrastructure for Network protocols. 32e6599cbSArnaldo Carvalho de Melo * 42e6599cbSArnaldo Carvalho de Melo * Definitions for request_sock 52e6599cbSArnaldo Carvalho de Melo * 62e6599cbSArnaldo Carvalho de Melo * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br> 72e6599cbSArnaldo Carvalho de Melo * 82e6599cbSArnaldo Carvalho de Melo * From code originally in include/net/tcp.h 92e6599cbSArnaldo Carvalho de Melo * 102e6599cbSArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or 112e6599cbSArnaldo Carvalho de Melo * modify it under the terms of the GNU General Public License 122e6599cbSArnaldo Carvalho de Melo * as published by the Free Software Foundation; either version 132e6599cbSArnaldo Carvalho de Melo * 2 of the License, or (at your option) any later version. 142e6599cbSArnaldo Carvalho de Melo */ 152e6599cbSArnaldo Carvalho de Melo #ifndef _REQUEST_SOCK_H 162e6599cbSArnaldo Carvalho de Melo #define _REQUEST_SOCK_H 172e6599cbSArnaldo Carvalho de Melo 182e6599cbSArnaldo Carvalho de Melo #include <linux/slab.h> 190e87506fSArnaldo Carvalho de Melo #include <linux/spinlock.h> 202e6599cbSArnaldo Carvalho de Melo #include <linux/types.h> 21547b792cSIlpo Järvinen #include <linux/bug.h> 220e87506fSArnaldo Carvalho de Melo 232e6599cbSArnaldo Carvalho de Melo #include <net/sock.h> 242e6599cbSArnaldo Carvalho de Melo 2560236fddSArnaldo Carvalho de Melo struct request_sock; 262e6599cbSArnaldo Carvalho de Melo struct sk_buff; 272e6599cbSArnaldo Carvalho de Melo struct dst_entry; 282e6599cbSArnaldo Carvalho de Melo struct proto; 292e6599cbSArnaldo Carvalho de Melo 3060236fddSArnaldo Carvalho de Melo struct request_sock_ops { 312e6599cbSArnaldo Carvalho de Melo int family; 32417ccf6bSAlexey Dobriyan unsigned int obj_size; 33e18b890bSChristoph Lameter struct kmem_cache *slab; 347e56b5d6SCatalin Marinas char *slab_name; 35ea3bea3aSEric Dumazet int (*rtx_syn_ack)(const struct sock *sk, 361a2c6181SChristoph Paasch struct request_sock *req); 37a00e7444SEric Dumazet void (*send_ack)(const struct sock *sk, struct sk_buff *skb, 3860236fddSArnaldo Carvalho de Melo struct request_sock *req); 39a00e7444SEric Dumazet void (*send_reset)(const struct sock *sk, 40cfb6eeb4SYOSHIFUJI Hideaki struct sk_buff *skb); 4160236fddSArnaldo Carvalho de Melo void (*destructor)(struct request_sock *req); 4242cb80a2SEric Dumazet void (*syn_ack_timeout)(const struct request_sock *req); 432e6599cbSArnaldo Carvalho de Melo }; 442e6599cbSArnaldo Carvalho de Melo 451b70e977SEric Dumazet int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req); 46e6c022a4SEric Dumazet 4760236fddSArnaldo Carvalho de Melo /* struct request_sock - mini sock to represent a connection request 482e6599cbSArnaldo Carvalho de Melo */ 4960236fddSArnaldo Carvalho de Melo struct request_sock { 50634fb979SEric Dumazet struct sock_common __req_common; 511e2e0117SEric Dumazet #define rsk_refcnt __req_common.skc_refcnt 5252452c54SEric Dumazet #define rsk_hash __req_common.skc_hash 538e5eb54dSEric Dumazet #define rsk_listener __req_common.skc_listener 54ed53d0abSEric Dumazet #define rsk_window_clamp __req_common.skc_window_clamp 55ed53d0abSEric Dumazet #define rsk_rcv_wnd __req_common.skc_rcv_wnd 561e2e0117SEric Dumazet 573fb62c5dSEric Dumazet struct request_sock *dl_next; 582e6599cbSArnaldo Carvalho de Melo u16 mss; 59e6c022a4SEric Dumazet u8 num_retrans; /* number of retransmits */ 60e6c022a4SEric Dumazet u8 cookie_ts:1; /* syncookie: encode tcpopts in timestamp */ 61e6c022a4SEric Dumazet u8 num_timeout:7; /* number of timeouts */ 622e6599cbSArnaldo Carvalho de Melo u32 ts_recent; 63fa76ce73SEric Dumazet struct timer_list rsk_timer; 6472a3effaSEric Dumazet const struct request_sock_ops *rsk_ops; 652e6599cbSArnaldo Carvalho de Melo struct sock *sk; 66cd8ae852SEric Dumazet u32 *saved_syn; 674237c75cSVenkat Yekkirala u32 secid; 686b877699SVenkat Yekkirala u32 peer_secid; 692e6599cbSArnaldo Carvalho de Melo }; 702e6599cbSArnaldo Carvalho de Melo 71b1f0a0e9SFlorian Westphal static inline struct request_sock *inet_reqsk(const struct sock *sk) 72b267cdd1SEric Dumazet { 73b267cdd1SEric Dumazet return (struct request_sock *)sk; 74b267cdd1SEric Dumazet } 75b267cdd1SEric Dumazet 76b267cdd1SEric Dumazet static inline struct sock *req_to_sk(struct request_sock *req) 77b267cdd1SEric Dumazet { 78b267cdd1SEric Dumazet return (struct sock *)req; 79b267cdd1SEric Dumazet } 80b267cdd1SEric Dumazet 814e9a578eSEric Dumazet static inline struct request_sock * 82a1a5344dSEric Dumazet reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener, 83a1a5344dSEric Dumazet bool attach_listener) 842e6599cbSArnaldo Carvalho de Melo { 85e96f78abSEric Dumazet struct request_sock *req; 86e96f78abSEric Dumazet 87e96f78abSEric Dumazet req = kmem_cache_alloc(ops->slab, GFP_ATOMIC | __GFP_NOWARN); 883a5d1c0eSEric Dumazet if (!req) 893a5d1c0eSEric Dumazet return NULL; 90a1a5344dSEric Dumazet req->rsk_listener = NULL; 913a5d1c0eSEric Dumazet if (attach_listener) { 923a5d1c0eSEric Dumazet if (unlikely(!atomic_inc_not_zero(&sk_listener->sk_refcnt))) { 933a5d1c0eSEric Dumazet kmem_cache_free(ops->slab, req); 943a5d1c0eSEric Dumazet return NULL; 95a1a5344dSEric Dumazet } 963a5d1c0eSEric Dumazet req->rsk_listener = sk_listener; 973a5d1c0eSEric Dumazet } 983a5d1c0eSEric Dumazet req->rsk_ops = ops; 99b267cdd1SEric Dumazet req_to_sk(req)->sk_prot = sk_listener->sk_prot; 100b267cdd1SEric Dumazet sk_node_init(&req_to_sk(req)->sk_node); 101004a5d01SEric Dumazet sk_tx_queue_clear(req_to_sk(req)); 102cd8ae852SEric Dumazet req->saved_syn = NULL; 1030470c8caSEric Dumazet atomic_set(&req->rsk_refcnt, 0); 1043a5d1c0eSEric Dumazet 1052e6599cbSArnaldo Carvalho de Melo return req; 1062e6599cbSArnaldo Carvalho de Melo } 1072e6599cbSArnaldo Carvalho de Melo 10860236fddSArnaldo Carvalho de Melo static inline void reqsk_free(struct request_sock *req) 1092e6599cbSArnaldo Carvalho de Melo { 11013854e5aSEric Dumazet /* temporary debugging */ 11113854e5aSEric Dumazet WARN_ON_ONCE(atomic_read(&req->rsk_refcnt) != 0); 11213854e5aSEric Dumazet 11360236fddSArnaldo Carvalho de Melo req->rsk_ops->destructor(req); 1144e9a578eSEric Dumazet if (req->rsk_listener) 1154e9a578eSEric Dumazet sock_put(req->rsk_listener); 116cd8ae852SEric Dumazet kfree(req->saved_syn); 11713854e5aSEric Dumazet kmem_cache_free(req->rsk_ops->slab, req); 1182e6599cbSArnaldo Carvalho de Melo } 1192e6599cbSArnaldo Carvalho de Melo 1201e2e0117SEric Dumazet static inline void reqsk_put(struct request_sock *req) 1211e2e0117SEric Dumazet { 1221e2e0117SEric Dumazet if (atomic_dec_and_test(&req->rsk_refcnt)) 1231e2e0117SEric Dumazet reqsk_free(req); 1241e2e0117SEric Dumazet } 1250e87506fSArnaldo Carvalho de Melo 12610467163SJerry Chu /* 12710467163SJerry Chu * For a TCP Fast Open listener - 12810467163SJerry Chu * lock - protects the access to all the reqsk, which is co-owned by 12910467163SJerry Chu * the listener and the child socket. 13010467163SJerry Chu * qlen - pending TFO requests (still in TCP_SYN_RECV). 13110467163SJerry Chu * max_qlen - max TFO reqs allowed before TFO is disabled. 13210467163SJerry Chu * 13310467163SJerry Chu * XXX (TFO) - ideally these fields can be made as part of "listen_sock" 13410467163SJerry Chu * structure above. But there is some implementation difficulty due to 13510467163SJerry Chu * listen_sock being part of request_sock_queue hence will be freed when 13610467163SJerry Chu * a listener is stopped. But TFO related fields may continue to be 13710467163SJerry Chu * accessed even after a listener is closed, until its sk_refcnt drops 13810467163SJerry Chu * to 0 implying no more outstanding TFO reqs. One solution is to keep 13910467163SJerry Chu * listen_opt around until sk_refcnt drops to 0. But there is some other 14010467163SJerry Chu * complexity that needs to be resolved. E.g., a listener can be disabled 14110467163SJerry Chu * temporarily through shutdown()->tcp_disconnect(), and re-enabled later. 14210467163SJerry Chu */ 14310467163SJerry Chu struct fastopen_queue { 14410467163SJerry Chu struct request_sock *rskq_rst_head; /* Keep track of past TFO */ 14510467163SJerry Chu struct request_sock *rskq_rst_tail; /* requests that caused RST. 14610467163SJerry Chu * This is part of the defense 14710467163SJerry Chu * against spoofing attack. 14810467163SJerry Chu */ 14910467163SJerry Chu spinlock_t lock; 15010467163SJerry Chu int qlen; /* # of pending (TCP_SYN_RECV) reqs */ 15110467163SJerry Chu int max_qlen; /* != 0 iff TFO is currently enabled */ 15210467163SJerry Chu }; 15310467163SJerry Chu 1540e87506fSArnaldo Carvalho de Melo /** struct request_sock_queue - queue of request_socks 1550e87506fSArnaldo Carvalho de Melo * 1560e87506fSArnaldo Carvalho de Melo * @rskq_accept_head - FIFO head of established children 1570e87506fSArnaldo Carvalho de Melo * @rskq_accept_tail - FIFO tail of established children 158295f7324SArnaldo Carvalho de Melo * @rskq_defer_accept - User waits for some data after accept() 1590e87506fSArnaldo Carvalho de Melo * 1600e87506fSArnaldo Carvalho de Melo */ 1610e87506fSArnaldo Carvalho de Melo struct request_sock_queue { 162fff1f300SEric Dumazet spinlock_t rskq_lock; 163fff1f300SEric Dumazet u8 rskq_defer_accept; 164ef547f2aSEric Dumazet 1658d2675f1SEric Dumazet u32 synflood_warned; 166aac065c5SEric Dumazet atomic_t qlen; 167aac065c5SEric Dumazet atomic_t young; 168aac065c5SEric Dumazet 1690e87506fSArnaldo Carvalho de Melo struct request_sock *rskq_accept_head; 1700e87506fSArnaldo Carvalho de Melo struct request_sock *rskq_accept_tail; 1710536fcc0SEric Dumazet struct fastopen_queue fastopenq; /* Check max_qlen != 0 to determine 1720536fcc0SEric Dumazet * if TFO is enabled. 17310467163SJerry Chu */ 1740e87506fSArnaldo Carvalho de Melo }; 1750e87506fSArnaldo Carvalho de Melo 176ef547f2aSEric Dumazet void reqsk_queue_alloc(struct request_sock_queue *queue); 1770e87506fSArnaldo Carvalho de Melo 178c0f4502aSJoe Perches void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req, 179c0f4502aSJoe Perches bool reset); 18083e3609eSArnaldo Carvalho de Melo 181fff1f300SEric Dumazet static inline bool reqsk_queue_empty(const struct request_sock_queue *queue) 1820e87506fSArnaldo Carvalho de Melo { 1830e87506fSArnaldo Carvalho de Melo return queue->rskq_accept_head == NULL; 1840e87506fSArnaldo Carvalho de Melo } 1850e87506fSArnaldo Carvalho de Melo 186fff1f300SEric Dumazet static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue, 187fff1f300SEric Dumazet struct sock *parent) 1880e87506fSArnaldo Carvalho de Melo { 189fff1f300SEric Dumazet struct request_sock *req; 1900e87506fSArnaldo Carvalho de Melo 191fff1f300SEric Dumazet spin_lock_bh(&queue->rskq_lock); 192fff1f300SEric Dumazet req = queue->rskq_accept_head; 193fff1f300SEric Dumazet if (req) { 194fff1f300SEric Dumazet sk_acceptq_removed(parent); 1950e87506fSArnaldo Carvalho de Melo queue->rskq_accept_head = req->dl_next; 1960e87506fSArnaldo Carvalho de Melo if (queue->rskq_accept_head == NULL) 1970e87506fSArnaldo Carvalho de Melo queue->rskq_accept_tail = NULL; 198fff1f300SEric Dumazet } 199fff1f300SEric Dumazet spin_unlock_bh(&queue->rskq_lock); 2000e87506fSArnaldo Carvalho de Melo return req; 2010e87506fSArnaldo Carvalho de Melo } 2020e87506fSArnaldo Carvalho de Melo 203fa76ce73SEric Dumazet static inline void reqsk_queue_removed(struct request_sock_queue *queue, 204fa76ce73SEric Dumazet const struct request_sock *req) 2050e87506fSArnaldo Carvalho de Melo { 206e6c022a4SEric Dumazet if (req->num_timeout == 0) 207aac065c5SEric Dumazet atomic_dec(&queue->young); 208aac065c5SEric Dumazet atomic_dec(&queue->qlen); 2090e87506fSArnaldo Carvalho de Melo } 2100e87506fSArnaldo Carvalho de Melo 211fa76ce73SEric Dumazet static inline void reqsk_queue_added(struct request_sock_queue *queue) 2120e87506fSArnaldo Carvalho de Melo { 213aac065c5SEric Dumazet atomic_inc(&queue->young); 214aac065c5SEric Dumazet atomic_inc(&queue->qlen); 2150e87506fSArnaldo Carvalho de Melo } 2160e87506fSArnaldo Carvalho de Melo 217463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len(const struct request_sock_queue *queue) 2180e87506fSArnaldo Carvalho de Melo { 219aac065c5SEric Dumazet return atomic_read(&queue->qlen); 2200e87506fSArnaldo Carvalho de Melo } 2210e87506fSArnaldo Carvalho de Melo 222463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len_young(const struct request_sock_queue *queue) 2230e87506fSArnaldo Carvalho de Melo { 224aac065c5SEric Dumazet return atomic_read(&queue->young); 2250e87506fSArnaldo Carvalho de Melo } 2260e87506fSArnaldo Carvalho de Melo 2272e6599cbSArnaldo Carvalho de Melo #endif /* _REQUEST_SOCK_H */ 228