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 <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. 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. }