ax25_ds_in.c 6.9 KB

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