ax25_std_in.c 11 KB

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