ax25_ds_timer.c 5.9 KB

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