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