inet_connection_sock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * NET Generic infrastructure for INET connection oriented protocols.
  4. *
  5. * Definitions for inet_connection_sock
  6. *
  7. * Authors: Many people, see the TCP sources
  8. *
  9. * From code originally in TCP
  10. */
  11. #ifndef _INET_CONNECTION_SOCK_H
  12. #define _INET_CONNECTION_SOCK_H
  13. #include <linux/compiler.h>
  14. #include <linux/string.h>
  15. #include <linux/timer.h>
  16. #include <linux/poll.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sockptr.h>
  19. #include <net/inet_sock.h>
  20. #include <net/request_sock.h>
  21. /* Cancel timers, when they are not required. */
  22. #undef INET_CSK_CLEAR_TIMERS
  23. struct inet_bind_bucket;
  24. struct inet_bind2_bucket;
  25. struct tcp_congestion_ops;
  26. /*
  27. * Pointers to address related TCP functions
  28. * (i.e. things that depend on the address family)
  29. */
  30. struct inet_connection_sock_af_ops {
  31. int (*queue_xmit)(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
  32. void (*send_check)(struct sock *sk, struct sk_buff *skb);
  33. int (*rebuild_header)(struct sock *sk);
  34. void (*sk_rx_dst_set)(struct sock *sk, const struct sk_buff *skb);
  35. int (*conn_request)(struct sock *sk, struct sk_buff *skb);
  36. struct sock *(*syn_recv_sock)(const struct sock *sk, struct sk_buff *skb,
  37. struct request_sock *req,
  38. struct dst_entry *dst,
  39. struct request_sock *req_unhash,
  40. bool *own_req);
  41. u16 net_header_len;
  42. u16 sockaddr_len;
  43. int (*setsockopt)(struct sock *sk, int level, int optname,
  44. sockptr_t optval, unsigned int optlen);
  45. int (*getsockopt)(struct sock *sk, int level, int optname,
  46. char __user *optval, int __user *optlen);
  47. void (*addr2sockaddr)(struct sock *sk, struct sockaddr *);
  48. void (*mtu_reduced)(struct sock *sk);
  49. };
  50. /** inet_connection_sock - INET connection oriented sock
  51. *
  52. * @icsk_accept_queue: FIFO of established children
  53. * @icsk_bind_hash: Bind node
  54. * @icsk_bind2_hash: Bind node in the bhash2 table
  55. * @icsk_timeout: Timeout
  56. * @icsk_retransmit_timer: Resend (no ack)
  57. * @icsk_rto: Retransmit timeout
  58. * @icsk_pmtu_cookie Last pmtu seen by socket
  59. * @icsk_ca_ops Pluggable congestion control hook
  60. * @icsk_af_ops Operations which are AF_INET{4,6} specific
  61. * @icsk_ulp_ops Pluggable ULP control hook
  62. * @icsk_ulp_data ULP private data
  63. * @icsk_clean_acked Clean acked data hook
  64. * @icsk_ca_state: Congestion control state
  65. * @icsk_retransmits: Number of unrecovered [RTO] timeouts
  66. * @icsk_pending: Scheduled timer event
  67. * @icsk_backoff: Backoff
  68. * @icsk_syn_retries: Number of allowed SYN (or equivalent) retries
  69. * @icsk_probes_out: unanswered 0 window probes
  70. * @icsk_ext_hdr_len: Network protocol overhead (IP/IPv6 options)
  71. * @icsk_ack: Delayed ACK control data
  72. * @icsk_mtup; MTU probing control data
  73. * @icsk_probes_tstamp: Probe timestamp (cleared by non-zero window ack)
  74. * @icsk_user_timeout: TCP_USER_TIMEOUT value
  75. */
  76. struct inet_connection_sock {
  77. /* inet_sock has to be the first member! */
  78. struct inet_sock icsk_inet;
  79. struct request_sock_queue icsk_accept_queue;
  80. struct inet_bind_bucket *icsk_bind_hash;
  81. struct inet_bind2_bucket *icsk_bind2_hash;
  82. unsigned long icsk_timeout;
  83. struct timer_list icsk_retransmit_timer;
  84. struct timer_list icsk_delack_timer;
  85. __u32 icsk_rto;
  86. __u32 icsk_rto_min;
  87. __u32 icsk_delack_max;
  88. __u32 icsk_pmtu_cookie;
  89. const struct tcp_congestion_ops *icsk_ca_ops;
  90. const struct inet_connection_sock_af_ops *icsk_af_ops;
  91. const struct tcp_ulp_ops *icsk_ulp_ops;
  92. void __rcu *icsk_ulp_data;
  93. void (*icsk_clean_acked)(struct sock *sk, u32 acked_seq);
  94. unsigned int (*icsk_sync_mss)(struct sock *sk, u32 pmtu);
  95. __u8 icsk_ca_state:5,
  96. icsk_ca_initialized:1,
  97. icsk_ca_setsockopt:1,
  98. icsk_ca_dst_locked:1;
  99. __u8 icsk_retransmits;
  100. __u8 icsk_pending;
  101. __u8 icsk_backoff;
  102. __u8 icsk_syn_retries;
  103. __u8 icsk_probes_out;
  104. __u16 icsk_ext_hdr_len;
  105. struct {
  106. __u8 pending; /* ACK is pending */
  107. __u8 quick; /* Scheduled number of quick acks */
  108. __u8 pingpong; /* The session is interactive */
  109. __u8 retry; /* Number of attempts */
  110. #define ATO_BITS 8
  111. __u32 ato:ATO_BITS, /* Predicted tick of soft clock */
  112. lrcv_flowlabel:20, /* last received ipv6 flowlabel */
  113. dst_quick_ack:1, /* cache dst RTAX_QUICKACK */
  114. unused:3;
  115. unsigned long timeout; /* Currently scheduled timeout */
  116. __u32 lrcvtime; /* timestamp of last received data packet */
  117. __u16 last_seg_size; /* Size of last incoming segment */
  118. __u16 rcv_mss; /* MSS used for delayed ACK decisions */
  119. } icsk_ack;
  120. struct {
  121. /* Range of MTUs to search */
  122. int search_high;
  123. int search_low;
  124. /* Information on the current probe. */
  125. u32 probe_size:31,
  126. /* Is the MTUP feature enabled for this connection? */
  127. enabled:1;
  128. u32 probe_timestamp;
  129. } icsk_mtup;
  130. u32 icsk_probes_tstamp;
  131. u32 icsk_user_timeout;
  132. u64 icsk_ca_priv[104 / sizeof(u64)];
  133. #define ICSK_CA_PRIV_SIZE sizeof_field(struct inet_connection_sock, icsk_ca_priv)
  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_LOSS_PROBE 5 /* Tail loss probe timer */
  139. #define ICSK_TIME_REO_TIMEOUT 6 /* Reordering timer */
  140. #define inet_csk(ptr) container_of_const(ptr, struct inet_connection_sock, icsk_inet.sk)
  141. static inline void *inet_csk_ca(const struct sock *sk)
  142. {
  143. return (void *)inet_csk(sk)->icsk_ca_priv;
  144. }
  145. struct sock *inet_csk_clone_lock(const struct sock *sk,
  146. const struct request_sock *req,
  147. const gfp_t priority);
  148. enum inet_csk_ack_state_t {
  149. ICSK_ACK_SCHED = 1,
  150. ICSK_ACK_TIMER = 2,
  151. ICSK_ACK_PUSHED = 4,
  152. ICSK_ACK_PUSHED2 = 8,
  153. ICSK_ACK_NOW = 16, /* Send the next ACK immediately (once) */
  154. ICSK_ACK_NOMEM = 32,
  155. };
  156. void inet_csk_init_xmit_timers(struct sock *sk,
  157. void (*retransmit_handler)(struct timer_list *),
  158. void (*delack_handler)(struct timer_list *),
  159. void (*keepalive_handler)(struct timer_list *));
  160. void inet_csk_clear_xmit_timers(struct sock *sk);
  161. void inet_csk_clear_xmit_timers_sync(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.pending = 0;
  186. icsk->icsk_ack.retry = 0;
  187. #ifdef INET_CSK_CLEAR_TIMERS
  188. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  189. #endif
  190. } else {
  191. pr_debug("inet_csk BUG: unknown timer value\n");
  192. }
  193. }
  194. /*
  195. * Reset the retransmission timer
  196. */
  197. static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
  198. unsigned long when,
  199. const unsigned long max_when)
  200. {
  201. struct inet_connection_sock *icsk = inet_csk(sk);
  202. if (when > max_when) {
  203. pr_debug("reset_xmit_timer: sk=%p %d when=0x%lx, caller=%p\n",
  204. sk, what, when, (void *)_THIS_IP_);
  205. when = max_when;
  206. }
  207. if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0 ||
  208. what == ICSK_TIME_LOSS_PROBE || 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, struct proto_accept_arg *arg);
  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. bool 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) > READ_ONCE(sk->sk_max_ack_backlog);
  253. }
  254. bool 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. static inline unsigned long
  257. reqsk_timeout(struct request_sock *req, unsigned long max_timeout)
  258. {
  259. u64 timeout = (u64)req->timeout << req->num_timeout;
  260. return (unsigned long)min_t(u64, timeout, max_timeout);
  261. }
  262. static inline void inet_csk_prepare_for_destroy_sock(struct sock *sk)
  263. {
  264. /* The below has to be done to allow calling inet_csk_destroy_sock */
  265. sock_set_flag(sk, SOCK_DEAD);
  266. this_cpu_inc(*sk->sk_prot->orphan_count);
  267. }
  268. void inet_csk_destroy_sock(struct sock *sk);
  269. void inet_csk_prepare_forced_close(struct sock *sk);
  270. /*
  271. * LISTEN is a special case for poll..
  272. */
  273. static inline __poll_t inet_csk_listen_poll(const struct sock *sk)
  274. {
  275. return !reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue) ?
  276. (EPOLLIN | EPOLLRDNORM) : 0;
  277. }
  278. int inet_csk_listen_start(struct sock *sk);
  279. void inet_csk_listen_stop(struct sock *sk);
  280. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr);
  281. /* update the fast reuse flag when adding a socket */
  282. void inet_csk_update_fastreuse(struct inet_bind_bucket *tb,
  283. struct sock *sk);
  284. struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu);
  285. static inline void inet_csk_enter_pingpong_mode(struct sock *sk)
  286. {
  287. inet_csk(sk)->icsk_ack.pingpong =
  288. READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pingpong_thresh);
  289. }
  290. static inline void inet_csk_exit_pingpong_mode(struct sock *sk)
  291. {
  292. inet_csk(sk)->icsk_ack.pingpong = 0;
  293. }
  294. static inline bool inet_csk_in_pingpong_mode(struct sock *sk)
  295. {
  296. return inet_csk(sk)->icsk_ack.pingpong >=
  297. READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pingpong_thresh);
  298. }
  299. static inline void inet_csk_inc_pingpong_cnt(struct sock *sk)
  300. {
  301. struct inet_connection_sock *icsk = inet_csk(sk);
  302. if (icsk->icsk_ack.pingpong < U8_MAX)
  303. icsk->icsk_ack.pingpong++;
  304. }
  305. static inline bool inet_csk_has_ulp(const struct sock *sk)
  306. {
  307. return inet_test_bit(IS_ICSK, sk) && !!inet_csk(sk)->icsk_ulp_ops;
  308. }
  309. static inline void inet_init_csk_locks(struct sock *sk)
  310. {
  311. struct inet_connection_sock *icsk = inet_csk(sk);
  312. spin_lock_init(&icsk->icsk_accept_queue.rskq_lock);
  313. spin_lock_init(&icsk->icsk_accept_queue.fastopenq.lock);
  314. }
  315. #endif /* _INET_CONNECTION_SOCK_H */