blob: eabffbb89795d921b0977989345b25d81f553ee0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/net.h>
33#include <linux/in6.h>
34#include <linux/netdevice.h>
35#include <linux/if_arp.h>
36#include <linux/icmpv6.h>
37#include <linux/init.h>
38#include <linux/route.h>
39#include <linux/rtnetlink.h>
40#include <linux/netfilter_ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000042#include <linux/hash.h>
Nicolas Dichtele8377352013-08-20 12:16:06 +020043#include <linux/etherdevice.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>
Pravin B Shelarc5441932013-03-25 14:49:35 +000050#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/ip6_route.h>
53#include <net/addrconf.h>
54#include <net/ip6_tunnel.h>
55#include <net/xfrm.h>
56#include <net/dsfield.h>
57#include <net/inet_ecn.h>
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070058#include <net/net_namespace.h>
59#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61MODULE_AUTHOR("Ville Nuorvala");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090062MODULE_DESCRIPTION("IPv6 tunneling device");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_LICENSE("GPL");
Tom Gundersenf98f89a2014-05-15 23:21:30 +020064MODULE_ALIAS_RTNL_LINK("ip6tnl");
stephen hemminger6dfbd872011-03-10 11:43:19 +000065MODULE_ALIAS_NETDEV("ip6tnl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric Dumazetddbe5032012-07-18 08:11:12 +000067#define HASH_SIZE_SHIFT 5
68#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +000070static bool log_ecn_error = true;
71module_param(log_ecn_error, bool, 0644);
72MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
73
Eric Dumazetddbe5032012-07-18 08:11:12 +000074static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
75{
76 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
77
78 return hash_32(hash, HASH_SIZE_SHIFT);
79}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Eric Dumazet8560f222010-09-28 03:23:34 +000081static int ip6_tnl_dev_init(struct net_device *dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090082static void ip6_tnl_dev_setup(struct net_device *dev);
Nicolas Dichtelc075b132012-11-09 06:10:01 +000083static struct rtnl_link_ops ip6_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Eric Dumazetf99189b2009-11-17 10:42:49 +000085static int ip6_tnl_net_id __read_mostly;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070086struct ip6_tnl_net {
Pavel Emelyanov15820e1292008-04-16 01:23:02 -070087 /* the IPv6 tunnel fallback device */
88 struct net_device *fb_tnl_dev;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -070089 /* lists for storing tunnels in use */
Eric Dumazet94767632010-09-15 20:25:34 +000090 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
91 struct ip6_tnl __rcu *tnls_wc[1];
92 struct ip6_tnl __rcu **tnls[2];
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070093};
94
Eric Dumazet8560f222010-09-28 03:23:34 +000095static struct net_device_stats *ip6_get_stats(struct net_device *dev)
96{
David S. Miller56a43422014-01-06 17:37:45 -050097 struct pcpu_sw_netstats tmp, sum = { 0 };
Eric Dumazet8560f222010-09-28 03:23:34 +000098 int i;
99
100 for_each_possible_cpu(i) {
Li RongQingabb60132014-01-02 13:20:12 +0800101 unsigned int start;
Li RongQing8f849852014-01-04 13:57:59 +0800102 const struct pcpu_sw_netstats *tstats =
103 per_cpu_ptr(dev->tstats, i);
Eric Dumazet8560f222010-09-28 03:23:34 +0000104
Li RongQingabb60132014-01-02 13:20:12 +0800105 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700106 start = u64_stats_fetch_begin_irq(&tstats->syncp);
Li RongQingabb60132014-01-02 13:20:12 +0800107 tmp.rx_packets = tstats->rx_packets;
108 tmp.rx_bytes = tstats->rx_bytes;
109 tmp.tx_packets = tstats->tx_packets;
110 tmp.tx_bytes = tstats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700111 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
Li RongQingabb60132014-01-02 13:20:12 +0800112
113 sum.rx_packets += tmp.rx_packets;
114 sum.rx_bytes += tmp.rx_bytes;
115 sum.tx_packets += tmp.tx_packets;
116 sum.tx_bytes += tmp.tx_bytes;
Eric Dumazet8560f222010-09-28 03:23:34 +0000117 }
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
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700129static void ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst,
130 struct dst_entry *dst)
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700131{
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700132 write_seqlock_bh(&idst->lock);
133 dst_release(rcu_dereference_protected(
134 idst->dst,
135 lockdep_is_held(&idst->lock.lock)));
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700136 if (dst) {
137 dst_hold(dst);
138 idst->cookie = rt6_get_cookie((struct rt6_info *)dst);
139 } else {
140 idst->cookie = 0;
141 }
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700142 rcu_assign_pointer(idst->dst, dst);
143 write_sequnlock_bh(&idst->lock);
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700144}
145
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700146struct dst_entry *ip6_tnl_dst_get(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700148 struct ip6_tnl_dst *idst;
149 struct dst_entry *dst;
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700150 unsigned int seq;
151 u32 cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700153 idst = raw_cpu_ptr(t->dst_cache);
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700154
155 rcu_read_lock();
156 do {
157 seq = read_seqbegin(&idst->lock);
158 dst = rcu_dereference(idst->dst);
159 cookie = idst->cookie;
160 } while (read_seqretry(&idst->lock, seq));
161
162 if (dst && !atomic_inc_not_zero(&dst->__refcnt))
163 dst = NULL;
164 rcu_read_unlock();
165
166 if (dst && dst->obsolete && !dst->ops->check(dst, cookie)) {
167 ip6_tnl_per_cpu_dst_set(idst, NULL);
168 dst_release(dst);
169 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return dst;
172}
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700173EXPORT_SYMBOL_GPL(ip6_tnl_dst_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000175void ip6_tnl_dst_reset(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700177 int i;
178
179 for_each_possible_cpu(i)
180 ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000182EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700184void ip6_tnl_dst_set(struct ip6_tnl *t, struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700186 ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), dst);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700189EXPORT_SYMBOL_GPL(ip6_tnl_dst_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700191void ip6_tnl_dst_destroy(struct ip6_tnl *t)
192{
193 if (!t->dst_cache)
194 return;
195
196 ip6_tnl_dst_reset(t);
197 free_percpu(t->dst_cache);
198}
199EXPORT_SYMBOL_GPL(ip6_tnl_dst_destroy);
200
201int ip6_tnl_dst_init(struct ip6_tnl *t)
202{
203 int i;
204
205 t->dst_cache = alloc_percpu(struct ip6_tnl_dst);
206 if (!t->dst_cache)
207 return -ENOMEM;
208
209 for_each_possible_cpu(i)
Martin KaFai Lau70da5b52015-09-15 14:30:09 -0700210 seqlock_init(&per_cpu_ptr(t->dst_cache, i)->lock);
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700211
212 return 0;
213}
214EXPORT_SYMBOL_GPL(ip6_tnl_dst_init);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900217 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900218 * @remote: the address of the tunnel exit-point
219 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900221 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900223 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * else %NULL
225 **/
226
Eric Dumazet2922bc82009-10-23 06:34:34 +0000227#define for_each_ip6_tunnel_rcu(start) \
228 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static struct ip6_tnl *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000231ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000233 unsigned int hash = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct ip6_tnl *t;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700235 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Steffen Klassertea3dc962014-11-05 08:03:50 +0100236 struct in6_addr any;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Eric Dumazetddbe5032012-07-18 08:11:12 +0000238 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (ipv6_addr_equal(local, &t->parms.laddr) &&
240 ipv6_addr_equal(remote, &t->parms.raddr) &&
241 (t->dev->flags & IFF_UP))
242 return t;
243 }
Steffen Klassertea3dc962014-11-05 08:03:50 +0100244
245 memset(&any, 0, sizeof(any));
246 hash = HASH(&any, local);
247 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
248 if (ipv6_addr_equal(local, &t->parms.laddr) &&
249 (t->dev->flags & IFF_UP))
250 return t;
251 }
252
253 hash = HASH(remote, &any);
254 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
255 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
256 (t->dev->flags & IFF_UP))
257 return t;
258 }
259
Eric Dumazet2922bc82009-10-23 06:34:34 +0000260 t = rcu_dereference(ip6n->tnls_wc[0]);
261 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return t;
263
264 return NULL;
265}
266
267/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900268 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900269 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
271 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900272 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * &struct in6_addr entries laddr and raddr in @p.
274 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900275 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 **/
277
Eric Dumazet94767632010-09-15 20:25:34 +0000278static struct ip6_tnl __rcu **
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000279ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000281 const struct in6_addr *remote = &p->raddr;
282 const struct in6_addr *local = &p->laddr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000283 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int prio = 0;
285
286 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
287 prio = 1;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000288 h = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700290 return &ip6n->tnls[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900294 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * @t: tunnel to be added
296 **/
297
298static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700299ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Eric Dumazet94767632010-09-15 20:25:34 +0000301 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Eric Dumazetcf778b02012-01-12 04:41:32 +0000303 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
304 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900308 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * @t: tunnel to be removed
310 **/
311
312static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700313ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Eric Dumazet94767632010-09-15 20:25:34 +0000315 struct ip6_tnl __rcu **tp;
316 struct ip6_tnl *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Eric Dumazet94767632010-09-15 20:25:34 +0000318 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
319 (iter = rtnl_dereference(*tp)) != NULL;
320 tp = &iter->next) {
321 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000322 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 }
325 }
326}
327
Eric Dumazet8560f222010-09-28 03:23:34 +0000328static void ip6_dev_free(struct net_device *dev)
329{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700330 struct ip6_tnl *t = netdev_priv(dev);
331
332 ip6_tnl_dst_destroy(t);
Eric Dumazet8560f222010-09-28 03:23:34 +0000333 free_percpu(dev->tstats);
334 free_netdev(dev);
335}
336
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000337static int ip6_tnl_create2(struct net_device *dev)
338{
339 struct ip6_tnl *t = netdev_priv(dev);
340 struct net *net = dev_net(dev);
341 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
342 int err;
343
344 t = netdev_priv(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000345
346 err = register_netdevice(dev);
347 if (err < 0)
348 goto out;
349
350 strcpy(t->parms.name, dev->name);
351 dev->rtnl_link_ops = &ip6_link_ops;
352
353 dev_hold(dev);
354 ip6_tnl_link(ip6n, t);
355 return 0;
356
357out:
358 return err;
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000362 * ip6_tnl_create - create a new tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * @p: tunnel parameters
364 * @pt: pointer to new tunnel
365 *
366 * Description:
367 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900368 *
369 * Return:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100370 * created tunnel or error pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 **/
372
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000373static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct net_device *dev;
376 struct ip6_tnl *t;
377 char name[IFNAMSIZ];
Nicolas Dichtel37355562015-03-16 15:56:05 +0100378 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800380 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800382 else
383 sprintf(name, "ip6tnl%%d");
384
Tom Gundersenc835a672014-07-14 16:37:24 +0200385 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
386 ip6_tnl_dev_setup);
Ian Morris63159f22015-03-29 14:00:04 +0100387 if (!dev)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800388 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Pavel Emelyanov554eb272008-04-16 01:24:13 -0700390 dev_net_set(dev, net);
391
Patrick McHardy2941a482006-01-08 22:05:26 -0800392 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 t->parms = *p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200394 t->net = dev_net(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000395 err = ip6_tnl_create2(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +0000396 if (err < 0)
397 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ville Nuorvala567131a2006-11-24 17:05:41 -0800399 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800400
401failed_free:
Eric Dumazet8560f222010-09-28 03:23:34 +0000402 ip6_dev_free(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800403failed:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100404 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900408 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900409 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * @create: != 0 if allowed to create new tunnel if no match found
411 *
412 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900413 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * based on @parms. If this is unsuccessful, but @create is set a new
415 * tunnel device is created and registered for use.
416 *
417 * Return:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100418 * matching tunnel or error pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 **/
420
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700421static struct ip6_tnl *ip6_tnl_locate(struct net *net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000422 struct __ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000424 const struct in6_addr *remote = &p->raddr;
425 const struct in6_addr *local = &p->laddr;
Eric Dumazet94767632010-09-15 20:25:34 +0000426 struct ip6_tnl __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700428 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Eric Dumazet94767632010-09-15 20:25:34 +0000430 for (tp = ip6_tnl_bucket(ip6n, p);
431 (t = rtnl_dereference(*tp)) != NULL;
432 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200434 ipv6_addr_equal(remote, &t->parms.raddr)) {
435 if (create)
Nicolas Dichtel37355562015-03-16 15:56:05 +0100436 return ERR_PTR(-EEXIST);
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200437
Ville Nuorvala567131a2006-11-24 17:05:41 -0800438 return t;
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 if (!create)
Nicolas Dichtel37355562015-03-16 15:56:05 +0100442 return ERR_PTR(-ENODEV);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700443 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900447 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900449 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900451 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 **/
453
454static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900455ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Patrick McHardy2941a482006-01-08 22:05:26 -0800457 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200458 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700459 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Eric Dumazet94767632010-09-15 20:25:34 +0000461 if (dev == ip6n->fb_tnl_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000462 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
Eric Dumazet94767632010-09-15 20:25:34 +0000463 else
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700464 ip6_tnl_unlink(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ip6_tnl_dst_reset(t);
466 dev_put(dev);
467}
468
469/**
470 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
471 * @skb: received socket buffer
472 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900473 * Return:
474 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * else index to encapsulation limit
476 **/
477
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000478__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000480 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 __u8 nexthdr = ipv6h->nexthdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100482 __u16 off = sizeof(*ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
485 __u16 optlen = 0;
486 struct ipv6_opt_hdr *hdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100487 if (raw + off + sizeof(*hdr) > skb->data &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
489 break;
490
491 hdr = (struct ipv6_opt_hdr *) (raw + off);
492 if (nexthdr == NEXTHDR_FRAGMENT) {
493 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
494 if (frag_hdr->frag_off)
495 break;
496 optlen = 8;
497 } else if (nexthdr == NEXTHDR_AUTH) {
498 optlen = (hdr->hdrlen + 2) << 2;
499 } else {
500 optlen = ipv6_optlen(hdr);
501 }
502 if (nexthdr == NEXTHDR_DEST) {
503 __u16 i = off + 2;
504 while (1) {
505 struct ipv6_tlv_tnl_enc_lim *tel;
506
507 /* No more room for encapsulation limit */
508 if (i + sizeof (*tel) > off + optlen)
509 break;
510
511 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
512 /* return index of option if found and valid */
513 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
514 tel->length == 1)
515 return i;
516 /* else jump to next option */
517 if (tel->type)
518 i += tel->length + 2;
519 else
520 i++;
521 }
522 }
523 nexthdr = hdr->nexthdr;
524 off += optlen;
525 }
526 return 0;
527}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000528EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900531 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
533 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900534 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * to the specifications in RFC 2473.
536 **/
537
Herbert Xud2acc342006-03-28 01:12:13 -0800538static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900539ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700540 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000542 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct ip6_tnl *t;
544 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700545 u8 rel_type = ICMPV6_DEST_UNREACH;
546 u8 rel_code = ICMPV6_ADDR_UNREACH;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300547 u8 tproto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 __u32 rel_info = 0;
549 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800550 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900552 /* If the packet doesn't contain the original IPv6 header we are
553 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 processing of the error. */
555
Eric Dumazet2922bc82009-10-23 06:34:34 +0000556 rcu_read_lock();
Ian Morrise5d08d72014-11-23 21:28:43 +0000557 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
Ian Morris63159f22015-03-29 14:00:04 +0100558 if (!t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out;
560
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300561 tproto = ACCESS_ONCE(t->parms.proto);
562 if (tproto != ipproto && tproto != 0)
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900563 goto out;
564
Herbert Xud2acc342006-03-28 01:12:13 -0800565 err = 0;
566
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900567 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 __u32 teli;
569 struct ipv6_tlv_tnl_enc_lim *tel;
570 __u32 mtu;
571 case ICMPV6_DEST_UNREACH:
Matt Bennett17a10c92015-09-25 11:01:47 +1200572 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
573 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rel_msg = 1;
575 break;
576 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900577 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Matt Bennett17a10c92015-09-25 11:01:47 +1200578 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
579 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 rel_msg = 1;
581 }
582 break;
583 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800584 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900585 if ((*code) == ICMPV6_HDR_FIELD)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000586 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Al Viro704eae12007-07-26 17:33:29 +0100588 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
590 if (tel->encap_limit == 0) {
Matt Bennett17a10c92015-09-25 11:01:47 +1200591 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
592 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 rel_msg = 1;
594 }
Joe Perchese87cc472012-05-13 21:56:26 +0000595 } else {
Matt Bennett17a10c92015-09-25 11:01:47 +1200596 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
597 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 break;
600 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100601 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (mtu < IPV6_MIN_MTU)
603 mtu = IPV6_MIN_MTU;
604 t->dev->mtu = mtu;
605
Ian Morrise5d08d72014-11-23 21:28:43 +0000606 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
607 if (len > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 rel_type = ICMPV6_PKT_TOOBIG;
609 rel_code = 0;
610 rel_info = mtu;
611 rel_msg = 1;
612 }
613 break;
614 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900615
616 *type = rel_type;
617 *code = rel_code;
618 *info = rel_info;
619 *msg = rel_msg;
620
621out:
Eric Dumazet2922bc82009-10-23 06:34:34 +0000622 rcu_read_unlock();
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900623 return err;
624}
625
626static int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900627ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700628 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900629{
630 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700631 u8 rel_type = type;
632 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100633 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900634 int err;
635 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000636 const struct iphdr *eiph;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900637 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -0700638 struct flowi4 fl4;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900639
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900640 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
641 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900642 if (err < 0)
643 return err;
644
645 if (rel_msg == 0)
646 return 0;
647
648 switch (rel_type) {
649 case ICMPV6_DEST_UNREACH:
650 if (rel_code != ICMPV6_ADDR_UNREACH)
651 return 0;
652 rel_type = ICMP_DEST_UNREACH;
653 rel_code = ICMP_HOST_UNREACH;
654 break;
655 case ICMPV6_PKT_TOOBIG:
656 if (rel_code != 0)
657 return 0;
658 rel_type = ICMP_DEST_UNREACH;
659 rel_code = ICMP_FRAG_NEEDED;
660 break;
David S. Millerec18d9a2012-07-12 00:25:15 -0700661 case NDISC_REDIRECT:
662 rel_type = ICMP_REDIRECT;
663 rel_code = ICMP_REDIR_HOST;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900664 default:
665 return 0;
666 }
667
668 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
669 return 0;
670
671 skb2 = skb_clone(skb, GFP_ATOMIC);
672 if (!skb2)
673 return 0;
674
Eric Dumazetadf30902009-06-02 05:19:30 +0000675 skb_dst_drop(skb2);
676
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900677 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700678 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700679 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900680
681 /* Try to guess incoming interface */
David S. Miller31e45432011-05-03 20:25:42 -0700682 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500683 eiph->saddr, 0,
684 0, 0,
685 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800686 if (IS_ERR(rt))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900687 goto out;
688
Changli Gaod8d1f302010-06-10 23:31:35 -0700689 skb2->dev = rt->dst.dev;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900690
691 /* route "incoming" packet */
692 if (rt->rt_flags & RTCF_LOCAL) {
693 ip_rt_put(rt);
694 rt = NULL;
David S. Miller31e45432011-05-03 20:25:42 -0700695 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500696 eiph->daddr, eiph->saddr,
697 0, 0,
698 IPPROTO_IPIP,
699 RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800700 if (IS_ERR(rt) ||
Changli Gaod8d1f302010-06-10 23:31:35 -0700701 rt->dst.dev->type != ARPHRD_TUNNEL) {
David S. Millerb23dd4f2011-03-02 14:31:35 -0800702 if (!IS_ERR(rt))
703 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900704 goto out;
705 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800706 skb_dst_set(skb2, &rt->dst);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900707 } else {
708 ip_rt_put(rt);
709 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
710 skb2->dev) ||
Eric Dumazetadf30902009-06-02 05:19:30 +0000711 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900712 goto out;
713 }
714
715 /* change mtu on this route */
716 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000717 if (rel_info > dst_mtu(skb_dst(skb2)))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900718 goto out;
719
David S. Miller6700c272012-07-17 03:29:28 -0700720 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900721 }
David S. Miller1ed5c482012-07-12 00:41:25 -0700722 if (rel_type == ICMP_REDIRECT)
David S. Miller6700c272012-07-17 03:29:28 -0700723 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900724
Al Viro704eae12007-07-26 17:33:29 +0100725 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900726
727out:
728 kfree_skb(skb2);
729 return 0;
730}
731
732static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900733ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700734 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900735{
736 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700737 u8 rel_type = type;
738 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100739 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900740 int err;
741
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900742 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
743 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900744 if (err < 0)
745 return err;
746
747 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct rt6_info *rt;
749 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Eric Dumazetadf30902009-06-02 05:19:30 +0000754 skb_dst_drop(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700756 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /* Try to guess incoming interface */
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700759 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
760 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
David S. Millerd1918542011-12-28 20:19:20 -0500762 if (rt && rt->dst.dev)
763 skb2->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000765 icmpv6_send(skb2, rel_type, rel_code, rel_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Amerigo Wang94e187c2012-10-29 00:13:19 +0000767 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 kfree_skb(skb2);
770 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900771
772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000775static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
776 const struct ipv6hdr *ipv6h,
777 struct sk_buff *skb)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900778{
779 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
780
781 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700782 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900783
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000784 return IP6_ECN_decapsulate(ipv6h, skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900785}
786
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000787static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
788 const struct ipv6hdr *ipv6h,
789 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900791 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800792 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000794 return IP6_ECN_decapsulate(ipv6h, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900796
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000797__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000798 const struct in6_addr *laddr,
799 const struct in6_addr *raddr)
800{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000801 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000802 int ltype = ipv6_addr_type(laddr);
803 int rtype = ipv6_addr_type(raddr);
804 __u32 flags = 0;
805
806 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
807 flags = IP6_TNL_F_CAP_PER_PACKET;
808 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
809 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
810 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
811 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
812 if (ltype&IPV6_ADDR_UNICAST)
813 flags |= IP6_TNL_F_CAP_XMIT;
814 if (rtype&IPV6_ADDR_UNICAST)
815 flags |= IP6_TNL_F_CAP_RCV;
816 }
817 return flags;
818}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000819EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000820
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100821/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000822int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000823 const struct in6_addr *laddr,
824 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800825{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000826 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800827 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200828 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800829
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000830 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
831 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
832 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900833 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800834
835 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100836 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800837
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000838 if ((ipv6_addr_is_multicast(laddr) ||
839 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
840 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800841 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800842 }
843 return ret;
844}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000845EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900848 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900850 * @protocol: ethernet protocol ID
851 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 *
853 * Return: 0
854 **/
855
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900856static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900857 __u8 ipproto,
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000858 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
859 const struct ipv6hdr *ipv6h,
860 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000863 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300864 u8 tproto;
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Eric Dumazet2922bc82009-10-23 06:34:34 +0000867 rcu_read_lock();
Ian Morrise5d08d72014-11-23 21:28:43 +0000868 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
Ian Morris53b24b82015-03-29 14:00:05 +0100869 if (t) {
Li RongQing8f849852014-01-04 13:57:59 +0800870 struct pcpu_sw_netstats *tstats;
Eric Dumazet8560f222010-09-28 03:23:34 +0000871
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300872 tproto = ACCESS_ONCE(t->parms.proto);
873 if (tproto != ipproto && tproto != 0) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000874 rcu_read_unlock();
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900875 goto discard;
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000879 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700880 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000883 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700884 t->dev->stats.rx_dropped++;
Eric Dumazet2922bc82009-10-23 06:34:34 +0000885 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 goto discard;
887 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700888 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700889 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900890 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700892
Nicolas Dichtelea231922013-09-02 15:34:58 +0200893 __skb_tunnel_rx(skb, t->dev, t->net);
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000894
895 err = dscp_ecn_decapsulate(t, ipv6h, skb);
896 if (unlikely(err)) {
897 if (log_ecn_error)
898 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
899 &ipv6h->saddr,
900 ipv6_get_dsfield(ipv6h));
901 if (err > 1) {
902 ++t->dev->stats.rx_frame_errors;
903 ++t->dev->stats.rx_errors;
904 rcu_read_unlock();
905 goto discard;
906 }
907 }
908
Eric Dumazet8560f222010-09-28 03:23:34 +0000909 tstats = this_cpu_ptr(t->dev->tstats);
Li RongQingabb60132014-01-02 13:20:12 +0800910 u64_stats_update_begin(&tstats->syncp);
Eric Dumazet8560f222010-09-28 03:23:34 +0000911 tstats->rx_packets++;
912 tstats->rx_bytes += skb->len;
Li RongQingabb60132014-01-02 13:20:12 +0800913 u64_stats_update_end(&tstats->syncp);
Eric Dumazet8560f222010-09-28 03:23:34 +0000914
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000915 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000916
Eric Dumazet2922bc82009-10-23 06:34:34 +0000917 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000920 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700922
923discard:
924 kfree_skb(skb);
925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900928static int ip4ip6_rcv(struct sk_buff *skb)
929{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900930 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
931 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900932}
933
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900934static int ip6ip6_rcv(struct sk_buff *skb)
935{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900936 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
937 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900938}
939
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800940struct ipv6_tel_txoption {
941 struct ipv6_txoptions ops;
942 __u8 dst_opt[8];
943};
944
945static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800947 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800949 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
950 opt->dst_opt[3] = 1;
951 opt->dst_opt[4] = encap_limit;
952 opt->dst_opt[5] = IPV6_TLV_PADN;
953 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800955 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
956 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900960 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900962 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *
964 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900965 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * doesn't match source of incoming packet.
967 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900968 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * 1 if conflict,
970 * 0 else
971 **/
972
Eric Dumazet92113bf2012-05-18 08:14:11 +0200973static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000974ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
977}
978
Steffen Klassertd5005142014-11-05 08:02:48 +0100979int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
980 const struct in6_addr *laddr,
981 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800982{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000983 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800984 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200985 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800986
Steffen Klassertd5005142014-11-05 08:02:48 +0100987 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
988 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
989 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800990 struct net_device *ldev = NULL;
991
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100992 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800993 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100994 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800995
Steffen Klassertd5005142014-11-05 08:02:48 +0100996 if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000997 pr_warn("%s xmit: Local address not yet configured!\n",
998 p->name);
Steffen Klassertd5005142014-11-05 08:02:48 +0100999 else if (!ipv6_addr_is_multicast(raddr) &&
1000 unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +00001001 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1002 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001003 else
1004 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +01001005 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001006 }
1007 return ret;
1008}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001009EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001012 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001014 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001015 * @dsfield: dscp code for outer header
1016 * @fl: flow of tunneled packet
1017 * @encap_limit: encapsulation limit
1018 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 *
1020 * Description:
1021 * Build new header and do some sanity checks on the packet before sending
1022 * it.
1023 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001024 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001025 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001026 * -1 fail
1027 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 **/
1029
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001030static int ip6_tnl_xmit2(struct sk_buff *skb,
1031 struct net_device *dev,
1032 __u8 dsfield,
David S. Miller4c9483b2011-03-12 16:22:43 -05001033 struct flowi6 *fl6,
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001034 int encap_limit,
1035 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Patrick McHardy2941a482006-01-08 22:05:26 -08001037 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001038 struct net *net = t->net;
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001039 struct net_device_stats *stats = &t->dev->stats;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001040 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001041 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001042 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct net_device *tdev;
1044 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -07001045 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001047 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Steffen Klassertea3dc962014-11-05 08:03:50 +01001049 /* NBMA tunnel */
1050 if (ipv6_addr_any(&t->parms.raddr)) {
1051 struct in6_addr *addr6;
1052 struct neighbour *neigh;
1053 int addr_type;
1054
1055 if (!skb_dst(skb))
1056 goto tx_err_link_failure;
1057
1058 neigh = dst_neigh_lookup(skb_dst(skb),
1059 &ipv6_hdr(skb)->daddr);
1060 if (!neigh)
1061 goto tx_err_link_failure;
1062
1063 addr6 = (struct in6_addr *)&neigh->primary_key;
1064 addr_type = ipv6_addr_type(addr6);
1065
1066 if (addr_type == IPV6_ADDR_ANY)
1067 addr6 = &ipv6_hdr(skb)->daddr;
1068
1069 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1070 neigh_release(neigh);
1071 } else if (!fl6->flowi6_mark)
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -07001072 dst = ip6_tnl_dst_get(t);
Steffen Klassertd5005142014-11-05 08:02:48 +01001073
1074 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1075 goto tx_err_link_failure;
1076
Eric Dumazet89b02122011-07-28 04:32:25 +00001077 if (!dst) {
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001078 dst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001080 if (dst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -07001081 goto tx_err_link_failure;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001082 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1083 if (IS_ERR(dst)) {
1084 err = PTR_ERR(dst);
1085 dst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -08001086 goto tx_err_link_failure;
1087 }
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001088 ndst = dst;
Patrick McHardya57ebc92005-09-08 14:27:47 -07001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 tdev = dst->dev;
1092
1093 if (tdev == dev) {
1094 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +00001095 net_warn_ratelimited("%s: Local routing loop detected!\n",
1096 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto tx_err_dst_release;
1098 }
Ian Morris67ba4152014-08-24 21:53:10 +01001099 mtu = dst_mtu(dst) - sizeof(*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001100 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 max_headroom += 8;
1102 mtu -= 8;
1103 }
1104 if (mtu < IPV6_MIN_MTU)
1105 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +00001106 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -07001107 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001109 *pmtu = mtu;
1110 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 goto tx_err_dst_release;
1112 }
1113
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001114 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * Okay, now see if we can stuff it in the buffer as-is.
1118 */
1119 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001120
Patrick McHardycfbba492007-07-09 15:33:40 -07001121 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1122 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001124
Ian Morrise5d08d72014-11-23 21:28:43 +00001125 new_skb = skb_realloc_headroom(skb, max_headroom);
1126 if (!new_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto tx_err_dst_release;
1128
1129 if (skb->sk)
1130 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +00001131 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 skb = new_skb;
1133 }
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001134
1135 if (!fl6->flowi6_mark && ndst)
1136 ip6_tnl_dst_set(t, ndst);
1137 skb_dst_set(skb, dst);
1138
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001139 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
David S. Miller4c9483b2011-03-12 16:22:43 -05001141 proto = fl6->flowi6_proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001142 if (encap_limit >= 0) {
1143 init_tel_txopt(&opt, encap_limit);
1144 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1145 }
Hannes Frederic Sowa3d483052013-08-18 13:46:52 +02001146
1147 if (likely(!skb->encapsulation)) {
1148 skb_reset_inner_headers(skb);
1149 skb->encapsulation = 1;
1150 }
1151
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001152 skb_push(skb, sizeof(struct ipv6hdr));
1153 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001154 ipv6h = ipv6_hdr(skb);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001155 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
Tom Herbert42240902015-07-31 16:52:12 -07001156 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ipv6h->hop_limit = t->parms.hop_limit;
1158 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001159 ipv6h->saddr = fl6->saddr;
1160 ipv6h->daddr = fl6->daddr;
David Miller79b16aa2015-04-05 22:19:09 -04001161 ip6tunnel_xmit(NULL, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
1163tx_err_link_failure:
1164 stats->tx_carrier_errors++;
1165 dst_link_failure(skb);
1166tx_err_dst_release:
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001167 dst_release(dst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001168 return err;
1169}
1170
1171static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001172ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1173{
1174 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001175 const struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001176 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001177 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001178 __u8 dsfield;
1179 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001180 u8 tproto;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001181 int err;
1182
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001183 tproto = ACCESS_ONCE(t->parms.proto);
Steffen Klassertd5005142014-11-05 08:02:48 +01001184 if (tproto != IPPROTO_IPIP && tproto != 0)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001185 return -1;
1186
1187 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1188 encap_limit = t->parms.encap_limit;
1189
Ian Morris67ba4152014-08-24 21:53:10 +01001190 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
David S. Miller4c9483b2011-03-12 16:22:43 -05001191 fl6.flowi6_proto = IPPROTO_IPIP;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001192
1193 dsfield = ipv4_get_dsfield(iph);
1194
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001195 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001196 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
Al Virob77f2fa2007-07-21 19:09:41 -07001197 & IPV6_TCLASS_MASK;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001198 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1199 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001200
David S. Miller4c9483b2011-03-12 16:22:43 -05001201 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001202 if (err != 0) {
1203 /* XXX: send ICMP error even if DF is not set. */
1204 if (err == -EMSGSIZE)
1205 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1206 htonl(mtu));
1207 return -1;
1208 }
1209
1210 return 0;
1211}
1212
1213static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001214ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1215{
1216 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001217 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001218 int encap_limit = -1;
1219 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001220 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001221 __u8 dsfield;
1222 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001223 u8 tproto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001224 int err;
1225
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001226 tproto = ACCESS_ONCE(t->parms.proto);
1227 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
Steffen Klassertd5005142014-11-05 08:02:48 +01001228 ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001229 return -1;
1230
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001231 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001232 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001233 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001234 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001235 if (tel->encap_limit == 0) {
1236 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001237 ICMPV6_HDR_FIELD, offset + 2);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001238 return -1;
1239 }
1240 encap_limit = tel->encap_limit - 1;
1241 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1242 encap_limit = t->parms.encap_limit;
1243
Ian Morris67ba4152014-08-24 21:53:10 +01001244 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
David S. Miller4c9483b2011-03-12 16:22:43 -05001245 fl6.flowi6_proto = IPPROTO_IPV6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001246
1247 dsfield = ipv6_get_dsfield(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001248 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001249 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001250 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
Florent Fourcot3308de22013-12-08 15:47:00 +01001251 fl6.flowlabel |= ip6_flowlabel(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001252 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1253 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001254
David S. Miller4c9483b2011-03-12 16:22:43 -05001255 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001256 if (err != 0) {
1257 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001258 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001259 return -1;
1260 }
1261
1262 return 0;
1263}
1264
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001265static netdev_tx_t
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001266ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1267{
1268 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001269 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001270 int ret;
1271
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001272 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001273 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001274 ret = ip4ip6_tnl_xmit(skb, dev);
1275 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001276 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001277 ret = ip6ip6_tnl_xmit(skb, dev);
1278 break;
1279 default:
1280 goto tx_err;
1281 }
1282
1283 if (ret < 0)
1284 goto tx_err;
1285
Patrick McHardy6ed10652009-06-23 06:03:08 +00001286 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288tx_err:
1289 stats->tx_errors++;
1290 stats->tx_dropped++;
1291 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001292 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001295static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001298 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001299 struct flowi6 *fl6 = &t->fl.u.ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001301 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1302 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001305 fl6->saddr = p->laddr;
1306 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001307 fl6->flowi6_oif = p->link;
1308 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001311 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001313 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001315 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1316 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1319 dev->flags |= IFF_POINTOPOINT;
1320 else
1321 dev->flags &= ~IFF_POINTOPOINT;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001324 int strict = (ipv6_addr_type(&p->raddr) &
1325 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1326
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001327 struct rt6_info *rt = rt6_lookup(t->net,
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001328 &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001329 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Ian Morris63159f22015-03-29 14:00:04 +01001331 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return;
1333
David S. Millerd1918542011-12-28 20:19:20 -05001334 if (rt->dst.dev) {
1335 dev->hard_header_len = rt->dst.dev->hard_header_len +
Ian Morris67ba4152014-08-24 21:53:10 +01001336 sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Ian Morris67ba4152014-08-24 21:53:10 +01001338 dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001339 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Ian Morris67ba4152014-08-24 21:53:10 +01001340 dev->mtu -= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 if (dev->mtu < IPV6_MIN_MTU)
1343 dev->mtu = IPV6_MIN_MTU;
1344 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001345 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347}
1348
1349/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001350 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * @t: tunnel to be changed
1352 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
1354 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001355 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 **/
1357
1358static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001359ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001361 t->parms.laddr = p->laddr;
1362 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 t->parms.flags = p->flags;
1364 t->parms.hop_limit = p->hop_limit;
1365 t->parms.encap_limit = p->encap_limit;
1366 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001367 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001368 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001369 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001370 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return 0;
1372}
1373
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001374static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1375{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001376 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001377 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1378 int err;
1379
1380 ip6_tnl_unlink(ip6n, t);
1381 synchronize_net();
1382 err = ip6_tnl_change(t, p);
1383 ip6_tnl_link(ip6n, t);
1384 netdev_state_change(t->dev);
1385 return err;
1386}
1387
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001388static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1389{
1390 /* for default tnl0 device allow to change only the proto */
1391 t->parms.proto = p->proto;
1392 netdev_state_change(t->dev);
1393 return 0;
1394}
1395
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001396static void
1397ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1398{
1399 p->laddr = u->laddr;
1400 p->raddr = u->raddr;
1401 p->flags = u->flags;
1402 p->hop_limit = u->hop_limit;
1403 p->encap_limit = u->encap_limit;
1404 p->flowinfo = u->flowinfo;
1405 p->link = u->link;
1406 p->proto = u->proto;
1407 memcpy(p->name, u->name, sizeof(u->name));
1408}
1409
1410static void
1411ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1412{
1413 u->laddr = p->laddr;
1414 u->raddr = p->raddr;
1415 u->flags = p->flags;
1416 u->hop_limit = p->hop_limit;
1417 u->encap_limit = p->encap_limit;
1418 u->flowinfo = p->flowinfo;
1419 u->link = p->link;
1420 u->proto = p->proto;
1421 memcpy(u->name, p->name, sizeof(u->name));
1422}
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001425 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * @dev: virtual device associated with tunnel
1427 * @ifr: parameters passed from userspace
1428 * @cmd: command to be performed
1429 *
1430 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001431 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001432 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 *
1434 * The possible commands are the following:
1435 * %SIOCGETTUNNEL: get tunnel parameters for device
1436 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1437 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1438 * %SIOCDELTUNNEL: delete tunnel
1439 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001440 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * initialization, can be used for creating other tunnel devices.
1442 *
1443 * Return:
1444 * 0 on success,
1445 * %-EFAULT if unable to copy data to or from userspace,
1446 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1447 * %-EINVAL if passed tunnel parameters are invalid,
1448 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1449 * %-ENODEV if attempting to change or delete a nonexisting device
1450 **/
1451
1452static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001453ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001457 struct __ip6_tnl_parm p1;
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001458 struct ip6_tnl *t = netdev_priv(dev);
1459 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001460 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 switch (cmd) {
1463 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001464 if (dev == ip6n->fb_tnl_dev) {
Ian Morris67ba4152014-08-24 21:53:10 +01001465 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 err = -EFAULT;
1467 break;
1468 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001469 ip6_tnl_parm_from_user(&p1, &p);
1470 t = ip6_tnl_locate(net, &p1, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001471 if (IS_ERR(t))
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001472 t = netdev_priv(dev);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001473 } else {
1474 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001475 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001476 ip6_tnl_parm_to_user(&p, &t->parms);
Ian Morris67ba4152014-08-24 21:53:10 +01001477 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 err = -EFAULT;
1479 }
1480 break;
1481 case SIOCADDTUNNEL:
1482 case SIOCCHGTUNNEL:
1483 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001484 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001486 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001487 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001489 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001490 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1491 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001493 ip6_tnl_parm_from_user(&p1, &p);
1494 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001495 if (cmd == SIOCCHGTUNNEL) {
Nicolas Dichtel37355562015-03-16 15:56:05 +01001496 if (!IS_ERR(t)) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001497 if (t->dev != dev) {
1498 err = -EEXIST;
1499 break;
1500 }
1501 } else
1502 t = netdev_priv(dev);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001503 if (dev == ip6n->fb_tnl_dev)
1504 err = ip6_tnl0_update(t, &p1);
1505 else
1506 err = ip6_tnl_update(t, &p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Nicolas Dichtel37355562015-03-16 15:56:05 +01001508 if (!IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001510 ip6_tnl_parm_to_user(&p, &t->parms);
1511 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001512 err = -EFAULT;
1513
Nicolas Dichtel37355562015-03-16 15:56:05 +01001514 } else {
1515 err = PTR_ERR(t);
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
1518 case SIOCDELTUNNEL:
1519 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001520 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
1522
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001523 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001524 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001525 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001527 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001528 ip6_tnl_parm_from_user(&p1, &p);
1529 t = ip6_tnl_locate(net, &p1, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001530 if (IS_ERR(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001532 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001533 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001535 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001537 err = 0;
1538 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 break;
1540 default:
1541 err = -EINVAL;
1542 }
1543 return err;
1544}
1545
1546/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001547 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 * @dev: virtual device associated with tunnel
1549 * @new_mtu: the new mtu
1550 *
1551 * Return:
1552 * 0 on success,
1553 * %-EINVAL if mtu too small
1554 **/
1555
1556static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001557ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001559 struct ip6_tnl *tnl = netdev_priv(dev);
1560
1561 if (tnl->parms.proto == IPPROTO_IPIP) {
1562 if (new_mtu < 68)
1563 return -EINVAL;
1564 } else {
1565 if (new_mtu < IPV6_MIN_MTU)
1566 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001568 if (new_mtu > 0xFFF8 - dev->hard_header_len)
1569 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 dev->mtu = new_mtu;
1571 return 0;
1572}
1573
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001574int ip6_tnl_get_iflink(const struct net_device *dev)
1575{
1576 struct ip6_tnl *t = netdev_priv(dev);
1577
1578 return t->parms.link;
1579}
1580EXPORT_SYMBOL(ip6_tnl_get_iflink);
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001581
1582static const struct net_device_ops ip6_tnl_netdev_ops = {
Steffen Klassert6c6151d2014-11-03 09:19:27 +01001583 .ndo_init = ip6_tnl_dev_init,
Eric Dumazet8560f222010-09-28 03:23:34 +00001584 .ndo_uninit = ip6_tnl_dev_uninit,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001585 .ndo_start_xmit = ip6_tnl_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001586 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001587 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001588 .ndo_get_stats = ip6_get_stats,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001589 .ndo_get_iflink = ip6_tnl_get_iflink,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001590};
1591
Eric Dumazet8560f222010-09-28 03:23:34 +00001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001594 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * @dev: virtual device associated with tunnel
1596 *
1597 * Description:
1598 * Initialize function pointers and device parameters
1599 **/
1600
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001601static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Anders Franzen381601e2010-11-24 05:47:18 +00001603 struct ip6_tnl *t;
1604
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001605 dev->netdev_ops = &ip6_tnl_netdev_ops;
Eric Dumazet8560f222010-09-28 03:23:34 +00001606 dev->destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 dev->type = ARPHRD_TUNNEL6;
Ian Morris67ba4152014-08-24 21:53:10 +01001609 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1610 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001611 t = netdev_priv(dev);
1612 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Ian Morris67ba4152014-08-24 21:53:10 +01001613 dev->mtu -= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 dev->flags |= IFF_NOARP;
1615 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001616 netif_keep_dst(dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02001617 /* This perm addr will be used as interface identifier by IPv6 */
1618 dev->addr_assign_type = NET_ADDR_RANDOM;
1619 eth_random_addr(dev->perm_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622
1623/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001624 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * @dev: virtual device associated with tunnel
1626 **/
1627
Eric Dumazet8560f222010-09-28 03:23:34 +00001628static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001629ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Patrick McHardy2941a482006-01-08 22:05:26 -08001631 struct ip6_tnl *t = netdev_priv(dev);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001632 int ret;
Eric Dumazet8560f222010-09-28 03:23:34 +00001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 t->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001635 t->net = dev_net(dev);
WANG Cong1c213bd2014-02-13 11:46:28 -08001636 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Eric Dumazet8560f222010-09-28 03:23:34 +00001637 if (!dev->tstats)
1638 return -ENOMEM;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001639
1640 ret = ip6_tnl_dst_init(t);
1641 if (ret) {
1642 free_percpu(dev->tstats);
1643 dev->tstats = NULL;
1644 return ret;
1645 }
1646
Eric Dumazet8560f222010-09-28 03:23:34 +00001647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001651 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * @dev: virtual device associated with tunnel
1653 **/
1654
Eric Dumazet8560f222010-09-28 03:23:34 +00001655static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Patrick McHardy2941a482006-01-08 22:05:26 -08001657 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001658 int err = ip6_tnl_dev_init_gen(dev);
1659
1660 if (err)
1661 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001662 ip6_tnl_link_config(t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001667 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * @dev: fallback device
1669 *
1670 * Return: 0
1671 **/
1672
Eric Dumazet8560f222010-09-28 03:23:34 +00001673static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Patrick McHardy2941a482006-01-08 22:05:26 -08001675 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001676 struct net *net = dev_net(dev);
1677 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001678
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001679 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001681
Eric Dumazetcf778b02012-01-12 04:41:32 +00001682 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001686static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1687{
1688 u8 proto;
1689
Susant Sahanic8965932014-05-10 00:11:32 +05301690 if (!data || !data[IFLA_IPTUN_PROTO])
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001691 return 0;
1692
1693 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1694 if (proto != IPPROTO_IPV6 &&
1695 proto != IPPROTO_IPIP &&
1696 proto != 0)
1697 return -EINVAL;
1698
1699 return 0;
1700}
1701
1702static void ip6_tnl_netlink_parms(struct nlattr *data[],
1703 struct __ip6_tnl_parm *parms)
1704{
1705 memset(parms, 0, sizeof(*parms));
1706
1707 if (!data)
1708 return;
1709
1710 if (data[IFLA_IPTUN_LINK])
1711 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1712
1713 if (data[IFLA_IPTUN_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001714 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001715
1716 if (data[IFLA_IPTUN_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001717 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001718
1719 if (data[IFLA_IPTUN_TTL])
1720 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1721
1722 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1723 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1724
1725 if (data[IFLA_IPTUN_FLOWINFO])
Nicolas Dichtel1ff05fb2012-11-15 04:06:42 +00001726 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001727
1728 if (data[IFLA_IPTUN_FLAGS])
1729 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1730
1731 if (data[IFLA_IPTUN_PROTO])
1732 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1733}
1734
1735static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1736 struct nlattr *tb[], struct nlattr *data[])
1737{
1738 struct net *net = dev_net(dev);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001739 struct ip6_tnl *nt, *t;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001740
1741 nt = netdev_priv(dev);
1742 ip6_tnl_netlink_parms(data, &nt->parms);
1743
Nicolas Dichtel37355562015-03-16 15:56:05 +01001744 t = ip6_tnl_locate(net, &nt->parms, 0);
1745 if (!IS_ERR(t))
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001746 return -EEXIST;
1747
1748 return ip6_tnl_create2(dev);
1749}
1750
1751static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1752 struct nlattr *data[])
1753{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001754 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001755 struct __ip6_tnl_parm p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001756 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001757 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1758
1759 if (dev == ip6n->fb_tnl_dev)
1760 return -EINVAL;
1761
1762 ip6_tnl_netlink_parms(data, &p);
1763
1764 t = ip6_tnl_locate(net, &p, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001765 if (!IS_ERR(t)) {
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001766 if (t->dev != dev)
1767 return -EEXIST;
1768 } else
1769 t = netdev_priv(dev);
1770
1771 return ip6_tnl_update(t, &p);
1772}
1773
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001774static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1775{
1776 struct net *net = dev_net(dev);
1777 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1778
1779 if (dev != ip6n->fb_tnl_dev)
1780 unregister_netdevice_queue(dev, head);
1781}
1782
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001783static size_t ip6_tnl_get_size(const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001784{
1785 return
1786 /* IFLA_IPTUN_LINK */
1787 nla_total_size(4) +
1788 /* IFLA_IPTUN_LOCAL */
1789 nla_total_size(sizeof(struct in6_addr)) +
1790 /* IFLA_IPTUN_REMOTE */
1791 nla_total_size(sizeof(struct in6_addr)) +
1792 /* IFLA_IPTUN_TTL */
1793 nla_total_size(1) +
1794 /* IFLA_IPTUN_ENCAP_LIMIT */
1795 nla_total_size(1) +
1796 /* IFLA_IPTUN_FLOWINFO */
1797 nla_total_size(4) +
1798 /* IFLA_IPTUN_FLAGS */
1799 nla_total_size(4) +
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001800 /* IFLA_IPTUN_PROTO */
1801 nla_total_size(1) +
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001802 0;
1803}
1804
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001805static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001806{
1807 struct ip6_tnl *tunnel = netdev_priv(dev);
1808 struct __ip6_tnl_parm *parm = &tunnel->parms;
1809
1810 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001811 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1812 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001813 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1814 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1815 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001816 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1817 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001818 goto nla_put_failure;
1819 return 0;
1820
1821nla_put_failure:
1822 return -EMSGSIZE;
1823}
1824
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001825struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1826{
1827 struct ip6_tnl *tunnel = netdev_priv(dev);
1828
1829 return tunnel->net;
1830}
1831EXPORT_SYMBOL(ip6_tnl_get_link_net);
1832
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001833static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1834 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1835 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1836 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1837 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1838 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1839 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1840 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1841 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1842};
1843
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001844static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1845 .kind = "ip6tnl",
1846 .maxtype = IFLA_IPTUN_MAX,
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001847 .policy = ip6_tnl_policy,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001848 .priv_size = sizeof(struct ip6_tnl),
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001849 .setup = ip6_tnl_dev_setup,
1850 .validate = ip6_tnl_validate,
1851 .newlink = ip6_tnl_newlink,
1852 .changelink = ip6_tnl_changelink,
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001853 .dellink = ip6_tnl_dellink,
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001854 .get_size = ip6_tnl_get_size,
1855 .fill_info = ip6_tnl_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001856 .get_link_net = ip6_tnl_get_link_net,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001857};
1858
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001859static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001860 .handler = ip4ip6_rcv,
1861 .err_handler = ip4ip6_err,
1862 .priority = 1,
1863};
1864
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001865static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07001866 .handler = ip6ip6_rcv,
1867 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001868 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869};
1870
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001871static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001872{
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001873 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001874 struct net_device *dev, *aux;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001875 int h;
1876 struct ip6_tnl *t;
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001877 LIST_HEAD(list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001878
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001879 for_each_netdev_safe(net, dev, aux)
1880 if (dev->rtnl_link_ops == &ip6_link_ops)
1881 unregister_netdevice_queue(dev, &list);
1882
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001883 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00001884 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Ian Morris53b24b82015-03-29 14:00:05 +01001885 while (t) {
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001886 /* If dev is in the same netns, it has already
1887 * been added to the list by the previous loop.
1888 */
1889 if (!net_eq(dev_net(t->dev), net))
1890 unregister_netdevice_queue(t->dev, &list);
Eric Dumazet94767632010-09-15 20:25:34 +00001891 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001892 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001893 }
1894
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001895 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001896}
1897
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001898static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001899{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001900 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00001901 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001902 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001903
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001904 ip6n->tnls[0] = ip6n->tnls_wc;
1905 ip6n->tnls[1] = ip6n->tnls_r_l;
1906
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001907 err = -ENOMEM;
1908 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001909 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001910
1911 if (!ip6n->fb_tnl_dev)
1912 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001913 dev_net_set(ip6n->fb_tnl_dev, net);
Nicolas Dichtelbb814092013-10-01 18:05:00 +02001914 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001915 /* FB netdevice is special: we have one, and only one per netns.
1916 * Allowing to move it to another netns is clearly unsafe.
1917 */
1918 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001919
Eric Dumazet8560f222010-09-28 03:23:34 +00001920 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1921 if (err < 0)
1922 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001923
1924 err = register_netdev(ip6n->fb_tnl_dev);
1925 if (err < 0)
1926 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00001927
1928 t = netdev_priv(ip6n->fb_tnl_dev);
1929
1930 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001931 return 0;
1932
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001933err_register:
Eric Dumazet8560f222010-09-28 03:23:34 +00001934 ip6_dev_free(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001935err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001936 return err;
1937}
1938
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001939static void __net_exit ip6_tnl_exit_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001940{
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001941 rtnl_lock();
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001942 ip6_tnl_destroy_tunnels(net);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001943 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001944}
1945
1946static struct pernet_operations ip6_tnl_net_ops = {
1947 .init = ip6_tnl_init_net,
1948 .exit = ip6_tnl_exit_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001949 .id = &ip6_tnl_net_id,
1950 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001951};
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/**
1954 * ip6_tunnel_init - register protocol and reserve needed resources
1955 *
1956 * Return: 0 on success
1957 **/
1958
1959static int __init ip6_tunnel_init(void)
1960{
1961 int err;
1962
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001963 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001964 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001965 goto out_pernet;
1966
1967 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1968 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001969 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001970 goto out_ip4ip6;
1971 }
1972
1973 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1974 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001975 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001976 goto out_ip6ip6;
1977 }
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001978 err = rtnl_link_register(&ip6_link_ops);
1979 if (err < 0)
1980 goto rtnl_link_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001983
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001984rtnl_link_failed:
1985 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001986out_ip6ip6:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001987 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001988out_ip4ip6:
1989 unregister_pernet_device(&ip6_tnl_net_ops);
1990out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return err;
1992}
1993
1994/**
1995 * ip6_tunnel_cleanup - free resources and unregister protocol
1996 **/
1997
1998static void __exit ip6_tunnel_cleanup(void)
1999{
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002000 rtnl_link_unregister(&ip6_link_ops);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09002001 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00002002 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09002003
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08002004 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00002005 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00002007 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010module_init(ip6_tunnel_init);
2011module_exit(ip6_tunnel_cleanup);