tunnel4.c 3.9 KB

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