lapb_subr.c 7.5 KB

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