aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv6/xfrm6_tunnel.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/ipv6/xfrm6_tunnel.c')
-rw-r--r--net/ipv6/xfrm6_tunnel.c145
1 files changed, 12 insertions, 133 deletions
diff --git a/net/ipv6/xfrm6_tunnel.c b/net/ipv6/xfrm6_tunnel.c
index d37768e5064f..7af227bb1551 100644
--- a/net/ipv6/xfrm6_tunnel.c
+++ b/net/ipv6/xfrm6_tunnel.c
@@ -21,7 +21,6 @@
21 * Based on net/ipv4/xfrm4_tunnel.c 21 * Based on net/ipv4/xfrm4_tunnel.c
22 * 22 *
23 */ 23 */
24#include <linux/config.h>
25#include <linux/module.h> 24#include <linux/module.h>
26#include <linux/xfrm.h> 25#include <linux/xfrm.h>
27#include <linux/list.h> 26#include <linux/list.h>
@@ -32,27 +31,6 @@
32#include <linux/icmpv6.h> 31#include <linux/icmpv6.h>
33#include <linux/mutex.h> 32#include <linux/mutex.h>
34 33
35#ifdef CONFIG_IPV6_XFRM6_TUNNEL_DEBUG
36# define X6TDEBUG 3
37#else
38# define X6TDEBUG 1
39#endif
40
41#define X6TPRINTK(fmt, args...) printk(fmt, ## args)
42#define X6TNOPRINTK(fmt, args...) do { ; } while(0)
43
44#if X6TDEBUG >= 1
45# define X6TPRINTK1 X6TPRINTK
46#else
47# define X6TPRINTK1 X6TNOPRINTK
48#endif
49
50#if X6TDEBUG >= 3
51# define X6TPRINTK3 X6TPRINTK
52#else
53# define X6TPRINTK3 X6TNOPRINTK
54#endif
55
56/* 34/*
57 * xfrm_tunnel_spi things are for allocating unique id ("spi") 35 * xfrm_tunnel_spi things are for allocating unique id ("spi")
58 * per xfrm_address_t. 36 * per xfrm_address_t.
@@ -63,15 +41,8 @@ struct xfrm6_tunnel_spi {
63 xfrm_address_t addr; 41 xfrm_address_t addr;
64 u32 spi; 42 u32 spi;
65 atomic_t refcnt; 43 atomic_t refcnt;
66#ifdef XFRM6_TUNNEL_SPI_MAGIC
67 u32 magic;
68#endif
69}; 44};
70 45
71#ifdef CONFIG_IPV6_XFRM6_TUNNEL_DEBUG
72# define XFRM6_TUNNEL_SPI_MAGIC 0xdeadbeef
73#endif
74
75static DEFINE_RWLOCK(xfrm6_tunnel_spi_lock); 46static DEFINE_RWLOCK(xfrm6_tunnel_spi_lock);
76 47
77static u32 xfrm6_tunnel_spi; 48static u32 xfrm6_tunnel_spi;
@@ -87,43 +58,15 @@ static kmem_cache_t *xfrm6_tunnel_spi_kmem __read_mostly;
87static struct hlist_head xfrm6_tunnel_spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE]; 58static struct hlist_head xfrm6_tunnel_spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE];
88static struct hlist_head xfrm6_tunnel_spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE]; 59static struct hlist_head xfrm6_tunnel_spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE];
89 60
90#ifdef XFRM6_TUNNEL_SPI_MAGIC
91static int x6spi_check_magic(const struct xfrm6_tunnel_spi *x6spi,
92 const char *name)
93{
94 if (unlikely(x6spi->magic != XFRM6_TUNNEL_SPI_MAGIC)) {
95 X6TPRINTK3(KERN_DEBUG "%s(): x6spi object "
96 "at %p has corrupted magic %08x "
97 "(should be %08x)\n",
98 name, x6spi, x6spi->magic, XFRM6_TUNNEL_SPI_MAGIC);
99 return -1;
100 }
101 return 0;
102}
103#else
104static int inline x6spi_check_magic(const struct xfrm6_tunnel_spi *x6spi,
105 const char *name)
106{
107 return 0;
108}
109#endif
110
111#define X6SPI_CHECK_MAGIC(x6spi) x6spi_check_magic((x6spi), __FUNCTION__)
112
113
114static unsigned inline xfrm6_tunnel_spi_hash_byaddr(xfrm_address_t *addr) 61static unsigned inline xfrm6_tunnel_spi_hash_byaddr(xfrm_address_t *addr)
115{ 62{
116 unsigned h; 63 unsigned h;
117 64
118 X6TPRINTK3(KERN_DEBUG "%s(addr=%p)\n", __FUNCTION__, addr);
119
120 h = addr->a6[0] ^ addr->a6[1] ^ addr->a6[2] ^ addr->a6[3]; 65 h = addr->a6[0] ^ addr->a6[1] ^ addr->a6[2] ^ addr->a6[3];
121 h ^= h >> 16; 66 h ^= h >> 16;
122 h ^= h >> 8; 67 h ^= h >> 8;
123 h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1; 68 h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1;
124 69
125 X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, h);
126
127 return h; 70 return h;
128} 71}
129 72
@@ -137,19 +80,13 @@ static int xfrm6_tunnel_spi_init(void)
137{ 80{
138 int i; 81 int i;
139 82
140 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
141
142 xfrm6_tunnel_spi = 0; 83 xfrm6_tunnel_spi = 0;
143 xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi", 84 xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi",
144 sizeof(struct xfrm6_tunnel_spi), 85 sizeof(struct xfrm6_tunnel_spi),
145 0, SLAB_HWCACHE_ALIGN, 86 0, SLAB_HWCACHE_ALIGN,
146 NULL, NULL); 87 NULL, NULL);
147 if (!xfrm6_tunnel_spi_kmem) { 88 if (!xfrm6_tunnel_spi_kmem)
148 X6TPRINTK1(KERN_ERR
149 "%s(): failed to allocate xfrm6_tunnel_spi_kmem\n",
150 __FUNCTION__);
151 return -ENOMEM; 89 return -ENOMEM;
152 }
153 90
154 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) 91 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
155 INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byaddr[i]); 92 INIT_HLIST_HEAD(&xfrm6_tunnel_spi_byaddr[i]);
@@ -162,22 +99,16 @@ static void xfrm6_tunnel_spi_fini(void)
162{ 99{
163 int i; 100 int i;
164 101
165 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
166
167 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) { 102 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) {
168 if (!hlist_empty(&xfrm6_tunnel_spi_byaddr[i])) 103 if (!hlist_empty(&xfrm6_tunnel_spi_byaddr[i]))
169 goto err; 104 return;
170 } 105 }
171 for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++) { 106 for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++) {
172 if (!hlist_empty(&xfrm6_tunnel_spi_byspi[i])) 107 if (!hlist_empty(&xfrm6_tunnel_spi_byspi[i]))
173 goto err; 108 return;
174 } 109 }
175 kmem_cache_destroy(xfrm6_tunnel_spi_kmem); 110 kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
176 xfrm6_tunnel_spi_kmem = NULL; 111 xfrm6_tunnel_spi_kmem = NULL;
177 return;
178err:
179 X6TPRINTK1(KERN_ERR "%s(): table is not empty\n", __FUNCTION__);
180 return;
181} 112}
182 113
183static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr) 114static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
@@ -185,19 +116,13 @@ static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
185 struct xfrm6_tunnel_spi *x6spi; 116 struct xfrm6_tunnel_spi *x6spi;
186 struct hlist_node *pos; 117 struct hlist_node *pos;
187 118
188 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
189
190 hlist_for_each_entry(x6spi, pos, 119 hlist_for_each_entry(x6spi, pos,
191 &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)], 120 &xfrm6_tunnel_spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
192 list_byaddr) { 121 list_byaddr) {
193 if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) { 122 if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0)
194 X6SPI_CHECK_MAGIC(x6spi);
195 X6TPRINTK3(KERN_DEBUG "%s() = %p(%u)\n", __FUNCTION__, x6spi, x6spi->spi);
196 return x6spi; 123 return x6spi;
197 }
198 } 124 }
199 125
200 X6TPRINTK3(KERN_DEBUG "%s() = NULL(0)\n", __FUNCTION__);
201 return NULL; 126 return NULL;
202} 127}
203 128
@@ -206,8 +131,6 @@ u32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
206 struct xfrm6_tunnel_spi *x6spi; 131 struct xfrm6_tunnel_spi *x6spi;
207 u32 spi; 132 u32 spi;
208 133
209 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
210
211 read_lock_bh(&xfrm6_tunnel_spi_lock); 134 read_lock_bh(&xfrm6_tunnel_spi_lock);
212 x6spi = __xfrm6_tunnel_spi_lookup(saddr); 135 x6spi = __xfrm6_tunnel_spi_lookup(saddr);
213 spi = x6spi ? x6spi->spi : 0; 136 spi = x6spi ? x6spi->spi : 0;
@@ -224,8 +147,6 @@ static u32 __xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
224 struct hlist_node *pos; 147 struct hlist_node *pos;
225 unsigned index; 148 unsigned index;
226 149
227 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
228
229 if (xfrm6_tunnel_spi < XFRM6_TUNNEL_SPI_MIN || 150 if (xfrm6_tunnel_spi < XFRM6_TUNNEL_SPI_MIN ||
230 xfrm6_tunnel_spi >= XFRM6_TUNNEL_SPI_MAX) 151 xfrm6_tunnel_spi >= XFRM6_TUNNEL_SPI_MAX)
231 xfrm6_tunnel_spi = XFRM6_TUNNEL_SPI_MIN; 152 xfrm6_tunnel_spi = XFRM6_TUNNEL_SPI_MIN;
@@ -259,18 +180,10 @@ try_next_2:;
259 spi = 0; 180 spi = 0;
260 goto out; 181 goto out;
261alloc_spi: 182alloc_spi:
262 X6TPRINTK3(KERN_DEBUG "%s(): allocate new spi for " NIP6_FMT "\n",
263 __FUNCTION__,
264 NIP6(*(struct in6_addr *)saddr));
265 x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, SLAB_ATOMIC); 183 x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, SLAB_ATOMIC);
266 if (!x6spi) { 184 if (!x6spi)
267 X6TPRINTK1(KERN_ERR "%s(): kmem_cache_alloc() failed\n",
268 __FUNCTION__);
269 goto out; 185 goto out;
270 } 186
271#ifdef XFRM6_TUNNEL_SPI_MAGIC
272 x6spi->magic = XFRM6_TUNNEL_SPI_MAGIC;
273#endif
274 memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr)); 187 memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
275 x6spi->spi = spi; 188 x6spi->spi = spi;
276 atomic_set(&x6spi->refcnt, 1); 189 atomic_set(&x6spi->refcnt, 1);
@@ -279,9 +192,7 @@ alloc_spi:
279 192
280 index = xfrm6_tunnel_spi_hash_byaddr(saddr); 193 index = xfrm6_tunnel_spi_hash_byaddr(saddr);
281 hlist_add_head(&x6spi->list_byaddr, &xfrm6_tunnel_spi_byaddr[index]); 194 hlist_add_head(&x6spi->list_byaddr, &xfrm6_tunnel_spi_byaddr[index]);
282 X6SPI_CHECK_MAGIC(x6spi);
283out: 195out:
284 X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
285 return spi; 196 return spi;
286} 197}
287 198
@@ -290,8 +201,6 @@ u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
290 struct xfrm6_tunnel_spi *x6spi; 201 struct xfrm6_tunnel_spi *x6spi;
291 u32 spi; 202 u32 spi;
292 203
293 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
294
295 write_lock_bh(&xfrm6_tunnel_spi_lock); 204 write_lock_bh(&xfrm6_tunnel_spi_lock);
296 x6spi = __xfrm6_tunnel_spi_lookup(saddr); 205 x6spi = __xfrm6_tunnel_spi_lookup(saddr);
297 if (x6spi) { 206 if (x6spi) {
@@ -301,8 +210,6 @@ u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
301 spi = __xfrm6_tunnel_alloc_spi(saddr); 210 spi = __xfrm6_tunnel_alloc_spi(saddr);
302 write_unlock_bh(&xfrm6_tunnel_spi_lock); 211 write_unlock_bh(&xfrm6_tunnel_spi_lock);
303 212
304 X6TPRINTK3(KERN_DEBUG "%s() = %u\n", __FUNCTION__, spi);
305
306 return spi; 213 return spi;
307} 214}
308 215
@@ -313,8 +220,6 @@ void xfrm6_tunnel_free_spi(xfrm_address_t *saddr)
313 struct xfrm6_tunnel_spi *x6spi; 220 struct xfrm6_tunnel_spi *x6spi;
314 struct hlist_node *pos, *n; 221 struct hlist_node *pos, *n;
315 222
316 X6TPRINTK3(KERN_DEBUG "%s(saddr=%p)\n", __FUNCTION__, saddr);
317
318 write_lock_bh(&xfrm6_tunnel_spi_lock); 223 write_lock_bh(&xfrm6_tunnel_spi_lock);
319 224
320 hlist_for_each_entry_safe(x6spi, pos, n, 225 hlist_for_each_entry_safe(x6spi, pos, n,
@@ -322,12 +227,6 @@ void xfrm6_tunnel_free_spi(xfrm_address_t *saddr)
322 list_byaddr) 227 list_byaddr)
323 { 228 {
324 if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) { 229 if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) {
325 X6TPRINTK3(KERN_DEBUG "%s(): x6spi object for " NIP6_FMT
326 " found at %p\n",
327 __FUNCTION__,
328 NIP6(*(struct in6_addr *)saddr),
329 x6spi);
330 X6SPI_CHECK_MAGIC(x6spi);
331 if (atomic_dec_and_test(&x6spi->refcnt)) { 230 if (atomic_dec_and_test(&x6spi->refcnt)) {
332 hlist_del(&x6spi->list_byaddr); 231 hlist_del(&x6spi->list_byaddr);
333 hlist_del(&x6spi->list_byspi); 232 hlist_del(&x6spi->list_byspi);
@@ -359,7 +258,7 @@ static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
359static int xfrm6_tunnel_rcv(struct sk_buff *skb) 258static int xfrm6_tunnel_rcv(struct sk_buff *skb)
360{ 259{
361 struct ipv6hdr *iph = skb->nh.ipv6h; 260 struct ipv6hdr *iph = skb->nh.ipv6h;
362 u32 spi; 261 __be32 spi;
363 262
364 spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&iph->saddr); 263 spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&iph->saddr);
365 return xfrm6_rcv_spi(skb, spi); 264 return xfrm6_rcv_spi(skb, spi);
@@ -378,20 +277,14 @@ static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
378 case ICMPV6_ADDR_UNREACH: 277 case ICMPV6_ADDR_UNREACH:
379 case ICMPV6_PORT_UNREACH: 278 case ICMPV6_PORT_UNREACH:
380 default: 279 default:
381 X6TPRINTK3(KERN_DEBUG
382 "xfrm6_tunnel: Destination Unreach.\n");
383 break; 280 break;
384 } 281 }
385 break; 282 break;
386 case ICMPV6_PKT_TOOBIG: 283 case ICMPV6_PKT_TOOBIG:
387 X6TPRINTK3(KERN_DEBUG
388 "xfrm6_tunnel: Packet Too Big.\n");
389 break; 284 break;
390 case ICMPV6_TIME_EXCEED: 285 case ICMPV6_TIME_EXCEED:
391 switch (code) { 286 switch (code) {
392 case ICMPV6_EXC_HOPLIMIT: 287 case ICMPV6_EXC_HOPLIMIT:
393 X6TPRINTK3(KERN_DEBUG
394 "xfrm6_tunnel: Too small Hoplimit.\n");
395 break; 288 break;
396 case ICMPV6_EXC_FRAGTIME: 289 case ICMPV6_EXC_FRAGTIME:
397 default: 290 default:
@@ -414,7 +307,7 @@ static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
414 307
415static int xfrm6_tunnel_init_state(struct xfrm_state *x) 308static int xfrm6_tunnel_init_state(struct xfrm_state *x)
416{ 309{
417 if (!x->props.mode) 310 if (x->props.mode != XFRM_MODE_TUNNEL)
418 return -EINVAL; 311 return -EINVAL;
419 312
420 if (x->encap) 313 if (x->encap)
@@ -448,22 +341,14 @@ static struct xfrm6_tunnel xfrm6_tunnel_handler = {
448 341
449static int __init xfrm6_tunnel_init(void) 342static int __init xfrm6_tunnel_init(void)
450{ 343{
451 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__); 344 if (xfrm_register_type(&xfrm6_tunnel_type, AF_INET6) < 0)
452
453 if (xfrm_register_type(&xfrm6_tunnel_type, AF_INET6) < 0) {
454 X6TPRINTK1(KERN_ERR
455 "xfrm6_tunnel init: can't add xfrm type\n");
456 return -EAGAIN; 345 return -EAGAIN;
457 } 346
458 if (xfrm6_tunnel_register(&xfrm6_tunnel_handler)) { 347 if (xfrm6_tunnel_register(&xfrm6_tunnel_handler)) {
459 X6TPRINTK1(KERN_ERR
460 "xfrm6_tunnel init(): can't add handler\n");
461 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6); 348 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
462 return -EAGAIN; 349 return -EAGAIN;
463 } 350 }
464 if (xfrm6_tunnel_spi_init() < 0) { 351 if (xfrm6_tunnel_spi_init() < 0) {
465 X6TPRINTK1(KERN_ERR
466 "xfrm6_tunnel init: failed to initialize spi\n");
467 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler); 352 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler);
468 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6); 353 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
469 return -EAGAIN; 354 return -EAGAIN;
@@ -473,15 +358,9 @@ static int __init xfrm6_tunnel_init(void)
473 358
474static void __exit xfrm6_tunnel_fini(void) 359static void __exit xfrm6_tunnel_fini(void)
475{ 360{
476 X6TPRINTK3(KERN_DEBUG "%s()\n", __FUNCTION__);
477
478 xfrm6_tunnel_spi_fini(); 361 xfrm6_tunnel_spi_fini();
479 if (xfrm6_tunnel_deregister(&xfrm6_tunnel_handler)) 362 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler);
480 X6TPRINTK1(KERN_ERR 363 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
481 "xfrm6_tunnel close: can't remove handler\n");
482 if (xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6) < 0)
483 X6TPRINTK1(KERN_ERR
484 "xfrm6_tunnel close: can't remove xfrm type\n");
485} 364}
486 365
487module_init(xfrm6_tunnel_init); 366module_init(xfrm6_tunnel_init);