blob: 09482f723064e88a4be559d957bde897e2d9dc3c [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
Amerigo Wang94e187c2012-10-29 00:13:19 +0000666 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 kfree_skb(skb2);
669 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900670
671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000674static void ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
675 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900676 struct sk_buff *skb)
677{
678 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
679
680 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700681 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900682
683 if (INET_ECN_is_ce(dsfield))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700684 IP_ECN_set_ce(ip_hdr(skb));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900685}
686
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000687static void ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
688 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900689 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900691 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800692 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900694 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700695 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900697
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000698__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000699 const struct in6_addr *laddr,
700 const struct in6_addr *raddr)
701{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000702 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000703 int ltype = ipv6_addr_type(laddr);
704 int rtype = ipv6_addr_type(raddr);
705 __u32 flags = 0;
706
707 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
708 flags = IP6_TNL_F_CAP_PER_PACKET;
709 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
710 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
711 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
712 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
713 if (ltype&IPV6_ADDR_UNICAST)
714 flags |= IP6_TNL_F_CAP_XMIT;
715 if (rtype&IPV6_ADDR_UNICAST)
716 flags |= IP6_TNL_F_CAP_RCV;
717 }
718 return flags;
719}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000720EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000721
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100722/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000723int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000724 const struct in6_addr *laddr,
725 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800726{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000727 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800728 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700729 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800730
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000731 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
732 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
733 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900734 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800735
736 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100737 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800738
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000739 if ((ipv6_addr_is_multicast(laddr) ||
740 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
741 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800742 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800743 }
744 return ret;
745}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000746EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900749 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900751 * @protocol: ethernet protocol ID
752 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *
754 * Return: 0
755 **/
756
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900757static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900758 __u8 ipproto,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000759 void (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
760 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900761 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000764 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Eric Dumazet2922bc82009-10-23 06:34:34 +0000766 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700768 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700769 &ipv6h->daddr)) != NULL) {
Eric Dumazet8560f222010-09-28 03:23:34 +0000770 struct pcpu_tstats *tstats;
771
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900772 if (t->parms.proto != ipproto && t->parms.proto != 0) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000773 rcu_read_unlock();
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900774 goto discard;
775 }
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000778 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700779 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000782 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700783 t->dev->stats.rx_dropped++;
Eric Dumazet2922bc82009-10-23 06:34:34 +0000784 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto discard;
786 }
787 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700788 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700789 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900790 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 skb->pkt_type = PACKET_HOST;
792 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700793
Eric Dumazet8560f222010-09-28 03:23:34 +0000794 tstats = this_cpu_ptr(t->dev->tstats);
795 tstats->rx_packets++;
796 tstats->rx_bytes += skb->len;
797
798 __skb_tunnel_rx(skb, t->dev);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900799
800 dscp_ecn_decapsulate(t, ipv6h, skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000801
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000802 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000803
Eric Dumazet2922bc82009-10-23 06:34:34 +0000804 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000807 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700809
810discard:
811 kfree_skb(skb);
812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900815static int ip4ip6_rcv(struct sk_buff *skb)
816{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900817 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
818 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900819}
820
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900821static int ip6ip6_rcv(struct sk_buff *skb)
822{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900823 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
824 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900825}
826
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800827struct ipv6_tel_txoption {
828 struct ipv6_txoptions ops;
829 __u8 dst_opt[8];
830};
831
832static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800834 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800836 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
837 opt->dst_opt[3] = 1;
838 opt->dst_opt[4] = encap_limit;
839 opt->dst_opt[5] = IPV6_TLV_PADN;
840 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800842 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
843 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900847 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900849 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 *
851 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900852 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * doesn't match source of incoming packet.
854 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900855 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * 1 if conflict,
857 * 0 else
858 **/
859
Eric Dumazet92113bf2012-05-18 08:14:11 +0200860static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000861ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
864}
865
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000866int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800867{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000868 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800869 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700870 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800871
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900872 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800873 struct net_device *ldev = NULL;
874
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100875 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800876 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100877 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800878
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700879 if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000880 pr_warn("%s xmit: Local address not yet configured!\n",
881 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800882 else if (!ipv6_addr_is_multicast(&p->raddr) &&
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700883 unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000884 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
885 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800886 else
887 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100888 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800889 }
890 return ret;
891}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000892EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900895 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900897 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900898 * @dsfield: dscp code for outer header
899 * @fl: flow of tunneled packet
900 * @encap_limit: encapsulation limit
901 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 *
903 * Description:
904 * Build new header and do some sanity checks on the packet before sending
905 * it.
906 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900907 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900908 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900909 * -1 fail
910 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 **/
912
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900913static int ip6_tnl_xmit2(struct sk_buff *skb,
914 struct net_device *dev,
915 __u8 dsfield,
David S. Miller4c9483b2011-03-12 16:22:43 -0500916 struct flowi6 *fl6,
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900917 int encap_limit,
918 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800920 struct net *net = dev_net(dev);
Patrick McHardy2941a482006-01-08 22:05:26 -0800921 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700922 struct net_device_stats *stats = &t->dev->stats;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700923 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800924 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400925 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct net_device *tdev;
927 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -0700928 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900930 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400933 if (!fl6->flowi6_mark)
934 dst = ip6_tnl_dst_check(t);
Eric Dumazet89b02122011-07-28 04:32:25 +0000935 if (!dst) {
936 ndst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Eric Dumazet89b02122011-07-28 04:32:25 +0000938 if (ndst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700939 goto tx_err_link_failure;
Eric Dumazet89b02122011-07-28 04:32:25 +0000940 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
941 if (IS_ERR(ndst)) {
942 err = PTR_ERR(ndst);
943 ndst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -0800944 goto tx_err_link_failure;
945 }
Eric Dumazet89b02122011-07-28 04:32:25 +0000946 dst = ndst;
Patrick McHardya57ebc92005-09-08 14:27:47 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 tdev = dst->dev;
950
951 if (tdev == dev) {
952 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +0000953 net_warn_ratelimited("%s: Local routing loop detected!\n",
954 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto tx_err_dst_release;
956 }
957 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800958 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 max_headroom += 8;
960 mtu -= 8;
961 }
962 if (mtu < IPV6_MIN_MTU)
963 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +0000964 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -0700965 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900967 *pmtu = mtu;
968 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 goto tx_err_dst_release;
970 }
971
972 /*
973 * Okay, now see if we can stuff it in the buffer as-is.
974 */
975 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900976
Patrick McHardycfbba492007-07-09 15:33:40 -0700977 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
978 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
982 goto tx_err_dst_release;
983
984 if (skb->sk)
985 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +0000986 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 skb = new_skb;
988 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000989 skb_dst_drop(skb);
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400990 if (fl6->flowi6_mark) {
991 skb_dst_set(skb, dst);
992 ndst = NULL;
993 } else {
994 skb_dst_set_noref(skb, dst);
995 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700996 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
David S. Miller4c9483b2011-03-12 16:22:43 -0500998 proto = fl6->flowi6_proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800999 if (encap_limit >= 0) {
1000 init_tel_txopt(&opt, encap_limit);
1001 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1002 }
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001003 skb_push(skb, sizeof(struct ipv6hdr));
1004 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001005 ipv6h = ipv6_hdr(skb);
David S. Miller4c9483b2011-03-12 16:22:43 -05001006 *(__be32*)ipv6h = fl6->flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dsfield = INET_ECN_encapsulate(0, dsfield);
1008 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 ipv6h->hop_limit = t->parms.hop_limit;
1010 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001011 ipv6h->saddr = fl6->saddr;
1012 ipv6h->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 nf_reset(skb);
1014 pkt_len = skb->len;
Herbert Xuef76bc22008-01-11 19:15:08 -08001015 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -02001017 if (net_xmit_eval(err) == 0) {
Eric Dumazet8560f222010-09-28 03:23:34 +00001018 struct pcpu_tstats *tstats = this_cpu_ptr(t->dev->tstats);
1019
1020 tstats->tx_bytes += pkt_len;
1021 tstats->tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
1023 stats->tx_errors++;
1024 stats->tx_aborted_errors++;
1025 }
Eric Dumazet89b02122011-07-28 04:32:25 +00001026 if (ndst)
1027 ip6_tnl_dst_store(t, ndst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029tx_err_link_failure:
1030 stats->tx_carrier_errors++;
1031 dst_link_failure(skb);
1032tx_err_dst_release:
Eric Dumazet89b02122011-07-28 04:32:25 +00001033 dst_release(ndst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001034 return err;
1035}
1036
1037static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001038ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1039{
1040 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001041 const struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001042 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001043 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001044 __u8 dsfield;
1045 __u32 mtu;
1046 int err;
1047
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001048 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
1049 !ip6_tnl_xmit_ctl(t))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001050 return -1;
1051
1052 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1053 encap_limit = t->parms.encap_limit;
1054
David S. Miller4c9483b2011-03-12 16:22:43 -05001055 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1056 fl6.flowi6_proto = IPPROTO_IPIP;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001057
1058 dsfield = ipv4_get_dsfield(iph);
1059
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001060 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001061 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
Al Virob77f2fa2007-07-21 19:09:41 -07001062 & IPV6_TCLASS_MASK;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001063 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1064 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001065
David S. Miller4c9483b2011-03-12 16:22:43 -05001066 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001067 if (err != 0) {
1068 /* XXX: send ICMP error even if DF is not set. */
1069 if (err == -EMSGSIZE)
1070 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1071 htonl(mtu));
1072 return -1;
1073 }
1074
1075 return 0;
1076}
1077
1078static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001079ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1080{
1081 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001082 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001083 int encap_limit = -1;
1084 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001085 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001086 __u8 dsfield;
1087 __u32 mtu;
1088 int err;
1089
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001090 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1091 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001092 return -1;
1093
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001094 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001095 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001096 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001097 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001098 if (tel->encap_limit == 0) {
1099 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001100 ICMPV6_HDR_FIELD, offset + 2);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001101 return -1;
1102 }
1103 encap_limit = tel->encap_limit - 1;
1104 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1105 encap_limit = t->parms.encap_limit;
1106
David S. Miller4c9483b2011-03-12 16:22:43 -05001107 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1108 fl6.flowi6_proto = IPPROTO_IPV6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001109
1110 dsfield = ipv6_get_dsfield(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001111 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001112 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001113 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
David S. Miller4c9483b2011-03-12 16:22:43 -05001114 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001115 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1116 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001117
David S. Miller4c9483b2011-03-12 16:22:43 -05001118 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001119 if (err != 0) {
1120 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001121 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001122 return -1;
1123 }
1124
1125 return 0;
1126}
1127
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001128static netdev_tx_t
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001129ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1130{
1131 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001132 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001133 int ret;
1134
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001135 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001136 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001137 ret = ip4ip6_tnl_xmit(skb, dev);
1138 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001139 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001140 ret = ip6ip6_tnl_xmit(skb, dev);
1141 break;
1142 default:
1143 goto tx_err;
1144 }
1145
1146 if (ret < 0)
1147 goto tx_err;
1148
Patrick McHardy6ed10652009-06-23 06:03:08 +00001149 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151tx_err:
1152 stats->tx_errors++;
1153 stats->tx_dropped++;
1154 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001155 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001158static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001161 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001162 struct flowi6 *fl6 = &t->fl.u.ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001164 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1165 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001168 fl6->saddr = p->laddr;
1169 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001170 fl6->flowi6_oif = p->link;
1171 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001174 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001176 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001178 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1179 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1182 dev->flags |= IFF_POINTOPOINT;
1183 else
1184 dev->flags &= ~IFF_POINTOPOINT;
1185
1186 dev->iflink = p->link;
1187
1188 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001189 int strict = (ipv6_addr_type(&p->raddr) &
1190 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1191
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001192 struct rt6_info *rt = rt6_lookup(dev_net(dev),
1193 &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001194 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (rt == NULL)
1197 return;
1198
David S. Millerd1918542011-12-28 20:19:20 -05001199 if (rt->dst.dev) {
1200 dev->hard_header_len = rt->dst.dev->hard_header_len +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 sizeof (struct ipv6hdr);
1202
David S. Millerd1918542011-12-28 20:19:20 -05001203 dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001204 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1205 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (dev->mtu < IPV6_MIN_MTU)
1208 dev->mtu = IPV6_MIN_MTU;
1209 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001210 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212}
1213
1214/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001215 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * @t: tunnel to be changed
1217 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 *
1219 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001220 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 **/
1222
1223static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001224ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001226 t->parms.laddr = p->laddr;
1227 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 t->parms.flags = p->flags;
1229 t->parms.hop_limit = p->hop_limit;
1230 t->parms.encap_limit = p->encap_limit;
1231 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001232 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001233 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001234 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001235 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return 0;
1237}
1238
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001239static void
1240ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1241{
1242 p->laddr = u->laddr;
1243 p->raddr = u->raddr;
1244 p->flags = u->flags;
1245 p->hop_limit = u->hop_limit;
1246 p->encap_limit = u->encap_limit;
1247 p->flowinfo = u->flowinfo;
1248 p->link = u->link;
1249 p->proto = u->proto;
1250 memcpy(p->name, u->name, sizeof(u->name));
1251}
1252
1253static void
1254ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1255{
1256 u->laddr = p->laddr;
1257 u->raddr = p->raddr;
1258 u->flags = p->flags;
1259 u->hop_limit = p->hop_limit;
1260 u->encap_limit = p->encap_limit;
1261 u->flowinfo = p->flowinfo;
1262 u->link = p->link;
1263 u->proto = p->proto;
1264 memcpy(u->name, p->name, sizeof(u->name));
1265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001268 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * @dev: virtual device associated with tunnel
1270 * @ifr: parameters passed from userspace
1271 * @cmd: command to be performed
1272 *
1273 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001274 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001275 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
1277 * The possible commands are the following:
1278 * %SIOCGETTUNNEL: get tunnel parameters for device
1279 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1280 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1281 * %SIOCDELTUNNEL: delete tunnel
1282 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001283 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 * initialization, can be used for creating other tunnel devices.
1285 *
1286 * Return:
1287 * 0 on success,
1288 * %-EFAULT if unable to copy data to or from userspace,
1289 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1290 * %-EINVAL if passed tunnel parameters are invalid,
1291 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1292 * %-ENODEV if attempting to change or delete a nonexisting device
1293 **/
1294
1295static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001296ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001300 struct __ip6_tnl_parm p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct ip6_tnl *t = NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001302 struct net *net = dev_net(dev);
1303 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 switch (cmd) {
1306 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001307 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001308 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 err = -EFAULT;
1310 break;
1311 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001312 ip6_tnl_parm_from_user(&p1, &p);
1313 t = ip6_tnl_locate(net, &p1, 0);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001314 } else {
1315 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001316 }
1317 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001318 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001319 ip6_tnl_parm_to_user(&p, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1321 err = -EFAULT;
1322 }
1323 break;
1324 case SIOCADDTUNNEL:
1325 case SIOCCHGTUNNEL:
1326 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (!capable(CAP_NET_ADMIN))
1328 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001329 err = -EFAULT;
1330 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001332 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001333 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1334 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001336 ip6_tnl_parm_from_user(&p1, &p);
1337 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001338 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001339 if (t != NULL) {
1340 if (t->dev != dev) {
1341 err = -EEXIST;
1342 break;
1343 }
1344 } else
1345 t = netdev_priv(dev);
1346
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001347 ip6_tnl_unlink(ip6n, t);
Pavel Emelyanov74b0b852010-10-27 05:43:53 +00001348 synchronize_net();
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001349 err = ip6_tnl_change(t, &p1);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001350 ip6_tnl_link(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 netdev_state_change(dev);
1352 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001353 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001355 ip6_tnl_parm_to_user(&p, &t->parms);
1356 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001357 err = -EFAULT;
1358
1359 } else
1360 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 break;
1362 case SIOCDELTUNNEL:
1363 err = -EPERM;
1364 if (!capable(CAP_NET_ADMIN))
1365 break;
1366
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001367 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001368 err = -EFAULT;
1369 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001371 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001372 ip6_tnl_parm_from_user(&p1, &p);
1373 t = ip6_tnl_locate(net, &p1, 0);
1374 if (t == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001376 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001377 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001379 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001381 err = 0;
1382 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 break;
1384 default:
1385 err = -EINVAL;
1386 }
1387 return err;
1388}
1389
1390/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001391 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * @dev: virtual device associated with tunnel
1393 * @new_mtu: the new mtu
1394 *
1395 * Return:
1396 * 0 on success,
1397 * %-EINVAL if mtu too small
1398 **/
1399
1400static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001401ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 if (new_mtu < IPV6_MIN_MTU) {
1404 return -EINVAL;
1405 }
1406 dev->mtu = new_mtu;
1407 return 0;
1408}
1409
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001410
1411static const struct net_device_ops ip6_tnl_netdev_ops = {
Eric Dumazet8560f222010-09-28 03:23:34 +00001412 .ndo_uninit = ip6_tnl_dev_uninit,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001413 .ndo_start_xmit = ip6_tnl_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001414 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001415 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001416 .ndo_get_stats = ip6_get_stats,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001417};
1418
Eric Dumazet8560f222010-09-28 03:23:34 +00001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001421 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * @dev: virtual device associated with tunnel
1423 *
1424 * Description:
1425 * Initialize function pointers and device parameters
1426 **/
1427
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001428static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Anders Franzen381601e2010-11-24 05:47:18 +00001430 struct ip6_tnl *t;
1431
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001432 dev->netdev_ops = &ip6_tnl_netdev_ops;
Eric Dumazet8560f222010-09-28 03:23:34 +00001433 dev->destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 dev->type = ARPHRD_TUNNEL6;
1436 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1437 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001438 t = netdev_priv(dev);
1439 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1440 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 dev->flags |= IFF_NOARP;
1442 dev->addr_len = sizeof(struct in6_addr);
Pavel Emelyanov554eb272008-04-16 01:24:13 -07001443 dev->features |= NETIF_F_NETNS_LOCAL;
Anders Franzen7e223de2010-10-19 03:50:47 +00001444 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447
1448/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001449 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 * @dev: virtual device associated with tunnel
1451 **/
1452
Eric Dumazet8560f222010-09-28 03:23:34 +00001453static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001454ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Patrick McHardy2941a482006-01-08 22:05:26 -08001456 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 t->dev = dev;
Eric Dumazet8560f222010-09-28 03:23:34 +00001459 dev->tstats = alloc_percpu(struct pcpu_tstats);
1460 if (!dev->tstats)
1461 return -ENOMEM;
1462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
1465/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001466 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * @dev: virtual device associated with tunnel
1468 **/
1469
Eric Dumazet8560f222010-09-28 03:23:34 +00001470static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Patrick McHardy2941a482006-01-08 22:05:26 -08001472 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001473 int err = ip6_tnl_dev_init_gen(dev);
1474
1475 if (err)
1476 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001477 ip6_tnl_link_config(t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001482 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * @dev: fallback device
1484 *
1485 * Return: 0
1486 **/
1487
Eric Dumazet8560f222010-09-28 03:23:34 +00001488static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Patrick McHardy2941a482006-01-08 22:05:26 -08001490 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001491 struct net *net = dev_net(dev);
1492 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001493 int err = ip6_tnl_dev_init_gen(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001494
Eric Dumazet8560f222010-09-28 03:23:34 +00001495 if (err)
1496 return err;
1497
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001498 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001500
1501 ip6_tnl_link_config(t);
1502
Eric Dumazetcf778b02012-01-12 04:41:32 +00001503 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001507static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001508 .handler = ip4ip6_rcv,
1509 .err_handler = ip4ip6_err,
1510 .priority = 1,
1511};
1512
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001513static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07001514 .handler = ip6ip6_rcv,
1515 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001516 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517};
1518
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001519static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001520{
1521 int h;
1522 struct ip6_tnl *t;
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001523 LIST_HEAD(list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001524
1525 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00001526 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001527 while (t != NULL) {
1528 unregister_netdevice_queue(t->dev, &list);
Eric Dumazet94767632010-09-15 20:25:34 +00001529 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001530 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001531 }
1532
Eric Dumazet94767632010-09-15 20:25:34 +00001533 t = rtnl_dereference(ip6n->tnls_wc[0]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001534 unregister_netdevice_queue(t->dev, &list);
1535 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001536}
1537
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001538static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001539{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001540 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00001541 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001542 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001543
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001544 ip6n->tnls[0] = ip6n->tnls_wc;
1545 ip6n->tnls[1] = ip6n->tnls_r_l;
1546
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001547 err = -ENOMEM;
1548 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1549 ip6_tnl_dev_setup);
1550
1551 if (!ip6n->fb_tnl_dev)
1552 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001553 dev_net_set(ip6n->fb_tnl_dev, net);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001554
Eric Dumazet8560f222010-09-28 03:23:34 +00001555 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1556 if (err < 0)
1557 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001558
1559 err = register_netdev(ip6n->fb_tnl_dev);
1560 if (err < 0)
1561 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00001562
1563 t = netdev_priv(ip6n->fb_tnl_dev);
1564
1565 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001566 return 0;
1567
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001568err_register:
Eric Dumazet8560f222010-09-28 03:23:34 +00001569 ip6_dev_free(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001570err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001571 return err;
1572}
1573
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001574static void __net_exit ip6_tnl_exit_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001575{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001576 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001577
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001578 rtnl_lock();
1579 ip6_tnl_destroy_tunnels(ip6n);
1580 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001581}
1582
1583static struct pernet_operations ip6_tnl_net_ops = {
1584 .init = ip6_tnl_init_net,
1585 .exit = ip6_tnl_exit_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001586 .id = &ip6_tnl_net_id,
1587 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001588};
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590/**
1591 * ip6_tunnel_init - register protocol and reserve needed resources
1592 *
1593 * Return: 0 on success
1594 **/
1595
1596static int __init ip6_tunnel_init(void)
1597{
1598 int err;
1599
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001600 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001601 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001602 goto out_pernet;
1603
1604 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1605 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001606 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001607 goto out_ip4ip6;
1608 }
1609
1610 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1611 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001612 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001613 goto out_ip6ip6;
1614 }
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001617
1618out_ip6ip6:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001619 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001620out_ip4ip6:
1621 unregister_pernet_device(&ip6_tnl_net_ops);
1622out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return err;
1624}
1625
1626/**
1627 * ip6_tunnel_cleanup - free resources and unregister protocol
1628 **/
1629
1630static void __exit ip6_tunnel_cleanup(void)
1631{
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001632 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00001633 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001634
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001635 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00001636 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001638 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641module_init(ip6_tunnel_init);
1642module_exit(ip6_tunnel_cleanup);