Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Linux NET3: IP/IP protocol decoder modified to support
0004  *          virtual tunnel interface
0005  *
0006  *  Authors:
0007  *      Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012
0008  */
0009 
0010 /*
0011    This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
0012 
0013    For comments look at net/ipv4/ip_gre.c --ANK
0014  */
0015 
0016 
0017 #include <linux/capability.h>
0018 #include <linux/module.h>
0019 #include <linux/types.h>
0020 #include <linux/kernel.h>
0021 #include <linux/uaccess.h>
0022 #include <linux/skbuff.h>
0023 #include <linux/netdevice.h>
0024 #include <linux/in.h>
0025 #include <linux/tcp.h>
0026 #include <linux/udp.h>
0027 #include <linux/if_arp.h>
0028 #include <linux/init.h>
0029 #include <linux/netfilter_ipv4.h>
0030 #include <linux/if_ether.h>
0031 #include <linux/icmpv6.h>
0032 
0033 #include <net/sock.h>
0034 #include <net/ip.h>
0035 #include <net/icmp.h>
0036 #include <net/ip_tunnels.h>
0037 #include <net/inet_ecn.h>
0038 #include <net/xfrm.h>
0039 #include <net/net_namespace.h>
0040 #include <net/netns/generic.h>
0041 
0042 static struct rtnl_link_ops vti_link_ops __read_mostly;
0043 
0044 static unsigned int vti_net_id __read_mostly;
0045 static int vti_tunnel_init(struct net_device *dev);
0046 
0047 static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
0048              int encap_type, bool update_skb_dev)
0049 {
0050     struct ip_tunnel *tunnel;
0051     const struct iphdr *iph = ip_hdr(skb);
0052     struct net *net = dev_net(skb->dev);
0053     struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
0054 
0055     tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
0056                   iph->saddr, iph->daddr, 0);
0057     if (tunnel) {
0058         if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
0059             goto drop;
0060 
0061         XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
0062 
0063         if (update_skb_dev)
0064             skb->dev = tunnel->dev;
0065 
0066         return xfrm_input(skb, nexthdr, spi, encap_type);
0067     }
0068 
0069     return -EINVAL;
0070 drop:
0071     kfree_skb(skb);
0072     return 0;
0073 }
0074 
0075 static int vti_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
0076                int encap_type)
0077 {
0078     return vti_input(skb, nexthdr, spi, encap_type, false);
0079 }
0080 
0081 static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
0082 {
0083     XFRM_SPI_SKB_CB(skb)->family = AF_INET;
0084     XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
0085 
0086     return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
0087 }
0088 
0089 static int vti_rcv_proto(struct sk_buff *skb)
0090 {
0091     return vti_rcv(skb, 0, false);
0092 }
0093 
0094 static int vti_rcv_cb(struct sk_buff *skb, int err)
0095 {
0096     unsigned short family;
0097     struct net_device *dev;
0098     struct xfrm_state *x;
0099     const struct xfrm_mode *inner_mode;
0100     struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
0101     u32 orig_mark = skb->mark;
0102     int ret;
0103 
0104     if (!tunnel)
0105         return 1;
0106 
0107     dev = tunnel->dev;
0108 
0109     if (err) {
0110         dev->stats.rx_errors++;
0111         dev->stats.rx_dropped++;
0112 
0113         return 0;
0114     }
0115 
0116     x = xfrm_input_state(skb);
0117 
0118     inner_mode = &x->inner_mode;
0119 
0120     if (x->sel.family == AF_UNSPEC) {
0121         inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
0122         if (inner_mode == NULL) {
0123             XFRM_INC_STATS(dev_net(skb->dev),
0124                        LINUX_MIB_XFRMINSTATEMODEERROR);
0125             return -EINVAL;
0126         }
0127     }
0128 
0129     family = inner_mode->family;
0130 
0131     skb->mark = be32_to_cpu(tunnel->parms.i_key);
0132     ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
0133     skb->mark = orig_mark;
0134 
0135     if (!ret)
0136         return -EPERM;
0137 
0138     skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
0139     skb->dev = dev;
0140     dev_sw_netstats_rx_add(dev, skb->len);
0141 
0142     return 0;
0143 }
0144 
0145 static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
0146 {
0147     xfrm_address_t *daddr = (xfrm_address_t *)&dst;
0148     xfrm_address_t *saddr = (xfrm_address_t *)&src;
0149 
0150     /* if there is no transform then this tunnel is not functional.
0151      * Or if the xfrm is not mode tunnel.
0152      */
0153     if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
0154         x->props.family != AF_INET)
0155         return false;
0156 
0157     if (!dst)
0158         return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
0159 
0160     if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
0161         return false;
0162 
0163     return true;
0164 }
0165 
0166 static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
0167                 struct flowi *fl)
0168 {
0169     struct ip_tunnel *tunnel = netdev_priv(dev);
0170     struct ip_tunnel_parm *parms = &tunnel->parms;
0171     struct dst_entry *dst = skb_dst(skb);
0172     struct net_device *tdev;    /* Device to other host */
0173     int pkt_len = skb->len;
0174     int err;
0175     int mtu;
0176 
0177     if (!dst) {
0178         switch (skb->protocol) {
0179         case htons(ETH_P_IP): {
0180             struct rtable *rt;
0181 
0182             fl->u.ip4.flowi4_oif = dev->ifindex;
0183             fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
0184             rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
0185             if (IS_ERR(rt)) {
0186                 dev->stats.tx_carrier_errors++;
0187                 goto tx_error_icmp;
0188             }
0189             dst = &rt->dst;
0190             skb_dst_set(skb, dst);
0191             break;
0192         }
0193 #if IS_ENABLED(CONFIG_IPV6)
0194         case htons(ETH_P_IPV6):
0195             fl->u.ip6.flowi6_oif = dev->ifindex;
0196             fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
0197             dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
0198             if (dst->error) {
0199                 dst_release(dst);
0200                 dst = NULL;
0201                 dev->stats.tx_carrier_errors++;
0202                 goto tx_error_icmp;
0203             }
0204             skb_dst_set(skb, dst);
0205             break;
0206 #endif
0207         default:
0208             dev->stats.tx_carrier_errors++;
0209             goto tx_error_icmp;
0210         }
0211     }
0212 
0213     dst_hold(dst);
0214     dst = xfrm_lookup_route(tunnel->net, dst, fl, NULL, 0);
0215     if (IS_ERR(dst)) {
0216         dev->stats.tx_carrier_errors++;
0217         goto tx_error_icmp;
0218     }
0219 
0220     if (dst->flags & DST_XFRM_QUEUE)
0221         goto xmit;
0222 
0223     if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
0224         dev->stats.tx_carrier_errors++;
0225         dst_release(dst);
0226         goto tx_error_icmp;
0227     }
0228 
0229     tdev = dst->dev;
0230 
0231     if (tdev == dev) {
0232         dst_release(dst);
0233         dev->stats.collisions++;
0234         goto tx_error;
0235     }
0236 
0237     mtu = dst_mtu(dst);
0238     if (skb->len > mtu) {
0239         skb_dst_update_pmtu_no_confirm(skb, mtu);
0240         if (skb->protocol == htons(ETH_P_IP)) {
0241             if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
0242                 goto xmit;
0243             icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
0244                       htonl(mtu));
0245         } else {
0246             if (mtu < IPV6_MIN_MTU)
0247                 mtu = IPV6_MIN_MTU;
0248 
0249             icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
0250         }
0251 
0252         dst_release(dst);
0253         goto tx_error;
0254     }
0255 
0256 xmit:
0257     skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
0258     skb_dst_set(skb, dst);
0259     skb->dev = skb_dst(skb)->dev;
0260 
0261     err = dst_output(tunnel->net, skb->sk, skb);
0262     if (net_xmit_eval(err) == 0)
0263         err = pkt_len;
0264     iptunnel_xmit_stats(dev, err);
0265     return NETDEV_TX_OK;
0266 
0267 tx_error_icmp:
0268     dst_link_failure(skb);
0269 tx_error:
0270     dev->stats.tx_errors++;
0271     kfree_skb(skb);
0272     return NETDEV_TX_OK;
0273 }
0274 
0275 /* This function assumes it is being called from dev_queue_xmit()
0276  * and that skb is filled properly by that function.
0277  */
0278 static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
0279 {
0280     struct ip_tunnel *tunnel = netdev_priv(dev);
0281     struct flowi fl;
0282 
0283     if (!pskb_inet_may_pull(skb))
0284         goto tx_err;
0285 
0286     memset(&fl, 0, sizeof(fl));
0287 
0288     switch (skb->protocol) {
0289     case htons(ETH_P_IP):
0290         xfrm_decode_session(skb, &fl, AF_INET);
0291         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
0292         break;
0293     case htons(ETH_P_IPV6):
0294         xfrm_decode_session(skb, &fl, AF_INET6);
0295         memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
0296         break;
0297     default:
0298         goto tx_err;
0299     }
0300 
0301     /* override mark with tunnel output key */
0302     fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
0303 
0304     return vti_xmit(skb, dev, &fl);
0305 
0306 tx_err:
0307     dev->stats.tx_errors++;
0308     kfree_skb(skb);
0309     return NETDEV_TX_OK;
0310 }
0311 
0312 static int vti4_err(struct sk_buff *skb, u32 info)
0313 {
0314     __be32 spi;
0315     __u32 mark;
0316     struct xfrm_state *x;
0317     struct ip_tunnel *tunnel;
0318     struct ip_esp_hdr *esph;
0319     struct ip_auth_hdr *ah ;
0320     struct ip_comp_hdr *ipch;
0321     struct net *net = dev_net(skb->dev);
0322     const struct iphdr *iph = (const struct iphdr *)skb->data;
0323     int protocol = iph->protocol;
0324     struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
0325 
0326     tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
0327                   iph->daddr, iph->saddr, 0);
0328     if (!tunnel)
0329         return -1;
0330 
0331     mark = be32_to_cpu(tunnel->parms.o_key);
0332 
0333     switch (protocol) {
0334     case IPPROTO_ESP:
0335         esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
0336         spi = esph->spi;
0337         break;
0338     case IPPROTO_AH:
0339         ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
0340         spi = ah->spi;
0341         break;
0342     case IPPROTO_COMP:
0343         ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
0344         spi = htonl(ntohs(ipch->cpi));
0345         break;
0346     default:
0347         return 0;
0348     }
0349 
0350     switch (icmp_hdr(skb)->type) {
0351     case ICMP_DEST_UNREACH:
0352         if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
0353             return 0;
0354         break;
0355     case ICMP_REDIRECT:
0356         break;
0357     default:
0358         return 0;
0359     }
0360 
0361     x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
0362                   spi, protocol, AF_INET);
0363     if (!x)
0364         return 0;
0365 
0366     if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
0367         ipv4_update_pmtu(skb, net, info, 0, protocol);
0368     else
0369         ipv4_redirect(skb, net, 0, protocol);
0370     xfrm_state_put(x);
0371 
0372     return 0;
0373 }
0374 
0375 static int
0376 vti_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
0377 {
0378     int err = 0;
0379 
0380     if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
0381         if (p->iph.version != 4 || p->iph.protocol != IPPROTO_IPIP ||
0382             p->iph.ihl != 5)
0383             return -EINVAL;
0384     }
0385 
0386     if (!(p->i_flags & GRE_KEY))
0387         p->i_key = 0;
0388     if (!(p->o_flags & GRE_KEY))
0389         p->o_key = 0;
0390 
0391     p->i_flags = VTI_ISVTI;
0392 
0393     err = ip_tunnel_ctl(dev, p, cmd);
0394     if (err)
0395         return err;
0396 
0397     if (cmd != SIOCDELTUNNEL) {
0398         p->i_flags |= GRE_KEY;
0399         p->o_flags |= GRE_KEY;
0400     }
0401     return 0;
0402 }
0403 
0404 static const struct net_device_ops vti_netdev_ops = {
0405     .ndo_init   = vti_tunnel_init,
0406     .ndo_uninit = ip_tunnel_uninit,
0407     .ndo_start_xmit = vti_tunnel_xmit,
0408     .ndo_siocdevprivate = ip_tunnel_siocdevprivate,
0409     .ndo_change_mtu = ip_tunnel_change_mtu,
0410     .ndo_get_stats64 = dev_get_tstats64,
0411     .ndo_get_iflink = ip_tunnel_get_iflink,
0412     .ndo_tunnel_ctl = vti_tunnel_ctl,
0413 };
0414 
0415 static void vti_tunnel_setup(struct net_device *dev)
0416 {
0417     dev->netdev_ops     = &vti_netdev_ops;
0418     dev->header_ops     = &ip_tunnel_header_ops;
0419     dev->type       = ARPHRD_TUNNEL;
0420     ip_tunnel_setup(dev, vti_net_id);
0421 }
0422 
0423 static int vti_tunnel_init(struct net_device *dev)
0424 {
0425     struct ip_tunnel *tunnel = netdev_priv(dev);
0426     struct iphdr *iph = &tunnel->parms.iph;
0427 
0428     __dev_addr_set(dev, &iph->saddr, 4);
0429     memcpy(dev->broadcast, &iph->daddr, 4);
0430 
0431     dev->flags      = IFF_NOARP;
0432     dev->addr_len       = 4;
0433     dev->features       |= NETIF_F_LLTX;
0434     netif_keep_dst(dev);
0435 
0436     return ip_tunnel_init(dev);
0437 }
0438 
0439 static void __net_init vti_fb_tunnel_init(struct net_device *dev)
0440 {
0441     struct ip_tunnel *tunnel = netdev_priv(dev);
0442     struct iphdr *iph = &tunnel->parms.iph;
0443 
0444     iph->version        = 4;
0445     iph->protocol       = IPPROTO_IPIP;
0446     iph->ihl        = 5;
0447 }
0448 
0449 static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
0450     .handler    =   vti_rcv_proto,
0451     .input_handler  =   vti_input_proto,
0452     .cb_handler =   vti_rcv_cb,
0453     .err_handler    =   vti4_err,
0454     .priority   =   100,
0455 };
0456 
0457 static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
0458     .handler    =   vti_rcv_proto,
0459     .input_handler  =   vti_input_proto,
0460     .cb_handler =   vti_rcv_cb,
0461     .err_handler    =   vti4_err,
0462     .priority   =   100,
0463 };
0464 
0465 static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
0466     .handler    =   vti_rcv_proto,
0467     .input_handler  =   vti_input_proto,
0468     .cb_handler =   vti_rcv_cb,
0469     .err_handler    =   vti4_err,
0470     .priority   =   100,
0471 };
0472 
0473 #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
0474 static int vti_rcv_tunnel(struct sk_buff *skb)
0475 {
0476     XFRM_SPI_SKB_CB(skb)->family = AF_INET;
0477     XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
0478 
0479     return vti_input(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr, 0, false);
0480 }
0481 
0482 static struct xfrm_tunnel vti_ipip_handler __read_mostly = {
0483     .handler    =   vti_rcv_tunnel,
0484     .cb_handler =   vti_rcv_cb,
0485     .err_handler    =   vti4_err,
0486     .priority   =   0,
0487 };
0488 
0489 #if IS_ENABLED(CONFIG_IPV6)
0490 static struct xfrm_tunnel vti_ipip6_handler __read_mostly = {
0491     .handler    =   vti_rcv_tunnel,
0492     .cb_handler =   vti_rcv_cb,
0493     .err_handler    =   vti4_err,
0494     .priority   =   0,
0495 };
0496 #endif
0497 #endif
0498 
0499 static int __net_init vti_init_net(struct net *net)
0500 {
0501     int err;
0502     struct ip_tunnel_net *itn;
0503 
0504     err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
0505     if (err)
0506         return err;
0507     itn = net_generic(net, vti_net_id);
0508     if (itn->fb_tunnel_dev)
0509         vti_fb_tunnel_init(itn->fb_tunnel_dev);
0510     return 0;
0511 }
0512 
0513 static void __net_exit vti_exit_batch_net(struct list_head *list_net)
0514 {
0515     ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops);
0516 }
0517 
0518 static struct pernet_operations vti_net_ops = {
0519     .init = vti_init_net,
0520     .exit_batch = vti_exit_batch_net,
0521     .id   = &vti_net_id,
0522     .size = sizeof(struct ip_tunnel_net),
0523 };
0524 
0525 static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
0526                    struct netlink_ext_ack *extack)
0527 {
0528     return 0;
0529 }
0530 
0531 static void vti_netlink_parms(struct nlattr *data[],
0532                   struct ip_tunnel_parm *parms,
0533                   __u32 *fwmark)
0534 {
0535     memset(parms, 0, sizeof(*parms));
0536 
0537     parms->iph.protocol = IPPROTO_IPIP;
0538 
0539     if (!data)
0540         return;
0541 
0542     parms->i_flags = VTI_ISVTI;
0543 
0544     if (data[IFLA_VTI_LINK])
0545         parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
0546 
0547     if (data[IFLA_VTI_IKEY])
0548         parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
0549 
0550     if (data[IFLA_VTI_OKEY])
0551         parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
0552 
0553     if (data[IFLA_VTI_LOCAL])
0554         parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
0555 
0556     if (data[IFLA_VTI_REMOTE])
0557         parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
0558 
0559     if (data[IFLA_VTI_FWMARK])
0560         *fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
0561 }
0562 
0563 static int vti_newlink(struct net *src_net, struct net_device *dev,
0564                struct nlattr *tb[], struct nlattr *data[],
0565                struct netlink_ext_ack *extack)
0566 {
0567     struct ip_tunnel_parm parms;
0568     __u32 fwmark = 0;
0569 
0570     vti_netlink_parms(data, &parms, &fwmark);
0571     return ip_tunnel_newlink(dev, tb, &parms, fwmark);
0572 }
0573 
0574 static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
0575               struct nlattr *data[],
0576               struct netlink_ext_ack *extack)
0577 {
0578     struct ip_tunnel *t = netdev_priv(dev);
0579     __u32 fwmark = t->fwmark;
0580     struct ip_tunnel_parm p;
0581 
0582     vti_netlink_parms(data, &p, &fwmark);
0583     return ip_tunnel_changelink(dev, tb, &p, fwmark);
0584 }
0585 
0586 static size_t vti_get_size(const struct net_device *dev)
0587 {
0588     return
0589         /* IFLA_VTI_LINK */
0590         nla_total_size(4) +
0591         /* IFLA_VTI_IKEY */
0592         nla_total_size(4) +
0593         /* IFLA_VTI_OKEY */
0594         nla_total_size(4) +
0595         /* IFLA_VTI_LOCAL */
0596         nla_total_size(4) +
0597         /* IFLA_VTI_REMOTE */
0598         nla_total_size(4) +
0599         /* IFLA_VTI_FWMARK */
0600         nla_total_size(4) +
0601         0;
0602 }
0603 
0604 static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
0605 {
0606     struct ip_tunnel *t = netdev_priv(dev);
0607     struct ip_tunnel_parm *p = &t->parms;
0608 
0609     if (nla_put_u32(skb, IFLA_VTI_LINK, p->link) ||
0610         nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key) ||
0611         nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key) ||
0612         nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr) ||
0613         nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr) ||
0614         nla_put_u32(skb, IFLA_VTI_FWMARK, t->fwmark))
0615         return -EMSGSIZE;
0616 
0617     return 0;
0618 }
0619 
0620 static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
0621     [IFLA_VTI_LINK]     = { .type = NLA_U32 },
0622     [IFLA_VTI_IKEY]     = { .type = NLA_U32 },
0623     [IFLA_VTI_OKEY]     = { .type = NLA_U32 },
0624     [IFLA_VTI_LOCAL]    = { .len = sizeof_field(struct iphdr, saddr) },
0625     [IFLA_VTI_REMOTE]   = { .len = sizeof_field(struct iphdr, daddr) },
0626     [IFLA_VTI_FWMARK]   = { .type = NLA_U32 },
0627 };
0628 
0629 static struct rtnl_link_ops vti_link_ops __read_mostly = {
0630     .kind       = "vti",
0631     .maxtype    = IFLA_VTI_MAX,
0632     .policy     = vti_policy,
0633     .priv_size  = sizeof(struct ip_tunnel),
0634     .setup      = vti_tunnel_setup,
0635     .validate   = vti_tunnel_validate,
0636     .newlink    = vti_newlink,
0637     .changelink = vti_changelink,
0638     .dellink        = ip_tunnel_dellink,
0639     .get_size   = vti_get_size,
0640     .fill_info  = vti_fill_info,
0641     .get_link_net   = ip_tunnel_get_link_net,
0642 };
0643 
0644 static int __init vti_init(void)
0645 {
0646     const char *msg;
0647     int err;
0648 
0649     pr_info("IPv4 over IPsec tunneling driver\n");
0650 
0651     msg = "tunnel device";
0652     err = register_pernet_device(&vti_net_ops);
0653     if (err < 0)
0654         goto pernet_dev_failed;
0655 
0656     msg = "tunnel protocols";
0657     err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
0658     if (err < 0)
0659         goto xfrm_proto_esp_failed;
0660     err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
0661     if (err < 0)
0662         goto xfrm_proto_ah_failed;
0663     err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
0664     if (err < 0)
0665         goto xfrm_proto_comp_failed;
0666 
0667 #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
0668     msg = "ipip tunnel";
0669     err = xfrm4_tunnel_register(&vti_ipip_handler, AF_INET);
0670     if (err < 0)
0671         goto xfrm_tunnel_ipip_failed;
0672 #if IS_ENABLED(CONFIG_IPV6)
0673     err = xfrm4_tunnel_register(&vti_ipip6_handler, AF_INET6);
0674     if (err < 0)
0675         goto xfrm_tunnel_ipip6_failed;
0676 #endif
0677 #endif
0678 
0679     msg = "netlink interface";
0680     err = rtnl_link_register(&vti_link_ops);
0681     if (err < 0)
0682         goto rtnl_link_failed;
0683 
0684     return err;
0685 
0686 rtnl_link_failed:
0687 #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
0688 #if IS_ENABLED(CONFIG_IPV6)
0689     xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
0690 xfrm_tunnel_ipip6_failed:
0691 #endif
0692     xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
0693 xfrm_tunnel_ipip_failed:
0694 #endif
0695     xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
0696 xfrm_proto_comp_failed:
0697     xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
0698 xfrm_proto_ah_failed:
0699     xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
0700 xfrm_proto_esp_failed:
0701     unregister_pernet_device(&vti_net_ops);
0702 pernet_dev_failed:
0703     pr_err("vti init: failed to register %s\n", msg);
0704     return err;
0705 }
0706 
0707 static void __exit vti_fini(void)
0708 {
0709     rtnl_link_unregister(&vti_link_ops);
0710 #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
0711 #if IS_ENABLED(CONFIG_IPV6)
0712     xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
0713 #endif
0714     xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
0715 #endif
0716     xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
0717     xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
0718     xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
0719     unregister_pernet_device(&vti_net_ops);
0720 }
0721 
0722 module_init(vti_init);
0723 module_exit(vti_fini);
0724 MODULE_LICENSE("GPL");
0725 MODULE_ALIAS_RTNL_LINK("vti");
0726 MODULE_ALIAS_NETDEV("ip_vti0");