timer.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/dccp/timer.c
  4. *
  5. * An implementation of the DCCP protocol
  6. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  7. */
  8. #include <linux/dccp.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/export.h>
  11. #include "dccp.h"
  12. /* sysctl variables governing numbers of retransmission attempts */
  13. int sysctl_dccp_request_retries __read_mostly = TCP_SYN_RETRIES;
  14. int sysctl_dccp_retries1 __read_mostly = TCP_RETR1;
  15. int sysctl_dccp_retries2 __read_mostly = TCP_RETR2;
  16. static void dccp_write_err(struct sock *sk)
  17. {
  18. sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
  19. sk->sk_error_report(sk);
  20. dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
  21. dccp_done(sk);
  22. __DCCP_INC_STATS(DCCP_MIB_ABORTONTIMEOUT);
  23. }
  24. /* A write timeout has occurred. Process the after effects. */
  25. static int dccp_write_timeout(struct sock *sk)
  26. {
  27. const struct inet_connection_sock *icsk = inet_csk(sk);
  28. int retry_until;
  29. if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) {
  30. if (icsk->icsk_retransmits != 0)
  31. dst_negative_advice(sk);
  32. retry_until = icsk->icsk_syn_retries ?
  33. : sysctl_dccp_request_retries;
  34. } else {
  35. if (icsk->icsk_retransmits >= sysctl_dccp_retries1) {
  36. /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu
  37. black hole detection. :-(
  38. It is place to make it. It is not made. I do not want
  39. to make it. It is disguisting. It does not work in any
  40. case. Let me to cite the same draft, which requires for
  41. us to implement this:
  42. "The one security concern raised by this memo is that ICMP black holes
  43. are often caused by over-zealous security administrators who block
  44. all ICMP messages. It is vitally important that those who design and
  45. deploy security systems understand the impact of strict filtering on
  46. upper-layer protocols. The safest web site in the world is worthless
  47. if most TCP implementations cannot transfer data from it. It would
  48. be far nicer to have all of the black holes fixed rather than fixing
  49. all of the TCP implementations."
  50. Golden words :-).
  51. */
  52. dst_negative_advice(sk);
  53. }
  54. retry_until = sysctl_dccp_retries2;
  55. /*
  56. * FIXME: see tcp_write_timout and tcp_out_of_resources
  57. */
  58. }
  59. if (icsk->icsk_retransmits >= retry_until) {
  60. /* Has it gone just too far? */
  61. dccp_write_err(sk);
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. /*
  67. * The DCCP retransmit timer.
  68. */
  69. static void dccp_retransmit_timer(struct sock *sk)
  70. {
  71. struct inet_connection_sock *icsk = inet_csk(sk);
  72. /*
  73. * More than 4MSL (8 minutes) has passed, a RESET(aborted) was
  74. * sent, no need to retransmit, this sock is dead.
  75. */
  76. if (dccp_write_timeout(sk))
  77. return;
  78. /*
  79. * We want to know the number of packets retransmitted, not the
  80. * total number of retransmissions of clones of original packets.
  81. */
  82. if (icsk->icsk_retransmits == 0)
  83. __DCCP_INC_STATS(DCCP_MIB_TIMEOUTS);
  84. if (dccp_retransmit_skb(sk) != 0) {
  85. /*
  86. * Retransmission failed because of local congestion,
  87. * do not backoff.
  88. */
  89. if (--icsk->icsk_retransmits == 0)
  90. icsk->icsk_retransmits = 1;
  91. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  92. min(icsk->icsk_rto,
  93. TCP_RESOURCE_PROBE_INTERVAL),
  94. DCCP_RTO_MAX);
  95. return;
  96. }
  97. icsk->icsk_backoff++;
  98. icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX);
  99. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto,
  100. DCCP_RTO_MAX);
  101. if (icsk->icsk_retransmits > sysctl_dccp_retries1)
  102. __sk_dst_reset(sk);
  103. }
  104. static void dccp_write_timer(struct timer_list *t)
  105. {
  106. struct inet_connection_sock *icsk =
  107. from_timer(icsk, t, icsk_retransmit_timer);
  108. struct sock *sk = &icsk->icsk_inet.sk;
  109. int event = 0;
  110. bh_lock_sock(sk);
  111. if (sock_owned_by_user(sk)) {
  112. /* Try again later */
  113. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  114. jiffies + (HZ / 20));
  115. goto out;
  116. }
  117. if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending)
  118. goto out;
  119. if (time_after(icsk->icsk_timeout, jiffies)) {
  120. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  121. icsk->icsk_timeout);
  122. goto out;
  123. }
  124. event = icsk->icsk_pending;
  125. icsk->icsk_pending = 0;
  126. switch (event) {
  127. case ICSK_TIME_RETRANS:
  128. dccp_retransmit_timer(sk);
  129. break;
  130. }
  131. out:
  132. bh_unlock_sock(sk);
  133. sock_put(sk);
  134. }
  135. static void dccp_keepalive_timer(struct timer_list *t)
  136. {
  137. struct sock *sk = from_timer(sk, t, sk_timer);
  138. pr_err("dccp should not use a keepalive timer !\n");
  139. sock_put(sk);
  140. }
  141. /* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */
  142. static void dccp_delack_timer(struct timer_list *t)
  143. {
  144. struct inet_connection_sock *icsk =
  145. from_timer(icsk, t, icsk_delack_timer);
  146. struct sock *sk = &icsk->icsk_inet.sk;
  147. bh_lock_sock(sk);
  148. if (sock_owned_by_user(sk)) {
  149. /* Try again later. */
  150. __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
  151. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  152. jiffies + TCP_DELACK_MIN);
  153. goto out;
  154. }
  155. if (sk->sk_state == DCCP_CLOSED ||
  156. !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
  157. goto out;
  158. if (time_after(icsk->icsk_ack.timeout, jiffies)) {
  159. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  160. icsk->icsk_ack.timeout);
  161. goto out;
  162. }
  163. icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
  164. if (inet_csk_ack_scheduled(sk)) {
  165. if (!inet_csk_in_pingpong_mode(sk)) {
  166. /* Delayed ACK missed: inflate ATO. */
  167. icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1,
  168. icsk->icsk_rto);
  169. } else {
  170. /* Delayed ACK missed: leave pingpong mode and
  171. * deflate ATO.
  172. */
  173. inet_csk_exit_pingpong_mode(sk);
  174. icsk->icsk_ack.ato = TCP_ATO_MIN;
  175. }
  176. dccp_send_ack(sk);
  177. __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
  178. }
  179. out:
  180. bh_unlock_sock(sk);
  181. sock_put(sk);
  182. }
  183. /**
  184. * dccp_write_xmitlet - Workhorse for CCID packet dequeueing interface
  185. * @data: Socket to act on
  186. *
  187. * See the comments above %ccid_dequeueing_decision for supported modes.
  188. */
  189. static void dccp_write_xmitlet(unsigned long data)
  190. {
  191. struct sock *sk = (struct sock *)data;
  192. bh_lock_sock(sk);
  193. if (sock_owned_by_user(sk))
  194. sk_reset_timer(sk, &dccp_sk(sk)->dccps_xmit_timer, jiffies + 1);
  195. else
  196. dccp_write_xmit(sk);
  197. bh_unlock_sock(sk);
  198. sock_put(sk);
  199. }
  200. static void dccp_write_xmit_timer(struct timer_list *t)
  201. {
  202. struct dccp_sock *dp = from_timer(dp, t, dccps_xmit_timer);
  203. struct sock *sk = &dp->dccps_inet_connection.icsk_inet.sk;
  204. dccp_write_xmitlet((unsigned long)sk);
  205. }
  206. void dccp_init_xmit_timers(struct sock *sk)
  207. {
  208. struct dccp_sock *dp = dccp_sk(sk);
  209. tasklet_init(&dp->dccps_xmitlet, dccp_write_xmitlet, (unsigned long)sk);
  210. timer_setup(&dp->dccps_xmit_timer, dccp_write_xmit_timer, 0);
  211. inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer,
  212. &dccp_keepalive_timer);
  213. }
  214. static ktime_t dccp_timestamp_seed;
  215. /**
  216. * dccp_timestamp - 10s of microseconds time source
  217. * Returns the number of 10s of microseconds since loading DCCP. This is native
  218. * DCCP time difference format (RFC 4340, sec. 13).
  219. * Please note: This will wrap around about circa every 11.9 hours.
  220. */
  221. u32 dccp_timestamp(void)
  222. {
  223. u64 delta = (u64)ktime_us_delta(ktime_get_real(), dccp_timestamp_seed);
  224. do_div(delta, 10);
  225. return delta;
  226. }
  227. EXPORT_SYMBOL_GPL(dccp_timestamp);
  228. void __init dccp_timestamping_init(void)
  229. {
  230. dccp_timestamp_seed = ktime_get_real();
  231. }