nr_subr.c 6.1 KB

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