inet_connection_sock.h 10 KB

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