inet_connection_sock.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * NET Generic infrastructure for INET connection oriented protocols.
  3. *
  4. * Definitions for inet_connection_sock
  5. *
  6. * Authors: Many people, see the TCP sources
  7. *
  8. * From code originally in TCP
  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 _INET_CONNECTION_SOCK_H
  16. #define _INET_CONNECTION_SOCK_H
  17. #include <linux/ip.h>
  18. #include <linux/string.h>
  19. #include <linux/timer.h>
  20. #include <net/request_sock.h>
  21. #define INET_CSK_DEBUG 1
  22. /* Cancel timers, when they are not required. */
  23. #undef INET_CSK_CLEAR_TIMERS
  24. struct inet_bind_bucket;
  25. struct inet_hashinfo;
  26. struct tcp_congestion_ops;
  27. /** inet_connection_sock - INET connection oriented sock
  28. *
  29. * @icsk_accept_queue: FIFO of established children
  30. * @icsk_bind_hash: Bind node
  31. * @icsk_timeout: Timeout
  32. * @icsk_retransmit_timer: Resend (no ack)
  33. * @icsk_rto: Retransmit timeout
  34. * @icsk_ca_ops Pluggable congestion control hook
  35. * @icsk_ca_state: Congestion control state
  36. * @icsk_retransmits: Number of unrecovered [RTO] timeouts
  37. * @icsk_pending: Scheduled timer event
  38. * @icsk_backoff: Backoff
  39. * @icsk_syn_retries: Number of allowed SYN (or equivalent) retries
  40. * @icsk_probes_out: unanswered 0 window probes
  41. * @icsk_ack: Delayed ACK control data
  42. */
  43. struct inet_connection_sock {
  44. /* inet_sock has to be the first member! */
  45. struct inet_sock icsk_inet;
  46. struct request_sock_queue icsk_accept_queue;
  47. struct inet_bind_bucket *icsk_bind_hash;
  48. unsigned long icsk_timeout;
  49. struct timer_list icsk_retransmit_timer;
  50. struct timer_list icsk_delack_timer;
  51. __u32 icsk_rto;
  52. struct tcp_congestion_ops *icsk_ca_ops;
  53. __u8 icsk_ca_state;
  54. __u8 icsk_retransmits;
  55. __u8 icsk_pending;
  56. __u8 icsk_backoff;
  57. __u8 icsk_syn_retries;
  58. __u8 icsk_probes_out;
  59. /* 2 BYTES HOLE, TRY TO PACK! */
  60. struct {
  61. __u8 pending; /* ACK is pending */
  62. __u8 quick; /* Scheduled number of quick acks */
  63. __u8 pingpong; /* The session is interactive */
  64. __u8 blocked; /* Delayed ACK was blocked by socket lock */
  65. __u32 ato; /* Predicted tick of soft clock */
  66. unsigned long timeout; /* Currently scheduled timeout */
  67. __u32 lrcvtime; /* timestamp of last received data packet */
  68. __u16 last_seg_size; /* Size of last incoming segment */
  69. __u16 rcv_mss; /* MSS used for delayed ACK decisions */
  70. } icsk_ack;
  71. u32 icsk_ca_priv[16];
  72. #define ICSK_CA_PRIV_SIZE (16 * sizeof(u32))
  73. };
  74. #define ICSK_TIME_RETRANS 1 /* Retransmit timer */
  75. #define ICSK_TIME_DACK 2 /* Delayed ack timer */
  76. #define ICSK_TIME_PROBE0 3 /* Zero window probe timer */
  77. #define ICSK_TIME_KEEPOPEN 4 /* Keepalive timer */
  78. static inline struct inet_connection_sock *inet_csk(const struct sock *sk)
  79. {
  80. return (struct inet_connection_sock *)sk;
  81. }
  82. static inline void *inet_csk_ca(const struct sock *sk)
  83. {
  84. return (void *)inet_csk(sk)->icsk_ca_priv;
  85. }
  86. extern struct sock *inet_csk_clone(struct sock *sk,
  87. const struct request_sock *req,
  88. const unsigned int __nocast priority);
  89. enum inet_csk_ack_state_t {
  90. ICSK_ACK_SCHED = 1,
  91. ICSK_ACK_TIMER = 2,
  92. ICSK_ACK_PUSHED = 4
  93. };
  94. extern void inet_csk_init_xmit_timers(struct sock *sk,
  95. void (*retransmit_handler)(unsigned long),
  96. void (*delack_handler)(unsigned long),
  97. void (*keepalive_handler)(unsigned long));
  98. extern void inet_csk_clear_xmit_timers(struct sock *sk);
  99. static inline void inet_csk_schedule_ack(struct sock *sk)
  100. {
  101. inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_SCHED;
  102. }
  103. static inline int inet_csk_ack_scheduled(const struct sock *sk)
  104. {
  105. return inet_csk(sk)->icsk_ack.pending & ICSK_ACK_SCHED;
  106. }
  107. static inline void inet_csk_delack_init(struct sock *sk)
  108. {
  109. memset(&inet_csk(sk)->icsk_ack, 0, sizeof(inet_csk(sk)->icsk_ack));
  110. }
  111. extern void inet_csk_delete_keepalive_timer(struct sock *sk);
  112. extern void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long timeout);
  113. #ifdef INET_CSK_DEBUG
  114. extern const char inet_csk_timer_bug_msg[];
  115. #endif
  116. static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
  117. {
  118. struct inet_connection_sock *icsk = inet_csk(sk);
  119. if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
  120. icsk->icsk_pending = 0;
  121. #ifdef INET_CSK_CLEAR_TIMERS
  122. sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
  123. #endif
  124. } else if (what == ICSK_TIME_DACK) {
  125. icsk->icsk_ack.blocked = icsk->icsk_ack.pending = 0;
  126. #ifdef INET_CSK_CLEAR_TIMERS
  127. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  128. #endif
  129. }
  130. #ifdef INET_CSK_DEBUG
  131. else {
  132. pr_debug("%s", inet_csk_timer_bug_msg);
  133. }
  134. #endif
  135. }
  136. /*
  137. * Reset the retransmission timer
  138. */
  139. static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
  140. unsigned long when,
  141. const unsigned long max_when)
  142. {
  143. struct inet_connection_sock *icsk = inet_csk(sk);
  144. if (when > max_when) {
  145. #ifdef INET_CSK_DEBUG
  146. pr_debug("reset_xmit_timer: sk=%p %d when=0x%lx, caller=%p\n",
  147. sk, what, when, current_text_addr());
  148. #endif
  149. when = max_when;
  150. }
  151. if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
  152. icsk->icsk_pending = what;
  153. icsk->icsk_timeout = jiffies + when;
  154. sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
  155. } else if (what == ICSK_TIME_DACK) {
  156. icsk->icsk_ack.pending |= ICSK_ACK_TIMER;
  157. icsk->icsk_ack.timeout = jiffies + when;
  158. sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
  159. }
  160. #ifdef INET_CSK_DEBUG
  161. else {
  162. pr_debug("%s", inet_csk_timer_bug_msg);
  163. }
  164. #endif
  165. }
  166. extern struct sock *inet_csk_accept(struct sock *sk, int flags, int *err);
  167. extern struct request_sock *inet_csk_search_req(const struct sock *sk,
  168. struct request_sock ***prevp,
  169. const __u16 rport,
  170. const __u32 raddr,
  171. const __u32 laddr);
  172. extern int inet_csk_get_port(struct inet_hashinfo *hashinfo,
  173. struct sock *sk, unsigned short snum);
  174. extern struct dst_entry* inet_csk_route_req(struct sock *sk,
  175. const struct request_sock *req);
  176. static inline void inet_csk_reqsk_queue_add(struct sock *sk,
  177. struct request_sock *req,
  178. struct sock *child)
  179. {
  180. reqsk_queue_add(&inet_csk(sk)->icsk_accept_queue, req, sk, child);
  181. }
  182. extern void inet_csk_reqsk_queue_hash_add(struct sock *sk,
  183. struct request_sock *req,
  184. const unsigned timeout);
  185. static inline void inet_csk_reqsk_queue_removed(struct sock *sk,
  186. struct request_sock *req)
  187. {
  188. if (reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req) == 0)
  189. inet_csk_delete_keepalive_timer(sk);
  190. }
  191. static inline void inet_csk_reqsk_queue_added(struct sock *sk,
  192. const unsigned long timeout)
  193. {
  194. if (reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue) == 0)
  195. inet_csk_reset_keepalive_timer(sk, timeout);
  196. }
  197. static inline int inet_csk_reqsk_queue_len(const struct sock *sk)
  198. {
  199. return reqsk_queue_len(&inet_csk(sk)->icsk_accept_queue);
  200. }
  201. static inline int inet_csk_reqsk_queue_young(const struct sock *sk)
  202. {
  203. return reqsk_queue_len_young(&inet_csk(sk)->icsk_accept_queue);
  204. }
  205. static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk)
  206. {
  207. return reqsk_queue_is_full(&inet_csk(sk)->icsk_accept_queue);
  208. }
  209. static inline void inet_csk_reqsk_queue_unlink(struct sock *sk,
  210. struct request_sock *req,
  211. struct request_sock **prev)
  212. {
  213. reqsk_queue_unlink(&inet_csk(sk)->icsk_accept_queue, req, prev);
  214. }
  215. static inline void inet_csk_reqsk_queue_drop(struct sock *sk,
  216. struct request_sock *req,
  217. struct request_sock **prev)
  218. {
  219. inet_csk_reqsk_queue_unlink(sk, req, prev);
  220. inet_csk_reqsk_queue_removed(sk, req);
  221. reqsk_free(req);
  222. }
  223. extern void inet_csk_reqsk_queue_prune(struct sock *parent,
  224. const unsigned long interval,
  225. const unsigned long timeout,
  226. const unsigned long max_rto);
  227. extern void inet_csk_destroy_sock(struct sock *sk);
  228. /*
  229. * LISTEN is a special case for poll..
  230. */
  231. static inline unsigned int inet_csk_listen_poll(const struct sock *sk)
  232. {
  233. return !reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue) ?
  234. (POLLIN | POLLRDNORM) : 0;
  235. }
  236. extern int inet_csk_listen_start(struct sock *sk, const int nr_table_entries);
  237. extern void inet_csk_listen_stop(struct sock *sk);
  238. #endif /* _INET_CONNECTION_SOCK_H */