Blame view
net/ipv6/xfrm6_tunnel.c
10.2 KB
1da177e4c
|
1 2 3 4 5 6 7 |
/* * Copyright (C)2003,2004 USAGI/WIDE Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. |
1ab1457c4
|
8 |
* |
1da177e4c
|
9 10 11 12 |
* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. |
1ab1457c4
|
13 |
* |
1da177e4c
|
14 |
* You should have received a copy of the GNU General Public License |
a99421d9b
|
15 |
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
1da177e4c
|
16 17 |
* * Authors Mitsuru KANDA <mk@linux-ipv6.org> |
67ba4152e
|
18 |
* YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> |
1da177e4c
|
19 20 21 22 |
* * Based on net/ipv4/xfrm4_tunnel.c * */ |
1da177e4c
|
23 24 |
#include <linux/module.h> #include <linux/xfrm.h> |
5a0e3ad6a
|
25 |
#include <linux/slab.h> |
91cc3bb0b
|
26 |
#include <linux/rculist.h> |
1da177e4c
|
27 28 29 |
#include <net/ip.h> #include <net/xfrm.h> #include <net/ipv6.h> |
1da177e4c
|
30 31 |
#include <linux/ipv6.h> #include <linux/icmpv6.h> |
4a3e2f711
|
32 |
#include <linux/mutex.h> |
a16647739
|
33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <net/netns/generic.h> #define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256 #define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256 #define XFRM6_TUNNEL_SPI_MIN 1 #define XFRM6_TUNNEL_SPI_MAX 0xffffffff struct xfrm6_tunnel_net { struct hlist_head spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE]; struct hlist_head spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE]; u32 spi; }; |
c7d03a00b
|
46 |
static unsigned int xfrm6_tunnel_net_id __read_mostly; |
a16647739
|
47 48 49 50 |
static inline struct xfrm6_tunnel_net *xfrm6_tunnel_pernet(struct net *net) { return net_generic(net, xfrm6_tunnel_net_id); } |
1da177e4c
|
51 |
|
1da177e4c
|
52 |
/* |
1ab1457c4
|
53 |
* xfrm_tunnel_spi things are for allocating unique id ("spi") |
1da177e4c
|
54 55 56 |
* per xfrm_address_t. */ struct xfrm6_tunnel_spi { |
91cc3bb0b
|
57 58 59 60 |
struct hlist_node list_byaddr; struct hlist_node list_byspi; xfrm_address_t addr; u32 spi; |
d12f3827e
|
61 |
refcount_t refcnt; |
91cc3bb0b
|
62 |
struct rcu_head rcu_head; |
1da177e4c
|
63 |
}; |
91cc3bb0b
|
64 |
static DEFINE_SPINLOCK(xfrm6_tunnel_spi_lock); |
1da177e4c
|
65 |
|
e18b890bb
|
66 |
static struct kmem_cache *xfrm6_tunnel_spi_kmem __read_mostly; |
1da177e4c
|
67 |
|
95c961747
|
68 |
static inline unsigned int xfrm6_tunnel_spi_hash_byaddr(const xfrm_address_t *addr) |
1da177e4c
|
69 |
{ |
95c961747
|
70 |
unsigned int h; |
1da177e4c
|
71 |
|
2b464f61f
|
72 |
h = ipv6_addr_hash((const struct in6_addr *)addr); |
1da177e4c
|
73 74 75 |
h ^= h >> 16; h ^= h >> 8; h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1; |
1da177e4c
|
76 77 |
return h; } |
95c961747
|
78 |
static inline unsigned int xfrm6_tunnel_spi_hash_byspi(u32 spi) |
1da177e4c
|
79 80 81 |
{ return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE; } |
b71d1d426
|
82 |
static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr) |
1da177e4c
|
83 |
{ |
a16647739
|
84 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); |
1da177e4c
|
85 |
struct xfrm6_tunnel_spi *x6spi; |
1da177e4c
|
86 |
|
b67bfe0d4
|
87 |
hlist_for_each_entry_rcu(x6spi, |
a16647739
|
88 |
&xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)], |
1da177e4c
|
89 |
list_byaddr) { |
ff88b30c7
|
90 |
if (xfrm6_addr_equal(&x6spi->addr, saddr)) |
1da177e4c
|
91 |
return x6spi; |
1da177e4c
|
92 |
} |
1da177e4c
|
93 94 |
return NULL; } |
b71d1d426
|
95 |
__be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr) |
1da177e4c
|
96 97 98 |
{ struct xfrm6_tunnel_spi *x6spi; u32 spi; |
91cc3bb0b
|
99 |
rcu_read_lock_bh(); |
a16647739
|
100 |
x6spi = __xfrm6_tunnel_spi_lookup(net, saddr); |
1da177e4c
|
101 |
spi = x6spi ? x6spi->spi : 0; |
91cc3bb0b
|
102 |
rcu_read_unlock_bh(); |
5b1225454
|
103 |
return htonl(spi); |
1da177e4c
|
104 |
} |
1da177e4c
|
105 |
EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup); |
a16647739
|
106 |
static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) |
df8ea19b5
|
107 |
{ |
a16647739
|
108 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); |
df8ea19b5
|
109 110 |
struct xfrm6_tunnel_spi *x6spi; int index = xfrm6_tunnel_spi_hash_byspi(spi); |
df8ea19b5
|
111 |
|
b67bfe0d4
|
112 |
hlist_for_each_entry(x6spi, |
a16647739
|
113 |
&xfrm6_tn->spi_byspi[index], |
df8ea19b5
|
114 115 116 117 118 119 |
list_byspi) { if (x6spi->spi == spi) return -1; } return index; } |
a16647739
|
120 |
static u32 __xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr) |
1da177e4c
|
121 |
{ |
a16647739
|
122 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); |
1da177e4c
|
123 124 |
u32 spi; struct xfrm6_tunnel_spi *x6spi; |
df8ea19b5
|
125 |
int index; |
1da177e4c
|
126 |
|
a16647739
|
127 128 129 |
if (xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN || xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX) xfrm6_tn->spi = XFRM6_TUNNEL_SPI_MIN; |
1da177e4c
|
130 |
else |
a16647739
|
131 |
xfrm6_tn->spi++; |
1da177e4c
|
132 |
|
a16647739
|
133 134 |
for (spi = xfrm6_tn->spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) { index = __xfrm6_tunnel_spi_check(net, spi); |
df8ea19b5
|
135 136 |
if (index >= 0) goto alloc_spi; |
1552557b3
|
137 138 139 |
if (spi == XFRM6_TUNNEL_SPI_MAX) break; |
1da177e4c
|
140 |
} |
a16647739
|
141 142 |
for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tn->spi; spi++) { index = __xfrm6_tunnel_spi_check(net, spi); |
df8ea19b5
|
143 144 |
if (index >= 0) goto alloc_spi; |
1da177e4c
|
145 146 147 148 |
} spi = 0; goto out; alloc_spi: |
a16647739
|
149 |
xfrm6_tn->spi = spi; |
54e6ecb23
|
150 |
x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, GFP_ATOMIC); |
a922ba551
|
151 |
if (!x6spi) |
1da177e4c
|
152 |
goto out; |
a922ba551
|
153 |
|
1da177e4c
|
154 155 |
memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr)); x6spi->spi = spi; |
d12f3827e
|
156 |
refcount_set(&x6spi->refcnt, 1); |
1da177e4c
|
157 |
|
a16647739
|
158 |
hlist_add_head_rcu(&x6spi->list_byspi, &xfrm6_tn->spi_byspi[index]); |
1da177e4c
|
159 160 |
index = xfrm6_tunnel_spi_hash_byaddr(saddr); |
a16647739
|
161 |
hlist_add_head_rcu(&x6spi->list_byaddr, &xfrm6_tn->spi_byaddr[index]); |
1da177e4c
|
162 |
out: |
1da177e4c
|
163 164 |
return spi; } |
a16647739
|
165 |
__be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr) |
1da177e4c
|
166 167 168 |
{ struct xfrm6_tunnel_spi *x6spi; u32 spi; |
91cc3bb0b
|
169 |
spin_lock_bh(&xfrm6_tunnel_spi_lock); |
a16647739
|
170 |
x6spi = __xfrm6_tunnel_spi_lookup(net, saddr); |
1da177e4c
|
171 |
if (x6spi) { |
d12f3827e
|
172 |
refcount_inc(&x6spi->refcnt); |
1da177e4c
|
173 174 |
spi = x6spi->spi; } else |
a16647739
|
175 |
spi = __xfrm6_tunnel_alloc_spi(net, saddr); |
91cc3bb0b
|
176 |
spin_unlock_bh(&xfrm6_tunnel_spi_lock); |
1da177e4c
|
177 |
|
5b1225454
|
178 |
return htonl(spi); |
1da177e4c
|
179 |
} |
1da177e4c
|
180 |
EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi); |
91cc3bb0b
|
181 182 183 184 185 |
static void x6spi_destroy_rcu(struct rcu_head *head) { kmem_cache_free(xfrm6_tunnel_spi_kmem, container_of(head, struct xfrm6_tunnel_spi, rcu_head)); } |
6f747aca5
|
186 |
static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr) |
1da177e4c
|
187 |
{ |
a16647739
|
188 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); |
1da177e4c
|
189 |
struct xfrm6_tunnel_spi *x6spi; |
b67bfe0d4
|
190 |
struct hlist_node *n; |
1da177e4c
|
191 |
|
91cc3bb0b
|
192 |
spin_lock_bh(&xfrm6_tunnel_spi_lock); |
1da177e4c
|
193 |
|
b67bfe0d4
|
194 |
hlist_for_each_entry_safe(x6spi, n, |
a16647739
|
195 |
&xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)], |
1da177e4c
|
196 197 |
list_byaddr) { |
ff88b30c7
|
198 |
if (xfrm6_addr_equal(&x6spi->addr, saddr)) { |
d12f3827e
|
199 |
if (refcount_dec_and_test(&x6spi->refcnt)) { |
91cc3bb0b
|
200 201 202 |
hlist_del_rcu(&x6spi->list_byaddr); hlist_del_rcu(&x6spi->list_byspi); call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu); |
1da177e4c
|
203 204 205 206 |
break; } } } |
91cc3bb0b
|
207 |
spin_unlock_bh(&xfrm6_tunnel_spi_lock); |
1da177e4c
|
208 |
} |
1da177e4c
|
209 210 |
static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb) { |
7b277b1a5
|
211 |
skb_push(skb, -skb_network_offset(skb)); |
1da177e4c
|
212 213 |
return 0; } |
e695633e2
|
214 |
static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) |
1da177e4c
|
215 |
{ |
04663d0b8
|
216 |
return skb_network_header(skb)[IP6CB(skb)->nhoff]; |
1da177e4c
|
217 |
} |
d2acc3479
|
218 |
static int xfrm6_tunnel_rcv(struct sk_buff *skb) |
1da177e4c
|
219 |
{ |
a16647739
|
220 |
struct net *net = dev_net(skb->dev); |
b71d1d426
|
221 |
const struct ipv6hdr *iph = ipv6_hdr(skb); |
a252cc237
|
222 |
__be32 spi; |
1da177e4c
|
223 |
|
b71d1d426
|
224 |
spi = xfrm6_tunnel_spi_lookup(net, (const xfrm_address_t *)&iph->saddr); |
63c43787d
|
225 |
return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL); |
1da177e4c
|
226 |
} |
d2acc3479
|
227 |
static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
d5fdd6bab
|
228 |
u8 type, u8 code, int offset, __be32 info) |
1da177e4c
|
229 |
{ |
1da177e4c
|
230 231 |
/* xfrm6_tunnel native err handling */ switch (type) { |
1ab1457c4
|
232 |
case ICMPV6_DEST_UNREACH: |
1da177e4c
|
233 |
switch (code) { |
1ab1457c4
|
234 |
case ICMPV6_NOROUTE: |
1da177e4c
|
235 236 237 238 239 |
case ICMPV6_ADM_PROHIBITED: case ICMPV6_NOT_NEIGHBOUR: case ICMPV6_ADDR_UNREACH: case ICMPV6_PORT_UNREACH: default: |
1da177e4c
|
240 241 242 243 |
break; } break; case ICMPV6_PKT_TOOBIG: |
1da177e4c
|
244 245 246 247 |
break; case ICMPV6_TIME_EXCEED: switch (code) { case ICMPV6_EXC_HOPLIMIT: |
1da177e4c
|
248 249 |
break; case ICMPV6_EXC_FRAGTIME: |
1ab1457c4
|
250 |
default: |
1da177e4c
|
251 252 253 254 255 256 257 258 259 260 261 262 263 |
break; } break; case ICMPV6_PARAMPROB: switch (code) { case ICMPV6_HDR_FIELD: break; case ICMPV6_UNK_NEXTHDR: break; case ICMPV6_UNK_OPTION: break; } break; default: break; } |
d2acc3479
|
264 265 |
return 0; |
1da177e4c
|
266 |
} |
72cb6962a
|
267 |
static int xfrm6_tunnel_init_state(struct xfrm_state *x) |
1da177e4c
|
268 |
{ |
7e49e6de3
|
269 |
if (x->props.mode != XFRM_MODE_TUNNEL) |
1da177e4c
|
270 271 272 273 274 275 276 277 278 279 280 281 |
return -EINVAL; if (x->encap) return -EINVAL; x->props.header_len = sizeof(struct ipv6hdr); return 0; } static void xfrm6_tunnel_destroy(struct xfrm_state *x) { |
a16647739
|
282 283 284 |
struct net *net = xs_net(x); xfrm6_tunnel_free_spi(net, (xfrm_address_t *)&x->props.saddr); |
1da177e4c
|
285 |
} |
533cb5b0a
|
286 |
static const struct xfrm_type xfrm6_tunnel_type = { |
1da177e4c
|
287 288 289 290 291 292 293 294 |
.description = "IP6IP6", .owner = THIS_MODULE, .proto = IPPROTO_IPV6, .init_state = xfrm6_tunnel_init_state, .destructor = xfrm6_tunnel_destroy, .input = xfrm6_tunnel_input, .output = xfrm6_tunnel_output, }; |
3ff2cfa55
|
295 |
static struct xfrm6_tunnel xfrm6_tunnel_handler __read_mostly = { |
1da177e4c
|
296 |
.handler = xfrm6_tunnel_rcv, |
d2acc3479
|
297 298 |
.err_handler = xfrm6_tunnel_err, .priority = 2, |
1da177e4c
|
299 |
}; |
3ff2cfa55
|
300 |
static struct xfrm6_tunnel xfrm46_tunnel_handler __read_mostly = { |
73d605d1a
|
301 302 303 304 |
.handler = xfrm6_tunnel_rcv, .err_handler = xfrm6_tunnel_err, .priority = 2, }; |
a16647739
|
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
static int __net_init xfrm6_tunnel_net_init(struct net *net) { struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); unsigned int i; for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) INIT_HLIST_HEAD(&xfrm6_tn->spi_byaddr[i]); for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++) INIT_HLIST_HEAD(&xfrm6_tn->spi_byspi[i]); xfrm6_tn->spi = 0; return 0; } static void __net_exit xfrm6_tunnel_net_exit(struct net *net) { |
baeb0dbbb
|
321 322 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); unsigned int i; |
b48c05ab5
|
323 324 |
xfrm_state_flush(net, IPSEC_PROTO_ANY, false); xfrm_flush_gc(); |
baeb0dbbb
|
325 326 327 328 329 |
for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++) WARN_ON_ONCE(!hlist_empty(&xfrm6_tn->spi_byaddr[i])); for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++) WARN_ON_ONCE(!hlist_empty(&xfrm6_tn->spi_byspi[i])); |
a16647739
|
330 331 332 333 334 335 336 337 |
} static struct pernet_operations xfrm6_tunnel_net_ops = { .init = xfrm6_tunnel_net_init, .exit = xfrm6_tunnel_net_exit, .id = &xfrm6_tunnel_net_id, .size = sizeof(struct xfrm6_tunnel_net), }; |
1da177e4c
|
338 339 |
static int __init xfrm6_tunnel_init(void) { |
e924960da
|
340 |
int rv; |
d5aa407f5
|
341 342 343 344 345 346 347 348 349 |
xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi", sizeof(struct xfrm6_tunnel_spi), 0, SLAB_HWCACHE_ALIGN, NULL); if (!xfrm6_tunnel_spi_kmem) return -ENOMEM; rv = register_pernet_subsys(&xfrm6_tunnel_net_ops); if (rv < 0) goto out_pernet; |
e924960da
|
350 351 |
rv = xfrm_register_type(&xfrm6_tunnel_type, AF_INET6); if (rv < 0) |
d5aa407f5
|
352 |
goto out_type; |
e924960da
|
353 354 |
rv = xfrm6_tunnel_register(&xfrm6_tunnel_handler, AF_INET6); if (rv < 0) |
d5aa407f5
|
355 |
goto out_xfrm6; |
e924960da
|
356 357 |
rv = xfrm6_tunnel_register(&xfrm46_tunnel_handler, AF_INET); if (rv < 0) |
d5aa407f5
|
358 |
goto out_xfrm46; |
1da177e4c
|
359 |
return 0; |
5ce1bbb97
|
360 |
|
d5aa407f5
|
361 |
out_xfrm46: |
5ce1bbb97
|
362 |
xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6); |
d5aa407f5
|
363 |
out_xfrm6: |
5ce1bbb97
|
364 |
xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6); |
d5aa407f5
|
365 366 367 368 |
out_type: unregister_pernet_subsys(&xfrm6_tunnel_net_ops); out_pernet: kmem_cache_destroy(xfrm6_tunnel_spi_kmem); |
e924960da
|
369 |
return rv; |
1da177e4c
|
370 371 372 373 |
} static void __exit xfrm6_tunnel_fini(void) { |
73d605d1a
|
374 375 |
xfrm6_tunnel_deregister(&xfrm46_tunnel_handler, AF_INET); xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6); |
a922ba551
|
376 |
xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6); |
d5aa407f5
|
377 378 |
unregister_pernet_subsys(&xfrm6_tunnel_net_ops); kmem_cache_destroy(xfrm6_tunnel_spi_kmem); |
1da177e4c
|
379 380 381 382 383 |
} module_init(xfrm6_tunnel_init); module_exit(xfrm6_tunnel_fini); MODULE_LICENSE("GPL"); |
d3d6dd3ad
|
384 |
MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_IPV6); |