ax25_ds_subr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) Jonathan Naylor G4KLX ([email protected])
  5. * Copyright (C) Joerg Reuter DL1BKE ([email protected])
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/types.h>
  9. #include <linux/socket.h>
  10. #include <linux/in.h>
  11. #include <linux/kernel.h>
  12. #include <linux/timer.h>
  13. #include <linux/string.h>
  14. #include <linux/sockios.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/net.h>
  17. #include <linux/gfp.h>
  18. #include <net/ax25.h>
  19. #include <linux/inet.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <net/sock.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. void ax25_ds_nr_error_recovery(ax25_cb *ax25)
  28. {
  29. ax25_ds_establish_data_link(ax25);
  30. }
  31. /*
  32. * dl1bke 960114: transmit I frames on DAMA poll
  33. */
  34. void ax25_ds_enquiry_response(ax25_cb *ax25)
  35. {
  36. ax25_cb *ax25o;
  37. /* Please note that neither DK4EG's nor DG2FEF's
  38. * DAMA spec mention the following behaviour as seen
  39. * with TheFirmware:
  40. *
  41. * DB0ACH->DL1BKE <RR C P R0> [DAMA]
  42. * DL1BKE->DB0ACH <I NR=0 NS=0>
  43. * DL1BKE-7->DB0PRA-6 DB0ACH <I C S3 R5>
  44. * DL1BKE->DB0ACH <RR R F R0>
  45. *
  46. * The Flexnet DAMA Master implementation apparently
  47. * insists on the "proper" AX.25 behaviour:
  48. *
  49. * DB0ACH->DL1BKE <RR C P R0> [DAMA]
  50. * DL1BKE->DB0ACH <RR R F R0>
  51. * DL1BKE->DB0ACH <I NR=0 NS=0>
  52. * DL1BKE-7->DB0PRA-6 DB0ACH <I C S3 R5>
  53. *
  54. * Flexnet refuses to send us *any* I frame if we send
  55. * a REJ in case AX25_COND_REJECT is set. It is superfluous in
  56. * this mode anyway (a RR or RNR invokes the retransmission).
  57. * Is this a Flexnet bug?
  58. */
  59. ax25_std_enquiry_response(ax25);
  60. if (!(ax25->condition & AX25_COND_PEER_RX_BUSY)) {
  61. ax25_requeue_frames(ax25);
  62. ax25_kick(ax25);
  63. }
  64. if (ax25->state == AX25_STATE_1 || ax25->state == AX25_STATE_2 || skb_peek(&ax25->ack_queue) != NULL)
  65. ax25_ds_t1_timeout(ax25);
  66. else
  67. ax25->n2count = 0;
  68. ax25_start_t3timer(ax25);
  69. ax25_ds_set_timer(ax25->ax25_dev);
  70. spin_lock(&ax25_list_lock);
  71. ax25_for_each(ax25o, &ax25_list) {
  72. if (ax25o == ax25)
  73. continue;
  74. if (ax25o->ax25_dev != ax25->ax25_dev)
  75. continue;
  76. if (ax25o->state == AX25_STATE_1 || ax25o->state == AX25_STATE_2) {
  77. ax25_ds_t1_timeout(ax25o);
  78. continue;
  79. }
  80. if (!(ax25o->condition & AX25_COND_PEER_RX_BUSY) && ax25o->state == AX25_STATE_3) {
  81. ax25_requeue_frames(ax25o);
  82. ax25_kick(ax25o);
  83. }
  84. if (ax25o->state == AX25_STATE_1 || ax25o->state == AX25_STATE_2 || skb_peek(&ax25o->ack_queue) != NULL)
  85. ax25_ds_t1_timeout(ax25o);
  86. /* do not start T3 for listening sockets (tnx DD8NE) */
  87. if (ax25o->state != AX25_STATE_0)
  88. ax25_start_t3timer(ax25o);
  89. }
  90. spin_unlock(&ax25_list_lock);
  91. }
  92. void ax25_ds_establish_data_link(ax25_cb *ax25)
  93. {
  94. ax25->condition &= AX25_COND_DAMA_MODE;
  95. ax25->n2count = 0;
  96. ax25_calculate_t1(ax25);
  97. ax25_start_t1timer(ax25);
  98. ax25_stop_t2timer(ax25);
  99. ax25_start_t3timer(ax25);
  100. }
  101. /*
  102. * :::FIXME:::
  103. * This is a kludge. Not all drivers recognize kiss commands.
  104. * We need a driver level request to switch duplex mode, that does
  105. * either SCC changing, PI config or KISS as required. Currently
  106. * this request isn't reliable.
  107. */
  108. static void ax25_kiss_cmd(ax25_dev *ax25_dev, unsigned char cmd, unsigned char param)
  109. {
  110. struct sk_buff *skb;
  111. unsigned char *p;
  112. if (ax25_dev->dev == NULL)
  113. return;
  114. if ((skb = alloc_skb(2, GFP_ATOMIC)) == NULL)
  115. return;
  116. skb_reset_network_header(skb);
  117. p = skb_put(skb, 2);
  118. *p++ = cmd;
  119. *p++ = param;
  120. skb->protocol = ax25_type_trans(skb, ax25_dev->dev);
  121. dev_queue_xmit(skb);
  122. }
  123. /*
  124. * A nasty problem arises if we count the number of DAMA connections
  125. * wrong, especially when connections on the device already existed
  126. * and our network node (or the sysop) decides to turn on DAMA Master
  127. * mode. We thus flag the 'real' slave connections with
  128. * ax25->dama_slave=1 and look on every disconnect if still slave
  129. * connections exist.
  130. */
  131. static int ax25_check_dama_slave(ax25_dev *ax25_dev)
  132. {
  133. ax25_cb *ax25;
  134. int res = 0;
  135. spin_lock(&ax25_list_lock);
  136. ax25_for_each(ax25, &ax25_list)
  137. if (ax25->ax25_dev == ax25_dev && (ax25->condition & AX25_COND_DAMA_MODE) && ax25->state > AX25_STATE_1) {
  138. res = 1;
  139. break;
  140. }
  141. spin_unlock(&ax25_list_lock);
  142. return res;
  143. }
  144. static void ax25_dev_dama_on(ax25_dev *ax25_dev)
  145. {
  146. if (ax25_dev == NULL)
  147. return;
  148. if (ax25_dev->dama.slave == 0)
  149. ax25_kiss_cmd(ax25_dev, 5, 1);
  150. ax25_dev->dama.slave = 1;
  151. ax25_ds_set_timer(ax25_dev);
  152. }
  153. void ax25_dev_dama_off(ax25_dev *ax25_dev)
  154. {
  155. if (ax25_dev == NULL)
  156. return;
  157. if (ax25_dev->dama.slave && !ax25_check_dama_slave(ax25_dev)) {
  158. ax25_kiss_cmd(ax25_dev, 5, 0);
  159. ax25_dev->dama.slave = 0;
  160. ax25_ds_del_timer(ax25_dev);
  161. }
  162. }
  163. void ax25_dama_on(ax25_cb *ax25)
  164. {
  165. ax25_dev_dama_on(ax25->ax25_dev);
  166. ax25->condition |= AX25_COND_DAMA_MODE;
  167. }
  168. void ax25_dama_off(ax25_cb *ax25)
  169. {
  170. ax25->condition &= ~AX25_COND_DAMA_MODE;
  171. ax25_dev_dama_off(ax25->ax25_dev);
  172. }