inet_connection_sock.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /* Cancel timers, when they are not required. */
  24. #undef INET_CSK_CLEAR_TIMERS
  25. struct inet_bind_bucket;
  26. struct tcp_congestion_ops;
  27. /*
  28. * Pointers to address related TCP functions
  29. * (i.e. things that depend on the address family)
  30. */
  31. struct inet_connection_sock_af_ops {
  32. int (*queue_xmit)(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
  33. void (*send_check)(struct sock *sk, struct sk_buff *skb);
  34. int (*rebuild_header)(struct sock *sk);
  35. void (*sk_rx_dst_set)(struct sock *sk, const struct sk_buff *skb);
  36. int (*conn_request)(struct sock *sk, struct sk_buff *skb);
  37. struct sock *(*syn_recv_sock)(const struct sock *sk, struct sk_buff *skb,
  38. struct request_sock *req,
  39. struct dst_entry *dst,
  40. struct request_sock *req_unhash,
  41. bool *own_req);
  42. u16 net_header_len;
  43. u16 net_frag_header_len;
  44. u16 sockaddr_len;
  45. int (*setsockopt)(struct sock *sk, int level, int optname,
  46. char __user *optval, unsigned int optlen);
  47. int (*getsockopt)(struct sock *sk, int level, int optname,
  48. char __user *optval, int __user *optlen);
  49. #ifdef CONFIG_COMPAT
  50. int (*compat_setsockopt)(struct sock *sk,
  51. int level, int optname,
  52. char __user *optval, unsigned int optlen);
  53. int (*compat_getsockopt)(struct sock *sk,
  54. int level, int optname,
  55. char __user *optval, int __user *optlen);
  56. #endif
  57. void (*addr2sockaddr)(struct sock *sk, struct sockaddr *);
  58. void (*mtu_reduced)(struct sock *sk);
  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_ulp_ops Pluggable ULP control hook
  71. * @icsk_ulp_data ULP private data
  72. * @icsk_clean_acked Clean acked data hook
  73. * @icsk_listen_portaddr_node hash to the portaddr listener hashtable
  74. * @icsk_ca_state: Congestion control state
  75. * @icsk_retransmits: Number of unrecovered [RTO] timeouts
  76. * @icsk_pending: Scheduled timer event
  77. * @icsk_backoff: Backoff
  78. * @icsk_syn_retries: Number of allowed SYN (or equivalent) retries
  79. * @icsk_probes_out: unanswered 0 window probes
  80. * @icsk_ext_hdr_len: Network protocol overhead (IP/IPv6 options)
  81. * @icsk_ack: Delayed ACK control data
  82. * @icsk_mtup; MTU probing control data
  83. */
  84. struct inet_connection_sock {
  85. /* inet_sock has to be the first member! */
  86. struct inet_sock icsk_inet;
  87. struct request_sock_queue icsk_accept_queue;
  88. struct inet_bind_bucket *icsk_bind_hash;
  89. unsigned long icsk_timeout;
  90. struct timer_list icsk_retransmit_timer;
  91. struct timer_list icsk_delack_timer;
  92. __u32 icsk_rto;
  93. __u32 icsk_pmtu_cookie;
  94. const struct tcp_congestion_ops *icsk_ca_ops;
  95. const struct inet_connection_sock_af_ops *icsk_af_ops;
  96. const struct tcp_ulp_ops *icsk_ulp_ops;
  97. void *icsk_ulp_data;
  98. void (*icsk_clean_acked)(struct sock *sk, u32 acked_seq);
  99. struct hlist_node icsk_listen_portaddr_node;
  100. unsigned int (*icsk_sync_mss)(struct sock *sk, u32 pmtu);
  101. __u8 icsk_ca_state:6,
  102. icsk_ca_setsockopt:1,
  103. icsk_ca_dst_locked:1;
  104. __u8 icsk_retransmits;
  105. __u8 icsk_pending;
  106. __u8 icsk_backoff;
  107. __u8 icsk_syn_retries;
  108. __u8 icsk_probes_out;
  109. __u16 icsk_ext_hdr_len;
  110. struct {
  111. __u8 pending; /* ACK is pending */
  112. __u8 quick; /* Scheduled number of quick acks */
  113. __u8 pingpong; /* The session is interactive */
  114. __u8 blocked; /* Delayed ACK was blocked by socket lock */
  115. __u32 ato; /* Predicted tick of soft clock */
  116. unsigned long timeout; /* Currently scheduled timeout */
  117. __u32 lrcvtime; /* timestamp of last received data packet */
  118. __u16 last_seg_size; /* Size of last incoming segment */
  119. __u16 rcv_mss; /* MSS used for delayed ACK decisions */
  120. } icsk_ack;
  121. struct {
  122. int enabled;
  123. /* Range of MTUs to search */
  124. int search_high;
  125. int search_low;
  126. /* Information on the current probe. */
  127. int probe_size;
  128. u32 probe_timestamp;
  129. } icsk_mtup;
  130. u32 icsk_user_timeout;
  131. u64 icsk_ca_priv[88 / sizeof(u64)];
  132. #define ICSK_CA_PRIV_SIZE (11 * sizeof(u64))
  133. };
  134. #define ICSK_TIME_RETRANS 1 /* Retransmit timer */
  135. #define ICSK_TIME_DACK 2 /* Delayed ack timer */
  136. #define ICSK_TIME_PROBE0 3 /* Zero window probe timer */
  137. #define ICSK_TIME_EARLY_RETRANS 4 /* Early retransmit timer */
  138. #define ICSK_TIME_LOSS_PROBE 5 /* Tail loss probe timer */
  139. #define ICSK_TIME_REO_TIMEOUT 6 /* Reordering timer */
  140. static inline struct inet_connection_sock *inet_csk(const struct sock *sk)
  141. {
  142. return (struct inet_connection_sock *)sk;
  143. }
  144. static inline void *inet_csk_ca(const struct sock *sk)
  145. {
  146. return (void *)inet_csk(sk)->icsk_ca_priv;
  147. }
  148. struct sock *inet_csk_clone_lock(const struct sock *sk,
  149. const struct request_sock *req,
  150. const gfp_t priority);
  151. enum inet_csk_ack_state_t {
  152. ICSK_ACK_SCHED = 1,
  153. ICSK_ACK_TIMER = 2,
  154. ICSK_ACK_PUSHED = 4,
  155. ICSK_ACK_PUSHED2 = 8
  156. };
  157. void inet_csk_init_xmit_timers(struct sock *sk,
  158. void (*retransmit_handler)(struct timer_list *),
  159. void (*delack_handler)(struct timer_list *),
  160. void (*keepalive_handler)(struct timer_list *));
  161. void inet_csk_clear_xmit_timers(struct sock *sk);
  162. static inline void inet_csk_schedule_ack(struct sock *sk)
  163. {
  164. inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_SCHED;
  165. }
  166. static inline int inet_csk_ack_scheduled(const struct sock *sk)
  167. {
  168. return inet_csk(sk)->icsk_ack.pending & ICSK_ACK_SCHED;
  169. }
  170. static inline void inet_csk_delack_init(struct sock *sk)
  171. {
  172. memset(&inet_csk(sk)->icsk_ack, 0, sizeof(inet_csk(sk)->icsk_ack));
  173. }
  174. void inet_csk_delete_keepalive_timer(struct sock *sk);
  175. void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long timeout);
  176. static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
  177. {
  178. struct inet_connection_sock *icsk = inet_csk(sk);
  179. if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
  180. icsk->icsk_pending = 0;
  181. #ifdef INET_CSK_CLEAR_TIMERS
  182. sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
  183. #endif
  184. } else if (what == ICSK_TIME_DACK) {
  185. icsk->icsk_ack.blocked = icsk->icsk_ack.pending = 0;
  186. #ifdef INET_CSK_CLEAR_TIMERS
  187. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  188. #endif
  189. } else {
  190. pr_debug("inet_csk BUG: unknown timer value\n");
  191. }
  192. }
  193. /*
  194. * Reset the retransmission timer
  195. */
  196. static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
  197. unsigned long when,
  198. const unsigned long max_when)
  199. {
  200. struct inet_connection_sock *icsk = inet_csk(sk);
  201. if (when > max_when) {
  202. pr_debug("reset_xmit_timer: sk=%p %d when=0x%lx, caller=%p\n",
  203. sk, what, when, current_text_addr());
  204. when = max_when;
  205. }
  206. if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0 ||
  207. what == ICSK_TIME_EARLY_RETRANS || what == ICSK_TIME_LOSS_PROBE ||
  208. what == ICSK_TIME_REO_TIMEOUT) {
  209. icsk->icsk_pending = what;
  210. icsk->icsk_timeout = jiffies + when;
  211. sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
  212. } else if (what == ICSK_TIME_DACK) {
  213. icsk->icsk_ack.pending |= ICSK_ACK_TIMER;
  214. icsk->icsk_ack.timeout = jiffies + when;
  215. sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
  216. } else {
  217. pr_debug("inet_csk BUG: unknown timer value\n");
  218. }
  219. }
  220. static inline unsigned long
  221. inet_csk_rto_backoff(const struct inet_connection_sock *icsk,
  222. unsigned long max_when)
  223. {
  224. u64 when = (u64)icsk->icsk_rto << icsk->icsk_backoff;
  225. return (unsigned long)min_t(u64, when, max_when);
  226. }
  227. struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern);
  228. int inet_csk_get_port(struct sock *sk, unsigned short snum);
  229. struct dst_entry *inet_csk_route_req(const struct sock *sk, struct flowi4 *fl4,
  230. const struct request_sock *req);
  231. struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
  232. struct sock *newsk,
  233. const struct request_sock *req);
  234. struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
  235. struct request_sock *req,
  236. struct sock *child);
  237. void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
  238. unsigned long timeout);
  239. struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
  240. struct request_sock *req,
  241. bool own_req);
  242. static inline void inet_csk_reqsk_queue_added(struct sock *sk)
  243. {
  244. reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue);
  245. }
  246. static inline int inet_csk_reqsk_queue_len(const struct sock *sk)
  247. {
  248. return reqsk_queue_len(&inet_csk(sk)->icsk_accept_queue);
  249. }
  250. static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk)
  251. {
  252. return inet_csk_reqsk_queue_len(sk) >= sk->sk_max_ack_backlog;
  253. }
  254. void inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req);
  255. void inet_csk_reqsk_queue_drop_and_put(struct sock *sk, struct request_sock *req);
  256. void inet_csk_destroy_sock(struct sock *sk);
  257. void inet_csk_prepare_forced_close(struct sock *sk);
  258. /*
  259. * LISTEN is a special case for poll..
  260. */
  261. static inline __poll_t inet_csk_listen_poll(const struct sock *sk)
  262. {
  263. return !reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue) ?
  264. (EPOLLIN | EPOLLRDNORM) : 0;
  265. }
  266. int inet_csk_listen_start(struct sock *sk, int backlog);
  267. void inet_csk_listen_stop(struct sock *sk);
  268. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr);
  269. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  270. char __user *optval, int __user *optlen);
  271. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  272. char __user *optval, unsigned int optlen);
  273. struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu);
  274. #endif /* _INET_CONNECTION_SOCK_H */