tunnel4.c 4.3 KB

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