xfrm4_protocol.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* xfrm4_protocol.c - Generic xfrm protocol multiplexer.
  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/tunnel4.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 <net/icmp.h>
  20. #include <net/ip.h>
  21. #include <net/protocol.h>
  22. #include <net/xfrm.h>
  23. static struct xfrm4_protocol __rcu *esp4_handlers __read_mostly;
  24. static struct xfrm4_protocol __rcu *ah4_handlers __read_mostly;
  25. static struct xfrm4_protocol __rcu *ipcomp4_handlers __read_mostly;
  26. static DEFINE_MUTEX(xfrm4_protocol_mutex);
  27. static inline struct xfrm4_protocol __rcu **proto_handlers(u8 protocol)
  28. {
  29. switch (protocol) {
  30. case IPPROTO_ESP:
  31. return &esp4_handlers;
  32. case IPPROTO_AH:
  33. return &ah4_handlers;
  34. case IPPROTO_COMP:
  35. return &ipcomp4_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 xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err)
  44. {
  45. int ret;
  46. struct xfrm4_protocol *handler;
  47. struct xfrm4_protocol __rcu **head = proto_handlers(protocol);
  48. if (!head)
  49. return 0;
  50. for_each_protocol_rcu(*head, handler)
  51. if ((ret = handler->cb_handler(skb, err)) <= 0)
  52. return ret;
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(xfrm4_rcv_cb);
  56. int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
  57. int encap_type)
  58. {
  59. int ret;
  60. struct xfrm4_protocol *handler;
  61. struct xfrm4_protocol __rcu **head = proto_handlers(nexthdr);
  62. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
  63. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  64. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  65. if (!head)
  66. goto out;
  67. for_each_protocol_rcu(*head, handler)
  68. if ((ret = handler->input_handler(skb, nexthdr, spi, encap_type)) != -EINVAL)
  69. return ret;
  70. out:
  71. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  72. kfree_skb(skb);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(xfrm4_rcv_encap);
  76. static int xfrm4_esp_rcv(struct sk_buff *skb)
  77. {
  78. int ret;
  79. struct xfrm4_protocol *handler;
  80. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
  81. for_each_protocol_rcu(esp4_handlers, handler)
  82. if ((ret = handler->handler(skb)) != -EINVAL)
  83. return ret;
  84. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  85. kfree_skb(skb);
  86. return 0;
  87. }
  88. static void xfrm4_esp_err(struct sk_buff *skb, u32 info)
  89. {
  90. struct xfrm4_protocol *handler;
  91. for_each_protocol_rcu(esp4_handlers, handler)
  92. if (!handler->err_handler(skb, info))
  93. break;
  94. }
  95. static int xfrm4_ah_rcv(struct sk_buff *skb)
  96. {
  97. int ret;
  98. struct xfrm4_protocol *handler;
  99. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
  100. for_each_protocol_rcu(ah4_handlers, handler)
  101. if ((ret = handler->handler(skb)) != -EINVAL)
  102. return ret;
  103. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  104. kfree_skb(skb);
  105. return 0;
  106. }
  107. static void xfrm4_ah_err(struct sk_buff *skb, u32 info)
  108. {
  109. struct xfrm4_protocol *handler;
  110. for_each_protocol_rcu(ah4_handlers, handler)
  111. if (!handler->err_handler(skb, info))
  112. break;
  113. }
  114. static int xfrm4_ipcomp_rcv(struct sk_buff *skb)
  115. {
  116. int ret;
  117. struct xfrm4_protocol *handler;
  118. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
  119. for_each_protocol_rcu(ipcomp4_handlers, handler)
  120. if ((ret = handler->handler(skb)) != -EINVAL)
  121. return ret;
  122. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  123. kfree_skb(skb);
  124. return 0;
  125. }
  126. static void xfrm4_ipcomp_err(struct sk_buff *skb, u32 info)
  127. {
  128. struct xfrm4_protocol *handler;
  129. for_each_protocol_rcu(ipcomp4_handlers, handler)
  130. if (!handler->err_handler(skb, info))
  131. break;
  132. }
  133. static const struct net_protocol esp4_protocol = {
  134. .handler = xfrm4_esp_rcv,
  135. .err_handler = xfrm4_esp_err,
  136. .no_policy = 1,
  137. .netns_ok = 1,
  138. };
  139. static const struct net_protocol ah4_protocol = {
  140. .handler = xfrm4_ah_rcv,
  141. .err_handler = xfrm4_ah_err,
  142. .no_policy = 1,
  143. .netns_ok = 1,
  144. };
  145. static const struct net_protocol ipcomp4_protocol = {
  146. .handler = xfrm4_ipcomp_rcv,
  147. .err_handler = xfrm4_ipcomp_err,
  148. .no_policy = 1,
  149. .netns_ok = 1,
  150. };
  151. static struct xfrm_input_afinfo xfrm4_input_afinfo = {
  152. .family = AF_INET,
  153. .owner = THIS_MODULE,
  154. .callback = xfrm4_rcv_cb,
  155. };
  156. static inline const struct net_protocol *netproto(unsigned char protocol)
  157. {
  158. switch (protocol) {
  159. case IPPROTO_ESP:
  160. return &esp4_protocol;
  161. case IPPROTO_AH:
  162. return &ah4_protocol;
  163. case IPPROTO_COMP:
  164. return &ipcomp4_protocol;
  165. }
  166. return NULL;
  167. }
  168. int xfrm4_protocol_register(struct xfrm4_protocol *handler,
  169. unsigned char protocol)
  170. {
  171. struct xfrm4_protocol __rcu **pprev;
  172. struct xfrm4_protocol *t;
  173. bool add_netproto = false;
  174. int ret = -EEXIST;
  175. int priority = handler->priority;
  176. if (!proto_handlers(protocol) || !netproto(protocol))
  177. return -EINVAL;
  178. mutex_lock(&xfrm4_protocol_mutex);
  179. if (!rcu_dereference_protected(*proto_handlers(protocol),
  180. lockdep_is_held(&xfrm4_protocol_mutex)))
  181. add_netproto = true;
  182. for (pprev = proto_handlers(protocol);
  183. (t = rcu_dereference_protected(*pprev,
  184. lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
  185. pprev = &t->next) {
  186. if (t->priority < priority)
  187. break;
  188. if (t->priority == priority)
  189. goto err;
  190. }
  191. handler->next = *pprev;
  192. rcu_assign_pointer(*pprev, handler);
  193. ret = 0;
  194. err:
  195. mutex_unlock(&xfrm4_protocol_mutex);
  196. if (add_netproto) {
  197. if (inet_add_protocol(netproto(protocol), protocol)) {
  198. pr_err("%s: can't add protocol\n", __func__);
  199. ret = -EAGAIN;
  200. }
  201. }
  202. return ret;
  203. }
  204. EXPORT_SYMBOL(xfrm4_protocol_register);
  205. int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
  206. unsigned char protocol)
  207. {
  208. struct xfrm4_protocol __rcu **pprev;
  209. struct xfrm4_protocol *t;
  210. int ret = -ENOENT;
  211. if (!proto_handlers(protocol) || !netproto(protocol))
  212. return -EINVAL;
  213. mutex_lock(&xfrm4_protocol_mutex);
  214. for (pprev = proto_handlers(protocol);
  215. (t = rcu_dereference_protected(*pprev,
  216. lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
  217. pprev = &t->next) {
  218. if (t == handler) {
  219. *pprev = handler->next;
  220. ret = 0;
  221. break;
  222. }
  223. }
  224. if (!rcu_dereference_protected(*proto_handlers(protocol),
  225. lockdep_is_held(&xfrm4_protocol_mutex))) {
  226. if (inet_del_protocol(netproto(protocol), protocol) < 0) {
  227. pr_err("%s: can't remove protocol\n", __func__);
  228. ret = -EAGAIN;
  229. }
  230. }
  231. mutex_unlock(&xfrm4_protocol_mutex);
  232. synchronize_net();
  233. return ret;
  234. }
  235. EXPORT_SYMBOL(xfrm4_protocol_deregister);
  236. void __init xfrm4_protocol_init(void)
  237. {
  238. xfrm_input_register_afinfo(&xfrm4_input_afinfo);
  239. }
  240. EXPORT_SYMBOL(xfrm4_protocol_init);