rose_link.c 6.6 KB

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