Blame view
net/ipv6/xfrm6_tunnel.c
9.93 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 15 16 17 18 19 20 21 22 23 |
* You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Authors Mitsuru KANDA <mk@linux-ipv6.org> * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> * * Based on net/ipv4/xfrm4_tunnel.c * */ |
1da177e4c
|
24 25 |
#include <linux/module.h> #include <linux/xfrm.h> |
5a0e3ad6a
|
26 |
#include <linux/slab.h> |
91cc3bb0b
|
27 |
#include <linux/rculist.h> |
1da177e4c
|
28 29 30 |
#include <net/ip.h> #include <net/xfrm.h> #include <net/ipv6.h> |
1da177e4c
|
31 32 |
#include <linux/ipv6.h> #include <linux/icmpv6.h> |
4a3e2f711
|
33 |
#include <linux/mutex.h> |
a16647739
|
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#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; }; static int xfrm6_tunnel_net_id __read_mostly; static inline struct xfrm6_tunnel_net *xfrm6_tunnel_pernet(struct net *net) { return net_generic(net, xfrm6_tunnel_net_id); } |
1da177e4c
|
53 |
|
1da177e4c
|
54 |
/* |
1ab1457c4
|
55 |
* xfrm_tunnel_spi things are for allocating unique id ("spi") |
1da177e4c
|
56 57 58 |
* per xfrm_address_t. */ struct xfrm6_tunnel_spi { |
91cc3bb0b
|
59 60 61 62 63 64 |
struct hlist_node list_byaddr; struct hlist_node list_byspi; xfrm_address_t addr; u32 spi; atomic_t refcnt; struct rcu_head rcu_head; |
1da177e4c
|
65 |
}; |
91cc3bb0b
|
66 |
static DEFINE_SPINLOCK(xfrm6_tunnel_spi_lock); |
1da177e4c
|
67 |
|
e18b890bb
|
68 |
static struct kmem_cache *xfrm6_tunnel_spi_kmem __read_mostly; |
1da177e4c
|
69 |
|
b71d1d426
|
70 |
static inline unsigned xfrm6_tunnel_spi_hash_byaddr(const xfrm_address_t *addr) |
1da177e4c
|
71 72 |
{ unsigned h; |
8c689a6ea
|
73 |
h = (__force u32)(addr->a6[0] ^ addr->a6[1] ^ addr->a6[2] ^ addr->a6[3]); |
1da177e4c
|
74 75 76 |
h ^= h >> 16; h ^= h >> 8; h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1; |
1da177e4c
|
77 78 |
return h; } |
b6f99a211
|
79 |
static inline unsigned xfrm6_tunnel_spi_hash_byspi(u32 spi) |
1da177e4c
|
80 81 82 |
{ return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE; } |
b71d1d426
|
83 |
static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr) |
1da177e4c
|
84 |
{ |
a16647739
|
85 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); |
1da177e4c
|
86 87 |
struct xfrm6_tunnel_spi *x6spi; struct hlist_node *pos; |
91cc3bb0b
|
88 |
hlist_for_each_entry_rcu(x6spi, pos, |
a16647739
|
89 |
&xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)], |
1da177e4c
|
90 |
list_byaddr) { |
a922ba551
|
91 |
if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) |
1da177e4c
|
92 |
return x6spi; |
1da177e4c
|
93 |
} |
1da177e4c
|
94 95 |
return NULL; } |
b71d1d426
|
96 |
__be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr) |
1da177e4c
|
97 98 99 |
{ struct xfrm6_tunnel_spi *x6spi; u32 spi; |
91cc3bb0b
|
100 |
rcu_read_lock_bh(); |
a16647739
|
101 |
x6spi = __xfrm6_tunnel_spi_lookup(net, saddr); |
1da177e4c
|
102 |
spi = x6spi ? x6spi->spi : 0; |
91cc3bb0b
|
103 |
rcu_read_unlock_bh(); |
5b1225454
|
104 |
return htonl(spi); |
1da177e4c
|
105 106 107 |
} EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup); |
a16647739
|
108 |
static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi) |
df8ea19b5
|
109 |
{ |
a16647739
|
110 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); |
df8ea19b5
|
111 112 113 114 115 |
struct xfrm6_tunnel_spi *x6spi; int index = xfrm6_tunnel_spi_hash_byspi(spi); struct hlist_node *pos; hlist_for_each_entry(x6spi, pos, |
a16647739
|
116 |
&xfrm6_tn->spi_byspi[index], |
df8ea19b5
|
117 118 119 120 121 122 |
list_byspi) { if (x6spi->spi == spi) return -1; } return index; } |
a16647739
|
123 |
static u32 __xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr) |
1da177e4c
|
124 |
{ |
a16647739
|
125 |
struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net); |
1da177e4c
|
126 127 |
u32 spi; struct xfrm6_tunnel_spi *x6spi; |
df8ea19b5
|
128 |
int index; |
1da177e4c
|
129 |
|
a16647739
|
130 131 132 |
if (xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN || xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX) xfrm6_tn->spi = XFRM6_TUNNEL_SPI_MIN; |
1da177e4c
|
133 |
else |
a16647739
|
134 |
xfrm6_tn->spi++; |
1da177e4c
|
135 |
|
a16647739
|
136 137 |
for (spi = xfrm6_tn->spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) { index = __xfrm6_tunnel_spi_check(net, spi); |
df8ea19b5
|
138 139 |
if (index >= 0) goto alloc_spi; |
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 156 |
memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr)); x6spi->spi = spi; atomic_set(&x6spi->refcnt, 1); |
a16647739
|
157 |
hlist_add_head_rcu(&x6spi->list_byspi, &xfrm6_tn->spi_byspi[index]); |
1da177e4c
|
158 159 |
index = xfrm6_tunnel_spi_hash_byaddr(saddr); |
a16647739
|
160 |
hlist_add_head_rcu(&x6spi->list_byaddr, &xfrm6_tn->spi_byaddr[index]); |
1da177e4c
|
161 |
out: |
1da177e4c
|
162 163 |
return spi; } |
a16647739
|
164 |
__be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr) |
1da177e4c
|
165 166 167 |
{ struct xfrm6_tunnel_spi *x6spi; u32 spi; |
91cc3bb0b
|
168 |
spin_lock_bh(&xfrm6_tunnel_spi_lock); |
a16647739
|
169 |
x6spi = __xfrm6_tunnel_spi_lookup(net, saddr); |
1da177e4c
|
170 171 172 173 |
if (x6spi) { atomic_inc(&x6spi->refcnt); spi = x6spi->spi; } else |
a16647739
|
174 |
spi = __xfrm6_tunnel_alloc_spi(net, saddr); |
91cc3bb0b
|
175 |
spin_unlock_bh(&xfrm6_tunnel_spi_lock); |
1da177e4c
|
176 |
|
5b1225454
|
177 |
return htonl(spi); |
1da177e4c
|
178 179 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 190 |
struct xfrm6_tunnel_spi *x6spi; struct hlist_node *pos, *n; |
91cc3bb0b
|
191 |
spin_lock_bh(&xfrm6_tunnel_spi_lock); |
1da177e4c
|
192 |
|
1ab1457c4
|
193 |
hlist_for_each_entry_safe(x6spi, pos, n, |
a16647739
|
194 |
&xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)], |
1da177e4c
|
195 196 197 |
list_byaddr) { if (memcmp(&x6spi->addr, saddr, sizeof(x6spi->addr)) == 0) { |
1da177e4c
|
198 |
if (atomic_dec_and_test(&x6spi->refcnt)) { |
91cc3bb0b
|
199 200 201 |
hlist_del_rcu(&x6spi->list_byaddr); hlist_del_rcu(&x6spi->list_byspi); call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu); |
1da177e4c
|
202 203 204 205 |
break; } } } |
91cc3bb0b
|
206 |
spin_unlock_bh(&xfrm6_tunnel_spi_lock); |
1da177e4c
|
207 |
} |
1da177e4c
|
208 209 |
static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb) { |
7b277b1a5
|
210 |
skb_push(skb, -skb_network_offset(skb)); |
1da177e4c
|
211 212 |
return 0; } |
e695633e2
|
213 |
static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) |
1da177e4c
|
214 |
{ |
04663d0b8
|
215 |
return skb_network_header(skb)[IP6CB(skb)->nhoff]; |
1da177e4c
|
216 |
} |
d2acc3479
|
217 |
static int xfrm6_tunnel_rcv(struct sk_buff *skb) |
1da177e4c
|
218 |
{ |
a16647739
|
219 |
struct net *net = dev_net(skb->dev); |
b71d1d426
|
220 |
const struct ipv6hdr *iph = ipv6_hdr(skb); |
a252cc237
|
221 |
__be32 spi; |
1da177e4c
|
222 |
|
b71d1d426
|
223 |
spi = xfrm6_tunnel_spi_lookup(net, (const xfrm_address_t *)&iph->saddr); |
6ac3f6649
|
224 |
return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi); |
1da177e4c
|
225 |
} |
d2acc3479
|
226 |
static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
d5fdd6bab
|
227 |
u8 type, u8 code, int offset, __be32 info) |
1da177e4c
|
228 |
{ |
1da177e4c
|
229 230 |
/* xfrm6_tunnel native err handling */ switch (type) { |
1ab1457c4
|
231 |
case ICMPV6_DEST_UNREACH: |
1da177e4c
|
232 |
switch (code) { |
1ab1457c4
|
233 |
case ICMPV6_NOROUTE: |
1da177e4c
|
234 235 236 237 238 |
case ICMPV6_ADM_PROHIBITED: case ICMPV6_NOT_NEIGHBOUR: case ICMPV6_ADDR_UNREACH: case ICMPV6_PORT_UNREACH: default: |
1da177e4c
|
239 240 241 242 |
break; } break; case ICMPV6_PKT_TOOBIG: |
1da177e4c
|
243 244 245 246 |
break; case ICMPV6_TIME_EXCEED: switch (code) { case ICMPV6_EXC_HOPLIMIT: |
1da177e4c
|
247 248 |
break; case ICMPV6_EXC_FRAGTIME: |
1ab1457c4
|
249 |
default: |
1da177e4c
|
250 251 252 253 254 255 256 257 258 259 260 261 262 |
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
|
263 264 |
return 0; |
1da177e4c
|
265 |
} |
72cb6962a
|
266 |
static int xfrm6_tunnel_init_state(struct xfrm_state *x) |
1da177e4c
|
267 |
{ |
7e49e6de3
|
268 |
if (x->props.mode != XFRM_MODE_TUNNEL) |
1da177e4c
|
269 270 271 272 273 274 275 276 277 278 279 280 |
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
|
281 282 283 |
struct net *net = xs_net(x); xfrm6_tunnel_free_spi(net, (xfrm_address_t *)&x->props.saddr); |
1da177e4c
|
284 |
} |
533cb5b0a
|
285 |
static const struct xfrm_type xfrm6_tunnel_type = { |
1da177e4c
|
286 287 288 289 290 291 292 293 |
.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
|
294 |
static struct xfrm6_tunnel xfrm6_tunnel_handler __read_mostly = { |
1da177e4c
|
295 |
.handler = xfrm6_tunnel_rcv, |
d2acc3479
|
296 297 |
.err_handler = xfrm6_tunnel_err, .priority = 2, |
1da177e4c
|
298 |
}; |
3ff2cfa55
|
299 |
static struct xfrm6_tunnel xfrm46_tunnel_handler __read_mostly = { |
73d605d1a
|
300 301 302 303 |
.handler = xfrm6_tunnel_rcv, .err_handler = xfrm6_tunnel_err, .priority = 2, }; |
a16647739
|
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
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) { } 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
|
328 329 |
static int __init xfrm6_tunnel_init(void) { |
e924960da
|
330 |
int rv; |
d5aa407f5
|
331 332 333 334 335 336 337 338 339 |
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
|
340 341 |
rv = xfrm_register_type(&xfrm6_tunnel_type, AF_INET6); if (rv < 0) |
d5aa407f5
|
342 |
goto out_type; |
e924960da
|
343 344 |
rv = xfrm6_tunnel_register(&xfrm6_tunnel_handler, AF_INET6); if (rv < 0) |
d5aa407f5
|
345 |
goto out_xfrm6; |
e924960da
|
346 347 |
rv = xfrm6_tunnel_register(&xfrm46_tunnel_handler, AF_INET); if (rv < 0) |
d5aa407f5
|
348 |
goto out_xfrm46; |
1da177e4c
|
349 |
return 0; |
5ce1bbb97
|
350 |
|
d5aa407f5
|
351 |
out_xfrm46: |
5ce1bbb97
|
352 |
xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6); |
d5aa407f5
|
353 |
out_xfrm6: |
5ce1bbb97
|
354 |
xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6); |
d5aa407f5
|
355 356 357 358 |
out_type: unregister_pernet_subsys(&xfrm6_tunnel_net_ops); out_pernet: kmem_cache_destroy(xfrm6_tunnel_spi_kmem); |
e924960da
|
359 |
return rv; |
1da177e4c
|
360 361 362 363 |
} static void __exit xfrm6_tunnel_fini(void) { |
73d605d1a
|
364 365 |
xfrm6_tunnel_deregister(&xfrm46_tunnel_handler, AF_INET); xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6); |
a922ba551
|
366 |
xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6); |
d5aa407f5
|
367 368 |
unregister_pernet_subsys(&xfrm6_tunnel_net_ops); kmem_cache_destroy(xfrm6_tunnel_spi_kmem); |
1da177e4c
|
369 370 371 372 373 |
} module_init(xfrm6_tunnel_init); module_exit(xfrm6_tunnel_fini); MODULE_LICENSE("GPL"); |
d3d6dd3ad
|
374 |
MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_IPV6); |