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