nr_timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/uaccess.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <net/netrom.h>
  28. static void nr_heartbeat_expiry(struct timer_list *);
  29. static void nr_t1timer_expiry(struct timer_list *);
  30. static void nr_t2timer_expiry(struct timer_list *);
  31. static void nr_t4timer_expiry(struct timer_list *);
  32. static void nr_idletimer_expiry(struct timer_list *);
  33. void nr_init_timers(struct sock *sk)
  34. {
  35. struct nr_sock *nr = nr_sk(sk);
  36. timer_setup(&nr->t1timer, nr_t1timer_expiry, 0);
  37. timer_setup(&nr->t2timer, nr_t2timer_expiry, 0);
  38. timer_setup(&nr->t4timer, nr_t4timer_expiry, 0);
  39. timer_setup(&nr->idletimer, nr_idletimer_expiry, 0);
  40. /* initialized by sock_init_data */
  41. sk->sk_timer.function = nr_heartbeat_expiry;
  42. }
  43. void nr_start_t1timer(struct sock *sk)
  44. {
  45. struct nr_sock *nr = nr_sk(sk);
  46. sk_reset_timer(sk, &nr->t1timer, jiffies + nr->t1);
  47. }
  48. void nr_start_t2timer(struct sock *sk)
  49. {
  50. struct nr_sock *nr = nr_sk(sk);
  51. sk_reset_timer(sk, &nr->t2timer, jiffies + nr->t2);
  52. }
  53. void nr_start_t4timer(struct sock *sk)
  54. {
  55. struct nr_sock *nr = nr_sk(sk);
  56. sk_reset_timer(sk, &nr->t4timer, jiffies + nr->t4);
  57. }
  58. void nr_start_idletimer(struct sock *sk)
  59. {
  60. struct nr_sock *nr = nr_sk(sk);
  61. if (nr->idle > 0)
  62. sk_reset_timer(sk, &nr->idletimer, jiffies + nr->idle);
  63. }
  64. void nr_start_heartbeat(struct sock *sk)
  65. {
  66. sk_reset_timer(sk, &sk->sk_timer, jiffies + 5 * HZ);
  67. }
  68. void nr_stop_t1timer(struct sock *sk)
  69. {
  70. sk_stop_timer(sk, &nr_sk(sk)->t1timer);
  71. }
  72. void nr_stop_t2timer(struct sock *sk)
  73. {
  74. sk_stop_timer(sk, &nr_sk(sk)->t2timer);
  75. }
  76. void nr_stop_t4timer(struct sock *sk)
  77. {
  78. sk_stop_timer(sk, &nr_sk(sk)->t4timer);
  79. }
  80. void nr_stop_idletimer(struct sock *sk)
  81. {
  82. sk_stop_timer(sk, &nr_sk(sk)->idletimer);
  83. }
  84. void nr_stop_heartbeat(struct sock *sk)
  85. {
  86. sk_stop_timer(sk, &sk->sk_timer);
  87. }
  88. int nr_t1timer_running(struct sock *sk)
  89. {
  90. return timer_pending(&nr_sk(sk)->t1timer);
  91. }
  92. static void nr_heartbeat_expiry(struct timer_list *t)
  93. {
  94. struct sock *sk = from_timer(sk, t, sk_timer);
  95. struct nr_sock *nr = nr_sk(sk);
  96. bh_lock_sock(sk);
  97. switch (nr->state) {
  98. case NR_STATE_0:
  99. /* Magic here: If we listen() and a new link dies before it
  100. is accepted() it isn't 'dead' so doesn't get removed. */
  101. if (sock_flag(sk, SOCK_DESTROY) ||
  102. (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
  103. sock_hold(sk);
  104. bh_unlock_sock(sk);
  105. nr_destroy_socket(sk);
  106. goto out;
  107. }
  108. break;
  109. case NR_STATE_3:
  110. /*
  111. * Check for the state of the receive buffer.
  112. */
  113. if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
  114. (nr->condition & NR_COND_OWN_RX_BUSY)) {
  115. nr->condition &= ~NR_COND_OWN_RX_BUSY;
  116. nr->condition &= ~NR_COND_ACK_PENDING;
  117. nr->vl = nr->vr;
  118. nr_write_internal(sk, NR_INFOACK);
  119. break;
  120. }
  121. break;
  122. }
  123. nr_start_heartbeat(sk);
  124. bh_unlock_sock(sk);
  125. out:
  126. sock_put(sk);
  127. }
  128. static void nr_t2timer_expiry(struct timer_list *t)
  129. {
  130. struct nr_sock *nr = from_timer(nr, t, t2timer);
  131. struct sock *sk = &nr->sock;
  132. bh_lock_sock(sk);
  133. if (nr->condition & NR_COND_ACK_PENDING) {
  134. nr->condition &= ~NR_COND_ACK_PENDING;
  135. nr_enquiry_response(sk);
  136. }
  137. bh_unlock_sock(sk);
  138. sock_put(sk);
  139. }
  140. static void nr_t4timer_expiry(struct timer_list *t)
  141. {
  142. struct nr_sock *nr = from_timer(nr, t, t4timer);
  143. struct sock *sk = &nr->sock;
  144. bh_lock_sock(sk);
  145. nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
  146. bh_unlock_sock(sk);
  147. sock_put(sk);
  148. }
  149. static void nr_idletimer_expiry(struct timer_list *t)
  150. {
  151. struct nr_sock *nr = from_timer(nr, t, idletimer);
  152. struct sock *sk = &nr->sock;
  153. bh_lock_sock(sk);
  154. nr_clear_queues(sk);
  155. nr->n2count = 0;
  156. nr_write_internal(sk, NR_DISCREQ);
  157. nr->state = NR_STATE_2;
  158. nr_start_t1timer(sk);
  159. nr_stop_t2timer(sk);
  160. nr_stop_t4timer(sk);
  161. sk->sk_state = TCP_CLOSE;
  162. sk->sk_err = 0;
  163. sk->sk_shutdown |= SEND_SHUTDOWN;
  164. if (!sock_flag(sk, SOCK_DEAD)) {
  165. sk->sk_state_change(sk);
  166. sock_set_flag(sk, SOCK_DEAD);
  167. }
  168. bh_unlock_sock(sk);
  169. sock_put(sk);
  170. }
  171. static void nr_t1timer_expiry(struct timer_list *t)
  172. {
  173. struct nr_sock *nr = from_timer(nr, t, t1timer);
  174. struct sock *sk = &nr->sock;
  175. bh_lock_sock(sk);
  176. switch (nr->state) {
  177. case NR_STATE_1:
  178. if (nr->n2count == nr->n2) {
  179. nr_disconnect(sk, ETIMEDOUT);
  180. goto out;
  181. } else {
  182. nr->n2count++;
  183. nr_write_internal(sk, NR_CONNREQ);
  184. }
  185. break;
  186. case NR_STATE_2:
  187. if (nr->n2count == nr->n2) {
  188. nr_disconnect(sk, ETIMEDOUT);
  189. goto out;
  190. } else {
  191. nr->n2count++;
  192. nr_write_internal(sk, NR_DISCREQ);
  193. }
  194. break;
  195. case NR_STATE_3:
  196. if (nr->n2count == nr->n2) {
  197. nr_disconnect(sk, ETIMEDOUT);
  198. goto out;
  199. } else {
  200. nr->n2count++;
  201. nr_requeue_frames(sk);
  202. }
  203. break;
  204. }
  205. nr_start_t1timer(sk);
  206. out:
  207. bh_unlock_sock(sk);
  208. sock_put(sk);
  209. }