rose_in.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) Jonathan Naylor G4KLX ([email protected])
  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/filter.h>
  13. #include <linux/types.h>
  14. #include <linux/socket.h>
  15. #include <linux/in.h>
  16. #include <linux/kernel.h>
  17. #include <linux/timer.h>
  18. #include <linux/string.h>
  19. #include <linux/sockios.h>
  20. #include <linux/net.h>
  21. #include <net/ax25.h>
  22. #include <linux/inet.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <net/sock.h>
  26. #include <net/tcp_states.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. #include <net/rose.h>
  31. /*
  32. * State machine for state 1, Awaiting Call Accepted State.
  33. * The handling of the timer(s) is in file rose_timer.c.
  34. * Handling of state 0 and connection release is in af_rose.c.
  35. */
  36. static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  37. {
  38. struct rose_sock *rose = rose_sk(sk);
  39. switch (frametype) {
  40. case ROSE_CALL_ACCEPTED:
  41. rose_stop_timer(sk);
  42. rose_start_idletimer(sk);
  43. rose->condition = 0x00;
  44. rose->vs = 0;
  45. rose->va = 0;
  46. rose->vr = 0;
  47. rose->vl = 0;
  48. rose->state = ROSE_STATE_3;
  49. sk->sk_state = TCP_ESTABLISHED;
  50. if (!sock_flag(sk, SOCK_DEAD))
  51. sk->sk_state_change(sk);
  52. break;
  53. case ROSE_CLEAR_REQUEST:
  54. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  55. rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
  56. rose->neighbour->use--;
  57. break;
  58. default:
  59. break;
  60. }
  61. return 0;
  62. }
  63. /*
  64. * State machine for state 2, Awaiting Clear Confirmation State.
  65. * The handling of the timer(s) is in file rose_timer.c
  66. * Handling of state 0 and connection release is in af_rose.c.
  67. */
  68. static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  69. {
  70. struct rose_sock *rose = rose_sk(sk);
  71. switch (frametype) {
  72. case ROSE_CLEAR_REQUEST:
  73. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  74. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  75. rose->neighbour->use--;
  76. break;
  77. case ROSE_CLEAR_CONFIRMATION:
  78. rose_disconnect(sk, 0, -1, -1);
  79. rose->neighbour->use--;
  80. break;
  81. default:
  82. break;
  83. }
  84. return 0;
  85. }
  86. /*
  87. * State machine for state 3, Connected State.
  88. * The handling of the timer(s) is in file rose_timer.c
  89. * Handling of state 0 and connection release is in af_rose.c.
  90. */
  91. static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
  92. {
  93. struct rose_sock *rose = rose_sk(sk);
  94. int queued = 0;
  95. switch (frametype) {
  96. case ROSE_RESET_REQUEST:
  97. rose_stop_timer(sk);
  98. rose_start_idletimer(sk);
  99. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  100. rose->condition = 0x00;
  101. rose->vs = 0;
  102. rose->vr = 0;
  103. rose->va = 0;
  104. rose->vl = 0;
  105. rose_requeue_frames(sk);
  106. break;
  107. case ROSE_CLEAR_REQUEST:
  108. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  109. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  110. rose->neighbour->use--;
  111. break;
  112. case ROSE_RR:
  113. case ROSE_RNR:
  114. if (!rose_validate_nr(sk, nr)) {
  115. rose_write_internal(sk, ROSE_RESET_REQUEST);
  116. rose->condition = 0x00;
  117. rose->vs = 0;
  118. rose->vr = 0;
  119. rose->va = 0;
  120. rose->vl = 0;
  121. rose->state = ROSE_STATE_4;
  122. rose_start_t2timer(sk);
  123. rose_stop_idletimer(sk);
  124. } else {
  125. rose_frames_acked(sk, nr);
  126. if (frametype == ROSE_RNR) {
  127. rose->condition |= ROSE_COND_PEER_RX_BUSY;
  128. } else {
  129. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  130. }
  131. }
  132. break;
  133. case ROSE_DATA: /* XXX */
  134. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  135. if (!rose_validate_nr(sk, nr)) {
  136. rose_write_internal(sk, ROSE_RESET_REQUEST);
  137. rose->condition = 0x00;
  138. rose->vs = 0;
  139. rose->vr = 0;
  140. rose->va = 0;
  141. rose->vl = 0;
  142. rose->state = ROSE_STATE_4;
  143. rose_start_t2timer(sk);
  144. rose_stop_idletimer(sk);
  145. break;
  146. }
  147. rose_frames_acked(sk, nr);
  148. if (ns == rose->vr) {
  149. rose_start_idletimer(sk);
  150. if (sk_filter_trim_cap(sk, skb, ROSE_MIN_LEN) == 0 &&
  151. __sock_queue_rcv_skb(sk, skb) == 0) {
  152. rose->vr = (rose->vr + 1) % ROSE_MODULUS;
  153. queued = 1;
  154. } else {
  155. /* Should never happen ! */
  156. rose_write_internal(sk, ROSE_RESET_REQUEST);
  157. rose->condition = 0x00;
  158. rose->vs = 0;
  159. rose->vr = 0;
  160. rose->va = 0;
  161. rose->vl = 0;
  162. rose->state = ROSE_STATE_4;
  163. rose_start_t2timer(sk);
  164. rose_stop_idletimer(sk);
  165. break;
  166. }
  167. if (atomic_read(&sk->sk_rmem_alloc) >
  168. (sk->sk_rcvbuf >> 1))
  169. rose->condition |= ROSE_COND_OWN_RX_BUSY;
  170. }
  171. /*
  172. * If the window is full, ack the frame, else start the
  173. * acknowledge hold back timer.
  174. */
  175. if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
  176. rose->condition &= ~ROSE_COND_ACK_PENDING;
  177. rose_stop_timer(sk);
  178. rose_enquiry_response(sk);
  179. } else {
  180. rose->condition |= ROSE_COND_ACK_PENDING;
  181. rose_start_hbtimer(sk);
  182. }
  183. break;
  184. default:
  185. printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
  186. break;
  187. }
  188. return queued;
  189. }
  190. /*
  191. * State machine for state 4, Awaiting Reset Confirmation State.
  192. * The handling of the timer(s) is in file rose_timer.c
  193. * Handling of state 0 and connection release is in af_rose.c.
  194. */
  195. static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  196. {
  197. struct rose_sock *rose = rose_sk(sk);
  198. switch (frametype) {
  199. case ROSE_RESET_REQUEST:
  200. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  201. fallthrough;
  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. }