x25_subr.c 9.2 KB

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