xref: /linux/net/ipv4/ip_vti.c (revision 9410645520e9b820069761f3450ef6661418e279)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21181412cSSaurabh /*
31181412cSSaurabh  *	Linux NET3: IP/IP protocol decoder modified to support
41181412cSSaurabh  *		    virtual tunnel interface
51181412cSSaurabh  *
61181412cSSaurabh  *	Authors:
71181412cSSaurabh  *		Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012
81181412cSSaurabh  */
91181412cSSaurabh 
101181412cSSaurabh /*
111181412cSSaurabh    This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
121181412cSSaurabh 
131181412cSSaurabh    For comments look at net/ipv4/ip_gre.c --ANK
141181412cSSaurabh  */
151181412cSSaurabh 
161181412cSSaurabh 
171181412cSSaurabh #include <linux/capability.h>
181181412cSSaurabh #include <linux/module.h>
191181412cSSaurabh #include <linux/types.h>
201181412cSSaurabh #include <linux/kernel.h>
211181412cSSaurabh #include <linux/uaccess.h>
221181412cSSaurabh #include <linux/skbuff.h>
231181412cSSaurabh #include <linux/netdevice.h>
241181412cSSaurabh #include <linux/in.h>
251181412cSSaurabh #include <linux/tcp.h>
261181412cSSaurabh #include <linux/udp.h>
271181412cSSaurabh #include <linux/if_arp.h>
281181412cSSaurabh #include <linux/init.h>
291181412cSSaurabh #include <linux/netfilter_ipv4.h>
301181412cSSaurabh #include <linux/if_ether.h>
3178a010ccSSteffen Klassert #include <linux/icmpv6.h>
321181412cSSaurabh 
331181412cSSaurabh #include <net/sock.h>
341181412cSSaurabh #include <net/ip.h>
351181412cSSaurabh #include <net/icmp.h>
36c5441932SPravin B Shelar #include <net/ip_tunnels.h>
371181412cSSaurabh #include <net/inet_ecn.h>
381181412cSSaurabh #include <net/xfrm.h>
391181412cSSaurabh #include <net/net_namespace.h>
401181412cSSaurabh #include <net/netns/generic.h>
411181412cSSaurabh 
421181412cSSaurabh static struct rtnl_link_ops vti_link_ops __read_mostly;
431181412cSSaurabh 
44c7d03a00SAlexey Dobriyan static unsigned int vti_net_id __read_mostly;
451181412cSSaurabh static int vti_tunnel_init(struct net_device *dev);
461181412cSSaurabh 
vti_input(struct sk_buff * skb,int nexthdr,__be32 spi,int encap_type,bool update_skb_dev)47df3893c1SSteffen Klassert static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
48f981c57fSJeremy Sowden 		     int encap_type, bool update_skb_dev)
491181412cSSaurabh {
501181412cSSaurabh 	struct ip_tunnel *tunnel;
511181412cSSaurabh 	const struct iphdr *iph = ip_hdr(skb);
52b9959fd3SAmerigo Wang 	struct net *net = dev_net(skb->dev);
53b9959fd3SAmerigo Wang 	struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
545832c4a7SAlexander Lobakin 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
551181412cSSaurabh 
565832c4a7SAlexander Lobakin 	__set_bit(IP_TUNNEL_NO_KEY_BIT, flags);
575832c4a7SAlexander Lobakin 
585832c4a7SAlexander Lobakin 	tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
59b9959fd3SAmerigo Wang 				  iph->saddr, iph->daddr, 0);
6000db4124SIan Morris 	if (tunnel) {
61df3893c1SSteffen Klassert 		if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
62df3893c1SSteffen Klassert 			goto drop;
63df3893c1SSteffen Klassert 
64df3893c1SSteffen Klassert 		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
65df3893c1SSteffen Klassert 
66f981c57fSJeremy Sowden 		if (update_skb_dev)
67dd9ee344SSu Yanjun 			skb->dev = tunnel->dev;
68dd9ee344SSu Yanjun 
69dd9ee344SSu Yanjun 		return xfrm_input(skb, nexthdr, spi, encap_type);
70dd9ee344SSu Yanjun 	}
71dd9ee344SSu Yanjun 
72dd9ee344SSu Yanjun 	return -EINVAL;
73dd9ee344SSu Yanjun drop:
74dd9ee344SSu Yanjun 	kfree_skb(skb);
75dd9ee344SSu Yanjun 	return 0;
76dd9ee344SSu Yanjun }
77dd9ee344SSu Yanjun 
vti_input_proto(struct sk_buff * skb,int nexthdr,__be32 spi,int encap_type)78f981c57fSJeremy Sowden static int vti_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
79f981c57fSJeremy Sowden 			   int encap_type)
80df3893c1SSteffen Klassert {
81f981c57fSJeremy Sowden 	return vti_input(skb, nexthdr, spi, encap_type, false);
82df3893c1SSteffen Klassert }
83df3893c1SSteffen Klassert 
vti_rcv(struct sk_buff * skb,__be32 spi,bool update_skb_dev)84f981c57fSJeremy Sowden static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
85dd9ee344SSu Yanjun {
86dd9ee344SSu Yanjun 	XFRM_SPI_SKB_CB(skb)->family = AF_INET;
87dd9ee344SSu Yanjun 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
88dd9ee344SSu Yanjun 
89f981c57fSJeremy Sowden 	return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
90f981c57fSJeremy Sowden }
91f981c57fSJeremy Sowden 
vti_rcv_proto(struct sk_buff * skb)92f981c57fSJeremy Sowden static int vti_rcv_proto(struct sk_buff *skb)
93f981c57fSJeremy Sowden {
94f981c57fSJeremy Sowden 	return vti_rcv(skb, 0, false);
95f981c57fSJeremy Sowden }
96f981c57fSJeremy Sowden 
vti_rcv_cb(struct sk_buff * skb,int err)97df3893c1SSteffen Klassert static int vti_rcv_cb(struct sk_buff *skb, int err)
98df3893c1SSteffen Klassert {
99df3893c1SSteffen Klassert 	unsigned short family;
100df3893c1SSteffen Klassert 	struct net_device *dev;
101df3893c1SSteffen Klassert 	struct xfrm_state *x;
1024c145dceSFlorian Westphal 	const struct xfrm_mode *inner_mode;
103df3893c1SSteffen Klassert 	struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
104d55c670cSAlexander Duyck 	u32 orig_mark = skb->mark;
105d55c670cSAlexander Duyck 	int ret;
1061181412cSSaurabh 
107df3893c1SSteffen Klassert 	if (!tunnel)
108df3893c1SSteffen Klassert 		return 1;
1097263a518SChristophe Gouault 
110df3893c1SSteffen Klassert 	dev = tunnel->dev;
111b2942004SSaurabh Mohan 
112df3893c1SSteffen Klassert 	if (err) {
113c4794d22SEric Dumazet 		DEV_STATS_INC(dev, rx_errors);
114c4794d22SEric Dumazet 		DEV_STATS_INC(dev, rx_dropped);
115df3893c1SSteffen Klassert 
116df3893c1SSteffen Klassert 		return 0;
117df3893c1SSteffen Klassert 	}
118df3893c1SSteffen Klassert 
119df3893c1SSteffen Klassert 	x = xfrm_input_state(skb);
1201fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 
121c9500d7bSFlorian Westphal 	inner_mode = &x->inner_mode;
1221fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 
1231fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 	if (x->sel.family == AF_UNSPEC) {
1241fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 		inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
1251fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 		if (inner_mode == NULL) {
1261fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 			XFRM_INC_STATS(dev_net(skb->dev),
1271fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 				       LINUX_MIB_XFRMINSTATEMODEERROR);
1281fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 			return -EINVAL;
1291fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 		}
1301fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 	}
1311fb81e09Sthomas.zeitlhofer+lkml@ze-it.at 
132b45714b1SFlorian Westphal 	family = inner_mode->family;
133df3893c1SSteffen Klassert 
134d55c670cSAlexander Duyck 	skb->mark = be32_to_cpu(tunnel->parms.i_key);
135d55c670cSAlexander Duyck 	ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
136d55c670cSAlexander Duyck 	skb->mark = orig_mark;
137d55c670cSAlexander Duyck 
138d55c670cSAlexander Duyck 	if (!ret)
139df3893c1SSteffen Klassert 		return -EPERM;
140df3893c1SSteffen Klassert 
141df3893c1SSteffen Klassert 	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
142df3893c1SSteffen Klassert 	skb->dev = dev;
143560b50cfSFabian Frederick 	dev_sw_netstats_rx_add(dev, skb->len);
1441181412cSSaurabh 
145df3893c1SSteffen Klassert 	return 0;
1461181412cSSaurabh }
1471181412cSSaurabh 
vti_state_check(const struct xfrm_state * x,__be32 dst,__be32 src)1486e2de802SSteffen Klassert static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
1496e2de802SSteffen Klassert {
1506e2de802SSteffen Klassert 	xfrm_address_t *daddr = (xfrm_address_t *)&dst;
1516e2de802SSteffen Klassert 	xfrm_address_t *saddr = (xfrm_address_t *)&src;
1526e2de802SSteffen Klassert 
1536e2de802SSteffen Klassert 	/* if there is no transform then this tunnel is not functional.
1546e2de802SSteffen Klassert 	 * Or if the xfrm is not mode tunnel.
1556e2de802SSteffen Klassert 	 */
1566e2de802SSteffen Klassert 	if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
1576e2de802SSteffen Klassert 	    x->props.family != AF_INET)
1586e2de802SSteffen Klassert 		return false;
1596e2de802SSteffen Klassert 
1606e2de802SSteffen Klassert 	if (!dst)
1616e2de802SSteffen Klassert 		return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
1626e2de802SSteffen Klassert 
1636e2de802SSteffen Klassert 	if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
1646e2de802SSteffen Klassert 		return false;
1656e2de802SSteffen Klassert 
1666e2de802SSteffen Klassert 	return true;
1676e2de802SSteffen Klassert }
1686e2de802SSteffen Klassert 
vti_xmit(struct sk_buff * skb,struct net_device * dev,struct flowi * fl)16978a010ccSSteffen Klassert static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
17078a010ccSSteffen Klassert 			    struct flowi *fl)
1711181412cSSaurabh {
1721181412cSSaurabh 	struct ip_tunnel *tunnel = netdev_priv(dev);
173117aef12SAlexander Lobakin 	struct ip_tunnel_parm_kern *parms = &tunnel->parms;
174a34cd4f3SSteffen Klassert 	struct dst_entry *dst = skb_dst(skb);
1751181412cSSaurabh 	struct net_device *tdev;	/* Device to other host */
17636f6ee22SAlexey Kodanev 	int pkt_len = skb->len;
177b9959fd3SAmerigo Wang 	int err;
178d6af1a31SSteffen Klassert 	int mtu;
1791181412cSSaurabh 
180a34cd4f3SSteffen Klassert 	if (!dst) {
181f1ed1026SNicolas Dichtel 		switch (skb->protocol) {
182f1ed1026SNicolas Dichtel 		case htons(ETH_P_IP): {
18395224166SNicolas Dichtel 			struct rtable *rt;
18495224166SNicolas Dichtel 
18595224166SNicolas Dichtel 			fl->u.ip4.flowi4_oif = dev->ifindex;
18695224166SNicolas Dichtel 			fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
18795224166SNicolas Dichtel 			rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
18895224166SNicolas Dichtel 			if (IS_ERR(rt)) {
189c4794d22SEric Dumazet 				DEV_STATS_INC(dev, tx_carrier_errors);
190df3893c1SSteffen Klassert 				goto tx_error_icmp;
191df3893c1SSteffen Klassert 			}
19295224166SNicolas Dichtel 			dst = &rt->dst;
19395224166SNicolas Dichtel 			skb_dst_set(skb, dst);
194f1ed1026SNicolas Dichtel 			break;
195f1ed1026SNicolas Dichtel 		}
196f1ed1026SNicolas Dichtel #if IS_ENABLED(CONFIG_IPV6)
197f1ed1026SNicolas Dichtel 		case htons(ETH_P_IPV6):
198f1ed1026SNicolas Dichtel 			fl->u.ip6.flowi6_oif = dev->ifindex;
199f1ed1026SNicolas Dichtel 			fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
200f1ed1026SNicolas Dichtel 			dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
201f1ed1026SNicolas Dichtel 			if (dst->error) {
202f1ed1026SNicolas Dichtel 				dst_release(dst);
203f1ed1026SNicolas Dichtel 				dst = NULL;
204c4794d22SEric Dumazet 				DEV_STATS_INC(dev, tx_carrier_errors);
205f1ed1026SNicolas Dichtel 				goto tx_error_icmp;
206f1ed1026SNicolas Dichtel 			}
207f1ed1026SNicolas Dichtel 			skb_dst_set(skb, dst);
208f1ed1026SNicolas Dichtel 			break;
209f1ed1026SNicolas Dichtel #endif
210f1ed1026SNicolas Dichtel 		default:
211c4794d22SEric Dumazet 			DEV_STATS_INC(dev, tx_carrier_errors);
212f1ed1026SNicolas Dichtel 			goto tx_error_icmp;
213f1ed1026SNicolas Dichtel 		}
21495224166SNicolas Dichtel 	}
215df3893c1SSteffen Klassert 
216a34cd4f3SSteffen Klassert 	dst_hold(dst);
217b328ecc4SSteffen Klassert 	dst = xfrm_lookup_route(tunnel->net, dst, fl, NULL, 0);
218a34cd4f3SSteffen Klassert 	if (IS_ERR(dst)) {
219c4794d22SEric Dumazet 		DEV_STATS_INC(dev, tx_carrier_errors);
2201181412cSSaurabh 		goto tx_error_icmp;
2211181412cSSaurabh 	}
222df3893c1SSteffen Klassert 
223b328ecc4SSteffen Klassert 	if (dst->flags & DST_XFRM_QUEUE)
224c7c1abfdSEyal Birger 		goto xmit;
225b328ecc4SSteffen Klassert 
2266e2de802SSteffen Klassert 	if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
227c4794d22SEric Dumazet 		DEV_STATS_INC(dev, tx_carrier_errors);
228a34cd4f3SSteffen Klassert 		dst_release(dst);
2291181412cSSaurabh 		goto tx_error_icmp;
2301181412cSSaurabh 	}
2316e2de802SSteffen Klassert 
232a34cd4f3SSteffen Klassert 	tdev = dst->dev;
2331181412cSSaurabh 
2341181412cSSaurabh 	if (tdev == dev) {
235a34cd4f3SSteffen Klassert 		dst_release(dst);
236c4794d22SEric Dumazet 		DEV_STATS_INC(dev, collisions);
2371181412cSSaurabh 		goto tx_error;
2381181412cSSaurabh 	}
2391181412cSSaurabh 
240d6af1a31SSteffen Klassert 	mtu = dst_mtu(dst);
241d6af1a31SSteffen Klassert 	if (skb->len > mtu) {
2428247a79eSHangbin Liu 		skb_dst_update_pmtu_no_confirm(skb, mtu);
243d6af1a31SSteffen Klassert 		if (skb->protocol == htons(ETH_P_IP)) {
244c7c1abfdSEyal Birger 			if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
245c7c1abfdSEyal Birger 				goto xmit;
2464372339eSJason A. Donenfeld 			icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
247d6af1a31SSteffen Klassert 				      htonl(mtu));
248d6af1a31SSteffen Klassert 		} else {
249d6af1a31SSteffen Klassert 			if (mtu < IPV6_MIN_MTU)
250d6af1a31SSteffen Klassert 				mtu = IPV6_MIN_MTU;
251d6af1a31SSteffen Klassert 
2524372339eSJason A. Donenfeld 			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
253d6af1a31SSteffen Klassert 		}
254d6af1a31SSteffen Klassert 
255d6af1a31SSteffen Klassert 		dst_release(dst);
256d6af1a31SSteffen Klassert 		goto tx_error;
257d6af1a31SSteffen Klassert 	}
258d6af1a31SSteffen Klassert 
259c7c1abfdSEyal Birger xmit:
260df3893c1SSteffen Klassert 	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
261a34cd4f3SSteffen Klassert 	skb_dst_set(skb, dst);
2621181412cSSaurabh 	skb->dev = skb_dst(skb)->dev;
2631181412cSSaurabh 
26413206b6bSEric W. Biederman 	err = dst_output(tunnel->net, skb->sk, skb);
265b9959fd3SAmerigo Wang 	if (net_xmit_eval(err) == 0)
26636f6ee22SAlexey Kodanev 		err = pkt_len;
267039f5062SPravin B Shelar 	iptunnel_xmit_stats(dev, err);
2681181412cSSaurabh 	return NETDEV_TX_OK;
2691181412cSSaurabh 
2701181412cSSaurabh tx_error_icmp:
2711181412cSSaurabh 	dst_link_failure(skb);
2721181412cSSaurabh tx_error:
273c4794d22SEric Dumazet 	DEV_STATS_INC(dev, tx_errors);
2743acfa1e7SEric Dumazet 	kfree_skb(skb);
2751181412cSSaurabh 	return NETDEV_TX_OK;
2761181412cSSaurabh }
2771181412cSSaurabh 
27878a010ccSSteffen Klassert /* This function assumes it is being called from dev_queue_xmit()
27978a010ccSSteffen Klassert  * and that skb is filled properly by that function.
28078a010ccSSteffen Klassert  */
vti_tunnel_xmit(struct sk_buff * skb,struct net_device * dev)28178a010ccSSteffen Klassert static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
28278a010ccSSteffen Klassert {
28378a010ccSSteffen Klassert 	struct ip_tunnel *tunnel = netdev_priv(dev);
28478a010ccSSteffen Klassert 	struct flowi fl;
28578a010ccSSteffen Klassert 
286cb9f1b78SWillem de Bruijn 	if (!pskb_inet_may_pull(skb))
287cb9f1b78SWillem de Bruijn 		goto tx_err;
288cb9f1b78SWillem de Bruijn 
28978a010ccSSteffen Klassert 	memset(&fl, 0, sizeof(fl));
29078a010ccSSteffen Klassert 
29178a010ccSSteffen Klassert 	switch (skb->protocol) {
29278a010ccSSteffen Klassert 	case htons(ETH_P_IP):
29378a010ccSSteffen Klassert 		memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
2942b1dc628SFlorian Westphal 		xfrm_decode_session(dev_net(dev), skb, &fl, AF_INET);
29578a010ccSSteffen Klassert 		break;
29678a010ccSSteffen Klassert 	case htons(ETH_P_IPV6):
29778a010ccSSteffen Klassert 		memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
2982b1dc628SFlorian Westphal 		xfrm_decode_session(dev_net(dev), skb, &fl, AF_INET6);
29978a010ccSSteffen Klassert 		break;
30078a010ccSSteffen Klassert 	default:
301cb9f1b78SWillem de Bruijn 		goto tx_err;
30278a010ccSSteffen Klassert 	}
30378a010ccSSteffen Klassert 
304cd5279c1SAlexander Duyck 	/* override mark with tunnel output key */
305cd5279c1SAlexander Duyck 	fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
306cd5279c1SAlexander Duyck 
30778a010ccSSteffen Klassert 	return vti_xmit(skb, dev, &fl);
308cb9f1b78SWillem de Bruijn 
309cb9f1b78SWillem de Bruijn tx_err:
310c4794d22SEric Dumazet 	DEV_STATS_INC(dev, tx_errors);
311cb9f1b78SWillem de Bruijn 	kfree_skb(skb);
312cb9f1b78SWillem de Bruijn 	return NETDEV_TX_OK;
31378a010ccSSteffen Klassert }
31478a010ccSSteffen Klassert 
vti4_err(struct sk_buff * skb,u32 info)315df3893c1SSteffen Klassert static int vti4_err(struct sk_buff *skb, u32 info)
316df3893c1SSteffen Klassert {
317df3893c1SSteffen Klassert 	__be32 spi;
3186d004d6cSSteffen Klassert 	__u32 mark;
319df3893c1SSteffen Klassert 	struct xfrm_state *x;
320df3893c1SSteffen Klassert 	struct ip_tunnel *tunnel;
321df3893c1SSteffen Klassert 	struct ip_esp_hdr *esph;
322df3893c1SSteffen Klassert 	struct ip_auth_hdr *ah ;
323df3893c1SSteffen Klassert 	struct ip_comp_hdr *ipch;
324df3893c1SSteffen Klassert 	struct net *net = dev_net(skb->dev);
325df3893c1SSteffen Klassert 	const struct iphdr *iph = (const struct iphdr *)skb->data;
326df3893c1SSteffen Klassert 	int protocol = iph->protocol;
327df3893c1SSteffen Klassert 	struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
3285832c4a7SAlexander Lobakin 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
329df3893c1SSteffen Klassert 
3305832c4a7SAlexander Lobakin 	__set_bit(IP_TUNNEL_NO_KEY_BIT, flags);
3315832c4a7SAlexander Lobakin 
3325832c4a7SAlexander Lobakin 	tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
333df3893c1SSteffen Klassert 				  iph->daddr, iph->saddr, 0);
334df3893c1SSteffen Klassert 	if (!tunnel)
335df3893c1SSteffen Klassert 		return -1;
336df3893c1SSteffen Klassert 
3376d004d6cSSteffen Klassert 	mark = be32_to_cpu(tunnel->parms.o_key);
3386d004d6cSSteffen Klassert 
339df3893c1SSteffen Klassert 	switch (protocol) {
340df3893c1SSteffen Klassert 	case IPPROTO_ESP:
341df3893c1SSteffen Klassert 		esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
342df3893c1SSteffen Klassert 		spi = esph->spi;
343df3893c1SSteffen Klassert 		break;
344df3893c1SSteffen Klassert 	case IPPROTO_AH:
345df3893c1SSteffen Klassert 		ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
346df3893c1SSteffen Klassert 		spi = ah->spi;
347df3893c1SSteffen Klassert 		break;
348df3893c1SSteffen Klassert 	case IPPROTO_COMP:
349df3893c1SSteffen Klassert 		ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
350df3893c1SSteffen Klassert 		spi = htonl(ntohs(ipch->cpi));
351df3893c1SSteffen Klassert 		break;
352df3893c1SSteffen Klassert 	default:
353df3893c1SSteffen Klassert 		return 0;
354df3893c1SSteffen Klassert 	}
355df3893c1SSteffen Klassert 
356df3893c1SSteffen Klassert 	switch (icmp_hdr(skb)->type) {
357df3893c1SSteffen Klassert 	case ICMP_DEST_UNREACH:
358df3893c1SSteffen Klassert 		if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
359df3893c1SSteffen Klassert 			return 0;
36079121184SGustavo A. R. Silva 		break;
361df3893c1SSteffen Klassert 	case ICMP_REDIRECT:
362df3893c1SSteffen Klassert 		break;
363df3893c1SSteffen Klassert 	default:
364df3893c1SSteffen Klassert 		return 0;
365df3893c1SSteffen Klassert 	}
366df3893c1SSteffen Klassert 
3676d004d6cSSteffen Klassert 	x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
368df3893c1SSteffen Klassert 			      spi, protocol, AF_INET);
369df3893c1SSteffen Klassert 	if (!x)
370df3893c1SSteffen Klassert 		return 0;
371df3893c1SSteffen Klassert 
372df3893c1SSteffen Klassert 	if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
373d888f396SMaciej Żenczykowski 		ipv4_update_pmtu(skb, net, info, 0, protocol);
374df3893c1SSteffen Klassert 	else
3751042caa7SMaciej Żenczykowski 		ipv4_redirect(skb, net, 0, protocol);
376df3893c1SSteffen Klassert 	xfrm_state_put(x);
377df3893c1SSteffen Klassert 
378df3893c1SSteffen Klassert 	return 0;
379df3893c1SSteffen Klassert }
380df3893c1SSteffen Klassert 
3811181412cSSaurabh static int
vti_tunnel_ctl(struct net_device * dev,struct ip_tunnel_parm_kern * p,int cmd)382117aef12SAlexander Lobakin vti_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p, int cmd)
3831181412cSSaurabh {
3845832c4a7SAlexander Lobakin 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
3851181412cSSaurabh 	int err = 0;
3861181412cSSaurabh 
387b9959fd3SAmerigo Wang 	if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
388607259a6SChristoph Hellwig 		if (p->iph.version != 4 || p->iph.protocol != IPPROTO_IPIP ||
389607259a6SChristoph Hellwig 		    p->iph.ihl != 5)
390b9959fd3SAmerigo Wang 			return -EINVAL;
3911181412cSSaurabh 	}
3921181412cSSaurabh 
3935832c4a7SAlexander Lobakin 	if (!ip_tunnel_flags_is_be16_compat(p->i_flags) ||
3945832c4a7SAlexander Lobakin 	    !ip_tunnel_flags_is_be16_compat(p->o_flags))
3955832c4a7SAlexander Lobakin 		return -EOVERFLOW;
3965832c4a7SAlexander Lobakin 
3975832c4a7SAlexander Lobakin 	if (!(ip_tunnel_flags_to_be16(p->i_flags) & GRE_KEY))
398607259a6SChristoph Hellwig 		p->i_key = 0;
3995832c4a7SAlexander Lobakin 	if (!(ip_tunnel_flags_to_be16(p->o_flags) & GRE_KEY))
400607259a6SChristoph Hellwig 		p->o_key = 0;
4017c8e6b9cSDmitry Popov 
4025832c4a7SAlexander Lobakin 	__set_bit(IP_TUNNEL_VTI_BIT, flags);
4035832c4a7SAlexander Lobakin 	ip_tunnel_flags_copy(p->i_flags, flags);
4047c8e6b9cSDmitry Popov 
405607259a6SChristoph Hellwig 	err = ip_tunnel_ctl(dev, p, cmd);
406b9959fd3SAmerigo Wang 	if (err)
407b9959fd3SAmerigo Wang 		return err;
408b9959fd3SAmerigo Wang 
409b9959fd3SAmerigo Wang 	if (cmd != SIOCDELTUNNEL) {
4105832c4a7SAlexander Lobakin 		ip_tunnel_flags_from_be16(flags, GRE_KEY);
4115832c4a7SAlexander Lobakin 		ip_tunnel_flags_or(p->i_flags, p->i_flags, flags);
4125832c4a7SAlexander Lobakin 		ip_tunnel_flags_or(p->o_flags, p->o_flags, flags);
4131181412cSSaurabh 	}
4141181412cSSaurabh 	return 0;
4151181412cSSaurabh }
4161181412cSSaurabh 
4171181412cSSaurabh static const struct net_device_ops vti_netdev_ops = {
4181181412cSSaurabh 	.ndo_init	= vti_tunnel_init,
419b9959fd3SAmerigo Wang 	.ndo_uninit	= ip_tunnel_uninit,
4201181412cSSaurabh 	.ndo_start_xmit	= vti_tunnel_xmit,
4213e7a1c7cSArnd Bergmann 	.ndo_siocdevprivate = ip_tunnel_siocdevprivate,
422b9959fd3SAmerigo Wang 	.ndo_change_mtu	= ip_tunnel_change_mtu,
4238f3feb24SHeiner Kallweit 	.ndo_get_stats64 = dev_get_tstats64,
4241e99584bSNicolas Dichtel 	.ndo_get_iflink = ip_tunnel_get_iflink,
425607259a6SChristoph Hellwig 	.ndo_tunnel_ctl	= vti_tunnel_ctl,
4261181412cSSaurabh };
4271181412cSSaurabh 
vti_tunnel_setup(struct net_device * dev)4281181412cSSaurabh static void vti_tunnel_setup(struct net_device *dev)
4291181412cSSaurabh {
4301181412cSSaurabh 	dev->netdev_ops		= &vti_netdev_ops;
431ab59d2b6SJason A. Donenfeld 	dev->header_ops		= &ip_tunnel_header_ops;
4328d89dcdfSNicolas Dichtel 	dev->type		= ARPHRD_TUNNEL;
433b9959fd3SAmerigo Wang 	ip_tunnel_setup(dev, vti_net_id);
434b9959fd3SAmerigo Wang }
435b9959fd3SAmerigo Wang 
vti_tunnel_init(struct net_device * dev)436b9959fd3SAmerigo Wang static int vti_tunnel_init(struct net_device *dev)
437b9959fd3SAmerigo Wang {
438b9959fd3SAmerigo Wang 	struct ip_tunnel *tunnel = netdev_priv(dev);
439b9959fd3SAmerigo Wang 	struct iphdr *iph = &tunnel->parms.iph;
440b9959fd3SAmerigo Wang 
4415a1b7e1aSJakub Kicinski 	__dev_addr_set(dev, &iph->saddr, 4);
442b9959fd3SAmerigo Wang 	memcpy(dev->broadcast, &iph->daddr, 4);
4431181412cSSaurabh 
4441181412cSSaurabh 	dev->flags		= IFF_NOARP;
4451181412cSSaurabh 	dev->addr_len		= 4;
446*00d066a4SAlexander Lobakin 	dev->lltx		= true;
44702875878SEric Dumazet 	netif_keep_dst(dev);
448b9959fd3SAmerigo Wang 
449b9959fd3SAmerigo Wang 	return ip_tunnel_init(dev);
4501181412cSSaurabh }
4511181412cSSaurabh 
vti_fb_tunnel_init(struct net_device * dev)452b9959fd3SAmerigo Wang static void __net_init vti_fb_tunnel_init(struct net_device *dev)
4531181412cSSaurabh {
4541181412cSSaurabh 	struct ip_tunnel *tunnel = netdev_priv(dev);
4551181412cSSaurabh 	struct iphdr *iph = &tunnel->parms.iph;
4561181412cSSaurabh 
4571181412cSSaurabh 	iph->version		= 4;
4581181412cSSaurabh 	iph->protocol		= IPPROTO_IPIP;
4591181412cSSaurabh 	iph->ihl		= 5;
4601181412cSSaurabh }
4611181412cSSaurabh 
462df3893c1SSteffen Klassert static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
463f981c57fSJeremy Sowden 	.handler	=	vti_rcv_proto,
464f981c57fSJeremy Sowden 	.input_handler	=	vti_input_proto,
465df3893c1SSteffen Klassert 	.cb_handler	=	vti_rcv_cb,
466df3893c1SSteffen Klassert 	.err_handler	=	vti4_err,
467df3893c1SSteffen Klassert 	.priority	=	100,
468df3893c1SSteffen Klassert };
469df3893c1SSteffen Klassert 
470df3893c1SSteffen Klassert static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
471f981c57fSJeremy Sowden 	.handler	=	vti_rcv_proto,
472f981c57fSJeremy Sowden 	.input_handler	=	vti_input_proto,
473df3893c1SSteffen Klassert 	.cb_handler	=	vti_rcv_cb,
474df3893c1SSteffen Klassert 	.err_handler	=	vti4_err,
475df3893c1SSteffen Klassert 	.priority	=	100,
476df3893c1SSteffen Klassert };
477df3893c1SSteffen Klassert 
478df3893c1SSteffen Klassert static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
479f981c57fSJeremy Sowden 	.handler	=	vti_rcv_proto,
480f981c57fSJeremy Sowden 	.input_handler	=	vti_input_proto,
481df3893c1SSteffen Klassert 	.cb_handler	=	vti_rcv_cb,
482df3893c1SSteffen Klassert 	.err_handler	=	vti4_err,
483df3893c1SSteffen Klassert 	.priority	=	100,
4841181412cSSaurabh };
4851181412cSSaurabh 
48687e66b96SXin Long #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
vti_rcv_tunnel(struct sk_buff * skb)48787e66b96SXin Long static int vti_rcv_tunnel(struct sk_buff *skb)
48887e66b96SXin Long {
48987e66b96SXin Long 	XFRM_SPI_SKB_CB(skb)->family = AF_INET;
49087e66b96SXin Long 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
49187e66b96SXin Long 
49287e66b96SXin Long 	return vti_input(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr, 0, false);
49387e66b96SXin Long }
49487e66b96SXin Long 
49587e66b96SXin Long static struct xfrm_tunnel vti_ipip_handler __read_mostly = {
496f981c57fSJeremy Sowden 	.handler	=	vti_rcv_tunnel,
49787e66b96SXin Long 	.cb_handler	=	vti_rcv_cb,
498dd9ee344SSu Yanjun 	.err_handler	=	vti4_err,
499dd9ee344SSu Yanjun 	.priority	=	0,
500dd9ee344SSu Yanjun };
501dd9ee344SSu Yanjun 
50261ee4137SYueHaibing #if IS_ENABLED(CONFIG_IPV6)
50355a48c7eSXin Long static struct xfrm_tunnel vti_ipip6_handler __read_mostly = {
50455a48c7eSXin Long 	.handler	=	vti_rcv_tunnel,
50555a48c7eSXin Long 	.cb_handler	=	vti_rcv_cb,
50655a48c7eSXin Long 	.err_handler	=	vti4_err,
50755a48c7eSXin Long 	.priority	=	0,
50855a48c7eSXin Long };
50987e66b96SXin Long #endif
51061ee4137SYueHaibing #endif
5111181412cSSaurabh 
vti_init_net(struct net * net)5121181412cSSaurabh static int __net_init vti_init_net(struct net *net)
5131181412cSSaurabh {
5141181412cSSaurabh 	int err;
515b9959fd3SAmerigo Wang 	struct ip_tunnel_net *itn;
5161181412cSSaurabh 
517b9959fd3SAmerigo Wang 	err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
5181181412cSSaurabh 	if (err)
5191181412cSSaurabh 		return err;
520b9959fd3SAmerigo Wang 	itn = net_generic(net, vti_net_id);
521cd1aa9c2SHaishuang Yan 	if (itn->fb_tunnel_dev)
522b9959fd3SAmerigo Wang 		vti_fb_tunnel_init(itn->fb_tunnel_dev);
523b9959fd3SAmerigo Wang 	return 0;
5241181412cSSaurabh }
5251181412cSSaurabh 
vti_exit_batch_rtnl(struct list_head * list_net,struct list_head * dev_to_kill)5269b5b3637SEric Dumazet static void __net_exit vti_exit_batch_rtnl(struct list_head *list_net,
5279b5b3637SEric Dumazet 					   struct list_head *dev_to_kill)
5281181412cSSaurabh {
5299b5b3637SEric Dumazet 	ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops,
5309b5b3637SEric Dumazet 			      dev_to_kill);
5311181412cSSaurabh }
5321181412cSSaurabh 
5331181412cSSaurabh static struct pernet_operations vti_net_ops = {
5341181412cSSaurabh 	.init = vti_init_net,
5359b5b3637SEric Dumazet 	.exit_batch_rtnl = vti_exit_batch_rtnl,
5361181412cSSaurabh 	.id   = &vti_net_id,
537b9959fd3SAmerigo Wang 	.size = sizeof(struct ip_tunnel_net),
5381181412cSSaurabh };
5391181412cSSaurabh 
vti_tunnel_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)540a8b8a889SMatthias Schiffer static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
541a8b8a889SMatthias Schiffer 			       struct netlink_ext_ack *extack)
5421181412cSSaurabh {
5431181412cSSaurabh 	return 0;
5441181412cSSaurabh }
5451181412cSSaurabh 
vti_netlink_parms(struct nlattr * data[],struct ip_tunnel_parm_kern * parms,__u32 * fwmark)5461181412cSSaurabh static void vti_netlink_parms(struct nlattr *data[],
547117aef12SAlexander Lobakin 			      struct ip_tunnel_parm_kern *parms,
5489830ad4cSCraig Gallek 			      __u32 *fwmark)
5491181412cSSaurabh {
5501181412cSSaurabh 	memset(parms, 0, sizeof(*parms));
5511181412cSSaurabh 
5521181412cSSaurabh 	parms->iph.protocol = IPPROTO_IPIP;
5531181412cSSaurabh 
5541181412cSSaurabh 	if (!data)
5551181412cSSaurabh 		return;
5561181412cSSaurabh 
5575832c4a7SAlexander Lobakin 	__set_bit(IP_TUNNEL_VTI_BIT, parms->i_flags);
558df3893c1SSteffen Klassert 
5591181412cSSaurabh 	if (data[IFLA_VTI_LINK])
5601181412cSSaurabh 		parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
5611181412cSSaurabh 
5621181412cSSaurabh 	if (data[IFLA_VTI_IKEY])
5631181412cSSaurabh 		parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
5641181412cSSaurabh 
5651181412cSSaurabh 	if (data[IFLA_VTI_OKEY])
5661181412cSSaurabh 		parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
5671181412cSSaurabh 
5681181412cSSaurabh 	if (data[IFLA_VTI_LOCAL])
56967b61f6cSJiri Benc 		parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
5701181412cSSaurabh 
5711181412cSSaurabh 	if (data[IFLA_VTI_REMOTE])
57267b61f6cSJiri Benc 		parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
5731181412cSSaurabh 
5749830ad4cSCraig Gallek 	if (data[IFLA_VTI_FWMARK])
5759830ad4cSCraig Gallek 		*fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
5761181412cSSaurabh }
5771181412cSSaurabh 
vti_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)5781181412cSSaurabh static int vti_newlink(struct net *src_net, struct net_device *dev,
5797a3f4a18SMatthias Schiffer 		       struct nlattr *tb[], struct nlattr *data[],
5807a3f4a18SMatthias Schiffer 		       struct netlink_ext_ack *extack)
5811181412cSSaurabh {
582117aef12SAlexander Lobakin 	struct ip_tunnel_parm_kern parms;
5839830ad4cSCraig Gallek 	__u32 fwmark = 0;
5841181412cSSaurabh 
5859830ad4cSCraig Gallek 	vti_netlink_parms(data, &parms, &fwmark);
5869830ad4cSCraig Gallek 	return ip_tunnel_newlink(dev, tb, &parms, fwmark);
5871181412cSSaurabh }
5881181412cSSaurabh 
vti_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)5891181412cSSaurabh static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
590ad744b22SMatthias Schiffer 			  struct nlattr *data[],
591ad744b22SMatthias Schiffer 			  struct netlink_ext_ack *extack)
5921181412cSSaurabh {
5939830ad4cSCraig Gallek 	struct ip_tunnel *t = netdev_priv(dev);
594117aef12SAlexander Lobakin 	struct ip_tunnel_parm_kern p;
5959830ad4cSCraig Gallek 	__u32 fwmark = t->fwmark;
5961181412cSSaurabh 
5979830ad4cSCraig Gallek 	vti_netlink_parms(data, &p, &fwmark);
5989830ad4cSCraig Gallek 	return ip_tunnel_changelink(dev, tb, &p, fwmark);
5991181412cSSaurabh }
6001181412cSSaurabh 
vti_get_size(const struct net_device * dev)6011181412cSSaurabh static size_t vti_get_size(const struct net_device *dev)
6021181412cSSaurabh {
6031181412cSSaurabh 	return
6041181412cSSaurabh 		/* IFLA_VTI_LINK */
6051181412cSSaurabh 		nla_total_size(4) +
6061181412cSSaurabh 		/* IFLA_VTI_IKEY */
6071181412cSSaurabh 		nla_total_size(4) +
6081181412cSSaurabh 		/* IFLA_VTI_OKEY */
6091181412cSSaurabh 		nla_total_size(4) +
6101181412cSSaurabh 		/* IFLA_VTI_LOCAL */
6111181412cSSaurabh 		nla_total_size(4) +
6121181412cSSaurabh 		/* IFLA_VTI_REMOTE */
6131181412cSSaurabh 		nla_total_size(4) +
6149830ad4cSCraig Gallek 		/* IFLA_VTI_FWMARK */
6159830ad4cSCraig Gallek 		nla_total_size(4) +
6161181412cSSaurabh 		0;
6171181412cSSaurabh }
6181181412cSSaurabh 
vti_fill_info(struct sk_buff * skb,const struct net_device * dev)6191181412cSSaurabh static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
6201181412cSSaurabh {
6211181412cSSaurabh 	struct ip_tunnel *t = netdev_priv(dev);
622117aef12SAlexander Lobakin 	struct ip_tunnel_parm_kern *p = &t->parms;
6231181412cSSaurabh 
6248ed508fdSHangbin Liu 	if (nla_put_u32(skb, IFLA_VTI_LINK, p->link) ||
6258ed508fdSHangbin Liu 	    nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key) ||
6268ed508fdSHangbin Liu 	    nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key) ||
6278ed508fdSHangbin Liu 	    nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr) ||
6288ed508fdSHangbin Liu 	    nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr) ||
6298ed508fdSHangbin Liu 	    nla_put_u32(skb, IFLA_VTI_FWMARK, t->fwmark))
6308ed508fdSHangbin Liu 		return -EMSGSIZE;
6311181412cSSaurabh 
6321181412cSSaurabh 	return 0;
6331181412cSSaurabh }
6341181412cSSaurabh 
6351181412cSSaurabh static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
6361181412cSSaurabh 	[IFLA_VTI_LINK]		= { .type = NLA_U32 },
6371181412cSSaurabh 	[IFLA_VTI_IKEY]		= { .type = NLA_U32 },
6381181412cSSaurabh 	[IFLA_VTI_OKEY]		= { .type = NLA_U32 },
639c593642cSPankaj Bharadiya 	[IFLA_VTI_LOCAL]	= { .len = sizeof_field(struct iphdr, saddr) },
640c593642cSPankaj Bharadiya 	[IFLA_VTI_REMOTE]	= { .len = sizeof_field(struct iphdr, daddr) },
6419830ad4cSCraig Gallek 	[IFLA_VTI_FWMARK]	= { .type = NLA_U32 },
6421181412cSSaurabh };
6431181412cSSaurabh 
6441181412cSSaurabh static struct rtnl_link_ops vti_link_ops __read_mostly = {
6451181412cSSaurabh 	.kind		= "vti",
6461181412cSSaurabh 	.maxtype	= IFLA_VTI_MAX,
6471181412cSSaurabh 	.policy		= vti_policy,
6481181412cSSaurabh 	.priv_size	= sizeof(struct ip_tunnel),
6491181412cSSaurabh 	.setup		= vti_tunnel_setup,
6501181412cSSaurabh 	.validate	= vti_tunnel_validate,
6511181412cSSaurabh 	.newlink	= vti_newlink,
6521181412cSSaurabh 	.changelink	= vti_changelink,
65320ea60caSlucien 	.dellink        = ip_tunnel_dellink,
6541181412cSSaurabh 	.get_size	= vti_get_size,
6551181412cSSaurabh 	.fill_info	= vti_fill_info,
6561728d4faSNicolas Dichtel 	.get_link_net	= ip_tunnel_get_link_net,
6571181412cSSaurabh };
6581181412cSSaurabh 
vti_init(void)6591181412cSSaurabh static int __init vti_init(void)
6601181412cSSaurabh {
6611990e4f8SMathias Krause 	const char *msg;
6621181412cSSaurabh 	int err;
6631181412cSSaurabh 
6641990e4f8SMathias Krause 	pr_info("IPv4 over IPsec tunneling driver\n");
6651181412cSSaurabh 
6661990e4f8SMathias Krause 	msg = "tunnel device";
6671181412cSSaurabh 	err = register_pernet_device(&vti_net_ops);
6681181412cSSaurabh 	if (err < 0)
6691990e4f8SMathias Krause 		goto pernet_dev_failed;
6701990e4f8SMathias Krause 
6711990e4f8SMathias Krause 	msg = "tunnel protocols";
672df3893c1SSteffen Klassert 	err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
6731990e4f8SMathias Krause 	if (err < 0)
6741990e4f8SMathias Krause 		goto xfrm_proto_esp_failed;
675df3893c1SSteffen Klassert 	err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
6761990e4f8SMathias Krause 	if (err < 0)
6771990e4f8SMathias Krause 		goto xfrm_proto_ah_failed;
678df3893c1SSteffen Klassert 	err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
6791990e4f8SMathias Krause 	if (err < 0)
6801990e4f8SMathias Krause 		goto xfrm_proto_comp_failed;
681df3893c1SSteffen Klassert 
68287e66b96SXin Long #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
683dd9ee344SSu Yanjun 	msg = "ipip tunnel";
68487e66b96SXin Long 	err = xfrm4_tunnel_register(&vti_ipip_handler, AF_INET);
68501ce31c5SJeremy Sowden 	if (err < 0)
686e6ce6457SXin Long 		goto xfrm_tunnel_ipip_failed;
687e6ce6457SXin Long #if IS_ENABLED(CONFIG_IPV6)
68855a48c7eSXin Long 	err = xfrm4_tunnel_register(&vti_ipip6_handler, AF_INET6);
689e6ce6457SXin Long 	if (err < 0)
690e6ce6457SXin Long 		goto xfrm_tunnel_ipip6_failed;
691e6ce6457SXin Long #endif
69287e66b96SXin Long #endif
693dd9ee344SSu Yanjun 
6941990e4f8SMathias Krause 	msg = "netlink interface";
6951181412cSSaurabh 	err = rtnl_link_register(&vti_link_ops);
6961181412cSSaurabh 	if (err < 0)
6971181412cSSaurabh 		goto rtnl_link_failed;
6981181412cSSaurabh 
6991181412cSSaurabh 	return err;
7001181412cSSaurabh 
7011181412cSSaurabh rtnl_link_failed:
70287e66b96SXin Long #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
703e6ce6457SXin Long #if IS_ENABLED(CONFIG_IPV6)
70455a48c7eSXin Long 	xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
705e6ce6457SXin Long xfrm_tunnel_ipip6_failed:
706e6ce6457SXin Long #endif
70787e66b96SXin Long 	xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
708e6ce6457SXin Long xfrm_tunnel_ipip_failed:
70987e66b96SXin Long #endif
7105483844cSJeremy Sowden 	xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
7111990e4f8SMathias Krause xfrm_proto_comp_failed:
712df3893c1SSteffen Klassert 	xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
7131990e4f8SMathias Krause xfrm_proto_ah_failed:
714df3893c1SSteffen Klassert 	xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
7151990e4f8SMathias Krause xfrm_proto_esp_failed:
7161181412cSSaurabh 	unregister_pernet_device(&vti_net_ops);
7171990e4f8SMathias Krause pernet_dev_failed:
7181990e4f8SMathias Krause 	pr_err("vti init: failed to register %s\n", msg);
7191181412cSSaurabh 	return err;
7201181412cSSaurabh }
7211181412cSSaurabh 
vti_fini(void)7221181412cSSaurabh static void __exit vti_fini(void)
7231181412cSSaurabh {
7241181412cSSaurabh 	rtnl_link_unregister(&vti_link_ops);
72587e66b96SXin Long #if IS_ENABLED(CONFIG_INET_XFRM_TUNNEL)
726e6ce6457SXin Long #if IS_ENABLED(CONFIG_IPV6)
72755a48c7eSXin Long 	xfrm4_tunnel_deregister(&vti_ipip6_handler, AF_INET6);
728e6ce6457SXin Long #endif
72987e66b96SXin Long 	xfrm4_tunnel_deregister(&vti_ipip_handler, AF_INET);
73087e66b96SXin Long #endif
7311990e4f8SMathias Krause 	xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
7321990e4f8SMathias Krause 	xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
7331990e4f8SMathias Krause 	xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
7341181412cSSaurabh 	unregister_pernet_device(&vti_net_ops);
7351181412cSSaurabh }
7361181412cSSaurabh 
7371181412cSSaurabh module_init(vti_init);
7381181412cSSaurabh module_exit(vti_fini);
739b058a5d2SBreno Leitao MODULE_DESCRIPTION("Virtual (secure) IP tunneling library");
7401181412cSSaurabh MODULE_LICENSE("GPL");
7411181412cSSaurabh MODULE_ALIAS_RTNL_LINK("vti");
7421181412cSSaurabh MODULE_ALIAS_NETDEV("ip_vti0");
743