blob: b5dd20c4599bb1fc2515f6a78b8a6ee3287d03e5 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09003 * IPv6 tunneling device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Linux INET6 implementation
5 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Ville Nuorvala <vnuorval@tcs.hut.fi>
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09008 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Based on:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +090011 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * RFC 2473
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Joe Perchesf3213832012-05-15 14:11:53 +000016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080019#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/types.h>
22#include <linux/sockios.h>
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +090023#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/if.h>
25#include <linux/in.h>
26#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/net.h>
28#include <linux/in6.h>
29#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/icmpv6.h>
32#include <linux/init.h>
33#include <linux/route.h>
34#include <linux/rtnetlink.h>
35#include <linux/netfilter_ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000037#include <linux/hash.h>
Nicolas Dichtele8377352013-08-20 12:16:06 +020038#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070041#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +090043#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000045#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/ip6_route.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
50#include <net/xfrm.h>
51#include <net/dsfield.h>
52#include <net/inet_ecn.h>
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070053#include <net/net_namespace.h>
54#include <net/netns/generic.h>
Alexei Starovoitov8d792662016-09-15 13:00:30 -070055#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57MODULE_AUTHOR("Ville Nuorvala");
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +090058MODULE_DESCRIPTION("IPv6 tunneling device");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_LICENSE("GPL");
Tom Gundersenf98f89a2014-05-15 23:21:30 +020060MODULE_ALIAS_RTNL_LINK("ip6tnl");
stephen hemminger6dfbd872011-03-10 11:43:19 +000061MODULE_ALIAS_NETDEV("ip6tnl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jiri Kosinae87a8f22016-08-10 11:03:35 +020063#define IP6_TUNNEL_HASH_SIZE_SHIFT 5
64#define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +000066static bool log_ecn_error = true;
67module_param(log_ecn_error, bool, 0644);
68MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
69
Eric Dumazetddbe5032012-07-18 08:11:12 +000070static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
71{
72 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
73
Jiri Kosinae87a8f22016-08-10 11:03:35 +020074 return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
Eric Dumazetddbe5032012-07-18 08:11:12 +000075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Eric Dumazet8560f222010-09-28 03:23:34 +000077static int ip6_tnl_dev_init(struct net_device *dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090078static void ip6_tnl_dev_setup(struct net_device *dev);
Nicolas Dichtelc075b132012-11-09 06:10:01 +000079static struct rtnl_link_ops ip6_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030081static unsigned int ip6_tnl_net_id __read_mostly;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070082struct ip6_tnl_net {
Pavel Emelyanov15820e1292008-04-16 01:23:02 -070083 /* the IPv6 tunnel fallback device */
84 struct net_device *fb_tnl_dev;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -070085 /* lists for storing tunnels in use */
Jiri Kosinae87a8f22016-08-10 11:03:35 +020086 struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
Eric Dumazet94767632010-09-15 20:25:34 +000087 struct ip6_tnl __rcu *tnls_wc[1];
88 struct ip6_tnl __rcu **tnls[2];
Alexei Starovoitov8d792662016-09-15 13:00:30 -070089 struct ip6_tnl __rcu *collect_md_tun;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070090};
91
Eric Dumazet8560f222010-09-28 03:23:34 +000092static struct net_device_stats *ip6_get_stats(struct net_device *dev)
93{
David S. Miller56a43422014-01-06 17:37:45 -050094 struct pcpu_sw_netstats tmp, sum = { 0 };
Eric Dumazet8560f222010-09-28 03:23:34 +000095 int i;
96
97 for_each_possible_cpu(i) {
Li RongQingabb60132014-01-02 13:20:12 +080098 unsigned int start;
Li RongQing8f849852014-01-04 13:57:59 +080099 const struct pcpu_sw_netstats *tstats =
100 per_cpu_ptr(dev->tstats, i);
Eric Dumazet8560f222010-09-28 03:23:34 +0000101
Li RongQingabb60132014-01-02 13:20:12 +0800102 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700103 start = u64_stats_fetch_begin_irq(&tstats->syncp);
Li RongQingabb60132014-01-02 13:20:12 +0800104 tmp.rx_packets = tstats->rx_packets;
105 tmp.rx_bytes = tstats->rx_bytes;
106 tmp.tx_packets = tstats->tx_packets;
107 tmp.tx_bytes = tstats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700108 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
Li RongQingabb60132014-01-02 13:20:12 +0800109
110 sum.rx_packets += tmp.rx_packets;
111 sum.rx_bytes += tmp.rx_bytes;
112 sum.tx_packets += tmp.tx_packets;
113 sum.tx_bytes += tmp.tx_bytes;
Eric Dumazet8560f222010-09-28 03:23:34 +0000114 }
115 dev->stats.rx_packets = sum.rx_packets;
116 dev->stats.rx_bytes = sum.rx_bytes;
117 dev->stats.tx_packets = sum.tx_packets;
118 dev->stats.tx_bytes = sum.tx_bytes;
119 return &dev->stats;
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900123 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900124 * @remote: the address of the tunnel exit-point
125 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900127 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900129 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * else %NULL
131 **/
132
Eric Dumazet2922bc82009-10-23 06:34:34 +0000133#define for_each_ip6_tunnel_rcu(start) \
134 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static struct ip6_tnl *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000137ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000139 unsigned int hash = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct ip6_tnl *t;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700141 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Steffen Klassertea3dc962014-11-05 08:03:50 +0100142 struct in6_addr any;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Eric Dumazetddbe5032012-07-18 08:11:12 +0000144 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (ipv6_addr_equal(local, &t->parms.laddr) &&
146 ipv6_addr_equal(remote, &t->parms.raddr) &&
147 (t->dev->flags & IFF_UP))
148 return t;
149 }
Steffen Klassertea3dc962014-11-05 08:03:50 +0100150
151 memset(&any, 0, sizeof(any));
152 hash = HASH(&any, local);
153 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
154 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Vadim Fedorenko68d00f32016-10-11 22:47:20 +0300155 ipv6_addr_any(&t->parms.raddr) &&
Steffen Klassertea3dc962014-11-05 08:03:50 +0100156 (t->dev->flags & IFF_UP))
157 return t;
158 }
159
160 hash = HASH(remote, &any);
161 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
162 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
Vadim Fedorenko68d00f32016-10-11 22:47:20 +0300163 ipv6_addr_any(&t->parms.laddr) &&
Steffen Klassertea3dc962014-11-05 08:03:50 +0100164 (t->dev->flags & IFF_UP))
165 return t;
166 }
167
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700168 t = rcu_dereference(ip6n->collect_md_tun);
Haishuang Yan6c1cb432017-09-12 17:47:57 +0800169 if (t && t->dev->flags & IFF_UP)
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700170 return t;
171
Eric Dumazet2922bc82009-10-23 06:34:34 +0000172 t = rcu_dereference(ip6n->tnls_wc[0]);
173 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return t;
175
176 return NULL;
177}
178
179/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900180 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900181 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *
183 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900184 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * &struct in6_addr entries laddr and raddr in @p.
186 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900187 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 **/
189
Eric Dumazet94767632010-09-15 20:25:34 +0000190static struct ip6_tnl __rcu **
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000191ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000193 const struct in6_addr *remote = &p->raddr;
194 const struct in6_addr *local = &p->laddr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000195 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int prio = 0;
197
198 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
199 prio = 1;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000200 h = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700202 return &ip6n->tnls[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900206 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * @t: tunnel to be added
208 **/
209
210static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700211ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Eric Dumazet94767632010-09-15 20:25:34 +0000213 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700215 if (t->parms.collect_md)
216 rcu_assign_pointer(ip6n->collect_md_tun, t);
Eric Dumazetcf778b02012-01-12 04:41:32 +0000217 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
218 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900222 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * @t: tunnel to be removed
224 **/
225
226static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700227ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Eric Dumazet94767632010-09-15 20:25:34 +0000229 struct ip6_tnl __rcu **tp;
230 struct ip6_tnl *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700232 if (t->parms.collect_md)
233 rcu_assign_pointer(ip6n->collect_md_tun, NULL);
234
Eric Dumazet94767632010-09-15 20:25:34 +0000235 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
236 (iter = rtnl_dereference(*tp)) != NULL;
237 tp = &iter->next) {
238 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000239 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
241 }
242 }
243}
244
Eric Dumazet8560f222010-09-28 03:23:34 +0000245static void ip6_dev_free(struct net_device *dev)
246{
Martin KaFai Laucdf34642015-09-15 14:30:07 -0700247 struct ip6_tnl *t = netdev_priv(dev);
248
Tom Herbert0d3c7032016-04-29 17:12:15 -0700249 gro_cells_destroy(&t->gro_cells);
Paolo Abeni607f7252016-02-12 15:43:54 +0100250 dst_cache_destroy(&t->dst_cache);
Eric Dumazet8560f222010-09-28 03:23:34 +0000251 free_percpu(dev->tstats);
Eric Dumazet8560f222010-09-28 03:23:34 +0000252}
253
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000254static int ip6_tnl_create2(struct net_device *dev)
255{
256 struct ip6_tnl *t = netdev_priv(dev);
257 struct net *net = dev_net(dev);
258 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
259 int err;
260
261 t = netdev_priv(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000262
Thadeu Lima de Souza Cascardob6ee3762016-04-01 17:17:50 -0300263 dev->rtnl_link_ops = &ip6_link_ops;
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000264 err = register_netdevice(dev);
265 if (err < 0)
266 goto out;
267
268 strcpy(t->parms.name, dev->name);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000269
270 dev_hold(dev);
271 ip6_tnl_link(ip6n, t);
272 return 0;
273
274out:
275 return err;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000279 * ip6_tnl_create - create a new tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * @p: tunnel parameters
281 * @pt: pointer to new tunnel
282 *
283 * Description:
284 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900285 *
286 * Return:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100287 * created tunnel or error pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 **/
289
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000290static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct net_device *dev;
293 struct ip6_tnl *t;
294 char name[IFNAMSIZ];
Eric Dumazetdb7a65e2018-04-05 06:39:30 -0700295 int err = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Eric Dumazetdb7a65e2018-04-05 06:39:30 -0700297 if (p->name[0]) {
298 if (!dev_valid_name(p->name))
299 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 strlcpy(name, p->name, IFNAMSIZ);
Eric Dumazetdb7a65e2018-04-05 06:39:30 -0700301 } else {
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800302 sprintf(name, "ip6tnl%%d");
Eric Dumazetdb7a65e2018-04-05 06:39:30 -0700303 }
304 err = -ENOMEM;
Tom Gundersenc835a672014-07-14 16:37:24 +0200305 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
306 ip6_tnl_dev_setup);
Ian Morris63159f22015-03-29 14:00:04 +0100307 if (!dev)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800308 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Pavel Emelyanov554eb272008-04-16 01:24:13 -0700310 dev_net_set(dev, net);
311
Patrick McHardy2941a482006-01-08 22:05:26 -0800312 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 t->parms = *p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200314 t->net = dev_net(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000315 err = ip6_tnl_create2(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +0000316 if (err < 0)
317 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Ville Nuorvala567131a2006-11-24 17:05:41 -0800319 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800320
321failed_free:
David S. Millercf124db2017-05-08 12:52:56 -0400322 free_netdev(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800323failed:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100324 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900328 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900329 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * @create: != 0 if allowed to create new tunnel if no match found
331 *
332 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900333 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * based on @parms. If this is unsuccessful, but @create is set a new
335 * tunnel device is created and registered for use.
336 *
337 * Return:
Nicolas Dichtel37355562015-03-16 15:56:05 +0100338 * matching tunnel or error pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 **/
340
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700341static struct ip6_tnl *ip6_tnl_locate(struct net *net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000342 struct __ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000344 const struct in6_addr *remote = &p->raddr;
345 const struct in6_addr *local = &p->laddr;
Eric Dumazet94767632010-09-15 20:25:34 +0000346 struct ip6_tnl __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700348 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Eric Dumazet94767632010-09-15 20:25:34 +0000350 for (tp = ip6_tnl_bucket(ip6n, p);
351 (t = rtnl_dereference(*tp)) != NULL;
352 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200354 ipv6_addr_equal(remote, &t->parms.raddr)) {
355 if (create)
Nicolas Dichtel37355562015-03-16 15:56:05 +0100356 return ERR_PTR(-EEXIST);
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200357
Ville Nuorvala567131a2006-11-24 17:05:41 -0800358 return t;
Steffen Klassert2b0bb012014-09-22 10:07:24 +0200359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 if (!create)
Nicolas Dichtel37355562015-03-16 15:56:05 +0100362 return ERR_PTR(-ENODEV);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700363 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900367 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900369 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900371 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 **/
373
374static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900375ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Patrick McHardy2941a482006-01-08 22:05:26 -0800377 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200378 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700379 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Eric Dumazet94767632010-09-15 20:25:34 +0000381 if (dev == ip6n->fb_tnl_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000382 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
Eric Dumazet94767632010-09-15 20:25:34 +0000383 else
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700384 ip6_tnl_unlink(ip6n, t);
Paolo Abeni607f7252016-02-12 15:43:54 +0100385 dst_cache_reset(&t->dst_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 dev_put(dev);
387}
388
389/**
390 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
391 * @skb: received socket buffer
392 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900393 * Return:
394 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * else index to encapsulation limit
396 **/
397
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000398__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800400 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
401 unsigned int nhoff = raw - skb->data;
402 unsigned int off = nhoff + sizeof(*ipv6h);
403 u8 next, nexthdr = ipv6h->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct ipv6_opt_hdr *hdr;
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800407 u16 optlen;
408
409 if (!pskb_may_pull(skb, off + sizeof(*hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
411
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800412 hdr = (struct ipv6_opt_hdr *)(skb->data + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (nexthdr == NEXTHDR_FRAGMENT) {
414 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
415 if (frag_hdr->frag_off)
416 break;
417 optlen = 8;
418 } else if (nexthdr == NEXTHDR_AUTH) {
yangxingwu416e8122019-07-10 21:14:10 +0800419 optlen = ipv6_authlen(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 } else {
421 optlen = ipv6_optlen(hdr);
422 }
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800423 /* cache hdr->nexthdr, since pskb_may_pull() might
424 * invalidate hdr
425 */
426 next = hdr->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (nexthdr == NEXTHDR_DEST) {
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800428 u16 i = 2;
429
430 /* Remember : hdr is no longer valid at this point. */
431 if (!pskb_may_pull(skb, off + optlen))
432 break;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 while (1) {
435 struct ipv6_tlv_tnl_enc_lim *tel;
436
437 /* No more room for encapsulation limit */
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800438 if (i + sizeof(*tel) > optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440
Dan Carpenter63117f02017-02-01 11:46:32 +0300441 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* return index of option if found and valid */
443 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
444 tel->length == 1)
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800445 return i + off - nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* else jump to next option */
447 if (tel->type)
448 i += tel->length + 2;
449 else
450 i++;
451 }
452 }
Eric Dumazetfbfa7432017-01-23 16:43:06 -0800453 nexthdr = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 off += optlen;
455 }
456 return 0;
457}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000458EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900461 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
463 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900464 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * to the specifications in RFC 2473.
466 **/
467
Herbert Xud2acc342006-03-28 01:12:13 -0800468static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900469ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700470 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Xin Long383c1f82017-11-11 19:06:51 +0800472 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
473 struct net *net = dev_net(skb->dev);
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700474 u8 rel_type = ICMPV6_DEST_UNREACH;
475 u8 rel_code = ICMPV6_ADDR_UNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 __u32 rel_info = 0;
Xin Long383c1f82017-11-11 19:06:51 +0800477 struct ip6_tnl *t;
Herbert Xud2acc342006-03-28 01:12:13 -0800478 int err = -ENOENT;
Xin Long383c1f82017-11-11 19:06:51 +0800479 int rel_msg = 0;
480 u8 tproto;
481 __u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900483 /* If the packet doesn't contain the original IPv6 header we are
484 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 processing of the error. */
486
Eric Dumazet2922bc82009-10-23 06:34:34 +0000487 rcu_read_lock();
Ian Morrise5d08d72014-11-23 21:28:43 +0000488 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
Ian Morris63159f22015-03-29 14:00:04 +0100489 if (!t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto out;
491
Mark Rutland6aa7de02017-10-23 14:07:29 -0700492 tproto = READ_ONCE(t->parms.proto);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +0300493 if (tproto != ipproto && tproto != 0)
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900494 goto out;
495
Herbert Xud2acc342006-03-28 01:12:13 -0800496 err = 0;
497
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900498 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct ipv6_tlv_tnl_enc_lim *tel;
Xin Longb00f5432017-11-11 19:06:52 +0800500 __u32 mtu, teli;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 case ICMPV6_DEST_UNREACH:
Matt Bennett17a10c92015-09-25 11:01:47 +1200502 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
503 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 rel_msg = 1;
505 break;
506 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900507 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Matt Bennett17a10c92015-09-25 11:01:47 +1200508 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
509 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 rel_msg = 1;
511 }
512 break;
513 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800514 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900515 if ((*code) == ICMPV6_HDR_FIELD)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000516 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Al Viro704eae12007-07-26 17:33:29 +0100518 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
520 if (tel->encap_limit == 0) {
Matt Bennett17a10c92015-09-25 11:01:47 +1200521 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
522 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 rel_msg = 1;
524 }
Joe Perchese87cc472012-05-13 21:56:26 +0000525 } else {
Matt Bennett17a10c92015-09-25 11:01:47 +1200526 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
527 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 break;
530 case ICMPV6_PKT_TOOBIG:
Xin Longb00f5432017-11-11 19:06:52 +0800531 ip6_update_pmtu(skb, net, htonl(*info), 0, 0,
532 sock_net_uid(net, NULL));
Al Viro704eae12007-07-26 17:33:29 +0100533 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (mtu < IPV6_MIN_MTU)
535 mtu = IPV6_MIN_MTU;
Ian Morrise5d08d72014-11-23 21:28:43 +0000536 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
537 if (len > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 rel_type = ICMPV6_PKT_TOOBIG;
539 rel_code = 0;
540 rel_info = mtu;
541 rel_msg = 1;
542 }
543 break;
Xin Long383c1f82017-11-11 19:06:51 +0800544 case NDISC_REDIRECT:
545 ip6_redirect(skb, net, skb->dev->ifindex, 0,
546 sock_net_uid(net, NULL));
547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900549
550 *type = rel_type;
551 *code = rel_code;
552 *info = rel_info;
553 *msg = rel_msg;
554
555out:
Eric Dumazet2922bc82009-10-23 06:34:34 +0000556 rcu_read_unlock();
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900557 return err;
558}
559
560static int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900561ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700562 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900563{
Xin Long77552cf2017-11-11 19:06:53 +0800564 __u32 rel_info = ntohl(info);
565 const struct iphdr *eiph;
566 struct sk_buff *skb2;
567 int err, rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700568 u8 rel_type = type;
569 u8 rel_code = code;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900570 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -0700571 struct flowi4 fl4;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900572
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900573 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
574 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900575 if (err < 0)
576 return err;
577
578 if (rel_msg == 0)
579 return 0;
580
581 switch (rel_type) {
582 case ICMPV6_DEST_UNREACH:
583 if (rel_code != ICMPV6_ADDR_UNREACH)
584 return 0;
585 rel_type = ICMP_DEST_UNREACH;
586 rel_code = ICMP_HOST_UNREACH;
587 break;
588 case ICMPV6_PKT_TOOBIG:
589 if (rel_code != 0)
590 return 0;
591 rel_type = ICMP_DEST_UNREACH;
592 rel_code = ICMP_FRAG_NEEDED;
593 break;
594 default:
595 return 0;
596 }
597
598 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
599 return 0;
600
601 skb2 = skb_clone(skb, GFP_ATOMIC);
602 if (!skb2)
603 return 0;
604
Eric Dumazetadf30902009-06-02 05:19:30 +0000605 skb_dst_drop(skb2);
606
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900607 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700608 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700609 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900610
611 /* Try to guess incoming interface */
Xin Long77552cf2017-11-11 19:06:53 +0800612 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr,
613 0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800614 if (IS_ERR(rt))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900615 goto out;
616
Changli Gaod8d1f302010-06-10 23:31:35 -0700617 skb2->dev = rt->dst.dev;
Xin Long77552cf2017-11-11 19:06:53 +0800618 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900619
620 /* route "incoming" packet */
621 if (rt->rt_flags & RTCF_LOCAL) {
David S. Miller31e4543d2011-05-03 20:25:42 -0700622 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
Xin Long77552cf2017-11-11 19:06:53 +0800623 eiph->daddr, eiph->saddr, 0, 0,
624 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
Sheena Mira-atob2e54b02019-04-01 13:04:42 +1300625 if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL6) {
David S. Millerb23dd4f2011-03-02 14:31:35 -0800626 if (!IS_ERR(rt))
627 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900628 goto out;
629 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800630 skb_dst_set(skb2, &rt->dst);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900631 } else {
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900632 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
633 skb2->dev) ||
Sheena Mira-atob2e54b02019-04-01 13:04:42 +1300634 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL6)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900635 goto out;
636 }
637
638 /* change mtu on this route */
639 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000640 if (rel_info > dst_mtu(skb_dst(skb2)))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900641 goto out;
642
Hangbin Liu7a1592b2019-12-22 10:51:13 +0800643 skb_dst_update_pmtu_no_confirm(skb2, rel_info);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900644 }
645
Al Viro704eae12007-07-26 17:33:29 +0100646 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900647
648out:
649 kfree_skb(skb2);
650 return 0;
651}
652
653static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900654ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700655 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900656{
Xin Long77552cf2017-11-11 19:06:53 +0800657 __u32 rel_info = ntohl(info);
658 int err, rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700659 u8 rel_type = type;
660 u8 rel_code = code;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900661
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900662 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
663 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900664 if (err < 0)
665 return err;
666
667 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct rt6_info *rt;
669 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Eric Dumazetadf30902009-06-02 05:19:30 +0000674 skb_dst_drop(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700676 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /* Try to guess incoming interface */
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700679 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
David Ahernb75cc8f2018-03-02 08:32:17 -0800680 NULL, 0, skb2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
David S. Millerd1918542011-12-28 20:19:20 -0500682 if (rt && rt->dst.dev)
683 skb2->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000685 icmpv6_send(skb2, rel_type, rel_code, rel_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Amerigo Wang94e187c2012-10-29 00:13:19 +0000687 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 kfree_skb(skb2);
690 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900691
692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000695static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
696 const struct ipv6hdr *ipv6h,
697 struct sk_buff *skb)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900698{
699 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
700
701 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700702 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900703
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000704 return IP6_ECN_decapsulate(ipv6h, skb);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900705}
706
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000707static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
708 const struct ipv6hdr *ipv6h,
709 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900711 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800712 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000714 return IP6_ECN_decapsulate(ipv6h, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900716
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000717__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000718 const struct in6_addr *laddr,
719 const struct in6_addr *raddr)
720{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000721 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000722 int ltype = ipv6_addr_type(laddr);
723 int rtype = ipv6_addr_type(raddr);
724 __u32 flags = 0;
725
726 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
727 flags = IP6_TNL_F_CAP_PER_PACKET;
728 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
729 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
730 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
731 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
732 if (ltype&IPV6_ADDR_UNICAST)
733 flags |= IP6_TNL_F_CAP_XMIT;
734 if (rtype&IPV6_ADDR_UNICAST)
735 flags |= IP6_TNL_F_CAP_RCV;
736 }
737 return flags;
738}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000739EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000740
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100741/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000742int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000743 const struct in6_addr *laddr,
744 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800745{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000746 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800747 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200748 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800749
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000750 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
751 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
752 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900753 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800754
755 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100756 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800757
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000758 if ((ipv6_addr_is_multicast(laddr) ||
David Ahern232378e2018-03-13 08:29:37 -0700759 likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false,
760 0, IFA_F_TENTATIVE))) &&
Shmulik Ladkani908d1402017-10-21 00:25:15 +0300761 ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) ||
David Ahern232378e2018-03-13 08:29:37 -0700762 likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true,
763 0, IFA_F_TENTATIVE))))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800764 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800765 }
766 return ret;
767}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000768EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Tom Herbert0d3c7032016-04-29 17:12:15 -0700770static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
771 const struct tnl_ptk_info *tpi,
772 struct metadata_dst *tun_dst,
773 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
774 const struct ipv6hdr *ipv6h,
775 struct sk_buff *skb),
776 bool log_ecn_err)
777{
778 struct pcpu_sw_netstats *tstats;
779 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
780 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Tom Herbert0d3c7032016-04-29 17:12:15 -0700782 if ((!(tpi->flags & TUNNEL_CSUM) &&
783 (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
784 ((tpi->flags & TUNNEL_CSUM) &&
785 !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
786 tunnel->dev->stats.rx_crc_errors++;
787 tunnel->dev->stats.rx_errors++;
788 goto drop;
789 }
790
791 if (tunnel->parms.i_flags & TUNNEL_SEQ) {
792 if (!(tpi->flags & TUNNEL_SEQ) ||
793 (tunnel->i_seqno &&
794 (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
795 tunnel->dev->stats.rx_fifo_errors++;
796 tunnel->dev->stats.rx_errors++;
797 goto drop;
798 }
799 tunnel->i_seqno = ntohl(tpi->seq) + 1;
800 }
801
802 skb->protocol = tpi->proto;
803
804 /* Warning: All skb pointers will be invalidated! */
805 if (tunnel->dev->type == ARPHRD_ETHER) {
806 if (!pskb_may_pull(skb, ETH_HLEN)) {
807 tunnel->dev->stats.rx_length_errors++;
808 tunnel->dev->stats.rx_errors++;
809 goto drop;
810 }
811
812 ipv6h = ipv6_hdr(skb);
813 skb->protocol = eth_type_trans(skb, tunnel->dev);
814 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
815 } else {
816 skb->dev = tunnel->dev;
817 }
818
819 skb_reset_network_header(skb);
820 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
821
822 __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
823
824 err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
825 if (unlikely(err)) {
826 if (log_ecn_err)
827 net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
828 &ipv6h->saddr,
829 ipv6_get_dsfield(ipv6h));
830 if (err > 1) {
831 ++tunnel->dev->stats.rx_frame_errors;
832 ++tunnel->dev->stats.rx_errors;
833 goto drop;
834 }
835 }
836
837 tstats = this_cpu_ptr(tunnel->dev->tstats);
838 u64_stats_update_begin(&tstats->syncp);
839 tstats->rx_packets++;
840 tstats->rx_bytes += skb->len;
841 u64_stats_update_end(&tstats->syncp);
842
843 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
844
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700845 if (tun_dst)
846 skb_dst_set(skb, (struct dst_entry *)tun_dst);
847
Tom Herbert0d3c7032016-04-29 17:12:15 -0700848 gro_cells_receive(&tunnel->gro_cells, skb);
849 return 0;
850
851drop:
Haishuang Yanf1925ca2017-06-15 10:29:30 +0800852 if (tun_dst)
853 dst_release((struct dst_entry *)tun_dst);
Tom Herbert0d3c7032016-04-29 17:12:15 -0700854 kfree_skb(skb);
855 return 0;
856}
857
858int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
859 const struct tnl_ptk_info *tpi,
860 struct metadata_dst *tun_dst,
861 bool log_ecn_err)
862{
William Tu6712abc2017-12-01 15:26:08 -0800863 return __ip6_tnl_rcv(t, skb, tpi, tun_dst, ip6ip6_dscp_ecn_decapsulate,
Tom Herbert0d3c7032016-04-29 17:12:15 -0700864 log_ecn_err);
865}
866EXPORT_SYMBOL(ip6_tnl_rcv);
867
868static const struct tnl_ptk_info tpi_v6 = {
869 /* no tunnel info required for ipxip6. */
870 .proto = htons(ETH_P_IPV6),
871};
872
873static const struct tnl_ptk_info tpi_v4 = {
874 /* no tunnel info required for ipxip6. */
875 .proto = htons(ETH_P_IP),
876};
877
878static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
879 const struct tnl_ptk_info *tpi,
880 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
881 const struct ipv6hdr *ipv6h,
882 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000885 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700886 struct metadata_dst *tun_dst = NULL;
Tom Herbert0d3c7032016-04-29 17:12:15 -0700887 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Eric Dumazet2922bc82009-10-23 06:34:34 +0000889 rcu_read_lock();
Ian Morrise5d08d72014-11-23 21:28:43 +0000890 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
Tom Herbert0d3c7032016-04-29 17:12:15 -0700891
Ian Morris53b24b82015-03-29 14:00:05 +0100892 if (t) {
Mark Rutland6aa7de02017-10-23 14:07:29 -0700893 u8 tproto = READ_ONCE(t->parms.proto);
Eric Dumazet8560f222010-09-28 03:23:34 +0000894
Tom Herbert0d3c7032016-04-29 17:12:15 -0700895 if (tproto != ipproto && tproto != 0)
896 goto drop;
897 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
898 goto drop;
Eric Dumazetcbb49692018-12-21 07:47:51 -0800899 ipv6h = ipv6_hdr(skb);
Tom Herbert0d3c7032016-04-29 17:12:15 -0700900 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
901 goto drop;
902 if (iptunnel_pull_header(skb, 0, tpi->proto, false))
903 goto drop;
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700904 if (t->parms.collect_md) {
905 tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
906 if (!tun_dst)
Nikita V. Shirokov74c4b652017-12-06 17:15:43 -0800907 goto drop;
Alexei Starovoitov8d792662016-09-15 13:00:30 -0700908 }
909 ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
Tom Herbert0d3c7032016-04-29 17:12:15 -0700910 log_ecn_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Herbert Xu50fba2a2006-04-04 13:50:45 -0700912
Tom Herbert0d3c7032016-04-29 17:12:15 -0700913 rcu_read_unlock();
914
915 return ret;
916
917drop:
918 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700919 kfree_skb(skb);
920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900923static int ip4ip6_rcv(struct sk_buff *skb)
924{
Nicolas Dichtelca4aa972016-05-10 16:08:17 +0200925 return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
Tom Herbert0d3c7032016-04-29 17:12:15 -0700926 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900927}
928
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900929static int ip6ip6_rcv(struct sk_buff *skb)
930{
Tom Herbert0d3c7032016-04-29 17:12:15 -0700931 return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
932 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900933}
934
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800935struct ipv6_tel_txoption {
936 struct ipv6_txoptions ops;
937 __u8 dst_opt[8];
938};
939
940static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800942 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800944 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
945 opt->dst_opt[3] = 1;
946 opt->dst_opt[4] = encap_limit;
947 opt->dst_opt[5] = IPV6_TLV_PADN;
948 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Craig Gallek89a23c82017-04-26 14:37:45 -0400950 opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800951 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900955 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900957 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 *
959 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900960 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * doesn't match source of incoming packet.
962 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900963 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * 1 if conflict,
965 * 0 else
966 **/
967
Eric Dumazet92113bf2012-05-18 08:14:11 +0200968static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000969ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
972}
973
Steffen Klassertd5005142014-11-05 08:02:48 +0100974int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
975 const struct in6_addr *laddr,
976 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800977{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000978 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800979 int ret = 0;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200980 struct net *net = t->net;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800981
William Tu6712abc2017-12-01 15:26:08 -0800982 if (t->parms.collect_md)
983 return 1;
984
Steffen Klassertd5005142014-11-05 08:02:48 +0100985 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
986 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
987 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800988 struct net_device *ldev = NULL;
989
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100990 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800991 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100992 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800993
David Ahern232378e2018-03-13 08:29:37 -0700994 if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false,
995 0, IFA_F_TENTATIVE)))
Joe Perchesf3213832012-05-15 14:11:53 +0000996 pr_warn("%s xmit: Local address not yet configured!\n",
997 p->name);
Shmulik Ladkani908d1402017-10-21 00:25:15 +0300998 else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) &&
999 !ipv6_addr_is_multicast(raddr) &&
David Ahern232378e2018-03-13 08:29:37 -07001000 unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev,
1001 true, 0, IFA_F_TENTATIVE)))
Joe Perchesf3213832012-05-15 14:11:53 +00001002 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1003 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001004 else
1005 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +01001006 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001007 }
1008 return ret;
1009}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001010EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/**
Tom Herbert8eb30be2016-04-29 17:12:18 -07001013 * ip6_tnl_xmit - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001015 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001016 * @dsfield: dscp code for outer header
Tom Herbert8eb30be2016-04-29 17:12:18 -07001017 * @fl6: flow of tunneled packet
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001018 * @encap_limit: encapsulation limit
1019 * @pmtu: Path MTU is stored if packet is too big
Tom Herbert8eb30be2016-04-29 17:12:18 -07001020 * @proto: next header value
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 *
1022 * Description:
1023 * Build new header and do some sanity checks on the packet before sending
1024 * it.
1025 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001026 * Return:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001027 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001028 * -1 fail
1029 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 **/
1031
Tom Herbert8eb30be2016-04-29 17:12:18 -07001032int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
1033 struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
1034 __u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Patrick McHardy2941a482006-01-08 22:05:26 -08001036 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001037 struct net *net = t->net;
Pavel Emelyanov3dca02af2008-05-21 14:17:05 -07001038 struct net_device_stats *stats = &t->dev->stats;
WANG Cong199ab002017-04-25 14:37:15 -07001039 struct ipv6hdr *ipv6h;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001040 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001041 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct net_device *tdev;
1043 int mtu;
Xin Longd41bb332017-09-28 13:24:07 +08001044 unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
Tom Herbert058214a2016-05-18 09:06:17 -07001045 unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
1046 unsigned int max_headroom = psh_hlen;
Paolo Abenib5c2d492016-11-16 16:26:46 +01001047 bool use_cache = false;
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001048 u8 hop_limit;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001049 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001051 if (t->parms.collect_md) {
1052 hop_limit = skb_tunnel_info(skb)->key.ttl;
1053 goto route_lookup;
1054 } else {
1055 hop_limit = t->parms.hop_limit;
1056 }
1057
Steffen Klassertea3dc962014-11-05 08:03:50 +01001058 /* NBMA tunnel */
1059 if (ipv6_addr_any(&t->parms.raddr)) {
WANG Cong199ab002017-04-25 14:37:15 -07001060 if (skb->protocol == htons(ETH_P_IPV6)) {
1061 struct in6_addr *addr6;
1062 struct neighbour *neigh;
1063 int addr_type;
Steffen Klassertea3dc962014-11-05 08:03:50 +01001064
WANG Cong199ab002017-04-25 14:37:15 -07001065 if (!skb_dst(skb))
1066 goto tx_err_link_failure;
Steffen Klassertea3dc962014-11-05 08:03:50 +01001067
WANG Cong199ab002017-04-25 14:37:15 -07001068 neigh = dst_neigh_lookup(skb_dst(skb),
1069 &ipv6_hdr(skb)->daddr);
1070 if (!neigh)
1071 goto tx_err_link_failure;
Steffen Klassertea3dc962014-11-05 08:03:50 +01001072
WANG Cong199ab002017-04-25 14:37:15 -07001073 addr6 = (struct in6_addr *)&neigh->primary_key;
1074 addr_type = ipv6_addr_type(addr6);
Steffen Klassertea3dc962014-11-05 08:03:50 +01001075
WANG Cong199ab002017-04-25 14:37:15 -07001076 if (addr_type == IPV6_ADDR_ANY)
1077 addr6 = &ipv6_hdr(skb)->daddr;
Steffen Klassertea3dc962014-11-05 08:03:50 +01001078
WANG Cong199ab002017-04-25 14:37:15 -07001079 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1080 neigh_release(neigh);
1081 }
Eli Cooper23263ec2017-12-25 10:43:49 +08001082 } else if (t->parms.proto != 0 && !(t->parms.flags &
1083 (IP6_TNL_F_USE_ORIG_TCLASS |
1084 IP6_TNL_F_USE_ORIG_FWMARK))) {
1085 /* enable the cache only if neither the outer protocol nor the
1086 * routing decision depends on the current inner header value
Paolo Abenib5c2d492016-11-16 16:26:46 +01001087 */
1088 use_cache = true;
1089 }
1090
1091 if (use_cache)
Paolo Abeni607f7252016-02-12 15:43:54 +01001092 dst = dst_cache_get(&t->dst_cache);
Steffen Klassertd5005142014-11-05 08:02:48 +01001093
1094 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1095 goto tx_err_link_failure;
1096
Eric Dumazet89b02122011-07-28 04:32:25 +00001097 if (!dst) {
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001098route_lookup:
Liam McBirnie5f733ee2017-06-01 15:36:01 +10001099 /* add dsfield to flowlabel for route lookup */
1100 fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
1101
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001102 dst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001104 if (dst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -07001105 goto tx_err_link_failure;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001106 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1107 if (IS_ERR(dst)) {
1108 err = PTR_ERR(dst);
1109 dst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -08001110 goto tx_err_link_failure;
1111 }
Shmulik Ladkani3789cab2018-08-06 15:00:59 +03001112 if (t->parms.collect_md && ipv6_addr_any(&fl6->saddr) &&
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001113 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
1114 &fl6->daddr, 0, &fl6->saddr))
1115 goto tx_err_link_failure;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001116 ndst = dst;
Patrick McHardya57ebc92005-09-08 14:27:47 -07001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 tdev = dst->dev;
1120
1121 if (tdev == dev) {
1122 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +00001123 net_warn_ratelimited("%s: Local routing loop detected!\n",
1124 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto tx_err_dst_release;
1126 }
Xin Longd41bb332017-09-28 13:24:07 +08001127 mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001128 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 max_headroom += 8;
1130 mtu -= 8;
1131 }
Xin Long82a40772018-08-05 22:46:07 +08001132 mtu = max(mtu, skb->protocol == htons(ETH_P_IPV6) ?
1133 IPV6_MIN_MTU : IPV4_MIN_MTU);
Xin Longc9fefa02017-12-18 14:26:21 +08001134
Hangbin Liu7a1592b2019-12-22 10:51:13 +08001135 skb_dst_update_pmtu_no_confirm(skb, mtu);
Xin Longd41bb332017-09-28 13:24:07 +08001136 if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001137 *pmtu = mtu;
1138 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 goto tx_err_dst_release;
1140 }
1141
Tom Herbert8eb30be2016-04-29 17:12:18 -07001142 if (t->err_count > 0) {
1143 if (time_before(jiffies,
1144 t->err_time + IP6TUNNEL_ERR_TIMEO)) {
1145 t->err_count--;
1146
1147 dst_link_failure(skb);
1148 } else {
1149 t->err_count = 0;
1150 }
1151 }
1152
Nicolas Dichtel963a88b2013-09-02 15:34:57 +02001153 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
1156 * Okay, now see if we can stuff it in the buffer as-is.
1157 */
1158 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001159
Patrick McHardycfbba492007-07-09 15:33:40 -07001160 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1161 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001163
Ian Morrise5d08d72014-11-23 21:28:43 +00001164 new_skb = skb_realloc_headroom(skb, max_headroom);
1165 if (!new_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 goto tx_err_dst_release;
1167
1168 if (skb->sk)
1169 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +00001170 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 skb = new_skb;
1172 }
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001173
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001174 if (t->parms.collect_md) {
1175 if (t->encap.type != TUNNEL_ENCAP_NONE)
1176 goto tx_err_dst_release;
1177 } else {
Paolo Abenib5c2d492016-11-16 16:26:46 +01001178 if (use_cache && ndst)
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001179 dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1180 }
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001181 skb_dst_set(skb, dst);
1182
Hangbin Liu36feaac2018-08-31 16:52:01 +08001183 if (hop_limit == 0) {
1184 if (skb->protocol == htons(ETH_P_IP))
1185 hop_limit = ip_hdr(skb)->ttl;
1186 else if (skb->protocol == htons(ETH_P_IPV6))
1187 hop_limit = ipv6_hdr(skb)->hop_limit;
1188 else
1189 hop_limit = ip6_dst_hoplimit(dst);
1190 }
Hannes Frederic Sowa3d483052013-08-18 13:46:52 +02001191
Tom Herbert058214a2016-05-18 09:06:17 -07001192 /* Calculate max headroom for all the headers and adjust
1193 * needed_headroom if necessary.
1194 */
Tom Herbert8eb30be2016-04-29 17:12:18 -07001195 max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
Tom Herbert058214a2016-05-18 09:06:17 -07001196 + dst->header_len + t->hlen;
Tom Herbert8eb30be2016-04-29 17:12:18 -07001197 if (max_headroom > dev->needed_headroom)
1198 dev->needed_headroom = max_headroom;
1199
Tom Herbert058214a2016-05-18 09:06:17 -07001200 err = ip6_tnl_encap(skb, t, &proto, fl6);
1201 if (err)
1202 return err;
1203
Stefano Briviod4d576f2018-10-18 21:25:07 +02001204 if (encap_limit >= 0) {
1205 init_tel_txopt(&opt, encap_limit);
1206 ipv6_push_frag_opts(skb, &opt.ops, &proto);
1207 }
1208
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001209 skb_push(skb, sizeof(struct ipv6hdr));
1210 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001211 ipv6h = ipv6_hdr(skb);
Peter Dawson0e9a7092017-05-26 06:35:18 +10001212 ip6_flow_hdr(ipv6h, dsfield,
Tom Herbert42240902015-07-31 16:52:12 -07001213 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001214 ipv6h->hop_limit = hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001216 ipv6h->saddr = fl6->saddr;
1217 ipv6h->daddr = fl6->daddr;
David Miller79b16aa2015-04-05 22:19:09 -04001218 ip6tunnel_xmit(NULL, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return 0;
1220tx_err_link_failure:
1221 stats->tx_carrier_errors++;
1222 dst_link_failure(skb);
1223tx_err_dst_release:
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001224 dst_release(dst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001225 return err;
1226}
Tom Herbert8eb30be2016-04-29 17:12:18 -07001227EXPORT_SYMBOL(ip6_tnl_xmit);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001228
1229static inline int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001230ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1231{
1232 struct ip6_tnl *t = netdev_priv(dev);
Paolo Abeni76c0ddd82018-09-19 15:02:07 +02001233 const struct iphdr *iph;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001234 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001235 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001236 __u8 dsfield;
1237 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001238 u8 tproto;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001239 int err;
1240
Paolo Abeni76c0ddd82018-09-19 15:02:07 +02001241 iph = ip_hdr(skb);
Bernie Harris5146d1f2016-02-22 12:58:05 +13001242 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1243
Mark Rutland6aa7de02017-10-23 14:07:29 -07001244 tproto = READ_ONCE(t->parms.proto);
Steffen Klassertd5005142014-11-05 08:02:48 +01001245 if (tproto != IPPROTO_IPIP && tproto != 0)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001246 return -1;
1247
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001248 if (t->parms.collect_md) {
1249 struct ip_tunnel_info *tun_info;
1250 const struct ip_tunnel_key *key;
1251
1252 tun_info = skb_tunnel_info(skb);
1253 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1254 ip_tunnel_info_af(tun_info) != AF_INET6))
1255 return -1;
1256 key = &tun_info->key;
1257 memset(&fl6, 0, sizeof(fl6));
1258 fl6.flowi6_proto = IPPROTO_IPIP;
Shmulik Ladkani3789cab2018-08-06 15:00:59 +03001259 fl6.saddr = key->u.ipv6.src;
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001260 fl6.daddr = key->u.ipv6.dst;
1261 fl6.flowlabel = key->label;
Haishuang Yan46f8cd92017-06-17 11:38:05 +08001262 dsfield = key->tos;
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001263 } else {
1264 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1265 encap_limit = t->parms.encap_limit;
1266
1267 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1268 fl6.flowi6_proto = IPPROTO_IPIP;
1269
1270 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
Peter Dawson0e9a7092017-05-26 06:35:18 +10001271 dsfield = ipv4_get_dsfield(iph);
1272 else
1273 dsfield = ip6_tclass(t->parms.flowinfo);
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001274 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1275 fl6.flowi6_mark = skb->mark;
Craig Gallek0a473b82017-04-19 12:30:53 -04001276 else
1277 fl6.flowi6_mark = t->parms.fwmark;
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001278 }
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001279
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09001280 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
Haishuang Yan01f5bff2019-07-26 00:40:17 +08001281 dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph));
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09001282
Tom Herbertb8921ca2016-05-18 09:06:23 -07001283 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
1284 return -1;
1285
1286 skb_set_inner_ipproto(skb, IPPROTO_IPIP);
1287
Tom Herbert8eb30be2016-04-29 17:12:18 -07001288 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1289 IPPROTO_IPIP);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001290 if (err != 0) {
1291 /* XXX: send ICMP error even if DF is not set. */
1292 if (err == -EMSGSIZE)
1293 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1294 htonl(mtu));
1295 return -1;
1296 }
1297
1298 return 0;
1299}
1300
1301static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001302ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1303{
1304 struct ip6_tnl *t = netdev_priv(dev);
Paolo Abeni76c0ddd82018-09-19 15:02:07 +02001305 struct ipv6hdr *ipv6h;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001306 int encap_limit = -1;
1307 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001308 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001309 __u8 dsfield;
1310 __u32 mtu;
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001311 u8 tproto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001312 int err;
1313
Paolo Abeni76c0ddd82018-09-19 15:02:07 +02001314 ipv6h = ipv6_hdr(skb);
Mark Rutland6aa7de02017-10-23 14:07:29 -07001315 tproto = READ_ONCE(t->parms.proto);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001316 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
Steffen Klassertd5005142014-11-05 08:02:48 +01001317 ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001318 return -1;
1319
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001320 if (t->parms.collect_md) {
1321 struct ip_tunnel_info *tun_info;
1322 const struct ip_tunnel_key *key;
1323
1324 tun_info = skb_tunnel_info(skb);
1325 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1326 ip_tunnel_info_af(tun_info) != AF_INET6))
1327 return -1;
1328 key = &tun_info->key;
1329 memset(&fl6, 0, sizeof(fl6));
1330 fl6.flowi6_proto = IPPROTO_IPV6;
Shmulik Ladkani3789cab2018-08-06 15:00:59 +03001331 fl6.saddr = key->u.ipv6.src;
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001332 fl6.daddr = key->u.ipv6.dst;
1333 fl6.flowlabel = key->label;
Haishuang Yan46f8cd92017-06-17 11:38:05 +08001334 dsfield = key->tos;
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001335 } else {
1336 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Eric Dumazet21b995a2017-01-23 16:43:05 -08001337 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
1338 ipv6h = ipv6_hdr(skb);
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001339 if (offset > 0) {
1340 struct ipv6_tlv_tnl_enc_lim *tel;
1341
1342 tel = (void *)&skb_network_header(skb)[offset];
1343 if (tel->encap_limit == 0) {
1344 icmpv6_send(skb, ICMPV6_PARAMPROB,
1345 ICMPV6_HDR_FIELD, offset + 2);
1346 return -1;
1347 }
1348 encap_limit = tel->encap_limit - 1;
1349 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
1350 encap_limit = t->parms.encap_limit;
1351 }
1352
1353 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1354 fl6.flowi6_proto = IPPROTO_IPV6;
1355
1356 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
Peter Dawson0e9a7092017-05-26 06:35:18 +10001357 dsfield = ipv6_get_dsfield(ipv6h);
1358 else
1359 dsfield = ip6_tclass(t->parms.flowinfo);
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001360 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1361 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1362 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1363 fl6.flowi6_mark = skb->mark;
Craig Gallek0a473b82017-04-19 12:30:53 -04001364 else
1365 fl6.flowi6_mark = t->parms.fwmark;
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001366 }
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001367
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09001368 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
Haishuang Yan01f5bff2019-07-26 00:40:17 +08001369 dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h));
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09001370
Tom Herbert815d22e2016-05-18 09:06:22 -07001371 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
1372 return -1;
1373
1374 skb_set_inner_ipproto(skb, IPPROTO_IPV6);
1375
Tom Herbert8eb30be2016-04-29 17:12:18 -07001376 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1377 IPPROTO_IPV6);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001378 if (err != 0) {
1379 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001380 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001381 return -1;
1382 }
1383
1384 return 0;
1385}
1386
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001387static netdev_tx_t
Tom Herbert8eb30be2016-04-29 17:12:18 -07001388ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001389{
1390 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02af2008-05-21 14:17:05 -07001391 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001392 int ret;
1393
Willem de Bruijncb9f1b72018-12-30 17:24:36 -05001394 if (!pskb_inet_may_pull(skb))
1395 goto tx_err;
1396
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001397 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001398 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001399 ret = ip4ip6_tnl_xmit(skb, dev);
1400 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001401 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001402 ret = ip6ip6_tnl_xmit(skb, dev);
1403 break;
1404 default:
1405 goto tx_err;
1406 }
1407
1408 if (ret < 0)
1409 goto tx_err;
1410
Patrick McHardy6ed10652009-06-23 06:03:08 +00001411 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413tx_err:
1414 stats->tx_errors++;
1415 stats->tx_dropped++;
1416 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001417 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001420static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001423 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001424 struct flowi6 *fl6 = &t->fl.u.ip6;
Tom Herbert058214a2016-05-18 09:06:17 -07001425 int t_hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001427 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1428 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001431 fl6->saddr = p->laddr;
1432 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001433 fl6->flowi6_oif = p->link;
1434 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001437 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001439 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001441 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1442 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1445 dev->flags |= IFF_POINTOPOINT;
1446 else
1447 dev->flags &= ~IFF_POINTOPOINT;
1448
Tom Herbert058214a2016-05-18 09:06:17 -07001449 t->tun_hlen = 0;
1450 t->hlen = t->encap_hlen + t->tun_hlen;
1451 t_hlen = t->hlen + sizeof(struct ipv6hdr);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001454 int strict = (ipv6_addr_type(&p->raddr) &
1455 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1456
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001457 struct rt6_info *rt = rt6_lookup(t->net,
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001458 &p->raddr, &p->laddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001459 p->link, NULL, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Ian Morris63159f22015-03-29 14:00:04 +01001461 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return;
1463
David S. Millerd1918542011-12-28 20:19:20 -05001464 if (rt->dst.dev) {
1465 dev->hard_header_len = rt->dst.dev->hard_header_len +
Tom Herbert058214a2016-05-18 09:06:17 -07001466 t_hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Tom Herbert058214a2016-05-18 09:06:17 -07001468 dev->mtu = rt->dst.dev->mtu - t_hlen;
Anders Franzen381601e2010-11-24 05:47:18 +00001469 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Ian Morris67ba4152014-08-24 21:53:10 +01001470 dev->mtu -= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (dev->mtu < IPV6_MIN_MTU)
1473 dev->mtu = IPV6_MIN_MTU;
1474 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001475 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477}
1478
1479/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001480 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * @t: tunnel to be changed
1482 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 *
1484 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001485 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 **/
1487
1488static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001489ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001491 t->parms.laddr = p->laddr;
1492 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 t->parms.flags = p->flags;
1494 t->parms.hop_limit = p->hop_limit;
1495 t->parms.encap_limit = p->encap_limit;
1496 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001497 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001498 t->parms.proto = p->proto;
Craig Gallek0a473b82017-04-19 12:30:53 -04001499 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +01001500 dst_cache_reset(&t->dst_cache);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001501 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return 0;
1503}
1504
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001505static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1506{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001507 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001508 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1509 int err;
1510
1511 ip6_tnl_unlink(ip6n, t);
1512 synchronize_net();
1513 err = ip6_tnl_change(t, p);
1514 ip6_tnl_link(ip6n, t);
1515 netdev_state_change(t->dev);
1516 return err;
1517}
1518
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001519static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1520{
1521 /* for default tnl0 device allow to change only the proto */
1522 t->parms.proto = p->proto;
1523 netdev_state_change(t->dev);
1524 return 0;
1525}
1526
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001527static void
1528ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1529{
1530 p->laddr = u->laddr;
1531 p->raddr = u->raddr;
1532 p->flags = u->flags;
1533 p->hop_limit = u->hop_limit;
1534 p->encap_limit = u->encap_limit;
1535 p->flowinfo = u->flowinfo;
1536 p->link = u->link;
1537 p->proto = u->proto;
1538 memcpy(p->name, u->name, sizeof(u->name));
1539}
1540
1541static void
1542ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1543{
1544 u->laddr = p->laddr;
1545 u->raddr = p->raddr;
1546 u->flags = p->flags;
1547 u->hop_limit = p->hop_limit;
1548 u->encap_limit = p->encap_limit;
1549 u->flowinfo = p->flowinfo;
1550 u->link = p->link;
1551 u->proto = p->proto;
1552 memcpy(u->name, p->name, sizeof(u->name));
1553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001556 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * @dev: virtual device associated with tunnel
1558 * @ifr: parameters passed from userspace
1559 * @cmd: command to be performed
1560 *
1561 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001562 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001563 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 *
1565 * The possible commands are the following:
1566 * %SIOCGETTUNNEL: get tunnel parameters for device
1567 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1568 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1569 * %SIOCDELTUNNEL: delete tunnel
1570 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001571 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * initialization, can be used for creating other tunnel devices.
1573 *
1574 * Return:
1575 * 0 on success,
1576 * %-EFAULT if unable to copy data to or from userspace,
1577 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1578 * %-EINVAL if passed tunnel parameters are invalid,
1579 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1580 * %-ENODEV if attempting to change or delete a nonexisting device
1581 **/
1582
1583static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001584ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001588 struct __ip6_tnl_parm p1;
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001589 struct ip6_tnl *t = netdev_priv(dev);
1590 struct net *net = t->net;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001591 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Tom Herbert0d3c7032016-04-29 17:12:15 -07001593 memset(&p1, 0, sizeof(p1));
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 switch (cmd) {
1596 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001597 if (dev == ip6n->fb_tnl_dev) {
Ian Morris67ba4152014-08-24 21:53:10 +01001598 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 err = -EFAULT;
1600 break;
1601 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001602 ip6_tnl_parm_from_user(&p1, &p);
1603 t = ip6_tnl_locate(net, &p1, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001604 if (IS_ERR(t))
Nicolas Dichtel74462f02014-04-16 11:19:34 +02001605 t = netdev_priv(dev);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001606 } else {
1607 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001608 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001609 ip6_tnl_parm_to_user(&p, &t->parms);
Ian Morris67ba4152014-08-24 21:53:10 +01001610 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 err = -EFAULT;
1612 }
1613 break;
1614 case SIOCADDTUNNEL:
1615 case SIOCCHGTUNNEL:
1616 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001617 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001619 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001620 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001622 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001623 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1624 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001626 ip6_tnl_parm_from_user(&p1, &p);
1627 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001628 if (cmd == SIOCCHGTUNNEL) {
Nicolas Dichtel37355562015-03-16 15:56:05 +01001629 if (!IS_ERR(t)) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001630 if (t->dev != dev) {
1631 err = -EEXIST;
1632 break;
1633 }
1634 } else
1635 t = netdev_priv(dev);
Alexey Andriyanovacf722f2014-10-29 10:54:52 +03001636 if (dev == ip6n->fb_tnl_dev)
1637 err = ip6_tnl0_update(t, &p1);
1638 else
1639 err = ip6_tnl_update(t, &p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
Nicolas Dichtel37355562015-03-16 15:56:05 +01001641 if (!IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001643 ip6_tnl_parm_to_user(&p, &t->parms);
1644 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001645 err = -EFAULT;
1646
Nicolas Dichtel37355562015-03-16 15:56:05 +01001647 } else {
1648 err = PTR_ERR(t);
1649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651 case SIOCDELTUNNEL:
1652 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001653 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
1655
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001656 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001657 err = -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001658 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001660 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001661 ip6_tnl_parm_from_user(&p1, &p);
1662 t = ip6_tnl_locate(net, &p1, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01001663 if (IS_ERR(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001665 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001666 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001668 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001670 err = 0;
1671 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
1673 default:
1674 err = -EINVAL;
1675 }
1676 return err;
1677}
1678
1679/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001680 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * @dev: virtual device associated with tunnel
1682 * @new_mtu: the new mtu
1683 *
1684 * Return:
1685 * 0 on success,
1686 * %-EINVAL if mtu too small
1687 **/
1688
Tom Herbert79ecb902016-04-29 17:12:20 -07001689int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001691 struct ip6_tnl *tnl = netdev_priv(dev);
1692
Xin Long2fa771b2017-12-25 14:45:12 +08001693 if (tnl->parms.proto == IPPROTO_IPV6) {
1694 if (new_mtu < IPV6_MIN_MTU)
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001695 return -EINVAL;
1696 } else {
Xin Long2fa771b2017-12-25 14:45:12 +08001697 if (new_mtu < ETH_MIN_MTU)
Oussama Ghorbel582442d2013-10-03 14:49:26 +01001698 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Nicolas Dichtelf7ff1fd2018-05-31 10:59:33 +02001700 if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) {
1701 if (new_mtu > IP6_MAX_MTU - dev->hard_header_len)
1702 return -EINVAL;
1703 } else {
1704 if (new_mtu > IP_MAX_MTU - dev->hard_header_len)
1705 return -EINVAL;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 dev->mtu = new_mtu;
1708 return 0;
1709}
Tom Herbert79ecb902016-04-29 17:12:20 -07001710EXPORT_SYMBOL(ip6_tnl_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001712int ip6_tnl_get_iflink(const struct net_device *dev)
1713{
1714 struct ip6_tnl *t = netdev_priv(dev);
1715
1716 return t->parms.link;
1717}
1718EXPORT_SYMBOL(ip6_tnl_get_iflink);
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001719
Tom Herbert058214a2016-05-18 09:06:17 -07001720int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
1721 unsigned int num)
1722{
1723 if (num >= MAX_IPTUN_ENCAP_OPS)
1724 return -ERANGE;
1725
1726 return !cmpxchg((const struct ip6_tnl_encap_ops **)
1727 &ip6tun_encaps[num],
1728 NULL, ops) ? 0 : -1;
1729}
1730EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
1731
1732int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
1733 unsigned int num)
1734{
1735 int ret;
1736
1737 if (num >= MAX_IPTUN_ENCAP_OPS)
1738 return -ERANGE;
1739
1740 ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
1741 &ip6tun_encaps[num],
1742 ops, NULL) == ops) ? 0 : -1;
1743
1744 synchronize_net();
1745
1746 return ret;
1747}
1748EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
1749
1750int ip6_tnl_encap_setup(struct ip6_tnl *t,
1751 struct ip_tunnel_encap *ipencap)
1752{
1753 int hlen;
1754
1755 memset(&t->encap, 0, sizeof(t->encap));
1756
1757 hlen = ip6_encap_hlen(ipencap);
1758 if (hlen < 0)
1759 return hlen;
1760
1761 t->encap.type = ipencap->type;
1762 t->encap.sport = ipencap->sport;
1763 t->encap.dport = ipencap->dport;
1764 t->encap.flags = ipencap->flags;
1765
1766 t->encap_hlen = hlen;
1767 t->hlen = t->encap_hlen + t->tun_hlen;
1768
1769 return 0;
1770}
1771EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
1772
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001773static const struct net_device_ops ip6_tnl_netdev_ops = {
Steffen Klassert6c6151d2014-11-03 09:19:27 +01001774 .ndo_init = ip6_tnl_dev_init,
Eric Dumazet8560f222010-09-28 03:23:34 +00001775 .ndo_uninit = ip6_tnl_dev_uninit,
Tom Herbert8eb30be2016-04-29 17:12:18 -07001776 .ndo_start_xmit = ip6_tnl_start_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001777 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001778 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001779 .ndo_get_stats = ip6_get_stats,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001780 .ndo_get_iflink = ip6_tnl_get_iflink,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001781};
1782
Tom Herbert51c052d2016-05-18 09:06:21 -07001783#define IPXIPX_FEATURES (NETIF_F_SG | \
1784 NETIF_F_FRAGLIST | \
1785 NETIF_F_HIGHDMA | \
1786 NETIF_F_GSO_SOFTWARE | \
1787 NETIF_F_HW_CSUM)
Eric Dumazet8560f222010-09-28 03:23:34 +00001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001790 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * @dev: virtual device associated with tunnel
1792 *
1793 * Description:
1794 * Initialize function pointers and device parameters
1795 **/
1796
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001797static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001799 dev->netdev_ops = &ip6_tnl_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001800 dev->needs_free_netdev = true;
1801 dev->priv_destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 dev->type = ARPHRD_TUNNEL6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 dev->flags |= IFF_NOARP;
1805 dev->addr_len = sizeof(struct in6_addr);
Tom Herbert058214a2016-05-18 09:06:17 -07001806 dev->features |= NETIF_F_LLTX;
Eric Dumazet02875872014-10-05 18:38:35 -07001807 netif_keep_dst(dev);
Tom Herbert51c052d2016-05-18 09:06:21 -07001808
1809 dev->features |= IPXIPX_FEATURES;
1810 dev->hw_features |= IPXIPX_FEATURES;
1811
Nicolas Dichtele8377352013-08-20 12:16:06 +02001812 /* This perm addr will be used as interface identifier by IPv6 */
1813 dev->addr_assign_type = NET_ADDR_RANDOM;
1814 eth_random_addr(dev->perm_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
1817
1818/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001819 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * @dev: virtual device associated with tunnel
1821 **/
1822
Eric Dumazet8560f222010-09-28 03:23:34 +00001823static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001824ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Patrick McHardy2941a482006-01-08 22:05:26 -08001826 struct ip6_tnl *t = netdev_priv(dev);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001827 int ret;
Tom Herbert058214a2016-05-18 09:06:17 -07001828 int t_hlen;
Eric Dumazet8560f222010-09-28 03:23:34 +00001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 t->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001831 t->net = dev_net(dev);
WANG Cong1c213bd2014-02-13 11:46:28 -08001832 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Eric Dumazet8560f222010-09-28 03:23:34 +00001833 if (!dev->tstats)
1834 return -ENOMEM;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001835
Paolo Abeni607f7252016-02-12 15:43:54 +01001836 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
Tom Herbert0d3c7032016-04-29 17:12:15 -07001837 if (ret)
1838 goto free_stats;
1839
1840 ret = gro_cells_init(&t->gro_cells, dev);
1841 if (ret)
1842 goto destroy_dst;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001843
Tom Herbert79ecb902016-04-29 17:12:20 -07001844 t->tun_hlen = 0;
Tom Herbert058214a2016-05-18 09:06:17 -07001845 t->hlen = t->encap_hlen + t->tun_hlen;
1846 t_hlen = t->hlen + sizeof(struct ipv6hdr);
1847
1848 dev->type = ARPHRD_TUNNEL6;
1849 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1850 dev->mtu = ETH_DATA_LEN - t_hlen;
1851 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1852 dev->mtu -= 8;
Jarod Wilsonb96f9af2016-10-20 13:55:24 -04001853 dev->min_mtu = ETH_MIN_MTU;
Nicolas Dichtelf7ff1fd2018-05-31 10:59:33 +02001854 dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len;
Tom Herbert79ecb902016-04-29 17:12:20 -07001855
Eric Dumazet8560f222010-09-28 03:23:34 +00001856 return 0;
Tom Herbert0d3c7032016-04-29 17:12:15 -07001857
1858destroy_dst:
1859 dst_cache_destroy(&t->dst_cache);
1860free_stats:
1861 free_percpu(dev->tstats);
1862 dev->tstats = NULL;
1863
1864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001868 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * @dev: virtual device associated with tunnel
1870 **/
1871
Eric Dumazet8560f222010-09-28 03:23:34 +00001872static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Patrick McHardy2941a482006-01-08 22:05:26 -08001874 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001875 int err = ip6_tnl_dev_init_gen(dev);
1876
1877 if (err)
1878 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001879 ip6_tnl_link_config(t);
William Dauchy5311a692020-01-21 21:49:54 +01001880 if (t->parms.collect_md)
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001881 netif_keep_dst(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001886 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * @dev: fallback device
1888 *
1889 * Return: 0
1890 **/
1891
Eric Dumazet8560f222010-09-28 03:23:34 +00001892static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Patrick McHardy2941a482006-01-08 22:05:26 -08001894 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001895 struct net *net = dev_net(dev);
1896 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001897
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001898 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001900
Eric Dumazetcf778b02012-01-12 04:41:32 +00001901 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001905static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[],
1906 struct netlink_ext_ack *extack)
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001907{
1908 u8 proto;
1909
Susant Sahanic8965932014-05-10 00:11:32 +05301910 if (!data || !data[IFLA_IPTUN_PROTO])
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001911 return 0;
1912
1913 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1914 if (proto != IPPROTO_IPV6 &&
1915 proto != IPPROTO_IPIP &&
1916 proto != 0)
1917 return -EINVAL;
1918
1919 return 0;
1920}
1921
1922static void ip6_tnl_netlink_parms(struct nlattr *data[],
1923 struct __ip6_tnl_parm *parms)
1924{
1925 memset(parms, 0, sizeof(*parms));
1926
1927 if (!data)
1928 return;
1929
1930 if (data[IFLA_IPTUN_LINK])
1931 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1932
1933 if (data[IFLA_IPTUN_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001934 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001935
1936 if (data[IFLA_IPTUN_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001937 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001938
1939 if (data[IFLA_IPTUN_TTL])
1940 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1941
1942 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1943 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1944
1945 if (data[IFLA_IPTUN_FLOWINFO])
Nicolas Dichtel1ff05fb2012-11-15 04:06:42 +00001946 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001947
1948 if (data[IFLA_IPTUN_FLAGS])
1949 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1950
1951 if (data[IFLA_IPTUN_PROTO])
1952 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001953
1954 if (data[IFLA_IPTUN_COLLECT_METADATA])
1955 parms->collect_md = true;
Craig Gallek0a473b82017-04-19 12:30:53 -04001956
1957 if (data[IFLA_IPTUN_FWMARK])
1958 parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001959}
1960
Tom Herbertb3a27b52016-05-18 09:06:20 -07001961static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
1962 struct ip_tunnel_encap *ipencap)
1963{
1964 bool ret = false;
1965
1966 memset(ipencap, 0, sizeof(*ipencap));
1967
1968 if (!data)
1969 return ret;
1970
1971 if (data[IFLA_IPTUN_ENCAP_TYPE]) {
1972 ret = true;
1973 ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
1974 }
1975
1976 if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
1977 ret = true;
1978 ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
1979 }
1980
1981 if (data[IFLA_IPTUN_ENCAP_SPORT]) {
1982 ret = true;
1983 ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
1984 }
1985
1986 if (data[IFLA_IPTUN_ENCAP_DPORT]) {
1987 ret = true;
1988 ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
1989 }
1990
1991 return ret;
1992}
1993
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001994static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001995 struct nlattr *tb[], struct nlattr *data[],
1996 struct netlink_ext_ack *extack)
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001997{
1998 struct net *net = dev_net(dev);
Alexei Starovoitov8d792662016-09-15 13:00:30 -07001999 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Tom Herbertb3a27b52016-05-18 09:06:20 -07002000 struct ip_tunnel_encap ipencap;
Xin Longa6aa8042018-02-27 19:19:40 +08002001 struct ip6_tnl *nt, *t;
2002 int err;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002003
2004 nt = netdev_priv(dev);
Tom Herbertb3a27b52016-05-18 09:06:20 -07002005
2006 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
Xin Longa6aa8042018-02-27 19:19:40 +08002007 err = ip6_tnl_encap_setup(nt, &ipencap);
Tom Herbertb3a27b52016-05-18 09:06:20 -07002008 if (err < 0)
2009 return err;
2010 }
2011
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002012 ip6_tnl_netlink_parms(data, &nt->parms);
2013
Alexei Starovoitov8d792662016-09-15 13:00:30 -07002014 if (nt->parms.collect_md) {
2015 if (rtnl_dereference(ip6n->collect_md_tun))
2016 return -EEXIST;
2017 } else {
2018 t = ip6_tnl_locate(net, &nt->parms, 0);
2019 if (!IS_ERR(t))
2020 return -EEXIST;
2021 }
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002022
Xin Longa6aa8042018-02-27 19:19:40 +08002023 err = ip6_tnl_create2(dev);
2024 if (!err && tb[IFLA_MTU])
2025 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2026
2027 return err;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002028}
2029
2030static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02002031 struct nlattr *data[],
2032 struct netlink_ext_ack *extack)
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002033{
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02002034 struct ip6_tnl *t = netdev_priv(dev);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002035 struct __ip6_tnl_parm p;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02002036 struct net *net = t->net;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002037 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Tom Herbertb3a27b52016-05-18 09:06:20 -07002038 struct ip_tunnel_encap ipencap;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002039
2040 if (dev == ip6n->fb_tnl_dev)
2041 return -EINVAL;
2042
Tom Herbertb3a27b52016-05-18 09:06:20 -07002043 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
2044 int err = ip6_tnl_encap_setup(t, &ipencap);
2045
2046 if (err < 0)
2047 return err;
2048 }
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002049 ip6_tnl_netlink_parms(data, &p);
Alexei Starovoitov8d792662016-09-15 13:00:30 -07002050 if (p.collect_md)
2051 return -EINVAL;
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002052
2053 t = ip6_tnl_locate(net, &p, 0);
Nicolas Dichtel37355562015-03-16 15:56:05 +01002054 if (!IS_ERR(t)) {
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002055 if (t->dev != dev)
2056 return -EEXIST;
2057 } else
2058 t = netdev_priv(dev);
2059
2060 return ip6_tnl_update(t, &p);
2061}
2062
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01002063static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
2064{
2065 struct net *net = dev_net(dev);
2066 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2067
2068 if (dev != ip6n->fb_tnl_dev)
2069 unregister_netdevice_queue(dev, head);
2070}
2071
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00002072static size_t ip6_tnl_get_size(const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002073{
2074 return
2075 /* IFLA_IPTUN_LINK */
2076 nla_total_size(4) +
2077 /* IFLA_IPTUN_LOCAL */
2078 nla_total_size(sizeof(struct in6_addr)) +
2079 /* IFLA_IPTUN_REMOTE */
2080 nla_total_size(sizeof(struct in6_addr)) +
2081 /* IFLA_IPTUN_TTL */
2082 nla_total_size(1) +
2083 /* IFLA_IPTUN_ENCAP_LIMIT */
2084 nla_total_size(1) +
2085 /* IFLA_IPTUN_FLOWINFO */
2086 nla_total_size(4) +
2087 /* IFLA_IPTUN_FLAGS */
2088 nla_total_size(4) +
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00002089 /* IFLA_IPTUN_PROTO */
2090 nla_total_size(1) +
Tom Herbertb3a27b52016-05-18 09:06:20 -07002091 /* IFLA_IPTUN_ENCAP_TYPE */
2092 nla_total_size(2) +
2093 /* IFLA_IPTUN_ENCAP_FLAGS */
2094 nla_total_size(2) +
2095 /* IFLA_IPTUN_ENCAP_SPORT */
2096 nla_total_size(2) +
2097 /* IFLA_IPTUN_ENCAP_DPORT */
2098 nla_total_size(2) +
Alexei Starovoitov8d792662016-09-15 13:00:30 -07002099 /* IFLA_IPTUN_COLLECT_METADATA */
2100 nla_total_size(0) +
Craig Gallek0a473b82017-04-19 12:30:53 -04002101 /* IFLA_IPTUN_FWMARK */
2102 nla_total_size(4) +
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002103 0;
2104}
2105
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00002106static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002107{
2108 struct ip6_tnl *tunnel = netdev_priv(dev);
2109 struct __ip6_tnl_parm *parm = &tunnel->parms;
2110
2111 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
Jiri Benc930345e2015-03-29 16:59:25 +02002112 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
2113 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002114 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
2115 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
2116 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00002117 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04002118 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
2119 nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark))
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002120 goto nla_put_failure;
Tom Herbertb3a27b52016-05-18 09:06:20 -07002121
Alexei Starovoitov8d792662016-09-15 13:00:30 -07002122 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
2123 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
2124 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
2125 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
Tom Herbertb3a27b52016-05-18 09:06:20 -07002126 goto nla_put_failure;
2127
Alexei Starovoitov8d792662016-09-15 13:00:30 -07002128 if (parm->collect_md)
2129 if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
2130 goto nla_put_failure;
Craig Gallek0a473b82017-04-19 12:30:53 -04002131
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002132 return 0;
2133
2134nla_put_failure:
2135 return -EMSGSIZE;
2136}
2137
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002138struct net *ip6_tnl_get_link_net(const struct net_device *dev)
2139{
2140 struct ip6_tnl *tunnel = netdev_priv(dev);
2141
2142 return tunnel->net;
2143}
2144EXPORT_SYMBOL(ip6_tnl_get_link_net);
2145
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002146static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
2147 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
2148 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
2149 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
2150 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
2151 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
2152 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
2153 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
2154 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
Tom Herbertb3a27b52016-05-18 09:06:20 -07002155 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
2156 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
2157 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
2158 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
Alexei Starovoitov8d792662016-09-15 13:00:30 -07002159 [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
Craig Gallek0a473b82017-04-19 12:30:53 -04002160 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002161};
2162
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002163static struct rtnl_link_ops ip6_link_ops __read_mostly = {
2164 .kind = "ip6tnl",
2165 .maxtype = IFLA_IPTUN_MAX,
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002166 .policy = ip6_tnl_policy,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002167 .priv_size = sizeof(struct ip6_tnl),
Nicolas Dichtel0b112452012-11-14 05:14:00 +00002168 .setup = ip6_tnl_dev_setup,
2169 .validate = ip6_tnl_validate,
2170 .newlink = ip6_tnl_newlink,
2171 .changelink = ip6_tnl_changelink,
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01002172 .dellink = ip6_tnl_dellink,
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00002173 .get_size = ip6_tnl_get_size,
2174 .fill_info = ip6_tnl_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002175 .get_link_net = ip6_tnl_get_link_net,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002176};
2177
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00002178static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09002179 .handler = ip4ip6_rcv,
2180 .err_handler = ip4ip6_err,
2181 .priority = 1,
2182};
2183
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00002184static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07002185 .handler = ip6ip6_rcv,
2186 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08002187 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188};
2189
Eric Dumazetbb401ca2017-09-19 16:27:08 -07002190static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07002191{
Nicolas Dichtel1e9f3d62013-11-14 15:47:03 +01002192 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02002193 struct net_device *dev, *aux;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07002194 int h;
2195 struct ip6_tnl *t;
2196
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02002197 for_each_netdev_safe(net, dev, aux)
2198 if (dev->rtnl_link_ops == &ip6_link_ops)
Eric Dumazetbb401ca2017-09-19 16:27:08 -07002199 unregister_netdevice_queue(dev, list);
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02002200
Jiri Kosinae87a8f22016-08-10 11:03:35 +02002201 for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00002202 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Ian Morris53b24b82015-03-29 14:00:05 +01002203 while (t) {
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02002204 /* If dev is in the same netns, it has already
2205 * been added to the list by the previous loop.
2206 */
2207 if (!net_eq(dev_net(t->dev), net))
Eric Dumazetbb401ca2017-09-19 16:27:08 -07002208 unregister_netdevice_queue(t->dev, list);
Eric Dumazet94767632010-09-15 20:25:34 +00002209 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00002210 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07002211 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07002212}
2213
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002214static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002215{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00002216 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00002217 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002218 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002219
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07002220 ip6n->tnls[0] = ip6n->tnls_wc;
2221 ip6n->tnls[1] = ip6n->tnls_r_l;
2222
Eric Dumazet79134e62018-03-08 12:51:41 -08002223 if (!net_has_fallback_tunnels(net))
2224 return 0;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07002225 err = -ENOMEM;
2226 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
Tom Gundersenc835a672014-07-14 16:37:24 +02002227 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07002228
2229 if (!ip6n->fb_tnl_dev)
2230 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08002231 dev_net_set(ip6n->fb_tnl_dev, net);
Nicolas Dichtelbb814092013-10-01 18:05:00 +02002232 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02002233 /* FB netdevice is special: we have one, and only one per netns.
2234 * Allowing to move it to another netns is clearly unsafe.
2235 */
2236 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07002237
Eric Dumazet8560f222010-09-28 03:23:34 +00002238 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
2239 if (err < 0)
2240 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07002241
2242 err = register_netdev(ip6n->fb_tnl_dev);
2243 if (err < 0)
2244 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00002245
2246 t = netdev_priv(ip6n->fb_tnl_dev);
2247
2248 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002249 return 0;
2250
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07002251err_register:
David S. Millercf124db2017-05-08 12:52:56 -04002252 free_netdev(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07002253err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002254 return err;
2255}
2256
Eric Dumazetbb401ca2017-09-19 16:27:08 -07002257static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002258{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07002259 struct net *net;
2260 LIST_HEAD(list);
2261
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07002262 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07002263 list_for_each_entry(net, net_list, exit_list)
2264 ip6_tnl_destroy_tunnels(net, &list);
2265 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07002266 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002267}
2268
2269static struct pernet_operations ip6_tnl_net_ops = {
2270 .init = ip6_tnl_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07002271 .exit_batch = ip6_tnl_exit_batch_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00002272 .id = &ip6_tnl_net_id,
2273 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002274};
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276/**
2277 * ip6_tunnel_init - register protocol and reserve needed resources
2278 *
2279 * Return: 0 on success
2280 **/
2281
2282static int __init ip6_tunnel_init(void)
2283{
2284 int err;
2285
Xin Long8c22dab2017-09-15 15:58:33 +08002286 if (!ipv6_mod_enabled())
2287 return -EOPNOTSUPP;
2288
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00002289 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07002290 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00002291 goto out_pernet;
2292
2293 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
2294 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002295 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00002296 goto out_ip4ip6;
2297 }
2298
2299 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
2300 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002301 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00002302 goto out_ip6ip6;
2303 }
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002304 err = rtnl_link_register(&ip6_link_ops);
2305 if (err < 0)
2306 goto rtnl_link_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00002309
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002310rtnl_link_failed:
2311 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00002312out_ip6ip6:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09002313 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00002314out_ip4ip6:
2315 unregister_pernet_device(&ip6_tnl_net_ops);
2316out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return err;
2318}
2319
2320/**
2321 * ip6_tunnel_cleanup - free resources and unregister protocol
2322 **/
2323
2324static void __exit ip6_tunnel_cleanup(void)
2325{
Nicolas Dichtelc075b132012-11-09 06:10:01 +00002326 rtnl_link_unregister(&ip6_link_ops);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09002327 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00002328 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09002329
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08002330 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00002331 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00002333 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336module_init(ip6_tunnel_init);
2337module_exit(ip6_tunnel_cleanup);