lapb_out.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * LAPB release 002
  3. *
  4. * This code REQUIRES 2.1.15 or higher/ NET3.038
  5. *
  6. * This module:
  7. * This module is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * History
  13. * LAPB 001 Jonathan Naylor Started Coding
  14. * LAPB 002 Jonathan Naylor New timer architecture.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/errno.h>
  18. #include <linux/types.h>
  19. #include <linux/socket.h>
  20. #include <linux/in.h>
  21. #include <linux/kernel.h>
  22. #include <linux/timer.h>
  23. #include <linux/string.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/inet.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/slab.h>
  29. #include <net/sock.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. #include <net/lapb.h>
  35. /*
  36. * This procedure is passed a buffer descriptor for an iframe. It builds
  37. * the rest of the control part of the frame and then writes it out.
  38. */
  39. static void lapb_send_iframe(struct lapb_cb *lapb, struct sk_buff *skb, int poll_bit)
  40. {
  41. unsigned char *frame;
  42. if (!skb)
  43. return;
  44. if (lapb->mode & LAPB_EXTENDED) {
  45. frame = skb_push(skb, 2);
  46. frame[0] = LAPB_I;
  47. frame[0] |= lapb->vs << 1;
  48. frame[1] = poll_bit ? LAPB_EPF : 0;
  49. frame[1] |= lapb->vr << 1;
  50. } else {
  51. frame = skb_push(skb, 1);
  52. *frame = LAPB_I;
  53. *frame |= poll_bit ? LAPB_SPF : 0;
  54. *frame |= lapb->vr << 5;
  55. *frame |= lapb->vs << 1;
  56. }
  57. lapb_dbg(1, "(%p) S%d TX I(%d) S%d R%d\n",
  58. lapb->dev, lapb->state, poll_bit, lapb->vs, lapb->vr);
  59. lapb_transmit_buffer(lapb, skb, LAPB_COMMAND);
  60. }
  61. void lapb_kick(struct lapb_cb *lapb)
  62. {
  63. struct sk_buff *skb, *skbn;
  64. unsigned short modulus, start, end;
  65. modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
  66. start = !skb_peek(&lapb->ack_queue) ? lapb->va : lapb->vs;
  67. end = (lapb->va + lapb->window) % modulus;
  68. if (!(lapb->condition & LAPB_PEER_RX_BUSY_CONDITION) &&
  69. start != end && skb_peek(&lapb->write_queue)) {
  70. lapb->vs = start;
  71. /*
  72. * Dequeue the frame and copy it.
  73. */
  74. skb = skb_dequeue(&lapb->write_queue);
  75. do {
  76. if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  77. skb_queue_head(&lapb->write_queue, skb);
  78. break;
  79. }
  80. if (skb->sk)
  81. skb_set_owner_w(skbn, skb->sk);
  82. /*
  83. * Transmit the frame copy.
  84. */
  85. lapb_send_iframe(lapb, skbn, LAPB_POLLOFF);
  86. lapb->vs = (lapb->vs + 1) % modulus;
  87. /*
  88. * Requeue the original data frame.
  89. */
  90. skb_queue_tail(&lapb->ack_queue, skb);
  91. } while (lapb->vs != end && (skb = skb_dequeue(&lapb->write_queue)) != NULL);
  92. lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
  93. if (!lapb_t1timer_running(lapb))
  94. lapb_start_t1timer(lapb);
  95. }
  96. }
  97. void lapb_transmit_buffer(struct lapb_cb *lapb, struct sk_buff *skb, int type)
  98. {
  99. unsigned char *ptr;
  100. ptr = skb_push(skb, 1);
  101. if (lapb->mode & LAPB_MLP) {
  102. if (lapb->mode & LAPB_DCE) {
  103. if (type == LAPB_COMMAND)
  104. *ptr = LAPB_ADDR_C;
  105. if (type == LAPB_RESPONSE)
  106. *ptr = LAPB_ADDR_D;
  107. } else {
  108. if (type == LAPB_COMMAND)
  109. *ptr = LAPB_ADDR_D;
  110. if (type == LAPB_RESPONSE)
  111. *ptr = LAPB_ADDR_C;
  112. }
  113. } else {
  114. if (lapb->mode & LAPB_DCE) {
  115. if (type == LAPB_COMMAND)
  116. *ptr = LAPB_ADDR_A;
  117. if (type == LAPB_RESPONSE)
  118. *ptr = LAPB_ADDR_B;
  119. } else {
  120. if (type == LAPB_COMMAND)
  121. *ptr = LAPB_ADDR_B;
  122. if (type == LAPB_RESPONSE)
  123. *ptr = LAPB_ADDR_A;
  124. }
  125. }
  126. lapb_dbg(2, "(%p) S%d TX %3ph\n", lapb->dev, lapb->state, skb->data);
  127. if (!lapb_data_transmit(lapb, skb))
  128. kfree_skb(skb);
  129. }
  130. void lapb_establish_data_link(struct lapb_cb *lapb)
  131. {
  132. lapb->condition = 0x00;
  133. lapb->n2count = 0;
  134. if (lapb->mode & LAPB_EXTENDED) {
  135. lapb_dbg(1, "(%p) S%d TX SABME(1)\n", lapb->dev, lapb->state);
  136. lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
  137. } else {
  138. lapb_dbg(1, "(%p) S%d TX SABM(1)\n", lapb->dev, lapb->state);
  139. lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
  140. }
  141. lapb_start_t1timer(lapb);
  142. lapb_stop_t2timer(lapb);
  143. }
  144. void lapb_enquiry_response(struct lapb_cb *lapb)
  145. {
  146. lapb_dbg(1, "(%p) S%d TX RR(1) R%d\n",
  147. lapb->dev, lapb->state, lapb->vr);
  148. lapb_send_control(lapb, LAPB_RR, LAPB_POLLON, LAPB_RESPONSE);
  149. lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
  150. }
  151. void lapb_timeout_response(struct lapb_cb *lapb)
  152. {
  153. lapb_dbg(1, "(%p) S%d TX RR(0) R%d\n",
  154. lapb->dev, lapb->state, lapb->vr);
  155. lapb_send_control(lapb, LAPB_RR, LAPB_POLLOFF, LAPB_RESPONSE);
  156. lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
  157. }
  158. void lapb_check_iframes_acked(struct lapb_cb *lapb, unsigned short nr)
  159. {
  160. if (lapb->vs == nr) {
  161. lapb_frames_acked(lapb, nr);
  162. lapb_stop_t1timer(lapb);
  163. lapb->n2count = 0;
  164. } else if (lapb->va != nr) {
  165. lapb_frames_acked(lapb, nr);
  166. lapb_start_t1timer(lapb);
  167. }
  168. }
  169. void lapb_check_need_response(struct lapb_cb *lapb, int type, int pf)
  170. {
  171. if (type == LAPB_COMMAND && pf)
  172. lapb_enquiry_response(lapb);
  173. }