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