rose_timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/fcntl.h>
  27. #include <linux/mm.h>
  28. #include <linux/interrupt.h>
  29. #include <net/rose.h>
  30. static void rose_heartbeat_expiry(unsigned long);
  31. static void rose_timer_expiry(unsigned long);
  32. static void rose_idletimer_expiry(unsigned long);
  33. void rose_start_heartbeat(struct sock *sk)
  34. {
  35. del_timer(&sk->sk_timer);
  36. sk->sk_timer.data = (unsigned long)sk;
  37. sk->sk_timer.function = &rose_heartbeat_expiry;
  38. sk->sk_timer.expires = jiffies + 5 * HZ;
  39. add_timer(&sk->sk_timer);
  40. }
  41. void rose_start_t1timer(struct sock *sk)
  42. {
  43. struct rose_sock *rose = rose_sk(sk);
  44. del_timer(&rose->timer);
  45. rose->timer.data = (unsigned long)sk;
  46. rose->timer.function = &rose_timer_expiry;
  47. rose->timer.expires = jiffies + rose->t1;
  48. add_timer(&rose->timer);
  49. }
  50. void rose_start_t2timer(struct sock *sk)
  51. {
  52. struct rose_sock *rose = rose_sk(sk);
  53. del_timer(&rose->timer);
  54. rose->timer.data = (unsigned long)sk;
  55. rose->timer.function = &rose_timer_expiry;
  56. rose->timer.expires = jiffies + rose->t2;
  57. add_timer(&rose->timer);
  58. }
  59. void rose_start_t3timer(struct sock *sk)
  60. {
  61. struct rose_sock *rose = rose_sk(sk);
  62. del_timer(&rose->timer);
  63. rose->timer.data = (unsigned long)sk;
  64. rose->timer.function = &rose_timer_expiry;
  65. rose->timer.expires = jiffies + rose->t3;
  66. add_timer(&rose->timer);
  67. }
  68. void rose_start_hbtimer(struct sock *sk)
  69. {
  70. struct rose_sock *rose = rose_sk(sk);
  71. del_timer(&rose->timer);
  72. rose->timer.data = (unsigned long)sk;
  73. rose->timer.function = &rose_timer_expiry;
  74. rose->timer.expires = jiffies + rose->hb;
  75. add_timer(&rose->timer);
  76. }
  77. void rose_start_idletimer(struct sock *sk)
  78. {
  79. struct rose_sock *rose = rose_sk(sk);
  80. del_timer(&rose->idletimer);
  81. if (rose->idle > 0) {
  82. rose->idletimer.data = (unsigned long)sk;
  83. rose->idletimer.function = &rose_idletimer_expiry;
  84. rose->idletimer.expires = jiffies + rose->idle;
  85. add_timer(&rose->idletimer);
  86. }
  87. }
  88. void rose_stop_heartbeat(struct sock *sk)
  89. {
  90. del_timer(&sk->sk_timer);
  91. }
  92. void rose_stop_timer(struct sock *sk)
  93. {
  94. del_timer(&rose_sk(sk)->timer);
  95. }
  96. void rose_stop_idletimer(struct sock *sk)
  97. {
  98. del_timer(&rose_sk(sk)->idletimer);
  99. }
  100. static void rose_heartbeat_expiry(unsigned long param)
  101. {
  102. struct sock *sk = (struct sock *)param;
  103. struct rose_sock *rose = rose_sk(sk);
  104. bh_lock_sock(sk);
  105. switch (rose->state) {
  106. case ROSE_STATE_0:
  107. /* Magic here: If we listen() and a new link dies before it
  108. is accepted() it isn't 'dead' so doesn't get removed. */
  109. if (sock_flag(sk, SOCK_DESTROY) ||
  110. (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
  111. bh_unlock_sock(sk);
  112. rose_destroy_socket(sk);
  113. return;
  114. }
  115. break;
  116. case ROSE_STATE_3:
  117. /*
  118. * Check for the state of the receive buffer.
  119. */
  120. if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
  121. (rose->condition & ROSE_COND_OWN_RX_BUSY)) {
  122. rose->condition &= ~ROSE_COND_OWN_RX_BUSY;
  123. rose->condition &= ~ROSE_COND_ACK_PENDING;
  124. rose->vl = rose->vr;
  125. rose_write_internal(sk, ROSE_RR);
  126. rose_stop_timer(sk); /* HB */
  127. break;
  128. }
  129. break;
  130. }
  131. rose_start_heartbeat(sk);
  132. bh_unlock_sock(sk);
  133. }
  134. static void rose_timer_expiry(unsigned long param)
  135. {
  136. struct sock *sk = (struct sock *)param;
  137. struct rose_sock *rose = rose_sk(sk);
  138. bh_lock_sock(sk);
  139. switch (rose->state) {
  140. case ROSE_STATE_1: /* T1 */
  141. case ROSE_STATE_4: /* T2 */
  142. rose_write_internal(sk, ROSE_CLEAR_REQUEST);
  143. rose->state = ROSE_STATE_2;
  144. rose_start_t3timer(sk);
  145. break;
  146. case ROSE_STATE_2: /* T3 */
  147. rose->neighbour->use--;
  148. rose_disconnect(sk, ETIMEDOUT, -1, -1);
  149. break;
  150. case ROSE_STATE_3: /* HB */
  151. if (rose->condition & ROSE_COND_ACK_PENDING) {
  152. rose->condition &= ~ROSE_COND_ACK_PENDING;
  153. rose_enquiry_response(sk);
  154. }
  155. break;
  156. }
  157. bh_unlock_sock(sk);
  158. }
  159. static void rose_idletimer_expiry(unsigned long param)
  160. {
  161. struct sock *sk = (struct sock *)param;
  162. bh_lock_sock(sk);
  163. rose_clear_queues(sk);
  164. rose_write_internal(sk, ROSE_CLEAR_REQUEST);
  165. rose_sk(sk)->state = ROSE_STATE_2;
  166. rose_start_t3timer(sk);
  167. sk->sk_state = TCP_CLOSE;
  168. sk->sk_err = 0;
  169. sk->sk_shutdown |= SEND_SHUTDOWN;
  170. if (!sock_flag(sk, SOCK_DEAD)) {
  171. sk->sk_state_change(sk);
  172. sock_set_flag(sk, SOCK_DEAD);
  173. }
  174. bh_unlock_sock(sk);
  175. }