ax25_ds_timer.c 5.9 KB

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