ax25_std_timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /* Magic here: If we listen() and a new link dies before it
  39. is accepted() it isn't 'dead' so doesn't get removed. */
  40. if (!sk || sock_flag(sk, SOCK_DESTROY) ||
  41. (sk->sk_state == TCP_LISTEN &&
  42. sock_flag(sk, SOCK_DEAD))) {
  43. if (sk) {
  44. sock_hold(sk);
  45. ax25_destroy_socket(ax25);
  46. bh_unlock_sock(sk);
  47. sock_put(sk);
  48. } else
  49. ax25_destroy_socket(ax25);
  50. return;
  51. }
  52. break;
  53. case AX25_STATE_3:
  54. case AX25_STATE_4:
  55. /*
  56. * Check the state of the receive buffer.
  57. */
  58. if (sk != NULL) {
  59. if (atomic_read(&sk->sk_rmem_alloc) <
  60. (sk->sk_rcvbuf >> 1) &&
  61. (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
  62. ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
  63. ax25->condition &= ~AX25_COND_ACK_PENDING;
  64. ax25_send_control(ax25, AX25_RR, AX25_POLLOFF, AX25_RESPONSE);
  65. break;
  66. }
  67. }
  68. }
  69. if (sk)
  70. bh_unlock_sock(sk);
  71. ax25_start_heartbeat(ax25);
  72. }
  73. void ax25_std_t2timer_expiry(ax25_cb *ax25)
  74. {
  75. if (ax25->condition & AX25_COND_ACK_PENDING) {
  76. ax25->condition &= ~AX25_COND_ACK_PENDING;
  77. ax25_std_timeout_response(ax25);
  78. }
  79. }
  80. void ax25_std_t3timer_expiry(ax25_cb *ax25)
  81. {
  82. ax25->n2count = 0;
  83. ax25_std_transmit_enquiry(ax25);
  84. ax25->state = AX25_STATE_4;
  85. }
  86. void ax25_std_idletimer_expiry(ax25_cb *ax25)
  87. {
  88. ax25_clear_queues(ax25);
  89. ax25->n2count = 0;
  90. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  91. ax25->state = AX25_STATE_2;
  92. ax25_calculate_t1(ax25);
  93. ax25_start_t1timer(ax25);
  94. ax25_stop_t2timer(ax25);
  95. ax25_stop_t3timer(ax25);
  96. if (ax25->sk != NULL) {
  97. bh_lock_sock(ax25->sk);
  98. ax25->sk->sk_state = TCP_CLOSE;
  99. ax25->sk->sk_err = 0;
  100. ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
  101. if (!sock_flag(ax25->sk, SOCK_DEAD)) {
  102. ax25->sk->sk_state_change(ax25->sk);
  103. sock_set_flag(ax25->sk, SOCK_DEAD);
  104. }
  105. bh_unlock_sock(ax25->sk);
  106. }
  107. }
  108. void ax25_std_t1timer_expiry(ax25_cb *ax25)
  109. {
  110. switch (ax25->state) {
  111. case AX25_STATE_1:
  112. if (ax25->n2count == ax25->n2) {
  113. if (ax25->modulus == AX25_MODULUS) {
  114. ax25_disconnect(ax25, ETIMEDOUT);
  115. return;
  116. } else {
  117. ax25->modulus = AX25_MODULUS;
  118. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  119. ax25->n2count = 0;
  120. ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
  121. }
  122. } else {
  123. ax25->n2count++;
  124. if (ax25->modulus == AX25_MODULUS)
  125. ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
  126. else
  127. ax25_send_control(ax25, AX25_SABME, AX25_POLLON, AX25_COMMAND);
  128. }
  129. break;
  130. case AX25_STATE_2:
  131. if (ax25->n2count == ax25->n2) {
  132. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  133. ax25_disconnect(ax25, ETIMEDOUT);
  134. return;
  135. } else {
  136. ax25->n2count++;
  137. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  138. }
  139. break;
  140. case AX25_STATE_3:
  141. ax25->n2count = 1;
  142. ax25_std_transmit_enquiry(ax25);
  143. ax25->state = AX25_STATE_4;
  144. break;
  145. case AX25_STATE_4:
  146. if (ax25->n2count == ax25->n2) {
  147. ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
  148. ax25_disconnect(ax25, ETIMEDOUT);
  149. return;
  150. } else {
  151. ax25->n2count++;
  152. ax25_std_transmit_enquiry(ax25);
  153. }
  154. break;
  155. }
  156. ax25_calculate_t1(ax25);
  157. ax25_start_t1timer(ax25);
  158. }