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