nr_out.c 5.8 KB

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