nr_in.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright Jonathan Naylor G4KLX ([email protected])
  5. * Copyright Darryl Miles G7LED ([email protected])
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/types.h>
  9. #include <linux/socket.h>
  10. #include <linux/in.h>
  11. #include <linux/kernel.h>
  12. #include <linux/timer.h>
  13. #include <linux/string.h>
  14. #include <linux/sockios.h>
  15. #include <linux/net.h>
  16. #include <linux/slab.h>
  17. #include <net/ax25.h>
  18. #include <linux/inet.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <net/sock.h>
  22. #include <net/tcp_states.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <net/netrom.h>
  28. static int nr_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
  29. {
  30. struct sk_buff *skbo, *skbn = skb;
  31. struct nr_sock *nr = nr_sk(sk);
  32. skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  33. nr_start_idletimer(sk);
  34. if (more) {
  35. nr->fraglen += skb->len;
  36. skb_queue_tail(&nr->frag_queue, skb);
  37. return 0;
  38. }
  39. if (!more && nr->fraglen > 0) { /* End of fragment */
  40. nr->fraglen += skb->len;
  41. skb_queue_tail(&nr->frag_queue, skb);
  42. if ((skbn = alloc_skb(nr->fraglen, GFP_ATOMIC)) == NULL)
  43. return 1;
  44. skb_reset_transport_header(skbn);
  45. while ((skbo = skb_dequeue(&nr->frag_queue)) != NULL) {
  46. skb_copy_from_linear_data(skbo,
  47. skb_put(skbn, skbo->len),
  48. skbo->len);
  49. kfree_skb(skbo);
  50. }
  51. nr->fraglen = 0;
  52. }
  53. return sock_queue_rcv_skb(sk, skbn);
  54. }
  55. /*
  56. * State machine for state 1, Awaiting Connection State.
  57. * The handling of the timer(s) is in file nr_timer.c.
  58. * Handling of state 0 and connection release is in netrom.c.
  59. */
  60. static int nr_state1_machine(struct sock *sk, struct sk_buff *skb,
  61. int frametype)
  62. {
  63. switch (frametype) {
  64. case NR_CONNACK: {
  65. struct nr_sock *nr = nr_sk(sk);
  66. nr_stop_t1timer(sk);
  67. nr_start_idletimer(sk);
  68. nr->your_index = skb->data[17];
  69. nr->your_id = skb->data[18];
  70. nr->vs = 0;
  71. nr->va = 0;
  72. nr->vr = 0;
  73. nr->vl = 0;
  74. nr->state = NR_STATE_3;
  75. nr->n2count = 0;
  76. nr->window = skb->data[20];
  77. sk->sk_state = TCP_ESTABLISHED;
  78. if (!sock_flag(sk, SOCK_DEAD))
  79. sk->sk_state_change(sk);
  80. break;
  81. }
  82. case NR_CONNACK | NR_CHOKE_FLAG:
  83. nr_disconnect(sk, ECONNREFUSED);
  84. break;
  85. case NR_RESET:
  86. if (sysctl_netrom_reset_circuit)
  87. nr_disconnect(sk, ECONNRESET);
  88. break;
  89. default:
  90. break;
  91. }
  92. return 0;
  93. }
  94. /*
  95. * State machine for state 2, Awaiting Release State.
  96. * The handling of the timer(s) is in file nr_timer.c
  97. * Handling of state 0 and connection release is in netrom.c.
  98. */
  99. static int nr_state2_machine(struct sock *sk, struct sk_buff *skb,
  100. int frametype)
  101. {
  102. switch (frametype) {
  103. case NR_CONNACK | NR_CHOKE_FLAG:
  104. nr_disconnect(sk, ECONNRESET);
  105. break;
  106. case NR_DISCREQ:
  107. nr_write_internal(sk, NR_DISCACK);
  108. fallthrough;
  109. case NR_DISCACK:
  110. nr_disconnect(sk, 0);
  111. break;
  112. case NR_RESET:
  113. if (sysctl_netrom_reset_circuit)
  114. nr_disconnect(sk, ECONNRESET);
  115. break;
  116. default:
  117. break;
  118. }
  119. return 0;
  120. }
  121. /*
  122. * State machine for state 3, Connected State.
  123. * The handling of the timer(s) is in file nr_timer.c
  124. * Handling of state 0 and connection release is in netrom.c.
  125. */
  126. static int nr_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype)
  127. {
  128. struct nr_sock *nrom = nr_sk(sk);
  129. struct sk_buff_head temp_queue;
  130. struct sk_buff *skbn;
  131. unsigned short save_vr;
  132. unsigned short nr, ns;
  133. int queued = 0;
  134. nr = skb->data[18];
  135. switch (frametype) {
  136. case NR_CONNREQ:
  137. nr_write_internal(sk, NR_CONNACK);
  138. break;
  139. case NR_DISCREQ:
  140. nr_write_internal(sk, NR_DISCACK);
  141. nr_disconnect(sk, 0);
  142. break;
  143. case NR_CONNACK | NR_CHOKE_FLAG:
  144. case NR_DISCACK:
  145. nr_disconnect(sk, ECONNRESET);
  146. break;
  147. case NR_INFOACK:
  148. case NR_INFOACK | NR_CHOKE_FLAG:
  149. case NR_INFOACK | NR_NAK_FLAG:
  150. case NR_INFOACK | NR_NAK_FLAG | NR_CHOKE_FLAG:
  151. if (frametype & NR_CHOKE_FLAG) {
  152. nrom->condition |= NR_COND_PEER_RX_BUSY;
  153. nr_start_t4timer(sk);
  154. } else {
  155. nrom->condition &= ~NR_COND_PEER_RX_BUSY;
  156. nr_stop_t4timer(sk);
  157. }
  158. if (!nr_validate_nr(sk, nr)) {
  159. break;
  160. }
  161. if (frametype & NR_NAK_FLAG) {
  162. nr_frames_acked(sk, nr);
  163. nr_send_nak_frame(sk);
  164. } else {
  165. if (nrom->condition & NR_COND_PEER_RX_BUSY) {
  166. nr_frames_acked(sk, nr);
  167. } else {
  168. nr_check_iframes_acked(sk, nr);
  169. }
  170. }
  171. break;
  172. case NR_INFO:
  173. case NR_INFO | NR_NAK_FLAG:
  174. case NR_INFO | NR_CHOKE_FLAG:
  175. case NR_INFO | NR_MORE_FLAG:
  176. case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG:
  177. case NR_INFO | NR_CHOKE_FLAG | NR_MORE_FLAG:
  178. case NR_INFO | NR_NAK_FLAG | NR_MORE_FLAG:
  179. case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG | NR_MORE_FLAG:
  180. if (frametype & NR_CHOKE_FLAG) {
  181. nrom->condition |= NR_COND_PEER_RX_BUSY;
  182. nr_start_t4timer(sk);
  183. } else {
  184. nrom->condition &= ~NR_COND_PEER_RX_BUSY;
  185. nr_stop_t4timer(sk);
  186. }
  187. if (nr_validate_nr(sk, nr)) {
  188. if (frametype & NR_NAK_FLAG) {
  189. nr_frames_acked(sk, nr);
  190. nr_send_nak_frame(sk);
  191. } else {
  192. if (nrom->condition & NR_COND_PEER_RX_BUSY) {
  193. nr_frames_acked(sk, nr);
  194. } else {
  195. nr_check_iframes_acked(sk, nr);
  196. }
  197. }
  198. }
  199. queued = 1;
  200. skb_queue_head(&nrom->reseq_queue, skb);
  201. if (nrom->condition & NR_COND_OWN_RX_BUSY)
  202. break;
  203. skb_queue_head_init(&temp_queue);
  204. do {
  205. save_vr = nrom->vr;
  206. while ((skbn = skb_dequeue(&nrom->reseq_queue)) != NULL) {
  207. ns = skbn->data[17];
  208. if (ns == nrom->vr) {
  209. if (nr_queue_rx_frame(sk, skbn, frametype & NR_MORE_FLAG) == 0) {
  210. nrom->vr = (nrom->vr + 1) % NR_MODULUS;
  211. } else {
  212. nrom->condition |= NR_COND_OWN_RX_BUSY;
  213. skb_queue_tail(&temp_queue, skbn);
  214. }
  215. } else if (nr_in_rx_window(sk, ns)) {
  216. skb_queue_tail(&temp_queue, skbn);
  217. } else {
  218. kfree_skb(skbn);
  219. }
  220. }
  221. while ((skbn = skb_dequeue(&temp_queue)) != NULL) {
  222. skb_queue_tail(&nrom->reseq_queue, skbn);
  223. }
  224. } while (save_vr != nrom->vr);
  225. /*
  226. * Window is full, ack it immediately.
  227. */
  228. if (((nrom->vl + nrom->window) % NR_MODULUS) == nrom->vr) {
  229. nr_enquiry_response(sk);
  230. } else {
  231. if (!(nrom->condition & NR_COND_ACK_PENDING)) {
  232. nrom->condition |= NR_COND_ACK_PENDING;
  233. nr_start_t2timer(sk);
  234. }
  235. }
  236. break;
  237. case NR_RESET:
  238. if (sysctl_netrom_reset_circuit)
  239. nr_disconnect(sk, ECONNRESET);
  240. break;
  241. default:
  242. break;
  243. }
  244. return queued;
  245. }
  246. /* Higher level upcall for a LAPB frame - called with sk locked */
  247. int nr_process_rx_frame(struct sock *sk, struct sk_buff *skb)
  248. {
  249. struct nr_sock *nr = nr_sk(sk);
  250. int queued = 0, frametype;
  251. if (nr->state == NR_STATE_0)
  252. return 0;
  253. frametype = skb->data[19];
  254. switch (nr->state) {
  255. case NR_STATE_1:
  256. queued = nr_state1_machine(sk, skb, frametype);
  257. break;
  258. case NR_STATE_2:
  259. queued = nr_state2_machine(sk, skb, frametype);
  260. break;
  261. case NR_STATE_3:
  262. queued = nr_state3_machine(sk, skb, frametype);
  263. break;
  264. }
  265. nr_kick(sk);
  266. return queued;
  267. }