tunnel4.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/mpls.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/slab.h>
  12. #include <net/icmp.h>
  13. #include <net/ip.h>
  14. #include <net/protocol.h>
  15. #include <net/xfrm.h>
  16. static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
  17. static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
  18. static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly;
  19. static DEFINE_MUTEX(tunnel4_mutex);
  20. static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
  21. {
  22. return (family == AF_INET) ? &tunnel4_handlers :
  23. (family == AF_INET6) ? &tunnel64_handlers :
  24. &tunnelmpls4_handlers;
  25. }
  26. int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
  27. {
  28. struct xfrm_tunnel __rcu **pprev;
  29. struct xfrm_tunnel *t;
  30. int ret = -EEXIST;
  31. int priority = handler->priority;
  32. mutex_lock(&tunnel4_mutex);
  33. for (pprev = fam_handlers(family);
  34. (t = rcu_dereference_protected(*pprev,
  35. lockdep_is_held(&tunnel4_mutex))) != NULL;
  36. pprev = &t->next) {
  37. if (t->priority > priority)
  38. break;
  39. if (t->priority == priority)
  40. goto err;
  41. }
  42. handler->next = *pprev;
  43. rcu_assign_pointer(*pprev, handler);
  44. ret = 0;
  45. err:
  46. mutex_unlock(&tunnel4_mutex);
  47. return ret;
  48. }
  49. EXPORT_SYMBOL(xfrm4_tunnel_register);
  50. int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
  51. {
  52. struct xfrm_tunnel __rcu **pprev;
  53. struct xfrm_tunnel *t;
  54. int ret = -ENOENT;
  55. mutex_lock(&tunnel4_mutex);
  56. for (pprev = fam_handlers(family);
  57. (t = rcu_dereference_protected(*pprev,
  58. lockdep_is_held(&tunnel4_mutex))) != NULL;
  59. pprev = &t->next) {
  60. if (t == handler) {
  61. *pprev = handler->next;
  62. ret = 0;
  63. break;
  64. }
  65. }
  66. mutex_unlock(&tunnel4_mutex);
  67. synchronize_net();
  68. return ret;
  69. }
  70. EXPORT_SYMBOL(xfrm4_tunnel_deregister);
  71. #define for_each_tunnel_rcu(head, handler) \
  72. for (handler = rcu_dereference(head); \
  73. handler != NULL; \
  74. handler = rcu_dereference(handler->next)) \
  75. static int tunnel4_rcv(struct sk_buff *skb)
  76. {
  77. struct xfrm_tunnel *handler;
  78. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  79. goto drop;
  80. for_each_tunnel_rcu(tunnel4_handlers, handler)
  81. if (!handler->handler(skb))
  82. return 0;
  83. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  84. drop:
  85. kfree_skb(skb);
  86. return 0;
  87. }
  88. #if IS_ENABLED(CONFIG_IPV6)
  89. static int tunnel64_rcv(struct sk_buff *skb)
  90. {
  91. struct xfrm_tunnel *handler;
  92. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  93. goto drop;
  94. for_each_tunnel_rcu(tunnel64_handlers, handler)
  95. if (!handler->handler(skb))
  96. return 0;
  97. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  98. drop:
  99. kfree_skb(skb);
  100. return 0;
  101. }
  102. #endif
  103. #if IS_ENABLED(CONFIG_MPLS)
  104. static int tunnelmpls4_rcv(struct sk_buff *skb)
  105. {
  106. struct xfrm_tunnel *handler;
  107. if (!pskb_may_pull(skb, sizeof(struct mpls_label)))
  108. goto drop;
  109. for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
  110. if (!handler->handler(skb))
  111. return 0;
  112. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  113. drop:
  114. kfree_skb(skb);
  115. return 0;
  116. }
  117. #endif
  118. static void tunnel4_err(struct sk_buff *skb, u32 info)
  119. {
  120. struct xfrm_tunnel *handler;
  121. for_each_tunnel_rcu(tunnel4_handlers, handler)
  122. if (!handler->err_handler(skb, info))
  123. break;
  124. }
  125. #if IS_ENABLED(CONFIG_IPV6)
  126. static void tunnel64_err(struct sk_buff *skb, u32 info)
  127. {
  128. struct xfrm_tunnel *handler;
  129. for_each_tunnel_rcu(tunnel64_handlers, handler)
  130. if (!handler->err_handler(skb, info))
  131. break;
  132. }
  133. #endif
  134. #if IS_ENABLED(CONFIG_MPLS)
  135. static void tunnelmpls4_err(struct sk_buff *skb, u32 info)
  136. {
  137. struct xfrm_tunnel *handler;
  138. for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
  139. if (!handler->err_handler(skb, info))
  140. break;
  141. }
  142. #endif
  143. static const struct net_protocol tunnel4_protocol = {
  144. .handler = tunnel4_rcv,
  145. .err_handler = tunnel4_err,
  146. .no_policy = 1,
  147. .netns_ok = 1,
  148. };
  149. #if IS_ENABLED(CONFIG_IPV6)
  150. static const struct net_protocol tunnel64_protocol = {
  151. .handler = tunnel64_rcv,
  152. .err_handler = tunnel64_err,
  153. .no_policy = 1,
  154. .netns_ok = 1,
  155. };
  156. #endif
  157. #if IS_ENABLED(CONFIG_MPLS)
  158. static const struct net_protocol tunnelmpls4_protocol = {
  159. .handler = tunnelmpls4_rcv,
  160. .err_handler = tunnelmpls4_err,
  161. .no_policy = 1,
  162. .netns_ok = 1,
  163. };
  164. #endif
  165. static int __init tunnel4_init(void)
  166. {
  167. if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  168. goto err;
  169. #if IS_ENABLED(CONFIG_IPV6)
  170. if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
  171. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  172. goto err;
  173. }
  174. #endif
  175. #if IS_ENABLED(CONFIG_MPLS)
  176. if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) {
  177. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  178. #if IS_ENABLED(CONFIG_IPV6)
  179. inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
  180. #endif
  181. goto err;
  182. }
  183. #endif
  184. return 0;
  185. err:
  186. pr_err("%s: can't add protocol\n", __func__);
  187. return -EAGAIN;
  188. }
  189. static void __exit tunnel4_fini(void)
  190. {
  191. #if IS_ENABLED(CONFIG_MPLS)
  192. if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS))
  193. pr_err("tunnelmpls4 close: can't remove protocol\n");
  194. #endif
  195. #if IS_ENABLED(CONFIG_IPV6)
  196. if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
  197. pr_err("tunnel64 close: can't remove protocol\n");
  198. #endif
  199. if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  200. pr_err("tunnel4 close: can't remove protocol\n");
  201. }
  202. module_init(tunnel4_init);
  203. module_exit(tunnel4_fini);
  204. MODULE_LICENSE("GPL");