Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszer...
[linux-2.6-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.
1ab1457c 8 *
1da177e4
LT
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.
1ab1457c 13 *
1da177e4 14 * You should have received a copy of the GNU General Public License
a99421d9 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1da177e4
LT
16 *
17 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
67ba4152 18 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
1da177e4
LT
19 *
20 * Based on net/ipv4/xfrm4_tunnel.c
21 *
22 */
1da177e4
LT
23#include <linux/module.h>
24#include <linux/xfrm.h>
5a0e3ad6 25#include <linux/slab.h>
91cc3bb0 26#include <linux/rculist.h>
1da177e4
LT
27#include <net/ip.h>
28#include <net/xfrm.h>
29#include <net/ipv6.h>
1da177e4
LT
30#include <linux/ipv6.h>
31#include <linux/icmpv6.h>
4a3e2f71 32#include <linux/mutex.h>
a1664773
AD
33#include <net/netns/generic.h>
34
35#define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
36#define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
37
38#define XFRM6_TUNNEL_SPI_MIN 1
39#define XFRM6_TUNNEL_SPI_MAX 0xffffffff
40
41struct xfrm6_tunnel_net {
42 struct hlist_head spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE];
43 struct hlist_head spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE];
44 u32 spi;
45};
46
47static int xfrm6_tunnel_net_id __read_mostly;
48static inline struct xfrm6_tunnel_net *xfrm6_tunnel_pernet(struct net *net)
49{
50 return net_generic(net, xfrm6_tunnel_net_id);
51}
1da177e4 52
1da177e4 53/*
1ab1457c 54 * xfrm_tunnel_spi things are for allocating unique id ("spi")
1da177e4
LT
55 * per xfrm_address_t.
56 */
57struct xfrm6_tunnel_spi {
91cc3bb0
ED
58 struct hlist_node list_byaddr;
59 struct hlist_node list_byspi;
60 xfrm_address_t addr;
61 u32 spi;
62 atomic_t refcnt;
63 struct rcu_head rcu_head;
1da177e4
LT
64};
65
91cc3bb0 66static DEFINE_SPINLOCK(xfrm6_tunnel_spi_lock);
1da177e4 67
e18b890b 68static struct kmem_cache *xfrm6_tunnel_spi_kmem __read_mostly;
1da177e4 69
95c96174 70static inline unsigned int xfrm6_tunnel_spi_hash_byaddr(const xfrm_address_t *addr)
1da177e4 71{
95c96174 72 unsigned int h;
1da177e4 73
2b464f61 74 h = ipv6_addr_hash((const struct in6_addr *)addr);
1da177e4
LT
75 h ^= h >> 16;
76 h ^= h >> 8;
77 h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1;
78
1da177e4
LT
79 return h;
80}
81
95c96174 82static inline unsigned int xfrm6_tunnel_spi_hash_byspi(u32 spi)
1da177e4
LT
83{
84 return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE;
85}
86
b71d1d42 87static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr)
1da177e4 88{
a1664773 89 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
1da177e4 90 struct xfrm6_tunnel_spi *x6spi;
1da177e4 91
b67bfe0d 92 hlist_for_each_entry_rcu(x6spi,
a1664773 93 &xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
1da177e4 94 list_byaddr) {
ff88b30c 95 if (xfrm6_addr_equal(&x6spi->addr, saddr))
1da177e4 96 return x6spi;
1da177e4
LT
97 }
98
1da177e4
LT
99 return NULL;
100}
101
b71d1d42 102__be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr)
1da177e4
LT
103{
104 struct xfrm6_tunnel_spi *x6spi;
105 u32 spi;
106
91cc3bb0 107 rcu_read_lock_bh();
a1664773 108 x6spi = __xfrm6_tunnel_spi_lookup(net, saddr);
1da177e4 109 spi = x6spi ? x6spi->spi : 0;
91cc3bb0 110 rcu_read_unlock_bh();
5b122545 111 return htonl(spi);
1da177e4 112}
1da177e4
LT
113EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup);
114
a1664773 115static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi)
df8ea19b 116{
a1664773 117 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
df8ea19b
YH
118 struct xfrm6_tunnel_spi *x6spi;
119 int index = xfrm6_tunnel_spi_hash_byspi(spi);
df8ea19b 120
b67bfe0d 121 hlist_for_each_entry(x6spi,
a1664773 122 &xfrm6_tn->spi_byspi[index],
df8ea19b
YH
123 list_byspi) {
124 if (x6spi->spi == spi)
125 return -1;
126 }
127 return index;
128}
129
a1664773 130static u32 __xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
1da177e4 131{
a1664773 132 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
1da177e4
LT
133 u32 spi;
134 struct xfrm6_tunnel_spi *x6spi;
df8ea19b 135 int index;
1da177e4 136
a1664773
AD
137 if (xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN ||
138 xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX)
139 xfrm6_tn->spi = XFRM6_TUNNEL_SPI_MIN;
1da177e4 140 else
a1664773 141 xfrm6_tn->spi++;
1da177e4 142
a1664773
AD
143 for (spi = xfrm6_tn->spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) {
144 index = __xfrm6_tunnel_spi_check(net, spi);
df8ea19b
YH
145 if (index >= 0)
146 goto alloc_spi;
1da177e4 147 }
a1664773
AD
148 for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tn->spi; spi++) {
149 index = __xfrm6_tunnel_spi_check(net, spi);
df8ea19b
YH
150 if (index >= 0)
151 goto alloc_spi;
1da177e4
LT
152 }
153 spi = 0;
154 goto out;
155alloc_spi:
a1664773 156 xfrm6_tn->spi = spi;
54e6ecb2 157 x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, GFP_ATOMIC);
a922ba55 158 if (!x6spi)
1da177e4 159 goto out;
a922ba55 160
1da177e4
LT
161 memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
162 x6spi->spi = spi;
163 atomic_set(&x6spi->refcnt, 1);
164
a1664773 165 hlist_add_head_rcu(&x6spi->list_byspi, &xfrm6_tn->spi_byspi[index]);
1da177e4
LT
166
167 index = xfrm6_tunnel_spi_hash_byaddr(saddr);
a1664773 168 hlist_add_head_rcu(&x6spi->list_byaddr, &xfrm6_tn->spi_byaddr[index]);
1da177e4 169out:
1da177e4
LT
170 return spi;
171}
172
a1664773 173__be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
1da177e4
LT
174{
175 struct xfrm6_tunnel_spi *x6spi;
176 u32 spi;
177
91cc3bb0 178 spin_lock_bh(&xfrm6_tunnel_spi_lock);
a1664773 179 x6spi = __xfrm6_tunnel_spi_lookup(net, saddr);
1da177e4
LT
180 if (x6spi) {
181 atomic_inc(&x6spi->refcnt);
182 spi = x6spi->spi;
183 } else
a1664773 184 spi = __xfrm6_tunnel_alloc_spi(net, saddr);
91cc3bb0 185 spin_unlock_bh(&xfrm6_tunnel_spi_lock);
1da177e4 186
5b122545 187 return htonl(spi);
1da177e4 188}
1da177e4
LT
189EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
190
91cc3bb0
ED
191static void x6spi_destroy_rcu(struct rcu_head *head)
192{
193 kmem_cache_free(xfrm6_tunnel_spi_kmem,
194 container_of(head, struct xfrm6_tunnel_spi, rcu_head));
195}
196
6f747aca 197static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr)
1da177e4 198{
a1664773 199 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
1da177e4 200 struct xfrm6_tunnel_spi *x6spi;
b67bfe0d 201 struct hlist_node *n;
1da177e4 202
91cc3bb0 203 spin_lock_bh(&xfrm6_tunnel_spi_lock);
1da177e4 204
b67bfe0d 205 hlist_for_each_entry_safe(x6spi, n,
a1664773 206 &xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
1da177e4
LT
207 list_byaddr)
208 {
ff88b30c 209 if (xfrm6_addr_equal(&x6spi->addr, saddr)) {
1da177e4 210 if (atomic_dec_and_test(&x6spi->refcnt)) {
91cc3bb0
ED
211 hlist_del_rcu(&x6spi->list_byaddr);
212 hlist_del_rcu(&x6spi->list_byspi);
213 call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu);
1da177e4
LT
214 break;
215 }
216 }
217 }
91cc3bb0 218 spin_unlock_bh(&xfrm6_tunnel_spi_lock);
1da177e4
LT
219}
220
1da177e4
LT
221static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
222{
7b277b1a 223 skb_push(skb, -skb_network_offset(skb));
1da177e4
LT
224 return 0;
225}
226
e695633e 227static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
1da177e4 228{
04663d0b 229 return skb_network_header(skb)[IP6CB(skb)->nhoff];
1da177e4
LT
230}
231
d2acc347 232static int xfrm6_tunnel_rcv(struct sk_buff *skb)
1da177e4 233{
a1664773 234 struct net *net = dev_net(skb->dev);
b71d1d42 235 const struct ipv6hdr *iph = ipv6_hdr(skb);
a252cc23 236 __be32 spi;
1da177e4 237
b71d1d42 238 spi = xfrm6_tunnel_spi_lookup(net, (const xfrm_address_t *)&iph->saddr);
6ac3f664 239 return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi);
1da177e4
LT
240}
241
d2acc347 242static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
d5fdd6ba 243 u8 type, u8 code, int offset, __be32 info)
1da177e4 244{
1da177e4
LT
245 /* xfrm6_tunnel native err handling */
246 switch (type) {
1ab1457c 247 case ICMPV6_DEST_UNREACH:
1da177e4 248 switch (code) {
1ab1457c 249 case ICMPV6_NOROUTE:
1da177e4
LT
250 case ICMPV6_ADM_PROHIBITED:
251 case ICMPV6_NOT_NEIGHBOUR:
252 case ICMPV6_ADDR_UNREACH:
253 case ICMPV6_PORT_UNREACH:
254 default:
1da177e4
LT
255 break;
256 }
257 break;
258 case ICMPV6_PKT_TOOBIG:
1da177e4
LT
259 break;
260 case ICMPV6_TIME_EXCEED:
261 switch (code) {
262 case ICMPV6_EXC_HOPLIMIT:
1da177e4
LT
263 break;
264 case ICMPV6_EXC_FRAGTIME:
1ab1457c 265 default:
1da177e4
LT
266 break;
267 }
268 break;
269 case ICMPV6_PARAMPROB:
270 switch (code) {
271 case ICMPV6_HDR_FIELD: break;
272 case ICMPV6_UNK_NEXTHDR: break;
273 case ICMPV6_UNK_OPTION: break;
274 }
275 break;
276 default:
277 break;
278 }
d2acc347
HX
279
280 return 0;
1da177e4
LT
281}
282
72cb6962 283static int xfrm6_tunnel_init_state(struct xfrm_state *x)
1da177e4 284{
7e49e6de 285 if (x->props.mode != XFRM_MODE_TUNNEL)
1da177e4
LT
286 return -EINVAL;
287
288 if (x->encap)
289 return -EINVAL;
290
291 x->props.header_len = sizeof(struct ipv6hdr);
292
293 return 0;
294}
295
296static void xfrm6_tunnel_destroy(struct xfrm_state *x)
297{
a1664773
AD
298 struct net *net = xs_net(x);
299
300 xfrm6_tunnel_free_spi(net, (xfrm_address_t *)&x->props.saddr);
1da177e4
LT
301}
302
533cb5b0 303static const struct xfrm_type xfrm6_tunnel_type = {
1da177e4
LT
304 .description = "IP6IP6",
305 .owner = THIS_MODULE,
306 .proto = IPPROTO_IPV6,
307 .init_state = xfrm6_tunnel_init_state,
308 .destructor = xfrm6_tunnel_destroy,
309 .input = xfrm6_tunnel_input,
310 .output = xfrm6_tunnel_output,
311};
312
3ff2cfa5 313static struct xfrm6_tunnel xfrm6_tunnel_handler __read_mostly = {
1da177e4 314 .handler = xfrm6_tunnel_rcv,
d2acc347
HX
315 .err_handler = xfrm6_tunnel_err,
316 .priority = 2,
1da177e4
LT
317};
318
3ff2cfa5 319static struct xfrm6_tunnel xfrm46_tunnel_handler __read_mostly = {
73d605d1
KM
320 .handler = xfrm6_tunnel_rcv,
321 .err_handler = xfrm6_tunnel_err,
322 .priority = 2,
323};
324
a1664773
AD
325static int __net_init xfrm6_tunnel_net_init(struct net *net)
326{
327 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
328 unsigned int i;
329
330 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
331 INIT_HLIST_HEAD(&xfrm6_tn->spi_byaddr[i]);
332 for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
333 INIT_HLIST_HEAD(&xfrm6_tn->spi_byspi[i]);
334 xfrm6_tn->spi = 0;
335
336 return 0;
337}
338
339static void __net_exit xfrm6_tunnel_net_exit(struct net *net)
340{
341}
342
343static struct pernet_operations xfrm6_tunnel_net_ops = {
344 .init = xfrm6_tunnel_net_init,
345 .exit = xfrm6_tunnel_net_exit,
346 .id = &xfrm6_tunnel_net_id,
347 .size = sizeof(struct xfrm6_tunnel_net),
348};
349
1da177e4
LT
350static int __init xfrm6_tunnel_init(void)
351{
e924960d
AD
352 int rv;
353
d5aa407f
AD
354 xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi",
355 sizeof(struct xfrm6_tunnel_spi),
356 0, SLAB_HWCACHE_ALIGN,
357 NULL);
358 if (!xfrm6_tunnel_spi_kmem)
359 return -ENOMEM;
360 rv = register_pernet_subsys(&xfrm6_tunnel_net_ops);
361 if (rv < 0)
362 goto out_pernet;
e924960d
AD
363 rv = xfrm_register_type(&xfrm6_tunnel_type, AF_INET6);
364 if (rv < 0)
d5aa407f 365 goto out_type;
e924960d
AD
366 rv = xfrm6_tunnel_register(&xfrm6_tunnel_handler, AF_INET6);
367 if (rv < 0)
d5aa407f 368 goto out_xfrm6;
e924960d
AD
369 rv = xfrm6_tunnel_register(&xfrm46_tunnel_handler, AF_INET);
370 if (rv < 0)
d5aa407f 371 goto out_xfrm46;
1da177e4 372 return 0;
5ce1bbb9 373
d5aa407f 374out_xfrm46:
5ce1bbb9 375 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
d5aa407f 376out_xfrm6:
5ce1bbb9 377 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
d5aa407f
AD
378out_type:
379 unregister_pernet_subsys(&xfrm6_tunnel_net_ops);
380out_pernet:
381 kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
e924960d 382 return rv;
1da177e4
LT
383}
384
385static void __exit xfrm6_tunnel_fini(void)
386{
73d605d1
KM
387 xfrm6_tunnel_deregister(&xfrm46_tunnel_handler, AF_INET);
388 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
a922ba55 389 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
d5aa407f
AD
390 unregister_pernet_subsys(&xfrm6_tunnel_net_ops);
391 kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
1da177e4
LT
392}
393
394module_init(xfrm6_tunnel_init);
395module_exit(xfrm6_tunnel_fini);
396MODULE_LICENSE("GPL");
d3d6dd3a 397MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_IPV6);