xfrm6_tunnel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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;
  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 "
  222. "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  223. __FUNCTION__,
  224. NIP6(*(struct in6_addr *)saddr));
  225. x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, SLAB_ATOMIC);
  226. if (!x6spi) {
  227. X6TPRINTK1(KERN_ERR "%s(): kmem_cache_alloc() failed\n",
  228. __FUNCTION__);
  229. goto out;
  230. }
  231. #ifdef XFRM6_TUNNEL_SPI_MAGIC
  232. x6spi->magic = XFRM6_TUNNEL_SPI_MAGIC;
  233. #endif
  234. memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
  235. x6spi->spi = spi;
  236. atomic_set(&x6spi->refcnt, 1);
  237. hlist_add_head(&x6spi->list_byspi, &xfrm6_tunnel_spi_byspi[index]);
  238. index = xfrm6_tunnel_spi_hash_byaddr(saddr);
  239. hlist_add_head(&x6spi->list_byaddr, &xfrm6_tunnel_spi_byaddr[index]);
  240. X6SPI_CHECK_MAGIC(x6spi);
  241. out:
  242. X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
  243. return spi;
  244. }
  245. u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
  246. {
  247. struct xfrm6_tunnel_spi *x6spi;
  248. u32 spi;
  249. X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
  250. write_lock_bh(&xfrm6_tunnel_spi_lock);
  251. x6spi = __xfrm6_tunnel_spi_lookup(saddr);
  252. if (x6spi) {
  253. atomic_inc(&x6spi->refcnt);
  254. spi = x6spi->spi;
  255. } else
  256. spi = __xfrm6_tunnel_alloc_spi(saddr);
  257. write_unlock_bh(&xfrm6_tunnel_spi_lock);
  258. X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
  259. return spi;
  260. }
  261. EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
  262. void xfrm6_tunnel_free_spi(xfrm_address_t *saddr)
  263. {
  264. struct xfrm6_tunnel_spi *x6spi;
  265. struct hlist_node *pos, *n;
  266. X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
  267. write_lock_bh(&xfrm6_tunnel_spi_lock);
  268. hlist_for_each_entry_safe(x6spi, pos, n,
  269. &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
  270. list_byaddr)
  271. {
  272. if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) {
  273. X6TPRINTK3(KERN_DEBUG "%s(): x6spi object "
  274. "for %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
  275. "found at %p\n",
  276. __FUNCTION__,
  277. NIP6(*(struct in6_addr *)saddr),
  278. x6spi);
  279. X6SPI_CHECK_MAGIC(x6spi);
  280. if (atomic_dec_and_test(&x6spi->refcnt)) {
  281. hlist_del(&x6spi->list_byaddr);
  282. hlist_del(&x6spi->list_byspi);
  283. kmem_cache_free(xfrm6_tunnel_spi_kmem, x6spi);
  284. break;
  285. }
  286. }
  287. }
  288. write_unlock_bh(&xfrm6_tunnel_spi_lock);
  289. }
  290. EXPORT_SYMBOL(xfrm6_tunnel_free_spi);
  291. static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  292. {
  293. struct ipv6hdr *top_iph;
  294. top_iph = (struct ipv6hdr *)skb->data;
  295. top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  296. return 0;
  297. }
  298. static int xfrm6_tunnel_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
  299. {
  300. return 0;
  301. }
  302. static struct xfrm6_tunnel *xfrm6_tunnel_handler;
  303. static DECLARE_MUTEX(xfrm6_tunnel_sem);
  304. int xfrm6_tunnel_register(struct xfrm6_tunnel *handler)
  305. {
  306. int ret;
  307. down(&xfrm6_tunnel_sem);
  308. ret = 0;
  309. if (xfrm6_tunnel_handler != NULL)
  310. ret = -EINVAL;
  311. if (!ret)
  312. xfrm6_tunnel_handler = handler;
  313. up(&xfrm6_tunnel_sem);
  314. return ret;
  315. }
  316. EXPORT_SYMBOL(xfrm6_tunnel_register);
  317. int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler)
  318. {
  319. int ret;
  320. down(&xfrm6_tunnel_sem);
  321. ret = 0;
  322. if (xfrm6_tunnel_handler != handler)
  323. ret = -EINVAL;
  324. if (!ret)
  325. xfrm6_tunnel_handler = NULL;
  326. up(&xfrm6_tunnel_sem);
  327. synchronize_net();
  328. return ret;
  329. }
  330. EXPORT_SYMBOL(xfrm6_tunnel_deregister);
  331. static int xfrm6_tunnel_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
  332. {
  333. struct sk_buff *skb = *pskb;
  334. struct xfrm6_tunnel *handler = xfrm6_tunnel_handler;
  335. struct ipv6hdr *iph = skb->nh.ipv6h;
  336. u32 spi;
  337. /* device-like_ip6ip6_handler() */
  338. if (handler && handler->handler(pskb, nhoffp) == 0)
  339. return 0;
  340. spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&iph->saddr);
  341. return xfrm6_rcv_spi(pskb, nhoffp, spi);
  342. }
  343. static void xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  344. int type, int code, int offset, __u32 info)
  345. {
  346. struct xfrm6_tunnel *handler = xfrm6_tunnel_handler;
  347. /* call here first for device-like ip6ip6 err handling */
  348. if (handler) {
  349. handler->err_handler(skb, opt, type, code, offset, info);
  350. return;
  351. }
  352. /* xfrm6_tunnel native err handling */
  353. switch (type) {
  354. case ICMPV6_DEST_UNREACH:
  355. switch (code) {
  356. case ICMPV6_NOROUTE:
  357. case ICMPV6_ADM_PROHIBITED:
  358. case ICMPV6_NOT_NEIGHBOUR:
  359. case ICMPV6_ADDR_UNREACH:
  360. case ICMPV6_PORT_UNREACH:
  361. default:
  362. X6TPRINTK3(KERN_DEBUG
  363. "xfrm6_tunnel: Destination Unreach.\n");
  364. break;
  365. }
  366. break;
  367. case ICMPV6_PKT_TOOBIG:
  368. X6TPRINTK3(KERN_DEBUG
  369. "xfrm6_tunnel: Packet Too Big.\n");
  370. break;
  371. case ICMPV6_TIME_EXCEED:
  372. switch (code) {
  373. case ICMPV6_EXC_HOPLIMIT:
  374. X6TPRINTK3(KERN_DEBUG
  375. "xfrm6_tunnel: Too small Hoplimit.\n");
  376. break;
  377. case ICMPV6_EXC_FRAGTIME:
  378. default:
  379. break;
  380. }
  381. break;
  382. case ICMPV6_PARAMPROB:
  383. switch (code) {
  384. case ICMPV6_HDR_FIELD: break;
  385. case ICMPV6_UNK_NEXTHDR: break;
  386. case ICMPV6_UNK_OPTION: break;
  387. }
  388. break;
  389. default:
  390. break;
  391. }
  392. return;
  393. }
  394. static int xfrm6_tunnel_init_state(struct xfrm_state *x)
  395. {
  396. if (!x->props.mode)
  397. return -EINVAL;
  398. if (x->encap)
  399. return -EINVAL;
  400. x->props.header_len = sizeof(struct ipv6hdr);
  401. return 0;
  402. }
  403. static void xfrm6_tunnel_destroy(struct xfrm_state *x)
  404. {
  405. xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
  406. }
  407. static struct xfrm_type xfrm6_tunnel_type = {
  408. .description = "IP6IP6",
  409. .owner = THIS_MODULE,
  410. .proto = IPPROTO_IPV6,
  411. .init_state = xfrm6_tunnel_init_state,
  412. .destructor = xfrm6_tunnel_destroy,
  413. .input = xfrm6_tunnel_input,
  414. .output = xfrm6_tunnel_output,
  415. };
  416. static struct inet6_protocol xfrm6_tunnel_protocol = {
  417. .handler = xfrm6_tunnel_rcv,
  418. .err_handler = xfrm6_tunnel_err,
  419. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  420. };
  421. static int __init xfrm6_tunnel_init(void)
  422. {
  423. X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
  424. if (xfrm_register_type(&xfrm6_tunnel_type, AF_INET6) < 0) {
  425. X6TPRINTK1(KERN_ERR
  426. "xfrm6_tunnel init: can't add xfrm type\n");
  427. return -EAGAIN;
  428. }
  429. if (inet6_add_protocol(&xfrm6_tunnel_protocol, IPPROTO_IPV6) < 0) {
  430. X6TPRINTK1(KERN_ERR
  431. "xfrm6_tunnel init(): can't add protocol\n");
  432. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  433. return -EAGAIN;
  434. }
  435. if (xfrm6_tunnel_spi_init() < 0) {
  436. X6TPRINTK1(KERN_ERR
  437. "xfrm6_tunnel init: failed to initialize spi\n");
  438. inet6_del_protocol(&xfrm6_tunnel_protocol, IPPROTO_IPV6);
  439. xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
  440. return -EAGAIN;
  441. }
  442. return 0;
  443. }
  444. static void __exit xfrm6_tunnel_fini(void)
  445. {
  446. X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
  447. xfrm6_tunnel_spi_fini();
  448. if (inet6_del_protocol(&xfrm6_tunnel_protocol, IPPROTO_IPV6) < 0)
  449. X6TPRINTK1(KERN_ERR
  450. "xfrm6_tunnel close: can't remove protocol\n");
  451. if (xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6) < 0)
  452. X6TPRINTK1(KERN_ERR
  453. "xfrm6_tunnel close: can't remove xfrm type\n");
  454. }
  455. module_init(xfrm6_tunnel_init);
  456. module_exit(xfrm6_tunnel_fini);
  457. MODULE_LICENSE("GPL");