xfrm6_tunnel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #ifdef CONFIG_IPV6_XFRM6_TUNNEL_DEBUG
  34. # define X6TDEBUG 3
  35. #else
  36. # define X6TDEBUG 1
  37. #endif
  38. #define X6TPRINTK(fmt, args...) printk(fmt, ## args)
  39. #define X6TNOPRINTK(fmt, args...) do { ; } while(0)
  40. #if X6TDEBUG >= 1
  41. # define X6TPRINTK1 X6TPRINTK
  42. #else
  43. # define X6TPRINTK1 X6TNOPRINTK
  44. #endif
  45. #if X6TDEBUG >= 3
  46. # define X6TPRINTK3 X6TPRINTK
  47. #else
  48. # define X6TPRINTK3 X6TNOPRINTK
  49. #endif
  50. /*
  51. * xfrm_tunnel_spi things are for allocating unique id ("spi")
  52. * per xfrm_address_t.
  53. */
  54. struct xfrm6_tunnel_spi {
  55. struct hlist_node list_byaddr;
  56. struct hlist_node list_byspi;
  57. xfrm_address_t addr;
  58. u32 spi;
  59. atomic_t refcnt;
  60. #ifdef XFRM6_TUNNEL_SPI_MAGIC
  61. u32 magic;
  62. #endif
  63. };
  64. #ifdef CONFIG_IPV6_XFRM6_TUNNEL_DEBUG
  65. # define XFRM6_TUNNEL_SPI_MAGIC 0xdeadbeef
  66. #endif
  67. static DEFINE_RWLOCK(xfrm6_tunnel_spi_lock);
  68. static u32 xfrm6_tunnel_spi;
  69. #define XFRM6_TUNNEL_SPI_MIN 1
  70. #define XFRM6_TUNNEL_SPI_MAX 0xffffffff
  71. static kmem_cache_t *xfrm6_tunnel_spi_kmem __read_mostly;
  72. #define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
  73. #define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
  74. static struct hlist_head xfrm6_tunnel_spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE];
  75. static struct hlist_head xfrm6_tunnel_spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE];
  76. #ifdef XFRM6_TUNNEL_SPI_MAGIC
  77. static int x6spi_check_magic(const struct xfrm6_tunnel_spi *x6spi,
  78. const char *name)
  79. {
  80. if (unlikely(x6spi->magic != XFRM6_TUNNEL_SPI_MAGIC)) {
  81. X6TPRINTK3(KERN_DEBUG "%s(): x6spi object "
  82. "at %p has corrupted magic %08x "
  83. "(should be %08x)\n",
  84. name, x6spi, x6spi->magic, XFRM6_TUNNEL_SPI_MAGIC);
  85. return -1;
  86. }
  87. return 0;
  88. }
  89. #else
  90. static int inline x6spi_check_magic(const struct xfrm6_tunnel_spi *x6spi,
  91. const char *name)
  92. {
  93. return 0;
  94. }
  95. #endif
  96. #define X6SPI_CHECK_MAGIC(x6spi) x6spi_check_magic((x6spi), __FUNCTION__)
  97. static unsigned inline xfrm6_tunnel_spi_hash_byaddr(xfrm_address_t *addr)
  98. {
  99. unsigned h;
  100. X6TPRINTK3(KERN_DEBUG "%s(addr=%p)\n", __FUNCTION__, addr);
  101. h = addr->a6[0] ^ addr->a6[1] ^ addr->a6[2] ^ addr->a6[3];
  102. h ^= h >> 16;
  103. h ^= h >> 8;
  104. h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1;
  105. X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, h);
  106. return h;
  107. }
  108. static unsigned inline xfrm6_tunnel_spi_hash_byspi(u32 spi)
  109. {
  110. return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE;
  111. }
  112. static int xfrm6_tunnel_spi_init(void)
  113. {
  114. int i;
  115. X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
  116. xfrm6_tunnel_spi = 0;
  117. xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi",
  118. sizeof(struct xfrm6_tunnel_spi),
  119. 0, SLAB_HWCACHE_ALIGN,
  120. NULL, NULL);
  121. if (!xfrm6_tunnel_spi_kmem) {
  122. X6TPRINTK1(KERN_ERR
  123. "%s(): failed to allocate xfrm6_tunnel_spi_kmem\n",
  124. __FUNCTION__);
  125. return -ENOMEM;
  126. }
  127. for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
  128. INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byaddr[i]);
  129. for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
  130. INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byspi[i]);
  131. return 0;
  132. }
  133. static void xfrm6_tunnel_spi_fini(void)
  134. {
  135. int i;
  136. X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
  137. for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) {
  138. if (!hlist_empty(&xfrm6_tunnel_spi_byaddr[i]))
  139. goto err;
  140. }
  141. for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++) {
  142. if (!hlist_empty(&xfrm6_tunnel_spi_byspi[i]))
  143. goto err;
  144. }
  145. kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
  146. xfrm6_tunnel_spi_kmem = NULL;
  147. return;
  148. err:
  149. X6TPRINTK1(KERN_ERR "%s(): table is not empty\n", __FUNCTION__);
  150. return;
  151. }
  152. static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
  153. {
  154. struct xfrm6_tunnel_spi *x6spi;
  155. struct hlist_node *pos;
  156. X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
  157. hlist_for_each_entry(x6spi, pos,
  158. &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
  159. list_byaddr) {
  160. if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) {
  161. X6SPI_CHECK_MAGIC(x6spi);
  162. X6TPRINTK3(KERN_DEBUG "%s() = %p(%u)\n", __FUNCTION__, x6spi, x6spi->spi);
  163. return x6spi;
  164. }
  165. }
  166. X6TPRINTK3(KERN_DEBUG "%s() = NULL(0)\n", __FUNCTION__);
  167. return NULL;
  168. }
  169. u32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
  170. {
  171. struct xfrm6_tunnel_spi *x6spi;
  172. u32 spi;
  173. X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
  174. read_lock_bh(&xfrm6_tunnel_spi_lock);
  175. x6spi = __xfrm6_tunnel_spi_lookup(saddr);
  176. spi = x6spi ? x6spi->spi : 0;
  177. read_unlock_bh(&xfrm6_tunnel_spi_lock);
  178. return spi;
  179. }
  180. EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup);
  181. static u32 __xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
  182. {
  183. u32 spi;
  184. struct xfrm6_tunnel_spi *x6spi;
  185. struct hlist_node *pos;
  186. unsigned index;
  187. X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
  188. if (xfrm6_tunnel_spi < XFRM6_TUNNEL_SPI_MIN ||
  189. xfrm6_tunnel_spi >= XFRM6_TUNNEL_SPI_MAX)
  190. xfrm6_tunnel_spi = XFRM6_TUNNEL_SPI_MIN;
  191. else
  192. xfrm6_tunnel_spi++;
  193. for (spi = xfrm6_tunnel_spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) {
  194. index = xfrm6_tunnel_spi_hash_byspi(spi);
  195. hlist_for_each_entry(x6spi, pos,
  196. &xfrm6_tunnel_spi_byspi[index],
  197. list_byspi) {
  198. if (x6spi->spi == spi)
  199. goto try_next_1;
  200. }
  201. xfrm6_tunnel_spi = spi;
  202. goto alloc_spi;
  203. try_next_1:;
  204. }
  205. for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tunnel_spi; spi++) {
  206. index = xfrm6_tunnel_spi_hash_byspi(spi);
  207. hlist_for_each_entry(x6spi, pos,
  208. &xfrm6_tunnel_spi_byspi[index],
  209. list_byspi) {
  210. if (x6spi->spi == spi)
  211. goto try_next_2;
  212. }
  213. xfrm6_tunnel_spi = spi;
  214. goto alloc_spi;
  215. try_next_2:;
  216. }
  217. spi = 0;
  218. goto out;
  219. alloc_spi:
  220. X6TPRINTK3(KERN_DEBUG "%s(): allocate new spi for " NIP6_FMT "\n",
  221. __FUNCTION__,
  222. NIP6(*(struct in6_addr *)saddr));
  223. x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, SLAB_ATOMIC);
  224. if (!x6spi) {
  225. X6TPRINTK1(KERN_ERR "%s(): kmem_cache_alloc() failed\n",
  226. __FUNCTION__);
  227. goto out;
  228. }
  229. #ifdef XFRM6_TUNNEL_SPI_MAGIC
  230. x6spi->magic = XFRM6_TUNNEL_SPI_MAGIC;
  231. #endif
  232. memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
  233. x6spi->spi = spi;
  234. atomic_set(&x6spi->refcnt, 1);
  235. hlist_add_head(&x6spi->list_byspi, &xfrm6_tunnel_spi_byspi[index]);
  236. index = xfrm6_tunnel_spi_hash_byaddr(saddr);
  237. hlist_add_head(&x6spi->list_byaddr, &xfrm6_tunnel_spi_byaddr[index]);
  238. X6SPI_CHECK_MAGIC(x6spi);
  239. out:
  240. X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
  241. return spi;
  242. }
  243. u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
  244. {
  245. struct xfrm6_tunnel_spi *x6spi;
  246. u32 spi;
  247. X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
  248. write_lock_bh(&xfrm6_tunnel_spi_lock);
  249. x6spi = __xfrm6_tunnel_spi_lookup(saddr);
  250. if (x6spi) {
  251. atomic_inc(&x6spi->refcnt);
  252. spi = x6spi->spi;
  253. } else
  254. spi = __xfrm6_tunnel_alloc_spi(saddr);
  255. write_unlock_bh(&xfrm6_tunnel_spi_lock);
  256. X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
  257. return spi;
  258. }
  259. EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
  260. void xfrm6_tunnel_free_spi(xfrm_address_t *saddr)
  261. {
  262. struct xfrm6_tunnel_spi *x6spi;
  263. struct hlist_node *pos, *n;
  264. X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
  265. write_lock_bh(&xfrm6_tunnel_spi_lock);
  266. hlist_for_each_entry_safe(x6spi, pos, n,
  267. &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
  268. list_byaddr)
  269. {
  270. if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) {
  271. X6TPRINTK3(KERN_DEBUG "%s(): x6spi object for " NIP6_FMT
  272. " found at %p\n",
  273. __FUNCTION__,
  274. NIP6(*(struct in6_addr *)saddr),
  275. x6spi);
  276. X6SPI_CHECK_MAGIC(x6spi);
  277. if (atomic_dec_and_test(&x6spi->refcnt)) {
  278. hlist_del(&x6spi->list_byaddr);
  279. hlist_del(&x6spi->list_byspi);
  280. kmem_cache_free(xfrm6_tunnel_spi_kmem, x6spi);
  281. break;
  282. }
  283. }
  284. }
  285. write_unlock_bh(&xfrm6_tunnel_spi_lock);
  286. }
  287. EXPORT_SYMBOL(xfrm6_tunnel_free_spi);
  288. static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  289. {
  290. struct ipv6hdr *top_iph;
  291. top_iph = (struct ipv6hdr *)skb->data;
  292. top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  293. return 0;
  294. }
  295. static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  296. {
  297. return 0;
  298. }
  299. static int xfrm6_tunnel_rcv(struct sk_buff *skb)
  300. {
  301. struct ipv6hdr *iph = skb->nh.ipv6h;
  302. u32 spi;
  303. spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&iph->saddr);
  304. return xfrm6_rcv_spi(skb, spi);
  305. }
  306. static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  307. int type, int code, int offset, __u32 info)
  308. {
  309. /* xfrm6_tunnel native err handling */
  310. switch (type) {
  311. case ICMPV6_DEST_UNREACH:
  312. switch (code) {
  313. case ICMPV6_NOROUTE:
  314. case ICMPV6_ADM_PROHIBITED:
  315. case ICMPV6_NOT_NEIGHBOUR:
  316. case ICMPV6_ADDR_UNREACH:
  317. case ICMPV6_PORT_UNREACH:
  318. default:
  319. X6TPRINTK3(KERN_DEBUG
  320. "xfrm6_tunnel: Destination Unreach.\n");
  321. break;
  322. }
  323. break;
  324. case ICMPV6_PKT_TOOBIG:
  325. X6TPRINTK3(KERN_DEBUG
  326. "xfrm6_tunnel: Packet Too Big.\n");
  327. break;
  328. case ICMPV6_TIME_EXCEED:
  329. switch (code) {
  330. case ICMPV6_EXC_HOPLIMIT:
  331. X6TPRINTK3(KERN_DEBUG
  332. "xfrm6_tunnel: Too small Hoplimit.\n");
  333. break;
  334. case ICMPV6_EXC_FRAGTIME:
  335. default:
  336. break;
  337. }
  338. break;
  339. case ICMPV6_PARAMPROB:
  340. switch (code) {
  341. case ICMPV6_HDR_FIELD: break;
  342. case ICMPV6_UNK_NEXTHDR: break;
  343. case ICMPV6_UNK_OPTION: break;
  344. }
  345. break;
  346. default:
  347. break;
  348. }
  349. return 0;
  350. }
  351. static int xfrm6_tunnel_init_state(struct xfrm_state *x)
  352. {
  353. if (!x->props.mode)
  354. return -EINVAL;
  355. if (x->encap)
  356. return -EINVAL;
  357. x->props.header_len = sizeof(struct ipv6hdr);
  358. return 0;
  359. }
  360. static void xfrm6_tunnel_destroy(struct xfrm_state *x)
  361. {
  362. xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
  363. }
  364. static struct xfrm_type xfrm6_tunnel_type = {
  365. .description = "IP6IP6",
  366. .owner = THIS_MODULE,
  367. .proto = IPPROTO_IPV6,
  368. .init_state = xfrm6_tunnel_init_state,
  369. .destructor = xfrm6_tunnel_destroy,
  370. .input = xfrm6_tunnel_input,
  371. .output = xfrm6_tunnel_output,
  372. };
  373. static struct xfrm6_tunnel xfrm6_tunnel_handler = {
  374. .handler = xfrm6_tunnel_rcv,
  375. .err_handler = xfrm6_tunnel_err,
  376. .priority = 2,
  377. };
  378. static int __init xfrm6_tunnel_init(void)
  379. {
  380. X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
  381. if (xfrm_register_type(&xfrm6_tunnel_type, AF_INET6) < 0) {
  382. X6TPRINTK1(KERN_ERR
  383. "xfrm6_tunnel init: can't add xfrm type\n");
  384. return -EAGAIN;
  385. }
  386. if (xfrm6_tunnel_register(&xfrm6_tunnel_handler)) {
  387. X6TPRINTK1(KERN_ERR
  388. "xfrm6_tunnel init(): can't add handler\n");
  389. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  390. return -EAGAIN;
  391. }
  392. if (xfrm6_tunnel_spi_init() < 0) {
  393. X6TPRINTK1(KERN_ERR
  394. "xfrm6_tunnel init: failed to initialize spi\n");
  395. xfrm6_tunnel_deregister(&xfrm6_tunnel_handler);
  396. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  397. return -EAGAIN;
  398. }
  399. return 0;
  400. }
  401. static void __exit xfrm6_tunnel_fini(void)
  402. {
  403. X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
  404. xfrm6_tunnel_spi_fini();
  405. if (xfrm6_tunnel_deregister(&xfrm6_tunnel_handler))
  406. X6TPRINTK1(KERN_ERR
  407. "xfrm6_tunnel close: can't remove handler\n");
  408. if (xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6) < 0)
  409. X6TPRINTK1(KERN_ERR
  410. "xfrm6_tunnel close: can't remove xfrm type\n");
  411. }
  412. module_init(xfrm6_tunnel_init);
  413. module_exit(xfrm6_tunnel_fini);
  414. MODULE_LICENSE("GPL");