rose_link.c 6.9 KB

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