ax25_std_timer.c 4.1 KB

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