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