ax25_std_timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <asm/system.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. void ax25_std_heartbeat_expiry(ax25_cb *ax25)
  33. {
  34. struct sock *sk = ax25->sk;
  35. if (sk)
  36. bh_lock_sock(sk);
  37. switch (ax25->state) {
  38. case AX25_STATE_0:
  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. sock_put(sk);
  49. } else
  50. ax25_destroy_socket(ax25);
  51. return;
  52. }
  53. break;
  54. case AX25_STATE_3:
  55. case AX25_STATE_4:
  56. /*
  57. * Check the state of the receive buffer.
  58. */
  59. if (sk != NULL) {
  60. if (atomic_read(&sk->sk_rmem_alloc) <
  61. (sk->sk_rcvbuf >> 1) &&
  62. (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
  63. ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
  64. ax25->condition &= ~AX25_COND_ACK_PENDING;
  65. ax25_send_control(ax25, AX25_RR, AX25_POLLOFF, AX25_RESPONSE);
  66. break;
  67. }
  68. }
  69. }
  70. if (sk)
  71. bh_unlock_sock(sk);
  72. ax25_start_heartbeat(ax25);
  73. }
  74. void ax25_std_t2timer_expiry(ax25_cb *ax25)
  75. {
  76. if (ax25->condition & AX25_COND_ACK_PENDING) {
  77. ax25->condition &= ~AX25_COND_ACK_PENDING;
  78. ax25_std_timeout_response(ax25);
  79. }
  80. }
  81. void ax25_std_t3timer_expiry(ax25_cb *ax25)
  82. {
  83. ax25->n2count = 0;
  84. ax25_std_transmit_enquiry(ax25);
  85. ax25->state = AX25_STATE_4;
  86. }
  87. void ax25_std_idletimer_expiry(ax25_cb *ax25)
  88. {
  89. ax25_clear_queues(ax25);
  90. ax25->n2count = 0;
  91. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  92. ax25->state = AX25_STATE_2;
  93. ax25_calculate_t1(ax25);
  94. ax25_start_t1timer(ax25);
  95. ax25_stop_t2timer(ax25);
  96. ax25_stop_t3timer(ax25);
  97. if (ax25->sk != NULL) {
  98. bh_lock_sock(ax25->sk);
  99. ax25->sk->sk_state = TCP_CLOSE;
  100. ax25->sk->sk_err = 0;
  101. ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
  102. if (!sock_flag(ax25->sk, SOCK_DEAD)) {
  103. ax25->sk->sk_state_change(ax25->sk);
  104. sock_set_flag(ax25->sk, SOCK_DEAD);
  105. }
  106. bh_unlock_sock(ax25->sk);
  107. }
  108. }
  109. void ax25_std_t1timer_expiry(ax25_cb *ax25)
  110. {
  111. switch (ax25->state) {
  112. case AX25_STATE_1:
  113. if (ax25->n2count == ax25->n2) {
  114. if (ax25->modulus == AX25_MODULUS) {
  115. ax25_disconnect(ax25, ETIMEDOUT);
  116. return;
  117. } else {
  118. ax25->modulus = AX25_MODULUS;
  119. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  120. ax25->n2count = 0;
  121. ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
  122. }
  123. } else {
  124. ax25->n2count++;
  125. if (ax25->modulus == AX25_MODULUS)
  126. ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
  127. else
  128. ax25_send_control(ax25, AX25_SABME, AX25_POLLON, AX25_COMMAND);
  129. }
  130. break;
  131. case AX25_STATE_2:
  132. if (ax25->n2count == ax25->n2) {
  133. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  134. ax25_disconnect(ax25, ETIMEDOUT);
  135. return;
  136. } else {
  137. ax25->n2count++;
  138. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  139. }
  140. break;
  141. case AX25_STATE_3:
  142. ax25->n2count = 1;
  143. ax25_std_transmit_enquiry(ax25);
  144. ax25->state = AX25_STATE_4;
  145. break;
  146. case AX25_STATE_4:
  147. if (ax25->n2count == ax25->n2) {
  148. ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
  149. ax25_disconnect(ax25, ETIMEDOUT);
  150. return;
  151. } else {
  152. ax25->n2count++;
  153. ax25_std_transmit_enquiry(ax25);
  154. }
  155. break;
  156. }
  157. ax25_calculate_t1(ax25);
  158. ax25_start_t1timer(ax25);
  159. }