PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/net/rose/rose_link.c

https://gitlab.com/deadnem/Singularity
C | 291 lines | 202 code | 58 blank | 31 comment | 29 complexity | c76d7a9609ef6204cb63f7208b193d30 MD5 | raw file
  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 <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. ax25_cb *ax25s;
  87. if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
  88. rose_call = (ax25_address *)neigh->dev->dev_addr;
  89. else
  90. rose_call = &rose_callsign;
  91. ax25s = neigh->ax25;
  92. neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
  93. if (ax25s)
  94. ax25_cb_put(ax25s);
  95. return neigh->ax25 != NULL;
  96. }
  97. /*
  98. * Interface to ax25_link_up. Changes my level 2 callsign depending
  99. * on whether we have a global ROSE callsign or use the default port
  100. * callsign.
  101. */
  102. static int rose_link_up(struct rose_neigh *neigh)
  103. {
  104. ax25_address *rose_call;
  105. ax25_cb *ax25s;
  106. if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
  107. rose_call = (ax25_address *)neigh->dev->dev_addr;
  108. else
  109. rose_call = &rose_callsign;
  110. ax25s = neigh->ax25;
  111. neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
  112. if (ax25s)
  113. ax25_cb_put(ax25s);
  114. return neigh->ax25 != NULL;
  115. }
  116. /*
  117. * This handles all restart and diagnostic frames.
  118. */
  119. void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
  120. {
  121. struct sk_buff *skbn;
  122. switch (frametype) {
  123. case ROSE_RESTART_REQUEST:
  124. rose_stop_t0timer(neigh);
  125. neigh->restarted = 1;
  126. neigh->dce_mode = (skb->data[3] == ROSE_DTE_ORIGINATED);
  127. rose_transmit_restart_confirmation(neigh);
  128. break;
  129. case ROSE_RESTART_CONFIRMATION:
  130. rose_stop_t0timer(neigh);
  131. neigh->restarted = 1;
  132. break;
  133. case ROSE_DIAGNOSTIC:
  134. printk(KERN_WARNING "ROSE: received diagnostic #%d - %02X %02X %02X\n", skb->data[3], skb->data[4], skb->data[5], skb->data[6]);
  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. }