ax25_ds_in.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/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 <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. * State machine for state 1, Awaiting Connection State.
  31. * The handling of the timer(s) is in file ax25_ds_timer.c.
  32. * Handling of state 0 and connection release is in ax25.c.
  33. */
  34. static int ax25_ds_state1_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
  35. {
  36. switch (frametype) {
  37. case AX25_SABM:
  38. ax25->modulus = AX25_MODULUS;
  39. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  40. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  41. break;
  42. case AX25_SABME:
  43. ax25->modulus = AX25_EMODULUS;
  44. ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
  45. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  46. break;
  47. case AX25_DISC:
  48. ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
  49. break;
  50. case AX25_UA:
  51. ax25_calculate_rtt(ax25);
  52. ax25_stop_t1timer(ax25);
  53. ax25_start_t3timer(ax25);
  54. ax25_start_idletimer(ax25);
  55. ax25->vs = 0;
  56. ax25->va = 0;
  57. ax25->vr = 0;
  58. ax25->state = AX25_STATE_3;
  59. ax25->n2count = 0;
  60. if (ax25->sk != NULL) {
  61. bh_lock_sock(ax25->sk);
  62. ax25->sk->sk_state = TCP_ESTABLISHED;
  63. /*
  64. * For WAIT_SABM connections we will produce an accept
  65. * ready socket here
  66. */
  67. if (!sock_flag(ax25->sk, SOCK_DEAD))
  68. ax25->sk->sk_state_change(ax25->sk);
  69. bh_unlock_sock(ax25->sk);
  70. }
  71. ax25_dama_on(ax25);
  72. /* according to DK4EG's spec we are required to
  73. * send a RR RESPONSE FINAL NR=0.
  74. */
  75. ax25_std_enquiry_response(ax25);
  76. break;
  77. case AX25_DM:
  78. if (pf)
  79. ax25_disconnect(ax25, ECONNREFUSED);
  80. break;
  81. default:
  82. if (pf)
  83. ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
  84. break;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * State machine for state 2, Awaiting Release State.
  90. * The handling of the timer(s) is in file ax25_ds_timer.c
  91. * Handling of state 0 and connection release is in ax25.c.
  92. */
  93. static int ax25_ds_state2_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
  94. {
  95. switch (frametype) {
  96. case AX25_SABM:
  97. case AX25_SABME:
  98. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  99. ax25_dama_off(ax25);
  100. break;
  101. case AX25_DISC:
  102. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  103. ax25_dama_off(ax25);
  104. ax25_disconnect(ax25, 0);
  105. break;
  106. case AX25_DM:
  107. case AX25_UA:
  108. if (pf) {
  109. ax25_dama_off(ax25);
  110. ax25_disconnect(ax25, 0);
  111. }
  112. break;
  113. case AX25_I:
  114. case AX25_REJ:
  115. case AX25_RNR:
  116. case AX25_RR:
  117. if (pf) {
  118. ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  119. ax25_dama_off(ax25);
  120. }
  121. break;
  122. default:
  123. break;
  124. }
  125. return 0;
  126. }
  127. /*
  128. * State machine for state 3, Connected State.
  129. * The handling of the timer(s) is in file ax25_timer.c
  130. * Handling of state 0 and connection release is in ax25.c.
  131. */
  132. static int ax25_ds_state3_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
  133. {
  134. int queued = 0;
  135. switch (frametype) {
  136. case AX25_SABM:
  137. case AX25_SABME:
  138. if (frametype == AX25_SABM) {
  139. ax25->modulus = AX25_MODULUS;
  140. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  141. } else {
  142. ax25->modulus = AX25_EMODULUS;
  143. ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
  144. }
  145. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  146. ax25_stop_t1timer(ax25);
  147. ax25_start_t3timer(ax25);
  148. ax25_start_idletimer(ax25);
  149. ax25->condition = 0x00;
  150. ax25->vs = 0;
  151. ax25->va = 0;
  152. ax25->vr = 0;
  153. ax25_requeue_frames(ax25);
  154. ax25_dama_on(ax25);
  155. break;
  156. case AX25_DISC:
  157. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  158. ax25_dama_off(ax25);
  159. ax25_disconnect(ax25, 0);
  160. break;
  161. case AX25_DM:
  162. ax25_dama_off(ax25);
  163. ax25_disconnect(ax25, ECONNRESET);
  164. break;
  165. case AX25_RR:
  166. case AX25_RNR:
  167. if (frametype == AX25_RR)
  168. ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
  169. else
  170. ax25->condition |= AX25_COND_PEER_RX_BUSY;
  171. if (ax25_validate_nr(ax25, nr)) {
  172. if (ax25_check_iframes_acked(ax25, nr))
  173. ax25->n2count=0;
  174. if (type == AX25_COMMAND && pf)
  175. ax25_ds_enquiry_response(ax25);
  176. } else {
  177. ax25_ds_nr_error_recovery(ax25);
  178. ax25->state = AX25_STATE_1;
  179. }
  180. break;
  181. case AX25_REJ:
  182. ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
  183. if (ax25_validate_nr(ax25, nr)) {
  184. if (ax25->va != nr)
  185. ax25->n2count=0;
  186. ax25_frames_acked(ax25, nr);
  187. ax25_calculate_rtt(ax25);
  188. ax25_stop_t1timer(ax25);
  189. ax25_start_t3timer(ax25);
  190. ax25_requeue_frames(ax25);
  191. if (type == AX25_COMMAND && pf)
  192. ax25_ds_enquiry_response(ax25);
  193. } else {
  194. ax25_ds_nr_error_recovery(ax25);
  195. ax25->state = AX25_STATE_1;
  196. }
  197. break;
  198. case AX25_I:
  199. if (!ax25_validate_nr(ax25, nr)) {
  200. ax25_ds_nr_error_recovery(ax25);
  201. ax25->state = AX25_STATE_1;
  202. break;
  203. }
  204. if (ax25->condition & AX25_COND_PEER_RX_BUSY) {
  205. ax25_frames_acked(ax25, nr);
  206. ax25->n2count = 0;
  207. } else {
  208. if (ax25_check_iframes_acked(ax25, nr))
  209. ax25->n2count = 0;
  210. }
  211. if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
  212. if (pf) ax25_ds_enquiry_response(ax25);
  213. break;
  214. }
  215. if (ns == ax25->vr) {
  216. ax25->vr = (ax25->vr + 1) % ax25->modulus;
  217. queued = ax25_rx_iframe(ax25, skb);
  218. if (ax25->condition & AX25_COND_OWN_RX_BUSY)
  219. ax25->vr = ns; /* ax25->vr - 1 */
  220. ax25->condition &= ~AX25_COND_REJECT;
  221. if (pf) {
  222. ax25_ds_enquiry_response(ax25);
  223. } else {
  224. if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
  225. ax25->condition |= AX25_COND_ACK_PENDING;
  226. ax25_start_t2timer(ax25);
  227. }
  228. }
  229. } else {
  230. if (ax25->condition & AX25_COND_REJECT) {
  231. if (pf) ax25_ds_enquiry_response(ax25);
  232. } else {
  233. ax25->condition |= AX25_COND_REJECT;
  234. ax25_ds_enquiry_response(ax25);
  235. ax25->condition &= ~AX25_COND_ACK_PENDING;
  236. }
  237. }
  238. break;
  239. case AX25_FRMR:
  240. case AX25_ILLEGAL:
  241. ax25_ds_establish_data_link(ax25);
  242. ax25->state = AX25_STATE_1;
  243. break;
  244. default:
  245. break;
  246. }
  247. return queued;
  248. }
  249. /*
  250. * Higher level upcall for a LAPB frame
  251. */
  252. int ax25_ds_frame_in(ax25_cb *ax25, struct sk_buff *skb, int type)
  253. {
  254. int queued = 0, frametype, ns, nr, pf;
  255. frametype = ax25_decode(ax25, skb, &ns, &nr, &pf);
  256. switch (ax25->state) {
  257. case AX25_STATE_1:
  258. queued = ax25_ds_state1_machine(ax25, skb, frametype, pf, type);
  259. break;
  260. case AX25_STATE_2:
  261. queued = ax25_ds_state2_machine(ax25, skb, frametype, pf, type);
  262. break;
  263. case AX25_STATE_3:
  264. queued = ax25_ds_state3_machine(ax25, skb, frametype, ns, nr, pf, type);
  265. break;
  266. }
  267. return queued;
  268. }