ax25_std_timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  8. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  9. * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
  10. * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr)
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/socket.h>
  15. #include <linux/in.h>
  16. #include <linux/kernel.h>
  17. #include <linux/timer.h>
  18. #include <linux/string.h>
  19. #include <linux/sockios.h>
  20. #include <linux/net.h>
  21. #include <net/ax25.h>
  22. #include <linux/inet.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <net/sock.h>
  26. #include <net/tcp_states.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/mm.h>
  30. #include <linux/interrupt.h>
  31. void ax25_std_heartbeat_expiry(ax25_cb *ax25)
  32. {
  33. struct sock *sk = ax25->sk;
  34. if (sk)
  35. bh_lock_sock(sk);
  36. switch (ax25->state) {
  37. case AX25_STATE_0:
  38. case AX25_STATE_2:
  39. /* Magic here: If we listen() and a new link dies before it
  40. is accepted() it isn't 'dead' so doesn't get removed. */
  41. if (!sk || sock_flag(sk, SOCK_DESTROY) ||
  42. (sk->sk_state == TCP_LISTEN &&
  43. sock_flag(sk, SOCK_DEAD))) {
  44. if (sk) {
  45. sock_hold(sk);
  46. ax25_destroy_socket(ax25);
  47. bh_unlock_sock(sk);
  48. /* Ungrab socket and destroy it */
  49. sock_put(sk);
  50. } else
  51. ax25_destroy_socket(ax25);
  52. return;
  53. }
  54. break;
  55. case AX25_STATE_3:
  56. case AX25_STATE_4:
  57. /*
  58. * Check the state of the receive buffer.
  59. */
  60. if (sk != NULL) {
  61. if (atomic_read(&sk->sk_rmem_alloc) <
  62. (sk->sk_rcvbuf >> 1) &&
  63. (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
  64. ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
  65. ax25->condition &= ~AX25_COND_ACK_PENDING;
  66. ax25_send_control(ax25, AX25_RR, AX25_POLLOFF, AX25_RESPONSE);
  67. break;
  68. }
  69. }
  70. }
  71. if (sk)
  72. bh_unlock_sock(sk);
  73. ax25_start_heartbeat(ax25);
  74. }
  75. void ax25_std_t2timer_expiry(ax25_cb *ax25)
  76. {
  77. if (ax25->condition & AX25_COND_ACK_PENDING) {
  78. ax25->condition &= ~AX25_COND_ACK_PENDING;
  79. ax25_std_timeout_response(ax25);
  80. }
  81. }
  82. void ax25_std_t3timer_expiry(ax25_cb *ax25)
  83. {
  84. ax25->n2count = 0;
  85. ax25_std_transmit_enquiry(ax25);
  86. ax25->state = AX25_STATE_4;
  87. }
  88. void ax25_std_idletimer_expiry(ax25_cb *ax25)
  89. {
  90. ax25_clear_queues(ax25);
  91. ax25->n2count = 0;
  92. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  93. ax25->state = AX25_STATE_2;
  94. ax25_calculate_t1(ax25);
  95. ax25_start_t1timer(ax25);
  96. ax25_stop_t2timer(ax25);
  97. ax25_stop_t3timer(ax25);
  98. if (ax25->sk != NULL) {
  99. bh_lock_sock(ax25->sk);
  100. ax25->sk->sk_state = TCP_CLOSE;
  101. ax25->sk->sk_err = 0;
  102. ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
  103. if (!sock_flag(ax25->sk, SOCK_DEAD)) {
  104. ax25->sk->sk_state_change(ax25->sk);
  105. sock_set_flag(ax25->sk, SOCK_DEAD);
  106. }
  107. bh_unlock_sock(ax25->sk);
  108. }
  109. }
  110. void ax25_std_t1timer_expiry(ax25_cb *ax25)
  111. {
  112. switch (ax25->state) {
  113. case AX25_STATE_1:
  114. if (ax25->n2count == ax25->n2) {
  115. if (ax25->modulus == AX25_MODULUS) {
  116. ax25_disconnect(ax25, ETIMEDOUT);
  117. return;
  118. } else {
  119. ax25->modulus = AX25_MODULUS;
  120. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  121. ax25->n2count = 0;
  122. ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
  123. }
  124. } else {
  125. ax25->n2count++;
  126. if (ax25->modulus == AX25_MODULUS)
  127. ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
  128. else
  129. ax25_send_control(ax25, AX25_SABME, AX25_POLLON, AX25_COMMAND);
  130. }
  131. break;
  132. case AX25_STATE_2:
  133. if (ax25->n2count == ax25->n2) {
  134. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  135. if (!sock_flag(ax25->sk, SOCK_DESTROY))
  136. ax25_disconnect(ax25, ETIMEDOUT);
  137. return;
  138. } else {
  139. ax25->n2count++;
  140. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  141. }
  142. break;
  143. case AX25_STATE_3:
  144. ax25->n2count = 1;
  145. ax25_std_transmit_enquiry(ax25);
  146. ax25->state = AX25_STATE_4;
  147. break;
  148. case AX25_STATE_4:
  149. if (ax25->n2count == ax25->n2) {
  150. ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
  151. ax25_disconnect(ax25, ETIMEDOUT);
  152. return;
  153. } else {
  154. ax25->n2count++;
  155. ax25_std_transmit_enquiry(ax25);
  156. }
  157. break;
  158. }
  159. ax25_calculate_t1(ax25);
  160. ax25_start_t1timer(ax25);
  161. }