ax25_ds_timer.c 5.8 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 <asm/uaccess.h>
  28. #include <asm/system.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. static void ax25_ds_timeout(unsigned long);
  33. /*
  34. * Add DAMA slave timeout timer to timer list.
  35. * Unlike the connection based timers the timeout function gets
  36. * triggered every second. Please note that NET_AX25_DAMA_SLAVE_TIMEOUT
  37. * (aka /proc/sys/net/ax25/{dev}/dama_slave_timeout) is still in
  38. * 1/10th of a second.
  39. */
  40. void ax25_ds_setup_timer(ax25_dev *ax25_dev)
  41. {
  42. setup_timer(&ax25_dev->dama.slave_timer, ax25_ds_timeout,
  43. (unsigned long)ax25_dev);
  44. }
  45. void ax25_ds_del_timer(ax25_dev *ax25_dev)
  46. {
  47. if (ax25_dev)
  48. del_timer(&ax25_dev->dama.slave_timer);
  49. }
  50. void ax25_ds_set_timer(ax25_dev *ax25_dev)
  51. {
  52. if (ax25_dev == NULL) /* paranoia */
  53. return;
  54. ax25_dev->dama.slave_timeout =
  55. msecs_to_jiffies(ax25_dev->values[AX25_VALUES_DS_TIMEOUT]) / 10;
  56. mod_timer(&ax25_dev->dama.slave_timer, jiffies + HZ);
  57. }
  58. /*
  59. * DAMA Slave Timeout
  60. * Silently discard all (slave) connections in case our master forgot us...
  61. */
  62. static void ax25_ds_timeout(unsigned long arg)
  63. {
  64. ax25_dev *ax25_dev = (struct ax25_dev *) arg;
  65. ax25_cb *ax25;
  66. struct hlist_node *node;
  67. if (ax25_dev == NULL || !ax25_dev->dama.slave)
  68. return; /* Yikes! */
  69. if (!ax25_dev->dama.slave_timeout || --ax25_dev->dama.slave_timeout) {
  70. ax25_ds_set_timer(ax25_dev);
  71. return;
  72. }
  73. spin_lock(&ax25_list_lock);
  74. ax25_for_each(ax25, node, &ax25_list) {
  75. if (ax25->ax25_dev != ax25_dev || !(ax25->condition & AX25_COND_DAMA_MODE))
  76. continue;
  77. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  78. ax25_disconnect(ax25, ETIMEDOUT);
  79. }
  80. spin_unlock(&ax25_list_lock);
  81. ax25_dev_dama_off(ax25_dev);
  82. }
  83. void ax25_ds_heartbeat_expiry(ax25_cb *ax25)
  84. {
  85. struct sock *sk=ax25->sk;
  86. if (sk)
  87. bh_lock_sock(sk);
  88. switch (ax25->state) {
  89. case AX25_STATE_0:
  90. /* Magic here: If we listen() and a new link dies before it
  91. is accepted() it isn't 'dead' so doesn't get removed. */
  92. if (!sk || sock_flag(sk, SOCK_DESTROY) ||
  93. (sk->sk_state == TCP_LISTEN &&
  94. sock_flag(sk, SOCK_DEAD))) {
  95. if (sk) {
  96. sock_hold(sk);
  97. ax25_destroy_socket(ax25);
  98. bh_unlock_sock(sk);
  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. 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. }