2 * IPv6 virtual tunneling interface
4 * Copyright (C) 2013 secunet Security Networks AG
7 * Steffen Klassert <steffen.klassert@secunet.com>
10 * net/ipv6/ip6_tunnel.c
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.
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>
27 #include <linux/if_tunnel.h>
28 #include <linux/net.h>
29 #include <linux/in6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/icmpv6.h>
33 #include <linux/init.h>
34 #include <linux/route.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/netfilter_ipv6.h>
37 #include <linux/slab.h>
38 #include <linux/hash.h>
40 #include <linux/uaccess.h>
41 #include <linux/atomic.h>
45 #include <net/ip_tunnels.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
51 #include <net/net_namespace.h>
52 #include <net/netns/generic.h>
54 #define HASH_SIZE_SHIFT 5
55 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
57 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
59 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
61 return hash_32(hash, HASH_SIZE_SHIFT);
64 static int vti6_dev_init(struct net_device *dev);
65 static void vti6_dev_setup(struct net_device *dev);
66 static struct rtnl_link_ops vti6_link_ops __read_mostly;
68 static int vti6_net_id __read_mostly;
70 /* the vti6 tunnel fallback device */
71 struct net_device *fb_tnl_dev;
72 /* lists for storing tunnels in use */
73 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
74 struct ip6_tnl __rcu *tnls_wc[1];
75 struct ip6_tnl __rcu **tnls[2];
78 static struct net_device_stats *vti6_get_stats(struct net_device *dev)
80 struct pcpu_tstats sum = { 0 };
83 for_each_possible_cpu(i) {
84 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
86 sum.rx_packets += tstats->rx_packets;
87 sum.rx_bytes += tstats->rx_bytes;
88 sum.tx_packets += tstats->tx_packets;
89 sum.tx_bytes += tstats->tx_bytes;
91 dev->stats.rx_packets = sum.rx_packets;
92 dev->stats.rx_bytes = sum.rx_bytes;
93 dev->stats.tx_packets = sum.tx_packets;
94 dev->stats.tx_bytes = sum.tx_bytes;
98 #define for_each_vti6_tunnel_rcu(start) \
99 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
102 * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
103 * @net: network namespace
104 * @remote: the address of the tunnel exit-point
105 * @local: the address of the tunnel entry-point
108 * tunnel matching given end-points if found,
109 * else fallback tunnel if its device is up,
112 static struct ip6_tnl *
113 vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
114 const struct in6_addr *local)
116 unsigned int hash = HASH(remote, local);
118 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
120 for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
121 if (ipv6_addr_equal(local, &t->parms.laddr) &&
122 ipv6_addr_equal(remote, &t->parms.raddr) &&
123 (t->dev->flags & IFF_UP))
126 t = rcu_dereference(ip6n->tnls_wc[0]);
127 if (t && (t->dev->flags & IFF_UP))
134 * vti6_tnl_bucket - get head of list matching given tunnel parameters
135 * @p: parameters containing tunnel end-points
138 * vti6_tnl_bucket() returns the head of the list matching the
139 * &struct in6_addr entries laddr and raddr in @p.
141 * Return: head of IPv6 tunnel list
143 static struct ip6_tnl __rcu **
144 vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
146 const struct in6_addr *remote = &p->raddr;
147 const struct in6_addr *local = &p->laddr;
151 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
153 h = HASH(remote, local);
155 return &ip6n->tnls[prio][h];
159 vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
161 struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
163 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
164 rcu_assign_pointer(*tp, t);
168 vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
170 struct ip6_tnl __rcu **tp;
171 struct ip6_tnl *iter;
173 for (tp = vti6_tnl_bucket(ip6n, &t->parms);
174 (iter = rtnl_dereference(*tp)) != NULL;
177 rcu_assign_pointer(*tp, t->next);
183 static void vti6_dev_free(struct net_device *dev)
185 free_percpu(dev->tstats);
189 static int vti6_tnl_create2(struct net_device *dev)
191 struct ip6_tnl *t = netdev_priv(dev);
192 struct net *net = dev_net(dev);
193 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
196 err = vti6_dev_init(dev);
200 err = register_netdevice(dev);
204 strcpy(t->parms.name, dev->name);
205 dev->rtnl_link_ops = &vti6_link_ops;
208 vti6_tnl_link(ip6n, t);
216 static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
218 struct net_device *dev;
224 strlcpy(name, p->name, IFNAMSIZ);
226 sprintf(name, "ip6_vti%%d");
228 dev = alloc_netdev(sizeof(*t), name, vti6_dev_setup);
232 dev_net_set(dev, net);
234 t = netdev_priv(dev);
236 t->net = dev_net(dev);
238 err = vti6_tnl_create2(dev);
251 * vti6_locate - find or create tunnel matching given parameters
252 * @net: network namespace
253 * @p: tunnel parameters
254 * @create: != 0 if allowed to create new tunnel if no match found
257 * vti6_locate() first tries to locate an existing tunnel
258 * based on @parms. If this is unsuccessful, but @create is set a new
259 * tunnel device is created and registered for use.
262 * matching tunnel or NULL
264 static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
267 const struct in6_addr *remote = &p->raddr;
268 const struct in6_addr *local = &p->laddr;
269 struct ip6_tnl __rcu **tp;
271 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
273 for (tp = vti6_tnl_bucket(ip6n, p);
274 (t = rtnl_dereference(*tp)) != NULL;
276 if (ipv6_addr_equal(local, &t->parms.laddr) &&
277 ipv6_addr_equal(remote, &t->parms.raddr))
282 return vti6_tnl_create(net, p);
286 * vti6_dev_uninit - tunnel device uninitializer
287 * @dev: the device to be destroyed
290 * vti6_dev_uninit() removes tunnel from its list
292 static void vti6_dev_uninit(struct net_device *dev)
294 struct ip6_tnl *t = netdev_priv(dev);
295 struct net *net = dev_net(dev);
296 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
298 if (dev == ip6n->fb_tnl_dev)
299 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
301 vti6_tnl_unlink(ip6n, t);
302 ip6_tnl_dst_reset(t);
306 static int vti6_rcv(struct sk_buff *skb)
309 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
313 if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
314 &ipv6h->daddr)) != NULL) {
315 struct pcpu_tstats *tstats;
317 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
322 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
327 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
328 t->dev->stats.rx_dropped++;
333 tstats = this_cpu_ptr(t->dev->tstats);
334 tstats->rx_packets++;
335 tstats->rx_bytes += skb->len;
353 * vti6_addr_conflict - compare packet addresses to tunnel's own
354 * @t: the outgoing tunnel device
355 * @hdr: IPv6 header from the incoming packet
358 * Avoid trivial tunneling loop by checking that tunnel exit-point
359 * doesn't match source of incoming packet.
366 vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
368 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
372 * vti6_xmit - send a packet
373 * @skb: the outgoing socket buffer
374 * @dev: the outgoing tunnel device
376 static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
378 struct net *net = dev_net(dev);
379 struct ip6_tnl *t = netdev_priv(dev);
380 struct net_device_stats *stats = &t->dev->stats;
381 struct dst_entry *dst = NULL, *ndst = NULL;
383 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
384 struct net_device *tdev;
387 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
388 !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
391 dst = ip6_tnl_dst_check(t);
393 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
395 ndst = ip6_route_output(net, NULL, &fl6);
398 goto tx_err_link_failure;
399 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(&fl6), NULL, 0);
403 goto tx_err_link_failure;
408 if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
409 goto tx_err_link_failure;
415 net_warn_ratelimited("%s: Local routing loop detected!\n",
417 goto tx_err_dst_release;
422 skb_dst_set_noref(skb, dst);
424 ip6tunnel_xmit(skb, dev);
426 dev->mtu = dst_mtu(ndst);
427 ip6_tnl_dst_store(t, ndst);
432 stats->tx_carrier_errors++;
433 dst_link_failure(skb);
440 vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
442 struct ip6_tnl *t = netdev_priv(dev);
443 struct net_device_stats *stats = &t->dev->stats;
446 switch (skb->protocol) {
447 case htons(ETH_P_IPV6):
448 ret = vti6_xmit(skb, dev);
466 static void vti6_link_config(struct ip6_tnl *t)
468 struct dst_entry *dst;
469 struct net_device *dev = t->dev;
470 struct __ip6_tnl_parm *p = &t->parms;
471 struct flowi6 *fl6 = &t->fl.u.ip6;
473 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
474 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
476 /* Set up flowi template */
477 fl6->saddr = p->laddr;
478 fl6->daddr = p->raddr;
479 fl6->flowi6_oif = p->link;
480 fl6->flowi6_mark = be32_to_cpu(p->i_key);
481 fl6->flowi6_proto = p->proto;
484 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
485 IP6_TNL_F_CAP_PER_PACKET);
486 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
488 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
489 dev->flags |= IFF_POINTOPOINT;
491 dev->flags &= ~IFF_POINTOPOINT;
493 dev->iflink = p->link;
495 if (p->flags & IP6_TNL_F_CAP_XMIT) {
497 dst = ip6_route_output(dev_net(dev), NULL, fl6);
501 dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
507 dev->hard_header_len = dst->dev->hard_header_len;
509 dev->mtu = dst_mtu(dst);
511 if (dev->mtu < IPV6_MIN_MTU)
512 dev->mtu = IPV6_MIN_MTU;
519 * vti6_tnl_change - update the tunnel parameters
520 * @t: tunnel to be changed
521 * @p: tunnel configuration parameters
524 * vti6_tnl_change() updates the tunnel parameters
527 vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
529 t->parms.laddr = p->laddr;
530 t->parms.raddr = p->raddr;
531 t->parms.link = p->link;
532 t->parms.i_key = p->i_key;
533 t->parms.o_key = p->o_key;
534 t->parms.proto = p->proto;
535 ip6_tnl_dst_reset(t);
540 static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
542 struct net *net = dev_net(t->dev);
543 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
546 vti6_tnl_unlink(ip6n, t);
548 err = vti6_tnl_change(t, p);
549 vti6_tnl_link(ip6n, t);
550 netdev_state_change(t->dev);
555 vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
564 memcpy(p->name, u->name, sizeof(u->name));
568 vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
577 memcpy(u->name, p->name, sizeof(u->name));
581 * vti6_tnl_ioctl - configure vti6 tunnels from userspace
582 * @dev: virtual device associated with tunnel
583 * @ifr: parameters passed from userspace
584 * @cmd: command to be performed
587 * vti6_ioctl() is used for managing vti6 tunnels
590 * The possible commands are the following:
591 * %SIOCGETTUNNEL: get tunnel parameters for device
592 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
593 * %SIOCCHGTUNNEL: change tunnel parameters to those given
594 * %SIOCDELTUNNEL: delete tunnel
596 * The fallback device "ip6_vti0", created during module
597 * initialization, can be used for creating other tunnel devices.
601 * %-EFAULT if unable to copy data to or from userspace,
602 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
603 * %-EINVAL if passed tunnel parameters are invalid,
604 * %-EEXIST if changing a tunnel's parameters would cause a conflict
605 * %-ENODEV if attempting to change or delete a nonexisting device
608 vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
611 struct ip6_tnl_parm2 p;
612 struct __ip6_tnl_parm p1;
613 struct ip6_tnl *t = NULL;
614 struct net *net = dev_net(dev);
615 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
619 if (dev == ip6n->fb_tnl_dev) {
620 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
624 vti6_parm_from_user(&p1, &p);
625 t = vti6_locate(net, &p1, 0);
627 memset(&p, 0, sizeof(p));
630 t = netdev_priv(dev);
631 vti6_parm_to_user(&p, &t->parms);
632 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
638 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
641 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
644 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
646 vti6_parm_from_user(&p1, &p);
647 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
648 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
655 t = netdev_priv(dev);
657 err = vti6_update(t, &p1);
661 vti6_parm_to_user(&p, &t->parms);
662 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
666 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
670 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
673 if (dev == ip6n->fb_tnl_dev) {
675 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
678 vti6_parm_from_user(&p1, &p);
679 t = vti6_locate(net, &p1, 0);
683 if (t->dev == ip6n->fb_tnl_dev)
688 unregister_netdevice(dev);
697 * vti6_tnl_change_mtu - change mtu manually for tunnel device
698 * @dev: virtual device associated with tunnel
699 * @new_mtu: the new mtu
703 * %-EINVAL if mtu too small
705 static int vti6_change_mtu(struct net_device *dev, int new_mtu)
707 if (new_mtu < IPV6_MIN_MTU)
714 static const struct net_device_ops vti6_netdev_ops = {
715 .ndo_uninit = vti6_dev_uninit,
716 .ndo_start_xmit = vti6_tnl_xmit,
717 .ndo_do_ioctl = vti6_ioctl,
718 .ndo_change_mtu = vti6_change_mtu,
719 .ndo_get_stats = vti6_get_stats,
723 * vti6_dev_setup - setup virtual tunnel device
724 * @dev: virtual device associated with tunnel
727 * Initialize function pointers and device parameters
729 static void vti6_dev_setup(struct net_device *dev)
733 dev->netdev_ops = &vti6_netdev_ops;
734 dev->destructor = vti6_dev_free;
736 dev->type = ARPHRD_TUNNEL6;
737 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
738 dev->mtu = ETH_DATA_LEN;
739 t = netdev_priv(dev);
740 dev->flags |= IFF_NOARP;
741 dev->addr_len = sizeof(struct in6_addr);
742 dev->features |= NETIF_F_NETNS_LOCAL;
743 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
747 * vti6_dev_init_gen - general initializer for all tunnel devices
748 * @dev: virtual device associated with tunnel
750 static inline int vti6_dev_init_gen(struct net_device *dev)
752 struct ip6_tnl *t = netdev_priv(dev);
755 t->net = dev_net(dev);
756 dev->tstats = alloc_percpu(struct pcpu_tstats);
763 * vti6_dev_init - initializer for all non fallback tunnel devices
764 * @dev: virtual device associated with tunnel
766 static int vti6_dev_init(struct net_device *dev)
768 struct ip6_tnl *t = netdev_priv(dev);
769 int err = vti6_dev_init_gen(dev);
778 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
779 * @dev: fallback device
783 static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
785 struct ip6_tnl *t = netdev_priv(dev);
786 struct net *net = dev_net(dev);
787 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
788 int err = vti6_dev_init_gen(dev);
793 t->parms.proto = IPPROTO_IPV6;
798 rcu_assign_pointer(ip6n->tnls_wc[0], t);
802 static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
807 static void vti6_netlink_parms(struct nlattr *data[],
808 struct __ip6_tnl_parm *parms)
810 memset(parms, 0, sizeof(*parms));
815 if (data[IFLA_VTI_LINK])
816 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
818 if (data[IFLA_VTI_LOCAL])
819 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
820 sizeof(struct in6_addr));
822 if (data[IFLA_VTI_REMOTE])
823 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
824 sizeof(struct in6_addr));
826 if (data[IFLA_VTI_IKEY])
827 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
829 if (data[IFLA_VTI_OKEY])
830 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
833 static int vti6_newlink(struct net *src_net, struct net_device *dev,
834 struct nlattr *tb[], struct nlattr *data[])
836 struct net *net = dev_net(dev);
839 nt = netdev_priv(dev);
840 vti6_netlink_parms(data, &nt->parms);
842 nt->parms.proto = IPPROTO_IPV6;
844 if (vti6_locate(net, &nt->parms, 0))
847 return vti6_tnl_create2(dev);
850 static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
851 struct nlattr *data[])
854 struct __ip6_tnl_parm p;
855 struct net *net = dev_net(dev);
856 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
858 if (dev == ip6n->fb_tnl_dev)
861 vti6_netlink_parms(data, &p);
863 t = vti6_locate(net, &p, 0);
869 t = netdev_priv(dev);
871 return vti6_update(t, &p);
874 static size_t vti6_get_size(const struct net_device *dev)
880 nla_total_size(sizeof(struct in6_addr)) +
881 /* IFLA_VTI_REMOTE */
882 nla_total_size(sizeof(struct in6_addr)) +
890 static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
892 struct ip6_tnl *tunnel = netdev_priv(dev);
893 struct __ip6_tnl_parm *parm = &tunnel->parms;
895 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
896 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
898 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
900 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
901 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
902 goto nla_put_failure;
909 static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
910 [IFLA_VTI_LINK] = { .type = NLA_U32 },
911 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
912 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
913 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
914 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
917 static struct rtnl_link_ops vti6_link_ops __read_mostly = {
919 .maxtype = IFLA_VTI_MAX,
920 .policy = vti6_policy,
921 .priv_size = sizeof(struct ip6_tnl),
922 .setup = vti6_dev_setup,
923 .validate = vti6_validate,
924 .newlink = vti6_newlink,
925 .changelink = vti6_changelink,
926 .get_size = vti6_get_size,
927 .fill_info = vti6_fill_info,
930 static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
935 static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
941 for (h = 0; h < HASH_SIZE; h++) {
942 t = rtnl_dereference(ip6n->tnls_r_l[h]);
944 unregister_netdevice_queue(t->dev, &list);
945 t = rtnl_dereference(t->next);
949 t = rtnl_dereference(ip6n->tnls_wc[0]);
950 unregister_netdevice_queue(t->dev, &list);
951 unregister_netdevice_many(&list);
954 static int __net_init vti6_init_net(struct net *net)
956 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
957 struct ip6_tnl *t = NULL;
960 ip6n->tnls[0] = ip6n->tnls_wc;
961 ip6n->tnls[1] = ip6n->tnls_r_l;
964 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
967 if (!ip6n->fb_tnl_dev)
969 dev_net_set(ip6n->fb_tnl_dev, net);
971 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
975 err = register_netdev(ip6n->fb_tnl_dev);
979 t = netdev_priv(ip6n->fb_tnl_dev);
981 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
985 vti6_dev_free(ip6n->fb_tnl_dev);
990 static void __net_exit vti6_exit_net(struct net *net)
992 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
995 vti6_destroy_tunnels(ip6n);
999 static struct pernet_operations vti6_net_ops = {
1000 .init = vti6_init_net,
1001 .exit = vti6_exit_net,
1003 .size = sizeof(struct vti6_net),
1007 * vti6_tunnel_init - register protocol and reserve needed resources
1009 * Return: 0 on success
1011 static int __init vti6_tunnel_init(void)
1015 err = register_pernet_device(&vti6_net_ops);
1019 err = xfrm6_mode_tunnel_input_register(&vti6_handler);
1021 pr_err("%s: can't register vti6\n", __func__);
1024 err = rtnl_link_register(&vti6_link_ops);
1026 goto rtnl_link_failed;
1031 xfrm6_mode_tunnel_input_deregister(&vti6_handler);
1033 unregister_pernet_device(&vti6_net_ops);
1039 * vti6_tunnel_cleanup - free resources and unregister protocol
1041 static void __exit vti6_tunnel_cleanup(void)
1043 rtnl_link_unregister(&vti6_link_ops);
1044 if (xfrm6_mode_tunnel_input_deregister(&vti6_handler))
1045 pr_info("%s: can't deregister vti6\n", __func__);
1047 unregister_pernet_device(&vti6_net_ops);
1050 module_init(vti6_tunnel_init);
1051 module_exit(vti6_tunnel_cleanup);
1052 MODULE_LICENSE("GPL");
1053 MODULE_ALIAS_RTNL_LINK("vti6");
1054 MODULE_ALIAS_NETDEV("ip6_vti0");
1055 MODULE_AUTHOR("Steffen Klassert");
1056 MODULE_DESCRIPTION("IPv6 virtual tunnel interface");