ax25_ds_timer.c 5.9 KB

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