blob: b23f9e63b1cd0a595ef795fdfd69f07ace3d4cda [file] [log] [blame]
Saurabh11814122012-07-17 09:44:54 +00001/*
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
38#include <net/sock.h>
39#include <net/ip.h>
40#include <net/icmp.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000041#include <net/ip_tunnels.h>
Saurabh11814122012-07-17 09:44:54 +000042#include <net/inet_ecn.h>
43#include <net/xfrm.h>
44#include <net/net_namespace.h>
45#include <net/netns/generic.h>
46
Saurabh11814122012-07-17 09:44:54 +000047static struct rtnl_link_ops vti_link_ops __read_mostly;
48
49static int vti_net_id __read_mostly;
Saurabh11814122012-07-17 09:44:54 +000050static int vti_tunnel_init(struct net_device *dev);
Saurabh11814122012-07-17 09:44:54 +000051
Steffen Klassertdf3893c2014-02-21 08:41:10 +010052static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
53 int encap_type)
Saurabh11814122012-07-17 09:44:54 +000054{
55 struct ip_tunnel *tunnel;
56 const struct iphdr *iph = ip_hdr(skb);
Amerigo Wangb9959fd2013-07-21 10:46:25 +080057 struct net *net = dev_net(skb->dev);
58 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
Saurabh11814122012-07-17 09:44:54 +000059
Amerigo Wangb9959fd2013-07-21 10:46:25 +080060 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
61 iph->saddr, iph->daddr, 0);
Saurabh11814122012-07-17 09:44:54 +000062 if (tunnel != NULL) {
Steffen Klassertdf3893c2014-02-21 08:41:10 +010063 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
64 goto drop;
Saurabh11814122012-07-17 09:44:54 +000065
Steffen Klassertdf3893c2014-02-21 08:41:10 +010066 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
67 skb->mark = be32_to_cpu(tunnel->parms.i_key);
Christophe Gouault7263a512013-10-08 17:21:22 +020068
Steffen Klassertdf3893c2014-02-21 08:41:10 +010069 return xfrm_input(skb, nexthdr, spi, encap_type);
Saurabh11814122012-07-17 09:44:54 +000070 }
Saurabh11814122012-07-17 09:44:54 +000071
Steffen Klassertdf3893c2014-02-21 08:41:10 +010072 return -EINVAL;
73drop:
74 kfree_skb(skb);
75 return 0;
76}
77
78static int vti_rcv(struct sk_buff *skb)
79{
80 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
81 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
82
83 return vti_input(skb, ip_hdr(skb)->protocol, 0, 0);
84}
85
86static int vti_rcv_cb(struct sk_buff *skb, int err)
87{
88 unsigned short family;
89 struct net_device *dev;
90 struct pcpu_sw_netstats *tstats;
91 struct xfrm_state *x;
92 struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
93
94 if (!tunnel)
95 return 1;
96
97 dev = tunnel->dev;
98
99 if (err) {
100 dev->stats.rx_errors++;
101 dev->stats.rx_dropped++;
102
103 return 0;
104 }
105
106 x = xfrm_input_state(skb);
107 family = x->inner_mode->afinfo->family;
108
109 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family))
110 return -EPERM;
111
112 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
113 skb->dev = dev;
114
115 tstats = this_cpu_ptr(dev->tstats);
116
117 u64_stats_update_begin(&tstats->syncp);
118 tstats->rx_packets++;
119 tstats->rx_bytes += skb->len;
120 u64_stats_update_end(&tstats->syncp);
121
122 return 0;
Saurabh11814122012-07-17 09:44:54 +0000123}
124
125/* This function assumes it is being called from dev_queue_xmit()
126 * and that skb is filled properly by that function.
127 */
Saurabh11814122012-07-17 09:44:54 +0000128static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
129{
130 struct ip_tunnel *tunnel = netdev_priv(dev);
Saurabh11814122012-07-17 09:44:54 +0000131 struct rtable *rt; /* Route to the other host */
132 struct net_device *tdev; /* Device to other host */
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100133 struct flowi fl;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800134 int err;
Saurabh11814122012-07-17 09:44:54 +0000135
136 if (skb->protocol != htons(ETH_P_IP))
137 goto tx_error;
138
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100139 memset(&fl, 0, sizeof(fl));
140 skb->mark = be32_to_cpu(tunnel->parms.o_key);
141 xfrm_decode_session(skb, &fl, AF_INET);
Saurabh11814122012-07-17 09:44:54 +0000142
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100143 if (!skb_dst(skb)) {
144 dev->stats.tx_carrier_errors++;
145 goto tx_error_icmp;
146 }
147
148 dst_hold(skb_dst(skb));
149 rt = (struct rtable *)xfrm_lookup(tunnel->net, skb_dst(skb), &fl, NULL, 0);
Saurabh11814122012-07-17 09:44:54 +0000150 if (IS_ERR(rt)) {
151 dev->stats.tx_carrier_errors++;
152 goto tx_error_icmp;
153 }
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100154
Saurabh11814122012-07-17 09:44:54 +0000155 /* if there is no transform then this tunnel is not functional.
156 * Or if the xfrm is not mode tunnel.
157 */
158 if (!rt->dst.xfrm ||
159 rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) {
160 dev->stats.tx_carrier_errors++;
fan.du236c9f82013-11-19 16:53:28 +0800161 ip_rt_put(rt);
Saurabh11814122012-07-17 09:44:54 +0000162 goto tx_error_icmp;
163 }
164 tdev = rt->dst.dev;
165
166 if (tdev == dev) {
167 ip_rt_put(rt);
168 dev->stats.collisions++;
169 goto tx_error;
170 }
171
172 if (tunnel->err_count > 0) {
173 if (time_before(jiffies,
174 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
175 tunnel->err_count--;
176 dst_link_failure(skb);
177 } else
178 tunnel->err_count = 0;
179 }
180
Saurabh Mohanbaafc772013-06-10 17:45:10 -0700181 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100182 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
Saurabh11814122012-07-17 09:44:54 +0000183 skb_dst_set(skb, &rt->dst);
Saurabh11814122012-07-17 09:44:54 +0000184 skb->dev = skb_dst(skb)->dev;
185
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800186 err = dst_output(skb);
187 if (net_xmit_eval(err) == 0)
188 err = skb->len;
189 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
Saurabh11814122012-07-17 09:44:54 +0000190 return NETDEV_TX_OK;
191
192tx_error_icmp:
193 dst_link_failure(skb);
194tx_error:
195 dev->stats.tx_errors++;
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800196 kfree_skb(skb);
Saurabh11814122012-07-17 09:44:54 +0000197 return NETDEV_TX_OK;
198}
199
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100200static int vti4_err(struct sk_buff *skb, u32 info)
201{
202 __be32 spi;
203 struct xfrm_state *x;
204 struct ip_tunnel *tunnel;
205 struct ip_esp_hdr *esph;
206 struct ip_auth_hdr *ah ;
207 struct ip_comp_hdr *ipch;
208 struct net *net = dev_net(skb->dev);
209 const struct iphdr *iph = (const struct iphdr *)skb->data;
210 int protocol = iph->protocol;
211 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
212
213 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
214 iph->daddr, iph->saddr, 0);
215 if (!tunnel)
216 return -1;
217
218 switch (protocol) {
219 case IPPROTO_ESP:
220 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
221 spi = esph->spi;
222 break;
223 case IPPROTO_AH:
224 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
225 spi = ah->spi;
226 break;
227 case IPPROTO_COMP:
228 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
229 spi = htonl(ntohs(ipch->cpi));
230 break;
231 default:
232 return 0;
233 }
234
235 switch (icmp_hdr(skb)->type) {
236 case ICMP_DEST_UNREACH:
237 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
238 return 0;
239 case ICMP_REDIRECT:
240 break;
241 default:
242 return 0;
243 }
244
245 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
246 spi, protocol, AF_INET);
247 if (!x)
248 return 0;
249
250 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
251 ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0);
252 else
253 ipv4_redirect(skb, net, 0, 0, protocol, 0);
254 xfrm_state_put(x);
255
256 return 0;
257}
258
Saurabh11814122012-07-17 09:44:54 +0000259static int
260vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
261{
262 int err = 0;
263 struct ip_tunnel_parm p;
Saurabh11814122012-07-17 09:44:54 +0000264
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800265 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
266 return -EFAULT;
Saurabh11814122012-07-17 09:44:54 +0000267
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800268 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
Saurabh11814122012-07-17 09:44:54 +0000269 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
270 p.iph.ihl != 5)
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800271 return -EINVAL;
Saurabh11814122012-07-17 09:44:54 +0000272 }
273
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100274 p.i_flags |= VTI_ISVTI;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800275 err = ip_tunnel_ioctl(dev, &p, cmd);
276 if (err)
277 return err;
Saurabh11814122012-07-17 09:44:54 +0000278
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800279 if (cmd != SIOCDELTUNNEL) {
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100280 p.i_flags |= GRE_KEY;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800281 p.o_flags |= GRE_KEY;
282 }
283
284 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
285 return -EFAULT;
Saurabh11814122012-07-17 09:44:54 +0000286 return 0;
287}
288
289static const struct net_device_ops vti_netdev_ops = {
290 .ndo_init = vti_tunnel_init,
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800291 .ndo_uninit = ip_tunnel_uninit,
Saurabh11814122012-07-17 09:44:54 +0000292 .ndo_start_xmit = vti_tunnel_xmit,
293 .ndo_do_ioctl = vti_tunnel_ioctl,
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800294 .ndo_change_mtu = ip_tunnel_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +0000295 .ndo_get_stats64 = ip_tunnel_get_stats64,
Saurabh11814122012-07-17 09:44:54 +0000296};
297
Saurabh11814122012-07-17 09:44:54 +0000298static void vti_tunnel_setup(struct net_device *dev)
299{
300 dev->netdev_ops = &vti_netdev_ops;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800301 ip_tunnel_setup(dev, vti_net_id);
302}
303
304static int vti_tunnel_init(struct net_device *dev)
305{
306 struct ip_tunnel *tunnel = netdev_priv(dev);
307 struct iphdr *iph = &tunnel->parms.iph;
308
309 memcpy(dev->dev_addr, &iph->saddr, 4);
310 memcpy(dev->broadcast, &iph->daddr, 4);
Saurabh11814122012-07-17 09:44:54 +0000311
312 dev->type = ARPHRD_TUNNEL;
313 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
314 dev->mtu = ETH_DATA_LEN;
315 dev->flags = IFF_NOARP;
316 dev->iflink = 0;
317 dev->addr_len = 4;
318 dev->features |= NETIF_F_NETNS_LOCAL;
319 dev->features |= NETIF_F_LLTX;
320 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800321
322 return ip_tunnel_init(dev);
Saurabh11814122012-07-17 09:44:54 +0000323}
324
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800325static void __net_init vti_fb_tunnel_init(struct net_device *dev)
Saurabh11814122012-07-17 09:44:54 +0000326{
327 struct ip_tunnel *tunnel = netdev_priv(dev);
328 struct iphdr *iph = &tunnel->parms.iph;
Saurabh11814122012-07-17 09:44:54 +0000329
Saurabh11814122012-07-17 09:44:54 +0000330 iph->version = 4;
331 iph->protocol = IPPROTO_IPIP;
332 iph->ihl = 5;
Saurabh11814122012-07-17 09:44:54 +0000333}
334
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100335static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
Saurabh11814122012-07-17 09:44:54 +0000336 .handler = vti_rcv,
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100337 .input_handler = vti_input,
338 .cb_handler = vti_rcv_cb,
339 .err_handler = vti4_err,
340 .priority = 100,
341};
342
343static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
344 .handler = vti_rcv,
345 .input_handler = vti_input,
346 .cb_handler = vti_rcv_cb,
347 .err_handler = vti4_err,
348 .priority = 100,
349};
350
351static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
352 .handler = vti_rcv,
353 .input_handler = vti_input,
354 .cb_handler = vti_rcv_cb,
355 .err_handler = vti4_err,
356 .priority = 100,
Saurabh11814122012-07-17 09:44:54 +0000357};
358
Saurabh11814122012-07-17 09:44:54 +0000359static int __net_init vti_init_net(struct net *net)
360{
361 int err;
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800362 struct ip_tunnel_net *itn;
Saurabh11814122012-07-17 09:44:54 +0000363
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800364 err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
Saurabh11814122012-07-17 09:44:54 +0000365 if (err)
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800366 return err;
367 itn = net_generic(net, vti_net_id);
368 vti_fb_tunnel_init(itn->fb_tunnel_dev);
Saurabh11814122012-07-17 09:44:54 +0000369 return 0;
Saurabh11814122012-07-17 09:44:54 +0000370}
371
372static void __net_exit vti_exit_net(struct net *net)
373{
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800374 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
Nicolas Dichtel6c742e72013-08-13 17:51:11 +0200375 ip_tunnel_delete_net(itn, &vti_link_ops);
Saurabh11814122012-07-17 09:44:54 +0000376}
377
378static struct pernet_operations vti_net_ops = {
379 .init = vti_init_net,
380 .exit = vti_exit_net,
381 .id = &vti_net_id,
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800382 .size = sizeof(struct ip_tunnel_net),
Saurabh11814122012-07-17 09:44:54 +0000383};
384
385static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
386{
387 return 0;
388}
389
390static void vti_netlink_parms(struct nlattr *data[],
391 struct ip_tunnel_parm *parms)
392{
393 memset(parms, 0, sizeof(*parms));
394
395 parms->iph.protocol = IPPROTO_IPIP;
396
397 if (!data)
398 return;
399
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100400 parms->i_flags = VTI_ISVTI;
401
Saurabh11814122012-07-17 09:44:54 +0000402 if (data[IFLA_VTI_LINK])
403 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
404
405 if (data[IFLA_VTI_IKEY])
406 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
407
408 if (data[IFLA_VTI_OKEY])
409 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
410
411 if (data[IFLA_VTI_LOCAL])
412 parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]);
413
414 if (data[IFLA_VTI_REMOTE])
415 parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]);
416
417}
418
419static int vti_newlink(struct net *src_net, struct net_device *dev,
420 struct nlattr *tb[], struct nlattr *data[])
421{
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800422 struct ip_tunnel_parm parms;
Saurabh11814122012-07-17 09:44:54 +0000423
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800424 vti_netlink_parms(data, &parms);
425 return ip_tunnel_newlink(dev, tb, &parms);
Saurabh11814122012-07-17 09:44:54 +0000426}
427
428static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
429 struct nlattr *data[])
430{
Saurabh11814122012-07-17 09:44:54 +0000431 struct ip_tunnel_parm p;
Saurabh11814122012-07-17 09:44:54 +0000432
Saurabh11814122012-07-17 09:44:54 +0000433 vti_netlink_parms(data, &p);
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800434 return ip_tunnel_changelink(dev, tb, &p);
Saurabh11814122012-07-17 09:44:54 +0000435}
436
437static size_t vti_get_size(const struct net_device *dev)
438{
439 return
440 /* IFLA_VTI_LINK */
441 nla_total_size(4) +
442 /* IFLA_VTI_IKEY */
443 nla_total_size(4) +
444 /* IFLA_VTI_OKEY */
445 nla_total_size(4) +
446 /* IFLA_VTI_LOCAL */
447 nla_total_size(4) +
448 /* IFLA_VTI_REMOTE */
449 nla_total_size(4) +
450 0;
451}
452
453static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
454{
455 struct ip_tunnel *t = netdev_priv(dev);
456 struct ip_tunnel_parm *p = &t->parms;
457
458 nla_put_u32(skb, IFLA_VTI_LINK, p->link);
459 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
460 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
461 nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr);
462 nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr);
463
464 return 0;
465}
466
467static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
468 [IFLA_VTI_LINK] = { .type = NLA_U32 },
469 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
470 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
471 [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
472 [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
473};
474
475static struct rtnl_link_ops vti_link_ops __read_mostly = {
476 .kind = "vti",
477 .maxtype = IFLA_VTI_MAX,
478 .policy = vti_policy,
479 .priv_size = sizeof(struct ip_tunnel),
480 .setup = vti_tunnel_setup,
481 .validate = vti_tunnel_validate,
482 .newlink = vti_newlink,
483 .changelink = vti_changelink,
484 .get_size = vti_get_size,
485 .fill_info = vti_fill_info,
486};
487
488static int __init vti_init(void)
489{
490 int err;
491
492 pr_info("IPv4 over IPSec tunneling driver\n");
493
494 err = register_pernet_device(&vti_net_ops);
495 if (err < 0)
496 return err;
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100497 err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
Saurabh11814122012-07-17 09:44:54 +0000498 if (err < 0) {
499 unregister_pernet_device(&vti_net_ops);
Amerigo Wangb9959fd2013-07-21 10:46:25 +0800500 pr_info("vti init: can't register tunnel\n");
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100501
502 return err;
503 }
504
505 err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
506 if (err < 0) {
507 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
508 unregister_pernet_device(&vti_net_ops);
509 pr_info("vti init: can't register tunnel\n");
510
511 return err;
512 }
513
514 err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
515 if (err < 0) {
516 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
517 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
518 unregister_pernet_device(&vti_net_ops);
519 pr_info("vti init: can't register tunnel\n");
520
521 return err;
Saurabh11814122012-07-17 09:44:54 +0000522 }
523
524 err = rtnl_link_register(&vti_link_ops);
525 if (err < 0)
526 goto rtnl_link_failed;
527
528 return err;
529
530rtnl_link_failed:
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100531 xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
532 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
533 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
Saurabh11814122012-07-17 09:44:54 +0000534 unregister_pernet_device(&vti_net_ops);
535 return err;
536}
537
538static void __exit vti_fini(void)
539{
540 rtnl_link_unregister(&vti_link_ops);
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100541 if (xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP))
Saurabh11814122012-07-17 09:44:54 +0000542 pr_info("vti close: can't deregister tunnel\n");
Steffen Klassertdf3893c2014-02-21 08:41:10 +0100543 if (xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH))
544 pr_info("vti close: can't deregister tunnel\n");
545 if (xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP))
546 pr_info("vti close: can't deregister tunnel\n");
547
Saurabh11814122012-07-17 09:44:54 +0000548
549 unregister_pernet_device(&vti_net_ops);
550}
551
552module_init(vti_init);
553module_exit(vti_fini);
554MODULE_LICENSE("GPL");
555MODULE_ALIAS_RTNL_LINK("vti");
556MODULE_ALIAS_NETDEV("ip_vti0");