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/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>
39 #include <linux/uaccess.h>
40 #include <linux/atomic.h>
44 #include <net/ip_tunnels.h>
46 #include <net/ip6_route.h>
47 #include <net/addrconf.h>
48 #include <net/ip6_tunnel.h>
50 #include <net/net_namespace.h>
51 #include <net/netns/generic.h>
53 #define HASH_SIZE_SHIFT 5
54 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
56 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
58 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
60 return hash_32(hash, HASH_SIZE_SHIFT);
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;
67 static int vti6_net_id __read_mostly;
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];
77 #define for_each_vti6_tunnel_rcu(start) \
78 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
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
87 * tunnel matching given end-points if found,
88 * else fallback tunnel if its device is up,
91 static struct ip6_tnl *
92 vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
93 const struct in6_addr *local)
95 unsigned int hash = HASH(remote, local);
97 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
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))
105 t = rcu_dereference(ip6n->tnls_wc[0]);
106 if (t && (t->dev->flags & IFF_UP))
113 * vti6_tnl_bucket - get head of list matching given tunnel parameters
114 * @p: parameters containing tunnel end-points
117 * vti6_tnl_bucket() returns the head of the list matching the
118 * &struct in6_addr entries laddr and raddr in @p.
120 * Return: head of IPv6 tunnel list
122 static struct ip6_tnl __rcu **
123 vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
125 const struct in6_addr *remote = &p->raddr;
126 const struct in6_addr *local = &p->laddr;
130 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
132 h = HASH(remote, local);
134 return &ip6n->tnls[prio][h];
138 vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
140 struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
142 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
143 rcu_assign_pointer(*tp, t);
147 vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
149 struct ip6_tnl __rcu **tp;
150 struct ip6_tnl *iter;
152 for (tp = vti6_tnl_bucket(ip6n, &t->parms);
153 (iter = rtnl_dereference(*tp)) != NULL;
156 rcu_assign_pointer(*tp, t->next);
162 static void vti6_dev_free(struct net_device *dev)
164 free_percpu(dev->tstats);
168 static int vti6_tnl_create2(struct net_device *dev)
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);
175 err = vti6_dev_init(dev);
179 err = register_netdevice(dev);
183 strcpy(t->parms.name, dev->name);
184 dev->rtnl_link_ops = &vti6_link_ops;
187 vti6_tnl_link(ip6n, t);
195 static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
197 struct net_device *dev;
203 strlcpy(name, p->name, IFNAMSIZ);
205 sprintf(name, "ip6_vti%%d");
207 dev = alloc_netdev(sizeof(*t), name, vti6_dev_setup);
211 dev_net_set(dev, net);
213 t = netdev_priv(dev);
215 t->net = dev_net(dev);
217 err = vti6_tnl_create2(dev);
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
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.
241 * matching tunnel or NULL
243 static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
246 const struct in6_addr *remote = &p->raddr;
247 const struct in6_addr *local = &p->laddr;
248 struct ip6_tnl __rcu **tp;
250 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
252 for (tp = vti6_tnl_bucket(ip6n, p);
253 (t = rtnl_dereference(*tp)) != NULL;
255 if (ipv6_addr_equal(local, &t->parms.laddr) &&
256 ipv6_addr_equal(remote, &t->parms.raddr))
261 return vti6_tnl_create(net, p);
265 * vti6_dev_uninit - tunnel device uninitializer
266 * @dev: the device to be destroyed
269 * vti6_dev_uninit() removes tunnel from its list
271 static void vti6_dev_uninit(struct net_device *dev)
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);
277 if (dev == ip6n->fb_tnl_dev)
278 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
280 vti6_tnl_unlink(ip6n, t);
281 ip6_tnl_dst_reset(t);
285 static int vti6_rcv(struct sk_buff *skb)
288 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
292 if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
293 &ipv6h->daddr)) != NULL) {
294 struct pcpu_sw_netstats *tstats;
296 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
301 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
306 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
307 t->dev->stats.rx_dropped++;
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);
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
339 * Avoid trivial tunneling loop by checking that tunnel exit-point
340 * doesn't match source of incoming packet.
347 vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
349 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
353 * vti6_xmit - send a packet
354 * @skb: the outgoing socket buffer
355 * @dev: the outgoing tunnel device
357 static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
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;
364 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
365 struct net_device *tdev;
368 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
369 !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
372 dst = ip6_tnl_dst_check(t);
374 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
376 ndst = ip6_route_output(net, NULL, &fl6);
379 goto tx_err_link_failure;
380 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(&fl6), NULL, 0);
384 goto tx_err_link_failure;
389 if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
390 goto tx_err_link_failure;
396 net_warn_ratelimited("%s: Local routing loop detected!\n",
398 goto tx_err_dst_release;
403 skb_dst_set_noref(skb, dst);
405 ip6tunnel_xmit(skb, dev);
407 dev->mtu = dst_mtu(ndst);
408 ip6_tnl_dst_store(t, ndst);
413 stats->tx_carrier_errors++;
414 dst_link_failure(skb);
421 vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
423 struct ip6_tnl *t = netdev_priv(dev);
424 struct net_device_stats *stats = &t->dev->stats;
427 switch (skb->protocol) {
428 case htons(ETH_P_IPV6):
429 ret = vti6_xmit(skb, dev);
447 static void vti6_link_config(struct ip6_tnl *t)
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;
454 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
455 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
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;
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);
469 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
470 dev->flags |= IFF_POINTOPOINT;
472 dev->flags &= ~IFF_POINTOPOINT;
474 dev->iflink = p->link;
476 if (p->flags & IP6_TNL_F_CAP_XMIT) {
478 dst = ip6_route_output(dev_net(dev), NULL, fl6);
482 dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
488 dev->hard_header_len = dst->dev->hard_header_len;
490 dev->mtu = dst_mtu(dst);
492 if (dev->mtu < IPV6_MIN_MTU)
493 dev->mtu = IPV6_MIN_MTU;
500 * vti6_tnl_change - update the tunnel parameters
501 * @t: tunnel to be changed
502 * @p: tunnel configuration parameters
505 * vti6_tnl_change() updates the tunnel parameters
508 vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
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);
521 static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
523 struct net *net = dev_net(t->dev);
524 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
527 vti6_tnl_unlink(ip6n, t);
529 err = vti6_tnl_change(t, p);
530 vti6_tnl_link(ip6n, t);
531 netdev_state_change(t->dev);
536 vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
545 memcpy(p->name, u->name, sizeof(u->name));
549 vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
558 memcpy(u->name, p->name, sizeof(u->name));
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
568 * vti6_ioctl() is used for managing vti6 tunnels
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
577 * The fallback device "ip6_vti0", created during module
578 * initialization, can be used for creating other tunnel devices.
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
589 vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
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);
600 if (dev == ip6n->fb_tnl_dev) {
601 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
605 vti6_parm_from_user(&p1, &p);
606 t = vti6_locate(net, &p1, 0);
608 memset(&p, 0, sizeof(p));
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)))
619 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
622 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
625 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
627 vti6_parm_from_user(&p1, &p);
628 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
629 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
636 t = netdev_priv(dev);
638 err = vti6_update(t, &p1);
642 vti6_parm_to_user(&p, &t->parms);
643 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
647 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
651 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
654 if (dev == ip6n->fb_tnl_dev) {
656 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
659 vti6_parm_from_user(&p1, &p);
660 t = vti6_locate(net, &p1, 0);
664 if (t->dev == ip6n->fb_tnl_dev)
669 unregister_netdevice(dev);
678 * vti6_tnl_change_mtu - change mtu manually for tunnel device
679 * @dev: virtual device associated with tunnel
680 * @new_mtu: the new mtu
684 * %-EINVAL if mtu too small
686 static int vti6_change_mtu(struct net_device *dev, int new_mtu)
688 if (new_mtu < IPV6_MIN_MTU)
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,
704 * vti6_dev_setup - setup virtual tunnel device
705 * @dev: virtual device associated with tunnel
708 * Initialize function pointers and device parameters
710 static void vti6_dev_setup(struct net_device *dev)
714 dev->netdev_ops = &vti6_netdev_ops;
715 dev->destructor = vti6_dev_free;
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;
728 * vti6_dev_init_gen - general initializer for all tunnel devices
729 * @dev: virtual device associated with tunnel
731 static inline int vti6_dev_init_gen(struct net_device *dev)
733 struct ip6_tnl *t = netdev_priv(dev);
737 t->net = dev_net(dev);
738 dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
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);
750 * vti6_dev_init - initializer for all non fallback tunnel devices
751 * @dev: virtual device associated with tunnel
753 static int vti6_dev_init(struct net_device *dev)
755 struct ip6_tnl *t = netdev_priv(dev);
756 int err = vti6_dev_init_gen(dev);
765 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
766 * @dev: fallback device
770 static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
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);
780 t->parms.proto = IPPROTO_IPV6;
785 rcu_assign_pointer(ip6n->tnls_wc[0], t);
789 static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
794 static void vti6_netlink_parms(struct nlattr *data[],
795 struct __ip6_tnl_parm *parms)
797 memset(parms, 0, sizeof(*parms));
802 if (data[IFLA_VTI_LINK])
803 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
805 if (data[IFLA_VTI_LOCAL])
806 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
807 sizeof(struct in6_addr));
809 if (data[IFLA_VTI_REMOTE])
810 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
811 sizeof(struct in6_addr));
813 if (data[IFLA_VTI_IKEY])
814 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
816 if (data[IFLA_VTI_OKEY])
817 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
820 static int vti6_newlink(struct net *src_net, struct net_device *dev,
821 struct nlattr *tb[], struct nlattr *data[])
823 struct net *net = dev_net(dev);
826 nt = netdev_priv(dev);
827 vti6_netlink_parms(data, &nt->parms);
829 nt->parms.proto = IPPROTO_IPV6;
831 if (vti6_locate(net, &nt->parms, 0))
834 return vti6_tnl_create2(dev);
837 static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
838 struct nlattr *data[])
841 struct __ip6_tnl_parm p;
842 struct net *net = dev_net(dev);
843 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
845 if (dev == ip6n->fb_tnl_dev)
848 vti6_netlink_parms(data, &p);
850 t = vti6_locate(net, &p, 0);
856 t = netdev_priv(dev);
858 return vti6_update(t, &p);
861 static size_t vti6_get_size(const struct net_device *dev)
867 nla_total_size(sizeof(struct in6_addr)) +
868 /* IFLA_VTI_REMOTE */
869 nla_total_size(sizeof(struct in6_addr)) +
877 static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
879 struct ip6_tnl *tunnel = netdev_priv(dev);
880 struct __ip6_tnl_parm *parm = &tunnel->parms;
882 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
883 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
885 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
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;
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 },
904 static struct rtnl_link_ops vti6_link_ops __read_mostly = {
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,
917 static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
922 static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
928 for (h = 0; h < HASH_SIZE; h++) {
929 t = rtnl_dereference(ip6n->tnls_r_l[h]);
931 unregister_netdevice_queue(t->dev, &list);
932 t = rtnl_dereference(t->next);
936 t = rtnl_dereference(ip6n->tnls_wc[0]);
937 unregister_netdevice_queue(t->dev, &list);
938 unregister_netdevice_many(&list);
941 static int __net_init vti6_init_net(struct net *net)
943 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
944 struct ip6_tnl *t = NULL;
947 ip6n->tnls[0] = ip6n->tnls_wc;
948 ip6n->tnls[1] = ip6n->tnls_r_l;
951 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
954 if (!ip6n->fb_tnl_dev)
956 dev_net_set(ip6n->fb_tnl_dev, net);
958 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
962 err = register_netdev(ip6n->fb_tnl_dev);
966 t = netdev_priv(ip6n->fb_tnl_dev);
968 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
972 vti6_dev_free(ip6n->fb_tnl_dev);
977 static void __net_exit vti6_exit_net(struct net *net)
979 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
982 vti6_destroy_tunnels(ip6n);
986 static struct pernet_operations vti6_net_ops = {
987 .init = vti6_init_net,
988 .exit = vti6_exit_net,
990 .size = sizeof(struct vti6_net),
994 * vti6_tunnel_init - register protocol and reserve needed resources
996 * Return: 0 on success
998 static int __init vti6_tunnel_init(void)
1002 err = register_pernet_device(&vti6_net_ops);
1006 err = xfrm6_mode_tunnel_input_register(&vti6_handler);
1008 pr_err("%s: can't register vti6\n", __func__);
1011 err = rtnl_link_register(&vti6_link_ops);
1013 goto rtnl_link_failed;
1018 xfrm6_mode_tunnel_input_deregister(&vti6_handler);
1020 unregister_pernet_device(&vti6_net_ops);
1026 * vti6_tunnel_cleanup - free resources and unregister protocol
1028 static void __exit vti6_tunnel_cleanup(void)
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__);
1034 unregister_pernet_device(&vti6_net_ops);
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");