xref: /openbmc/linux/net/ipv6/ip6_vti.c (revision afc98d90)
1 /*
2  *	IPv6 virtual tunneling interface
3  *
4  *	Copyright (C) 2013 secunet Security Networks AG
5  *
6  *	Author:
7  *	Steffen Klassert <steffen.klassert@secunet.com>
8  *
9  *	Based on:
10  *	net/ipv6/ip6_tunnel.c
11  *
12  *	This program is free software; you can redistribute it and/or
13  *	modify it under the terms of the GNU General Public License
14  *	as published by the Free Software Foundation; either version
15  *	2 of the License, or (at your option) any later version.
16  */
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 
39 #include <linux/uaccess.h>
40 #include <linux/atomic.h>
41 
42 #include <net/icmp.h>
43 #include <net/ip.h>
44 #include <net/ip_tunnels.h>
45 #include <net/ipv6.h>
46 #include <net/ip6_route.h>
47 #include <net/addrconf.h>
48 #include <net/ip6_tunnel.h>
49 #include <net/xfrm.h>
50 #include <net/net_namespace.h>
51 #include <net/netns/generic.h>
52 
53 #define HASH_SIZE_SHIFT  5
54 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
55 
56 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
57 {
58 	u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
59 
60 	return hash_32(hash, HASH_SIZE_SHIFT);
61 }
62 
63 static int vti6_dev_init(struct net_device *dev);
64 static void vti6_dev_setup(struct net_device *dev);
65 static struct rtnl_link_ops vti6_link_ops __read_mostly;
66 
67 static int vti6_net_id __read_mostly;
68 struct vti6_net {
69 	/* the vti6 tunnel fallback device */
70 	struct net_device *fb_tnl_dev;
71 	/* lists for storing tunnels in use */
72 	struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
73 	struct ip6_tnl __rcu *tnls_wc[1];
74 	struct ip6_tnl __rcu **tnls[2];
75 };
76 
77 #define for_each_vti6_tunnel_rcu(start) \
78 	for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
79 
80 /**
81  * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
82  *   @net: network namespace
83  *   @remote: the address of the tunnel exit-point
84  *   @local: the address of the tunnel entry-point
85  *
86  * Return:
87  *   tunnel matching given end-points if found,
88  *   else fallback tunnel if its device is up,
89  *   else %NULL
90  **/
91 static struct ip6_tnl *
92 vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
93 		const struct in6_addr *local)
94 {
95 	unsigned int hash = HASH(remote, local);
96 	struct ip6_tnl *t;
97 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
98 
99 	for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
100 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
101 		    ipv6_addr_equal(remote, &t->parms.raddr) &&
102 		    (t->dev->flags & IFF_UP))
103 			return t;
104 	}
105 	t = rcu_dereference(ip6n->tnls_wc[0]);
106 	if (t && (t->dev->flags & IFF_UP))
107 		return t;
108 
109 	return NULL;
110 }
111 
112 /**
113  * vti6_tnl_bucket - get head of list matching given tunnel parameters
114  *   @p: parameters containing tunnel end-points
115  *
116  * Description:
117  *   vti6_tnl_bucket() returns the head of the list matching the
118  *   &struct in6_addr entries laddr and raddr in @p.
119  *
120  * Return: head of IPv6 tunnel list
121  **/
122 static struct ip6_tnl __rcu **
123 vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
124 {
125 	const struct in6_addr *remote = &p->raddr;
126 	const struct in6_addr *local = &p->laddr;
127 	unsigned int h = 0;
128 	int prio = 0;
129 
130 	if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
131 		prio = 1;
132 		h = HASH(remote, local);
133 	}
134 	return &ip6n->tnls[prio][h];
135 }
136 
137 static void
138 vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
139 {
140 	struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
141 
142 	rcu_assign_pointer(t->next , rtnl_dereference(*tp));
143 	rcu_assign_pointer(*tp, t);
144 }
145 
146 static void
147 vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
148 {
149 	struct ip6_tnl __rcu **tp;
150 	struct ip6_tnl *iter;
151 
152 	for (tp = vti6_tnl_bucket(ip6n, &t->parms);
153 	     (iter = rtnl_dereference(*tp)) != NULL;
154 	     tp = &iter->next) {
155 		if (t == iter) {
156 			rcu_assign_pointer(*tp, t->next);
157 			break;
158 		}
159 	}
160 }
161 
162 static void vti6_dev_free(struct net_device *dev)
163 {
164 	free_percpu(dev->tstats);
165 	free_netdev(dev);
166 }
167 
168 static int vti6_tnl_create2(struct net_device *dev)
169 {
170 	struct ip6_tnl *t = netdev_priv(dev);
171 	struct net *net = dev_net(dev);
172 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
173 	int err;
174 
175 	err = vti6_dev_init(dev);
176 	if (err < 0)
177 		goto out;
178 
179 	err = register_netdevice(dev);
180 	if (err < 0)
181 		goto out;
182 
183 	strcpy(t->parms.name, dev->name);
184 	dev->rtnl_link_ops = &vti6_link_ops;
185 
186 	dev_hold(dev);
187 	vti6_tnl_link(ip6n, t);
188 
189 	return 0;
190 
191 out:
192 	return err;
193 }
194 
195 static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
196 {
197 	struct net_device *dev;
198 	struct ip6_tnl *t;
199 	char name[IFNAMSIZ];
200 	int err;
201 
202 	if (p->name[0])
203 		strlcpy(name, p->name, IFNAMSIZ);
204 	else
205 		sprintf(name, "ip6_vti%%d");
206 
207 	dev = alloc_netdev(sizeof(*t), name, vti6_dev_setup);
208 	if (dev == NULL)
209 		goto failed;
210 
211 	dev_net_set(dev, net);
212 
213 	t = netdev_priv(dev);
214 	t->parms = *p;
215 	t->net = dev_net(dev);
216 
217 	err = vti6_tnl_create2(dev);
218 	if (err < 0)
219 		goto failed_free;
220 
221 	return t;
222 
223 failed_free:
224 	vti6_dev_free(dev);
225 failed:
226 	return NULL;
227 }
228 
229 /**
230  * vti6_locate - find or create tunnel matching given parameters
231  *   @net: network namespace
232  *   @p: tunnel parameters
233  *   @create: != 0 if allowed to create new tunnel if no match found
234  *
235  * Description:
236  *   vti6_locate() first tries to locate an existing tunnel
237  *   based on @parms. If this is unsuccessful, but @create is set a new
238  *   tunnel device is created and registered for use.
239  *
240  * Return:
241  *   matching tunnel or NULL
242  **/
243 static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
244 				   int create)
245 {
246 	const struct in6_addr *remote = &p->raddr;
247 	const struct in6_addr *local = &p->laddr;
248 	struct ip6_tnl __rcu **tp;
249 	struct ip6_tnl *t;
250 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
251 
252 	for (tp = vti6_tnl_bucket(ip6n, p);
253 	     (t = rtnl_dereference(*tp)) != NULL;
254 	     tp = &t->next) {
255 		if (ipv6_addr_equal(local, &t->parms.laddr) &&
256 		    ipv6_addr_equal(remote, &t->parms.raddr))
257 			return t;
258 	}
259 	if (!create)
260 		return NULL;
261 	return vti6_tnl_create(net, p);
262 }
263 
264 /**
265  * vti6_dev_uninit - tunnel device uninitializer
266  *   @dev: the device to be destroyed
267  *
268  * Description:
269  *   vti6_dev_uninit() removes tunnel from its list
270  **/
271 static void vti6_dev_uninit(struct net_device *dev)
272 {
273 	struct ip6_tnl *t = netdev_priv(dev);
274 	struct net *net = dev_net(dev);
275 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
276 
277 	if (dev == ip6n->fb_tnl_dev)
278 		RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
279 	else
280 		vti6_tnl_unlink(ip6n, t);
281 	ip6_tnl_dst_reset(t);
282 	dev_put(dev);
283 }
284 
285 static int vti6_rcv(struct sk_buff *skb)
286 {
287 	struct ip6_tnl *t;
288 	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
289 
290 	rcu_read_lock();
291 
292 	if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
293 				 &ipv6h->daddr)) != NULL) {
294 		struct pcpu_sw_netstats *tstats;
295 
296 		if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
297 			rcu_read_unlock();
298 			goto discard;
299 		}
300 
301 		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
302 			rcu_read_unlock();
303 			return 0;
304 		}
305 
306 		if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
307 			t->dev->stats.rx_dropped++;
308 			rcu_read_unlock();
309 			goto discard;
310 		}
311 
312 		tstats = this_cpu_ptr(t->dev->tstats);
313 		u64_stats_update_begin(&tstats->syncp);
314 		tstats->rx_packets++;
315 		tstats->rx_bytes += skb->len;
316 		u64_stats_update_end(&tstats->syncp);
317 
318 		skb->mark = 0;
319 		secpath_reset(skb);
320 		skb->dev = t->dev;
321 
322 		rcu_read_unlock();
323 		return 0;
324 	}
325 	rcu_read_unlock();
326 	return 1;
327 
328 discard:
329 	kfree_skb(skb);
330 	return 0;
331 }
332 
333 /**
334  * vti6_addr_conflict - compare packet addresses to tunnel's own
335  *   @t: the outgoing tunnel device
336  *   @hdr: IPv6 header from the incoming packet
337  *
338  * Description:
339  *   Avoid trivial tunneling loop by checking that tunnel exit-point
340  *   doesn't match source of incoming packet.
341  *
342  * Return:
343  *   1 if conflict,
344  *   0 else
345  **/
346 static inline bool
347 vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
348 {
349 	return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
350 }
351 
352 /**
353  * vti6_xmit - send a packet
354  *   @skb: the outgoing socket buffer
355  *   @dev: the outgoing tunnel device
356  **/
357 static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
358 {
359 	struct net *net = dev_net(dev);
360 	struct ip6_tnl *t = netdev_priv(dev);
361 	struct net_device_stats *stats = &t->dev->stats;
362 	struct dst_entry *dst = NULL, *ndst = NULL;
363 	struct flowi6 fl6;
364 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
365 	struct net_device *tdev;
366 	int err = -1;
367 
368 	if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
369 	    !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
370 		return err;
371 
372 	dst = ip6_tnl_dst_check(t);
373 	if (!dst) {
374 		memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
375 
376 		ndst = ip6_route_output(net, NULL, &fl6);
377 
378 		if (ndst->error)
379 			goto tx_err_link_failure;
380 		ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(&fl6), NULL, 0);
381 		if (IS_ERR(ndst)) {
382 			err = PTR_ERR(ndst);
383 			ndst = NULL;
384 			goto tx_err_link_failure;
385 		}
386 		dst = ndst;
387 	}
388 
389 	if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
390 		goto tx_err_link_failure;
391 
392 	tdev = dst->dev;
393 
394 	if (tdev == dev) {
395 		stats->collisions++;
396 		net_warn_ratelimited("%s: Local routing loop detected!\n",
397 				     t->parms.name);
398 		goto tx_err_dst_release;
399 	}
400 
401 
402 	skb_dst_drop(skb);
403 	skb_dst_set_noref(skb, dst);
404 
405 	ip6tunnel_xmit(skb, dev);
406 	if (ndst) {
407 		dev->mtu = dst_mtu(ndst);
408 		ip6_tnl_dst_store(t, ndst);
409 	}
410 
411 	return 0;
412 tx_err_link_failure:
413 	stats->tx_carrier_errors++;
414 	dst_link_failure(skb);
415 tx_err_dst_release:
416 	dst_release(ndst);
417 	return err;
418 }
419 
420 static netdev_tx_t
421 vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
422 {
423 	struct ip6_tnl *t = netdev_priv(dev);
424 	struct net_device_stats *stats = &t->dev->stats;
425 	int ret;
426 
427 	switch (skb->protocol) {
428 	case htons(ETH_P_IPV6):
429 		ret = vti6_xmit(skb, dev);
430 		break;
431 	default:
432 		goto tx_err;
433 	}
434 
435 	if (ret < 0)
436 		goto tx_err;
437 
438 	return NETDEV_TX_OK;
439 
440 tx_err:
441 	stats->tx_errors++;
442 	stats->tx_dropped++;
443 	kfree_skb(skb);
444 	return NETDEV_TX_OK;
445 }
446 
447 static void vti6_link_config(struct ip6_tnl *t)
448 {
449 	struct dst_entry *dst;
450 	struct net_device *dev = t->dev;
451 	struct __ip6_tnl_parm *p = &t->parms;
452 	struct flowi6 *fl6 = &t->fl.u.ip6;
453 
454 	memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
455 	memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
456 
457 	/* Set up flowi template */
458 	fl6->saddr = p->laddr;
459 	fl6->daddr = p->raddr;
460 	fl6->flowi6_oif = p->link;
461 	fl6->flowi6_mark = be32_to_cpu(p->i_key);
462 	fl6->flowi6_proto = p->proto;
463 	fl6->flowlabel = 0;
464 
465 	p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
466 		      IP6_TNL_F_CAP_PER_PACKET);
467 	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
468 
469 	if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
470 		dev->flags |= IFF_POINTOPOINT;
471 	else
472 		dev->flags &= ~IFF_POINTOPOINT;
473 
474 	dev->iflink = p->link;
475 
476 	if (p->flags & IP6_TNL_F_CAP_XMIT) {
477 
478 		dst = ip6_route_output(dev_net(dev), NULL, fl6);
479 		if (dst->error)
480 			return;
481 
482 		dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
483 				  NULL, 0);
484 		if (IS_ERR(dst))
485 			return;
486 
487 		if (dst->dev) {
488 			dev->hard_header_len = dst->dev->hard_header_len;
489 
490 			dev->mtu = dst_mtu(dst);
491 
492 			if (dev->mtu < IPV6_MIN_MTU)
493 				dev->mtu = IPV6_MIN_MTU;
494 		}
495 		dst_release(dst);
496 	}
497 }
498 
499 /**
500  * vti6_tnl_change - update the tunnel parameters
501  *   @t: tunnel to be changed
502  *   @p: tunnel configuration parameters
503  *
504  * Description:
505  *   vti6_tnl_change() updates the tunnel parameters
506  **/
507 static int
508 vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
509 {
510 	t->parms.laddr = p->laddr;
511 	t->parms.raddr = p->raddr;
512 	t->parms.link = p->link;
513 	t->parms.i_key = p->i_key;
514 	t->parms.o_key = p->o_key;
515 	t->parms.proto = p->proto;
516 	ip6_tnl_dst_reset(t);
517 	vti6_link_config(t);
518 	return 0;
519 }
520 
521 static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
522 {
523 	struct net *net = dev_net(t->dev);
524 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
525 	int err;
526 
527 	vti6_tnl_unlink(ip6n, t);
528 	synchronize_net();
529 	err = vti6_tnl_change(t, p);
530 	vti6_tnl_link(ip6n, t);
531 	netdev_state_change(t->dev);
532 	return err;
533 }
534 
535 static void
536 vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
537 {
538 	p->laddr = u->laddr;
539 	p->raddr = u->raddr;
540 	p->link = u->link;
541 	p->i_key = u->i_key;
542 	p->o_key = u->o_key;
543 	p->proto = u->proto;
544 
545 	memcpy(p->name, u->name, sizeof(u->name));
546 }
547 
548 static void
549 vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
550 {
551 	u->laddr = p->laddr;
552 	u->raddr = p->raddr;
553 	u->link = p->link;
554 	u->i_key = p->i_key;
555 	u->o_key = p->o_key;
556 	u->proto = p->proto;
557 
558 	memcpy(u->name, p->name, sizeof(u->name));
559 }
560 
561 /**
562  * vti6_tnl_ioctl - configure vti6 tunnels from userspace
563  *   @dev: virtual device associated with tunnel
564  *   @ifr: parameters passed from userspace
565  *   @cmd: command to be performed
566  *
567  * Description:
568  *   vti6_ioctl() is used for managing vti6 tunnels
569  *   from userspace.
570  *
571  *   The possible commands are the following:
572  *     %SIOCGETTUNNEL: get tunnel parameters for device
573  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
574  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
575  *     %SIOCDELTUNNEL: delete tunnel
576  *
577  *   The fallback device "ip6_vti0", created during module
578  *   initialization, can be used for creating other tunnel devices.
579  *
580  * Return:
581  *   0 on success,
582  *   %-EFAULT if unable to copy data to or from userspace,
583  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
584  *   %-EINVAL if passed tunnel parameters are invalid,
585  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
586  *   %-ENODEV if attempting to change or delete a nonexisting device
587  **/
588 static int
589 vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
590 {
591 	int err = 0;
592 	struct ip6_tnl_parm2 p;
593 	struct __ip6_tnl_parm p1;
594 	struct ip6_tnl *t = NULL;
595 	struct net *net = dev_net(dev);
596 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
597 
598 	switch (cmd) {
599 	case SIOCGETTUNNEL:
600 		if (dev == ip6n->fb_tnl_dev) {
601 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
602 				err = -EFAULT;
603 				break;
604 			}
605 			vti6_parm_from_user(&p1, &p);
606 			t = vti6_locate(net, &p1, 0);
607 		} else {
608 			memset(&p, 0, sizeof(p));
609 		}
610 		if (t == NULL)
611 			t = netdev_priv(dev);
612 		vti6_parm_to_user(&p, &t->parms);
613 		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
614 			err = -EFAULT;
615 		break;
616 	case SIOCADDTUNNEL:
617 	case SIOCCHGTUNNEL:
618 		err = -EPERM;
619 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
620 			break;
621 		err = -EFAULT;
622 		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
623 			break;
624 		err = -EINVAL;
625 		if (p.proto != IPPROTO_IPV6  && p.proto != 0)
626 			break;
627 		vti6_parm_from_user(&p1, &p);
628 		t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
629 		if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
630 			if (t != NULL) {
631 				if (t->dev != dev) {
632 					err = -EEXIST;
633 					break;
634 				}
635 			} else
636 				t = netdev_priv(dev);
637 
638 			err = vti6_update(t, &p1);
639 		}
640 		if (t) {
641 			err = 0;
642 			vti6_parm_to_user(&p, &t->parms);
643 			if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
644 				err = -EFAULT;
645 
646 		} else
647 			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
648 		break;
649 	case SIOCDELTUNNEL:
650 		err = -EPERM;
651 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
652 			break;
653 
654 		if (dev == ip6n->fb_tnl_dev) {
655 			err = -EFAULT;
656 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
657 				break;
658 			err = -ENOENT;
659 			vti6_parm_from_user(&p1, &p);
660 			t = vti6_locate(net, &p1, 0);
661 			if (t == NULL)
662 				break;
663 			err = -EPERM;
664 			if (t->dev == ip6n->fb_tnl_dev)
665 				break;
666 			dev = t->dev;
667 		}
668 		err = 0;
669 		unregister_netdevice(dev);
670 		break;
671 	default:
672 		err = -EINVAL;
673 	}
674 	return err;
675 }
676 
677 /**
678  * vti6_tnl_change_mtu - change mtu manually for tunnel device
679  *   @dev: virtual device associated with tunnel
680  *   @new_mtu: the new mtu
681  *
682  * Return:
683  *   0 on success,
684  *   %-EINVAL if mtu too small
685  **/
686 static int vti6_change_mtu(struct net_device *dev, int new_mtu)
687 {
688 	if (new_mtu < IPV6_MIN_MTU)
689 		return -EINVAL;
690 
691 	dev->mtu = new_mtu;
692 	return 0;
693 }
694 
695 static const struct net_device_ops vti6_netdev_ops = {
696 	.ndo_uninit	= vti6_dev_uninit,
697 	.ndo_start_xmit = vti6_tnl_xmit,
698 	.ndo_do_ioctl	= vti6_ioctl,
699 	.ndo_change_mtu = vti6_change_mtu,
700 	.ndo_get_stats64 = ip_tunnel_get_stats64,
701 };
702 
703 /**
704  * vti6_dev_setup - setup virtual tunnel device
705  *   @dev: virtual device associated with tunnel
706  *
707  * Description:
708  *   Initialize function pointers and device parameters
709  **/
710 static void vti6_dev_setup(struct net_device *dev)
711 {
712 	struct ip6_tnl *t;
713 
714 	dev->netdev_ops = &vti6_netdev_ops;
715 	dev->destructor = vti6_dev_free;
716 
717 	dev->type = ARPHRD_TUNNEL6;
718 	dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
719 	dev->mtu = ETH_DATA_LEN;
720 	t = netdev_priv(dev);
721 	dev->flags |= IFF_NOARP;
722 	dev->addr_len = sizeof(struct in6_addr);
723 	dev->features |= NETIF_F_NETNS_LOCAL;
724 	dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
725 }
726 
727 /**
728  * vti6_dev_init_gen - general initializer for all tunnel devices
729  *   @dev: virtual device associated with tunnel
730  **/
731 static inline int vti6_dev_init_gen(struct net_device *dev)
732 {
733 	struct ip6_tnl *t = netdev_priv(dev);
734 	int i;
735 
736 	t->dev = dev;
737 	t->net = dev_net(dev);
738 	dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
739 	if (!dev->tstats)
740 		return -ENOMEM;
741 	for_each_possible_cpu(i) {
742 		struct pcpu_sw_netstats *stats;
743 		stats = per_cpu_ptr(dev->tstats, i);
744 		u64_stats_init(&stats->syncp);
745 	}
746 	return 0;
747 }
748 
749 /**
750  * vti6_dev_init - initializer for all non fallback tunnel devices
751  *   @dev: virtual device associated with tunnel
752  **/
753 static int vti6_dev_init(struct net_device *dev)
754 {
755 	struct ip6_tnl *t = netdev_priv(dev);
756 	int err = vti6_dev_init_gen(dev);
757 
758 	if (err)
759 		return err;
760 	vti6_link_config(t);
761 	return 0;
762 }
763 
764 /**
765  * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
766  *   @dev: fallback device
767  *
768  * Return: 0
769  **/
770 static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
771 {
772 	struct ip6_tnl *t = netdev_priv(dev);
773 	struct net *net = dev_net(dev);
774 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
775 	int err = vti6_dev_init_gen(dev);
776 
777 	if (err)
778 		return err;
779 
780 	t->parms.proto = IPPROTO_IPV6;
781 	dev_hold(dev);
782 
783 	vti6_link_config(t);
784 
785 	rcu_assign_pointer(ip6n->tnls_wc[0], t);
786 	return 0;
787 }
788 
789 static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
790 {
791 	return 0;
792 }
793 
794 static void vti6_netlink_parms(struct nlattr *data[],
795 			       struct __ip6_tnl_parm *parms)
796 {
797 	memset(parms, 0, sizeof(*parms));
798 
799 	if (!data)
800 		return;
801 
802 	if (data[IFLA_VTI_LINK])
803 		parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
804 
805 	if (data[IFLA_VTI_LOCAL])
806 		nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
807 			   sizeof(struct in6_addr));
808 
809 	if (data[IFLA_VTI_REMOTE])
810 		nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
811 			   sizeof(struct in6_addr));
812 
813 	if (data[IFLA_VTI_IKEY])
814 		parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
815 
816 	if (data[IFLA_VTI_OKEY])
817 		parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
818 }
819 
820 static int vti6_newlink(struct net *src_net, struct net_device *dev,
821 			struct nlattr *tb[], struct nlattr *data[])
822 {
823 	struct net *net = dev_net(dev);
824 	struct ip6_tnl *nt;
825 
826 	nt = netdev_priv(dev);
827 	vti6_netlink_parms(data, &nt->parms);
828 
829 	nt->parms.proto = IPPROTO_IPV6;
830 
831 	if (vti6_locate(net, &nt->parms, 0))
832 		return -EEXIST;
833 
834 	return vti6_tnl_create2(dev);
835 }
836 
837 static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
838 			   struct nlattr *data[])
839 {
840 	struct ip6_tnl *t;
841 	struct __ip6_tnl_parm p;
842 	struct net *net = dev_net(dev);
843 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
844 
845 	if (dev == ip6n->fb_tnl_dev)
846 		return -EINVAL;
847 
848 	vti6_netlink_parms(data, &p);
849 
850 	t = vti6_locate(net, &p, 0);
851 
852 	if (t) {
853 		if (t->dev != dev)
854 			return -EEXIST;
855 	} else
856 		t = netdev_priv(dev);
857 
858 	return vti6_update(t, &p);
859 }
860 
861 static size_t vti6_get_size(const struct net_device *dev)
862 {
863 	return
864 		/* IFLA_VTI_LINK */
865 		nla_total_size(4) +
866 		/* IFLA_VTI_LOCAL */
867 		nla_total_size(sizeof(struct in6_addr)) +
868 		/* IFLA_VTI_REMOTE */
869 		nla_total_size(sizeof(struct in6_addr)) +
870 		/* IFLA_VTI_IKEY */
871 		nla_total_size(4) +
872 		/* IFLA_VTI_OKEY */
873 		nla_total_size(4) +
874 		0;
875 }
876 
877 static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
878 {
879 	struct ip6_tnl *tunnel = netdev_priv(dev);
880 	struct __ip6_tnl_parm *parm = &tunnel->parms;
881 
882 	if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
883 	    nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
884 		    &parm->laddr) ||
885 	    nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
886 		    &parm->raddr) ||
887 	    nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
888 	    nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
889 		goto nla_put_failure;
890 	return 0;
891 
892 nla_put_failure:
893 	return -EMSGSIZE;
894 }
895 
896 static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
897 	[IFLA_VTI_LINK]		= { .type = NLA_U32 },
898 	[IFLA_VTI_LOCAL]	= { .len = sizeof(struct in6_addr) },
899 	[IFLA_VTI_REMOTE]	= { .len = sizeof(struct in6_addr) },
900 	[IFLA_VTI_IKEY]		= { .type = NLA_U32 },
901 	[IFLA_VTI_OKEY]		= { .type = NLA_U32 },
902 };
903 
904 static struct rtnl_link_ops vti6_link_ops __read_mostly = {
905 	.kind		= "vti6",
906 	.maxtype	= IFLA_VTI_MAX,
907 	.policy		= vti6_policy,
908 	.priv_size	= sizeof(struct ip6_tnl),
909 	.setup		= vti6_dev_setup,
910 	.validate	= vti6_validate,
911 	.newlink	= vti6_newlink,
912 	.changelink	= vti6_changelink,
913 	.get_size	= vti6_get_size,
914 	.fill_info	= vti6_fill_info,
915 };
916 
917 static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
918 	.handler	= vti6_rcv,
919 	.priority	=	1,
920 };
921 
922 static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
923 {
924 	int h;
925 	struct ip6_tnl *t;
926 	LIST_HEAD(list);
927 
928 	for (h = 0; h < HASH_SIZE; h++) {
929 		t = rtnl_dereference(ip6n->tnls_r_l[h]);
930 		while (t != NULL) {
931 			unregister_netdevice_queue(t->dev, &list);
932 			t = rtnl_dereference(t->next);
933 		}
934 	}
935 
936 	t = rtnl_dereference(ip6n->tnls_wc[0]);
937 	unregister_netdevice_queue(t->dev, &list);
938 	unregister_netdevice_many(&list);
939 }
940 
941 static int __net_init vti6_init_net(struct net *net)
942 {
943 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
944 	struct ip6_tnl *t = NULL;
945 	int err;
946 
947 	ip6n->tnls[0] = ip6n->tnls_wc;
948 	ip6n->tnls[1] = ip6n->tnls_r_l;
949 
950 	err = -ENOMEM;
951 	ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
952 					vti6_dev_setup);
953 
954 	if (!ip6n->fb_tnl_dev)
955 		goto err_alloc_dev;
956 	dev_net_set(ip6n->fb_tnl_dev, net);
957 
958 	err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
959 	if (err < 0)
960 		goto err_register;
961 
962 	err = register_netdev(ip6n->fb_tnl_dev);
963 	if (err < 0)
964 		goto err_register;
965 
966 	t = netdev_priv(ip6n->fb_tnl_dev);
967 
968 	strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
969 	return 0;
970 
971 err_register:
972 	vti6_dev_free(ip6n->fb_tnl_dev);
973 err_alloc_dev:
974 	return err;
975 }
976 
977 static void __net_exit vti6_exit_net(struct net *net)
978 {
979 	struct vti6_net *ip6n = net_generic(net, vti6_net_id);
980 
981 	rtnl_lock();
982 	vti6_destroy_tunnels(ip6n);
983 	rtnl_unlock();
984 }
985 
986 static struct pernet_operations vti6_net_ops = {
987 	.init = vti6_init_net,
988 	.exit = vti6_exit_net,
989 	.id   = &vti6_net_id,
990 	.size = sizeof(struct vti6_net),
991 };
992 
993 /**
994  * vti6_tunnel_init - register protocol and reserve needed resources
995  *
996  * Return: 0 on success
997  **/
998 static int __init vti6_tunnel_init(void)
999 {
1000 	int  err;
1001 
1002 	err = register_pernet_device(&vti6_net_ops);
1003 	if (err < 0)
1004 		goto out_pernet;
1005 
1006 	err = xfrm6_mode_tunnel_input_register(&vti6_handler);
1007 	if (err < 0) {
1008 		pr_err("%s: can't register vti6\n", __func__);
1009 		goto out;
1010 	}
1011 	err = rtnl_link_register(&vti6_link_ops);
1012 	if (err < 0)
1013 		goto rtnl_link_failed;
1014 
1015 	return 0;
1016 
1017 rtnl_link_failed:
1018 	xfrm6_mode_tunnel_input_deregister(&vti6_handler);
1019 out:
1020 	unregister_pernet_device(&vti6_net_ops);
1021 out_pernet:
1022 	return err;
1023 }
1024 
1025 /**
1026  * vti6_tunnel_cleanup - free resources and unregister protocol
1027  **/
1028 static void __exit vti6_tunnel_cleanup(void)
1029 {
1030 	rtnl_link_unregister(&vti6_link_ops);
1031 	if (xfrm6_mode_tunnel_input_deregister(&vti6_handler))
1032 		pr_info("%s: can't deregister vti6\n", __func__);
1033 
1034 	unregister_pernet_device(&vti6_net_ops);
1035 }
1036 
1037 module_init(vti6_tunnel_init);
1038 module_exit(vti6_tunnel_cleanup);
1039 MODULE_LICENSE("GPL");
1040 MODULE_ALIAS_RTNL_LINK("vti6");
1041 MODULE_ALIAS_NETDEV("ip6_vti0");
1042 MODULE_AUTHOR("Steffen Klassert");
1043 MODULE_DESCRIPTION("IPv6 virtual tunnel interface");
1044