nr_in.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <net/ax25.h>
  20. #include <linux/inet.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <net/sock.h>
  24. #include <net/tcp_states.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/system.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. #include <net/netrom.h>
  31. static int nr_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
  32. {
  33. struct sk_buff *skbo, *skbn = skb;
  34. struct nr_sock *nr = nr_sk(sk);
  35. skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  36. nr_start_idletimer(sk);
  37. if (more) {
  38. nr->fraglen += skb->len;
  39. skb_queue_tail(&nr->frag_queue, skb);
  40. return 0;
  41. }
  42. if (!more && nr->fraglen > 0) { /* End of fragment */
  43. nr->fraglen += skb->len;
  44. skb_queue_tail(&nr->frag_queue, skb);
  45. if ((skbn = alloc_skb(nr->fraglen, GFP_ATOMIC)) == NULL)
  46. return 1;
  47. skbn->h.raw = skbn->data;
  48. while ((skbo = skb_dequeue(&nr->frag_queue)) != NULL) {
  49. memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
  50. kfree_skb(skbo);
  51. }
  52. nr->fraglen = 0;
  53. }
  54. return sock_queue_rcv_skb(sk, skbn);
  55. }
  56. /*
  57. * State machine for state 1, Awaiting Connection State.
  58. * The handling of the timer(s) is in file nr_timer.c.
  59. * Handling of state 0 and connection release is in netrom.c.
  60. */
  61. static int nr_state1_machine(struct sock *sk, struct sk_buff *skb,
  62. int frametype)
  63. {
  64. switch (frametype) {
  65. case NR_CONNACK: {
  66. struct nr_sock *nr = nr_sk(sk);
  67. nr_stop_t1timer(sk);
  68. nr_start_idletimer(sk);
  69. nr->your_index = skb->data[17];
  70. nr->your_id = skb->data[18];
  71. nr->vs = 0;
  72. nr->va = 0;
  73. nr->vr = 0;
  74. nr->vl = 0;
  75. nr->state = NR_STATE_3;
  76. nr->n2count = 0;
  77. nr->window = skb->data[20];
  78. sk->sk_state = TCP_ESTABLISHED;
  79. if (!sock_flag(sk, SOCK_DEAD))
  80. sk->sk_state_change(sk);
  81. break;
  82. }
  83. case NR_CONNACK | NR_CHOKE_FLAG:
  84. nr_disconnect(sk, ECONNREFUSED);
  85. break;
  86. case NR_RESET:
  87. if (sysctl_netrom_reset_circuit)
  88. nr_disconnect(sk, ECONNRESET);
  89. break;
  90. default:
  91. break;
  92. }
  93. return 0;
  94. }
  95. /*
  96. * State machine for state 2, Awaiting Release State.
  97. * The handling of the timer(s) is in file nr_timer.c
  98. * Handling of state 0 and connection release is in netrom.c.
  99. */
  100. static int nr_state2_machine(struct sock *sk, struct sk_buff *skb,
  101. int frametype)
  102. {
  103. switch (frametype) {
  104. case NR_CONNACK | NR_CHOKE_FLAG:
  105. nr_disconnect(sk, ECONNRESET);
  106. break;
  107. case NR_DISCREQ:
  108. nr_write_internal(sk, NR_DISCACK);
  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. }