xref: /openbmc/linux/net/ipv4/ip_vti.c (revision 62975d27)
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->header_ops		= &ip_tunnel_header_ops;
445 	dev->type		= ARPHRD_TUNNEL;
446 	ip_tunnel_setup(dev, vti_net_id);
447 }
448 
449 static int vti_tunnel_init(struct net_device *dev)
450 {
451 	struct ip_tunnel *tunnel = netdev_priv(dev);
452 	struct iphdr *iph = &tunnel->parms.iph;
453 
454 	memcpy(dev->dev_addr, &iph->saddr, 4);
455 	memcpy(dev->broadcast, &iph->daddr, 4);
456 
457 	dev->flags		= IFF_NOARP;
458 	dev->addr_len		= 4;
459 	dev->features		|= NETIF_F_LLTX;
460 	netif_keep_dst(dev);
461 
462 	return ip_tunnel_init(dev);
463 }
464 
465 static void __net_init vti_fb_tunnel_init(struct net_device *dev)
466 {
467 	struct ip_tunnel *tunnel = netdev_priv(dev);
468 	struct iphdr *iph = &tunnel->parms.iph;
469 
470 	iph->version		= 4;
471 	iph->protocol		= IPPROTO_IPIP;
472 	iph->ihl		= 5;
473 }
474 
475 static struct xfrm4_protocol vti_esp4_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 xfrm4_protocol vti_ah4_protocol __read_mostly = {
484 	.handler	=	vti_rcv_proto,
485 	.input_handler	=	vti_input_proto,
486 	.cb_handler	=	vti_rcv_cb,
487 	.err_handler	=	vti4_err,
488 	.priority	=	100,
489 };
490 
491 static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
492 	.handler	=	vti_rcv_proto,
493 	.input_handler	=	vti_input_proto,
494 	.cb_handler	=	vti_rcv_cb,
495 	.err_handler	=	vti4_err,
496 	.priority	=	100,
497 };
498 
499 static struct xfrm_tunnel ipip_handler __read_mostly = {
500 	.handler	=	vti_rcv_tunnel,
501 	.err_handler	=	vti4_err,
502 	.priority	=	0,
503 };
504 
505 static int __net_init vti_init_net(struct net *net)
506 {
507 	int err;
508 	struct ip_tunnel_net *itn;
509 
510 	err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
511 	if (err)
512 		return err;
513 	itn = net_generic(net, vti_net_id);
514 	if (itn->fb_tunnel_dev)
515 		vti_fb_tunnel_init(itn->fb_tunnel_dev);
516 	return 0;
517 }
518 
519 static void __net_exit vti_exit_batch_net(struct list_head *list_net)
520 {
521 	ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops);
522 }
523 
524 static struct pernet_operations vti_net_ops = {
525 	.init = vti_init_net,
526 	.exit_batch = vti_exit_batch_net,
527 	.id   = &vti_net_id,
528 	.size = sizeof(struct ip_tunnel_net),
529 };
530 
531 static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
532 			       struct netlink_ext_ack *extack)
533 {
534 	return 0;
535 }
536 
537 static void vti_netlink_parms(struct nlattr *data[],
538 			      struct ip_tunnel_parm *parms,
539 			      __u32 *fwmark)
540 {
541 	memset(parms, 0, sizeof(*parms));
542 
543 	parms->iph.protocol = IPPROTO_IPIP;
544 
545 	if (!data)
546 		return;
547 
548 	parms->i_flags = VTI_ISVTI;
549 
550 	if (data[IFLA_VTI_LINK])
551 		parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
552 
553 	if (data[IFLA_VTI_IKEY])
554 		parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
555 
556 	if (data[IFLA_VTI_OKEY])
557 		parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
558 
559 	if (data[IFLA_VTI_LOCAL])
560 		parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
561 
562 	if (data[IFLA_VTI_REMOTE])
563 		parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
564 
565 	if (data[IFLA_VTI_FWMARK])
566 		*fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
567 }
568 
569 static int vti_newlink(struct net *src_net, struct net_device *dev,
570 		       struct nlattr *tb[], struct nlattr *data[],
571 		       struct netlink_ext_ack *extack)
572 {
573 	struct ip_tunnel_parm parms;
574 	__u32 fwmark = 0;
575 
576 	vti_netlink_parms(data, &parms, &fwmark);
577 	return ip_tunnel_newlink(dev, tb, &parms, fwmark);
578 }
579 
580 static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
581 			  struct nlattr *data[],
582 			  struct netlink_ext_ack *extack)
583 {
584 	struct ip_tunnel *t = netdev_priv(dev);
585 	__u32 fwmark = t->fwmark;
586 	struct ip_tunnel_parm p;
587 
588 	vti_netlink_parms(data, &p, &fwmark);
589 	return ip_tunnel_changelink(dev, tb, &p, fwmark);
590 }
591 
592 static size_t vti_get_size(const struct net_device *dev)
593 {
594 	return
595 		/* IFLA_VTI_LINK */
596 		nla_total_size(4) +
597 		/* IFLA_VTI_IKEY */
598 		nla_total_size(4) +
599 		/* IFLA_VTI_OKEY */
600 		nla_total_size(4) +
601 		/* IFLA_VTI_LOCAL */
602 		nla_total_size(4) +
603 		/* IFLA_VTI_REMOTE */
604 		nla_total_size(4) +
605 		/* IFLA_VTI_FWMARK */
606 		nla_total_size(4) +
607 		0;
608 }
609 
610 static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
611 {
612 	struct ip_tunnel *t = netdev_priv(dev);
613 	struct ip_tunnel_parm *p = &t->parms;
614 
615 	if (nla_put_u32(skb, IFLA_VTI_LINK, p->link) ||
616 	    nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key) ||
617 	    nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key) ||
618 	    nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr) ||
619 	    nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr) ||
620 	    nla_put_u32(skb, IFLA_VTI_FWMARK, t->fwmark))
621 		return -EMSGSIZE;
622 
623 	return 0;
624 }
625 
626 static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
627 	[IFLA_VTI_LINK]		= { .type = NLA_U32 },
628 	[IFLA_VTI_IKEY]		= { .type = NLA_U32 },
629 	[IFLA_VTI_OKEY]		= { .type = NLA_U32 },
630 	[IFLA_VTI_LOCAL]	= { .len = sizeof_field(struct iphdr, saddr) },
631 	[IFLA_VTI_REMOTE]	= { .len = sizeof_field(struct iphdr, daddr) },
632 	[IFLA_VTI_FWMARK]	= { .type = NLA_U32 },
633 };
634 
635 static struct rtnl_link_ops vti_link_ops __read_mostly = {
636 	.kind		= "vti",
637 	.maxtype	= IFLA_VTI_MAX,
638 	.policy		= vti_policy,
639 	.priv_size	= sizeof(struct ip_tunnel),
640 	.setup		= vti_tunnel_setup,
641 	.validate	= vti_tunnel_validate,
642 	.newlink	= vti_newlink,
643 	.changelink	= vti_changelink,
644 	.dellink        = ip_tunnel_dellink,
645 	.get_size	= vti_get_size,
646 	.fill_info	= vti_fill_info,
647 	.get_link_net	= ip_tunnel_get_link_net,
648 };
649 
650 static int __init vti_init(void)
651 {
652 	const char *msg;
653 	int err;
654 
655 	pr_info("IPv4 over IPsec tunneling driver\n");
656 
657 	msg = "tunnel device";
658 	err = register_pernet_device(&vti_net_ops);
659 	if (err < 0)
660 		goto pernet_dev_failed;
661 
662 	msg = "tunnel protocols";
663 	err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
664 	if (err < 0)
665 		goto xfrm_proto_esp_failed;
666 	err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
667 	if (err < 0)
668 		goto xfrm_proto_ah_failed;
669 	err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
670 	if (err < 0)
671 		goto xfrm_proto_comp_failed;
672 
673 	msg = "ipip tunnel";
674 	err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
675 	if (err < 0)
676 		goto xfrm_tunnel_failed;
677 
678 	msg = "netlink interface";
679 	err = rtnl_link_register(&vti_link_ops);
680 	if (err < 0)
681 		goto rtnl_link_failed;
682 
683 	return err;
684 
685 rtnl_link_failed:
686 	xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
687 xfrm_tunnel_failed:
688 	xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
689 xfrm_proto_comp_failed:
690 	xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
691 xfrm_proto_ah_failed:
692 	xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
693 xfrm_proto_esp_failed:
694 	unregister_pernet_device(&vti_net_ops);
695 pernet_dev_failed:
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 }
709 
710 module_init(vti_init);
711 module_exit(vti_fini);
712 MODULE_LICENSE("GPL");
713 MODULE_ALIAS_RTNL_LINK("vti");
714 MODULE_ALIAS_NETDEV("ip_vti0");
715