rose_timer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) Jonathan Naylor G4KLX ([email protected])
  5. * Copyright (C) 2002 Ralf Baechle DO1GRB ([email protected])
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/types.h>
  9. #include <linux/socket.h>
  10. #include <linux/in.h>
  11. #include <linux/kernel.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/timer.h>
  14. #include <linux/string.h>
  15. #include <linux/sockios.h>
  16. #include <linux/net.h>
  17. #include <net/ax25.h>
  18. #include <linux/inet.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <net/sock.h>
  22. #include <net/tcp_states.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/mm.h>
  25. #include <linux/interrupt.h>
  26. #include <net/rose.h>
  27. static void rose_heartbeat_expiry(struct timer_list *t);
  28. static void rose_timer_expiry(struct timer_list *);
  29. static void rose_idletimer_expiry(struct timer_list *);
  30. void rose_start_heartbeat(struct sock *sk)
  31. {
  32. sk_stop_timer(sk, &sk->sk_timer);
  33. sk->sk_timer.function = rose_heartbeat_expiry;
  34. sk->sk_timer.expires = jiffies + 5 * HZ;
  35. sk_reset_timer(sk, &sk->sk_timer, sk->sk_timer.expires);
  36. }
  37. void rose_start_t1timer(struct sock *sk)
  38. {
  39. struct rose_sock *rose = rose_sk(sk);
  40. sk_stop_timer(sk, &rose->timer);
  41. rose->timer.function = rose_timer_expiry;
  42. rose->timer.expires = jiffies + rose->t1;
  43. sk_reset_timer(sk, &rose->timer, rose->timer.expires);
  44. }
  45. void rose_start_t2timer(struct sock *sk)
  46. {
  47. struct rose_sock *rose = rose_sk(sk);
  48. sk_stop_timer(sk, &rose->timer);
  49. rose->timer.function = rose_timer_expiry;
  50. rose->timer.expires = jiffies + rose->t2;
  51. sk_reset_timer(sk, &rose->timer, rose->timer.expires);
  52. }
  53. void rose_start_t3timer(struct sock *sk)
  54. {
  55. struct rose_sock *rose = rose_sk(sk);
  56. sk_stop_timer(sk, &rose->timer);
  57. rose->timer.function = rose_timer_expiry;
  58. rose->timer.expires = jiffies + rose->t3;
  59. sk_reset_timer(sk, &rose->timer, rose->timer.expires);
  60. }
  61. void rose_start_hbtimer(struct sock *sk)
  62. {
  63. struct rose_sock *rose = rose_sk(sk);
  64. sk_stop_timer(sk, &rose->timer);
  65. rose->timer.function = rose_timer_expiry;
  66. rose->timer.expires = jiffies + rose->hb;
  67. sk_reset_timer(sk, &rose->timer, rose->timer.expires);
  68. }
  69. void rose_start_idletimer(struct sock *sk)
  70. {
  71. struct rose_sock *rose = rose_sk(sk);
  72. sk_stop_timer(sk, &rose->idletimer);
  73. if (rose->idle > 0) {
  74. rose->idletimer.function = rose_idletimer_expiry;
  75. rose->idletimer.expires = jiffies + rose->idle;
  76. sk_reset_timer(sk, &rose->idletimer, rose->idletimer.expires);
  77. }
  78. }
  79. void rose_stop_heartbeat(struct sock *sk)
  80. {
  81. sk_stop_timer(sk, &sk->sk_timer);
  82. }
  83. void rose_stop_timer(struct sock *sk)
  84. {
  85. sk_stop_timer(sk, &rose_sk(sk)->timer);
  86. }
  87. void rose_stop_idletimer(struct sock *sk)
  88. {
  89. sk_stop_timer(sk, &rose_sk(sk)->idletimer);
  90. }
  91. static void rose_heartbeat_expiry(struct timer_list *t)
  92. {
  93. struct sock *sk = from_timer(sk, t, sk_timer);
  94. struct rose_sock *rose = rose_sk(sk);
  95. bh_lock_sock(sk);
  96. switch (rose->state) {
  97. case ROSE_STATE_0:
  98. /* Magic here: If we listen() and a new link dies before it
  99. is accepted() it isn't 'dead' so doesn't get removed. */
  100. if (sock_flag(sk, SOCK_DESTROY) ||
  101. (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
  102. bh_unlock_sock(sk);
  103. rose_destroy_socket(sk);
  104. sock_put(sk);
  105. return;
  106. }
  107. break;
  108. case ROSE_STATE_3:
  109. /*
  110. * Check for the state of the receive buffer.
  111. */
  112. if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
  113. (rose->condition & ROSE_COND_OWN_RX_BUSY)) {
  114. rose->condition &= ~ROSE_COND_OWN_RX_BUSY;
  115. rose->condition &= ~ROSE_COND_ACK_PENDING;
  116. rose->vl = rose->vr;
  117. rose_write_internal(sk, ROSE_RR);
  118. rose_stop_timer(sk); /* HB */
  119. break;
  120. }
  121. break;
  122. }
  123. rose_start_heartbeat(sk);
  124. bh_unlock_sock(sk);
  125. sock_put(sk);
  126. }
  127. static void rose_timer_expiry(struct timer_list *t)
  128. {
  129. struct rose_sock *rose = from_timer(rose, t, timer);
  130. struct sock *sk = &rose->sock;
  131. bh_lock_sock(sk);
  132. switch (rose->state) {
  133. case ROSE_STATE_1: /* T1 */
  134. case ROSE_STATE_4: /* T2 */
  135. rose_write_internal(sk, ROSE_CLEAR_REQUEST);
  136. rose->state = ROSE_STATE_2;
  137. rose_start_t3timer(sk);
  138. break;
  139. case ROSE_STATE_2: /* T3 */
  140. rose->neighbour->use--;
  141. rose_disconnect(sk, ETIMEDOUT, -1, -1);
  142. break;
  143. case ROSE_STATE_3: /* HB */
  144. if (rose->condition & ROSE_COND_ACK_PENDING) {
  145. rose->condition &= ~ROSE_COND_ACK_PENDING;
  146. rose_enquiry_response(sk);
  147. }
  148. break;
  149. }
  150. bh_unlock_sock(sk);
  151. sock_put(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. sock_put(sk);
  171. }