tunnel4.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* tunnel4.c: Generic IP tunnel transformer.
  2. *
  3. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <net/icmp.h>
  11. #include <net/ip.h>
  12. #include <net/protocol.h>
  13. #include <net/xfrm.h>
  14. static struct xfrm_tunnel *tunnel4_handlers;
  15. static struct xfrm_tunnel *tunnel64_handlers;
  16. static DEFINE_MUTEX(tunnel4_mutex);
  17. int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
  18. {
  19. struct xfrm_tunnel **pprev;
  20. int ret = -EEXIST;
  21. int priority = handler->priority;
  22. mutex_lock(&tunnel4_mutex);
  23. for (pprev = (family == AF_INET) ? &tunnel4_handlers : &tunnel64_handlers;
  24. *pprev; pprev = &(*pprev)->next) {
  25. if ((*pprev)->priority > priority)
  26. break;
  27. if ((*pprev)->priority == priority)
  28. goto err;
  29. }
  30. handler->next = *pprev;
  31. *pprev = handler;
  32. ret = 0;
  33. err:
  34. mutex_unlock(&tunnel4_mutex);
  35. return ret;
  36. }
  37. EXPORT_SYMBOL(xfrm4_tunnel_register);
  38. int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
  39. {
  40. struct xfrm_tunnel **pprev;
  41. int ret = -ENOENT;
  42. mutex_lock(&tunnel4_mutex);
  43. for (pprev = (family == AF_INET) ? &tunnel4_handlers : &tunnel64_handlers;
  44. *pprev; pprev = &(*pprev)->next) {
  45. if (*pprev == handler) {
  46. *pprev = handler->next;
  47. ret = 0;
  48. break;
  49. }
  50. }
  51. mutex_unlock(&tunnel4_mutex);
  52. synchronize_net();
  53. return ret;
  54. }
  55. EXPORT_SYMBOL(xfrm4_tunnel_deregister);
  56. static int tunnel4_rcv(struct sk_buff *skb)
  57. {
  58. struct xfrm_tunnel *handler;
  59. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  60. goto drop;
  61. for (handler = tunnel4_handlers; handler; handler = handler->next)
  62. if (!handler->handler(skb))
  63. return 0;
  64. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  65. drop:
  66. kfree_skb(skb);
  67. return 0;
  68. }
  69. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  70. static int tunnel64_rcv(struct sk_buff *skb)
  71. {
  72. struct xfrm_tunnel *handler;
  73. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  74. goto drop;
  75. for (handler = tunnel64_handlers; handler; handler = handler->next)
  76. if (!handler->handler(skb))
  77. return 0;
  78. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  79. drop:
  80. kfree_skb(skb);
  81. return 0;
  82. }
  83. #endif
  84. static void tunnel4_err(struct sk_buff *skb, u32 info)
  85. {
  86. struct xfrm_tunnel *handler;
  87. for (handler = tunnel4_handlers; handler; handler = handler->next)
  88. if (!handler->err_handler(skb, info))
  89. break;
  90. }
  91. static struct net_protocol tunnel4_protocol = {
  92. .handler = tunnel4_rcv,
  93. .err_handler = tunnel4_err,
  94. .no_policy = 1,
  95. };
  96. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  97. static struct net_protocol tunnel64_protocol = {
  98. .handler = tunnel64_rcv,
  99. .err_handler = tunnel4_err,
  100. .no_policy = 1,
  101. };
  102. #endif
  103. static int __init tunnel4_init(void)
  104. {
  105. if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
  106. printk(KERN_ERR "tunnel4 init: can't add protocol\n");
  107. return -EAGAIN;
  108. }
  109. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  110. if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
  111. printk(KERN_ERR "tunnel64 init: can't add protocol\n");
  112. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  113. return -EAGAIN;
  114. }
  115. #endif
  116. return 0;
  117. }
  118. static void __exit tunnel4_fini(void)
  119. {
  120. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  121. if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
  122. printk(KERN_ERR "tunnel64 close: can't remove protocol\n");
  123. #endif
  124. if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  125. printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
  126. }
  127. module_init(tunnel4_init);
  128. module_exit(tunnel4_fini);
  129. MODULE_LICENSE("GPL");