tunnel6.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C)2003,2004 USAGI/WIDE Project
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Authors Mitsuru KANDA <mk@linux-ipv6.org>
  19. * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
  20. */
  21. #include <linux/icmpv6.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/slab.h>
  28. #include <net/ipv6.h>
  29. #include <net/protocol.h>
  30. #include <net/xfrm.h>
  31. static struct xfrm6_tunnel *tunnel6_handlers __read_mostly;
  32. static struct xfrm6_tunnel *tunnel46_handlers __read_mostly;
  33. static DEFINE_MUTEX(tunnel6_mutex);
  34. int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
  35. {
  36. struct xfrm6_tunnel **pprev;
  37. int ret = -EEXIST;
  38. int priority = handler->priority;
  39. mutex_lock(&tunnel6_mutex);
  40. for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
  41. *pprev; pprev = &(*pprev)->next) {
  42. if ((*pprev)->priority > priority)
  43. break;
  44. if ((*pprev)->priority == priority)
  45. goto err;
  46. }
  47. handler->next = *pprev;
  48. rcu_assign_pointer(*pprev, handler);
  49. ret = 0;
  50. err:
  51. mutex_unlock(&tunnel6_mutex);
  52. return ret;
  53. }
  54. EXPORT_SYMBOL(xfrm6_tunnel_register);
  55. int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
  56. {
  57. struct xfrm6_tunnel **pprev;
  58. int ret = -ENOENT;
  59. mutex_lock(&tunnel6_mutex);
  60. for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
  61. *pprev; pprev = &(*pprev)->next) {
  62. if (*pprev == handler) {
  63. *pprev = handler->next;
  64. ret = 0;
  65. break;
  66. }
  67. }
  68. mutex_unlock(&tunnel6_mutex);
  69. synchronize_net();
  70. return ret;
  71. }
  72. EXPORT_SYMBOL(xfrm6_tunnel_deregister);
  73. #define for_each_tunnel_rcu(head, handler) \
  74. for (handler = rcu_dereference(head); \
  75. handler != NULL; \
  76. handler = rcu_dereference(handler->next)) \
  77. static int tunnel6_rcv(struct sk_buff *skb)
  78. {
  79. struct xfrm6_tunnel *handler;
  80. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  81. goto drop;
  82. for_each_tunnel_rcu(tunnel6_handlers, handler)
  83. if (!handler->handler(skb))
  84. return 0;
  85. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  86. drop:
  87. kfree_skb(skb);
  88. return 0;
  89. }
  90. static int tunnel46_rcv(struct sk_buff *skb)
  91. {
  92. struct xfrm6_tunnel *handler;
  93. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  94. goto drop;
  95. for_each_tunnel_rcu(tunnel46_handlers, handler)
  96. if (!handler->handler(skb))
  97. return 0;
  98. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  99. drop:
  100. kfree_skb(skb);
  101. return 0;
  102. }
  103. static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  104. u8 type, u8 code, int offset, __be32 info)
  105. {
  106. struct xfrm6_tunnel *handler;
  107. for_each_tunnel_rcu(tunnel6_handlers, handler)
  108. if (!handler->err_handler(skb, opt, type, code, offset, info))
  109. break;
  110. }
  111. static const struct inet6_protocol tunnel6_protocol = {
  112. .handler = tunnel6_rcv,
  113. .err_handler = tunnel6_err,
  114. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  115. };
  116. static const struct inet6_protocol tunnel46_protocol = {
  117. .handler = tunnel46_rcv,
  118. .err_handler = tunnel6_err,
  119. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  120. };
  121. static int __init tunnel6_init(void)
  122. {
  123. if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
  124. printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
  125. return -EAGAIN;
  126. }
  127. if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
  128. printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
  129. inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
  130. return -EAGAIN;
  131. }
  132. return 0;
  133. }
  134. static void __exit tunnel6_fini(void)
  135. {
  136. if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
  137. printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
  138. if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
  139. printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
  140. }
  141. module_init(tunnel6_init);
  142. module_exit(tunnel6_fini);
  143. MODULE_LICENSE("GPL");