ax25_std_in.c 11 KB

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