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