request_sock.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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)(struct sock *sk,
  39. struct request_sock *req);
  40. };
  41. int inet_rtx_syn_ack(struct sock *parent, struct request_sock *req);
  42. /* struct request_sock - mini sock to represent a connection request
  43. */
  44. struct request_sock {
  45. struct sock_common __req_common;
  46. #define rsk_refcnt __req_common.skc_refcnt
  47. #define rsk_hash __req_common.skc_hash
  48. struct request_sock *dl_next;
  49. struct sock *rsk_listener;
  50. u16 mss;
  51. u8 num_retrans; /* number of retransmits */
  52. u8 cookie_ts:1; /* syncookie: encode tcpopts in timestamp */
  53. u8 num_timeout:7; /* number of timeouts */
  54. /* The following two fields can be easily recomputed I think -AK */
  55. u32 window_clamp; /* window clamp at creation time */
  56. u32 rcv_wnd; /* rcv_wnd offered first time */
  57. u32 ts_recent;
  58. unsigned long expires;
  59. const struct request_sock_ops *rsk_ops;
  60. struct sock *sk;
  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. /* Following is temporary. It is coupled with debugging
  73. * helpers in reqsk_put() & reqsk_free()
  74. */
  75. atomic_set(&req->rsk_refcnt, 0);
  76. }
  77. return req;
  78. }
  79. static inline struct request_sock *inet_reqsk(struct sock *sk)
  80. {
  81. return (struct request_sock *)sk;
  82. }
  83. static inline struct sock *req_to_sk(struct request_sock *req)
  84. {
  85. return (struct sock *)req;
  86. }
  87. static inline void reqsk_free(struct request_sock *req)
  88. {
  89. /* temporary debugging */
  90. WARN_ON_ONCE(atomic_read(&req->rsk_refcnt) != 0);
  91. req->rsk_ops->destructor(req);
  92. if (req->rsk_listener)
  93. sock_put(req->rsk_listener);
  94. kmem_cache_free(req->rsk_ops->slab, req);
  95. }
  96. static inline void reqsk_put(struct request_sock *req)
  97. {
  98. /* temporary debugging, until req sock are put into ehash table */
  99. WARN_ON_ONCE(atomic_read(&req->rsk_refcnt) != 1);
  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. u8 max_qlen_log;
  110. u8 synflood_warned;
  111. /* 2 bytes hole, try to use */
  112. int qlen;
  113. int qlen_young;
  114. int clock_hand;
  115. u32 hash_rnd;
  116. u32 nr_table_entries;
  117. struct request_sock *syn_table[0];
  118. };
  119. /*
  120. * For a TCP Fast Open listener -
  121. * lock - protects the access to all the reqsk, which is co-owned by
  122. * the listener and the child socket.
  123. * qlen - pending TFO requests (still in TCP_SYN_RECV).
  124. * max_qlen - max TFO reqs allowed before TFO is disabled.
  125. *
  126. * XXX (TFO) - ideally these fields can be made as part of "listen_sock"
  127. * structure above. But there is some implementation difficulty due to
  128. * listen_sock being part of request_sock_queue hence will be freed when
  129. * a listener is stopped. But TFO related fields may continue to be
  130. * accessed even after a listener is closed, until its sk_refcnt drops
  131. * to 0 implying no more outstanding TFO reqs. One solution is to keep
  132. * listen_opt around until sk_refcnt drops to 0. But there is some other
  133. * complexity that needs to be resolved. E.g., a listener can be disabled
  134. * temporarily through shutdown()->tcp_disconnect(), and re-enabled later.
  135. */
  136. struct fastopen_queue {
  137. struct request_sock *rskq_rst_head; /* Keep track of past TFO */
  138. struct request_sock *rskq_rst_tail; /* requests that caused RST.
  139. * This is part of the defense
  140. * against spoofing attack.
  141. */
  142. spinlock_t lock;
  143. int qlen; /* # of pending (TCP_SYN_RECV) reqs */
  144. int max_qlen; /* != 0 iff TFO is currently enabled */
  145. };
  146. /** struct request_sock_queue - queue of request_socks
  147. *
  148. * @rskq_accept_head - FIFO head of established children
  149. * @rskq_accept_tail - FIFO tail of established children
  150. * @rskq_defer_accept - User waits for some data after accept()
  151. * @syn_wait_lock - serializer
  152. *
  153. * %syn_wait_lock is necessary only to avoid proc interface having to grab the main
  154. * lock sock while browsing the listening hash (otherwise it's deadlock prone).
  155. *
  156. * This lock is acquired in read mode only from listening_get_next() seq_file
  157. * op and it's acquired in write mode _only_ from code that is actively
  158. * changing rskq_accept_head. All readers that are holding the master sock lock
  159. * don't need to grab this lock in read mode too as rskq_accept_head. writes
  160. * are always protected from the main sock lock.
  161. */
  162. struct request_sock_queue {
  163. struct request_sock *rskq_accept_head;
  164. struct request_sock *rskq_accept_tail;
  165. rwlock_t syn_wait_lock;
  166. u8 rskq_defer_accept;
  167. /* 3 bytes hole, try to pack */
  168. struct listen_sock *listen_opt;
  169. struct fastopen_queue *fastopenq; /* This is non-NULL iff TFO has been
  170. * enabled on this listener. Check
  171. * max_qlen != 0 in fastopen_queue
  172. * to determine if TFO is enabled
  173. * right at this moment.
  174. */
  175. };
  176. int reqsk_queue_alloc(struct request_sock_queue *queue,
  177. unsigned int nr_table_entries);
  178. void __reqsk_queue_destroy(struct request_sock_queue *queue);
  179. void reqsk_queue_destroy(struct request_sock_queue *queue);
  180. void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  181. bool reset);
  182. static inline struct request_sock *
  183. reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
  184. {
  185. struct request_sock *req = queue->rskq_accept_head;
  186. queue->rskq_accept_head = NULL;
  187. return req;
  188. }
  189. static inline int reqsk_queue_empty(struct request_sock_queue *queue)
  190. {
  191. return queue->rskq_accept_head == NULL;
  192. }
  193. static inline void reqsk_queue_unlink(struct request_sock_queue *queue,
  194. struct request_sock *req)
  195. {
  196. struct listen_sock *lopt = queue->listen_opt;
  197. struct request_sock **prev;
  198. write_lock(&queue->syn_wait_lock);
  199. prev = &lopt->syn_table[req->rsk_hash];
  200. while (*prev != req)
  201. prev = &(*prev)->dl_next;
  202. *prev = req->dl_next;
  203. write_unlock(&queue->syn_wait_lock);
  204. }
  205. static inline void reqsk_queue_add(struct request_sock_queue *queue,
  206. struct request_sock *req,
  207. struct sock *parent,
  208. struct sock *child)
  209. {
  210. req->sk = child;
  211. sk_acceptq_added(parent);
  212. if (queue->rskq_accept_head == NULL)
  213. queue->rskq_accept_head = req;
  214. else
  215. queue->rskq_accept_tail->dl_next = req;
  216. queue->rskq_accept_tail = req;
  217. req->dl_next = NULL;
  218. }
  219. static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue)
  220. {
  221. struct request_sock *req = queue->rskq_accept_head;
  222. WARN_ON(req == NULL);
  223. queue->rskq_accept_head = req->dl_next;
  224. if (queue->rskq_accept_head == NULL)
  225. queue->rskq_accept_tail = NULL;
  226. return req;
  227. }
  228. static inline int reqsk_queue_removed(struct request_sock_queue *queue,
  229. struct request_sock *req)
  230. {
  231. struct listen_sock *lopt = queue->listen_opt;
  232. if (req->num_timeout == 0)
  233. --lopt->qlen_young;
  234. return --lopt->qlen;
  235. }
  236. static inline int reqsk_queue_added(struct request_sock_queue *queue)
  237. {
  238. struct listen_sock *lopt = queue->listen_opt;
  239. const int prev_qlen = lopt->qlen;
  240. lopt->qlen_young++;
  241. lopt->qlen++;
  242. return prev_qlen;
  243. }
  244. static inline int reqsk_queue_len(const struct request_sock_queue *queue)
  245. {
  246. return queue->listen_opt != NULL ? queue->listen_opt->qlen : 0;
  247. }
  248. static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
  249. {
  250. return queue->listen_opt->qlen_young;
  251. }
  252. static inline int reqsk_queue_is_full(const struct request_sock_queue *queue)
  253. {
  254. return queue->listen_opt->qlen >> queue->listen_opt->max_qlen_log;
  255. }
  256. static inline void reqsk_queue_hash_req(struct request_sock_queue *queue,
  257. u32 hash, struct request_sock *req,
  258. unsigned long timeout)
  259. {
  260. struct listen_sock *lopt = queue->listen_opt;
  261. req->expires = jiffies + timeout;
  262. req->num_retrans = 0;
  263. req->num_timeout = 0;
  264. req->sk = NULL;
  265. /* before letting lookups find us, make sure all req fields
  266. * are committed to memory and refcnt initialized.
  267. */
  268. smp_wmb();
  269. atomic_set(&req->rsk_refcnt, 1);
  270. req->rsk_hash = hash;
  271. write_lock(&queue->syn_wait_lock);
  272. req->dl_next = lopt->syn_table[hash];
  273. lopt->syn_table[hash] = req;
  274. write_unlock(&queue->syn_wait_lock);
  275. }
  276. #endif /* _REQUEST_SOCK_H */