blob: c0b138c2099256bdcb041caa72407f234963c601 [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/*
3 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
4 * Linux INET6 implementation
5 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Changes:
11 * Roger Venning <r.venning@telstra.com>: 6to4 support
12 * Nate Thompson <nate@thebog.net>: 6to4 support
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -040013 * Fred Templin <fred.l.templin@boeing.com>: isatap support
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/socket.h>
23#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/net.h>
25#include <linux/in6.h>
26#include <linux/netdevice.h>
27#include <linux/if_arp.h>
28#include <linux/icmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/netfilter_ipv4.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080033#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <net/sock.h>
36#include <net/snmp.h>
37
38#include <net/ipv6.h>
39#include <net/protocol.h>
40#include <net/transp_v6.h>
41#include <net/ip6_fib.h>
42#include <net/ip6_route.h>
43#include <net/ndisc.h>
44#include <net/addrconf.h>
45#include <net/ip.h>
46#include <net/udp.h>
47#include <net/icmp.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000048#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/inet_ecn.h>
50#include <net/xfrm.h>
51#include <net/dsfield.h>
Pavel Emelyanov8190d902008-04-16 01:15:17 -070052#include <net/net_namespace.h>
53#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
57
58 For comments look at net/ipv4/ip_gre.c --ANK
59 */
60
Jiri Kosinae87a8f22016-08-10 11:03:35 +020061#define IP6_SIT_HASH_SIZE 16
Al Viroe69a4ad2006-11-14 20:56:00 -080062#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +000064static bool log_ecn_error = true;
65module_param(log_ecn_error, bool, 0644);
66MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
67
Eric Dumazet15fc1f72010-09-27 00:38:18 +000068static int ipip6_tunnel_init(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void ipip6_tunnel_setup(struct net_device *dev);
Eric Dumazet15fc1f72010-09-27 00:38:18 +000070static void ipip6_dev_free(struct net_device *dev);
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +000071static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
72 __be32 *v4dst);
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +000073static struct rtnl_link_ops sit_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030075static unsigned int sit_net_id __read_mostly;
Pavel Emelyanov8190d902008-04-16 01:15:17 -070076struct sit_net {
Jiri Kosinae87a8f22016-08-10 11:03:35 +020077 struct ip_tunnel __rcu *tunnels_r_l[IP6_SIT_HASH_SIZE];
78 struct ip_tunnel __rcu *tunnels_r[IP6_SIT_HASH_SIZE];
79 struct ip_tunnel __rcu *tunnels_l[IP6_SIT_HASH_SIZE];
Eric Dumazet3a43be32010-09-15 11:35:10 +000080 struct ip_tunnel __rcu *tunnels_wc[1];
81 struct ip_tunnel __rcu **tunnels[4];
Pavel Emelyanov29182172008-04-16 01:16:38 -070082
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -070083 struct net_device *fb_tunnel_dev;
Pavel Emelyanov8190d902008-04-16 01:15:17 -070084};
85
Christoph Hellwigfd5d6872020-05-19 15:03:15 +020086static inline struct sit_net *dev_to_sit_net(struct net_device *dev)
87{
88 struct ip_tunnel *t = netdev_priv(dev);
89
90 return net_generic(t->net, sit_net_id);
91}
92
Eric Dumazet4543c102009-10-23 17:52:14 +000093/*
94 * Must be invoked with rcu_read_lock
95 */
Eldad Zack3e866702012-04-01 07:49:01 +000096static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
David Ahern3051fbe2017-10-30 10:07:17 -070097 struct net_device *dev,
98 __be32 remote, __be32 local,
99 int sifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Eric Dumazet3a43be32010-09-15 11:35:10 +0000101 unsigned int h0 = HASH(remote);
102 unsigned int h1 = HASH(local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct ip_tunnel *t;
Pavel Emelyanov29182172008-04-16 01:16:38 -0700104 struct sit_net *sitn = net_generic(net, sit_net_id);
David Ahern3051fbe2017-10-30 10:07:17 -0700105 int ifindex = dev ? dev->ifindex : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Amerigo Wange086cad2012-11-11 21:52:34 +0000107 for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (local == t->parms.iph.saddr &&
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000109 remote == t->parms.iph.daddr &&
David Ahern3051fbe2017-10-30 10:07:17 -0700110 (!dev || !t->parms.link || ifindex == t->parms.link ||
111 sifindex == t->parms.link) &&
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000112 (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return t;
114 }
Amerigo Wange086cad2012-11-11 21:52:34 +0000115 for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000116 if (remote == t->parms.iph.daddr &&
David Ahern3051fbe2017-10-30 10:07:17 -0700117 (!dev || !t->parms.link || ifindex == t->parms.link ||
118 sifindex == t->parms.link) &&
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000119 (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return t;
121 }
Amerigo Wange086cad2012-11-11 21:52:34 +0000122 for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000123 if (local == t->parms.iph.saddr &&
David Ahern3051fbe2017-10-30 10:07:17 -0700124 (!dev || !t->parms.link || ifindex == t->parms.link ||
125 sifindex == t->parms.link) &&
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000126 (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return t;
128 }
Eric Dumazet4543c102009-10-23 17:52:14 +0000129 t = rcu_dereference(sitn->tunnels_wc[0]);
Ian Morris53b24b82015-03-29 14:00:05 +0100130 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return t;
132 return NULL;
133}
134
Eric Dumazet3a43be32010-09-15 11:35:10 +0000135static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700136 struct ip_tunnel_parm *parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900138 __be32 remote = parms->iph.daddr;
139 __be32 local = parms->iph.saddr;
Eric Dumazet3a43be32010-09-15 11:35:10 +0000140 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int prio = 0;
142
143 if (remote) {
144 prio |= 2;
145 h ^= HASH(remote);
146 }
147 if (local) {
148 prio |= 1;
149 h ^= HASH(local);
150 }
Pavel Emelyanov29182172008-04-16 01:16:38 -0700151 return &sitn->tunnels[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Eric Dumazet3a43be32010-09-15 11:35:10 +0000154static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700155 struct ip_tunnel *t)
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900156{
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700157 return __ipip6_bucket(sitn, &t->parms);
YOSHIFUJI Hideaki420fe232007-04-24 20:44:47 +0900158}
159
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700160static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Eric Dumazet3a43be32010-09-15 11:35:10 +0000162 struct ip_tunnel __rcu **tp;
163 struct ip_tunnel *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Eric Dumazet3a43be32010-09-15 11:35:10 +0000165 for (tp = ipip6_bucket(sitn, t);
166 (iter = rtnl_dereference(*tp)) != NULL;
167 tp = &iter->next) {
168 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000169 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 break;
171 }
172 }
173}
174
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700175static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Eric Dumazet3a43be32010-09-15 11:35:10 +0000177 struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Eric Dumazetcf778b02012-01-12 04:41:32 +0000179 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
180 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
YOSHIFUJI Hideaki / 吉藤英明e0c93942009-10-11 03:31:34 +0000183static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000184{
185#ifdef CONFIG_IPV6_SIT_6RD
YOSHIFUJI Hideaki / 吉藤英明e0c93942009-10-11 03:31:34 +0000186 struct ip_tunnel *t = netdev_priv(dev);
187
Eric Dumazet79134e62018-03-08 12:51:41 -0800188 if (dev == sitn->fb_tunnel_dev || !sitn->fb_tunnel_dev) {
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000189 ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
190 t->ip6rd.relay_prefix = 0;
191 t->ip6rd.prefixlen = 16;
192 t->ip6rd.relay_prefixlen = 0;
193 } else {
194 struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
195 memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
196 }
197#endif
198}
199
Nicolas Dichtelf37234162012-11-14 05:14:07 +0000200static int ipip6_tunnel_create(struct net_device *dev)
201{
202 struct ip_tunnel *t = netdev_priv(dev);
203 struct net *net = dev_net(dev);
204 struct sit_net *sitn = net_generic(net, sit_net_id);
205 int err;
206
Jakub Kicinski5a1b7e12021-10-12 09:06:34 -0700207 __dev_addr_set(dev, &t->parms.iph.saddr, 4);
Steffen Klassertebe084a2014-11-03 09:19:29 +0100208 memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
Nicolas Dichtelf37234162012-11-14 05:14:07 +0000209
Nicolas Dichteld440b722012-11-15 04:06:41 +0000210 if ((__force u16)t->parms.i_flags & SIT_ISATAP)
Nicolas Dichtelf37234162012-11-14 05:14:07 +0000211 dev->priv_flags |= IFF_ISATAP;
212
Thadeu Lima de Souza Cascardo87e57392016-01-25 11:29:19 -0200213 dev->rtnl_link_ops = &sit_link_ops;
214
Nicolas Dichtelf37234162012-11-14 05:14:07 +0000215 err = register_netdevice(dev);
216 if (err < 0)
217 goto out;
218
Steffen Klassertebe084a2014-11-03 09:19:29 +0100219 ipip6_tunnel_clone_6rd(dev, sitn);
220
Nicolas Dichtelf37234162012-11-14 05:14:07 +0000221 ipip6_tunnel_link(sitn, t);
222 return 0;
223
224out:
225 return err;
226}
227
Eric Dumazet3a43be32010-09-15 11:35:10 +0000228static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700229 struct ip_tunnel_parm *parms, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Al Viroe69a4ad2006-11-14 20:56:00 -0800231 __be32 remote = parms->iph.daddr;
232 __be32 local = parms->iph.saddr;
Eric Dumazet3a43be32010-09-15 11:35:10 +0000233 struct ip_tunnel *t, *nt;
234 struct ip_tunnel __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 char name[IFNAMSIZ];
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700237 struct sit_net *sitn = net_generic(net, sit_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Eric Dumazet3a43be32010-09-15 11:35:10 +0000239 for (tp = __ipip6_bucket(sitn, parms);
240 (t = rtnl_dereference(*tp)) != NULL;
241 tp = &t->next) {
Sascha Hlusiak8db99e52009-05-19 12:56:48 +0000242 if (local == t->parms.iph.saddr &&
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000243 remote == t->parms.iph.daddr &&
244 parms->link == t->parms.link) {
Sascha Hlusiak8db99e52009-05-19 12:56:48 +0000245 if (create)
246 return NULL;
247 else
248 return t;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 if (!create)
252 goto failed;
253
Eric Dumazetb95211e2018-04-05 06:39:28 -0700254 if (parms->name[0]) {
255 if (!dev_valid_name(parms->name))
256 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 strlcpy(name, parms->name, IFNAMSIZ);
Eric Dumazetb95211e2018-04-05 06:39:28 -0700258 } else {
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000259 strcpy(name, "sit%d");
Eric Dumazetb95211e2018-04-05 06:39:28 -0700260 }
Tom Gundersenc835a672014-07-14 16:37:24 +0200261 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
262 ipip6_tunnel_setup);
Ian Morris63159f22015-03-29 14:00:04 +0100263 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return NULL;
265
Pavel Emelyanov7a971462008-04-16 01:17:18 -0700266 dev_net_set(dev, net);
267
Patrick McHardy2941a482006-01-08 22:05:26 -0800268 nt = netdev_priv(dev);
Stephen Hemminger1326c3d2008-11-20 20:33:56 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 nt->parms = *parms;
Nicolas Dichtelf37234162012-11-14 05:14:07 +0000271 if (ipip6_tunnel_create(dev) < 0)
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800272 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
zhang kai261ba782021-06-02 18:36:26 +0800274 if (!parms->name[0])
275 strcpy(parms->name, dev->name);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return nt;
278
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800279failed_free:
David S. Millercf124db2017-05-08 12:52:56 -0400280 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281failed:
282 return NULL;
283}
284
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000285#define for_each_prl_rcu(start) \
286 for (prl = rcu_dereference(start); \
287 prl; \
288 prl = rcu_dereference(prl->next))
289
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400290static struct ip_tunnel_prl_entry *
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900291__ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400292{
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000293 struct ip_tunnel_prl_entry *prl;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400294
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000295 for_each_prl_rcu(t->prl)
296 if (prl->addr == addr)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400297 break;
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000298 return prl;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400299
300}
301
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +0200302static int ipip6_tunnel_get_prl(struct net_device *dev, struct ip_tunnel_prl __user *a)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900303{
Christoph Hellwigfd5d6872020-05-19 15:03:15 +0200304 struct ip_tunnel *t = netdev_priv(dev);
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700305 struct ip_tunnel_prl kprl, *kp;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900306 struct ip_tunnel_prl_entry *prl;
307 unsigned int cmax, c = 0, ca, len;
308 int ret = 0;
309
Christoph Hellwigfd5d6872020-05-19 15:03:15 +0200310 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
311 return -EINVAL;
312
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700313 if (copy_from_user(&kprl, a, sizeof(kprl)))
314 return -EFAULT;
315 cmax = kprl.datalen / sizeof(kprl);
316 if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900317 cmax = 1;
318
319 /* For simple GET or for root users,
320 * we try harder to allocate.
321 */
322 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
Vasily Averin1b51d822021-07-19 13:44:50 +0300323 kcalloc(cmax, sizeof(*kp), GFP_KERNEL_ACCOUNT | __GFP_NOWARN) :
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900324 NULL;
325
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000326 rcu_read_lock();
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900327
kernel test robot284fda12021-03-27 10:29:32 +0100328 ca = min(t->prl_count, cmax);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900329
330 if (!kp) {
331 /* We don't try hard to allocate much memory for
332 * non-root users.
333 * For root users, retry allocating enough memory for
334 * the answer.
335 */
Vasily Averin1b51d822021-07-19 13:44:50 +0300336 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC | __GFP_ACCOUNT |
337 __GFP_NOWARN);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900338 if (!kp) {
339 ret = -ENOMEM;
340 goto out;
341 }
342 }
343
344 c = 0;
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000345 for_each_prl_rcu(t->prl) {
Sascha Hlusiak298bf122009-09-29 11:27:05 +0000346 if (c >= cmax)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900347 break;
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700348 if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900349 continue;
350 kp[c].addr = prl->addr;
351 kp[c].flags = prl->flags;
352 c++;
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700353 if (kprl.addr != htonl(INADDR_ANY))
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900354 break;
355 }
356out:
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000357 rcu_read_unlock();
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900358
359 len = sizeof(*kp) * c;
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700360 ret = 0;
361 if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
362 ret = -EFAULT;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900363
364 kfree(kp);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900365
YOSHIFUJI Hideaki2b4743b2008-06-16 16:48:20 -0700366 return ret;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900367}
368
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400369static int
370ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
371{
372 struct ip_tunnel_prl_entry *p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900373 int err = 0;
374
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900375 if (a->addr == htonl(INADDR_ANY))
376 return -EINVAL;
377
Eric Dumazetaac4ddd2010-06-01 00:26:58 -0700378 ASSERT_RTNL();
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400379
Eric Dumazet3a43be32010-09-15 11:35:10 +0000380 for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900381 if (p->addr == a->addr) {
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000382 if (chg) {
383 p->flags = a->flags;
384 goto out;
385 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900386 err = -EEXIST;
387 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400388 }
389 }
390
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900391 if (chg) {
392 err = -ENXIO;
393 goto out;
394 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400395
396 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900397 if (!p) {
398 err = -ENOBUFS;
399 goto out;
400 }
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400401
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400402 p->next = t->prl;
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900403 p->addr = a->addr;
404 p->flags = a->flags;
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000405 t->prl_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +0000406 rcu_assign_pointer(t->prl, p);
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900407out:
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900408 return err;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400409}
410
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000411static void prl_list_destroy_rcu(struct rcu_head *head)
412{
413 struct ip_tunnel_prl_entry *p, *n;
414
415 p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
416 do {
Eric Dumazet753ea8e2011-01-20 07:16:24 +0000417 n = rcu_dereference_protected(p->next, 1);
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000418 kfree(p);
419 p = n;
420 } while (p);
421}
422
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400423static int
424ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
425{
Eric Dumazet753ea8e2011-01-20 07:16:24 +0000426 struct ip_tunnel_prl_entry *x;
427 struct ip_tunnel_prl_entry __rcu **p;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900428 int err = 0;
429
Eric Dumazetaac4ddd2010-06-01 00:26:58 -0700430 ASSERT_RTNL();
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400431
YOSHIFUJI Hideaki0009ae12008-03-22 17:50:59 +0900432 if (a && a->addr != htonl(INADDR_ANY)) {
Eric Dumazet753ea8e2011-01-20 07:16:24 +0000433 for (p = &t->prl;
434 (x = rtnl_dereference(*p)) != NULL;
435 p = &x->next) {
436 if (x->addr == a->addr) {
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400437 *p = x->next;
Paul E. McKenney11c476f32011-05-02 00:56:57 -0700438 kfree_rcu(x, rcu_head);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900439 t->prl_count--;
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900440 goto out;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400441 }
442 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900443 err = -ENXIO;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400444 } else {
Eric Dumazet753ea8e2011-01-20 07:16:24 +0000445 x = rtnl_dereference(t->prl);
446 if (x) {
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000447 t->prl_count = 0;
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000448 call_rcu(&x->rcu_head, prl_list_destroy_rcu);
449 t->prl = NULL;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400450 }
451 }
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900452out:
Sascha Hlusiak4b27960172009-05-19 12:56:50 +0000453 return err;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400454}
455
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +0200456static int ipip6_tunnel_prl_ctl(struct net_device *dev,
457 struct ip_tunnel_prl __user *data, int cmd)
Christoph Hellwigfd5d6872020-05-19 15:03:15 +0200458{
459 struct ip_tunnel *t = netdev_priv(dev);
460 struct ip_tunnel_prl prl;
461 int err;
462
463 if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
464 return -EPERM;
465 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
466 return -EINVAL;
467
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +0200468 if (copy_from_user(&prl, data, sizeof(prl)))
Christoph Hellwigfd5d6872020-05-19 15:03:15 +0200469 return -EFAULT;
470
471 switch (cmd) {
472 case SIOCDELPRL:
473 err = ipip6_tunnel_del_prl(t, &prl);
474 break;
475 case SIOCADDPRL:
476 case SIOCCHGPRL:
477 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
478 break;
479 }
480 dst_cache_reset(&t->dst_cache);
481 netdev_state_change(dev);
482 return err;
483}
484
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400485static int
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000486isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400487{
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900488 struct ip_tunnel_prl_entry *p;
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400489 int ok = 1;
490
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000491 rcu_read_lock();
YOSHIFUJI Hideaki3fcfa122008-03-22 17:42:57 +0900492 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400493 if (p) {
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +0900494 if (p->flags & PRL_DEFAULT)
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400495 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
496 else
497 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
498 } else {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000499 const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
500
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400501 if (ipv6_addr_is_isatap(addr6) &&
502 (addr6->s6_addr32[3] == iph->saddr) &&
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -0400503 ipv6_chk_prefix(addr6, t->dev))
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400504 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
505 else
506 ok = 0;
507 }
Eric Dumazetef9a9d12009-10-23 17:51:26 +0000508 rcu_read_unlock();
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -0400509 return ok;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static void ipip6_tunnel_uninit(struct net_device *dev)
513{
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +0200514 struct ip_tunnel *tunnel = netdev_priv(dev);
515 struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
Pavel Emelyanovca8def12008-04-16 01:15:39 -0700516
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -0700517 if (dev == sitn->fb_tunnel_dev) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000518 RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else {
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +0200520 ipip6_tunnel_unlink(sitn, tunnel);
521 ipip6_tunnel_del_prl(tunnel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Paolo Abenie09acdd2016-02-12 15:43:55 +0100523 dst_cache_reset(&tunnel->dst_cache);
Eric Dumazetc0fd4072021-12-04 20:22:05 -0800524 dev_put_track(dev, &tunnel->dev_tracker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800527static int ipip6_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000529 const struct iphdr *iph = (const struct iphdr *)skb->data;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300530 const int type = icmp_hdr(skb)->type;
531 const int code = icmp_hdr(skb)->code;
Eric Dumazet20e19542016-06-18 21:52:06 -0700532 unsigned int data_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct ip_tunnel *t;
David Ahern3051fbe2017-10-30 10:07:17 -0700534 int sifindex;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800535 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 switch (type) {
538 default:
539 case ICMP_PARAMETERPROB:
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 case ICMP_DEST_UNREACH:
543 switch (code) {
544 case ICMP_SR_FAILED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* Impossible event. */
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 default:
548 /* All others are translated to HOST_UNREACH.
549 rfc2003 contains "deep thoughts" about NET_UNREACH,
550 I believe they are just ether pollution. --ANK
551 */
552 break;
553 }
554 break;
555 case ICMP_TIME_EXCEEDED:
556 if (code != ICMP_EXC_TTL)
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800557 return 0;
Eric Dumazet20e19542016-06-18 21:52:06 -0700558 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
David S. Millerec18d9a2012-07-12 00:25:15 -0700560 case ICMP_REDIRECT:
561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800564 err = -ENOENT;
565
David Ahern3051fbe2017-10-30 10:07:17 -0700566 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
567 t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
568 iph->daddr, iph->saddr, sifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100569 if (!t)
David S. Miller36393392012-06-14 22:21:46 -0700570 goto out;
571
572 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
573 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700574 t->parms.link, iph->protocol);
David S. Miller36393392012-06-14 22:21:46 -0700575 err = 0;
576 goto out;
577 }
David S. Millerec18d9a2012-07-12 00:25:15 -0700578 if (type == ICMP_REDIRECT) {
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700579 ipv4_redirect(skb, dev_net(skb->dev), t->parms.link,
580 iph->protocol);
David S. Millerec18d9a2012-07-12 00:25:15 -0700581 err = 0;
582 goto out;
583 }
David S. Miller36393392012-06-14 22:21:46 -0700584
Eric Dumazet2d7a3b22016-06-18 21:52:04 -0700585 err = 0;
Hangbin Liu173656a2019-02-07 18:36:11 +0800586 if (__in6_dev_get(skb->dev) &&
587 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto out;
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800589
Eric Dumazet2d7a3b22016-06-18 21:52:04 -0700590 if (t->parms.iph.daddr == 0)
Oussama Ghorbelca15a072013-11-22 16:23:20 +0100591 goto out;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
594 goto out;
595
Wei Yongjunbb800872009-02-24 23:37:19 -0800596 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 t->err_count++;
598 else
599 t->err_count = 1;
600 t->err_time = jiffies;
601out:
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -0800602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +0000605static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
606 const struct in6_addr *v6addr)
607{
608 __be32 v4embed = 0;
609 if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
610 return true;
611 return false;
612}
613
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +0300614/* Checks if an address matches an address on the tunnel interface.
615 * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
616 * Long story:
617 * This function is called after we considered the packet as spoofed
618 * in is_spoofed_6rd.
619 * We may have a router that is doing NAT for proto 41 packets
620 * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
621 * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
622 * function will return true, dropping the packet.
623 * But, we can still check if is spoofed against the IP
624 * addresses associated with the interface.
625 */
626static bool only_dnatted(const struct ip_tunnel *tunnel,
627 const struct in6_addr *v6dst)
628{
629 int prefix_len;
630
631#ifdef CONFIG_IPV6_SIT_6RD
632 prefix_len = tunnel->ip6rd.prefixlen + 32
633 - tunnel->ip6rd.relay_prefixlen;
634#else
635 prefix_len = 48;
636#endif
637 return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
638}
639
640/* Returns true if a packet is spoofed */
641static bool packet_is_spoofed(struct sk_buff *skb,
642 const struct iphdr *iph,
643 struct ip_tunnel *tunnel)
644{
645 const struct ipv6hdr *ipv6h;
646
647 if (tunnel->dev->priv_flags & IFF_ISATAP) {
648 if (!isatap_chksrc(skb, iph, tunnel))
649 return true;
650
651 return false;
652 }
653
654 if (tunnel->dev->flags & IFF_POINTOPOINT)
655 return false;
656
657 ipv6h = ipv6_hdr(skb);
658
659 if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
660 net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
661 &iph->saddr, &ipv6h->saddr,
662 &iph->daddr, &ipv6h->daddr);
663 return true;
664 }
665
666 if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
667 return false;
668
669 if (only_dnatted(tunnel, &ipv6h->daddr))
670 return false;
671
672 net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
673 &iph->saddr, &ipv6h->saddr,
674 &iph->daddr, &ipv6h->daddr);
675 return true;
676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static int ipip6_rcv(struct sk_buff *skb)
679{
Hannes Frederic Sowa1ad759d2013-01-18 09:18:17 +0000680 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct ip_tunnel *tunnel;
David Ahern3051fbe2017-10-30 10:07:17 -0700682 int sifindex;
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
David Ahern3051fbe2017-10-30 10:07:17 -0700685 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
Sascha Hlusiak4fddbf52009-05-19 12:56:49 +0000686 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
David Ahern3051fbe2017-10-30 10:07:17 -0700687 iph->saddr, iph->daddr, sifindex);
Ian Morris53b24b82015-03-29 14:00:05 +0100688 if (tunnel) {
Li RongQing8f849852014-01-04 13:57:59 +0800689 struct pcpu_sw_netstats *tstats;
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000690
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000691 if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
692 tunnel->parms.iph.protocol != 0)
693 goto out;
694
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700695 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700696 skb_reset_network_header(skb);
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800697 IPCB(skb)->flags = 0;
Jesse Grossa09a4c82016-03-19 09:32:02 -0700698 skb->dev = tunnel->dev;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100699
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +0300700 if (packet_is_spoofed(skb, iph, tunnel)) {
701 tunnel->dev->stats.rx_errors++;
702 goto out;
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000703 }
704
Jesse Grossa09a4c82016-03-19 09:32:02 -0700705 if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6),
706 !net_eq(tunnel->net, dev_net(tunnel->dev))))
707 goto out;
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000708
Lorenzo Bianconibb9bd812019-04-04 16:37:53 +0200709 /* skb can be uncloned in iptunnel_pull_header, so
710 * old iph is no longer valid
711 */
712 iph = (const struct iphdr *)skb_mac_header(skb);
Guillaume Nault730eed272021-06-25 15:33:08 +0200713 skb_reset_mac_header(skb);
714
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000715 err = IP_ECN_decapsulate(iph, skb);
716 if (unlikely(err)) {
717 if (log_ecn_error)
718 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
719 &iph->saddr, iph->tos);
720 if (err > 1) {
721 ++tunnel->dev->stats.rx_frame_errors;
722 ++tunnel->dev->stats.rx_errors;
723 goto out;
724 }
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100725 }
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700726
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000727 tstats = this_cpu_ptr(tunnel->dev->tstats);
Li RongQingc3ac17c2014-01-02 08:49:36 +0800728 u64_stats_update_begin(&tstats->syncp);
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000729 tstats->rx_packets++;
730 tstats->rx_bytes += skb->len;
Li RongQingc3ac17c2014-01-02 08:49:36 +0800731 u64_stats_update_end(&tstats->syncp);
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000732
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000733 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736 }
737
David McCullough6dcdd1b2010-11-29 19:32:34 +0000738 /* no tunnel matched, let upstream know, ipsec may handle it */
David McCullough6dcdd1b2010-11-29 19:32:34 +0000739 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740out:
David S. Miller36ca34c2008-05-08 23:40:26 -0700741 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return 0;
743}
744
Simon Horman49dbe7a2016-07-07 07:56:13 +0200745static const struct tnl_ptk_info ipip_tpi = {
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000746 /* no tunnel info required for ipip. */
747 .proto = htons(ETH_P_IP),
748};
749
Simon Horman49dbe7a2016-07-07 07:56:13 +0200750#if IS_ENABLED(CONFIG_MPLS)
751static const struct tnl_ptk_info mplsip_tpi = {
752 /* no tunnel info required for mplsip. */
753 .proto = htons(ETH_P_MPLS_UC),
754};
755#endif
756
757static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000758{
Pravin B Shelar3d7b46c2013-06-17 17:50:02 -0700759 const struct iphdr *iph;
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000760 struct ip_tunnel *tunnel;
David Ahern3051fbe2017-10-30 10:07:17 -0700761 int sifindex;
762
763 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000764
Pravin B Shelar3d7b46c2013-06-17 17:50:02 -0700765 iph = ip_hdr(skb);
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000766 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
David Ahern3051fbe2017-10-30 10:07:17 -0700767 iph->saddr, iph->daddr, sifindex);
Ian Morris53b24b82015-03-29 14:00:05 +0100768 if (tunnel) {
Simon Horman49dbe7a2016-07-07 07:56:13 +0200769 const struct tnl_ptk_info *tpi;
770
771 if (tunnel->parms.iph.protocol != ipproto &&
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000772 tunnel->parms.iph.protocol != 0)
773 goto drop;
774
775 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
776 goto drop;
Simon Horman49dbe7a2016-07-07 07:56:13 +0200777#if IS_ENABLED(CONFIG_MPLS)
778 if (ipproto == IPPROTO_MPLS)
779 tpi = &mplsip_tpi;
780 else
781#endif
782 tpi = &ipip_tpi;
783 if (iptunnel_pull_header(skb, 0, tpi->proto, false))
Li Hongjun737e8282013-08-28 11:54:50 +0200784 goto drop;
Guillaume Nault730eed272021-06-25 15:33:08 +0200785 skb_reset_mac_header(skb);
786
Simon Horman49dbe7a2016-07-07 07:56:13 +0200787 return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +0000788 }
789
790 return 1;
791
792drop:
793 kfree_skb(skb);
794 return 0;
795}
796
Simon Horman49dbe7a2016-07-07 07:56:13 +0200797static int ipip_rcv(struct sk_buff *skb)
798{
799 return sit_tunnel_rcv(skb, IPPROTO_IPIP);
800}
801
802#if IS_ENABLED(CONFIG_MPLS)
803static int mplsip_rcv(struct sk_buff *skb)
804{
805 return sit_tunnel_rcv(skb, IPPROTO_MPLS);
806}
807#endif
808
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000809/*
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +0000810 * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
811 * stores the embedded IPv4 address in v4dst and returns true.
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000812 */
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +0000813static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
814 __be32 *v4dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000816#ifdef CONFIG_IPV6_SIT_6RD
817 if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
818 tunnel->ip6rd.prefixlen)) {
Eric Dumazet3a43be32010-09-15 11:35:10 +0000819 unsigned int pbw0, pbi0;
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000820 int pbi1;
821 u32 d;
822
823 pbw0 = tunnel->ip6rd.prefixlen >> 5;
824 pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
825
Miaohe Lina843dc42019-03-11 16:29:32 +0800826 d = tunnel->ip6rd.relay_prefixlen < 32 ?
827 (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
828 tunnel->ip6rd.relay_prefixlen : 0;
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000829
830 pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
831 if (pbi1 > 0)
YOSHIFUJI Hideaki / 吉藤英明e7db38c2009-10-11 03:44:45 +0000832 d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000833 (32 - pbi1);
834
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +0000835 *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
836 return true;
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000837 }
838#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (v6dst->s6_addr16[0] == htons(0x2002)) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900840 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +0000841 memcpy(v4dst, &v6dst->s6_addr16[1], 4);
842 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +0000844#endif
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +0000845 return false;
846}
847
848static inline __be32 try_6rd(struct ip_tunnel *tunnel,
849 const struct in6_addr *v6dst)
850{
851 __be32 dst = 0;
852 check_6rd(tunnel, v6dst, &dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return dst;
854}
855
856/*
857 * This function assumes it is being called from dev_queue_xmit()
858 * and that skb is filled properly by that function.
859 */
860
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000861static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
862 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Patrick McHardy2941a482006-01-08 22:05:26 -0800864 struct ip_tunnel *tunnel = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000865 const struct iphdr *tiph = &tunnel->parms.iph;
866 const struct ipv6hdr *iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 u8 tos = tunnel->parms.iph.tos;
Herbert Xu292f4f32009-11-09 08:42:01 +0000868 __be16 df = tiph->frag_off;
Ian Morris67ba4152014-08-24 21:53:10 +0100869 struct rtable *rt; /* Route to the other host */
870 struct net_device *tdev; /* Device to other host */
871 unsigned int max_headroom; /* The extra header space needed */
Al Viroe69a4ad2006-11-14 20:56:00 -0800872 __be32 dst = tiph->daddr;
David S. Miller31e4543d2011-05-03 20:25:42 -0700873 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int mtu;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000875 const struct in6_addr *addr6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int addr_type;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -0700877 u8 ttl;
Tom Herbert14909662014-09-17 12:25:59 -0700878 u8 protocol = IPPROTO_IPV6;
879 int t_hlen = tunnel->hlen + sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Lionel Elie Mamanec2bceb32011-08-13 14:04:38 +0000881 if (tos == 1)
882 tos = ipv6_get_dsfield(iph6);
883
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100884 /* ISATAP (RFC4214) - must come before 6to4 */
885 if (dev->priv_flags & IFF_ISATAP) {
886 struct neighbour *neigh = NULL;
David S. Miller1e2927b2012-01-26 15:23:21 -0500887 bool do_tx_error = false;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100888
Eric Dumazetadf30902009-06-02 05:19:30 +0000889 if (skb_dst(skb))
David S. Miller1e2927b2012-01-26 15:23:21 -0500890 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100891
Ian Morris63159f22015-03-29 14:00:04 +0100892 if (!neigh) {
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +0300893 net_dbg_ratelimited("nexthop == NULL\n");
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100894 goto tx_error;
895 }
896
Eldad Zack3e866702012-04-01 07:49:01 +0000897 addr6 = (const struct in6_addr *)&neigh->primary_key;
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100898 addr_type = ipv6_addr_type(addr6);
899
900 if ((addr_type & IPV6_ADDR_UNICAST) &&
901 ipv6_addr_is_isatap(addr6))
902 dst = addr6->s6_addr32[3];
903 else
David S. Miller1e2927b2012-01-26 15:23:21 -0500904 do_tx_error = true;
905
906 neigh_release(neigh);
907 if (do_tx_error)
Fred L. Templinc7dc89c2007-11-29 22:11:40 +1100908 goto tx_error;
909 }
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!dst)
Hannes Frederic Sowa218774d2013-01-29 08:24:25 +0000912 dst = try_6rd(tunnel, &iph6->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (!dst) {
915 struct neighbour *neigh = NULL;
David S. Miller1e2927b2012-01-26 15:23:21 -0500916 bool do_tx_error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Eric Dumazetadf30902009-06-02 05:19:30 +0000918 if (skb_dst(skb))
David S. Miller1e2927b2012-01-26 15:23:21 -0500919 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Ian Morris63159f22015-03-29 14:00:04 +0100921 if (!neigh) {
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +0300922 net_dbg_ratelimited("nexthop == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto tx_error;
924 }
925
Eldad Zack3e866702012-04-01 07:49:01 +0000926 addr6 = (const struct in6_addr *)&neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 addr_type = ipv6_addr_type(addr6);
928
929 if (addr_type == IPV6_ADDR_ANY) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700930 addr6 = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 addr_type = ipv6_addr_type(addr6);
932 }
933
David S. Miller1e2927b2012-01-26 15:23:21 -0500934 if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
935 dst = addr6->s6_addr32[3];
936 else
937 do_tx_error = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
David S. Miller1e2927b2012-01-26 15:23:21 -0500939 neigh_release(neigh);
940 if (do_tx_error)
941 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
Craig Gallek9830ad42017-04-19 12:30:54 -0400944 flowi4_init_output(&fl4, tunnel->parms.link, tunnel->fwmark,
945 RT_TOS(tos), RT_SCOPE_UNIVERSE, IPPROTO_IPV6,
946 0, dst, tiph->saddr, 0, 0,
947 sock_net_uid(tunnel->net, NULL));
Craig Gallek9830ad42017-04-19 12:30:54 -0400948
Haishuang Yan6e3d1bb2019-07-14 21:31:22 +0800949 rt = dst_cache_get_ip4(&tunnel->dst_cache, &fl4.saddr);
950 if (!rt) {
951 rt = ip_route_output_flow(tunnel->net, &fl4, NULL);
952 if (IS_ERR(rt)) {
953 dev->stats.tx_carrier_errors++;
954 goto tx_error_icmp;
955 }
956 dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst, fl4.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Haishuang Yan6e3d1bb2019-07-14 21:31:22 +0800958
Ignat Korchagined6ae5c2022-01-07 12:38:42 +0000959 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 ip_rt_put(rt);
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000961 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto tx_error_icmp;
963 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700964 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (tdev == dev) {
967 ip_rt_put(rt);
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000968 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 goto tx_error;
970 }
971
Tom Herbert7e133182016-05-18 09:06:10 -0700972 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) {
Tom Herbert14909662014-09-17 12:25:59 -0700973 ip_rt_put(rt);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400974 goto tx_error;
Tom Herbert14909662014-09-17 12:25:59 -0700975 }
976
Herbert Xu292f4f32009-11-09 08:42:01 +0000977 if (df) {
Tom Herbert14909662014-09-17 12:25:59 -0700978 mtu = dst_mtu(&rt->dst) - t_hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
zhang kai7f0e8692021-06-02 09:50:39 +0800980 if (mtu < IPV4_MIN_MTU) {
Eric Dumazet15fc1f72010-09-27 00:38:18 +0000981 dev->stats.collisions++;
Herbert Xu292f4f32009-11-09 08:42:01 +0000982 ip_rt_put(rt);
983 goto tx_error;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Herbert Xu292f4f32009-11-09 08:42:01 +0000986 if (mtu < IPV6_MIN_MTU) {
987 mtu = IPV6_MIN_MTU;
988 df = 0;
989 }
990
Nicolas Dichtelf15ca722018-01-25 19:03:03 +0100991 if (tunnel->parms.iph.daddr)
Hangbin Liu4d42df42019-12-22 10:51:15 +0800992 skb_dst_update_pmtu_no_confirm(skb, mtu);
Herbert Xu292f4f32009-11-09 08:42:01 +0000993
Eric Dumazet58a47822013-12-16 06:31:23 -0800994 if (skb->len > mtu && !skb_is_gso(skb)) {
Jason A. Donenfeld43723392021-02-27 01:40:19 +0100995 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Herbert Xu292f4f32009-11-09 08:42:01 +0000996 ip_rt_put(rt);
997 goto tx_error;
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
1001 if (tunnel->err_count > 0) {
Wei Yongjunbb800872009-02-24 23:37:19 -08001002 if (time_before(jiffies,
1003 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 tunnel->err_count--;
1005 dst_link_failure(skb);
1006 } else
1007 tunnel->err_count = 0;
1008 }
1009
1010 /*
1011 * Okay, now see if we can stuff it in the buffer as-is.
1012 */
Tom Herbert14909662014-09-17 12:25:59 -07001013 max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Patrick McHardycfbba492007-07-09 15:33:40 -07001015 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1016 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1018 if (!new_skb) {
1019 ip_rt_put(rt);
Eric Dumazet15fc1f72010-09-27 00:38:18 +00001020 dev->stats.tx_dropped++;
Gao feng66028312013-11-25 17:21:24 +08001021 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001022 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 if (skb->sk)
1025 skb_set_owner_w(new_skb, skb->sk);
1026 dev_kfree_skb(skb);
1027 skb = new_skb;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001028 iph6 = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001030 ttl = tiph->ttl;
1031 if (ttl == 0)
1032 ttl = iph6->hop_limit;
1033 tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Tom Herbert14909662014-09-17 12:25:59 -07001035 if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0) {
Li RongQing6a9eadc2013-12-20 17:20:12 +08001036 ip_rt_put(rt);
Tom Herbert14909662014-09-17 12:25:59 -07001037 goto tx_error;
Li RongQing6a9eadc2013-12-20 17:20:12 +08001038 }
Hannes Frederic Sowa3d483052013-08-18 13:46:52 +02001039
Tom Herbert469471c2014-09-29 20:22:30 -07001040 skb_set_inner_ipproto(skb, IPPROTO_IPV6);
1041
Pravin B Shelar039f5062015-12-24 14:34:54 -08001042 iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
1043 df, !net_eq(tunnel->net, dev_net(dev)));
Patrick McHardy6ed10652009-06-23 06:03:08 +00001044 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046tx_error_icmp:
1047 dst_link_failure(skb);
1048tx_error:
Gao feng66028312013-11-25 17:21:24 +08001049 kfree_skb(skb);
Eric Dumazet61c1db72013-10-20 20:47:30 -07001050 dev->stats.tx_errors++;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001051 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Simon Horman49dbe7a2016-07-07 07:56:13 +02001054static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
1055 struct net_device *dev, u8 ipproto)
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001056{
1057 struct ip_tunnel *tunnel = netdev_priv(dev);
1058 const struct iphdr *tiph = &tunnel->parms.iph;
1059
Tom Herbert7e133182016-05-18 09:06:10 -07001060 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
Alexander Duyckaed069d2016-04-14 15:33:37 -04001061 goto tx_error;
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001062
Simon Horman49dbe7a2016-07-07 07:56:13 +02001063 skb_set_inner_ipproto(skb, ipproto);
Tom Herbert469471c2014-09-29 20:22:30 -07001064
Simon Horman49dbe7a2016-07-07 07:56:13 +02001065 ip_tunnel_xmit(skb, dev, tiph, ipproto);
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001066 return NETDEV_TX_OK;
Alexander Duyckaed069d2016-04-14 15:33:37 -04001067tx_error:
1068 kfree_skb(skb);
Eric Dumazet61c1db72013-10-20 20:47:30 -07001069 dev->stats.tx_errors++;
1070 return NETDEV_TX_OK;
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001071}
1072
1073static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
1074 struct net_device *dev)
1075{
Willem de Bruijncb9f1b72018-12-30 17:24:36 -05001076 if (!pskb_inet_may_pull(skb))
1077 goto tx_err;
1078
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001079 switch (skb->protocol) {
1080 case htons(ETH_P_IP):
Simon Horman49dbe7a2016-07-07 07:56:13 +02001081 sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001082 break;
1083 case htons(ETH_P_IPV6):
1084 ipip6_tunnel_xmit(skb, dev);
1085 break;
Simon Horman49dbe7a2016-07-07 07:56:13 +02001086#if IS_ENABLED(CONFIG_MPLS)
1087 case htons(ETH_P_MPLS_UC):
1088 sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
1089 break;
1090#endif
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001091 default:
1092 goto tx_err;
1093 }
1094
1095 return NETDEV_TX_OK;
1096
1097tx_err:
1098 dev->stats.tx_errors++;
Gao feng66028312013-11-25 17:21:24 +08001099 kfree_skb(skb);
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001100 return NETDEV_TX_OK;
1101
1102}
1103
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001104static void ipip6_tunnel_bind_dev(struct net_device *dev)
1105{
1106 struct net_device *tdev = NULL;
1107 struct ip_tunnel *tunnel;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001108 const struct iphdr *iph;
David S. Miller31e4543d2011-05-03 20:25:42 -07001109 struct flowi4 fl4;
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001110
1111 tunnel = netdev_priv(dev);
1112 iph = &tunnel->parms.iph;
1113
1114 if (iph->daddr) {
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001115 struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
1116 NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001117 iph->daddr, iph->saddr,
1118 0, 0,
1119 IPPROTO_IPV6,
1120 RT_TOS(iph->tos),
1121 tunnel->parms.link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001122
1123 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001124 tdev = rt->dst.dev;
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001125 ip_rt_put(rt);
1126 }
1127 dev->flags |= IFF_POINTOPOINT;
1128 }
1129
1130 if (!tdev && tunnel->parms.link)
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001131 tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001132
Stephen Suryaputraff6ab322019-05-06 15:00:01 -04001133 if (tdev && !netif_is_l3_master(tdev)) {
Tom Herbert14909662014-09-17 12:25:59 -07001134 int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1135
Tom Herbert14909662014-09-17 12:25:59 -07001136 dev->mtu = tdev->mtu - t_hlen;
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001137 if (dev->mtu < IPV6_MIN_MTU)
1138 dev->mtu = IPV6_MIN_MTU;
1139 }
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001140}
1141
Craig Gallek9830ad42017-04-19 12:30:54 -04001142static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p,
1143 __u32 fwmark)
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001144{
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001145 struct net *net = t->net;
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001146 struct sit_net *sitn = net_generic(net, sit_net_id);
1147
1148 ipip6_tunnel_unlink(sitn, t);
1149 synchronize_net();
1150 t->parms.iph.saddr = p->iph.saddr;
1151 t->parms.iph.daddr = p->iph.daddr;
Jakub Kicinski5a1b7e12021-10-12 09:06:34 -07001152 __dev_addr_set(t->dev, &p->iph.saddr, 4);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001153 memcpy(t->dev->broadcast, &p->iph.daddr, 4);
1154 ipip6_tunnel_link(sitn, t);
1155 t->parms.iph.ttl = p->iph.ttl;
1156 t->parms.iph.tos = p->iph.tos;
Hangbin Liuf859b4a2017-11-30 10:41:14 +08001157 t->parms.iph.frag_off = p->iph.frag_off;
Craig Gallek9830ad42017-04-19 12:30:54 -04001158 if (t->parms.link != p->link || t->fwmark != fwmark) {
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001159 t->parms.link = p->link;
Craig Gallek9830ad42017-04-19 12:30:54 -04001160 t->fwmark = fwmark;
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001161 ipip6_tunnel_bind_dev(t->dev);
1162 }
Paolo Abenie09acdd2016-02-12 15:43:55 +01001163 dst_cache_reset(&t->dst_cache);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001164 netdev_state_change(t->dev);
1165}
1166
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001167#ifdef CONFIG_IPV6_SIT_6RD
1168static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
1169 struct ip_tunnel_6rd *ip6rd)
1170{
1171 struct in6_addr prefix;
1172 __be32 relay_prefix;
1173
1174 if (ip6rd->relay_prefixlen > 32 ||
1175 ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
1176 return -EINVAL;
1177
1178 ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
1179 if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
1180 return -EINVAL;
1181 if (ip6rd->relay_prefixlen)
1182 relay_prefix = ip6rd->relay_prefix &
1183 htonl(0xffffffffUL <<
1184 (32 - ip6rd->relay_prefixlen));
1185 else
1186 relay_prefix = 0;
1187 if (relay_prefix != ip6rd->relay_prefix)
1188 return -EINVAL;
1189
1190 t->ip6rd.prefix = prefix;
1191 t->ip6rd.relay_prefix = relay_prefix;
1192 t->ip6rd.prefixlen = ip6rd->prefixlen;
1193 t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
Paolo Abenie09acdd2016-02-12 15:43:55 +01001194 dst_cache_reset(&t->dst_cache);
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001195 netdev_state_change(t->dev);
1196 return 0;
1197}
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001198
1199static int
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001200ipip6_tunnel_get6rd(struct net_device *dev, struct ip_tunnel_parm __user *data)
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001201{
1202 struct ip_tunnel *t = netdev_priv(dev);
1203 struct ip_tunnel_6rd ip6rd;
1204 struct ip_tunnel_parm p;
1205
1206 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001207 if (copy_from_user(&p, data, sizeof(p)))
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001208 return -EFAULT;
1209 t = ipip6_tunnel_locate(t->net, &p, 0);
1210 }
1211 if (!t)
1212 t = netdev_priv(dev);
1213
1214 ip6rd.prefix = t->ip6rd.prefix;
1215 ip6rd.relay_prefix = t->ip6rd.relay_prefix;
1216 ip6rd.prefixlen = t->ip6rd.prefixlen;
1217 ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001218 if (copy_to_user(data, &ip6rd, sizeof(ip6rd)))
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001219 return -EFAULT;
1220 return 0;
1221}
1222
1223static int
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001224ipip6_tunnel_6rdctl(struct net_device *dev, struct ip_tunnel_6rd __user *data,
1225 int cmd)
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001226{
1227 struct ip_tunnel *t = netdev_priv(dev);
1228 struct ip_tunnel_6rd ip6rd;
1229 int err;
1230
1231 if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
1232 return -EPERM;
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001233 if (copy_from_user(&ip6rd, data, sizeof(ip6rd)))
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001234 return -EFAULT;
1235
1236 if (cmd != SIOCDEL6RD) {
1237 err = ipip6_tunnel_update_6rd(t, &ip6rd);
1238 if (err < 0)
1239 return err;
1240 } else
1241 ipip6_tunnel_clone_6rd(dev, dev_to_sit_net(dev));
1242 return 0;
1243}
1244
1245#endif /* CONFIG_IPV6_SIT_6RD */
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001246
Wei Yongjun03ff4972016-08-13 01:54:15 +00001247static bool ipip6_valid_ip_proto(u8 ipproto)
Simon Horman49dbe7a2016-07-07 07:56:13 +02001248{
1249 return ipproto == IPPROTO_IPV6 ||
1250 ipproto == IPPROTO_IPIP ||
1251#if IS_ENABLED(CONFIG_MPLS)
1252 ipproto == IPPROTO_MPLS ||
1253#endif
1254 ipproto == 0;
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257static int
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001258__ipip6_tunnel_ioctl_validate(struct net *net, struct ip_tunnel_parm *p)
1259{
1260 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1261 return -EPERM;
1262
1263 if (!ipip6_valid_ip_proto(p->iph.protocol))
1264 return -EINVAL;
1265 if (p->iph.version != 4 ||
1266 p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)))
1267 return -EINVAL;
1268
1269 if (p->iph.ttl)
1270 p->iph.frag_off |= htons(IP_DF);
1271 return 0;
1272}
1273
1274static int
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001275ipip6_tunnel_get(struct net_device *dev, struct ip_tunnel_parm *p)
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001276{
1277 struct ip_tunnel *t = netdev_priv(dev);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001278
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001279 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
1280 t = ipip6_tunnel_locate(t->net, p, 0);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001281 if (!t)
1282 t = netdev_priv(dev);
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001283 memcpy(p, &t->parms, sizeof(*p));
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001284 return 0;
1285}
1286
1287static int
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001288ipip6_tunnel_add(struct net_device *dev, struct ip_tunnel_parm *p)
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001289{
1290 struct ip_tunnel *t = netdev_priv(dev);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001291 int err;
1292
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001293 err = __ipip6_tunnel_ioctl_validate(t->net, p);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001294 if (err)
1295 return err;
1296
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001297 t = ipip6_tunnel_locate(t->net, p, 1);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001298 if (!t)
1299 return -ENOBUFS;
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001300 return 0;
1301}
1302
1303static int
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001304ipip6_tunnel_change(struct net_device *dev, struct ip_tunnel_parm *p)
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001305{
1306 struct ip_tunnel *t = netdev_priv(dev);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001307 int err;
1308
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001309 err = __ipip6_tunnel_ioctl_validate(t->net, p);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001310 if (err)
1311 return err;
1312
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001313 t = ipip6_tunnel_locate(t->net, p, 0);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001314 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
1315 if (!t)
1316 return -ENOENT;
1317 } else {
1318 if (t) {
1319 if (t->dev != dev)
1320 return -EEXIST;
1321 } else {
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001322 if (((dev->flags & IFF_POINTOPOINT) && !p->iph.daddr) ||
1323 (!(dev->flags & IFF_POINTOPOINT) && p->iph.daddr))
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001324 return -EINVAL;
1325 t = netdev_priv(dev);
1326 }
1327
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001328 ipip6_tunnel_update(t, p, t->fwmark);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001329 }
1330
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001331 return 0;
1332}
1333
1334static int
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001335ipip6_tunnel_del(struct net_device *dev, struct ip_tunnel_parm *p)
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001336{
1337 struct ip_tunnel *t = netdev_priv(dev);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001338
1339 if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
1340 return -EPERM;
1341
1342 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001343 t = ipip6_tunnel_locate(t->net, p, 0);
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001344 if (!t)
1345 return -ENOENT;
1346 if (t == netdev_priv(dev_to_sit_net(dev)->fb_tunnel_dev))
1347 return -EPERM;
1348 dev = t->dev;
1349 }
1350 unregister_netdevice(dev);
1351 return 0;
1352}
1353
1354static int
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001355ipip6_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
1356{
1357 switch (cmd) {
1358 case SIOCGETTUNNEL:
1359 return ipip6_tunnel_get(dev, p);
1360 case SIOCADDTUNNEL:
1361 return ipip6_tunnel_add(dev, p);
1362 case SIOCCHGTUNNEL:
1363 return ipip6_tunnel_change(dev, p);
1364 case SIOCDELTUNNEL:
1365 return ipip6_tunnel_del(dev, p);
1366 default:
1367 return -EINVAL;
1368 }
1369}
1370
1371static int
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001372ipip6_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
1373 void __user *data, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 switch (cmd) {
1376 case SIOCGETTUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 case SIOCADDTUNNEL:
1378 case SIOCCHGTUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 case SIOCDELTUNNEL:
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001380 return ip_tunnel_siocdevprivate(dev, ifr, data, cmd);
YOSHIFUJI Hideaki300aaee2008-03-24 18:28:39 +09001381 case SIOCGETPRL:
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001382 return ipip6_tunnel_get_prl(dev, data);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001383 case SIOCADDPRL:
1384 case SIOCDELPRL:
1385 case SIOCCHGPRL:
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001386 return ipip6_tunnel_prl_ctl(dev, data, cmd);
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +00001387#ifdef CONFIG_IPV6_SIT_6RD
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001388 case SIOCGET6RD:
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001389 return ipip6_tunnel_get6rd(dev, data);
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +00001390 case SIOCADD6RD:
1391 case SIOCCHG6RD:
1392 case SIOCDEL6RD:
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001393 return ipip6_tunnel_6rdctl(dev, data, cmd);
YOSHIFUJI Hideaki / 吉藤英明fa857af2009-09-22 23:43:14 +00001394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 default:
Christoph Hellwigfd5d6872020-05-19 15:03:15 +02001396 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001400static const struct net_device_ops ipip6_netdev_ops = {
Steffen Klassertebe084a2014-11-03 09:19:29 +01001401 .ndo_init = ipip6_tunnel_init,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001402 .ndo_uninit = ipip6_tunnel_uninit,
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001403 .ndo_start_xmit = sit_tunnel_xmit,
Arnd Bergmann3e7a1c72021-07-27 15:45:06 +02001404 .ndo_siocdevprivate = ipip6_tunnel_siocdevprivate,
Heiner Kallweit98d7fc42020-11-07 21:54:33 +01001405 .ndo_get_stats64 = dev_get_tstats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +02001406 .ndo_get_iflink = ip_tunnel_get_iflink,
Christoph Hellwigf60fe2d2020-05-19 15:03:16 +02001407 .ndo_tunnel_ctl = ipip6_tunnel_ctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001408};
1409
Eric Dumazet15fc1f72010-09-27 00:38:18 +00001410static void ipip6_dev_free(struct net_device *dev)
1411{
Nicolas Dichtelcf71d2bc2014-02-20 10:19:31 +01001412 struct ip_tunnel *tunnel = netdev_priv(dev);
1413
Paolo Abenie09acdd2016-02-12 15:43:55 +01001414 dst_cache_destroy(&tunnel->dst_cache);
Eric Dumazet15fc1f72010-09-27 00:38:18 +00001415 free_percpu(dev->tstats);
Eric Dumazet15fc1f72010-09-27 00:38:18 +00001416}
1417
Eric Dumazet61c1db72013-10-20 20:47:30 -07001418#define SIT_FEATURES (NETIF_F_SG | \
1419 NETIF_F_FRAGLIST | \
1420 NETIF_F_HIGHDMA | \
1421 NETIF_F_GSO_SOFTWARE | \
1422 NETIF_F_HW_CSUM)
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424static void ipip6_tunnel_setup(struct net_device *dev)
1425{
Tom Herbert14909662014-09-17 12:25:59 -07001426 struct ip_tunnel *tunnel = netdev_priv(dev);
1427 int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1428
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001429 dev->netdev_ops = &ipip6_netdev_ops;
Jason A. Donenfeld75ea1f42020-06-29 19:06:24 -06001430 dev->header_ops = &ip_tunnel_header_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001431 dev->needs_free_netdev = true;
1432 dev->priv_destructor = ipip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 dev->type = ARPHRD_SIT;
Tom Herbert14909662014-09-17 12:25:59 -07001435 dev->mtu = ETH_DATA_LEN - t_hlen;
Jarod Wilsonb96f9af2016-10-20 13:55:24 -04001436 dev->min_mtu = IPV6_MIN_MTU;
Nicolas Dichtelf7ff1fd2018-05-31 10:59:33 +02001437 dev->max_mtu = IP6_MAX_MTU - t_hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 dev->flags = IFF_NOARP;
Eric Dumazet02875872014-10-05 18:38:35 -07001439 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 dev->addr_len = 4;
Eric Dumazet8df40d12010-09-28 02:53:41 +00001441 dev->features |= NETIF_F_LLTX;
Eric Dumazet61c1db72013-10-20 20:47:30 -07001442 dev->features |= SIT_FEATURES;
1443 dev->hw_features |= SIT_FEATURES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Eric Dumazet15fc1f72010-09-27 00:38:18 +00001446static int ipip6_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001448 struct ip_tunnel *tunnel = netdev_priv(dev);
Paolo Abenie09acdd2016-02-12 15:43:55 +01001449 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 tunnel->dev = dev;
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001452 tunnel->net = dev_net(dev);
Steffen Klassertebe084a2014-11-03 09:19:29 +01001453 strcpy(tunnel->parms.name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Michal Schmidt8a4a50f2007-12-13 09:47:00 -08001455 ipip6_tunnel_bind_dev(dev);
WANG Cong1c213bd2014-02-13 11:46:28 -08001456 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Eric Dumazet15fc1f72010-09-27 00:38:18 +00001457 if (!dev->tstats)
1458 return -ENOMEM;
1459
Paolo Abenie09acdd2016-02-12 15:43:55 +01001460 err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1461 if (err) {
Nicolas Dichtelcf71d2bc2014-02-20 10:19:31 +01001462 free_percpu(dev->tstats);
WANG Congd7426c62017-02-08 10:02:13 -08001463 dev->tstats = NULL;
Paolo Abenie09acdd2016-02-12 15:43:55 +01001464 return err;
Nicolas Dichtelcf71d2bc2014-02-20 10:19:31 +01001465 }
Eric Dumazetc0fd4072021-12-04 20:22:05 -08001466 dev_hold_track(dev, &tunnel->dev_tracker, GFP_KERNEL);
Eric Dumazet15fc1f72010-09-27 00:38:18 +00001467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Eric Dumazet4ece9002015-11-02 17:08:19 -08001470static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Patrick McHardy2941a482006-01-08 22:05:26 -08001472 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct iphdr *iph = &tunnel->parms.iph;
Pavel Emelyanov29182172008-04-16 01:16:38 -07001474 struct net *net = dev_net(dev);
1475 struct sit_net *sitn = net_generic(net, sit_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 iph->version = 4;
1478 iph->protocol = IPPROTO_IPV6;
1479 iph->ihl = 5;
1480 iph->ttl = 64;
1481
Eric Dumazetcf778b02012-01-12 04:41:32 +00001482 rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001485static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
1486 struct netlink_ext_ack *extack)
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001487{
1488 u8 proto;
1489
Nicolas Dichtelc2ff6822013-06-19 12:03:13 +02001490 if (!data || !data[IFLA_IPTUN_PROTO])
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001491 return 0;
1492
1493 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
Simon Horman49dbe7a2016-07-07 07:56:13 +02001494 if (!ipip6_valid_ip_proto(proto))
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001495 return -EINVAL;
1496
1497 return 0;
1498}
1499
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001500static void ipip6_netlink_parms(struct nlattr *data[],
Craig Gallek9830ad42017-04-19 12:30:54 -04001501 struct ip_tunnel_parm *parms,
1502 __u32 *fwmark)
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001503{
1504 memset(parms, 0, sizeof(*parms));
1505
1506 parms->iph.version = 4;
1507 parms->iph.protocol = IPPROTO_IPV6;
1508 parms->iph.ihl = 5;
1509 parms->iph.ttl = 64;
1510
1511 if (!data)
1512 return;
1513
1514 if (data[IFLA_IPTUN_LINK])
1515 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1516
1517 if (data[IFLA_IPTUN_LOCAL])
Nicolas Dichteld440b722012-11-15 04:06:41 +00001518 parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001519
1520 if (data[IFLA_IPTUN_REMOTE])
Nicolas Dichteld440b722012-11-15 04:06:41 +00001521 parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001522
1523 if (data[IFLA_IPTUN_TTL]) {
1524 parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
1525 if (parms->iph.ttl)
1526 parms->iph.frag_off = htons(IP_DF);
1527 }
1528
1529 if (data[IFLA_IPTUN_TOS])
1530 parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
1531
1532 if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
1533 parms->iph.frag_off = htons(IP_DF);
1534
1535 if (data[IFLA_IPTUN_FLAGS])
Nicolas Dichteld440b722012-11-15 04:06:41 +00001536 parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001537
1538 if (data[IFLA_IPTUN_PROTO])
1539 parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1540
Craig Gallek9830ad42017-04-19 12:30:54 -04001541 if (data[IFLA_IPTUN_FWMARK])
1542 *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001543}
1544
Tom Herbert14909662014-09-17 12:25:59 -07001545/* This function returns true when ENCAP attributes are present in the nl msg */
1546static bool ipip6_netlink_encap_parms(struct nlattr *data[],
1547 struct ip_tunnel_encap *ipencap)
1548{
1549 bool ret = false;
1550
1551 memset(ipencap, 0, sizeof(*ipencap));
1552
1553 if (!data)
1554 return ret;
1555
1556 if (data[IFLA_IPTUN_ENCAP_TYPE]) {
1557 ret = true;
1558 ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
1559 }
1560
1561 if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
1562 ret = true;
1563 ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
1564 }
1565
1566 if (data[IFLA_IPTUN_ENCAP_SPORT]) {
1567 ret = true;
Eric Dumazeta409cae2015-02-04 15:12:04 -08001568 ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
Tom Herbert14909662014-09-17 12:25:59 -07001569 }
1570
1571 if (data[IFLA_IPTUN_ENCAP_DPORT]) {
1572 ret = true;
Eric Dumazeta409cae2015-02-04 15:12:04 -08001573 ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
Tom Herbert14909662014-09-17 12:25:59 -07001574 }
1575
1576 return ret;
1577}
1578
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001579#ifdef CONFIG_IPV6_SIT_6RD
1580/* This function returns true when 6RD attributes are present in the nl msg */
1581static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
1582 struct ip_tunnel_6rd *ip6rd)
1583{
1584 bool ret = false;
1585 memset(ip6rd, 0, sizeof(*ip6rd));
1586
1587 if (!data)
1588 return ret;
1589
1590 if (data[IFLA_IPTUN_6RD_PREFIX]) {
1591 ret = true;
Jiri Benc67b61f62015-03-29 16:59:26 +02001592 ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001593 }
1594
1595 if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
1596 ret = true;
1597 ip6rd->relay_prefix =
1598 nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
1599 }
1600
1601 if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
1602 ret = true;
1603 ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
1604 }
1605
1606 if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
1607 ret = true;
1608 ip6rd->relay_prefixlen =
1609 nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
1610 }
1611
1612 return ret;
1613}
1614#endif
1615
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001616static int ipip6_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001617 struct nlattr *tb[], struct nlattr *data[],
1618 struct netlink_ext_ack *extack)
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001619{
1620 struct net *net = dev_net(dev);
1621 struct ip_tunnel *nt;
Tom Herbert14909662014-09-17 12:25:59 -07001622 struct ip_tunnel_encap ipencap;
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001623#ifdef CONFIG_IPV6_SIT_6RD
1624 struct ip_tunnel_6rd ip6rd;
1625#endif
1626 int err;
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001627
1628 nt = netdev_priv(dev);
Tom Herbert14909662014-09-17 12:25:59 -07001629
1630 if (ipip6_netlink_encap_parms(data, &ipencap)) {
1631 err = ip_tunnel_encap_setup(nt, &ipencap);
1632 if (err < 0)
1633 return err;
1634 }
1635
Craig Gallek9830ad42017-04-19 12:30:54 -04001636 ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001637
1638 if (ipip6_tunnel_locate(net, &nt->parms, 0))
1639 return -EEXIST;
1640
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001641 err = ipip6_tunnel_create(dev);
1642 if (err < 0)
1643 return err;
1644
Xin Long2b3957c2018-02-27 19:19:41 +08001645 if (tb[IFLA_MTU]) {
1646 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
1647
Nicolas Dichtelf7ff1fd2018-05-31 10:59:33 +02001648 if (mtu >= IPV6_MIN_MTU &&
1649 mtu <= IP6_MAX_MTU - dev->hard_header_len)
Xin Long2b3957c2018-02-27 19:19:41 +08001650 dev->mtu = mtu;
1651 }
1652
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001653#ifdef CONFIG_IPV6_SIT_6RD
Jakub Kicinski47e4bb12021-01-13 17:29:47 -08001654 if (ipip6_netlink_6rd_parms(data, &ip6rd)) {
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001655 err = ipip6_tunnel_update_6rd(nt, &ip6rd);
Jakub Kicinski47e4bb12021-01-13 17:29:47 -08001656 if (err < 0)
1657 unregister_netdevice_queue(dev, NULL);
1658 }
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001659#endif
1660
1661 return err;
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001662}
1663
1664static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001665 struct nlattr *data[],
1666 struct netlink_ext_ack *extack)
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001667{
Nicolas Dichtel86bd68b2013-07-03 17:00:34 +02001668 struct ip_tunnel *t = netdev_priv(dev);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001669 struct ip_tunnel_parm p;
Tom Herbert14909662014-09-17 12:25:59 -07001670 struct ip_tunnel_encap ipencap;
Nicolas Dichtel86bd68b2013-07-03 17:00:34 +02001671 struct net *net = t->net;
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001672 struct sit_net *sitn = net_generic(net, sit_net_id);
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001673#ifdef CONFIG_IPV6_SIT_6RD
1674 struct ip_tunnel_6rd ip6rd;
1675#endif
Craig Gallek9830ad42017-04-19 12:30:54 -04001676 __u32 fwmark = t->fwmark;
Tom Herbert14909662014-09-17 12:25:59 -07001677 int err;
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001678
1679 if (dev == sitn->fb_tunnel_dev)
1680 return -EINVAL;
1681
Tom Herbert14909662014-09-17 12:25:59 -07001682 if (ipip6_netlink_encap_parms(data, &ipencap)) {
1683 err = ip_tunnel_encap_setup(t, &ipencap);
1684 if (err < 0)
1685 return err;
1686 }
1687
Craig Gallek9830ad42017-04-19 12:30:54 -04001688 ipip6_netlink_parms(data, &p, &fwmark);
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001689
1690 if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
1691 (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
1692 return -EINVAL;
1693
1694 t = ipip6_tunnel_locate(net, &p, 0);
1695
1696 if (t) {
1697 if (t->dev != dev)
1698 return -EEXIST;
1699 } else
1700 t = netdev_priv(dev);
1701
Craig Gallek9830ad42017-04-19 12:30:54 -04001702 ipip6_tunnel_update(t, &p, fwmark);
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001703
1704#ifdef CONFIG_IPV6_SIT_6RD
1705 if (ipip6_netlink_6rd_parms(data, &ip6rd))
1706 return ipip6_tunnel_update_6rd(t, &ip6rd);
1707#endif
1708
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001709 return 0;
1710}
1711
Nicolas Dichtele4c94a92012-11-14 05:14:06 +00001712static size_t ipip6_get_size(const struct net_device *dev)
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001713{
1714 return
1715 /* IFLA_IPTUN_LINK */
1716 nla_total_size(4) +
1717 /* IFLA_IPTUN_LOCAL */
1718 nla_total_size(4) +
1719 /* IFLA_IPTUN_REMOTE */
1720 nla_total_size(4) +
1721 /* IFLA_IPTUN_TTL */
1722 nla_total_size(1) +
1723 /* IFLA_IPTUN_TOS */
1724 nla_total_size(1) +
Nicolas Dichtela12c9a82012-11-14 05:14:05 +00001725 /* IFLA_IPTUN_PMTUDISC */
1726 nla_total_size(1) +
1727 /* IFLA_IPTUN_FLAGS */
1728 nla_total_size(2) +
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001729 /* IFLA_IPTUN_PROTO */
1730 nla_total_size(1) +
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001731#ifdef CONFIG_IPV6_SIT_6RD
1732 /* IFLA_IPTUN_6RD_PREFIX */
1733 nla_total_size(sizeof(struct in6_addr)) +
1734 /* IFLA_IPTUN_6RD_RELAY_PREFIX */
1735 nla_total_size(4) +
1736 /* IFLA_IPTUN_6RD_PREFIXLEN */
1737 nla_total_size(2) +
1738 /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
1739 nla_total_size(2) +
1740#endif
Tom Herbert14909662014-09-17 12:25:59 -07001741 /* IFLA_IPTUN_ENCAP_TYPE */
1742 nla_total_size(2) +
1743 /* IFLA_IPTUN_ENCAP_FLAGS */
1744 nla_total_size(2) +
1745 /* IFLA_IPTUN_ENCAP_SPORT */
1746 nla_total_size(2) +
1747 /* IFLA_IPTUN_ENCAP_DPORT */
1748 nla_total_size(2) +
Craig Gallek9830ad42017-04-19 12:30:54 -04001749 /* IFLA_IPTUN_FWMARK */
1750 nla_total_size(4) +
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001751 0;
1752}
1753
Nicolas Dichtele4c94a92012-11-14 05:14:06 +00001754static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001755{
1756 struct ip_tunnel *tunnel = netdev_priv(dev);
1757 struct ip_tunnel_parm *parm = &tunnel->parms;
1758
1759 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001760 nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
1761 nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001762 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
Nicolas Dichtela12c9a82012-11-14 05:14:05 +00001763 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
1764 nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
1765 !!(parm->iph.frag_off & htons(IP_DF))) ||
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001766 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
Craig Gallek9830ad42017-04-19 12:30:54 -04001767 nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags) ||
1768 nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001769 goto nla_put_failure;
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001770
1771#ifdef CONFIG_IPV6_SIT_6RD
Jiri Benc930345e2015-03-29 16:59:25 +02001772 if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
1773 &tunnel->ip6rd.prefix) ||
1774 nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
1775 tunnel->ip6rd.relay_prefix) ||
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001776 nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
1777 tunnel->ip6rd.prefixlen) ||
1778 nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
1779 tunnel->ip6rd.relay_prefixlen))
1780 goto nla_put_failure;
1781#endif
1782
Tom Herbert14909662014-09-17 12:25:59 -07001783 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
1784 tunnel->encap.type) ||
Eric Dumazeta409cae2015-02-04 15:12:04 -08001785 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
Tom Herbert14909662014-09-17 12:25:59 -07001786 tunnel->encap.sport) ||
Eric Dumazeta409cae2015-02-04 15:12:04 -08001787 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
Tom Herbert14909662014-09-17 12:25:59 -07001788 tunnel->encap.dport) ||
1789 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
Tom Herberte1b2cb62014-11-05 16:49:38 -08001790 tunnel->encap.flags))
Tom Herbert14909662014-09-17 12:25:59 -07001791 goto nla_put_failure;
1792
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001793 return 0;
1794
1795nla_put_failure:
1796 return -EMSGSIZE;
1797}
1798
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001799static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
1800 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1801 [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
1802 [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
1803 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1804 [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
1805 [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
1806 [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001807 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
Nicolas Dichtele2f1f072012-11-19 22:41:45 +00001808#ifdef CONFIG_IPV6_SIT_6RD
1809 [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
1810 [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
1811 [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
1812 [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
1813#endif
Tom Herbert14909662014-09-17 12:25:59 -07001814 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
1815 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
1816 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
1817 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
Craig Gallek9830ad42017-04-19 12:30:54 -04001818 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001819};
1820
Willem de Bruijn94342662013-11-13 21:27:38 -05001821static void ipip6_dellink(struct net_device *dev, struct list_head *head)
1822{
1823 struct net *net = dev_net(dev);
1824 struct sit_net *sitn = net_generic(net, sit_net_id);
1825
1826 if (dev != sitn->fb_tunnel_dev)
1827 unregister_netdevice_queue(dev, head);
1828}
1829
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001830static struct rtnl_link_ops sit_link_ops __read_mostly = {
1831 .kind = "sit",
1832 .maxtype = IFLA_IPTUN_MAX,
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001833 .policy = ipip6_policy,
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001834 .priv_size = sizeof(struct ip_tunnel),
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001835 .setup = ipip6_tunnel_setup,
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001836 .validate = ipip6_validate,
Nicolas Dichtelf37234162012-11-14 05:14:07 +00001837 .newlink = ipip6_newlink,
1838 .changelink = ipip6_changelink,
Nicolas Dichtele4c94a92012-11-14 05:14:06 +00001839 .get_size = ipip6_get_size,
1840 .fill_info = ipip6_fill_info,
Willem de Bruijn94342662013-11-13 21:27:38 -05001841 .dellink = ipip6_dellink,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001842 .get_link_net = ip_tunnel_get_link_net,
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001843};
1844
Eric Dumazet6dcd8142010-08-30 07:04:14 +00001845static struct xfrm_tunnel sit_handler __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 .handler = ipip6_rcv,
1847 .err_handler = ipip6_err,
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001848 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849};
1850
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001851static struct xfrm_tunnel ipip_handler __read_mostly = {
1852 .handler = ipip_rcv,
1853 .err_handler = ipip6_err,
1854 .priority = 2,
1855};
1856
Simon Horman49dbe7a2016-07-07 07:56:13 +02001857#if IS_ENABLED(CONFIG_MPLS)
1858static struct xfrm_tunnel mplsip_handler __read_mostly = {
1859 .handler = mplsip_rcv,
1860 .err_handler = ipip6_err,
1861 .priority = 2,
1862};
1863#endif
1864
Willem de Bruijn94342662013-11-13 21:27:38 -05001865static void __net_exit sit_destroy_tunnels(struct net *net,
1866 struct list_head *head)
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001867{
Willem de Bruijn94342662013-11-13 21:27:38 -05001868 struct sit_net *sitn = net_generic(net, sit_net_id);
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001869 struct net_device *dev, *aux;
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001870 int prio;
1871
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001872 for_each_netdev_safe(net, dev, aux)
1873 if (dev->rtnl_link_ops == &sit_link_ops)
1874 unregister_netdevice_queue(dev, head);
1875
Hristo Venev610f8c02021-04-12 20:41:16 +03001876 for (prio = 0; prio < 4; prio++) {
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001877 int h;
Hristo Venev610f8c02021-04-12 20:41:16 +03001878 for (h = 0; h < (prio ? IP6_SIT_HASH_SIZE : 1); h++) {
Eric Dumazet753ea8e2011-01-20 07:16:24 +00001879 struct ip_tunnel *t;
Eric Dumazet62808f92009-10-28 04:37:43 +00001880
Eric Dumazet753ea8e2011-01-20 07:16:24 +00001881 t = rtnl_dereference(sitn->tunnels[prio][h]);
Ian Morris53b24b82015-03-29 14:00:05 +01001882 while (t) {
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001883 /* If dev is in the same netns, it has already
1884 * been added to the list by the previous loop.
1885 */
Nicolas Dichtelfc8f9992013-08-13 17:51:10 +02001886 if (!net_eq(dev_net(t->dev), net))
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001887 unregister_netdevice_queue(t->dev,
1888 head);
Eric Dumazet753ea8e2011-01-20 07:16:24 +00001889 t = rtnl_dereference(t->next);
Eric Dumazet62808f92009-10-28 04:37:43 +00001890 }
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001891 }
1892 }
1893}
1894
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001895static int __net_init sit_init_net(struct net *net)
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001896{
Eric W. Biederman67101172009-11-29 15:46:16 +00001897 struct sit_net *sitn = net_generic(net, sit_net_id);
Ted Feng72b36012011-12-08 00:46:21 +00001898 struct ip_tunnel *t;
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001899 int err;
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001900
Pavel Emelyanov29182172008-04-16 01:16:38 -07001901 sitn->tunnels[0] = sitn->tunnels_wc;
1902 sitn->tunnels[1] = sitn->tunnels_l;
1903 sitn->tunnels[2] = sitn->tunnels_r;
1904 sitn->tunnels[3] = sitn->tunnels_r_l;
1905
Eric Dumazet79134e62018-03-08 12:51:41 -08001906 if (!net_has_fallback_tunnels(net))
1907 return 0;
1908
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001909 sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001910 NET_NAME_UNKNOWN,
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001911 ipip6_tunnel_setup);
1912 if (!sitn->fb_tunnel_dev) {
1913 err = -ENOMEM;
1914 goto err_alloc_dev;
1915 }
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001916 dev_net_set(sitn->fb_tunnel_dev, net);
Nicolas Dichtel205983c2013-10-01 18:04:59 +02001917 sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +02001918 /* FB netdevice is special: we have one, and only one per netns.
1919 * Allowing to move it to another netns is clearly unsafe.
1920 */
1921 sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001922
Ian Morrise5d08d72014-11-23 21:28:43 +00001923 err = register_netdev(sitn->fb_tunnel_dev);
1924 if (err)
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001925 goto err_reg_dev;
1926
Eric Dumazet4ece9002015-11-02 17:08:19 -08001927 ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
1928 ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
1929
Ted Feng72b36012011-12-08 00:46:21 +00001930 t = netdev_priv(sitn->fb_tunnel_dev);
1931
1932 strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001933 return 0;
1934
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001935err_reg_dev:
Mao Wenan07f12b22019-03-01 23:06:40 +08001936 free_netdev(sitn->fb_tunnel_dev);
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001937err_alloc_dev:
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001938 return err;
1939}
1940
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001941static void __net_exit sit_exit_batch_net(struct list_head *net_list)
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001942{
Eric Dumazet62808f92009-10-28 04:37:43 +00001943 LIST_HEAD(list);
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001944 struct net *net;
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001945
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001946 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001947 list_for_each_entry(net, net_list, exit_list)
1948 sit_destroy_tunnels(net, &list);
1949
Eric Dumazet62808f92009-10-28 04:37:43 +00001950 unregister_netdevice_many(&list);
Pavel Emelyanovcd3dbc12008-04-16 01:16:18 -07001951 rtnl_unlock();
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001952}
1953
1954static struct pernet_operations sit_net_ops = {
1955 .init = sit_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001956 .exit_batch = sit_exit_batch_net,
Eric W. Biederman67101172009-11-29 15:46:16 +00001957 .id = &sit_net_id,
1958 .size = sizeof(struct sit_net),
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001959};
1960
Adrian Bunk89c89452006-11-20 16:56:48 -08001961static void __exit sit_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001963 rtnl_link_unregister(&sit_link_ops);
Kazunori MIYAZAWAc73cb5a2007-02-13 12:55:25 -08001964 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001965 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
Simon Horman49dbe7a2016-07-07 07:56:13 +02001966#if IS_ENABLED(CONFIG_MPLS)
1967 xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
1968#endif
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001969
Eric W. Biederman67101172009-11-29 15:46:16 +00001970 unregister_pernet_device(&sit_net_ops);
Eric Dumazetef9a9d12009-10-23 17:51:26 +00001971 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Adrian Bunk89c89452006-11-20 16:56:48 -08001974static int __init sit_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
1976 int err;
1977
Simon Horman49dbe7a2016-07-07 07:56:13 +02001978 pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Eric W. Biederman67101172009-11-29 15:46:16 +00001980 err = register_pernet_device(&sit_net_ops);
Pavel Emelyanov8190d902008-04-16 01:15:17 -07001981 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001982 return err;
1983 err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
1984 if (err < 0) {
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001985 pr_info("%s: can't register ip6ip4\n", __func__);
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00001986 goto xfrm_tunnel_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001987 }
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00001988 err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
1989 if (err < 0) {
1990 pr_info("%s: can't register ip4ip4\n", __func__);
1991 goto xfrm_tunnel4_failed;
1992 }
Simon Horman49dbe7a2016-07-07 07:56:13 +02001993#if IS_ENABLED(CONFIG_MPLS)
1994 err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
1995 if (err < 0) {
1996 pr_info("%s: can't register mplsip\n", __func__);
1997 goto xfrm_tunnel_mpls_failed;
1998 }
1999#endif
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00002000 err = rtnl_link_register(&sit_link_ops);
2001 if (err < 0)
2002 goto rtnl_link_failed;
2003
2004out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return err;
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00002006
2007rtnl_link_failed:
Simon Horman49dbe7a2016-07-07 07:56:13 +02002008#if IS_ENABLED(CONFIG_MPLS)
2009 xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
2010xfrm_tunnel_mpls_failed:
2011#endif
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +00002012 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
2013xfrm_tunnel4_failed:
Nicolas Dichtelba3e3f52012-11-09 06:10:00 +00002014 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
2015xfrm_tunnel_failed:
2016 unregister_pernet_device(&sit_net_ops);
2017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
Joerg Roedel989e5b92006-10-10 14:47:44 -07002019
2020module_init(sit_init);
2021module_exit(sit_cleanup);
Jan Dittmer39c85082006-10-13 15:05:53 -07002022MODULE_LICENSE("GPL");
Tom Gundersenf98f89a2014-05-15 23:21:30 +02002023MODULE_ALIAS_RTNL_LINK("sit");
Vasiliy Kulikov8909c9a2011-03-02 00:33:13 +03002024MODULE_ALIAS_NETDEV("sit0");