nr_subr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. */
  9. #include <linux/errno.h>
  10. #include <linux/types.h>
  11. #include <linux/socket.h>
  12. #include <linux/in.h>
  13. #include <linux/kernel.h>
  14. #include <linux/timer.h>
  15. #include <linux/string.h>
  16. #include <linux/sockios.h>
  17. #include <linux/net.h>
  18. #include <linux/slab.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. /*
  32. * This routine purges all of the queues of frames.
  33. */
  34. void nr_clear_queues(struct sock *sk)
  35. {
  36. struct nr_sock *nr = nr_sk(sk);
  37. skb_queue_purge(&sk->sk_write_queue);
  38. skb_queue_purge(&nr->ack_queue);
  39. skb_queue_purge(&nr->reseq_queue);
  40. skb_queue_purge(&nr->frag_queue);
  41. }
  42. /*
  43. * This routine purges the input queue of those frames that have been
  44. * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
  45. * SDL diagram.
  46. */
  47. void nr_frames_acked(struct sock *sk, unsigned short nr)
  48. {
  49. struct nr_sock *nrom = nr_sk(sk);
  50. struct sk_buff *skb;
  51. /*
  52. * Remove all the ack-ed frames from the ack queue.
  53. */
  54. if (nrom->va != nr) {
  55. while (skb_peek(&nrom->ack_queue) != NULL && nrom->va != nr) {
  56. skb = skb_dequeue(&nrom->ack_queue);
  57. kfree_skb(skb);
  58. nrom->va = (nrom->va + 1) % NR_MODULUS;
  59. }
  60. }
  61. }
  62. /*
  63. * Requeue all the un-ack-ed frames on the output queue to be picked
  64. * up by nr_kick called from the timer. This arrangement handles the
  65. * possibility of an empty output queue.
  66. */
  67. void nr_requeue_frames(struct sock *sk)
  68. {
  69. struct sk_buff *skb, *skb_prev = NULL;
  70. while ((skb = skb_dequeue(&nr_sk(sk)->ack_queue)) != NULL) {
  71. if (skb_prev == NULL)
  72. skb_queue_head(&sk->sk_write_queue, skb);
  73. else
  74. skb_append(skb_prev, skb, &sk->sk_write_queue);
  75. skb_prev = skb;
  76. }
  77. }
  78. /*
  79. * Validate that the value of nr is between va and vs. Return true or
  80. * false for testing.
  81. */
  82. int nr_validate_nr(struct sock *sk, unsigned short nr)
  83. {
  84. struct nr_sock *nrom = nr_sk(sk);
  85. unsigned short vc = nrom->va;
  86. while (vc != nrom->vs) {
  87. if (nr == vc) return 1;
  88. vc = (vc + 1) % NR_MODULUS;
  89. }
  90. return nr == nrom->vs;
  91. }
  92. /*
  93. * Check that ns is within the receive window.
  94. */
  95. int nr_in_rx_window(struct sock *sk, unsigned short ns)
  96. {
  97. struct nr_sock *nr = nr_sk(sk);
  98. unsigned short vc = nr->vr;
  99. unsigned short vt = (nr->vl + nr->window) % NR_MODULUS;
  100. while (vc != vt) {
  101. if (ns == vc) return 1;
  102. vc = (vc + 1) % NR_MODULUS;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * This routine is called when the HDLC layer internally generates a
  108. * control frame.
  109. */
  110. void nr_write_internal(struct sock *sk, int frametype)
  111. {
  112. struct nr_sock *nr = nr_sk(sk);
  113. struct sk_buff *skb;
  114. unsigned char *dptr;
  115. int len, timeout;
  116. len = NR_NETWORK_LEN + NR_TRANSPORT_LEN;
  117. switch (frametype & 0x0F) {
  118. case NR_CONNREQ:
  119. len += 17;
  120. break;
  121. case NR_CONNACK:
  122. len += (nr->bpqext) ? 2 : 1;
  123. break;
  124. case NR_DISCREQ:
  125. case NR_DISCACK:
  126. case NR_INFOACK:
  127. break;
  128. default:
  129. printk(KERN_ERR "NET/ROM: nr_write_internal - invalid frame type %d\n", frametype);
  130. return;
  131. }
  132. if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
  133. return;
  134. /*
  135. * Space for AX.25 and NET/ROM network header
  136. */
  137. skb_reserve(skb, NR_NETWORK_LEN);
  138. dptr = skb_put(skb, skb_tailroom(skb));
  139. switch (frametype & 0x0F) {
  140. case NR_CONNREQ:
  141. timeout = nr->t1 / HZ;
  142. *dptr++ = nr->my_index;
  143. *dptr++ = nr->my_id;
  144. *dptr++ = 0;
  145. *dptr++ = 0;
  146. *dptr++ = frametype;
  147. *dptr++ = nr->window;
  148. memcpy(dptr, &nr->user_addr, AX25_ADDR_LEN);
  149. dptr[6] &= ~AX25_CBIT;
  150. dptr[6] &= ~AX25_EBIT;
  151. dptr[6] |= AX25_SSSID_SPARE;
  152. dptr += AX25_ADDR_LEN;
  153. memcpy(dptr, &nr->source_addr, AX25_ADDR_LEN);
  154. dptr[6] &= ~AX25_CBIT;
  155. dptr[6] &= ~AX25_EBIT;
  156. dptr[6] |= AX25_SSSID_SPARE;
  157. dptr += AX25_ADDR_LEN;
  158. *dptr++ = timeout % 256;
  159. *dptr++ = timeout / 256;
  160. break;
  161. case NR_CONNACK:
  162. *dptr++ = nr->your_index;
  163. *dptr++ = nr->your_id;
  164. *dptr++ = nr->my_index;
  165. *dptr++ = nr->my_id;
  166. *dptr++ = frametype;
  167. *dptr++ = nr->window;
  168. if (nr->bpqext) *dptr++ = sysctl_netrom_network_ttl_initialiser;
  169. break;
  170. case NR_DISCREQ:
  171. case NR_DISCACK:
  172. *dptr++ = nr->your_index;
  173. *dptr++ = nr->your_id;
  174. *dptr++ = 0;
  175. *dptr++ = 0;
  176. *dptr++ = frametype;
  177. break;
  178. case NR_INFOACK:
  179. *dptr++ = nr->your_index;
  180. *dptr++ = nr->your_id;
  181. *dptr++ = 0;
  182. *dptr++ = nr->vr;
  183. *dptr++ = frametype;
  184. break;
  185. }
  186. nr_transmit_buffer(sk, skb);
  187. }
  188. /*
  189. * This routine is called to send an error reply.
  190. */
  191. void __nr_transmit_reply(struct sk_buff *skb, int mine, unsigned char cmdflags)
  192. {
  193. struct sk_buff *skbn;
  194. unsigned char *dptr;
  195. int len;
  196. len = NR_NETWORK_LEN + NR_TRANSPORT_LEN + 1;
  197. if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL)
  198. return;
  199. skb_reserve(skbn, 0);
  200. dptr = skb_put(skbn, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  201. skb_copy_from_linear_data_offset(skb, 7, dptr, AX25_ADDR_LEN);
  202. dptr[6] &= ~AX25_CBIT;
  203. dptr[6] &= ~AX25_EBIT;
  204. dptr[6] |= AX25_SSSID_SPARE;
  205. dptr += AX25_ADDR_LEN;
  206. skb_copy_from_linear_data(skb, dptr, AX25_ADDR_LEN);
  207. dptr[6] &= ~AX25_CBIT;
  208. dptr[6] |= AX25_EBIT;
  209. dptr[6] |= AX25_SSSID_SPARE;
  210. dptr += AX25_ADDR_LEN;
  211. *dptr++ = sysctl_netrom_network_ttl_initialiser;
  212. if (mine) {
  213. *dptr++ = 0;
  214. *dptr++ = 0;
  215. *dptr++ = skb->data[15];
  216. *dptr++ = skb->data[16];
  217. } else {
  218. *dptr++ = skb->data[15];
  219. *dptr++ = skb->data[16];
  220. *dptr++ = 0;
  221. *dptr++ = 0;
  222. }
  223. *dptr++ = cmdflags;
  224. *dptr++ = 0;
  225. if (!nr_route_frame(skbn, NULL))
  226. kfree_skb(skbn);
  227. }
  228. void nr_disconnect(struct sock *sk, int reason)
  229. {
  230. nr_stop_t1timer(sk);
  231. nr_stop_t2timer(sk);
  232. nr_stop_t4timer(sk);
  233. nr_stop_idletimer(sk);
  234. nr_clear_queues(sk);
  235. nr_sk(sk)->state = NR_STATE_0;
  236. sk->sk_state = TCP_CLOSE;
  237. sk->sk_err = reason;
  238. sk->sk_shutdown |= SEND_SHUTDOWN;
  239. if (!sock_flag(sk, SOCK_DEAD)) {
  240. sk->sk_state_change(sk);
  241. sock_set_flag(sk, SOCK_DEAD);
  242. }
  243. }