ax25_ip.c 5.1 KB

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