tunnel4.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 __read_mostly;
  16. static struct xfrm_tunnel *tunnel64_handlers __read_mostly;
  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. #define for_each_tunnel_rcu(head, handler) \
  60. for (handler = rcu_dereference(head); \
  61. handler != NULL; \
  62. handler = rcu_dereference(handler->next)) \
  63. static int tunnel4_rcv(struct sk_buff *skb)
  64. {
  65. struct xfrm_tunnel *handler;
  66. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  67. goto drop;
  68. for_each_tunnel_rcu(tunnel4_handlers, handler)
  69. if (!handler->handler(skb))
  70. return 0;
  71. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  72. drop:
  73. kfree_skb(skb);
  74. return 0;
  75. }
  76. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  77. static int tunnel64_rcv(struct sk_buff *skb)
  78. {
  79. struct xfrm_tunnel *handler;
  80. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  81. goto drop;
  82. for_each_tunnel_rcu(tunnel64_handlers, handler)
  83. if (!handler->handler(skb))
  84. return 0;
  85. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  86. drop:
  87. kfree_skb(skb);
  88. return 0;
  89. }
  90. #endif
  91. static void tunnel4_err(struct sk_buff *skb, u32 info)
  92. {
  93. struct xfrm_tunnel *handler;
  94. for_each_tunnel_rcu(tunnel4_handlers, handler)
  95. if (!handler->err_handler(skb, info))
  96. break;
  97. }
  98. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  99. static void tunnel64_err(struct sk_buff *skb, u32 info)
  100. {
  101. struct xfrm_tunnel *handler;
  102. for_each_tunnel_rcu(tunnel64_handlers, handler)
  103. if (!handler->err_handler(skb, info))
  104. break;
  105. }
  106. #endif
  107. static const struct net_protocol tunnel4_protocol = {
  108. .handler = tunnel4_rcv,
  109. .err_handler = tunnel4_err,
  110. .no_policy = 1,
  111. .netns_ok = 1,
  112. };
  113. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  114. static const struct net_protocol tunnel64_protocol = {
  115. .handler = tunnel64_rcv,
  116. .err_handler = tunnel64_err,
  117. .no_policy = 1,
  118. .netns_ok = 1,
  119. };
  120. #endif
  121. static int __init tunnel4_init(void)
  122. {
  123. if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
  124. printk(KERN_ERR "tunnel4 init: can't add protocol\n");
  125. return -EAGAIN;
  126. }
  127. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  128. if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
  129. printk(KERN_ERR "tunnel64 init: can't add protocol\n");
  130. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  131. return -EAGAIN;
  132. }
  133. #endif
  134. return 0;
  135. }
  136. static void __exit tunnel4_fini(void)
  137. {
  138. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  139. if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
  140. printk(KERN_ERR "tunnel64 close: can't remove protocol\n");
  141. #endif
  142. if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  143. printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
  144. }
  145. module_init(tunnel4_init);
  146. module_exit(tunnel4_fini);
  147. MODULE_LICENSE("GPL");