1 /*
2  *	IPv6 tunneling device
3  *	Linux INET6 implementation
4  *
5  *	Authors:
6  *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
7  *	Yasuyuki Kozakai	<kozakai@linux-ipv6.org>
8  *
9  *      Based on:
10  *      linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
11  *
12  *      RFC 2473
13  *
14  *	This program is free software; you can redistribute it and/or
15  *      modify it under the terms of the GNU General Public License
16  *      as published by the Free Software Foundation; either version
17  *      2 of the License, or (at your option) any later version.
18  *
19  */
20 
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/icmp.h>
29 #include <linux/if.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/icmpv6.h>
37 #include <linux/init.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/netfilter_ipv6.h>
41 #include <linux/slab.h>
42 #include <linux/hash.h>
43 #include <linux/etherdevice.h>
44 
45 #include <asm/uaccess.h>
46 #include <linux/atomic.h>
47 
48 #include <net/icmp.h>
49 #include <net/ip.h>
50 #include <net/ip_tunnels.h>
51 #include <net/ipv6.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
54 #include <net/ip6_tunnel.h>
55 #include <net/xfrm.h>
56 #include <net/dsfield.h>
57 #include <net/inet_ecn.h>
58 #include <net/net_namespace.h>
59 #include <net/netns/generic.h>
60 
61 MODULE_AUTHOR("Ville Nuorvala");
62 MODULE_DESCRIPTION("IPv6 tunneling device");
63 MODULE_LICENSE("GPL");
64 MODULE_ALIAS_RTNL_LINK("ip6tnl");
65 MODULE_ALIAS_NETDEV("ip6tnl0");
66 
67 #define HASH_SIZE_SHIFT  5
68 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
69 
70 static bool log_ecn_error = true;
71 module_param(log_ecn_error, bool, 0644);
72 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
73 
HASH(const struct in6_addr * addr1,const struct in6_addr * addr2)74 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
75 {
76 	u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
77 
78 	return hash_32(hash, HASH_SIZE_SHIFT);
79 }
80 
81 static int ip6_tnl_dev_init(struct net_device *dev);
82 static void ip6_tnl_dev_setup(struct net_device *dev);
83 static struct rtnl_link_ops ip6_link_ops __read_mostly;
84 
85 static int ip6_tnl_net_id __read_mostly;
86 struct ip6_tnl_net {
87 	/* the IPv6 tunnel fallback device */
88 	struct net_device *fb_tnl_dev;
89 	/* lists for storing tunnels in use */
90 	struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
91 	struct ip6_tnl __rcu *tnls_wc[1];
92 	struct ip6_tnl __rcu **tnls[2];
93 };
94 
ip6_get_stats(struct net_device * dev)95 static struct net_device_stats *ip6_get_stats(struct net_device *dev)
96 {
97 	struct pcpu_sw_netstats tmp, sum = { 0 };
98 	int i;
99 
100 	for_each_possible_cpu(i) {
101 		unsigned int start;
102 		const struct pcpu_sw_netstats *tstats =
103 						   per_cpu_ptr(dev->tstats, i);
104 
105 		do {
106 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
107 			tmp.rx_packets = tstats->rx_packets;
108 			tmp.rx_bytes = tstats->rx_bytes;
109 			tmp.tx_packets = tstats->tx_packets;
110 			tmp.tx_bytes =  tstats->tx_bytes;
111 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
112 
113 		sum.rx_packets += tmp.rx_packets;
114 		sum.rx_bytes   += tmp.rx_bytes;
115 		sum.tx_packets += tmp.tx_packets;
116 		sum.tx_bytes   += tmp.tx_bytes;
117 	}
118 	dev->stats.rx_packets = sum.rx_packets;
119 	dev->stats.rx_bytes   = sum.rx_bytes;
120 	dev->stats.tx_packets = sum.tx_packets;
121 	dev->stats.tx_bytes   = sum.tx_bytes;
122 	return &dev->stats;
123 }
124 
125 /*
126  * Locking : hash tables are protected by RCU and RTNL
127  */
128 
ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst * idst,struct dst_entry * dst)129 static void ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst,
130 				    struct dst_entry *dst)
131 {
132 	write_seqlock_bh(&idst->lock);
133 	dst_release(rcu_dereference_protected(
134 			    idst->dst,
135 			    lockdep_is_held(&idst->lock.lock)));
136 	if (dst) {
137 		dst_hold(dst);
138 		idst->cookie = rt6_get_cookie((struct rt6_info *)dst);
139 	} else {
140 		idst->cookie = 0;
141 	}
142 	rcu_assign_pointer(idst->dst, dst);
143 	write_sequnlock_bh(&idst->lock);
144 }
145 
ip6_tnl_dst_get(struct ip6_tnl * t)146 struct dst_entry *ip6_tnl_dst_get(struct ip6_tnl *t)
147 {
148 	struct ip6_tnl_dst *idst;
149 	struct dst_entry *dst;
150 	unsigned int seq;
151 	u32 cookie;
152 
153 	idst = raw_cpu_ptr(t->dst_cache);
154 
155 	rcu_read_lock();
156 	do {
157 		seq = read_seqbegin(&idst->lock);
158 		dst = rcu_dereference(idst->dst);
159 		cookie = idst->cookie;
160 	} while (read_seqretry(&idst->lock, seq));
161 
162 	if (dst && !atomic_inc_not_zero(&dst->__refcnt))
163 		dst = NULL;
164 	rcu_read_unlock();
165 
166 	if (dst && dst->obsolete && !dst->ops->check(dst, cookie)) {
167 		ip6_tnl_per_cpu_dst_set(idst, NULL);
168 		dst_release(dst);
169 		dst = NULL;
170 	}
171 	return dst;
172 }
173 EXPORT_SYMBOL_GPL(ip6_tnl_dst_get);
174 
ip6_tnl_dst_reset(struct ip6_tnl * t)175 void ip6_tnl_dst_reset(struct ip6_tnl *t)
176 {
177 	int i;
178 
179 	for_each_possible_cpu(i)
180 		ip6_tnl_per_cpu_dst_set(per_cpu_ptr(t->dst_cache, i), NULL);
181 }
182 EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
183 
ip6_tnl_dst_set(struct ip6_tnl * t,struct dst_entry * dst)184 void ip6_tnl_dst_set(struct ip6_tnl *t, struct dst_entry *dst)
185 {
186 	ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), dst);
187 
188 }
189 EXPORT_SYMBOL_GPL(ip6_tnl_dst_set);
190 
ip6_tnl_dst_destroy(struct ip6_tnl * t)191 void ip6_tnl_dst_destroy(struct ip6_tnl *t)
192 {
193 	if (!t->dst_cache)
194 		return;
195 
196 	ip6_tnl_dst_reset(t);
197 	free_percpu(t->dst_cache);
198 }
199 EXPORT_SYMBOL_GPL(ip6_tnl_dst_destroy);
200 
ip6_tnl_dst_init(struct ip6_tnl * t)201 int ip6_tnl_dst_init(struct ip6_tnl *t)
202 {
203 	int i;
204 
205 	t->dst_cache = alloc_percpu(struct ip6_tnl_dst);
206 	if (!t->dst_cache)
207 		return -ENOMEM;
208 
209 	for_each_possible_cpu(i)
210 		seqlock_init(&per_cpu_ptr(t->dst_cache, i)->lock);
211 
212 	return 0;
213 }
214 EXPORT_SYMBOL_GPL(ip6_tnl_dst_init);
215 
216 /**
217  * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
218  *   @remote: the address of the tunnel exit-point
219  *   @local: the address of the tunnel entry-point
220  *
221  * Return:
222  *   tunnel matching given end-points if found,
223  *   else fallback tunnel if its device is up,
224  *   else %NULL
225  **/
226 
227 #define for_each_ip6_tunnel_rcu(start) \
228 	for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
229 
230 static struct ip6_tnl *
ip6_tnl_lookup(struct net * net,const struct in6_addr * remote,const struct in6_addr * local)231 ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
232 {
233 	unsigned int hash = HASH(remote, local);
234 	struct ip6_tnl *t;
235 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
236 	struct in6_addr any;
237 
238 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
239 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
240 		    ipv6_addr_equal(remote, &t->parms.raddr) &&
241 		    (t->dev->flags & IFF_UP))
242 			return t;
243 	}
244 
245 	memset(&any, 0, sizeof(any));
246 	hash = HASH(&any, local);
247 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
248 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
249 		    (t->dev->flags & IFF_UP))
250 			return t;
251 	}
252 
253 	hash = HASH(remote, &any);
254 	for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
255 		if (ipv6_addr_equal(remote, &t->parms.raddr) &&
256 		    (t->dev->flags & IFF_UP))
257 			return t;
258 	}
259 
260 	t = rcu_dereference(ip6n->tnls_wc[0]);
261 	if (t && (t->dev->flags & IFF_UP))
262 		return t;
263 
264 	return NULL;
265 }
266 
267 /**
268  * ip6_tnl_bucket - get head of list matching given tunnel parameters
269  *   @p: parameters containing tunnel end-points
270  *
271  * Description:
272  *   ip6_tnl_bucket() returns the head of the list matching the
273  *   &struct in6_addr entries laddr and raddr in @p.
274  *
275  * Return: head of IPv6 tunnel list
276  **/
277 
278 static struct ip6_tnl __rcu **
ip6_tnl_bucket(struct ip6_tnl_net * ip6n,const struct __ip6_tnl_parm * p)279 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
280 {
281 	const struct in6_addr *remote = &p->raddr;
282 	const struct in6_addr *local = &p->laddr;
283 	unsigned int h = 0;
284 	int prio = 0;
285 
286 	if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
287 		prio = 1;
288 		h = HASH(remote, local);
289 	}
290 	return &ip6n->tnls[prio][h];
291 }
292 
293 /**
294  * ip6_tnl_link - add tunnel to hash table
295  *   @t: tunnel to be added
296  **/
297 
298 static void
ip6_tnl_link(struct ip6_tnl_net * ip6n,struct ip6_tnl * t)299 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
300 {
301 	struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
302 
303 	rcu_assign_pointer(t->next , rtnl_dereference(*tp));
304 	rcu_assign_pointer(*tp, t);
305 }
306 
307 /**
308  * ip6_tnl_unlink - remove tunnel from hash table
309  *   @t: tunnel to be removed
310  **/
311 
312 static void
ip6_tnl_unlink(struct ip6_tnl_net * ip6n,struct ip6_tnl * t)313 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
314 {
315 	struct ip6_tnl __rcu **tp;
316 	struct ip6_tnl *iter;
317 
318 	for (tp = ip6_tnl_bucket(ip6n, &t->parms);
319 	     (iter = rtnl_dereference(*tp)) != NULL;
320 	     tp = &iter->next) {
321 		if (t == iter) {
322 			rcu_assign_pointer(*tp, t->next);
323 			break;
324 		}
325 	}
326 }
327 
ip6_dev_free(struct net_device * dev)328 static void ip6_dev_free(struct net_device *dev)
329 {
330 	struct ip6_tnl *t = netdev_priv(dev);
331 
332 	ip6_tnl_dst_destroy(t);
333 	free_percpu(dev->tstats);
334 	free_netdev(dev);
335 }
336 
ip6_tnl_create2(struct net_device * dev)337 static int ip6_tnl_create2(struct net_device *dev)
338 {
339 	struct ip6_tnl *t = netdev_priv(dev);
340 	struct net *net = dev_net(dev);
341 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
342 	int err;
343 
344 	t = netdev_priv(dev);
345 
346 	dev->rtnl_link_ops = &ip6_link_ops;
347 	err = register_netdevice(dev);
348 	if (err < 0)
349 		goto out;
350 
351 	strcpy(t->parms.name, dev->name);
352 
353 	dev_hold(dev);
354 	ip6_tnl_link(ip6n, t);
355 	return 0;
356 
357 out:
358 	return err;
359 }
360 
361 /**
362  * ip6_tnl_create - create a new tunnel
363  *   @p: tunnel parameters
364  *   @pt: pointer to new tunnel
365  *
366  * Description:
367  *   Create tunnel matching given parameters.
368  *
369  * Return:
370  *   created tunnel or error pointer
371  **/
372 
ip6_tnl_create(struct net * net,struct __ip6_tnl_parm * p)373 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
374 {
375 	struct net_device *dev;
376 	struct ip6_tnl *t;
377 	char name[IFNAMSIZ];
378 	int err = -ENOMEM;
379 
380 	if (p->name[0])
381 		strlcpy(name, p->name, IFNAMSIZ);
382 	else
383 		sprintf(name, "ip6tnl%%d");
384 
385 	dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
386 			   ip6_tnl_dev_setup);
387 	if (!dev)
388 		goto failed;
389 
390 	dev_net_set(dev, net);
391 
392 	t = netdev_priv(dev);
393 	t->parms = *p;
394 	t->net = dev_net(dev);
395 	err = ip6_tnl_create2(dev);
396 	if (err < 0)
397 		goto failed_free;
398 
399 	return t;
400 
401 failed_free:
402 	ip6_dev_free(dev);
403 failed:
404 	return ERR_PTR(err);
405 }
406 
407 /**
408  * ip6_tnl_locate - find or create tunnel matching given parameters
409  *   @p: tunnel parameters
410  *   @create: != 0 if allowed to create new tunnel if no match found
411  *
412  * Description:
413  *   ip6_tnl_locate() first tries to locate an existing tunnel
414  *   based on @parms. If this is unsuccessful, but @create is set a new
415  *   tunnel device is created and registered for use.
416  *
417  * Return:
418  *   matching tunnel or error pointer
419  **/
420 
ip6_tnl_locate(struct net * net,struct __ip6_tnl_parm * p,int create)421 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
422 		struct __ip6_tnl_parm *p, int create)
423 {
424 	const struct in6_addr *remote = &p->raddr;
425 	const struct in6_addr *local = &p->laddr;
426 	struct ip6_tnl __rcu **tp;
427 	struct ip6_tnl *t;
428 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
429 
430 	for (tp = ip6_tnl_bucket(ip6n, p);
431 	     (t = rtnl_dereference(*tp)) != NULL;
432 	     tp = &t->next) {
433 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
434 		    ipv6_addr_equal(remote, &t->parms.raddr)) {
435 			if (create)
436 				return ERR_PTR(-EEXIST);
437 
438 			return t;
439 		}
440 	}
441 	if (!create)
442 		return ERR_PTR(-ENODEV);
443 	return ip6_tnl_create(net, p);
444 }
445 
446 /**
447  * ip6_tnl_dev_uninit - tunnel device uninitializer
448  *   @dev: the device to be destroyed
449  *
450  * Description:
451  *   ip6_tnl_dev_uninit() removes tunnel from its list
452  **/
453 
454 static void
ip6_tnl_dev_uninit(struct net_device * dev)455 ip6_tnl_dev_uninit(struct net_device *dev)
456 {
457 	struct ip6_tnl *t = netdev_priv(dev);
458 	struct net *net = t->net;
459 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
460 
461 	if (dev == ip6n->fb_tnl_dev)
462 		RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
463 	else
464 		ip6_tnl_unlink(ip6n, t);
465 	ip6_tnl_dst_reset(t);
466 	dev_put(dev);
467 }
468 
469 /**
470  * parse_tvl_tnl_enc_lim - handle encapsulation limit option
471  *   @skb: received socket buffer
472  *
473  * Return:
474  *   0 if none was found,
475  *   else index to encapsulation limit
476  **/
477 
ip6_tnl_parse_tlv_enc_lim(struct sk_buff * skb,__u8 * raw)478 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
479 {
480 	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
481 	__u8 nexthdr = ipv6h->nexthdr;
482 	__u16 off = sizeof(*ipv6h);
483 
484 	while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
485 		__u16 optlen = 0;
486 		struct ipv6_opt_hdr *hdr;
487 		if (raw + off + sizeof(*hdr) > skb->data &&
488 		    !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
489 			break;
490 
491 		hdr = (struct ipv6_opt_hdr *) (raw + off);
492 		if (nexthdr == NEXTHDR_FRAGMENT) {
493 			struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
494 			if (frag_hdr->frag_off)
495 				break;
496 			optlen = 8;
497 		} else if (nexthdr == NEXTHDR_AUTH) {
498 			optlen = (hdr->hdrlen + 2) << 2;
499 		} else {
500 			optlen = ipv6_optlen(hdr);
501 		}
502 		if (nexthdr == NEXTHDR_DEST) {
503 			__u16 i = off + 2;
504 			while (1) {
505 				struct ipv6_tlv_tnl_enc_lim *tel;
506 
507 				/* No more room for encapsulation limit */
508 				if (i + sizeof (*tel) > off + optlen)
509 					break;
510 
511 				tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
512 				/* return index of option if found and valid */
513 				if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
514 				    tel->length == 1)
515 					return i;
516 				/* else jump to next option */
517 				if (tel->type)
518 					i += tel->length + 2;
519 				else
520 					i++;
521 			}
522 		}
523 		nexthdr = hdr->nexthdr;
524 		off += optlen;
525 	}
526 	return 0;
527 }
528 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
529 
530 /**
531  * ip6_tnl_err - tunnel error handler
532  *
533  * Description:
534  *   ip6_tnl_err() should handle errors in the tunnel according
535  *   to the specifications in RFC 2473.
536  **/
537 
538 static int
ip6_tnl_err(struct sk_buff * skb,__u8 ipproto,struct inet6_skb_parm * opt,u8 * type,u8 * code,int * msg,__u32 * info,int offset)539 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
540 	    u8 *type, u8 *code, int *msg, __u32 *info, int offset)
541 {
542 	const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
543 	struct ip6_tnl *t;
544 	int rel_msg = 0;
545 	u8 rel_type = ICMPV6_DEST_UNREACH;
546 	u8 rel_code = ICMPV6_ADDR_UNREACH;
547 	u8 tproto;
548 	__u32 rel_info = 0;
549 	__u16 len;
550 	int err = -ENOENT;
551 
552 	/* If the packet doesn't contain the original IPv6 header we are
553 	   in trouble since we might need the source address for further
554 	   processing of the error. */
555 
556 	rcu_read_lock();
557 	t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
558 	if (!t)
559 		goto out;
560 
561 	tproto = ACCESS_ONCE(t->parms.proto);
562 	if (tproto != ipproto && tproto != 0)
563 		goto out;
564 
565 	err = 0;
566 
567 	switch (*type) {
568 		__u32 teli;
569 		struct ipv6_tlv_tnl_enc_lim *tel;
570 		__u32 mtu;
571 	case ICMPV6_DEST_UNREACH:
572 		net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
573 				    t->parms.name);
574 		rel_msg = 1;
575 		break;
576 	case ICMPV6_TIME_EXCEED:
577 		if ((*code) == ICMPV6_EXC_HOPLIMIT) {
578 			net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
579 					    t->parms.name);
580 			rel_msg = 1;
581 		}
582 		break;
583 	case ICMPV6_PARAMPROB:
584 		teli = 0;
585 		if ((*code) == ICMPV6_HDR_FIELD)
586 			teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
587 
588 		if (teli && teli == *info - 2) {
589 			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
590 			if (tel->encap_limit == 0) {
591 				net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
592 						    t->parms.name);
593 				rel_msg = 1;
594 			}
595 		} else {
596 			net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
597 					    t->parms.name);
598 		}
599 		break;
600 	case ICMPV6_PKT_TOOBIG:
601 		mtu = *info - offset;
602 		if (mtu < IPV6_MIN_MTU)
603 			mtu = IPV6_MIN_MTU;
604 		t->dev->mtu = mtu;
605 
606 		len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
607 		if (len > mtu) {
608 			rel_type = ICMPV6_PKT_TOOBIG;
609 			rel_code = 0;
610 			rel_info = mtu;
611 			rel_msg = 1;
612 		}
613 		break;
614 	}
615 
616 	*type = rel_type;
617 	*code = rel_code;
618 	*info = rel_info;
619 	*msg = rel_msg;
620 
621 out:
622 	rcu_read_unlock();
623 	return err;
624 }
625 
626 static int
ip4ip6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)627 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
628 	   u8 type, u8 code, int offset, __be32 info)
629 {
630 	int rel_msg = 0;
631 	u8 rel_type = type;
632 	u8 rel_code = code;
633 	__u32 rel_info = ntohl(info);
634 	int err;
635 	struct sk_buff *skb2;
636 	const struct iphdr *eiph;
637 	struct rtable *rt;
638 	struct flowi4 fl4;
639 
640 	err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
641 			  &rel_msg, &rel_info, offset);
642 	if (err < 0)
643 		return err;
644 
645 	if (rel_msg == 0)
646 		return 0;
647 
648 	switch (rel_type) {
649 	case ICMPV6_DEST_UNREACH:
650 		if (rel_code != ICMPV6_ADDR_UNREACH)
651 			return 0;
652 		rel_type = ICMP_DEST_UNREACH;
653 		rel_code = ICMP_HOST_UNREACH;
654 		break;
655 	case ICMPV6_PKT_TOOBIG:
656 		if (rel_code != 0)
657 			return 0;
658 		rel_type = ICMP_DEST_UNREACH;
659 		rel_code = ICMP_FRAG_NEEDED;
660 		break;
661 	case NDISC_REDIRECT:
662 		rel_type = ICMP_REDIRECT;
663 		rel_code = ICMP_REDIR_HOST;
664 	default:
665 		return 0;
666 	}
667 
668 	if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
669 		return 0;
670 
671 	skb2 = skb_clone(skb, GFP_ATOMIC);
672 	if (!skb2)
673 		return 0;
674 
675 	skb_dst_drop(skb2);
676 
677 	skb_pull(skb2, offset);
678 	skb_reset_network_header(skb2);
679 	eiph = ip_hdr(skb2);
680 
681 	/* Try to guess incoming interface */
682 	rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
683 				   eiph->saddr, 0,
684 				   0, 0,
685 				   IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
686 	if (IS_ERR(rt))
687 		goto out;
688 
689 	skb2->dev = rt->dst.dev;
690 
691 	/* route "incoming" packet */
692 	if (rt->rt_flags & RTCF_LOCAL) {
693 		ip_rt_put(rt);
694 		rt = NULL;
695 		rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
696 					   eiph->daddr, eiph->saddr,
697 					   0, 0,
698 					   IPPROTO_IPIP,
699 					   RT_TOS(eiph->tos), 0);
700 		if (IS_ERR(rt) ||
701 		    rt->dst.dev->type != ARPHRD_TUNNEL) {
702 			if (!IS_ERR(rt))
703 				ip_rt_put(rt);
704 			goto out;
705 		}
706 		skb_dst_set(skb2, &rt->dst);
707 	} else {
708 		ip_rt_put(rt);
709 		if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
710 				   skb2->dev) ||
711 		    skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
712 			goto out;
713 	}
714 
715 	/* change mtu on this route */
716 	if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
717 		if (rel_info > dst_mtu(skb_dst(skb2)))
718 			goto out;
719 
720 		skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
721 	}
722 	if (rel_type == ICMP_REDIRECT)
723 		skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
724 
725 	icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
726 
727 out:
728 	kfree_skb(skb2);
729 	return 0;
730 }
731 
732 static int
ip6ip6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)733 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
734 	   u8 type, u8 code, int offset, __be32 info)
735 {
736 	int rel_msg = 0;
737 	u8 rel_type = type;
738 	u8 rel_code = code;
739 	__u32 rel_info = ntohl(info);
740 	int err;
741 
742 	err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
743 			  &rel_msg, &rel_info, offset);
744 	if (err < 0)
745 		return err;
746 
747 	if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
748 		struct rt6_info *rt;
749 		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
750 
751 		if (!skb2)
752 			return 0;
753 
754 		skb_dst_drop(skb2);
755 		skb_pull(skb2, offset);
756 		skb_reset_network_header(skb2);
757 
758 		/* Try to guess incoming interface */
759 		rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
760 				NULL, 0, 0);
761 
762 		if (rt && rt->dst.dev)
763 			skb2->dev = rt->dst.dev;
764 
765 		icmpv6_send(skb2, rel_type, rel_code, rel_info);
766 
767 		ip6_rt_put(rt);
768 
769 		kfree_skb(skb2);
770 	}
771 
772 	return 0;
773 }
774 
ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl * t,const struct ipv6hdr * ipv6h,struct sk_buff * skb)775 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
776 				       const struct ipv6hdr *ipv6h,
777 				       struct sk_buff *skb)
778 {
779 	__u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
780 
781 	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
782 		ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
783 
784 	return IP6_ECN_decapsulate(ipv6h, skb);
785 }
786 
ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl * t,const struct ipv6hdr * ipv6h,struct sk_buff * skb)787 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
788 				       const struct ipv6hdr *ipv6h,
789 				       struct sk_buff *skb)
790 {
791 	if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
792 		ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
793 
794 	return IP6_ECN_decapsulate(ipv6h, skb);
795 }
796 
ip6_tnl_get_cap(struct ip6_tnl * t,const struct in6_addr * laddr,const struct in6_addr * raddr)797 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
798 			     const struct in6_addr *laddr,
799 			     const struct in6_addr *raddr)
800 {
801 	struct __ip6_tnl_parm *p = &t->parms;
802 	int ltype = ipv6_addr_type(laddr);
803 	int rtype = ipv6_addr_type(raddr);
804 	__u32 flags = 0;
805 
806 	if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
807 		flags = IP6_TNL_F_CAP_PER_PACKET;
808 	} else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
809 		   rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
810 		   !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
811 		   (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
812 		if (ltype&IPV6_ADDR_UNICAST)
813 			flags |= IP6_TNL_F_CAP_XMIT;
814 		if (rtype&IPV6_ADDR_UNICAST)
815 			flags |= IP6_TNL_F_CAP_RCV;
816 	}
817 	return flags;
818 }
819 EXPORT_SYMBOL(ip6_tnl_get_cap);
820 
821 /* called with rcu_read_lock() */
ip6_tnl_rcv_ctl(struct ip6_tnl * t,const struct in6_addr * laddr,const struct in6_addr * raddr)822 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
823 				  const struct in6_addr *laddr,
824 				  const struct in6_addr *raddr)
825 {
826 	struct __ip6_tnl_parm *p = &t->parms;
827 	int ret = 0;
828 	struct net *net = t->net;
829 
830 	if ((p->flags & IP6_TNL_F_CAP_RCV) ||
831 	    ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
832 	     (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
833 		struct net_device *ldev = NULL;
834 
835 		if (p->link)
836 			ldev = dev_get_by_index_rcu(net, p->link);
837 
838 		if ((ipv6_addr_is_multicast(laddr) ||
839 		     likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
840 		    likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
841 			ret = 1;
842 	}
843 	return ret;
844 }
845 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
846 
847 /**
848  * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
849  *   @skb: received socket buffer
850  *   @protocol: ethernet protocol ID
851  *   @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
852  *
853  * Return: 0
854  **/
855 
ip6_tnl_rcv(struct sk_buff * skb,__u16 protocol,__u8 ipproto,int (* dscp_ecn_decapsulate)(const struct ip6_tnl * t,const struct ipv6hdr * ipv6h,struct sk_buff * skb))856 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
857 		       __u8 ipproto,
858 		       int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
859 						   const struct ipv6hdr *ipv6h,
860 						   struct sk_buff *skb))
861 {
862 	struct ip6_tnl *t;
863 	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
864 	u8 tproto;
865 	int err;
866 
867 	rcu_read_lock();
868 	t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
869 	if (t) {
870 		struct pcpu_sw_netstats *tstats;
871 
872 		tproto = ACCESS_ONCE(t->parms.proto);
873 		if (tproto != ipproto && tproto != 0) {
874 			rcu_read_unlock();
875 			goto discard;
876 		}
877 
878 		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
879 			rcu_read_unlock();
880 			goto discard;
881 		}
882 
883 		if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
884 			t->dev->stats.rx_dropped++;
885 			rcu_read_unlock();
886 			goto discard;
887 		}
888 		skb->mac_header = skb->network_header;
889 		skb_reset_network_header(skb);
890 		skb->protocol = htons(protocol);
891 		memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
892 
893 		__skb_tunnel_rx(skb, t->dev, t->net);
894 
895 		err = dscp_ecn_decapsulate(t, ipv6h, skb);
896 		if (unlikely(err)) {
897 			if (log_ecn_error)
898 				net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
899 						     &ipv6h->saddr,
900 						     ipv6_get_dsfield(ipv6h));
901 			if (err > 1) {
902 				++t->dev->stats.rx_frame_errors;
903 				++t->dev->stats.rx_errors;
904 				rcu_read_unlock();
905 				goto discard;
906 			}
907 		}
908 
909 		tstats = this_cpu_ptr(t->dev->tstats);
910 		u64_stats_update_begin(&tstats->syncp);
911 		tstats->rx_packets++;
912 		tstats->rx_bytes += skb->len;
913 		u64_stats_update_end(&tstats->syncp);
914 
915 		netif_rx(skb);
916 
917 		rcu_read_unlock();
918 		return 0;
919 	}
920 	rcu_read_unlock();
921 	return 1;
922 
923 discard:
924 	kfree_skb(skb);
925 	return 0;
926 }
927 
ip4ip6_rcv(struct sk_buff * skb)928 static int ip4ip6_rcv(struct sk_buff *skb)
929 {
930 	return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
931 			   ip4ip6_dscp_ecn_decapsulate);
932 }
933 
ip6ip6_rcv(struct sk_buff * skb)934 static int ip6ip6_rcv(struct sk_buff *skb)
935 {
936 	return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
937 			   ip6ip6_dscp_ecn_decapsulate);
938 }
939 
940 struct ipv6_tel_txoption {
941 	struct ipv6_txoptions ops;
942 	__u8 dst_opt[8];
943 };
944 
init_tel_txopt(struct ipv6_tel_txoption * opt,__u8 encap_limit)945 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
946 {
947 	memset(opt, 0, sizeof(struct ipv6_tel_txoption));
948 
949 	opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
950 	opt->dst_opt[3] = 1;
951 	opt->dst_opt[4] = encap_limit;
952 	opt->dst_opt[5] = IPV6_TLV_PADN;
953 	opt->dst_opt[6] = 1;
954 
955 	opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
956 	opt->ops.opt_nflen = 8;
957 }
958 
959 /**
960  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
961  *   @t: the outgoing tunnel device
962  *   @hdr: IPv6 header from the incoming packet
963  *
964  * Description:
965  *   Avoid trivial tunneling loop by checking that tunnel exit-point
966  *   doesn't match source of incoming packet.
967  *
968  * Return:
969  *   1 if conflict,
970  *   0 else
971  **/
972 
973 static inline bool
ip6_tnl_addr_conflict(const struct ip6_tnl * t,const struct ipv6hdr * hdr)974 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
975 {
976 	return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
977 }
978 
ip6_tnl_xmit_ctl(struct ip6_tnl * t,const struct in6_addr * laddr,const struct in6_addr * raddr)979 int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
980 		     const struct in6_addr *laddr,
981 		     const struct in6_addr *raddr)
982 {
983 	struct __ip6_tnl_parm *p = &t->parms;
984 	int ret = 0;
985 	struct net *net = t->net;
986 
987 	if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
988 	    ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
989 	     (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
990 		struct net_device *ldev = NULL;
991 
992 		rcu_read_lock();
993 		if (p->link)
994 			ldev = dev_get_by_index_rcu(net, p->link);
995 
996 		if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
997 			pr_warn("%s xmit: Local address not yet configured!\n",
998 				p->name);
999 		else if (!ipv6_addr_is_multicast(raddr) &&
1000 			 unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
1001 			pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1002 				p->name);
1003 		else
1004 			ret = 1;
1005 		rcu_read_unlock();
1006 	}
1007 	return ret;
1008 }
1009 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1010 
1011 /**
1012  * ip6_tnl_xmit2 - encapsulate packet and send
1013  *   @skb: the outgoing socket buffer
1014  *   @dev: the outgoing tunnel device
1015  *   @dsfield: dscp code for outer header
1016  *   @fl: flow of tunneled packet
1017  *   @encap_limit: encapsulation limit
1018  *   @pmtu: Path MTU is stored if packet is too big
1019  *
1020  * Description:
1021  *   Build new header and do some sanity checks on the packet before sending
1022  *   it.
1023  *
1024  * Return:
1025  *   0 on success
1026  *   -1 fail
1027  *   %-EMSGSIZE message too big. return mtu in this case.
1028  **/
1029 
ip6_tnl_xmit2(struct sk_buff * skb,struct net_device * dev,__u8 dsfield,struct flowi6 * fl6,int encap_limit,__u32 * pmtu)1030 static int ip6_tnl_xmit2(struct sk_buff *skb,
1031 			 struct net_device *dev,
1032 			 __u8 dsfield,
1033 			 struct flowi6 *fl6,
1034 			 int encap_limit,
1035 			 __u32 *pmtu)
1036 {
1037 	struct ip6_tnl *t = netdev_priv(dev);
1038 	struct net *net = t->net;
1039 	struct net_device_stats *stats = &t->dev->stats;
1040 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1041 	struct ipv6_tel_txoption opt;
1042 	struct dst_entry *dst = NULL, *ndst = NULL;
1043 	struct net_device *tdev;
1044 	int mtu;
1045 	unsigned int max_headroom = sizeof(struct ipv6hdr);
1046 	u8 proto;
1047 	int err = -1;
1048 
1049 	/* NBMA tunnel */
1050 	if (ipv6_addr_any(&t->parms.raddr)) {
1051 		struct in6_addr *addr6;
1052 		struct neighbour *neigh;
1053 		int addr_type;
1054 
1055 		if (!skb_dst(skb))
1056 			goto tx_err_link_failure;
1057 
1058 		neigh = dst_neigh_lookup(skb_dst(skb),
1059 					 &ipv6_hdr(skb)->daddr);
1060 		if (!neigh)
1061 			goto tx_err_link_failure;
1062 
1063 		addr6 = (struct in6_addr *)&neigh->primary_key;
1064 		addr_type = ipv6_addr_type(addr6);
1065 
1066 		if (addr_type == IPV6_ADDR_ANY)
1067 			addr6 = &ipv6_hdr(skb)->daddr;
1068 
1069 		memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1070 		neigh_release(neigh);
1071 	} else if (!fl6->flowi6_mark)
1072 		dst = ip6_tnl_dst_get(t);
1073 
1074 	if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1075 		goto tx_err_link_failure;
1076 
1077 	if (!dst) {
1078 		dst = ip6_route_output(net, NULL, fl6);
1079 
1080 		if (dst->error)
1081 			goto tx_err_link_failure;
1082 		dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1083 		if (IS_ERR(dst)) {
1084 			err = PTR_ERR(dst);
1085 			dst = NULL;
1086 			goto tx_err_link_failure;
1087 		}
1088 		ndst = dst;
1089 	}
1090 
1091 	tdev = dst->dev;
1092 
1093 	if (tdev == dev) {
1094 		stats->collisions++;
1095 		net_warn_ratelimited("%s: Local routing loop detected!\n",
1096 				     t->parms.name);
1097 		goto tx_err_dst_release;
1098 	}
1099 	mtu = dst_mtu(dst) - sizeof(*ipv6h);
1100 	if (encap_limit >= 0) {
1101 		max_headroom += 8;
1102 		mtu -= 8;
1103 	}
1104 	if (mtu < IPV6_MIN_MTU)
1105 		mtu = IPV6_MIN_MTU;
1106 	if (skb_dst(skb))
1107 		skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1108 	if (skb->len > mtu) {
1109 		*pmtu = mtu;
1110 		err = -EMSGSIZE;
1111 		goto tx_err_dst_release;
1112 	}
1113 
1114 	skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1115 
1116 	/*
1117 	 * Okay, now see if we can stuff it in the buffer as-is.
1118 	 */
1119 	max_headroom += LL_RESERVED_SPACE(tdev);
1120 
1121 	if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1122 	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1123 		struct sk_buff *new_skb;
1124 
1125 		new_skb = skb_realloc_headroom(skb, max_headroom);
1126 		if (!new_skb)
1127 			goto tx_err_dst_release;
1128 
1129 		if (skb->sk)
1130 			skb_set_owner_w(new_skb, skb->sk);
1131 		consume_skb(skb);
1132 		skb = new_skb;
1133 	}
1134 
1135 	if (!fl6->flowi6_mark && ndst)
1136 		ip6_tnl_dst_set(t, ndst);
1137 	skb_dst_set(skb, dst);
1138 
1139 	skb->transport_header = skb->network_header;
1140 
1141 	proto = fl6->flowi6_proto;
1142 	if (encap_limit >= 0) {
1143 		init_tel_txopt(&opt, encap_limit);
1144 		ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1145 	}
1146 
1147 	if (likely(!skb->encapsulation)) {
1148 		skb_reset_inner_headers(skb);
1149 		skb->encapsulation = 1;
1150 	}
1151 
1152 	skb_push(skb, sizeof(struct ipv6hdr));
1153 	skb_reset_network_header(skb);
1154 	ipv6h = ipv6_hdr(skb);
1155 	ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
1156 		     ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1157 	ipv6h->hop_limit = t->parms.hop_limit;
1158 	ipv6h->nexthdr = proto;
1159 	ipv6h->saddr = fl6->saddr;
1160 	ipv6h->daddr = fl6->daddr;
1161 	ip6tunnel_xmit(NULL, skb, dev);
1162 	return 0;
1163 tx_err_link_failure:
1164 	stats->tx_carrier_errors++;
1165 	dst_link_failure(skb);
1166 tx_err_dst_release:
1167 	dst_release(dst);
1168 	return err;
1169 }
1170 
1171 static inline int
ip4ip6_tnl_xmit(struct sk_buff * skb,struct net_device * dev)1172 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1173 {
1174 	struct ip6_tnl *t = netdev_priv(dev);
1175 	const struct iphdr  *iph = ip_hdr(skb);
1176 	int encap_limit = -1;
1177 	struct flowi6 fl6;
1178 	__u8 dsfield;
1179 	__u32 mtu;
1180 	u8 tproto;
1181 	int err;
1182 
1183 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1184 
1185 	tproto = ACCESS_ONCE(t->parms.proto);
1186 	if (tproto != IPPROTO_IPIP && tproto != 0)
1187 		return -1;
1188 
1189 	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1190 		encap_limit = t->parms.encap_limit;
1191 
1192 	memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1193 	fl6.flowi6_proto = IPPROTO_IPIP;
1194 
1195 	dsfield = ipv4_get_dsfield(iph);
1196 
1197 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1198 		fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1199 					  & IPV6_TCLASS_MASK;
1200 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1201 		fl6.flowi6_mark = skb->mark;
1202 
1203 	err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1204 	if (err != 0) {
1205 		/* XXX: send ICMP error even if DF is not set. */
1206 		if (err == -EMSGSIZE)
1207 			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1208 				  htonl(mtu));
1209 		return -1;
1210 	}
1211 
1212 	return 0;
1213 }
1214 
1215 static inline int
ip6ip6_tnl_xmit(struct sk_buff * skb,struct net_device * dev)1216 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1217 {
1218 	struct ip6_tnl *t = netdev_priv(dev);
1219 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1220 	int encap_limit = -1;
1221 	__u16 offset;
1222 	struct flowi6 fl6;
1223 	__u8 dsfield;
1224 	__u32 mtu;
1225 	u8 tproto;
1226 	int err;
1227 
1228 	tproto = ACCESS_ONCE(t->parms.proto);
1229 	if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1230 	    ip6_tnl_addr_conflict(t, ipv6h))
1231 		return -1;
1232 
1233 	offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1234 	if (offset > 0) {
1235 		struct ipv6_tlv_tnl_enc_lim *tel;
1236 		tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1237 		if (tel->encap_limit == 0) {
1238 			icmpv6_send(skb, ICMPV6_PARAMPROB,
1239 				    ICMPV6_HDR_FIELD, offset + 2);
1240 			return -1;
1241 		}
1242 		encap_limit = tel->encap_limit - 1;
1243 	} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1244 		encap_limit = t->parms.encap_limit;
1245 
1246 	memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1247 	fl6.flowi6_proto = IPPROTO_IPV6;
1248 
1249 	dsfield = ipv6_get_dsfield(ipv6h);
1250 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1251 		fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1252 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1253 		fl6.flowlabel |= ip6_flowlabel(ipv6h);
1254 	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1255 		fl6.flowi6_mark = skb->mark;
1256 
1257 	err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1258 	if (err != 0) {
1259 		if (err == -EMSGSIZE)
1260 			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1261 		return -1;
1262 	}
1263 
1264 	return 0;
1265 }
1266 
1267 static netdev_tx_t
ip6_tnl_xmit(struct sk_buff * skb,struct net_device * dev)1268 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1269 {
1270 	struct ip6_tnl *t = netdev_priv(dev);
1271 	struct net_device_stats *stats = &t->dev->stats;
1272 	int ret;
1273 
1274 	switch (skb->protocol) {
1275 	case htons(ETH_P_IP):
1276 		ret = ip4ip6_tnl_xmit(skb, dev);
1277 		break;
1278 	case htons(ETH_P_IPV6):
1279 		ret = ip6ip6_tnl_xmit(skb, dev);
1280 		break;
1281 	default:
1282 		goto tx_err;
1283 	}
1284 
1285 	if (ret < 0)
1286 		goto tx_err;
1287 
1288 	return NETDEV_TX_OK;
1289 
1290 tx_err:
1291 	stats->tx_errors++;
1292 	stats->tx_dropped++;
1293 	kfree_skb(skb);
1294 	return NETDEV_TX_OK;
1295 }
1296 
ip6_tnl_link_config(struct ip6_tnl * t)1297 static void ip6_tnl_link_config(struct ip6_tnl *t)
1298 {
1299 	struct net_device *dev = t->dev;
1300 	struct __ip6_tnl_parm *p = &t->parms;
1301 	struct flowi6 *fl6 = &t->fl.u.ip6;
1302 
1303 	memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1304 	memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1305 
1306 	/* Set up flowi template */
1307 	fl6->saddr = p->laddr;
1308 	fl6->daddr = p->raddr;
1309 	fl6->flowi6_oif = p->link;
1310 	fl6->flowlabel = 0;
1311 
1312 	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1313 		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1314 	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1315 		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1316 
1317 	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1318 	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1319 
1320 	if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1321 		dev->flags |= IFF_POINTOPOINT;
1322 	else
1323 		dev->flags &= ~IFF_POINTOPOINT;
1324 
1325 	if (p->flags & IP6_TNL_F_CAP_XMIT) {
1326 		int strict = (ipv6_addr_type(&p->raddr) &
1327 			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1328 
1329 		struct rt6_info *rt = rt6_lookup(t->net,
1330 						 &p->raddr, &p->laddr,
1331 						 p->link, strict);
1332 
1333 		if (!rt)
1334 			return;
1335 
1336 		if (rt->dst.dev) {
1337 			dev->hard_header_len = rt->dst.dev->hard_header_len +
1338 				sizeof(struct ipv6hdr);
1339 
1340 			dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
1341 			if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1342 				dev->mtu -= 8;
1343 
1344 			if (dev->mtu < IPV6_MIN_MTU)
1345 				dev->mtu = IPV6_MIN_MTU;
1346 		}
1347 		ip6_rt_put(rt);
1348 	}
1349 }
1350 
1351 /**
1352  * ip6_tnl_change - update the tunnel parameters
1353  *   @t: tunnel to be changed
1354  *   @p: tunnel configuration parameters
1355  *
1356  * Description:
1357  *   ip6_tnl_change() updates the tunnel parameters
1358  **/
1359 
1360 static int
ip6_tnl_change(struct ip6_tnl * t,const struct __ip6_tnl_parm * p)1361 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1362 {
1363 	t->parms.laddr = p->laddr;
1364 	t->parms.raddr = p->raddr;
1365 	t->parms.flags = p->flags;
1366 	t->parms.hop_limit = p->hop_limit;
1367 	t->parms.encap_limit = p->encap_limit;
1368 	t->parms.flowinfo = p->flowinfo;
1369 	t->parms.link = p->link;
1370 	t->parms.proto = p->proto;
1371 	ip6_tnl_dst_reset(t);
1372 	ip6_tnl_link_config(t);
1373 	return 0;
1374 }
1375 
ip6_tnl_update(struct ip6_tnl * t,struct __ip6_tnl_parm * p)1376 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1377 {
1378 	struct net *net = t->net;
1379 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1380 	int err;
1381 
1382 	ip6_tnl_unlink(ip6n, t);
1383 	synchronize_net();
1384 	err = ip6_tnl_change(t, p);
1385 	ip6_tnl_link(ip6n, t);
1386 	netdev_state_change(t->dev);
1387 	return err;
1388 }
1389 
ip6_tnl0_update(struct ip6_tnl * t,struct __ip6_tnl_parm * p)1390 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1391 {
1392 	/* for default tnl0 device allow to change only the proto */
1393 	t->parms.proto = p->proto;
1394 	netdev_state_change(t->dev);
1395 	return 0;
1396 }
1397 
1398 static void
ip6_tnl_parm_from_user(struct __ip6_tnl_parm * p,const struct ip6_tnl_parm * u)1399 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1400 {
1401 	p->laddr = u->laddr;
1402 	p->raddr = u->raddr;
1403 	p->flags = u->flags;
1404 	p->hop_limit = u->hop_limit;
1405 	p->encap_limit = u->encap_limit;
1406 	p->flowinfo = u->flowinfo;
1407 	p->link = u->link;
1408 	p->proto = u->proto;
1409 	memcpy(p->name, u->name, sizeof(u->name));
1410 }
1411 
1412 static void
ip6_tnl_parm_to_user(struct ip6_tnl_parm * u,const struct __ip6_tnl_parm * p)1413 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1414 {
1415 	u->laddr = p->laddr;
1416 	u->raddr = p->raddr;
1417 	u->flags = p->flags;
1418 	u->hop_limit = p->hop_limit;
1419 	u->encap_limit = p->encap_limit;
1420 	u->flowinfo = p->flowinfo;
1421 	u->link = p->link;
1422 	u->proto = p->proto;
1423 	memcpy(u->name, p->name, sizeof(u->name));
1424 }
1425 
1426 /**
1427  * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1428  *   @dev: virtual device associated with tunnel
1429  *   @ifr: parameters passed from userspace
1430  *   @cmd: command to be performed
1431  *
1432  * Description:
1433  *   ip6_tnl_ioctl() is used for managing IPv6 tunnels
1434  *   from userspace.
1435  *
1436  *   The possible commands are the following:
1437  *     %SIOCGETTUNNEL: get tunnel parameters for device
1438  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1439  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
1440  *     %SIOCDELTUNNEL: delete tunnel
1441  *
1442  *   The fallback device "ip6tnl0", created during module
1443  *   initialization, can be used for creating other tunnel devices.
1444  *
1445  * Return:
1446  *   0 on success,
1447  *   %-EFAULT if unable to copy data to or from userspace,
1448  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
1449  *   %-EINVAL if passed tunnel parameters are invalid,
1450  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
1451  *   %-ENODEV if attempting to change or delete a nonexisting device
1452  **/
1453 
1454 static int
ip6_tnl_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1455 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1456 {
1457 	int err = 0;
1458 	struct ip6_tnl_parm p;
1459 	struct __ip6_tnl_parm p1;
1460 	struct ip6_tnl *t = netdev_priv(dev);
1461 	struct net *net = t->net;
1462 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1463 
1464 	switch (cmd) {
1465 	case SIOCGETTUNNEL:
1466 		if (dev == ip6n->fb_tnl_dev) {
1467 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1468 				err = -EFAULT;
1469 				break;
1470 			}
1471 			ip6_tnl_parm_from_user(&p1, &p);
1472 			t = ip6_tnl_locate(net, &p1, 0);
1473 			if (IS_ERR(t))
1474 				t = netdev_priv(dev);
1475 		} else {
1476 			memset(&p, 0, sizeof(p));
1477 		}
1478 		ip6_tnl_parm_to_user(&p, &t->parms);
1479 		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1480 			err = -EFAULT;
1481 		}
1482 		break;
1483 	case SIOCADDTUNNEL:
1484 	case SIOCCHGTUNNEL:
1485 		err = -EPERM;
1486 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1487 			break;
1488 		err = -EFAULT;
1489 		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1490 			break;
1491 		err = -EINVAL;
1492 		if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1493 		    p.proto != 0)
1494 			break;
1495 		ip6_tnl_parm_from_user(&p1, &p);
1496 		t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1497 		if (cmd == SIOCCHGTUNNEL) {
1498 			if (!IS_ERR(t)) {
1499 				if (t->dev != dev) {
1500 					err = -EEXIST;
1501 					break;
1502 				}
1503 			} else
1504 				t = netdev_priv(dev);
1505 			if (dev == ip6n->fb_tnl_dev)
1506 				err = ip6_tnl0_update(t, &p1);
1507 			else
1508 				err = ip6_tnl_update(t, &p1);
1509 		}
1510 		if (!IS_ERR(t)) {
1511 			err = 0;
1512 			ip6_tnl_parm_to_user(&p, &t->parms);
1513 			if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1514 				err = -EFAULT;
1515 
1516 		} else {
1517 			err = PTR_ERR(t);
1518 		}
1519 		break;
1520 	case SIOCDELTUNNEL:
1521 		err = -EPERM;
1522 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1523 			break;
1524 
1525 		if (dev == ip6n->fb_tnl_dev) {
1526 			err = -EFAULT;
1527 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1528 				break;
1529 			err = -ENOENT;
1530 			ip6_tnl_parm_from_user(&p1, &p);
1531 			t = ip6_tnl_locate(net, &p1, 0);
1532 			if (IS_ERR(t))
1533 				break;
1534 			err = -EPERM;
1535 			if (t->dev == ip6n->fb_tnl_dev)
1536 				break;
1537 			dev = t->dev;
1538 		}
1539 		err = 0;
1540 		unregister_netdevice(dev);
1541 		break;
1542 	default:
1543 		err = -EINVAL;
1544 	}
1545 	return err;
1546 }
1547 
1548 /**
1549  * ip6_tnl_change_mtu - change mtu manually for tunnel device
1550  *   @dev: virtual device associated with tunnel
1551  *   @new_mtu: the new mtu
1552  *
1553  * Return:
1554  *   0 on success,
1555  *   %-EINVAL if mtu too small
1556  **/
1557 
1558 static int
ip6_tnl_change_mtu(struct net_device * dev,int new_mtu)1559 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1560 {
1561 	struct ip6_tnl *tnl = netdev_priv(dev);
1562 
1563 	if (tnl->parms.proto == IPPROTO_IPIP) {
1564 		if (new_mtu < 68)
1565 			return -EINVAL;
1566 	} else {
1567 		if (new_mtu < IPV6_MIN_MTU)
1568 			return -EINVAL;
1569 	}
1570 	if (new_mtu > 0xFFF8 - dev->hard_header_len)
1571 		return -EINVAL;
1572 	dev->mtu = new_mtu;
1573 	return 0;
1574 }
1575 
ip6_tnl_get_iflink(const struct net_device * dev)1576 int ip6_tnl_get_iflink(const struct net_device *dev)
1577 {
1578 	struct ip6_tnl *t = netdev_priv(dev);
1579 
1580 	return t->parms.link;
1581 }
1582 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1583 
1584 static const struct net_device_ops ip6_tnl_netdev_ops = {
1585 	.ndo_init	= ip6_tnl_dev_init,
1586 	.ndo_uninit	= ip6_tnl_dev_uninit,
1587 	.ndo_start_xmit = ip6_tnl_xmit,
1588 	.ndo_do_ioctl	= ip6_tnl_ioctl,
1589 	.ndo_change_mtu = ip6_tnl_change_mtu,
1590 	.ndo_get_stats	= ip6_get_stats,
1591 	.ndo_get_iflink = ip6_tnl_get_iflink,
1592 };
1593 
1594 
1595 /**
1596  * ip6_tnl_dev_setup - setup virtual tunnel device
1597  *   @dev: virtual device associated with tunnel
1598  *
1599  * Description:
1600  *   Initialize function pointers and device parameters
1601  **/
1602 
ip6_tnl_dev_setup(struct net_device * dev)1603 static void ip6_tnl_dev_setup(struct net_device *dev)
1604 {
1605 	struct ip6_tnl *t;
1606 
1607 	dev->netdev_ops = &ip6_tnl_netdev_ops;
1608 	dev->destructor = ip6_dev_free;
1609 
1610 	dev->type = ARPHRD_TUNNEL6;
1611 	dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1612 	dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
1613 	t = netdev_priv(dev);
1614 	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1615 		dev->mtu -= 8;
1616 	dev->flags |= IFF_NOARP;
1617 	dev->addr_len = sizeof(struct in6_addr);
1618 	netif_keep_dst(dev);
1619 	/* This perm addr will be used as interface identifier by IPv6 */
1620 	dev->addr_assign_type = NET_ADDR_RANDOM;
1621 	eth_random_addr(dev->perm_addr);
1622 }
1623 
1624 
1625 /**
1626  * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1627  *   @dev: virtual device associated with tunnel
1628  **/
1629 
1630 static inline int
ip6_tnl_dev_init_gen(struct net_device * dev)1631 ip6_tnl_dev_init_gen(struct net_device *dev)
1632 {
1633 	struct ip6_tnl *t = netdev_priv(dev);
1634 	int ret;
1635 
1636 	t->dev = dev;
1637 	t->net = dev_net(dev);
1638 	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1639 	if (!dev->tstats)
1640 		return -ENOMEM;
1641 
1642 	ret = ip6_tnl_dst_init(t);
1643 	if (ret) {
1644 		free_percpu(dev->tstats);
1645 		dev->tstats = NULL;
1646 		return ret;
1647 	}
1648 
1649 	return 0;
1650 }
1651 
1652 /**
1653  * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1654  *   @dev: virtual device associated with tunnel
1655  **/
1656 
ip6_tnl_dev_init(struct net_device * dev)1657 static int ip6_tnl_dev_init(struct net_device *dev)
1658 {
1659 	struct ip6_tnl *t = netdev_priv(dev);
1660 	int err = ip6_tnl_dev_init_gen(dev);
1661 
1662 	if (err)
1663 		return err;
1664 	ip6_tnl_link_config(t);
1665 	return 0;
1666 }
1667 
1668 /**
1669  * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1670  *   @dev: fallback device
1671  *
1672  * Return: 0
1673  **/
1674 
ip6_fb_tnl_dev_init(struct net_device * dev)1675 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1676 {
1677 	struct ip6_tnl *t = netdev_priv(dev);
1678 	struct net *net = dev_net(dev);
1679 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1680 
1681 	t->parms.proto = IPPROTO_IPV6;
1682 	dev_hold(dev);
1683 
1684 	rcu_assign_pointer(ip6n->tnls_wc[0], t);
1685 	return 0;
1686 }
1687 
ip6_tnl_validate(struct nlattr * tb[],struct nlattr * data[])1688 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1689 {
1690 	u8 proto;
1691 
1692 	if (!data || !data[IFLA_IPTUN_PROTO])
1693 		return 0;
1694 
1695 	proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1696 	if (proto != IPPROTO_IPV6 &&
1697 	    proto != IPPROTO_IPIP &&
1698 	    proto != 0)
1699 		return -EINVAL;
1700 
1701 	return 0;
1702 }
1703 
ip6_tnl_netlink_parms(struct nlattr * data[],struct __ip6_tnl_parm * parms)1704 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1705 				  struct __ip6_tnl_parm *parms)
1706 {
1707 	memset(parms, 0, sizeof(*parms));
1708 
1709 	if (!data)
1710 		return;
1711 
1712 	if (data[IFLA_IPTUN_LINK])
1713 		parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1714 
1715 	if (data[IFLA_IPTUN_LOCAL])
1716 		parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1717 
1718 	if (data[IFLA_IPTUN_REMOTE])
1719 		parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1720 
1721 	if (data[IFLA_IPTUN_TTL])
1722 		parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1723 
1724 	if (data[IFLA_IPTUN_ENCAP_LIMIT])
1725 		parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1726 
1727 	if (data[IFLA_IPTUN_FLOWINFO])
1728 		parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1729 
1730 	if (data[IFLA_IPTUN_FLAGS])
1731 		parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1732 
1733 	if (data[IFLA_IPTUN_PROTO])
1734 		parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1735 }
1736 
ip6_tnl_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])1737 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1738 			   struct nlattr *tb[], struct nlattr *data[])
1739 {
1740 	struct net *net = dev_net(dev);
1741 	struct ip6_tnl *nt, *t;
1742 
1743 	nt = netdev_priv(dev);
1744 	ip6_tnl_netlink_parms(data, &nt->parms);
1745 
1746 	t = ip6_tnl_locate(net, &nt->parms, 0);
1747 	if (!IS_ERR(t))
1748 		return -EEXIST;
1749 
1750 	return ip6_tnl_create2(dev);
1751 }
1752 
ip6_tnl_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])1753 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1754 			      struct nlattr *data[])
1755 {
1756 	struct ip6_tnl *t = netdev_priv(dev);
1757 	struct __ip6_tnl_parm p;
1758 	struct net *net = t->net;
1759 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1760 
1761 	if (dev == ip6n->fb_tnl_dev)
1762 		return -EINVAL;
1763 
1764 	ip6_tnl_netlink_parms(data, &p);
1765 
1766 	t = ip6_tnl_locate(net, &p, 0);
1767 	if (!IS_ERR(t)) {
1768 		if (t->dev != dev)
1769 			return -EEXIST;
1770 	} else
1771 		t = netdev_priv(dev);
1772 
1773 	return ip6_tnl_update(t, &p);
1774 }
1775 
ip6_tnl_dellink(struct net_device * dev,struct list_head * head)1776 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1777 {
1778 	struct net *net = dev_net(dev);
1779 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1780 
1781 	if (dev != ip6n->fb_tnl_dev)
1782 		unregister_netdevice_queue(dev, head);
1783 }
1784 
ip6_tnl_get_size(const struct net_device * dev)1785 static size_t ip6_tnl_get_size(const struct net_device *dev)
1786 {
1787 	return
1788 		/* IFLA_IPTUN_LINK */
1789 		nla_total_size(4) +
1790 		/* IFLA_IPTUN_LOCAL */
1791 		nla_total_size(sizeof(struct in6_addr)) +
1792 		/* IFLA_IPTUN_REMOTE */
1793 		nla_total_size(sizeof(struct in6_addr)) +
1794 		/* IFLA_IPTUN_TTL */
1795 		nla_total_size(1) +
1796 		/* IFLA_IPTUN_ENCAP_LIMIT */
1797 		nla_total_size(1) +
1798 		/* IFLA_IPTUN_FLOWINFO */
1799 		nla_total_size(4) +
1800 		/* IFLA_IPTUN_FLAGS */
1801 		nla_total_size(4) +
1802 		/* IFLA_IPTUN_PROTO */
1803 		nla_total_size(1) +
1804 		0;
1805 }
1806 
ip6_tnl_fill_info(struct sk_buff * skb,const struct net_device * dev)1807 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1808 {
1809 	struct ip6_tnl *tunnel = netdev_priv(dev);
1810 	struct __ip6_tnl_parm *parm = &tunnel->parms;
1811 
1812 	if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1813 	    nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1814 	    nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
1815 	    nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1816 	    nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1817 	    nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1818 	    nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1819 	    nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
1820 		goto nla_put_failure;
1821 	return 0;
1822 
1823 nla_put_failure:
1824 	return -EMSGSIZE;
1825 }
1826 
ip6_tnl_get_link_net(const struct net_device * dev)1827 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1828 {
1829 	struct ip6_tnl *tunnel = netdev_priv(dev);
1830 
1831 	return tunnel->net;
1832 }
1833 EXPORT_SYMBOL(ip6_tnl_get_link_net);
1834 
1835 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1836 	[IFLA_IPTUN_LINK]		= { .type = NLA_U32 },
1837 	[IFLA_IPTUN_LOCAL]		= { .len = sizeof(struct in6_addr) },
1838 	[IFLA_IPTUN_REMOTE]		= { .len = sizeof(struct in6_addr) },
1839 	[IFLA_IPTUN_TTL]		= { .type = NLA_U8 },
1840 	[IFLA_IPTUN_ENCAP_LIMIT]	= { .type = NLA_U8 },
1841 	[IFLA_IPTUN_FLOWINFO]		= { .type = NLA_U32 },
1842 	[IFLA_IPTUN_FLAGS]		= { .type = NLA_U32 },
1843 	[IFLA_IPTUN_PROTO]		= { .type = NLA_U8 },
1844 };
1845 
1846 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1847 	.kind		= "ip6tnl",
1848 	.maxtype	= IFLA_IPTUN_MAX,
1849 	.policy		= ip6_tnl_policy,
1850 	.priv_size	= sizeof(struct ip6_tnl),
1851 	.setup		= ip6_tnl_dev_setup,
1852 	.validate	= ip6_tnl_validate,
1853 	.newlink	= ip6_tnl_newlink,
1854 	.changelink	= ip6_tnl_changelink,
1855 	.dellink	= ip6_tnl_dellink,
1856 	.get_size	= ip6_tnl_get_size,
1857 	.fill_info	= ip6_tnl_fill_info,
1858 	.get_link_net	= ip6_tnl_get_link_net,
1859 };
1860 
1861 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1862 	.handler	= ip4ip6_rcv,
1863 	.err_handler	= ip4ip6_err,
1864 	.priority	=	1,
1865 };
1866 
1867 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1868 	.handler	= ip6ip6_rcv,
1869 	.err_handler	= ip6ip6_err,
1870 	.priority	=	1,
1871 };
1872 
ip6_tnl_destroy_tunnels(struct net * net)1873 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1874 {
1875 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1876 	struct net_device *dev, *aux;
1877 	int h;
1878 	struct ip6_tnl *t;
1879 	LIST_HEAD(list);
1880 
1881 	for_each_netdev_safe(net, dev, aux)
1882 		if (dev->rtnl_link_ops == &ip6_link_ops)
1883 			unregister_netdevice_queue(dev, &list);
1884 
1885 	for (h = 0; h < HASH_SIZE; h++) {
1886 		t = rtnl_dereference(ip6n->tnls_r_l[h]);
1887 		while (t) {
1888 			/* If dev is in the same netns, it has already
1889 			 * been added to the list by the previous loop.
1890 			 */
1891 			if (!net_eq(dev_net(t->dev), net))
1892 				unregister_netdevice_queue(t->dev, &list);
1893 			t = rtnl_dereference(t->next);
1894 		}
1895 	}
1896 
1897 	unregister_netdevice_many(&list);
1898 }
1899 
ip6_tnl_init_net(struct net * net)1900 static int __net_init ip6_tnl_init_net(struct net *net)
1901 {
1902 	struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1903 	struct ip6_tnl *t = NULL;
1904 	int err;
1905 
1906 	ip6n->tnls[0] = ip6n->tnls_wc;
1907 	ip6n->tnls[1] = ip6n->tnls_r_l;
1908 
1909 	err = -ENOMEM;
1910 	ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1911 					NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
1912 
1913 	if (!ip6n->fb_tnl_dev)
1914 		goto err_alloc_dev;
1915 	dev_net_set(ip6n->fb_tnl_dev, net);
1916 	ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_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 	ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
1921 
1922 	err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1923 	if (err < 0)
1924 		goto err_register;
1925 
1926 	err = register_netdev(ip6n->fb_tnl_dev);
1927 	if (err < 0)
1928 		goto err_register;
1929 
1930 	t = netdev_priv(ip6n->fb_tnl_dev);
1931 
1932 	strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1933 	return 0;
1934 
1935 err_register:
1936 	ip6_dev_free(ip6n->fb_tnl_dev);
1937 err_alloc_dev:
1938 	return err;
1939 }
1940 
ip6_tnl_exit_net(struct net * net)1941 static void __net_exit ip6_tnl_exit_net(struct net *net)
1942 {
1943 	rtnl_lock();
1944 	ip6_tnl_destroy_tunnels(net);
1945 	rtnl_unlock();
1946 }
1947 
1948 static struct pernet_operations ip6_tnl_net_ops = {
1949 	.init = ip6_tnl_init_net,
1950 	.exit = ip6_tnl_exit_net,
1951 	.id   = &ip6_tnl_net_id,
1952 	.size = sizeof(struct ip6_tnl_net),
1953 };
1954 
1955 /**
1956  * ip6_tunnel_init - register protocol and reserve needed resources
1957  *
1958  * Return: 0 on success
1959  **/
1960 
ip6_tunnel_init(void)1961 static int __init ip6_tunnel_init(void)
1962 {
1963 	int  err;
1964 
1965 	err = register_pernet_device(&ip6_tnl_net_ops);
1966 	if (err < 0)
1967 		goto out_pernet;
1968 
1969 	err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1970 	if (err < 0) {
1971 		pr_err("%s: can't register ip4ip6\n", __func__);
1972 		goto out_ip4ip6;
1973 	}
1974 
1975 	err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1976 	if (err < 0) {
1977 		pr_err("%s: can't register ip6ip6\n", __func__);
1978 		goto out_ip6ip6;
1979 	}
1980 	err = rtnl_link_register(&ip6_link_ops);
1981 	if (err < 0)
1982 		goto rtnl_link_failed;
1983 
1984 	return 0;
1985 
1986 rtnl_link_failed:
1987 	xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1988 out_ip6ip6:
1989 	xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1990 out_ip4ip6:
1991 	unregister_pernet_device(&ip6_tnl_net_ops);
1992 out_pernet:
1993 	return err;
1994 }
1995 
1996 /**
1997  * ip6_tunnel_cleanup - free resources and unregister protocol
1998  **/
1999 
ip6_tunnel_cleanup(void)2000 static void __exit ip6_tunnel_cleanup(void)
2001 {
2002 	rtnl_link_unregister(&ip6_link_ops);
2003 	if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
2004 		pr_info("%s: can't deregister ip4ip6\n", __func__);
2005 
2006 	if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
2007 		pr_info("%s: can't deregister ip6ip6\n", __func__);
2008 
2009 	unregister_pernet_device(&ip6_tnl_net_ops);
2010 }
2011 
2012 module_init(ip6_tunnel_init);
2013 module_exit(ip6_tunnel_cleanup);
2014