nr_subr.c 5.8 KB

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