Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 over IPv6 tunnel device |
| 3 | * Linux INET6 implementation |
| 4 | * |
| 5 | * Authors: |
| 6 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
| 7 | * |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Based on: |
| 11 | * linux/net/ipv6/sit.c |
| 12 | * |
| 13 | * RFC 2473 |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 23 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/errno.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/sockios.h> |
| 27 | #include <linux/if.h> |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/ip.h> |
| 30 | #include <linux/if_tunnel.h> |
| 31 | #include <linux/net.h> |
| 32 | #include <linux/in6.h> |
| 33 | #include <linux/netdevice.h> |
| 34 | #include <linux/if_arp.h> |
| 35 | #include <linux/icmpv6.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/route.h> |
| 38 | #include <linux/rtnetlink.h> |
| 39 | #include <linux/netfilter_ipv6.h> |
| 40 | |
| 41 | #include <asm/uaccess.h> |
| 42 | #include <asm/atomic.h> |
| 43 | |
| 44 | #include <net/ip.h> |
| 45 | #include <net/ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <net/ip6_route.h> |
| 47 | #include <net/addrconf.h> |
| 48 | #include <net/ip6_tunnel.h> |
| 49 | #include <net/xfrm.h> |
| 50 | #include <net/dsfield.h> |
| 51 | #include <net/inet_ecn.h> |
| 52 | |
| 53 | MODULE_AUTHOR("Ville Nuorvala"); |
| 54 | MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel"); |
| 55 | MODULE_LICENSE("GPL"); |
| 56 | |
| 57 | #define IPV6_TLV_TEL_DST_SIZE 8 |
| 58 | |
| 59 | #ifdef IP6_TNL_DEBUG |
| 60 | #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__) |
| 61 | #else |
| 62 | #define IP6_TNL_TRACE(x...) do {;} while(0) |
| 63 | #endif |
| 64 | |
| 65 | #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) |
| 66 | |
| 67 | #define HASH_SIZE 32 |
| 68 | |
| 69 | #define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \ |
| 70 | (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \ |
| 71 | (HASH_SIZE - 1)) |
| 72 | |
| 73 | static int ip6ip6_fb_tnl_dev_init(struct net_device *dev); |
| 74 | static int ip6ip6_tnl_dev_init(struct net_device *dev); |
| 75 | static void ip6ip6_tnl_dev_setup(struct net_device *dev); |
| 76 | |
| 77 | /* the IPv6 tunnel fallback device */ |
| 78 | static struct net_device *ip6ip6_fb_tnl_dev; |
| 79 | |
| 80 | |
| 81 | /* lists for storing tunnels in use */ |
| 82 | static struct ip6_tnl *tnls_r_l[HASH_SIZE]; |
| 83 | static struct ip6_tnl *tnls_wc[1]; |
| 84 | static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l }; |
| 85 | |
| 86 | /* lock for the tunnel lists */ |
| 87 | static DEFINE_RWLOCK(ip6ip6_lock); |
| 88 | |
| 89 | static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t) |
| 90 | { |
| 91 | struct dst_entry *dst = t->dst_cache; |
| 92 | |
| 93 | if (dst && dst->obsolete && |
| 94 | dst->ops->check(dst, t->dst_cookie) == NULL) { |
| 95 | t->dst_cache = NULL; |
| 96 | dst_release(dst); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | return dst; |
| 101 | } |
| 102 | |
| 103 | static inline void ip6_tnl_dst_reset(struct ip6_tnl *t) |
| 104 | { |
| 105 | dst_release(t->dst_cache); |
| 106 | t->dst_cache = NULL; |
| 107 | } |
| 108 | |
| 109 | static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst) |
| 110 | { |
| 111 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 112 | t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; |
| 113 | dst_release(t->dst_cache); |
| 114 | t->dst_cache = dst; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
| 119 | * @remote: the address of the tunnel exit-point |
| 120 | * @local: the address of the tunnel entry-point |
| 121 | * |
| 122 | * Return: |
| 123 | * tunnel matching given end-points if found, |
| 124 | * else fallback tunnel if its device is up, |
| 125 | * else %NULL |
| 126 | **/ |
| 127 | |
| 128 | static struct ip6_tnl * |
| 129 | ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local) |
| 130 | { |
| 131 | unsigned h0 = HASH(remote); |
| 132 | unsigned h1 = HASH(local); |
| 133 | struct ip6_tnl *t; |
| 134 | |
| 135 | for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) { |
| 136 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 137 | ipv6_addr_equal(remote, &t->parms.raddr) && |
| 138 | (t->dev->flags & IFF_UP)) |
| 139 | return t; |
| 140 | } |
| 141 | if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP)) |
| 142 | return t; |
| 143 | |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * ip6ip6_bucket - get head of list matching given tunnel parameters |
| 149 | * @p: parameters containing tunnel end-points |
| 150 | * |
| 151 | * Description: |
| 152 | * ip6ip6_bucket() returns the head of the list matching the |
| 153 | * &struct in6_addr entries laddr and raddr in @p. |
| 154 | * |
| 155 | * Return: head of IPv6 tunnel list |
| 156 | **/ |
| 157 | |
| 158 | static struct ip6_tnl ** |
| 159 | ip6ip6_bucket(struct ip6_tnl_parm *p) |
| 160 | { |
| 161 | struct in6_addr *remote = &p->raddr; |
| 162 | struct in6_addr *local = &p->laddr; |
| 163 | unsigned h = 0; |
| 164 | int prio = 0; |
| 165 | |
| 166 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { |
| 167 | prio = 1; |
| 168 | h = HASH(remote) ^ HASH(local); |
| 169 | } |
| 170 | return &tnls[prio][h]; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * ip6ip6_tnl_link - add tunnel to hash table |
| 175 | * @t: tunnel to be added |
| 176 | **/ |
| 177 | |
| 178 | static void |
| 179 | ip6ip6_tnl_link(struct ip6_tnl *t) |
| 180 | { |
| 181 | struct ip6_tnl **tp = ip6ip6_bucket(&t->parms); |
| 182 | |
| 183 | t->next = *tp; |
| 184 | write_lock_bh(&ip6ip6_lock); |
| 185 | *tp = t; |
| 186 | write_unlock_bh(&ip6ip6_lock); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * ip6ip6_tnl_unlink - remove tunnel from hash table |
| 191 | * @t: tunnel to be removed |
| 192 | **/ |
| 193 | |
| 194 | static void |
| 195 | ip6ip6_tnl_unlink(struct ip6_tnl *t) |
| 196 | { |
| 197 | struct ip6_tnl **tp; |
| 198 | |
| 199 | for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) { |
| 200 | if (t == *tp) { |
| 201 | write_lock_bh(&ip6ip6_lock); |
| 202 | *tp = t->next; |
| 203 | write_unlock_bh(&ip6ip6_lock); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * ip6_tnl_create() - create a new tunnel |
| 211 | * @p: tunnel parameters |
| 212 | * @pt: pointer to new tunnel |
| 213 | * |
| 214 | * Description: |
| 215 | * Create tunnel matching given parameters. |
| 216 | * |
| 217 | * Return: |
| 218 | * 0 on success |
| 219 | **/ |
| 220 | |
| 221 | static int |
| 222 | ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt) |
| 223 | { |
| 224 | struct net_device *dev; |
| 225 | struct ip6_tnl *t; |
| 226 | char name[IFNAMSIZ]; |
| 227 | int err; |
| 228 | |
| 229 | if (p->name[0]) { |
| 230 | strlcpy(name, p->name, IFNAMSIZ); |
| 231 | } else { |
| 232 | int i; |
| 233 | for (i = 1; i < IP6_TNL_MAX; i++) { |
| 234 | sprintf(name, "ip6tnl%d", i); |
| 235 | if (__dev_get_by_name(name) == NULL) |
| 236 | break; |
| 237 | } |
| 238 | if (i == IP6_TNL_MAX) |
| 239 | return -ENOBUFS; |
| 240 | } |
| 241 | dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup); |
| 242 | if (dev == NULL) |
| 243 | return -ENOMEM; |
| 244 | |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 245 | t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | dev->init = ip6ip6_tnl_dev_init; |
| 247 | t->parms = *p; |
| 248 | |
| 249 | if ((err = register_netdevice(dev)) < 0) { |
| 250 | free_netdev(dev); |
| 251 | return err; |
| 252 | } |
| 253 | dev_hold(dev); |
| 254 | |
| 255 | ip6ip6_tnl_link(t); |
| 256 | *pt = t; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * ip6ip6_tnl_locate - find or create tunnel matching given parameters |
| 262 | * @p: tunnel parameters |
| 263 | * @create: != 0 if allowed to create new tunnel if no match found |
| 264 | * |
| 265 | * Description: |
| 266 | * ip6ip6_tnl_locate() first tries to locate an existing tunnel |
| 267 | * based on @parms. If this is unsuccessful, but @create is set a new |
| 268 | * tunnel device is created and registered for use. |
| 269 | * |
| 270 | * Return: |
| 271 | * 0 if tunnel located or created, |
| 272 | * -EINVAL if parameters incorrect, |
| 273 | * -ENODEV if no matching tunnel available |
| 274 | **/ |
| 275 | |
| 276 | static int |
| 277 | ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create) |
| 278 | { |
| 279 | struct in6_addr *remote = &p->raddr; |
| 280 | struct in6_addr *local = &p->laddr; |
| 281 | struct ip6_tnl *t; |
| 282 | |
| 283 | if (p->proto != IPPROTO_IPV6) |
| 284 | return -EINVAL; |
| 285 | |
| 286 | for (t = *ip6ip6_bucket(p); t; t = t->next) { |
| 287 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 288 | ipv6_addr_equal(remote, &t->parms.raddr)) { |
| 289 | *pt = t; |
| 290 | return (create ? -EEXIST : 0); |
| 291 | } |
| 292 | } |
| 293 | if (!create) |
| 294 | return -ENODEV; |
| 295 | |
| 296 | return ip6_tnl_create(p, pt); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * ip6ip6_tnl_dev_uninit - tunnel device uninitializer |
| 301 | * @dev: the device to be destroyed |
| 302 | * |
| 303 | * Description: |
| 304 | * ip6ip6_tnl_dev_uninit() removes tunnel from its list |
| 305 | **/ |
| 306 | |
| 307 | static void |
| 308 | ip6ip6_tnl_dev_uninit(struct net_device *dev) |
| 309 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 310 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | if (dev == ip6ip6_fb_tnl_dev) { |
| 313 | write_lock_bh(&ip6ip6_lock); |
| 314 | tnls_wc[0] = NULL; |
| 315 | write_unlock_bh(&ip6ip6_lock); |
| 316 | } else { |
| 317 | ip6ip6_tnl_unlink(t); |
| 318 | } |
| 319 | ip6_tnl_dst_reset(t); |
| 320 | dev_put(dev); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option |
| 325 | * @skb: received socket buffer |
| 326 | * |
| 327 | * Return: |
| 328 | * 0 if none was found, |
| 329 | * else index to encapsulation limit |
| 330 | **/ |
| 331 | |
| 332 | static __u16 |
| 333 | parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw) |
| 334 | { |
| 335 | struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw; |
| 336 | __u8 nexthdr = ipv6h->nexthdr; |
| 337 | __u16 off = sizeof (*ipv6h); |
| 338 | |
| 339 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { |
| 340 | __u16 optlen = 0; |
| 341 | struct ipv6_opt_hdr *hdr; |
| 342 | if (raw + off + sizeof (*hdr) > skb->data && |
| 343 | !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr))) |
| 344 | break; |
| 345 | |
| 346 | hdr = (struct ipv6_opt_hdr *) (raw + off); |
| 347 | if (nexthdr == NEXTHDR_FRAGMENT) { |
| 348 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; |
| 349 | if (frag_hdr->frag_off) |
| 350 | break; |
| 351 | optlen = 8; |
| 352 | } else if (nexthdr == NEXTHDR_AUTH) { |
| 353 | optlen = (hdr->hdrlen + 2) << 2; |
| 354 | } else { |
| 355 | optlen = ipv6_optlen(hdr); |
| 356 | } |
| 357 | if (nexthdr == NEXTHDR_DEST) { |
| 358 | __u16 i = off + 2; |
| 359 | while (1) { |
| 360 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 361 | |
| 362 | /* No more room for encapsulation limit */ |
| 363 | if (i + sizeof (*tel) > off + optlen) |
| 364 | break; |
| 365 | |
| 366 | tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i]; |
| 367 | /* return index of option if found and valid */ |
| 368 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && |
| 369 | tel->length == 1) |
| 370 | return i; |
| 371 | /* else jump to next option */ |
| 372 | if (tel->type) |
| 373 | i += tel->length + 2; |
| 374 | else |
| 375 | i++; |
| 376 | } |
| 377 | } |
| 378 | nexthdr = hdr->nexthdr; |
| 379 | off += optlen; |
| 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * ip6ip6_err - tunnel error handler |
| 386 | * |
| 387 | * Description: |
| 388 | * ip6ip6_err() should handle errors in the tunnel according |
| 389 | * to the specifications in RFC 2473. |
| 390 | **/ |
| 391 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 392 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Al Viro | 04ce690 | 2006-11-08 00:21:01 -0800 | [diff] [blame] | 394 | int type, int code, int offset, __be32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
| 396 | struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data; |
| 397 | struct ip6_tnl *t; |
| 398 | int rel_msg = 0; |
| 399 | int rel_type = ICMPV6_DEST_UNREACH; |
| 400 | int rel_code = ICMPV6_ADDR_UNREACH; |
| 401 | __u32 rel_info = 0; |
| 402 | __u16 len; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 403 | int err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | /* If the packet doesn't contain the original IPv6 header we are |
| 406 | in trouble since we might need the source address for further |
| 407 | processing of the error. */ |
| 408 | |
| 409 | read_lock(&ip6ip6_lock); |
| 410 | if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL) |
| 411 | goto out; |
| 412 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 413 | err = 0; |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | switch (type) { |
| 416 | __u32 teli; |
| 417 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 418 | __u32 mtu; |
| 419 | case ICMPV6_DEST_UNREACH: |
| 420 | if (net_ratelimit()) |
| 421 | printk(KERN_WARNING |
| 422 | "%s: Path to destination invalid " |
| 423 | "or inactive!\n", t->parms.name); |
| 424 | rel_msg = 1; |
| 425 | break; |
| 426 | case ICMPV6_TIME_EXCEED: |
| 427 | if (code == ICMPV6_EXC_HOPLIMIT) { |
| 428 | if (net_ratelimit()) |
| 429 | printk(KERN_WARNING |
| 430 | "%s: Too small hop limit or " |
| 431 | "routing loop in tunnel!\n", |
| 432 | t->parms.name); |
| 433 | rel_msg = 1; |
| 434 | } |
| 435 | break; |
| 436 | case ICMPV6_PARAMPROB: |
| 437 | /* ignore if parameter problem not caused by a tunnel |
| 438 | encapsulation limit sub-option */ |
| 439 | if (code != ICMPV6_HDR_FIELD) { |
| 440 | break; |
| 441 | } |
| 442 | teli = parse_tlv_tnl_enc_lim(skb, skb->data); |
| 443 | |
| 444 | if (teli && teli == ntohl(info) - 2) { |
| 445 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
| 446 | if (tel->encap_limit == 0) { |
| 447 | if (net_ratelimit()) |
| 448 | printk(KERN_WARNING |
| 449 | "%s: Too small encapsulation " |
| 450 | "limit or routing loop in " |
| 451 | "tunnel!\n", t->parms.name); |
| 452 | rel_msg = 1; |
| 453 | } |
| 454 | } |
| 455 | break; |
| 456 | case ICMPV6_PKT_TOOBIG: |
| 457 | mtu = ntohl(info) - offset; |
| 458 | if (mtu < IPV6_MIN_MTU) |
| 459 | mtu = IPV6_MIN_MTU; |
| 460 | t->dev->mtu = mtu; |
| 461 | |
Al Viro | cc6cdac | 2006-02-18 16:02:18 -0500 | [diff] [blame] | 462 | if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | rel_type = ICMPV6_PKT_TOOBIG; |
| 464 | rel_code = 0; |
| 465 | rel_info = mtu; |
| 466 | rel_msg = 1; |
| 467 | } |
| 468 | break; |
| 469 | } |
| 470 | if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) { |
| 471 | struct rt6_info *rt; |
| 472 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
| 473 | if (!skb2) |
| 474 | goto out; |
| 475 | |
| 476 | dst_release(skb2->dst); |
| 477 | skb2->dst = NULL; |
| 478 | skb_pull(skb2, offset); |
| 479 | skb2->nh.raw = skb2->data; |
| 480 | |
| 481 | /* Try to guess incoming interface */ |
| 482 | rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0); |
| 483 | |
| 484 | if (rt && rt->rt6i_dev) |
| 485 | skb2->dev = rt->rt6i_dev; |
| 486 | |
| 487 | icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev); |
| 488 | |
| 489 | if (rt) |
| 490 | dst_release(&rt->u.dst); |
| 491 | |
| 492 | kfree_skb(skb2); |
| 493 | } |
| 494 | out: |
| 495 | read_unlock(&ip6ip6_lock); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 496 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph, |
| 500 | struct sk_buff *skb) |
| 501 | { |
| 502 | struct ipv6hdr *inner_iph = skb->nh.ipv6h; |
| 503 | |
| 504 | if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph))) |
| 505 | IP6_ECN_set_ce(inner_iph); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally |
| 510 | * @skb: received socket buffer |
| 511 | * |
| 512 | * Return: 0 |
| 513 | **/ |
| 514 | |
| 515 | static int |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 516 | ip6ip6_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | struct ipv6hdr *ipv6h; |
| 519 | struct ip6_tnl *t; |
| 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | ipv6h = skb->nh.ipv6h; |
| 522 | |
| 523 | read_lock(&ip6ip6_lock); |
| 524 | |
| 525 | if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) { |
| 526 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
Ken-ichirou MATSUZAWA | 9f0ede5 | 2005-11-09 13:08:29 -0800 | [diff] [blame] | 527 | read_unlock(&ip6ip6_lock); |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 528 | goto discard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) { |
| 532 | t->stat.rx_dropped++; |
| 533 | read_unlock(&ip6ip6_lock); |
| 534 | goto discard; |
| 535 | } |
| 536 | secpath_reset(skb); |
| 537 | skb->mac.raw = skb->nh.raw; |
| 538 | skb->nh.raw = skb->data; |
| 539 | skb->protocol = htons(ETH_P_IPV6); |
| 540 | skb->pkt_type = PACKET_HOST; |
| 541 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); |
| 542 | skb->dev = t->dev; |
| 543 | dst_release(skb->dst); |
| 544 | skb->dst = NULL; |
Yasuyuki Kozakai | 53ab61c | 2006-11-06 10:06:23 -0800 | [diff] [blame] | 545 | nf_reset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
| 547 | ipv6_copy_dscp(ipv6h, skb->nh.ipv6h); |
| 548 | ip6ip6_ecn_decapsulate(ipv6h, skb); |
| 549 | t->stat.rx_packets++; |
| 550 | t->stat.rx_bytes += skb->len; |
| 551 | netif_rx(skb); |
| 552 | read_unlock(&ip6ip6_lock); |
| 553 | return 0; |
| 554 | } |
| 555 | read_unlock(&ip6ip6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | return 1; |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 557 | |
| 558 | discard: |
| 559 | kfree_skb(skb); |
| 560 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static inline struct ipv6_txoptions *create_tel(__u8 encap_limit) |
| 564 | { |
| 565 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 566 | struct ipv6_txoptions *opt; |
| 567 | __u8 *raw; |
| 568 | |
| 569 | int opt_len = sizeof(*opt) + 8; |
| 570 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 571 | if (!(opt = kzalloc(opt_len, GFP_ATOMIC))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | return NULL; |
| 573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | opt->tot_len = opt_len; |
| 575 | opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1); |
| 576 | opt->opt_nflen = 8; |
| 577 | |
| 578 | tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1); |
| 579 | tel->type = IPV6_TLV_TNL_ENCAP_LIMIT; |
| 580 | tel->length = 1; |
| 581 | tel->encap_limit = encap_limit; |
| 582 | |
| 583 | raw = (__u8 *) opt->dst0opt; |
| 584 | raw[5] = IPV6_TLV_PADN; |
| 585 | raw[6] = 1; |
| 586 | |
| 587 | return opt; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
| 592 | * @t: the outgoing tunnel device |
| 593 | * @hdr: IPv6 header from the incoming packet |
| 594 | * |
| 595 | * Description: |
| 596 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
| 597 | * doesn't match source of incoming packet. |
| 598 | * |
| 599 | * Return: |
| 600 | * 1 if conflict, |
| 601 | * 0 else |
| 602 | **/ |
| 603 | |
| 604 | static inline int |
| 605 | ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr) |
| 606 | { |
| 607 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * ip6ip6_tnl_xmit - encapsulate packet and send |
| 612 | * @skb: the outgoing socket buffer |
| 613 | * @dev: the outgoing tunnel device |
| 614 | * |
| 615 | * Description: |
| 616 | * Build new header and do some sanity checks on the packet before sending |
| 617 | * it. |
| 618 | * |
| 619 | * Return: |
| 620 | * 0 |
| 621 | **/ |
| 622 | |
| 623 | static int |
| 624 | ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 625 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 626 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | struct net_device_stats *stats = &t->stat; |
| 628 | struct ipv6hdr *ipv6h = skb->nh.ipv6h; |
| 629 | struct ipv6_txoptions *opt = NULL; |
| 630 | int encap_limit = -1; |
| 631 | __u16 offset; |
| 632 | struct flowi fl; |
| 633 | struct dst_entry *dst; |
| 634 | struct net_device *tdev; |
| 635 | int mtu; |
| 636 | int max_headroom = sizeof(struct ipv6hdr); |
| 637 | u8 proto; |
| 638 | int err; |
| 639 | int pkt_len; |
| 640 | int dsfield; |
| 641 | |
| 642 | if (t->recursion++) { |
| 643 | stats->collisions++; |
| 644 | goto tx_err; |
| 645 | } |
| 646 | if (skb->protocol != htons(ETH_P_IPV6) || |
| 647 | !(t->parms.flags & IP6_TNL_F_CAP_XMIT) || |
| 648 | ip6ip6_tnl_addr_conflict(t, ipv6h)) { |
| 649 | goto tx_err; |
| 650 | } |
| 651 | if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) { |
| 652 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 653 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset]; |
| 654 | if (tel->encap_limit == 0) { |
| 655 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
| 656 | ICMPV6_HDR_FIELD, offset + 2, skb->dev); |
| 657 | goto tx_err; |
| 658 | } |
| 659 | encap_limit = tel->encap_limit - 1; |
| 660 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { |
| 661 | encap_limit = t->parms.encap_limit; |
| 662 | } |
| 663 | memcpy(&fl, &t->fl, sizeof (fl)); |
| 664 | proto = fl.proto; |
| 665 | |
| 666 | dsfield = ipv6_get_dsfield(ipv6h); |
| 667 | if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)) |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 668 | fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 670 | fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL) |
| 673 | goto tx_err; |
| 674 | |
| 675 | if ((dst = ip6_tnl_dst_check(t)) != NULL) |
| 676 | dst_hold(dst); |
Patrick McHardy | a57ebc9 | 2005-09-08 14:27:47 -0700 | [diff] [blame] | 677 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | dst = ip6_route_output(NULL, &fl); |
| 679 | |
Patrick McHardy | a57ebc9 | 2005-09-08 14:27:47 -0700 | [diff] [blame] | 680 | if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0) |
| 681 | goto tx_err_link_failure; |
| 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | tdev = dst->dev; |
| 685 | |
| 686 | if (tdev == dev) { |
| 687 | stats->collisions++; |
| 688 | if (net_ratelimit()) |
| 689 | printk(KERN_WARNING |
| 690 | "%s: Local routing loop detected!\n", |
| 691 | t->parms.name); |
| 692 | goto tx_err_dst_release; |
| 693 | } |
| 694 | mtu = dst_mtu(dst) - sizeof (*ipv6h); |
| 695 | if (opt) { |
| 696 | max_headroom += 8; |
| 697 | mtu -= 8; |
| 698 | } |
| 699 | if (mtu < IPV6_MIN_MTU) |
| 700 | mtu = IPV6_MIN_MTU; |
| 701 | if (skb->dst && mtu < dst_mtu(skb->dst)) { |
| 702 | struct rt6_info *rt = (struct rt6_info *) skb->dst; |
| 703 | rt->rt6i_flags |= RTF_MODIFIED; |
| 704 | rt->u.dst.metrics[RTAX_MTU-1] = mtu; |
| 705 | } |
| 706 | if (skb->len > mtu) { |
| 707 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev); |
| 708 | goto tx_err_dst_release; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Okay, now see if we can stuff it in the buffer as-is. |
| 713 | */ |
| 714 | max_headroom += LL_RESERVED_SPACE(tdev); |
| 715 | |
| 716 | if (skb_headroom(skb) < max_headroom || |
| 717 | skb_cloned(skb) || skb_shared(skb)) { |
| 718 | struct sk_buff *new_skb; |
| 719 | |
| 720 | if (!(new_skb = skb_realloc_headroom(skb, max_headroom))) |
| 721 | goto tx_err_dst_release; |
| 722 | |
| 723 | if (skb->sk) |
| 724 | skb_set_owner_w(new_skb, skb->sk); |
| 725 | kfree_skb(skb); |
| 726 | skb = new_skb; |
| 727 | } |
| 728 | dst_release(skb->dst); |
| 729 | skb->dst = dst_clone(dst); |
| 730 | |
| 731 | skb->h.raw = skb->nh.raw; |
| 732 | |
| 733 | if (opt) |
| 734 | ipv6_push_nfrag_opts(skb, opt, &proto, NULL); |
| 735 | |
| 736 | skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr)); |
| 737 | ipv6h = skb->nh.ipv6h; |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 738 | *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | dsfield = INET_ECN_encapsulate(0, dsfield); |
| 740 | ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield); |
| 741 | ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 742 | ipv6h->hop_limit = t->parms.hop_limit; |
| 743 | ipv6h->nexthdr = proto; |
| 744 | ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src); |
| 745 | ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst); |
| 746 | nf_reset(skb); |
| 747 | pkt_len = skb->len; |
| 748 | err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, |
| 749 | skb->dst->dev, dst_output); |
| 750 | |
Gerrit Renker | b9df3cb | 2006-11-14 11:21:36 -0200 | [diff] [blame] | 751 | if (net_xmit_eval(err) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | stats->tx_bytes += pkt_len; |
| 753 | stats->tx_packets++; |
| 754 | } else { |
| 755 | stats->tx_errors++; |
| 756 | stats->tx_aborted_errors++; |
| 757 | } |
| 758 | ip6_tnl_dst_store(t, dst); |
| 759 | |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 760 | kfree(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
| 762 | t->recursion--; |
| 763 | return 0; |
| 764 | tx_err_link_failure: |
| 765 | stats->tx_carrier_errors++; |
| 766 | dst_link_failure(skb); |
| 767 | tx_err_dst_release: |
| 768 | dst_release(dst); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 769 | kfree(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | tx_err: |
| 771 | stats->tx_errors++; |
| 772 | stats->tx_dropped++; |
| 773 | kfree_skb(skb); |
| 774 | t->recursion--; |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static void ip6_tnl_set_cap(struct ip6_tnl *t) |
| 779 | { |
| 780 | struct ip6_tnl_parm *p = &t->parms; |
| 781 | struct in6_addr *laddr = &p->laddr; |
| 782 | struct in6_addr *raddr = &p->raddr; |
| 783 | int ltype = ipv6_addr_type(laddr); |
| 784 | int rtype = ipv6_addr_type(raddr); |
| 785 | |
| 786 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV); |
| 787 | |
| 788 | if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY && |
| 789 | ((ltype|rtype) & |
| 790 | (IPV6_ADDR_UNICAST| |
| 791 | IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL| |
| 792 | IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) { |
| 793 | struct net_device *ldev = NULL; |
| 794 | int l_ok = 1; |
| 795 | int r_ok = 1; |
| 796 | |
| 797 | if (p->link) |
| 798 | ldev = dev_get_by_index(p->link); |
| 799 | |
| 800 | if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0)) |
| 801 | l_ok = 0; |
| 802 | |
| 803 | if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0)) |
| 804 | r_ok = 0; |
| 805 | |
| 806 | if (l_ok && r_ok) { |
| 807 | if (ltype&IPV6_ADDR_UNICAST) |
| 808 | p->flags |= IP6_TNL_F_CAP_XMIT; |
| 809 | if (rtype&IPV6_ADDR_UNICAST) |
| 810 | p->flags |= IP6_TNL_F_CAP_RCV; |
| 811 | } |
| 812 | if (ldev) |
| 813 | dev_put(ldev); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | static void ip6ip6_tnl_link_config(struct ip6_tnl *t) |
| 818 | { |
| 819 | struct net_device *dev = t->dev; |
| 820 | struct ip6_tnl_parm *p = &t->parms; |
| 821 | struct flowi *fl = &t->fl; |
| 822 | |
| 823 | memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
| 824 | memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
| 825 | |
| 826 | /* Set up flowi template */ |
| 827 | ipv6_addr_copy(&fl->fl6_src, &p->laddr); |
| 828 | ipv6_addr_copy(&fl->fl6_dst, &p->raddr); |
| 829 | fl->oif = p->link; |
| 830 | fl->fl6_flowlabel = 0; |
| 831 | |
| 832 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) |
| 833 | fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
| 834 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
| 835 | fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
| 836 | |
| 837 | ip6_tnl_set_cap(t); |
| 838 | |
| 839 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) |
| 840 | dev->flags |= IFF_POINTOPOINT; |
| 841 | else |
| 842 | dev->flags &= ~IFF_POINTOPOINT; |
| 843 | |
| 844 | dev->iflink = p->link; |
| 845 | |
| 846 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
| 847 | struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr, |
| 848 | p->link, 0); |
| 849 | |
| 850 | if (rt == NULL) |
| 851 | return; |
| 852 | |
| 853 | if (rt->rt6i_dev) { |
| 854 | dev->hard_header_len = rt->rt6i_dev->hard_header_len + |
| 855 | sizeof (struct ipv6hdr); |
| 856 | |
| 857 | dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr); |
| 858 | |
| 859 | if (dev->mtu < IPV6_MIN_MTU) |
| 860 | dev->mtu = IPV6_MIN_MTU; |
| 861 | } |
| 862 | dst_release(&rt->u.dst); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * ip6ip6_tnl_change - update the tunnel parameters |
| 868 | * @t: tunnel to be changed |
| 869 | * @p: tunnel configuration parameters |
| 870 | * @active: != 0 if tunnel is ready for use |
| 871 | * |
| 872 | * Description: |
| 873 | * ip6ip6_tnl_change() updates the tunnel parameters |
| 874 | **/ |
| 875 | |
| 876 | static int |
| 877 | ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p) |
| 878 | { |
| 879 | ipv6_addr_copy(&t->parms.laddr, &p->laddr); |
| 880 | ipv6_addr_copy(&t->parms.raddr, &p->raddr); |
| 881 | t->parms.flags = p->flags; |
| 882 | t->parms.hop_limit = p->hop_limit; |
| 883 | t->parms.encap_limit = p->encap_limit; |
| 884 | t->parms.flowinfo = p->flowinfo; |
Gabor Fekete | 8181b8c | 2005-06-08 14:54:38 -0700 | [diff] [blame] | 885 | t->parms.link = p->link; |
Hugo Santos | 0c08889 | 2006-02-24 13:16:25 -0800 | [diff] [blame] | 886 | ip6_tnl_dst_reset(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | ip6ip6_tnl_link_config(t); |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace |
| 893 | * @dev: virtual device associated with tunnel |
| 894 | * @ifr: parameters passed from userspace |
| 895 | * @cmd: command to be performed |
| 896 | * |
| 897 | * Description: |
| 898 | * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels |
| 899 | * from userspace. |
| 900 | * |
| 901 | * The possible commands are the following: |
| 902 | * %SIOCGETTUNNEL: get tunnel parameters for device |
| 903 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters |
| 904 | * %SIOCCHGTUNNEL: change tunnel parameters to those given |
| 905 | * %SIOCDELTUNNEL: delete tunnel |
| 906 | * |
| 907 | * The fallback device "ip6tnl0", created during module |
| 908 | * initialization, can be used for creating other tunnel devices. |
| 909 | * |
| 910 | * Return: |
| 911 | * 0 on success, |
| 912 | * %-EFAULT if unable to copy data to or from userspace, |
| 913 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set |
| 914 | * %-EINVAL if passed tunnel parameters are invalid, |
| 915 | * %-EEXIST if changing a tunnel's parameters would cause a conflict |
| 916 | * %-ENODEV if attempting to change or delete a nonexisting device |
| 917 | **/ |
| 918 | |
| 919 | static int |
| 920 | ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 921 | { |
| 922 | int err = 0; |
| 923 | int create; |
| 924 | struct ip6_tnl_parm p; |
| 925 | struct ip6_tnl *t = NULL; |
| 926 | |
| 927 | switch (cmd) { |
| 928 | case SIOCGETTUNNEL: |
| 929 | if (dev == ip6ip6_fb_tnl_dev) { |
| 930 | if (copy_from_user(&p, |
| 931 | ifr->ifr_ifru.ifru_data, |
| 932 | sizeof (p))) { |
| 933 | err = -EFAULT; |
| 934 | break; |
| 935 | } |
| 936 | if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV) |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 937 | t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | else if (err) |
| 939 | break; |
| 940 | } else |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 941 | t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | memcpy(&p, &t->parms, sizeof (p)); |
| 944 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) { |
| 945 | err = -EFAULT; |
| 946 | } |
| 947 | break; |
| 948 | case SIOCADDTUNNEL: |
| 949 | case SIOCCHGTUNNEL: |
| 950 | err = -EPERM; |
| 951 | create = (cmd == SIOCADDTUNNEL); |
| 952 | if (!capable(CAP_NET_ADMIN)) |
| 953 | break; |
| 954 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) { |
| 955 | err = -EFAULT; |
| 956 | break; |
| 957 | } |
| 958 | if (!create && dev != ip6ip6_fb_tnl_dev) { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 959 | t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) { |
| 962 | break; |
| 963 | } |
| 964 | if (cmd == SIOCCHGTUNNEL) { |
| 965 | if (t->dev != dev) { |
| 966 | err = -EEXIST; |
| 967 | break; |
| 968 | } |
| 969 | ip6ip6_tnl_unlink(t); |
| 970 | err = ip6ip6_tnl_change(t, &p); |
| 971 | ip6ip6_tnl_link(t); |
| 972 | netdev_state_change(dev); |
| 973 | } |
| 974 | if (copy_to_user(ifr->ifr_ifru.ifru_data, |
| 975 | &t->parms, sizeof (p))) { |
| 976 | err = -EFAULT; |
| 977 | } else { |
| 978 | err = 0; |
| 979 | } |
| 980 | break; |
| 981 | case SIOCDELTUNNEL: |
| 982 | err = -EPERM; |
| 983 | if (!capable(CAP_NET_ADMIN)) |
| 984 | break; |
| 985 | |
| 986 | if (dev == ip6ip6_fb_tnl_dev) { |
| 987 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, |
| 988 | sizeof (p))) { |
| 989 | err = -EFAULT; |
| 990 | break; |
| 991 | } |
| 992 | err = ip6ip6_tnl_locate(&p, &t, 0); |
| 993 | if (err) |
| 994 | break; |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 995 | if (t == netdev_priv(ip6ip6_fb_tnl_dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | err = -EPERM; |
| 997 | break; |
| 998 | } |
| 999 | } else { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1000 | t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | err = unregister_netdevice(t->dev); |
| 1003 | break; |
| 1004 | default: |
| 1005 | err = -EINVAL; |
| 1006 | } |
| 1007 | return err; |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * ip6ip6_tnl_get_stats - return the stats for tunnel device |
| 1012 | * @dev: virtual device associated with tunnel |
| 1013 | * |
| 1014 | * Return: stats for device |
| 1015 | **/ |
| 1016 | |
| 1017 | static struct net_device_stats * |
| 1018 | ip6ip6_tnl_get_stats(struct net_device *dev) |
| 1019 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1020 | return &(((struct ip6_tnl *)netdev_priv(dev))->stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device |
| 1025 | * @dev: virtual device associated with tunnel |
| 1026 | * @new_mtu: the new mtu |
| 1027 | * |
| 1028 | * Return: |
| 1029 | * 0 on success, |
| 1030 | * %-EINVAL if mtu too small |
| 1031 | **/ |
| 1032 | |
| 1033 | static int |
| 1034 | ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) |
| 1035 | { |
| 1036 | if (new_mtu < IPV6_MIN_MTU) { |
| 1037 | return -EINVAL; |
| 1038 | } |
| 1039 | dev->mtu = new_mtu; |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * ip6ip6_tnl_dev_setup - setup virtual tunnel device |
| 1045 | * @dev: virtual device associated with tunnel |
| 1046 | * |
| 1047 | * Description: |
| 1048 | * Initialize function pointers and device parameters |
| 1049 | **/ |
| 1050 | |
| 1051 | static void ip6ip6_tnl_dev_setup(struct net_device *dev) |
| 1052 | { |
| 1053 | SET_MODULE_OWNER(dev); |
| 1054 | dev->uninit = ip6ip6_tnl_dev_uninit; |
| 1055 | dev->destructor = free_netdev; |
| 1056 | dev->hard_start_xmit = ip6ip6_tnl_xmit; |
| 1057 | dev->get_stats = ip6ip6_tnl_get_stats; |
| 1058 | dev->do_ioctl = ip6ip6_tnl_ioctl; |
| 1059 | dev->change_mtu = ip6ip6_tnl_change_mtu; |
| 1060 | |
| 1061 | dev->type = ARPHRD_TUNNEL6; |
| 1062 | dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr); |
| 1063 | dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr); |
| 1064 | dev->flags |= IFF_NOARP; |
| 1065 | dev->addr_len = sizeof(struct in6_addr); |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | /** |
| 1070 | * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices |
| 1071 | * @dev: virtual device associated with tunnel |
| 1072 | **/ |
| 1073 | |
| 1074 | static inline void |
| 1075 | ip6ip6_tnl_dev_init_gen(struct net_device *dev) |
| 1076 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1077 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | t->fl.proto = IPPROTO_IPV6; |
| 1079 | t->dev = dev; |
| 1080 | strcpy(t->parms.name, dev->name); |
| 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices |
| 1085 | * @dev: virtual device associated with tunnel |
| 1086 | **/ |
| 1087 | |
| 1088 | static int |
| 1089 | ip6ip6_tnl_dev_init(struct net_device *dev) |
| 1090 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1091 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | ip6ip6_tnl_dev_init_gen(dev); |
| 1093 | ip6ip6_tnl_link_config(t); |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device |
| 1099 | * @dev: fallback device |
| 1100 | * |
| 1101 | * Return: 0 |
| 1102 | **/ |
| 1103 | |
| 1104 | static int |
| 1105 | ip6ip6_fb_tnl_dev_init(struct net_device *dev) |
| 1106 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1107 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | ip6ip6_tnl_dev_init_gen(dev); |
| 1109 | dev_hold(dev); |
| 1110 | tnls_wc[0] = t; |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static struct xfrm6_tunnel ip6ip6_handler = { |
Patrick McHardy | 0303770 | 2005-07-19 14:03:34 -0700 | [diff] [blame] | 1115 | .handler = ip6ip6_rcv, |
| 1116 | .err_handler = ip6ip6_err, |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1117 | .priority = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | }; |
| 1119 | |
| 1120 | /** |
| 1121 | * ip6_tunnel_init - register protocol and reserve needed resources |
| 1122 | * |
| 1123 | * Return: 0 on success |
| 1124 | **/ |
| 1125 | |
| 1126 | static int __init ip6_tunnel_init(void) |
| 1127 | { |
| 1128 | int err; |
| 1129 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1130 | if (xfrm6_tunnel_register(&ip6ip6_handler)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | printk(KERN_ERR "ip6ip6 init: can't register tunnel\n"); |
| 1132 | return -EAGAIN; |
| 1133 | } |
| 1134 | ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", |
| 1135 | ip6ip6_tnl_dev_setup); |
| 1136 | |
| 1137 | if (!ip6ip6_fb_tnl_dev) { |
| 1138 | err = -ENOMEM; |
| 1139 | goto fail; |
| 1140 | } |
| 1141 | ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init; |
| 1142 | |
| 1143 | if ((err = register_netdev(ip6ip6_fb_tnl_dev))) { |
| 1144 | free_netdev(ip6ip6_fb_tnl_dev); |
| 1145 | goto fail; |
| 1146 | } |
| 1147 | return 0; |
| 1148 | fail: |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1149 | xfrm6_tunnel_deregister(&ip6ip6_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | return err; |
| 1151 | } |
| 1152 | |
Yasuyuki Kozakai | b3fdd9f | 2006-11-06 10:06:22 -0800 | [diff] [blame] | 1153 | static void __exit ip6ip6_destroy_tunnels(void) |
| 1154 | { |
| 1155 | int h; |
| 1156 | struct ip6_tnl *t; |
| 1157 | |
| 1158 | for (h = 0; h < HASH_SIZE; h++) { |
| 1159 | while ((t = tnls_r_l[h]) != NULL) |
| 1160 | unregister_netdevice(t->dev); |
| 1161 | } |
| 1162 | |
| 1163 | t = tnls_wc[0]; |
| 1164 | unregister_netdevice(t->dev); |
| 1165 | } |
| 1166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | /** |
| 1168 | * ip6_tunnel_cleanup - free resources and unregister protocol |
| 1169 | **/ |
| 1170 | |
| 1171 | static void __exit ip6_tunnel_cleanup(void) |
| 1172 | { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1173 | if (xfrm6_tunnel_deregister(&ip6ip6_handler)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n"); |
| 1175 | |
Yasuyuki Kozakai | b3fdd9f | 2006-11-06 10:06:22 -0800 | [diff] [blame] | 1176 | rtnl_lock(); |
| 1177 | ip6ip6_destroy_tunnels(); |
| 1178 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | module_init(ip6_tunnel_init); |
| 1182 | module_exit(ip6_tunnel_cleanup); |