tunnel6.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #define pr_fmt(fmt) "IPv6: " fmt
  22. #include <linux/icmpv6.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/slab.h>
  29. #include <net/ipv6.h>
  30. #include <net/protocol.h>
  31. #include <net/xfrm.h>
  32. static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
  33. static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
  34. static DEFINE_MUTEX(tunnel6_mutex);
  35. int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
  36. {
  37. struct xfrm6_tunnel __rcu **pprev;
  38. struct xfrm6_tunnel *t;
  39. int ret = -EEXIST;
  40. int priority = handler->priority;
  41. mutex_lock(&tunnel6_mutex);
  42. for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
  43. (t = rcu_dereference_protected(*pprev,
  44. lockdep_is_held(&tunnel6_mutex))) != NULL;
  45. pprev = &t->next) {
  46. if (t->priority > priority)
  47. break;
  48. if (t->priority == priority)
  49. goto err;
  50. }
  51. handler->next = *pprev;
  52. rcu_assign_pointer(*pprev, handler);
  53. ret = 0;
  54. err:
  55. mutex_unlock(&tunnel6_mutex);
  56. return ret;
  57. }
  58. EXPORT_SYMBOL(xfrm6_tunnel_register);
  59. int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
  60. {
  61. struct xfrm6_tunnel __rcu **pprev;
  62. struct xfrm6_tunnel *t;
  63. int ret = -ENOENT;
  64. mutex_lock(&tunnel6_mutex);
  65. for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
  66. (t = rcu_dereference_protected(*pprev,
  67. lockdep_is_held(&tunnel6_mutex))) != NULL;
  68. pprev = &t->next) {
  69. if (t == handler) {
  70. *pprev = handler->next;
  71. ret = 0;
  72. break;
  73. }
  74. }
  75. mutex_unlock(&tunnel6_mutex);
  76. synchronize_net();
  77. return ret;
  78. }
  79. EXPORT_SYMBOL(xfrm6_tunnel_deregister);
  80. #define for_each_tunnel_rcu(head, handler) \
  81. for (handler = rcu_dereference(head); \
  82. handler != NULL; \
  83. handler = rcu_dereference(handler->next)) \
  84. static int tunnel6_rcv(struct sk_buff *skb)
  85. {
  86. struct xfrm6_tunnel *handler;
  87. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  88. goto drop;
  89. for_each_tunnel_rcu(tunnel6_handlers, handler)
  90. if (!handler->handler(skb))
  91. return 0;
  92. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  93. drop:
  94. kfree_skb(skb);
  95. return 0;
  96. }
  97. static int tunnel46_rcv(struct sk_buff *skb)
  98. {
  99. struct xfrm6_tunnel *handler;
  100. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  101. goto drop;
  102. for_each_tunnel_rcu(tunnel46_handlers, handler)
  103. if (!handler->handler(skb))
  104. return 0;
  105. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  106. drop:
  107. kfree_skb(skb);
  108. return 0;
  109. }
  110. static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  111. u8 type, u8 code, int offset, __be32 info)
  112. {
  113. struct xfrm6_tunnel *handler;
  114. for_each_tunnel_rcu(tunnel6_handlers, handler)
  115. if (!handler->err_handler(skb, opt, type, code, offset, info))
  116. break;
  117. }
  118. static void tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  119. u8 type, u8 code, int offset, __be32 info)
  120. {
  121. struct xfrm6_tunnel *handler;
  122. for_each_tunnel_rcu(tunnel46_handlers, handler)
  123. if (!handler->err_handler(skb, opt, type, code, offset, info))
  124. break;
  125. }
  126. static const struct inet6_protocol tunnel6_protocol = {
  127. .handler = tunnel6_rcv,
  128. .err_handler = tunnel6_err,
  129. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  130. };
  131. static const struct inet6_protocol tunnel46_protocol = {
  132. .handler = tunnel46_rcv,
  133. .err_handler = tunnel46_err,
  134. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  135. };
  136. static int __init tunnel6_init(void)
  137. {
  138. if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
  139. pr_err("%s: can't add protocol\n", __func__);
  140. return -EAGAIN;
  141. }
  142. if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
  143. pr_err("%s: can't add protocol\n", __func__);
  144. inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
  145. return -EAGAIN;
  146. }
  147. return 0;
  148. }
  149. static void __exit tunnel6_fini(void)
  150. {
  151. if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
  152. pr_err("%s: can't remove protocol\n", __func__);
  153. if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
  154. pr_err("%s: can't remove protocol\n", __func__);
  155. }
  156. module_init(tunnel6_init);
  157. module_exit(tunnel6_fini);
  158. MODULE_LICENSE("GPL");