xref: /linux/net/ipv6/sit.c (revision b693b51e0829b96a5c43f45c3fba3d11f6f09d2f)
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, READ_ONCE(t->err_time) + IPTUNNEL_ERR_TIMEO))
599 		WRITE_ONCE(t->err_count, READ_ONCE(t->err_count) + 1);
600 	else
601 		WRITE_ONCE(t->err_count, 1);
602 	WRITE_ONCE(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 	int err_count, mtu;
913 	struct flowi4 fl4;
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 	iph6 = ipv6_hdr(skb);
965 
966 	if (df) {
967 		mtu = dst4_mtu(&rt->dst) - t_hlen;
968 
969 		if (mtu < IPV4_MIN_MTU) {
970 			DEV_STATS_INC(dev, collisions);
971 			ip_rt_put(rt);
972 			goto tx_error;
973 		}
974 
975 		if (mtu < IPV6_MIN_MTU) {
976 			mtu = IPV6_MIN_MTU;
977 			df = 0;
978 		}
979 
980 		if (tunnel->parms.iph.daddr)
981 			skb_dst_update_pmtu_no_confirm(skb, mtu);
982 
983 		if (skb->len > mtu && !skb_is_gso(skb)) {
984 			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
985 			ip_rt_put(rt);
986 			goto tx_error;
987 		}
988 	}
989 
990 	err_count = READ_ONCE(tunnel->err_count);
991 	if (err_count > 0) {
992 		if (time_before(jiffies,
993 				READ_ONCE(tunnel->err_time) + IPTUNNEL_ERR_TIMEO)) {
994 			WRITE_ONCE(tunnel->err_count, err_count - 1);
995 			dst_link_failure(skb);
996 		} else {
997 			WRITE_ONCE(tunnel->err_count,  0);
998 		}
999 	}
1000 
1001 	/*
1002 	 * Okay, now see if we can stuff it in the buffer as-is.
1003 	 */
1004 	max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
1005 
1006 	if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1007 	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1008 		struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1009 		if (!new_skb) {
1010 			ip_rt_put(rt);
1011 			DEV_STATS_INC(dev, tx_dropped);
1012 			kfree_skb(skb);
1013 			return NETDEV_TX_OK;
1014 		}
1015 		if (skb->sk)
1016 			skb_set_owner_w(new_skb, skb->sk);
1017 		dev_kfree_skb(skb);
1018 		skb = new_skb;
1019 		iph6 = ipv6_hdr(skb);
1020 	}
1021 	ttl = tiph->ttl;
1022 	if (ttl == 0)
1023 		ttl = iph6->hop_limit;
1024 	tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
1025 
1026 	if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0) {
1027 		ip_rt_put(rt);
1028 		goto tx_error;
1029 	}
1030 
1031 	skb_set_inner_ipproto(skb, IPPROTO_IPV6);
1032 
1033 	iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
1034 		      df, !net_eq(tunnel->net, dev_net(dev)), 0);
1035 	return NETDEV_TX_OK;
1036 
1037 tx_error_icmp:
1038 	dst_link_failure(skb);
1039 tx_error:
1040 	kfree_skb(skb);
1041 	DEV_STATS_INC(dev, tx_errors);
1042 	return NETDEV_TX_OK;
1043 }
1044 
1045 static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
1046 				     struct net_device *dev, u8 ipproto)
1047 {
1048 	struct ip_tunnel *tunnel = netdev_priv(dev);
1049 	const struct iphdr  *tiph = &tunnel->parms.iph;
1050 
1051 	if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
1052 		goto tx_error;
1053 
1054 	skb_set_inner_ipproto(skb, ipproto);
1055 
1056 	ip_tunnel_xmit(skb, dev, tiph, ipproto);
1057 	return NETDEV_TX_OK;
1058 tx_error:
1059 	kfree_skb(skb);
1060 	DEV_STATS_INC(dev, tx_errors);
1061 	return NETDEV_TX_OK;
1062 }
1063 
1064 static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
1065 				   struct net_device *dev)
1066 {
1067 	if (!pskb_inet_may_pull(skb))
1068 		goto tx_err;
1069 
1070 	switch (skb->protocol) {
1071 	case htons(ETH_P_IP):
1072 		sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
1073 		break;
1074 	case htons(ETH_P_IPV6):
1075 		ipip6_tunnel_xmit(skb, dev);
1076 		break;
1077 #if IS_ENABLED(CONFIG_MPLS)
1078 	case htons(ETH_P_MPLS_UC):
1079 		sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
1080 		break;
1081 #endif
1082 	default:
1083 		goto tx_err;
1084 	}
1085 
1086 	return NETDEV_TX_OK;
1087 
1088 tx_err:
1089 	DEV_STATS_INC(dev, tx_errors);
1090 	kfree_skb(skb);
1091 	return NETDEV_TX_OK;
1092 
1093 }
1094 
1095 static void ipip6_tunnel_bind_dev(struct net_device *dev)
1096 {
1097 	struct ip_tunnel *tunnel = netdev_priv(dev);
1098 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1099 	struct net_device *tdev = NULL;
1100 	int hlen = LL_MAX_HEADER;
1101 	const struct iphdr *iph;
1102 	struct flowi4 fl4;
1103 
1104 	iph = &tunnel->parms.iph;
1105 
1106 	if (iph->daddr) {
1107 		struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
1108 							  NULL,
1109 							  iph->daddr, iph->saddr,
1110 							  0, 0,
1111 							  IPPROTO_IPV6,
1112 							  iph->tos & INET_DSCP_MASK,
1113 							  tunnel->parms.link);
1114 
1115 		if (!IS_ERR(rt)) {
1116 			tdev = rt->dst.dev;
1117 			ip_rt_put(rt);
1118 		}
1119 		dev->flags |= IFF_POINTOPOINT;
1120 	}
1121 
1122 	if (!tdev && tunnel->parms.link)
1123 		tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
1124 
1125 	if (tdev && !netif_is_l3_master(tdev)) {
1126 		int mtu;
1127 
1128 		mtu = tdev->mtu - t_hlen;
1129 		if (mtu < IPV6_MIN_MTU)
1130 			mtu = IPV6_MIN_MTU;
1131 		WRITE_ONCE(dev->mtu, mtu);
1132 		hlen = tdev->hard_header_len + tdev->needed_headroom;
1133 	}
1134 	dev->needed_headroom = t_hlen + hlen;
1135 }
1136 
1137 static void ipip6_tunnel_update(struct ip_tunnel *t,
1138 				struct ip_tunnel_parm_kern *p,
1139 				__u32 fwmark)
1140 {
1141 	struct net *net = t->net;
1142 	struct sit_net *sitn = net_generic(net, sit_net_id);
1143 
1144 	ipip6_tunnel_unlink(sitn, t);
1145 	synchronize_net();
1146 	t->parms.iph.saddr = p->iph.saddr;
1147 	t->parms.iph.daddr = p->iph.daddr;
1148 	__dev_addr_set(t->dev, &p->iph.saddr, 4);
1149 	memcpy(t->dev->broadcast, &p->iph.daddr, 4);
1150 	ipip6_tunnel_link(sitn, t);
1151 	t->parms.iph.ttl = p->iph.ttl;
1152 	t->parms.iph.tos = p->iph.tos;
1153 	t->parms.iph.frag_off = p->iph.frag_off;
1154 	if (t->parms.link != p->link || t->fwmark != fwmark) {
1155 		t->parms.link = p->link;
1156 		t->fwmark = fwmark;
1157 		ipip6_tunnel_bind_dev(t->dev);
1158 	}
1159 	dst_cache_reset(&t->dst_cache);
1160 	netdev_state_change(t->dev);
1161 }
1162 
1163 #ifdef CONFIG_IPV6_SIT_6RD
1164 static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
1165 				   struct ip_tunnel_6rd *ip6rd)
1166 {
1167 	struct in6_addr prefix;
1168 	__be32 relay_prefix;
1169 
1170 	if (ip6rd->relay_prefixlen > 32 ||
1171 	    ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
1172 		return -EINVAL;
1173 
1174 	ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
1175 	if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
1176 		return -EINVAL;
1177 	if (ip6rd->relay_prefixlen)
1178 		relay_prefix = ip6rd->relay_prefix &
1179 			       htonl(0xffffffffUL <<
1180 				     (32 - ip6rd->relay_prefixlen));
1181 	else
1182 		relay_prefix = 0;
1183 	if (relay_prefix != ip6rd->relay_prefix)
1184 		return -EINVAL;
1185 
1186 	t->ip6rd.prefix = prefix;
1187 	t->ip6rd.relay_prefix = relay_prefix;
1188 	t->ip6rd.prefixlen = ip6rd->prefixlen;
1189 	t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
1190 	dst_cache_reset(&t->dst_cache);
1191 	netdev_state_change(t->dev);
1192 	return 0;
1193 }
1194 
1195 static int
1196 ipip6_tunnel_get6rd(struct net_device *dev, struct ip_tunnel_parm __user *data)
1197 {
1198 	struct ip_tunnel *t = netdev_priv(dev);
1199 	struct ip_tunnel_parm_kern p;
1200 	struct ip_tunnel_6rd ip6rd;
1201 
1202 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
1203 		if (!ip_tunnel_parm_from_user(&p, data))
1204 			return -EFAULT;
1205 		t = ipip6_tunnel_locate(t->net, &p, 0);
1206 	}
1207 	if (!t)
1208 		t = netdev_priv(dev);
1209 
1210 	ip6rd.prefix = t->ip6rd.prefix;
1211 	ip6rd.relay_prefix = t->ip6rd.relay_prefix;
1212 	ip6rd.prefixlen = t->ip6rd.prefixlen;
1213 	ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
1214 	if (copy_to_user(data, &ip6rd, sizeof(ip6rd)))
1215 		return -EFAULT;
1216 	return 0;
1217 }
1218 
1219 static int
1220 ipip6_tunnel_6rdctl(struct net_device *dev, struct ip_tunnel_6rd __user *data,
1221 		    int cmd)
1222 {
1223 	struct ip_tunnel *t = netdev_priv(dev);
1224 	struct ip_tunnel_6rd ip6rd;
1225 	int err;
1226 
1227 	if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
1228 		return -EPERM;
1229 	if (copy_from_user(&ip6rd, data, sizeof(ip6rd)))
1230 		return -EFAULT;
1231 
1232 	if (cmd != SIOCDEL6RD) {
1233 		err = ipip6_tunnel_update_6rd(t, &ip6rd);
1234 		if (err < 0)
1235 			return err;
1236 	} else
1237 		ipip6_tunnel_clone_6rd(dev, dev_to_sit_net(dev));
1238 	return 0;
1239 }
1240 
1241 #endif /* CONFIG_IPV6_SIT_6RD */
1242 
1243 static bool ipip6_valid_ip_proto(u8 ipproto)
1244 {
1245 	return ipproto == IPPROTO_IPV6 ||
1246 		ipproto == IPPROTO_IPIP ||
1247 #if IS_ENABLED(CONFIG_MPLS)
1248 		ipproto == IPPROTO_MPLS ||
1249 #endif
1250 		ipproto == 0;
1251 }
1252 
1253 static int
1254 __ipip6_tunnel_ioctl_validate(struct net *net, struct ip_tunnel_parm_kern *p)
1255 {
1256 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1257 		return -EPERM;
1258 
1259 	if (!ipip6_valid_ip_proto(p->iph.protocol))
1260 		return -EINVAL;
1261 	if (p->iph.version != 4 ||
1262 	    p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)))
1263 		return -EINVAL;
1264 
1265 	if (p->iph.ttl)
1266 		p->iph.frag_off |= htons(IP_DF);
1267 	return 0;
1268 }
1269 
1270 static int
1271 ipip6_tunnel_get(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1272 {
1273 	struct ip_tunnel *t = netdev_priv(dev);
1274 
1275 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev)
1276 		t = ipip6_tunnel_locate(t->net, p, 0);
1277 	if (!t)
1278 		t = netdev_priv(dev);
1279 	memcpy(p, &t->parms, sizeof(*p));
1280 	return 0;
1281 }
1282 
1283 static int
1284 ipip6_tunnel_add(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1285 {
1286 	struct ip_tunnel *t = netdev_priv(dev);
1287 	int err;
1288 
1289 	err = __ipip6_tunnel_ioctl_validate(t->net, p);
1290 	if (err)
1291 		return err;
1292 
1293 	t = ipip6_tunnel_locate(t->net, p, 1);
1294 	if (!t)
1295 		return -ENOBUFS;
1296 	return 0;
1297 }
1298 
1299 static int
1300 ipip6_tunnel_change(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1301 {
1302 	struct ip_tunnel *t = netdev_priv(dev);
1303 	int err;
1304 
1305 	err = __ipip6_tunnel_ioctl_validate(t->net, p);
1306 	if (err)
1307 		return err;
1308 
1309 	t = ipip6_tunnel_locate(t->net, p, 0);
1310 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
1311 		if (!t)
1312 			return -ENOENT;
1313 	} else {
1314 		if (t) {
1315 			if (t->dev != dev)
1316 				return -EEXIST;
1317 		} else {
1318 			if (((dev->flags & IFF_POINTOPOINT) && !p->iph.daddr) ||
1319 			    (!(dev->flags & IFF_POINTOPOINT) && p->iph.daddr))
1320 				return -EINVAL;
1321 			t = netdev_priv(dev);
1322 		}
1323 
1324 		ipip6_tunnel_update(t, p, t->fwmark);
1325 	}
1326 
1327 	return 0;
1328 }
1329 
1330 static int
1331 ipip6_tunnel_del(struct net_device *dev, struct ip_tunnel_parm_kern *p)
1332 {
1333 	struct ip_tunnel *t = netdev_priv(dev);
1334 
1335 	if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN))
1336 		return -EPERM;
1337 
1338 	if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) {
1339 		t = ipip6_tunnel_locate(t->net, p, 0);
1340 		if (!t)
1341 			return -ENOENT;
1342 		if (t == netdev_priv(dev_to_sit_net(dev)->fb_tunnel_dev))
1343 			return -EPERM;
1344 		dev = t->dev;
1345 	}
1346 	unregister_netdevice(dev);
1347 	return 0;
1348 }
1349 
1350 static int
1351 ipip6_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p,
1352 		 int cmd)
1353 {
1354 	switch (cmd) {
1355 	case SIOCGETTUNNEL:
1356 		return ipip6_tunnel_get(dev, p);
1357 	case SIOCADDTUNNEL:
1358 		return ipip6_tunnel_add(dev, p);
1359 	case SIOCCHGTUNNEL:
1360 		return ipip6_tunnel_change(dev, p);
1361 	case SIOCDELTUNNEL:
1362 		return ipip6_tunnel_del(dev, p);
1363 	default:
1364 		return -EINVAL;
1365 	}
1366 }
1367 
1368 static int
1369 ipip6_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
1370 			    void __user *data, int cmd)
1371 {
1372 	switch (cmd) {
1373 	case SIOCGETTUNNEL:
1374 	case SIOCADDTUNNEL:
1375 	case SIOCCHGTUNNEL:
1376 	case SIOCDELTUNNEL:
1377 		return ip_tunnel_siocdevprivate(dev, ifr, data, cmd);
1378 	case SIOCGETPRL:
1379 		return ipip6_tunnel_get_prl(dev, data);
1380 	case SIOCADDPRL:
1381 	case SIOCDELPRL:
1382 	case SIOCCHGPRL:
1383 		return ipip6_tunnel_prl_ctl(dev, data, cmd);
1384 #ifdef CONFIG_IPV6_SIT_6RD
1385 	case SIOCGET6RD:
1386 		return ipip6_tunnel_get6rd(dev, data);
1387 	case SIOCADD6RD:
1388 	case SIOCCHG6RD:
1389 	case SIOCDEL6RD:
1390 		return ipip6_tunnel_6rdctl(dev, data, cmd);
1391 #endif
1392 	default:
1393 		return -EINVAL;
1394 	}
1395 }
1396 
1397 static const struct net_device_ops ipip6_netdev_ops = {
1398 	.ndo_init	= ipip6_tunnel_init,
1399 	.ndo_uninit	= ipip6_tunnel_uninit,
1400 	.ndo_start_xmit	= sit_tunnel_xmit,
1401 	.ndo_siocdevprivate = ipip6_tunnel_siocdevprivate,
1402 	.ndo_get_iflink = ip_tunnel_get_iflink,
1403 	.ndo_tunnel_ctl = ipip6_tunnel_ctl,
1404 };
1405 
1406 static void ipip6_dev_free(struct net_device *dev)
1407 {
1408 	struct ip_tunnel *tunnel = netdev_priv(dev);
1409 
1410 	dst_cache_destroy(&tunnel->dst_cache);
1411 }
1412 
1413 #define SIT_FEATURES (NETIF_F_SG	   | \
1414 		      NETIF_F_FRAGLIST	   | \
1415 		      NETIF_F_HIGHDMA	   | \
1416 		      NETIF_F_GSO_SOFTWARE | \
1417 		      NETIF_F_HW_CSUM)
1418 
1419 static void ipip6_tunnel_setup(struct net_device *dev)
1420 {
1421 	struct ip_tunnel *tunnel = netdev_priv(dev);
1422 	int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1423 
1424 	dev->netdev_ops		= &ipip6_netdev_ops;
1425 	dev->header_ops		= &ip_tunnel_header_ops;
1426 	dev->needs_free_netdev	= true;
1427 	dev->priv_destructor	= ipip6_dev_free;
1428 
1429 	dev->type		= ARPHRD_SIT;
1430 	dev->mtu		= ETH_DATA_LEN - t_hlen;
1431 	dev->min_mtu		= IPV6_MIN_MTU;
1432 	dev->max_mtu		= IP6_MAX_MTU - t_hlen;
1433 	dev->flags		= IFF_NOARP;
1434 	netif_keep_dst(dev);
1435 	dev->addr_len		= 4;
1436 	dev->lltx		= true;
1437 	dev->features		|= SIT_FEATURES;
1438 	dev->hw_features	|= SIT_FEATURES;
1439 	dev->pcpu_stat_type	= NETDEV_PCPU_STAT_TSTATS;
1440 
1441 }
1442 
1443 static int ipip6_tunnel_init(struct net_device *dev)
1444 {
1445 	struct ip_tunnel *tunnel = netdev_priv(dev);
1446 	int err;
1447 
1448 	tunnel->dev = dev;
1449 	strscpy(tunnel->parms.name, dev->name);
1450 
1451 	ipip6_tunnel_bind_dev(dev);
1452 
1453 	err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1454 	if (err)
1455 		return err;
1456 
1457 	netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1458 	netdev_lockdep_set_classes(dev);
1459 	return 0;
1460 }
1461 
1462 static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
1463 {
1464 	struct ip_tunnel *tunnel = netdev_priv(dev);
1465 	struct iphdr *iph = &tunnel->parms.iph;
1466 	struct net *net = dev_net(dev);
1467 	struct sit_net *sitn = net_generic(net, sit_net_id);
1468 
1469 	iph->version		= 4;
1470 	iph->protocol		= IPPROTO_IPV6;
1471 	iph->ihl		= 5;
1472 	iph->ttl		= 64;
1473 
1474 	rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
1475 }
1476 
1477 static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
1478 			  struct netlink_ext_ack *extack)
1479 {
1480 	u8 proto;
1481 
1482 	if (!data || !data[IFLA_IPTUN_PROTO])
1483 		return 0;
1484 
1485 	proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1486 	if (!ipip6_valid_ip_proto(proto))
1487 		return -EINVAL;
1488 
1489 	return 0;
1490 }
1491 
1492 static void ipip6_netlink_parms(struct nlattr *data[],
1493 				struct ip_tunnel_parm_kern *parms,
1494 				__u32 *fwmark)
1495 {
1496 	memset(parms, 0, sizeof(*parms));
1497 
1498 	parms->iph.version = 4;
1499 	parms->iph.protocol = IPPROTO_IPV6;
1500 	parms->iph.ihl = 5;
1501 	parms->iph.ttl = 64;
1502 
1503 	if (!data)
1504 		return;
1505 
1506 	ip_tunnel_netlink_parms(data, parms);
1507 
1508 	if (data[IFLA_IPTUN_FWMARK])
1509 		*fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
1510 }
1511 
1512 #ifdef CONFIG_IPV6_SIT_6RD
1513 /* This function returns true when 6RD attributes are present in the nl msg */
1514 static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
1515 				    struct ip_tunnel_6rd *ip6rd)
1516 {
1517 	bool ret = false;
1518 	memset(ip6rd, 0, sizeof(*ip6rd));
1519 
1520 	if (!data)
1521 		return ret;
1522 
1523 	if (data[IFLA_IPTUN_6RD_PREFIX]) {
1524 		ret = true;
1525 		ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
1526 	}
1527 
1528 	if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
1529 		ret = true;
1530 		ip6rd->relay_prefix =
1531 			nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
1532 	}
1533 
1534 	if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
1535 		ret = true;
1536 		ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
1537 	}
1538 
1539 	if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
1540 		ret = true;
1541 		ip6rd->relay_prefixlen =
1542 			nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
1543 	}
1544 
1545 	return ret;
1546 }
1547 #endif
1548 
1549 static int ipip6_newlink(struct net_device *dev,
1550 			 struct rtnl_newlink_params *params,
1551 			 struct netlink_ext_ack *extack)
1552 {
1553 	struct nlattr **data = params->data;
1554 	struct nlattr **tb = params->tb;
1555 	struct ip_tunnel *nt;
1556 	struct ip_tunnel_encap ipencap;
1557 #ifdef CONFIG_IPV6_SIT_6RD
1558 	struct ip_tunnel_6rd ip6rd;
1559 #endif
1560 	struct net *net;
1561 	int err;
1562 
1563 	net = params->link_net ? : dev_net(dev);
1564 	nt = netdev_priv(dev);
1565 	nt->net = net;
1566 
1567 	if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
1568 		err = ip_tunnel_encap_setup(nt, &ipencap);
1569 		if (err < 0)
1570 			return err;
1571 	}
1572 
1573 	ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
1574 
1575 	if (ipip6_tunnel_locate(net, &nt->parms, 0))
1576 		return -EEXIST;
1577 
1578 	err = ipip6_tunnel_create(dev);
1579 	if (err < 0)
1580 		return err;
1581 
1582 	if (tb[IFLA_MTU]) {
1583 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
1584 
1585 		if (mtu >= IPV6_MIN_MTU &&
1586 		    mtu <= IP6_MAX_MTU - dev->hard_header_len)
1587 			dev->mtu = mtu;
1588 	}
1589 
1590 #ifdef CONFIG_IPV6_SIT_6RD
1591 	if (ipip6_netlink_6rd_parms(data, &ip6rd)) {
1592 		err = ipip6_tunnel_update_6rd(nt, &ip6rd);
1593 		if (err < 0)
1594 			unregister_netdevice_queue(dev, NULL);
1595 	}
1596 #endif
1597 
1598 	return err;
1599 }
1600 
1601 static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
1602 			    struct nlattr *data[],
1603 			    struct netlink_ext_ack *extack)
1604 {
1605 	struct ip_tunnel *t = netdev_priv(dev);
1606 	struct ip_tunnel_encap ipencap;
1607 	struct ip_tunnel_parm_kern p;
1608 	struct net *net = t->net;
1609 	struct sit_net *sitn = net_generic(net, sit_net_id);
1610 #ifdef CONFIG_IPV6_SIT_6RD
1611 	struct ip_tunnel_6rd ip6rd;
1612 #endif
1613 	__u32 fwmark = t->fwmark;
1614 	int err;
1615 
1616 	if (dev == sitn->fb_tunnel_dev)
1617 		return -EINVAL;
1618 
1619 	if (ip_tunnel_netlink_encap_parms(data, &ipencap)) {
1620 		err = ip_tunnel_encap_setup(t, &ipencap);
1621 		if (err < 0)
1622 			return err;
1623 	}
1624 
1625 	ipip6_netlink_parms(data, &p, &fwmark);
1626 
1627 	if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
1628 	    (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
1629 		return -EINVAL;
1630 
1631 	t = ipip6_tunnel_locate(net, &p, 0);
1632 
1633 	if (t) {
1634 		if (t->dev != dev)
1635 			return -EEXIST;
1636 	} else
1637 		t = netdev_priv(dev);
1638 
1639 	ipip6_tunnel_update(t, &p, fwmark);
1640 
1641 #ifdef CONFIG_IPV6_SIT_6RD
1642 	if (ipip6_netlink_6rd_parms(data, &ip6rd))
1643 		return ipip6_tunnel_update_6rd(t, &ip6rd);
1644 #endif
1645 
1646 	return 0;
1647 }
1648 
1649 static size_t ipip6_get_size(const struct net_device *dev)
1650 {
1651 	return
1652 		/* IFLA_IPTUN_LINK */
1653 		nla_total_size(4) +
1654 		/* IFLA_IPTUN_LOCAL */
1655 		nla_total_size(4) +
1656 		/* IFLA_IPTUN_REMOTE */
1657 		nla_total_size(4) +
1658 		/* IFLA_IPTUN_TTL */
1659 		nla_total_size(1) +
1660 		/* IFLA_IPTUN_TOS */
1661 		nla_total_size(1) +
1662 		/* IFLA_IPTUN_PMTUDISC */
1663 		nla_total_size(1) +
1664 		/* IFLA_IPTUN_FLAGS */
1665 		nla_total_size(2) +
1666 		/* IFLA_IPTUN_PROTO */
1667 		nla_total_size(1) +
1668 #ifdef CONFIG_IPV6_SIT_6RD
1669 		/* IFLA_IPTUN_6RD_PREFIX */
1670 		nla_total_size(sizeof(struct in6_addr)) +
1671 		/* IFLA_IPTUN_6RD_RELAY_PREFIX */
1672 		nla_total_size(4) +
1673 		/* IFLA_IPTUN_6RD_PREFIXLEN */
1674 		nla_total_size(2) +
1675 		/* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
1676 		nla_total_size(2) +
1677 #endif
1678 		/* IFLA_IPTUN_ENCAP_TYPE */
1679 		nla_total_size(2) +
1680 		/* IFLA_IPTUN_ENCAP_FLAGS */
1681 		nla_total_size(2) +
1682 		/* IFLA_IPTUN_ENCAP_SPORT */
1683 		nla_total_size(2) +
1684 		/* IFLA_IPTUN_ENCAP_DPORT */
1685 		nla_total_size(2) +
1686 		/* IFLA_IPTUN_FWMARK */
1687 		nla_total_size(4) +
1688 		0;
1689 }
1690 
1691 static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1692 {
1693 	struct ip_tunnel *tunnel = netdev_priv(dev);
1694 	struct ip_tunnel_parm_kern *parm = &tunnel->parms;
1695 
1696 	if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1697 	    nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
1698 	    nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
1699 	    nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
1700 	    nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
1701 	    nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
1702 		       !!(parm->iph.frag_off & htons(IP_DF))) ||
1703 	    nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
1704 	    nla_put_be16(skb, IFLA_IPTUN_FLAGS,
1705 			 ip_tunnel_flags_to_be16(parm->i_flags)) ||
1706 	    nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
1707 		goto nla_put_failure;
1708 
1709 #ifdef CONFIG_IPV6_SIT_6RD
1710 	if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
1711 			     &tunnel->ip6rd.prefix) ||
1712 	    nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
1713 			    tunnel->ip6rd.relay_prefix) ||
1714 	    nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
1715 			tunnel->ip6rd.prefixlen) ||
1716 	    nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
1717 			tunnel->ip6rd.relay_prefixlen))
1718 		goto nla_put_failure;
1719 #endif
1720 
1721 	if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
1722 			tunnel->encap.type) ||
1723 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
1724 			tunnel->encap.sport) ||
1725 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
1726 			tunnel->encap.dport) ||
1727 	    nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
1728 			tunnel->encap.flags))
1729 		goto nla_put_failure;
1730 
1731 	return 0;
1732 
1733 nla_put_failure:
1734 	return -EMSGSIZE;
1735 }
1736 
1737 static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
1738 	[IFLA_IPTUN_LINK]		= { .type = NLA_U32 },
1739 	[IFLA_IPTUN_LOCAL]		= { .type = NLA_U32 },
1740 	[IFLA_IPTUN_REMOTE]		= { .type = NLA_U32 },
1741 	[IFLA_IPTUN_TTL]		= { .type = NLA_U8 },
1742 	[IFLA_IPTUN_TOS]		= { .type = NLA_U8 },
1743 	[IFLA_IPTUN_PMTUDISC]		= { .type = NLA_U8 },
1744 	[IFLA_IPTUN_FLAGS]		= { .type = NLA_U16 },
1745 	[IFLA_IPTUN_PROTO]		= { .type = NLA_U8 },
1746 #ifdef CONFIG_IPV6_SIT_6RD
1747 	[IFLA_IPTUN_6RD_PREFIX]		= { .len = sizeof(struct in6_addr) },
1748 	[IFLA_IPTUN_6RD_RELAY_PREFIX]	= { .type = NLA_U32 },
1749 	[IFLA_IPTUN_6RD_PREFIXLEN]	= { .type = NLA_U16 },
1750 	[IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
1751 #endif
1752 	[IFLA_IPTUN_ENCAP_TYPE]		= { .type = NLA_U16 },
1753 	[IFLA_IPTUN_ENCAP_FLAGS]	= { .type = NLA_U16 },
1754 	[IFLA_IPTUN_ENCAP_SPORT]	= { .type = NLA_U16 },
1755 	[IFLA_IPTUN_ENCAP_DPORT]	= { .type = NLA_U16 },
1756 	[IFLA_IPTUN_FWMARK]		= { .type = NLA_U32 },
1757 };
1758 
1759 static void ipip6_dellink(struct net_device *dev, struct list_head *head)
1760 {
1761 	struct net *net = dev_net(dev);
1762 	struct sit_net *sitn = net_generic(net, sit_net_id);
1763 
1764 	if (dev != sitn->fb_tunnel_dev)
1765 		unregister_netdevice_queue(dev, head);
1766 }
1767 
1768 static struct rtnl_link_ops sit_link_ops __read_mostly = {
1769 	.kind		= "sit",
1770 	.maxtype	= IFLA_IPTUN_MAX,
1771 	.policy		= ipip6_policy,
1772 	.priv_size	= sizeof(struct ip_tunnel),
1773 	.setup		= ipip6_tunnel_setup,
1774 	.validate	= ipip6_validate,
1775 	.newlink	= ipip6_newlink,
1776 	.changelink	= ipip6_changelink,
1777 	.get_size	= ipip6_get_size,
1778 	.fill_info	= ipip6_fill_info,
1779 	.dellink	= ipip6_dellink,
1780 	.get_link_net	= ip_tunnel_get_link_net,
1781 };
1782 
1783 static struct xfrm_tunnel sit_handler __read_mostly = {
1784 	.handler	=	ipip6_rcv,
1785 	.err_handler	=	ipip6_err,
1786 	.priority	=	1,
1787 };
1788 
1789 static struct xfrm_tunnel ipip_handler __read_mostly = {
1790 	.handler	=	ipip_rcv,
1791 	.err_handler	=	ipip6_err,
1792 	.priority	=	2,
1793 };
1794 
1795 #if IS_ENABLED(CONFIG_MPLS)
1796 static struct xfrm_tunnel mplsip_handler __read_mostly = {
1797 	.handler	=	mplsip_rcv,
1798 	.err_handler	=	ipip6_err,
1799 	.priority	=	2,
1800 };
1801 #endif
1802 
1803 static void __net_exit sit_exit_rtnl_net(struct net *net, struct list_head *head)
1804 {
1805 	struct sit_net *sitn = net_generic(net, sit_net_id);
1806 	struct net_device *dev, *aux;
1807 	int prio;
1808 
1809 	for_each_netdev_safe(net, dev, aux)
1810 		if (dev->rtnl_link_ops == &sit_link_ops)
1811 			unregister_netdevice_queue(dev, head);
1812 
1813 	for (prio = 0; prio < 4; prio++) {
1814 		int h;
1815 		for (h = 0; h < (prio ? IP6_SIT_HASH_SIZE : 1); h++) {
1816 			struct ip_tunnel *t;
1817 
1818 			t = rtnl_net_dereference(net, sitn->tunnels[prio][h]);
1819 			while (t) {
1820 				/* If dev is in the same netns, it has already
1821 				 * been added to the list by the previous loop.
1822 				 */
1823 				if (!net_eq(dev_net(t->dev), net))
1824 					unregister_netdevice_queue(t->dev, head);
1825 
1826 				t = rtnl_net_dereference(net, t->next);
1827 			}
1828 		}
1829 	}
1830 }
1831 
1832 static int __net_init sit_init_net(struct net *net)
1833 {
1834 	struct sit_net *sitn = net_generic(net, sit_net_id);
1835 	struct ip_tunnel *t;
1836 	int err;
1837 
1838 	sitn->tunnels[0] = sitn->tunnels_wc;
1839 	sitn->tunnels[1] = sitn->tunnels_l;
1840 	sitn->tunnels[2] = sitn->tunnels_r;
1841 	sitn->tunnels[3] = sitn->tunnels_r_l;
1842 
1843 	if (!net_has_fallback_tunnels(net))
1844 		return 0;
1845 
1846 	sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1847 					   NET_NAME_UNKNOWN,
1848 					   ipip6_tunnel_setup);
1849 	if (!sitn->fb_tunnel_dev) {
1850 		err = -ENOMEM;
1851 		goto err_alloc_dev;
1852 	}
1853 	dev_net_set(sitn->fb_tunnel_dev, net);
1854 	sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
1855 	/* FB netdevice is special: we have one, and only one per netns.
1856 	 * Allowing to move it to another netns is clearly unsafe.
1857 	 */
1858 	sitn->fb_tunnel_dev->netns_immutable = true;
1859 
1860 	t = netdev_priv(sitn->fb_tunnel_dev);
1861 	t->net = net;
1862 
1863 	err = register_netdev(sitn->fb_tunnel_dev);
1864 	if (err)
1865 		goto err_reg_dev;
1866 
1867 	ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
1868 	ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
1869 
1870 	strscpy(t->parms.name, sitn->fb_tunnel_dev->name);
1871 	return 0;
1872 
1873 err_reg_dev:
1874 	free_netdev(sitn->fb_tunnel_dev);
1875 err_alloc_dev:
1876 	return err;
1877 }
1878 
1879 static struct pernet_operations sit_net_ops = {
1880 	.init = sit_init_net,
1881 	.exit_rtnl = sit_exit_rtnl_net,
1882 	.id   = &sit_net_id,
1883 	.size = sizeof(struct sit_net),
1884 };
1885 
1886 static void __exit sit_cleanup(void)
1887 {
1888 	rtnl_link_unregister(&sit_link_ops);
1889 	xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1890 	xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
1891 #if IS_ENABLED(CONFIG_MPLS)
1892 	xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
1893 #endif
1894 
1895 	unregister_pernet_device(&sit_net_ops);
1896 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
1897 }
1898 
1899 static int __init sit_init(void)
1900 {
1901 	int err;
1902 
1903 	pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n");
1904 
1905 	err = register_pernet_device(&sit_net_ops);
1906 	if (err < 0)
1907 		return err;
1908 	err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
1909 	if (err < 0) {
1910 		pr_info("%s: can't register ip6ip4\n", __func__);
1911 		goto xfrm_tunnel_failed;
1912 	}
1913 	err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
1914 	if (err < 0) {
1915 		pr_info("%s: can't register ip4ip4\n", __func__);
1916 		goto xfrm_tunnel4_failed;
1917 	}
1918 #if IS_ENABLED(CONFIG_MPLS)
1919 	err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
1920 	if (err < 0) {
1921 		pr_info("%s: can't register mplsip\n", __func__);
1922 		goto xfrm_tunnel_mpls_failed;
1923 	}
1924 #endif
1925 	err = rtnl_link_register(&sit_link_ops);
1926 	if (err < 0)
1927 		goto rtnl_link_failed;
1928 
1929 out:
1930 	return err;
1931 
1932 rtnl_link_failed:
1933 #if IS_ENABLED(CONFIG_MPLS)
1934 	xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
1935 xfrm_tunnel_mpls_failed:
1936 #endif
1937 	xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
1938 xfrm_tunnel4_failed:
1939 	xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1940 xfrm_tunnel_failed:
1941 	unregister_pernet_device(&sit_net_ops);
1942 	goto out;
1943 }
1944 
1945 module_init(sit_init);
1946 module_exit(sit_cleanup);
1947 MODULE_DESCRIPTION("IPv6-in-IPv4 tunnel SIT driver");
1948 MODULE_LICENSE("GPL");
1949 MODULE_ALIAS_RTNL_LINK("sit");
1950 MODULE_ALIAS_NETDEV("sit0");
1951