ax25_std_in.c 11 KB

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