xfrm6_protocol.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* xfrm6_protocol.c - Generic xfrm protocol multiplexer for ipv6.
  2. *
  3. * Copyright (C) 2013 secunet Security Networks AG
  4. *
  5. * Author:
  6. * Steffen Klassert <steffen.klassert@secunet.com>
  7. *
  8. * Based on:
  9. * net/ipv4/xfrm4_protocol.c
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/mutex.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/icmpv6.h>
  20. #include <net/ipv6.h>
  21. #include <net/protocol.h>
  22. #include <net/xfrm.h>
  23. static struct xfrm6_protocol __rcu *esp6_handlers __read_mostly;
  24. static struct xfrm6_protocol __rcu *ah6_handlers __read_mostly;
  25. static struct xfrm6_protocol __rcu *ipcomp6_handlers __read_mostly;
  26. static DEFINE_MUTEX(xfrm6_protocol_mutex);
  27. static inline struct xfrm6_protocol __rcu **proto_handlers(u8 protocol)
  28. {
  29. switch (protocol) {
  30. case IPPROTO_ESP:
  31. return &esp6_handlers;
  32. case IPPROTO_AH:
  33. return &ah6_handlers;
  34. case IPPROTO_COMP:
  35. return &ipcomp6_handlers;
  36. }
  37. return NULL;
  38. }
  39. #define for_each_protocol_rcu(head, handler) \
  40. for (handler = rcu_dereference(head); \
  41. handler != NULL; \
  42. handler = rcu_dereference(handler->next)) \
  43. int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
  44. {
  45. int ret;
  46. struct xfrm6_protocol *handler;
  47. struct xfrm6_protocol __rcu **head = proto_handlers(protocol);
  48. if (!head)
  49. return 0;
  50. for_each_protocol_rcu(*proto_handlers(protocol), handler)
  51. if ((ret = handler->cb_handler(skb, err)) <= 0)
  52. return ret;
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(xfrm6_rcv_cb);
  56. static int xfrm6_esp_rcv(struct sk_buff *skb)
  57. {
  58. int ret;
  59. struct xfrm6_protocol *handler;
  60. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
  61. for_each_protocol_rcu(esp6_handlers, handler)
  62. if ((ret = handler->handler(skb)) != -EINVAL)
  63. return ret;
  64. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  65. kfree_skb(skb);
  66. return 0;
  67. }
  68. static void xfrm6_esp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  69. u8 type, u8 code, int offset, __be32 info)
  70. {
  71. struct xfrm6_protocol *handler;
  72. for_each_protocol_rcu(esp6_handlers, handler)
  73. if (!handler->err_handler(skb, opt, type, code, offset, info))
  74. break;
  75. }
  76. static int xfrm6_ah_rcv(struct sk_buff *skb)
  77. {
  78. int ret;
  79. struct xfrm6_protocol *handler;
  80. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
  81. for_each_protocol_rcu(ah6_handlers, handler)
  82. if ((ret = handler->handler(skb)) != -EINVAL)
  83. return ret;
  84. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  85. kfree_skb(skb);
  86. return 0;
  87. }
  88. static void xfrm6_ah_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  89. u8 type, u8 code, int offset, __be32 info)
  90. {
  91. struct xfrm6_protocol *handler;
  92. for_each_protocol_rcu(ah6_handlers, handler)
  93. if (!handler->err_handler(skb, opt, type, code, offset, info))
  94. break;
  95. }
  96. static int xfrm6_ipcomp_rcv(struct sk_buff *skb)
  97. {
  98. int ret;
  99. struct xfrm6_protocol *handler;
  100. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
  101. for_each_protocol_rcu(ipcomp6_handlers, handler)
  102. if ((ret = handler->handler(skb)) != -EINVAL)
  103. return ret;
  104. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
  105. kfree_skb(skb);
  106. return 0;
  107. }
  108. static void xfrm6_ipcomp_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  109. u8 type, u8 code, int offset, __be32 info)
  110. {
  111. struct xfrm6_protocol *handler;
  112. for_each_protocol_rcu(ipcomp6_handlers, handler)
  113. if (!handler->err_handler(skb, opt, type, code, offset, info))
  114. break;
  115. }
  116. static const struct inet6_protocol esp6_protocol = {
  117. .handler = xfrm6_esp_rcv,
  118. .err_handler = xfrm6_esp_err,
  119. .flags = INET6_PROTO_NOPOLICY,
  120. };
  121. static const struct inet6_protocol ah6_protocol = {
  122. .handler = xfrm6_ah_rcv,
  123. .err_handler = xfrm6_ah_err,
  124. .flags = INET6_PROTO_NOPOLICY,
  125. };
  126. static const struct inet6_protocol ipcomp6_protocol = {
  127. .handler = xfrm6_ipcomp_rcv,
  128. .err_handler = xfrm6_ipcomp_err,
  129. .flags = INET6_PROTO_NOPOLICY,
  130. };
  131. static struct xfrm_input_afinfo xfrm6_input_afinfo = {
  132. .family = AF_INET6,
  133. .owner = THIS_MODULE,
  134. .callback = xfrm6_rcv_cb,
  135. };
  136. static inline const struct inet6_protocol *netproto(unsigned char protocol)
  137. {
  138. switch (protocol) {
  139. case IPPROTO_ESP:
  140. return &esp6_protocol;
  141. case IPPROTO_AH:
  142. return &ah6_protocol;
  143. case IPPROTO_COMP:
  144. return &ipcomp6_protocol;
  145. }
  146. return NULL;
  147. }
  148. int xfrm6_protocol_register(struct xfrm6_protocol *handler,
  149. unsigned char protocol)
  150. {
  151. struct xfrm6_protocol __rcu **pprev;
  152. struct xfrm6_protocol *t;
  153. bool add_netproto = false;
  154. int ret = -EEXIST;
  155. int priority = handler->priority;
  156. if (!proto_handlers(protocol) || !netproto(protocol))
  157. return -EINVAL;
  158. mutex_lock(&xfrm6_protocol_mutex);
  159. if (!rcu_dereference_protected(*proto_handlers(protocol),
  160. lockdep_is_held(&xfrm6_protocol_mutex)))
  161. add_netproto = true;
  162. for (pprev = proto_handlers(protocol);
  163. (t = rcu_dereference_protected(*pprev,
  164. lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
  165. pprev = &t->next) {
  166. if (t->priority < priority)
  167. break;
  168. if (t->priority == priority)
  169. goto err;
  170. }
  171. handler->next = *pprev;
  172. rcu_assign_pointer(*pprev, handler);
  173. ret = 0;
  174. err:
  175. mutex_unlock(&xfrm6_protocol_mutex);
  176. if (add_netproto) {
  177. if (inet6_add_protocol(netproto(protocol), protocol)) {
  178. pr_err("%s: can't add protocol\n", __func__);
  179. ret = -EAGAIN;
  180. }
  181. }
  182. return ret;
  183. }
  184. EXPORT_SYMBOL(xfrm6_protocol_register);
  185. int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
  186. unsigned char protocol)
  187. {
  188. struct xfrm6_protocol __rcu **pprev;
  189. struct xfrm6_protocol *t;
  190. int ret = -ENOENT;
  191. if (!proto_handlers(protocol) || !netproto(protocol))
  192. return -EINVAL;
  193. mutex_lock(&xfrm6_protocol_mutex);
  194. for (pprev = proto_handlers(protocol);
  195. (t = rcu_dereference_protected(*pprev,
  196. lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
  197. pprev = &t->next) {
  198. if (t == handler) {
  199. *pprev = handler->next;
  200. ret = 0;
  201. break;
  202. }
  203. }
  204. if (!rcu_dereference_protected(*proto_handlers(protocol),
  205. lockdep_is_held(&xfrm6_protocol_mutex))) {
  206. if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
  207. pr_err("%s: can't remove protocol\n", __func__);
  208. ret = -EAGAIN;
  209. }
  210. }
  211. mutex_unlock(&xfrm6_protocol_mutex);
  212. synchronize_net();
  213. return ret;
  214. }
  215. EXPORT_SYMBOL(xfrm6_protocol_deregister);
  216. int __init xfrm6_protocol_init(void)
  217. {
  218. return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
  219. }
  220. void xfrm6_protocol_fini(void)
  221. {
  222. xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);
  223. }