rose_in.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 (sock_queue_rcv_skb(sk, skb) == 0) {
  153. rose->vr = (rose->vr + 1) % ROSE_MODULUS;
  154. queued = 1;
  155. } else {
  156. /* Should never happen ! */
  157. rose_write_internal(sk, ROSE_RESET_REQUEST);
  158. rose->condition = 0x00;
  159. rose->vs = 0;
  160. rose->vr = 0;
  161. rose->va = 0;
  162. rose->vl = 0;
  163. rose->state = ROSE_STATE_4;
  164. rose_start_t2timer(sk);
  165. rose_stop_idletimer(sk);
  166. break;
  167. }
  168. if (atomic_read(&sk->sk_rmem_alloc) >
  169. (sk->sk_rcvbuf >> 1))
  170. rose->condition |= ROSE_COND_OWN_RX_BUSY;
  171. }
  172. /*
  173. * If the window is full, ack the frame, else start the
  174. * acknowledge hold back timer.
  175. */
  176. if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
  177. rose->condition &= ~ROSE_COND_ACK_PENDING;
  178. rose_stop_timer(sk);
  179. rose_enquiry_response(sk);
  180. } else {
  181. rose->condition |= ROSE_COND_ACK_PENDING;
  182. rose_start_hbtimer(sk);
  183. }
  184. break;
  185. default:
  186. printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
  187. break;
  188. }
  189. return queued;
  190. }
  191. /*
  192. * State machine for state 4, Awaiting Reset Confirmation State.
  193. * The handling of the timer(s) is in file rose_timer.c
  194. * Handling of state 0 and connection release is in af_rose.c.
  195. */
  196. static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  197. {
  198. struct rose_sock *rose = rose_sk(sk);
  199. switch (frametype) {
  200. case ROSE_RESET_REQUEST:
  201. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  202. case ROSE_RESET_CONFIRMATION:
  203. rose_stop_timer(sk);
  204. rose_start_idletimer(sk);
  205. rose->condition = 0x00;
  206. rose->va = 0;
  207. rose->vr = 0;
  208. rose->vs = 0;
  209. rose->vl = 0;
  210. rose->state = ROSE_STATE_3;
  211. rose_requeue_frames(sk);
  212. break;
  213. case ROSE_CLEAR_REQUEST:
  214. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  215. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  216. rose->neighbour->use--;
  217. break;
  218. default:
  219. break;
  220. }
  221. return 0;
  222. }
  223. /*
  224. * State machine for state 5, Awaiting Call Acceptance State.
  225. * The handling of the timer(s) is in file rose_timer.c
  226. * Handling of state 0 and connection release is in af_rose.c.
  227. */
  228. static int rose_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  229. {
  230. if (frametype == ROSE_CLEAR_REQUEST) {
  231. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  232. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  233. rose_sk(sk)->neighbour->use--;
  234. }
  235. return 0;
  236. }
  237. /* Higher level upcall for a LAPB frame */
  238. int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb)
  239. {
  240. struct rose_sock *rose = rose_sk(sk);
  241. int queued = 0, frametype, ns, nr, q, d, m;
  242. if (rose->state == ROSE_STATE_0)
  243. return 0;
  244. frametype = rose_decode(skb, &ns, &nr, &q, &d, &m);
  245. switch (rose->state) {
  246. case ROSE_STATE_1:
  247. queued = rose_state1_machine(sk, skb, frametype);
  248. break;
  249. case ROSE_STATE_2:
  250. queued = rose_state2_machine(sk, skb, frametype);
  251. break;
  252. case ROSE_STATE_3:
  253. queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
  254. break;
  255. case ROSE_STATE_4:
  256. queued = rose_state4_machine(sk, skb, frametype);
  257. break;
  258. case ROSE_STATE_5:
  259. queued = rose_state5_machine(sk, skb, frametype);
  260. break;
  261. }
  262. rose_kick(sk);
  263. return queued;
  264. }