lapb_timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * LAPB release 002
  4. *
  5. * This code REQUIRES 2.1.15 or higher/ NET3.038
  6. *
  7. * History
  8. * LAPB 001 Jonathan Naylor Started Coding
  9. * LAPB 002 Jonathan Naylor New timer architecture.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/jiffies.h>
  18. #include <linux/timer.h>
  19. #include <linux/string.h>
  20. #include <linux/sockios.h>
  21. #include <linux/net.h>
  22. #include <linux/inet.h>
  23. #include <linux/skbuff.h>
  24. #include <net/sock.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/mm.h>
  28. #include <linux/interrupt.h>
  29. #include <net/lapb.h>
  30. static void lapb_t1timer_expiry(struct timer_list *);
  31. static void lapb_t2timer_expiry(struct timer_list *);
  32. void lapb_start_t1timer(struct lapb_cb *lapb)
  33. {
  34. del_timer(&lapb->t1timer);
  35. lapb->t1timer.function = lapb_t1timer_expiry;
  36. lapb->t1timer.expires = jiffies + lapb->t1;
  37. lapb->t1timer_running = true;
  38. add_timer(&lapb->t1timer);
  39. }
  40. void lapb_start_t2timer(struct lapb_cb *lapb)
  41. {
  42. del_timer(&lapb->t2timer);
  43. lapb->t2timer.function = lapb_t2timer_expiry;
  44. lapb->t2timer.expires = jiffies + lapb->t2;
  45. lapb->t2timer_running = true;
  46. add_timer(&lapb->t2timer);
  47. }
  48. void lapb_stop_t1timer(struct lapb_cb *lapb)
  49. {
  50. lapb->t1timer_running = false;
  51. del_timer(&lapb->t1timer);
  52. }
  53. void lapb_stop_t2timer(struct lapb_cb *lapb)
  54. {
  55. lapb->t2timer_running = false;
  56. del_timer(&lapb->t2timer);
  57. }
  58. int lapb_t1timer_running(struct lapb_cb *lapb)
  59. {
  60. return lapb->t1timer_running;
  61. }
  62. static void lapb_t2timer_expiry(struct timer_list *t)
  63. {
  64. struct lapb_cb *lapb = from_timer(lapb, t, t2timer);
  65. spin_lock_bh(&lapb->lock);
  66. if (timer_pending(&lapb->t2timer)) /* A new timer has been set up */
  67. goto out;
  68. if (!lapb->t2timer_running) /* The timer has been stopped */
  69. goto out;
  70. if (lapb->condition & LAPB_ACK_PENDING_CONDITION) {
  71. lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
  72. lapb_timeout_response(lapb);
  73. }
  74. lapb->t2timer_running = false;
  75. out:
  76. spin_unlock_bh(&lapb->lock);
  77. }
  78. static void lapb_t1timer_expiry(struct timer_list *t)
  79. {
  80. struct lapb_cb *lapb = from_timer(lapb, t, t1timer);
  81. spin_lock_bh(&lapb->lock);
  82. if (timer_pending(&lapb->t1timer)) /* A new timer has been set up */
  83. goto out;
  84. if (!lapb->t1timer_running) /* The timer has been stopped */
  85. goto out;
  86. switch (lapb->state) {
  87. /*
  88. * If we are a DCE, send DM up to N2 times, then switch to
  89. * STATE_1 and send SABM(E).
  90. */
  91. case LAPB_STATE_0:
  92. if (lapb->mode & LAPB_DCE &&
  93. lapb->n2count != lapb->n2) {
  94. lapb->n2count++;
  95. lapb_send_control(lapb, LAPB_DM, LAPB_POLLOFF, LAPB_RESPONSE);
  96. } else {
  97. lapb->state = LAPB_STATE_1;
  98. lapb_establish_data_link(lapb);
  99. }
  100. break;
  101. /*
  102. * Awaiting connection state, send SABM(E), up to N2 times.
  103. */
  104. case LAPB_STATE_1:
  105. if (lapb->n2count == lapb->n2) {
  106. lapb_clear_queues(lapb);
  107. lapb->state = LAPB_STATE_0;
  108. lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
  109. lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
  110. lapb->t1timer_running = false;
  111. goto out;
  112. } else {
  113. lapb->n2count++;
  114. if (lapb->mode & LAPB_EXTENDED) {
  115. lapb_dbg(1, "(%p) S1 TX SABME(1)\n",
  116. lapb->dev);
  117. lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
  118. } else {
  119. lapb_dbg(1, "(%p) S1 TX SABM(1)\n",
  120. lapb->dev);
  121. lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
  122. }
  123. }
  124. break;
  125. /*
  126. * Awaiting disconnection state, send DISC, up to N2 times.
  127. */
  128. case LAPB_STATE_2:
  129. if (lapb->n2count == lapb->n2) {
  130. lapb_clear_queues(lapb);
  131. lapb->state = LAPB_STATE_0;
  132. lapb_disconnect_confirmation(lapb, LAPB_TIMEDOUT);
  133. lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
  134. lapb->t1timer_running = false;
  135. goto out;
  136. } else {
  137. lapb->n2count++;
  138. lapb_dbg(1, "(%p) S2 TX DISC(1)\n", lapb->dev);
  139. lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
  140. }
  141. break;
  142. /*
  143. * Data transfer state, restransmit I frames, up to N2 times.
  144. */
  145. case LAPB_STATE_3:
  146. if (lapb->n2count == lapb->n2) {
  147. lapb_clear_queues(lapb);
  148. lapb->state = LAPB_STATE_0;
  149. lapb_stop_t2timer(lapb);
  150. lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
  151. lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
  152. lapb->t1timer_running = false;
  153. goto out;
  154. } else {
  155. lapb->n2count++;
  156. lapb_requeue_frames(lapb);
  157. lapb_kick(lapb);
  158. }
  159. break;
  160. /*
  161. * Frame reject state, restransmit FRMR frames, up to N2 times.
  162. */
  163. case LAPB_STATE_4:
  164. if (lapb->n2count == lapb->n2) {
  165. lapb_clear_queues(lapb);
  166. lapb->state = LAPB_STATE_0;
  167. lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
  168. lapb_dbg(0, "(%p) S4 -> S0\n", lapb->dev);
  169. lapb->t1timer_running = false;
  170. goto out;
  171. } else {
  172. lapb->n2count++;
  173. lapb_transmit_frmr(lapb);
  174. }
  175. break;
  176. }
  177. lapb_start_t1timer(lapb);
  178. out:
  179. spin_unlock_bh(&lapb->lock);
  180. }