lapb_subr.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. */
  15. #include <linux/errno.h>
  16. #include <linux/types.h>
  17. #include <linux/socket.h>
  18. #include <linux/in.h>
  19. #include <linux/kernel.h>
  20. #include <linux/timer.h>
  21. #include <linux/string.h>
  22. #include <linux/sockios.h>
  23. #include <linux/net.h>
  24. #include <linux/inet.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/slab.h>
  27. #include <net/sock.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. #include <net/lapb.h>
  33. /*
  34. * This routine purges all the queues of frames.
  35. */
  36. void lapb_clear_queues(struct lapb_cb *lapb)
  37. {
  38. skb_queue_purge(&lapb->write_queue);
  39. skb_queue_purge(&lapb->ack_queue);
  40. }
  41. /*
  42. * This routine purges the input queue of those frames that have been
  43. * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
  44. * SDL diagram.
  45. */
  46. void lapb_frames_acked(struct lapb_cb *lapb, unsigned short nr)
  47. {
  48. struct sk_buff *skb;
  49. int modulus;
  50. modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
  51. /*
  52. * Remove all the ack-ed frames from the ack queue.
  53. */
  54. if (lapb->va != nr)
  55. while (skb_peek(&lapb->ack_queue) && lapb->va != nr) {
  56. skb = skb_dequeue(&lapb->ack_queue);
  57. kfree_skb(skb);
  58. lapb->va = (lapb->va + 1) % modulus;
  59. }
  60. }
  61. void lapb_requeue_frames(struct lapb_cb *lapb)
  62. {
  63. struct sk_buff *skb, *skb_prev = NULL;
  64. /*
  65. * Requeue all the un-ack-ed frames on the output queue to be picked
  66. * up by lapb_kick called from the timer. This arrangement handles the
  67. * possibility of an empty output queue.
  68. */
  69. while ((skb = skb_dequeue(&lapb->ack_queue)) != NULL) {
  70. if (!skb_prev)
  71. skb_queue_head(&lapb->write_queue, skb);
  72. else
  73. skb_append(skb_prev, skb, &lapb->write_queue);
  74. skb_prev = skb;
  75. }
  76. }
  77. /*
  78. * Validate that the value of nr is between va and vs. Return true or
  79. * false for testing.
  80. */
  81. int lapb_validate_nr(struct lapb_cb *lapb, unsigned short nr)
  82. {
  83. unsigned short vc = lapb->va;
  84. int modulus;
  85. modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
  86. while (vc != lapb->vs) {
  87. if (nr == vc)
  88. return 1;
  89. vc = (vc + 1) % modulus;
  90. }
  91. return nr == lapb->vs;
  92. }
  93. /*
  94. * This routine is the centralised routine for parsing the control
  95. * information for the different frame formats.
  96. */
  97. int lapb_decode(struct lapb_cb *lapb, struct sk_buff *skb,
  98. struct lapb_frame *frame)
  99. {
  100. frame->type = LAPB_ILLEGAL;
  101. #if LAPB_DEBUG > 2
  102. printk(KERN_DEBUG "lapb: (%p) S%d RX %02X %02X %02X\n",
  103. lapb->dev, lapb->state,
  104. skb->data[0], skb->data[1], skb->data[2]);
  105. #endif
  106. /* We always need to look at 2 bytes, sometimes we need
  107. * to look at 3 and those cases are handled below.
  108. */
  109. if (!pskb_may_pull(skb, 2))
  110. return -1;
  111. if (lapb->mode & LAPB_MLP) {
  112. if (lapb->mode & LAPB_DCE) {
  113. if (skb->data[0] == LAPB_ADDR_D)
  114. frame->cr = LAPB_COMMAND;
  115. if (skb->data[0] == LAPB_ADDR_C)
  116. frame->cr = LAPB_RESPONSE;
  117. } else {
  118. if (skb->data[0] == LAPB_ADDR_C)
  119. frame->cr = LAPB_COMMAND;
  120. if (skb->data[0] == LAPB_ADDR_D)
  121. frame->cr = LAPB_RESPONSE;
  122. }
  123. } else {
  124. if (lapb->mode & LAPB_DCE) {
  125. if (skb->data[0] == LAPB_ADDR_B)
  126. frame->cr = LAPB_COMMAND;
  127. if (skb->data[0] == LAPB_ADDR_A)
  128. frame->cr = LAPB_RESPONSE;
  129. } else {
  130. if (skb->data[0] == LAPB_ADDR_A)
  131. frame->cr = LAPB_COMMAND;
  132. if (skb->data[0] == LAPB_ADDR_B)
  133. frame->cr = LAPB_RESPONSE;
  134. }
  135. }
  136. skb_pull(skb, 1);
  137. if (lapb->mode & LAPB_EXTENDED) {
  138. if (!(skb->data[0] & LAPB_S)) {
  139. if (!pskb_may_pull(skb, 2))
  140. return -1;
  141. /*
  142. * I frame - carries NR/NS/PF
  143. */
  144. frame->type = LAPB_I;
  145. frame->ns = (skb->data[0] >> 1) & 0x7F;
  146. frame->nr = (skb->data[1] >> 1) & 0x7F;
  147. frame->pf = skb->data[1] & LAPB_EPF;
  148. frame->control[0] = skb->data[0];
  149. frame->control[1] = skb->data[1];
  150. skb_pull(skb, 2);
  151. } else if ((skb->data[0] & LAPB_U) == 1) {
  152. if (!pskb_may_pull(skb, 2))
  153. return -1;
  154. /*
  155. * S frame - take out PF/NR
  156. */
  157. frame->type = skb->data[0] & 0x0F;
  158. frame->nr = (skb->data[1] >> 1) & 0x7F;
  159. frame->pf = skb->data[1] & LAPB_EPF;
  160. frame->control[0] = skb->data[0];
  161. frame->control[1] = skb->data[1];
  162. skb_pull(skb, 2);
  163. } else if ((skb->data[0] & LAPB_U) == 3) {
  164. /*
  165. * U frame - take out PF
  166. */
  167. frame->type = skb->data[0] & ~LAPB_SPF;
  168. frame->pf = skb->data[0] & LAPB_SPF;
  169. frame->control[0] = skb->data[0];
  170. frame->control[1] = 0x00;
  171. skb_pull(skb, 1);
  172. }
  173. } else {
  174. if (!(skb->data[0] & LAPB_S)) {
  175. /*
  176. * I frame - carries NR/NS/PF
  177. */
  178. frame->type = LAPB_I;
  179. frame->ns = (skb->data[0] >> 1) & 0x07;
  180. frame->nr = (skb->data[0] >> 5) & 0x07;
  181. frame->pf = skb->data[0] & LAPB_SPF;
  182. } else if ((skb->data[0] & LAPB_U) == 1) {
  183. /*
  184. * S frame - take out PF/NR
  185. */
  186. frame->type = skb->data[0] & 0x0F;
  187. frame->nr = (skb->data[0] >> 5) & 0x07;
  188. frame->pf = skb->data[0] & LAPB_SPF;
  189. } else if ((skb->data[0] & LAPB_U) == 3) {
  190. /*
  191. * U frame - take out PF
  192. */
  193. frame->type = skb->data[0] & ~LAPB_SPF;
  194. frame->pf = skb->data[0] & LAPB_SPF;
  195. }
  196. frame->control[0] = skb->data[0];
  197. skb_pull(skb, 1);
  198. }
  199. return 0;
  200. }
  201. /*
  202. * This routine is called when the HDLC layer internally generates a
  203. * command or response for the remote machine ( eg. RR, UA etc. ).
  204. * Only supervisory or unnumbered frames are processed, FRMRs are handled
  205. * by lapb_transmit_frmr below.
  206. */
  207. void lapb_send_control(struct lapb_cb *lapb, int frametype,
  208. int poll_bit, int type)
  209. {
  210. struct sk_buff *skb;
  211. unsigned char *dptr;
  212. if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL)
  213. return;
  214. skb_reserve(skb, LAPB_HEADER_LEN + 1);
  215. if (lapb->mode & LAPB_EXTENDED) {
  216. if ((frametype & LAPB_U) == LAPB_U) {
  217. dptr = skb_put(skb, 1);
  218. *dptr = frametype;
  219. *dptr |= poll_bit ? LAPB_SPF : 0;
  220. } else {
  221. dptr = skb_put(skb, 2);
  222. dptr[0] = frametype;
  223. dptr[1] = (lapb->vr << 1);
  224. dptr[1] |= poll_bit ? LAPB_EPF : 0;
  225. }
  226. } else {
  227. dptr = skb_put(skb, 1);
  228. *dptr = frametype;
  229. *dptr |= poll_bit ? LAPB_SPF : 0;
  230. if ((frametype & LAPB_U) == LAPB_S) /* S frames carry NR */
  231. *dptr |= (lapb->vr << 5);
  232. }
  233. lapb_transmit_buffer(lapb, skb, type);
  234. }
  235. /*
  236. * This routine generates FRMRs based on information previously stored in
  237. * the LAPB control block.
  238. */
  239. void lapb_transmit_frmr(struct lapb_cb *lapb)
  240. {
  241. struct sk_buff *skb;
  242. unsigned char *dptr;
  243. if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL)
  244. return;
  245. skb_reserve(skb, LAPB_HEADER_LEN + 1);
  246. if (lapb->mode & LAPB_EXTENDED) {
  247. dptr = skb_put(skb, 6);
  248. *dptr++ = LAPB_FRMR;
  249. *dptr++ = lapb->frmr_data.control[0];
  250. *dptr++ = lapb->frmr_data.control[1];
  251. *dptr++ = (lapb->vs << 1) & 0xFE;
  252. *dptr = (lapb->vr << 1) & 0xFE;
  253. if (lapb->frmr_data.cr == LAPB_RESPONSE)
  254. *dptr |= 0x01;
  255. dptr++;
  256. *dptr++ = lapb->frmr_type;
  257. #if LAPB_DEBUG > 1
  258. printk(KERN_DEBUG "lapb: (%p) S%d TX FRMR %02X %02X %02X %02X %02X\n",
  259. lapb->dev, lapb->state,
  260. skb->data[1], skb->data[2], skb->data[3],
  261. skb->data[4], skb->data[5]);
  262. #endif
  263. } else {
  264. dptr = skb_put(skb, 4);
  265. *dptr++ = LAPB_FRMR;
  266. *dptr++ = lapb->frmr_data.control[0];
  267. *dptr = (lapb->vs << 1) & 0x0E;
  268. *dptr |= (lapb->vr << 5) & 0xE0;
  269. if (lapb->frmr_data.cr == LAPB_RESPONSE)
  270. *dptr |= 0x10;
  271. dptr++;
  272. *dptr++ = lapb->frmr_type;
  273. #if LAPB_DEBUG > 1
  274. printk(KERN_DEBUG "lapb: (%p) S%d TX FRMR %02X %02X %02X\n",
  275. lapb->dev, lapb->state, skb->data[1],
  276. skb->data[2], skb->data[3]);
  277. #endif
  278. }
  279. lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE);
  280. }