rose_in.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <asm/system.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/mm.h>
  32. #include <linux/interrupt.h>
  33. #include <net/rose.h>
  34. /*
  35. * State machine for state 1, Awaiting Call Accepted State.
  36. * The handling of the timer(s) is in file rose_timer.c.
  37. * Handling of state 0 and connection release is in af_rose.c.
  38. */
  39. static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  40. {
  41. struct rose_sock *rose = rose_sk(sk);
  42. switch (frametype) {
  43. case ROSE_CALL_ACCEPTED:
  44. rose_stop_timer(sk);
  45. rose_start_idletimer(sk);
  46. rose->condition = 0x00;
  47. rose->vs = 0;
  48. rose->va = 0;
  49. rose->vr = 0;
  50. rose->vl = 0;
  51. rose->state = ROSE_STATE_3;
  52. sk->sk_state = TCP_ESTABLISHED;
  53. if (!sock_flag(sk, SOCK_DEAD))
  54. sk->sk_state_change(sk);
  55. break;
  56. case ROSE_CLEAR_REQUEST:
  57. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  58. rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
  59. rose->neighbour->use--;
  60. break;
  61. default:
  62. break;
  63. }
  64. return 0;
  65. }
  66. /*
  67. * State machine for state 2, Awaiting Clear Confirmation State.
  68. * The handling of the timer(s) is in file rose_timer.c
  69. * Handling of state 0 and connection release is in af_rose.c.
  70. */
  71. static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  72. {
  73. struct rose_sock *rose = rose_sk(sk);
  74. switch (frametype) {
  75. case ROSE_CLEAR_REQUEST:
  76. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  77. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  78. rose->neighbour->use--;
  79. break;
  80. case ROSE_CLEAR_CONFIRMATION:
  81. rose_disconnect(sk, 0, -1, -1);
  82. rose->neighbour->use--;
  83. break;
  84. default:
  85. break;
  86. }
  87. return 0;
  88. }
  89. /*
  90. * State machine for state 3, Connected State.
  91. * The handling of the timer(s) is in file rose_timer.c
  92. * Handling of state 0 and connection release is in af_rose.c.
  93. */
  94. static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
  95. {
  96. struct rose_sock *rose = rose_sk(sk);
  97. int queued = 0;
  98. switch (frametype) {
  99. case ROSE_RESET_REQUEST:
  100. rose_stop_timer(sk);
  101. rose_start_idletimer(sk);
  102. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  103. rose->condition = 0x00;
  104. rose->vs = 0;
  105. rose->vr = 0;
  106. rose->va = 0;
  107. rose->vl = 0;
  108. rose_requeue_frames(sk);
  109. break;
  110. case ROSE_CLEAR_REQUEST:
  111. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  112. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  113. rose->neighbour->use--;
  114. break;
  115. case ROSE_RR:
  116. case ROSE_RNR:
  117. if (!rose_validate_nr(sk, nr)) {
  118. rose_write_internal(sk, ROSE_RESET_REQUEST);
  119. rose->condition = 0x00;
  120. rose->vs = 0;
  121. rose->vr = 0;
  122. rose->va = 0;
  123. rose->vl = 0;
  124. rose->state = ROSE_STATE_4;
  125. rose_start_t2timer(sk);
  126. rose_stop_idletimer(sk);
  127. } else {
  128. rose_frames_acked(sk, nr);
  129. if (frametype == ROSE_RNR) {
  130. rose->condition |= ROSE_COND_PEER_RX_BUSY;
  131. } else {
  132. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  133. }
  134. }
  135. break;
  136. case ROSE_DATA: /* XXX */
  137. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  138. if (!rose_validate_nr(sk, nr)) {
  139. rose_write_internal(sk, ROSE_RESET_REQUEST);
  140. rose->condition = 0x00;
  141. rose->vs = 0;
  142. rose->vr = 0;
  143. rose->va = 0;
  144. rose->vl = 0;
  145. rose->state = ROSE_STATE_4;
  146. rose_start_t2timer(sk);
  147. rose_stop_idletimer(sk);
  148. break;
  149. }
  150. rose_frames_acked(sk, nr);
  151. if (ns == rose->vr) {
  152. rose_start_idletimer(sk);
  153. if (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 / 2))
  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. }