lapb_out.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/timer.h>
  18. #include <linux/string.h>
  19. #include <linux/sockios.h>
  20. #include <linux/net.h>
  21. #include <linux/inet.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/slab.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. /*
  31. * This procedure is passed a buffer descriptor for an iframe. It builds
  32. * the rest of the control part of the frame and then writes it out.
  33. */
  34. static void lapb_send_iframe(struct lapb_cb *lapb, struct sk_buff *skb, int poll_bit)
  35. {
  36. unsigned char *frame;
  37. if (!skb)
  38. return;
  39. if (lapb->mode & LAPB_EXTENDED) {
  40. frame = skb_push(skb, 2);
  41. frame[0] = LAPB_I;
  42. frame[0] |= lapb->vs << 1;
  43. frame[1] = poll_bit ? LAPB_EPF : 0;
  44. frame[1] |= lapb->vr << 1;
  45. } else {
  46. frame = skb_push(skb, 1);
  47. *frame = LAPB_I;
  48. *frame |= poll_bit ? LAPB_SPF : 0;
  49. *frame |= lapb->vr << 5;
  50. *frame |= lapb->vs << 1;
  51. }
  52. lapb_dbg(1, "(%p) S%d TX I(%d) S%d R%d\n",
  53. lapb->dev, lapb->state, poll_bit, lapb->vs, lapb->vr);
  54. lapb_transmit_buffer(lapb, skb, LAPB_COMMAND);
  55. }
  56. void lapb_kick(struct lapb_cb *lapb)
  57. {
  58. struct sk_buff *skb, *skbn;
  59. unsigned short modulus, start, end;
  60. modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
  61. start = !skb_peek(&lapb->ack_queue) ? lapb->va : lapb->vs;
  62. end = (lapb->va + lapb->window) % modulus;
  63. if (!(lapb->condition & LAPB_PEER_RX_BUSY_CONDITION) &&
  64. start != end && skb_peek(&lapb->write_queue)) {
  65. lapb->vs = start;
  66. /*
  67. * Dequeue the frame and copy it.
  68. */
  69. skb = skb_dequeue(&lapb->write_queue);
  70. do {
  71. skbn = skb_copy(skb, GFP_ATOMIC);
  72. if (!skbn) {
  73. skb_queue_head(&lapb->write_queue, skb);
  74. break;
  75. }
  76. if (skb->sk)
  77. skb_set_owner_w(skbn, skb->sk);
  78. /*
  79. * Transmit the frame copy.
  80. */
  81. lapb_send_iframe(lapb, skbn, LAPB_POLLOFF);
  82. lapb->vs = (lapb->vs + 1) % modulus;
  83. /*
  84. * Requeue the original data frame.
  85. */
  86. skb_queue_tail(&lapb->ack_queue, skb);
  87. } while (lapb->vs != end && (skb = skb_dequeue(&lapb->write_queue)) != NULL);
  88. lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
  89. if (!lapb_t1timer_running(lapb))
  90. lapb_start_t1timer(lapb);
  91. }
  92. }
  93. void lapb_transmit_buffer(struct lapb_cb *lapb, struct sk_buff *skb, int type)
  94. {
  95. unsigned char *ptr;
  96. ptr = skb_push(skb, 1);
  97. if (lapb->mode & LAPB_MLP) {
  98. if (lapb->mode & LAPB_DCE) {
  99. if (type == LAPB_COMMAND)
  100. *ptr = LAPB_ADDR_C;
  101. if (type == LAPB_RESPONSE)
  102. *ptr = LAPB_ADDR_D;
  103. } else {
  104. if (type == LAPB_COMMAND)
  105. *ptr = LAPB_ADDR_D;
  106. if (type == LAPB_RESPONSE)
  107. *ptr = LAPB_ADDR_C;
  108. }
  109. } else {
  110. if (lapb->mode & LAPB_DCE) {
  111. if (type == LAPB_COMMAND)
  112. *ptr = LAPB_ADDR_A;
  113. if (type == LAPB_RESPONSE)
  114. *ptr = LAPB_ADDR_B;
  115. } else {
  116. if (type == LAPB_COMMAND)
  117. *ptr = LAPB_ADDR_B;
  118. if (type == LAPB_RESPONSE)
  119. *ptr = LAPB_ADDR_A;
  120. }
  121. }
  122. lapb_dbg(2, "(%p) S%d TX %3ph\n", lapb->dev, lapb->state, skb->data);
  123. if (!lapb_data_transmit(lapb, skb))
  124. kfree_skb(skb);
  125. }
  126. void lapb_establish_data_link(struct lapb_cb *lapb)
  127. {
  128. lapb->condition = 0x00;
  129. lapb->n2count = 0;
  130. if (lapb->mode & LAPB_EXTENDED) {
  131. lapb_dbg(1, "(%p) S%d TX SABME(1)\n", lapb->dev, lapb->state);
  132. lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
  133. } else {
  134. lapb_dbg(1, "(%p) S%d TX SABM(1)\n", lapb->dev, lapb->state);
  135. lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
  136. }
  137. lapb_start_t1timer(lapb);
  138. lapb_stop_t2timer(lapb);
  139. }
  140. void lapb_enquiry_response(struct lapb_cb *lapb)
  141. {
  142. lapb_dbg(1, "(%p) S%d TX RR(1) R%d\n",
  143. lapb->dev, lapb->state, lapb->vr);
  144. lapb_send_control(lapb, LAPB_RR, LAPB_POLLON, LAPB_RESPONSE);
  145. lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
  146. }
  147. void lapb_timeout_response(struct lapb_cb *lapb)
  148. {
  149. lapb_dbg(1, "(%p) S%d TX RR(0) R%d\n",
  150. lapb->dev, lapb->state, lapb->vr);
  151. lapb_send_control(lapb, LAPB_RR, LAPB_POLLOFF, LAPB_RESPONSE);
  152. lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
  153. }
  154. void lapb_check_iframes_acked(struct lapb_cb *lapb, unsigned short nr)
  155. {
  156. if (lapb->vs == nr) {
  157. lapb_frames_acked(lapb, nr);
  158. lapb_stop_t1timer(lapb);
  159. lapb->n2count = 0;
  160. } else if (lapb->va != nr) {
  161. lapb_frames_acked(lapb, nr);
  162. lapb_start_t1timer(lapb);
  163. }
  164. }
  165. void lapb_check_need_response(struct lapb_cb *lapb, int type, int pf)
  166. {
  167. if (type == LAPB_COMMAND && pf)
  168. lapb_enquiry_response(lapb);
  169. }