tunnel6.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <net/protocol.h>
  27. #include <net/xfrm.h>
  28. static struct xfrm6_tunnel *tunnel6_handlers;
  29. static DEFINE_MUTEX(tunnel6_mutex);
  30. int xfrm6_tunnel_register(struct xfrm6_tunnel *handler)
  31. {
  32. struct xfrm6_tunnel **pprev;
  33. int ret = -EEXIST;
  34. int priority = handler->priority;
  35. mutex_lock(&tunnel6_mutex);
  36. for (pprev = &tunnel6_handlers; *pprev; pprev = &(*pprev)->next) {
  37. if ((*pprev)->priority > priority)
  38. break;
  39. if ((*pprev)->priority == priority)
  40. goto err;
  41. }
  42. handler->next = *pprev;
  43. *pprev = handler;
  44. ret = 0;
  45. err:
  46. mutex_unlock(&tunnel6_mutex);
  47. return ret;
  48. }
  49. EXPORT_SYMBOL(xfrm6_tunnel_register);
  50. int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler)
  51. {
  52. struct xfrm6_tunnel **pprev;
  53. int ret = -ENOENT;
  54. mutex_lock(&tunnel6_mutex);
  55. for (pprev = &tunnel6_handlers; *pprev; pprev = &(*pprev)->next) {
  56. if (*pprev == handler) {
  57. *pprev = handler->next;
  58. ret = 0;
  59. break;
  60. }
  61. }
  62. mutex_unlock(&tunnel6_mutex);
  63. synchronize_net();
  64. return ret;
  65. }
  66. EXPORT_SYMBOL(xfrm6_tunnel_deregister);
  67. static int tunnel6_rcv(struct sk_buff **pskb)
  68. {
  69. struct sk_buff *skb = *pskb;
  70. struct xfrm6_tunnel *handler;
  71. for (handler = tunnel6_handlers; handler; handler = handler->next)
  72. if (!handler->handler(skb))
  73. return 0;
  74. kfree_skb(skb);
  75. return 0;
  76. }
  77. static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  78. int type, int code, int offset, __u32 info)
  79. {
  80. struct xfrm6_tunnel *handler;
  81. for (handler = tunnel6_handlers; handler; handler = handler->next)
  82. if (!handler->err_handler(skb, opt, type, code, offset, info))
  83. break;
  84. }
  85. static struct inet6_protocol tunnel6_protocol = {
  86. .handler = tunnel6_rcv,
  87. .err_handler = tunnel6_err,
  88. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  89. };
  90. static int __init tunnel6_init(void)
  91. {
  92. if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
  93. printk(KERN_ERR "tunnel6 init(): can't add protocol\n");
  94. return -EAGAIN;
  95. }
  96. return 0;
  97. }
  98. static void __exit tunnel6_fini(void)
  99. {
  100. if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
  101. printk(KERN_ERR "tunnel6 close: can't remove protocol\n");
  102. }
  103. module_init(tunnel6_init);
  104. module_exit(tunnel6_fini);
  105. MODULE_LICENSE("GPL");