request_sock.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * NET Generic infrastructure for Network protocols.
  3. *
  4. * Definitions for request_sock
  5. *
  6. * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  7. *
  8. * From code originally in include/net/tcp.h
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #ifndef _REQUEST_SOCK_H
  16. #define _REQUEST_SOCK_H
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/types.h>
  20. #include <linux/bug.h>
  21. #include <net/sock.h>
  22. struct request_sock;
  23. struct sk_buff;
  24. struct dst_entry;
  25. struct proto;
  26. struct request_sock_ops {
  27. int family;
  28. int obj_size;
  29. struct kmem_cache *slab;
  30. char *slab_name;
  31. int (*rtx_syn_ack)(struct sock *sk,
  32. struct request_sock *req);
  33. void (*send_ack)(struct sock *sk, struct sk_buff *skb,
  34. struct request_sock *req);
  35. void (*send_reset)(struct sock *sk,
  36. struct sk_buff *skb);
  37. void (*destructor)(struct request_sock *req);
  38. void (*syn_ack_timeout)(const struct request_sock *req);
  39. };
  40. int inet_rtx_syn_ack(struct sock *parent, struct request_sock *req);
  41. /* struct request_sock - mini sock to represent a connection request
  42. */
  43. struct request_sock {
  44. struct sock_common __req_common;
  45. #define rsk_refcnt __req_common.skc_refcnt
  46. #define rsk_hash __req_common.skc_hash
  47. struct request_sock *dl_next;
  48. struct sock *rsk_listener;
  49. u16 mss;
  50. u8 num_retrans; /* number of retransmits */
  51. u8 cookie_ts:1; /* syncookie: encode tcpopts in timestamp */
  52. u8 num_timeout:7; /* number of timeouts */
  53. /* The following two fields can be easily recomputed I think -AK */
  54. u32 window_clamp; /* window clamp at creation time */
  55. u32 rcv_wnd; /* rcv_wnd offered first time */
  56. u32 ts_recent;
  57. struct timer_list rsk_timer;
  58. const struct request_sock_ops *rsk_ops;
  59. struct sock *sk;
  60. u32 *saved_syn;
  61. u32 secid;
  62. u32 peer_secid;
  63. };
  64. static inline struct request_sock *
  65. reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener)
  66. {
  67. struct request_sock *req = kmem_cache_alloc(ops->slab, GFP_ATOMIC);
  68. if (req) {
  69. req->rsk_ops = ops;
  70. sock_hold(sk_listener);
  71. req->rsk_listener = sk_listener;
  72. req->saved_syn = NULL;
  73. /* Following is temporary. It is coupled with debugging
  74. * helpers in reqsk_put() & reqsk_free()
  75. */
  76. atomic_set(&req->rsk_refcnt, 0);
  77. }
  78. return req;
  79. }
  80. static inline struct request_sock *inet_reqsk(struct sock *sk)
  81. {
  82. return (struct request_sock *)sk;
  83. }
  84. static inline struct sock *req_to_sk(struct request_sock *req)
  85. {
  86. return (struct sock *)req;
  87. }
  88. static inline void reqsk_free(struct request_sock *req)
  89. {
  90. /* temporary debugging */
  91. WARN_ON_ONCE(atomic_read(&req->rsk_refcnt) != 0);
  92. req->rsk_ops->destructor(req);
  93. if (req->rsk_listener)
  94. sock_put(req->rsk_listener);
  95. kfree(req->saved_syn);
  96. kmem_cache_free(req->rsk_ops->slab, req);
  97. }
  98. static inline void reqsk_put(struct request_sock *req)
  99. {
  100. if (atomic_dec_and_test(&req->rsk_refcnt))
  101. reqsk_free(req);
  102. }
  103. extern int sysctl_max_syn_backlog;
  104. /** struct listen_sock - listen state
  105. *
  106. * @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs
  107. */
  108. struct listen_sock {
  109. int qlen_inc; /* protected by listener lock */
  110. int young_inc;/* protected by listener lock */
  111. /* following fields can be updated by timer */
  112. atomic_t qlen_dec; /* qlen = qlen_inc - qlen_dec */
  113. atomic_t young_dec;
  114. u8 max_qlen_log ____cacheline_aligned_in_smp;
  115. u8 synflood_warned;
  116. /* 2 bytes hole, try to use */
  117. u32 hash_rnd;
  118. u32 nr_table_entries;
  119. struct request_sock *syn_table[0];
  120. };
  121. /*
  122. * For a TCP Fast Open listener -
  123. * lock - protects the access to all the reqsk, which is co-owned by
  124. * the listener and the child socket.
  125. * qlen - pending TFO requests (still in TCP_SYN_RECV).
  126. * max_qlen - max TFO reqs allowed before TFO is disabled.
  127. *
  128. * XXX (TFO) - ideally these fields can be made as part of "listen_sock"
  129. * structure above. But there is some implementation difficulty due to
  130. * listen_sock being part of request_sock_queue hence will be freed when
  131. * a listener is stopped. But TFO related fields may continue to be
  132. * accessed even after a listener is closed, until its sk_refcnt drops
  133. * to 0 implying no more outstanding TFO reqs. One solution is to keep
  134. * listen_opt around until sk_refcnt drops to 0. But there is some other
  135. * complexity that needs to be resolved. E.g., a listener can be disabled
  136. * temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
  137. */
  138. struct fastopen_queue {
  139. struct request_sock *rskq_rst_head; /* Keep track of past TFO */
  140. struct request_sock *rskq_rst_tail; /* requests that caused RST.
  141. * This is part of the defense
  142. * against spoofing attack.
  143. */
  144. spinlock_t lock;
  145. int qlen; /* # of pending (TCP_SYN_RECV) reqs */
  146. int max_qlen; /* != 0 iff TFO is currently enabled */
  147. };
  148. /** struct request_sock_queue - queue of request_socks
  149. *
  150. * @rskq_accept_head - FIFO head of established children
  151. * @rskq_accept_tail - FIFO tail of established children
  152. * @rskq_defer_accept - User waits for some data after accept()
  153. * @syn_wait_lock - serializer
  154. *
  155. * %syn_wait_lock is necessary only to avoid proc interface having to grab the main
  156. * lock sock while browsing the listening hash (otherwise it's deadlock prone).
  157. *
  158. */
  159. struct request_sock_queue {
  160. struct request_sock *rskq_accept_head;
  161. struct request_sock *rskq_accept_tail;
  162. u8 rskq_defer_accept;
  163. struct listen_sock *listen_opt;
  164. struct fastopen_queue *fastopenq; /* This is non-NULL iff TFO has been
  165. * enabled on this listener. Check
  166. * max_qlen != 0 in fastopen_queue
  167. * to determine if TFO is enabled
  168. * right at this moment.
  169. */
  170. /* temporary alignment, our goal is to get rid of this lock */
  171. spinlock_t syn_wait_lock ____cacheline_aligned_in_smp;
  172. };
  173. int reqsk_queue_alloc(struct request_sock_queue *queue,
  174. unsigned int nr_table_entries);
  175. void __reqsk_queue_destroy(struct request_sock_queue *queue);
  176. void reqsk_queue_destroy(struct request_sock_queue *queue);
  177. void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  178. bool reset);
  179. static inline struct request_sock *
  180. reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
  181. {
  182. struct request_sock *req = queue->rskq_accept_head;
  183. queue->rskq_accept_head = NULL;
  184. return req;
  185. }
  186. static inline int reqsk_queue_empty(struct request_sock_queue *queue)
  187. {
  188. return queue->rskq_accept_head == NULL;
  189. }
  190. static inline void reqsk_queue_add(struct request_sock_queue *queue,
  191. struct request_sock *req,
  192. struct sock *parent,
  193. struct sock *child)
  194. {
  195. req->sk = child;
  196. sk_acceptq_added(parent);
  197. if (queue->rskq_accept_head == NULL)
  198. queue->rskq_accept_head = req;
  199. else
  200. queue->rskq_accept_tail->dl_next = req;
  201. queue->rskq_accept_tail = req;
  202. req->dl_next = NULL;
  203. }
  204. static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue)
  205. {
  206. struct request_sock *req = queue->rskq_accept_head;
  207. WARN_ON(req == NULL);
  208. queue->rskq_accept_head = req->dl_next;
  209. if (queue->rskq_accept_head == NULL)
  210. queue->rskq_accept_tail = NULL;
  211. return req;
  212. }
  213. static inline void reqsk_queue_removed(struct request_sock_queue *queue,
  214. const struct request_sock *req)
  215. {
  216. struct listen_sock *lopt = queue->listen_opt;
  217. if (req->num_timeout == 0)
  218. atomic_inc(&lopt->young_dec);
  219. atomic_inc(&lopt->qlen_dec);
  220. }
  221. static inline void reqsk_queue_added(struct request_sock_queue *queue)
  222. {
  223. struct listen_sock *lopt = queue->listen_opt;
  224. lopt->young_inc++;
  225. lopt->qlen_inc++;
  226. }
  227. static inline int listen_sock_qlen(const struct listen_sock *lopt)
  228. {
  229. return lopt->qlen_inc - atomic_read(&lopt->qlen_dec);
  230. }
  231. static inline int listen_sock_young(const struct listen_sock *lopt)
  232. {
  233. return lopt->young_inc - atomic_read(&lopt->young_dec);
  234. }
  235. static inline int reqsk_queue_len(const struct request_sock_queue *queue)
  236. {
  237. const struct listen_sock *lopt = queue->listen_opt;
  238. return lopt ? listen_sock_qlen(lopt) : 0;
  239. }
  240. static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
  241. {
  242. return listen_sock_young(queue->listen_opt);
  243. }
  244. static inline int reqsk_queue_is_full(const struct request_sock_queue *queue)
  245. {
  246. return reqsk_queue_len(queue) >> queue->listen_opt->max_qlen_log;
  247. }
  248. void reqsk_queue_hash_req(struct request_sock_queue *queue,
  249. u32 hash, struct request_sock *req,
  250. unsigned long timeout);
  251. #endif /* _REQUEST_SOCK_H */