request_sock.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* struct request_sock - mini sock to represent a connection request
  47. */
  48. struct request_sock {
  49. struct request_sock *dl_next; /* Must be first member! */
  50. u16 mss;
  51. u8 retrans;
  52. u8 cookie_ts; /* syncookie: encode tcpopts in timestamp */
  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. unsigned long expires;
  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 *reqsk_alloc(const struct request_sock_ops *ops)
  64. {
  65. struct request_sock *req = kmem_cache_alloc(ops->slab, GFP_ATOMIC);
  66. if (req != NULL)
  67. req->rsk_ops = ops;
  68. return req;
  69. }
  70. static inline void __reqsk_free(struct request_sock *req)
  71. {
  72. kmem_cache_free(req->rsk_ops->slab, req);
  73. }
  74. static inline void reqsk_free(struct request_sock *req)
  75. {
  76. req->rsk_ops->destructor(req);
  77. __reqsk_free(req);
  78. }
  79. extern int sysctl_max_syn_backlog;
  80. /** struct listen_sock - listen state
  81. *
  82. * @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs
  83. */
  84. struct listen_sock {
  85. u8 max_qlen_log;
  86. /* 3 bytes hole, try to use */
  87. int qlen;
  88. int qlen_young;
  89. int clock_hand;
  90. u32 hash_rnd;
  91. u32 nr_table_entries;
  92. struct request_sock *syn_table[0];
  93. };
  94. /** struct request_sock_queue - queue of request_socks
  95. *
  96. * @rskq_accept_head - FIFO head of established children
  97. * @rskq_accept_tail - FIFO tail of established children
  98. * @rskq_defer_accept - User waits for some data after accept()
  99. * @syn_wait_lock - serializer
  100. *
  101. * %syn_wait_lock is necessary only to avoid proc interface having to grab the main
  102. * lock sock while browsing the listening hash (otherwise it's deadlock prone).
  103. *
  104. * This lock is acquired in read mode only from listening_get_next() seq_file
  105. * op and it's acquired in write mode _only_ from code that is actively
  106. * changing rskq_accept_head. All readers that are holding the master sock lock
  107. * don't need to grab this lock in read mode too as rskq_accept_head. writes
  108. * are always protected from the main sock lock.
  109. */
  110. struct request_sock_queue {
  111. struct request_sock *rskq_accept_head;
  112. struct request_sock *rskq_accept_tail;
  113. rwlock_t syn_wait_lock;
  114. u8 rskq_defer_accept;
  115. /* 3 bytes hole, try to pack */
  116. struct listen_sock *listen_opt;
  117. };
  118. extern int reqsk_queue_alloc(struct request_sock_queue *queue,
  119. unsigned int nr_table_entries);
  120. extern void __reqsk_queue_destroy(struct request_sock_queue *queue);
  121. extern void reqsk_queue_destroy(struct request_sock_queue *queue);
  122. static inline struct request_sock *
  123. reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
  124. {
  125. struct request_sock *req = queue->rskq_accept_head;
  126. queue->rskq_accept_head = NULL;
  127. return req;
  128. }
  129. static inline int reqsk_queue_empty(struct request_sock_queue *queue)
  130. {
  131. return queue->rskq_accept_head == NULL;
  132. }
  133. static inline void reqsk_queue_unlink(struct request_sock_queue *queue,
  134. struct request_sock *req,
  135. struct request_sock **prev_req)
  136. {
  137. write_lock(&queue->syn_wait_lock);
  138. *prev_req = req->dl_next;
  139. write_unlock(&queue->syn_wait_lock);
  140. }
  141. static inline void reqsk_queue_add(struct request_sock_queue *queue,
  142. struct request_sock *req,
  143. struct sock *parent,
  144. struct sock *child)
  145. {
  146. req->sk = child;
  147. sk_acceptq_added(parent);
  148. if (queue->rskq_accept_head == NULL)
  149. queue->rskq_accept_head = req;
  150. else
  151. queue->rskq_accept_tail->dl_next = req;
  152. queue->rskq_accept_tail = req;
  153. req->dl_next = NULL;
  154. }
  155. static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue)
  156. {
  157. struct request_sock *req = queue->rskq_accept_head;
  158. WARN_ON(req == NULL);
  159. queue->rskq_accept_head = req->dl_next;
  160. if (queue->rskq_accept_head == NULL)
  161. queue->rskq_accept_tail = NULL;
  162. return req;
  163. }
  164. static inline struct sock *reqsk_queue_get_child(struct request_sock_queue *queue,
  165. struct sock *parent)
  166. {
  167. struct request_sock *req = reqsk_queue_remove(queue);
  168. struct sock *child = req->sk;
  169. WARN_ON(child == NULL);
  170. sk_acceptq_removed(parent);
  171. __reqsk_free(req);
  172. return child;
  173. }
  174. static inline int reqsk_queue_removed(struct request_sock_queue *queue,
  175. struct request_sock *req)
  176. {
  177. struct listen_sock *lopt = queue->listen_opt;
  178. if (req->retrans == 0)
  179. --lopt->qlen_young;
  180. return --lopt->qlen;
  181. }
  182. static inline int reqsk_queue_added(struct request_sock_queue *queue)
  183. {
  184. struct listen_sock *lopt = queue->listen_opt;
  185. const int prev_qlen = lopt->qlen;
  186. lopt->qlen_young++;
  187. lopt->qlen++;
  188. return prev_qlen;
  189. }
  190. static inline int reqsk_queue_len(const struct request_sock_queue *queue)
  191. {
  192. return queue->listen_opt != NULL ? queue->listen_opt->qlen : 0;
  193. }
  194. static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
  195. {
  196. return queue->listen_opt->qlen_young;
  197. }
  198. static inline int reqsk_queue_is_full(const struct request_sock_queue *queue)
  199. {
  200. return queue->listen_opt->qlen >> queue->listen_opt->max_qlen_log;
  201. }
  202. static inline void reqsk_queue_hash_req(struct request_sock_queue *queue,
  203. u32 hash, struct request_sock *req,
  204. unsigned long timeout)
  205. {
  206. struct listen_sock *lopt = queue->listen_opt;
  207. req->expires = jiffies + timeout;
  208. req->retrans = 0;
  209. req->sk = NULL;
  210. req->dl_next = lopt->syn_table[hash];
  211. write_lock(&queue->syn_wait_lock);
  212. lopt->syn_table[hash] = req;
  213. write_unlock(&queue->syn_wait_lock);
  214. }
  215. #endif /* _REQUEST_SOCK_H */