blob: 4550d08f44dea81a4446cfa33e903aa5c8a33f05 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#ifdef IP6_TNL_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +000068#define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#else
70#define IP6_TNL_TRACE(x...) do {;} while(0)
71#endif
72
Eric Dumazetddbe5032012-07-18 08:11:12 +000073#define HASH_SIZE_SHIFT 5
74#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +000076static bool log_ecn_error = true;
77module_param(log_ecn_error, bool, 0644);
78MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
79
Eric Dumazetddbe5032012-07-18 08:11:12 +000080static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
81{
82 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
83
84 return hash_32(hash, HASH_SIZE_SHIFT);
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Eric Dumazet8560f222010-09-28 03:23:34 +000087static int ip6_tnl_dev_init(struct net_device *dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090088static void ip6_tnl_dev_setup(struct net_device *dev);
Nicolas Dichtelc075b132012-11-09 06:10:01 +000089static struct rtnl_link_ops ip6_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Eric Dumazetf99189b2009-11-17 10:42:49 +000091static int ip6_tnl_net_id __read_mostly;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070092struct ip6_tnl_net {
Pavel Emelyanov15820e1292008-04-16 01:23:02 -070093 /* the IPv6 tunnel fallback device */
94 struct net_device *fb_tnl_dev;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -070095 /* lists for storing tunnels in use */
Eric Dumazet94767632010-09-15 20:25:34 +000096 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
97 struct ip6_tnl __rcu *tnls_wc[1];
98 struct ip6_tnl __rcu **tnls[2];
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070099};
100
Eric Dumazet8560f222010-09-28 03:23:34 +0000101static struct net_device_stats *ip6_get_stats(struct net_device *dev)
102{
David S. Miller56a43422014-01-06 17:37:45 -0500103 struct pcpu_sw_netstats tmp, sum = { 0 };
Eric Dumazet8560f222010-09-28 03:23:34 +0000104 int i;
105
106 for_each_possible_cpu(i) {
Li RongQingabb60132014-01-02 13:20:12 +0800107 unsigned int start;
Li RongQing8f849852014-01-04 13:57:59 +0800108 const struct pcpu_sw_netstats *tstats =
109 per_cpu_ptr(dev->tstats, i);
Eric Dumazet8560f222010-09-28 03:23:34 +0000110
Li RongQingabb60132014-01-02 13:20:12 +0800111 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700112 start = u64_stats_fetch_begin_irq(&tstats->syncp);
Li RongQingabb60132014-01-02 13:20:12 +0800113 tmp.rx_packets = tstats->rx_packets;
114 tmp.rx_bytes = tstats->rx_bytes;
115 tmp.tx_packets = tstats->tx_packets;
116 tmp.tx_bytes = tstats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700117 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
Li RongQingabb60132014-01-02 13:20:12 +0800118
119 sum.rx_packets += tmp.rx_packets;
120 sum.rx_bytes += tmp.rx_bytes;
121 sum.tx_packets += tmp.tx_packets;
122 sum.tx_bytes += tmp.tx_bytes;
Eric Dumazet8560f222010-09-28 03:23:34 +0000123 }
124 dev->stats.rx_packets = sum.rx_packets;
125 dev->stats.rx_bytes = sum.rx_bytes;
126 dev->stats.tx_packets = sum.tx_packets;
127 dev->stats.tx_bytes = sum.tx_bytes;
128 return &dev->stats;
129}
130
Eric Dumazet2922bc82009-10-23 06:34:34 +0000131/*
Eric Dumazet94767632010-09-15 20:25:34 +0000132 * Locking : hash tables are protected by RCU and RTNL
Eric Dumazet2922bc82009-10-23 06:34:34 +0000133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000135struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct dst_entry *dst = t->dst_cache;
138
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900139 if (dst && dst->obsolete &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dst->ops->check(dst, t->dst_cookie) == NULL) {
141 t->dst_cache = NULL;
142 dst_release(dst);
143 return NULL;
144 }
145
146 return dst;
147}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000148EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000150void ip6_tnl_dst_reset(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 dst_release(t->dst_cache);
153 t->dst_cache = NULL;
154}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000155EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000157void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct rt6_info *rt = (struct rt6_info *) dst;
160 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
161 dst_release(t->dst_cache);
162 t->dst_cache = dst;
163}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000164EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900167 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900168 * @remote: the address of the tunnel exit-point
169 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900171 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900173 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * else %NULL
175 **/
176
Eric Dumazet2922bc82009-10-23 06:34:34 +0000177#define for_each_ip6_tunnel_rcu(start) \
178 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct ip6_tnl *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000181ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000183 unsigned int hash = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct ip6_tnl *t;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700185 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Steffen Klassertea3dc962014-11-05 08:03:50 +0100186 struct in6_addr any;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Eric Dumazetddbe5032012-07-18 08:11:12 +0000188 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (ipv6_addr_equal(local, &t->parms.laddr) &&
190 ipv6_addr_equal(remote, &t->parms.raddr) &&
191 (t->dev->flags & IFF_UP))
192 return t;
193 }
Steffen Klassertea3dc962014-11-05 08:03:50 +0100194
195 memset(&any, 0, sizeof(any));
196 hash = HASH(&any, local);
197 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
198 if (ipv6_addr_equal(local, &t->parms.laddr) &&
199 (t->dev->flags & IFF_UP))
200 return t;
201 }
202
203 hash = HASH(remote, &any);
204 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
205 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
206 (t->dev->flags & IFF_UP))
207 return t;
208 }
209
Eric Dumazet2922bc82009-10-23 06:34:34 +0000210 t = rcu_dereference(ip6n->tnls_wc[0]);
211 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return t;
213
214 return NULL;
215}
216
217/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900218 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900219 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
221 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900222 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * &struct in6_addr entries laddr and raddr in @p.
224 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900225 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 **/
227
Eric Dumazet94767632010-09-15 20:25:34 +0000228static struct ip6_tnl __rcu **
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000229ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000231 const struct in6_addr *remote = &p->raddr;
232 const struct in6_addr *local = &p->laddr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000233 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int prio = 0;
235
236 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
237 prio = 1;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000238 h = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700240 return &ip6n->tnls[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900244 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * @t: tunnel to be added
246 **/
247
248static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700249ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Eric Dumazet94767632010-09-15 20:25:34 +0000251 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Eric Dumazetcf778b02012-01-12 04:41:32 +0000253 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
254 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900258 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * @t: tunnel to be removed
260 **/
261
262static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700263ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Eric Dumazet94767632010-09-15 20:25:34 +0000265 struct ip6_tnl __rcu **tp;
266 struct ip6_tnl *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Eric Dumazet94767632010-09-15 20:25:34 +0000268 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
269 (iter = rtnl_dereference(*tp)) != NULL;
270 tp = &iter->next) {
271 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000272 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 break;
274 }
275 }
276}
277
Eric Dumazet8560f222010-09-28 03:23:34 +0000278static void ip6_dev_free(struct net_device *dev)
279{
280 free_percpu(dev->tstats);
281 free_netdev(dev);
282}
283
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000284static int ip6_tnl_create2(struct net_device *dev)
285{
286 struct ip6_tnl *t = netdev_priv(dev);
287 struct net *net = dev_net(dev);
288 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
289 int err;
290
291 t = netdev_priv(dev);
292 err = ip6_tnl_dev_init(dev);
293 if (err < 0)
294 goto out;
295
296 err = register_netdevice(dev);
297 if (err < 0)
298 goto out;
299
300 strcpy(t->parms.name, dev->name);
301 dev->rtnl_link_ops = &ip6_link_ops;
302
303 dev_hold(dev);
304 ip6_tnl_link(ip6n, t);
305 return 0;
306
307out:
308 return err;
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000312 * ip6_tnl_create - create a new tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * @p: tunnel parameters
314 * @pt: pointer to new tunnel
315 *
316 * Description:
317 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900318 *
319 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800320 * created tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 **/
322
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000323static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct net_device *dev;
326 struct ip6_tnl *t;
327 char name[IFNAMSIZ];
328 int err;
329
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800330 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800332 else
333 sprintf(name, "ip6tnl%%d");
334
Tom Gundersenc835a672014-07-14 16:37:24 +0200335 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
336 ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800338 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Pavel Emelyanov554eb272008-04-16 01:24:13 -0700340 dev_net_set(dev, net);
341
Patrick McHardy2941a482006-01-08 22:05:26 -0800342 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 t->parms = *p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200344 t->net = dev_net(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000345 err = ip6_tnl_create2(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +0000346 if (err < 0)
347 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Ville Nuorvala567131a2006-11-24 17:05:41 -0800349 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800350
351failed_free:
Eric Dumazet8560f222010-09-28 03:23:34 +0000352 ip6_dev_free(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800353failed:
354 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900358 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900359 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @create: != 0 if allowed to create new tunnel if no match found
361 *
362 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900363 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * based on @parms. If this is unsuccessful, but @create is set a new
365 * tunnel device is created and registered for use.
366 *
367 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800368 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 **/
370
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700371static struct ip6_tnl *ip6_tnl_locate(struct net *net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000372 struct __ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000374 const struct in6_addr *remote = &p->raddr;
375 const struct in6_addr *local = &p->laddr;
Eric Dumazet94767632010-09-15 20:25:34 +0000376 struct ip6_tnl __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700378 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Eric Dumazet94767632010-09-15 20:25:34 +0000380 for (tp = ip6_tnl_bucket(ip6n, p);
381 (t = rtnl_dereference(*tp)) != NULL;
382 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200384 ipv6_addr_equal(remote, &t->parms.raddr)) {
385 if (create)
386 return NULL;
387
Ville Nuorvala567131a2006-11-24 17:05:41 -0800388 return t;
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800392 return NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700393 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900397 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900399 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900401 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 **/
403
404static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900405ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Patrick McHardy2941a482006-01-08 22:05:26 -0800407 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200408 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700409 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Eric Dumazet94767632010-09-15 20:25:34 +0000411 if (dev == ip6n->fb_tnl_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000412 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
Eric Dumazet94767632010-09-15 20:25:34 +0000413 else
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700414 ip6_tnl_unlink(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ip6_tnl_dst_reset(t);
416 dev_put(dev);
417}
418
419/**
420 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
421 * @skb: received socket buffer
422 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900423 * Return:
424 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * else index to encapsulation limit
426 **/
427
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000428__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000430 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 __u8 nexthdr = ipv6h->nexthdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100432 __u16 off = sizeof(*ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
435 __u16 optlen = 0;
436 struct ipv6_opt_hdr *hdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100437 if (raw + off + sizeof(*hdr) > skb->data &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
439 break;
440
441 hdr = (struct ipv6_opt_hdr *) (raw + off);
442 if (nexthdr == NEXTHDR_FRAGMENT) {
443 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
444 if (frag_hdr->frag_off)
445 break;
446 optlen = 8;
447 } else if (nexthdr == NEXTHDR_AUTH) {
448 optlen = (hdr->hdrlen + 2) << 2;
449 } else {
450 optlen = ipv6_optlen(hdr);
451 }
452 if (nexthdr == NEXTHDR_DEST) {
453 __u16 i = off + 2;
454 while (1) {
455 struct ipv6_tlv_tnl_enc_lim *tel;
456
457 /* No more room for encapsulation limit */
458 if (i + sizeof (*tel) > off + optlen)
459 break;
460
461 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
462 /* return index of option if found and valid */
463 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
464 tel->length == 1)
465 return i;
466 /* else jump to next option */
467 if (tel->type)
468 i += tel->length + 2;
469 else
470 i++;
471 }
472 }
473 nexthdr = hdr->nexthdr;
474 off += optlen;
475 }
476 return 0;
477}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000478EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900481 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
483 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900484 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * to the specifications in RFC 2473.
486 **/
487
Herbert Xud2acc342006-03-28 01:12:13 -0800488static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900489ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700490 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000492 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct ip6_tnl *t;
494 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700495 u8 rel_type = ICMPV6_DEST_UNREACH;
496 u8 rel_code = ICMPV6_ADDR_UNREACH;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300497 u8 tproto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 __u32 rel_info = 0;
499 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800500 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900502 /* If the packet doesn't contain the original IPv6 header we are
503 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 processing of the error. */
505
Eric Dumazet2922bc82009-10-23 06:34:34 +0000506 rcu_read_lock();
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700507 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700508 &ipv6h->saddr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto out;
510
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300511 tproto = ACCESS_ONCE(t->parms.proto);
512 if (tproto != ipproto && tproto != 0)
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900513 goto out;
514
Herbert Xud2acc342006-03-28 01:12:13 -0800515 err = 0;
516
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900517 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 __u32 teli;
519 struct ipv6_tlv_tnl_enc_lim *tel;
520 __u32 mtu;
521 case ICMPV6_DEST_UNREACH:
Joe Perchese87cc472012-05-13 21:56:26 +0000522 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
523 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 rel_msg = 1;
525 break;
526 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900527 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Joe Perchese87cc472012-05-13 21:56:26 +0000528 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
529 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 rel_msg = 1;
531 }
532 break;
533 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800534 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900535 if ((*code) == ICMPV6_HDR_FIELD)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000536 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Al Viro704eae12007-07-26 17:33:29 +0100538 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
540 if (tel->encap_limit == 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000541 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
542 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 rel_msg = 1;
544 }
Joe Perchese87cc472012-05-13 21:56:26 +0000545 } else {
546 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
547 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 break;
550 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100551 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (mtu < IPV6_MIN_MTU)
553 mtu = IPV6_MIN_MTU;
554 t->dev->mtu = mtu;
555
Ian Morris67ba4152014-08-24 21:53:10 +0100556 if ((len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 rel_type = ICMPV6_PKT_TOOBIG;
558 rel_code = 0;
559 rel_info = mtu;
560 rel_msg = 1;
561 }
562 break;
563 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900564
565 *type = rel_type;
566 *code = rel_code;
567 *info = rel_info;
568 *msg = rel_msg;
569
570out:
Eric Dumazet2922bc82009-10-23 06:34:34 +0000571 rcu_read_unlock();
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900572 return err;
573}
574
575static int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900576ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700577 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900578{
579 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700580 u8 rel_type = type;
581 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100582 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900583 int err;
584 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000585 const struct iphdr *eiph;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900586 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -0700587 struct flowi4 fl4;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900588
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900589 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
590 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900591 if (err < 0)
592 return err;
593
594 if (rel_msg == 0)
595 return 0;
596
597 switch (rel_type) {
598 case ICMPV6_DEST_UNREACH:
599 if (rel_code != ICMPV6_ADDR_UNREACH)
600 return 0;
601 rel_type = ICMP_DEST_UNREACH;
602 rel_code = ICMP_HOST_UNREACH;
603 break;
604 case ICMPV6_PKT_TOOBIG:
605 if (rel_code != 0)
606 return 0;
607 rel_type = ICMP_DEST_UNREACH;
608 rel_code = ICMP_FRAG_NEEDED;
609 break;
David S. Millerec18d9a2012-07-12 00:25:15 -0700610 case NDISC_REDIRECT:
611 rel_type = ICMP_REDIRECT;
612 rel_code = ICMP_REDIR_HOST;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900613 default:
614 return 0;
615 }
616
617 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
618 return 0;
619
620 skb2 = skb_clone(skb, GFP_ATOMIC);
621 if (!skb2)
622 return 0;
623
Eric Dumazetadf30902009-06-02 05:19:30 +0000624 skb_dst_drop(skb2);
625
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900626 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700627 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700628 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900629
630 /* Try to guess incoming interface */
David S. Miller31e45432011-05-03 20:25:42 -0700631 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500632 eiph->saddr, 0,
633 0, 0,
634 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800635 if (IS_ERR(rt))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900636 goto out;
637
Changli Gaod8d1f302010-06-10 23:31:35 -0700638 skb2->dev = rt->dst.dev;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900639
640 /* route "incoming" packet */
641 if (rt->rt_flags & RTCF_LOCAL) {
642 ip_rt_put(rt);
643 rt = NULL;
David S. Miller31e45432011-05-03 20:25:42 -0700644 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500645 eiph->daddr, eiph->saddr,
646 0, 0,
647 IPPROTO_IPIP,
648 RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800649 if (IS_ERR(rt) ||
Changli Gaod8d1f302010-06-10 23:31:35 -0700650 rt->dst.dev->type != ARPHRD_TUNNEL) {
David S. Millerb23dd4f2011-03-02 14:31:35 -0800651 if (!IS_ERR(rt))
652 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900653 goto out;
654 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800655 skb_dst_set(skb2, &rt->dst);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900656 } else {
657 ip_rt_put(rt);
658 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
659 skb2->dev) ||
Eric Dumazetadf30902009-06-02 05:19:30 +0000660 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900661 goto out;
662 }
663
664 /* change mtu on this route */
665 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000666 if (rel_info > dst_mtu(skb_dst(skb2)))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900667 goto out;
668
David S. Miller6700c272012-07-17 03:29:28 -0700669 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900670 }
David S. Miller1ed5c482012-07-12 00:41:25 -0700671 if (rel_type == ICMP_REDIRECT)
David S. Miller6700c272012-07-17 03:29:28 -0700672 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900673
Al Viro704eae12007-07-26 17:33:29 +0100674 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900675
676out:
677 kfree_skb(skb2);
678 return 0;
679}
680
681static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900682ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700683 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900684{
685 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700686 u8 rel_type = type;
687 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100688 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900689 int err;
690
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900691 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
692 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900693 if (err < 0)
694 return err;
695
696 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct rt6_info *rt;
698 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Eric Dumazetadf30902009-06-02 05:19:30 +0000703 skb_dst_drop(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700705 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* Try to guess incoming interface */
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700708 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
709 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
David S. Millerd1918542011-12-28 20:19:20 -0500711 if (rt && rt->dst.dev)
712 skb2->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000714 icmpv6_send(skb2, rel_type, rel_code, rel_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Amerigo Wang94e187c2012-10-29 00:13:19 +0000716 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 kfree_skb(skb2);
719 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900720
721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000724static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
725 const struct ipv6hdr *ipv6h,
726 struct sk_buff *skb)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900727{
728 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
729
730 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700731 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900732
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000733 return IP6_ECN_decapsulate(ipv6h, skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900734}
735
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000736static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
737 const struct ipv6hdr *ipv6h,
738 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900740 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800741 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000743 return IP6_ECN_decapsulate(ipv6h, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900745
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000746__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000747 const struct in6_addr *laddr,
748 const struct in6_addr *raddr)
749{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000750 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000751 int ltype = ipv6_addr_type(laddr);
752 int rtype = ipv6_addr_type(raddr);
753 __u32 flags = 0;
754
755 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
756 flags = IP6_TNL_F_CAP_PER_PACKET;
757 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
758 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
759 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
760 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
761 if (ltype&IPV6_ADDR_UNICAST)
762 flags |= IP6_TNL_F_CAP_XMIT;
763 if (rtype&IPV6_ADDR_UNICAST)
764 flags |= IP6_TNL_F_CAP_RCV;
765 }
766 return flags;
767}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000768EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000769
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100770/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000771int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000772 const struct in6_addr *laddr,
773 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800774{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000775 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800776 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200777 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800778
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000779 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
780 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
781 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900782 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800783
784 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100785 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800786
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000787 if ((ipv6_addr_is_multicast(laddr) ||
788 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
789 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800790 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800791 }
792 return ret;
793}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000794EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900797 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900799 * @protocol: ethernet protocol ID
800 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *
802 * Return: 0
803 **/
804
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900805static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900806 __u8 ipproto,
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000807 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
808 const struct ipv6hdr *ipv6h,
809 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000812 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300813 u8 tproto;
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000814 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Eric Dumazet2922bc82009-10-23 06:34:34 +0000816 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700818 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700819 &ipv6h->daddr)) != NULL) {
Li RongQing8f849852014-01-04 13:57:59 +0800820 struct pcpu_sw_netstats *tstats;
Eric Dumazet8560f222010-09-28 03:23:34 +0000821
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300822 tproto = ACCESS_ONCE(t->parms.proto);
823 if (tproto != ipproto && tproto != 0) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000824 rcu_read_unlock();
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900825 goto discard;
826 }
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000829 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700830 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000833 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700834 t->dev->stats.rx_dropped++;
Eric Dumazet2922bc82009-10-23 06:34:34 +0000835 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto discard;
837 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700838 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700839 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900840 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700842
Nicolas Dichtelea231922013-09-02 15:34:58 +0200843 __skb_tunnel_rx(skb, t->dev, t->net);
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000844
845 err = dscp_ecn_decapsulate(t, ipv6h, skb);
846 if (unlikely(err)) {
847 if (log_ecn_error)
848 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
849 &ipv6h->saddr,
850 ipv6_get_dsfield(ipv6h));
851 if (err > 1) {
852 ++t->dev->stats.rx_frame_errors;
853 ++t->dev->stats.rx_errors;
854 rcu_read_unlock();
855 goto discard;
856 }
857 }
858
Eric Dumazet8560f222010-09-28 03:23:34 +0000859 tstats = this_cpu_ptr(t->dev->tstats);
Li RongQingabb60132014-01-02 13:20:12 +0800860 u64_stats_update_begin(&tstats->syncp);
Eric Dumazet8560f222010-09-28 03:23:34 +0000861 tstats->rx_packets++;
862 tstats->rx_bytes += skb->len;
Li RongQingabb60132014-01-02 13:20:12 +0800863 u64_stats_update_end(&tstats->syncp);
Eric Dumazet8560f222010-09-28 03:23:34 +0000864
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000865 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000866
Eric Dumazet2922bc82009-10-23 06:34:34 +0000867 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000870 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700872
873discard:
874 kfree_skb(skb);
875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900878static int ip4ip6_rcv(struct sk_buff *skb)
879{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900880 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
881 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900882}
883
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900884static int ip6ip6_rcv(struct sk_buff *skb)
885{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900886 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
887 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900888}
889
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800890struct ipv6_tel_txoption {
891 struct ipv6_txoptions ops;
892 __u8 dst_opt[8];
893};
894
895static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800897 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800899 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
900 opt->dst_opt[3] = 1;
901 opt->dst_opt[4] = encap_limit;
902 opt->dst_opt[5] = IPV6_TLV_PADN;
903 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800905 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
906 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900910 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900912 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 *
914 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900915 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * doesn't match source of incoming packet.
917 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900918 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * 1 if conflict,
920 * 0 else
921 **/
922
Eric Dumazet92113bf2012-05-18 08:14:11 +0200923static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000924ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
927}
928
Steffen Klassertd5005142014-11-05 08:02:48 +0100929int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
930 const struct in6_addr *laddr,
931 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800932{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000933 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800934 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200935 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800936
Steffen Klassertd5005142014-11-05 08:02:48 +0100937 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
938 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
939 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800940 struct net_device *ldev = NULL;
941
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100942 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800943 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100944 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800945
Steffen Klassertd5005142014-11-05 08:02:48 +0100946 if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000947 pr_warn("%s xmit: Local address not yet configured!\n",
948 p->name);
Steffen Klassertd5005142014-11-05 08:02:48 +0100949 else if (!ipv6_addr_is_multicast(raddr) &&
950 unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000951 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
952 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800953 else
954 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100955 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800956 }
957 return ret;
958}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000959EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900962 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900964 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900965 * @dsfield: dscp code for outer header
966 * @fl: flow of tunneled packet
967 * @encap_limit: encapsulation limit
968 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 *
970 * Description:
971 * Build new header and do some sanity checks on the packet before sending
972 * it.
973 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900974 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900975 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900976 * -1 fail
977 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 **/
979
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900980static int ip6_tnl_xmit2(struct sk_buff *skb,
981 struct net_device *dev,
982 __u8 dsfield,
David S. Miller4c9483b2011-03-12 16:22:43 -0500983 struct flowi6 *fl6,
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900984 int encap_limit,
985 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Patrick McHardy2941a482006-01-08 22:05:26 -0800987 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200988 struct net *net = t->net;
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700989 struct net_device_stats *stats = &t->dev->stats;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700990 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800991 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400992 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct net_device *tdev;
994 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -0700995 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900997 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Steffen Klassertea3dc962014-11-05 08:03:50 +0100999 /* NBMA tunnel */
1000 if (ipv6_addr_any(&t->parms.raddr)) {
1001 struct in6_addr *addr6;
1002 struct neighbour *neigh;
1003 int addr_type;
1004
1005 if (!skb_dst(skb))
1006 goto tx_err_link_failure;
1007
1008 neigh = dst_neigh_lookup(skb_dst(skb),
1009 &ipv6_hdr(skb)->daddr);
1010 if (!neigh)
1011 goto tx_err_link_failure;
1012
1013 addr6 = (struct in6_addr *)&neigh->primary_key;
1014 addr_type = ipv6_addr_type(addr6);
1015
1016 if (addr_type == IPV6_ADDR_ANY)
1017 addr6 = &ipv6_hdr(skb)->daddr;
1018
1019 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1020 neigh_release(neigh);
1021 } else if (!fl6->flowi6_mark)
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001022 dst = ip6_tnl_dst_check(t);
Steffen Klassertd5005142014-11-05 08:02:48 +01001023
1024 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1025 goto tx_err_link_failure;
1026
Eric Dumazet89b02122011-07-28 04:32:25 +00001027 if (!dst) {
1028 ndst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Eric Dumazet89b02122011-07-28 04:32:25 +00001030 if (ndst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -07001031 goto tx_err_link_failure;
Eric Dumazet89b02122011-07-28 04:32:25 +00001032 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
1033 if (IS_ERR(ndst)) {
1034 err = PTR_ERR(ndst);
1035 ndst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -08001036 goto tx_err_link_failure;
1037 }
Eric Dumazet89b02122011-07-28 04:32:25 +00001038 dst = ndst;
Patrick McHardya57ebc92005-09-08 14:27:47 -07001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 tdev = dst->dev;
1042
1043 if (tdev == dev) {
1044 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +00001045 net_warn_ratelimited("%s: Local routing loop detected!\n",
1046 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto tx_err_dst_release;
1048 }
Ian Morris67ba4152014-08-24 21:53:10 +01001049 mtu = dst_mtu(dst) - sizeof(*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001050 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 max_headroom += 8;
1052 mtu -= 8;
1053 }
1054 if (mtu < IPV6_MIN_MTU)
1055 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +00001056 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -07001057 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001059 *pmtu = mtu;
1060 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto tx_err_dst_release;
1062 }
1063
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001064 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * Okay, now see if we can stuff it in the buffer as-is.
1068 */
1069 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001070
Patrick McHardycfbba492007-07-09 15:33:40 -07001071 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1072 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
1076 goto tx_err_dst_release;
1077
1078 if (skb->sk)
1079 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +00001080 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 skb = new_skb;
1082 }
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001083 if (fl6->flowi6_mark) {
1084 skb_dst_set(skb, dst);
1085 ndst = NULL;
1086 } else {
1087 skb_dst_set_noref(skb, dst);
1088 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001089 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
David S. Miller4c9483b2011-03-12 16:22:43 -05001091 proto = fl6->flowi6_proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001092 if (encap_limit >= 0) {
1093 init_tel_txopt(&opt, encap_limit);
1094 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1095 }
Hannes Frederic Sowa3d483052013-08-18 13:46:52 +02001096
1097 if (likely(!skb->encapsulation)) {
1098 skb_reset_inner_headers(skb);
1099 skb->encapsulation = 1;
1100 }
1101
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001102 skb_push(skb, sizeof(struct ipv6hdr));
1103 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001104 ipv6h = ipv6_hdr(skb);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001105 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
1106 ip6_make_flowlabel(net, skb, fl6->flowlabel, false));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 ipv6h->hop_limit = t->parms.hop_limit;
1108 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001109 ipv6h->saddr = fl6->saddr;
1110 ipv6h->daddr = fl6->daddr;
Cong Wange8f72ea2013-03-09 23:00:39 +00001111 ip6tunnel_xmit(skb, dev);
Eric Dumazet89b02122011-07-28 04:32:25 +00001112 if (ndst)
1113 ip6_tnl_dst_store(t, ndst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return 0;
1115tx_err_link_failure:
1116 stats->tx_carrier_errors++;
1117 dst_link_failure(skb);
1118tx_err_dst_release:
Eric Dumazet89b02122011-07-28 04:32:25 +00001119 dst_release(ndst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001120 return err;
1121}
1122
1123static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001124ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1125{
1126 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001127 const struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001128 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001129 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001130 __u8 dsfield;
1131 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001132 u8 tproto;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001133 int err;
1134
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001135 tproto = ACCESS_ONCE(t->parms.proto);
Steffen Klassertd5005142014-11-05 08:02:48 +01001136 if (tproto != IPPROTO_IPIP && tproto != 0)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001137 return -1;
1138
1139 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1140 encap_limit = t->parms.encap_limit;
1141
Ian Morris67ba4152014-08-24 21:53:10 +01001142 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
David S. Miller4c9483b2011-03-12 16:22:43 -05001143 fl6.flowi6_proto = IPPROTO_IPIP;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001144
1145 dsfield = ipv4_get_dsfield(iph);
1146
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001147 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001148 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
Al Virob77f2fa2007-07-21 19:09:41 -07001149 & IPV6_TCLASS_MASK;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001150 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1151 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001152
David S. Miller4c9483b2011-03-12 16:22:43 -05001153 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001154 if (err != 0) {
1155 /* XXX: send ICMP error even if DF is not set. */
1156 if (err == -EMSGSIZE)
1157 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1158 htonl(mtu));
1159 return -1;
1160 }
1161
1162 return 0;
1163}
1164
1165static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001166ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1167{
1168 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001169 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001170 int encap_limit = -1;
1171 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001172 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001173 __u8 dsfield;
1174 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001175 u8 tproto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001176 int err;
1177
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001178 tproto = ACCESS_ONCE(t->parms.proto);
1179 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
Steffen Klassertd5005142014-11-05 08:02:48 +01001180 ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001181 return -1;
1182
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001183 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001184 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001185 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001186 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001187 if (tel->encap_limit == 0) {
1188 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001189 ICMPV6_HDR_FIELD, offset + 2);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001190 return -1;
1191 }
1192 encap_limit = tel->encap_limit - 1;
1193 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1194 encap_limit = t->parms.encap_limit;
1195
Ian Morris67ba4152014-08-24 21:53:10 +01001196 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
David S. Miller4c9483b2011-03-12 16:22:43 -05001197 fl6.flowi6_proto = IPPROTO_IPV6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001198
1199 dsfield = ipv6_get_dsfield(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001200 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001201 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001202 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
Florent Fourcot3308de22013-12-08 15:47:00 +01001203 fl6.flowlabel |= ip6_flowlabel(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001204 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1205 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001206
David S. Miller4c9483b2011-03-12 16:22:43 -05001207 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001208 if (err != 0) {
1209 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001210 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001211 return -1;
1212 }
1213
1214 return 0;
1215}
1216
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001217static netdev_tx_t
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001218ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1219{
1220 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001221 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001222 int ret;
1223
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001224 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001225 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001226 ret = ip4ip6_tnl_xmit(skb, dev);
1227 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001228 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001229 ret = ip6ip6_tnl_xmit(skb, dev);
1230 break;
1231 default:
1232 goto tx_err;
1233 }
1234
1235 if (ret < 0)
1236 goto tx_err;
1237
Patrick McHardy6ed10652009-06-23 06:03:08 +00001238 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240tx_err:
1241 stats->tx_errors++;
1242 stats->tx_dropped++;
1243 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001244 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001247static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001250 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001251 struct flowi6 *fl6 = &t->fl.u.ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001253 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1254 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001257 fl6->saddr = p->laddr;
1258 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001259 fl6->flowi6_oif = p->link;
1260 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001263 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001265 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001267 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1268 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1271 dev->flags |= IFF_POINTOPOINT;
1272 else
1273 dev->flags &= ~IFF_POINTOPOINT;
1274
1275 dev->iflink = p->link;
1276
1277 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001278 int strict = (ipv6_addr_type(&p->raddr) &
1279 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1280
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001281 struct rt6_info *rt = rt6_lookup(t->net,
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001282 &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001283 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (rt == NULL)
1286 return;
1287
David S. Millerd1918542011-12-28 20:19:20 -05001288 if (rt->dst.dev) {
1289 dev->hard_header_len = rt->dst.dev->hard_header_len +
Ian Morris67ba4152014-08-24 21:53:10 +01001290 sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Ian Morris67ba4152014-08-24 21:53:10 +01001292 dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001293 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Ian Morris67ba4152014-08-24 21:53:10 +01001294 dev->mtu -= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (dev->mtu < IPV6_MIN_MTU)
1297 dev->mtu = IPV6_MIN_MTU;
1298 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001299 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301}
1302
1303/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001304 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 * @t: tunnel to be changed
1306 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 *
1308 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001309 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 **/
1311
1312static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001313ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001315 t->parms.laddr = p->laddr;
1316 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 t->parms.flags = p->flags;
1318 t->parms.hop_limit = p->hop_limit;
1319 t->parms.encap_limit = p->encap_limit;
1320 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001321 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001322 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001323 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001324 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
1327
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001328static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1329{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001330 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001331 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1332 int err;
1333
1334 ip6_tnl_unlink(ip6n, t);
1335 synchronize_net();
1336 err = ip6_tnl_change(t, p);
1337 ip6_tnl_link(ip6n, t);
1338 netdev_state_change(t->dev);
1339 return err;
1340}
1341
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001342static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1343{
1344 /* for default tnl0 device allow to change only the proto */
1345 t->parms.proto = p->proto;
1346 netdev_state_change(t->dev);
1347 return 0;
1348}
1349
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001350static void
1351ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1352{
1353 p->laddr = u->laddr;
1354 p->raddr = u->raddr;
1355 p->flags = u->flags;
1356 p->hop_limit = u->hop_limit;
1357 p->encap_limit = u->encap_limit;
1358 p->flowinfo = u->flowinfo;
1359 p->link = u->link;
1360 p->proto = u->proto;
1361 memcpy(p->name, u->name, sizeof(u->name));
1362}
1363
1364static void
1365ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1366{
1367 u->laddr = p->laddr;
1368 u->raddr = p->raddr;
1369 u->flags = p->flags;
1370 u->hop_limit = p->hop_limit;
1371 u->encap_limit = p->encap_limit;
1372 u->flowinfo = p->flowinfo;
1373 u->link = p->link;
1374 u->proto = p->proto;
1375 memcpy(u->name, p->name, sizeof(u->name));
1376}
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001379 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * @dev: virtual device associated with tunnel
1381 * @ifr: parameters passed from userspace
1382 * @cmd: command to be performed
1383 *
1384 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001385 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001386 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 *
1388 * The possible commands are the following:
1389 * %SIOCGETTUNNEL: get tunnel parameters for device
1390 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1391 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1392 * %SIOCDELTUNNEL: delete tunnel
1393 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001394 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * initialization, can be used for creating other tunnel devices.
1396 *
1397 * Return:
1398 * 0 on success,
1399 * %-EFAULT if unable to copy data to or from userspace,
1400 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1401 * %-EINVAL if passed tunnel parameters are invalid,
1402 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1403 * %-ENODEV if attempting to change or delete a nonexisting device
1404 **/
1405
1406static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001407ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001411 struct __ip6_tnl_parm p1;
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001412 struct ip6_tnl *t = netdev_priv(dev);
1413 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001414 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 switch (cmd) {
1417 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001418 if (dev == ip6n->fb_tnl_dev) {
Ian Morris67ba4152014-08-24 21:53:10 +01001419 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 err = -EFAULT;
1421 break;
1422 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001423 ip6_tnl_parm_from_user(&p1, &p);
1424 t = ip6_tnl_locate(net, &p1, 0);
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001425 if (t == NULL)
1426 t = netdev_priv(dev);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001427 } else {
1428 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001429 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001430 ip6_tnl_parm_to_user(&p, &t->parms);
Ian Morris67ba4152014-08-24 21:53:10 +01001431 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 err = -EFAULT;
1433 }
1434 break;
1435 case SIOCADDTUNNEL:
1436 case SIOCCHGTUNNEL:
1437 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001438 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001440 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001441 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001443 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001444 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1445 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001447 ip6_tnl_parm_from_user(&p1, &p);
1448 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001449 if (cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001450 if (t != NULL) {
1451 if (t->dev != dev) {
1452 err = -EEXIST;
1453 break;
1454 }
1455 } else
1456 t = netdev_priv(dev);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001457 if (dev == ip6n->fb_tnl_dev)
1458 err = ip6_tnl0_update(t, &p1);
1459 else
1460 err = ip6_tnl_update(t, &p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001462 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001464 ip6_tnl_parm_to_user(&p, &t->parms);
1465 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001466 err = -EFAULT;
1467
1468 } else
1469 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471 case SIOCDELTUNNEL:
1472 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001473 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001476 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001477 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001478 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001480 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001481 ip6_tnl_parm_from_user(&p1, &p);
1482 t = ip6_tnl_locate(net, &p1, 0);
1483 if (t == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001485 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001486 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001488 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001490 err = 0;
1491 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 break;
1493 default:
1494 err = -EINVAL;
1495 }
1496 return err;
1497}
1498
1499/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001500 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 * @dev: virtual device associated with tunnel
1502 * @new_mtu: the new mtu
1503 *
1504 * Return:
1505 * 0 on success,
1506 * %-EINVAL if mtu too small
1507 **/
1508
1509static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001510ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001512 struct ip6_tnl *tnl = netdev_priv(dev);
1513
1514 if (tnl->parms.proto == IPPROTO_IPIP) {
1515 if (new_mtu < 68)
1516 return -EINVAL;
1517 } else {
1518 if (new_mtu < IPV6_MIN_MTU)
1519 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001521 if (new_mtu > 0xFFF8 - dev->hard_header_len)
1522 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 dev->mtu = new_mtu;
1524 return 0;
1525}
1526
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001527
1528static const struct net_device_ops ip6_tnl_netdev_ops = {
Eric Dumazet8560f222010-09-28 03:23:34 +00001529 .ndo_uninit = ip6_tnl_dev_uninit,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001530 .ndo_start_xmit = ip6_tnl_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001531 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001532 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001533 .ndo_get_stats = ip6_get_stats,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001534};
1535
Eric Dumazet8560f222010-09-28 03:23:34 +00001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001538 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * @dev: virtual device associated with tunnel
1540 *
1541 * Description:
1542 * Initialize function pointers and device parameters
1543 **/
1544
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001545static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Anders Franzen381601e2010-11-24 05:47:18 +00001547 struct ip6_tnl *t;
1548
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001549 dev->netdev_ops = &ip6_tnl_netdev_ops;
Eric Dumazet8560f222010-09-28 03:23:34 +00001550 dev->destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 dev->type = ARPHRD_TUNNEL6;
Ian Morris67ba4152014-08-24 21:53:10 +01001553 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1554 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001555 t = netdev_priv(dev);
1556 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Ian Morris67ba4152014-08-24 21:53:10 +01001557 dev->mtu -= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 dev->flags |= IFF_NOARP;
1559 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001560 netif_keep_dst(dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02001561 /* This perm addr will be used as interface identifier by IPv6 */
1562 dev->addr_assign_type = NET_ADDR_RANDOM;
1563 eth_random_addr(dev->perm_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566
1567/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001568 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * @dev: virtual device associated with tunnel
1570 **/
1571
Eric Dumazet8560f222010-09-28 03:23:34 +00001572static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001573ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Patrick McHardy2941a482006-01-08 22:05:26 -08001575 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 t->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001578 t->net = dev_net(dev);
WANG Cong1c213bd2014-02-13 11:46:28 -08001579 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Eric Dumazet8560f222010-09-28 03:23:34 +00001580 if (!dev->tstats)
1581 return -ENOMEM;
1582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001586 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * @dev: virtual device associated with tunnel
1588 **/
1589
Eric Dumazet8560f222010-09-28 03:23:34 +00001590static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Patrick McHardy2941a482006-01-08 22:05:26 -08001592 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001593 int err = ip6_tnl_dev_init_gen(dev);
1594
1595 if (err)
1596 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001597 ip6_tnl_link_config(t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001602 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * @dev: fallback device
1604 *
1605 * Return: 0
1606 **/
1607
Eric Dumazet8560f222010-09-28 03:23:34 +00001608static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Patrick McHardy2941a482006-01-08 22:05:26 -08001610 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001611 struct net *net = dev_net(dev);
1612 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001613 int err = ip6_tnl_dev_init_gen(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001614
Eric Dumazet8560f222010-09-28 03:23:34 +00001615 if (err)
1616 return err;
1617
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001618 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001620
1621 ip6_tnl_link_config(t);
1622
Eric Dumazetcf778b02012-01-12 04:41:32 +00001623 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001627static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1628{
1629 u8 proto;
1630
Susant Sahanic8965932014-05-10 00:11:32 +05301631 if (!data || !data[IFLA_IPTUN_PROTO])
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001632 return 0;
1633
1634 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1635 if (proto != IPPROTO_IPV6 &&
1636 proto != IPPROTO_IPIP &&
1637 proto != 0)
1638 return -EINVAL;
1639
1640 return 0;
1641}
1642
1643static void ip6_tnl_netlink_parms(struct nlattr *data[],
1644 struct __ip6_tnl_parm *parms)
1645{
1646 memset(parms, 0, sizeof(*parms));
1647
1648 if (!data)
1649 return;
1650
1651 if (data[IFLA_IPTUN_LINK])
1652 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1653
1654 if (data[IFLA_IPTUN_LOCAL])
1655 nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL],
1656 sizeof(struct in6_addr));
1657
1658 if (data[IFLA_IPTUN_REMOTE])
1659 nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE],
1660 sizeof(struct in6_addr));
1661
1662 if (data[IFLA_IPTUN_TTL])
1663 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1664
1665 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1666 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1667
1668 if (data[IFLA_IPTUN_FLOWINFO])
Nicolas Dichtel1ff05fb2012-11-15 04:06:42 +00001669 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001670
1671 if (data[IFLA_IPTUN_FLAGS])
1672 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1673
1674 if (data[IFLA_IPTUN_PROTO])
1675 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1676}
1677
1678static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1679 struct nlattr *tb[], struct nlattr *data[])
1680{
1681 struct net *net = dev_net(dev);
1682 struct ip6_tnl *nt;
1683
1684 nt = netdev_priv(dev);
1685 ip6_tnl_netlink_parms(data, &nt->parms);
1686
1687 if (ip6_tnl_locate(net, &nt->parms, 0))
1688 return -EEXIST;
1689
1690 return ip6_tnl_create2(dev);
1691}
1692
1693static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1694 struct nlattr *data[])
1695{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001696 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001697 struct __ip6_tnl_parm p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001698 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001699 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1700
1701 if (dev == ip6n->fb_tnl_dev)
1702 return -EINVAL;
1703
1704 ip6_tnl_netlink_parms(data, &p);
1705
1706 t = ip6_tnl_locate(net, &p, 0);
1707
1708 if (t) {
1709 if (t->dev != dev)
1710 return -EEXIST;
1711 } else
1712 t = netdev_priv(dev);
1713
1714 return ip6_tnl_update(t, &p);
1715}
1716
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001717static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1718{
1719 struct net *net = dev_net(dev);
1720 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1721
1722 if (dev != ip6n->fb_tnl_dev)
1723 unregister_netdevice_queue(dev, head);
1724}
1725
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001726static size_t ip6_tnl_get_size(const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001727{
1728 return
1729 /* IFLA_IPTUN_LINK */
1730 nla_total_size(4) +
1731 /* IFLA_IPTUN_LOCAL */
1732 nla_total_size(sizeof(struct in6_addr)) +
1733 /* IFLA_IPTUN_REMOTE */
1734 nla_total_size(sizeof(struct in6_addr)) +
1735 /* IFLA_IPTUN_TTL */
1736 nla_total_size(1) +
1737 /* IFLA_IPTUN_ENCAP_LIMIT */
1738 nla_total_size(1) +
1739 /* IFLA_IPTUN_FLOWINFO */
1740 nla_total_size(4) +
1741 /* IFLA_IPTUN_FLAGS */
1742 nla_total_size(4) +
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001743 /* IFLA_IPTUN_PROTO */
1744 nla_total_size(1) +
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001745 0;
1746}
1747
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001748static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001749{
1750 struct ip6_tnl *tunnel = netdev_priv(dev);
1751 struct __ip6_tnl_parm *parm = &tunnel->parms;
1752
1753 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1754 nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001755 &parm->laddr) ||
Ding Zhi0d2ede92013-09-16 11:31:15 +02001756 nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
1757 &parm->raddr) ||
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001758 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1759 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1760 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001761 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1762 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001763 goto nla_put_failure;
1764 return 0;
1765
1766nla_put_failure:
1767 return -EMSGSIZE;
1768}
1769
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001770static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1771 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1772 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1773 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1774 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1775 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1776 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1777 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1778 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1779};
1780
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001781static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1782 .kind = "ip6tnl",
1783 .maxtype = IFLA_IPTUN_MAX,
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001784 .policy = ip6_tnl_policy,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001785 .priv_size = sizeof(struct ip6_tnl),
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001786 .setup = ip6_tnl_dev_setup,
1787 .validate = ip6_tnl_validate,
1788 .newlink = ip6_tnl_newlink,
1789 .changelink = ip6_tnl_changelink,
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001790 .dellink = ip6_tnl_dellink,
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001791 .get_size = ip6_tnl_get_size,
1792 .fill_info = ip6_tnl_fill_info,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001793};
1794
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001795static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001796 .handler = ip4ip6_rcv,
1797 .err_handler = ip4ip6_err,
1798 .priority = 1,
1799};
1800
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001801static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07001802 .handler = ip6ip6_rcv,
1803 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001804 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805};
1806
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001807static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001808{
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001809 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001810 struct net_device *dev, *aux;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001811 int h;
1812 struct ip6_tnl *t;
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001813 LIST_HEAD(list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001814
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001815 for_each_netdev_safe(net, dev, aux)
1816 if (dev->rtnl_link_ops == &ip6_link_ops)
1817 unregister_netdevice_queue(dev, &list);
1818
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001819 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00001820 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001821 while (t != NULL) {
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001822 /* If dev is in the same netns, it has already
1823 * been added to the list by the previous loop.
1824 */
1825 if (!net_eq(dev_net(t->dev), net))
1826 unregister_netdevice_queue(t->dev, &list);
Eric Dumazet94767632010-09-15 20:25:34 +00001827 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001828 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001829 }
1830
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001831 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001832}
1833
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001834static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001835{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001836 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00001837 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001838 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001839
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001840 ip6n->tnls[0] = ip6n->tnls_wc;
1841 ip6n->tnls[1] = ip6n->tnls_r_l;
1842
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001843 err = -ENOMEM;
1844 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001845 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001846
1847 if (!ip6n->fb_tnl_dev)
1848 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001849 dev_net_set(ip6n->fb_tnl_dev, net);
Nicolas Dichtelbb814092013-10-01 18:05:00 +02001850 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001851 /* FB netdevice is special: we have one, and only one per netns.
1852 * Allowing to move it to another netns is clearly unsafe.
1853 */
1854 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001855
Eric Dumazet8560f222010-09-28 03:23:34 +00001856 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1857 if (err < 0)
1858 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001859
1860 err = register_netdev(ip6n->fb_tnl_dev);
1861 if (err < 0)
1862 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00001863
1864 t = netdev_priv(ip6n->fb_tnl_dev);
1865
1866 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001867 return 0;
1868
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001869err_register:
Eric Dumazet8560f222010-09-28 03:23:34 +00001870 ip6_dev_free(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001871err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001872 return err;
1873}
1874
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001875static void __net_exit ip6_tnl_exit_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001876{
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001877 rtnl_lock();
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001878 ip6_tnl_destroy_tunnels(net);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001879 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001880}
1881
1882static struct pernet_operations ip6_tnl_net_ops = {
1883 .init = ip6_tnl_init_net,
1884 .exit = ip6_tnl_exit_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001885 .id = &ip6_tnl_net_id,
1886 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001887};
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889/**
1890 * ip6_tunnel_init - register protocol and reserve needed resources
1891 *
1892 * Return: 0 on success
1893 **/
1894
1895static int __init ip6_tunnel_init(void)
1896{
1897 int err;
1898
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001899 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001900 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001901 goto out_pernet;
1902
1903 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1904 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001905 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001906 goto out_ip4ip6;
1907 }
1908
1909 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1910 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001911 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001912 goto out_ip6ip6;
1913 }
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001914 err = rtnl_link_register(&ip6_link_ops);
1915 if (err < 0)
1916 goto rtnl_link_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001919
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001920rtnl_link_failed:
1921 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001922out_ip6ip6:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001923 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001924out_ip4ip6:
1925 unregister_pernet_device(&ip6_tnl_net_ops);
1926out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return err;
1928}
1929
1930/**
1931 * ip6_tunnel_cleanup - free resources and unregister protocol
1932 **/
1933
1934static void __exit ip6_tunnel_cleanup(void)
1935{
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001936 rtnl_link_unregister(&ip6_link_ops);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001937 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00001938 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001939
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001940 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00001941 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001943 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946module_init(ip6_tunnel_init);
1947module_exit(ip6_tunnel_cleanup);