blob: 851cf6d1eb45a76cfa9f6c04255ba0344c76ebb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09002 * IPv6 tunneling device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Ville Nuorvala <vnuorval@tcs.hut.fi>
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09007 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Based on:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +090010 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * RFC 2473
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 */
20
Joe Perchesf3213832012-05-15 14:11:53 +000021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080024#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/types.h>
27#include <linux/sockios.h>
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +090028#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/if.h>
30#include <linux/in.h>
31#include <linux/ip.h>
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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-02-15 00:43:16 +090062MODULE_DESCRIPTION("IPv6 tunneling device");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_LICENSE("GPL");
Tom Gundersenf98f89a2014-05-15 23:21:30 +020064MODULE_ALIAS_RTNL_LINK("ip6tnl");
stephen hemminger6dfbd872011-03-10 11:43:19 +000065MODULE_ALIAS_NETDEV("ip6tnl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric Dumazetddbe5032012-07-18 08:11:12 +000067#define HASH_SIZE_SHIFT 5
68#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +000070static bool log_ecn_error = true;
71module_param(log_ecn_error, bool, 0644);
72MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
73
Eric Dumazetddbe5032012-07-18 08:11:12 +000074static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
75{
76 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
77
78 return hash_32(hash, HASH_SIZE_SHIFT);
79}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Eric Dumazet8560f222010-09-28 03:23:34 +000081static int ip6_tnl_dev_init(struct net_device *dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090082static void ip6_tnl_dev_setup(struct net_device *dev);
Nicolas Dichtelc075b132012-11-09 06:10:01 +000083static struct rtnl_link_ops ip6_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Eric Dumazetf99189b2009-11-17 10:42:49 +000085static int ip6_tnl_net_id __read_mostly;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070086struct ip6_tnl_net {
Pavel Emelyanov15820e1292008-04-16 01:23:02 -070087 /* the IPv6 tunnel fallback device */
88 struct net_device *fb_tnl_dev;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -070089 /* lists for storing tunnels in use */
Eric Dumazet94767632010-09-15 20:25:34 +000090 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
91 struct ip6_tnl __rcu *tnls_wc[1];
92 struct ip6_tnl __rcu **tnls[2];
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070093};
94
Eric Dumazet8560f222010-09-28 03:23:34 +000095static struct net_device_stats *ip6_get_stats(struct net_device *dev)
96{
David S. Miller56a43422014-01-06 17:37:45 -050097 struct pcpu_sw_netstats tmp, sum = { 0 };
Eric Dumazet8560f222010-09-28 03:23:34 +000098 int i;
99
100 for_each_possible_cpu(i) {
Li RongQingabb60132014-01-02 13:20:12 +0800101 unsigned int start;
Li RongQing8f849852014-01-04 13:57:59 +0800102 const struct pcpu_sw_netstats *tstats =
103 per_cpu_ptr(dev->tstats, i);
Eric Dumazet8560f222010-09-28 03:23:34 +0000104
Li RongQingabb60132014-01-02 13:20:12 +0800105 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700106 start = u64_stats_fetch_begin_irq(&tstats->syncp);
Li RongQingabb60132014-01-02 13:20:12 +0800107 tmp.rx_packets = tstats->rx_packets;
108 tmp.rx_bytes = tstats->rx_bytes;
109 tmp.tx_packets = tstats->tx_packets;
110 tmp.tx_bytes = tstats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700111 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
Li RongQingabb60132014-01-02 13:20:12 +0800112
113 sum.rx_packets += tmp.rx_packets;
114 sum.rx_bytes += tmp.rx_bytes;
115 sum.tx_packets += tmp.tx_packets;
116 sum.tx_bytes += tmp.tx_bytes;
Eric Dumazet8560f222010-09-28 03:23:34 +0000117 }
118 dev->stats.rx_packets = sum.rx_packets;
119 dev->stats.rx_bytes = sum.rx_bytes;
120 dev->stats.tx_packets = sum.tx_packets;
121 dev->stats.tx_bytes = sum.tx_bytes;
122 return &dev->stats;
123}
124
Eric Dumazet2922bc82009-10-23 06:34:34 +0000125/*
Eric Dumazet94767632010-09-15 20:25:34 +0000126 * Locking : hash tables are protected by RCU and RTNL
Eric Dumazet2922bc82009-10-23 06:34:34 +0000127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700129static void __ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst,
130 struct dst_entry *dst)
131{
132 dst_release(idst->dst);
133 if (dst) {
134 dst_hold(dst);
135 idst->cookie = rt6_get_cookie((struct rt6_info *)dst);
136 } else {
137 idst->cookie = 0;
138 }
139 idst->dst = dst;
140}
141
142static void ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst,
143 struct dst_entry *dst)
144{
145
146 spin_lock_bh(&idst->lock);
147 __ip6_tnl_per_cpu_dst_set(idst, dst);
148 spin_unlock_bh(&idst->lock);
149}
150
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700151struct dst_entry *ip6_tnl_dst_get(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700153 struct ip6_tnl_dst *idst;
154 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700156 idst = raw_cpu_ptr(t->dst_cache);
157 spin_lock_bh(&idst->lock);
158 dst = idst->dst;
159 if (dst) {
160 if (!dst->obsolete || dst->ops->check(dst, idst->cookie)) {
161 dst_hold(idst->dst);
162 } else {
163 __ip6_tnl_per_cpu_dst_set(idst, NULL);
164 dst = NULL;
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700167 spin_unlock_bh(&idst->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return dst;
169}
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700170EXPORT_SYMBOL_GPL(ip6_tnl_dst_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000172void ip6_tnl_dst_reset(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700174 int i;
175
176 for_each_possible_cpu(i)
177 ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000179EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700181void ip6_tnl_dst_set(struct ip6_tnl *t, struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700183 ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), dst);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -0700186EXPORT_SYMBOL_GPL(ip6_tnl_dst_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700188void ip6_tnl_dst_destroy(struct ip6_tnl *t)
189{
190 if (!t->dst_cache)
191 return;
192
193 ip6_tnl_dst_reset(t);
194 free_percpu(t->dst_cache);
195}
196EXPORT_SYMBOL_GPL(ip6_tnl_dst_destroy);
197
198int ip6_tnl_dst_init(struct ip6_tnl *t)
199{
200 int i;
201
202 t->dst_cache = alloc_percpu(struct ip6_tnl_dst);
203 if (!t->dst_cache)
204 return -ENOMEM;
205
206 for_each_possible_cpu(i)
207 spin_lock_init(&per_cpu_ptr(t->dst_cache, i)->lock);
208
209 return 0;
210}
211EXPORT_SYMBOL_GPL(ip6_tnl_dst_init);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900214 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900215 * @remote: the address of the tunnel exit-point
216 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900218 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900220 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * else %NULL
222 **/
223
Eric Dumazet2922bc82009-10-23 06:34:34 +0000224#define for_each_ip6_tunnel_rcu(start) \
225 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static struct ip6_tnl *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000228ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000230 unsigned int hash = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct ip6_tnl *t;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700232 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Steffen Klassertea3dc962014-11-05 08:03:50 +0100233 struct in6_addr any;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Eric Dumazetddbe5032012-07-18 08:11:12 +0000235 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (ipv6_addr_equal(local, &t->parms.laddr) &&
237 ipv6_addr_equal(remote, &t->parms.raddr) &&
238 (t->dev->flags & IFF_UP))
239 return t;
240 }
Steffen Klassertea3dc962014-11-05 08:03:50 +0100241
242 memset(&any, 0, sizeof(any));
243 hash = HASH(&any, local);
244 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
245 if (ipv6_addr_equal(local, &t->parms.laddr) &&
246 (t->dev->flags & IFF_UP))
247 return t;
248 }
249
250 hash = HASH(remote, &any);
251 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
252 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
253 (t->dev->flags & IFF_UP))
254 return t;
255 }
256
Eric Dumazet2922bc82009-10-23 06:34:34 +0000257 t = rcu_dereference(ip6n->tnls_wc[0]);
258 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return t;
260
261 return NULL;
262}
263
264/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900265 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900266 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *
268 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900269 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * &struct in6_addr entries laddr and raddr in @p.
271 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900272 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 **/
274
Eric Dumazet94767632010-09-15 20:25:34 +0000275static struct ip6_tnl __rcu **
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000276ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000278 const struct in6_addr *remote = &p->raddr;
279 const struct in6_addr *local = &p->laddr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000280 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int prio = 0;
282
283 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
284 prio = 1;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000285 h = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700287 return &ip6n->tnls[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900291 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * @t: tunnel to be added
293 **/
294
295static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700296ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Eric Dumazet94767632010-09-15 20:25:34 +0000298 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Eric Dumazetcf778b02012-01-12 04:41:32 +0000300 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
301 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900305 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * @t: tunnel to be removed
307 **/
308
309static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700310ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Eric Dumazet94767632010-09-15 20:25:34 +0000312 struct ip6_tnl __rcu **tp;
313 struct ip6_tnl *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Eric Dumazet94767632010-09-15 20:25:34 +0000315 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
316 (iter = rtnl_dereference(*tp)) != NULL;
317 tp = &iter->next) {
318 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000319 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
321 }
322 }
323}
324
Eric Dumazet8560f222010-09-28 03:23:34 +0000325static void ip6_dev_free(struct net_device *dev)
326{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700327 struct ip6_tnl *t = netdev_priv(dev);
328
329 ip6_tnl_dst_destroy(t);
Eric Dumazet8560f222010-09-28 03:23:34 +0000330 free_percpu(dev->tstats);
331 free_netdev(dev);
332}
333
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000334static int ip6_tnl_create2(struct net_device *dev)
335{
336 struct ip6_tnl *t = netdev_priv(dev);
337 struct net *net = dev_net(dev);
338 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
339 int err;
340
341 t = netdev_priv(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000342
343 err = register_netdevice(dev);
344 if (err < 0)
345 goto out;
346
347 strcpy(t->parms.name, dev->name);
348 dev->rtnl_link_ops = &ip6_link_ops;
349
350 dev_hold(dev);
351 ip6_tnl_link(ip6n, t);
352 return 0;
353
354out:
355 return err;
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000359 * ip6_tnl_create - create a new tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @p: tunnel parameters
361 * @pt: pointer to new tunnel
362 *
363 * Description:
364 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900365 *
366 * Return:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100367 * created tunnel or error pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 **/
369
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000370static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct net_device *dev;
373 struct ip6_tnl *t;
374 char name[IFNAMSIZ];
Nicolas Dichtel37355562015-03-16 15:56:05 +0100375 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800377 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800379 else
380 sprintf(name, "ip6tnl%%d");
381
Tom Gundersenc835a672014-07-14 16:37:24 +0200382 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
383 ip6_tnl_dev_setup);
Ian Morris63159f22015-03-29 14:00:04 +0100384 if (!dev)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800385 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Pavel Emelyanov554eb272008-04-16 01:24:13 -0700387 dev_net_set(dev, net);
388
Patrick McHardy2941a482006-01-08 22:05:26 -0800389 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 t->parms = *p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200391 t->net = dev_net(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000392 err = ip6_tnl_create2(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +0000393 if (err < 0)
394 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ville Nuorvala567131a2006-11-24 17:05:41 -0800396 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800397
398failed_free:
Eric Dumazet8560f222010-09-28 03:23:34 +0000399 ip6_dev_free(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800400failed:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100401 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900405 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900406 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * @create: != 0 if allowed to create new tunnel if no match found
408 *
409 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900410 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * based on @parms. If this is unsuccessful, but @create is set a new
412 * tunnel device is created and registered for use.
413 *
414 * Return:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100415 * matching tunnel or error pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 **/
417
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700418static struct ip6_tnl *ip6_tnl_locate(struct net *net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000419 struct __ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000421 const struct in6_addr *remote = &p->raddr;
422 const struct in6_addr *local = &p->laddr;
Eric Dumazet94767632010-09-15 20:25:34 +0000423 struct ip6_tnl __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700425 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Eric Dumazet94767632010-09-15 20:25:34 +0000427 for (tp = ip6_tnl_bucket(ip6n, p);
428 (t = rtnl_dereference(*tp)) != NULL;
429 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200431 ipv6_addr_equal(remote, &t->parms.raddr)) {
432 if (create)
Nicolas Dichtel37355562015-03-16 15:56:05 +0100433 return ERR_PTR(-EEXIST);
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200434
Ville Nuorvala567131a2006-11-24 17:05:41 -0800435 return t;
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 if (!create)
Nicolas Dichtel37355562015-03-16 15:56:05 +0100439 return ERR_PTR(-ENODEV);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700440 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900444 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900446 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900448 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 **/
450
451static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900452ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Patrick McHardy2941a482006-01-08 22:05:26 -0800454 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200455 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700456 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Eric Dumazet94767632010-09-15 20:25:34 +0000458 if (dev == ip6n->fb_tnl_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000459 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
Eric Dumazet94767632010-09-15 20:25:34 +0000460 else
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700461 ip6_tnl_unlink(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 ip6_tnl_dst_reset(t);
463 dev_put(dev);
464}
465
466/**
467 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
468 * @skb: received socket buffer
469 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900470 * Return:
471 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * else index to encapsulation limit
473 **/
474
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000475__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000477 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 __u8 nexthdr = ipv6h->nexthdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100479 __u16 off = sizeof(*ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
482 __u16 optlen = 0;
483 struct ipv6_opt_hdr *hdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100484 if (raw + off + sizeof(*hdr) > skb->data &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
486 break;
487
488 hdr = (struct ipv6_opt_hdr *) (raw + off);
489 if (nexthdr == NEXTHDR_FRAGMENT) {
490 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
491 if (frag_hdr->frag_off)
492 break;
493 optlen = 8;
494 } else if (nexthdr == NEXTHDR_AUTH) {
495 optlen = (hdr->hdrlen + 2) << 2;
496 } else {
497 optlen = ipv6_optlen(hdr);
498 }
499 if (nexthdr == NEXTHDR_DEST) {
500 __u16 i = off + 2;
501 while (1) {
502 struct ipv6_tlv_tnl_enc_lim *tel;
503
504 /* No more room for encapsulation limit */
505 if (i + sizeof (*tel) > off + optlen)
506 break;
507
508 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
509 /* return index of option if found and valid */
510 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
511 tel->length == 1)
512 return i;
513 /* else jump to next option */
514 if (tel->type)
515 i += tel->length + 2;
516 else
517 i++;
518 }
519 }
520 nexthdr = hdr->nexthdr;
521 off += optlen;
522 }
523 return 0;
524}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000525EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900528 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 *
530 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900531 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * to the specifications in RFC 2473.
533 **/
534
Herbert Xud2acc342006-03-28 01:12:13 -0800535static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900536ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700537 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000539 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct ip6_tnl *t;
541 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700542 u8 rel_type = ICMPV6_DEST_UNREACH;
543 u8 rel_code = ICMPV6_ADDR_UNREACH;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300544 u8 tproto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 __u32 rel_info = 0;
546 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800547 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900549 /* If the packet doesn't contain the original IPv6 header we are
550 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 processing of the error. */
552
Eric Dumazet2922bc82009-10-23 06:34:34 +0000553 rcu_read_lock();
Ian Morrise5d08d72014-11-23 21:28:43 +0000554 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
Ian Morris63159f22015-03-29 14:00:04 +0100555 if (!t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto out;
557
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300558 tproto = ACCESS_ONCE(t->parms.proto);
559 if (tproto != ipproto && tproto != 0)
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900560 goto out;
561
Herbert Xud2acc342006-03-28 01:12:13 -0800562 err = 0;
563
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900564 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 __u32 teli;
566 struct ipv6_tlv_tnl_enc_lim *tel;
567 __u32 mtu;
568 case ICMPV6_DEST_UNREACH:
Joe Perchese87cc472012-05-13 21:56:26 +0000569 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
570 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 rel_msg = 1;
572 break;
573 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900574 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Joe Perchese87cc472012-05-13 21:56:26 +0000575 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
576 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 rel_msg = 1;
578 }
579 break;
580 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800581 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900582 if ((*code) == ICMPV6_HDR_FIELD)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000583 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Al Viro704eae12007-07-26 17:33:29 +0100585 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
587 if (tel->encap_limit == 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000588 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
589 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 rel_msg = 1;
591 }
Joe Perchese87cc472012-05-13 21:56:26 +0000592 } else {
593 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
594 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 break;
597 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100598 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (mtu < IPV6_MIN_MTU)
600 mtu = IPV6_MIN_MTU;
601 t->dev->mtu = mtu;
602
Ian Morrise5d08d72014-11-23 21:28:43 +0000603 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
604 if (len > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 rel_type = ICMPV6_PKT_TOOBIG;
606 rel_code = 0;
607 rel_info = mtu;
608 rel_msg = 1;
609 }
610 break;
611 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900612
613 *type = rel_type;
614 *code = rel_code;
615 *info = rel_info;
616 *msg = rel_msg;
617
618out:
Eric Dumazet2922bc82009-10-23 06:34:34 +0000619 rcu_read_unlock();
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900620 return err;
621}
622
623static int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900624ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700625 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900626{
627 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700628 u8 rel_type = type;
629 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100630 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900631 int err;
632 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000633 const struct iphdr *eiph;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900634 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -0700635 struct flowi4 fl4;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900636
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900637 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
638 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900639 if (err < 0)
640 return err;
641
642 if (rel_msg == 0)
643 return 0;
644
645 switch (rel_type) {
646 case ICMPV6_DEST_UNREACH:
647 if (rel_code != ICMPV6_ADDR_UNREACH)
648 return 0;
649 rel_type = ICMP_DEST_UNREACH;
650 rel_code = ICMP_HOST_UNREACH;
651 break;
652 case ICMPV6_PKT_TOOBIG:
653 if (rel_code != 0)
654 return 0;
655 rel_type = ICMP_DEST_UNREACH;
656 rel_code = ICMP_FRAG_NEEDED;
657 break;
David S. Millerec18d9a2012-07-12 00:25:15 -0700658 case NDISC_REDIRECT:
659 rel_type = ICMP_REDIRECT;
660 rel_code = ICMP_REDIR_HOST;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900661 default:
662 return 0;
663 }
664
665 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
666 return 0;
667
668 skb2 = skb_clone(skb, GFP_ATOMIC);
669 if (!skb2)
670 return 0;
671
Eric Dumazetadf30902009-06-02 05:19:30 +0000672 skb_dst_drop(skb2);
673
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900674 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700675 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700676 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900677
678 /* Try to guess incoming interface */
David S. Miller31e4543d2011-05-03 20:25:42 -0700679 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500680 eiph->saddr, 0,
681 0, 0,
682 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800683 if (IS_ERR(rt))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900684 goto out;
685
Changli Gaod8d1f302010-06-10 23:31:35 -0700686 skb2->dev = rt->dst.dev;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900687
688 /* route "incoming" packet */
689 if (rt->rt_flags & RTCF_LOCAL) {
690 ip_rt_put(rt);
691 rt = NULL;
David S. Miller31e4543d2011-05-03 20:25:42 -0700692 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500693 eiph->daddr, eiph->saddr,
694 0, 0,
695 IPPROTO_IPIP,
696 RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800697 if (IS_ERR(rt) ||
Changli Gaod8d1f302010-06-10 23:31:35 -0700698 rt->dst.dev->type != ARPHRD_TUNNEL) {
David S. Millerb23dd4f2011-03-02 14:31:35 -0800699 if (!IS_ERR(rt))
700 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900701 goto out;
702 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800703 skb_dst_set(skb2, &rt->dst);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900704 } else {
705 ip_rt_put(rt);
706 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
707 skb2->dev) ||
Eric Dumazetadf30902009-06-02 05:19:30 +0000708 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900709 goto out;
710 }
711
712 /* change mtu on this route */
713 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000714 if (rel_info > dst_mtu(skb_dst(skb2)))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900715 goto out;
716
David S. Miller6700c272012-07-17 03:29:28 -0700717 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900718 }
David S. Miller1ed5c482012-07-12 00:41:25 -0700719 if (rel_type == ICMP_REDIRECT)
David S. Miller6700c272012-07-17 03:29:28 -0700720 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900721
Al Viro704eae12007-07-26 17:33:29 +0100722 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900723
724out:
725 kfree_skb(skb2);
726 return 0;
727}
728
729static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900730ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700731 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900732{
733 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700734 u8 rel_type = type;
735 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100736 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900737 int err;
738
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900739 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
740 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900741 if (err < 0)
742 return err;
743
744 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct rt6_info *rt;
746 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Eric Dumazetadf30902009-06-02 05:19:30 +0000751 skb_dst_drop(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700753 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* Try to guess incoming interface */
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700756 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
757 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
David S. Millerd1918542011-12-28 20:19:20 -0500759 if (rt && rt->dst.dev)
760 skb2->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000762 icmpv6_send(skb2, rel_type, rel_code, rel_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Amerigo Wang94e187c2012-10-29 00:13:19 +0000764 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 kfree_skb(skb2);
767 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900768
769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000772static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
773 const struct ipv6hdr *ipv6h,
774 struct sk_buff *skb)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900775{
776 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
777
778 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700779 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900780
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000781 return IP6_ECN_decapsulate(ipv6h, skb);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900782}
783
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000784static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
785 const struct ipv6hdr *ipv6h,
786 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900788 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800789 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000791 return IP6_ECN_decapsulate(ipv6h, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900793
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000794__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000795 const struct in6_addr *laddr,
796 const struct in6_addr *raddr)
797{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000798 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000799 int ltype = ipv6_addr_type(laddr);
800 int rtype = ipv6_addr_type(raddr);
801 __u32 flags = 0;
802
803 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
804 flags = IP6_TNL_F_CAP_PER_PACKET;
805 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
806 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
807 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
808 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
809 if (ltype&IPV6_ADDR_UNICAST)
810 flags |= IP6_TNL_F_CAP_XMIT;
811 if (rtype&IPV6_ADDR_UNICAST)
812 flags |= IP6_TNL_F_CAP_RCV;
813 }
814 return flags;
815}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000816EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000817
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100818/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000819int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000820 const struct in6_addr *laddr,
821 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800822{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000823 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800824 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200825 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800826
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000827 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
828 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
829 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900830 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800831
832 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100833 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800834
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000835 if ((ipv6_addr_is_multicast(laddr) ||
836 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
837 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800838 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800839 }
840 return ret;
841}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000842EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900845 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900847 * @protocol: ethernet protocol ID
848 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 *
850 * Return: 0
851 **/
852
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900853static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900854 __u8 ipproto,
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000855 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
856 const struct ipv6hdr *ipv6h,
857 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000860 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300861 u8 tproto;
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000862 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Eric Dumazet2922bc82009-10-23 06:34:34 +0000864 rcu_read_lock();
Ian Morrise5d08d72014-11-23 21:28:43 +0000865 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
Ian Morris53b24b82015-03-29 14:00:05 +0100866 if (t) {
Li RongQing8f849852014-01-04 13:57:59 +0800867 struct pcpu_sw_netstats *tstats;
Eric Dumazet8560f222010-09-28 03:23:34 +0000868
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300869 tproto = ACCESS_ONCE(t->parms.proto);
870 if (tproto != ipproto && tproto != 0) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000871 rcu_read_unlock();
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900872 goto discard;
873 }
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000876 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700877 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000880 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700881 t->dev->stats.rx_dropped++;
Eric Dumazet2922bc82009-10-23 06:34:34 +0000882 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto discard;
884 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700885 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700886 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900887 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700889
Nicolas Dichtelea231922013-09-02 15:34:58 +0200890 __skb_tunnel_rx(skb, t->dev, t->net);
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000891
892 err = dscp_ecn_decapsulate(t, ipv6h, skb);
893 if (unlikely(err)) {
894 if (log_ecn_error)
895 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
896 &ipv6h->saddr,
897 ipv6_get_dsfield(ipv6h));
898 if (err > 1) {
899 ++t->dev->stats.rx_frame_errors;
900 ++t->dev->stats.rx_errors;
901 rcu_read_unlock();
902 goto discard;
903 }
904 }
905
Eric Dumazet8560f222010-09-28 03:23:34 +0000906 tstats = this_cpu_ptr(t->dev->tstats);
Li RongQingabb60132014-01-02 13:20:12 +0800907 u64_stats_update_begin(&tstats->syncp);
Eric Dumazet8560f222010-09-28 03:23:34 +0000908 tstats->rx_packets++;
909 tstats->rx_bytes += skb->len;
Li RongQingabb60132014-01-02 13:20:12 +0800910 u64_stats_update_end(&tstats->syncp);
Eric Dumazet8560f222010-09-28 03:23:34 +0000911
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000912 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000913
Eric Dumazet2922bc82009-10-23 06:34:34 +0000914 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000917 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700919
920discard:
921 kfree_skb(skb);
922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900925static int ip4ip6_rcv(struct sk_buff *skb)
926{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900927 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
928 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900929}
930
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900931static int ip6ip6_rcv(struct sk_buff *skb)
932{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900933 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
934 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900935}
936
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800937struct ipv6_tel_txoption {
938 struct ipv6_txoptions ops;
939 __u8 dst_opt[8];
940};
941
942static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800944 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800946 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
947 opt->dst_opt[3] = 1;
948 opt->dst_opt[4] = encap_limit;
949 opt->dst_opt[5] = IPV6_TLV_PADN;
950 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800952 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
953 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900957 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900959 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 *
961 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900962 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * doesn't match source of incoming packet.
964 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900965 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * 1 if conflict,
967 * 0 else
968 **/
969
Eric Dumazet92113bf2012-05-18 08:14:11 +0200970static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000971ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
974}
975
Steffen Klassertd5005142014-11-05 08:02:48 +0100976int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
977 const struct in6_addr *laddr,
978 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800979{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000980 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800981 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200982 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800983
Steffen Klassertd5005142014-11-05 08:02:48 +0100984 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
985 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
986 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800987 struct net_device *ldev = NULL;
988
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100989 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800990 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100991 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800992
Steffen Klassertd5005142014-11-05 08:02:48 +0100993 if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000994 pr_warn("%s xmit: Local address not yet configured!\n",
995 p->name);
Steffen Klassertd5005142014-11-05 08:02:48 +0100996 else if (!ipv6_addr_is_multicast(raddr) &&
997 unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000998 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
999 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001000 else
1001 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +01001002 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001003 }
1004 return ret;
1005}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001006EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001009 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001011 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001012 * @dsfield: dscp code for outer header
1013 * @fl: flow of tunneled packet
1014 * @encap_limit: encapsulation limit
1015 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
1017 * Description:
1018 * Build new header and do some sanity checks on the packet before sending
1019 * it.
1020 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001021 * Return:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001022 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001023 * -1 fail
1024 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 **/
1026
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001027static int ip6_tnl_xmit2(struct sk_buff *skb,
1028 struct net_device *dev,
1029 __u8 dsfield,
David S. Miller4c9483b2011-03-12 16:22:43 -05001030 struct flowi6 *fl6,
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001031 int encap_limit,
1032 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Patrick McHardy2941a482006-01-08 22:05:26 -08001034 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001035 struct net *net = t->net;
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001036 struct net_device_stats *stats = &t->dev->stats;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001037 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001038 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001039 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct net_device *tdev;
1041 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -07001042 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001044 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Steffen Klassertea3dc962014-11-05 08:03:50 +01001046 /* NBMA tunnel */
1047 if (ipv6_addr_any(&t->parms.raddr)) {
1048 struct in6_addr *addr6;
1049 struct neighbour *neigh;
1050 int addr_type;
1051
1052 if (!skb_dst(skb))
1053 goto tx_err_link_failure;
1054
1055 neigh = dst_neigh_lookup(skb_dst(skb),
1056 &ipv6_hdr(skb)->daddr);
1057 if (!neigh)
1058 goto tx_err_link_failure;
1059
1060 addr6 = (struct in6_addr *)&neigh->primary_key;
1061 addr_type = ipv6_addr_type(addr6);
1062
1063 if (addr_type == IPV6_ADDR_ANY)
1064 addr6 = &ipv6_hdr(skb)->daddr;
1065
1066 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1067 neigh_release(neigh);
1068 } else if (!fl6->flowi6_mark)
Martin KaFai Lauf230d1e2015-09-15 14:30:06 -07001069 dst = ip6_tnl_dst_get(t);
Steffen Klassertd5005142014-11-05 08:02:48 +01001070
1071 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1072 goto tx_err_link_failure;
1073
Eric Dumazet89b02122011-07-28 04:32:25 +00001074 if (!dst) {
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001075 dst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001077 if (dst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -07001078 goto tx_err_link_failure;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001079 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1080 if (IS_ERR(dst)) {
1081 err = PTR_ERR(dst);
1082 dst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -08001083 goto tx_err_link_failure;
1084 }
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001085 ndst = dst;
Patrick McHardya57ebc92005-09-08 14:27:47 -07001086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 tdev = dst->dev;
1089
1090 if (tdev == dev) {
1091 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +00001092 net_warn_ratelimited("%s: Local routing loop detected!\n",
1093 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto tx_err_dst_release;
1095 }
Ian Morris67ba4152014-08-24 21:53:10 +01001096 mtu = dst_mtu(dst) - sizeof(*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001097 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 max_headroom += 8;
1099 mtu -= 8;
1100 }
1101 if (mtu < IPV6_MIN_MTU)
1102 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +00001103 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -07001104 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001106 *pmtu = mtu;
1107 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto tx_err_dst_release;
1109 }
1110
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001111 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /*
1114 * Okay, now see if we can stuff it in the buffer as-is.
1115 */
1116 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001117
Patrick McHardycfbba492007-07-09 15:33:40 -07001118 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1119 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001121
Ian Morrise5d08d72014-11-23 21:28:43 +00001122 new_skb = skb_realloc_headroom(skb, max_headroom);
1123 if (!new_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto tx_err_dst_release;
1125
1126 if (skb->sk)
1127 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +00001128 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 skb = new_skb;
1130 }
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001131
1132 if (!fl6->flowi6_mark && ndst)
1133 ip6_tnl_dst_set(t, ndst);
1134 skb_dst_set(skb, dst);
1135
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001136 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
David S. Miller4c9483b2011-03-12 16:22:43 -05001138 proto = fl6->flowi6_proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001139 if (encap_limit >= 0) {
1140 init_tel_txopt(&opt, encap_limit);
1141 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1142 }
Hannes Frederic Sowa3d483052013-08-18 13:46:52 +02001143
1144 if (likely(!skb->encapsulation)) {
1145 skb_reset_inner_headers(skb);
1146 skb->encapsulation = 1;
1147 }
1148
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001149 skb_push(skb, sizeof(struct ipv6hdr));
1150 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001151 ipv6h = ipv6_hdr(skb);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001152 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
Tom Herbert42240902015-07-31 16:52:12 -07001153 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ipv6h->hop_limit = t->parms.hop_limit;
1155 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001156 ipv6h->saddr = fl6->saddr;
1157 ipv6h->daddr = fl6->daddr;
David Miller79b16aa2015-04-05 22:19:09 -04001158 ip6tunnel_xmit(NULL, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
1160tx_err_link_failure:
1161 stats->tx_carrier_errors++;
1162 dst_link_failure(skb);
1163tx_err_dst_release:
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001164 dst_release(dst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001165 return err;
1166}
1167
1168static inline int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001169ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1170{
1171 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001172 const struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001173 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001174 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001175 __u8 dsfield;
1176 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001177 u8 tproto;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001178 int err;
1179
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001180 tproto = ACCESS_ONCE(t->parms.proto);
Steffen Klassertd5005142014-11-05 08:02:48 +01001181 if (tproto != IPPROTO_IPIP && tproto != 0)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001182 return -1;
1183
1184 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1185 encap_limit = t->parms.encap_limit;
1186
Ian Morris67ba4152014-08-24 21:53:10 +01001187 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
David S. Miller4c9483b2011-03-12 16:22:43 -05001188 fl6.flowi6_proto = IPPROTO_IPIP;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001189
1190 dsfield = ipv4_get_dsfield(iph);
1191
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001192 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001193 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
Al Virob77f2fa2007-07-21 19:09:41 -07001194 & IPV6_TCLASS_MASK;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001195 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1196 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001197
David S. Miller4c9483b2011-03-12 16:22:43 -05001198 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001199 if (err != 0) {
1200 /* XXX: send ICMP error even if DF is not set. */
1201 if (err == -EMSGSIZE)
1202 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1203 htonl(mtu));
1204 return -1;
1205 }
1206
1207 return 0;
1208}
1209
1210static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001211ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1212{
1213 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001214 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001215 int encap_limit = -1;
1216 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001217 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001218 __u8 dsfield;
1219 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001220 u8 tproto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001221 int err;
1222
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001223 tproto = ACCESS_ONCE(t->parms.proto);
1224 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
Steffen Klassertd5005142014-11-05 08:02:48 +01001225 ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001226 return -1;
1227
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001228 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001229 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001230 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001231 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001232 if (tel->encap_limit == 0) {
1233 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001234 ICMPV6_HDR_FIELD, offset + 2);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001235 return -1;
1236 }
1237 encap_limit = tel->encap_limit - 1;
1238 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1239 encap_limit = t->parms.encap_limit;
1240
Ian Morris67ba4152014-08-24 21:53:10 +01001241 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
David S. Miller4c9483b2011-03-12 16:22:43 -05001242 fl6.flowi6_proto = IPPROTO_IPV6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001243
1244 dsfield = ipv6_get_dsfield(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001245 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001246 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001247 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
Florent Fourcot3308de22013-12-08 15:47:00 +01001248 fl6.flowlabel |= ip6_flowlabel(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001249 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1250 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001251
David S. Miller4c9483b2011-03-12 16:22:43 -05001252 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001253 if (err != 0) {
1254 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001255 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001256 return -1;
1257 }
1258
1259 return 0;
1260}
1261
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001262static netdev_tx_t
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001263ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1264{
1265 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001266 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001267 int ret;
1268
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001269 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001270 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001271 ret = ip4ip6_tnl_xmit(skb, dev);
1272 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001273 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001274 ret = ip6ip6_tnl_xmit(skb, dev);
1275 break;
1276 default:
1277 goto tx_err;
1278 }
1279
1280 if (ret < 0)
1281 goto tx_err;
1282
Patrick McHardy6ed10652009-06-23 06:03:08 +00001283 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285tx_err:
1286 stats->tx_errors++;
1287 stats->tx_dropped++;
1288 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001289 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001292static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001295 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001296 struct flowi6 *fl6 = &t->fl.u.ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001298 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1299 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001302 fl6->saddr = p->laddr;
1303 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001304 fl6->flowi6_oif = p->link;
1305 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001308 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001310 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001312 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1313 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1316 dev->flags |= IFF_POINTOPOINT;
1317 else
1318 dev->flags &= ~IFF_POINTOPOINT;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001321 int strict = (ipv6_addr_type(&p->raddr) &
1322 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1323
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001324 struct rt6_info *rt = rt6_lookup(t->net,
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001325 &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001326 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Ian Morris63159f22015-03-29 14:00:04 +01001328 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return;
1330
David S. Millerd1918542011-12-28 20:19:20 -05001331 if (rt->dst.dev) {
1332 dev->hard_header_len = rt->dst.dev->hard_header_len +
Ian Morris67ba4152014-08-24 21:53:10 +01001333 sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Ian Morris67ba4152014-08-24 21:53:10 +01001335 dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001336 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Ian Morris67ba4152014-08-24 21:53:10 +01001337 dev->mtu -= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (dev->mtu < IPV6_MIN_MTU)
1340 dev->mtu = IPV6_MIN_MTU;
1341 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001342 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344}
1345
1346/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001347 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * @t: tunnel to be changed
1349 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
1351 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001352 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 **/
1354
1355static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001356ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001358 t->parms.laddr = p->laddr;
1359 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 t->parms.flags = p->flags;
1361 t->parms.hop_limit = p->hop_limit;
1362 t->parms.encap_limit = p->encap_limit;
1363 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001364 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001365 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001366 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001367 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return 0;
1369}
1370
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001371static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1372{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001373 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001374 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1375 int err;
1376
1377 ip6_tnl_unlink(ip6n, t);
1378 synchronize_net();
1379 err = ip6_tnl_change(t, p);
1380 ip6_tnl_link(ip6n, t);
1381 netdev_state_change(t->dev);
1382 return err;
1383}
1384
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001385static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1386{
1387 /* for default tnl0 device allow to change only the proto */
1388 t->parms.proto = p->proto;
1389 netdev_state_change(t->dev);
1390 return 0;
1391}
1392
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001393static void
1394ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1395{
1396 p->laddr = u->laddr;
1397 p->raddr = u->raddr;
1398 p->flags = u->flags;
1399 p->hop_limit = u->hop_limit;
1400 p->encap_limit = u->encap_limit;
1401 p->flowinfo = u->flowinfo;
1402 p->link = u->link;
1403 p->proto = u->proto;
1404 memcpy(p->name, u->name, sizeof(u->name));
1405}
1406
1407static void
1408ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1409{
1410 u->laddr = p->laddr;
1411 u->raddr = p->raddr;
1412 u->flags = p->flags;
1413 u->hop_limit = p->hop_limit;
1414 u->encap_limit = p->encap_limit;
1415 u->flowinfo = p->flowinfo;
1416 u->link = p->link;
1417 u->proto = p->proto;
1418 memcpy(u->name, p->name, sizeof(u->name));
1419}
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001422 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * @dev: virtual device associated with tunnel
1424 * @ifr: parameters passed from userspace
1425 * @cmd: command to be performed
1426 *
1427 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001428 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001429 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 *
1431 * The possible commands are the following:
1432 * %SIOCGETTUNNEL: get tunnel parameters for device
1433 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1434 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1435 * %SIOCDELTUNNEL: delete tunnel
1436 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001437 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * initialization, can be used for creating other tunnel devices.
1439 *
1440 * Return:
1441 * 0 on success,
1442 * %-EFAULT if unable to copy data to or from userspace,
1443 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1444 * %-EINVAL if passed tunnel parameters are invalid,
1445 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1446 * %-ENODEV if attempting to change or delete a nonexisting device
1447 **/
1448
1449static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001450ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001454 struct __ip6_tnl_parm p1;
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001455 struct ip6_tnl *t = netdev_priv(dev);
1456 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001457 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 switch (cmd) {
1460 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001461 if (dev == ip6n->fb_tnl_dev) {
Ian Morris67ba4152014-08-24 21:53:10 +01001462 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 err = -EFAULT;
1464 break;
1465 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001466 ip6_tnl_parm_from_user(&p1, &p);
1467 t = ip6_tnl_locate(net, &p1, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001468 if (IS_ERR(t))
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001469 t = netdev_priv(dev);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001470 } else {
1471 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001472 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001473 ip6_tnl_parm_to_user(&p, &t->parms);
Ian Morris67ba4152014-08-24 21:53:10 +01001474 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 err = -EFAULT;
1476 }
1477 break;
1478 case SIOCADDTUNNEL:
1479 case SIOCCHGTUNNEL:
1480 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001481 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001483 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001484 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001486 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001487 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1488 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001490 ip6_tnl_parm_from_user(&p1, &p);
1491 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001492 if (cmd == SIOCCHGTUNNEL) {
Nicolas Dichtel37355562015-03-16 15:56:05 +01001493 if (!IS_ERR(t)) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001494 if (t->dev != dev) {
1495 err = -EEXIST;
1496 break;
1497 }
1498 } else
1499 t = netdev_priv(dev);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001500 if (dev == ip6n->fb_tnl_dev)
1501 err = ip6_tnl0_update(t, &p1);
1502 else
1503 err = ip6_tnl_update(t, &p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Nicolas Dichtel37355562015-03-16 15:56:05 +01001505 if (!IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001507 ip6_tnl_parm_to_user(&p, &t->parms);
1508 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001509 err = -EFAULT;
1510
Nicolas Dichtel37355562015-03-16 15:56:05 +01001511 } else {
1512 err = PTR_ERR(t);
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 break;
1515 case SIOCDELTUNNEL:
1516 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001517 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 break;
1519
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001520 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001521 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001522 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001524 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001525 ip6_tnl_parm_from_user(&p1, &p);
1526 t = ip6_tnl_locate(net, &p1, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001527 if (IS_ERR(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001529 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001530 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001532 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001534 err = 0;
1535 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 break;
1537 default:
1538 err = -EINVAL;
1539 }
1540 return err;
1541}
1542
1543/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001544 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * @dev: virtual device associated with tunnel
1546 * @new_mtu: the new mtu
1547 *
1548 * Return:
1549 * 0 on success,
1550 * %-EINVAL if mtu too small
1551 **/
1552
1553static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001554ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001556 struct ip6_tnl *tnl = netdev_priv(dev);
1557
1558 if (tnl->parms.proto == IPPROTO_IPIP) {
1559 if (new_mtu < 68)
1560 return -EINVAL;
1561 } else {
1562 if (new_mtu < IPV6_MIN_MTU)
1563 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001565 if (new_mtu > 0xFFF8 - dev->hard_header_len)
1566 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 dev->mtu = new_mtu;
1568 return 0;
1569}
1570
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001571int ip6_tnl_get_iflink(const struct net_device *dev)
1572{
1573 struct ip6_tnl *t = netdev_priv(dev);
1574
1575 return t->parms.link;
1576}
1577EXPORT_SYMBOL(ip6_tnl_get_iflink);
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001578
1579static const struct net_device_ops ip6_tnl_netdev_ops = {
Steffen Klassert6c6151d2014-11-03 09:19:27 +01001580 .ndo_init = ip6_tnl_dev_init,
Eric Dumazet8560f222010-09-28 03:23:34 +00001581 .ndo_uninit = ip6_tnl_dev_uninit,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001582 .ndo_start_xmit = ip6_tnl_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001583 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001584 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001585 .ndo_get_stats = ip6_get_stats,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001586 .ndo_get_iflink = ip6_tnl_get_iflink,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001587};
1588
Eric Dumazet8560f222010-09-28 03:23:34 +00001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001591 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 * @dev: virtual device associated with tunnel
1593 *
1594 * Description:
1595 * Initialize function pointers and device parameters
1596 **/
1597
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001598static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Anders Franzen381601e2010-11-24 05:47:18 +00001600 struct ip6_tnl *t;
1601
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001602 dev->netdev_ops = &ip6_tnl_netdev_ops;
Eric Dumazet8560f222010-09-28 03:23:34 +00001603 dev->destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 dev->type = ARPHRD_TUNNEL6;
Ian Morris67ba4152014-08-24 21:53:10 +01001606 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1607 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001608 t = netdev_priv(dev);
1609 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Ian Morris67ba4152014-08-24 21:53:10 +01001610 dev->mtu -= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 dev->flags |= IFF_NOARP;
1612 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001613 netif_keep_dst(dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02001614 /* This perm addr will be used as interface identifier by IPv6 */
1615 dev->addr_assign_type = NET_ADDR_RANDOM;
1616 eth_random_addr(dev->perm_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619
1620/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001621 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * @dev: virtual device associated with tunnel
1623 **/
1624
Eric Dumazet8560f222010-09-28 03:23:34 +00001625static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001626ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Patrick McHardy2941a482006-01-08 22:05:26 -08001628 struct ip6_tnl *t = netdev_priv(dev);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001629 int ret;
Eric Dumazet8560f222010-09-28 03:23:34 +00001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 t->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001632 t->net = dev_net(dev);
WANG Cong1c213bd2014-02-13 11:46:28 -08001633 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Eric Dumazet8560f222010-09-28 03:23:34 +00001634 if (!dev->tstats)
1635 return -ENOMEM;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001636
1637 ret = ip6_tnl_dst_init(t);
1638 if (ret) {
1639 free_percpu(dev->tstats);
1640 dev->tstats = NULL;
1641 return ret;
1642 }
1643
Eric Dumazet8560f222010-09-28 03:23:34 +00001644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
1647/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001648 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 * @dev: virtual device associated with tunnel
1650 **/
1651
Eric Dumazet8560f222010-09-28 03:23:34 +00001652static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Patrick McHardy2941a482006-01-08 22:05:26 -08001654 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001655 int err = ip6_tnl_dev_init_gen(dev);
1656
1657 if (err)
1658 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001659 ip6_tnl_link_config(t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001664 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 * @dev: fallback device
1666 *
1667 * Return: 0
1668 **/
1669
Eric Dumazet8560f222010-09-28 03:23:34 +00001670static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Patrick McHardy2941a482006-01-08 22:05:26 -08001672 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001673 struct net *net = dev_net(dev);
1674 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001675
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001676 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001678
Eric Dumazetcf778b02012-01-12 04:41:32 +00001679 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001683static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1684{
1685 u8 proto;
1686
Susant Sahanic8965932014-05-10 00:11:32 +05301687 if (!data || !data[IFLA_IPTUN_PROTO])
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001688 return 0;
1689
1690 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1691 if (proto != IPPROTO_IPV6 &&
1692 proto != IPPROTO_IPIP &&
1693 proto != 0)
1694 return -EINVAL;
1695
1696 return 0;
1697}
1698
1699static void ip6_tnl_netlink_parms(struct nlattr *data[],
1700 struct __ip6_tnl_parm *parms)
1701{
1702 memset(parms, 0, sizeof(*parms));
1703
1704 if (!data)
1705 return;
1706
1707 if (data[IFLA_IPTUN_LINK])
1708 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1709
1710 if (data[IFLA_IPTUN_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001711 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001712
1713 if (data[IFLA_IPTUN_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001714 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001715
1716 if (data[IFLA_IPTUN_TTL])
1717 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1718
1719 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1720 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1721
1722 if (data[IFLA_IPTUN_FLOWINFO])
Nicolas Dichtel1ff05fb2012-11-15 04:06:42 +00001723 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001724
1725 if (data[IFLA_IPTUN_FLAGS])
1726 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1727
1728 if (data[IFLA_IPTUN_PROTO])
1729 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1730}
1731
1732static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1733 struct nlattr *tb[], struct nlattr *data[])
1734{
1735 struct net *net = dev_net(dev);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001736 struct ip6_tnl *nt, *t;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001737
1738 nt = netdev_priv(dev);
1739 ip6_tnl_netlink_parms(data, &nt->parms);
1740
Nicolas Dichtel37355562015-03-16 15:56:05 +01001741 t = ip6_tnl_locate(net, &nt->parms, 0);
1742 if (!IS_ERR(t))
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001743 return -EEXIST;
1744
1745 return ip6_tnl_create2(dev);
1746}
1747
1748static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1749 struct nlattr *data[])
1750{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001751 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001752 struct __ip6_tnl_parm p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001753 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001754 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1755
1756 if (dev == ip6n->fb_tnl_dev)
1757 return -EINVAL;
1758
1759 ip6_tnl_netlink_parms(data, &p);
1760
1761 t = ip6_tnl_locate(net, &p, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001762 if (!IS_ERR(t)) {
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001763 if (t->dev != dev)
1764 return -EEXIST;
1765 } else
1766 t = netdev_priv(dev);
1767
1768 return ip6_tnl_update(t, &p);
1769}
1770
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001771static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1772{
1773 struct net *net = dev_net(dev);
1774 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1775
1776 if (dev != ip6n->fb_tnl_dev)
1777 unregister_netdevice_queue(dev, head);
1778}
1779
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001780static size_t ip6_tnl_get_size(const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001781{
1782 return
1783 /* IFLA_IPTUN_LINK */
1784 nla_total_size(4) +
1785 /* IFLA_IPTUN_LOCAL */
1786 nla_total_size(sizeof(struct in6_addr)) +
1787 /* IFLA_IPTUN_REMOTE */
1788 nla_total_size(sizeof(struct in6_addr)) +
1789 /* IFLA_IPTUN_TTL */
1790 nla_total_size(1) +
1791 /* IFLA_IPTUN_ENCAP_LIMIT */
1792 nla_total_size(1) +
1793 /* IFLA_IPTUN_FLOWINFO */
1794 nla_total_size(4) +
1795 /* IFLA_IPTUN_FLAGS */
1796 nla_total_size(4) +
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001797 /* IFLA_IPTUN_PROTO */
1798 nla_total_size(1) +
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001799 0;
1800}
1801
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001802static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001803{
1804 struct ip6_tnl *tunnel = netdev_priv(dev);
1805 struct __ip6_tnl_parm *parm = &tunnel->parms;
1806
1807 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001808 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1809 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001810 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1811 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1812 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001813 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1814 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001815 goto nla_put_failure;
1816 return 0;
1817
1818nla_put_failure:
1819 return -EMSGSIZE;
1820}
1821
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001822struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1823{
1824 struct ip6_tnl *tunnel = netdev_priv(dev);
1825
1826 return tunnel->net;
1827}
1828EXPORT_SYMBOL(ip6_tnl_get_link_net);
1829
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001830static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1831 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1832 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1833 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1834 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1835 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1836 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1837 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1838 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1839};
1840
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001841static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1842 .kind = "ip6tnl",
1843 .maxtype = IFLA_IPTUN_MAX,
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001844 .policy = ip6_tnl_policy,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001845 .priv_size = sizeof(struct ip6_tnl),
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001846 .setup = ip6_tnl_dev_setup,
1847 .validate = ip6_tnl_validate,
1848 .newlink = ip6_tnl_newlink,
1849 .changelink = ip6_tnl_changelink,
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001850 .dellink = ip6_tnl_dellink,
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001851 .get_size = ip6_tnl_get_size,
1852 .fill_info = ip6_tnl_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001853 .get_link_net = ip6_tnl_get_link_net,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001854};
1855
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001856static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001857 .handler = ip4ip6_rcv,
1858 .err_handler = ip4ip6_err,
1859 .priority = 1,
1860};
1861
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001862static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07001863 .handler = ip6ip6_rcv,
1864 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001865 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866};
1867
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001868static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001869{
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001870 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001871 struct net_device *dev, *aux;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001872 int h;
1873 struct ip6_tnl *t;
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001874 LIST_HEAD(list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001875
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001876 for_each_netdev_safe(net, dev, aux)
1877 if (dev->rtnl_link_ops == &ip6_link_ops)
1878 unregister_netdevice_queue(dev, &list);
1879
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001880 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00001881 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Ian Morris53b24b82015-03-29 14:00:05 +01001882 while (t) {
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001883 /* If dev is in the same netns, it has already
1884 * been added to the list by the previous loop.
1885 */
1886 if (!net_eq(dev_net(t->dev), net))
1887 unregister_netdevice_queue(t->dev, &list);
Eric Dumazet94767632010-09-15 20:25:34 +00001888 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001889 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001890 }
1891
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001892 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001893}
1894
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001895static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001896{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001897 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00001898 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001899 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001900
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001901 ip6n->tnls[0] = ip6n->tnls_wc;
1902 ip6n->tnls[1] = ip6n->tnls_r_l;
1903
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001904 err = -ENOMEM;
1905 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001906 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001907
1908 if (!ip6n->fb_tnl_dev)
1909 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001910 dev_net_set(ip6n->fb_tnl_dev, net);
Nicolas Dichtelbb814092013-10-01 18:05:00 +02001911 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001912 /* FB netdevice is special: we have one, and only one per netns.
1913 * Allowing to move it to another netns is clearly unsafe.
1914 */
1915 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001916
Eric Dumazet8560f222010-09-28 03:23:34 +00001917 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1918 if (err < 0)
1919 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001920
1921 err = register_netdev(ip6n->fb_tnl_dev);
1922 if (err < 0)
1923 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00001924
1925 t = netdev_priv(ip6n->fb_tnl_dev);
1926
1927 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001928 return 0;
1929
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001930err_register:
Eric Dumazet8560f222010-09-28 03:23:34 +00001931 ip6_dev_free(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001932err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001933 return err;
1934}
1935
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001936static void __net_exit ip6_tnl_exit_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001937{
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001938 rtnl_lock();
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01001939 ip6_tnl_destroy_tunnels(net);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001940 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001941}
1942
1943static struct pernet_operations ip6_tnl_net_ops = {
1944 .init = ip6_tnl_init_net,
1945 .exit = ip6_tnl_exit_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001946 .id = &ip6_tnl_net_id,
1947 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001948};
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950/**
1951 * ip6_tunnel_init - register protocol and reserve needed resources
1952 *
1953 * Return: 0 on success
1954 **/
1955
1956static int __init ip6_tunnel_init(void)
1957{
1958 int err;
1959
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001960 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001961 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001962 goto out_pernet;
1963
1964 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1965 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001966 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001967 goto out_ip4ip6;
1968 }
1969
1970 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1971 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001972 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001973 goto out_ip6ip6;
1974 }
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001975 err = rtnl_link_register(&ip6_link_ops);
1976 if (err < 0)
1977 goto rtnl_link_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001980
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001981rtnl_link_failed:
1982 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001983out_ip6ip6:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001984 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001985out_ip4ip6:
1986 unregister_pernet_device(&ip6_tnl_net_ops);
1987out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return err;
1989}
1990
1991/**
1992 * ip6_tunnel_cleanup - free resources and unregister protocol
1993 **/
1994
1995static void __exit ip6_tunnel_cleanup(void)
1996{
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001997 rtnl_link_unregister(&ip6_link_ops);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001998 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00001999 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09002000
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08002001 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00002002 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00002004 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
2007module_init(ip6_tunnel_init);
2008module_exit(ip6_tunnel_cleanup);