ax25_std_in.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) Alan Cox GW4PTS ([email protected])
  5. * Copyright (C) Jonathan Naylor G4KLX ([email protected])
  6. * Copyright (C) Joerg Reuter DL1BKE ([email protected])
  7. * Copyright (C) Hans-Joachim Hetscher DD8NE ([email protected])
  8. *
  9. * Most of this code is based on the SDL diagrams published in the 7th ARRL
  10. * Computer Networking Conference papers. The diagrams have mistakes in them,
  11. * but are mostly correct. Before you modify the code could you read the SDL
  12. * diagrams as the code is not obvious and probably very easy to break.
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/types.h>
  16. #include <linux/socket.h>
  17. #include <linux/in.h>
  18. #include <linux/kernel.h>
  19. #include <linux/timer.h>
  20. #include <linux/string.h>
  21. #include <linux/sockios.h>
  22. #include <linux/net.h>
  23. #include <net/ax25.h>
  24. #include <linux/inet.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <net/sock.h>
  28. #include <net/tcp_states.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/mm.h>
  32. #include <linux/interrupt.h>
  33. /*
  34. * State machine for state 1, Awaiting Connection State.
  35. * The handling of the timer(s) is in file ax25_std_timer.c.
  36. * Handling of state 0 and connection release is in ax25.c.
  37. */
  38. static int ax25_std_state1_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
  39. {
  40. switch (frametype) {
  41. case AX25_SABM:
  42. ax25->modulus = AX25_MODULUS;
  43. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  44. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  45. break;
  46. case AX25_SABME:
  47. ax25->modulus = AX25_EMODULUS;
  48. ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
  49. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  50. break;
  51. case AX25_DISC:
  52. ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
  53. break;
  54. case AX25_UA:
  55. if (pf) {
  56. ax25_calculate_rtt(ax25);
  57. ax25_stop_t1timer(ax25);
  58. ax25_start_t3timer(ax25);
  59. ax25_start_idletimer(ax25);
  60. ax25->vs = 0;
  61. ax25->va = 0;
  62. ax25->vr = 0;
  63. ax25->state = AX25_STATE_3;
  64. ax25->n2count = 0;
  65. if (ax25->sk != NULL) {
  66. bh_lock_sock(ax25->sk);
  67. ax25->sk->sk_state = TCP_ESTABLISHED;
  68. /* For WAIT_SABM connections we will produce an accept ready socket here */
  69. if (!sock_flag(ax25->sk, SOCK_DEAD))
  70. ax25->sk->sk_state_change(ax25->sk);
  71. bh_unlock_sock(ax25->sk);
  72. }
  73. }
  74. break;
  75. case AX25_DM:
  76. if (pf) {
  77. if (ax25->modulus == AX25_MODULUS) {
  78. ax25_disconnect(ax25, ECONNREFUSED);
  79. } else {
  80. ax25->modulus = AX25_MODULUS;
  81. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  82. }
  83. }
  84. break;
  85. default:
  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_std_timer.c
  93. * Handling of state 0 and connection release is in ax25.c.
  94. */
  95. static int ax25_std_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_DM, pf, AX25_RESPONSE);
  101. break;
  102. case AX25_DISC:
  103. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  104. ax25_disconnect(ax25, 0);
  105. break;
  106. case AX25_DM:
  107. case AX25_UA:
  108. if (pf)
  109. ax25_disconnect(ax25, 0);
  110. break;
  111. case AX25_I:
  112. case AX25_REJ:
  113. case AX25_RNR:
  114. case AX25_RR:
  115. if (pf) ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
  116. break;
  117. default:
  118. break;
  119. }
  120. return 0;
  121. }
  122. /*
  123. * State machine for state 3, Connected State.
  124. * The handling of the timer(s) is in file ax25_std_timer.c
  125. * Handling of state 0 and connection release is in ax25.c.
  126. */
  127. static int ax25_std_state3_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
  128. {
  129. int queued = 0;
  130. switch (frametype) {
  131. case AX25_SABM:
  132. case AX25_SABME:
  133. if (frametype == AX25_SABM) {
  134. ax25->modulus = AX25_MODULUS;
  135. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  136. } else {
  137. ax25->modulus = AX25_EMODULUS;
  138. ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
  139. }
  140. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  141. ax25_stop_t1timer(ax25);
  142. ax25_stop_t2timer(ax25);
  143. ax25_start_t3timer(ax25);
  144. ax25_start_idletimer(ax25);
  145. ax25->condition = 0x00;
  146. ax25->vs = 0;
  147. ax25->va = 0;
  148. ax25->vr = 0;
  149. ax25_requeue_frames(ax25);
  150. break;
  151. case AX25_DISC:
  152. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  153. ax25_disconnect(ax25, 0);
  154. break;
  155. case AX25_DM:
  156. ax25_disconnect(ax25, ECONNRESET);
  157. break;
  158. case AX25_RR:
  159. case AX25_RNR:
  160. if (frametype == AX25_RR)
  161. ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
  162. else
  163. ax25->condition |= AX25_COND_PEER_RX_BUSY;
  164. if (type == AX25_COMMAND && pf)
  165. ax25_std_enquiry_response(ax25);
  166. if (ax25_validate_nr(ax25, nr)) {
  167. ax25_check_iframes_acked(ax25, nr);
  168. } else {
  169. ax25_std_nr_error_recovery(ax25);
  170. ax25->state = AX25_STATE_1;
  171. }
  172. break;
  173. case AX25_REJ:
  174. ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
  175. if (type == AX25_COMMAND && pf)
  176. ax25_std_enquiry_response(ax25);
  177. if (ax25_validate_nr(ax25, nr)) {
  178. ax25_frames_acked(ax25, nr);
  179. ax25_calculate_rtt(ax25);
  180. ax25_stop_t1timer(ax25);
  181. ax25_start_t3timer(ax25);
  182. ax25_requeue_frames(ax25);
  183. } else {
  184. ax25_std_nr_error_recovery(ax25);
  185. ax25->state = AX25_STATE_1;
  186. }
  187. break;
  188. case AX25_I:
  189. if (!ax25_validate_nr(ax25, nr)) {
  190. ax25_std_nr_error_recovery(ax25);
  191. ax25->state = AX25_STATE_1;
  192. break;
  193. }
  194. if (ax25->condition & AX25_COND_PEER_RX_BUSY) {
  195. ax25_frames_acked(ax25, nr);
  196. } else {
  197. ax25_check_iframes_acked(ax25, nr);
  198. }
  199. if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
  200. if (pf) ax25_std_enquiry_response(ax25);
  201. break;
  202. }
  203. if (ns == ax25->vr) {
  204. ax25->vr = (ax25->vr + 1) % ax25->modulus;
  205. queued = ax25_rx_iframe(ax25, skb);
  206. if (ax25->condition & AX25_COND_OWN_RX_BUSY)
  207. ax25->vr = ns; /* ax25->vr - 1 */
  208. ax25->condition &= ~AX25_COND_REJECT;
  209. if (pf) {
  210. ax25_std_enquiry_response(ax25);
  211. } else {
  212. if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
  213. ax25->condition |= AX25_COND_ACK_PENDING;
  214. ax25_start_t2timer(ax25);
  215. }
  216. }
  217. } else {
  218. if (ax25->condition & AX25_COND_REJECT) {
  219. if (pf) ax25_std_enquiry_response(ax25);
  220. } else {
  221. ax25->condition |= AX25_COND_REJECT;
  222. ax25_send_control(ax25, AX25_REJ, pf, AX25_RESPONSE);
  223. ax25->condition &= ~AX25_COND_ACK_PENDING;
  224. }
  225. }
  226. break;
  227. case AX25_FRMR:
  228. case AX25_ILLEGAL:
  229. ax25_std_establish_data_link(ax25);
  230. ax25->state = AX25_STATE_1;
  231. break;
  232. default:
  233. break;
  234. }
  235. return queued;
  236. }
  237. /*
  238. * State machine for state 4, Timer Recovery State.
  239. * The handling of the timer(s) is in file ax25_std_timer.c
  240. * Handling of state 0 and connection release is in ax25.c.
  241. */
  242. static int ax25_std_state4_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
  243. {
  244. int queued = 0;
  245. switch (frametype) {
  246. case AX25_SABM:
  247. case AX25_SABME:
  248. if (frametype == AX25_SABM) {
  249. ax25->modulus = AX25_MODULUS;
  250. ax25->window = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  251. } else {
  252. ax25->modulus = AX25_EMODULUS;
  253. ax25->window = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
  254. }
  255. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  256. ax25_stop_t1timer(ax25);
  257. ax25_stop_t2timer(ax25);
  258. ax25_start_t3timer(ax25);
  259. ax25_start_idletimer(ax25);
  260. ax25->condition = 0x00;
  261. ax25->vs = 0;
  262. ax25->va = 0;
  263. ax25->vr = 0;
  264. ax25->state = AX25_STATE_3;
  265. ax25->n2count = 0;
  266. ax25_requeue_frames(ax25);
  267. break;
  268. case AX25_DISC:
  269. ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
  270. ax25_disconnect(ax25, 0);
  271. break;
  272. case AX25_DM:
  273. ax25_disconnect(ax25, ECONNRESET);
  274. break;
  275. case AX25_RR:
  276. case AX25_RNR:
  277. if (frametype == AX25_RR)
  278. ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
  279. else
  280. ax25->condition |= AX25_COND_PEER_RX_BUSY;
  281. if (type == AX25_RESPONSE && pf) {
  282. ax25_stop_t1timer(ax25);
  283. ax25->n2count = 0;
  284. if (ax25_validate_nr(ax25, nr)) {
  285. ax25_frames_acked(ax25, nr);
  286. if (ax25->vs == ax25->va) {
  287. ax25_start_t3timer(ax25);
  288. ax25->state = AX25_STATE_3;
  289. } else {
  290. ax25_requeue_frames(ax25);
  291. }
  292. } else {
  293. ax25_std_nr_error_recovery(ax25);
  294. ax25->state = AX25_STATE_1;
  295. }
  296. break;
  297. }
  298. if (type == AX25_COMMAND && pf)
  299. ax25_std_enquiry_response(ax25);
  300. if (ax25_validate_nr(ax25, nr)) {
  301. ax25_frames_acked(ax25, nr);
  302. } else {
  303. ax25_std_nr_error_recovery(ax25);
  304. ax25->state = AX25_STATE_1;
  305. }
  306. break;
  307. case AX25_REJ:
  308. ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
  309. if (pf && type == AX25_RESPONSE) {
  310. ax25_stop_t1timer(ax25);
  311. ax25->n2count = 0;
  312. if (ax25_validate_nr(ax25, nr)) {
  313. ax25_frames_acked(ax25, nr);
  314. if (ax25->vs == ax25->va) {
  315. ax25_start_t3timer(ax25);
  316. ax25->state = AX25_STATE_3;
  317. } else {
  318. ax25_requeue_frames(ax25);
  319. }
  320. } else {
  321. ax25_std_nr_error_recovery(ax25);
  322. ax25->state = AX25_STATE_1;
  323. }
  324. break;
  325. }
  326. if (type == AX25_COMMAND && pf)
  327. ax25_std_enquiry_response(ax25);
  328. if (ax25_validate_nr(ax25, nr)) {
  329. ax25_frames_acked(ax25, nr);
  330. ax25_requeue_frames(ax25);
  331. } else {
  332. ax25_std_nr_error_recovery(ax25);
  333. ax25->state = AX25_STATE_1;
  334. }
  335. break;
  336. case AX25_I:
  337. if (!ax25_validate_nr(ax25, nr)) {
  338. ax25_std_nr_error_recovery(ax25);
  339. ax25->state = AX25_STATE_1;
  340. break;
  341. }
  342. ax25_frames_acked(ax25, nr);
  343. if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
  344. if (pf)
  345. ax25_std_enquiry_response(ax25);
  346. break;
  347. }
  348. if (ns == ax25->vr) {
  349. ax25->vr = (ax25->vr + 1) % ax25->modulus;
  350. queued = ax25_rx_iframe(ax25, skb);
  351. if (ax25->condition & AX25_COND_OWN_RX_BUSY)
  352. ax25->vr = ns; /* ax25->vr - 1 */
  353. ax25->condition &= ~AX25_COND_REJECT;
  354. if (pf) {
  355. ax25_std_enquiry_response(ax25);
  356. } else {
  357. if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
  358. ax25->condition |= AX25_COND_ACK_PENDING;
  359. ax25_start_t2timer(ax25);
  360. }
  361. }
  362. } else {
  363. if (ax25->condition & AX25_COND_REJECT) {
  364. if (pf) ax25_std_enquiry_response(ax25);
  365. } else {
  366. ax25->condition |= AX25_COND_REJECT;
  367. ax25_send_control(ax25, AX25_REJ, pf, AX25_RESPONSE);
  368. ax25->condition &= ~AX25_COND_ACK_PENDING;
  369. }
  370. }
  371. break;
  372. case AX25_FRMR:
  373. case AX25_ILLEGAL:
  374. ax25_std_establish_data_link(ax25);
  375. ax25->state = AX25_STATE_1;
  376. break;
  377. default:
  378. break;
  379. }
  380. return queued;
  381. }
  382. /*
  383. * Higher level upcall for a LAPB frame
  384. */
  385. int ax25_std_frame_in(ax25_cb *ax25, struct sk_buff *skb, int type)
  386. {
  387. int queued = 0, frametype, ns, nr, pf;
  388. frametype = ax25_decode(ax25, skb, &ns, &nr, &pf);
  389. switch (ax25->state) {
  390. case AX25_STATE_1:
  391. queued = ax25_std_state1_machine(ax25, skb, frametype, pf, type);
  392. break;
  393. case AX25_STATE_2:
  394. queued = ax25_std_state2_machine(ax25, skb, frametype, pf, type);
  395. break;
  396. case AX25_STATE_3:
  397. queued = ax25_std_state3_machine(ax25, skb, frametype, ns, nr, pf, type);
  398. break;
  399. case AX25_STATE_4:
  400. queued = ax25_std_state4_machine(ax25, skb, frametype, ns, nr, pf, type);
  401. break;
  402. }
  403. ax25_kick(ax25);
  404. return queued;
  405. }