nr_out.c 5.9 KB

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