ax25_ip.c 5.3 KB

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