rose_link.c 6.7 KB

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