ax25_ds_timer.c 5.9 KB

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