xref: /openbmc/linux/net/ipv6/ip6_tunnel.c (revision 62975d27)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPv6 tunneling device
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
8  *	Yasuyuki Kozakai	<kozakai@linux-ipv6.org>
9  *
10  *      Based on:
11  *      linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
12  *
13  *      RFC 2473
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/sockios.h>
23 #include <linux/icmp.h>
24 #include <linux/if.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/net.h>
28 #include <linux/in6.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/init.h>
33 #include <linux/route.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/netfilter_ipv6.h>
36 #include <linux/slab.h>
37 #include <linux/hash.h>
38 #include <linux/etherdevice.h>
39 
40 #include <linux/uaccess.h>
41 #include <linux/atomic.h>
42 
43 #include <net/icmp.h>
44 #include <net/ip.h>
45 #include <net/ip_tunnels.h>
46 #include <net/ipv6.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/xfrm.h>
51 #include <net/dsfield.h>
52 #include <net/inet_ecn.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
55 #include <net/dst_metadata.h>
56 
57 MODULE_AUTHOR("Ville Nuorvala");
58 MODULE_DESCRIPTION("IPv6 tunneling device");
59 MODULE_LICENSE("GPL");
60 MODULE_ALIAS_RTNL_LINK("ip6tnl");
61 MODULE_ALIAS_NETDEV("ip6tnl0");
62 
63 #define IP6_TUNNEL_HASH_SIZE_SHIFT  5
64 #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
65 
66 static bool log_ecn_error = true;
67 module_param(log_ecn_error, bool, 0644);
68 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
69 
70 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
71 {
72 	u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
73 
74 	return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
75 }
76 
77 static int ip6_tnl_dev_init(struct net_device *dev);
78 static void ip6_tnl_dev_setup(struct net_device *dev);
79 static struct rtnl_link_ops ip6_link_ops __read_mostly;
80 
81 static unsigned int ip6_tnl_net_id __read_mostly;
82 struct ip6_tnl_net {
83 	/* the IPv6 tunnel fallback device */
84 	struct net_device *fb_tnl_dev;
85 	/* lists for storing tunnels in use */
86 	struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
87 	struct ip6_tnl __rcu *tnls_wc[1];
88 	struct ip6_tnl __rcu **tnls[2];
89 	struct ip6_tnl __rcu *collect_md_tun;
90 };
91 
92 static inline int ip6_tnl_mpls_supported(void)
93 {
94 	return IS_ENABLED(CONFIG_MPLS);
95 }
96 
97 static struct net_device_stats *ip6_get_stats(struct net_device *dev)
98 {
99 	struct pcpu_sw_netstats tmp, sum = { 0 };
100 	int i;
101 
102 	for_each_possible_cpu(i) {
103 		unsigned int start;
104 		const struct pcpu_sw_netstats *tstats =
105 						   per_cpu_ptr(dev->tstats, i);
106 
107 		do {
108 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
109 			tmp.rx_packets = tstats->rx_packets;
110 			tmp.rx_bytes = tstats->rx_bytes;
111 			tmp.tx_packets = tstats->tx_packets;
112 			tmp.tx_bytes =  tstats->tx_bytes;
113 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
114 
115 		sum.rx_packets += tmp.rx_packets;
116 		sum.rx_bytes   += tmp.rx_bytes;
117 		sum.tx_packets += tmp.tx_packets;
118 		sum.tx_bytes   += tmp.tx_bytes;
119 	}
120 	dev->stats.rx_packets = sum.rx_packets;
121 	dev->stats.rx_bytes   = sum.rx_bytes;
122 	dev->stats.tx_packets = sum.tx_packets;
123 	dev->stats.tx_bytes   = sum.tx_bytes;
124 	return &dev->stats;
125 }
126 
127 /**
128  * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
129  *   @link: ifindex of underlying interface
130  *   @remote: the address of the tunnel exit-point
131  *   @local: the address of the tunnel entry-point
132  *
133  * Return:
134  *   tunnel matching given end-points if found,
135  *   else fallback tunnel if its device is up,
136  *   else %NULL
137  **/
138 
139 #define for_each_ip6_tunnel_rcu(start) \
140 	for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
141 
142 static struct ip6_tnl *
143 ip6_tnl_lookup(struct net *net, int link,
144 	       const struct in6_addr *remote, const struct in6_addr *local)
145 {
146 	unsigned int hash = HASH(remote, local);
147 	struct ip6_tnl *t, *cand = NULL;
148 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
149 	struct in6_addr any;
150 
151 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
152 		if (!ipv6_addr_equal(local, &t->parms.laddr) ||
153 		    !ipv6_addr_equal(remote, &t->parms.raddr) ||
154 		    !(t->dev->flags & IFF_UP))
155 			continue;
156 
157 		if (link == t->parms.link)
158 			return t;
159 		else
160 			cand = t;
161 	}
162 
163 	memset(&any, 0, sizeof(any));
164 	hash = HASH(&any, local);
165 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
166 		if (!ipv6_addr_equal(local, &t->parms.laddr) ||
167 		    !ipv6_addr_any(&t->parms.raddr) ||
168 		    !(t->dev->flags & IFF_UP))
169 			continue;
170 
171 		if (link == t->parms.link)
172 			return t;
173 		else if (!cand)
174 			cand = t;
175 	}
176 
177 	hash = HASH(remote, &any);
178 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
179 		if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
180 		    !ipv6_addr_any(&t->parms.laddr) ||
181 		    !(t->dev->flags & IFF_UP))
182 			continue;
183 
184 		if (link == t->parms.link)
185 			return t;
186 		else if (!cand)
187 			cand = t;
188 	}
189 
190 	if (cand)
191 		return cand;
192 
193 	t = rcu_dereference(ip6n->collect_md_tun);
194 	if (t && t->dev->flags & IFF_UP)
195 		return t;
196 
197 	t = rcu_dereference(ip6n->tnls_wc[0]);
198 	if (t && (t->dev->flags & IFF_UP))
199 		return t;
200 
201 	return NULL;
202 }
203 
204 /**
205  * ip6_tnl_bucket - get head of list matching given tunnel parameters
206  *   @p: parameters containing tunnel end-points
207  *
208  * Description:
209  *   ip6_tnl_bucket() returns the head of the list matching the
210  *   &struct in6_addr entries laddr and raddr in @p.
211  *
212  * Return: head of IPv6 tunnel list
213  **/
214 
215 static struct ip6_tnl __rcu **
216 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
217 {
218 	const struct in6_addr *remote = &p->raddr;
219 	const struct in6_addr *local = &p->laddr;
220 	unsigned int h = 0;
221 	int prio = 0;
222 
223 	if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
224 		prio = 1;
225 		h = HASH(remote, local);
226 	}
227 	return &ip6n->tnls[prio][h];
228 }
229 
230 /**
231  * ip6_tnl_link - add tunnel to hash table
232  *   @t: tunnel to be added
233  **/
234 
235 static void
236 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
237 {
238 	struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
239 
240 	if (t->parms.collect_md)
241 		rcu_assign_pointer(ip6n->collect_md_tun, t);
242 	rcu_assign_pointer(t->next , rtnl_dereference(*tp));
243 	rcu_assign_pointer(*tp, t);
244 }
245 
246 /**
247  * ip6_tnl_unlink - remove tunnel from hash table
248  *   @t: tunnel to be removed
249  **/
250 
251 static void
252 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
253 {
254 	struct ip6_tnl __rcu **tp;
255 	struct ip6_tnl *iter;
256 
257 	if (t->parms.collect_md)
258 		rcu_assign_pointer(ip6n->collect_md_tun, NULL);
259 
260 	for (tp = ip6_tnl_bucket(ip6n, &t->parms);
261 	     (iter = rtnl_dereference(*tp)) != NULL;
262 	     tp = &iter->next) {
263 		if (t == iter) {
264 			rcu_assign_pointer(*tp, t->next);
265 			break;
266 		}
267 	}
268 }
269 
270 static void ip6_dev_free(struct net_device *dev)
271 {
272 	struct ip6_tnl *t = netdev_priv(dev);
273 
274 	gro_cells_destroy(&t->gro_cells);
275 	dst_cache_destroy(&t->dst_cache);
276 	free_percpu(dev->tstats);
277 }
278 
279 static int ip6_tnl_create2(struct net_device *dev)
280 {
281 	struct ip6_tnl *t = netdev_priv(dev);
282 	struct net *net = dev_net(dev);
283 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
284 	int err;
285 
286 	t = netdev_priv(dev);
287 
288 	dev->rtnl_link_ops = &ip6_link_ops;
289 	err = register_netdevice(dev);
290 	if (err < 0)
291 		goto out;
292 
293 	strcpy(t->parms.name, dev->name);
294 
295 	dev_hold(dev);
296 	ip6_tnl_link(ip6n, t);
297 	return 0;
298 
299 out:
300 	return err;
301 }
302 
303 /**
304  * ip6_tnl_create - create a new tunnel
305  *   @p: tunnel parameters
306  *   @pt: pointer to new tunnel
307  *
308  * Description:
309  *   Create tunnel matching given parameters.
310  *
311  * Return:
312  *   created tunnel or error pointer
313  **/
314 
315 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
316 {
317 	struct net_device *dev;
318 	struct ip6_tnl *t;
319 	char name[IFNAMSIZ];
320 	int err = -E2BIG;
321 
322 	if (p->name[0]) {
323 		if (!dev_valid_name(p->name))
324 			goto failed;
325 		strlcpy(name, p->name, IFNAMSIZ);
326 	} else {
327 		sprintf(name, "ip6tnl%%d");
328 	}
329 	err = -ENOMEM;
330 	dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
331 			   ip6_tnl_dev_setup);
332 	if (!dev)
333 		goto failed;
334 
335 	dev_net_set(dev, net);
336 
337 	t = netdev_priv(dev);
338 	t->parms = *p;
339 	t->net = dev_net(dev);
340 	err = ip6_tnl_create2(dev);
341 	if (err < 0)
342 		goto failed_free;
343 
344 	return t;
345 
346 failed_free:
347 	free_netdev(dev);
348 failed:
349 	return ERR_PTR(err);
350 }
351 
352 /**
353  * ip6_tnl_locate - find or create tunnel matching given parameters
354  *   @p: tunnel parameters
355  *   @create: != 0 if allowed to create new tunnel if no match found
356  *
357  * Description:
358  *   ip6_tnl_locate() first tries to locate an existing tunnel
359  *   based on @parms. If this is unsuccessful, but @create is set a new
360  *   tunnel device is created and registered for use.
361  *
362  * Return:
363  *   matching tunnel or error pointer
364  **/
365 
366 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
367 		struct __ip6_tnl_parm *p, int create)
368 {
369 	const struct in6_addr *remote = &p->raddr;
370 	const struct in6_addr *local = &p->laddr;
371 	struct ip6_tnl __rcu **tp;
372 	struct ip6_tnl *t;
373 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
374 
375 	for (tp = ip6_tnl_bucket(ip6n, p);
376 	     (t = rtnl_dereference(*tp)) != NULL;
377 	     tp = &t->next) {
378 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
379 		    ipv6_addr_equal(remote, &t->parms.raddr) &&
380 		    p->link == t->parms.link) {
381 			if (create)
382 				return ERR_PTR(-EEXIST);
383 
384 			return t;
385 		}
386 	}
387 	if (!create)
388 		return ERR_PTR(-ENODEV);
389 	return ip6_tnl_create(net, p);
390 }
391 
392 /**
393  * ip6_tnl_dev_uninit - tunnel device uninitializer
394  *   @dev: the device to be destroyed
395  *
396  * Description:
397  *   ip6_tnl_dev_uninit() removes tunnel from its list
398  **/
399 
400 static void
401 ip6_tnl_dev_uninit(struct net_device *dev)
402 {
403 	struct ip6_tnl *t = netdev_priv(dev);
404 	struct net *net = t->net;
405 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
406 
407 	if (dev == ip6n->fb_tnl_dev)
408 		RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
409 	else
410 		ip6_tnl_unlink(ip6n, t);
411 	dst_cache_reset(&t->dst_cache);
412 	dev_put(dev);
413 }
414 
415 /**
416  * parse_tvl_tnl_enc_lim - handle encapsulation limit option
417  *   @skb: received socket buffer
418  *
419  * Return:
420  *   0 if none was found,
421  *   else index to encapsulation limit
422  **/
423 
424 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
425 {
426 	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
427 	unsigned int nhoff = raw - skb->data;
428 	unsigned int off = nhoff + sizeof(*ipv6h);
429 	u8 next, nexthdr = ipv6h->nexthdr;
430 
431 	while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
432 		struct ipv6_opt_hdr *hdr;
433 		u16 optlen;
434 
435 		if (!pskb_may_pull(skb, off + sizeof(*hdr)))
436 			break;
437 
438 		hdr = (struct ipv6_opt_hdr *)(skb->data + off);
439 		if (nexthdr == NEXTHDR_FRAGMENT) {
440 			struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
441 			if (frag_hdr->frag_off)
442 				break;
443 			optlen = 8;
444 		} else if (nexthdr == NEXTHDR_AUTH) {
445 			optlen = ipv6_authlen(hdr);
446 		} else {
447 			optlen = ipv6_optlen(hdr);
448 		}
449 		/* cache hdr->nexthdr, since pskb_may_pull() might
450 		 * invalidate hdr
451 		 */
452 		next = hdr->nexthdr;
453 		if (nexthdr == NEXTHDR_DEST) {
454 			u16 i = 2;
455 
456 			/* Remember : hdr is no longer valid at this point. */
457 			if (!pskb_may_pull(skb, off + optlen))
458 				break;
459 
460 			while (1) {
461 				struct ipv6_tlv_tnl_enc_lim *tel;
462 
463 				/* No more room for encapsulation limit */
464 				if (i + sizeof(*tel) > optlen)
465 					break;
466 
467 				tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
468 				/* return index of option if found and valid */
469 				if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
470 				    tel->length == 1)
471 					return i + off - nhoff;
472 				/* else jump to next option */
473 				if (tel->type)
474 					i += tel->length + 2;
475 				else
476 					i++;
477 			}
478 		}
479 		nexthdr = next;
480 		off += optlen;
481 	}
482 	return 0;
483 }
484 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
485 
486 /**
487  * ip6_tnl_err - tunnel error handler
488  *
489  * Description:
490  *   ip6_tnl_err() should handle errors in the tunnel according
491  *   to the specifications in RFC 2473.
492  **/
493 
494 static int
495 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
496 	    u8 *type, u8 *code, int *msg, __u32 *info, int offset)
497 {
498 	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
499 	struct net *net = dev_net(skb->dev);
500 	u8 rel_type = ICMPV6_DEST_UNREACH;
501 	u8 rel_code = ICMPV6_ADDR_UNREACH;
502 	__u32 rel_info = 0;
503 	struct ip6_tnl *t;
504 	int err = -ENOENT;
505 	int rel_msg = 0;
506 	u8 tproto;
507 	__u16 len;
508 
509 	/* If the packet doesn't contain the original IPv6 header we are
510 	   in trouble since we might need the source address for further
511 	   processing of the error. */
512 
513 	rcu_read_lock();
514 	t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->daddr, &ipv6h->saddr);
515 	if (!t)
516 		goto out;
517 
518 	tproto = READ_ONCE(t->parms.proto);
519 	if (tproto != ipproto && tproto != 0)
520 		goto out;
521 
522 	err = 0;
523 
524 	switch (*type) {
525 	case ICMPV6_DEST_UNREACH:
526 		net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
527 				    t->parms.name);
528 		rel_msg = 1;
529 		break;
530 	case ICMPV6_TIME_EXCEED:
531 		if ((*code) == ICMPV6_EXC_HOPLIMIT) {
532 			net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
533 					    t->parms.name);
534 			rel_msg = 1;
535 		}
536 		break;
537 	case ICMPV6_PARAMPROB: {
538 		struct ipv6_tlv_tnl_enc_lim *tel;
539 		__u32 teli;
540 
541 		teli = 0;
542 		if ((*code) == ICMPV6_HDR_FIELD)
543 			teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
544 
545 		if (teli && teli == *info - 2) {
546 			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
547 			if (tel->encap_limit == 0) {
548 				net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
549 						    t->parms.name);
550 				rel_msg = 1;
551 			}
552 		} else {
553 			net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
554 					    t->parms.name);
555 		}
556 		break;
557 	}
558 	case ICMPV6_PKT_TOOBIG: {
559 		__u32 mtu;
560 
561 		ip6_update_pmtu(skb, net, htonl(*info), 0, 0,
562 				sock_net_uid(net, NULL));
563 		mtu = *info - offset;
564 		if (mtu < IPV6_MIN_MTU)
565 			mtu = IPV6_MIN_MTU;
566 		len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
567 		if (len > mtu) {
568 			rel_type = ICMPV6_PKT_TOOBIG;
569 			rel_code = 0;
570 			rel_info = mtu;
571 			rel_msg = 1;
572 		}
573 		break;
574 	}
575 	case NDISC_REDIRECT:
576 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
577 			     sock_net_uid(net, NULL));
578 		break;
579 	}
580 
581 	*type = rel_type;
582 	*code = rel_code;
583 	*info = rel_info;
584 	*msg = rel_msg;
585 
586 out:
587 	rcu_read_unlock();
588 	return err;
589 }
590 
591 static int
592 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
593 	   u8 type, u8 code, int offset, __be32 info)
594 {
595 	__u32 rel_info = ntohl(info);
596 	const struct iphdr *eiph;
597 	struct sk_buff *skb2;
598 	int err, rel_msg = 0;
599 	u8 rel_type = type;
600 	u8 rel_code = code;
601 	struct rtable *rt;
602 	struct flowi4 fl4;
603 
604 	err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
605 			  &rel_msg, &rel_info, offset);
606 	if (err < 0)
607 		return err;
608 
609 	if (rel_msg == 0)
610 		return 0;
611 
612 	switch (rel_type) {
613 	case ICMPV6_DEST_UNREACH:
614 		if (rel_code != ICMPV6_ADDR_UNREACH)
615 			return 0;
616 		rel_type = ICMP_DEST_UNREACH;
617 		rel_code = ICMP_HOST_UNREACH;
618 		break;
619 	case ICMPV6_PKT_TOOBIG:
620 		if (rel_code != 0)
621 			return 0;
622 		rel_type = ICMP_DEST_UNREACH;
623 		rel_code = ICMP_FRAG_NEEDED;
624 		break;
625 	default:
626 		return 0;
627 	}
628 
629 	if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
630 		return 0;
631 
632 	skb2 = skb_clone(skb, GFP_ATOMIC);
633 	if (!skb2)
634 		return 0;
635 
636 	skb_dst_drop(skb2);
637 
638 	skb_pull(skb2, offset);
639 	skb_reset_network_header(skb2);
640 	eiph = ip_hdr(skb2);
641 
642 	/* Try to guess incoming interface */
643 	rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr,
644 				   0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
645 	if (IS_ERR(rt))
646 		goto out;
647 
648 	skb2->dev = rt->dst.dev;
649 	ip_rt_put(rt);
650 
651 	/* route "incoming" packet */
652 	if (rt->rt_flags & RTCF_LOCAL) {
653 		rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
654 					   eiph->daddr, eiph->saddr, 0, 0,
655 					   IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
656 		if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL6) {
657 			if (!IS_ERR(rt))
658 				ip_rt_put(rt);
659 			goto out;
660 		}
661 		skb_dst_set(skb2, &rt->dst);
662 	} else {
663 		if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
664 				   skb2->dev) ||
665 		    skb_dst(skb2)->dev->type != ARPHRD_TUNNEL6)
666 			goto out;
667 	}
668 
669 	/* change mtu on this route */
670 	if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
671 		if (rel_info > dst_mtu(skb_dst(skb2)))
672 			goto out;
673 
674 		skb_dst_update_pmtu_no_confirm(skb2, rel_info);
675 	}
676 
677 	icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
678 
679 out:
680 	kfree_skb(skb2);
681 	return 0;
682 }
683 
684 static int
685 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
686 	   u8 type, u8 code, int offset, __be32 info)
687 {
688 	__u32 rel_info = ntohl(info);
689 	int err, rel_msg = 0;
690 	u8 rel_type = type;
691 	u8 rel_code = code;
692 
693 	err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
694 			  &rel_msg, &rel_info, offset);
695 	if (err < 0)
696 		return err;
697 
698 	if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
699 		struct rt6_info *rt;
700 		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
701 
702 		if (!skb2)
703 			return 0;
704 
705 		skb_dst_drop(skb2);
706 		skb_pull(skb2, offset);
707 		skb_reset_network_header(skb2);
708 
709 		/* Try to guess incoming interface */
710 		rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
711 				NULL, 0, skb2, 0);
712 
713 		if (rt && rt->dst.dev)
714 			skb2->dev = rt->dst.dev;
715 
716 		icmpv6_send(skb2, rel_type, rel_code, rel_info);
717 
718 		ip6_rt_put(rt);
719 
720 		kfree_skb(skb2);
721 	}
722 
723 	return 0;
724 }
725 
726 static int
727 mplsip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
728 	    u8 type, u8 code, int offset, __be32 info)
729 {
730 	__u32 rel_info = ntohl(info);
731 	int err, rel_msg = 0;
732 	u8 rel_type = type;
733 	u8 rel_code = code;
734 
735 	err = ip6_tnl_err(skb, IPPROTO_MPLS, opt, &rel_type, &rel_code,
736 			  &rel_msg, &rel_info, offset);
737 	return err;
738 }
739 
740 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
741 				       const struct ipv6hdr *ipv6h,
742 				       struct sk_buff *skb)
743 {
744 	__u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
745 
746 	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
747 		ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
748 
749 	return IP6_ECN_decapsulate(ipv6h, skb);
750 }
751 
752 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
753 				       const struct ipv6hdr *ipv6h,
754 				       struct sk_buff *skb)
755 {
756 	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
757 		ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
758 
759 	return IP6_ECN_decapsulate(ipv6h, skb);
760 }
761 
762 static inline int mplsip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
763 					       const struct ipv6hdr *ipv6h,
764 					       struct sk_buff *skb)
765 {
766 	/* ECN is not supported in AF_MPLS */
767 	return 0;
768 }
769 
770 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
771 			     const struct in6_addr *laddr,
772 			     const struct in6_addr *raddr)
773 {
774 	struct __ip6_tnl_parm *p = &t->parms;
775 	int ltype = ipv6_addr_type(laddr);
776 	int rtype = ipv6_addr_type(raddr);
777 	__u32 flags = 0;
778 
779 	if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
780 		flags = IP6_TNL_F_CAP_PER_PACKET;
781 	} else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
782 		   rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
783 		   !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
784 		   (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
785 		if (ltype&IPV6_ADDR_UNICAST)
786 			flags |= IP6_TNL_F_CAP_XMIT;
787 		if (rtype&IPV6_ADDR_UNICAST)
788 			flags |= IP6_TNL_F_CAP_RCV;
789 	}
790 	return flags;
791 }
792 EXPORT_SYMBOL(ip6_tnl_get_cap);
793 
794 /* called with rcu_read_lock() */
795 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
796 				  const struct in6_addr *laddr,
797 				  const struct in6_addr *raddr)
798 {
799 	struct __ip6_tnl_parm *p = &t->parms;
800 	int ret = 0;
801 	struct net *net = t->net;
802 
803 	if ((p->flags & IP6_TNL_F_CAP_RCV) ||
804 	    ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
805 	     (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
806 		struct net_device *ldev = NULL;
807 
808 		if (p->link)
809 			ldev = dev_get_by_index_rcu(net, p->link);
810 
811 		if ((ipv6_addr_is_multicast(laddr) ||
812 		     likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false,
813 						    0, IFA_F_TENTATIVE))) &&
814 		    ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) ||
815 		     likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true,
816 						     0, IFA_F_TENTATIVE))))
817 			ret = 1;
818 	}
819 	return ret;
820 }
821 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
822 
823 static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
824 			 const struct tnl_ptk_info *tpi,
825 			 struct metadata_dst *tun_dst,
826 			 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
827 						const struct ipv6hdr *ipv6h,
828 						struct sk_buff *skb),
829 			 bool log_ecn_err)
830 {
831 	struct pcpu_sw_netstats *tstats;
832 	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
833 	int err;
834 
835 	if ((!(tpi->flags & TUNNEL_CSUM) &&
836 	     (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
837 	    ((tpi->flags & TUNNEL_CSUM) &&
838 	     !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
839 		tunnel->dev->stats.rx_crc_errors++;
840 		tunnel->dev->stats.rx_errors++;
841 		goto drop;
842 	}
843 
844 	if (tunnel->parms.i_flags & TUNNEL_SEQ) {
845 		if (!(tpi->flags & TUNNEL_SEQ) ||
846 		    (tunnel->i_seqno &&
847 		     (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
848 			tunnel->dev->stats.rx_fifo_errors++;
849 			tunnel->dev->stats.rx_errors++;
850 			goto drop;
851 		}
852 		tunnel->i_seqno = ntohl(tpi->seq) + 1;
853 	}
854 
855 	skb->protocol = tpi->proto;
856 
857 	/* Warning: All skb pointers will be invalidated! */
858 	if (tunnel->dev->type == ARPHRD_ETHER) {
859 		if (!pskb_may_pull(skb, ETH_HLEN)) {
860 			tunnel->dev->stats.rx_length_errors++;
861 			tunnel->dev->stats.rx_errors++;
862 			goto drop;
863 		}
864 
865 		ipv6h = ipv6_hdr(skb);
866 		skb->protocol = eth_type_trans(skb, tunnel->dev);
867 		skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
868 	} else {
869 		skb->dev = tunnel->dev;
870 	}
871 
872 	skb_reset_network_header(skb);
873 	memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
874 
875 	__skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
876 
877 	err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
878 	if (unlikely(err)) {
879 		if (log_ecn_err)
880 			net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
881 					     &ipv6h->saddr,
882 					     ipv6_get_dsfield(ipv6h));
883 		if (err > 1) {
884 			++tunnel->dev->stats.rx_frame_errors;
885 			++tunnel->dev->stats.rx_errors;
886 			goto drop;
887 		}
888 	}
889 
890 	tstats = this_cpu_ptr(tunnel->dev->tstats);
891 	u64_stats_update_begin(&tstats->syncp);
892 	tstats->rx_packets++;
893 	tstats->rx_bytes += skb->len;
894 	u64_stats_update_end(&tstats->syncp);
895 
896 	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
897 
898 	if (tun_dst)
899 		skb_dst_set(skb, (struct dst_entry *)tun_dst);
900 
901 	gro_cells_receive(&tunnel->gro_cells, skb);
902 	return 0;
903 
904 drop:
905 	if (tun_dst)
906 		dst_release((struct dst_entry *)tun_dst);
907 	kfree_skb(skb);
908 	return 0;
909 }
910 
911 int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
912 		const struct tnl_ptk_info *tpi,
913 		struct metadata_dst *tun_dst,
914 		bool log_ecn_err)
915 {
916 	return __ip6_tnl_rcv(t, skb, tpi, tun_dst, ip6ip6_dscp_ecn_decapsulate,
917 			     log_ecn_err);
918 }
919 EXPORT_SYMBOL(ip6_tnl_rcv);
920 
921 static const struct tnl_ptk_info tpi_v6 = {
922 	/* no tunnel info required for ipxip6. */
923 	.proto = htons(ETH_P_IPV6),
924 };
925 
926 static const struct tnl_ptk_info tpi_v4 = {
927 	/* no tunnel info required for ipxip6. */
928 	.proto = htons(ETH_P_IP),
929 };
930 
931 static const struct tnl_ptk_info tpi_mpls = {
932 	/* no tunnel info required for mplsip6. */
933 	.proto = htons(ETH_P_MPLS_UC),
934 };
935 
936 static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
937 		      const struct tnl_ptk_info *tpi,
938 		      int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
939 						  const struct ipv6hdr *ipv6h,
940 						  struct sk_buff *skb))
941 {
942 	struct ip6_tnl *t;
943 	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
944 	struct metadata_dst *tun_dst = NULL;
945 	int ret = -1;
946 
947 	rcu_read_lock();
948 	t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->saddr, &ipv6h->daddr);
949 
950 	if (t) {
951 		u8 tproto = READ_ONCE(t->parms.proto);
952 
953 		if (tproto != ipproto && tproto != 0)
954 			goto drop;
955 		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
956 			goto drop;
957 		ipv6h = ipv6_hdr(skb);
958 		if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
959 			goto drop;
960 		if (iptunnel_pull_header(skb, 0, tpi->proto, false))
961 			goto drop;
962 		if (t->parms.collect_md) {
963 			tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
964 			if (!tun_dst)
965 				goto drop;
966 		}
967 		ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
968 				    log_ecn_error);
969 	}
970 
971 	rcu_read_unlock();
972 
973 	return ret;
974 
975 drop:
976 	rcu_read_unlock();
977 	kfree_skb(skb);
978 	return 0;
979 }
980 
981 static int ip4ip6_rcv(struct sk_buff *skb)
982 {
983 	return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
984 			  ip4ip6_dscp_ecn_decapsulate);
985 }
986 
987 static int ip6ip6_rcv(struct sk_buff *skb)
988 {
989 	return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
990 			  ip6ip6_dscp_ecn_decapsulate);
991 }
992 
993 static int mplsip6_rcv(struct sk_buff *skb)
994 {
995 	return ipxip6_rcv(skb, IPPROTO_MPLS, &tpi_mpls,
996 			  mplsip6_dscp_ecn_decapsulate);
997 }
998 
999 struct ipv6_tel_txoption {
1000 	struct ipv6_txoptions ops;
1001 	__u8 dst_opt[8];
1002 };
1003 
1004 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
1005 {
1006 	memset(opt, 0, sizeof(struct ipv6_tel_txoption));
1007 
1008 	opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
1009 	opt->dst_opt[3] = 1;
1010 	opt->dst_opt[4] = encap_limit;
1011 	opt->dst_opt[5] = IPV6_TLV_PADN;
1012 	opt->dst_opt[6] = 1;
1013 
1014 	opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt;
1015 	opt->ops.opt_nflen = 8;
1016 }
1017 
1018 /**
1019  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
1020  *   @t: the outgoing tunnel device
1021  *   @hdr: IPv6 header from the incoming packet
1022  *
1023  * Description:
1024  *   Avoid trivial tunneling loop by checking that tunnel exit-point
1025  *   doesn't match source of incoming packet.
1026  *
1027  * Return:
1028  *   1 if conflict,
1029  *   0 else
1030  **/
1031 
1032 static inline bool
1033 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
1034 {
1035 	return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
1036 }
1037 
1038 int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
1039 		     const struct in6_addr *laddr,
1040 		     const struct in6_addr *raddr)
1041 {
1042 	struct __ip6_tnl_parm *p = &t->parms;
1043 	int ret = 0;
1044 	struct net *net = t->net;
1045 
1046 	if (t->parms.collect_md)
1047 		return 1;
1048 
1049 	if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
1050 	    ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
1051 	     (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
1052 		struct net_device *ldev = NULL;
1053 
1054 		rcu_read_lock();
1055 		if (p->link)
1056 			ldev = dev_get_by_index_rcu(net, p->link);
1057 
1058 		if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false,
1059 						      0, IFA_F_TENTATIVE)))
1060 			pr_warn("%s xmit: Local address not yet configured!\n",
1061 				p->name);
1062 		else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) &&
1063 			 !ipv6_addr_is_multicast(raddr) &&
1064 			 unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev,
1065 							  true, 0, IFA_F_TENTATIVE)))
1066 			pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1067 				p->name);
1068 		else
1069 			ret = 1;
1070 		rcu_read_unlock();
1071 	}
1072 	return ret;
1073 }
1074 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1075 
1076 /**
1077  * ip6_tnl_xmit - encapsulate packet and send
1078  *   @skb: the outgoing socket buffer
1079  *   @dev: the outgoing tunnel device
1080  *   @dsfield: dscp code for outer header
1081  *   @fl6: flow of tunneled packet
1082  *   @encap_limit: encapsulation limit
1083  *   @pmtu: Path MTU is stored if packet is too big
1084  *   @proto: next header value
1085  *
1086  * Description:
1087  *   Build new header and do some sanity checks on the packet before sending
1088  *   it.
1089  *
1090  * Return:
1091  *   0 on success
1092  *   -1 fail
1093  *   %-EMSGSIZE message too big. return mtu in this case.
1094  **/
1095 
1096 int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
1097 		 struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
1098 		 __u8 proto)
1099 {
1100 	struct ip6_tnl *t = netdev_priv(dev);
1101 	struct net *net = t->net;
1102 	struct net_device_stats *stats = &t->dev->stats;
1103 	struct ipv6hdr *ipv6h;
1104 	struct ipv6_tel_txoption opt;
1105 	struct dst_entry *dst = NULL, *ndst = NULL;
1106 	struct net_device *tdev;
1107 	int mtu;
1108 	unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
1109 	unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
1110 	unsigned int max_headroom = psh_hlen;
1111 	bool use_cache = false;
1112 	u8 hop_limit;
1113 	int err = -1;
1114 
1115 	if (t->parms.collect_md) {
1116 		hop_limit = skb_tunnel_info(skb)->key.ttl;
1117 		goto route_lookup;
1118 	} else {
1119 		hop_limit = t->parms.hop_limit;
1120 	}
1121 
1122 	/* NBMA tunnel */
1123 	if (ipv6_addr_any(&t->parms.raddr)) {
1124 		if (skb->protocol == htons(ETH_P_IPV6)) {
1125 			struct in6_addr *addr6;
1126 			struct neighbour *neigh;
1127 			int addr_type;
1128 
1129 			if (!skb_dst(skb))
1130 				goto tx_err_link_failure;
1131 
1132 			neigh = dst_neigh_lookup(skb_dst(skb),
1133 						 &ipv6_hdr(skb)->daddr);
1134 			if (!neigh)
1135 				goto tx_err_link_failure;
1136 
1137 			addr6 = (struct in6_addr *)&neigh->primary_key;
1138 			addr_type = ipv6_addr_type(addr6);
1139 
1140 			if (addr_type == IPV6_ADDR_ANY)
1141 				addr6 = &ipv6_hdr(skb)->daddr;
1142 
1143 			memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1144 			neigh_release(neigh);
1145 		}
1146 	} else if (t->parms.proto != 0 && !(t->parms.flags &
1147 					    (IP6_TNL_F_USE_ORIG_TCLASS |
1148 					     IP6_TNL_F_USE_ORIG_FWMARK))) {
1149 		/* enable the cache only if neither the outer protocol nor the
1150 		 * routing decision depends on the current inner header value
1151 		 */
1152 		use_cache = true;
1153 	}
1154 
1155 	if (use_cache)
1156 		dst = dst_cache_get(&t->dst_cache);
1157 
1158 	if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1159 		goto tx_err_link_failure;
1160 
1161 	if (!dst) {
1162 route_lookup:
1163 		/* add dsfield to flowlabel for route lookup */
1164 		fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
1165 
1166 		dst = ip6_route_output(net, NULL, fl6);
1167 
1168 		if (dst->error)
1169 			goto tx_err_link_failure;
1170 		dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1171 		if (IS_ERR(dst)) {
1172 			err = PTR_ERR(dst);
1173 			dst = NULL;
1174 			goto tx_err_link_failure;
1175 		}
1176 		if (t->parms.collect_md && ipv6_addr_any(&fl6->saddr) &&
1177 		    ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
1178 				       &fl6->daddr, 0, &fl6->saddr))
1179 			goto tx_err_link_failure;
1180 		ndst = dst;
1181 	}
1182 
1183 	tdev = dst->dev;
1184 
1185 	if (tdev == dev) {
1186 		stats->collisions++;
1187 		net_warn_ratelimited("%s: Local routing loop detected!\n",
1188 				     t->parms.name);
1189 		goto tx_err_dst_release;
1190 	}
1191 	mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
1192 	if (encap_limit >= 0) {
1193 		max_headroom += 8;
1194 		mtu -= 8;
1195 	}
1196 	mtu = max(mtu, skb->protocol == htons(ETH_P_IPV6) ?
1197 		       IPV6_MIN_MTU : IPV4_MIN_MTU);
1198 
1199 	skb_dst_update_pmtu_no_confirm(skb, mtu);
1200 	if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
1201 		*pmtu = mtu;
1202 		err = -EMSGSIZE;
1203 		goto tx_err_dst_release;
1204 	}
1205 
1206 	if (t->err_count > 0) {
1207 		if (time_before(jiffies,
1208 				t->err_time + IP6TUNNEL_ERR_TIMEO)) {
1209 			t->err_count--;
1210 
1211 			dst_link_failure(skb);
1212 		} else {
1213 			t->err_count = 0;
1214 		}
1215 	}
1216 
1217 	skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1218 
1219 	/*
1220 	 * Okay, now see if we can stuff it in the buffer as-is.
1221 	 */
1222 	max_headroom += LL_RESERVED_SPACE(tdev);
1223 
1224 	if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1225 	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1226 		struct sk_buff *new_skb;
1227 
1228 		new_skb = skb_realloc_headroom(skb, max_headroom);
1229 		if (!new_skb)
1230 			goto tx_err_dst_release;
1231 
1232 		if (skb->sk)
1233 			skb_set_owner_w(new_skb, skb->sk);
1234 		consume_skb(skb);
1235 		skb = new_skb;
1236 	}
1237 
1238 	if (t->parms.collect_md) {
1239 		if (t->encap.type != TUNNEL_ENCAP_NONE)
1240 			goto tx_err_dst_release;
1241 	} else {
1242 		if (use_cache && ndst)
1243 			dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1244 	}
1245 	skb_dst_set(skb, dst);
1246 
1247 	if (hop_limit == 0) {
1248 		if (skb->protocol == htons(ETH_P_IP))
1249 			hop_limit = ip_hdr(skb)->ttl;
1250 		else if (skb->protocol == htons(ETH_P_IPV6))
1251 			hop_limit = ipv6_hdr(skb)->hop_limit;
1252 		else
1253 			hop_limit = ip6_dst_hoplimit(dst);
1254 	}
1255 
1256 	/* Calculate max headroom for all the headers and adjust
1257 	 * needed_headroom if necessary.
1258 	 */
1259 	max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
1260 			+ dst->header_len + t->hlen;
1261 	if (max_headroom > dev->needed_headroom)
1262 		dev->needed_headroom = max_headroom;
1263 
1264 	err = ip6_tnl_encap(skb, t, &proto, fl6);
1265 	if (err)
1266 		return err;
1267 
1268 	if (encap_limit >= 0) {
1269 		init_tel_txopt(&opt, encap_limit);
1270 		ipv6_push_frag_opts(skb, &opt.ops, &proto);
1271 	}
1272 
1273 	skb_set_inner_ipproto(skb, proto);
1274 
1275 	skb_push(skb, sizeof(struct ipv6hdr));
1276 	skb_reset_network_header(skb);
1277 	ipv6h = ipv6_hdr(skb);
1278 	ip6_flow_hdr(ipv6h, dsfield,
1279 		     ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1280 	ipv6h->hop_limit = hop_limit;
1281 	ipv6h->nexthdr = proto;
1282 	ipv6h->saddr = fl6->saddr;
1283 	ipv6h->daddr = fl6->daddr;
1284 	ip6tunnel_xmit(NULL, skb, dev);
1285 	return 0;
1286 tx_err_link_failure:
1287 	stats->tx_carrier_errors++;
1288 	dst_link_failure(skb);
1289 tx_err_dst_release:
1290 	dst_release(dst);
1291 	return err;
1292 }
1293 EXPORT_SYMBOL(ip6_tnl_xmit);
1294 
1295 static inline int
1296 ipxip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev,
1297 		u8 protocol)
1298 {
1299 	struct ip6_tnl *t = netdev_priv(dev);
1300 	struct ipv6hdr *ipv6h;
1301 	const struct iphdr  *iph;
1302 	int encap_limit = -1;
1303 	__u16 offset;
1304 	struct flowi6 fl6;
1305 	__u8 dsfield, orig_dsfield;
1306 	__u32 mtu;
1307 	u8 tproto;
1308 	int err;
1309 
1310 	tproto = READ_ONCE(t->parms.proto);
1311 	if (tproto != protocol && tproto != 0)
1312 		return -1;
1313 
1314 	if (t->parms.collect_md) {
1315 		struct ip_tunnel_info *tun_info;
1316 		const struct ip_tunnel_key *key;
1317 
1318 		tun_info = skb_tunnel_info(skb);
1319 		if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1320 			     ip_tunnel_info_af(tun_info) != AF_INET6))
1321 			return -1;
1322 		key = &tun_info->key;
1323 		memset(&fl6, 0, sizeof(fl6));
1324 		fl6.flowi6_proto = protocol;
1325 		fl6.saddr = key->u.ipv6.src;
1326 		fl6.daddr = key->u.ipv6.dst;
1327 		fl6.flowlabel = key->label;
1328 		dsfield =  key->tos;
1329 		switch (protocol) {
1330 		case IPPROTO_IPIP:
1331 			iph = ip_hdr(skb);
1332 			orig_dsfield = ipv4_get_dsfield(iph);
1333 			break;
1334 		case IPPROTO_IPV6:
1335 			ipv6h = ipv6_hdr(skb);
1336 			orig_dsfield = ipv6_get_dsfield(ipv6h);
1337 			break;
1338 		default:
1339 			orig_dsfield = dsfield;
1340 			break;
1341 		}
1342 	} else {
1343 		if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1344 			encap_limit = t->parms.encap_limit;
1345 		if (protocol == IPPROTO_IPV6) {
1346 			offset = ip6_tnl_parse_tlv_enc_lim(skb,
1347 						skb_network_header(skb));
1348 			/* ip6_tnl_parse_tlv_enc_lim() might have
1349 			 * reallocated skb->head
1350 			 */
1351 			if (offset > 0) {
1352 				struct ipv6_tlv_tnl_enc_lim *tel;
1353 
1354 				tel = (void *)&skb_network_header(skb)[offset];
1355 				if (tel->encap_limit == 0) {
1356 					icmpv6_send(skb, ICMPV6_PARAMPROB,
1357 						ICMPV6_HDR_FIELD, offset + 2);
1358 					return -1;
1359 				}
1360 				encap_limit = tel->encap_limit - 1;
1361 			}
1362 		}
1363 
1364 		memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1365 		fl6.flowi6_proto = protocol;
1366 
1367 		if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1368 			fl6.flowi6_mark = skb->mark;
1369 		else
1370 			fl6.flowi6_mark = t->parms.fwmark;
1371 		switch (protocol) {
1372 		case IPPROTO_IPIP:
1373 			iph = ip_hdr(skb);
1374 			orig_dsfield = ipv4_get_dsfield(iph);
1375 			if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1376 				dsfield = orig_dsfield;
1377 			else
1378 				dsfield = ip6_tclass(t->parms.flowinfo);
1379 			break;
1380 		case IPPROTO_IPV6:
1381 			ipv6h = ipv6_hdr(skb);
1382 			orig_dsfield = ipv6_get_dsfield(ipv6h);
1383 			if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1384 				dsfield = orig_dsfield;
1385 			else
1386 				dsfield = ip6_tclass(t->parms.flowinfo);
1387 			if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1388 				fl6.flowlabel |= ip6_flowlabel(ipv6h);
1389 			break;
1390 		default:
1391 			orig_dsfield = dsfield = ip6_tclass(t->parms.flowinfo);
1392 			break;
1393 		}
1394 	}
1395 
1396 	fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1397 	dsfield = INET_ECN_encapsulate(dsfield, orig_dsfield);
1398 
1399 	if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
1400 		return -1;
1401 
1402 	err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1403 			   protocol);
1404 	if (err != 0) {
1405 		/* XXX: send ICMP error even if DF is not set. */
1406 		if (err == -EMSGSIZE)
1407 			switch (protocol) {
1408 			case IPPROTO_IPIP:
1409 				icmp_send(skb, ICMP_DEST_UNREACH,
1410 					  ICMP_FRAG_NEEDED, htonl(mtu));
1411 				break;
1412 			case IPPROTO_IPV6:
1413 				icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1414 				break;
1415 			default:
1416 				break;
1417 			}
1418 		return -1;
1419 	}
1420 
1421 	return 0;
1422 }
1423 
1424 static netdev_tx_t
1425 ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
1426 {
1427 	struct ip6_tnl *t = netdev_priv(dev);
1428 	struct net_device_stats *stats = &t->dev->stats;
1429 	u8 ipproto;
1430 	int ret;
1431 
1432 	if (!pskb_inet_may_pull(skb))
1433 		goto tx_err;
1434 
1435 	switch (skb->protocol) {
1436 	case htons(ETH_P_IP):
1437 		ipproto = IPPROTO_IPIP;
1438 		break;
1439 	case htons(ETH_P_IPV6):
1440 		if (ip6_tnl_addr_conflict(t, ipv6_hdr(skb)))
1441 			goto tx_err;
1442 		ipproto = IPPROTO_IPV6;
1443 		break;
1444 	case htons(ETH_P_MPLS_UC):
1445 		ipproto = IPPROTO_MPLS;
1446 		break;
1447 	default:
1448 		goto tx_err;
1449 	}
1450 
1451 	ret = ipxip6_tnl_xmit(skb, dev, ipproto);
1452 	if (ret < 0)
1453 		goto tx_err;
1454 
1455 	return NETDEV_TX_OK;
1456 
1457 tx_err:
1458 	stats->tx_errors++;
1459 	stats->tx_dropped++;
1460 	kfree_skb(skb);
1461 	return NETDEV_TX_OK;
1462 }
1463 
1464 static void ip6_tnl_link_config(struct ip6_tnl *t)
1465 {
1466 	struct net_device *dev = t->dev;
1467 	struct net_device *tdev = NULL;
1468 	struct __ip6_tnl_parm *p = &t->parms;
1469 	struct flowi6 *fl6 = &t->fl.u.ip6;
1470 	unsigned int mtu;
1471 	int t_hlen;
1472 
1473 	memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1474 	memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1475 
1476 	/* Set up flowi template */
1477 	fl6->saddr = p->laddr;
1478 	fl6->daddr = p->raddr;
1479 	fl6->flowi6_oif = p->link;
1480 	fl6->flowlabel = 0;
1481 
1482 	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1483 		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1484 	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1485 		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1486 
1487 	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1488 	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1489 
1490 	if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1491 		dev->flags |= IFF_POINTOPOINT;
1492 	else
1493 		dev->flags &= ~IFF_POINTOPOINT;
1494 
1495 	t->tun_hlen = 0;
1496 	t->hlen = t->encap_hlen + t->tun_hlen;
1497 	t_hlen = t->hlen + sizeof(struct ipv6hdr);
1498 
1499 	if (p->flags & IP6_TNL_F_CAP_XMIT) {
1500 		int strict = (ipv6_addr_type(&p->raddr) &
1501 			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1502 
1503 		struct rt6_info *rt = rt6_lookup(t->net,
1504 						 &p->raddr, &p->laddr,
1505 						 p->link, NULL, strict);
1506 		if (rt) {
1507 			tdev = rt->dst.dev;
1508 			ip6_rt_put(rt);
1509 		}
1510 
1511 		if (!tdev && p->link)
1512 			tdev = __dev_get_by_index(t->net, p->link);
1513 
1514 		if (tdev) {
1515 			dev->hard_header_len = tdev->hard_header_len + t_hlen;
1516 			mtu = min_t(unsigned int, tdev->mtu, IP6_MAX_MTU);
1517 
1518 			dev->mtu = mtu - t_hlen;
1519 			if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1520 				dev->mtu -= 8;
1521 
1522 			if (dev->mtu < IPV6_MIN_MTU)
1523 				dev->mtu = IPV6_MIN_MTU;
1524 		}
1525 	}
1526 }
1527 
1528 /**
1529  * ip6_tnl_change - update the tunnel parameters
1530  *   @t: tunnel to be changed
1531  *   @p: tunnel configuration parameters
1532  *
1533  * Description:
1534  *   ip6_tnl_change() updates the tunnel parameters
1535  **/
1536 
1537 static int
1538 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1539 {
1540 	t->parms.laddr = p->laddr;
1541 	t->parms.raddr = p->raddr;
1542 	t->parms.flags = p->flags;
1543 	t->parms.hop_limit = p->hop_limit;
1544 	t->parms.encap_limit = p->encap_limit;
1545 	t->parms.flowinfo = p->flowinfo;
1546 	t->parms.link = p->link;
1547 	t->parms.proto = p->proto;
1548 	t->parms.fwmark = p->fwmark;
1549 	dst_cache_reset(&t->dst_cache);
1550 	ip6_tnl_link_config(t);
1551 	return 0;
1552 }
1553 
1554 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1555 {
1556 	struct net *net = t->net;
1557 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1558 	int err;
1559 
1560 	ip6_tnl_unlink(ip6n, t);
1561 	synchronize_net();
1562 	err = ip6_tnl_change(t, p);
1563 	ip6_tnl_link(ip6n, t);
1564 	netdev_state_change(t->dev);
1565 	return err;
1566 }
1567 
1568 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1569 {
1570 	/* for default tnl0 device allow to change only the proto */
1571 	t->parms.proto = p->proto;
1572 	netdev_state_change(t->dev);
1573 	return 0;
1574 }
1575 
1576 static void
1577 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1578 {
1579 	p->laddr = u->laddr;
1580 	p->raddr = u->raddr;
1581 	p->flags = u->flags;
1582 	p->hop_limit = u->hop_limit;
1583 	p->encap_limit = u->encap_limit;
1584 	p->flowinfo = u->flowinfo;
1585 	p->link = u->link;
1586 	p->proto = u->proto;
1587 	memcpy(p->name, u->name, sizeof(u->name));
1588 }
1589 
1590 static void
1591 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1592 {
1593 	u->laddr = p->laddr;
1594 	u->raddr = p->raddr;
1595 	u->flags = p->flags;
1596 	u->hop_limit = p->hop_limit;
1597 	u->encap_limit = p->encap_limit;
1598 	u->flowinfo = p->flowinfo;
1599 	u->link = p->link;
1600 	u->proto = p->proto;
1601 	memcpy(u->name, p->name, sizeof(u->name));
1602 }
1603 
1604 /**
1605  * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1606  *   @dev: virtual device associated with tunnel
1607  *   @ifr: parameters passed from userspace
1608  *   @cmd: command to be performed
1609  *
1610  * Description:
1611  *   ip6_tnl_ioctl() is used for managing IPv6 tunnels
1612  *   from userspace.
1613  *
1614  *   The possible commands are the following:
1615  *     %SIOCGETTUNNEL: get tunnel parameters for device
1616  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1617  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
1618  *     %SIOCDELTUNNEL: delete tunnel
1619  *
1620  *   The fallback device "ip6tnl0", created during module
1621  *   initialization, can be used for creating other tunnel devices.
1622  *
1623  * Return:
1624  *   0 on success,
1625  *   %-EFAULT if unable to copy data to or from userspace,
1626  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
1627  *   %-EINVAL if passed tunnel parameters are invalid,
1628  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
1629  *   %-ENODEV if attempting to change or delete a nonexisting device
1630  **/
1631 
1632 static int
1633 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1634 {
1635 	int err = 0;
1636 	struct ip6_tnl_parm p;
1637 	struct __ip6_tnl_parm p1;
1638 	struct ip6_tnl *t = netdev_priv(dev);
1639 	struct net *net = t->net;
1640 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1641 
1642 	memset(&p1, 0, sizeof(p1));
1643 
1644 	switch (cmd) {
1645 	case SIOCGETTUNNEL:
1646 		if (dev == ip6n->fb_tnl_dev) {
1647 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1648 				err = -EFAULT;
1649 				break;
1650 			}
1651 			ip6_tnl_parm_from_user(&p1, &p);
1652 			t = ip6_tnl_locate(net, &p1, 0);
1653 			if (IS_ERR(t))
1654 				t = netdev_priv(dev);
1655 		} else {
1656 			memset(&p, 0, sizeof(p));
1657 		}
1658 		ip6_tnl_parm_to_user(&p, &t->parms);
1659 		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1660 			err = -EFAULT;
1661 		}
1662 		break;
1663 	case SIOCADDTUNNEL:
1664 	case SIOCCHGTUNNEL:
1665 		err = -EPERM;
1666 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1667 			break;
1668 		err = -EFAULT;
1669 		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1670 			break;
1671 		err = -EINVAL;
1672 		if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1673 		    p.proto != 0)
1674 			break;
1675 		ip6_tnl_parm_from_user(&p1, &p);
1676 		t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1677 		if (cmd == SIOCCHGTUNNEL) {
1678 			if (!IS_ERR(t)) {
1679 				if (t->dev != dev) {
1680 					err = -EEXIST;
1681 					break;
1682 				}
1683 			} else
1684 				t = netdev_priv(dev);
1685 			if (dev == ip6n->fb_tnl_dev)
1686 				err = ip6_tnl0_update(t, &p1);
1687 			else
1688 				err = ip6_tnl_update(t, &p1);
1689 		}
1690 		if (!IS_ERR(t)) {
1691 			err = 0;
1692 			ip6_tnl_parm_to_user(&p, &t->parms);
1693 			if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1694 				err = -EFAULT;
1695 
1696 		} else {
1697 			err = PTR_ERR(t);
1698 		}
1699 		break;
1700 	case SIOCDELTUNNEL:
1701 		err = -EPERM;
1702 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1703 			break;
1704 
1705 		if (dev == ip6n->fb_tnl_dev) {
1706 			err = -EFAULT;
1707 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1708 				break;
1709 			err = -ENOENT;
1710 			ip6_tnl_parm_from_user(&p1, &p);
1711 			t = ip6_tnl_locate(net, &p1, 0);
1712 			if (IS_ERR(t))
1713 				break;
1714 			err = -EPERM;
1715 			if (t->dev == ip6n->fb_tnl_dev)
1716 				break;
1717 			dev = t->dev;
1718 		}
1719 		err = 0;
1720 		unregister_netdevice(dev);
1721 		break;
1722 	default:
1723 		err = -EINVAL;
1724 	}
1725 	return err;
1726 }
1727 
1728 /**
1729  * ip6_tnl_change_mtu - change mtu manually for tunnel device
1730  *   @dev: virtual device associated with tunnel
1731  *   @new_mtu: the new mtu
1732  *
1733  * Return:
1734  *   0 on success,
1735  *   %-EINVAL if mtu too small
1736  **/
1737 
1738 int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1739 {
1740 	struct ip6_tnl *tnl = netdev_priv(dev);
1741 
1742 	if (tnl->parms.proto == IPPROTO_IPV6) {
1743 		if (new_mtu < IPV6_MIN_MTU)
1744 			return -EINVAL;
1745 	} else {
1746 		if (new_mtu < ETH_MIN_MTU)
1747 			return -EINVAL;
1748 	}
1749 	if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) {
1750 		if (new_mtu > IP6_MAX_MTU - dev->hard_header_len)
1751 			return -EINVAL;
1752 	} else {
1753 		if (new_mtu > IP_MAX_MTU - dev->hard_header_len)
1754 			return -EINVAL;
1755 	}
1756 	dev->mtu = new_mtu;
1757 	return 0;
1758 }
1759 EXPORT_SYMBOL(ip6_tnl_change_mtu);
1760 
1761 int ip6_tnl_get_iflink(const struct net_device *dev)
1762 {
1763 	struct ip6_tnl *t = netdev_priv(dev);
1764 
1765 	return t->parms.link;
1766 }
1767 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1768 
1769 int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
1770 			  unsigned int num)
1771 {
1772 	if (num >= MAX_IPTUN_ENCAP_OPS)
1773 		return -ERANGE;
1774 
1775 	return !cmpxchg((const struct ip6_tnl_encap_ops **)
1776 			&ip6tun_encaps[num],
1777 			NULL, ops) ? 0 : -1;
1778 }
1779 EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
1780 
1781 int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
1782 			  unsigned int num)
1783 {
1784 	int ret;
1785 
1786 	if (num >= MAX_IPTUN_ENCAP_OPS)
1787 		return -ERANGE;
1788 
1789 	ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
1790 		       &ip6tun_encaps[num],
1791 		       ops, NULL) == ops) ? 0 : -1;
1792 
1793 	synchronize_net();
1794 
1795 	return ret;
1796 }
1797 EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
1798 
1799 int ip6_tnl_encap_setup(struct ip6_tnl *t,
1800 			struct ip_tunnel_encap *ipencap)
1801 {
1802 	int hlen;
1803 
1804 	memset(&t->encap, 0, sizeof(t->encap));
1805 
1806 	hlen = ip6_encap_hlen(ipencap);
1807 	if (hlen < 0)
1808 		return hlen;
1809 
1810 	t->encap.type = ipencap->type;
1811 	t->encap.sport = ipencap->sport;
1812 	t->encap.dport = ipencap->dport;
1813 	t->encap.flags = ipencap->flags;
1814 
1815 	t->encap_hlen = hlen;
1816 	t->hlen = t->encap_hlen + t->tun_hlen;
1817 
1818 	return 0;
1819 }
1820 EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
1821 
1822 static const struct net_device_ops ip6_tnl_netdev_ops = {
1823 	.ndo_init	= ip6_tnl_dev_init,
1824 	.ndo_uninit	= ip6_tnl_dev_uninit,
1825 	.ndo_start_xmit = ip6_tnl_start_xmit,
1826 	.ndo_do_ioctl	= ip6_tnl_ioctl,
1827 	.ndo_change_mtu = ip6_tnl_change_mtu,
1828 	.ndo_get_stats	= ip6_get_stats,
1829 	.ndo_get_iflink = ip6_tnl_get_iflink,
1830 };
1831 
1832 #define IPXIPX_FEATURES (NETIF_F_SG |		\
1833 			 NETIF_F_FRAGLIST |	\
1834 			 NETIF_F_HIGHDMA |	\
1835 			 NETIF_F_GSO_SOFTWARE |	\
1836 			 NETIF_F_HW_CSUM)
1837 
1838 /**
1839  * ip6_tnl_dev_setup - setup virtual tunnel device
1840  *   @dev: virtual device associated with tunnel
1841  *
1842  * Description:
1843  *   Initialize function pointers and device parameters
1844  **/
1845 
1846 static void ip6_tnl_dev_setup(struct net_device *dev)
1847 {
1848 	dev->netdev_ops = &ip6_tnl_netdev_ops;
1849 	dev->header_ops = &ip_tunnel_header_ops;
1850 	dev->needs_free_netdev = true;
1851 	dev->priv_destructor = ip6_dev_free;
1852 
1853 	dev->type = ARPHRD_TUNNEL6;
1854 	dev->flags |= IFF_NOARP;
1855 	dev->addr_len = sizeof(struct in6_addr);
1856 	dev->features |= NETIF_F_LLTX;
1857 	netif_keep_dst(dev);
1858 
1859 	dev->features		|= IPXIPX_FEATURES;
1860 	dev->hw_features	|= IPXIPX_FEATURES;
1861 
1862 	/* This perm addr will be used as interface identifier by IPv6 */
1863 	dev->addr_assign_type = NET_ADDR_RANDOM;
1864 	eth_random_addr(dev->perm_addr);
1865 }
1866 
1867 
1868 /**
1869  * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1870  *   @dev: virtual device associated with tunnel
1871  **/
1872 
1873 static inline int
1874 ip6_tnl_dev_init_gen(struct net_device *dev)
1875 {
1876 	struct ip6_tnl *t = netdev_priv(dev);
1877 	int ret;
1878 	int t_hlen;
1879 
1880 	t->dev = dev;
1881 	t->net = dev_net(dev);
1882 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1883 	if (!dev->tstats)
1884 		return -ENOMEM;
1885 
1886 	ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
1887 	if (ret)
1888 		goto free_stats;
1889 
1890 	ret = gro_cells_init(&t->gro_cells, dev);
1891 	if (ret)
1892 		goto destroy_dst;
1893 
1894 	t->tun_hlen = 0;
1895 	t->hlen = t->encap_hlen + t->tun_hlen;
1896 	t_hlen = t->hlen + sizeof(struct ipv6hdr);
1897 
1898 	dev->type = ARPHRD_TUNNEL6;
1899 	dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1900 	dev->mtu = ETH_DATA_LEN - t_hlen;
1901 	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1902 		dev->mtu -= 8;
1903 	dev->min_mtu = ETH_MIN_MTU;
1904 	dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len;
1905 
1906 	return 0;
1907 
1908 destroy_dst:
1909 	dst_cache_destroy(&t->dst_cache);
1910 free_stats:
1911 	free_percpu(dev->tstats);
1912 	dev->tstats = NULL;
1913 
1914 	return ret;
1915 }
1916 
1917 /**
1918  * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1919  *   @dev: virtual device associated with tunnel
1920  **/
1921 
1922 static int ip6_tnl_dev_init(struct net_device *dev)
1923 {
1924 	struct ip6_tnl *t = netdev_priv(dev);
1925 	int err = ip6_tnl_dev_init_gen(dev);
1926 
1927 	if (err)
1928 		return err;
1929 	ip6_tnl_link_config(t);
1930 	if (t->parms.collect_md)
1931 		netif_keep_dst(dev);
1932 	return 0;
1933 }
1934 
1935 /**
1936  * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1937  *   @dev: fallback device
1938  *
1939  * Return: 0
1940  **/
1941 
1942 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1943 {
1944 	struct ip6_tnl *t = netdev_priv(dev);
1945 	struct net *net = dev_net(dev);
1946 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1947 
1948 	t->parms.proto = IPPROTO_IPV6;
1949 	dev_hold(dev);
1950 
1951 	rcu_assign_pointer(ip6n->tnls_wc[0], t);
1952 	return 0;
1953 }
1954 
1955 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[],
1956 			    struct netlink_ext_ack *extack)
1957 {
1958 	u8 proto;
1959 
1960 	if (!data || !data[IFLA_IPTUN_PROTO])
1961 		return 0;
1962 
1963 	proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1964 	if (proto != IPPROTO_IPV6 &&
1965 	    proto != IPPROTO_IPIP &&
1966 	    proto != 0)
1967 		return -EINVAL;
1968 
1969 	return 0;
1970 }
1971 
1972 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1973 				  struct __ip6_tnl_parm *parms)
1974 {
1975 	memset(parms, 0, sizeof(*parms));
1976 
1977 	if (!data)
1978 		return;
1979 
1980 	if (data[IFLA_IPTUN_LINK])
1981 		parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1982 
1983 	if (data[IFLA_IPTUN_LOCAL])
1984 		parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1985 
1986 	if (data[IFLA_IPTUN_REMOTE])
1987 		parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1988 
1989 	if (data[IFLA_IPTUN_TTL])
1990 		parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1991 
1992 	if (data[IFLA_IPTUN_ENCAP_LIMIT])
1993 		parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1994 
1995 	if (data[IFLA_IPTUN_FLOWINFO])
1996 		parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1997 
1998 	if (data[IFLA_IPTUN_FLAGS])
1999 		parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
2000 
2001 	if (data[IFLA_IPTUN_PROTO])
2002 		parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
2003 
2004 	if (data[IFLA_IPTUN_COLLECT_METADATA])
2005 		parms->collect_md = true;
2006 
2007 	if (data[IFLA_IPTUN_FWMARK])
2008 		parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
2009 }
2010 
2011 static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
2012 					struct ip_tunnel_encap *ipencap)
2013 {
2014 	bool ret = false;
2015 
2016 	memset(ipencap, 0, sizeof(*ipencap));
2017 
2018 	if (!data)
2019 		return ret;
2020 
2021 	if (data[IFLA_IPTUN_ENCAP_TYPE]) {
2022 		ret = true;
2023 		ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
2024 	}
2025 
2026 	if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
2027 		ret = true;
2028 		ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
2029 	}
2030 
2031 	if (data[IFLA_IPTUN_ENCAP_SPORT]) {
2032 		ret = true;
2033 		ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
2034 	}
2035 
2036 	if (data[IFLA_IPTUN_ENCAP_DPORT]) {
2037 		ret = true;
2038 		ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
2039 	}
2040 
2041 	return ret;
2042 }
2043 
2044 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
2045 			   struct nlattr *tb[], struct nlattr *data[],
2046 			   struct netlink_ext_ack *extack)
2047 {
2048 	struct net *net = dev_net(dev);
2049 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2050 	struct ip_tunnel_encap ipencap;
2051 	struct ip6_tnl *nt, *t;
2052 	int err;
2053 
2054 	nt = netdev_priv(dev);
2055 
2056 	if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
2057 		err = ip6_tnl_encap_setup(nt, &ipencap);
2058 		if (err < 0)
2059 			return err;
2060 	}
2061 
2062 	ip6_tnl_netlink_parms(data, &nt->parms);
2063 
2064 	if (nt->parms.collect_md) {
2065 		if (rtnl_dereference(ip6n->collect_md_tun))
2066 			return -EEXIST;
2067 	} else {
2068 		t = ip6_tnl_locate(net, &nt->parms, 0);
2069 		if (!IS_ERR(t))
2070 			return -EEXIST;
2071 	}
2072 
2073 	err = ip6_tnl_create2(dev);
2074 	if (!err && tb[IFLA_MTU])
2075 		ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2076 
2077 	return err;
2078 }
2079 
2080 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
2081 			      struct nlattr *data[],
2082 			      struct netlink_ext_ack *extack)
2083 {
2084 	struct ip6_tnl *t = netdev_priv(dev);
2085 	struct __ip6_tnl_parm p;
2086 	struct net *net = t->net;
2087 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2088 	struct ip_tunnel_encap ipencap;
2089 
2090 	if (dev == ip6n->fb_tnl_dev)
2091 		return -EINVAL;
2092 
2093 	if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
2094 		int err = ip6_tnl_encap_setup(t, &ipencap);
2095 
2096 		if (err < 0)
2097 			return err;
2098 	}
2099 	ip6_tnl_netlink_parms(data, &p);
2100 	if (p.collect_md)
2101 		return -EINVAL;
2102 
2103 	t = ip6_tnl_locate(net, &p, 0);
2104 	if (!IS_ERR(t)) {
2105 		if (t->dev != dev)
2106 			return -EEXIST;
2107 	} else
2108 		t = netdev_priv(dev);
2109 
2110 	return ip6_tnl_update(t, &p);
2111 }
2112 
2113 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
2114 {
2115 	struct net *net = dev_net(dev);
2116 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2117 
2118 	if (dev != ip6n->fb_tnl_dev)
2119 		unregister_netdevice_queue(dev, head);
2120 }
2121 
2122 static size_t ip6_tnl_get_size(const struct net_device *dev)
2123 {
2124 	return
2125 		/* IFLA_IPTUN_LINK */
2126 		nla_total_size(4) +
2127 		/* IFLA_IPTUN_LOCAL */
2128 		nla_total_size(sizeof(struct in6_addr)) +
2129 		/* IFLA_IPTUN_REMOTE */
2130 		nla_total_size(sizeof(struct in6_addr)) +
2131 		/* IFLA_IPTUN_TTL */
2132 		nla_total_size(1) +
2133 		/* IFLA_IPTUN_ENCAP_LIMIT */
2134 		nla_total_size(1) +
2135 		/* IFLA_IPTUN_FLOWINFO */
2136 		nla_total_size(4) +
2137 		/* IFLA_IPTUN_FLAGS */
2138 		nla_total_size(4) +
2139 		/* IFLA_IPTUN_PROTO */
2140 		nla_total_size(1) +
2141 		/* IFLA_IPTUN_ENCAP_TYPE */
2142 		nla_total_size(2) +
2143 		/* IFLA_IPTUN_ENCAP_FLAGS */
2144 		nla_total_size(2) +
2145 		/* IFLA_IPTUN_ENCAP_SPORT */
2146 		nla_total_size(2) +
2147 		/* IFLA_IPTUN_ENCAP_DPORT */
2148 		nla_total_size(2) +
2149 		/* IFLA_IPTUN_COLLECT_METADATA */
2150 		nla_total_size(0) +
2151 		/* IFLA_IPTUN_FWMARK */
2152 		nla_total_size(4) +
2153 		0;
2154 }
2155 
2156 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
2157 {
2158 	struct ip6_tnl *tunnel = netdev_priv(dev);
2159 	struct __ip6_tnl_parm *parm = &tunnel->parms;
2160 
2161 	if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
2162 	    nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
2163 	    nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
2164 	    nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
2165 	    nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
2166 	    nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
2167 	    nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
2168 	    nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
2169 	    nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark))
2170 		goto nla_put_failure;
2171 
2172 	if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
2173 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
2174 	    nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
2175 	    nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
2176 		goto nla_put_failure;
2177 
2178 	if (parm->collect_md)
2179 		if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
2180 			goto nla_put_failure;
2181 
2182 	return 0;
2183 
2184 nla_put_failure:
2185 	return -EMSGSIZE;
2186 }
2187 
2188 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
2189 {
2190 	struct ip6_tnl *tunnel = netdev_priv(dev);
2191 
2192 	return tunnel->net;
2193 }
2194 EXPORT_SYMBOL(ip6_tnl_get_link_net);
2195 
2196 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
2197 	[IFLA_IPTUN_LINK]		= { .type = NLA_U32 },
2198 	[IFLA_IPTUN_LOCAL]		= { .len = sizeof(struct in6_addr) },
2199 	[IFLA_IPTUN_REMOTE]		= { .len = sizeof(struct in6_addr) },
2200 	[IFLA_IPTUN_TTL]		= { .type = NLA_U8 },
2201 	[IFLA_IPTUN_ENCAP_LIMIT]	= { .type = NLA_U8 },
2202 	[IFLA_IPTUN_FLOWINFO]		= { .type = NLA_U32 },
2203 	[IFLA_IPTUN_FLAGS]		= { .type = NLA_U32 },
2204 	[IFLA_IPTUN_PROTO]		= { .type = NLA_U8 },
2205 	[IFLA_IPTUN_ENCAP_TYPE]		= { .type = NLA_U16 },
2206 	[IFLA_IPTUN_ENCAP_FLAGS]	= { .type = NLA_U16 },
2207 	[IFLA_IPTUN_ENCAP_SPORT]	= { .type = NLA_U16 },
2208 	[IFLA_IPTUN_ENCAP_DPORT]	= { .type = NLA_U16 },
2209 	[IFLA_IPTUN_COLLECT_METADATA]	= { .type = NLA_FLAG },
2210 	[IFLA_IPTUN_FWMARK]		= { .type = NLA_U32 },
2211 };
2212 
2213 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
2214 	.kind		= "ip6tnl",
2215 	.maxtype	= IFLA_IPTUN_MAX,
2216 	.policy		= ip6_tnl_policy,
2217 	.priv_size	= sizeof(struct ip6_tnl),
2218 	.setup		= ip6_tnl_dev_setup,
2219 	.validate	= ip6_tnl_validate,
2220 	.newlink	= ip6_tnl_newlink,
2221 	.changelink	= ip6_tnl_changelink,
2222 	.dellink	= ip6_tnl_dellink,
2223 	.get_size	= ip6_tnl_get_size,
2224 	.fill_info	= ip6_tnl_fill_info,
2225 	.get_link_net	= ip6_tnl_get_link_net,
2226 };
2227 
2228 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
2229 	.handler	= ip4ip6_rcv,
2230 	.err_handler	= ip4ip6_err,
2231 	.priority	=	1,
2232 };
2233 
2234 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
2235 	.handler	= ip6ip6_rcv,
2236 	.err_handler	= ip6ip6_err,
2237 	.priority	=	1,
2238 };
2239 
2240 static struct xfrm6_tunnel mplsip6_handler __read_mostly = {
2241 	.handler	= mplsip6_rcv,
2242 	.err_handler	= mplsip6_err,
2243 	.priority	=	1,
2244 };
2245 
2246 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list)
2247 {
2248 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2249 	struct net_device *dev, *aux;
2250 	int h;
2251 	struct ip6_tnl *t;
2252 
2253 	for_each_netdev_safe(net, dev, aux)
2254 		if (dev->rtnl_link_ops == &ip6_link_ops)
2255 			unregister_netdevice_queue(dev, list);
2256 
2257 	for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
2258 		t = rtnl_dereference(ip6n->tnls_r_l[h]);
2259 		while (t) {
2260 			/* If dev is in the same netns, it has already
2261 			 * been added to the list by the previous loop.
2262 			 */
2263 			if (!net_eq(dev_net(t->dev), net))
2264 				unregister_netdevice_queue(t->dev, list);
2265 			t = rtnl_dereference(t->next);
2266 		}
2267 	}
2268 }
2269 
2270 static int __net_init ip6_tnl_init_net(struct net *net)
2271 {
2272 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2273 	struct ip6_tnl *t = NULL;
2274 	int err;
2275 
2276 	ip6n->tnls[0] = ip6n->tnls_wc;
2277 	ip6n->tnls[1] = ip6n->tnls_r_l;
2278 
2279 	if (!net_has_fallback_tunnels(net))
2280 		return 0;
2281 	err = -ENOMEM;
2282 	ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
2283 					NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
2284 
2285 	if (!ip6n->fb_tnl_dev)
2286 		goto err_alloc_dev;
2287 	dev_net_set(ip6n->fb_tnl_dev, net);
2288 	ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
2289 	/* FB netdevice is special: we have one, and only one per netns.
2290 	 * Allowing to move it to another netns is clearly unsafe.
2291 	 */
2292 	ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
2293 
2294 	err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
2295 	if (err < 0)
2296 		goto err_register;
2297 
2298 	err = register_netdev(ip6n->fb_tnl_dev);
2299 	if (err < 0)
2300 		goto err_register;
2301 
2302 	t = netdev_priv(ip6n->fb_tnl_dev);
2303 
2304 	strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
2305 	return 0;
2306 
2307 err_register:
2308 	free_netdev(ip6n->fb_tnl_dev);
2309 err_alloc_dev:
2310 	return err;
2311 }
2312 
2313 static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list)
2314 {
2315 	struct net *net;
2316 	LIST_HEAD(list);
2317 
2318 	rtnl_lock();
2319 	list_for_each_entry(net, net_list, exit_list)
2320 		ip6_tnl_destroy_tunnels(net, &list);
2321 	unregister_netdevice_many(&list);
2322 	rtnl_unlock();
2323 }
2324 
2325 static struct pernet_operations ip6_tnl_net_ops = {
2326 	.init = ip6_tnl_init_net,
2327 	.exit_batch = ip6_tnl_exit_batch_net,
2328 	.id   = &ip6_tnl_net_id,
2329 	.size = sizeof(struct ip6_tnl_net),
2330 };
2331 
2332 /**
2333  * ip6_tunnel_init - register protocol and reserve needed resources
2334  *
2335  * Return: 0 on success
2336  **/
2337 
2338 static int __init ip6_tunnel_init(void)
2339 {
2340 	int  err;
2341 
2342 	if (!ipv6_mod_enabled())
2343 		return -EOPNOTSUPP;
2344 
2345 	err = register_pernet_device(&ip6_tnl_net_ops);
2346 	if (err < 0)
2347 		goto out_pernet;
2348 
2349 	err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
2350 	if (err < 0) {
2351 		pr_err("%s: can't register ip4ip6\n", __func__);
2352 		goto out_ip4ip6;
2353 	}
2354 
2355 	err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
2356 	if (err < 0) {
2357 		pr_err("%s: can't register ip6ip6\n", __func__);
2358 		goto out_ip6ip6;
2359 	}
2360 
2361 	if (ip6_tnl_mpls_supported()) {
2362 		err = xfrm6_tunnel_register(&mplsip6_handler, AF_MPLS);
2363 		if (err < 0) {
2364 			pr_err("%s: can't register mplsip6\n", __func__);
2365 			goto out_mplsip6;
2366 		}
2367 	}
2368 
2369 	err = rtnl_link_register(&ip6_link_ops);
2370 	if (err < 0)
2371 		goto rtnl_link_failed;
2372 
2373 	return 0;
2374 
2375 rtnl_link_failed:
2376 	if (ip6_tnl_mpls_supported())
2377 		xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS);
2378 out_mplsip6:
2379 	xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
2380 out_ip6ip6:
2381 	xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
2382 out_ip4ip6:
2383 	unregister_pernet_device(&ip6_tnl_net_ops);
2384 out_pernet:
2385 	return err;
2386 }
2387 
2388 /**
2389  * ip6_tunnel_cleanup - free resources and unregister protocol
2390  **/
2391 
2392 static void __exit ip6_tunnel_cleanup(void)
2393 {
2394 	rtnl_link_unregister(&ip6_link_ops);
2395 	if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
2396 		pr_info("%s: can't deregister ip4ip6\n", __func__);
2397 
2398 	if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
2399 		pr_info("%s: can't deregister ip6ip6\n", __func__);
2400 
2401 	if (ip6_tnl_mpls_supported() &&
2402 	    xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS))
2403 		pr_info("%s: can't deregister mplsip6\n", __func__);
2404 	unregister_pernet_device(&ip6_tnl_net_ops);
2405 }
2406 
2407 module_init(ip6_tunnel_init);
2408 module_exit(ip6_tunnel_cleanup);
2409