request_sock.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 secid;
  61. u32 peer_secid;
  62. };
  63. static inline struct request_sock *
  64. reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener)
  65. {
  66. struct request_sock *req = kmem_cache_alloc(ops->slab, GFP_ATOMIC);
  67. if (req) {
  68. req->rsk_ops = ops;
  69. sock_hold(sk_listener);
  70. req->rsk_listener = sk_listener;
  71. /* Following is temporary. It is coupled with debugging
  72. * helpers in reqsk_put() & reqsk_free()
  73. */
  74. atomic_set(&req->rsk_refcnt, 0);
  75. }
  76. return req;
  77. }
  78. static inline struct request_sock *inet_reqsk(struct sock *sk)
  79. {
  80. return (struct request_sock *)sk;
  81. }
  82. static inline struct sock *req_to_sk(struct request_sock *req)
  83. {
  84. return (struct sock *)req;
  85. }
  86. static inline void reqsk_free(struct request_sock *req)
  87. {
  88. /* temporary debugging */
  89. WARN_ON_ONCE(atomic_read(&req->rsk_refcnt) != 0);
  90. req->rsk_ops->destructor(req);
  91. if (req->rsk_listener)
  92. sock_put(req->rsk_listener);
  93. kmem_cache_free(req->rsk_ops->slab, req);
  94. }
  95. static inline void reqsk_put(struct request_sock *req)
  96. {
  97. if (atomic_dec_and_test(&req->rsk_refcnt))
  98. reqsk_free(req);
  99. }
  100. extern int sysctl_max_syn_backlog;
  101. /** struct listen_sock - listen state
  102. *
  103. * @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs
  104. */
  105. struct listen_sock {
  106. int qlen_inc; /* protected by listener lock */
  107. int young_inc;/* protected by listener lock */
  108. /* following fields can be updated by timer */
  109. atomic_t qlen_dec; /* qlen = qlen_inc - qlen_dec */
  110. atomic_t young_dec;
  111. u8 max_qlen_log ____cacheline_aligned_in_smp;
  112. u8 synflood_warned;
  113. /* 2 bytes hole, try to use */
  114. u32 hash_rnd;
  115. u32 nr_table_entries;
  116. struct request_sock *syn_table[0];
  117. };
  118. /*
  119. * For a TCP Fast Open listener -
  120. * lock - protects the access to all the reqsk, which is co-owned by
  121. * the listener and the child socket.
  122. * qlen - pending TFO requests (still in TCP_SYN_RECV).
  123. * max_qlen - max TFO reqs allowed before TFO is disabled.
  124. *
  125. * XXX (TFO) - ideally these fields can be made as part of "listen_sock"
  126. * structure above. But there is some implementation difficulty due to
  127. * listen_sock being part of request_sock_queue hence will be freed when
  128. * a listener is stopped. But TFO related fields may continue to be
  129. * accessed even after a listener is closed, until its sk_refcnt drops
  130. * to 0 implying no more outstanding TFO reqs. One solution is to keep
  131. * listen_opt around until sk_refcnt drops to 0. But there is some other
  132. * complexity that needs to be resolved. E.g., a listener can be disabled
  133. * temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
  134. */
  135. struct fastopen_queue {
  136. struct request_sock *rskq_rst_head; /* Keep track of past TFO */
  137. struct request_sock *rskq_rst_tail; /* requests that caused RST.
  138. * This is part of the defense
  139. * against spoofing attack.
  140. */
  141. spinlock_t lock;
  142. int qlen; /* # of pending (TCP_SYN_RECV) reqs */
  143. int max_qlen; /* != 0 iff TFO is currently enabled */
  144. };
  145. /** struct request_sock_queue - queue of request_socks
  146. *
  147. * @rskq_accept_head - FIFO head of established children
  148. * @rskq_accept_tail - FIFO tail of established children
  149. * @rskq_defer_accept - User waits for some data after accept()
  150. * @syn_wait_lock - serializer
  151. *
  152. * %syn_wait_lock is necessary only to avoid proc interface having to grab the main
  153. * lock sock while browsing the listening hash (otherwise it's deadlock prone).
  154. *
  155. */
  156. struct request_sock_queue {
  157. struct request_sock *rskq_accept_head;
  158. struct request_sock *rskq_accept_tail;
  159. u8 rskq_defer_accept;
  160. struct listen_sock *listen_opt;
  161. struct fastopen_queue *fastopenq; /* This is non-NULL iff TFO has been
  162. * enabled on this listener. Check
  163. * max_qlen != 0 in fastopen_queue
  164. * to determine if TFO is enabled
  165. * right at this moment.
  166. */
  167. /* temporary alignment, our goal is to get rid of this lock */
  168. spinlock_t syn_wait_lock ____cacheline_aligned_in_smp;
  169. };
  170. int reqsk_queue_alloc(struct request_sock_queue *queue,
  171. unsigned int nr_table_entries);
  172. void __reqsk_queue_destroy(struct request_sock_queue *queue);
  173. void reqsk_queue_destroy(struct request_sock_queue *queue);
  174. void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  175. bool reset);
  176. static inline struct request_sock *
  177. reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
  178. {
  179. struct request_sock *req = queue->rskq_accept_head;
  180. queue->rskq_accept_head = NULL;
  181. return req;
  182. }
  183. static inline int reqsk_queue_empty(struct request_sock_queue *queue)
  184. {
  185. return queue->rskq_accept_head == NULL;
  186. }
  187. static inline void reqsk_queue_add(struct request_sock_queue *queue,
  188. struct request_sock *req,
  189. struct sock *parent,
  190. struct sock *child)
  191. {
  192. req->sk = child;
  193. sk_acceptq_added(parent);
  194. if (queue->rskq_accept_head == NULL)
  195. queue->rskq_accept_head = req;
  196. else
  197. queue->rskq_accept_tail->dl_next = req;
  198. queue->rskq_accept_tail = req;
  199. req->dl_next = NULL;
  200. }
  201. static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue)
  202. {
  203. struct request_sock *req = queue->rskq_accept_head;
  204. WARN_ON(req == NULL);
  205. queue->rskq_accept_head = req->dl_next;
  206. if (queue->rskq_accept_head == NULL)
  207. queue->rskq_accept_tail = NULL;
  208. return req;
  209. }
  210. static inline void reqsk_queue_removed(struct request_sock_queue *queue,
  211. const struct request_sock *req)
  212. {
  213. struct listen_sock *lopt = queue->listen_opt;
  214. if (req->num_timeout == 0)
  215. atomic_inc(&lopt->young_dec);
  216. atomic_inc(&lopt->qlen_dec);
  217. }
  218. static inline void reqsk_queue_added(struct request_sock_queue *queue)
  219. {
  220. struct listen_sock *lopt = queue->listen_opt;
  221. lopt->young_inc++;
  222. lopt->qlen_inc++;
  223. }
  224. static inline int listen_sock_qlen(const struct listen_sock *lopt)
  225. {
  226. return lopt->qlen_inc - atomic_read(&lopt->qlen_dec);
  227. }
  228. static inline int listen_sock_young(const struct listen_sock *lopt)
  229. {
  230. return lopt->young_inc - atomic_read(&lopt->young_dec);
  231. }
  232. static inline int reqsk_queue_len(const struct request_sock_queue *queue)
  233. {
  234. const struct listen_sock *lopt = queue->listen_opt;
  235. return lopt ? listen_sock_qlen(lopt) : 0;
  236. }
  237. static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
  238. {
  239. return listen_sock_young(queue->listen_opt);
  240. }
  241. static inline int reqsk_queue_is_full(const struct request_sock_queue *queue)
  242. {
  243. return reqsk_queue_len(queue) >> queue->listen_opt->max_qlen_log;
  244. }
  245. void reqsk_queue_hash_req(struct request_sock_queue *queue,
  246. u32 hash, struct request_sock *req,
  247. unsigned long timeout);
  248. #endif /* _REQUEST_SOCK_H */