timer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * net/dccp/timer.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/dccp.h>
  13. #include <linux/skbuff.h>
  14. #include "dccp.h"
  15. /* sysctl variables governing numbers of retransmission attempts */
  16. int sysctl_dccp_request_retries __read_mostly = TCP_SYN_RETRIES;
  17. int sysctl_dccp_retries1 __read_mostly = TCP_RETR1;
  18. int sysctl_dccp_retries2 __read_mostly = TCP_RETR2;
  19. static void dccp_write_err(struct sock *sk)
  20. {
  21. sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
  22. sk->sk_error_report(sk);
  23. dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
  24. dccp_done(sk);
  25. DCCP_INC_STATS_BH(DCCP_MIB_ABORTONTIMEOUT);
  26. }
  27. /* A write timeout has occurred. Process the after effects. */
  28. static int dccp_write_timeout(struct sock *sk)
  29. {
  30. const struct inet_connection_sock *icsk = inet_csk(sk);
  31. int retry_until;
  32. if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) {
  33. if (icsk->icsk_retransmits != 0)
  34. dst_negative_advice(&sk->sk_dst_cache);
  35. retry_until = icsk->icsk_syn_retries ?
  36. : sysctl_dccp_request_retries;
  37. } else {
  38. if (icsk->icsk_retransmits >= sysctl_dccp_retries1) {
  39. /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu
  40. black hole detection. :-(
  41. It is place to make it. It is not made. I do not want
  42. to make it. It is disguisting. It does not work in any
  43. case. Let me to cite the same draft, which requires for
  44. us to implement this:
  45. "The one security concern raised by this memo is that ICMP black holes
  46. are often caused by over-zealous security administrators who block
  47. all ICMP messages. It is vitally important that those who design and
  48. deploy security systems understand the impact of strict filtering on
  49. upper-layer protocols. The safest web site in the world is worthless
  50. if most TCP implementations cannot transfer data from it. It would
  51. be far nicer to have all of the black holes fixed rather than fixing
  52. all of the TCP implementations."
  53. Golden words :-).
  54. */
  55. dst_negative_advice(&sk->sk_dst_cache);
  56. }
  57. retry_until = sysctl_dccp_retries2;
  58. /*
  59. * FIXME: see tcp_write_timout and tcp_out_of_resources
  60. */
  61. }
  62. if (icsk->icsk_retransmits >= retry_until) {
  63. /* Has it gone just too far? */
  64. dccp_write_err(sk);
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. /*
  70. * The DCCP retransmit timer.
  71. */
  72. static void dccp_retransmit_timer(struct sock *sk)
  73. {
  74. struct inet_connection_sock *icsk = inet_csk(sk);
  75. /* retransmit timer is used for feature negotiation throughout
  76. * connection. In this case, no packet is re-transmitted, but rather an
  77. * ack is generated and pending changes are placed into its options.
  78. */
  79. if (sk->sk_send_head == NULL) {
  80. dccp_pr_debug("feat negotiation retransmit timeout %p\n", sk);
  81. if (sk->sk_state == DCCP_OPEN)
  82. dccp_send_ack(sk);
  83. goto backoff;
  84. }
  85. /*
  86. * sk->sk_send_head has to have one skb with
  87. * DCCP_SKB_CB(skb)->dccpd_type set to one of the retransmittable DCCP
  88. * packet types. The only packets eligible for retransmission are:
  89. * -- Requests in client-REQUEST state (sec. 8.1.1)
  90. * -- Acks in client-PARTOPEN state (sec. 8.1.5)
  91. * -- CloseReq in server-CLOSEREQ state (sec. 8.3)
  92. * -- Close in node-CLOSING state (sec. 8.3) */
  93. BUG_TRAP(sk->sk_send_head != NULL);
  94. /*
  95. * More than than 4MSL (8 minutes) has passed, a RESET(aborted) was
  96. * sent, no need to retransmit, this sock is dead.
  97. */
  98. if (dccp_write_timeout(sk))
  99. goto out;
  100. /*
  101. * We want to know the number of packets retransmitted, not the
  102. * total number of retransmissions of clones of original packets.
  103. */
  104. if (icsk->icsk_retransmits == 0)
  105. DCCP_INC_STATS_BH(DCCP_MIB_TIMEOUTS);
  106. if (dccp_retransmit_skb(sk, sk->sk_send_head) < 0) {
  107. /*
  108. * Retransmission failed because of local congestion,
  109. * do not backoff.
  110. */
  111. if (icsk->icsk_retransmits == 0)
  112. icsk->icsk_retransmits = 1;
  113. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  114. min(icsk->icsk_rto,
  115. TCP_RESOURCE_PROBE_INTERVAL),
  116. DCCP_RTO_MAX);
  117. goto out;
  118. }
  119. backoff:
  120. icsk->icsk_backoff++;
  121. icsk->icsk_retransmits++;
  122. icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX);
  123. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto,
  124. DCCP_RTO_MAX);
  125. if (icsk->icsk_retransmits > sysctl_dccp_retries1)
  126. __sk_dst_reset(sk);
  127. out:;
  128. }
  129. static void dccp_write_timer(unsigned long data)
  130. {
  131. struct sock *sk = (struct sock *)data;
  132. struct inet_connection_sock *icsk = inet_csk(sk);
  133. int event = 0;
  134. bh_lock_sock(sk);
  135. if (sock_owned_by_user(sk)) {
  136. /* Try again later */
  137. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  138. jiffies + (HZ / 20));
  139. goto out;
  140. }
  141. if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending)
  142. goto out;
  143. if (time_after(icsk->icsk_timeout, jiffies)) {
  144. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  145. icsk->icsk_timeout);
  146. goto out;
  147. }
  148. event = icsk->icsk_pending;
  149. icsk->icsk_pending = 0;
  150. switch (event) {
  151. case ICSK_TIME_RETRANS:
  152. dccp_retransmit_timer(sk);
  153. break;
  154. }
  155. out:
  156. bh_unlock_sock(sk);
  157. sock_put(sk);
  158. }
  159. /*
  160. * Timer for listening sockets
  161. */
  162. static void dccp_response_timer(struct sock *sk)
  163. {
  164. inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, DCCP_TIMEOUT_INIT,
  165. DCCP_RTO_MAX);
  166. }
  167. static void dccp_keepalive_timer(unsigned long data)
  168. {
  169. struct sock *sk = (struct sock *)data;
  170. /* Only process if socket is not in use. */
  171. bh_lock_sock(sk);
  172. if (sock_owned_by_user(sk)) {
  173. /* Try again later. */
  174. inet_csk_reset_keepalive_timer(sk, HZ / 20);
  175. goto out;
  176. }
  177. if (sk->sk_state == DCCP_LISTEN) {
  178. dccp_response_timer(sk);
  179. goto out;
  180. }
  181. out:
  182. bh_unlock_sock(sk);
  183. sock_put(sk);
  184. }
  185. /* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */
  186. static void dccp_delack_timer(unsigned long data)
  187. {
  188. struct sock *sk = (struct sock *)data;
  189. struct inet_connection_sock *icsk = inet_csk(sk);
  190. bh_lock_sock(sk);
  191. if (sock_owned_by_user(sk)) {
  192. /* Try again later. */
  193. icsk->icsk_ack.blocked = 1;
  194. NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOCKED);
  195. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  196. jiffies + TCP_DELACK_MIN);
  197. goto out;
  198. }
  199. if (sk->sk_state == DCCP_CLOSED ||
  200. !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
  201. goto out;
  202. if (time_after(icsk->icsk_ack.timeout, jiffies)) {
  203. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  204. icsk->icsk_ack.timeout);
  205. goto out;
  206. }
  207. icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
  208. if (inet_csk_ack_scheduled(sk)) {
  209. if (!icsk->icsk_ack.pingpong) {
  210. /* Delayed ACK missed: inflate ATO. */
  211. icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1,
  212. icsk->icsk_rto);
  213. } else {
  214. /* Delayed ACK missed: leave pingpong mode and
  215. * deflate ATO.
  216. */
  217. icsk->icsk_ack.pingpong = 0;
  218. icsk->icsk_ack.ato = TCP_ATO_MIN;
  219. }
  220. dccp_send_ack(sk);
  221. NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKS);
  222. }
  223. out:
  224. bh_unlock_sock(sk);
  225. sock_put(sk);
  226. }
  227. /* Transmit-delay timer: used by the CCIDs to delay actual send time */
  228. static void dccp_write_xmit_timer(unsigned long data)
  229. {
  230. struct sock *sk = (struct sock *)data;
  231. struct dccp_sock *dp = dccp_sk(sk);
  232. bh_lock_sock(sk);
  233. if (sock_owned_by_user(sk))
  234. sk_reset_timer(sk, &dp->dccps_xmit_timer, jiffies+1);
  235. else
  236. dccp_write_xmit(sk, 0);
  237. bh_unlock_sock(sk);
  238. sock_put(sk);
  239. }
  240. static void dccp_init_write_xmit_timer(struct sock *sk)
  241. {
  242. struct dccp_sock *dp = dccp_sk(sk);
  243. init_timer(&dp->dccps_xmit_timer);
  244. dp->dccps_xmit_timer.data = (unsigned long)sk;
  245. dp->dccps_xmit_timer.function = dccp_write_xmit_timer;
  246. }
  247. void dccp_init_xmit_timers(struct sock *sk)
  248. {
  249. dccp_init_write_xmit_timer(sk);
  250. inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer,
  251. &dccp_keepalive_timer);
  252. }