tunnel6.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C)2003,2004 USAGI/WIDE Project
  4. *
  5. * Authors Mitsuru KANDA <[email protected]>
  6. * YOSHIFUJI Hideaki <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "IPv6: " fmt
  9. #include <linux/icmpv6.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/slab.h>
  16. #include <net/ipv6.h>
  17. #include <net/protocol.h>
  18. #include <net/xfrm.h>
  19. static struct xfrm6_tunnel __rcu *tunnel6_handlers __read_mostly;
  20. static struct xfrm6_tunnel __rcu *tunnel46_handlers __read_mostly;
  21. static struct xfrm6_tunnel __rcu *tunnelmpls6_handlers __read_mostly;
  22. static DEFINE_MUTEX(tunnel6_mutex);
  23. static inline int xfrm6_tunnel_mpls_supported(void)
  24. {
  25. return IS_ENABLED(CONFIG_MPLS);
  26. }
  27. int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family)
  28. {
  29. struct xfrm6_tunnel __rcu **pprev;
  30. struct xfrm6_tunnel *t;
  31. int ret = -EEXIST;
  32. int priority = handler->priority;
  33. mutex_lock(&tunnel6_mutex);
  34. switch (family) {
  35. case AF_INET6:
  36. pprev = &tunnel6_handlers;
  37. break;
  38. case AF_INET:
  39. pprev = &tunnel46_handlers;
  40. break;
  41. case AF_MPLS:
  42. pprev = &tunnelmpls6_handlers;
  43. break;
  44. default:
  45. goto err;
  46. }
  47. for (; (t = rcu_dereference_protected(*pprev,
  48. lockdep_is_held(&tunnel6_mutex))) != NULL;
  49. pprev = &t->next) {
  50. if (t->priority > priority)
  51. break;
  52. if (t->priority == priority)
  53. goto err;
  54. }
  55. handler->next = *pprev;
  56. rcu_assign_pointer(*pprev, handler);
  57. ret = 0;
  58. err:
  59. mutex_unlock(&tunnel6_mutex);
  60. return ret;
  61. }
  62. EXPORT_SYMBOL(xfrm6_tunnel_register);
  63. int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family)
  64. {
  65. struct xfrm6_tunnel __rcu **pprev;
  66. struct xfrm6_tunnel *t;
  67. int ret = -ENOENT;
  68. mutex_lock(&tunnel6_mutex);
  69. switch (family) {
  70. case AF_INET6:
  71. pprev = &tunnel6_handlers;
  72. break;
  73. case AF_INET:
  74. pprev = &tunnel46_handlers;
  75. break;
  76. case AF_MPLS:
  77. pprev = &tunnelmpls6_handlers;
  78. break;
  79. default:
  80. goto err;
  81. }
  82. for (; (t = rcu_dereference_protected(*pprev,
  83. lockdep_is_held(&tunnel6_mutex))) != NULL;
  84. pprev = &t->next) {
  85. if (t == handler) {
  86. *pprev = handler->next;
  87. ret = 0;
  88. break;
  89. }
  90. }
  91. err:
  92. mutex_unlock(&tunnel6_mutex);
  93. synchronize_net();
  94. return ret;
  95. }
  96. EXPORT_SYMBOL(xfrm6_tunnel_deregister);
  97. #define for_each_tunnel_rcu(head, handler) \
  98. for (handler = rcu_dereference(head); \
  99. handler != NULL; \
  100. handler = rcu_dereference(handler->next)) \
  101. static int tunnelmpls6_rcv(struct sk_buff *skb)
  102. {
  103. struct xfrm6_tunnel *handler;
  104. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  105. goto drop;
  106. for_each_tunnel_rcu(tunnelmpls6_handlers, handler)
  107. if (!handler->handler(skb))
  108. return 0;
  109. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  110. drop:
  111. kfree_skb(skb);
  112. return 0;
  113. }
  114. static int tunnel6_rcv(struct sk_buff *skb)
  115. {
  116. struct xfrm6_tunnel *handler;
  117. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  118. goto drop;
  119. for_each_tunnel_rcu(tunnel6_handlers, handler)
  120. if (!handler->handler(skb))
  121. return 0;
  122. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  123. drop:
  124. kfree_skb(skb);
  125. return 0;
  126. }
  127. #if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
  128. static int tunnel6_rcv_cb(struct sk_buff *skb, u8 proto, int err)
  129. {
  130. struct xfrm6_tunnel __rcu *head;
  131. struct xfrm6_tunnel *handler;
  132. int ret;
  133. head = (proto == IPPROTO_IPV6) ? tunnel6_handlers : tunnel46_handlers;
  134. for_each_tunnel_rcu(head, handler) {
  135. if (handler->cb_handler) {
  136. ret = handler->cb_handler(skb, err);
  137. if (ret <= 0)
  138. return ret;
  139. }
  140. }
  141. return 0;
  142. }
  143. static const struct xfrm_input_afinfo tunnel6_input_afinfo = {
  144. .family = AF_INET6,
  145. .is_ipip = true,
  146. .callback = tunnel6_rcv_cb,
  147. };
  148. #endif
  149. static int tunnel46_rcv(struct sk_buff *skb)
  150. {
  151. struct xfrm6_tunnel *handler;
  152. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  153. goto drop;
  154. for_each_tunnel_rcu(tunnel46_handlers, handler)
  155. if (!handler->handler(skb))
  156. return 0;
  157. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  158. drop:
  159. kfree_skb(skb);
  160. return 0;
  161. }
  162. static int tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  163. u8 type, u8 code, int offset, __be32 info)
  164. {
  165. struct xfrm6_tunnel *handler;
  166. for_each_tunnel_rcu(tunnel6_handlers, handler)
  167. if (!handler->err_handler(skb, opt, type, code, offset, info))
  168. return 0;
  169. return -ENOENT;
  170. }
  171. static int tunnel46_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  172. u8 type, u8 code, int offset, __be32 info)
  173. {
  174. struct xfrm6_tunnel *handler;
  175. for_each_tunnel_rcu(tunnel46_handlers, handler)
  176. if (!handler->err_handler(skb, opt, type, code, offset, info))
  177. return 0;
  178. return -ENOENT;
  179. }
  180. static int tunnelmpls6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  181. u8 type, u8 code, int offset, __be32 info)
  182. {
  183. struct xfrm6_tunnel *handler;
  184. for_each_tunnel_rcu(tunnelmpls6_handlers, handler)
  185. if (!handler->err_handler(skb, opt, type, code, offset, info))
  186. return 0;
  187. return -ENOENT;
  188. }
  189. static const struct inet6_protocol tunnel6_protocol = {
  190. .handler = tunnel6_rcv,
  191. .err_handler = tunnel6_err,
  192. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  193. };
  194. static const struct inet6_protocol tunnel46_protocol = {
  195. .handler = tunnel46_rcv,
  196. .err_handler = tunnel46_err,
  197. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  198. };
  199. static const struct inet6_protocol tunnelmpls6_protocol = {
  200. .handler = tunnelmpls6_rcv,
  201. .err_handler = tunnelmpls6_err,
  202. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  203. };
  204. static int __init tunnel6_init(void)
  205. {
  206. if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) {
  207. pr_err("%s: can't add protocol\n", __func__);
  208. return -EAGAIN;
  209. }
  210. if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) {
  211. pr_err("%s: can't add protocol\n", __func__);
  212. inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
  213. return -EAGAIN;
  214. }
  215. if (xfrm6_tunnel_mpls_supported() &&
  216. inet6_add_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS)) {
  217. pr_err("%s: can't add protocol\n", __func__);
  218. inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
  219. inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP);
  220. return -EAGAIN;
  221. }
  222. #if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
  223. if (xfrm_input_register_afinfo(&tunnel6_input_afinfo)) {
  224. pr_err("%s: can't add input afinfo\n", __func__);
  225. inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
  226. inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP);
  227. if (xfrm6_tunnel_mpls_supported())
  228. inet6_del_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS);
  229. return -EAGAIN;
  230. }
  231. #endif
  232. return 0;
  233. }
  234. static void __exit tunnel6_fini(void)
  235. {
  236. #if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
  237. if (xfrm_input_unregister_afinfo(&tunnel6_input_afinfo))
  238. pr_err("%s: can't remove input afinfo\n", __func__);
  239. #endif
  240. if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
  241. pr_err("%s: can't remove protocol\n", __func__);
  242. if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
  243. pr_err("%s: can't remove protocol\n", __func__);
  244. if (xfrm6_tunnel_mpls_supported() &&
  245. inet6_del_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS))
  246. pr_err("%s: can't remove protocol\n", __func__);
  247. }
  248. module_init(tunnel6_init);
  249. module_exit(tunnel6_fini);
  250. MODULE_LICENSE("GPL");