rose_link.c 6.7 KB

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