xfrm4_protocol.c 6.4 KB

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