ax25_subr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  8. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  9. * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
  10. * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr)
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/socket.h>
  15. #include <linux/in.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/timer.h>
  19. #include <linux/string.h>
  20. #include <linux/sockios.h>
  21. #include <linux/net.h>
  22. #include <net/ax25.h>
  23. #include <linux/inet.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <net/sock.h>
  27. #include <net/tcp_states.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/system.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/mm.h>
  32. #include <linux/interrupt.h>
  33. /*
  34. * This routine purges all the queues of frames.
  35. */
  36. void ax25_clear_queues(ax25_cb *ax25)
  37. {
  38. skb_queue_purge(&ax25->write_queue);
  39. skb_queue_purge(&ax25->ack_queue);
  40. skb_queue_purge(&ax25->reseq_queue);
  41. skb_queue_purge(&ax25->frag_queue);
  42. }
  43. /*
  44. * This routine purges the input queue of those frames that have been
  45. * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
  46. * SDL diagram.
  47. */
  48. void ax25_frames_acked(ax25_cb *ax25, unsigned short nr)
  49. {
  50. struct sk_buff *skb;
  51. /*
  52. * Remove all the ack-ed frames from the ack queue.
  53. */
  54. if (ax25->va != nr) {
  55. while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) {
  56. skb = skb_dequeue(&ax25->ack_queue);
  57. kfree_skb(skb);
  58. ax25->va = (ax25->va + 1) % ax25->modulus;
  59. }
  60. }
  61. }
  62. void ax25_requeue_frames(ax25_cb *ax25)
  63. {
  64. struct sk_buff *skb, *skb_prev = NULL;
  65. /*
  66. * Requeue all the un-ack-ed frames on the output queue to be picked
  67. * up by ax25_kick called from the timer. This arrangement handles the
  68. * possibility of an empty output queue.
  69. */
  70. while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) {
  71. if (skb_prev == NULL)
  72. skb_queue_head(&ax25->write_queue, skb);
  73. else
  74. skb_append(skb_prev, skb, &ax25->write_queue);
  75. skb_prev = skb;
  76. }
  77. }
  78. /*
  79. * Validate that the value of nr is between va and vs. Return true or
  80. * false for testing.
  81. */
  82. int ax25_validate_nr(ax25_cb *ax25, unsigned short nr)
  83. {
  84. unsigned short vc = ax25->va;
  85. while (vc != ax25->vs) {
  86. if (nr == vc) return 1;
  87. vc = (vc + 1) % ax25->modulus;
  88. }
  89. if (nr == ax25->vs) return 1;
  90. return 0;
  91. }
  92. /*
  93. * This routine is the centralised routine for parsing the control
  94. * information for the different frame formats.
  95. */
  96. int ax25_decode(ax25_cb *ax25, struct sk_buff *skb, int *ns, int *nr, int *pf)
  97. {
  98. unsigned char *frame;
  99. int frametype = AX25_ILLEGAL;
  100. frame = skb->data;
  101. *ns = *nr = *pf = 0;
  102. if (ax25->modulus == AX25_MODULUS) {
  103. if ((frame[0] & AX25_S) == 0) {
  104. frametype = AX25_I; /* I frame - carries NR/NS/PF */
  105. *ns = (frame[0] >> 1) & 0x07;
  106. *nr = (frame[0] >> 5) & 0x07;
  107. *pf = frame[0] & AX25_PF;
  108. } else if ((frame[0] & AX25_U) == 1) { /* S frame - take out PF/NR */
  109. frametype = frame[0] & 0x0F;
  110. *nr = (frame[0] >> 5) & 0x07;
  111. *pf = frame[0] & AX25_PF;
  112. } else if ((frame[0] & AX25_U) == 3) { /* U frame - take out PF */
  113. frametype = frame[0] & ~AX25_PF;
  114. *pf = frame[0] & AX25_PF;
  115. }
  116. skb_pull(skb, 1);
  117. } else {
  118. if ((frame[0] & AX25_S) == 0) {
  119. frametype = AX25_I; /* I frame - carries NR/NS/PF */
  120. *ns = (frame[0] >> 1) & 0x7F;
  121. *nr = (frame[1] >> 1) & 0x7F;
  122. *pf = frame[1] & AX25_EPF;
  123. skb_pull(skb, 2);
  124. } else if ((frame[0] & AX25_U) == 1) { /* S frame - take out PF/NR */
  125. frametype = frame[0] & 0x0F;
  126. *nr = (frame[1] >> 1) & 0x7F;
  127. *pf = frame[1] & AX25_EPF;
  128. skb_pull(skb, 2);
  129. } else if ((frame[0] & AX25_U) == 3) { /* U frame - take out PF */
  130. frametype = frame[0] & ~AX25_PF;
  131. *pf = frame[0] & AX25_PF;
  132. skb_pull(skb, 1);
  133. }
  134. }
  135. return frametype;
  136. }
  137. /*
  138. * This routine is called when the HDLC layer internally generates a
  139. * command or response for the remote machine ( eg. RR, UA etc. ).
  140. * Only supervisory or unnumbered frames are processed.
  141. */
  142. void ax25_send_control(ax25_cb *ax25, int frametype, int poll_bit, int type)
  143. {
  144. struct sk_buff *skb;
  145. unsigned char *dptr;
  146. if ((skb = alloc_skb(ax25->ax25_dev->dev->hard_header_len + 2, GFP_ATOMIC)) == NULL)
  147. return;
  148. skb_reserve(skb, ax25->ax25_dev->dev->hard_header_len);
  149. skb->nh.raw = skb->data;
  150. /* Assume a response - address structure for DTE */
  151. if (ax25->modulus == AX25_MODULUS) {
  152. dptr = skb_put(skb, 1);
  153. *dptr = frametype;
  154. *dptr |= (poll_bit) ? AX25_PF : 0;
  155. if ((frametype & AX25_U) == AX25_S) /* S frames carry NR */
  156. *dptr |= (ax25->vr << 5);
  157. } else {
  158. if ((frametype & AX25_U) == AX25_U) {
  159. dptr = skb_put(skb, 1);
  160. *dptr = frametype;
  161. *dptr |= (poll_bit) ? AX25_PF : 0;
  162. } else {
  163. dptr = skb_put(skb, 2);
  164. dptr[0] = frametype;
  165. dptr[1] = (ax25->vr << 1);
  166. dptr[1] |= (poll_bit) ? AX25_EPF : 0;
  167. }
  168. }
  169. ax25_transmit_buffer(ax25, skb, type);
  170. }
  171. /*
  172. * Send a 'DM' to an unknown connection attempt, or an invalid caller.
  173. *
  174. * Note: src here is the sender, thus it's the target of the DM
  175. */
  176. void ax25_return_dm(struct net_device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi)
  177. {
  178. struct sk_buff *skb;
  179. char *dptr;
  180. ax25_digi retdigi;
  181. if (dev == NULL)
  182. return;
  183. if ((skb = alloc_skb(dev->hard_header_len + 1, GFP_ATOMIC)) == NULL)
  184. return; /* Next SABM will get DM'd */
  185. skb_reserve(skb, dev->hard_header_len);
  186. skb->nh.raw = skb->data;
  187. ax25_digi_invert(digi, &retdigi);
  188. dptr = skb_put(skb, 1);
  189. *dptr = AX25_DM | AX25_PF;
  190. /*
  191. * Do the address ourselves
  192. */
  193. dptr = skb_push(skb, ax25_addr_size(digi));
  194. dptr += ax25_addr_build(dptr, dest, src, &retdigi, AX25_RESPONSE, AX25_MODULUS);
  195. ax25_queue_xmit(skb, dev);
  196. }
  197. /*
  198. * Exponential backoff for AX.25
  199. */
  200. void ax25_calculate_t1(ax25_cb *ax25)
  201. {
  202. int n, t = 2;
  203. switch (ax25->backoff) {
  204. case 0:
  205. break;
  206. case 1:
  207. t += 2 * ax25->n2count;
  208. break;
  209. case 2:
  210. for (n = 0; n < ax25->n2count; n++)
  211. t *= 2;
  212. if (t > 8) t = 8;
  213. break;
  214. }
  215. ax25->t1 = t * ax25->rtt;
  216. }
  217. /*
  218. * Calculate the Round Trip Time
  219. */
  220. void ax25_calculate_rtt(ax25_cb *ax25)
  221. {
  222. if (ax25->backoff == 0)
  223. return;
  224. if (ax25_t1timer_running(ax25) && ax25->n2count == 0)
  225. ax25->rtt = (9 * ax25->rtt + ax25->t1 - ax25_display_timer(&ax25->t1timer)) / 10;
  226. if (ax25->rtt < AX25_T1CLAMPLO)
  227. ax25->rtt = AX25_T1CLAMPLO;
  228. if (ax25->rtt > AX25_T1CLAMPHI)
  229. ax25->rtt = AX25_T1CLAMPHI;
  230. }
  231. void ax25_disconnect(ax25_cb *ax25, int reason)
  232. {
  233. ax25_clear_queues(ax25);
  234. ax25_stop_t1timer(ax25);
  235. ax25_stop_t2timer(ax25);
  236. ax25_stop_t3timer(ax25);
  237. ax25_stop_idletimer(ax25);
  238. ax25->state = AX25_STATE_0;
  239. ax25_link_failed(ax25, reason);
  240. if (ax25->sk != NULL) {
  241. bh_lock_sock(ax25->sk);
  242. ax25->sk->sk_state = TCP_CLOSE;
  243. ax25->sk->sk_err = reason;
  244. ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
  245. if (!sock_flag(ax25->sk, SOCK_DEAD)) {
  246. ax25->sk->sk_state_change(ax25->sk);
  247. sock_set_flag(ax25->sk, SOCK_DEAD);
  248. }
  249. bh_unlock_sock(ax25->sk);
  250. }
  251. }