xfrm6_protocol.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 const struct xfrm_input_afinfo xfrm6_input_afinfo = {
  132. .family = AF_INET6,
  133. .callback = xfrm6_rcv_cb,
  134. };
  135. static inline const struct inet6_protocol *netproto(unsigned char protocol)
  136. {
  137. switch (protocol) {
  138. case IPPROTO_ESP:
  139. return &esp6_protocol;
  140. case IPPROTO_AH:
  141. return &ah6_protocol;
  142. case IPPROTO_COMP:
  143. return &ipcomp6_protocol;
  144. }
  145. return NULL;
  146. }
  147. int xfrm6_protocol_register(struct xfrm6_protocol *handler,
  148. unsigned char protocol)
  149. {
  150. struct xfrm6_protocol __rcu **pprev;
  151. struct xfrm6_protocol *t;
  152. bool add_netproto = false;
  153. int ret = -EEXIST;
  154. int priority = handler->priority;
  155. if (!proto_handlers(protocol) || !netproto(protocol))
  156. return -EINVAL;
  157. mutex_lock(&xfrm6_protocol_mutex);
  158. if (!rcu_dereference_protected(*proto_handlers(protocol),
  159. lockdep_is_held(&xfrm6_protocol_mutex)))
  160. add_netproto = true;
  161. for (pprev = proto_handlers(protocol);
  162. (t = rcu_dereference_protected(*pprev,
  163. lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
  164. pprev = &t->next) {
  165. if (t->priority < priority)
  166. break;
  167. if (t->priority == priority)
  168. goto err;
  169. }
  170. handler->next = *pprev;
  171. rcu_assign_pointer(*pprev, handler);
  172. ret = 0;
  173. err:
  174. mutex_unlock(&xfrm6_protocol_mutex);
  175. if (add_netproto) {
  176. if (inet6_add_protocol(netproto(protocol), protocol)) {
  177. pr_err("%s: can't add protocol\n", __func__);
  178. ret = -EAGAIN;
  179. }
  180. }
  181. return ret;
  182. }
  183. EXPORT_SYMBOL(xfrm6_protocol_register);
  184. int xfrm6_protocol_deregister(struct xfrm6_protocol *handler,
  185. unsigned char protocol)
  186. {
  187. struct xfrm6_protocol __rcu **pprev;
  188. struct xfrm6_protocol *t;
  189. int ret = -ENOENT;
  190. if (!proto_handlers(protocol) || !netproto(protocol))
  191. return -EINVAL;
  192. mutex_lock(&xfrm6_protocol_mutex);
  193. for (pprev = proto_handlers(protocol);
  194. (t = rcu_dereference_protected(*pprev,
  195. lockdep_is_held(&xfrm6_protocol_mutex))) != NULL;
  196. pprev = &t->next) {
  197. if (t == handler) {
  198. *pprev = handler->next;
  199. ret = 0;
  200. break;
  201. }
  202. }
  203. if (!rcu_dereference_protected(*proto_handlers(protocol),
  204. lockdep_is_held(&xfrm6_protocol_mutex))) {
  205. if (inet6_del_protocol(netproto(protocol), protocol) < 0) {
  206. pr_err("%s: can't remove protocol\n", __func__);
  207. ret = -EAGAIN;
  208. }
  209. }
  210. mutex_unlock(&xfrm6_protocol_mutex);
  211. synchronize_net();
  212. return ret;
  213. }
  214. EXPORT_SYMBOL(xfrm6_protocol_deregister);
  215. int __init xfrm6_protocol_init(void)
  216. {
  217. return xfrm_input_register_afinfo(&xfrm6_input_afinfo);
  218. }
  219. void xfrm6_protocol_fini(void)
  220. {
  221. xfrm_input_unregister_afinfo(&xfrm6_input_afinfo);
  222. }