xfrm6_tunnel.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/list.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. /*
  34. * xfrm_tunnel_spi things are for allocating unique id ("spi")
  35. * per xfrm_address_t.
  36. */
  37. struct xfrm6_tunnel_spi {
  38. struct hlist_node list_byaddr;
  39. struct hlist_node list_byspi;
  40. xfrm_address_t addr;
  41. u32 spi;
  42. atomic_t refcnt;
  43. };
  44. static DEFINE_RWLOCK(xfrm6_tunnel_spi_lock);
  45. static u32 xfrm6_tunnel_spi;
  46. #define XFRM6_TUNNEL_SPI_MIN 1
  47. #define XFRM6_TUNNEL_SPI_MAX 0xffffffff
  48. static struct kmem_cache *xfrm6_tunnel_spi_kmem __read_mostly;
  49. #define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
  50. #define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
  51. static struct hlist_head xfrm6_tunnel_spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE];
  52. static struct hlist_head xfrm6_tunnel_spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE];
  53. static inline unsigned xfrm6_tunnel_spi_hash_byaddr(xfrm_address_t *addr)
  54. {
  55. unsigned h;
  56. h = (__force u32)(addr->a6[0] ^ addr->a6[1] ^ addr->a6[2] ^ addr->a6[3]);
  57. h ^= h >> 16;
  58. h ^= h >> 8;
  59. h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1;
  60. return h;
  61. }
  62. static inline unsigned xfrm6_tunnel_spi_hash_byspi(u32 spi)
  63. {
  64. return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE;
  65. }
  66. static int xfrm6_tunnel_spi_init(void)
  67. {
  68. int i;
  69. xfrm6_tunnel_spi = 0;
  70. xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi",
  71. sizeof(struct xfrm6_tunnel_spi),
  72. 0, SLAB_HWCACHE_ALIGN,
  73. NULL);
  74. if (!xfrm6_tunnel_spi_kmem)
  75. return -ENOMEM;
  76. for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
  77. INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byaddr[i]);
  78. for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
  79. INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byspi[i]);
  80. return 0;
  81. }
  82. static void xfrm6_tunnel_spi_fini(void)
  83. {
  84. int i;
  85. for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) {
  86. if (!hlist_empty(&xfrm6_tunnel_spi_byaddr[i]))
  87. return;
  88. }
  89. for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++) {
  90. if (!hlist_empty(&xfrm6_tunnel_spi_byspi[i]))
  91. return;
  92. }
  93. kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
  94. xfrm6_tunnel_spi_kmem = NULL;
  95. }
  96. static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
  97. {
  98. struct xfrm6_tunnel_spi *x6spi;
  99. struct hlist_node *pos;
  100. hlist_for_each_entry(x6spi, pos,
  101. &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
  102. list_byaddr) {
  103. if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0)
  104. return x6spi;
  105. }
  106. return NULL;
  107. }
  108. __be32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
  109. {
  110. struct xfrm6_tunnel_spi *x6spi;
  111. u32 spi;
  112. read_lock_bh(&xfrm6_tunnel_spi_lock);
  113. x6spi = __xfrm6_tunnel_spi_lookup(saddr);
  114. spi = x6spi ? x6spi->spi : 0;
  115. read_unlock_bh(&xfrm6_tunnel_spi_lock);
  116. return htonl(spi);
  117. }
  118. EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup);
  119. static u32 __xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
  120. {
  121. u32 spi;
  122. struct xfrm6_tunnel_spi *x6spi;
  123. struct hlist_node *pos;
  124. unsigned index;
  125. if (xfrm6_tunnel_spi < XFRM6_TUNNEL_SPI_MIN ||
  126. xfrm6_tunnel_spi >= XFRM6_TUNNEL_SPI_MAX)
  127. xfrm6_tunnel_spi = XFRM6_TUNNEL_SPI_MIN;
  128. else
  129. xfrm6_tunnel_spi++;
  130. for (spi = xfrm6_tunnel_spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) {
  131. index = xfrm6_tunnel_spi_hash_byspi(spi);
  132. hlist_for_each_entry(x6spi, pos,
  133. &xfrm6_tunnel_spi_byspi[index],
  134. list_byspi) {
  135. if (x6spi->spi == spi)
  136. goto try_next_1;
  137. }
  138. xfrm6_tunnel_spi = spi;
  139. goto alloc_spi;
  140. try_next_1:;
  141. }
  142. for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tunnel_spi; spi++) {
  143. index = xfrm6_tunnel_spi_hash_byspi(spi);
  144. hlist_for_each_entry(x6spi, pos,
  145. &xfrm6_tunnel_spi_byspi[index],
  146. list_byspi) {
  147. if (x6spi->spi == spi)
  148. goto try_next_2;
  149. }
  150. xfrm6_tunnel_spi = spi;
  151. goto alloc_spi;
  152. try_next_2:;
  153. }
  154. spi = 0;
  155. goto out;
  156. alloc_spi:
  157. x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, GFP_ATOMIC);
  158. if (!x6spi)
  159. goto out;
  160. memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
  161. x6spi->spi = spi;
  162. atomic_set(&x6spi->refcnt, 1);
  163. hlist_add_head(&x6spi->list_byspi, &xfrm6_tunnel_spi_byspi[index]);
  164. index = xfrm6_tunnel_spi_hash_byaddr(saddr);
  165. hlist_add_head(&x6spi->list_byaddr, &xfrm6_tunnel_spi_byaddr[index]);
  166. out:
  167. return spi;
  168. }
  169. __be32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
  170. {
  171. struct xfrm6_tunnel_spi *x6spi;
  172. u32 spi;
  173. write_lock_bh(&xfrm6_tunnel_spi_lock);
  174. x6spi = __xfrm6_tunnel_spi_lookup(saddr);
  175. if (x6spi) {
  176. atomic_inc(&x6spi->refcnt);
  177. spi = x6spi->spi;
  178. } else
  179. spi = __xfrm6_tunnel_alloc_spi(saddr);
  180. write_unlock_bh(&xfrm6_tunnel_spi_lock);
  181. return htonl(spi);
  182. }
  183. EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
  184. void xfrm6_tunnel_free_spi(xfrm_address_t *saddr)
  185. {
  186. struct xfrm6_tunnel_spi *x6spi;
  187. struct hlist_node *pos, *n;
  188. write_lock_bh(&xfrm6_tunnel_spi_lock);
  189. hlist_for_each_entry_safe(x6spi, pos, n,
  190. &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
  191. list_byaddr)
  192. {
  193. if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) {
  194. if (atomic_dec_and_test(&x6spi->refcnt)) {
  195. hlist_del(&x6spi->list_byaddr);
  196. hlist_del(&x6spi->list_byspi);
  197. kmem_cache_free(xfrm6_tunnel_spi_kmem, x6spi);
  198. break;
  199. }
  200. }
  201. }
  202. write_unlock_bh(&xfrm6_tunnel_spi_lock);
  203. }
  204. EXPORT_SYMBOL(xfrm6_tunnel_free_spi);
  205. static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  206. {
  207. skb_push(skb, -skb_network_offset(skb));
  208. return 0;
  209. }
  210. static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  211. {
  212. return 0;
  213. }
  214. static int xfrm6_tunnel_rcv(struct sk_buff *skb)
  215. {
  216. struct ipv6hdr *iph = ipv6_hdr(skb);
  217. __be32 spi;
  218. spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&iph->saddr);
  219. return xfrm6_rcv_spi(skb, spi) > 0 ? : 0;
  220. }
  221. static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  222. int type, int code, int offset, __be32 info)
  223. {
  224. /* xfrm6_tunnel native err handling */
  225. switch (type) {
  226. case ICMPV6_DEST_UNREACH:
  227. switch (code) {
  228. case ICMPV6_NOROUTE:
  229. case ICMPV6_ADM_PROHIBITED:
  230. case ICMPV6_NOT_NEIGHBOUR:
  231. case ICMPV6_ADDR_UNREACH:
  232. case ICMPV6_PORT_UNREACH:
  233. default:
  234. break;
  235. }
  236. break;
  237. case ICMPV6_PKT_TOOBIG:
  238. break;
  239. case ICMPV6_TIME_EXCEED:
  240. switch (code) {
  241. case ICMPV6_EXC_HOPLIMIT:
  242. break;
  243. case ICMPV6_EXC_FRAGTIME:
  244. default:
  245. break;
  246. }
  247. break;
  248. case ICMPV6_PARAMPROB:
  249. switch (code) {
  250. case ICMPV6_HDR_FIELD: break;
  251. case ICMPV6_UNK_NEXTHDR: break;
  252. case ICMPV6_UNK_OPTION: break;
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. return 0;
  259. }
  260. static int xfrm6_tunnel_init_state(struct xfrm_state *x)
  261. {
  262. if (x->props.mode != XFRM_MODE_TUNNEL)
  263. return -EINVAL;
  264. if (x->encap)
  265. return -EINVAL;
  266. x->props.header_len = sizeof(struct ipv6hdr);
  267. return 0;
  268. }
  269. static void xfrm6_tunnel_destroy(struct xfrm_state *x)
  270. {
  271. xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
  272. }
  273. static struct xfrm_type xfrm6_tunnel_type = {
  274. .description = "IP6IP6",
  275. .owner = THIS_MODULE,
  276. .proto = IPPROTO_IPV6,
  277. .init_state = xfrm6_tunnel_init_state,
  278. .destructor = xfrm6_tunnel_destroy,
  279. .input = xfrm6_tunnel_input,
  280. .output = xfrm6_tunnel_output,
  281. };
  282. static struct xfrm6_tunnel xfrm6_tunnel_handler = {
  283. .handler = xfrm6_tunnel_rcv,
  284. .err_handler = xfrm6_tunnel_err,
  285. .priority = 2,
  286. };
  287. static struct xfrm6_tunnel xfrm46_tunnel_handler = {
  288. .handler = xfrm6_tunnel_rcv,
  289. .err_handler = xfrm6_tunnel_err,
  290. .priority = 2,
  291. };
  292. static int __init xfrm6_tunnel_init(void)
  293. {
  294. if (xfrm_register_type(&xfrm6_tunnel_type, AF_INET6) < 0)
  295. return -EAGAIN;
  296. if (xfrm6_tunnel_register(&xfrm6_tunnel_handler, AF_INET6)) {
  297. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  298. return -EAGAIN;
  299. }
  300. if (xfrm6_tunnel_register(&xfrm46_tunnel_handler, AF_INET)) {
  301. xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
  302. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  303. return -EAGAIN;
  304. }
  305. if (xfrm6_tunnel_spi_init() < 0) {
  306. xfrm6_tunnel_deregister(&xfrm46_tunnel_handler, AF_INET);
  307. xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
  308. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  309. return -EAGAIN;
  310. }
  311. return 0;
  312. }
  313. static void __exit xfrm6_tunnel_fini(void)
  314. {
  315. xfrm6_tunnel_spi_fini();
  316. xfrm6_tunnel_deregister(&xfrm46_tunnel_handler, AF_INET);
  317. xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
  318. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  319. }
  320. module_init(xfrm6_tunnel_init);
  321. module_exit(xfrm6_tunnel_fini);
  322. MODULE_LICENSE("GPL");
  323. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_IPV6);