xref: /linux/net/ipv6/sit.c (revision 3c6132ccc58e8adf044166728e783f55dc323ef9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
9  *
10  *	Changes:
11  * Roger Venning <r.venning@telstra.com>:	6to4 support
12  * Nate Thompson <nate@thebog.net>:		6to4 support
13  * Fred Templin <fred.l.templin@boeing.com>:	isatap support
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include <linux/module.h>
19 #include <linux/capability.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/string.h>
25 #include <linux/net.h>
26 #include <linux/in6.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/icmp.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <linux/init.h>
33 #include <linux/netfilter_ipv4.h>
34 #include <linux/if_ether.h>
35 
36 #include <net/sock.h>
37 #include <net/snmp.h>
38 
39 #include <net/ipv6.h>
40 #include <net/protocol.h>
41 #include <net/transp_v6.h>
42 #include <net/ip6_fib.h>
43 #include <net/ip6_route.h>
44 #include <net/ndisc.h>
45 #include <net/addrconf.h>
46 #include <net/ip.h>
47 #include <net/udp.h>
48 #include <net/icmp.h>
49 #include <net/ip_tunnels.h>
50 #include <net/inet_ecn.h>
51 #include <net/xfrm.h>
52 #include <net/dsfield.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
55 #include <net/netdev_lock.h>
56 #include <net/inet_dscp.h>
57 
58 /*
59    This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
60 
61    For comments look at net/ipv4/ip_gre.c --ANK
62  */
63 
64 #define IP6_SIT_HASH_SIZE  16
65 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
66 
67 static bool log_ecn_error = true;
68 module_param(log_ecn_error, bool, 0644);
69 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
70 
71 static int ipip6_tunnel_init(struct net_device *dev);
72 static void ipip6_tunnel_setup(struct net_device *dev);
73 static void ipip6_dev_free(struct net_device *dev);
74 static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
75 		      __be32 *v4dst);
76 static struct rtnl_link_ops sit_link_ops __read_mostly;
77 
78 static unsigned int sit_net_id __read_mostly;
79 struct sit_net {
80 	struct ip_tunnel __rcu *tunnels_r_l[IP6_SIT_HASH_SIZE];
81 	struct ip_tunnel __rcu *tunnels_r[IP6_SIT_HASH_SIZE];
82 	struct ip_tunnel __rcu *tunnels_l[IP6_SIT_HASH_SIZE];
83 	struct ip_tunnel __rcu *tunnels_wc[1];
84 	struct ip_tunnel __rcu **tunnels[4];
85 
86 	struct net_device *fb_tunnel_dev;
87 };
88 
89 static inline struct sit_net *dev_to_sit_net(struct net_device *dev)
90 {
91 	struct ip_tunnel *t = netdev_priv(dev);
92 
93 	return net_generic(t->net, sit_net_id);
94 }
95 
96 /*
97  * Must be invoked with rcu_read_lock
98  */
99 static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
100 					     struct net_device *dev,
101 					     __be32 remote, __be32 local,
102 					     int sifindex)
103 {
104 	unsigned int h0 = HASH(remote);
105 	unsigned int h1 = HASH(local);
106 	struct ip_tunnel *t;
107 	struct sit_net *sitn = net_generic(net, sit_net_id);
108 	int ifindex = dev ? dev->ifindex : 0;
109 
110 	for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
111 		if (local == t->parms.iph.saddr &&
112 		    remote == t->parms.iph.daddr &&
113 		    (!dev || !t->parms.link || ifindex == t->parms.link ||
114 		     sifindex == t->parms.link) &&
115 		    (t->dev->flags & IFF_UP))
116 			return t;
117 	}
118 	for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
119 		if (remote == t->parms.iph.daddr &&
120 		    (!dev || !t->parms.link || ifindex == t->parms.link ||
121 		     sifindex == t->parms.link) &&
122 		    (t->dev->flags & IFF_UP))
123 			return t;
124 	}
125 	for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
126 		if (local == t->parms.iph.saddr &&
127 		    (!dev || !t->parms.link || ifindex == t->parms.link ||
128 		     sifindex == t->parms.link) &&
129 		    (t->dev->flags & IFF_UP))
130 			return t;
131 	}
132 	t = rcu_dereference(sitn->tunnels_wc[0]);
133 	if (t && (t->dev->flags & IFF_UP))
134 		return t;
135 	return NULL;
136 }
137 
138 static struct ip_tunnel __rcu **
139 __ipip6_bucket(struct sit_net *sitn, struct ip_tunnel_parm_kern *parms)
140 {
141 	__be32 remote = parms->iph.daddr;
142 	__be32 local = parms->iph.saddr;
143 	unsigned int h = 0;
144 	int prio = 0;
145 
146 	if (remote) {
147 		prio |= 2;
148 		h ^= HASH(remote);
149 	}
150 	if (local) {
151 		prio |= 1;
152 		h ^= HASH(local);
153 	}
154 	return &sitn->tunnels[prio][h];
155 }
156 
157 static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
158 		struct ip_tunnel *t)
159 {
160 	return __ipip6_bucket(sitn, &t->parms);
161 }
162 
163 static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
164 {
165 	struct ip_tunnel __rcu **tp;
166 	struct ip_tunnel *iter;
167 
168 	for (tp = ipip6_bucket(sitn, t);
169 	     (iter = rtnl_dereference(*tp)) != NULL;
170 	     tp = &iter->next) {
171 		if (t == iter) {
172 			rcu_assign_pointer(*tp, t->next);
173 			break;
174 		}
175 	}
176 }
177 
178 static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
179 {
180 	struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
181 
182 	rcu_assign_pointer(t->next, rtnl_dereference(*tp));
183 	rcu_assign_pointer(*tp, t);
184 }
185 
186 static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
187 {
188 #ifdef CONFIG_IPV6_SIT_6RD
189 	struct ip_tunnel *t = netdev_priv(dev);
190 
191 	if (dev == sitn->fb_tunnel_dev || !sitn->fb_tunnel_dev) {
192 		ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
193 		t->ip6rd.relay_prefix = 0;
194 		t->ip6rd.prefixlen = 16;
195 		t->ip6rd.relay_prefixlen = 0;
196 	} else {
197 		struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
198 		memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
199 	}
200 #endif
201 }
202 
203 static int ipip6_tunnel_create(struct net_device *dev)
204 {
205 	struct ip_tunnel *t = netdev_priv(dev);
206 	struct sit_net *sitn = net_generic(t->net, sit_net_id);
207 	int err;
208 
209 	__dev_addr_set(dev, &t->parms.iph.saddr, 4);
210 	memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
211 
212 	if (test_bit(IP_TUNNEL_SIT_ISATAP_BIT, t->parms.i_flags))
213 		dev->priv_flags |= IFF_ISATAP;
214 
215 	dev->rtnl_link_ops = &sit_link_ops;
216 
217 	err = register_netdevice(dev);
218 	if (err < 0)
219 		goto out;
220 
221 	ipip6_tunnel_clone_6rd(dev, sitn);
222 
223 	ipip6_tunnel_link(sitn, t);
224 	return 0;
225 
226 out:
227 	return err;
228 }
229 
230 static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
231 					     struct ip_tunnel_parm_kern *parms,
232 					     int create)
233 {
234 	__be32 remote = parms->iph.daddr;
235 	__be32 local = parms->iph.saddr;
236 	struct ip_tunnel *t, *nt;
237 	struct ip_tunnel __rcu **tp;
238 	struct net_device *dev;
239 	char name[IFNAMSIZ];
240 	struct sit_net *sitn = net_generic(net, sit_net_id);
241 
242 	for (tp = __ipip6_bucket(sitn, parms);
243 	    (t = rtnl_dereference(*tp)) != NULL;
244 	     tp = &t->next) {
245 		if (local == t->parms.iph.saddr &&
246 		    remote == t->parms.iph.daddr &&
247 		    parms->link == t->parms.link) {
248 			if (create)
249 				return NULL;
250 			else
251 				return t;
252 		}
253 	}
254 	if (!create)
255 		goto failed;
256 
257 	if (parms->name[0]) {
258 		if (!dev_valid_name(parms->name))
259 			goto failed;
260 		strscpy(name, parms->name);
261 	} else {
262 		strscpy(name, "sit%d");
263 	}
264 	dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
265 			   ipip6_tunnel_setup);
266 	if (!dev)
267 		return NULL;
268 
269 	dev_net_set(dev, net);
270 
271 	nt = netdev_priv(dev);
272 
273 	nt->net = net;
274 	nt->parms = *parms;
275 	if (ipip6_tunnel_create(dev) < 0)
276 		goto failed_free;
277 
278 	if (!parms->name[0])
279 		strscpy(parms->name, dev->name);
280 
281 	return nt;
282 
283 failed_free:
284 	free_netdev(dev);
285 failed:
286 	return NULL;
287 }
288 
289 #define for_each_prl_rcu(start)			\
290 	for (prl = rcu_dereference(start);	\
291 	     prl;				\
292 	     prl = rcu_dereference(prl->next))
293 
294 static struct ip_tunnel_prl_entry *
295 __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
296 {
297 	struct ip_tunnel_prl_entry *prl;
298 
299 	for_each_prl_rcu(t->prl)
300 		if (prl->addr == addr)
301 			break;
302 	return prl;
303 
304 }
305 
306 static int ipip6_tunnel_get_prl(struct net_device *dev, struct ip_tunnel_prl __user *a)
307 {
308 	struct ip_tunnel *t = netdev_priv(dev);
309 	struct ip_tunnel_prl kprl, *kp;
310 	struct ip_tunnel_prl_entry *prl;
311 	unsigned int cmax, c = 0, ca, len;
312 	int ret;
313 
314 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
315 		return -EINVAL;
316 
317 	if (copy_from_user(&kprl, a, sizeof(kprl)))
318 		return -EFAULT;
319 	cmax = kprl.datalen / sizeof(kprl);
320 	if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
321 		cmax = 1;
322 
323 	/* For simple GET or for root users,
324 	 * we try harder to allocate.
325 	 */
326 	kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
327 		kzalloc_objs(*kp, cmax, GFP_KERNEL_ACCOUNT | __GFP_NOWARN) :
328 		NULL;
329 
330 	ca = min(t->prl_count, cmax);
331 
332 	if (!kp) {
333 		/* We don't try hard to allocate much memory for
334 		 * non-root users.
335 		 * For root users, retry allocating enough memory for
336 		 * the answer.
337 		 */
338 		kp = kzalloc_objs(*kp, ca,
339 				  GFP_ATOMIC | __GFP_ACCOUNT | __GFP_NOWARN);
340 		if (!kp) {
341 			ret = -ENOMEM;
342 			goto out;
343 		}
344 	}
345 
346 	rcu_read_lock();
347 	for_each_prl_rcu(t->prl) {
348 		if (c >= cmax)
349 			break;
350 		if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
351 			continue;
352 		kp[c].addr = prl->addr;
353 		kp[c].flags = prl->flags;
354 		c++;
355 		if (kprl.addr != htonl(INADDR_ANY))
356 			break;
357 	}
358 
359 	rcu_read_unlock();
360 
361 	len = sizeof(*kp) * c;
362 	ret = 0;
363 	if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
364 		ret = -EFAULT;
365 
366 	kfree(kp);
367 out:
368 	return ret;
369 }
370 
371 static int
372 ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
373 {
374 	struct ip_tunnel_prl_entry *p;
375 	int err = 0;
376 
377 	if (a->addr == htonl(INADDR_ANY))
378 		return -EINVAL;
379 
380 	ASSERT_RTNL();
381 
382 	for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
383 		if (p->addr == a->addr) {
384 			if (chg) {
385 				p->flags = a->flags;
386 				goto out;
387 			}
388 			err = -EEXIST;
389 			goto out;
390 		}
391 	}
392 
393 	if (chg) {
394 		err = -ENXIO;
395 		goto out;
396 	}
397 
398 	p = kzalloc_obj(struct ip_tunnel_prl_entry);
399 	if (!p) {
400 		err = -ENOBUFS;
401 		goto out;
402 	}
403 
404 	p->next = t->prl;
405 	p->addr = a->addr;
406 	p->flags = a->flags;
407 	t->prl_count++;
408 	rcu_assign_pointer(t->prl, p);
409 out:
410 	return err;
411 }
412 
413 static void prl_list_destroy_rcu(struct rcu_head *head)
414 {
415 	struct ip_tunnel_prl_entry *p, *n;
416 
417 	p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
418 	do {
419 		n = rcu_dereference_protected(p->next, 1);
420 		kfree(p);
421 		p = n;
422 	} while (p);
423 }
424 
425 static int
426 ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
427 {
428 	struct ip_tunnel_prl_entry *x;
429 	struct ip_tunnel_prl_entry __rcu **p;
430 	int err = 0;
431 
432 	ASSERT_RTNL();
433 
434 	if (a && a->addr != htonl(INADDR_ANY)) {
435 		for (p = &t->prl;
436 		     (x = rtnl_dereference(*p)) != NULL;
437 		     p = &x->next) {
438 			if (x->addr == a->addr) {
439 				*p = x->next;
440 				kfree_rcu(x, rcu_head);
441 				t->prl_count--;
442 				goto out;
443 			}
444 		}
445 		err = -ENXIO;
446 	} else {
447 		x = rtnl_dereference(t->prl);
448 		if (x) {
449 			t->prl_count = 0;
450 			call_rcu(&x->rcu_head, prl_list_destroy_rcu);
451 			t->prl = NULL;
452 		}
453 	}
454 out:
455 	return err;
456 }
457 
458 static int ipip6_tunnel_prl_ctl(struct net_device *dev,
459 				struct ip_tunnel_prl __user *data, int cmd)
460 {
461 	struct ip_tunnel *t = netdev_priv(dev);
462 	struct ip_tunnel_prl prl;
463 	int err;
464 
465 	if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
466 		return -EPERM;
467 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
468 		return -EINVAL;
469 
470 	if (copy_from_user(&prl, data, sizeof(prl)))
471 		return -EFAULT;
472 
473 	switch (cmd) {
474 	case SIOCDELPRL:
475 		err = ipip6_tunnel_del_prl(t, &prl);
476 		break;
477 	case SIOCADDPRL:
478 	case SIOCCHGPRL:
479 		err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
480 		break;
481 	}
482 	dst_cache_reset(&t->dst_cache);
483 	netdev_state_change(dev);
484 	return err;
485 }
486 
487 static int
488 isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
489 {
490 	struct ip_tunnel_prl_entry *p;
491 	int ok = 1;
492 
493 	rcu_read_lock();
494 	p = __ipip6_tunnel_locate_prl(t, iph->saddr);
495 	if (p) {
496 		if (p->flags & PRL_DEFAULT)
497 			skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
498 		else
499 			skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
500 	} else {
501 		const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
502 
503 		if (ipv6_addr_is_isatap(addr6) &&
504 		    (addr6->s6_addr32[3] == iph->saddr) &&
505 		    ipv6_chk_prefix(addr6, t->dev))
506 			skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
507 		else
508 			ok = 0;
509 	}
510 	rcu_read_unlock();
511 	return ok;
512 }
513 
514 static void ipip6_tunnel_uninit(struct net_device *dev)
515 {
516 	struct ip_tunnel *tunnel = netdev_priv(dev);
517 	struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
518 
519 	if (dev == sitn->fb_tunnel_dev) {
520 		RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
521 	} else {
522 		ipip6_tunnel_unlink(sitn, tunnel);
523 		ipip6_tunnel_del_prl(tunnel, NULL);
524 	}
525 	dst_cache_reset(&tunnel->dst_cache);
526 	netdev_put(dev, &tunnel->dev_tracker);
527 }
528 
529 static int ipip6_err(struct sk_buff *skb, u32 info)
530 {
531 	const struct iphdr *iph = (const struct iphdr *)skb->data;
532 	const int type = icmp_hdr(skb)->type;
533 	const int code = icmp_hdr(skb)->code;
534 	unsigned int data_len = 0;
535 	struct ip_tunnel *t;
536 	int sifindex;
537 	int err;
538 
539 	switch (type) {
540 	default:
541 	case ICMP_PARAMETERPROB:
542 		return 0;
543 
544 	case ICMP_DEST_UNREACH:
545 		switch (code) {
546 		case ICMP_SR_FAILED:
547 			/* Impossible event. */
548 			return 0;
549 		default:
550 			/* All others are translated to HOST_UNREACH.
551 			   rfc2003 contains "deep thoughts" about NET_UNREACH,
552 			   I believe they are just ether pollution. --ANK
553 			 */
554 			break;
555 		}
556 		break;
557 	case ICMP_TIME_EXCEEDED:
558 		if (code != ICMP_EXC_TTL)
559 			return 0;
560 		data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
561 		break;
562 	case ICMP_REDIRECT:
563 		break;
564 	}
565 
566 	err = -ENOENT;
567 
568 	sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
569 	t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
570 				iph->daddr, iph->saddr, sifindex);
571 	if (!t)
572 		goto out;
573 
574 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
575 		ipv4_update_pmtu(skb, dev_net(skb->dev), info,
576 				 t->parms.link, iph->protocol);
577 		err = 0;
578 		goto out;
579 	}
580 	if (type == ICMP_REDIRECT) {
581 		ipv4_redirect(skb, dev_net(skb->dev), t->parms.link,
582 			      iph->protocol);
583 		err = 0;
584 		goto out;
585 	}
586 
587 	err = 0;
588 	if (__in6_dev_get(skb->dev) &&
589 	    !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len))
590 		goto out;
591 
592 	if (t->parms.iph.daddr == 0)
593 		goto out;
594 
595 	if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
596 		goto out;
597 
598 	if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
599 		t->err_count++;
600 	else
601 		t->err_count = 1;
602 	t->err_time = jiffies;
603 out:
604 	return err;
605 }
606 
607 static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
608 				  const struct in6_addr *v6addr)
609 {
610 	__be32 v4embed = 0;
611 	if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
612 		return true;
613 	return false;
614 }
615 
616 /* Checks if an address matches an address on the tunnel interface.
617  * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
618  * Long story:
619  * This function is called after we considered the packet as spoofed
620  * in is_spoofed_6rd.
621  * We may have a router that is doing NAT for proto 41 packets
622  * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
623  * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
624  * function will return true, dropping the packet.
625  * But, we can still check if is spoofed against the IP
626  * addresses associated with the interface.
627  */
628 static bool only_dnatted(const struct ip_tunnel *tunnel,
629 	const struct in6_addr *v6dst)
630 {
631 	int prefix_len;
632 
633 #ifdef CONFIG_IPV6_SIT_6RD
634 	prefix_len = tunnel->ip6rd.prefixlen + 32
635 		- tunnel->ip6rd.relay_prefixlen;
636 #else
637 	prefix_len = 48;
638 #endif
639 	return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
640 }
641 
642 /* Returns true if a packet is spoofed */
643 static bool packet_is_spoofed(struct sk_buff *skb,
644 			      const struct iphdr *iph,
645 			      struct ip_tunnel *tunnel)
646 {
647 	const struct ipv6hdr *ipv6h;
648 
649 	if (tunnel->dev->priv_flags & IFF_ISATAP) {
650 		if (!isatap_chksrc(skb, iph, tunnel))
651 			return true;
652 
653 		return false;
654 	}
655 
656 	if (tunnel->dev->flags & IFF_POINTOPOINT)
657 		return false;
658 
659 	ipv6h = ipv6_hdr(skb);
660 
661 	if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
662 		net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
663 				     &iph->saddr, &ipv6h->saddr,
664 				     &iph->daddr, &ipv6h->daddr);
665 		return true;
666 	}
667 
668 	if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
669 		return false;
670 
671 	if (only_dnatted(tunnel, &ipv6h->daddr))
672 		return false;
673 
674 	net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
675 			     &iph->saddr, &ipv6h->saddr,
676 			     &iph->daddr, &ipv6h->daddr);
677 	return true;
678 }
679 
680 static int ipip6_rcv(struct sk_buff *skb)
681 {
682 	const struct iphdr *iph = ip_hdr(skb);
683 	struct ip_tunnel *tunnel;
684 	int sifindex;
685 	int err;
686 
687 	sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
688 	tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
689 				     iph->saddr, iph->daddr, sifindex);
690 	if (tunnel) {
691 		if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
692 		    tunnel->parms.iph.protocol != 0)
693 			goto out;
694 
695 		skb->mac_header = skb->network_header;
696 		skb_reset_network_header(skb);
697 		IPCB(skb)->flags = 0;
698 		skb->dev = tunnel->dev;
699 
700 		if (packet_is_spoofed(skb, iph, tunnel)) {
701 			DEV_STATS_INC(tunnel->dev, rx_errors);
702 			goto out;
703 		}
704 
705 		if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6),
706 		    !net_eq(tunnel->net, dev_net(tunnel->dev))))
707 			goto out;
708 
709 		/* 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);
713 		skb_reset_mac_header(skb);
714 
715 		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 				DEV_STATS_INC(tunnel->dev, rx_frame_errors);
722 				DEV_STATS_INC(tunnel->dev, rx_errors);
723 				goto out;
724 			}
725 		}
726 
727 		dev_sw_netstats_rx_add(tunnel->dev, skb->len);
728 
729 		netif_rx(skb);
730 
731 		return 0;
732 	}
733 
734 	/* no tunnel matched,  let upstream know, ipsec may handle it */
735 	return 1;
736 out:
737 	kfree_skb(skb);
738 	return 0;
739 }
740 
741 static const struct tnl_ptk_info ipip_tpi = {
742 	/* no tunnel info required for ipip. */
743 	.proto = htons(ETH_P_IP),
744 };
745 
746 #if IS_ENABLED(CONFIG_MPLS)
747 static const struct tnl_ptk_info mplsip_tpi = {
748 	/* no tunnel info required for mplsip. */
749 	.proto = htons(ETH_P_MPLS_UC),
750 };
751 #endif
752 
753 static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
754 {
755 	const struct iphdr *iph;
756 	struct ip_tunnel *tunnel;
757 	int sifindex;
758 
759 	sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
760 
761 	iph = ip_hdr(skb);
762 	tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
763 				     iph->saddr, iph->daddr, sifindex);
764 	if (tunnel) {
765 		const struct tnl_ptk_info *tpi;
766 
767 		if (tunnel->parms.iph.protocol != ipproto &&
768 		    tunnel->parms.iph.protocol != 0)
769 			goto drop;
770 
771 		if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
772 			goto drop;
773 #if IS_ENABLED(CONFIG_MPLS)
774 		if (ipproto == IPPROTO_MPLS)
775 			tpi = &mplsip_tpi;
776 		else
777 #endif
778 			tpi = &ipip_tpi;
779 		if (iptunnel_pull_header(skb, 0, tpi->proto, false))
780 			goto drop;
781 		skb_reset_mac_header(skb);
782 
783 		return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
784 	}
785 
786 	return 1;
787 
788 drop:
789 	kfree_skb(skb);
790 	return 0;
791 }
792 
793 static int ipip_rcv(struct sk_buff *skb)
794 {
795 	return sit_tunnel_rcv(skb, IPPROTO_IPIP);
796 }
797 
798 #if IS_ENABLED(CONFIG_MPLS)
799 static int mplsip_rcv(struct sk_buff *skb)
800 {
801 	return sit_tunnel_rcv(skb, IPPROTO_MPLS);
802 }
803 #endif
804 
805 /*
806  * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
807  * stores the embedded IPv4 address in v4dst and returns true.
808  */
809 static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
810 		      __be32 *v4dst)
811 {
812 #ifdef CONFIG_IPV6_SIT_6RD
813 	if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
814 			      tunnel->ip6rd.prefixlen)) {
815 		unsigned int pbw0, pbi0;
816 		int pbi1;
817 		u32 d;
818 
819 		pbw0 = tunnel->ip6rd.prefixlen >> 5;
820 		pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
821 
822 		d = tunnel->ip6rd.relay_prefixlen < 32 ?
823 			(ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
824 		    tunnel->ip6rd.relay_prefixlen : 0;
825 
826 		pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
827 		if (pbi1 > 0)
828 			d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
829 			     (32 - pbi1);
830 
831 		*v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
832 		return true;
833 	}
834 #else
835 	if (v6dst->s6_addr16[0] == htons(0x2002)) {
836 		/* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
837 		memcpy(v4dst, &v6dst->s6_addr16[1], 4);
838 		return true;
839 	}
840 #endif
841 	return false;
842 }
843 
844 static inline __be32 try_6rd(struct ip_tunnel *tunnel,
845 			     const struct in6_addr *v6dst)
846 {
847 	__be32 dst = 0;
848 	check_6rd(tunnel, v6dst, &dst);
849 	return dst;
850 }
851 
852 static bool ipip6_tunnel_dst_find(struct sk_buff *skb, __be32 *dst,
853 				  bool is_isatap)
854 {
855 	const struct ipv6hdr *iph6 = ipv6_hdr(skb);
856 	struct neighbour *neigh = NULL;
857 	const struct in6_addr *addr6;
858 	bool found = false;
859 	int addr_type;
860 
861 	if (skb_dst(skb))
862 		neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
863 
864 	if (!neigh) {
865 		net_dbg_ratelimited("nexthop == NULL\n");
866 		return false;
867 	}
868 
869 	addr6 = (const struct in6_addr *)&neigh->primary_key;
870 	addr_type = ipv6_addr_type(addr6);
871 
872 	if (is_isatap) {
873 		if ((addr_type & IPV6_ADDR_UNICAST) &&
874 		    ipv6_addr_is_isatap(addr6)) {
875 			*dst = addr6->s6_addr32[3];
876 			found = true;
877 		}
878 	} else {
879 		if (addr_type == IPV6_ADDR_ANY) {
880 			addr6 = &ipv6_hdr(skb)->daddr;
881 			addr_type = ipv6_addr_type(addr6);
882 		}
883 
884 		if ((addr_type & IPV6_ADDR_COMPATv4) != 0) {
885 			*dst = addr6->s6_addr32[3];
886 			found = true;
887 		}
888 	}
889 
890 	neigh_release(neigh);
891 
892 	return found;
893 }
894 
895 /*
896  *	This function assumes it is being called from dev_queue_xmit()
897  *	and that skb is filled properly by that function.
898  */
899 
900 static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
901 				     struct net_device *dev)
902 {
903 	struct ip_tunnel *tunnel = netdev_priv(dev);
904 	const struct iphdr  *tiph = &tunnel->parms.iph;
905 	const struct ipv6hdr *iph6 = ipv6_hdr(skb);
906 	u8     tos = tunnel->parms.iph.tos;
907 	__be16 df = tiph->frag_off;
908 	struct rtable *rt;		/* Route to the other host */
909 	struct net_device *tdev;	/* Device to other host */
910 	unsigned int max_headroom;	/* The extra header space needed */
911 	__be32 dst = tiph->daddr;
912 	struct flowi4 fl4;
913 	int    mtu;
914 	u8 ttl;
915 	u8 protocol = IPPROTO_IPV6;
916 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
917 
918 	if (tos == 1)
919 		tos = ipv6_get_dsfield(iph6);
920 
921 	/* ISATAP (RFC4214) - must come before 6to4 */
922 	if ((dev->priv_flags & IFF_ISATAP) &&
923 	    !ipip6_tunnel_dst_find(skb, &dst, true))
924 		goto tx_error;
925 
926 	if (!dst)
927 		dst = try_6rd(tunnel, &iph6->daddr);
928 
929 	if (!dst && !ipip6_tunnel_dst_find(skb, &dst, false))
930 		goto tx_error;
931 
932 	flowi4_init_output(&fl4, tunnel->parms.link, tunnel->fwmark,
933 			   tos & INET_DSCP_MASK, RT_SCOPE_UNIVERSE,
934 			   IPPROTO_IPV6, 0, dst, tiph->saddr, 0, 0,
935 			   sock_net_uid(tunnel->net, NULL));
936 
937 	rt = dst_cache_get_ip4(&tunnel->dst_cache, &fl4.saddr);
938 	if (!rt) {
939 		rt = ip_route_output_flow(tunnel->net, &fl4, NULL);
940 		if (IS_ERR(rt)) {
941 			DEV_STATS_INC(dev, tx_carrier_errors);
942 			goto tx_error_icmp;
943 		}
944 		dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst, fl4.saddr);
945 	}
946 
947 	if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
948 		ip_rt_put(rt);
949 		DEV_STATS_INC(dev, tx_carrier_errors);
950 		goto tx_error_icmp;
951 	}
952 	tdev = rt->dst.dev;
953 
954 	if (tdev == dev) {
955 		ip_rt_put(rt);
956 		DEV_STATS_INC(dev, collisions);
957 		goto tx_error;
958 	}
959 
960 	if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) {
961 		ip_rt_put(rt);
962 		goto tx_error;
963 	}
964 
965 	if (df) {
966 		mtu = dst4_mtu(&rt->dst) - t_hlen;
967 
968 		if (mtu < IPV4_MIN_MTU) {
969 			DEV_STATS_INC(dev, collisions);
970 			ip_rt_put(rt);
971 			goto tx_error;
972 		}
973 
974 		if (mtu < IPV6_MIN_MTU) {
975 			mtu = IPV6_MIN_MTU;
976 			df = 0;
977 		}
978 
979 		if (tunnel->parms.iph.daddr)
980 			skb_dst_update_pmtu_no_confirm(skb, mtu);
981 
982 		if (skb->len > mtu && !skb_is_gso(skb)) {
983 			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
984 			ip_rt_put(rt);
985 			goto tx_error;
986 		}
987 	}
988 
989 	if (tunnel->err_count > 0) {
990 		if (time_before(jiffies,
991 				tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
992 			tunnel->err_count--;
993 			dst_link_failure(skb);
994 		} else
995 			tunnel->err_count = 0;
996 	}
997 
998 	/*
999 	 * Okay, now see if we can stuff it in the buffer as-is.
1000 	 */
1001 	max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
1002 
1003 	if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1004 	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1005 		struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1006 		if (!new_skb) {
1007 			ip_rt_put(rt);
1008 			DEV_STATS_INC(dev, tx_dropped);
1009 			kfree_skb(skb);
1010 			return NETDEV_TX_OK;
1011 		}
1012 		if (skb->sk)
1013 			skb_set_owner_w(new_skb, skb->sk);
1014 		dev_kfree_skb(skb);
1015 		skb = new_skb;
1016 		iph6 = ipv6_hdr(skb);
1017 	}
1018 	ttl = tiph->ttl;
1019 	if (ttl == 0)
1020 		ttl = iph6->hop_limit;
1021 	tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
1022 
1023 	if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0) {
1024 		ip_rt_put(rt);
1025 		goto tx_error;
1026 	}
1027 
1028 	skb_set_inner_ipproto(skb, IPPROTO_IPV6);
1029 
1030 	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
1031 		      df, !net_eq(tunnel->net, dev_net(dev)), 0);
1032 	return NETDEV_TX_OK;
1033 
1034 tx_error_icmp:
1035 	dst_link_failure(skb);
1036 tx_error:
1037 	kfree_skb(skb);
1038 	DEV_STATS_INC(dev, tx_errors);
1039 	return NETDEV_TX_OK;
1040 }
1041 
1042 static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
1043 				     struct net_device *dev, u8 ipproto)
1044 {
1045 	struct ip_tunnel *tunnel = netdev_priv(dev);
1046 	const struct iphdr  *tiph = &tunnel->parms.iph;
1047 
1048 	if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
1049 		goto tx_error;
1050 
1051 	skb_set_inner_ipproto(skb, ipproto);
1052 
1053 	ip_tunnel_xmit(skb, dev, tiph, ipproto);
1054 	return NETDEV_TX_OK;
1055 tx_error:
1056 	kfree_skb(skb);
1057 	DEV_STATS_INC(dev, tx_errors);
1058 	return NETDEV_TX_OK;
1059 }
1060 
1061 static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
1062 				   struct net_device *dev)
1063 {
1064 	if (!pskb_inet_may_pull(skb))
1065 		goto tx_err;
1066 
1067 	switch (skb->protocol) {
1068 	case htons(ETH_P_IP):
1069 		sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
1070 		break;
1071 	case htons(ETH_P_IPV6):
1072 		ipip6_tunnel_xmit(skb, dev);
1073 		break;
1074 #if IS_ENABLED(CONFIG_MPLS)
1075 	case htons(ETH_P_MPLS_UC):
1076 		sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
1077 		break;
1078 #endif
1079 	default:
1080 		goto tx_err;
1081 	}
1082 
1083 	return NETDEV_TX_OK;
1084 
1085 tx_err:
1086 	DEV_STATS_INC(dev, tx_errors);
1087 	kfree_skb(skb);
1088 	return NETDEV_TX_OK;
1089 
1090 }
1091 
1092 static void ipip6_tunnel_bind_dev(struct net_device *dev)
1093 {
1094 	struct ip_tunnel *tunnel = netdev_priv(dev);
1095 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1096 	struct net_device *tdev = NULL;
1097 	int hlen = LL_MAX_HEADER;
1098 	const struct iphdr *iph;
1099 	struct flowi4 fl4;
1100 
1101 	iph = &tunnel->parms.iph;
1102 
1103 	if (iph->daddr) {
1104 		struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
1105 							  NULL,
1106 							  iph->daddr, iph->saddr,
1107 							  0, 0,
1108 							  IPPROTO_IPV6,
1109 							  iph->tos & INET_DSCP_MASK,
1110 							  tunnel->parms.link);
1111 
1112 		if (!IS_ERR(rt)) {
1113 			tdev = rt->dst.dev;
1114 			ip_rt_put(rt);
1115 		}
1116 		dev->flags |= IFF_POINTOPOINT;
1117 	}
1118 
1119 	if (!tdev && tunnel->parms.link)
1120 		tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
1121 
1122 	if (tdev && !netif_is_l3_master(tdev)) {
1123 		int mtu;
1124 
1125 		mtu = tdev->mtu - t_hlen;
1126 		if (mtu < IPV6_MIN_MTU)
1127 			mtu = IPV6_MIN_MTU;
1128 		WRITE_ONCE(dev->mtu, mtu);
1129 		hlen = tdev->hard_header_len + tdev->needed_headroom;
1130 	}
1131 	dev->needed_headroom = t_hlen + hlen;
1132 }
1133 
1134 static void ipip6_tunnel_update(struct ip_tunnel *t,
1135 				struct ip_tunnel_parm_kern *p,
1136 				__u32 fwmark)
1137 {
1138 	struct net *net = t->net;
1139 	struct sit_net *sitn = net_generic(net, sit_net_id);
1140 
1141 	ipip6_tunnel_unlink(sitn, t);
1142 	synchronize_net();
1143 	t->parms.iph.saddr = p->iph.saddr;
1144 	t->parms.iph.daddr = p->iph.daddr;
1145 	__dev_addr_set(t->dev, &p->iph.saddr, 4);
1146 	memcpy(t->dev->broadcast, &p->iph.daddr, 4);
1147 	ipip6_tunnel_link(sitn, t);
1148 	t->parms.iph.ttl = p->iph.ttl;
1149 	t->parms.iph.tos = p->iph.tos;
1150 	t->parms.iph.frag_off = p->iph.frag_off;
1151 	if (t->parms.link != p->link || t->fwmark != fwmark) {
1152 		t->parms.link = p->link;
1153 		t->fwmark = fwmark;
1154 		ipip6_tunnel_bind_dev(t->dev);
1155 	}
1156 	dst_cache_reset(&t->dst_cache);
1157 	netdev_state_change(t->dev);
1158 }
1159 
1160 #ifdef CONFIG_IPV6_SIT_6RD
1161 static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
1162 				   struct ip_tunnel_6rd *ip6rd)
1163 {
1164 	struct in6_addr prefix;
1165 	__be32 relay_prefix;
1166 
1167 	if (ip6rd->relay_prefixlen > 32 ||
1168 	    ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
1169 		return -EINVAL;
1170 
1171 	ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
1172 	if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
1173 		return -EINVAL;
1174 	if (ip6rd->relay_prefixlen)
1175 		relay_prefix = ip6rd->relay_prefix &
1176 			       htonl(0xffffffffUL <<
1177 				     (32 - ip6rd->relay_prefixlen));
1178 	else
1179 		relay_prefix = 0;
1180 	if (relay_prefix != ip6rd->relay_prefix)
1181 		return -EINVAL;
1182 
1183 	t->ip6rd.prefix = prefix;
1184 	t->ip6rd.relay_prefix = relay_prefix;
1185 	t->ip6rd.prefixlen = ip6rd->prefixlen;
1186 	t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
1187 	dst_cache_reset(&t->dst_cache);
1188 	netdev_state_change(t->dev);
1189 	return 0;
1190 }
1191 
1192 static int
1193 ipip6_tunnel_get6rd(struct net_device *dev, struct ip_tunnel_parm __user *data)
1194 {
1195 	struct ip_tunnel *t = netdev_priv(dev);
1196 	struct ip_tunnel_parm_kern p;
1197 	struct ip_tunnel_6rd ip6rd;
1198 
1199 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
1200 		if (!ip_tunnel_parm_from_user(&p, data))
1201 			return -EFAULT;
1202 		t = ipip6_tunnel_locate(t->net, &p, 0);
1203 	}
1204 	if (!t)
1205 		t = netdev_priv(dev);
1206 
1207 	ip6rd.prefix = t->ip6rd.prefix;
1208 	ip6rd.relay_prefix = t->ip6rd.relay_prefix;
1209 	ip6rd.prefixlen = t->ip6rd.prefixlen;
1210 	ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
1211 	if (copy_to_user(data, &ip6rd, sizeof(ip6rd)))
1212 		return -EFAULT;
1213 	return 0;
1214 }
1215 
1216 static int
1217 ipip6_tunnel_6rdctl(struct net_device *dev, struct ip_tunnel_6rd __user *data,
1218 		    int cmd)
1219 {
1220 	struct ip_tunnel *t = netdev_priv(dev);
1221 	struct ip_tunnel_6rd ip6rd;
1222 	int err;
1223 
1224 	if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
1225 		return -EPERM;
1226 	if (copy_from_user(&ip6rd, data, sizeof(ip6rd)))
1227 		return -EFAULT;
1228 
1229 	if (cmd != SIOCDEL6RD) {
1230 		err = ipip6_tunnel_update_6rd(t, &ip6rd);
1231 		if (err < 0)
1232 			return err;
1233 	} else
1234 		ipip6_tunnel_clone_6rd(dev, dev_to_sit_net(dev));
1235 	return 0;
1236 }
1237 
1238 #endif /* CONFIG_IPV6_SIT_6RD */
1239 
1240 static bool ipip6_valid_ip_proto(u8 ipproto)
1241 {
1242 	return ipproto == IPPROTO_IPV6 ||
1243 		ipproto == IPPROTO_IPIP ||
1244 #if IS_ENABLED(CONFIG_MPLS)
1245 		ipproto == IPPROTO_MPLS ||
1246 #endif
1247 		ipproto == 0;
1248 }
1249 
1250 static int
1251 __ipip6_tunnel_ioctl_validate(struct net *net, struct ip_tunnel_parm_kern *p)
1252 {
1253 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1254 		return -EPERM;
1255 
1256 	if (!ipip6_valid_ip_proto(p->iph.protocol))
1257 		return -EINVAL;
1258 	if (p->iph.version != 4 ||
1259 	    p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)))
1260 		return -EINVAL;
1261 
1262 	if (p->iph.ttl)
1263 		p->iph.frag_off |= htons(IP_DF);
1264 	return 0;
1265 }
1266 
1267 static int
1268 ipip6_tunnel_get(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1269 {
1270 	struct ip_tunnel *t = netdev_priv(dev);
1271 
1272 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
1273 		t = ipip6_tunnel_locate(t->net, p, 0);
1274 	if (!t)
1275 		t = netdev_priv(dev);
1276 	memcpy(p, &t->parms, sizeof(*p));
1277 	return 0;
1278 }
1279 
1280 static int
1281 ipip6_tunnel_add(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1282 {
1283 	struct ip_tunnel *t = netdev_priv(dev);
1284 	int err;
1285 
1286 	err = __ipip6_tunnel_ioctl_validate(t->net, p);
1287 	if (err)
1288 		return err;
1289 
1290 	t = ipip6_tunnel_locate(t->net, p, 1);
1291 	if (!t)
1292 		return -ENOBUFS;
1293 	return 0;
1294 }
1295 
1296 static int
1297 ipip6_tunnel_change(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1298 {
1299 	struct ip_tunnel *t = netdev_priv(dev);
1300 	int err;
1301 
1302 	err = __ipip6_tunnel_ioctl_validate(t->net, p);
1303 	if (err)
1304 		return err;
1305 
1306 	t = ipip6_tunnel_locate(t->net, p, 0);
1307 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
1308 		if (!t)
1309 			return -ENOENT;
1310 	} else {
1311 		if (t) {
1312 			if (t->dev != dev)
1313 				return -EEXIST;
1314 		} else {
1315 			if (((dev->flags & IFF_POINTOPOINT) && !p->iph.daddr) ||
1316 			    (!(dev->flags & IFF_POINTOPOINT) && p->iph.daddr))
1317 				return -EINVAL;
1318 			t = netdev_priv(dev);
1319 		}
1320 
1321 		ipip6_tunnel_update(t, p, t->fwmark);
1322 	}
1323 
1324 	return 0;
1325 }
1326 
1327 static int
1328 ipip6_tunnel_del(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1329 {
1330 	struct ip_tunnel *t = netdev_priv(dev);
1331 
1332 	if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
1333 		return -EPERM;
1334 
1335 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
1336 		t = ipip6_tunnel_locate(t->net, p, 0);
1337 		if (!t)
1338 			return -ENOENT;
1339 		if (t == netdev_priv(dev_to_sit_net(dev)->fb_tunnel_dev))
1340 			return -EPERM;
1341 		dev = t->dev;
1342 	}
1343 	unregister_netdevice(dev);
1344 	return 0;
1345 }
1346 
1347 static int
1348 ipip6_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p,
1349 		 int cmd)
1350 {
1351 	switch (cmd) {
1352 	case SIOCGETTUNNEL:
1353 		return ipip6_tunnel_get(dev, p);
1354 	case SIOCADDTUNNEL:
1355 		return ipip6_tunnel_add(dev, p);
1356 	case SIOCCHGTUNNEL:
1357 		return ipip6_tunnel_change(dev, p);
1358 	case SIOCDELTUNNEL:
1359 		return ipip6_tunnel_del(dev, p);
1360 	default:
1361 		return -EINVAL;
1362 	}
1363 }
1364 
1365 static int
1366 ipip6_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
1367 			    void __user *data, int cmd)
1368 {
1369 	switch (cmd) {
1370 	case SIOCGETTUNNEL:
1371 	case SIOCADDTUNNEL:
1372 	case SIOCCHGTUNNEL:
1373 	case SIOCDELTUNNEL:
1374 		return ip_tunnel_siocdevprivate(dev, ifr, data, cmd);
1375 	case SIOCGETPRL:
1376 		return ipip6_tunnel_get_prl(dev, data);
1377 	case SIOCADDPRL:
1378 	case SIOCDELPRL:
1379 	case SIOCCHGPRL:
1380 		return ipip6_tunnel_prl_ctl(dev, data, cmd);
1381 #ifdef CONFIG_IPV6_SIT_6RD
1382 	case SIOCGET6RD:
1383 		return ipip6_tunnel_get6rd(dev, data);
1384 	case SIOCADD6RD:
1385 	case SIOCCHG6RD:
1386 	case SIOCDEL6RD:
1387 		return ipip6_tunnel_6rdctl(dev, data, cmd);
1388 #endif
1389 	default:
1390 		return -EINVAL;
1391 	}
1392 }
1393 
1394 static const struct net_device_ops ipip6_netdev_ops = {
1395 	.ndo_init	= ipip6_tunnel_init,
1396 	.ndo_uninit	= ipip6_tunnel_uninit,
1397 	.ndo_start_xmit	= sit_tunnel_xmit,
1398 	.ndo_siocdevprivate = ipip6_tunnel_siocdevprivate,
1399 	.ndo_get_iflink = ip_tunnel_get_iflink,
1400 	.ndo_tunnel_ctl = ipip6_tunnel_ctl,
1401 };
1402 
1403 static void ipip6_dev_free(struct net_device *dev)
1404 {
1405 	struct ip_tunnel *tunnel = netdev_priv(dev);
1406 
1407 	dst_cache_destroy(&tunnel->dst_cache);
1408 }
1409 
1410 #define SIT_FEATURES (NETIF_F_SG	   | \
1411 		      NETIF_F_FRAGLIST	   | \
1412 		      NETIF_F_HIGHDMA	   | \
1413 		      NETIF_F_GSO_SOFTWARE | \
1414 		      NETIF_F_HW_CSUM)
1415 
1416 static void ipip6_tunnel_setup(struct net_device *dev)
1417 {
1418 	struct ip_tunnel *tunnel = netdev_priv(dev);
1419 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1420 
1421 	dev->netdev_ops		= &ipip6_netdev_ops;
1422 	dev->header_ops		= &ip_tunnel_header_ops;
1423 	dev->needs_free_netdev	= true;
1424 	dev->priv_destructor	= ipip6_dev_free;
1425 
1426 	dev->type		= ARPHRD_SIT;
1427 	dev->mtu		= ETH_DATA_LEN - t_hlen;
1428 	dev->min_mtu		= IPV6_MIN_MTU;
1429 	dev->max_mtu		= IP6_MAX_MTU - t_hlen;
1430 	dev->flags		= IFF_NOARP;
1431 	netif_keep_dst(dev);
1432 	dev->addr_len		= 4;
1433 	dev->lltx		= true;
1434 	dev->features		|= SIT_FEATURES;
1435 	dev->hw_features	|= SIT_FEATURES;
1436 	dev->pcpu_stat_type	= NETDEV_PCPU_STAT_TSTATS;
1437 
1438 }
1439 
1440 static int ipip6_tunnel_init(struct net_device *dev)
1441 {
1442 	struct ip_tunnel *tunnel = netdev_priv(dev);
1443 	int err;
1444 
1445 	tunnel->dev = dev;
1446 	strscpy(tunnel->parms.name, dev->name);
1447 
1448 	ipip6_tunnel_bind_dev(dev);
1449 
1450 	err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1451 	if (err)
1452 		return err;
1453 
1454 	netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1455 	netdev_lockdep_set_classes(dev);
1456 	return 0;
1457 }
1458 
1459 static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
1460 {
1461 	struct ip_tunnel *tunnel = netdev_priv(dev);
1462 	struct iphdr *iph = &tunnel->parms.iph;
1463 	struct net *net = dev_net(dev);
1464 	struct sit_net *sitn = net_generic(net, sit_net_id);
1465 
1466 	iph->version		= 4;
1467 	iph->protocol		= IPPROTO_IPV6;
1468 	iph->ihl		= 5;
1469 	iph->ttl		= 64;
1470 
1471 	rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
1472 }
1473 
1474 static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
1475 			  struct netlink_ext_ack *extack)
1476 {
1477 	u8 proto;
1478 
1479 	if (!data || !data[IFLA_IPTUN_PROTO])
1480 		return 0;
1481 
1482 	proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1483 	if (!ipip6_valid_ip_proto(proto))
1484 		return -EINVAL;
1485 
1486 	return 0;
1487 }
1488 
1489 static void ipip6_netlink_parms(struct nlattr *data[],
1490 				struct ip_tunnel_parm_kern *parms,
1491 				__u32 *fwmark)
1492 {
1493 	memset(parms, 0, sizeof(*parms));
1494 
1495 	parms->iph.version = 4;
1496 	parms->iph.protocol = IPPROTO_IPV6;
1497 	parms->iph.ihl = 5;
1498 	parms->iph.ttl = 64;
1499 
1500 	if (!data)
1501 		return;
1502 
1503 	ip_tunnel_netlink_parms(data, parms);
1504 
1505 	if (data[IFLA_IPTUN_FWMARK])
1506 		*fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
1507 }
1508 
1509 #ifdef CONFIG_IPV6_SIT_6RD
1510 /* This function returns true when 6RD attributes are present in the nl msg */
1511 static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
1512 				    struct ip_tunnel_6rd *ip6rd)
1513 {
1514 	bool ret = false;
1515 	memset(ip6rd, 0, sizeof(*ip6rd));
1516 
1517 	if (!data)
1518 		return ret;
1519 
1520 	if (data[IFLA_IPTUN_6RD_PREFIX]) {
1521 		ret = true;
1522 		ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
1523 	}
1524 
1525 	if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
1526 		ret = true;
1527 		ip6rd->relay_prefix =
1528 			nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
1529 	}
1530 
1531 	if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
1532 		ret = true;
1533 		ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
1534 	}
1535 
1536 	if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
1537 		ret = true;
1538 		ip6rd->relay_prefixlen =
1539 			nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
1540 	}
1541 
1542 	return ret;
1543 }
1544 #endif
1545 
1546 static int ipip6_newlink(struct net_device *dev,
1547 			 struct rtnl_newlink_params *params,
1548 			 struct netlink_ext_ack *extack)
1549 {
1550 	struct nlattr **data = params->data;
1551 	struct nlattr **tb = params->tb;
1552 	struct ip_tunnel *nt;
1553 	struct ip_tunnel_encap ipencap;
1554 #ifdef CONFIG_IPV6_SIT_6RD
1555 	struct ip_tunnel_6rd ip6rd;
1556 #endif
1557 	struct net *net;
1558 	int err;
1559 
1560 	net = params->link_net ? : dev_net(dev);
1561 	nt = netdev_priv(dev);
1562 	nt->net = net;
1563 
1564 	if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
1565 		err = ip_tunnel_encap_setup(nt, &ipencap);
1566 		if (err < 0)
1567 			return err;
1568 	}
1569 
1570 	ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
1571 
1572 	if (ipip6_tunnel_locate(net, &nt->parms, 0))
1573 		return -EEXIST;
1574 
1575 	err = ipip6_tunnel_create(dev);
1576 	if (err < 0)
1577 		return err;
1578 
1579 	if (tb[IFLA_MTU]) {
1580 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
1581 
1582 		if (mtu >= IPV6_MIN_MTU &&
1583 		    mtu <= IP6_MAX_MTU - dev->hard_header_len)
1584 			dev->mtu = mtu;
1585 	}
1586 
1587 #ifdef CONFIG_IPV6_SIT_6RD
1588 	if (ipip6_netlink_6rd_parms(data, &ip6rd)) {
1589 		err = ipip6_tunnel_update_6rd(nt, &ip6rd);
1590 		if (err < 0)
1591 			unregister_netdevice_queue(dev, NULL);
1592 	}
1593 #endif
1594 
1595 	return err;
1596 }
1597 
1598 static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
1599 			    struct nlattr *data[],
1600 			    struct netlink_ext_ack *extack)
1601 {
1602 	struct ip_tunnel *t = netdev_priv(dev);
1603 	struct ip_tunnel_encap ipencap;
1604 	struct ip_tunnel_parm_kern p;
1605 	struct net *net = t->net;
1606 	struct sit_net *sitn = net_generic(net, sit_net_id);
1607 #ifdef CONFIG_IPV6_SIT_6RD
1608 	struct ip_tunnel_6rd ip6rd;
1609 #endif
1610 	__u32 fwmark = t->fwmark;
1611 	int err;
1612 
1613 	if (dev == sitn->fb_tunnel_dev)
1614 		return -EINVAL;
1615 
1616 	if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
1617 		err = ip_tunnel_encap_setup(t, &ipencap);
1618 		if (err < 0)
1619 			return err;
1620 	}
1621 
1622 	ipip6_netlink_parms(data, &p, &fwmark);
1623 
1624 	if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
1625 	    (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
1626 		return -EINVAL;
1627 
1628 	t = ipip6_tunnel_locate(net, &p, 0);
1629 
1630 	if (t) {
1631 		if (t->dev != dev)
1632 			return -EEXIST;
1633 	} else
1634 		t = netdev_priv(dev);
1635 
1636 	ipip6_tunnel_update(t, &p, fwmark);
1637 
1638 #ifdef CONFIG_IPV6_SIT_6RD
1639 	if (ipip6_netlink_6rd_parms(data, &ip6rd))
1640 		return ipip6_tunnel_update_6rd(t, &ip6rd);
1641 #endif
1642 
1643 	return 0;
1644 }
1645 
1646 static size_t ipip6_get_size(const struct net_device *dev)
1647 {
1648 	return
1649 		/* IFLA_IPTUN_LINK */
1650 		nla_total_size(4) +
1651 		/* IFLA_IPTUN_LOCAL */
1652 		nla_total_size(4) +
1653 		/* IFLA_IPTUN_REMOTE */
1654 		nla_total_size(4) +
1655 		/* IFLA_IPTUN_TTL */
1656 		nla_total_size(1) +
1657 		/* IFLA_IPTUN_TOS */
1658 		nla_total_size(1) +
1659 		/* IFLA_IPTUN_PMTUDISC */
1660 		nla_total_size(1) +
1661 		/* IFLA_IPTUN_FLAGS */
1662 		nla_total_size(2) +
1663 		/* IFLA_IPTUN_PROTO */
1664 		nla_total_size(1) +
1665 #ifdef CONFIG_IPV6_SIT_6RD
1666 		/* IFLA_IPTUN_6RD_PREFIX */
1667 		nla_total_size(sizeof(struct in6_addr)) +
1668 		/* IFLA_IPTUN_6RD_RELAY_PREFIX */
1669 		nla_total_size(4) +
1670 		/* IFLA_IPTUN_6RD_PREFIXLEN */
1671 		nla_total_size(2) +
1672 		/* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
1673 		nla_total_size(2) +
1674 #endif
1675 		/* IFLA_IPTUN_ENCAP_TYPE */
1676 		nla_total_size(2) +
1677 		/* IFLA_IPTUN_ENCAP_FLAGS */
1678 		nla_total_size(2) +
1679 		/* IFLA_IPTUN_ENCAP_SPORT */
1680 		nla_total_size(2) +
1681 		/* IFLA_IPTUN_ENCAP_DPORT */
1682 		nla_total_size(2) +
1683 		/* IFLA_IPTUN_FWMARK */
1684 		nla_total_size(4) +
1685 		0;
1686 }
1687 
1688 static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1689 {
1690 	struct ip_tunnel *tunnel = netdev_priv(dev);
1691 	struct ip_tunnel_parm_kern *parm = &tunnel->parms;
1692 
1693 	if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1694 	    nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
1695 	    nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
1696 	    nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
1697 	    nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
1698 	    nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
1699 		       !!(parm->iph.frag_off & htons(IP_DF))) ||
1700 	    nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
1701 	    nla_put_be16(skb, IFLA_IPTUN_FLAGS,
1702 			 ip_tunnel_flags_to_be16(parm->i_flags)) ||
1703 	    nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
1704 		goto nla_put_failure;
1705 
1706 #ifdef CONFIG_IPV6_SIT_6RD
1707 	if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
1708 			     &tunnel->ip6rd.prefix) ||
1709 	    nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
1710 			    tunnel->ip6rd.relay_prefix) ||
1711 	    nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
1712 			tunnel->ip6rd.prefixlen) ||
1713 	    nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
1714 			tunnel->ip6rd.relay_prefixlen))
1715 		goto nla_put_failure;
1716 #endif
1717 
1718 	if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
1719 			tunnel->encap.type) ||
1720 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
1721 			tunnel->encap.sport) ||
1722 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
1723 			tunnel->encap.dport) ||
1724 	    nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
1725 			tunnel->encap.flags))
1726 		goto nla_put_failure;
1727 
1728 	return 0;
1729 
1730 nla_put_failure:
1731 	return -EMSGSIZE;
1732 }
1733 
1734 static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
1735 	[IFLA_IPTUN_LINK]		= { .type = NLA_U32 },
1736 	[IFLA_IPTUN_LOCAL]		= { .type = NLA_U32 },
1737 	[IFLA_IPTUN_REMOTE]		= { .type = NLA_U32 },
1738 	[IFLA_IPTUN_TTL]		= { .type = NLA_U8 },
1739 	[IFLA_IPTUN_TOS]		= { .type = NLA_U8 },
1740 	[IFLA_IPTUN_PMTUDISC]		= { .type = NLA_U8 },
1741 	[IFLA_IPTUN_FLAGS]		= { .type = NLA_U16 },
1742 	[IFLA_IPTUN_PROTO]		= { .type = NLA_U8 },
1743 #ifdef CONFIG_IPV6_SIT_6RD
1744 	[IFLA_IPTUN_6RD_PREFIX]		= { .len = sizeof(struct in6_addr) },
1745 	[IFLA_IPTUN_6RD_RELAY_PREFIX]	= { .type = NLA_U32 },
1746 	[IFLA_IPTUN_6RD_PREFIXLEN]	= { .type = NLA_U16 },
1747 	[IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
1748 #endif
1749 	[IFLA_IPTUN_ENCAP_TYPE]		= { .type = NLA_U16 },
1750 	[IFLA_IPTUN_ENCAP_FLAGS]	= { .type = NLA_U16 },
1751 	[IFLA_IPTUN_ENCAP_SPORT]	= { .type = NLA_U16 },
1752 	[IFLA_IPTUN_ENCAP_DPORT]	= { .type = NLA_U16 },
1753 	[IFLA_IPTUN_FWMARK]		= { .type = NLA_U32 },
1754 };
1755 
1756 static void ipip6_dellink(struct net_device *dev, struct list_head *head)
1757 {
1758 	struct net *net = dev_net(dev);
1759 	struct sit_net *sitn = net_generic(net, sit_net_id);
1760 
1761 	if (dev != sitn->fb_tunnel_dev)
1762 		unregister_netdevice_queue(dev, head);
1763 }
1764 
1765 static struct rtnl_link_ops sit_link_ops __read_mostly = {
1766 	.kind		= "sit",
1767 	.maxtype	= IFLA_IPTUN_MAX,
1768 	.policy		= ipip6_policy,
1769 	.priv_size	= sizeof(struct ip_tunnel),
1770 	.setup		= ipip6_tunnel_setup,
1771 	.validate	= ipip6_validate,
1772 	.newlink	= ipip6_newlink,
1773 	.changelink	= ipip6_changelink,
1774 	.get_size	= ipip6_get_size,
1775 	.fill_info	= ipip6_fill_info,
1776 	.dellink	= ipip6_dellink,
1777 	.get_link_net	= ip_tunnel_get_link_net,
1778 };
1779 
1780 static struct xfrm_tunnel sit_handler __read_mostly = {
1781 	.handler	=	ipip6_rcv,
1782 	.err_handler	=	ipip6_err,
1783 	.priority	=	1,
1784 };
1785 
1786 static struct xfrm_tunnel ipip_handler __read_mostly = {
1787 	.handler	=	ipip_rcv,
1788 	.err_handler	=	ipip6_err,
1789 	.priority	=	2,
1790 };
1791 
1792 #if IS_ENABLED(CONFIG_MPLS)
1793 static struct xfrm_tunnel mplsip_handler __read_mostly = {
1794 	.handler	=	mplsip_rcv,
1795 	.err_handler	=	ipip6_err,
1796 	.priority	=	2,
1797 };
1798 #endif
1799 
1800 static void __net_exit sit_exit_rtnl_net(struct net *net, struct list_head *head)
1801 {
1802 	struct sit_net *sitn = net_generic(net, sit_net_id);
1803 	struct net_device *dev, *aux;
1804 	int prio;
1805 
1806 	for_each_netdev_safe(net, dev, aux)
1807 		if (dev->rtnl_link_ops == &sit_link_ops)
1808 			unregister_netdevice_queue(dev, head);
1809 
1810 	for (prio = 0; prio < 4; prio++) {
1811 		int h;
1812 		for (h = 0; h < (prio ? IP6_SIT_HASH_SIZE : 1); h++) {
1813 			struct ip_tunnel *t;
1814 
1815 			t = rtnl_net_dereference(net, sitn->tunnels[prio][h]);
1816 			while (t) {
1817 				/* If dev is in the same netns, it has already
1818 				 * been added to the list by the previous loop.
1819 				 */
1820 				if (!net_eq(dev_net(t->dev), net))
1821 					unregister_netdevice_queue(t->dev, head);
1822 
1823 				t = rtnl_net_dereference(net, t->next);
1824 			}
1825 		}
1826 	}
1827 }
1828 
1829 static int __net_init sit_init_net(struct net *net)
1830 {
1831 	struct sit_net *sitn = net_generic(net, sit_net_id);
1832 	struct ip_tunnel *t;
1833 	int err;
1834 
1835 	sitn->tunnels[0] = sitn->tunnels_wc;
1836 	sitn->tunnels[1] = sitn->tunnels_l;
1837 	sitn->tunnels[2] = sitn->tunnels_r;
1838 	sitn->tunnels[3] = sitn->tunnels_r_l;
1839 
1840 	if (!net_has_fallback_tunnels(net))
1841 		return 0;
1842 
1843 	sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1844 					   NET_NAME_UNKNOWN,
1845 					   ipip6_tunnel_setup);
1846 	if (!sitn->fb_tunnel_dev) {
1847 		err = -ENOMEM;
1848 		goto err_alloc_dev;
1849 	}
1850 	dev_net_set(sitn->fb_tunnel_dev, net);
1851 	sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
1852 	/* FB netdevice is special: we have one, and only one per netns.
1853 	 * Allowing to move it to another netns is clearly unsafe.
1854 	 */
1855 	sitn->fb_tunnel_dev->netns_immutable = true;
1856 
1857 	t = netdev_priv(sitn->fb_tunnel_dev);
1858 	t->net = net;
1859 
1860 	err = register_netdev(sitn->fb_tunnel_dev);
1861 	if (err)
1862 		goto err_reg_dev;
1863 
1864 	ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
1865 	ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
1866 
1867 	strscpy(t->parms.name, sitn->fb_tunnel_dev->name);
1868 	return 0;
1869 
1870 err_reg_dev:
1871 	free_netdev(sitn->fb_tunnel_dev);
1872 err_alloc_dev:
1873 	return err;
1874 }
1875 
1876 static struct pernet_operations sit_net_ops = {
1877 	.init = sit_init_net,
1878 	.exit_rtnl = sit_exit_rtnl_net,
1879 	.id   = &sit_net_id,
1880 	.size = sizeof(struct sit_net),
1881 };
1882 
1883 static void __exit sit_cleanup(void)
1884 {
1885 	rtnl_link_unregister(&sit_link_ops);
1886 	xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1887 	xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
1888 #if IS_ENABLED(CONFIG_MPLS)
1889 	xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
1890 #endif
1891 
1892 	unregister_pernet_device(&sit_net_ops);
1893 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
1894 }
1895 
1896 static int __init sit_init(void)
1897 {
1898 	int err;
1899 
1900 	pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n");
1901 
1902 	err = register_pernet_device(&sit_net_ops);
1903 	if (err < 0)
1904 		return err;
1905 	err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
1906 	if (err < 0) {
1907 		pr_info("%s: can't register ip6ip4\n", __func__);
1908 		goto xfrm_tunnel_failed;
1909 	}
1910 	err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
1911 	if (err < 0) {
1912 		pr_info("%s: can't register ip4ip4\n", __func__);
1913 		goto xfrm_tunnel4_failed;
1914 	}
1915 #if IS_ENABLED(CONFIG_MPLS)
1916 	err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
1917 	if (err < 0) {
1918 		pr_info("%s: can't register mplsip\n", __func__);
1919 		goto xfrm_tunnel_mpls_failed;
1920 	}
1921 #endif
1922 	err = rtnl_link_register(&sit_link_ops);
1923 	if (err < 0)
1924 		goto rtnl_link_failed;
1925 
1926 out:
1927 	return err;
1928 
1929 rtnl_link_failed:
1930 #if IS_ENABLED(CONFIG_MPLS)
1931 	xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
1932 xfrm_tunnel_mpls_failed:
1933 #endif
1934 	xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
1935 xfrm_tunnel4_failed:
1936 	xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1937 xfrm_tunnel_failed:
1938 	unregister_pernet_device(&sit_net_ops);
1939 	goto out;
1940 }
1941 
1942 module_init(sit_init);
1943 module_exit(sit_cleanup);
1944 MODULE_DESCRIPTION("IPv6-in-IPv4 tunnel SIT driver");
1945 MODULE_LICENSE("GPL");
1946 MODULE_ALIAS_RTNL_LINK("sit");
1947 MODULE_ALIAS_NETDEV("sit0");
1948