nr_timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  5. * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
  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. if (sk->sk_state == TCP_LISTEN)
  104. sock_hold(sk);
  105. bh_unlock_sock(sk);
  106. nr_destroy_socket(sk);
  107. goto out;
  108. }
  109. break;
  110. case NR_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. (nr->condition & NR_COND_OWN_RX_BUSY)) {
  116. nr->condition &= ~NR_COND_OWN_RX_BUSY;
  117. nr->condition &= ~NR_COND_ACK_PENDING;
  118. nr->vl = nr->vr;
  119. nr_write_internal(sk, NR_INFOACK);
  120. break;
  121. }
  122. break;
  123. }
  124. nr_start_heartbeat(sk);
  125. bh_unlock_sock(sk);
  126. out:
  127. sock_put(sk);
  128. }
  129. static void nr_t2timer_expiry(struct timer_list *t)
  130. {
  131. struct nr_sock *nr = from_timer(nr, t, t2timer);
  132. struct sock *sk = &nr->sock;
  133. bh_lock_sock(sk);
  134. if (nr->condition & NR_COND_ACK_PENDING) {
  135. nr->condition &= ~NR_COND_ACK_PENDING;
  136. nr_enquiry_response(sk);
  137. }
  138. bh_unlock_sock(sk);
  139. sock_put(sk);
  140. }
  141. static void nr_t4timer_expiry(struct timer_list *t)
  142. {
  143. struct nr_sock *nr = from_timer(nr, t, t4timer);
  144. struct sock *sk = &nr->sock;
  145. bh_lock_sock(sk);
  146. nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
  147. bh_unlock_sock(sk);
  148. sock_put(sk);
  149. }
  150. static void nr_idletimer_expiry(struct timer_list *t)
  151. {
  152. struct nr_sock *nr = from_timer(nr, t, idletimer);
  153. struct sock *sk = &nr->sock;
  154. bh_lock_sock(sk);
  155. nr_clear_queues(sk);
  156. nr->n2count = 0;
  157. nr_write_internal(sk, NR_DISCREQ);
  158. nr->state = NR_STATE_2;
  159. nr_start_t1timer(sk);
  160. nr_stop_t2timer(sk);
  161. nr_stop_t4timer(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. }
  172. static void nr_t1timer_expiry(struct timer_list *t)
  173. {
  174. struct nr_sock *nr = from_timer(nr, t, t1timer);
  175. struct sock *sk = &nr->sock;
  176. bh_lock_sock(sk);
  177. switch (nr->state) {
  178. case NR_STATE_1:
  179. if (nr->n2count == nr->n2) {
  180. nr_disconnect(sk, ETIMEDOUT);
  181. goto out;
  182. } else {
  183. nr->n2count++;
  184. nr_write_internal(sk, NR_CONNREQ);
  185. }
  186. break;
  187. case NR_STATE_2:
  188. if (nr->n2count == nr->n2) {
  189. nr_disconnect(sk, ETIMEDOUT);
  190. goto out;
  191. } else {
  192. nr->n2count++;
  193. nr_write_internal(sk, NR_DISCREQ);
  194. }
  195. break;
  196. case NR_STATE_3:
  197. if (nr->n2count == nr->n2) {
  198. nr_disconnect(sk, ETIMEDOUT);
  199. goto out;
  200. } else {
  201. nr->n2count++;
  202. nr_requeue_frames(sk);
  203. }
  204. break;
  205. }
  206. nr_start_t1timer(sk);
  207. out:
  208. bh_unlock_sock(sk);
  209. sock_put(sk);
  210. }