nr_out.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/types.h>
  12. #include <linux/socket.h>
  13. #include <linux/in.h>
  14. #include <linux/kernel.h>
  15. #include <linux/timer.h>
  16. #include <linux/string.h>
  17. #include <linux/sockios.h>
  18. #include <linux/net.h>
  19. #include <linux/slab.h>
  20. #include <net/ax25.h>
  21. #include <linux/inet.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/skbuff.h>
  24. #include <net/sock.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/system.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. #include <net/netrom.h>
  31. /*
  32. * This is where all NET/ROM frames pass, except for IP-over-NET/ROM which
  33. * cannot be fragmented in this manner.
  34. */
  35. void nr_output(struct sock *sk, struct sk_buff *skb)
  36. {
  37. struct sk_buff *skbn;
  38. unsigned char transport[NR_TRANSPORT_LEN];
  39. int err, frontlen, len;
  40. if (skb->len - NR_TRANSPORT_LEN > NR_MAX_PACKET_SIZE) {
  41. /* Save a copy of the Transport Header */
  42. skb_copy_from_linear_data(skb, transport, NR_TRANSPORT_LEN);
  43. skb_pull(skb, NR_TRANSPORT_LEN);
  44. frontlen = skb_headroom(skb);
  45. while (skb->len > 0) {
  46. if ((skbn = sock_alloc_send_skb(sk, frontlen + NR_MAX_PACKET_SIZE, 0, &err)) == NULL)
  47. return;
  48. skb_reserve(skbn, frontlen);
  49. len = (NR_MAX_PACKET_SIZE > skb->len) ? skb->len : NR_MAX_PACKET_SIZE;
  50. /* Copy the user data */
  51. skb_copy_from_linear_data(skb, skb_put(skbn, len), len);
  52. skb_pull(skb, len);
  53. /* Duplicate the Transport Header */
  54. skb_push(skbn, NR_TRANSPORT_LEN);
  55. skb_copy_to_linear_data(skbn, transport,
  56. NR_TRANSPORT_LEN);
  57. if (skb->len > 0)
  58. skbn->data[4] |= NR_MORE_FLAG;
  59. skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
  60. }
  61. kfree_skb(skb);
  62. } else {
  63. skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
  64. }
  65. nr_kick(sk);
  66. }
  67. /*
  68. * This procedure is passed a buffer descriptor for an iframe. It builds
  69. * the rest of the control part of the frame and then writes it out.
  70. */
  71. static void nr_send_iframe(struct sock *sk, struct sk_buff *skb)
  72. {
  73. struct nr_sock *nr = nr_sk(sk);
  74. if (skb == NULL)
  75. return;
  76. skb->data[2] = nr->vs;
  77. skb->data[3] = nr->vr;
  78. if (nr->condition & NR_COND_OWN_RX_BUSY)
  79. skb->data[4] |= NR_CHOKE_FLAG;
  80. nr_start_idletimer(sk);
  81. nr_transmit_buffer(sk, skb);
  82. }
  83. void nr_send_nak_frame(struct sock *sk)
  84. {
  85. struct sk_buff *skb, *skbn;
  86. struct nr_sock *nr = nr_sk(sk);
  87. if ((skb = skb_peek(&nr->ack_queue)) == NULL)
  88. return;
  89. if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL)
  90. return;
  91. skbn->data[2] = nr->va;
  92. skbn->data[3] = nr->vr;
  93. if (nr->condition & NR_COND_OWN_RX_BUSY)
  94. skbn->data[4] |= NR_CHOKE_FLAG;
  95. nr_transmit_buffer(sk, skbn);
  96. nr->condition &= ~NR_COND_ACK_PENDING;
  97. nr->vl = nr->vr;
  98. nr_stop_t1timer(sk);
  99. }
  100. void nr_kick(struct sock *sk)
  101. {
  102. struct nr_sock *nr = nr_sk(sk);
  103. struct sk_buff *skb, *skbn;
  104. unsigned short start, end;
  105. if (nr->state != NR_STATE_3)
  106. return;
  107. if (nr->condition & NR_COND_PEER_RX_BUSY)
  108. return;
  109. if (!skb_peek(&sk->sk_write_queue))
  110. return;
  111. start = (skb_peek(&nr->ack_queue) == NULL) ? nr->va : nr->vs;
  112. end = (nr->va + nr->window) % NR_MODULUS;
  113. if (start == end)
  114. return;
  115. nr->vs = start;
  116. /*
  117. * Transmit data until either we're out of data to send or
  118. * the window is full.
  119. */
  120. /*
  121. * Dequeue the frame and copy it.
  122. */
  123. skb = skb_dequeue(&sk->sk_write_queue);
  124. do {
  125. if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  126. skb_queue_head(&sk->sk_write_queue, skb);
  127. break;
  128. }
  129. skb_set_owner_w(skbn, sk);
  130. /*
  131. * Transmit the frame copy.
  132. */
  133. nr_send_iframe(sk, skbn);
  134. nr->vs = (nr->vs + 1) % NR_MODULUS;
  135. /*
  136. * Requeue the original data frame.
  137. */
  138. skb_queue_tail(&nr->ack_queue, skb);
  139. } while (nr->vs != end &&
  140. (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
  141. nr->vl = nr->vr;
  142. nr->condition &= ~NR_COND_ACK_PENDING;
  143. if (!nr_t1timer_running(sk))
  144. nr_start_t1timer(sk);
  145. }
  146. void nr_transmit_buffer(struct sock *sk, struct sk_buff *skb)
  147. {
  148. struct nr_sock *nr = nr_sk(sk);
  149. unsigned char *dptr;
  150. /*
  151. * Add the protocol byte and network header.
  152. */
  153. dptr = skb_push(skb, NR_NETWORK_LEN);
  154. memcpy(dptr, &nr->source_addr, AX25_ADDR_LEN);
  155. dptr[6] &= ~AX25_CBIT;
  156. dptr[6] &= ~AX25_EBIT;
  157. dptr[6] |= AX25_SSSID_SPARE;
  158. dptr += AX25_ADDR_LEN;
  159. memcpy(dptr, &nr->dest_addr, AX25_ADDR_LEN);
  160. dptr[6] &= ~AX25_CBIT;
  161. dptr[6] |= AX25_EBIT;
  162. dptr[6] |= AX25_SSSID_SPARE;
  163. dptr += AX25_ADDR_LEN;
  164. *dptr++ = sysctl_netrom_network_ttl_initialiser;
  165. if (!nr_route_frame(skb, NULL)) {
  166. kfree_skb(skb);
  167. nr_disconnect(sk, ENETUNREACH);
  168. }
  169. }
  170. /*
  171. * The following routines are taken from page 170 of the 7th ARRL Computer
  172. * Networking Conference paper, as is the whole state machine.
  173. */
  174. void nr_establish_data_link(struct sock *sk)
  175. {
  176. struct nr_sock *nr = nr_sk(sk);
  177. nr->condition = 0x00;
  178. nr->n2count = 0;
  179. nr_write_internal(sk, NR_CONNREQ);
  180. nr_stop_t2timer(sk);
  181. nr_stop_t4timer(sk);
  182. nr_stop_idletimer(sk);
  183. nr_start_t1timer(sk);
  184. }
  185. /*
  186. * Never send a NAK when we are CHOKEd.
  187. */
  188. void nr_enquiry_response(struct sock *sk)
  189. {
  190. struct nr_sock *nr = nr_sk(sk);
  191. int frametype = NR_INFOACK;
  192. if (nr->condition & NR_COND_OWN_RX_BUSY) {
  193. frametype |= NR_CHOKE_FLAG;
  194. } else {
  195. if (skb_peek(&nr->reseq_queue) != NULL)
  196. frametype |= NR_NAK_FLAG;
  197. }
  198. nr_write_internal(sk, frametype);
  199. nr->vl = nr->vr;
  200. nr->condition &= ~NR_COND_ACK_PENDING;
  201. }
  202. void nr_check_iframes_acked(struct sock *sk, unsigned short nr)
  203. {
  204. struct nr_sock *nrom = nr_sk(sk);
  205. if (nrom->vs == nr) {
  206. nr_frames_acked(sk, nr);
  207. nr_stop_t1timer(sk);
  208. nrom->n2count = 0;
  209. } else {
  210. if (nrom->va != nr) {
  211. nr_frames_acked(sk, nr);
  212. nr_start_t1timer(sk);
  213. }
  214. }
  215. }