rose_timer.c 4.9 KB

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