lapb_out.c 5.2 KB

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