rose_in.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/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_neigh_put(rose->neighbour);
  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_neigh_put(rose->neighbour);
  76. break;
  77. case ROSE_CLEAR_CONFIRMATION:
  78. rose_disconnect(sk, 0, -1, -1);
  79. rose_neigh_put(rose->neighbour);
  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. enum skb_drop_reason dr; /* ignored */
  94. struct rose_sock *rose = rose_sk(sk);
  95. int queued = 0;
  96. switch (frametype) {
  97. case ROSE_RESET_REQUEST:
  98. rose_stop_timer(sk);
  99. rose_start_idletimer(sk);
  100. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  101. rose->condition = 0x00;
  102. rose->vs = 0;
  103. rose->vr = 0;
  104. rose->va = 0;
  105. rose->vl = 0;
  106. rose_requeue_frames(sk);
  107. break;
  108. case ROSE_CLEAR_REQUEST:
  109. rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
  110. rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
  111. rose_neigh_put(rose->neighbour);
  112. break;
  113. case ROSE_RR:
  114. case ROSE_RNR:
  115. if (!rose_validate_nr(sk, nr)) {
  116. rose_write_internal(sk, ROSE_RESET_REQUEST);
  117. rose->condition = 0x00;
  118. rose->vs = 0;
  119. rose->vr = 0;
  120. rose->va = 0;
  121. rose->vl = 0;
  122. rose->state = ROSE_STATE_4;
  123. rose_start_t2timer(sk);
  124. rose_stop_idletimer(sk);
  125. } else {
  126. rose_frames_acked(sk, nr);
  127. if (frametype == ROSE_RNR) {
  128. rose->condition |= ROSE_COND_PEER_RX_BUSY;
  129. } else {
  130. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  131. }
  132. }
  133. break;
  134. case ROSE_DATA: /* XXX */
  135. rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
  136. if (!rose_validate_nr(sk, nr)) {
  137. rose_write_internal(sk, ROSE_RESET_REQUEST);
  138. rose->condition = 0x00;
  139. rose->vs = 0;
  140. rose->vr = 0;
  141. rose->va = 0;
  142. rose->vl = 0;
  143. rose->state = ROSE_STATE_4;
  144. rose_start_t2timer(sk);
  145. rose_stop_idletimer(sk);
  146. break;
  147. }
  148. rose_frames_acked(sk, nr);
  149. if (ns == rose->vr) {
  150. rose_start_idletimer(sk);
  151. if (!sk_filter_trim_cap(sk, skb, ROSE_MIN_LEN, &dr) &&
  152. __sock_queue_rcv_skb(sk, skb) == 0) {
  153. rose->vr = (rose->vr + 1) % ROSE_MODULUS;
  154. queued = 1;
  155. } else {
  156. /* Should never happen ! */
  157. rose_write_internal(sk, ROSE_RESET_REQUEST);
  158. rose->condition = 0x00;
  159. rose->vs = 0;
  160. rose->vr = 0;
  161. rose->va = 0;
  162. rose->vl = 0;
  163. rose->state = ROSE_STATE_4;
  164. rose_start_t2timer(sk);
  165. rose_stop_idletimer(sk);
  166. break;
  167. }
  168. if (atomic_read(&sk->sk_rmem_alloc) >
  169. (sk->sk_rcvbuf >> 1))
  170. rose->condition |= ROSE_COND_OWN_RX_BUSY;
  171. }
  172. /*
  173. * If the window is full, ack the frame, else start the
  174. * acknowledge hold back timer.
  175. */
  176. if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
  177. rose->condition &= ~ROSE_COND_ACK_PENDING;
  178. rose_stop_timer(sk);
  179. rose_enquiry_response(sk);
  180. } else {
  181. rose->condition |= ROSE_COND_ACK_PENDING;
  182. rose_start_hbtimer(sk);
  183. }
  184. break;
  185. default:
  186. printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
  187. break;
  188. }
  189. return queued;
  190. }
  191. /*
  192. * State machine for state 4, Awaiting Reset Confirmation State.
  193. * The handling of the timer(s) is in file rose_timer.c
  194. * Handling of state 0 and connection release is in af_rose.c.
  195. */
  196. static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  197. {
  198. struct rose_sock *rose = rose_sk(sk);
  199. switch (frametype) {
  200. case ROSE_RESET_REQUEST:
  201. rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
  202. fallthrough;
  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_neigh_put(rose->neighbour);
  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_neigh_put(rose_sk(sk)->neighbour);
  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. }