tunnel6.c 4.8 KB

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