xfrm6_tunnel.c 9.8 KB

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