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