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