rose_in.c 7.5 KB

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