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