rose_link.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) 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/jiffies.h>
  12. #include <linux/timer.h>
  13. #include <linux/string.h>
  14. #include <linux/sockios.h>
  15. #include <linux/net.h>
  16. #include <linux/slab.h>
  17. #include <net/ax25.h>
  18. #include <linux/inet.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <net/sock.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <net/rose.h>
  26. static void rose_ftimer_expiry(struct timer_list *);
  27. static void rose_t0timer_expiry(struct timer_list *);
  28. static void rose_transmit_restart_confirmation(struct rose_neigh *neigh);
  29. static void rose_transmit_restart_request(struct rose_neigh *neigh);
  30. void rose_start_ftimer(struct rose_neigh *neigh)
  31. {
  32. del_timer(&neigh->ftimer);
  33. neigh->ftimer.function = rose_ftimer_expiry;
  34. neigh->ftimer.expires =
  35. jiffies + msecs_to_jiffies(sysctl_rose_link_fail_timeout);
  36. add_timer(&neigh->ftimer);
  37. }
  38. static void rose_start_t0timer(struct rose_neigh *neigh)
  39. {
  40. del_timer(&neigh->t0timer);
  41. neigh->t0timer.function = rose_t0timer_expiry;
  42. neigh->t0timer.expires =
  43. jiffies + msecs_to_jiffies(sysctl_rose_restart_request_timeout);
  44. add_timer(&neigh->t0timer);
  45. }
  46. void rose_stop_ftimer(struct rose_neigh *neigh)
  47. {
  48. del_timer(&neigh->ftimer);
  49. }
  50. void rose_stop_t0timer(struct rose_neigh *neigh)
  51. {
  52. del_timer(&neigh->t0timer);
  53. }
  54. int rose_ftimer_running(struct rose_neigh *neigh)
  55. {
  56. return timer_pending(&neigh->ftimer);
  57. }
  58. static int rose_t0timer_running(struct rose_neigh *neigh)
  59. {
  60. return timer_pending(&neigh->t0timer);
  61. }
  62. static void rose_ftimer_expiry(struct timer_list *t)
  63. {
  64. }
  65. static void rose_t0timer_expiry(struct timer_list *t)
  66. {
  67. struct rose_neigh *neigh = from_timer(neigh, t, t0timer);
  68. rose_transmit_restart_request(neigh);
  69. neigh->dce_mode = 0;
  70. rose_start_t0timer(neigh);
  71. }
  72. /*
  73. * Interface to ax25_send_frame. Changes my level 2 callsign depending
  74. * on whether we have a global ROSE callsign or use the default port
  75. * callsign.
  76. */
  77. static int rose_send_frame(struct sk_buff *skb, struct rose_neigh *neigh)
  78. {
  79. const ax25_address *rose_call;
  80. ax25_cb *ax25s;
  81. if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
  82. rose_call = (const ax25_address *)neigh->dev->dev_addr;
  83. else
  84. rose_call = &rose_callsign;
  85. ax25s = neigh->ax25;
  86. neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
  87. if (ax25s)
  88. ax25_cb_put(ax25s);
  89. return neigh->ax25 != NULL;
  90. }
  91. /*
  92. * Interface to ax25_link_up. Changes my level 2 callsign depending
  93. * on whether we have a global ROSE callsign or use the default port
  94. * callsign.
  95. */
  96. static int rose_link_up(struct rose_neigh *neigh)
  97. {
  98. const ax25_address *rose_call;
  99. ax25_cb *ax25s;
  100. if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
  101. rose_call = (const ax25_address *)neigh->dev->dev_addr;
  102. else
  103. rose_call = &rose_callsign;
  104. ax25s = neigh->ax25;
  105. neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
  106. if (ax25s)
  107. ax25_cb_put(ax25s);
  108. return neigh->ax25 != NULL;
  109. }
  110. /*
  111. * This handles all restart and diagnostic frames.
  112. */
  113. void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
  114. {
  115. struct sk_buff *skbn;
  116. switch (frametype) {
  117. case ROSE_RESTART_REQUEST:
  118. rose_stop_t0timer(neigh);
  119. neigh->restarted = 1;
  120. neigh->dce_mode = (skb->data[3] == ROSE_DTE_ORIGINATED);
  121. rose_transmit_restart_confirmation(neigh);
  122. break;
  123. case ROSE_RESTART_CONFIRMATION:
  124. rose_stop_t0timer(neigh);
  125. neigh->restarted = 1;
  126. break;
  127. case ROSE_DIAGNOSTIC:
  128. pr_warn("ROSE: received diagnostic #%d - %3ph\n", skb->data[3],
  129. skb->data + 4);
  130. break;
  131. default:
  132. printk(KERN_WARNING "ROSE: received unknown %02X with LCI 000\n", frametype);
  133. break;
  134. }
  135. if (neigh->restarted) {
  136. while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
  137. if (!rose_send_frame(skbn, neigh))
  138. kfree_skb(skbn);
  139. }
  140. }
  141. /*
  142. * This routine is called when a Restart Request is needed
  143. */
  144. static void rose_transmit_restart_request(struct rose_neigh *neigh)
  145. {
  146. struct sk_buff *skb;
  147. unsigned char *dptr;
  148. int len;
  149. len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
  150. if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
  151. return;
  152. skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
  153. dptr = skb_put(skb, ROSE_MIN_LEN + 3);
  154. *dptr++ = AX25_P_ROSE;
  155. *dptr++ = ROSE_GFI;
  156. *dptr++ = 0x00;
  157. *dptr++ = ROSE_RESTART_REQUEST;
  158. *dptr++ = ROSE_DTE_ORIGINATED;
  159. *dptr++ = 0;
  160. if (!rose_send_frame(skb, neigh))
  161. kfree_skb(skb);
  162. }
  163. /*
  164. * This routine is called when a Restart Confirmation is needed
  165. */
  166. static void rose_transmit_restart_confirmation(struct rose_neigh *neigh)
  167. {
  168. struct sk_buff *skb;
  169. unsigned char *dptr;
  170. int len;
  171. len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
  172. if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
  173. return;
  174. skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
  175. dptr = skb_put(skb, ROSE_MIN_LEN + 1);
  176. *dptr++ = AX25_P_ROSE;
  177. *dptr++ = ROSE_GFI;
  178. *dptr++ = 0x00;
  179. *dptr++ = ROSE_RESTART_CONFIRMATION;
  180. if (!rose_send_frame(skb, neigh))
  181. kfree_skb(skb);
  182. }
  183. /*
  184. * This routine is called when a Clear Request is needed outside of the context
  185. * of a connected socket.
  186. */
  187. void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause, unsigned char diagnostic)
  188. {
  189. struct sk_buff *skb;
  190. unsigned char *dptr;
  191. int len;
  192. if (!neigh->dev)
  193. return;
  194. len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
  195. if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
  196. return;
  197. skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
  198. dptr = skb_put(skb, ROSE_MIN_LEN + 3);
  199. *dptr++ = AX25_P_ROSE;
  200. *dptr++ = ((lci >> 8) & 0x0F) | ROSE_GFI;
  201. *dptr++ = ((lci >> 0) & 0xFF);
  202. *dptr++ = ROSE_CLEAR_REQUEST;
  203. *dptr++ = cause;
  204. *dptr++ = diagnostic;
  205. if (!rose_send_frame(skb, neigh))
  206. kfree_skb(skb);
  207. }
  208. void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh)
  209. {
  210. unsigned char *dptr;
  211. if (neigh->loopback) {
  212. rose_loopback_queue(skb, neigh);
  213. return;
  214. }
  215. if (!rose_link_up(neigh))
  216. neigh->restarted = 0;
  217. dptr = skb_push(skb, 1);
  218. *dptr++ = AX25_P_ROSE;
  219. if (neigh->restarted) {
  220. if (!rose_send_frame(skb, neigh))
  221. kfree_skb(skb);
  222. } else {
  223. skb_queue_tail(&neigh->queue, skb);
  224. if (!rose_t0timer_running(neigh)) {
  225. rose_transmit_restart_request(neigh);
  226. neigh->dce_mode = 0;
  227. rose_start_t0timer(neigh);
  228. }
  229. }
  230. }