2 * Copyright (C)2003,2004 USAGI/WIDE Project
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.
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.
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
18 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
19 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
21 * Based on net/ipv4/xfrm4_tunnel.c
24 #include <linux/module.h>
25 #include <linux/xfrm.h>
26 #include <linux/list.h>
30 #include <linux/ipv6.h>
31 #include <linux/icmpv6.h>
32 #include <linux/mutex.h>
34 #ifdef CONFIG_IPV6_XFRM6_TUNNEL_DEBUG
40 #define X6TPRINTK(fmt, args...) printk(fmt, ## args)
41 #define X6TNOPRINTK(fmt, args...) do { ; } while(0)
44 # define X6TPRINTK1 X6TPRINTK
46 # define X6TPRINTK1 X6TNOPRINTK
50 # define X6TPRINTK3 X6TPRINTK
52 # define X6TPRINTK3 X6TNOPRINTK
56 * xfrm_tunnel_spi things are for allocating unique id ("spi")
59 struct xfrm6_tunnel_spi {
60 struct hlist_node list_byaddr;
61 struct hlist_node list_byspi;
65 #ifdef XFRM6_TUNNEL_SPI_MAGIC
70 #ifdef CONFIG_IPV6_XFRM6_TUNNEL_DEBUG
71 # define XFRM6_TUNNEL_SPI_MAGIC 0xdeadbeef
74 static DEFINE_RWLOCK(xfrm6_tunnel_spi_lock);
76 static u32 xfrm6_tunnel_spi;
78 #define XFRM6_TUNNEL_SPI_MIN 1
79 #define XFRM6_TUNNEL_SPI_MAX 0xffffffff
81 static kmem_cache_t *xfrm6_tunnel_spi_kmem __read_mostly;
83 #define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
84 #define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
86 static struct hlist_head xfrm6_tunnel_spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE];
87 static struct hlist_head xfrm6_tunnel_spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE];
89 #ifdef XFRM6_TUNNEL_SPI_MAGIC
90 static int x6spi_check_magic(const struct xfrm6_tunnel_spi *x6spi,
93 if (unlikely(x6spi->magic != XFRM6_TUNNEL_SPI_MAGIC)) {
94 X6TPRINTK3(KERN_DEBUG "%s(): x6spi object "
95 "at %p has corrupted magic %08x "
97 name, x6spi, x6spi->magic, XFRM6_TUNNEL_SPI_MAGIC);
103 static int inline x6spi_check_magic(const struct xfrm6_tunnel_spi *x6spi,
110 #define X6SPI_CHECK_MAGIC(x6spi) x6spi_check_magic((x6spi), __FUNCTION__)
113 static unsigned inline xfrm6_tunnel_spi_hash_byaddr(xfrm_address_t *addr)
117 X6TPRINTK3(KERN_DEBUG "%s(addr=%p)\n", __FUNCTION__, addr);
119 h = addr->a6[0] ^ addr->a6[1] ^ addr->a6[2] ^ addr->a6[3];
122 h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1;
124 X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, h);
129 static unsigned inline xfrm6_tunnel_spi_hash_byspi(u32 spi)
131 return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE;
135 static int xfrm6_tunnel_spi_init(void)
139 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
141 xfrm6_tunnel_spi = 0;
142 xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi",
143 sizeof(struct xfrm6_tunnel_spi),
144 0, SLAB_HWCACHE_ALIGN,
146 if (!xfrm6_tunnel_spi_kmem) {
148 "%s(): failed to allocate xfrm6_tunnel_spi_kmem\n",
153 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
154 INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byaddr[i]);
155 for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
156 INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byspi[i]);
160 static void xfrm6_tunnel_spi_fini(void)
164 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
166 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) {
167 if (!hlist_empty(&xfrm6_tunnel_spi_byaddr[i]))
170 for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++) {
171 if (!hlist_empty(&xfrm6_tunnel_spi_byspi[i]))
174 kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
175 xfrm6_tunnel_spi_kmem = NULL;
178 X6TPRINTK1(KERN_ERR "%s(): table is not empty\n", __FUNCTION__);
182 static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
184 struct xfrm6_tunnel_spi *x6spi;
185 struct hlist_node *pos;
187 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
189 hlist_for_each_entry(x6spi, pos,
190 &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
192 if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) {
193 X6SPI_CHECK_MAGIC(x6spi);
194 X6TPRINTK3(KERN_DEBUG "%s() = %p(%u)\n", __FUNCTION__, x6spi, x6spi->spi);
199 X6TPRINTK3(KERN_DEBUG "%s() = NULL(0)\n", __FUNCTION__);
203 u32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
205 struct xfrm6_tunnel_spi *x6spi;
208 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
210 read_lock_bh(&xfrm6_tunnel_spi_lock);
211 x6spi = __xfrm6_tunnel_spi_lookup(saddr);
212 spi = x6spi ? x6spi->spi : 0;
213 read_unlock_bh(&xfrm6_tunnel_spi_lock);
217 EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup);
219 static u32 __xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
222 struct xfrm6_tunnel_spi *x6spi;
223 struct hlist_node *pos;
226 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
228 if (xfrm6_tunnel_spi < XFRM6_TUNNEL_SPI_MIN ||
229 xfrm6_tunnel_spi >= XFRM6_TUNNEL_SPI_MAX)
230 xfrm6_tunnel_spi = XFRM6_TUNNEL_SPI_MIN;
234 for (spi = xfrm6_tunnel_spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) {
235 index = xfrm6_tunnel_spi_hash_byspi(spi);
236 hlist_for_each_entry(x6spi, pos,
237 &xfrm6_tunnel_spi_byspi[index],
239 if (x6spi->spi == spi)
242 xfrm6_tunnel_spi = spi;
246 for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tunnel_spi; spi++) {
247 index = xfrm6_tunnel_spi_hash_byspi(spi);
248 hlist_for_each_entry(x6spi, pos,
249 &xfrm6_tunnel_spi_byspi[index],
251 if (x6spi->spi == spi)
254 xfrm6_tunnel_spi = spi;
261 X6TPRINTK3(KERN_DEBUG "%s(): allocate new spi for " NIP6_FMT "\n",
263 NIP6(*(struct in6_addr *)saddr));
264 x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, SLAB_ATOMIC);
266 X6TPRINTK1(KERN_ERR "%s(): kmem_cache_alloc() failed\n",
270 #ifdef XFRM6_TUNNEL_SPI_MAGIC
271 x6spi->magic = XFRM6_TUNNEL_SPI_MAGIC;
273 memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
275 atomic_set(&x6spi->refcnt, 1);
277 hlist_add_head(&x6spi->list_byspi, &xfrm6_tunnel_spi_byspi[index]);
279 index = xfrm6_tunnel_spi_hash_byaddr(saddr);
280 hlist_add_head(&x6spi->list_byaddr, &xfrm6_tunnel_spi_byaddr[index]);
281 X6SPI_CHECK_MAGIC(x6spi);
283 X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
287 u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
289 struct xfrm6_tunnel_spi *x6spi;
292 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
294 write_lock_bh(&xfrm6_tunnel_spi_lock);
295 x6spi = __xfrm6_tunnel_spi_lookup(saddr);
297 atomic_inc(&x6spi->refcnt);
300 spi = __xfrm6_tunnel_alloc_spi(saddr);
301 write_unlock_bh(&xfrm6_tunnel_spi_lock);
303 X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
308 EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
310 void xfrm6_tunnel_free_spi(xfrm_address_t *saddr)
312 struct xfrm6_tunnel_spi *x6spi;
313 struct hlist_node *pos, *n;
315 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
317 write_lock_bh(&xfrm6_tunnel_spi_lock);
319 hlist_for_each_entry_safe(x6spi, pos, n,
320 &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
323 if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) {
324 X6TPRINTK3(KERN_DEBUG "%s(): x6spi object for " NIP6_FMT
327 NIP6(*(struct in6_addr *)saddr),
329 X6SPI_CHECK_MAGIC(x6spi);
330 if (atomic_dec_and_test(&x6spi->refcnt)) {
331 hlist_del(&x6spi->list_byaddr);
332 hlist_del(&x6spi->list_byspi);
333 kmem_cache_free(xfrm6_tunnel_spi_kmem, x6spi);
338 write_unlock_bh(&xfrm6_tunnel_spi_lock);
341 EXPORT_SYMBOL(xfrm6_tunnel_free_spi);
343 static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
345 struct ipv6hdr *top_iph;
347 top_iph = (struct ipv6hdr *)skb->data;
348 top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
353 static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
358 static int xfrm6_tunnel_rcv(struct sk_buff *skb)
360 struct ipv6hdr *iph = skb->nh.ipv6h;
363 spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&iph->saddr);
364 return xfrm6_rcv_spi(skb, spi);
367 static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
368 int type, int code, int offset, __u32 info)
370 /* xfrm6_tunnel native err handling */
372 case ICMPV6_DEST_UNREACH:
375 case ICMPV6_ADM_PROHIBITED:
376 case ICMPV6_NOT_NEIGHBOUR:
377 case ICMPV6_ADDR_UNREACH:
378 case ICMPV6_PORT_UNREACH:
380 X6TPRINTK3(KERN_DEBUG
381 "xfrm6_tunnel: Destination Unreach.\n");
385 case ICMPV6_PKT_TOOBIG:
386 X6TPRINTK3(KERN_DEBUG
387 "xfrm6_tunnel: Packet Too Big.\n");
389 case ICMPV6_TIME_EXCEED:
391 case ICMPV6_EXC_HOPLIMIT:
392 X6TPRINTK3(KERN_DEBUG
393 "xfrm6_tunnel: Too small Hoplimit.\n");
395 case ICMPV6_EXC_FRAGTIME:
400 case ICMPV6_PARAMPROB:
402 case ICMPV6_HDR_FIELD: break;
403 case ICMPV6_UNK_NEXTHDR: break;
404 case ICMPV6_UNK_OPTION: break;
414 static int xfrm6_tunnel_init_state(struct xfrm_state *x)
422 x->props.header_len = sizeof(struct ipv6hdr);
427 static void xfrm6_tunnel_destroy(struct xfrm_state *x)
429 xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
432 static struct xfrm_type xfrm6_tunnel_type = {
433 .description = "IP6IP6",
434 .owner = THIS_MODULE,
435 .proto = IPPROTO_IPV6,
436 .init_state = xfrm6_tunnel_init_state,
437 .destructor = xfrm6_tunnel_destroy,
438 .input = xfrm6_tunnel_input,
439 .output = xfrm6_tunnel_output,
442 static struct xfrm6_tunnel xfrm6_tunnel_handler = {
443 .handler = xfrm6_tunnel_rcv,
444 .err_handler = xfrm6_tunnel_err,
448 static int __init xfrm6_tunnel_init(void)
450 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
452 if (xfrm_register_type(&xfrm6_tunnel_type, AF_INET6) < 0) {
454 "xfrm6_tunnel init: can't add xfrm type\n");
457 if (xfrm6_tunnel_register(&xfrm6_tunnel_handler)) {
459 "xfrm6_tunnel init(): can't add handler\n");
460 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
463 if (xfrm6_tunnel_spi_init() < 0) {
465 "xfrm6_tunnel init: failed to initialize spi\n");
466 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler);
467 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
473 static void __exit xfrm6_tunnel_fini(void)
475 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
477 xfrm6_tunnel_spi_fini();
478 if (xfrm6_tunnel_deregister(&xfrm6_tunnel_handler))
480 "xfrm6_tunnel close: can't remove handler\n");
481 if (xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6) < 0)
483 "xfrm6_tunnel close: can't remove xfrm type\n");
486 module_init(xfrm6_tunnel_init);
487 module_exit(xfrm6_tunnel_fini);
488 MODULE_LICENSE("GPL");