tunnel6.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  32. static struct xfrm6_tunnel *tunnel46_handlers;
  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. *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. static int tunnel6_rcv(struct sk_buff *skb)
  74. {
  75. struct xfrm6_tunnel *handler;
  76. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  77. goto drop;
  78. for (handler = tunnel6_handlers; handler; handler = handler->next)
  79. if (!handler->handler(skb))
  80. return 0;
  81. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  82. drop:
  83. kfree_skb(skb);
  84. return 0;
  85. }
  86. static int tunnel46_rcv(struct sk_buff *skb)
  87. {
  88. struct xfrm6_tunnel *handler;
  89. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  90. goto drop;
  91. for (handler = tunnel46_handlers; handler; handler = handler->next)
  92. if (!handler->handler(skb))
  93. return 0;
  94. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  95. drop:
  96. kfree_skb(skb);
  97. return 0;
  98. }
  99. static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  100. u8 type, u8 code, int offset, __be32 info)
  101. {
  102. struct xfrm6_tunnel *handler;
  103. for (handler = tunnel6_handlers; handler; handler = handler->next)
  104. if (!handler->err_handler(skb, opt, type, code, offset, info))
  105. break;
  106. }
  107. static const struct inet6_protocol tunnel6_protocol = {
  108. .handler = tunnel6_rcv,
  109. .err_handler = tunnel6_err,
  110. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  111. };
  112. static const struct inet6_protocol tunnel46_protocol = {
  113. .handler = tunnel46_rcv,
  114. .err_handler = tunnel6_err,
  115. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  116. };
  117. static int __init tunnel6_init(void)
  118. {
  119. if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
  120. printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
  121. return -EAGAIN;
  122. }
  123. if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
  124. printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
  125. inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
  126. return -EAGAIN;
  127. }
  128. return 0;
  129. }
  130. static void __exit tunnel6_fini(void)
  131. {
  132. if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
  133. printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
  134. if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
  135. printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
  136. }
  137. module_init(tunnel6_init);
  138. module_exit(tunnel6_fini);
  139. MODULE_LICENSE("GPL");