blob: cb7e2ded6f08cce17f8fb11a7e7e119e8564d661 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09002 * IPv6 tunneling device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Ville Nuorvala <vnuorval@tcs.hut.fi>
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09007 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Based on:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090010 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * RFC 2473
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 */
20
Joe Perchesf3213832012-05-15 14:11:53 +000021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080024#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/types.h>
27#include <linux/sockios.h>
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090028#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000043#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070046#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090048#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/ip.h>
50#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip6_route.h>
52#include <net/addrconf.h>
53#include <net/ip6_tunnel.h>
54#include <net/xfrm.h>
55#include <net/dsfield.h>
56#include <net/inet_ecn.h>
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070057#include <net/net_namespace.h>
58#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60MODULE_AUTHOR("Ville Nuorvala");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090061MODULE_DESCRIPTION("IPv6 tunneling device");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_LICENSE("GPL");
stephen hemminger6dfbd872011-03-10 11:43:19 +000063MODULE_ALIAS_NETDEV("ip6tnl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#ifdef IP6_TNL_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +000066#define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#else
68#define IP6_TNL_TRACE(x...) do {;} while(0)
69#endif
70
71#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090072#define IPV6_TCLASS_SHIFT 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric Dumazetddbe5032012-07-18 08:11:12 +000074#define HASH_SIZE_SHIFT 5
75#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Eric Dumazetddbe5032012-07-18 08:11:12 +000077static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
78{
79 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
80
81 return hash_32(hash, HASH_SIZE_SHIFT);
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Eric Dumazet8560f222010-09-28 03:23:34 +000084static int ip6_tnl_dev_init(struct net_device *dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090085static void ip6_tnl_dev_setup(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Eric Dumazetf99189b2009-11-17 10:42:49 +000087static int ip6_tnl_net_id __read_mostly;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070088struct ip6_tnl_net {
Pavel Emelyanov15820e1292008-04-16 01:23:02 -070089 /* the IPv6 tunnel fallback device */
90 struct net_device *fb_tnl_dev;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -070091 /* lists for storing tunnels in use */
Eric Dumazet94767632010-09-15 20:25:34 +000092 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
93 struct ip6_tnl __rcu *tnls_wc[1];
94 struct ip6_tnl __rcu **tnls[2];
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070095};
96
Eric Dumazet8560f222010-09-28 03:23:34 +000097/* often modified stats are per cpu, other are shared (netdev->stats) */
98struct pcpu_tstats {
99 unsigned long rx_packets;
100 unsigned long rx_bytes;
101 unsigned long tx_packets;
102 unsigned long tx_bytes;
Eric Dumazet8ce120f2011-11-04 23:19:28 +0000103} __attribute__((aligned(4*sizeof(unsigned long))));
Eric Dumazet8560f222010-09-28 03:23:34 +0000104
105static struct net_device_stats *ip6_get_stats(struct net_device *dev)
106{
107 struct pcpu_tstats sum = { 0 };
108 int i;
109
110 for_each_possible_cpu(i) {
111 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
112
113 sum.rx_packets += tstats->rx_packets;
114 sum.rx_bytes += tstats->rx_bytes;
115 sum.tx_packets += tstats->tx_packets;
116 sum.tx_bytes += tstats->tx_bytes;
117 }
118 dev->stats.rx_packets = sum.rx_packets;
119 dev->stats.rx_bytes = sum.rx_bytes;
120 dev->stats.tx_packets = sum.tx_packets;
121 dev->stats.tx_bytes = sum.tx_bytes;
122 return &dev->stats;
123}
124
Eric Dumazet2922bc82009-10-23 06:34:34 +0000125/*
Eric Dumazet94767632010-09-15 20:25:34 +0000126 * Locking : hash tables are protected by RCU and RTNL
Eric Dumazet2922bc82009-10-23 06:34:34 +0000127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000129struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct dst_entry *dst = t->dst_cache;
132
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900133 if (dst && dst->obsolete &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dst->ops->check(dst, t->dst_cookie) == NULL) {
135 t->dst_cache = NULL;
136 dst_release(dst);
137 return NULL;
138 }
139
140 return dst;
141}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000142EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000144void ip6_tnl_dst_reset(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 dst_release(t->dst_cache);
147 t->dst_cache = NULL;
148}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000149EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000151void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct rt6_info *rt = (struct rt6_info *) dst;
154 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
155 dst_release(t->dst_cache);
156 t->dst_cache = dst;
157}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000158EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900161 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900162 * @remote: the address of the tunnel exit-point
163 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900165 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900167 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * else %NULL
169 **/
170
Eric Dumazet2922bc82009-10-23 06:34:34 +0000171#define for_each_ip6_tunnel_rcu(start) \
172 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static struct ip6_tnl *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000175ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000177 unsigned int hash = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct ip6_tnl *t;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700179 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Eric Dumazetddbe5032012-07-18 08:11:12 +0000181 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (ipv6_addr_equal(local, &t->parms.laddr) &&
183 ipv6_addr_equal(remote, &t->parms.raddr) &&
184 (t->dev->flags & IFF_UP))
185 return t;
186 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000187 t = rcu_dereference(ip6n->tnls_wc[0]);
188 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return t;
190
191 return NULL;
192}
193
194/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900195 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900196 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
198 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900199 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * &struct in6_addr entries laddr and raddr in @p.
201 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900202 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 **/
204
Eric Dumazet94767632010-09-15 20:25:34 +0000205static struct ip6_tnl __rcu **
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000206ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000208 const struct in6_addr *remote = &p->raddr;
209 const struct in6_addr *local = &p->laddr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000210 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int prio = 0;
212
213 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
214 prio = 1;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000215 h = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700217 return &ip6n->tnls[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900221 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * @t: tunnel to be added
223 **/
224
225static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700226ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Eric Dumazet94767632010-09-15 20:25:34 +0000228 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Dumazetcf778b02012-01-12 04:41:32 +0000230 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
231 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900235 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * @t: tunnel to be removed
237 **/
238
239static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700240ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Eric Dumazet94767632010-09-15 20:25:34 +0000242 struct ip6_tnl __rcu **tp;
243 struct ip6_tnl *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Eric Dumazet94767632010-09-15 20:25:34 +0000245 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
246 (iter = rtnl_dereference(*tp)) != NULL;
247 tp = &iter->next) {
248 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000249 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251 }
252 }
253}
254
Eric Dumazet8560f222010-09-28 03:23:34 +0000255static void ip6_dev_free(struct net_device *dev)
256{
257 free_percpu(dev->tstats);
258 free_netdev(dev);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000262 * ip6_tnl_create - create a new tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * @p: tunnel parameters
264 * @pt: pointer to new tunnel
265 *
266 * Description:
267 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900268 *
269 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800270 * created tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 **/
272
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000273static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct net_device *dev;
276 struct ip6_tnl *t;
277 char name[IFNAMSIZ];
278 int err;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700279 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800281 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800283 else
284 sprintf(name, "ip6tnl%%d");
285
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900286 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800288 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Pavel Emelyanov554eb272008-04-16 01:24:13 -0700290 dev_net_set(dev, net);
291
Patrick McHardy2941a482006-01-08 22:05:26 -0800292 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 t->parms = *p;
Eric Dumazet8560f222010-09-28 03:23:34 +0000294 err = ip6_tnl_dev_init(dev);
295 if (err < 0)
296 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800298 if ((err = register_netdevice(dev)) < 0)
299 goto failed_free;
300
Josh Boyer731abb92011-11-10 15:10:23 +0000301 strcpy(t->parms.name, dev->name);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 dev_hold(dev);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700304 ip6_tnl_link(ip6n, t);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800305 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800306
307failed_free:
Eric Dumazet8560f222010-09-28 03:23:34 +0000308 ip6_dev_free(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800309failed:
310 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900314 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900315 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * @create: != 0 if allowed to create new tunnel if no match found
317 *
318 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900319 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * based on @parms. If this is unsuccessful, but @create is set a new
321 * tunnel device is created and registered for use.
322 *
323 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800324 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 **/
326
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700327static struct ip6_tnl *ip6_tnl_locate(struct net *net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000328 struct __ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000330 const struct in6_addr *remote = &p->raddr;
331 const struct in6_addr *local = &p->laddr;
Eric Dumazet94767632010-09-15 20:25:34 +0000332 struct ip6_tnl __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700334 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Eric Dumazet94767632010-09-15 20:25:34 +0000336 for (tp = ip6_tnl_bucket(ip6n, p);
337 (t = rtnl_dereference(*tp)) != NULL;
338 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Ville Nuorvala567131a2006-11-24 17:05:41 -0800340 ipv6_addr_equal(remote, &t->parms.raddr))
341 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800344 return NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700345 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900349 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900351 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900353 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 **/
355
356static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900357ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Patrick McHardy2941a482006-01-08 22:05:26 -0800359 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700360 struct net *net = dev_net(dev);
361 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Eric Dumazet94767632010-09-15 20:25:34 +0000363 if (dev == ip6n->fb_tnl_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000364 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
Eric Dumazet94767632010-09-15 20:25:34 +0000365 else
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700366 ip6_tnl_unlink(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ip6_tnl_dst_reset(t);
368 dev_put(dev);
369}
370
371/**
372 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
373 * @skb: received socket buffer
374 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900375 * Return:
376 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * else index to encapsulation limit
378 **/
379
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000380__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000382 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 __u8 nexthdr = ipv6h->nexthdr;
384 __u16 off = sizeof (*ipv6h);
385
386 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
387 __u16 optlen = 0;
388 struct ipv6_opt_hdr *hdr;
389 if (raw + off + sizeof (*hdr) > skb->data &&
390 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
391 break;
392
393 hdr = (struct ipv6_opt_hdr *) (raw + off);
394 if (nexthdr == NEXTHDR_FRAGMENT) {
395 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
396 if (frag_hdr->frag_off)
397 break;
398 optlen = 8;
399 } else if (nexthdr == NEXTHDR_AUTH) {
400 optlen = (hdr->hdrlen + 2) << 2;
401 } else {
402 optlen = ipv6_optlen(hdr);
403 }
404 if (nexthdr == NEXTHDR_DEST) {
405 __u16 i = off + 2;
406 while (1) {
407 struct ipv6_tlv_tnl_enc_lim *tel;
408
409 /* No more room for encapsulation limit */
410 if (i + sizeof (*tel) > off + optlen)
411 break;
412
413 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
414 /* return index of option if found and valid */
415 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
416 tel->length == 1)
417 return i;
418 /* else jump to next option */
419 if (tel->type)
420 i += tel->length + 2;
421 else
422 i++;
423 }
424 }
425 nexthdr = hdr->nexthdr;
426 off += optlen;
427 }
428 return 0;
429}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000430EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900433 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *
435 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900436 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * to the specifications in RFC 2473.
438 **/
439
Herbert Xud2acc342006-03-28 01:12:13 -0800440static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900441ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700442 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000444 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct ip6_tnl *t;
446 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700447 u8 rel_type = ICMPV6_DEST_UNREACH;
448 u8 rel_code = ICMPV6_ADDR_UNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 __u32 rel_info = 0;
450 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800451 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900453 /* If the packet doesn't contain the original IPv6 header we are
454 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 processing of the error. */
456
Eric Dumazet2922bc82009-10-23 06:34:34 +0000457 rcu_read_lock();
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700458 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700459 &ipv6h->saddr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out;
461
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900462 if (t->parms.proto != ipproto && t->parms.proto != 0)
463 goto out;
464
Herbert Xud2acc342006-03-28 01:12:13 -0800465 err = 0;
466
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900467 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 __u32 teli;
469 struct ipv6_tlv_tnl_enc_lim *tel;
470 __u32 mtu;
471 case ICMPV6_DEST_UNREACH:
Joe Perchese87cc472012-05-13 21:56:26 +0000472 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
473 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 rel_msg = 1;
475 break;
476 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900477 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Joe Perchese87cc472012-05-13 21:56:26 +0000478 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
479 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 rel_msg = 1;
481 }
482 break;
483 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800484 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900485 if ((*code) == ICMPV6_HDR_FIELD)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000486 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Al Viro704eae12007-07-26 17:33:29 +0100488 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
490 if (tel->encap_limit == 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000491 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
492 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 rel_msg = 1;
494 }
Joe Perchese87cc472012-05-13 21:56:26 +0000495 } else {
496 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
497 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 break;
500 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100501 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (mtu < IPV6_MIN_MTU)
503 mtu = IPV6_MIN_MTU;
504 t->dev->mtu = mtu;
505
Al Virocc6cdac2006-02-18 16:02:18 -0500506 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 rel_type = ICMPV6_PKT_TOOBIG;
508 rel_code = 0;
509 rel_info = mtu;
510 rel_msg = 1;
511 }
512 break;
513 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900514
515 *type = rel_type;
516 *code = rel_code;
517 *info = rel_info;
518 *msg = rel_msg;
519
520out:
Eric Dumazet2922bc82009-10-23 06:34:34 +0000521 rcu_read_unlock();
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900522 return err;
523}
524
525static int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900526ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700527 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900528{
529 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700530 u8 rel_type = type;
531 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100532 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900533 int err;
534 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000535 const struct iphdr *eiph;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900536 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -0700537 struct flowi4 fl4;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900538
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900539 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
540 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900541 if (err < 0)
542 return err;
543
544 if (rel_msg == 0)
545 return 0;
546
547 switch (rel_type) {
548 case ICMPV6_DEST_UNREACH:
549 if (rel_code != ICMPV6_ADDR_UNREACH)
550 return 0;
551 rel_type = ICMP_DEST_UNREACH;
552 rel_code = ICMP_HOST_UNREACH;
553 break;
554 case ICMPV6_PKT_TOOBIG:
555 if (rel_code != 0)
556 return 0;
557 rel_type = ICMP_DEST_UNREACH;
558 rel_code = ICMP_FRAG_NEEDED;
559 break;
David S. Millerec18d9a2012-07-12 00:25:15 -0700560 case NDISC_REDIRECT:
561 rel_type = ICMP_REDIRECT;
562 rel_code = ICMP_REDIR_HOST;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900563 default:
564 return 0;
565 }
566
567 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
568 return 0;
569
570 skb2 = skb_clone(skb, GFP_ATOMIC);
571 if (!skb2)
572 return 0;
573
Eric Dumazetadf30902009-06-02 05:19:30 +0000574 skb_dst_drop(skb2);
575
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900576 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700577 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700578 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900579
580 /* Try to guess incoming interface */
David S. Miller31e45432011-05-03 20:25:42 -0700581 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500582 eiph->saddr, 0,
583 0, 0,
584 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800585 if (IS_ERR(rt))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900586 goto out;
587
Changli Gaod8d1f302010-06-10 23:31:35 -0700588 skb2->dev = rt->dst.dev;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900589
590 /* route "incoming" packet */
591 if (rt->rt_flags & RTCF_LOCAL) {
592 ip_rt_put(rt);
593 rt = NULL;
David S. Miller31e45432011-05-03 20:25:42 -0700594 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500595 eiph->daddr, eiph->saddr,
596 0, 0,
597 IPPROTO_IPIP,
598 RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800599 if (IS_ERR(rt) ||
Changli Gaod8d1f302010-06-10 23:31:35 -0700600 rt->dst.dev->type != ARPHRD_TUNNEL) {
David S. Millerb23dd4f2011-03-02 14:31:35 -0800601 if (!IS_ERR(rt))
602 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900603 goto out;
604 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800605 skb_dst_set(skb2, &rt->dst);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900606 } else {
607 ip_rt_put(rt);
608 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
609 skb2->dev) ||
Eric Dumazetadf30902009-06-02 05:19:30 +0000610 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900611 goto out;
612 }
613
614 /* change mtu on this route */
615 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000616 if (rel_info > dst_mtu(skb_dst(skb2)))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900617 goto out;
618
David S. Miller6700c272012-07-17 03:29:28 -0700619 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900620 }
David S. Miller1ed5c482012-07-12 00:41:25 -0700621 if (rel_type == ICMP_REDIRECT)
David S. Miller6700c272012-07-17 03:29:28 -0700622 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900623
Al Viro704eae12007-07-26 17:33:29 +0100624 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900625
626out:
627 kfree_skb(skb2);
628 return 0;
629}
630
631static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900632ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700633 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900634{
635 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700636 u8 rel_type = type;
637 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100638 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900639 int err;
640
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900641 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
642 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900643 if (err < 0)
644 return err;
645
646 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct rt6_info *rt;
648 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Eric Dumazetadf30902009-06-02 05:19:30 +0000653 skb_dst_drop(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700655 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* Try to guess incoming interface */
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700658 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
659 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David S. Millerd1918542011-12-28 20:19:20 -0500661 if (rt && rt->dst.dev)
662 skb2->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000664 icmpv6_send(skb2, rel_type, rel_code, rel_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -0700667 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 kfree_skb(skb2);
670 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900671
672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000675static void ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
676 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900677 struct sk_buff *skb)
678{
679 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
680
681 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700682 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900683
684 if (INET_ECN_is_ce(dsfield))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700685 IP_ECN_set_ce(ip_hdr(skb));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900686}
687
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000688static void ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
689 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900690 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900692 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800693 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900695 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700696 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900698
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000699__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000700 const struct in6_addr *laddr,
701 const struct in6_addr *raddr)
702{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000703 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000704 int ltype = ipv6_addr_type(laddr);
705 int rtype = ipv6_addr_type(raddr);
706 __u32 flags = 0;
707
708 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
709 flags = IP6_TNL_F_CAP_PER_PACKET;
710 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
711 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
712 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
713 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
714 if (ltype&IPV6_ADDR_UNICAST)
715 flags |= IP6_TNL_F_CAP_XMIT;
716 if (rtype&IPV6_ADDR_UNICAST)
717 flags |= IP6_TNL_F_CAP_RCV;
718 }
719 return flags;
720}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000721EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000722
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100723/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000724int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000725 const struct in6_addr *laddr,
726 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800727{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000728 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800729 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700730 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800731
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000732 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
733 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
734 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900735 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800736
737 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100738 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800739
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000740 if ((ipv6_addr_is_multicast(laddr) ||
741 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
742 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800743 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800744 }
745 return ret;
746}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000747EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900750 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900752 * @protocol: ethernet protocol ID
753 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 *
755 * Return: 0
756 **/
757
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900758static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900759 __u8 ipproto,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000760 void (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
761 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900762 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000765 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Eric Dumazet2922bc82009-10-23 06:34:34 +0000767 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700769 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700770 &ipv6h->daddr)) != NULL) {
Eric Dumazet8560f222010-09-28 03:23:34 +0000771 struct pcpu_tstats *tstats;
772
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900773 if (t->parms.proto != ipproto && t->parms.proto != 0) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000774 rcu_read_unlock();
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900775 goto discard;
776 }
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000779 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700780 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000783 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700784 t->dev->stats.rx_dropped++;
Eric Dumazet2922bc82009-10-23 06:34:34 +0000785 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto discard;
787 }
788 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700789 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700790 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900791 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 skb->pkt_type = PACKET_HOST;
793 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700794
Eric Dumazet8560f222010-09-28 03:23:34 +0000795 tstats = this_cpu_ptr(t->dev->tstats);
796 tstats->rx_packets++;
797 tstats->rx_bytes += skb->len;
798
799 __skb_tunnel_rx(skb, t->dev);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900800
801 dscp_ecn_decapsulate(t, ipv6h, skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000802
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000803 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000804
Eric Dumazet2922bc82009-10-23 06:34:34 +0000805 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 0;
807 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000808 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700810
811discard:
812 kfree_skb(skb);
813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900816static int ip4ip6_rcv(struct sk_buff *skb)
817{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900818 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
819 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900820}
821
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900822static int ip6ip6_rcv(struct sk_buff *skb)
823{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900824 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
825 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900826}
827
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800828struct ipv6_tel_txoption {
829 struct ipv6_txoptions ops;
830 __u8 dst_opt[8];
831};
832
833static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800835 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800837 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
838 opt->dst_opt[3] = 1;
839 opt->dst_opt[4] = encap_limit;
840 opt->dst_opt[5] = IPV6_TLV_PADN;
841 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800843 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
844 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900848 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900850 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 *
852 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900853 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * doesn't match source of incoming packet.
855 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900856 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * 1 if conflict,
858 * 0 else
859 **/
860
Eric Dumazet92113bf2012-05-18 08:14:11 +0200861static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000862ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
865}
866
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000867int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800868{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000869 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800870 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700871 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800872
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900873 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800874 struct net_device *ldev = NULL;
875
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100876 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800877 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100878 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800879
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700880 if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000881 pr_warn("%s xmit: Local address not yet configured!\n",
882 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800883 else if (!ipv6_addr_is_multicast(&p->raddr) &&
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700884 unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000885 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
886 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800887 else
888 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100889 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800890 }
891 return ret;
892}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000893EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900896 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900898 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900899 * @dsfield: dscp code for outer header
900 * @fl: flow of tunneled packet
901 * @encap_limit: encapsulation limit
902 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 *
904 * Description:
905 * Build new header and do some sanity checks on the packet before sending
906 * it.
907 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900908 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900909 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900910 * -1 fail
911 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 **/
913
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900914static int ip6_tnl_xmit2(struct sk_buff *skb,
915 struct net_device *dev,
916 __u8 dsfield,
David S. Miller4c9483b2011-03-12 16:22:43 -0500917 struct flowi6 *fl6,
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900918 int encap_limit,
919 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800921 struct net *net = dev_net(dev);
Patrick McHardy2941a482006-01-08 22:05:26 -0800922 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700923 struct net_device_stats *stats = &t->dev->stats;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700924 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800925 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400926 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct net_device *tdev;
928 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -0700929 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900931 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400934 if (!fl6->flowi6_mark)
935 dst = ip6_tnl_dst_check(t);
Eric Dumazet89b02122011-07-28 04:32:25 +0000936 if (!dst) {
937 ndst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Eric Dumazet89b02122011-07-28 04:32:25 +0000939 if (ndst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700940 goto tx_err_link_failure;
Eric Dumazet89b02122011-07-28 04:32:25 +0000941 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
942 if (IS_ERR(ndst)) {
943 err = PTR_ERR(ndst);
944 ndst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -0800945 goto tx_err_link_failure;
946 }
Eric Dumazet89b02122011-07-28 04:32:25 +0000947 dst = ndst;
Patrick McHardya57ebc92005-09-08 14:27:47 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 tdev = dst->dev;
951
952 if (tdev == dev) {
953 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +0000954 net_warn_ratelimited("%s: Local routing loop detected!\n",
955 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto tx_err_dst_release;
957 }
958 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800959 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 max_headroom += 8;
961 mtu -= 8;
962 }
963 if (mtu < IPV6_MIN_MTU)
964 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +0000965 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -0700966 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900968 *pmtu = mtu;
969 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto tx_err_dst_release;
971 }
972
973 /*
974 * Okay, now see if we can stuff it in the buffer as-is.
975 */
976 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900977
Patrick McHardycfbba492007-07-09 15:33:40 -0700978 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
979 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
983 goto tx_err_dst_release;
984
985 if (skb->sk)
986 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +0000987 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 skb = new_skb;
989 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000990 skb_dst_drop(skb);
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400991 if (fl6->flowi6_mark) {
992 skb_dst_set(skb, dst);
993 ndst = NULL;
994 } else {
995 skb_dst_set_noref(skb, dst);
996 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700997 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
David S. Miller4c9483b2011-03-12 16:22:43 -0500999 proto = fl6->flowi6_proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001000 if (encap_limit >= 0) {
1001 init_tel_txopt(&opt, encap_limit);
1002 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1003 }
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001004 skb_push(skb, sizeof(struct ipv6hdr));
1005 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001006 ipv6h = ipv6_hdr(skb);
David S. Miller4c9483b2011-03-12 16:22:43 -05001007 *(__be32*)ipv6h = fl6->flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 dsfield = INET_ECN_encapsulate(0, dsfield);
1009 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ipv6h->hop_limit = t->parms.hop_limit;
1011 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001012 ipv6h->saddr = fl6->saddr;
1013 ipv6h->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 nf_reset(skb);
1015 pkt_len = skb->len;
Herbert Xuef76bc22008-01-11 19:15:08 -08001016 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -02001018 if (net_xmit_eval(err) == 0) {
Eric Dumazet8560f222010-09-28 03:23:34 +00001019 struct pcpu_tstats *tstats = this_cpu_ptr(t->dev->tstats);
1020
1021 tstats->tx_bytes += pkt_len;
1022 tstats->tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else {
1024 stats->tx_errors++;
1025 stats->tx_aborted_errors++;
1026 }
Eric Dumazet89b02122011-07-28 04:32:25 +00001027 if (ndst)
1028 ip6_tnl_dst_store(t, ndst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return 0;
1030tx_err_link_failure:
1031 stats->tx_carrier_errors++;
1032 dst_link_failure(skb);
1033tx_err_dst_release:
Eric Dumazet89b02122011-07-28 04:32:25 +00001034 dst_release(ndst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001035 return err;
1036}
1037
1038static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001039ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1040{
1041 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001042 const struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001043 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001044 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001045 __u8 dsfield;
1046 __u32 mtu;
1047 int err;
1048
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001049 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
1050 !ip6_tnl_xmit_ctl(t))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001051 return -1;
1052
1053 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1054 encap_limit = t->parms.encap_limit;
1055
David S. Miller4c9483b2011-03-12 16:22:43 -05001056 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1057 fl6.flowi6_proto = IPPROTO_IPIP;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001058
1059 dsfield = ipv4_get_dsfield(iph);
1060
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001061 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001062 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
Al Virob77f2fa2007-07-21 19:09:41 -07001063 & IPV6_TCLASS_MASK;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001064 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1065 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001066
David S. Miller4c9483b2011-03-12 16:22:43 -05001067 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001068 if (err != 0) {
1069 /* XXX: send ICMP error even if DF is not set. */
1070 if (err == -EMSGSIZE)
1071 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1072 htonl(mtu));
1073 return -1;
1074 }
1075
1076 return 0;
1077}
1078
1079static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001080ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1081{
1082 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001083 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001084 int encap_limit = -1;
1085 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001086 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001087 __u8 dsfield;
1088 __u32 mtu;
1089 int err;
1090
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001091 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1092 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001093 return -1;
1094
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001095 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001096 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001097 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001098 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001099 if (tel->encap_limit == 0) {
1100 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001101 ICMPV6_HDR_FIELD, offset + 2);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001102 return -1;
1103 }
1104 encap_limit = tel->encap_limit - 1;
1105 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1106 encap_limit = t->parms.encap_limit;
1107
David S. Miller4c9483b2011-03-12 16:22:43 -05001108 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1109 fl6.flowi6_proto = IPPROTO_IPV6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001110
1111 dsfield = ipv6_get_dsfield(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001112 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001113 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001114 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
David S. Miller4c9483b2011-03-12 16:22:43 -05001115 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001116 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1117 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001118
David S. Miller4c9483b2011-03-12 16:22:43 -05001119 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001120 if (err != 0) {
1121 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001122 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001123 return -1;
1124 }
1125
1126 return 0;
1127}
1128
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001129static netdev_tx_t
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001130ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1131{
1132 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001133 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001134 int ret;
1135
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001136 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001137 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001138 ret = ip4ip6_tnl_xmit(skb, dev);
1139 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001140 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001141 ret = ip6ip6_tnl_xmit(skb, dev);
1142 break;
1143 default:
1144 goto tx_err;
1145 }
1146
1147 if (ret < 0)
1148 goto tx_err;
1149
Patrick McHardy6ed10652009-06-23 06:03:08 +00001150 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152tx_err:
1153 stats->tx_errors++;
1154 stats->tx_dropped++;
1155 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001156 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001159static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001162 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001163 struct flowi6 *fl6 = &t->fl.u.ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001165 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1166 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001169 fl6->saddr = p->laddr;
1170 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001171 fl6->flowi6_oif = p->link;
1172 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001175 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001177 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001179 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1180 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1183 dev->flags |= IFF_POINTOPOINT;
1184 else
1185 dev->flags &= ~IFF_POINTOPOINT;
1186
1187 dev->iflink = p->link;
1188
1189 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001190 int strict = (ipv6_addr_type(&p->raddr) &
1191 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1192
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001193 struct rt6_info *rt = rt6_lookup(dev_net(dev),
1194 &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001195 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (rt == NULL)
1198 return;
1199
David S. Millerd1918542011-12-28 20:19:20 -05001200 if (rt->dst.dev) {
1201 dev->hard_header_len = rt->dst.dev->hard_header_len +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 sizeof (struct ipv6hdr);
1203
David S. Millerd1918542011-12-28 20:19:20 -05001204 dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001205 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1206 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (dev->mtu < IPV6_MIN_MTU)
1209 dev->mtu = IPV6_MIN_MTU;
1210 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001211 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213}
1214
1215/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001216 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * @t: tunnel to be changed
1218 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 *
1220 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001221 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 **/
1223
1224static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001225ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001227 t->parms.laddr = p->laddr;
1228 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 t->parms.flags = p->flags;
1230 t->parms.hop_limit = p->hop_limit;
1231 t->parms.encap_limit = p->encap_limit;
1232 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001233 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001234 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001235 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001236 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return 0;
1238}
1239
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001240static void
1241ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1242{
1243 p->laddr = u->laddr;
1244 p->raddr = u->raddr;
1245 p->flags = u->flags;
1246 p->hop_limit = u->hop_limit;
1247 p->encap_limit = u->encap_limit;
1248 p->flowinfo = u->flowinfo;
1249 p->link = u->link;
1250 p->proto = u->proto;
1251 memcpy(p->name, u->name, sizeof(u->name));
1252}
1253
1254static void
1255ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1256{
1257 u->laddr = p->laddr;
1258 u->raddr = p->raddr;
1259 u->flags = p->flags;
1260 u->hop_limit = p->hop_limit;
1261 u->encap_limit = p->encap_limit;
1262 u->flowinfo = p->flowinfo;
1263 u->link = p->link;
1264 u->proto = p->proto;
1265 memcpy(u->name, p->name, sizeof(u->name));
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001269 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * @dev: virtual device associated with tunnel
1271 * @ifr: parameters passed from userspace
1272 * @cmd: command to be performed
1273 *
1274 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001275 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001276 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 *
1278 * The possible commands are the following:
1279 * %SIOCGETTUNNEL: get tunnel parameters for device
1280 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1281 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1282 * %SIOCDELTUNNEL: delete tunnel
1283 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001284 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 * initialization, can be used for creating other tunnel devices.
1286 *
1287 * Return:
1288 * 0 on success,
1289 * %-EFAULT if unable to copy data to or from userspace,
1290 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1291 * %-EINVAL if passed tunnel parameters are invalid,
1292 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1293 * %-ENODEV if attempting to change or delete a nonexisting device
1294 **/
1295
1296static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001297ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001301 struct __ip6_tnl_parm p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct ip6_tnl *t = NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001303 struct net *net = dev_net(dev);
1304 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 switch (cmd) {
1307 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001308 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001309 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 err = -EFAULT;
1311 break;
1312 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001313 ip6_tnl_parm_from_user(&p1, &p);
1314 t = ip6_tnl_locate(net, &p1, 0);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001315 } else {
1316 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001317 }
1318 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001319 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001320 ip6_tnl_parm_to_user(&p, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1322 err = -EFAULT;
1323 }
1324 break;
1325 case SIOCADDTUNNEL:
1326 case SIOCCHGTUNNEL:
1327 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (!capable(CAP_NET_ADMIN))
1329 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001330 err = -EFAULT;
1331 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001333 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001334 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1335 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001337 ip6_tnl_parm_from_user(&p1, &p);
1338 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001339 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001340 if (t != NULL) {
1341 if (t->dev != dev) {
1342 err = -EEXIST;
1343 break;
1344 }
1345 } else
1346 t = netdev_priv(dev);
1347
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001348 ip6_tnl_unlink(ip6n, t);
Pavel Emelyanov74b0b852010-10-27 05:43:53 +00001349 synchronize_net();
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001350 err = ip6_tnl_change(t, &p1);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001351 ip6_tnl_link(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 netdev_state_change(dev);
1353 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001354 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001356 ip6_tnl_parm_to_user(&p, &t->parms);
1357 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001358 err = -EFAULT;
1359
1360 } else
1361 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
1363 case SIOCDELTUNNEL:
1364 err = -EPERM;
1365 if (!capable(CAP_NET_ADMIN))
1366 break;
1367
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001368 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001369 err = -EFAULT;
1370 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001372 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001373 ip6_tnl_parm_from_user(&p1, &p);
1374 t = ip6_tnl_locate(net, &p1, 0);
1375 if (t == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001377 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001378 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001380 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001382 err = 0;
1383 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 default:
1386 err = -EINVAL;
1387 }
1388 return err;
1389}
1390
1391/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001392 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * @dev: virtual device associated with tunnel
1394 * @new_mtu: the new mtu
1395 *
1396 * Return:
1397 * 0 on success,
1398 * %-EINVAL if mtu too small
1399 **/
1400
1401static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001402ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 if (new_mtu < IPV6_MIN_MTU) {
1405 return -EINVAL;
1406 }
1407 dev->mtu = new_mtu;
1408 return 0;
1409}
1410
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001411
1412static const struct net_device_ops ip6_tnl_netdev_ops = {
Eric Dumazet8560f222010-09-28 03:23:34 +00001413 .ndo_uninit = ip6_tnl_dev_uninit,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001414 .ndo_start_xmit = ip6_tnl_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001415 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001416 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001417 .ndo_get_stats = ip6_get_stats,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001418};
1419
Eric Dumazet8560f222010-09-28 03:23:34 +00001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001422 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * @dev: virtual device associated with tunnel
1424 *
1425 * Description:
1426 * Initialize function pointers and device parameters
1427 **/
1428
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001429static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Anders Franzen381601e2010-11-24 05:47:18 +00001431 struct ip6_tnl *t;
1432
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001433 dev->netdev_ops = &ip6_tnl_netdev_ops;
Eric Dumazet8560f222010-09-28 03:23:34 +00001434 dev->destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 dev->type = ARPHRD_TUNNEL6;
1437 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1438 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001439 t = netdev_priv(dev);
1440 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1441 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 dev->flags |= IFF_NOARP;
1443 dev->addr_len = sizeof(struct in6_addr);
Pavel Emelyanov554eb272008-04-16 01:24:13 -07001444 dev->features |= NETIF_F_NETNS_LOCAL;
Anders Franzen7e223de2010-10-19 03:50:47 +00001445 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448
1449/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001450 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 * @dev: virtual device associated with tunnel
1452 **/
1453
Eric Dumazet8560f222010-09-28 03:23:34 +00001454static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001455ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Patrick McHardy2941a482006-01-08 22:05:26 -08001457 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 t->dev = dev;
Eric Dumazet8560f222010-09-28 03:23:34 +00001460 dev->tstats = alloc_percpu(struct pcpu_tstats);
1461 if (!dev->tstats)
1462 return -ENOMEM;
1463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001467 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 * @dev: virtual device associated with tunnel
1469 **/
1470
Eric Dumazet8560f222010-09-28 03:23:34 +00001471static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Patrick McHardy2941a482006-01-08 22:05:26 -08001473 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001474 int err = ip6_tnl_dev_init_gen(dev);
1475
1476 if (err)
1477 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001478 ip6_tnl_link_config(t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001483 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * @dev: fallback device
1485 *
1486 * Return: 0
1487 **/
1488
Eric Dumazet8560f222010-09-28 03:23:34 +00001489static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Patrick McHardy2941a482006-01-08 22:05:26 -08001491 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001492 struct net *net = dev_net(dev);
1493 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001494 int err = ip6_tnl_dev_init_gen(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001495
Eric Dumazet8560f222010-09-28 03:23:34 +00001496 if (err)
1497 return err;
1498
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001499 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001501
1502 ip6_tnl_link_config(t);
1503
Eric Dumazetcf778b02012-01-12 04:41:32 +00001504 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001508static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001509 .handler = ip4ip6_rcv,
1510 .err_handler = ip4ip6_err,
1511 .priority = 1,
1512};
1513
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001514static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07001515 .handler = ip6ip6_rcv,
1516 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001517 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518};
1519
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001520static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001521{
1522 int h;
1523 struct ip6_tnl *t;
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001524 LIST_HEAD(list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001525
1526 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00001527 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001528 while (t != NULL) {
1529 unregister_netdevice_queue(t->dev, &list);
Eric Dumazet94767632010-09-15 20:25:34 +00001530 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001531 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001532 }
1533
Eric Dumazet94767632010-09-15 20:25:34 +00001534 t = rtnl_dereference(ip6n->tnls_wc[0]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001535 unregister_netdevice_queue(t->dev, &list);
1536 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001537}
1538
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001539static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001540{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001541 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00001542 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001543 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001544
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001545 ip6n->tnls[0] = ip6n->tnls_wc;
1546 ip6n->tnls[1] = ip6n->tnls_r_l;
1547
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001548 err = -ENOMEM;
1549 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1550 ip6_tnl_dev_setup);
1551
1552 if (!ip6n->fb_tnl_dev)
1553 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001554 dev_net_set(ip6n->fb_tnl_dev, net);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001555
Eric Dumazet8560f222010-09-28 03:23:34 +00001556 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1557 if (err < 0)
1558 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001559
1560 err = register_netdev(ip6n->fb_tnl_dev);
1561 if (err < 0)
1562 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00001563
1564 t = netdev_priv(ip6n->fb_tnl_dev);
1565
1566 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001567 return 0;
1568
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001569err_register:
Eric Dumazet8560f222010-09-28 03:23:34 +00001570 ip6_dev_free(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001571err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001572 return err;
1573}
1574
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001575static void __net_exit ip6_tnl_exit_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001576{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001577 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001578
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001579 rtnl_lock();
1580 ip6_tnl_destroy_tunnels(ip6n);
1581 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001582}
1583
1584static struct pernet_operations ip6_tnl_net_ops = {
1585 .init = ip6_tnl_init_net,
1586 .exit = ip6_tnl_exit_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001587 .id = &ip6_tnl_net_id,
1588 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001589};
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591/**
1592 * ip6_tunnel_init - register protocol and reserve needed resources
1593 *
1594 * Return: 0 on success
1595 **/
1596
1597static int __init ip6_tunnel_init(void)
1598{
1599 int err;
1600
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001601 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001602 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001603 goto out_pernet;
1604
1605 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1606 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001607 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001608 goto out_ip4ip6;
1609 }
1610
1611 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1612 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001613 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001614 goto out_ip6ip6;
1615 }
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001618
1619out_ip6ip6:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001620 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001621out_ip4ip6:
1622 unregister_pernet_device(&ip6_tnl_net_ops);
1623out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return err;
1625}
1626
1627/**
1628 * ip6_tunnel_cleanup - free resources and unregister protocol
1629 **/
1630
1631static void __exit ip6_tunnel_cleanup(void)
1632{
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001633 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00001634 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001635
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001636 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00001637 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001639 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
1642module_init(ip6_tunnel_init);
1643module_exit(ip6_tunnel_cleanup);