rose_in.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  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/fcntl.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. #include <net/rose.h>
  33. /*
  34. * State machine for state 1, Awaiting Call Accepted State.
  35. * The handling of the timer(s) is in file rose_timer.c.
  36. * Handling of state 0 and connection release is in af_rose.c.
  37. */
  38. static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  39. {
  40. struct rose_sock *rose = rose_sk(sk);
  41. switch (frametype) {
  42. case ROSE_CALL_ACCEPTED:
  43. rose_stop_timer(sk);
  44. rose_start_idletimer(sk);
  45. rose->condition = 0x00;
  46. rose->vs = 0;
  47. rose->va = 0;
  48. rose->vr = 0;
  49. rose->vl = 0;
  50. rose->state = ROSE_STATE_3;
  51. sk->sk_state = TCP_ESTABLISHED;
  52. if (!sock_flag(sk, SOCK_DEAD))
  53. sk->sk_state_change(sk);
  54. break;
  55. case ROSE_CLEAR_REQUEST:
  56. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  57. rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
  58. rose->neighbour->use--;
  59. break;
  60. default:
  61. break;
  62. }
  63. return 0;
  64. }
  65. /*
  66. * State machine for state 2, Awaiting Clear Confirmation State.
  67. * The handling of the timer(s) is in file rose_timer.c
  68. * Handling of state 0 and connection release is in af_rose.c.
  69. */
  70. static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  71. {
  72. struct rose_sock *rose = rose_sk(sk);
  73. switch (frametype) {
  74. case ROSE_CLEAR_REQUEST:
  75. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  76. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  77. rose->neighbour->use--;
  78. break;
  79. case ROSE_CLEAR_CONFIRMATION:
  80. rose_disconnect(sk, 0, -1, -1);
  81. rose->neighbour->use--;
  82. break;
  83. default:
  84. break;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * State machine for state 3, Connected State.
  90. * The handling of the timer(s) is in file rose_timer.c
  91. * Handling of state 0 and connection release is in af_rose.c.
  92. */
  93. static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
  94. {
  95. struct rose_sock *rose = rose_sk(sk);
  96. int queued = 0;
  97. switch (frametype) {
  98. case ROSE_RESET_REQUEST:
  99. rose_stop_timer(sk);
  100. rose_start_idletimer(sk);
  101. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  102. rose->condition = 0x00;
  103. rose->vs = 0;
  104. rose->vr = 0;
  105. rose->va = 0;
  106. rose->vl = 0;
  107. rose_requeue_frames(sk);
  108. break;
  109. case ROSE_CLEAR_REQUEST:
  110. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  111. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  112. rose->neighbour->use--;
  113. break;
  114. case ROSE_RR:
  115. case ROSE_RNR:
  116. if (!rose_validate_nr(sk, nr)) {
  117. rose_write_internal(sk, ROSE_RESET_REQUEST);
  118. rose->condition = 0x00;
  119. rose->vs = 0;
  120. rose->vr = 0;
  121. rose->va = 0;
  122. rose->vl = 0;
  123. rose->state = ROSE_STATE_4;
  124. rose_start_t2timer(sk);
  125. rose_stop_idletimer(sk);
  126. } else {
  127. rose_frames_acked(sk, nr);
  128. if (frametype == ROSE_RNR) {
  129. rose->condition |= ROSE_COND_PEER_RX_BUSY;
  130. } else {
  131. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  132. }
  133. }
  134. break;
  135. case ROSE_DATA: /* XXX */
  136. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  137. if (!rose_validate_nr(sk, nr)) {
  138. rose_write_internal(sk, ROSE_RESET_REQUEST);
  139. rose->condition = 0x00;
  140. rose->vs = 0;
  141. rose->vr = 0;
  142. rose->va = 0;
  143. rose->vl = 0;
  144. rose->state = ROSE_STATE_4;
  145. rose_start_t2timer(sk);
  146. rose_stop_idletimer(sk);
  147. break;
  148. }
  149. rose_frames_acked(sk, nr);
  150. if (ns == rose->vr) {
  151. rose_start_idletimer(sk);
  152. if (sk_filter_trim_cap(sk, skb, ROSE_MIN_LEN) == 0 &&
  153. __sock_queue_rcv_skb(sk, skb) == 0) {
  154. rose->vr = (rose->vr + 1) % ROSE_MODULUS;
  155. queued = 1;
  156. } else {
  157. /* Should never happen ! */
  158. rose_write_internal(sk, ROSE_RESET_REQUEST);
  159. rose->condition = 0x00;
  160. rose->vs = 0;
  161. rose->vr = 0;
  162. rose->va = 0;
  163. rose->vl = 0;
  164. rose->state = ROSE_STATE_4;
  165. rose_start_t2timer(sk);
  166. rose_stop_idletimer(sk);
  167. break;
  168. }
  169. if (atomic_read(&sk->sk_rmem_alloc) >
  170. (sk->sk_rcvbuf >> 1))
  171. rose->condition |= ROSE_COND_OWN_RX_BUSY;
  172. }
  173. /*
  174. * If the window is full, ack the frame, else start the
  175. * acknowledge hold back timer.
  176. */
  177. if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
  178. rose->condition &= ~ROSE_COND_ACK_PENDING;
  179. rose_stop_timer(sk);
  180. rose_enquiry_response(sk);
  181. } else {
  182. rose->condition |= ROSE_COND_ACK_PENDING;
  183. rose_start_hbtimer(sk);
  184. }
  185. break;
  186. default:
  187. printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
  188. break;
  189. }
  190. return queued;
  191. }
  192. /*
  193. * State machine for state 4, Awaiting Reset Confirmation State.
  194. * The handling of the timer(s) is in file rose_timer.c
  195. * Handling of state 0 and connection release is in af_rose.c.
  196. */
  197. static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  198. {
  199. struct rose_sock *rose = rose_sk(sk);
  200. switch (frametype) {
  201. case ROSE_RESET_REQUEST:
  202. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  203. case ROSE_RESET_CONFIRMATION:
  204. rose_stop_timer(sk);
  205. rose_start_idletimer(sk);
  206. rose->condition = 0x00;
  207. rose->va = 0;
  208. rose->vr = 0;
  209. rose->vs = 0;
  210. rose->vl = 0;
  211. rose->state = ROSE_STATE_3;
  212. rose_requeue_frames(sk);
  213. break;
  214. case ROSE_CLEAR_REQUEST:
  215. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  216. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  217. rose->neighbour->use--;
  218. break;
  219. default:
  220. break;
  221. }
  222. return 0;
  223. }
  224. /*
  225. * State machine for state 5, Awaiting Call Acceptance State.
  226. * The handling of the timer(s) is in file rose_timer.c
  227. * Handling of state 0 and connection release is in af_rose.c.
  228. */
  229. static int rose_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  230. {
  231. if (frametype == ROSE_CLEAR_REQUEST) {
  232. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  233. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  234. rose_sk(sk)->neighbour->use--;
  235. }
  236. return 0;
  237. }
  238. /* Higher level upcall for a LAPB frame */
  239. int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb)
  240. {
  241. struct rose_sock *rose = rose_sk(sk);
  242. int queued = 0, frametype, ns, nr, q, d, m;
  243. if (rose->state == ROSE_STATE_0)
  244. return 0;
  245. frametype = rose_decode(skb, &ns, &nr, &q, &d, &m);
  246. switch (rose->state) {
  247. case ROSE_STATE_1:
  248. queued = rose_state1_machine(sk, skb, frametype);
  249. break;
  250. case ROSE_STATE_2:
  251. queued = rose_state2_machine(sk, skb, frametype);
  252. break;
  253. case ROSE_STATE_3:
  254. queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
  255. break;
  256. case ROSE_STATE_4:
  257. queued = rose_state4_machine(sk, skb, frametype);
  258. break;
  259. case ROSE_STATE_5:
  260. queued = rose_state5_machine(sk, skb, frametype);
  261. break;
  262. }
  263. rose_kick(sk);
  264. return queued;
  265. }