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