xfrm6_tunnel.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C)2003,2004 USAGI/WIDE Project
  4. *
  5. * Authors Mitsuru KANDA <mk@linux-ipv6.org>
  6. * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
  7. *
  8. * Based on net/ipv4/xfrm4_tunnel.c
  9. */
  10. #include <linux/module.h>
  11. #include <linux/xfrm.h>
  12. #include <linux/slab.h>
  13. #include <linux/rculist.h>
  14. #include <net/ip.h>
  15. #include <net/xfrm.h>
  16. #include <net/ipv6.h>
  17. #include <linux/ipv6.h>
  18. #include <linux/icmpv6.h>
  19. #include <linux/mutex.h>
  20. #include <net/netns/generic.h>
  21. #define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
  22. #define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
  23. #define XFRM6_TUNNEL_SPI_MIN 1
  24. #define XFRM6_TUNNEL_SPI_MAX 0xffffffff
  25. struct xfrm6_tunnel_net {
  26. struct hlist_head spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE];
  27. struct hlist_head spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE];
  28. u32 spi;
  29. };
  30. static unsigned int xfrm6_tunnel_net_id __read_mostly;
  31. static inline struct xfrm6_tunnel_net *xfrm6_tunnel_pernet(struct net *net)
  32. {
  33. return net_generic(net, xfrm6_tunnel_net_id);
  34. }
  35. /*
  36. * xfrm_tunnel_spi things are for allocating unique id ("spi")
  37. * per xfrm_address_t.
  38. */
  39. struct xfrm6_tunnel_spi {
  40. struct hlist_node list_byaddr;
  41. struct hlist_node list_byspi;
  42. xfrm_address_t addr;
  43. u32 spi;
  44. refcount_t refcnt;
  45. struct rcu_head rcu_head;
  46. };
  47. static DEFINE_SPINLOCK(xfrm6_tunnel_spi_lock);
  48. static struct kmem_cache *xfrm6_tunnel_spi_kmem __read_mostly;
  49. static inline unsigned int xfrm6_tunnel_spi_hash_byaddr(const xfrm_address_t *addr)
  50. {
  51. unsigned int h;
  52. h = ipv6_addr_hash((const struct in6_addr *)addr);
  53. h ^= h >> 16;
  54. h ^= h >> 8;
  55. h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1;
  56. return h;
  57. }
  58. static inline unsigned int xfrm6_tunnel_spi_hash_byspi(u32 spi)
  59. {
  60. return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE;
  61. }
  62. static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr)
  63. {
  64. struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
  65. struct xfrm6_tunnel_spi *x6spi;
  66. hlist_for_each_entry_rcu(x6spi,
  67. &xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
  68. list_byaddr, lockdep_is_held(&xfrm6_tunnel_spi_lock)) {
  69. if (xfrm6_addr_equal(&x6spi->addr, saddr))
  70. return x6spi;
  71. }
  72. return NULL;
  73. }
  74. __be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr)
  75. {
  76. struct xfrm6_tunnel_spi *x6spi;
  77. u32 spi;
  78. rcu_read_lock_bh();
  79. x6spi = __xfrm6_tunnel_spi_lookup(net, saddr);
  80. spi = x6spi ? x6spi->spi : 0;
  81. rcu_read_unlock_bh();
  82. return htonl(spi);
  83. }
  84. EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup);
  85. static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi)
  86. {
  87. struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
  88. struct xfrm6_tunnel_spi *x6spi;
  89. int index = xfrm6_tunnel_spi_hash_byspi(spi);
  90. hlist_for_each_entry(x6spi,
  91. &xfrm6_tn->spi_byspi[index],
  92. list_byspi) {
  93. if (x6spi->spi == spi)
  94. return -1;
  95. }
  96. return index;
  97. }
  98. static u32 __xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
  99. {
  100. struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
  101. u32 spi;
  102. struct xfrm6_tunnel_spi *x6spi;
  103. int index;
  104. if (xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN ||
  105. xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX)
  106. xfrm6_tn->spi = XFRM6_TUNNEL_SPI_MIN;
  107. else
  108. xfrm6_tn->spi++;
  109. for (spi = xfrm6_tn->spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) {
  110. index = __xfrm6_tunnel_spi_check(net, spi);
  111. if (index >= 0)
  112. goto alloc_spi;
  113. if (spi == XFRM6_TUNNEL_SPI_MAX)
  114. break;
  115. }
  116. for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tn->spi; spi++) {
  117. index = __xfrm6_tunnel_spi_check(net, spi);
  118. if (index >= 0)
  119. goto alloc_spi;
  120. }
  121. spi = 0;
  122. goto out;
  123. alloc_spi:
  124. xfrm6_tn->spi = spi;
  125. x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, GFP_ATOMIC);
  126. if (!x6spi)
  127. goto out;
  128. memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
  129. x6spi->spi = spi;
  130. refcount_set(&x6spi->refcnt, 1);
  131. hlist_add_head_rcu(&x6spi->list_byspi, &xfrm6_tn->spi_byspi[index]);
  132. index = xfrm6_tunnel_spi_hash_byaddr(saddr);
  133. hlist_add_head_rcu(&x6spi->list_byaddr, &xfrm6_tn->spi_byaddr[index]);
  134. out:
  135. return spi;
  136. }
  137. __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
  138. {
  139. struct xfrm6_tunnel_spi *x6spi;
  140. u32 spi;
  141. spin_lock_bh(&xfrm6_tunnel_spi_lock);
  142. x6spi = __xfrm6_tunnel_spi_lookup(net, saddr);
  143. if (x6spi) {
  144. refcount_inc(&x6spi->refcnt);
  145. spi = x6spi->spi;
  146. } else
  147. spi = __xfrm6_tunnel_alloc_spi(net, saddr);
  148. spin_unlock_bh(&xfrm6_tunnel_spi_lock);
  149. return htonl(spi);
  150. }
  151. EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
  152. static void x6spi_destroy_rcu(struct rcu_head *head)
  153. {
  154. kmem_cache_free(xfrm6_tunnel_spi_kmem,
  155. container_of(head, struct xfrm6_tunnel_spi, rcu_head));
  156. }
  157. static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr)
  158. {
  159. struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
  160. struct xfrm6_tunnel_spi *x6spi;
  161. struct hlist_node *n;
  162. spin_lock_bh(&xfrm6_tunnel_spi_lock);
  163. hlist_for_each_entry_safe(x6spi, n,
  164. &xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
  165. list_byaddr)
  166. {
  167. if (xfrm6_addr_equal(&x6spi->addr, saddr)) {
  168. if (refcount_dec_and_test(&x6spi->refcnt)) {
  169. hlist_del_rcu(&x6spi->list_byaddr);
  170. hlist_del_rcu(&x6spi->list_byspi);
  171. call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu);
  172. break;
  173. }
  174. }
  175. }
  176. spin_unlock_bh(&xfrm6_tunnel_spi_lock);
  177. }
  178. static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  179. {
  180. skb_push(skb, -skb_network_offset(skb));
  181. return 0;
  182. }
  183. static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  184. {
  185. return skb_network_header(skb)[IP6CB(skb)->nhoff];
  186. }
  187. static int xfrm6_tunnel_rcv(struct sk_buff *skb)
  188. {
  189. struct net *net = dev_net(skb->dev);
  190. const struct ipv6hdr *iph = ipv6_hdr(skb);
  191. __be32 spi;
  192. spi = xfrm6_tunnel_spi_lookup(net, (const xfrm_address_t *)&iph->saddr);
  193. return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL);
  194. }
  195. static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  196. u8 type, u8 code, int offset, __be32 info)
  197. {
  198. /* xfrm6_tunnel native err handling */
  199. switch (type) {
  200. case ICMPV6_DEST_UNREACH:
  201. switch (code) {
  202. case ICMPV6_NOROUTE:
  203. case ICMPV6_ADM_PROHIBITED:
  204. case ICMPV6_NOT_NEIGHBOUR:
  205. case ICMPV6_ADDR_UNREACH:
  206. case ICMPV6_PORT_UNREACH:
  207. default:
  208. break;
  209. }
  210. break;
  211. case ICMPV6_PKT_TOOBIG:
  212. break;
  213. case ICMPV6_TIME_EXCEED:
  214. switch (code) {
  215. case ICMPV6_EXC_HOPLIMIT:
  216. break;
  217. case ICMPV6_EXC_FRAGTIME:
  218. default:
  219. break;
  220. }
  221. break;
  222. case ICMPV6_PARAMPROB:
  223. switch (code) {
  224. case ICMPV6_HDR_FIELD: break;
  225. case ICMPV6_UNK_NEXTHDR: break;
  226. case ICMPV6_UNK_OPTION: break;
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. return 0;
  233. }
  234. static int xfrm6_tunnel_init_state(struct xfrm_state *x)
  235. {
  236. if (x->props.mode != XFRM_MODE_TUNNEL)
  237. return -EINVAL;
  238. if (x->encap)
  239. return -EINVAL;
  240. x->props.header_len = sizeof(struct ipv6hdr);
  241. return 0;
  242. }
  243. static void xfrm6_tunnel_destroy(struct xfrm_state *x)
  244. {
  245. struct net *net = xs_net(x);
  246. xfrm6_tunnel_free_spi(net, (xfrm_address_t *)&x->props.saddr);
  247. }
  248. static const struct xfrm_type xfrm6_tunnel_type = {
  249. .description = "IP6IP6",
  250. .owner = THIS_MODULE,
  251. .proto = IPPROTO_IPV6,
  252. .init_state = xfrm6_tunnel_init_state,
  253. .destructor = xfrm6_tunnel_destroy,
  254. .input = xfrm6_tunnel_input,
  255. .output = xfrm6_tunnel_output,
  256. };
  257. static struct xfrm6_tunnel xfrm6_tunnel_handler __read_mostly = {
  258. .handler = xfrm6_tunnel_rcv,
  259. .err_handler = xfrm6_tunnel_err,
  260. .priority = 3,
  261. };
  262. static struct xfrm6_tunnel xfrm46_tunnel_handler __read_mostly = {
  263. .handler = xfrm6_tunnel_rcv,
  264. .err_handler = xfrm6_tunnel_err,
  265. .priority = 3,
  266. };
  267. static int __net_init xfrm6_tunnel_net_init(struct net *net)
  268. {
  269. struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
  270. unsigned int i;
  271. for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
  272. INIT_HLIST_HEAD(&xfrm6_tn->spi_byaddr[i]);
  273. for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
  274. INIT_HLIST_HEAD(&xfrm6_tn->spi_byspi[i]);
  275. xfrm6_tn->spi = 0;
  276. return 0;
  277. }
  278. static void __net_exit xfrm6_tunnel_net_exit(struct net *net)
  279. {
  280. struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
  281. unsigned int i;
  282. xfrm_flush_gc();
  283. xfrm_state_flush(net, 0, false, true);
  284. for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
  285. WARN_ON_ONCE(!hlist_empty(&xfrm6_tn->spi_byaddr[i]));
  286. for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
  287. WARN_ON_ONCE(!hlist_empty(&xfrm6_tn->spi_byspi[i]));
  288. }
  289. static struct pernet_operations xfrm6_tunnel_net_ops = {
  290. .init = xfrm6_tunnel_net_init,
  291. .exit = xfrm6_tunnel_net_exit,
  292. .id = &xfrm6_tunnel_net_id,
  293. .size = sizeof(struct xfrm6_tunnel_net),
  294. };
  295. static int __init xfrm6_tunnel_init(void)
  296. {
  297. int rv;
  298. xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi",
  299. sizeof(struct xfrm6_tunnel_spi),
  300. 0, SLAB_HWCACHE_ALIGN,
  301. NULL);
  302. if (!xfrm6_tunnel_spi_kmem)
  303. return -ENOMEM;
  304. rv = register_pernet_subsys(&xfrm6_tunnel_net_ops);
  305. if (rv < 0)
  306. goto out_pernet;
  307. rv = xfrm_register_type(&xfrm6_tunnel_type, AF_INET6);
  308. if (rv < 0)
  309. goto out_type;
  310. rv = xfrm6_tunnel_register(&xfrm6_tunnel_handler, AF_INET6);
  311. if (rv < 0)
  312. goto out_xfrm6;
  313. rv = xfrm6_tunnel_register(&xfrm46_tunnel_handler, AF_INET);
  314. if (rv < 0)
  315. goto out_xfrm46;
  316. return 0;
  317. out_xfrm46:
  318. xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
  319. out_xfrm6:
  320. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  321. out_type:
  322. unregister_pernet_subsys(&xfrm6_tunnel_net_ops);
  323. out_pernet:
  324. kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
  325. return rv;
  326. }
  327. static void __exit xfrm6_tunnel_fini(void)
  328. {
  329. xfrm6_tunnel_deregister(&xfrm46_tunnel_handler, AF_INET);
  330. xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
  331. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  332. unregister_pernet_subsys(&xfrm6_tunnel_net_ops);
  333. /* Someone maybe has gotten the xfrm6_tunnel_spi.
  334. * So need to wait it.
  335. */
  336. rcu_barrier();
  337. kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
  338. }
  339. module_init(xfrm6_tunnel_init);
  340. module_exit(xfrm6_tunnel_fini);
  341. MODULE_LICENSE("GPL");
  342. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_IPV6);