ax25_ip.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/module.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/if_arp.h>
  24. #include <linux/skbuff.h>
  25. #include <net/sock.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/system.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/termios.h> /* For TIOCINQ/OUTQ */
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/notifier.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/stat.h>
  35. #include <linux/netfilter.h>
  36. #include <linux/sysctl.h>
  37. #include <net/ip.h>
  38. #include <net/arp.h>
  39. /*
  40. * IP over AX.25 encapsulation.
  41. */
  42. /*
  43. * Shove an AX.25 UI header on an IP packet and handle ARP
  44. */
  45. #ifdef CONFIG_INET
  46. int ax25_hard_header(struct sk_buff *skb, struct net_device *dev,
  47. unsigned short type, const void *daddr,
  48. const void *saddr, unsigned len)
  49. {
  50. unsigned char *buff;
  51. /* they sometimes come back to us... */
  52. if (type == ETH_P_AX25)
  53. return 0;
  54. /* header is an AX.25 UI frame from us to them */
  55. buff = skb_push(skb, AX25_HEADER_LEN);
  56. *buff++ = 0x00; /* KISS DATA */
  57. if (daddr != NULL)
  58. memcpy(buff, daddr, dev->addr_len); /* Address specified */
  59. buff[6] &= ~AX25_CBIT;
  60. buff[6] &= ~AX25_EBIT;
  61. buff[6] |= AX25_SSSID_SPARE;
  62. buff += AX25_ADDR_LEN;
  63. if (saddr != NULL)
  64. memcpy(buff, saddr, dev->addr_len);
  65. else
  66. memcpy(buff, dev->dev_addr, dev->addr_len);
  67. buff[6] &= ~AX25_CBIT;
  68. buff[6] |= AX25_EBIT;
  69. buff[6] |= AX25_SSSID_SPARE;
  70. buff += AX25_ADDR_LEN;
  71. *buff++ = AX25_UI; /* UI */
  72. /* Append a suitable AX.25 PID */
  73. switch (type) {
  74. case ETH_P_IP:
  75. *buff++ = AX25_P_IP;
  76. break;
  77. case ETH_P_ARP:
  78. *buff++ = AX25_P_ARP;
  79. break;
  80. default:
  81. printk(KERN_ERR "AX.25: ax25_hard_header - wrong protocol type 0x%2.2x\n", type);
  82. *buff++ = 0;
  83. break;
  84. }
  85. if (daddr != NULL)
  86. return AX25_HEADER_LEN;
  87. return -AX25_HEADER_LEN; /* Unfinished header */
  88. }
  89. int ax25_rebuild_header(struct sk_buff *skb)
  90. {
  91. struct sk_buff *ourskb;
  92. unsigned char *bp = skb->data;
  93. ax25_route *route;
  94. struct net_device *dev = NULL;
  95. ax25_address *src, *dst;
  96. ax25_digi *digipeat = NULL;
  97. ax25_dev *ax25_dev;
  98. ax25_cb *ax25;
  99. char ip_mode = ' ';
  100. dst = (ax25_address *)(bp + 1);
  101. src = (ax25_address *)(bp + 8);
  102. if (arp_find(bp + 1, skb))
  103. return 1;
  104. route = ax25_get_route(dst, NULL);
  105. if (route) {
  106. digipeat = route->digipeat;
  107. dev = route->dev;
  108. ip_mode = route->ip_mode;
  109. }
  110. if (dev == NULL)
  111. dev = skb->dev;
  112. if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) {
  113. goto put;
  114. }
  115. if (bp[16] == AX25_P_IP) {
  116. if (ip_mode == 'V' || (ip_mode == ' ' && ax25_dev->values[AX25_VALUES_IPDEFMODE])) {
  117. /*
  118. * We copy the buffer and release the original thereby
  119. * keeping it straight
  120. *
  121. * Note: we report 1 back so the caller will
  122. * not feed the frame direct to the physical device
  123. * We don't want that to happen. (It won't be upset
  124. * as we have pulled the frame from the queue by
  125. * freeing it).
  126. *
  127. * NB: TCP modifies buffers that are still
  128. * on a device queue, thus we use skb_copy()
  129. * instead of using skb_clone() unless this
  130. * gets fixed.
  131. */
  132. ax25_address src_c;
  133. ax25_address dst_c;
  134. if ((ourskb = skb_copy(skb, GFP_ATOMIC)) == NULL) {
  135. kfree_skb(skb);
  136. goto put;
  137. }
  138. if (skb->sk != NULL)
  139. skb_set_owner_w(ourskb, skb->sk);
  140. kfree_skb(skb);
  141. /* dl9sau: bugfix
  142. * after kfree_skb(), dst and src which were pointer
  143. * to bp which is part of skb->data would not be valid
  144. * anymore hope that after skb_pull(ourskb, ..) our
  145. * dsc_c and src_c will not become invalid
  146. */
  147. bp = ourskb->data;
  148. dst_c = *(ax25_address *)(bp + 1);
  149. src_c = *(ax25_address *)(bp + 8);
  150. skb_pull(ourskb, AX25_HEADER_LEN - 1); /* Keep PID */
  151. skb_reset_network_header(ourskb);
  152. ax25=ax25_send_frame(
  153. ourskb,
  154. ax25_dev->values[AX25_VALUES_PACLEN],
  155. &src_c,
  156. &dst_c, digipeat, dev);
  157. if (ax25) {
  158. ax25_cb_put(ax25);
  159. }
  160. goto put;
  161. }
  162. }
  163. bp[7] &= ~AX25_CBIT;
  164. bp[7] &= ~AX25_EBIT;
  165. bp[7] |= AX25_SSSID_SPARE;
  166. bp[14] &= ~AX25_CBIT;
  167. bp[14] |= AX25_EBIT;
  168. bp[14] |= AX25_SSSID_SPARE;
  169. skb_pull(skb, AX25_KISS_HEADER_LEN);
  170. if (digipeat != NULL) {
  171. if ((ourskb = ax25_rt_build_path(skb, src, dst, route->digipeat)) == NULL) {
  172. kfree_skb(skb);
  173. goto put;
  174. }
  175. skb = ourskb;
  176. }
  177. ax25_queue_xmit(skb, dev);
  178. put:
  179. if (route)
  180. ax25_put_route(route);
  181. return 1;
  182. }
  183. #else /* INET */
  184. int ax25_hard_header(struct sk_buff *skb, struct net_device *dev,
  185. unsigned short type, const void *daddr,
  186. const void *saddr, unsigned len)
  187. {
  188. return -AX25_HEADER_LEN;
  189. }
  190. int ax25_rebuild_header(struct sk_buff *skb)
  191. {
  192. return 1;
  193. }
  194. #endif
  195. const struct header_ops ax25_header_ops = {
  196. .create = ax25_hard_header,
  197. .rebuild = ax25_rebuild_header,
  198. };
  199. EXPORT_SYMBOL(ax25_hard_header);
  200. EXPORT_SYMBOL(ax25_rebuild_header);
  201. EXPORT_SYMBOL(ax25_header_ops);