ax25_ds_timer.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /* 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. sock_put(sk);
  98. } else
  99. ax25_destroy_socket(ax25);
  100. return;
  101. }
  102. break;
  103. case AX25_STATE_3:
  104. /*
  105. * Check the state of the receive buffer.
  106. */
  107. if (sk != NULL) {
  108. if (atomic_read(&sk->sk_rmem_alloc) <
  109. (sk->sk_rcvbuf >> 1) &&
  110. (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
  111. ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
  112. ax25->condition &= ~AX25_COND_ACK_PENDING;
  113. break;
  114. }
  115. }
  116. break;
  117. }
  118. if (sk)
  119. bh_unlock_sock(sk);
  120. ax25_start_heartbeat(ax25);
  121. }
  122. /* dl1bke 960114: T3 works much like the IDLE timeout, but
  123. * gets reloaded with every frame for this
  124. * connection.
  125. */
  126. void ax25_ds_t3timer_expiry(ax25_cb *ax25)
  127. {
  128. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  129. ax25_dama_off(ax25);
  130. ax25_disconnect(ax25, ETIMEDOUT);
  131. }
  132. /* dl1bke 960228: close the connection when IDLE expires.
  133. * unlike T3 this timer gets reloaded only on
  134. * I frames.
  135. */
  136. void ax25_ds_idletimer_expiry(ax25_cb *ax25)
  137. {
  138. ax25_clear_queues(ax25);
  139. ax25->n2count = 0;
  140. ax25->state = AX25_STATE_2;
  141. ax25_calculate_t1(ax25);
  142. ax25_start_t1timer(ax25);
  143. ax25_stop_t3timer(ax25);
  144. if (ax25->sk != NULL) {
  145. bh_lock_sock(ax25->sk);
  146. ax25->sk->sk_state = TCP_CLOSE;
  147. ax25->sk->sk_err = 0;
  148. ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
  149. if (!sock_flag(ax25->sk, SOCK_DEAD)) {
  150. ax25->sk->sk_state_change(ax25->sk);
  151. sock_set_flag(ax25->sk, SOCK_DEAD);
  152. }
  153. bh_unlock_sock(ax25->sk);
  154. }
  155. }
  156. /* dl1bke 960114: The DAMA protocol requires to send data and SABM/DISC
  157. * within the poll of any connected channel. Remember
  158. * that we are not allowed to send anything unless we
  159. * get polled by the Master.
  160. *
  161. * Thus we'll have to do parts of our T1 handling in
  162. * ax25_enquiry_response().
  163. */
  164. void ax25_ds_t1_timeout(ax25_cb *ax25)
  165. {
  166. switch (ax25->state) {
  167. case AX25_STATE_1:
  168. if (ax25->n2count == ax25->n2) {
  169. if (ax25->modulus == AX25_MODULUS) {
  170. ax25_disconnect(ax25, ETIMEDOUT);
  171. return;
  172. } else {
  173. ax25->modulus = AX25_MODULUS;
  174. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  175. ax25->n2count = 0;
  176. ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
  177. }
  178. } else {
  179. ax25->n2count++;
  180. if (ax25->modulus == AX25_MODULUS)
  181. ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
  182. else
  183. ax25_send_control(ax25, AX25_SABME, AX25_POLLOFF, AX25_COMMAND);
  184. }
  185. break;
  186. case AX25_STATE_2:
  187. if (ax25->n2count == ax25->n2) {
  188. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  189. ax25_disconnect(ax25, ETIMEDOUT);
  190. return;
  191. } else {
  192. ax25->n2count++;
  193. }
  194. break;
  195. case AX25_STATE_3:
  196. if (ax25->n2count == ax25->n2) {
  197. ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
  198. ax25_disconnect(ax25, ETIMEDOUT);
  199. return;
  200. } else {
  201. ax25->n2count++;
  202. }
  203. break;
  204. }
  205. ax25_calculate_t1(ax25);
  206. ax25_start_t1timer(ax25);
  207. }