rose_in.c 7.3 KB

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