Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 2 | * IPv6 tunneling device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Linux INET6 implementation |
| 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 7 | * Yasuyuki Kozakai <kozakai@linux-ipv6.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * $Id$ |
| 10 | * |
| 11 | * Based on: |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 12 | * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
| 14 | * RFC 2473 |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * as published by the Free Software Foundation; either version |
| 19 | * 2 of the License, or (at your option) any later version. |
| 20 | * |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 24 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/errno.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/sockios.h> |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 28 | #include <linux/icmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/if.h> |
| 30 | #include <linux/in.h> |
| 31 | #include <linux/ip.h> |
| 32 | #include <linux/if_tunnel.h> |
| 33 | #include <linux/net.h> |
| 34 | #include <linux/in6.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/if_arp.h> |
| 37 | #include <linux/icmpv6.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/route.h> |
| 40 | #include <linux/rtnetlink.h> |
| 41 | #include <linux/netfilter_ipv6.h> |
| 42 | |
| 43 | #include <asm/uaccess.h> |
| 44 | #include <asm/atomic.h> |
| 45 | |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 46 | #include <net/icmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <net/ip.h> |
| 48 | #include <net/ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <net/ip6_route.h> |
| 50 | #include <net/addrconf.h> |
| 51 | #include <net/ip6_tunnel.h> |
| 52 | #include <net/xfrm.h> |
| 53 | #include <net/dsfield.h> |
| 54 | #include <net/inet_ecn.h> |
| 55 | |
| 56 | MODULE_AUTHOR("Ville Nuorvala"); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 57 | MODULE_DESCRIPTION("IPv6 tunneling device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | MODULE_LICENSE("GPL"); |
| 59 | |
| 60 | #define IPV6_TLV_TEL_DST_SIZE 8 |
| 61 | |
| 62 | #ifdef IP6_TNL_DEBUG |
| 63 | #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__) |
| 64 | #else |
| 65 | #define IP6_TNL_TRACE(x...) do {;} while(0) |
| 66 | #endif |
| 67 | |
| 68 | #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 69 | #define IPV6_TCLASS_SHIFT 20 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | #define HASH_SIZE 32 |
| 72 | |
Al Viro | e69a4adc | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 73 | #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 74 | (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \ |
| 75 | (HASH_SIZE - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 77 | static int ip6_fb_tnl_dev_init(struct net_device *dev); |
| 78 | static int ip6_tnl_dev_init(struct net_device *dev); |
| 79 | static void ip6_tnl_dev_setup(struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* the IPv6 tunnel fallback device */ |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 82 | static struct net_device *ip6_fb_tnl_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | |
| 85 | /* lists for storing tunnels in use */ |
| 86 | static struct ip6_tnl *tnls_r_l[HASH_SIZE]; |
| 87 | static struct ip6_tnl *tnls_wc[1]; |
| 88 | static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l }; |
| 89 | |
| 90 | /* lock for the tunnel lists */ |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 91 | static DEFINE_RWLOCK(ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t) |
| 94 | { |
| 95 | struct dst_entry *dst = t->dst_cache; |
| 96 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 97 | if (dst && dst->obsolete && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | dst->ops->check(dst, t->dst_cookie) == NULL) { |
| 99 | t->dst_cache = NULL; |
| 100 | dst_release(dst); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | return dst; |
| 105 | } |
| 106 | |
| 107 | static inline void ip6_tnl_dst_reset(struct ip6_tnl *t) |
| 108 | { |
| 109 | dst_release(t->dst_cache); |
| 110 | t->dst_cache = NULL; |
| 111 | } |
| 112 | |
| 113 | static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst) |
| 114 | { |
| 115 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 116 | t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; |
| 117 | dst_release(t->dst_cache); |
| 118 | t->dst_cache = dst; |
| 119 | } |
| 120 | |
| 121 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 122 | * ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 123 | * @remote: the address of the tunnel exit-point |
| 124 | * @local: the address of the tunnel entry-point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 126 | * Return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | * tunnel matching given end-points if found, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 128 | * else fallback tunnel if its device is up, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * else %NULL |
| 130 | **/ |
| 131 | |
| 132 | static struct ip6_tnl * |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 133 | ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | unsigned h0 = HASH(remote); |
| 136 | unsigned h1 = HASH(local); |
| 137 | struct ip6_tnl *t; |
| 138 | |
| 139 | for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) { |
| 140 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 141 | ipv6_addr_equal(remote, &t->parms.raddr) && |
| 142 | (t->dev->flags & IFF_UP)) |
| 143 | return t; |
| 144 | } |
| 145 | if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP)) |
| 146 | return t; |
| 147 | |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 152 | * ip6_tnl_bucket - get head of list matching given tunnel parameters |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 153 | * @p: parameters containing tunnel end-points |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * |
| 155 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 156 | * ip6_tnl_bucket() returns the head of the list matching the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | * &struct in6_addr entries laddr and raddr in @p. |
| 158 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 159 | * Return: head of IPv6 tunnel list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | **/ |
| 161 | |
| 162 | static struct ip6_tnl ** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 163 | ip6_tnl_bucket(struct ip6_tnl_parm *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
| 165 | struct in6_addr *remote = &p->raddr; |
| 166 | struct in6_addr *local = &p->laddr; |
| 167 | unsigned h = 0; |
| 168 | int prio = 0; |
| 169 | |
| 170 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { |
| 171 | prio = 1; |
| 172 | h = HASH(remote) ^ HASH(local); |
| 173 | } |
| 174 | return &tnls[prio][h]; |
| 175 | } |
| 176 | |
| 177 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 178 | * ip6_tnl_link - add tunnel to hash table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * @t: tunnel to be added |
| 180 | **/ |
| 181 | |
| 182 | static void |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 183 | ip6_tnl_link(struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 185 | struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | t->next = *tp; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 188 | write_lock_bh(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | *tp = t; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 190 | write_unlock_bh(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 194 | * ip6_tnl_unlink - remove tunnel from hash table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | * @t: tunnel to be removed |
| 196 | **/ |
| 197 | |
| 198 | static void |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 199 | ip6_tnl_unlink(struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
| 201 | struct ip6_tnl **tp; |
| 202 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 203 | for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (t == *tp) { |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 205 | write_lock_bh(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | *tp = t->next; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 207 | write_unlock_bh(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * ip6_tnl_create() - create a new tunnel |
| 215 | * @p: tunnel parameters |
| 216 | * @pt: pointer to new tunnel |
| 217 | * |
| 218 | * Description: |
| 219 | * Create tunnel matching given parameters. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 220 | * |
| 221 | * Return: |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 222 | * created tunnel or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | **/ |
| 224 | |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 225 | static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | struct net_device *dev; |
| 228 | struct ip6_tnl *t; |
| 229 | char name[IFNAMSIZ]; |
| 230 | int err; |
| 231 | |
Pavel Emelyanov | 34cc7ba | 2008-02-23 20:19:20 -0800 | [diff] [blame] | 232 | if (p->name[0]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | strlcpy(name, p->name, IFNAMSIZ); |
Pavel Emelyanov | 34cc7ba | 2008-02-23 20:19:20 -0800 | [diff] [blame] | 234 | else |
| 235 | sprintf(name, "ip6tnl%%d"); |
| 236 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 237 | dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (dev == NULL) |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 239 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Pavel Emelyanov | b37d428b | 2008-02-26 23:51:04 -0800 | [diff] [blame] | 241 | if (strchr(name, '%')) { |
| 242 | if (dev_alloc_name(dev, name) < 0) |
| 243 | goto failed_free; |
| 244 | } |
| 245 | |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 246 | t = netdev_priv(dev); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 247 | dev->init = ip6_tnl_dev_init; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | t->parms = *p; |
| 249 | |
Pavel Emelyanov | b37d428b | 2008-02-26 23:51:04 -0800 | [diff] [blame] | 250 | if ((err = register_netdevice(dev)) < 0) |
| 251 | goto failed_free; |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | dev_hold(dev); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 254 | ip6_tnl_link(t); |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 255 | return t; |
Pavel Emelyanov | b37d428b | 2008-02-26 23:51:04 -0800 | [diff] [blame] | 256 | |
| 257 | failed_free: |
| 258 | free_netdev(dev); |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 259 | failed: |
| 260 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 264 | * ip6_tnl_locate - find or create tunnel matching given parameters |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 265 | * @p: tunnel parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | * @create: != 0 if allowed to create new tunnel if no match found |
| 267 | * |
| 268 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 269 | * ip6_tnl_locate() first tries to locate an existing tunnel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * based on @parms. If this is unsuccessful, but @create is set a new |
| 271 | * tunnel device is created and registered for use. |
| 272 | * |
| 273 | * Return: |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 274 | * matching tunnel or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | **/ |
| 276 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 277 | static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
| 279 | struct in6_addr *remote = &p->raddr; |
| 280 | struct in6_addr *local = &p->laddr; |
| 281 | struct ip6_tnl *t; |
| 282 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 283 | for (t = *ip6_tnl_bucket(p); t; t = t->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 285 | ipv6_addr_equal(remote, &t->parms.raddr)) |
| 286 | return t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | if (!create) |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 289 | return NULL; |
| 290 | return ip6_tnl_create(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 294 | * ip6_tnl_dev_uninit - tunnel device uninitializer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | * @dev: the device to be destroyed |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 296 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 298 | * ip6_tnl_dev_uninit() removes tunnel from its list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | **/ |
| 300 | |
| 301 | static void |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 302 | ip6_tnl_dev_uninit(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 304 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 306 | if (dev == ip6_fb_tnl_dev) { |
| 307 | write_lock_bh(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | tnls_wc[0] = NULL; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 309 | write_unlock_bh(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } else { |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 311 | ip6_tnl_unlink(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | ip6_tnl_dst_reset(t); |
| 314 | dev_put(dev); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option |
| 319 | * @skb: received socket buffer |
| 320 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 321 | * Return: |
| 322 | * 0 if none was found, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | * else index to encapsulation limit |
| 324 | **/ |
| 325 | |
| 326 | static __u16 |
| 327 | parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw) |
| 328 | { |
| 329 | struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw; |
| 330 | __u8 nexthdr = ipv6h->nexthdr; |
| 331 | __u16 off = sizeof (*ipv6h); |
| 332 | |
| 333 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { |
| 334 | __u16 optlen = 0; |
| 335 | struct ipv6_opt_hdr *hdr; |
| 336 | if (raw + off + sizeof (*hdr) > skb->data && |
| 337 | !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr))) |
| 338 | break; |
| 339 | |
| 340 | hdr = (struct ipv6_opt_hdr *) (raw + off); |
| 341 | if (nexthdr == NEXTHDR_FRAGMENT) { |
| 342 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; |
| 343 | if (frag_hdr->frag_off) |
| 344 | break; |
| 345 | optlen = 8; |
| 346 | } else if (nexthdr == NEXTHDR_AUTH) { |
| 347 | optlen = (hdr->hdrlen + 2) << 2; |
| 348 | } else { |
| 349 | optlen = ipv6_optlen(hdr); |
| 350 | } |
| 351 | if (nexthdr == NEXTHDR_DEST) { |
| 352 | __u16 i = off + 2; |
| 353 | while (1) { |
| 354 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 355 | |
| 356 | /* No more room for encapsulation limit */ |
| 357 | if (i + sizeof (*tel) > off + optlen) |
| 358 | break; |
| 359 | |
| 360 | tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i]; |
| 361 | /* return index of option if found and valid */ |
| 362 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && |
| 363 | tel->length == 1) |
| 364 | return i; |
| 365 | /* else jump to next option */ |
| 366 | if (tel->type) |
| 367 | i += tel->length + 2; |
| 368 | else |
| 369 | i++; |
| 370 | } |
| 371 | } |
| 372 | nexthdr = hdr->nexthdr; |
| 373 | off += optlen; |
| 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /** |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 379 | * ip6_tnl_err - tunnel error handler |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * |
| 381 | * Description: |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 382 | * ip6_tnl_err() should handle errors in the tunnel according |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | * to the specifications in RFC 2473. |
| 384 | **/ |
| 385 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 386 | static int |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 387 | ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt, |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 388 | int *type, int *code, int *msg, __u32 *info, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
| 390 | struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data; |
| 391 | struct ip6_tnl *t; |
| 392 | int rel_msg = 0; |
| 393 | int rel_type = ICMPV6_DEST_UNREACH; |
| 394 | int rel_code = ICMPV6_ADDR_UNREACH; |
| 395 | __u32 rel_info = 0; |
| 396 | __u16 len; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 397 | int err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 399 | /* If the packet doesn't contain the original IPv6 header we are |
| 400 | in trouble since we might need the source address for further |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | processing of the error. */ |
| 402 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 403 | read_lock(&ip6_tnl_lock); |
| 404 | if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | goto out; |
| 406 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 407 | if (t->parms.proto != ipproto && t->parms.proto != 0) |
| 408 | goto out; |
| 409 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 410 | err = 0; |
| 411 | |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 412 | switch (*type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | __u32 teli; |
| 414 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 415 | __u32 mtu; |
| 416 | case ICMPV6_DEST_UNREACH: |
| 417 | if (net_ratelimit()) |
| 418 | printk(KERN_WARNING |
| 419 | "%s: Path to destination invalid " |
| 420 | "or inactive!\n", t->parms.name); |
| 421 | rel_msg = 1; |
| 422 | break; |
| 423 | case ICMPV6_TIME_EXCEED: |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 424 | if ((*code) == ICMPV6_EXC_HOPLIMIT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (net_ratelimit()) |
| 426 | printk(KERN_WARNING |
| 427 | "%s: Too small hop limit or " |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 428 | "routing loop in tunnel!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | t->parms.name); |
| 430 | rel_msg = 1; |
| 431 | } |
| 432 | break; |
| 433 | case ICMPV6_PARAMPROB: |
Ville Nuorvala | 107a5fe | 2006-11-24 17:08:58 -0800 | [diff] [blame] | 434 | teli = 0; |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 435 | if ((*code) == ICMPV6_HDR_FIELD) |
Ville Nuorvala | 107a5fe | 2006-11-24 17:08:58 -0800 | [diff] [blame] | 436 | teli = parse_tlv_tnl_enc_lim(skb, skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 438 | if (teli && teli == *info - 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
| 440 | if (tel->encap_limit == 0) { |
| 441 | if (net_ratelimit()) |
| 442 | printk(KERN_WARNING |
| 443 | "%s: Too small encapsulation " |
| 444 | "limit or routing loop in " |
| 445 | "tunnel!\n", t->parms.name); |
| 446 | rel_msg = 1; |
| 447 | } |
Ville Nuorvala | 107a5fe | 2006-11-24 17:08:58 -0800 | [diff] [blame] | 448 | } else if (net_ratelimit()) { |
| 449 | printk(KERN_WARNING |
| 450 | "%s: Recipient unable to parse tunneled " |
| 451 | "packet!\n ", t->parms.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | break; |
| 454 | case ICMPV6_PKT_TOOBIG: |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 455 | mtu = *info - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (mtu < IPV6_MIN_MTU) |
| 457 | mtu = IPV6_MIN_MTU; |
| 458 | t->dev->mtu = mtu; |
| 459 | |
Al Viro | cc6cdac | 2006-02-18 16:02:18 -0500 | [diff] [blame] | 460 | if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | rel_type = ICMPV6_PKT_TOOBIG; |
| 462 | rel_code = 0; |
| 463 | rel_info = mtu; |
| 464 | rel_msg = 1; |
| 465 | } |
| 466 | break; |
| 467 | } |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 468 | |
| 469 | *type = rel_type; |
| 470 | *code = rel_code; |
| 471 | *info = rel_info; |
| 472 | *msg = rel_msg; |
| 473 | |
| 474 | out: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 475 | read_unlock(&ip6_tnl_lock); |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 476 | return err; |
| 477 | } |
| 478 | |
| 479 | static int |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 480 | ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 481 | int type, int code, int offset, __be32 info) |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 482 | { |
| 483 | int rel_msg = 0; |
| 484 | int rel_type = type; |
| 485 | int rel_code = code; |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 486 | __u32 rel_info = ntohl(info); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 487 | int err; |
| 488 | struct sk_buff *skb2; |
| 489 | struct iphdr *eiph; |
| 490 | struct flowi fl; |
| 491 | struct rtable *rt; |
| 492 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 493 | err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code, |
| 494 | &rel_msg, &rel_info, offset); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 495 | if (err < 0) |
| 496 | return err; |
| 497 | |
| 498 | if (rel_msg == 0) |
| 499 | return 0; |
| 500 | |
| 501 | switch (rel_type) { |
| 502 | case ICMPV6_DEST_UNREACH: |
| 503 | if (rel_code != ICMPV6_ADDR_UNREACH) |
| 504 | return 0; |
| 505 | rel_type = ICMP_DEST_UNREACH; |
| 506 | rel_code = ICMP_HOST_UNREACH; |
| 507 | break; |
| 508 | case ICMPV6_PKT_TOOBIG: |
| 509 | if (rel_code != 0) |
| 510 | return 0; |
| 511 | rel_type = ICMP_DEST_UNREACH; |
| 512 | rel_code = ICMP_FRAG_NEEDED; |
| 513 | break; |
| 514 | default: |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | if (!pskb_may_pull(skb, offset + sizeof(struct iphdr))) |
| 519 | return 0; |
| 520 | |
| 521 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 522 | if (!skb2) |
| 523 | return 0; |
| 524 | |
| 525 | dst_release(skb2->dst); |
| 526 | skb2->dst = NULL; |
| 527 | skb_pull(skb2, offset); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 528 | skb_reset_network_header(skb2); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 529 | eiph = ip_hdr(skb2); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 530 | |
| 531 | /* Try to guess incoming interface */ |
| 532 | memset(&fl, 0, sizeof(fl)); |
| 533 | fl.fl4_dst = eiph->saddr; |
| 534 | fl.fl4_tos = RT_TOS(eiph->tos); |
| 535 | fl.proto = IPPROTO_IPIP; |
Denis V. Lunev | f206351 | 2008-01-22 22:07:34 -0800 | [diff] [blame] | 536 | if (ip_route_output_key(&init_net, &rt, &fl)) |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 537 | goto out; |
| 538 | |
| 539 | skb2->dev = rt->u.dst.dev; |
| 540 | |
| 541 | /* route "incoming" packet */ |
| 542 | if (rt->rt_flags & RTCF_LOCAL) { |
| 543 | ip_rt_put(rt); |
| 544 | rt = NULL; |
| 545 | fl.fl4_dst = eiph->daddr; |
| 546 | fl.fl4_src = eiph->saddr; |
| 547 | fl.fl4_tos = eiph->tos; |
Denis V. Lunev | f206351 | 2008-01-22 22:07:34 -0800 | [diff] [blame] | 548 | if (ip_route_output_key(&init_net, &rt, &fl) || |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 549 | rt->u.dst.dev->type != ARPHRD_TUNNEL) { |
| 550 | ip_rt_put(rt); |
| 551 | goto out; |
| 552 | } |
Denis V. Lunev | 9937ded | 2008-02-18 20:49:36 -0800 | [diff] [blame] | 553 | skb2->dst = (struct dst_entry *)rt; |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 554 | } else { |
| 555 | ip_rt_put(rt); |
| 556 | if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, |
| 557 | skb2->dev) || |
| 558 | skb2->dst->dev->type != ARPHRD_TUNNEL) |
| 559 | goto out; |
| 560 | } |
| 561 | |
| 562 | /* change mtu on this route */ |
| 563 | if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) { |
| 564 | if (rel_info > dst_mtu(skb2->dst)) |
| 565 | goto out; |
| 566 | |
| 567 | skb2->dst->ops->update_pmtu(skb2->dst, rel_info); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 568 | } |
| 569 | |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 570 | icmp_send(skb2, rel_type, rel_code, htonl(rel_info)); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 571 | |
| 572 | out: |
| 573 | kfree_skb(skb2); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 578 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 579 | int type, int code, int offset, __be32 info) |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 580 | { |
| 581 | int rel_msg = 0; |
| 582 | int rel_type = type; |
| 583 | int rel_code = code; |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 584 | __u32 rel_info = ntohl(info); |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 585 | int err; |
| 586 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 587 | err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code, |
| 588 | &rel_msg, &rel_info, offset); |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 589 | if (err < 0) |
| 590 | return err; |
| 591 | |
| 592 | if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | struct rt6_info *rt; |
| 594 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
Ville Nuorvala | 305d4b3 | 2006-11-24 17:06:53 -0800 | [diff] [blame] | 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | if (!skb2) |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 597 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
| 599 | dst_release(skb2->dst); |
| 600 | skb2->dst = NULL; |
| 601 | skb_pull(skb2, offset); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 602 | skb_reset_network_header(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | /* Try to guess incoming interface */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 605 | rt = rt6_lookup(&ipv6_hdr(skb2)->saddr, NULL, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | if (rt && rt->rt6i_dev) |
| 608 | skb2->dev = rt->rt6i_dev; |
| 609 | |
| 610 | icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev); |
| 611 | |
| 612 | if (rt) |
| 613 | dst_release(&rt->u.dst); |
| 614 | |
| 615 | kfree_skb(skb2); |
| 616 | } |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 617 | |
| 618 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 621 | static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t, |
| 622 | struct ipv6hdr *ipv6h, |
| 623 | struct sk_buff *skb) |
| 624 | { |
| 625 | __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; |
| 626 | |
| 627 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 628 | ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 629 | |
| 630 | if (INET_ECN_is_ce(dsfield)) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 631 | IP_ECN_set_ce(ip_hdr(skb)); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 632 | } |
| 633 | |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 634 | static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t, |
| 635 | struct ipv6hdr *ipv6h, |
| 636 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 638 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
Herbert Xu | 29bb43b4 | 2007-11-13 21:40:13 -0800 | [diff] [blame] | 639 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 641 | if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h))) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 642 | IP6_ECN_set_ce(ipv6_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 644 | |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 645 | static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t) |
| 646 | { |
| 647 | struct ip6_tnl_parm *p = &t->parms; |
| 648 | int ret = 0; |
| 649 | |
| 650 | if (p->flags & IP6_TNL_F_CAP_RCV) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 651 | struct net_device *ldev = NULL; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 652 | |
| 653 | if (p->link) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 654 | ldev = dev_get_by_index(&init_net, p->link); |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 655 | |
| 656 | if ((ipv6_addr_is_multicast(&p->laddr) || |
Daniel Lezcano | bfeade0 | 2008-01-10 22:43:18 -0800 | [diff] [blame] | 657 | likely(ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) && |
| 658 | likely(!ipv6_chk_addr(&init_net, &p->raddr, NULL, 0))) |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 659 | ret = 1; |
| 660 | |
| 661 | if (ldev) |
| 662 | dev_put(ldev); |
| 663 | } |
| 664 | return ret; |
| 665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 668 | * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | * @skb: received socket buffer |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 670 | * @protocol: ethernet protocol ID |
| 671 | * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | * |
| 673 | * Return: 0 |
| 674 | **/ |
| 675 | |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 676 | static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol, |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 677 | __u8 ipproto, |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 678 | void (*dscp_ecn_decapsulate)(struct ip6_tnl *t, |
| 679 | struct ipv6hdr *ipv6h, |
| 680 | struct sk_buff *skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | struct ip6_tnl *t; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 683 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 685 | read_lock(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 687 | if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) { |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 688 | if (t->parms.proto != ipproto && t->parms.proto != 0) { |
| 689 | read_unlock(&ip6_tnl_lock); |
| 690 | goto discard; |
| 691 | } |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 694 | read_unlock(&ip6_tnl_lock); |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 695 | goto discard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 698 | if (!ip6_tnl_rcv_ctl(t)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | t->stat.rx_dropped++; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 700 | read_unlock(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | goto discard; |
| 702 | } |
| 703 | secpath_reset(skb); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 704 | skb->mac_header = skb->network_header; |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 705 | skb_reset_network_header(skb); |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 706 | skb->protocol = htons(protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | skb->pkt_type = PACKET_HOST; |
| 708 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); |
| 709 | skb->dev = t->dev; |
| 710 | dst_release(skb->dst); |
| 711 | skb->dst = NULL; |
Yasuyuki Kozakai | 53ab61c | 2006-11-06 10:06:23 -0800 | [diff] [blame] | 712 | nf_reset(skb); |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 713 | |
| 714 | dscp_ecn_decapsulate(t, ipv6h, skb); |
| 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | t->stat.rx_packets++; |
| 717 | t->stat.rx_bytes += skb->len; |
| 718 | netif_rx(skb); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 719 | read_unlock(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | return 0; |
| 721 | } |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 722 | read_unlock(&ip6_tnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | return 1; |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 724 | |
| 725 | discard: |
| 726 | kfree_skb(skb); |
| 727 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 730 | static int ip4ip6_rcv(struct sk_buff *skb) |
| 731 | { |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 732 | return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP, |
| 733 | ip4ip6_dscp_ecn_decapsulate); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 734 | } |
| 735 | |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 736 | static int ip6ip6_rcv(struct sk_buff *skb) |
| 737 | { |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 738 | return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6, |
| 739 | ip6ip6_dscp_ecn_decapsulate); |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 740 | } |
| 741 | |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 742 | struct ipv6_tel_txoption { |
| 743 | struct ipv6_txoptions ops; |
| 744 | __u8 dst_opt[8]; |
| 745 | }; |
| 746 | |
| 747 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 749 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 751 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; |
| 752 | opt->dst_opt[3] = 1; |
| 753 | opt->dst_opt[4] = encap_limit; |
| 754 | opt->dst_opt[5] = IPV6_TLV_PADN; |
| 755 | opt->dst_opt[6] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 757 | opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt; |
| 758 | opt->ops.opt_nflen = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 762 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | * @t: the outgoing tunnel device |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 764 | * @hdr: IPv6 header from the incoming packet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | * |
| 766 | * Description: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 767 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | * doesn't match source of incoming packet. |
| 769 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 770 | * Return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | * 1 if conflict, |
| 772 | * 0 else |
| 773 | **/ |
| 774 | |
| 775 | static inline int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 776 | ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
| 778 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); |
| 779 | } |
| 780 | |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 781 | static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t) |
| 782 | { |
| 783 | struct ip6_tnl_parm *p = &t->parms; |
| 784 | int ret = 0; |
| 785 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 786 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 787 | struct net_device *ldev = NULL; |
| 788 | |
| 789 | if (p->link) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 790 | ldev = dev_get_by_index(&init_net, p->link); |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 791 | |
Daniel Lezcano | bfeade0 | 2008-01-10 22:43:18 -0800 | [diff] [blame] | 792 | if (unlikely(!ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 793 | printk(KERN_WARNING |
| 794 | "%s xmit: Local address not yet configured!\n", |
| 795 | p->name); |
| 796 | else if (!ipv6_addr_is_multicast(&p->raddr) && |
Daniel Lezcano | bfeade0 | 2008-01-10 22:43:18 -0800 | [diff] [blame] | 797 | unlikely(ipv6_chk_addr(&init_net, &p->raddr, NULL, 0))) |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 798 | printk(KERN_WARNING |
| 799 | "%s xmit: Routing loop! " |
| 800 | "Remote address found on this node!\n", |
| 801 | p->name); |
| 802 | else |
| 803 | ret = 1; |
| 804 | if (ldev) |
| 805 | dev_put(ldev); |
| 806 | } |
| 807 | return ret; |
| 808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | /** |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 810 | * ip6_tnl_xmit2 - encapsulate packet and send |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | * @skb: the outgoing socket buffer |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 812 | * @dev: the outgoing tunnel device |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 813 | * @dsfield: dscp code for outer header |
| 814 | * @fl: flow of tunneled packet |
| 815 | * @encap_limit: encapsulation limit |
| 816 | * @pmtu: Path MTU is stored if packet is too big |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | * |
| 818 | * Description: |
| 819 | * Build new header and do some sanity checks on the packet before sending |
| 820 | * it. |
| 821 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 822 | * Return: |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 823 | * 0 on success |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 824 | * -1 fail |
| 825 | * %-EMSGSIZE message too big. return mtu in this case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | **/ |
| 827 | |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 828 | static int ip6_tnl_xmit2(struct sk_buff *skb, |
| 829 | struct net_device *dev, |
| 830 | __u8 dsfield, |
| 831 | struct flowi *fl, |
| 832 | int encap_limit, |
| 833 | __u32 *pmtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 835 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | struct net_device_stats *stats = &t->stat; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 837 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 838 | struct ipv6_tel_txoption opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | struct dst_entry *dst; |
| 840 | struct net_device *tdev; |
| 841 | int mtu; |
Chuck Lever | c2636b4 | 2007-10-23 21:07:32 -0700 | [diff] [blame] | 842 | unsigned int max_headroom = sizeof(struct ipv6hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | u8 proto; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 844 | int err = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | int pkt_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | if ((dst = ip6_tnl_dst_check(t)) != NULL) |
| 848 | dst_hold(dst); |
Patrick McHardy | a57ebc9 | 2005-09-08 14:27:47 -0700 | [diff] [blame] | 849 | else { |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 850 | dst = ip6_route_output(NULL, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 852 | if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0) |
Patrick McHardy | a57ebc9 | 2005-09-08 14:27:47 -0700 | [diff] [blame] | 853 | goto tx_err_link_failure; |
| 854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
| 856 | tdev = dst->dev; |
| 857 | |
| 858 | if (tdev == dev) { |
| 859 | stats->collisions++; |
| 860 | if (net_ratelimit()) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 861 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | "%s: Local routing loop detected!\n", |
| 863 | t->parms.name); |
| 864 | goto tx_err_dst_release; |
| 865 | } |
| 866 | mtu = dst_mtu(dst) - sizeof (*ipv6h); |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 867 | if (encap_limit >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | max_headroom += 8; |
| 869 | mtu -= 8; |
| 870 | } |
| 871 | if (mtu < IPV6_MIN_MTU) |
| 872 | mtu = IPV6_MIN_MTU; |
Yasuyuki Kozakai | 2689205 | 2006-09-10 03:59:17 +0900 | [diff] [blame] | 873 | if (skb->dst) |
| 874 | skb->dst->ops->update_pmtu(skb->dst, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | if (skb->len > mtu) { |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 876 | *pmtu = mtu; |
| 877 | err = -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | goto tx_err_dst_release; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Okay, now see if we can stuff it in the buffer as-is. |
| 883 | */ |
| 884 | max_headroom += LL_RESERVED_SPACE(tdev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 885 | |
Patrick McHardy | cfbba49 | 2007-07-09 15:33:40 -0700 | [diff] [blame] | 886 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
| 887 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | struct sk_buff *new_skb; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | if (!(new_skb = skb_realloc_headroom(skb, max_headroom))) |
| 891 | goto tx_err_dst_release; |
| 892 | |
| 893 | if (skb->sk) |
| 894 | skb_set_owner_w(new_skb, skb->sk); |
| 895 | kfree_skb(skb); |
| 896 | skb = new_skb; |
| 897 | } |
| 898 | dst_release(skb->dst); |
| 899 | skb->dst = dst_clone(dst); |
| 900 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 901 | skb->transport_header = skb->network_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 903 | proto = fl->proto; |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 904 | if (encap_limit >= 0) { |
| 905 | init_tel_txopt(&opt, encap_limit); |
| 906 | ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); |
| 907 | } |
Arnaldo Carvalho de Melo | e2d1bca | 2007-04-10 20:46:21 -0700 | [diff] [blame] | 908 | skb_push(skb, sizeof(struct ipv6hdr)); |
| 909 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 910 | ipv6h = ipv6_hdr(skb); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 911 | *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | dsfield = INET_ECN_encapsulate(0, dsfield); |
| 913 | ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | ipv6h->hop_limit = t->parms.hop_limit; |
| 915 | ipv6h->nexthdr = proto; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 916 | ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src); |
| 917 | ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | nf_reset(skb); |
| 919 | pkt_len = skb->len; |
Herbert Xu | ef76bc2 | 2008-01-11 19:15:08 -0800 | [diff] [blame] | 920 | err = ip6_local_out(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
Gerrit Renker | b9df3cb | 2006-11-14 11:21:36 -0200 | [diff] [blame] | 922 | if (net_xmit_eval(err) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | stats->tx_bytes += pkt_len; |
| 924 | stats->tx_packets++; |
| 925 | } else { |
| 926 | stats->tx_errors++; |
| 927 | stats->tx_aborted_errors++; |
| 928 | } |
| 929 | ip6_tnl_dst_store(t, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | return 0; |
| 931 | tx_err_link_failure: |
| 932 | stats->tx_carrier_errors++; |
| 933 | dst_link_failure(skb); |
| 934 | tx_err_dst_release: |
| 935 | dst_release(dst); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 936 | return err; |
| 937 | } |
| 938 | |
| 939 | static inline int |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 940 | ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 941 | { |
| 942 | struct ip6_tnl *t = netdev_priv(dev); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 943 | struct iphdr *iph = ip_hdr(skb); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 944 | int encap_limit = -1; |
| 945 | struct flowi fl; |
| 946 | __u8 dsfield; |
| 947 | __u32 mtu; |
| 948 | int err; |
| 949 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 950 | if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) || |
| 951 | !ip6_tnl_xmit_ctl(t)) |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 952 | return -1; |
| 953 | |
| 954 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 955 | encap_limit = t->parms.encap_limit; |
| 956 | |
| 957 | memcpy(&fl, &t->fl, sizeof (fl)); |
| 958 | fl.proto = IPPROTO_IPIP; |
| 959 | |
| 960 | dsfield = ipv4_get_dsfield(iph); |
| 961 | |
| 962 | if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)) |
Al Viro | b77f2fa | 2007-07-21 19:09:41 -0700 | [diff] [blame] | 963 | fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) |
| 964 | & IPV6_TCLASS_MASK; |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 965 | |
| 966 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu); |
| 967 | if (err != 0) { |
| 968 | /* XXX: send ICMP error even if DF is not set. */ |
| 969 | if (err == -EMSGSIZE) |
| 970 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 971 | htonl(mtu)); |
| 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static inline int |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 979 | ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 980 | { |
| 981 | struct ip6_tnl *t = netdev_priv(dev); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 982 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 983 | int encap_limit = -1; |
| 984 | __u16 offset; |
| 985 | struct flowi fl; |
| 986 | __u8 dsfield; |
| 987 | __u32 mtu; |
| 988 | int err; |
| 989 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 990 | if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) || |
| 991 | !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h)) |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 992 | return -1; |
| 993 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 994 | offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb)); |
| 995 | if (offset > 0) { |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 996 | struct ipv6_tlv_tnl_enc_lim *tel; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 997 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 998 | if (tel->encap_limit == 0) { |
| 999 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
| 1000 | ICMPV6_HDR_FIELD, offset + 2, skb->dev); |
| 1001 | return -1; |
| 1002 | } |
| 1003 | encap_limit = tel->encap_limit - 1; |
| 1004 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1005 | encap_limit = t->parms.encap_limit; |
| 1006 | |
| 1007 | memcpy(&fl, &t->fl, sizeof (fl)); |
| 1008 | fl.proto = IPPROTO_IPV6; |
| 1009 | |
| 1010 | dsfield = ipv6_get_dsfield(ipv6h); |
| 1011 | if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)) |
| 1012 | fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); |
| 1013 | if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
| 1014 | fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK); |
| 1015 | |
| 1016 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu); |
| 1017 | if (err != 0) { |
| 1018 | if (err == -EMSGSIZE) |
| 1019 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev); |
| 1020 | return -1; |
| 1021 | } |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static int |
| 1027 | ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1028 | { |
| 1029 | struct ip6_tnl *t = netdev_priv(dev); |
| 1030 | struct net_device_stats *stats = &t->stat; |
| 1031 | int ret; |
| 1032 | |
| 1033 | if (t->recursion++) { |
| 1034 | t->stat.collisions++; |
| 1035 | goto tx_err; |
| 1036 | } |
| 1037 | |
| 1038 | switch (skb->protocol) { |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1039 | case __constant_htons(ETH_P_IP): |
| 1040 | ret = ip4ip6_tnl_xmit(skb, dev); |
| 1041 | break; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1042 | case __constant_htons(ETH_P_IPV6): |
| 1043 | ret = ip6ip6_tnl_xmit(skb, dev); |
| 1044 | break; |
| 1045 | default: |
| 1046 | goto tx_err; |
| 1047 | } |
| 1048 | |
| 1049 | if (ret < 0) |
| 1050 | goto tx_err; |
| 1051 | |
| 1052 | t->recursion--; |
| 1053 | return 0; |
| 1054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | tx_err: |
| 1056 | stats->tx_errors++; |
| 1057 | stats->tx_dropped++; |
| 1058 | kfree_skb(skb); |
| 1059 | t->recursion--; |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | static void ip6_tnl_set_cap(struct ip6_tnl *t) |
| 1064 | { |
| 1065 | struct ip6_tnl_parm *p = &t->parms; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 1066 | int ltype = ipv6_addr_type(&p->laddr); |
| 1067 | int rtype = ipv6_addr_type(&p->raddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
| 1069 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV); |
| 1070 | |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 1071 | if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && |
| 1072 | rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && |
| 1073 | !((ltype|rtype) & IPV6_ADDR_LOOPBACK) && |
Ville Nuorvala | 305d4b3 | 2006-11-24 17:06:53 -0800 | [diff] [blame] | 1074 | (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) { |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 1075 | if (ltype&IPV6_ADDR_UNICAST) |
| 1076 | p->flags |= IP6_TNL_F_CAP_XMIT; |
| 1077 | if (rtype&IPV6_ADDR_UNICAST) |
| 1078 | p->flags |= IP6_TNL_F_CAP_RCV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1082 | static void ip6_tnl_link_config(struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
| 1084 | struct net_device *dev = t->dev; |
| 1085 | struct ip6_tnl_parm *p = &t->parms; |
| 1086 | struct flowi *fl = &t->fl; |
| 1087 | |
| 1088 | memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
| 1089 | memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
| 1090 | |
| 1091 | /* Set up flowi template */ |
| 1092 | ipv6_addr_copy(&fl->fl6_src, &p->laddr); |
| 1093 | ipv6_addr_copy(&fl->fl6_dst, &p->raddr); |
| 1094 | fl->oif = p->link; |
| 1095 | fl->fl6_flowlabel = 0; |
| 1096 | |
| 1097 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) |
| 1098 | fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
| 1099 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
| 1100 | fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
| 1101 | |
| 1102 | ip6_tnl_set_cap(t); |
| 1103 | |
| 1104 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) |
| 1105 | dev->flags |= IFF_POINTOPOINT; |
| 1106 | else |
| 1107 | dev->flags &= ~IFF_POINTOPOINT; |
| 1108 | |
| 1109 | dev->iflink = p->link; |
| 1110 | |
| 1111 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
Ville Nuorvala | 305d4b3 | 2006-11-24 17:06:53 -0800 | [diff] [blame] | 1112 | int strict = (ipv6_addr_type(&p->raddr) & |
| 1113 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); |
| 1114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr, |
Ville Nuorvala | 305d4b3 | 2006-11-24 17:06:53 -0800 | [diff] [blame] | 1116 | p->link, strict); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
| 1118 | if (rt == NULL) |
| 1119 | return; |
| 1120 | |
| 1121 | if (rt->rt6i_dev) { |
| 1122 | dev->hard_header_len = rt->rt6i_dev->hard_header_len + |
| 1123 | sizeof (struct ipv6hdr); |
| 1124 | |
| 1125 | dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr); |
| 1126 | |
| 1127 | if (dev->mtu < IPV6_MIN_MTU) |
| 1128 | dev->mtu = IPV6_MIN_MTU; |
| 1129 | } |
| 1130 | dst_release(&rt->u.dst); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1135 | * ip6_tnl_change - update the tunnel parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | * @t: tunnel to be changed |
| 1137 | * @p: tunnel configuration parameters |
| 1138 | * @active: != 0 if tunnel is ready for use |
| 1139 | * |
| 1140 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1141 | * ip6_tnl_change() updates the tunnel parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | **/ |
| 1143 | |
| 1144 | static int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1145 | ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | { |
| 1147 | ipv6_addr_copy(&t->parms.laddr, &p->laddr); |
| 1148 | ipv6_addr_copy(&t->parms.raddr, &p->raddr); |
| 1149 | t->parms.flags = p->flags; |
| 1150 | t->parms.hop_limit = p->hop_limit; |
| 1151 | t->parms.encap_limit = p->encap_limit; |
| 1152 | t->parms.flowinfo = p->flowinfo; |
Gabor Fekete | 8181b8c | 2005-06-08 14:54:38 -0700 | [diff] [blame] | 1153 | t->parms.link = p->link; |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 1154 | t->parms.proto = p->proto; |
Hugo Santos | 0c08889 | 2006-02-24 13:16:25 -0800 | [diff] [blame] | 1155 | ip6_tnl_dst_reset(t); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1156 | ip6_tnl_link_config(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1161 | * ip6_tnl_ioctl - configure ipv6 tunnels from userspace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | * @dev: virtual device associated with tunnel |
| 1163 | * @ifr: parameters passed from userspace |
| 1164 | * @cmd: command to be performed |
| 1165 | * |
| 1166 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1167 | * ip6_tnl_ioctl() is used for managing IPv6 tunnels |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1168 | * from userspace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | * |
| 1170 | * The possible commands are the following: |
| 1171 | * %SIOCGETTUNNEL: get tunnel parameters for device |
| 1172 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters |
| 1173 | * %SIOCCHGTUNNEL: change tunnel parameters to those given |
| 1174 | * %SIOCDELTUNNEL: delete tunnel |
| 1175 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1176 | * The fallback device "ip6tnl0", created during module |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | * initialization, can be used for creating other tunnel devices. |
| 1178 | * |
| 1179 | * Return: |
| 1180 | * 0 on success, |
| 1181 | * %-EFAULT if unable to copy data to or from userspace, |
| 1182 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set |
| 1183 | * %-EINVAL if passed tunnel parameters are invalid, |
| 1184 | * %-EEXIST if changing a tunnel's parameters would cause a conflict |
| 1185 | * %-ENODEV if attempting to change or delete a nonexisting device |
| 1186 | **/ |
| 1187 | |
| 1188 | static int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1189 | ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | { |
| 1191 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | struct ip6_tnl_parm p; |
| 1193 | struct ip6_tnl *t = NULL; |
| 1194 | |
| 1195 | switch (cmd) { |
| 1196 | case SIOCGETTUNNEL: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1197 | if (dev == ip6_fb_tnl_dev) { |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1198 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | err = -EFAULT; |
| 1200 | break; |
| 1201 | } |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1202 | t = ip6_tnl_locate(&p, 0); |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1203 | } |
| 1204 | if (t == NULL) |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1205 | t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | memcpy(&p, &t->parms, sizeof (p)); |
| 1207 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) { |
| 1208 | err = -EFAULT; |
| 1209 | } |
| 1210 | break; |
| 1211 | case SIOCADDTUNNEL: |
| 1212 | case SIOCCHGTUNNEL: |
| 1213 | err = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | if (!capable(CAP_NET_ADMIN)) |
| 1215 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1216 | err = -EFAULT; |
| 1217 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1219 | err = -EINVAL; |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 1220 | if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && |
| 1221 | p.proto != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | break; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1223 | t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL); |
| 1224 | if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) { |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1225 | if (t != NULL) { |
| 1226 | if (t->dev != dev) { |
| 1227 | err = -EEXIST; |
| 1228 | break; |
| 1229 | } |
| 1230 | } else |
| 1231 | t = netdev_priv(dev); |
| 1232 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1233 | ip6_tnl_unlink(t); |
| 1234 | err = ip6_tnl_change(t, &p); |
| 1235 | ip6_tnl_link(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | netdev_state_change(dev); |
| 1237 | } |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1238 | if (t) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | err = 0; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1240 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p))) |
| 1241 | err = -EFAULT; |
| 1242 | |
| 1243 | } else |
| 1244 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | break; |
| 1246 | case SIOCDELTUNNEL: |
| 1247 | err = -EPERM; |
| 1248 | if (!capable(CAP_NET_ADMIN)) |
| 1249 | break; |
| 1250 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1251 | if (dev == ip6_fb_tnl_dev) { |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1252 | err = -EFAULT; |
| 1253 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1255 | err = -ENOENT; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1256 | if ((t = ip6_tnl_locate(&p, 0)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1258 | err = -EPERM; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1259 | if (t->dev == ip6_fb_tnl_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1261 | dev = t->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | } |
Stephen Hemminger | 22f8cde | 2007-02-07 00:09:58 -0800 | [diff] [blame] | 1263 | err = 0; |
| 1264 | unregister_netdevice(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | break; |
| 1266 | default: |
| 1267 | err = -EINVAL; |
| 1268 | } |
| 1269 | return err; |
| 1270 | } |
| 1271 | |
| 1272 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1273 | * ip6_tnl_get_stats - return the stats for tunnel device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | * @dev: virtual device associated with tunnel |
| 1275 | * |
| 1276 | * Return: stats for device |
| 1277 | **/ |
| 1278 | |
| 1279 | static struct net_device_stats * |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1280 | ip6_tnl_get_stats(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1282 | return &(((struct ip6_tnl *)netdev_priv(dev))->stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1286 | * ip6_tnl_change_mtu - change mtu manually for tunnel device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | * @dev: virtual device associated with tunnel |
| 1288 | * @new_mtu: the new mtu |
| 1289 | * |
| 1290 | * Return: |
| 1291 | * 0 on success, |
| 1292 | * %-EINVAL if mtu too small |
| 1293 | **/ |
| 1294 | |
| 1295 | static int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1296 | ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | { |
| 1298 | if (new_mtu < IPV6_MIN_MTU) { |
| 1299 | return -EINVAL; |
| 1300 | } |
| 1301 | dev->mtu = new_mtu; |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1306 | * ip6_tnl_dev_setup - setup virtual tunnel device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | * @dev: virtual device associated with tunnel |
| 1308 | * |
| 1309 | * Description: |
| 1310 | * Initialize function pointers and device parameters |
| 1311 | **/ |
| 1312 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1313 | static void ip6_tnl_dev_setup(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | { |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1315 | dev->uninit = ip6_tnl_dev_uninit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | dev->destructor = free_netdev; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1317 | dev->hard_start_xmit = ip6_tnl_xmit; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1318 | dev->get_stats = ip6_tnl_get_stats; |
| 1319 | dev->do_ioctl = ip6_tnl_ioctl; |
| 1320 | dev->change_mtu = ip6_tnl_change_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | dev->type = ARPHRD_TUNNEL6; |
| 1323 | dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr); |
| 1324 | dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr); |
| 1325 | dev->flags |= IFF_NOARP; |
| 1326 | dev->addr_len = sizeof(struct in6_addr); |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1331 | * ip6_tnl_dev_init_gen - general initializer for all tunnel devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | * @dev: virtual device associated with tunnel |
| 1333 | **/ |
| 1334 | |
| 1335 | static inline void |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1336 | ip6_tnl_dev_init_gen(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1338 | struct ip6_tnl *t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | t->dev = dev; |
| 1340 | strcpy(t->parms.name, dev->name); |
| 1341 | } |
| 1342 | |
| 1343 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1344 | * ip6_tnl_dev_init - initializer for all non fallback tunnel devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | * @dev: virtual device associated with tunnel |
| 1346 | **/ |
| 1347 | |
| 1348 | static int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1349 | ip6_tnl_dev_init(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1351 | struct ip6_tnl *t = netdev_priv(dev); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1352 | ip6_tnl_dev_init_gen(dev); |
| 1353 | ip6_tnl_link_config(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1358 | * ip6_fb_tnl_dev_init - initializer for fallback tunnel device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | * @dev: fallback device |
| 1360 | * |
| 1361 | * Return: 0 |
| 1362 | **/ |
| 1363 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1364 | static int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1365 | ip6_fb_tnl_dev_init(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1367 | struct ip6_tnl *t = netdev_priv(dev); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1368 | ip6_tnl_dev_init_gen(dev); |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 1369 | t->parms.proto = IPPROTO_IPV6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | dev_hold(dev); |
| 1371 | tnls_wc[0] = t; |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1375 | static struct xfrm6_tunnel ip4ip6_handler = { |
| 1376 | .handler = ip4ip6_rcv, |
| 1377 | .err_handler = ip4ip6_err, |
| 1378 | .priority = 1, |
| 1379 | }; |
| 1380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | static struct xfrm6_tunnel ip6ip6_handler = { |
Patrick McHardy | 0303770 | 2005-07-19 14:03:34 -0700 | [diff] [blame] | 1382 | .handler = ip6ip6_rcv, |
| 1383 | .err_handler = ip6ip6_err, |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1384 | .priority = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | }; |
| 1386 | |
| 1387 | /** |
| 1388 | * ip6_tunnel_init - register protocol and reserve needed resources |
| 1389 | * |
| 1390 | * Return: 0 on success |
| 1391 | **/ |
| 1392 | |
| 1393 | static int __init ip6_tunnel_init(void) |
| 1394 | { |
| 1395 | int err; |
| 1396 | |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1397 | if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) { |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1398 | printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n"); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1399 | err = -EAGAIN; |
| 1400 | goto out; |
| 1401 | } |
| 1402 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 1403 | if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) { |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1404 | printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n"); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1405 | err = -EAGAIN; |
| 1406 | goto unreg_ip4ip6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1408 | ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", |
| 1409 | ip6_tnl_dev_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1411 | if (!ip6_fb_tnl_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | err = -ENOMEM; |
| 1413 | goto fail; |
| 1414 | } |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1415 | ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1417 | if ((err = register_netdev(ip6_fb_tnl_dev))) { |
| 1418 | free_netdev(ip6_fb_tnl_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | goto fail; |
| 1420 | } |
| 1421 | return 0; |
| 1422 | fail: |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 1423 | xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1424 | unreg_ip4ip6: |
| 1425 | xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); |
| 1426 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return err; |
| 1428 | } |
| 1429 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1430 | static void __exit ip6_tnl_destroy_tunnels(void) |
Yasuyuki Kozakai | b3fdd9f | 2006-11-06 10:06:22 -0800 | [diff] [blame] | 1431 | { |
| 1432 | int h; |
| 1433 | struct ip6_tnl *t; |
| 1434 | |
| 1435 | for (h = 0; h < HASH_SIZE; h++) { |
| 1436 | while ((t = tnls_r_l[h]) != NULL) |
| 1437 | unregister_netdevice(t->dev); |
| 1438 | } |
| 1439 | |
| 1440 | t = tnls_wc[0]; |
| 1441 | unregister_netdevice(t->dev); |
| 1442 | } |
| 1443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | /** |
| 1445 | * ip6_tunnel_cleanup - free resources and unregister protocol |
| 1446 | **/ |
| 1447 | |
| 1448 | static void __exit ip6_tunnel_cleanup(void) |
| 1449 | { |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1450 | if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1451 | printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n"); |
Yasuyuki Kozakai | c4d3efaf | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1452 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 1453 | if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1454 | printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
Yasuyuki Kozakai | b3fdd9f | 2006-11-06 10:06:22 -0800 | [diff] [blame] | 1456 | rtnl_lock(); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1457 | ip6_tnl_destroy_tunnels(); |
Yasuyuki Kozakai | b3fdd9f | 2006-11-06 10:06:22 -0800 | [diff] [blame] | 1458 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | module_init(ip6_tunnel_init); |
| 1462 | module_exit(ip6_tunnel_cleanup); |