nr_timer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/types.h>
  12. #include <linux/socket.h>
  13. #include <linux/in.h>
  14. #include <linux/kernel.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/timer.h>
  17. #include <linux/string.h>
  18. #include <linux/sockios.h>
  19. #include <linux/net.h>
  20. #include <net/ax25.h>
  21. #include <linux/inet.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/skbuff.h>
  24. #include <net/sock.h>
  25. #include <net/tcp_states.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. #include <net/netrom.h>
  31. static void nr_heartbeat_expiry(struct timer_list *);
  32. static void nr_t1timer_expiry(struct timer_list *);
  33. static void nr_t2timer_expiry(struct timer_list *);
  34. static void nr_t4timer_expiry(struct timer_list *);
  35. static void nr_idletimer_expiry(struct timer_list *);
  36. void nr_init_timers(struct sock *sk)
  37. {
  38. struct nr_sock *nr = nr_sk(sk);
  39. timer_setup(&nr->t1timer, nr_t1timer_expiry, 0);
  40. timer_setup(&nr->t2timer, nr_t2timer_expiry, 0);
  41. timer_setup(&nr->t4timer, nr_t4timer_expiry, 0);
  42. timer_setup(&nr->idletimer, nr_idletimer_expiry, 0);
  43. /* initialized by sock_init_data */
  44. sk->sk_timer.function = nr_heartbeat_expiry;
  45. }
  46. void nr_start_t1timer(struct sock *sk)
  47. {
  48. struct nr_sock *nr = nr_sk(sk);
  49. sk_reset_timer(sk, &nr->t1timer, jiffies + nr->t1);
  50. }
  51. void nr_start_t2timer(struct sock *sk)
  52. {
  53. struct nr_sock *nr = nr_sk(sk);
  54. sk_reset_timer(sk, &nr->t2timer, jiffies + nr->t2);
  55. }
  56. void nr_start_t4timer(struct sock *sk)
  57. {
  58. struct nr_sock *nr = nr_sk(sk);
  59. sk_reset_timer(sk, &nr->t4timer, jiffies + nr->t4);
  60. }
  61. void nr_start_idletimer(struct sock *sk)
  62. {
  63. struct nr_sock *nr = nr_sk(sk);
  64. if (nr->idle > 0)
  65. sk_reset_timer(sk, &nr->idletimer, jiffies + nr->idle);
  66. }
  67. void nr_start_heartbeat(struct sock *sk)
  68. {
  69. sk_reset_timer(sk, &sk->sk_timer, jiffies + 5 * HZ);
  70. }
  71. void nr_stop_t1timer(struct sock *sk)
  72. {
  73. sk_stop_timer(sk, &nr_sk(sk)->t1timer);
  74. }
  75. void nr_stop_t2timer(struct sock *sk)
  76. {
  77. sk_stop_timer(sk, &nr_sk(sk)->t2timer);
  78. }
  79. void nr_stop_t4timer(struct sock *sk)
  80. {
  81. sk_stop_timer(sk, &nr_sk(sk)->t4timer);
  82. }
  83. void nr_stop_idletimer(struct sock *sk)
  84. {
  85. sk_stop_timer(sk, &nr_sk(sk)->idletimer);
  86. }
  87. void nr_stop_heartbeat(struct sock *sk)
  88. {
  89. sk_stop_timer(sk, &sk->sk_timer);
  90. }
  91. int nr_t1timer_running(struct sock *sk)
  92. {
  93. return timer_pending(&nr_sk(sk)->t1timer);
  94. }
  95. static void nr_heartbeat_expiry(struct timer_list *t)
  96. {
  97. struct sock *sk = from_timer(sk, t, sk_timer);
  98. struct nr_sock *nr = nr_sk(sk);
  99. bh_lock_sock(sk);
  100. switch (nr->state) {
  101. case NR_STATE_0:
  102. /* Magic here: If we listen() and a new link dies before it
  103. is accepted() it isn't 'dead' so doesn't get removed. */
  104. if (sock_flag(sk, SOCK_DESTROY) ||
  105. (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
  106. sock_hold(sk);
  107. bh_unlock_sock(sk);
  108. nr_destroy_socket(sk);
  109. sock_put(sk);
  110. return;
  111. }
  112. break;
  113. case NR_STATE_3:
  114. /*
  115. * Check for the state of the receive buffer.
  116. */
  117. if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
  118. (nr->condition & NR_COND_OWN_RX_BUSY)) {
  119. nr->condition &= ~NR_COND_OWN_RX_BUSY;
  120. nr->condition &= ~NR_COND_ACK_PENDING;
  121. nr->vl = nr->vr;
  122. nr_write_internal(sk, NR_INFOACK);
  123. break;
  124. }
  125. break;
  126. }
  127. nr_start_heartbeat(sk);
  128. bh_unlock_sock(sk);
  129. }
  130. static void nr_t2timer_expiry(struct timer_list *t)
  131. {
  132. struct nr_sock *nr = from_timer(nr, t, t2timer);
  133. struct sock *sk = &nr->sock;
  134. bh_lock_sock(sk);
  135. if (nr->condition & NR_COND_ACK_PENDING) {
  136. nr->condition &= ~NR_COND_ACK_PENDING;
  137. nr_enquiry_response(sk);
  138. }
  139. bh_unlock_sock(sk);
  140. }
  141. static void nr_t4timer_expiry(struct timer_list *t)
  142. {
  143. struct nr_sock *nr = from_timer(nr, t, t4timer);
  144. struct sock *sk = &nr->sock;
  145. bh_lock_sock(sk);
  146. nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
  147. bh_unlock_sock(sk);
  148. }
  149. static void nr_idletimer_expiry(struct timer_list *t)
  150. {
  151. struct nr_sock *nr = from_timer(nr, t, idletimer);
  152. struct sock *sk = &nr->sock;
  153. bh_lock_sock(sk);
  154. nr_clear_queues(sk);
  155. nr->n2count = 0;
  156. nr_write_internal(sk, NR_DISCREQ);
  157. nr->state = NR_STATE_2;
  158. nr_start_t1timer(sk);
  159. nr_stop_t2timer(sk);
  160. nr_stop_t4timer(sk);
  161. sk->sk_state = TCP_CLOSE;
  162. sk->sk_err = 0;
  163. sk->sk_shutdown |= SEND_SHUTDOWN;
  164. if (!sock_flag(sk, SOCK_DEAD)) {
  165. sk->sk_state_change(sk);
  166. sock_set_flag(sk, SOCK_DEAD);
  167. }
  168. bh_unlock_sock(sk);
  169. }
  170. static void nr_t1timer_expiry(struct timer_list *t)
  171. {
  172. struct nr_sock *nr = from_timer(nr, t, t1timer);
  173. struct sock *sk = &nr->sock;
  174. bh_lock_sock(sk);
  175. switch (nr->state) {
  176. case NR_STATE_1:
  177. if (nr->n2count == nr->n2) {
  178. nr_disconnect(sk, ETIMEDOUT);
  179. bh_unlock_sock(sk);
  180. return;
  181. } else {
  182. nr->n2count++;
  183. nr_write_internal(sk, NR_CONNREQ);
  184. }
  185. break;
  186. case NR_STATE_2:
  187. if (nr->n2count == nr->n2) {
  188. nr_disconnect(sk, ETIMEDOUT);
  189. bh_unlock_sock(sk);
  190. return;
  191. } else {
  192. nr->n2count++;
  193. nr_write_internal(sk, NR_DISCREQ);
  194. }
  195. break;
  196. case NR_STATE_3:
  197. if (nr->n2count == nr->n2) {
  198. nr_disconnect(sk, ETIMEDOUT);
  199. bh_unlock_sock(sk);
  200. return;
  201. } else {
  202. nr->n2count++;
  203. nr_requeue_frames(sk);
  204. }
  205. break;
  206. }
  207. nr_start_t1timer(sk);
  208. bh_unlock_sock(sk);
  209. }