tunnel6.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <net/ipv6.h>
  28. #include <net/protocol.h>
  29. #include <net/xfrm.h>
  30. static struct xfrm6_tunnel *tunnel6_handlers;
  31. static struct xfrm6_tunnel *tunnel46_handlers;
  32. static DEFINE_MUTEX(tunnel6_mutex);
  33. int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
  34. {
  35. struct xfrm6_tunnel **pprev;
  36. int ret = -EEXIST;
  37. int priority = handler->priority;
  38. mutex_lock(&tunnel6_mutex);
  39. for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
  40. *pprev; pprev = &(*pprev)->next) {
  41. if ((*pprev)->priority > priority)
  42. break;
  43. if ((*pprev)->priority == priority)
  44. goto err;
  45. }
  46. handler->next = *pprev;
  47. *pprev = handler;
  48. ret = 0;
  49. err:
  50. mutex_unlock(&tunnel6_mutex);
  51. return ret;
  52. }
  53. EXPORT_SYMBOL(xfrm6_tunnel_register);
  54. int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
  55. {
  56. struct xfrm6_tunnel **pprev;
  57. int ret = -ENOENT;
  58. mutex_lock(&tunnel6_mutex);
  59. for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers;
  60. *pprev; pprev = &(*pprev)->next) {
  61. if (*pprev == handler) {
  62. *pprev = handler->next;
  63. ret = 0;
  64. break;
  65. }
  66. }
  67. mutex_unlock(&tunnel6_mutex);
  68. synchronize_net();
  69. return ret;
  70. }
  71. EXPORT_SYMBOL(xfrm6_tunnel_deregister);
  72. static int tunnel6_rcv(struct sk_buff **pskb)
  73. {
  74. struct sk_buff *skb = *pskb;
  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, skb->dev);
  82. drop:
  83. kfree_skb(skb);
  84. return 0;
  85. }
  86. static int tunnel46_rcv(struct sk_buff **pskb)
  87. {
  88. struct sk_buff *skb = *pskb;
  89. struct xfrm6_tunnel *handler;
  90. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  91. goto drop;
  92. for (handler = tunnel46_handlers; handler; handler = handler->next)
  93. if (!handler->handler(skb))
  94. return 0;
  95. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, skb->dev);
  96. drop:
  97. kfree_skb(skb);
  98. return 0;
  99. }
  100. static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  101. int type, int code, int offset, __be32 info)
  102. {
  103. struct xfrm6_tunnel *handler;
  104. for (handler = tunnel6_handlers; handler; handler = handler->next)
  105. if (!handler->err_handler(skb, opt, type, code, offset, info))
  106. break;
  107. }
  108. static struct inet6_protocol tunnel6_protocol = {
  109. .handler = tunnel6_rcv,
  110. .err_handler = tunnel6_err,
  111. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  112. };
  113. static struct inet6_protocol tunnel46_protocol = {
  114. .handler = tunnel46_rcv,
  115. .err_handler = tunnel6_err,
  116. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  117. };
  118. static int __init tunnel6_init(void)
  119. {
  120. if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
  121. printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
  122. return -EAGAIN;
  123. }
  124. if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
  125. printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
  126. inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
  127. return -EAGAIN;
  128. }
  129. return 0;
  130. }
  131. static void __exit tunnel6_fini(void)
  132. {
  133. if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
  134. printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
  135. if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
  136. printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
  137. }
  138. module_init(tunnel6_init);
  139. module_exit(tunnel6_fini);
  140. MODULE_LICENSE("GPL");