xfrm4_protocol.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 const struct xfrm_input_afinfo xfrm4_input_afinfo = {
  152. .family = AF_INET,
  153. .callback = xfrm4_rcv_cb,
  154. };
  155. static inline const struct net_protocol *netproto(unsigned char protocol)
  156. {
  157. switch (protocol) {
  158. case IPPROTO_ESP:
  159. return &esp4_protocol;
  160. case IPPROTO_AH:
  161. return &ah4_protocol;
  162. case IPPROTO_COMP:
  163. return &ipcomp4_protocol;
  164. }
  165. return NULL;
  166. }
  167. int xfrm4_protocol_register(struct xfrm4_protocol *handler,
  168. unsigned char protocol)
  169. {
  170. struct xfrm4_protocol __rcu **pprev;
  171. struct xfrm4_protocol *t;
  172. bool add_netproto = false;
  173. int ret = -EEXIST;
  174. int priority = handler->priority;
  175. if (!proto_handlers(protocol) || !netproto(protocol))
  176. return -EINVAL;
  177. mutex_lock(&xfrm4_protocol_mutex);
  178. if (!rcu_dereference_protected(*proto_handlers(protocol),
  179. lockdep_is_held(&xfrm4_protocol_mutex)))
  180. add_netproto = true;
  181. for (pprev = proto_handlers(protocol);
  182. (t = rcu_dereference_protected(*pprev,
  183. lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
  184. pprev = &t->next) {
  185. if (t->priority < priority)
  186. break;
  187. if (t->priority == priority)
  188. goto err;
  189. }
  190. handler->next = *pprev;
  191. rcu_assign_pointer(*pprev, handler);
  192. ret = 0;
  193. err:
  194. mutex_unlock(&xfrm4_protocol_mutex);
  195. if (add_netproto) {
  196. if (inet_add_protocol(netproto(protocol), protocol)) {
  197. pr_err("%s: can't add protocol\n", __func__);
  198. ret = -EAGAIN;
  199. }
  200. }
  201. return ret;
  202. }
  203. EXPORT_SYMBOL(xfrm4_protocol_register);
  204. int xfrm4_protocol_deregister(struct xfrm4_protocol *handler,
  205. unsigned char protocol)
  206. {
  207. struct xfrm4_protocol __rcu **pprev;
  208. struct xfrm4_protocol *t;
  209. int ret = -ENOENT;
  210. if (!proto_handlers(protocol) || !netproto(protocol))
  211. return -EINVAL;
  212. mutex_lock(&xfrm4_protocol_mutex);
  213. for (pprev = proto_handlers(protocol);
  214. (t = rcu_dereference_protected(*pprev,
  215. lockdep_is_held(&xfrm4_protocol_mutex))) != NULL;
  216. pprev = &t->next) {
  217. if (t == handler) {
  218. *pprev = handler->next;
  219. ret = 0;
  220. break;
  221. }
  222. }
  223. if (!rcu_dereference_protected(*proto_handlers(protocol),
  224. lockdep_is_held(&xfrm4_protocol_mutex))) {
  225. if (inet_del_protocol(netproto(protocol), protocol) < 0) {
  226. pr_err("%s: can't remove protocol\n", __func__);
  227. ret = -EAGAIN;
  228. }
  229. }
  230. mutex_unlock(&xfrm4_protocol_mutex);
  231. synchronize_net();
  232. return ret;
  233. }
  234. EXPORT_SYMBOL(xfrm4_protocol_deregister);
  235. void __init xfrm4_protocol_init(void)
  236. {
  237. xfrm_input_register_afinfo(&xfrm4_input_afinfo);
  238. }
  239. EXPORT_SYMBOL(xfrm4_protocol_init);