x25_out.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 New timer architecture.
  19. * 2000-09-04 Henner Eisen Prevented x25_output() skb leakage.
  20. * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
  21. * 2000-11-10 Henner Eisen x25_send_iframe(): re-queued frames
  22. * needed cleaned seq-number fields.
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/socket.h>
  26. #include <linux/kernel.h>
  27. #include <linux/string.h>
  28. #include <linux/skbuff.h>
  29. #include <net/sock.h>
  30. #include <net/x25.h>
  31. static int x25_pacsize_to_bytes(unsigned int pacsize)
  32. {
  33. int bytes = 1;
  34. if (!pacsize)
  35. return 128;
  36. while (pacsize-- > 0)
  37. bytes *= 2;
  38. return bytes;
  39. }
  40. /*
  41. * This is where all X.25 information frames pass.
  42. *
  43. * Returns the amount of user data bytes sent on success
  44. * or a negative error code on failure.
  45. */
  46. int x25_output(struct sock *sk, struct sk_buff *skb)
  47. {
  48. struct sk_buff *skbn;
  49. unsigned char header[X25_EXT_MIN_LEN];
  50. int err, frontlen, len;
  51. int sent=0, noblock = X25_SKB_CB(skb)->flags & MSG_DONTWAIT;
  52. struct x25_sock *x25 = x25_sk(sk);
  53. int header_len = x25->neighbour->extended ? X25_EXT_MIN_LEN :
  54. X25_STD_MIN_LEN;
  55. int max_len = x25_pacsize_to_bytes(x25->facilities.pacsize_out);
  56. if (skb->len - header_len > max_len) {
  57. /* Save a copy of the Header */
  58. skb_copy_from_linear_data(skb, header, header_len);
  59. skb_pull(skb, header_len);
  60. frontlen = skb_headroom(skb);
  61. while (skb->len > 0) {
  62. release_sock(sk);
  63. skbn = sock_alloc_send_skb(sk, frontlen + max_len,
  64. noblock, &err);
  65. lock_sock(sk);
  66. if (!skbn) {
  67. if (err == -EWOULDBLOCK && noblock){
  68. kfree_skb(skb);
  69. return sent;
  70. }
  71. SOCK_DEBUG(sk, "x25_output: fragment alloc"
  72. " failed, err=%d, %d bytes "
  73. "sent\n", err, sent);
  74. return err;
  75. }
  76. skb_reserve(skbn, frontlen);
  77. len = max_len > skb->len ? skb->len : max_len;
  78. /* Copy the user data */
  79. skb_copy_from_linear_data(skb, skb_put(skbn, len), len);
  80. skb_pull(skb, len);
  81. /* Duplicate the Header */
  82. skb_push(skbn, header_len);
  83. skb_copy_to_linear_data(skbn, header, header_len);
  84. if (skb->len > 0) {
  85. if (x25->neighbour->extended)
  86. skbn->data[3] |= X25_EXT_M_BIT;
  87. else
  88. skbn->data[2] |= X25_STD_M_BIT;
  89. }
  90. skb_queue_tail(&sk->sk_write_queue, skbn);
  91. sent += len;
  92. }
  93. kfree_skb(skb);
  94. } else {
  95. skb_queue_tail(&sk->sk_write_queue, skb);
  96. sent = skb->len - header_len;
  97. }
  98. return sent;
  99. }
  100. /*
  101. * This procedure is passed a buffer descriptor for an iframe. It builds
  102. * the rest of the control part of the frame and then writes it out.
  103. */
  104. static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
  105. {
  106. struct x25_sock *x25 = x25_sk(sk);
  107. if (!skb)
  108. return;
  109. if (x25->neighbour->extended) {
  110. skb->data[2] = (x25->vs << 1) & 0xFE;
  111. skb->data[3] &= X25_EXT_M_BIT;
  112. skb->data[3] |= (x25->vr << 1) & 0xFE;
  113. } else {
  114. skb->data[2] &= X25_STD_M_BIT;
  115. skb->data[2] |= (x25->vs << 1) & 0x0E;
  116. skb->data[2] |= (x25->vr << 5) & 0xE0;
  117. }
  118. x25_transmit_link(skb, x25->neighbour);
  119. }
  120. void x25_kick(struct sock *sk)
  121. {
  122. struct sk_buff *skb, *skbn;
  123. unsigned short start, end;
  124. int modulus;
  125. struct x25_sock *x25 = x25_sk(sk);
  126. if (x25->state != X25_STATE_3)
  127. return;
  128. /*
  129. * Transmit interrupt data.
  130. */
  131. if (skb_peek(&x25->interrupt_out_queue) != NULL &&
  132. !test_and_set_bit(X25_INTERRUPT_FLAG, &x25->flags)) {
  133. skb = skb_dequeue(&x25->interrupt_out_queue);
  134. x25_transmit_link(skb, x25->neighbour);
  135. }
  136. if (x25->condition & X25_COND_PEER_RX_BUSY)
  137. return;
  138. if (!skb_peek(&sk->sk_write_queue))
  139. return;
  140. modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
  141. start = skb_peek(&x25->ack_queue) ? x25->vs : x25->va;
  142. end = (x25->va + x25->facilities.winsize_out) % modulus;
  143. if (start == end)
  144. return;
  145. x25->vs = start;
  146. /*
  147. * Transmit data until either we're out of data to send or
  148. * the window is full.
  149. */
  150. skb = skb_dequeue(&sk->sk_write_queue);
  151. do {
  152. if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  153. skb_queue_head(&sk->sk_write_queue, skb);
  154. break;
  155. }
  156. skb_set_owner_w(skbn, sk);
  157. /*
  158. * Transmit the frame copy.
  159. */
  160. x25_send_iframe(sk, skbn);
  161. x25->vs = (x25->vs + 1) % modulus;
  162. /*
  163. * Requeue the original data frame.
  164. */
  165. skb_queue_tail(&x25->ack_queue, skb);
  166. } while (x25->vs != end &&
  167. (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
  168. x25->vl = x25->vr;
  169. x25->condition &= ~X25_COND_ACK_PENDING;
  170. x25_stop_timer(sk);
  171. }
  172. /*
  173. * The following routines are taken from page 170 of the 7th ARRL Computer
  174. * Networking Conference paper, as is the whole state machine.
  175. */
  176. void x25_enquiry_response(struct sock *sk)
  177. {
  178. struct x25_sock *x25 = x25_sk(sk);
  179. if (x25->condition & X25_COND_OWN_RX_BUSY)
  180. x25_write_internal(sk, X25_RNR);
  181. else
  182. x25_write_internal(sk, X25_RR);
  183. x25->vl = x25->vr;
  184. x25->condition &= ~X25_COND_ACK_PENDING;
  185. x25_stop_timer(sk);
  186. }