rose_timer.c 4.8 KB

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