timer.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/config.h>
  13. #include <linux/dccp.h>
  14. #include <linux/skbuff.h>
  15. #include "dccp.h"
  16. static void dccp_write_timer(unsigned long data);
  17. static void dccp_keepalive_timer(unsigned long data);
  18. static void dccp_delack_timer(unsigned long data);
  19. void dccp_init_xmit_timers(struct sock *sk)
  20. {
  21. inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer,
  22. &dccp_keepalive_timer);
  23. }
  24. static void dccp_write_err(struct sock *sk)
  25. {
  26. sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
  27. sk->sk_error_report(sk);
  28. dccp_v4_send_reset(sk, DCCP_RESET_CODE_ABORTED);
  29. dccp_done(sk);
  30. DCCP_INC_STATS_BH(DCCP_MIB_ABORTONTIMEOUT);
  31. }
  32. /* A write timeout has occurred. Process the after effects. */
  33. static int dccp_write_timeout(struct sock *sk)
  34. {
  35. const struct inet_connection_sock *icsk = inet_csk(sk);
  36. int retry_until;
  37. if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) {
  38. if (icsk->icsk_retransmits != 0)
  39. dst_negative_advice(&sk->sk_dst_cache);
  40. retry_until = icsk->icsk_syn_retries ? :
  41. /* FIXME! */ 3 /* FIXME! sysctl_tcp_syn_retries */;
  42. } else {
  43. if (icsk->icsk_retransmits >=
  44. /* FIXME! sysctl_tcp_retries1 */ 5 /* FIXME! */) {
  45. /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu
  46. black hole detection. :-(
  47. It is place to make it. It is not made. I do not want
  48. to make it. It is disguisting. It does not work in any
  49. case. Let me to cite the same draft, which requires for
  50. us to implement this:
  51. "The one security concern raised by this memo is that ICMP black holes
  52. are often caused by over-zealous security administrators who block
  53. all ICMP messages. It is vitally important that those who design and
  54. deploy security systems understand the impact of strict filtering on
  55. upper-layer protocols. The safest web site in the world is worthless
  56. if most TCP implementations cannot transfer data from it. It would
  57. be far nicer to have all of the black holes fixed rather than fixing
  58. all of the TCP implementations."
  59. Golden words :-).
  60. */
  61. dst_negative_advice(&sk->sk_dst_cache);
  62. }
  63. retry_until = /* FIXME! */ 15 /* FIXME! sysctl_tcp_retries2 */;
  64. /*
  65. * FIXME: see tcp_write_timout and tcp_out_of_resources
  66. */
  67. }
  68. if (icsk->icsk_retransmits >= retry_until) {
  69. /* Has it gone just too far? */
  70. dccp_write_err(sk);
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. /* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */
  76. static void dccp_delack_timer(unsigned long data)
  77. {
  78. struct sock *sk = (struct sock *)data;
  79. struct inet_connection_sock *icsk = inet_csk(sk);
  80. bh_lock_sock(sk);
  81. if (sock_owned_by_user(sk)) {
  82. /* Try again later. */
  83. icsk->icsk_ack.blocked = 1;
  84. NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOCKED);
  85. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  86. jiffies + TCP_DELACK_MIN);
  87. goto out;
  88. }
  89. if (sk->sk_state == DCCP_CLOSED ||
  90. !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
  91. goto out;
  92. if (time_after(icsk->icsk_ack.timeout, jiffies)) {
  93. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  94. icsk->icsk_ack.timeout);
  95. goto out;
  96. }
  97. icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
  98. if (inet_csk_ack_scheduled(sk)) {
  99. if (!icsk->icsk_ack.pingpong) {
  100. /* Delayed ACK missed: inflate ATO. */
  101. icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1,
  102. icsk->icsk_rto);
  103. } else {
  104. /* Delayed ACK missed: leave pingpong mode and
  105. * deflate ATO.
  106. */
  107. icsk->icsk_ack.pingpong = 0;
  108. icsk->icsk_ack.ato = TCP_ATO_MIN;
  109. }
  110. dccp_send_ack(sk);
  111. NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKS);
  112. }
  113. out:
  114. bh_unlock_sock(sk);
  115. sock_put(sk);
  116. }
  117. /*
  118. * The DCCP retransmit timer.
  119. */
  120. static void dccp_retransmit_timer(struct sock *sk)
  121. {
  122. struct inet_connection_sock *icsk = inet_csk(sk);
  123. /*
  124. * sk->sk_send_head has to have one skb with
  125. * DCCP_SKB_CB(skb)->dccpd_type set to one of the retransmittable DCCP
  126. * packet types (REQUEST, RESPONSE, the ACK in the 3way handshake
  127. * (PARTOPEN timer), etc).
  128. */
  129. BUG_TRAP(sk->sk_send_head != NULL);
  130. /*
  131. * More than than 4MSL (8 minutes) has passed, a RESET(aborted) was
  132. * sent, no need to retransmit, this sock is dead.
  133. */
  134. if (dccp_write_timeout(sk))
  135. goto out;
  136. /*
  137. * We want to know the number of packets retransmitted, not the
  138. * total number of retransmissions of clones of original packets.
  139. */
  140. if (icsk->icsk_retransmits == 0)
  141. DCCP_INC_STATS_BH(DCCP_MIB_TIMEOUTS);
  142. if (dccp_retransmit_skb(sk, sk->sk_send_head) < 0) {
  143. /*
  144. * Retransmission failed because of local congestion,
  145. * do not backoff.
  146. */
  147. if (icsk->icsk_retransmits == 0)
  148. icsk->icsk_retransmits = 1;
  149. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  150. min(icsk->icsk_rto,
  151. TCP_RESOURCE_PROBE_INTERVAL),
  152. DCCP_RTO_MAX);
  153. goto out;
  154. }
  155. icsk->icsk_backoff++;
  156. icsk->icsk_retransmits++;
  157. icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX);
  158. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto,
  159. DCCP_RTO_MAX);
  160. if (icsk->icsk_retransmits > 3 /* FIXME: sysctl_dccp_retries1 */)
  161. __sk_dst_reset(sk);
  162. out:;
  163. }
  164. static void dccp_write_timer(unsigned long data)
  165. {
  166. struct sock *sk = (struct sock *)data;
  167. struct inet_connection_sock *icsk = inet_csk(sk);
  168. int event = 0;
  169. bh_lock_sock(sk);
  170. if (sock_owned_by_user(sk)) {
  171. /* Try again later */
  172. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  173. jiffies + (HZ / 20));
  174. goto out;
  175. }
  176. if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending)
  177. goto out;
  178. if (time_after(icsk->icsk_timeout, jiffies)) {
  179. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  180. icsk->icsk_timeout);
  181. goto out;
  182. }
  183. event = icsk->icsk_pending;
  184. icsk->icsk_pending = 0;
  185. switch (event) {
  186. case ICSK_TIME_RETRANS:
  187. dccp_retransmit_timer(sk);
  188. break;
  189. }
  190. out:
  191. bh_unlock_sock(sk);
  192. sock_put(sk);
  193. }
  194. /*
  195. * Timer for listening sockets
  196. */
  197. static void dccp_response_timer(struct sock *sk)
  198. {
  199. inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, DCCP_TIMEOUT_INIT,
  200. DCCP_RTO_MAX);
  201. }
  202. static void dccp_keepalive_timer(unsigned long data)
  203. {
  204. struct sock *sk = (struct sock *)data;
  205. /* Only process if socket is not in use. */
  206. bh_lock_sock(sk);
  207. if (sock_owned_by_user(sk)) {
  208. /* Try again later. */
  209. inet_csk_reset_keepalive_timer(sk, HZ / 20);
  210. goto out;
  211. }
  212. if (sk->sk_state == DCCP_LISTEN) {
  213. dccp_response_timer(sk);
  214. goto out;
  215. }
  216. out:
  217. bh_unlock_sock(sk);
  218. sock_put(sk);
  219. }