nr_timer.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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(unsigned long);
  32. static void nr_t1timer_expiry(unsigned long);
  33. static void nr_t2timer_expiry(unsigned long);
  34. static void nr_t4timer_expiry(unsigned long);
  35. static void nr_idletimer_expiry(unsigned long);
  36. void nr_init_timers(struct sock *sk)
  37. {
  38. struct nr_sock *nr = nr_sk(sk);
  39. setup_timer(&nr->t1timer, nr_t1timer_expiry, (unsigned long)sk);
  40. setup_timer(&nr->t2timer, nr_t2timer_expiry, (unsigned long)sk);
  41. setup_timer(&nr->t4timer, nr_t4timer_expiry, (unsigned long)sk);
  42. setup_timer(&nr->idletimer, nr_idletimer_expiry, (unsigned long)sk);
  43. /* initialized by sock_init_data */
  44. sk->sk_timer.data = (unsigned long)sk;
  45. sk->sk_timer.function = &nr_heartbeat_expiry;
  46. }
  47. void nr_start_t1timer(struct sock *sk)
  48. {
  49. struct nr_sock *nr = nr_sk(sk);
  50. mod_timer(&nr->t1timer, jiffies + nr->t1);
  51. }
  52. void nr_start_t2timer(struct sock *sk)
  53. {
  54. struct nr_sock *nr = nr_sk(sk);
  55. mod_timer(&nr->t2timer, jiffies + nr->t2);
  56. }
  57. void nr_start_t4timer(struct sock *sk)
  58. {
  59. struct nr_sock *nr = nr_sk(sk);
  60. mod_timer(&nr->t4timer, jiffies + nr->t4);
  61. }
  62. void nr_start_idletimer(struct sock *sk)
  63. {
  64. struct nr_sock *nr = nr_sk(sk);
  65. if (nr->idle > 0)
  66. mod_timer(&nr->idletimer, jiffies + nr->idle);
  67. }
  68. void nr_start_heartbeat(struct sock *sk)
  69. {
  70. mod_timer(&sk->sk_timer, jiffies + 5 * HZ);
  71. }
  72. void nr_stop_t1timer(struct sock *sk)
  73. {
  74. del_timer(&nr_sk(sk)->t1timer);
  75. }
  76. void nr_stop_t2timer(struct sock *sk)
  77. {
  78. del_timer(&nr_sk(sk)->t2timer);
  79. }
  80. void nr_stop_t4timer(struct sock *sk)
  81. {
  82. del_timer(&nr_sk(sk)->t4timer);
  83. }
  84. void nr_stop_idletimer(struct sock *sk)
  85. {
  86. del_timer(&nr_sk(sk)->idletimer);
  87. }
  88. void nr_stop_heartbeat(struct sock *sk)
  89. {
  90. del_timer(&sk->sk_timer);
  91. }
  92. int nr_t1timer_running(struct sock *sk)
  93. {
  94. return timer_pending(&nr_sk(sk)->t1timer);
  95. }
  96. static void nr_heartbeat_expiry(unsigned long param)
  97. {
  98. struct sock *sk = (struct sock *)param;
  99. struct nr_sock *nr = nr_sk(sk);
  100. bh_lock_sock(sk);
  101. switch (nr->state) {
  102. case NR_STATE_0:
  103. /* Magic here: If we listen() and a new link dies before it
  104. is accepted() it isn't 'dead' so doesn't get removed. */
  105. if (sock_flag(sk, SOCK_DESTROY) ||
  106. (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
  107. sock_hold(sk);
  108. bh_unlock_sock(sk);
  109. nr_destroy_socket(sk);
  110. sock_put(sk);
  111. return;
  112. }
  113. break;
  114. case NR_STATE_3:
  115. /*
  116. * Check for the state of the receive buffer.
  117. */
  118. if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
  119. (nr->condition & NR_COND_OWN_RX_BUSY)) {
  120. nr->condition &= ~NR_COND_OWN_RX_BUSY;
  121. nr->condition &= ~NR_COND_ACK_PENDING;
  122. nr->vl = nr->vr;
  123. nr_write_internal(sk, NR_INFOACK);
  124. break;
  125. }
  126. break;
  127. }
  128. nr_start_heartbeat(sk);
  129. bh_unlock_sock(sk);
  130. }
  131. static void nr_t2timer_expiry(unsigned long param)
  132. {
  133. struct sock *sk = (struct sock *)param;
  134. struct nr_sock *nr = nr_sk(sk);
  135. bh_lock_sock(sk);
  136. if (nr->condition & NR_COND_ACK_PENDING) {
  137. nr->condition &= ~NR_COND_ACK_PENDING;
  138. nr_enquiry_response(sk);
  139. }
  140. bh_unlock_sock(sk);
  141. }
  142. static void nr_t4timer_expiry(unsigned long param)
  143. {
  144. struct sock *sk = (struct sock *)param;
  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(unsigned long param)
  150. {
  151. struct sock *sk = (struct sock *)param;
  152. struct nr_sock *nr = nr_sk(sk);
  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(unsigned long param)
  171. {
  172. struct sock *sk = (struct sock *)param;
  173. struct nr_sock *nr = nr_sk(sk);
  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. }