xfrm6_tunnel.c 13 KB

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