lapb_subr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 %02X %02X %02X\n",
  103. lapb->dev, lapb->state,
  104. skb->data[0], skb->data[1], skb->data[2]);
  105. /* We always need to look at 2 bytes, sometimes we need
  106. * to look at 3 and those cases are handled below.
  107. */
  108. if (!pskb_may_pull(skb, 2))
  109. return -1;
  110. if (lapb->mode & LAPB_MLP) {
  111. if (lapb->mode & LAPB_DCE) {
  112. if (skb->data[0] == LAPB_ADDR_D)
  113. frame->cr = LAPB_COMMAND;
  114. if (skb->data[0] == LAPB_ADDR_C)
  115. frame->cr = LAPB_RESPONSE;
  116. } else {
  117. if (skb->data[0] == LAPB_ADDR_C)
  118. frame->cr = LAPB_COMMAND;
  119. if (skb->data[0] == LAPB_ADDR_D)
  120. frame->cr = LAPB_RESPONSE;
  121. }
  122. } else {
  123. if (lapb->mode & LAPB_DCE) {
  124. if (skb->data[0] == LAPB_ADDR_B)
  125. frame->cr = LAPB_COMMAND;
  126. if (skb->data[0] == LAPB_ADDR_A)
  127. frame->cr = LAPB_RESPONSE;
  128. } else {
  129. if (skb->data[0] == LAPB_ADDR_A)
  130. frame->cr = LAPB_COMMAND;
  131. if (skb->data[0] == LAPB_ADDR_B)
  132. frame->cr = LAPB_RESPONSE;
  133. }
  134. }
  135. skb_pull(skb, 1);
  136. if (lapb->mode & LAPB_EXTENDED) {
  137. if (!(skb->data[0] & LAPB_S)) {
  138. if (!pskb_may_pull(skb, 2))
  139. return -1;
  140. /*
  141. * I frame - carries NR/NS/PF
  142. */
  143. frame->type = LAPB_I;
  144. frame->ns = (skb->data[0] >> 1) & 0x7F;
  145. frame->nr = (skb->data[1] >> 1) & 0x7F;
  146. frame->pf = skb->data[1] & LAPB_EPF;
  147. frame->control[0] = skb->data[0];
  148. frame->control[1] = skb->data[1];
  149. skb_pull(skb, 2);
  150. } else if ((skb->data[0] & LAPB_U) == 1) {
  151. if (!pskb_may_pull(skb, 2))
  152. return -1;
  153. /*
  154. * S frame - take out PF/NR
  155. */
  156. frame->type = skb->data[0] & 0x0F;
  157. frame->nr = (skb->data[1] >> 1) & 0x7F;
  158. frame->pf = skb->data[1] & LAPB_EPF;
  159. frame->control[0] = skb->data[0];
  160. frame->control[1] = skb->data[1];
  161. skb_pull(skb, 2);
  162. } else if ((skb->data[0] & LAPB_U) == 3) {
  163. /*
  164. * U frame - take out PF
  165. */
  166. frame->type = skb->data[0] & ~LAPB_SPF;
  167. frame->pf = skb->data[0] & LAPB_SPF;
  168. frame->control[0] = skb->data[0];
  169. frame->control[1] = 0x00;
  170. skb_pull(skb, 1);
  171. }
  172. } else {
  173. if (!(skb->data[0] & LAPB_S)) {
  174. /*
  175. * I frame - carries NR/NS/PF
  176. */
  177. frame->type = LAPB_I;
  178. frame->ns = (skb->data[0] >> 1) & 0x07;
  179. frame->nr = (skb->data[0] >> 5) & 0x07;
  180. frame->pf = skb->data[0] & LAPB_SPF;
  181. } else if ((skb->data[0] & LAPB_U) == 1) {
  182. /*
  183. * S frame - take out PF/NR
  184. */
  185. frame->type = skb->data[0] & 0x0F;
  186. frame->nr = (skb->data[0] >> 5) & 0x07;
  187. frame->pf = skb->data[0] & LAPB_SPF;
  188. } else if ((skb->data[0] & LAPB_U) == 3) {
  189. /*
  190. * U frame - take out PF
  191. */
  192. frame->type = skb->data[0] & ~LAPB_SPF;
  193. frame->pf = skb->data[0] & LAPB_SPF;
  194. }
  195. frame->control[0] = skb->data[0];
  196. skb_pull(skb, 1);
  197. }
  198. return 0;
  199. }
  200. /*
  201. * This routine is called when the HDLC layer internally generates a
  202. * command or response for the remote machine ( eg. RR, UA etc. ).
  203. * Only supervisory or unnumbered frames are processed, FRMRs are handled
  204. * by lapb_transmit_frmr below.
  205. */
  206. void lapb_send_control(struct lapb_cb *lapb, int frametype,
  207. int poll_bit, int type)
  208. {
  209. struct sk_buff *skb;
  210. unsigned char *dptr;
  211. if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL)
  212. return;
  213. skb_reserve(skb, LAPB_HEADER_LEN + 1);
  214. if (lapb->mode & LAPB_EXTENDED) {
  215. if ((frametype & LAPB_U) == LAPB_U) {
  216. dptr = skb_put(skb, 1);
  217. *dptr = frametype;
  218. *dptr |= poll_bit ? LAPB_SPF : 0;
  219. } else {
  220. dptr = skb_put(skb, 2);
  221. dptr[0] = frametype;
  222. dptr[1] = (lapb->vr << 1);
  223. dptr[1] |= poll_bit ? LAPB_EPF : 0;
  224. }
  225. } else {
  226. dptr = skb_put(skb, 1);
  227. *dptr = frametype;
  228. *dptr |= poll_bit ? LAPB_SPF : 0;
  229. if ((frametype & LAPB_U) == LAPB_S) /* S frames carry NR */
  230. *dptr |= (lapb->vr << 5);
  231. }
  232. lapb_transmit_buffer(lapb, skb, type);
  233. }
  234. /*
  235. * This routine generates FRMRs based on information previously stored in
  236. * the LAPB control block.
  237. */
  238. void lapb_transmit_frmr(struct lapb_cb *lapb)
  239. {
  240. struct sk_buff *skb;
  241. unsigned char *dptr;
  242. if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL)
  243. return;
  244. skb_reserve(skb, LAPB_HEADER_LEN + 1);
  245. if (lapb->mode & LAPB_EXTENDED) {
  246. dptr = skb_put(skb, 6);
  247. *dptr++ = LAPB_FRMR;
  248. *dptr++ = lapb->frmr_data.control[0];
  249. *dptr++ = lapb->frmr_data.control[1];
  250. *dptr++ = (lapb->vs << 1) & 0xFE;
  251. *dptr = (lapb->vr << 1) & 0xFE;
  252. if (lapb->frmr_data.cr == LAPB_RESPONSE)
  253. *dptr |= 0x01;
  254. dptr++;
  255. *dptr++ = lapb->frmr_type;
  256. lapb_dbg(1, "(%p) S%d TX FRMR %02X %02X %02X %02X %02X\n",
  257. lapb->dev, lapb->state,
  258. skb->data[1], skb->data[2], skb->data[3],
  259. skb->data[4], skb->data[5]);
  260. } else {
  261. dptr = skb_put(skb, 4);
  262. *dptr++ = LAPB_FRMR;
  263. *dptr++ = lapb->frmr_data.control[0];
  264. *dptr = (lapb->vs << 1) & 0x0E;
  265. *dptr |= (lapb->vr << 5) & 0xE0;
  266. if (lapb->frmr_data.cr == LAPB_RESPONSE)
  267. *dptr |= 0x10;
  268. dptr++;
  269. *dptr++ = lapb->frmr_type;
  270. lapb_dbg(1, "(%p) S%d TX FRMR %02X %02X %02X\n",
  271. lapb->dev, lapb->state, skb->data[1],
  272. skb->data[2], skb->data[3]);
  273. }
  274. lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE);
  275. }