nr_timer.c 5.5 KB

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