xfrm6_tunnel.c 13 KB

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