nr_in.c 6.9 KB

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