ax25_ds_in.c 7.1 KB

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