inet_connection_sock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/compiler.h>
  18. #include <linux/string.h>
  19. #include <linux/timer.h>
  20. #include <linux/poll.h>
  21. #include <net/inet_sock.h>
  22. #include <net/request_sock.h>
  23. #define INET_CSK_DEBUG 1
  24. /* Cancel timers, when they are not required. */
  25. #undef INET_CSK_CLEAR_TIMERS
  26. struct inet_bind_bucket;
  27. struct tcp_congestion_ops;
  28. /*
  29. * Pointers to address related TCP functions
  30. * (i.e. things that depend on the address family)
  31. */
  32. struct inet_connection_sock_af_ops {
  33. int (*queue_xmit)(struct sk_buff *skb, struct flowi *fl);
  34. void (*send_check)(struct sock *sk, struct sk_buff *skb);
  35. int (*rebuild_header)(struct sock *sk);
  36. void (*sk_rx_dst_set)(struct sock *sk, const struct sk_buff *skb);
  37. int (*conn_request)(struct sock *sk, struct sk_buff *skb);
  38. struct sock *(*syn_recv_sock)(struct sock *sk, struct sk_buff *skb,
  39. struct request_sock *req,
  40. struct dst_entry *dst);
  41. u16 net_header_len;
  42. u16 net_frag_header_len;
  43. u16 sockaddr_len;
  44. int (*setsockopt)(struct sock *sk, int level, int optname,
  45. char __user *optval, unsigned int optlen);
  46. int (*getsockopt)(struct sock *sk, int level, int optname,
  47. char __user *optval, int __user *optlen);
  48. #ifdef CONFIG_COMPAT
  49. int (*compat_setsockopt)(struct sock *sk,
  50. int level, int optname,
  51. char __user *optval, unsigned int optlen);
  52. int (*compat_getsockopt)(struct sock *sk,
  53. int level, int optname,
  54. char __user *optval, int __user *optlen);
  55. #endif
  56. void (*addr2sockaddr)(struct sock *sk, struct sockaddr *);
  57. int (*bind_conflict)(const struct sock *sk,
  58. const struct inet_bind_bucket *tb, bool relax);
  59. };
  60. /** inet_connection_sock - INET connection oriented sock
  61. *
  62. * @icsk_accept_queue: FIFO of established children
  63. * @icsk_bind_hash: Bind node
  64. * @icsk_timeout: Timeout
  65. * @icsk_retransmit_timer: Resend (no ack)
  66. * @icsk_rto: Retransmit timeout
  67. * @icsk_pmtu_cookie Last pmtu seen by socket
  68. * @icsk_ca_ops Pluggable congestion control hook
  69. * @icsk_af_ops Operations which are AF_INET{4,6} specific
  70. * @icsk_ca_state: Congestion control state
  71. * @icsk_retransmits: Number of unrecovered [RTO] timeouts
  72. * @icsk_pending: Scheduled timer event
  73. * @icsk_backoff: Backoff
  74. * @icsk_syn_retries: Number of allowed SYN (or equivalent) retries
  75. * @icsk_probes_out: unanswered 0 window probes
  76. * @icsk_ext_hdr_len: Network protocol overhead (IP/IPv6 options)
  77. * @icsk_ack: Delayed ACK control data
  78. * @icsk_mtup; MTU probing control data
  79. */
  80. struct inet_connection_sock {
  81. /* inet_sock has to be the first member! */
  82. struct inet_sock icsk_inet;
  83. struct request_sock_queue icsk_accept_queue;
  84. struct inet_bind_bucket *icsk_bind_hash;
  85. unsigned long icsk_timeout;
  86. struct timer_list icsk_retransmit_timer;
  87. struct timer_list icsk_delack_timer;
  88. __u32 icsk_rto;
  89. __u32 icsk_pmtu_cookie;
  90. const struct tcp_congestion_ops *icsk_ca_ops;
  91. const struct inet_connection_sock_af_ops *icsk_af_ops;
  92. unsigned int (*icsk_sync_mss)(struct sock *sk, u32 pmtu);
  93. __u8 icsk_ca_state;
  94. __u8 icsk_retransmits;
  95. __u8 icsk_pending;
  96. __u8 icsk_backoff;
  97. __u8 icsk_syn_retries;
  98. __u8 icsk_probes_out;
  99. __u16 icsk_ext_hdr_len;
  100. struct {
  101. __u8 pending; /* ACK is pending */
  102. __u8 quick; /* Scheduled number of quick acks */
  103. __u8 pingpong; /* The session is interactive */
  104. __u8 blocked; /* Delayed ACK was blocked by socket lock */
  105. __u32 ato; /* Predicted tick of soft clock */
  106. unsigned long timeout; /* Currently scheduled timeout */
  107. __u32 lrcvtime; /* timestamp of last received data packet */
  108. __u16 last_seg_size; /* Size of last incoming segment */
  109. __u16 rcv_mss; /* MSS used for delayed ACK decisions */
  110. } icsk_ack;
  111. struct {
  112. int enabled;
  113. /* Range of MTUs to search */
  114. int search_high;
  115. int search_low;
  116. /* Information on the current probe. */
  117. int probe_size;
  118. } icsk_mtup;
  119. u32 icsk_ca_priv[16];
  120. u32 icsk_user_timeout;
  121. #define ICSK_CA_PRIV_SIZE (16 * sizeof(u32))
  122. };
  123. #define ICSK_TIME_RETRANS 1 /* Retransmit timer */
  124. #define ICSK_TIME_DACK 2 /* Delayed ack timer */
  125. #define ICSK_TIME_PROBE0 3 /* Zero window probe timer */
  126. #define ICSK_TIME_EARLY_RETRANS 4 /* Early retransmit timer */
  127. #define ICSK_TIME_LOSS_PROBE 5 /* Tail loss probe timer */
  128. static inline struct inet_connection_sock *inet_csk(const struct sock *sk)
  129. {
  130. return (struct inet_connection_sock *)sk;
  131. }
  132. static inline void *inet_csk_ca(const struct sock *sk)
  133. {
  134. return (void *)inet_csk(sk)->icsk_ca_priv;
  135. }
  136. extern struct sock *inet_csk_clone_lock(const struct sock *sk,
  137. const struct request_sock *req,
  138. const gfp_t priority);
  139. enum inet_csk_ack_state_t {
  140. ICSK_ACK_SCHED = 1,
  141. ICSK_ACK_TIMER = 2,
  142. ICSK_ACK_PUSHED = 4,
  143. ICSK_ACK_PUSHED2 = 8
  144. };
  145. extern void inet_csk_init_xmit_timers(struct sock *sk,
  146. void (*retransmit_handler)(unsigned long),
  147. void (*delack_handler)(unsigned long),
  148. void (*keepalive_handler)(unsigned long));
  149. extern void inet_csk_clear_xmit_timers(struct sock *sk);
  150. static inline void inet_csk_schedule_ack(struct sock *sk)
  151. {
  152. inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_SCHED;
  153. }
  154. static inline int inet_csk_ack_scheduled(const struct sock *sk)
  155. {
  156. return inet_csk(sk)->icsk_ack.pending & ICSK_ACK_SCHED;
  157. }
  158. static inline void inet_csk_delack_init(struct sock *sk)
  159. {
  160. memset(&inet_csk(sk)->icsk_ack, 0, sizeof(inet_csk(sk)->icsk_ack));
  161. }
  162. extern void inet_csk_delete_keepalive_timer(struct sock *sk);
  163. extern void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long timeout);
  164. #ifdef INET_CSK_DEBUG
  165. extern const char inet_csk_timer_bug_msg[];
  166. #endif
  167. static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
  168. {
  169. struct inet_connection_sock *icsk = inet_csk(sk);
  170. if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
  171. icsk->icsk_pending = 0;
  172. #ifdef INET_CSK_CLEAR_TIMERS
  173. sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
  174. #endif
  175. } else if (what == ICSK_TIME_DACK) {
  176. icsk->icsk_ack.blocked = icsk->icsk_ack.pending = 0;
  177. #ifdef INET_CSK_CLEAR_TIMERS
  178. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  179. #endif
  180. }
  181. #ifdef INET_CSK_DEBUG
  182. else {
  183. pr_debug("%s", inet_csk_timer_bug_msg);
  184. }
  185. #endif
  186. }
  187. /*
  188. * Reset the retransmission timer
  189. */
  190. static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
  191. unsigned long when,
  192. const unsigned long max_when)
  193. {
  194. struct inet_connection_sock *icsk = inet_csk(sk);
  195. if (when > max_when) {
  196. #ifdef INET_CSK_DEBUG
  197. pr_debug("reset_xmit_timer: sk=%p %d when=0x%lx, caller=%p\n",
  198. sk, what, when, current_text_addr());
  199. #endif
  200. when = max_when;
  201. }
  202. if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0 ||
  203. what == ICSK_TIME_EARLY_RETRANS || what == ICSK_TIME_LOSS_PROBE) {
  204. icsk->icsk_pending = what;
  205. icsk->icsk_timeout = jiffies + when;
  206. sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
  207. } else if (what == ICSK_TIME_DACK) {
  208. icsk->icsk_ack.pending |= ICSK_ACK_TIMER;
  209. icsk->icsk_ack.timeout = jiffies + when;
  210. sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
  211. }
  212. #ifdef INET_CSK_DEBUG
  213. else {
  214. pr_debug("%s", inet_csk_timer_bug_msg);
  215. }
  216. #endif
  217. }
  218. extern struct sock *inet_csk_accept(struct sock *sk, int flags, int *err);
  219. extern struct request_sock *inet_csk_search_req(const struct sock *sk,
  220. struct request_sock ***prevp,
  221. const __be16 rport,
  222. const __be32 raddr,
  223. const __be32 laddr);
  224. extern int inet_csk_bind_conflict(const struct sock *sk,
  225. const struct inet_bind_bucket *tb, bool relax);
  226. extern int inet_csk_get_port(struct sock *sk, unsigned short snum);
  227. extern struct dst_entry* inet_csk_route_req(struct sock *sk,
  228. struct flowi4 *fl4,
  229. const struct request_sock *req);
  230. extern struct dst_entry* inet_csk_route_child_sock(struct sock *sk,
  231. struct sock *newsk,
  232. const struct request_sock *req);
  233. static inline void inet_csk_reqsk_queue_add(struct sock *sk,
  234. struct request_sock *req,
  235. struct sock *child)
  236. {
  237. reqsk_queue_add(&inet_csk(sk)->icsk_accept_queue, req, sk, child);
  238. }
  239. extern void inet_csk_reqsk_queue_hash_add(struct sock *sk,
  240. struct request_sock *req,
  241. unsigned long timeout);
  242. static inline void inet_csk_reqsk_queue_removed(struct sock *sk,
  243. struct request_sock *req)
  244. {
  245. if (reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req) == 0)
  246. inet_csk_delete_keepalive_timer(sk);
  247. }
  248. static inline void inet_csk_reqsk_queue_added(struct sock *sk,
  249. const unsigned long timeout)
  250. {
  251. if (reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue) == 0)
  252. inet_csk_reset_keepalive_timer(sk, timeout);
  253. }
  254. static inline int inet_csk_reqsk_queue_len(const struct sock *sk)
  255. {
  256. return reqsk_queue_len(&inet_csk(sk)->icsk_accept_queue);
  257. }
  258. static inline int inet_csk_reqsk_queue_young(const struct sock *sk)
  259. {
  260. return reqsk_queue_len_young(&inet_csk(sk)->icsk_accept_queue);
  261. }
  262. static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk)
  263. {
  264. return reqsk_queue_is_full(&inet_csk(sk)->icsk_accept_queue);
  265. }
  266. static inline void inet_csk_reqsk_queue_unlink(struct sock *sk,
  267. struct request_sock *req,
  268. struct request_sock **prev)
  269. {
  270. reqsk_queue_unlink(&inet_csk(sk)->icsk_accept_queue, req, prev);
  271. }
  272. static inline void inet_csk_reqsk_queue_drop(struct sock *sk,
  273. struct request_sock *req,
  274. struct request_sock **prev)
  275. {
  276. inet_csk_reqsk_queue_unlink(sk, req, prev);
  277. inet_csk_reqsk_queue_removed(sk, req);
  278. reqsk_free(req);
  279. }
  280. extern void inet_csk_reqsk_queue_prune(struct sock *parent,
  281. const unsigned long interval,
  282. const unsigned long timeout,
  283. const unsigned long max_rto);
  284. extern void inet_csk_destroy_sock(struct sock *sk);
  285. extern void inet_csk_prepare_forced_close(struct sock *sk);
  286. /*
  287. * LISTEN is a special case for poll..
  288. */
  289. static inline unsigned int inet_csk_listen_poll(const struct sock *sk)
  290. {
  291. return !reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue) ?
  292. (POLLIN | POLLRDNORM) : 0;
  293. }
  294. extern int inet_csk_listen_start(struct sock *sk, const int nr_table_entries);
  295. extern void inet_csk_listen_stop(struct sock *sk);
  296. extern void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr);
  297. extern int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  298. char __user *optval, int __user *optlen);
  299. extern int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  300. char __user *optval, unsigned int optlen);
  301. extern struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu);
  302. #endif /* _INET_CONNECTION_SOCK_H */