ax25_ds_subr.c 5.1 KB

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