ax25_ds_timer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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) Joerg Reuter DL1BKE (jreuter@yaina.de)
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/types.h>
  9. #include <linux/socket.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/in.h>
  12. #include <linux/kernel.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/timer.h>
  15. #include <linux/string.h>
  16. #include <linux/sockios.h>
  17. #include <linux/net.h>
  18. #include <net/tcp_states.h>
  19. #include <net/ax25.h>
  20. #include <linux/inet.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <net/sock.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. static void ax25_ds_timeout(struct timer_list *);
  29. /*
  30. * Add DAMA slave timeout timer to timer list.
  31. * Unlike the connection based timers the timeout function gets
  32. * triggered every second. Please note that NET_AX25_DAMA_SLAVE_TIMEOUT
  33. * (aka /proc/sys/net/ax25/{dev}/dama_slave_timeout) is still in
  34. * 1/10th of a second.
  35. */
  36. void ax25_ds_setup_timer(ax25_dev *ax25_dev)
  37. {
  38. timer_setup(&ax25_dev->dama.slave_timer, ax25_ds_timeout, 0);
  39. }
  40. void ax25_ds_del_timer(ax25_dev *ax25_dev)
  41. {
  42. if (ax25_dev)
  43. del_timer(&ax25_dev->dama.slave_timer);
  44. }
  45. void ax25_ds_set_timer(ax25_dev *ax25_dev)
  46. {
  47. if (ax25_dev == NULL) /* paranoia */
  48. return;
  49. ax25_dev->dama.slave_timeout =
  50. msecs_to_jiffies(ax25_dev->values[AX25_VALUES_DS_TIMEOUT]) / 10;
  51. mod_timer(&ax25_dev->dama.slave_timer, jiffies + HZ);
  52. }
  53. /*
  54. * DAMA Slave Timeout
  55. * Silently discard all (slave) connections in case our master forgot us...
  56. */
  57. static void ax25_ds_timeout(struct timer_list *t)
  58. {
  59. ax25_dev *ax25_dev = from_timer(ax25_dev, t, dama.slave_timer);
  60. ax25_cb *ax25;
  61. if (ax25_dev == NULL || !ax25_dev->dama.slave)
  62. return; /* Yikes! */
  63. if (!ax25_dev->dama.slave_timeout || --ax25_dev->dama.slave_timeout) {
  64. ax25_ds_set_timer(ax25_dev);
  65. return;
  66. }
  67. spin_lock(&ax25_list_lock);
  68. ax25_for_each(ax25, &ax25_list) {
  69. if (ax25->ax25_dev != ax25_dev || !(ax25->condition & AX25_COND_DAMA_MODE))
  70. continue;
  71. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  72. ax25_disconnect(ax25, ETIMEDOUT);
  73. }
  74. spin_unlock(&ax25_list_lock);
  75. ax25_dev_dama_off(ax25_dev);
  76. }
  77. void ax25_ds_heartbeat_expiry(ax25_cb *ax25)
  78. {
  79. struct sock *sk=ax25->sk;
  80. if (sk)
  81. bh_lock_sock(sk);
  82. switch (ax25->state) {
  83. case AX25_STATE_0:
  84. case AX25_STATE_2:
  85. /* Magic here: If we listen() and a new link dies before it
  86. is accepted() it isn't 'dead' so doesn't get removed. */
  87. if (!sk || sock_flag(sk, SOCK_DESTROY) ||
  88. (sk->sk_state == TCP_LISTEN &&
  89. sock_flag(sk, SOCK_DEAD))) {
  90. if (sk) {
  91. sock_hold(sk);
  92. ax25_destroy_socket(ax25);
  93. bh_unlock_sock(sk);
  94. /* Ungrab socket and destroy it */
  95. sock_put(sk);
  96. } else
  97. ax25_destroy_socket(ax25);
  98. return;
  99. }
  100. break;
  101. case AX25_STATE_3:
  102. /*
  103. * Check the state of the receive buffer.
  104. */
  105. if (sk != NULL) {
  106. if (atomic_read(&sk->sk_rmem_alloc) <
  107. (sk->sk_rcvbuf >> 1) &&
  108. (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
  109. ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
  110. ax25->condition &= ~AX25_COND_ACK_PENDING;
  111. break;
  112. }
  113. }
  114. break;
  115. }
  116. if (sk)
  117. bh_unlock_sock(sk);
  118. ax25_start_heartbeat(ax25);
  119. }
  120. /* dl1bke 960114: T3 works much like the IDLE timeout, but
  121. * gets reloaded with every frame for this
  122. * connection.
  123. */
  124. void ax25_ds_t3timer_expiry(ax25_cb *ax25)
  125. {
  126. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  127. ax25_dama_off(ax25);
  128. ax25_disconnect(ax25, ETIMEDOUT);
  129. }
  130. /* dl1bke 960228: close the connection when IDLE expires.
  131. * unlike T3 this timer gets reloaded only on
  132. * I frames.
  133. */
  134. void ax25_ds_idletimer_expiry(ax25_cb *ax25)
  135. {
  136. ax25_clear_queues(ax25);
  137. ax25->n2count = 0;
  138. ax25->state = AX25_STATE_2;
  139. ax25_calculate_t1(ax25);
  140. ax25_start_t1timer(ax25);
  141. ax25_stop_t3timer(ax25);
  142. if (ax25->sk != NULL) {
  143. bh_lock_sock(ax25->sk);
  144. ax25->sk->sk_state = TCP_CLOSE;
  145. ax25->sk->sk_err = 0;
  146. ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
  147. if (!sock_flag(ax25->sk, SOCK_DEAD)) {
  148. ax25->sk->sk_state_change(ax25->sk);
  149. sock_set_flag(ax25->sk, SOCK_DEAD);
  150. }
  151. bh_unlock_sock(ax25->sk);
  152. }
  153. }
  154. /* dl1bke 960114: The DAMA protocol requires to send data and SABM/DISC
  155. * within the poll of any connected channel. Remember
  156. * that we are not allowed to send anything unless we
  157. * get polled by the Master.
  158. *
  159. * Thus we'll have to do parts of our T1 handling in
  160. * ax25_enquiry_response().
  161. */
  162. void ax25_ds_t1_timeout(ax25_cb *ax25)
  163. {
  164. switch (ax25->state) {
  165. case AX25_STATE_1:
  166. if (ax25->n2count == ax25->n2) {
  167. if (ax25->modulus == AX25_MODULUS) {
  168. ax25_disconnect(ax25, ETIMEDOUT);
  169. return;
  170. } else {
  171. ax25->modulus = AX25_MODULUS;
  172. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  173. ax25->n2count = 0;
  174. ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
  175. }
  176. } else {
  177. ax25->n2count++;
  178. if (ax25->modulus == AX25_MODULUS)
  179. ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
  180. else
  181. ax25_send_control(ax25, AX25_SABME, AX25_POLLOFF, AX25_COMMAND);
  182. }
  183. break;
  184. case AX25_STATE_2:
  185. if (ax25->n2count == ax25->n2) {
  186. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  187. if (!sock_flag(ax25->sk, SOCK_DESTROY))
  188. ax25_disconnect(ax25, ETIMEDOUT);
  189. return;
  190. } else {
  191. ax25->n2count++;
  192. }
  193. break;
  194. case AX25_STATE_3:
  195. if (ax25->n2count == ax25->n2) {
  196. ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
  197. ax25_disconnect(ax25, ETIMEDOUT);
  198. return;
  199. } else {
  200. ax25->n2count++;
  201. }
  202. break;
  203. }
  204. ax25_calculate_t1(ax25);
  205. ax25_start_t1timer(ax25);
  206. }