nr_in.c 7.0 KB

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