PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/net/netrom/nr_in.c

http://github.com/torvalds/linux
C | 302 lines | 242 code | 34 blank | 26 comment | 50 complexity | 236ed8ea0989803ef9bae3642d4c09c3 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  5. * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
  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. /* fall through */
  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. ns = skb->data[17];
  136. switch (frametype) {
  137. case NR_CONNREQ:
  138. nr_write_internal(sk, NR_CONNACK);
  139. break;
  140. case NR_DISCREQ:
  141. nr_write_internal(sk, NR_DISCACK);
  142. nr_disconnect(sk, 0);
  143. break;
  144. case NR_CONNACK | NR_CHOKE_FLAG:
  145. case NR_DISCACK:
  146. nr_disconnect(sk, ECONNRESET);
  147. break;
  148. case NR_INFOACK:
  149. case NR_INFOACK | NR_CHOKE_FLAG:
  150. case NR_INFOACK | NR_NAK_FLAG:
  151. case NR_INFOACK | NR_NAK_FLAG | NR_CHOKE_FLAG:
  152. if (frametype & NR_CHOKE_FLAG) {
  153. nrom->condition |= NR_COND_PEER_RX_BUSY;
  154. nr_start_t4timer(sk);
  155. } else {
  156. nrom->condition &= ~NR_COND_PEER_RX_BUSY;
  157. nr_stop_t4timer(sk);
  158. }
  159. if (!nr_validate_nr(sk, nr)) {
  160. break;
  161. }
  162. if (frametype & NR_NAK_FLAG) {
  163. nr_frames_acked(sk, nr);
  164. nr_send_nak_frame(sk);
  165. } else {
  166. if (nrom->condition & NR_COND_PEER_RX_BUSY) {
  167. nr_frames_acked(sk, nr);
  168. } else {
  169. nr_check_iframes_acked(sk, nr);
  170. }
  171. }
  172. break;
  173. case NR_INFO:
  174. case NR_INFO | NR_NAK_FLAG:
  175. case NR_INFO | NR_CHOKE_FLAG:
  176. case NR_INFO | NR_MORE_FLAG:
  177. case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG:
  178. case NR_INFO | NR_CHOKE_FLAG | NR_MORE_FLAG:
  179. case NR_INFO | NR_NAK_FLAG | NR_MORE_FLAG:
  180. case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG | NR_MORE_FLAG:
  181. if (frametype & NR_CHOKE_FLAG) {
  182. nrom->condition |= NR_COND_PEER_RX_BUSY;
  183. nr_start_t4timer(sk);
  184. } else {
  185. nrom->condition &= ~NR_COND_PEER_RX_BUSY;
  186. nr_stop_t4timer(sk);
  187. }
  188. if (nr_validate_nr(sk, nr)) {
  189. if (frametype & NR_NAK_FLAG) {
  190. nr_frames_acked(sk, nr);
  191. nr_send_nak_frame(sk);
  192. } else {
  193. if (nrom->condition & NR_COND_PEER_RX_BUSY) {
  194. nr_frames_acked(sk, nr);
  195. } else {
  196. nr_check_iframes_acked(sk, nr);
  197. }
  198. }
  199. }
  200. queued = 1;
  201. skb_queue_head(&nrom->reseq_queue, skb);
  202. if (nrom->condition & NR_COND_OWN_RX_BUSY)
  203. break;
  204. skb_queue_head_init(&temp_queue);
  205. do {
  206. save_vr = nrom->vr;
  207. while ((skbn = skb_dequeue(&nrom->reseq_queue)) != NULL) {
  208. ns = skbn->data[17];
  209. if (ns == nrom->vr) {
  210. if (nr_queue_rx_frame(sk, skbn, frametype & NR_MORE_FLAG) == 0) {
  211. nrom->vr = (nrom->vr + 1) % NR_MODULUS;
  212. } else {
  213. nrom->condition |= NR_COND_OWN_RX_BUSY;
  214. skb_queue_tail(&temp_queue, skbn);
  215. }
  216. } else if (nr_in_rx_window(sk, ns)) {
  217. skb_queue_tail(&temp_queue, skbn);
  218. } else {
  219. kfree_skb(skbn);
  220. }
  221. }
  222. while ((skbn = skb_dequeue(&temp_queue)) != NULL) {
  223. skb_queue_tail(&nrom->reseq_queue, skbn);
  224. }
  225. } while (save_vr != nrom->vr);
  226. /*
  227. * Window is full, ack it immediately.
  228. */
  229. if (((nrom->vl + nrom->window) % NR_MODULUS) == nrom->vr) {
  230. nr_enquiry_response(sk);
  231. } else {
  232. if (!(nrom->condition & NR_COND_ACK_PENDING)) {
  233. nrom->condition |= NR_COND_ACK_PENDING;
  234. nr_start_t2timer(sk);
  235. }
  236. }
  237. break;
  238. case NR_RESET:
  239. if (sysctl_netrom_reset_circuit)
  240. nr_disconnect(sk, ECONNRESET);
  241. break;
  242. default:
  243. break;
  244. }
  245. return queued;
  246. }
  247. /* Higher level upcall for a LAPB frame - called with sk locked */
  248. int nr_process_rx_frame(struct sock *sk, struct sk_buff *skb)
  249. {
  250. struct nr_sock *nr = nr_sk(sk);
  251. int queued = 0, frametype;
  252. if (nr->state == NR_STATE_0)
  253. return 0;
  254. frametype = skb->data[19];
  255. switch (nr->state) {
  256. case NR_STATE_1:
  257. queued = nr_state1_machine(sk, skb, frametype);
  258. break;
  259. case NR_STATE_2:
  260. queued = nr_state2_machine(sk, skb, frametype);
  261. break;
  262. case NR_STATE_3:
  263. queued = nr_state3_machine(sk, skb, frametype);
  264. break;
  265. }
  266. nr_kick(sk);
  267. return queued;
  268. }