rose_timer.c 4.7 KB

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