blob: 424ed45ef122b9f5180c9ea33a5e8b792eb294d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Yasuyuki Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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);
Nicolas Dichtelc075b132012-11-09 06:10:01 +000086static struct rtnl_link_ops ip6_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Eric Dumazetf99189b2009-11-17 10:42:49 +000088static int ip6_tnl_net_id __read_mostly;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070089struct ip6_tnl_net {
Pavel Emelyanov15820e1292008-04-16 01:23:02 -070090 /* the IPv6 tunnel fallback device */
91 struct net_device *fb_tnl_dev;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -070092 /* lists for storing tunnels in use */
Eric Dumazet94767632010-09-15 20:25:34 +000093 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
94 struct ip6_tnl __rcu *tnls_wc[1];
95 struct ip6_tnl __rcu **tnls[2];
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070096};
97
Eric Dumazet8560f222010-09-28 03:23:34 +000098/* often modified stats are per cpu, other are shared (netdev->stats) */
99struct pcpu_tstats {
100 unsigned long rx_packets;
101 unsigned long rx_bytes;
102 unsigned long tx_packets;
103 unsigned long tx_bytes;
Eric Dumazet8ce120f2011-11-04 23:19:28 +0000104} __attribute__((aligned(4*sizeof(unsigned long))));
Eric Dumazet8560f222010-09-28 03:23:34 +0000105
106static struct net_device_stats *ip6_get_stats(struct net_device *dev)
107{
108 struct pcpu_tstats sum = { 0 };
109 int i;
110
111 for_each_possible_cpu(i) {
112 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
113
114 sum.rx_packets += tstats->rx_packets;
115 sum.rx_bytes += tstats->rx_bytes;
116 sum.tx_packets += tstats->tx_packets;
117 sum.tx_bytes += tstats->tx_bytes;
118 }
119 dev->stats.rx_packets = sum.rx_packets;
120 dev->stats.rx_bytes = sum.rx_bytes;
121 dev->stats.tx_packets = sum.tx_packets;
122 dev->stats.tx_bytes = sum.tx_bytes;
123 return &dev->stats;
124}
125
Eric Dumazet2922bc82009-10-23 06:34:34 +0000126/*
Eric Dumazet94767632010-09-15 20:25:34 +0000127 * Locking : hash tables are protected by RCU and RTNL
Eric Dumazet2922bc82009-10-23 06:34:34 +0000128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000130struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct dst_entry *dst = t->dst_cache;
133
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900134 if (dst && dst->obsolete &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 dst->ops->check(dst, t->dst_cookie) == NULL) {
136 t->dst_cache = NULL;
137 dst_release(dst);
138 return NULL;
139 }
140
141 return dst;
142}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000143EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000145void ip6_tnl_dst_reset(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 dst_release(t->dst_cache);
148 t->dst_cache = NULL;
149}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000150EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000152void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct rt6_info *rt = (struct rt6_info *) dst;
155 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
156 dst_release(t->dst_cache);
157 t->dst_cache = dst;
158}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000159EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900162 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900163 * @remote: the address of the tunnel exit-point
164 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900166 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900168 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * else %NULL
170 **/
171
Eric Dumazet2922bc82009-10-23 06:34:34 +0000172#define for_each_ip6_tunnel_rcu(start) \
173 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static struct ip6_tnl *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000176ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000178 unsigned int hash = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct ip6_tnl *t;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700180 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Eric Dumazetddbe5032012-07-18 08:11:12 +0000182 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (ipv6_addr_equal(local, &t->parms.laddr) &&
184 ipv6_addr_equal(remote, &t->parms.raddr) &&
185 (t->dev->flags & IFF_UP))
186 return t;
187 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000188 t = rcu_dereference(ip6n->tnls_wc[0]);
189 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return t;
191
192 return NULL;
193}
194
195/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900196 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900197 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900200 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * &struct in6_addr entries laddr and raddr in @p.
202 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900203 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 **/
205
Eric Dumazet94767632010-09-15 20:25:34 +0000206static struct ip6_tnl __rcu **
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000207ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000209 const struct in6_addr *remote = &p->raddr;
210 const struct in6_addr *local = &p->laddr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000211 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int prio = 0;
213
214 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
215 prio = 1;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000216 h = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700218 return &ip6n->tnls[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900222 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * @t: tunnel to be added
224 **/
225
226static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700227ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Eric Dumazet94767632010-09-15 20:25:34 +0000229 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Eric Dumazetcf778b02012-01-12 04:41:32 +0000231 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
232 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900236 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * @t: tunnel to be removed
238 **/
239
240static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700241ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Eric Dumazet94767632010-09-15 20:25:34 +0000243 struct ip6_tnl __rcu **tp;
244 struct ip6_tnl *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Eric Dumazet94767632010-09-15 20:25:34 +0000246 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
247 (iter = rtnl_dereference(*tp)) != NULL;
248 tp = &iter->next) {
249 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000250 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 }
253 }
254}
255
Eric Dumazet8560f222010-09-28 03:23:34 +0000256static void ip6_dev_free(struct net_device *dev)
257{
258 free_percpu(dev->tstats);
259 free_netdev(dev);
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000263 * ip6_tnl_create - create a new tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * @p: tunnel parameters
265 * @pt: pointer to new tunnel
266 *
267 * Description:
268 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900269 *
270 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800271 * created tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 **/
273
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000274static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct net_device *dev;
277 struct ip6_tnl *t;
278 char name[IFNAMSIZ];
279 int err;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700280 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Pavel Emelyanov34cc7ba62008-02-23 20:19:20 -0800282 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba62008-02-23 20:19:20 -0800284 else
285 sprintf(name, "ip6tnl%%d");
286
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900287 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800289 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Pavel Emelyanov554eb272008-04-16 01:24:13 -0700291 dev_net_set(dev, net);
292
Patrick McHardy2941a482006-01-08 22:05:26 -0800293 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 t->parms = *p;
Eric Dumazet8560f222010-09-28 03:23:34 +0000295 err = ip6_tnl_dev_init(dev);
296 if (err < 0)
297 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800299 if ((err = register_netdevice(dev)) < 0)
300 goto failed_free;
301
Josh Boyer731abb92011-11-10 15:10:23 +0000302 strcpy(t->parms.name, dev->name);
Nicolas Dichtelc075b132012-11-09 06:10:01 +0000303 dev->rtnl_link_ops = &ip6_link_ops;
Josh Boyer731abb92011-11-10 15:10:23 +0000304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 dev_hold(dev);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700306 ip6_tnl_link(ip6n, t);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800307 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800308
309failed_free:
Eric Dumazet8560f222010-09-28 03:23:34 +0000310 ip6_dev_free(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800311failed:
312 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900316 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900317 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * @create: != 0 if allowed to create new tunnel if no match found
319 *
320 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900321 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * based on @parms. If this is unsuccessful, but @create is set a new
323 * tunnel device is created and registered for use.
324 *
325 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800326 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 **/
328
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700329static struct ip6_tnl *ip6_tnl_locate(struct net *net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000330 struct __ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000332 const struct in6_addr *remote = &p->raddr;
333 const struct in6_addr *local = &p->laddr;
Eric Dumazet94767632010-09-15 20:25:34 +0000334 struct ip6_tnl __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700336 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Eric Dumazet94767632010-09-15 20:25:34 +0000338 for (tp = ip6_tnl_bucket(ip6n, p);
339 (t = rtnl_dereference(*tp)) != NULL;
340 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Ville Nuorvala567131a2006-11-24 17:05:41 -0800342 ipv6_addr_equal(remote, &t->parms.raddr))
343 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800346 return NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700347 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900351 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900355 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 **/
357
358static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900359ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Patrick McHardy2941a482006-01-08 22:05:26 -0800361 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700362 struct net *net = dev_net(dev);
363 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Eric Dumazet94767632010-09-15 20:25:34 +0000365 if (dev == ip6n->fb_tnl_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000366 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
Eric Dumazet94767632010-09-15 20:25:34 +0000367 else
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700368 ip6_tnl_unlink(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ip6_tnl_dst_reset(t);
370 dev_put(dev);
371}
372
373/**
374 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
375 * @skb: received socket buffer
376 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900377 * Return:
378 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * else index to encapsulation limit
380 **/
381
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000382__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000384 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 __u8 nexthdr = ipv6h->nexthdr;
386 __u16 off = sizeof (*ipv6h);
387
388 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
389 __u16 optlen = 0;
390 struct ipv6_opt_hdr *hdr;
391 if (raw + off + sizeof (*hdr) > skb->data &&
392 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
393 break;
394
395 hdr = (struct ipv6_opt_hdr *) (raw + off);
396 if (nexthdr == NEXTHDR_FRAGMENT) {
397 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
398 if (frag_hdr->frag_off)
399 break;
400 optlen = 8;
401 } else if (nexthdr == NEXTHDR_AUTH) {
402 optlen = (hdr->hdrlen + 2) << 2;
403 } else {
404 optlen = ipv6_optlen(hdr);
405 }
406 if (nexthdr == NEXTHDR_DEST) {
407 __u16 i = off + 2;
408 while (1) {
409 struct ipv6_tlv_tnl_enc_lim *tel;
410
411 /* No more room for encapsulation limit */
412 if (i + sizeof (*tel) > off + optlen)
413 break;
414
415 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
416 /* return index of option if found and valid */
417 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
418 tel->length == 1)
419 return i;
420 /* else jump to next option */
421 if (tel->type)
422 i += tel->length + 2;
423 else
424 i++;
425 }
426 }
427 nexthdr = hdr->nexthdr;
428 off += optlen;
429 }
430 return 0;
431}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000432EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900435 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
437 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900438 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * to the specifications in RFC 2473.
440 **/
441
Herbert Xud2acc342006-03-28 01:12:13 -0800442static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900443ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700444 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000446 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct ip6_tnl *t;
448 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700449 u8 rel_type = ICMPV6_DEST_UNREACH;
450 u8 rel_code = ICMPV6_ADDR_UNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 __u32 rel_info = 0;
452 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800453 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900455 /* If the packet doesn't contain the original IPv6 header we are
456 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 processing of the error. */
458
Eric Dumazet2922bc82009-10-23 06:34:34 +0000459 rcu_read_lock();
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700460 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700461 &ipv6h->saddr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out;
463
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900464 if (t->parms.proto != ipproto && t->parms.proto != 0)
465 goto out;
466
Herbert Xud2acc342006-03-28 01:12:13 -0800467 err = 0;
468
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900469 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 __u32 teli;
471 struct ipv6_tlv_tnl_enc_lim *tel;
472 __u32 mtu;
473 case ICMPV6_DEST_UNREACH:
Joe Perchese87cc472012-05-13 21:56:26 +0000474 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
475 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 rel_msg = 1;
477 break;
478 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900479 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Joe Perchese87cc472012-05-13 21:56:26 +0000480 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
481 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 rel_msg = 1;
483 }
484 break;
485 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800486 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900487 if ((*code) == ICMPV6_HDR_FIELD)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000488 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Al Viro704eae12007-07-26 17:33:29 +0100490 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
492 if (tel->encap_limit == 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000493 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
494 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 rel_msg = 1;
496 }
Joe Perchese87cc472012-05-13 21:56:26 +0000497 } else {
498 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
499 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 break;
502 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100503 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (mtu < IPV6_MIN_MTU)
505 mtu = IPV6_MIN_MTU;
506 t->dev->mtu = mtu;
507
Al Virocc6cdac2006-02-18 16:02:18 -0500508 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 rel_type = ICMPV6_PKT_TOOBIG;
510 rel_code = 0;
511 rel_info = mtu;
512 rel_msg = 1;
513 }
514 break;
515 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900516
517 *type = rel_type;
518 *code = rel_code;
519 *info = rel_info;
520 *msg = rel_msg;
521
522out:
Eric Dumazet2922bc82009-10-23 06:34:34 +0000523 rcu_read_unlock();
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900524 return err;
525}
526
527static int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900528ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700529 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900530{
531 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700532 u8 rel_type = type;
533 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100534 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900535 int err;
536 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000537 const struct iphdr *eiph;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900538 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -0700539 struct flowi4 fl4;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900540
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900541 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
542 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900543 if (err < 0)
544 return err;
545
546 if (rel_msg == 0)
547 return 0;
548
549 switch (rel_type) {
550 case ICMPV6_DEST_UNREACH:
551 if (rel_code != ICMPV6_ADDR_UNREACH)
552 return 0;
553 rel_type = ICMP_DEST_UNREACH;
554 rel_code = ICMP_HOST_UNREACH;
555 break;
556 case ICMPV6_PKT_TOOBIG:
557 if (rel_code != 0)
558 return 0;
559 rel_type = ICMP_DEST_UNREACH;
560 rel_code = ICMP_FRAG_NEEDED;
561 break;
David S. Millerec18d9a2012-07-12 00:25:15 -0700562 case NDISC_REDIRECT:
563 rel_type = ICMP_REDIRECT;
564 rel_code = ICMP_REDIR_HOST;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900565 default:
566 return 0;
567 }
568
569 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
570 return 0;
571
572 skb2 = skb_clone(skb, GFP_ATOMIC);
573 if (!skb2)
574 return 0;
575
Eric Dumazetadf30902009-06-02 05:19:30 +0000576 skb_dst_drop(skb2);
577
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900578 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700579 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700580 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900581
582 /* Try to guess incoming interface */
David S. Miller31e45432011-05-03 20:25:42 -0700583 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500584 eiph->saddr, 0,
585 0, 0,
586 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800587 if (IS_ERR(rt))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900588 goto out;
589
Changli Gaod8d1f302010-06-10 23:31:35 -0700590 skb2->dev = rt->dst.dev;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900591
592 /* route "incoming" packet */
593 if (rt->rt_flags & RTCF_LOCAL) {
594 ip_rt_put(rt);
595 rt = NULL;
David S. Miller31e45432011-05-03 20:25:42 -0700596 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500597 eiph->daddr, eiph->saddr,
598 0, 0,
599 IPPROTO_IPIP,
600 RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800601 if (IS_ERR(rt) ||
Changli Gaod8d1f302010-06-10 23:31:35 -0700602 rt->dst.dev->type != ARPHRD_TUNNEL) {
David S. Millerb23dd4f2011-03-02 14:31:35 -0800603 if (!IS_ERR(rt))
604 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900605 goto out;
606 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800607 skb_dst_set(skb2, &rt->dst);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900608 } else {
609 ip_rt_put(rt);
610 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
611 skb2->dev) ||
Eric Dumazetadf30902009-06-02 05:19:30 +0000612 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900613 goto out;
614 }
615
616 /* change mtu on this route */
617 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000618 if (rel_info > dst_mtu(skb_dst(skb2)))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900619 goto out;
620
David S. Miller6700c272012-07-17 03:29:28 -0700621 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900622 }
David S. Miller1ed5c482012-07-12 00:41:25 -0700623 if (rel_type == ICMP_REDIRECT)
David S. Miller6700c272012-07-17 03:29:28 -0700624 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900625
Al Viro704eae12007-07-26 17:33:29 +0100626 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900627
628out:
629 kfree_skb(skb2);
630 return 0;
631}
632
633static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900634ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700635 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900636{
637 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700638 u8 rel_type = type;
639 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100640 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900641 int err;
642
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900643 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
644 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900645 if (err < 0)
646 return err;
647
648 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct rt6_info *rt;
650 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Eric Dumazetadf30902009-06-02 05:19:30 +0000655 skb_dst_drop(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700657 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* Try to guess incoming interface */
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700660 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
661 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
David S. Millerd1918542011-12-28 20:19:20 -0500663 if (rt && rt->dst.dev)
664 skb2->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000666 icmpv6_send(skb2, rel_type, rel_code, rel_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Amerigo Wang94e187c2012-10-29 00:13:19 +0000668 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 kfree_skb(skb2);
671 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900672
673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000676static void ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
677 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900678 struct sk_buff *skb)
679{
680 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
681
682 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700683 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900684
685 if (INET_ECN_is_ce(dsfield))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700686 IP_ECN_set_ce(ip_hdr(skb));
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900687}
688
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000689static void ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
690 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900691 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900693 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800694 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900696 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700697 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900699
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000700__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000701 const struct in6_addr *laddr,
702 const struct in6_addr *raddr)
703{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000704 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000705 int ltype = ipv6_addr_type(laddr);
706 int rtype = ipv6_addr_type(raddr);
707 __u32 flags = 0;
708
709 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
710 flags = IP6_TNL_F_CAP_PER_PACKET;
711 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
712 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
713 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
714 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
715 if (ltype&IPV6_ADDR_UNICAST)
716 flags |= IP6_TNL_F_CAP_XMIT;
717 if (rtype&IPV6_ADDR_UNICAST)
718 flags |= IP6_TNL_F_CAP_RCV;
719 }
720 return flags;
721}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000722EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000723
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100724/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000725int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000726 const struct in6_addr *laddr,
727 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800728{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000729 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800730 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700731 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800732
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000733 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
734 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
735 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900736 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800737
738 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100739 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800740
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000741 if ((ipv6_addr_is_multicast(laddr) ||
742 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
743 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800744 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800745 }
746 return ret;
747}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000748EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900751 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900753 * @protocol: ethernet protocol ID
754 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *
756 * Return: 0
757 **/
758
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900759static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900760 __u8 ipproto,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000761 void (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
762 const struct ipv6hdr *ipv6h,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900763 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000766 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Eric Dumazet2922bc82009-10-23 06:34:34 +0000768 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700770 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700771 &ipv6h->daddr)) != NULL) {
Eric Dumazet8560f222010-09-28 03:23:34 +0000772 struct pcpu_tstats *tstats;
773
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900774 if (t->parms.proto != ipproto && t->parms.proto != 0) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000775 rcu_read_unlock();
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900776 goto discard;
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000780 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700781 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000784 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700785 t->dev->stats.rx_dropped++;
Eric Dumazet2922bc82009-10-23 06:34:34 +0000786 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto discard;
788 }
789 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700790 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700791 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900792 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 skb->pkt_type = PACKET_HOST;
794 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700795
Eric Dumazet8560f222010-09-28 03:23:34 +0000796 tstats = this_cpu_ptr(t->dev->tstats);
797 tstats->rx_packets++;
798 tstats->rx_bytes += skb->len;
799
800 __skb_tunnel_rx(skb, t->dev);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900801
802 dscp_ecn_decapsulate(t, ipv6h, skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000803
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000804 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000805
Eric Dumazet2922bc82009-10-23 06:34:34 +0000806 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000809 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700811
812discard:
813 kfree_skb(skb);
814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900817static int ip4ip6_rcv(struct sk_buff *skb)
818{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900819 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
820 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900821}
822
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900823static int ip6ip6_rcv(struct sk_buff *skb)
824{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900825 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
826 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900827}
828
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800829struct ipv6_tel_txoption {
830 struct ipv6_txoptions ops;
831 __u8 dst_opt[8];
832};
833
834static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800836 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800838 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
839 opt->dst_opt[3] = 1;
840 opt->dst_opt[4] = encap_limit;
841 opt->dst_opt[5] = IPV6_TLV_PADN;
842 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800844 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
845 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900849 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900851 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 *
853 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900854 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * doesn't match source of incoming packet.
856 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900857 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * 1 if conflict,
859 * 0 else
860 **/
861
Eric Dumazet92113bf2012-05-18 08:14:11 +0200862static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000863ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
866}
867
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000868int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800869{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000870 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800871 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700872 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800873
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900874 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800875 struct net_device *ldev = NULL;
876
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100877 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800878 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100879 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800880
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700881 if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000882 pr_warn("%s xmit: Local address not yet configured!\n",
883 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800884 else if (!ipv6_addr_is_multicast(&p->raddr) &&
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700885 unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000886 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
887 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800888 else
889 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100890 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800891 }
892 return ret;
893}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000894EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900897 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900899 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900900 * @dsfield: dscp code for outer header
901 * @fl: flow of tunneled packet
902 * @encap_limit: encapsulation limit
903 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 *
905 * Description:
906 * Build new header and do some sanity checks on the packet before sending
907 * it.
908 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900909 * Return:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900910 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900911 * -1 fail
912 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 **/
914
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900915static int ip6_tnl_xmit2(struct sk_buff *skb,
916 struct net_device *dev,
917 __u8 dsfield,
David S. Miller4c9483b2011-03-12 16:22:43 -0500918 struct flowi6 *fl6,
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900919 int encap_limit,
920 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800922 struct net *net = dev_net(dev);
Patrick McHardy2941a482006-01-08 22:05:26 -0800923 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700924 struct net_device_stats *stats = &t->dev->stats;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700925 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800926 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400927 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct net_device *tdev;
929 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -0700930 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900932 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400935 if (!fl6->flowi6_mark)
936 dst = ip6_tnl_dst_check(t);
Eric Dumazet89b02122011-07-28 04:32:25 +0000937 if (!dst) {
938 ndst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Eric Dumazet89b02122011-07-28 04:32:25 +0000940 if (ndst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700941 goto tx_err_link_failure;
Eric Dumazet89b02122011-07-28 04:32:25 +0000942 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
943 if (IS_ERR(ndst)) {
944 err = PTR_ERR(ndst);
945 ndst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -0800946 goto tx_err_link_failure;
947 }
Eric Dumazet89b02122011-07-28 04:32:25 +0000948 dst = ndst;
Patrick McHardya57ebc92005-09-08 14:27:47 -0700949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 tdev = dst->dev;
952
953 if (tdev == dev) {
954 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +0000955 net_warn_ratelimited("%s: Local routing loop detected!\n",
956 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 goto tx_err_dst_release;
958 }
959 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800960 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 max_headroom += 8;
962 mtu -= 8;
963 }
964 if (mtu < IPV6_MIN_MTU)
965 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +0000966 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -0700967 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900969 *pmtu = mtu;
970 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto tx_err_dst_release;
972 }
973
974 /*
975 * Okay, now see if we can stuff it in the buffer as-is.
976 */
977 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900978
Patrick McHardycfbba492007-07-09 15:33:40 -0700979 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
980 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
984 goto tx_err_dst_release;
985
986 if (skb->sk)
987 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +0000988 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 skb = new_skb;
990 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000991 skb_dst_drop(skb);
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400992 if (fl6->flowi6_mark) {
993 skb_dst_set(skb, dst);
994 ndst = NULL;
995 } else {
996 skb_dst_set_noref(skb, dst);
997 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700998 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
David S. Miller4c9483b2011-03-12 16:22:43 -05001000 proto = fl6->flowi6_proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001001 if (encap_limit >= 0) {
1002 init_tel_txopt(&opt, encap_limit);
1003 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1004 }
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001005 skb_push(skb, sizeof(struct ipv6hdr));
1006 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001007 ipv6h = ipv6_hdr(skb);
David S. Miller4c9483b2011-03-12 16:22:43 -05001008 *(__be32*)ipv6h = fl6->flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 dsfield = INET_ECN_encapsulate(0, dsfield);
1010 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 ipv6h->hop_limit = t->parms.hop_limit;
1012 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001013 ipv6h->saddr = fl6->saddr;
1014 ipv6h->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 nf_reset(skb);
1016 pkt_len = skb->len;
Herbert Xuef76bc22008-01-11 19:15:08 -08001017 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -02001019 if (net_xmit_eval(err) == 0) {
Eric Dumazet8560f222010-09-28 03:23:34 +00001020 struct pcpu_tstats *tstats = this_cpu_ptr(t->dev->tstats);
1021
1022 tstats->tx_bytes += pkt_len;
1023 tstats->tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 } else {
1025 stats->tx_errors++;
1026 stats->tx_aborted_errors++;
1027 }
Eric Dumazet89b02122011-07-28 04:32:25 +00001028 if (ndst)
1029 ip6_tnl_dst_store(t, ndst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 0;
1031tx_err_link_failure:
1032 stats->tx_carrier_errors++;
1033 dst_link_failure(skb);
1034tx_err_dst_release:
Eric Dumazet89b02122011-07-28 04:32:25 +00001035 dst_release(ndst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001036 return err;
1037}
1038
1039static inline int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001040ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1041{
1042 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001043 const struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001044 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001045 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001046 __u8 dsfield;
1047 __u32 mtu;
1048 int err;
1049
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001050 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
1051 !ip6_tnl_xmit_ctl(t))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001052 return -1;
1053
1054 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1055 encap_limit = t->parms.encap_limit;
1056
David S. Miller4c9483b2011-03-12 16:22:43 -05001057 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1058 fl6.flowi6_proto = IPPROTO_IPIP;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001059
1060 dsfield = ipv4_get_dsfield(iph);
1061
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001062 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001063 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
Al Virob77f2fa2007-07-21 19:09:41 -07001064 & IPV6_TCLASS_MASK;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001065 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1066 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001067
David S. Miller4c9483b2011-03-12 16:22:43 -05001068 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001069 if (err != 0) {
1070 /* XXX: send ICMP error even if DF is not set. */
1071 if (err == -EMSGSIZE)
1072 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1073 htonl(mtu));
1074 return -1;
1075 }
1076
1077 return 0;
1078}
1079
1080static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001081ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1082{
1083 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001084 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001085 int encap_limit = -1;
1086 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001087 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001088 __u8 dsfield;
1089 __u32 mtu;
1090 int err;
1091
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001092 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1093 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001094 return -1;
1095
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001096 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001097 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001098 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001099 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001100 if (tel->encap_limit == 0) {
1101 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001102 ICMPV6_HDR_FIELD, offset + 2);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001103 return -1;
1104 }
1105 encap_limit = tel->encap_limit - 1;
1106 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1107 encap_limit = t->parms.encap_limit;
1108
David S. Miller4c9483b2011-03-12 16:22:43 -05001109 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1110 fl6.flowi6_proto = IPPROTO_IPV6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001111
1112 dsfield = ipv6_get_dsfield(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001113 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001114 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001115 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
David S. Miller4c9483b2011-03-12 16:22:43 -05001116 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001117 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1118 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001119
David S. Miller4c9483b2011-03-12 16:22:43 -05001120 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001121 if (err != 0) {
1122 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001123 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001124 return -1;
1125 }
1126
1127 return 0;
1128}
1129
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001130static netdev_tx_t
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001131ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1132{
1133 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001134 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001135 int ret;
1136
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001137 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001138 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001139 ret = ip4ip6_tnl_xmit(skb, dev);
1140 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001141 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001142 ret = ip6ip6_tnl_xmit(skb, dev);
1143 break;
1144 default:
1145 goto tx_err;
1146 }
1147
1148 if (ret < 0)
1149 goto tx_err;
1150
Patrick McHardy6ed10652009-06-23 06:03:08 +00001151 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153tx_err:
1154 stats->tx_errors++;
1155 stats->tx_dropped++;
1156 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001157 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001160static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001163 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001164 struct flowi6 *fl6 = &t->fl.u.ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001166 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1167 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001170 fl6->saddr = p->laddr;
1171 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001172 fl6->flowi6_oif = p->link;
1173 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001176 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001178 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001180 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1181 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1184 dev->flags |= IFF_POINTOPOINT;
1185 else
1186 dev->flags &= ~IFF_POINTOPOINT;
1187
1188 dev->iflink = p->link;
1189
1190 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001191 int strict = (ipv6_addr_type(&p->raddr) &
1192 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1193
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001194 struct rt6_info *rt = rt6_lookup(dev_net(dev),
1195 &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001196 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (rt == NULL)
1199 return;
1200
David S. Millerd1918542011-12-28 20:19:20 -05001201 if (rt->dst.dev) {
1202 dev->hard_header_len = rt->dst.dev->hard_header_len +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 sizeof (struct ipv6hdr);
1204
David S. Millerd1918542011-12-28 20:19:20 -05001205 dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001206 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1207 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (dev->mtu < IPV6_MIN_MTU)
1210 dev->mtu = IPV6_MIN_MTU;
1211 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001212 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
1216/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001217 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * @t: tunnel to be changed
1219 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 *
1221 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001222 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 **/
1224
1225static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001226ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001228 t->parms.laddr = p->laddr;
1229 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 t->parms.flags = p->flags;
1231 t->parms.hop_limit = p->hop_limit;
1232 t->parms.encap_limit = p->encap_limit;
1233 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001234 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001235 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001236 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001237 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239}
1240
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001241static void
1242ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1243{
1244 p->laddr = u->laddr;
1245 p->raddr = u->raddr;
1246 p->flags = u->flags;
1247 p->hop_limit = u->hop_limit;
1248 p->encap_limit = u->encap_limit;
1249 p->flowinfo = u->flowinfo;
1250 p->link = u->link;
1251 p->proto = u->proto;
1252 memcpy(p->name, u->name, sizeof(u->name));
1253}
1254
1255static void
1256ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1257{
1258 u->laddr = p->laddr;
1259 u->raddr = p->raddr;
1260 u->flags = p->flags;
1261 u->hop_limit = p->hop_limit;
1262 u->encap_limit = p->encap_limit;
1263 u->flowinfo = p->flowinfo;
1264 u->link = p->link;
1265 u->proto = p->proto;
1266 memcpy(u->name, p->name, sizeof(u->name));
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001270 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * @dev: virtual device associated with tunnel
1272 * @ifr: parameters passed from userspace
1273 * @cmd: command to be performed
1274 *
1275 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001276 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001277 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 *
1279 * The possible commands are the following:
1280 * %SIOCGETTUNNEL: get tunnel parameters for device
1281 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1282 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1283 * %SIOCDELTUNNEL: delete tunnel
1284 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001285 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 * initialization, can be used for creating other tunnel devices.
1287 *
1288 * Return:
1289 * 0 on success,
1290 * %-EFAULT if unable to copy data to or from userspace,
1291 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1292 * %-EINVAL if passed tunnel parameters are invalid,
1293 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1294 * %-ENODEV if attempting to change or delete a nonexisting device
1295 **/
1296
1297static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001298ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001302 struct __ip6_tnl_parm p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct ip6_tnl *t = NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001304 struct net *net = dev_net(dev);
1305 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 switch (cmd) {
1308 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001309 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001310 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 err = -EFAULT;
1312 break;
1313 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001314 ip6_tnl_parm_from_user(&p1, &p);
1315 t = ip6_tnl_locate(net, &p1, 0);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001316 } else {
1317 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001318 }
1319 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001320 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001321 ip6_tnl_parm_to_user(&p, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1323 err = -EFAULT;
1324 }
1325 break;
1326 case SIOCADDTUNNEL:
1327 case SIOCCHGTUNNEL:
1328 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!capable(CAP_NET_ADMIN))
1330 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001331 err = -EFAULT;
1332 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001334 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001335 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1336 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001338 ip6_tnl_parm_from_user(&p1, &p);
1339 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001340 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001341 if (t != NULL) {
1342 if (t->dev != dev) {
1343 err = -EEXIST;
1344 break;
1345 }
1346 } else
1347 t = netdev_priv(dev);
1348
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001349 ip6_tnl_unlink(ip6n, t);
Pavel Emelyanov74b0b852010-10-27 05:43:53 +00001350 synchronize_net();
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001351 err = ip6_tnl_change(t, &p1);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001352 ip6_tnl_link(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 netdev_state_change(dev);
1354 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001355 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001357 ip6_tnl_parm_to_user(&p, &t->parms);
1358 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001359 err = -EFAULT;
1360
1361 } else
1362 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
1364 case SIOCDELTUNNEL:
1365 err = -EPERM;
1366 if (!capable(CAP_NET_ADMIN))
1367 break;
1368
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001369 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001370 err = -EFAULT;
1371 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001373 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001374 ip6_tnl_parm_from_user(&p1, &p);
1375 t = ip6_tnl_locate(net, &p1, 0);
1376 if (t == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001378 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001379 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001381 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001383 err = 0;
1384 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386 default:
1387 err = -EINVAL;
1388 }
1389 return err;
1390}
1391
1392/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001393 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * @dev: virtual device associated with tunnel
1395 * @new_mtu: the new mtu
1396 *
1397 * Return:
1398 * 0 on success,
1399 * %-EINVAL if mtu too small
1400 **/
1401
1402static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001403ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 if (new_mtu < IPV6_MIN_MTU) {
1406 return -EINVAL;
1407 }
1408 dev->mtu = new_mtu;
1409 return 0;
1410}
1411
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001412
1413static const struct net_device_ops ip6_tnl_netdev_ops = {
Eric Dumazet8560f222010-09-28 03:23:34 +00001414 .ndo_uninit = ip6_tnl_dev_uninit,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001415 .ndo_start_xmit = ip6_tnl_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001416 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001417 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001418 .ndo_get_stats = ip6_get_stats,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001419};
1420
Eric Dumazet8560f222010-09-28 03:23:34 +00001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001423 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * @dev: virtual device associated with tunnel
1425 *
1426 * Description:
1427 * Initialize function pointers and device parameters
1428 **/
1429
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001430static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Anders Franzen381601e2010-11-24 05:47:18 +00001432 struct ip6_tnl *t;
1433
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001434 dev->netdev_ops = &ip6_tnl_netdev_ops;
Eric Dumazet8560f222010-09-28 03:23:34 +00001435 dev->destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 dev->type = ARPHRD_TUNNEL6;
1438 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1439 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001440 t = netdev_priv(dev);
1441 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1442 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 dev->flags |= IFF_NOARP;
1444 dev->addr_len = sizeof(struct in6_addr);
Pavel Emelyanov554eb272008-04-16 01:24:13 -07001445 dev->features |= NETIF_F_NETNS_LOCAL;
Anders Franzen7e223de2010-10-19 03:50:47 +00001446 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449
1450/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001451 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * @dev: virtual device associated with tunnel
1453 **/
1454
Eric Dumazet8560f222010-09-28 03:23:34 +00001455static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001456ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Patrick McHardy2941a482006-01-08 22:05:26 -08001458 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 t->dev = dev;
Eric Dumazet8560f222010-09-28 03:23:34 +00001461 dev->tstats = alloc_percpu(struct pcpu_tstats);
1462 if (!dev->tstats)
1463 return -ENOMEM;
1464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001468 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 * @dev: virtual device associated with tunnel
1470 **/
1471
Eric Dumazet8560f222010-09-28 03:23:34 +00001472static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Patrick McHardy2941a482006-01-08 22:05:26 -08001474 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001475 int err = ip6_tnl_dev_init_gen(dev);
1476
1477 if (err)
1478 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001479 ip6_tnl_link_config(t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001484 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 * @dev: fallback device
1486 *
1487 * Return: 0
1488 **/
1489
Eric Dumazet8560f222010-09-28 03:23:34 +00001490static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Patrick McHardy2941a482006-01-08 22:05:26 -08001492 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001493 struct net *net = dev_net(dev);
1494 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001495 int err = ip6_tnl_dev_init_gen(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001496
Eric Dumazet8560f222010-09-28 03:23:34 +00001497 if (err)
1498 return err;
1499
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001500 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001502
1503 ip6_tnl_link_config(t);
1504
Eric Dumazetcf778b02012-01-12 04:41:32 +00001505 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001509static size_t ip6_get_size(const struct net_device *dev)
1510{
1511 return
1512 /* IFLA_IPTUN_LINK */
1513 nla_total_size(4) +
1514 /* IFLA_IPTUN_LOCAL */
1515 nla_total_size(sizeof(struct in6_addr)) +
1516 /* IFLA_IPTUN_REMOTE */
1517 nla_total_size(sizeof(struct in6_addr)) +
1518 /* IFLA_IPTUN_TTL */
1519 nla_total_size(1) +
1520 /* IFLA_IPTUN_ENCAP_LIMIT */
1521 nla_total_size(1) +
1522 /* IFLA_IPTUN_FLOWINFO */
1523 nla_total_size(4) +
1524 /* IFLA_IPTUN_FLAGS */
1525 nla_total_size(4) +
1526 0;
1527}
1528
1529static int ip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1530{
1531 struct ip6_tnl *tunnel = netdev_priv(dev);
1532 struct __ip6_tnl_parm *parm = &tunnel->parms;
1533
1534 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1535 nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
1536 &parm->raddr) ||
1537 nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
1538 &parm->laddr) ||
1539 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1540 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1541 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1542 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags))
1543 goto nla_put_failure;
1544 return 0;
1545
1546nla_put_failure:
1547 return -EMSGSIZE;
1548}
1549
1550static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1551 .kind = "ip6tnl",
1552 .maxtype = IFLA_IPTUN_MAX,
1553 .priv_size = sizeof(struct ip6_tnl),
1554 .get_size = ip6_get_size,
1555 .fill_info = ip6_fill_info,
1556};
1557
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001558static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001559 .handler = ip4ip6_rcv,
1560 .err_handler = ip4ip6_err,
1561 .priority = 1,
1562};
1563
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001564static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07001565 .handler = ip6ip6_rcv,
1566 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001567 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568};
1569
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001570static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001571{
1572 int h;
1573 struct ip6_tnl *t;
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001574 LIST_HEAD(list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001575
1576 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00001577 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001578 while (t != NULL) {
1579 unregister_netdevice_queue(t->dev, &list);
Eric Dumazet94767632010-09-15 20:25:34 +00001580 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001581 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001582 }
1583
Eric Dumazet94767632010-09-15 20:25:34 +00001584 t = rtnl_dereference(ip6n->tnls_wc[0]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001585 unregister_netdevice_queue(t->dev, &list);
1586 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001587}
1588
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001589static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001590{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001591 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00001592 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001593 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001594
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001595 ip6n->tnls[0] = ip6n->tnls_wc;
1596 ip6n->tnls[1] = ip6n->tnls_r_l;
1597
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001598 err = -ENOMEM;
1599 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1600 ip6_tnl_dev_setup);
1601
1602 if (!ip6n->fb_tnl_dev)
1603 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001604 dev_net_set(ip6n->fb_tnl_dev, net);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001605
Eric Dumazet8560f222010-09-28 03:23:34 +00001606 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1607 if (err < 0)
1608 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001609
1610 err = register_netdev(ip6n->fb_tnl_dev);
1611 if (err < 0)
1612 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00001613
1614 t = netdev_priv(ip6n->fb_tnl_dev);
1615
1616 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001617 return 0;
1618
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001619err_register:
Eric Dumazet8560f222010-09-28 03:23:34 +00001620 ip6_dev_free(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001621err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001622 return err;
1623}
1624
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001625static void __net_exit ip6_tnl_exit_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001626{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001627 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001628
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001629 rtnl_lock();
1630 ip6_tnl_destroy_tunnels(ip6n);
1631 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001632}
1633
1634static struct pernet_operations ip6_tnl_net_ops = {
1635 .init = ip6_tnl_init_net,
1636 .exit = ip6_tnl_exit_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001637 .id = &ip6_tnl_net_id,
1638 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001639};
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/**
1642 * ip6_tunnel_init - register protocol and reserve needed resources
1643 *
1644 * Return: 0 on success
1645 **/
1646
1647static int __init ip6_tunnel_init(void)
1648{
1649 int err;
1650
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001651 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001652 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001653 goto out_pernet;
1654
1655 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1656 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001657 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001658 goto out_ip4ip6;
1659 }
1660
1661 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1662 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001663 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001664 goto out_ip6ip6;
1665 }
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001666 err = rtnl_link_register(&ip6_link_ops);
1667 if (err < 0)
1668 goto rtnl_link_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001671
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001672rtnl_link_failed:
1673 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001674out_ip6ip6:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001675 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001676out_ip4ip6:
1677 unregister_pernet_device(&ip6_tnl_net_ops);
1678out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return err;
1680}
1681
1682/**
1683 * ip6_tunnel_cleanup - free resources and unregister protocol
1684 **/
1685
1686static void __exit ip6_tunnel_cleanup(void)
1687{
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001688 rtnl_link_unregister(&ip6_link_ops);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001689 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00001690 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001691
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001692 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00001693 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001695 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
1698module_init(ip6_tunnel_init);
1699module_exit(ip6_tunnel_cleanup);