ax25_subr.c 7.0 KB

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