nr_timer.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <asm/uaccess.h>
  27. #include <asm/system.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/mm.h>
  30. #include <linux/interrupt.h>
  31. #include <net/netrom.h>
  32. static void nr_heartbeat_expiry(unsigned long);
  33. static void nr_t1timer_expiry(unsigned long);
  34. static void nr_t2timer_expiry(unsigned long);
  35. static void nr_t4timer_expiry(unsigned long);
  36. static void nr_idletimer_expiry(unsigned long);
  37. void nr_init_timers(struct sock *sk)
  38. {
  39. struct nr_sock *nr = nr_sk(sk);
  40. setup_timer(&nr->t1timer, nr_t1timer_expiry, (unsigned long)sk);
  41. setup_timer(&nr->t2timer, nr_t2timer_expiry, (unsigned long)sk);
  42. setup_timer(&nr->t4timer, nr_t4timer_expiry, (unsigned long)sk);
  43. setup_timer(&nr->idletimer, nr_idletimer_expiry, (unsigned long)sk);
  44. /* initialized by sock_init_data */
  45. sk->sk_timer.data = (unsigned long)sk;
  46. sk->sk_timer.function = &nr_heartbeat_expiry;
  47. }
  48. void nr_start_t1timer(struct sock *sk)
  49. {
  50. struct nr_sock *nr = nr_sk(sk);
  51. mod_timer(&nr->t1timer, jiffies + nr->t1);
  52. }
  53. void nr_start_t2timer(struct sock *sk)
  54. {
  55. struct nr_sock *nr = nr_sk(sk);
  56. mod_timer(&nr->t2timer, jiffies + nr->t2);
  57. }
  58. void nr_start_t4timer(struct sock *sk)
  59. {
  60. struct nr_sock *nr = nr_sk(sk);
  61. mod_timer(&nr->t4timer, jiffies + nr->t4);
  62. }
  63. void nr_start_idletimer(struct sock *sk)
  64. {
  65. struct nr_sock *nr = nr_sk(sk);
  66. if (nr->idle > 0)
  67. mod_timer(&nr->idletimer, jiffies + nr->idle);
  68. }
  69. void nr_start_heartbeat(struct sock *sk)
  70. {
  71. mod_timer(&sk->sk_timer, jiffies + 5 * HZ);
  72. }
  73. void nr_stop_t1timer(struct sock *sk)
  74. {
  75. del_timer(&nr_sk(sk)->t1timer);
  76. }
  77. void nr_stop_t2timer(struct sock *sk)
  78. {
  79. del_timer(&nr_sk(sk)->t2timer);
  80. }
  81. void nr_stop_t4timer(struct sock *sk)
  82. {
  83. del_timer(&nr_sk(sk)->t4timer);
  84. }
  85. void nr_stop_idletimer(struct sock *sk)
  86. {
  87. del_timer(&nr_sk(sk)->idletimer);
  88. }
  89. void nr_stop_heartbeat(struct sock *sk)
  90. {
  91. del_timer(&sk->sk_timer);
  92. }
  93. int nr_t1timer_running(struct sock *sk)
  94. {
  95. return timer_pending(&nr_sk(sk)->t1timer);
  96. }
  97. static void nr_heartbeat_expiry(unsigned long param)
  98. {
  99. struct sock *sk = (struct sock *)param;
  100. struct nr_sock *nr = nr_sk(sk);
  101. bh_lock_sock(sk);
  102. switch (nr->state) {
  103. case NR_STATE_0:
  104. /* Magic here: If we listen() and a new link dies before it
  105. is accepted() it isn't 'dead' so doesn't get removed. */
  106. if (sock_flag(sk, SOCK_DESTROY) ||
  107. (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
  108. sock_hold(sk);
  109. bh_unlock_sock(sk);
  110. nr_destroy_socket(sk);
  111. sock_put(sk);
  112. return;
  113. }
  114. break;
  115. case NR_STATE_3:
  116. /*
  117. * Check for the state of the receive buffer.
  118. */
  119. if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
  120. (nr->condition & NR_COND_OWN_RX_BUSY)) {
  121. nr->condition &= ~NR_COND_OWN_RX_BUSY;
  122. nr->condition &= ~NR_COND_ACK_PENDING;
  123. nr->vl = nr->vr;
  124. nr_write_internal(sk, NR_INFOACK);
  125. break;
  126. }
  127. break;
  128. }
  129. nr_start_heartbeat(sk);
  130. bh_unlock_sock(sk);
  131. }
  132. static void nr_t2timer_expiry(unsigned long param)
  133. {
  134. struct sock *sk = (struct sock *)param;
  135. struct nr_sock *nr = nr_sk(sk);
  136. bh_lock_sock(sk);
  137. if (nr->condition & NR_COND_ACK_PENDING) {
  138. nr->condition &= ~NR_COND_ACK_PENDING;
  139. nr_enquiry_response(sk);
  140. }
  141. bh_unlock_sock(sk);
  142. }
  143. static void nr_t4timer_expiry(unsigned long param)
  144. {
  145. struct sock *sk = (struct sock *)param;
  146. bh_lock_sock(sk);
  147. nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
  148. bh_unlock_sock(sk);
  149. }
  150. static void nr_idletimer_expiry(unsigned long param)
  151. {
  152. struct sock *sk = (struct sock *)param;
  153. struct nr_sock *nr = nr_sk(sk);
  154. bh_lock_sock(sk);
  155. nr_clear_queues(sk);
  156. nr->n2count = 0;
  157. nr_write_internal(sk, NR_DISCREQ);
  158. nr->state = NR_STATE_2;
  159. nr_start_t1timer(sk);
  160. nr_stop_t2timer(sk);
  161. nr_stop_t4timer(sk);
  162. sk->sk_state = TCP_CLOSE;
  163. sk->sk_err = 0;
  164. sk->sk_shutdown |= SEND_SHUTDOWN;
  165. if (!sock_flag(sk, SOCK_DEAD)) {
  166. sk->sk_state_change(sk);
  167. sock_set_flag(sk, SOCK_DEAD);
  168. }
  169. bh_unlock_sock(sk);
  170. }
  171. static void nr_t1timer_expiry(unsigned long param)
  172. {
  173. struct sock *sk = (struct sock *)param;
  174. struct nr_sock *nr = nr_sk(sk);
  175. bh_lock_sock(sk);
  176. switch (nr->state) {
  177. case NR_STATE_1:
  178. if (nr->n2count == nr->n2) {
  179. nr_disconnect(sk, ETIMEDOUT);
  180. bh_unlock_sock(sk);
  181. return;
  182. } else {
  183. nr->n2count++;
  184. nr_write_internal(sk, NR_CONNREQ);
  185. }
  186. break;
  187. case NR_STATE_2:
  188. if (nr->n2count == nr->n2) {
  189. nr_disconnect(sk, ETIMEDOUT);
  190. bh_unlock_sock(sk);
  191. return;
  192. } else {
  193. nr->n2count++;
  194. nr_write_internal(sk, NR_DISCREQ);
  195. }
  196. break;
  197. case NR_STATE_3:
  198. if (nr->n2count == nr->n2) {
  199. nr_disconnect(sk, ETIMEDOUT);
  200. bh_unlock_sock(sk);
  201. return;
  202. } else {
  203. nr->n2count++;
  204. nr_requeue_frames(sk);
  205. }
  206. break;
  207. }
  208. nr_start_t1timer(sk);
  209. bh_unlock_sock(sk);
  210. }