GNU Linux-libre 4.4.288-gnu1
[releases.git] / net / ipv4 / ip_vti.c
1 /*
2  *      Linux NET3: IP/IP protocol decoder modified to support
3  *                  virtual tunnel interface
4  *
5  *      Authors:
6  *              Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  *
13  */
14
15 /*
16    This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
17
18    For comments look at net/ipv4/ip_gre.c --ANK
19  */
20
21
22 #include <linux/capability.h>
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/uaccess.h>
27 #include <linux/skbuff.h>
28 #include <linux/netdevice.h>
29 #include <linux/in.h>
30 #include <linux/tcp.h>
31 #include <linux/udp.h>
32 #include <linux/if_arp.h>
33 #include <linux/mroute.h>
34 #include <linux/init.h>
35 #include <linux/netfilter_ipv4.h>
36 #include <linux/if_ether.h>
37 #include <linux/icmpv6.h>
38
39 #include <net/sock.h>
40 #include <net/ip.h>
41 #include <net/icmp.h>
42 #include <net/ip_tunnels.h>
43 #include <net/inet_ecn.h>
44 #include <net/xfrm.h>
45 #include <net/net_namespace.h>
46 #include <net/netns/generic.h>
47
48 static struct rtnl_link_ops vti_link_ops __read_mostly;
49
50 static int vti_net_id __read_mostly;
51 static int vti_tunnel_init(struct net_device *dev);
52
53 static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
54                      int encap_type, bool update_skb_dev)
55 {
56         struct ip_tunnel *tunnel;
57         const struct iphdr *iph = ip_hdr(skb);
58         struct net *net = dev_net(skb->dev);
59         struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
60
61         tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
62                                   iph->saddr, iph->daddr, 0);
63         if (tunnel) {
64                 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
65                         goto drop;
66
67                 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
68
69                 if (update_skb_dev)
70                         skb->dev = tunnel->dev;
71
72                 return xfrm_input(skb, nexthdr, spi, encap_type);
73         }
74
75         return -EINVAL;
76 drop:
77         kfree_skb(skb);
78         return 0;
79 }
80
81 static int vti_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
82                            int encap_type)
83 {
84         return vti_input(skb, nexthdr, spi, encap_type, false);
85 }
86
87 static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
88 {
89         XFRM_SPI_SKB_CB(skb)->family = AF_INET;
90         XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
91
92         return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
93 }
94
95 static int vti_rcv_proto(struct sk_buff *skb)
96 {
97         return vti_rcv(skb, 0, false);
98 }
99
100 static int vti_rcv_tunnel(struct sk_buff *skb)
101 {
102         struct ip_tunnel_net *itn = net_generic(dev_net(skb->dev), vti_net_id);
103         const struct iphdr *iph = ip_hdr(skb);
104         struct ip_tunnel *tunnel;
105
106         tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
107                                   iph->saddr, iph->daddr, 0);
108         if (tunnel) {
109                 struct tnl_ptk_info tpi = {
110                         .proto = htons(ETH_P_IP),
111                 };
112
113                 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
114                         goto drop;
115                 if (iptunnel_pull_header(skb, 0, tpi.proto))
116                         goto drop;
117                 return ip_tunnel_rcv(tunnel, skb, &tpi, NULL, false);
118         }
119
120         return -EINVAL;
121 drop:
122         kfree_skb(skb);
123         return 0;
124 }
125
126 static int vti_rcv_cb(struct sk_buff *skb, int err)
127 {
128         unsigned short family;
129         struct net_device *dev;
130         struct pcpu_sw_netstats *tstats;
131         struct xfrm_state *x;
132         struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
133         u32 orig_mark = skb->mark;
134         int ret;
135
136         if (!tunnel)
137                 return 1;
138
139         dev = tunnel->dev;
140
141         if (err) {
142                 dev->stats.rx_errors++;
143                 dev->stats.rx_dropped++;
144
145                 return 0;
146         }
147
148         x = xfrm_input_state(skb);
149         family = x->inner_mode->afinfo->family;
150
151         skb->mark = be32_to_cpu(tunnel->parms.i_key);
152         ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
153         skb->mark = orig_mark;
154
155         if (!ret)
156                 return -EPERM;
157
158         skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
159         skb->dev = dev;
160
161         tstats = this_cpu_ptr(dev->tstats);
162
163         u64_stats_update_begin(&tstats->syncp);
164         tstats->rx_packets++;
165         tstats->rx_bytes += skb->len;
166         u64_stats_update_end(&tstats->syncp);
167
168         return 0;
169 }
170
171 static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
172 {
173         xfrm_address_t *daddr = (xfrm_address_t *)&dst;
174         xfrm_address_t *saddr = (xfrm_address_t *)&src;
175
176         /* if there is no transform then this tunnel is not functional.
177          * Or if the xfrm is not mode tunnel.
178          */
179         if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
180             x->props.family != AF_INET)
181                 return false;
182
183         if (!dst)
184                 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
185
186         if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
187                 return false;
188
189         return true;
190 }
191
192 static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
193                             struct flowi *fl)
194 {
195         struct ip_tunnel *tunnel = netdev_priv(dev);
196         struct ip_tunnel_parm *parms = &tunnel->parms;
197         struct dst_entry *dst = skb_dst(skb);
198         struct net_device *tdev;        /* Device to other host */
199         int pkt_len = skb->len;
200         int err;
201
202         if (!dst) {
203                 switch (skb->protocol) {
204                 case htons(ETH_P_IP): {
205                         struct rtable *rt;
206
207                         fl->u.ip4.flowi4_oif = dev->ifindex;
208                         fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
209                         rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
210                         if (IS_ERR(rt)) {
211                                 dev->stats.tx_carrier_errors++;
212                                 goto tx_error_icmp;
213                         }
214                         dst = &rt->dst;
215                         skb_dst_set(skb, dst);
216                         break;
217                 }
218 #if IS_ENABLED(CONFIG_IPV6)
219                 case htons(ETH_P_IPV6):
220                         fl->u.ip6.flowi6_oif = dev->ifindex;
221                         fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
222                         dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
223                         if (dst->error) {
224                                 dst_release(dst);
225                                 dst = NULL;
226                                 dev->stats.tx_carrier_errors++;
227                                 goto tx_error_icmp;
228                         }
229                         skb_dst_set(skb, dst);
230                         break;
231 #endif
232                 default:
233                         dev->stats.tx_carrier_errors++;
234                         goto tx_error_icmp;
235                 }
236         }
237
238         dst_hold(dst);
239         dst = xfrm_lookup(tunnel->net, dst, fl, NULL, 0);
240         if (IS_ERR(dst)) {
241                 dev->stats.tx_carrier_errors++;
242                 goto tx_error_icmp;
243         }
244
245         if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
246                 dev->stats.tx_carrier_errors++;
247                 dst_release(dst);
248                 goto tx_error_icmp;
249         }
250
251         tdev = dst->dev;
252
253         if (tdev == dev) {
254                 dst_release(dst);
255                 dev->stats.collisions++;
256                 goto tx_error;
257         }
258
259         if (tunnel->err_count > 0) {
260                 if (time_before(jiffies,
261                                 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
262                         tunnel->err_count--;
263                         dst_link_failure(skb);
264                 } else
265                         tunnel->err_count = 0;
266         }
267
268         skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
269         skb_dst_set(skb, dst);
270         skb->dev = skb_dst(skb)->dev;
271
272         err = dst_output(tunnel->net, skb->sk, skb);
273         if (net_xmit_eval(err) == 0)
274                 err = pkt_len;
275         iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
276         return NETDEV_TX_OK;
277
278 tx_error_icmp:
279         dst_link_failure(skb);
280 tx_error:
281         dev->stats.tx_errors++;
282         kfree_skb(skb);
283         return NETDEV_TX_OK;
284 }
285
286 /* This function assumes it is being called from dev_queue_xmit()
287  * and that skb is filled properly by that function.
288  */
289 static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
290 {
291         struct ip_tunnel *tunnel = netdev_priv(dev);
292         struct flowi fl;
293
294         memset(&fl, 0, sizeof(fl));
295
296         switch (skb->protocol) {
297         case htons(ETH_P_IP):
298                 xfrm_decode_session(skb, &fl, AF_INET);
299                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
300                 break;
301         case htons(ETH_P_IPV6):
302                 xfrm_decode_session(skb, &fl, AF_INET6);
303                 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
304                 break;
305         default:
306                 dev->stats.tx_errors++;
307                 dev_kfree_skb(skb);
308                 return NETDEV_TX_OK;
309         }
310
311         /* override mark with tunnel output key */
312         fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
313
314         return vti_xmit(skb, dev, &fl);
315 }
316
317 static int vti4_err(struct sk_buff *skb, u32 info)
318 {
319         __be32 spi;
320         __u32 mark;
321         struct xfrm_state *x;
322         struct ip_tunnel *tunnel;
323         struct ip_esp_hdr *esph;
324         struct ip_auth_hdr *ah ;
325         struct ip_comp_hdr *ipch;
326         struct net *net = dev_net(skb->dev);
327         const struct iphdr *iph = (const struct iphdr *)skb->data;
328         int protocol = iph->protocol;
329         struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
330
331         tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
332                                   iph->daddr, iph->saddr, 0);
333         if (!tunnel)
334                 return -1;
335
336         mark = be32_to_cpu(tunnel->parms.o_key);
337
338         switch (protocol) {
339         case IPPROTO_ESP:
340                 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
341                 spi = esph->spi;
342                 break;
343         case IPPROTO_AH:
344                 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
345                 spi = ah->spi;
346                 break;
347         case IPPROTO_COMP:
348                 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
349                 spi = htonl(ntohs(ipch->cpi));
350                 break;
351         default:
352                 return 0;
353         }
354
355         switch (icmp_hdr(skb)->type) {
356         case ICMP_DEST_UNREACH:
357                 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
358                         return 0;
359         case ICMP_REDIRECT:
360                 break;
361         default:
362                 return 0;
363         }
364
365         x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
366                               spi, protocol, AF_INET);
367         if (!x)
368                 return 0;
369
370         if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
371                 ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0);
372         else
373                 ipv4_redirect(skb, net, 0, 0, protocol, 0);
374         xfrm_state_put(x);
375
376         return 0;
377 }
378
379 static int
380 vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
381 {
382         int err = 0;
383         struct ip_tunnel_parm p;
384
385         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
386                 return -EFAULT;
387
388         if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
389                 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
390                     p.iph.ihl != 5)
391                         return -EINVAL;
392         }
393
394         if (!(p.i_flags & GRE_KEY))
395                 p.i_key = 0;
396         if (!(p.o_flags & GRE_KEY))
397                 p.o_key = 0;
398
399         p.i_flags = VTI_ISVTI;
400
401         err = ip_tunnel_ioctl(dev, &p, cmd);
402         if (err)
403                 return err;
404
405         if (cmd != SIOCDELTUNNEL) {
406                 p.i_flags |= GRE_KEY;
407                 p.o_flags |= GRE_KEY;
408         }
409
410         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
411                 return -EFAULT;
412         return 0;
413 }
414
415 static const struct net_device_ops vti_netdev_ops = {
416         .ndo_init       = vti_tunnel_init,
417         .ndo_uninit     = ip_tunnel_uninit,
418         .ndo_start_xmit = vti_tunnel_xmit,
419         .ndo_do_ioctl   = vti_tunnel_ioctl,
420         .ndo_change_mtu = ip_tunnel_change_mtu,
421         .ndo_get_stats64 = ip_tunnel_get_stats64,
422         .ndo_get_iflink = ip_tunnel_get_iflink,
423 };
424
425 static void vti_tunnel_setup(struct net_device *dev)
426 {
427         dev->netdev_ops         = &vti_netdev_ops;
428         dev->type               = ARPHRD_TUNNEL;
429         ip_tunnel_setup(dev, vti_net_id);
430 }
431
432 static int vti_tunnel_init(struct net_device *dev)
433 {
434         struct ip_tunnel *tunnel = netdev_priv(dev);
435         struct iphdr *iph = &tunnel->parms.iph;
436
437         memcpy(dev->dev_addr, &iph->saddr, 4);
438         memcpy(dev->broadcast, &iph->daddr, 4);
439
440         dev->mtu                = ETH_DATA_LEN;
441         dev->flags              = IFF_NOARP;
442         dev->addr_len           = 4;
443         dev->features           |= NETIF_F_LLTX;
444         netif_keep_dst(dev);
445
446         return ip_tunnel_init(dev);
447 }
448
449 static void __net_init vti_fb_tunnel_init(struct net_device *dev)
450 {
451         struct ip_tunnel *tunnel = netdev_priv(dev);
452         struct iphdr *iph = &tunnel->parms.iph;
453
454         iph->version            = 4;
455         iph->protocol           = IPPROTO_IPIP;
456         iph->ihl                = 5;
457 }
458
459 static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
460         .handler        =       vti_rcv_proto,
461         .input_handler  =       vti_input_proto,
462         .cb_handler     =       vti_rcv_cb,
463         .err_handler    =       vti4_err,
464         .priority       =       100,
465 };
466
467 static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
468         .handler        =       vti_rcv_proto,
469         .input_handler  =       vti_input_proto,
470         .cb_handler     =       vti_rcv_cb,
471         .err_handler    =       vti4_err,
472         .priority       =       100,
473 };
474
475 static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
476         .handler        =       vti_rcv_proto,
477         .input_handler  =       vti_input_proto,
478         .cb_handler     =       vti_rcv_cb,
479         .err_handler    =       vti4_err,
480         .priority       =       100,
481 };
482
483 static struct xfrm_tunnel ipip_handler __read_mostly = {
484         .handler        =       vti_rcv_tunnel,
485         .err_handler    =       vti4_err,
486         .priority       =       0,
487 };
488
489 static int __net_init vti_init_net(struct net *net)
490 {
491         int err;
492         struct ip_tunnel_net *itn;
493
494         err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
495         if (err)
496                 return err;
497         itn = net_generic(net, vti_net_id);
498         vti_fb_tunnel_init(itn->fb_tunnel_dev);
499         return 0;
500 }
501
502 static void __net_exit vti_exit_net(struct net *net)
503 {
504         struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
505         ip_tunnel_delete_net(itn, &vti_link_ops);
506 }
507
508 static struct pernet_operations vti_net_ops = {
509         .init = vti_init_net,
510         .exit = vti_exit_net,
511         .id   = &vti_net_id,
512         .size = sizeof(struct ip_tunnel_net),
513 };
514
515 static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
516 {
517         return 0;
518 }
519
520 static void vti_netlink_parms(struct nlattr *data[],
521                               struct ip_tunnel_parm *parms)
522 {
523         memset(parms, 0, sizeof(*parms));
524
525         parms->iph.protocol = IPPROTO_IPIP;
526
527         if (!data)
528                 return;
529
530         parms->i_flags = VTI_ISVTI;
531
532         if (data[IFLA_VTI_LINK])
533                 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
534
535         if (data[IFLA_VTI_IKEY])
536                 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
537
538         if (data[IFLA_VTI_OKEY])
539                 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
540
541         if (data[IFLA_VTI_LOCAL])
542                 parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
543
544         if (data[IFLA_VTI_REMOTE])
545                 parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
546
547 }
548
549 static int vti_newlink(struct net *src_net, struct net_device *dev,
550                        struct nlattr *tb[], struct nlattr *data[])
551 {
552         struct ip_tunnel_parm parms;
553
554         vti_netlink_parms(data, &parms);
555         return ip_tunnel_newlink(dev, tb, &parms);
556 }
557
558 static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
559                           struct nlattr *data[])
560 {
561         struct ip_tunnel_parm p;
562
563         vti_netlink_parms(data, &p);
564         return ip_tunnel_changelink(dev, tb, &p);
565 }
566
567 static size_t vti_get_size(const struct net_device *dev)
568 {
569         return
570                 /* IFLA_VTI_LINK */
571                 nla_total_size(4) +
572                 /* IFLA_VTI_IKEY */
573                 nla_total_size(4) +
574                 /* IFLA_VTI_OKEY */
575                 nla_total_size(4) +
576                 /* IFLA_VTI_LOCAL */
577                 nla_total_size(4) +
578                 /* IFLA_VTI_REMOTE */
579                 nla_total_size(4) +
580                 0;
581 }
582
583 static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
584 {
585         struct ip_tunnel *t = netdev_priv(dev);
586         struct ip_tunnel_parm *p = &t->parms;
587
588         nla_put_u32(skb, IFLA_VTI_LINK, p->link);
589         nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
590         nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
591         nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr);
592         nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr);
593
594         return 0;
595 }
596
597 static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
598         [IFLA_VTI_LINK]         = { .type = NLA_U32 },
599         [IFLA_VTI_IKEY]         = { .type = NLA_U32 },
600         [IFLA_VTI_OKEY]         = { .type = NLA_U32 },
601         [IFLA_VTI_LOCAL]        = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
602         [IFLA_VTI_REMOTE]       = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
603 };
604
605 static struct rtnl_link_ops vti_link_ops __read_mostly = {
606         .kind           = "vti",
607         .maxtype        = IFLA_VTI_MAX,
608         .policy         = vti_policy,
609         .priv_size      = sizeof(struct ip_tunnel),
610         .setup          = vti_tunnel_setup,
611         .validate       = vti_tunnel_validate,
612         .newlink        = vti_newlink,
613         .changelink     = vti_changelink,
614         .dellink        = ip_tunnel_dellink,
615         .get_size       = vti_get_size,
616         .fill_info      = vti_fill_info,
617         .get_link_net   = ip_tunnel_get_link_net,
618 };
619
620 static bool is_vti_tunnel(const struct net_device *dev)
621 {
622         return dev->netdev_ops == &vti_netdev_ops;
623 }
624
625 static int vti_device_event(struct notifier_block *unused,
626                             unsigned long event, void *ptr)
627 {
628         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
629         struct ip_tunnel *tunnel = netdev_priv(dev);
630
631         if (!is_vti_tunnel(dev))
632                 return NOTIFY_DONE;
633
634         switch (event) {
635         case NETDEV_DOWN:
636                 if (!net_eq(tunnel->net, dev_net(dev)))
637                         xfrm_garbage_collect(tunnel->net);
638                 break;
639         }
640         return NOTIFY_DONE;
641 }
642
643 static struct notifier_block vti_notifier_block __read_mostly = {
644         .notifier_call = vti_device_event,
645 };
646
647 static int __init vti_init(void)
648 {
649         const char *msg;
650         int err;
651
652         pr_info("IPv4 over IPsec tunneling driver\n");
653
654         register_netdevice_notifier(&vti_notifier_block);
655
656         msg = "tunnel device";
657         err = register_pernet_device(&vti_net_ops);
658         if (err < 0)
659                 goto pernet_dev_failed;
660
661         msg = "tunnel protocols";
662         err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
663         if (err < 0)
664                 goto xfrm_proto_esp_failed;
665         err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
666         if (err < 0)
667                 goto xfrm_proto_ah_failed;
668         err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
669         if (err < 0)
670                 goto xfrm_proto_comp_failed;
671
672         msg = "ipip tunnel";
673         err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
674         if (err < 0)
675                 goto xfrm_tunnel_failed;
676
677         msg = "netlink interface";
678         err = rtnl_link_register(&vti_link_ops);
679         if (err < 0)
680                 goto rtnl_link_failed;
681
682         return err;
683
684 rtnl_link_failed:
685         xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
686 xfrm_tunnel_failed:
687         xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
688 xfrm_proto_comp_failed:
689         xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
690 xfrm_proto_ah_failed:
691         xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
692 xfrm_proto_esp_failed:
693         unregister_pernet_device(&vti_net_ops);
694 pernet_dev_failed:
695         unregister_netdevice_notifier(&vti_notifier_block);
696         pr_err("vti init: failed to register %s\n", msg);
697         return err;
698 }
699
700 static void __exit vti_fini(void)
701 {
702         rtnl_link_unregister(&vti_link_ops);
703         xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
704         xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
705         xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
706         xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
707         unregister_pernet_device(&vti_net_ops);
708         unregister_netdevice_notifier(&vti_notifier_block);
709 }
710
711 module_init(vti_init);
712 module_exit(vti_fini);
713 MODULE_LICENSE("GPL");
714 MODULE_ALIAS_RTNL_LINK("vti");
715 MODULE_ALIAS_NETDEV("ip_vti0");