tunnel4.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* tunnel4.c: Generic IP tunnel transformer.
  3. *
  4. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/mutex.h>
  9. #include <linux/mpls.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/slab.h>
  13. #include <net/icmp.h>
  14. #include <net/ip.h>
  15. #include <net/protocol.h>
  16. #include <net/xfrm.h>
  17. static struct xfrm_tunnel __rcu *tunnel4_handlers __read_mostly;
  18. static struct xfrm_tunnel __rcu *tunnel64_handlers __read_mostly;
  19. static struct xfrm_tunnel __rcu *tunnelmpls4_handlers __read_mostly;
  20. static DEFINE_MUTEX(tunnel4_mutex);
  21. static inline struct xfrm_tunnel __rcu **fam_handlers(unsigned short family)
  22. {
  23. return (family == AF_INET) ? &tunnel4_handlers :
  24. (family == AF_INET6) ? &tunnel64_handlers :
  25. &tunnelmpls4_handlers;
  26. }
  27. int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
  28. {
  29. struct xfrm_tunnel __rcu **pprev;
  30. struct xfrm_tunnel *t;
  31. int ret = -EEXIST;
  32. int priority = handler->priority;
  33. mutex_lock(&tunnel4_mutex);
  34. for (pprev = fam_handlers(family);
  35. (t = rcu_dereference_protected(*pprev,
  36. lockdep_is_held(&tunnel4_mutex))) != NULL;
  37. pprev = &t->next) {
  38. if (t->priority > priority)
  39. break;
  40. if (t->priority == priority)
  41. goto err;
  42. }
  43. handler->next = *pprev;
  44. rcu_assign_pointer(*pprev, handler);
  45. ret = 0;
  46. err:
  47. mutex_unlock(&tunnel4_mutex);
  48. return ret;
  49. }
  50. EXPORT_SYMBOL(xfrm4_tunnel_register);
  51. int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
  52. {
  53. struct xfrm_tunnel __rcu **pprev;
  54. struct xfrm_tunnel *t;
  55. int ret = -ENOENT;
  56. mutex_lock(&tunnel4_mutex);
  57. for (pprev = fam_handlers(family);
  58. (t = rcu_dereference_protected(*pprev,
  59. lockdep_is_held(&tunnel4_mutex))) != NULL;
  60. pprev = &t->next) {
  61. if (t == handler) {
  62. *pprev = handler->next;
  63. ret = 0;
  64. break;
  65. }
  66. }
  67. mutex_unlock(&tunnel4_mutex);
  68. synchronize_net();
  69. return ret;
  70. }
  71. EXPORT_SYMBOL(xfrm4_tunnel_deregister);
  72. #define for_each_tunnel_rcu(head, handler) \
  73. for (handler = rcu_dereference(head); \
  74. handler != NULL; \
  75. handler = rcu_dereference(handler->next)) \
  76. static int tunnel4_rcv(struct sk_buff *skb)
  77. {
  78. struct xfrm_tunnel *handler;
  79. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  80. goto drop;
  81. for_each_tunnel_rcu(tunnel4_handlers, handler)
  82. if (!handler->handler(skb))
  83. return 0;
  84. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  85. drop:
  86. kfree_skb(skb);
  87. return 0;
  88. }
  89. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  90. static int tunnel4_rcv_cb(struct sk_buff *skb, u8 proto, int err)
  91. {
  92. struct xfrm_tunnel __rcu *head;
  93. struct xfrm_tunnel *handler;
  94. int ret;
  95. head = (proto == IPPROTO_IPIP) ? tunnel4_handlers : tunnel64_handlers;
  96. for_each_tunnel_rcu(head, handler) {
  97. if (handler->cb_handler) {
  98. ret = handler->cb_handler(skb, err);
  99. if (ret <= 0)
  100. return ret;
  101. }
  102. }
  103. return 0;
  104. }
  105. static const struct xfrm_input_afinfo tunnel4_input_afinfo = {
  106. .family = AF_INET,
  107. .is_ipip = true,
  108. .callback = tunnel4_rcv_cb,
  109. };
  110. #endif
  111. #if IS_ENABLED(CONFIG_IPV6)
  112. static int tunnel64_rcv(struct sk_buff *skb)
  113. {
  114. struct xfrm_tunnel *handler;
  115. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  116. goto drop;
  117. for_each_tunnel_rcu(tunnel64_handlers, handler)
  118. if (!handler->handler(skb))
  119. return 0;
  120. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  121. drop:
  122. kfree_skb(skb);
  123. return 0;
  124. }
  125. #endif
  126. #if IS_ENABLED(CONFIG_MPLS)
  127. static int tunnelmpls4_rcv(struct sk_buff *skb)
  128. {
  129. struct xfrm_tunnel *handler;
  130. if (!pskb_may_pull(skb, sizeof(struct mpls_label)))
  131. goto drop;
  132. for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
  133. if (!handler->handler(skb))
  134. return 0;
  135. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  136. drop:
  137. kfree_skb(skb);
  138. return 0;
  139. }
  140. #endif
  141. static int tunnel4_err(struct sk_buff *skb, u32 info)
  142. {
  143. struct xfrm_tunnel *handler;
  144. for_each_tunnel_rcu(tunnel4_handlers, handler)
  145. if (!handler->err_handler(skb, info))
  146. return 0;
  147. return -ENOENT;
  148. }
  149. #if IS_ENABLED(CONFIG_IPV6)
  150. static int tunnel64_err(struct sk_buff *skb, u32 info)
  151. {
  152. struct xfrm_tunnel *handler;
  153. for_each_tunnel_rcu(tunnel64_handlers, handler)
  154. if (!handler->err_handler(skb, info))
  155. return 0;
  156. return -ENOENT;
  157. }
  158. #endif
  159. #if IS_ENABLED(CONFIG_MPLS)
  160. static int tunnelmpls4_err(struct sk_buff *skb, u32 info)
  161. {
  162. struct xfrm_tunnel *handler;
  163. for_each_tunnel_rcu(tunnelmpls4_handlers, handler)
  164. if (!handler->err_handler(skb, info))
  165. return 0;
  166. return -ENOENT;
  167. }
  168. #endif
  169. static const struct net_protocol tunnel4_protocol = {
  170. .handler = tunnel4_rcv,
  171. .err_handler = tunnel4_err,
  172. .no_policy = 1,
  173. .netns_ok = 1,
  174. };
  175. #if IS_ENABLED(CONFIG_IPV6)
  176. static const struct net_protocol tunnel64_protocol = {
  177. .handler = tunnel64_rcv,
  178. .err_handler = tunnel64_err,
  179. .no_policy = 1,
  180. .netns_ok = 1,
  181. };
  182. #endif
  183. #if IS_ENABLED(CONFIG_MPLS)
  184. static const struct net_protocol tunnelmpls4_protocol = {
  185. .handler = tunnelmpls4_rcv,
  186. .err_handler = tunnelmpls4_err,
  187. .no_policy = 1,
  188. .netns_ok = 1,
  189. };
  190. #endif
  191. static int __init tunnel4_init(void)
  192. {
  193. if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  194. goto err;
  195. #if IS_ENABLED(CONFIG_IPV6)
  196. if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
  197. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  198. goto err;
  199. }
  200. #endif
  201. #if IS_ENABLED(CONFIG_MPLS)
  202. if (inet_add_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS)) {
  203. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  204. #if IS_ENABLED(CONFIG_IPV6)
  205. inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
  206. #endif
  207. goto err;
  208. }
  209. #endif
  210. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  211. if (xfrm_input_register_afinfo(&tunnel4_input_afinfo)) {
  212. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  213. #if IS_ENABLED(CONFIG_IPV6)
  214. inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6);
  215. #endif
  216. #if IS_ENABLED(CONFIG_MPLS)
  217. inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS);
  218. #endif
  219. goto err;
  220. }
  221. #endif
  222. return 0;
  223. err:
  224. pr_err("%s: can't add protocol\n", __func__);
  225. return -EAGAIN;
  226. }
  227. static void __exit tunnel4_fini(void)
  228. {
  229. #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
  230. if (xfrm_input_unregister_afinfo(&tunnel4_input_afinfo))
  231. pr_err("tunnel4 close: can't remove input afinfo\n");
  232. #endif
  233. #if IS_ENABLED(CONFIG_MPLS)
  234. if (inet_del_protocol(&tunnelmpls4_protocol, IPPROTO_MPLS))
  235. pr_err("tunnelmpls4 close: can't remove protocol\n");
  236. #endif
  237. #if IS_ENABLED(CONFIG_IPV6)
  238. if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
  239. pr_err("tunnel64 close: can't remove protocol\n");
  240. #endif
  241. if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  242. pr_err("tunnel4 close: can't remove protocol\n");
  243. }
  244. module_init(tunnel4_init);
  245. module_exit(tunnel4_fini);
  246. MODULE_LICENSE("GPL");