ax25_ds_in.c 7.1 KB

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