ax25_ip.c 5.3 KB

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