x25_subr.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * X.25 Packet Layer release 002
  3. *
  4. * This is ALPHA test software. This code may break your machine,
  5. * randomly fail to work with new releases, misbehave and/or generally
  6. * screw up. It might even work.
  7. *
  8. * This code REQUIRES 2.1.15 or higher
  9. *
  10. * This module:
  11. * This module is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * History
  17. * X.25 001 Jonathan Naylor Started coding.
  18. * X.25 002 Jonathan Naylor Centralised disconnection processing.
  19. * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
  20. * negotiation.
  21. * jun/24/01 Arnaldo C. Melo use skb_queue_purge, cleanups
  22. * apr/04/15 Shaun Pereira Fast select with no
  23. * restriction on response.
  24. */
  25. #define pr_fmt(fmt) "X25: " fmt
  26. #include <linux/slab.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/skbuff.h>
  30. #include <net/sock.h>
  31. #include <net/tcp_states.h>
  32. #include <net/x25.h>
  33. /*
  34. * This routine purges all of the queues of frames.
  35. */
  36. void x25_clear_queues(struct sock *sk)
  37. {
  38. struct x25_sock *x25 = x25_sk(sk);
  39. skb_queue_purge(&sk->sk_write_queue);
  40. skb_queue_purge(&x25->ack_queue);
  41. skb_queue_purge(&x25->interrupt_in_queue);
  42. skb_queue_purge(&x25->interrupt_out_queue);
  43. skb_queue_purge(&x25->fragment_queue);
  44. }
  45. /*
  46. * This routine purges the input queue of those frames that have been
  47. * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
  48. * SDL diagram.
  49. */
  50. void x25_frames_acked(struct sock *sk, unsigned short nr)
  51. {
  52. struct sk_buff *skb;
  53. struct x25_sock *x25 = x25_sk(sk);
  54. int modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
  55. /*
  56. * Remove all the ack-ed frames from the ack queue.
  57. */
  58. if (x25->va != nr)
  59. while (skb_peek(&x25->ack_queue) && x25->va != nr) {
  60. skb = skb_dequeue(&x25->ack_queue);
  61. kfree_skb(skb);
  62. x25->va = (x25->va + 1) % modulus;
  63. }
  64. }
  65. void x25_requeue_frames(struct sock *sk)
  66. {
  67. struct sk_buff *skb, *skb_prev = NULL;
  68. /*
  69. * Requeue all the un-ack-ed frames on the output queue to be picked
  70. * up by x25_kick. This arrangement handles the possibility of an empty
  71. * output queue.
  72. */
  73. while ((skb = skb_dequeue(&x25_sk(sk)->ack_queue)) != NULL) {
  74. if (!skb_prev)
  75. skb_queue_head(&sk->sk_write_queue, skb);
  76. else
  77. skb_append(skb_prev, skb, &sk->sk_write_queue);
  78. skb_prev = skb;
  79. }
  80. }
  81. /*
  82. * Validate that the value of nr is between va and vs. Return true or
  83. * false for testing.
  84. */
  85. int x25_validate_nr(struct sock *sk, unsigned short nr)
  86. {
  87. struct x25_sock *x25 = x25_sk(sk);
  88. unsigned short vc = x25->va;
  89. int modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
  90. while (vc != x25->vs) {
  91. if (nr == vc)
  92. return 1;
  93. vc = (vc + 1) % modulus;
  94. }
  95. return nr == x25->vs ? 1 : 0;
  96. }
  97. /*
  98. * This routine is called when the packet layer internally generates a
  99. * control frame.
  100. */
  101. void x25_write_internal(struct sock *sk, int frametype)
  102. {
  103. struct x25_sock *x25 = x25_sk(sk);
  104. struct sk_buff *skb;
  105. unsigned char *dptr;
  106. unsigned char facilities[X25_MAX_FAC_LEN];
  107. unsigned char addresses[1 + X25_ADDR_LEN];
  108. unsigned char lci1, lci2;
  109. /*
  110. * Default safe frame size.
  111. */
  112. int len = X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
  113. /*
  114. * Adjust frame size.
  115. */
  116. switch (frametype) {
  117. case X25_CALL_REQUEST:
  118. len += 1 + X25_ADDR_LEN + X25_MAX_FAC_LEN + X25_MAX_CUD_LEN;
  119. break;
  120. case X25_CALL_ACCEPTED: /* fast sel with no restr on resp */
  121. if (x25->facilities.reverse & 0x80) {
  122. len += 1 + X25_MAX_FAC_LEN + X25_MAX_CUD_LEN;
  123. } else {
  124. len += 1 + X25_MAX_FAC_LEN;
  125. }
  126. break;
  127. case X25_CLEAR_REQUEST:
  128. case X25_RESET_REQUEST:
  129. len += 2;
  130. break;
  131. case X25_RR:
  132. case X25_RNR:
  133. case X25_REJ:
  134. case X25_CLEAR_CONFIRMATION:
  135. case X25_INTERRUPT_CONFIRMATION:
  136. case X25_RESET_CONFIRMATION:
  137. break;
  138. default:
  139. pr_err("invalid frame type %02X\n", frametype);
  140. return;
  141. }
  142. if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
  143. return;
  144. /*
  145. * Space for Ethernet and 802.2 LLC headers.
  146. */
  147. skb_reserve(skb, X25_MAX_L2_LEN);
  148. /*
  149. * Make space for the GFI and LCI, and fill them in.
  150. */
  151. dptr = skb_put(skb, 2);
  152. lci1 = (x25->lci >> 8) & 0x0F;
  153. lci2 = (x25->lci >> 0) & 0xFF;
  154. if (x25->neighbour->extended) {
  155. *dptr++ = lci1 | X25_GFI_EXTSEQ;
  156. *dptr++ = lci2;
  157. } else {
  158. *dptr++ = lci1 | X25_GFI_STDSEQ;
  159. *dptr++ = lci2;
  160. }
  161. /*
  162. * Now fill in the frame type specific information.
  163. */
  164. switch (frametype) {
  165. case X25_CALL_REQUEST:
  166. dptr = skb_put(skb, 1);
  167. *dptr++ = X25_CALL_REQUEST;
  168. len = x25_addr_aton(addresses, &x25->dest_addr,
  169. &x25->source_addr);
  170. dptr = skb_put(skb, len);
  171. memcpy(dptr, addresses, len);
  172. len = x25_create_facilities(facilities,
  173. &x25->facilities,
  174. &x25->dte_facilities,
  175. x25->neighbour->global_facil_mask);
  176. dptr = skb_put(skb, len);
  177. memcpy(dptr, facilities, len);
  178. dptr = skb_put(skb, x25->calluserdata.cudlength);
  179. memcpy(dptr, x25->calluserdata.cuddata,
  180. x25->calluserdata.cudlength);
  181. x25->calluserdata.cudlength = 0;
  182. break;
  183. case X25_CALL_ACCEPTED:
  184. dptr = skb_put(skb, 2);
  185. *dptr++ = X25_CALL_ACCEPTED;
  186. *dptr++ = 0x00; /* Address lengths */
  187. len = x25_create_facilities(facilities,
  188. &x25->facilities,
  189. &x25->dte_facilities,
  190. x25->vc_facil_mask);
  191. dptr = skb_put(skb, len);
  192. memcpy(dptr, facilities, len);
  193. /* fast select with no restriction on response
  194. allows call user data. Userland must
  195. ensure it is ours and not theirs */
  196. if(x25->facilities.reverse & 0x80) {
  197. dptr = skb_put(skb,
  198. x25->calluserdata.cudlength);
  199. memcpy(dptr, x25->calluserdata.cuddata,
  200. x25->calluserdata.cudlength);
  201. }
  202. x25->calluserdata.cudlength = 0;
  203. break;
  204. case X25_CLEAR_REQUEST:
  205. dptr = skb_put(skb, 3);
  206. *dptr++ = frametype;
  207. *dptr++ = x25->causediag.cause;
  208. *dptr++ = x25->causediag.diagnostic;
  209. break;
  210. case X25_RESET_REQUEST:
  211. dptr = skb_put(skb, 3);
  212. *dptr++ = frametype;
  213. *dptr++ = 0x00; /* XXX */
  214. *dptr++ = 0x00; /* XXX */
  215. break;
  216. case X25_RR:
  217. case X25_RNR:
  218. case X25_REJ:
  219. if (x25->neighbour->extended) {
  220. dptr = skb_put(skb, 2);
  221. *dptr++ = frametype;
  222. *dptr++ = (x25->vr << 1) & 0xFE;
  223. } else {
  224. dptr = skb_put(skb, 1);
  225. *dptr = frametype;
  226. *dptr++ |= (x25->vr << 5) & 0xE0;
  227. }
  228. break;
  229. case X25_CLEAR_CONFIRMATION:
  230. case X25_INTERRUPT_CONFIRMATION:
  231. case X25_RESET_CONFIRMATION:
  232. dptr = skb_put(skb, 1);
  233. *dptr = frametype;
  234. break;
  235. }
  236. x25_transmit_link(skb, x25->neighbour);
  237. }
  238. /*
  239. * Unpick the contents of the passed X.25 Packet Layer frame.
  240. */
  241. int x25_decode(struct sock *sk, struct sk_buff *skb, int *ns, int *nr, int *q,
  242. int *d, int *m)
  243. {
  244. struct x25_sock *x25 = x25_sk(sk);
  245. unsigned char *frame;
  246. if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
  247. return X25_ILLEGAL;
  248. frame = skb->data;
  249. *ns = *nr = *q = *d = *m = 0;
  250. switch (frame[2]) {
  251. case X25_CALL_REQUEST:
  252. case X25_CALL_ACCEPTED:
  253. case X25_CLEAR_REQUEST:
  254. case X25_CLEAR_CONFIRMATION:
  255. case X25_INTERRUPT:
  256. case X25_INTERRUPT_CONFIRMATION:
  257. case X25_RESET_REQUEST:
  258. case X25_RESET_CONFIRMATION:
  259. case X25_RESTART_REQUEST:
  260. case X25_RESTART_CONFIRMATION:
  261. case X25_REGISTRATION_REQUEST:
  262. case X25_REGISTRATION_CONFIRMATION:
  263. case X25_DIAGNOSTIC:
  264. return frame[2];
  265. }
  266. if (x25->neighbour->extended) {
  267. if (frame[2] == X25_RR ||
  268. frame[2] == X25_RNR ||
  269. frame[2] == X25_REJ) {
  270. if (!pskb_may_pull(skb, X25_EXT_MIN_LEN))
  271. return X25_ILLEGAL;
  272. frame = skb->data;
  273. *nr = (frame[3] >> 1) & 0x7F;
  274. return frame[2];
  275. }
  276. } else {
  277. if ((frame[2] & 0x1F) == X25_RR ||
  278. (frame[2] & 0x1F) == X25_RNR ||
  279. (frame[2] & 0x1F) == X25_REJ) {
  280. *nr = (frame[2] >> 5) & 0x07;
  281. return frame[2] & 0x1F;
  282. }
  283. }
  284. if (x25->neighbour->extended) {
  285. if ((frame[2] & 0x01) == X25_DATA) {
  286. if (!pskb_may_pull(skb, X25_EXT_MIN_LEN))
  287. return X25_ILLEGAL;
  288. frame = skb->data;
  289. *q = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
  290. *d = (frame[0] & X25_D_BIT) == X25_D_BIT;
  291. *m = (frame[3] & X25_EXT_M_BIT) == X25_EXT_M_BIT;
  292. *nr = (frame[3] >> 1) & 0x7F;
  293. *ns = (frame[2] >> 1) & 0x7F;
  294. return X25_DATA;
  295. }
  296. } else {
  297. if ((frame[2] & 0x01) == X25_DATA) {
  298. *q = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
  299. *d = (frame[0] & X25_D_BIT) == X25_D_BIT;
  300. *m = (frame[2] & X25_STD_M_BIT) == X25_STD_M_BIT;
  301. *nr = (frame[2] >> 5) & 0x07;
  302. *ns = (frame[2] >> 1) & 0x07;
  303. return X25_DATA;
  304. }
  305. }
  306. pr_debug("invalid PLP frame %02X %02X %02X\n",
  307. frame[0], frame[1], frame[2]);
  308. return X25_ILLEGAL;
  309. }
  310. void x25_disconnect(struct sock *sk, int reason, unsigned char cause,
  311. unsigned char diagnostic)
  312. {
  313. struct x25_sock *x25 = x25_sk(sk);
  314. x25_clear_queues(sk);
  315. x25_stop_timer(sk);
  316. x25->lci = 0;
  317. x25->state = X25_STATE_0;
  318. x25->causediag.cause = cause;
  319. x25->causediag.diagnostic = diagnostic;
  320. sk->sk_state = TCP_CLOSE;
  321. sk->sk_err = reason;
  322. sk->sk_shutdown |= SEND_SHUTDOWN;
  323. if (!sock_flag(sk, SOCK_DEAD)) {
  324. sk->sk_state_change(sk);
  325. sock_set_flag(sk, SOCK_DEAD);
  326. }
  327. }
  328. /*
  329. * Clear an own-rx-busy condition and tell the peer about this, provided
  330. * that there is a significant amount of free receive buffer space available.
  331. */
  332. void x25_check_rbuf(struct sock *sk)
  333. {
  334. struct x25_sock *x25 = x25_sk(sk);
  335. if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf >> 1) &&
  336. (x25->condition & X25_COND_OWN_RX_BUSY)) {
  337. x25->condition &= ~X25_COND_OWN_RX_BUSY;
  338. x25->condition &= ~X25_COND_ACK_PENDING;
  339. x25->vl = x25->vr;
  340. x25_write_internal(sk, X25_RR);
  341. x25_stop_timer(sk);
  342. }
  343. }