request_sock.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. /* empty to "strongly type" an otherwise void parameter.
  27. */
  28. struct request_values {
  29. };
  30. struct request_sock_ops {
  31. int family;
  32. int obj_size;
  33. struct kmem_cache *slab;
  34. char *slab_name;
  35. int (*rtx_syn_ack)(struct sock *sk,
  36. struct request_sock *req,
  37. struct request_values *rvp);
  38. void (*send_ack)(struct sock *sk, struct sk_buff *skb,
  39. struct request_sock *req);
  40. void (*send_reset)(struct sock *sk,
  41. struct sk_buff *skb);
  42. void (*destructor)(struct request_sock *req);
  43. void (*syn_ack_timeout)(struct sock *sk,
  44. struct request_sock *req);
  45. };
  46. extern int inet_rtx_syn_ack(struct sock *parent, struct request_sock *req);
  47. /* struct request_sock - mini sock to represent a connection request
  48. */
  49. struct request_sock {
  50. struct request_sock *dl_next; /* Must be first member! */
  51. u16 mss;
  52. u8 num_retrans; /* number of retransmits */
  53. u8 cookie_ts:1; /* syncookie: encode tcpopts in timestamp */
  54. u8 num_timeout:7; /* number of timeouts */
  55. /* The following two fields can be easily recomputed I think -AK */
  56. u32 window_clamp; /* window clamp at creation time */
  57. u32 rcv_wnd; /* rcv_wnd offered first time */
  58. u32 ts_recent;
  59. unsigned long expires;
  60. const struct request_sock_ops *rsk_ops;
  61. struct sock *sk;
  62. u32 secid;
  63. u32 peer_secid;
  64. };
  65. static inline struct request_sock *reqsk_alloc(const struct request_sock_ops *ops)
  66. {
  67. struct request_sock *req = kmem_cache_alloc(ops->slab, GFP_ATOMIC);
  68. if (req != NULL)
  69. req->rsk_ops = ops;
  70. return req;
  71. }
  72. static inline void __reqsk_free(struct request_sock *req)
  73. {
  74. kmem_cache_free(req->rsk_ops->slab, req);
  75. }
  76. static inline void reqsk_free(struct request_sock *req)
  77. {
  78. req->rsk_ops->destructor(req);
  79. __reqsk_free(req);
  80. }
  81. extern int sysctl_max_syn_backlog;
  82. /** struct listen_sock - listen state
  83. *
  84. * @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs
  85. */
  86. struct listen_sock {
  87. u8 max_qlen_log;
  88. u8 synflood_warned;
  89. /* 2 bytes hole, try to use */
  90. int qlen;
  91. int qlen_young;
  92. int clock_hand;
  93. u32 hash_rnd;
  94. u32 nr_table_entries;
  95. struct request_sock *syn_table[0];
  96. };
  97. /*
  98. * For a TCP Fast Open listener -
  99. * lock - protects the access to all the reqsk, which is co-owned by
  100. * the listener and the child socket.
  101. * qlen - pending TFO requests (still in TCP_SYN_RECV).
  102. * max_qlen - max TFO reqs allowed before TFO is disabled.
  103. *
  104. * XXX (TFO) - ideally these fields can be made as part of "listen_sock"
  105. * structure above. But there is some implementation difficulty due to
  106. * listen_sock being part of request_sock_queue hence will be freed when
  107. * a listener is stopped. But TFO related fields may continue to be
  108. * accessed even after a listener is closed, until its sk_refcnt drops
  109. * to 0 implying no more outstanding TFO reqs. One solution is to keep
  110. * listen_opt around until sk_refcnt drops to 0. But there is some other
  111. * complexity that needs to be resolved. E.g., a listener can be disabled
  112. * temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
  113. */
  114. struct fastopen_queue {
  115. struct request_sock *rskq_rst_head; /* Keep track of past TFO */
  116. struct request_sock *rskq_rst_tail; /* requests that caused RST.
  117. * This is part of the defense
  118. * against spoofing attack.
  119. */
  120. spinlock_t lock;
  121. int qlen; /* # of pending (TCP_SYN_RECV) reqs */
  122. int max_qlen; /* != 0 iff TFO is currently enabled */
  123. };
  124. /** struct request_sock_queue - queue of request_socks
  125. *
  126. * @rskq_accept_head - FIFO head of established children
  127. * @rskq_accept_tail - FIFO tail of established children
  128. * @rskq_defer_accept - User waits for some data after accept()
  129. * @syn_wait_lock - serializer
  130. *
  131. * %syn_wait_lock is necessary only to avoid proc interface having to grab the main
  132. * lock sock while browsing the listening hash (otherwise it's deadlock prone).
  133. *
  134. * This lock is acquired in read mode only from listening_get_next() seq_file
  135. * op and it's acquired in write mode _only_ from code that is actively
  136. * changing rskq_accept_head. All readers that are holding the master sock lock
  137. * don't need to grab this lock in read mode too as rskq_accept_head. writes
  138. * are always protected from the main sock lock.
  139. */
  140. struct request_sock_queue {
  141. struct request_sock *rskq_accept_head;
  142. struct request_sock *rskq_accept_tail;
  143. rwlock_t syn_wait_lock;
  144. u8 rskq_defer_accept;
  145. /* 3 bytes hole, try to pack */
  146. struct listen_sock *listen_opt;
  147. struct fastopen_queue *fastopenq; /* This is non-NULL iff TFO has been
  148. * enabled on this listener. Check
  149. * max_qlen != 0 in fastopen_queue
  150. * to determine if TFO is enabled
  151. * right at this moment.
  152. */
  153. };
  154. extern int reqsk_queue_alloc(struct request_sock_queue *queue,
  155. unsigned int nr_table_entries);
  156. extern void __reqsk_queue_destroy(struct request_sock_queue *queue);
  157. extern void reqsk_queue_destroy(struct request_sock_queue *queue);
  158. extern void reqsk_fastopen_remove(struct sock *sk,
  159. struct request_sock *req, bool reset);
  160. static inline struct request_sock *
  161. reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
  162. {
  163. struct request_sock *req = queue->rskq_accept_head;
  164. queue->rskq_accept_head = NULL;
  165. return req;
  166. }
  167. static inline int reqsk_queue_empty(struct request_sock_queue *queue)
  168. {
  169. return queue->rskq_accept_head == NULL;
  170. }
  171. static inline void reqsk_queue_unlink(struct request_sock_queue *queue,
  172. struct request_sock *req,
  173. struct request_sock **prev_req)
  174. {
  175. write_lock(&queue->syn_wait_lock);
  176. *prev_req = req->dl_next;
  177. write_unlock(&queue->syn_wait_lock);
  178. }
  179. static inline void reqsk_queue_add(struct request_sock_queue *queue,
  180. struct request_sock *req,
  181. struct sock *parent,
  182. struct sock *child)
  183. {
  184. req->sk = child;
  185. sk_acceptq_added(parent);
  186. if (queue->rskq_accept_head == NULL)
  187. queue->rskq_accept_head = req;
  188. else
  189. queue->rskq_accept_tail->dl_next = req;
  190. queue->rskq_accept_tail = req;
  191. req->dl_next = NULL;
  192. }
  193. static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue)
  194. {
  195. struct request_sock *req = queue->rskq_accept_head;
  196. WARN_ON(req == NULL);
  197. queue->rskq_accept_head = req->dl_next;
  198. if (queue->rskq_accept_head == NULL)
  199. queue->rskq_accept_tail = NULL;
  200. return req;
  201. }
  202. static inline int reqsk_queue_removed(struct request_sock_queue *queue,
  203. struct request_sock *req)
  204. {
  205. struct listen_sock *lopt = queue->listen_opt;
  206. if (req->num_timeout == 0)
  207. --lopt->qlen_young;
  208. return --lopt->qlen;
  209. }
  210. static inline int reqsk_queue_added(struct request_sock_queue *queue)
  211. {
  212. struct listen_sock *lopt = queue->listen_opt;
  213. const int prev_qlen = lopt->qlen;
  214. lopt->qlen_young++;
  215. lopt->qlen++;
  216. return prev_qlen;
  217. }
  218. static inline int reqsk_queue_len(const struct request_sock_queue *queue)
  219. {
  220. return queue->listen_opt != NULL ? queue->listen_opt->qlen : 0;
  221. }
  222. static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
  223. {
  224. return queue->listen_opt->qlen_young;
  225. }
  226. static inline int reqsk_queue_is_full(const struct request_sock_queue *queue)
  227. {
  228. return queue->listen_opt->qlen >> queue->listen_opt->max_qlen_log;
  229. }
  230. static inline void reqsk_queue_hash_req(struct request_sock_queue *queue,
  231. u32 hash, struct request_sock *req,
  232. unsigned long timeout)
  233. {
  234. struct listen_sock *lopt = queue->listen_opt;
  235. req->expires = jiffies + timeout;
  236. req->num_retrans = 0;
  237. req->num_timeout = 0;
  238. req->sk = NULL;
  239. req->dl_next = lopt->syn_table[hash];
  240. write_lock(&queue->syn_wait_lock);
  241. lopt->syn_table[hash] = req;
  242. write_unlock(&queue->syn_wait_lock);
  243. }
  244. #endif /* _REQUEST_SOCK_H */