ax25_ip.c 5.1 KB

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