xref: /openbmc/linux/net/ipv4/ip_tunnel_core.c (revision 0eb8eb2f)
1c9422999SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20e6fbc5bSPravin B Shelar /*
30e6fbc5bSPravin B Shelar  * Copyright (c) 2013 Nicira, Inc.
40e6fbc5bSPravin B Shelar  */
50e6fbc5bSPravin B Shelar 
60e6fbc5bSPravin B Shelar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
70e6fbc5bSPravin B Shelar 
80e6fbc5bSPravin B Shelar #include <linux/types.h>
90e6fbc5bSPravin B Shelar #include <linux/kernel.h>
100e6fbc5bSPravin B Shelar #include <linux/skbuff.h>
110e6fbc5bSPravin B Shelar #include <linux/netdevice.h>
120e6fbc5bSPravin B Shelar #include <linux/in.h>
130e6fbc5bSPravin B Shelar #include <linux/if_arp.h>
140e6fbc5bSPravin B Shelar #include <linux/init.h>
150e6fbc5bSPravin B Shelar #include <linux/in6.h>
160e6fbc5bSPravin B Shelar #include <linux/inetdevice.h>
170e6fbc5bSPravin B Shelar #include <linux/netfilter_ipv4.h>
180e6fbc5bSPravin B Shelar #include <linux/etherdevice.h>
190e6fbc5bSPravin B Shelar #include <linux/if_ether.h>
200e6fbc5bSPravin B Shelar #include <linux/if_vlan.h>
21e7030878SThomas Graf #include <linux/static_key.h>
220e6fbc5bSPravin B Shelar 
230e6fbc5bSPravin B Shelar #include <net/ip.h>
240e6fbc5bSPravin B Shelar #include <net/icmp.h>
250e6fbc5bSPravin B Shelar #include <net/protocol.h>
260e6fbc5bSPravin B Shelar #include <net/ip_tunnels.h>
27058214a4STom Herbert #include <net/ip6_tunnel.h>
280e6fbc5bSPravin B Shelar #include <net/arp.h>
290e6fbc5bSPravin B Shelar #include <net/checksum.h>
300e6fbc5bSPravin B Shelar #include <net/dsfield.h>
310e6fbc5bSPravin B Shelar #include <net/inet_ecn.h>
320e6fbc5bSPravin B Shelar #include <net/xfrm.h>
330e6fbc5bSPravin B Shelar #include <net/net_namespace.h>
340e6fbc5bSPravin B Shelar #include <net/netns/generic.h>
350e6fbc5bSPravin B Shelar #include <net/rtnetlink.h>
3663d008a4SJiri Benc #include <net/dst_metadata.h>
370e6fbc5bSPravin B Shelar 
3855c2bc14STom Herbert const struct ip_tunnel_encap_ops __rcu *
3955c2bc14STom Herbert 		iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
4055c2bc14STom Herbert EXPORT_SYMBOL(iptun_encaps);
4155c2bc14STom Herbert 
42058214a4STom Herbert const struct ip6_tnl_encap_ops __rcu *
43058214a4STom Herbert 		ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
44058214a4STom Herbert EXPORT_SYMBOL(ip6tun_encaps);
45058214a4STom Herbert 
46039f5062SPravin B Shelar void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
470e6fbc5bSPravin B Shelar 		   __be32 src, __be32 dst, __u8 proto,
48963a88b3SNicolas Dichtel 		   __u8 tos, __u8 ttl, __be16 df, bool xnet)
490e6fbc5bSPravin B Shelar {
50bc22a0e2SNicolas Dichtel 	int pkt_len = skb->len - skb_inner_network_offset(skb);
51f859b0f6SEric W. Biederman 	struct net *net = dev_net(rt->dst.dev);
52039f5062SPravin B Shelar 	struct net_device *dev = skb->dev;
530e6fbc5bSPravin B Shelar 	struct iphdr *iph;
540e6fbc5bSPravin B Shelar 	int err;
550e6fbc5bSPravin B Shelar 
56963a88b3SNicolas Dichtel 	skb_scrub_packet(skb, xnet);
57963a88b3SNicolas Dichtel 
58bf8d85d4SEric Dumazet 	skb_clear_hash_if_not_l4(skb);
590e6fbc5bSPravin B Shelar 	skb_dst_set(skb, &rt->dst);
600e6fbc5bSPravin B Shelar 	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
610e6fbc5bSPravin B Shelar 
620e6fbc5bSPravin B Shelar 	/* Push down and install the IP header. */
6378a3694dSSteffen Klassert 	skb_push(skb, sizeof(struct iphdr));
640e6fbc5bSPravin B Shelar 	skb_reset_network_header(skb);
650e6fbc5bSPravin B Shelar 
660e6fbc5bSPravin B Shelar 	iph = ip_hdr(skb);
670e6fbc5bSPravin B Shelar 
680e6fbc5bSPravin B Shelar 	iph->version	=	4;
690e6fbc5bSPravin B Shelar 	iph->ihl	=	sizeof(struct iphdr) >> 2;
7016f7eb2bSSabrina Dubroca 	iph->frag_off	=	ip_mtu_locked(&rt->dst) ? 0 : df;
710e6fbc5bSPravin B Shelar 	iph->protocol	=	proto;
720e6fbc5bSPravin B Shelar 	iph->tos	=	tos;
730e6fbc5bSPravin B Shelar 	iph->daddr	=	dst;
740e6fbc5bSPravin B Shelar 	iph->saddr	=	src;
750e6fbc5bSPravin B Shelar 	iph->ttl	=	ttl;
76f859b0f6SEric W. Biederman 	__ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
770e6fbc5bSPravin B Shelar 
7833224b16SEric W. Biederman 	err = ip_local_out(net, sk, skb);
795684abf7SXin Long 
805684abf7SXin Long 	if (dev) {
810e6fbc5bSPravin B Shelar 		if (unlikely(net_xmit_eval(err)))
820e6fbc5bSPravin B Shelar 			pkt_len = 0;
83039f5062SPravin B Shelar 		iptunnel_xmit_stats(dev, pkt_len);
840e6fbc5bSPravin B Shelar 	}
855684abf7SXin Long }
860e6fbc5bSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_xmit);
873d7b46cdSPravin B Shelar 
88a6d5bbf3SJiri Benc int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
89a6d5bbf3SJiri Benc 			   __be16 inner_proto, bool raw_proto, bool xnet)
903d7b46cdSPravin B Shelar {
913d7b46cdSPravin B Shelar 	if (unlikely(!pskb_may_pull(skb, hdr_len)))
923d7b46cdSPravin B Shelar 		return -ENOMEM;
933d7b46cdSPravin B Shelar 
943d7b46cdSPravin B Shelar 	skb_pull_rcsum(skb, hdr_len);
953d7b46cdSPravin B Shelar 
96a6d5bbf3SJiri Benc 	if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
971245dfc8SLi RongQing 		struct ethhdr *eh;
983d7b46cdSPravin B Shelar 
993d7b46cdSPravin B Shelar 		if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1003d7b46cdSPravin B Shelar 			return -ENOMEM;
1013d7b46cdSPravin B Shelar 
1021245dfc8SLi RongQing 		eh = (struct ethhdr *)skb->data;
103d181ddcaSAlexander Duyck 		if (likely(eth_proto_is_802_3(eh->h_proto)))
1043d7b46cdSPravin B Shelar 			skb->protocol = eh->h_proto;
1053d7b46cdSPravin B Shelar 		else
1063d7b46cdSPravin B Shelar 			skb->protocol = htons(ETH_P_802_2);
1073d7b46cdSPravin B Shelar 
1083d7b46cdSPravin B Shelar 	} else {
1093d7b46cdSPravin B Shelar 		skb->protocol = inner_proto;
1103d7b46cdSPravin B Shelar 	}
1113d7b46cdSPravin B Shelar 
1127539fadcSTom Herbert 	skb_clear_hash_if_not_l4(skb);
1133e2ed0c2SMichał Mirosław 	__vlan_hwaccel_clear_tag(skb);
1143d7b46cdSPravin B Shelar 	skb_set_queue_mapping(skb, 0);
1157f290c94SJiri Benc 	skb_scrub_packet(skb, xnet);
116a09a4c8dSJesse Gross 
117a09a4c8dSJesse Gross 	return iptunnel_pull_offloads(skb);
1183d7b46cdSPravin B Shelar }
119a6d5bbf3SJiri Benc EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
1202d26f0a3SEric Dumazet 
12163d008a4SJiri Benc struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
12263d008a4SJiri Benc 					     gfp_t flags)
12363d008a4SJiri Benc {
12463d008a4SJiri Benc 	struct metadata_dst *res;
12563d008a4SJiri Benc 	struct ip_tunnel_info *dst, *src;
12663d008a4SJiri Benc 
1273fcece12SJakub Kicinski 	if (!md || md->type != METADATA_IP_TUNNEL ||
1283fcece12SJakub Kicinski 	    md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
12963d008a4SJiri Benc 		return NULL;
13063d008a4SJiri Benc 
131f52f11ecSXin Long 	src = &md->u.tun_info;
132f52f11ecSXin Long 	res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
13363d008a4SJiri Benc 	if (!res)
13463d008a4SJiri Benc 		return NULL;
13563d008a4SJiri Benc 
13663d008a4SJiri Benc 	dst = &res->u.tun_info;
13763d008a4SJiri Benc 	dst->key.tun_id = src->key.tun_id;
13863d008a4SJiri Benc 	if (src->mode & IP_TUNNEL_INFO_IPV6)
13963d008a4SJiri Benc 		memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
14063d008a4SJiri Benc 		       sizeof(struct in6_addr));
14163d008a4SJiri Benc 	else
14263d008a4SJiri Benc 		dst->key.u.ipv4.dst = src->key.u.ipv4.src;
1437bdca378Swenxu 	dst->key.tun_flags = src->key.tun_flags;
14463d008a4SJiri Benc 	dst->mode = src->mode | IP_TUNNEL_INFO_TX;
145f52f11ecSXin Long 	ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
146f52f11ecSXin Long 				src->options_len, 0);
14763d008a4SJiri Benc 
14863d008a4SJiri Benc 	return res;
14963d008a4SJiri Benc }
15063d008a4SJiri Benc EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
15163d008a4SJiri Benc 
152aed069dfSAlexander Duyck int iptunnel_handle_offloads(struct sk_buff *skb,
1532d26f0a3SEric Dumazet 			     int gso_type_mask)
1542d26f0a3SEric Dumazet {
1552d26f0a3SEric Dumazet 	int err;
1562d26f0a3SEric Dumazet 
1572d26f0a3SEric Dumazet 	if (likely(!skb->encapsulation)) {
1582d26f0a3SEric Dumazet 		skb_reset_inner_headers(skb);
1592d26f0a3SEric Dumazet 		skb->encapsulation = 1;
1602d26f0a3SEric Dumazet 	}
1612d26f0a3SEric Dumazet 
1622d26f0a3SEric Dumazet 	if (skb_is_gso(skb)) {
1639580bf2eSEric Dumazet 		err = skb_header_unclone(skb, GFP_ATOMIC);
1642d26f0a3SEric Dumazet 		if (unlikely(err))
165aed069dfSAlexander Duyck 			return err;
1662d26f0a3SEric Dumazet 		skb_shinfo(skb)->gso_type |= gso_type_mask;
167aed069dfSAlexander Duyck 		return 0;
1682d26f0a3SEric Dumazet 	}
1692d26f0a3SEric Dumazet 
1706fa79666SEdward Cree 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
1712d26f0a3SEric Dumazet 		skb->ip_summed = CHECKSUM_NONE;
1726fa79666SEdward Cree 		/* We clear encapsulation here to prevent badly-written
1736fa79666SEdward Cree 		 * drivers potentially deciding to offload an inner checksum
1746fa79666SEdward Cree 		 * if we set CHECKSUM_PARTIAL on the outer header.
1756fa79666SEdward Cree 		 * This should go away when the drivers are all fixed.
1766fa79666SEdward Cree 		 */
177179bc67fSEdward Cree 		skb->encapsulation = 0;
178179bc67fSEdward Cree 	}
1792d26f0a3SEric Dumazet 
180aed069dfSAlexander Duyck 	return 0;
1812d26f0a3SEric Dumazet }
1822d26f0a3SEric Dumazet EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
183ebe44f35SDavid S. Miller 
184ebe44f35SDavid S. Miller /* Often modified stats are per cpu, other are shared (netdev->stats) */
185bc1f4470Sstephen hemminger void ip_tunnel_get_stats64(struct net_device *dev,
186ebe44f35SDavid S. Miller 			   struct rtnl_link_stats64 *tot)
187ebe44f35SDavid S. Miller {
188ebe44f35SDavid S. Miller 	int i;
189ebe44f35SDavid S. Miller 
190c24a5964SAlexander Duyck 	netdev_stats_to_stats64(tot, &dev->stats);
191c24a5964SAlexander Duyck 
192ebe44f35SDavid S. Miller 	for_each_possible_cpu(i) {
193ebe44f35SDavid S. Miller 		const struct pcpu_sw_netstats *tstats =
194ebe44f35SDavid S. Miller 						   per_cpu_ptr(dev->tstats, i);
195ebe44f35SDavid S. Miller 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
196ebe44f35SDavid S. Miller 		unsigned int start;
197ebe44f35SDavid S. Miller 
198ebe44f35SDavid S. Miller 		do {
19957a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
200ebe44f35SDavid S. Miller 			rx_packets = tstats->rx_packets;
201ebe44f35SDavid S. Miller 			tx_packets = tstats->tx_packets;
202ebe44f35SDavid S. Miller 			rx_bytes = tstats->rx_bytes;
203ebe44f35SDavid S. Miller 			tx_bytes = tstats->tx_bytes;
20457a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
205ebe44f35SDavid S. Miller 
206ebe44f35SDavid S. Miller 		tot->rx_packets += rx_packets;
207ebe44f35SDavid S. Miller 		tot->tx_packets += tx_packets;
208ebe44f35SDavid S. Miller 		tot->rx_bytes   += rx_bytes;
209ebe44f35SDavid S. Miller 		tot->tx_bytes   += tx_bytes;
210ebe44f35SDavid S. Miller 	}
211ebe44f35SDavid S. Miller }
212ebe44f35SDavid S. Miller EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
2133093fbe7SThomas Graf 
214a1c234f9SJiri Benc static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
215a1c234f9SJiri Benc 	[LWTUNNEL_IP_ID]	= { .type = NLA_U64 },
216a1c234f9SJiri Benc 	[LWTUNNEL_IP_DST]	= { .type = NLA_U32 },
217a1c234f9SJiri Benc 	[LWTUNNEL_IP_SRC]	= { .type = NLA_U32 },
218a1c234f9SJiri Benc 	[LWTUNNEL_IP_TTL]	= { .type = NLA_U8 },
219a1c234f9SJiri Benc 	[LWTUNNEL_IP_TOS]	= { .type = NLA_U8 },
220a1c234f9SJiri Benc 	[LWTUNNEL_IP_FLAGS]	= { .type = NLA_U16 },
2213093fbe7SThomas Graf };
2223093fbe7SThomas Graf 
22330357d7dSDavid Ahern static int ip_tun_build_state(struct nlattr *attr,
224127eb7cdSTom Herbert 			      unsigned int family, const void *cfg,
2259ae28727SDavid Ahern 			      struct lwtunnel_state **ts,
2269ae28727SDavid Ahern 			      struct netlink_ext_ack *extack)
2273093fbe7SThomas Graf {
2283093fbe7SThomas Graf 	struct ip_tunnel_info *tun_info;
2293093fbe7SThomas Graf 	struct lwtunnel_state *new_state;
230a1c234f9SJiri Benc 	struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
2313093fbe7SThomas Graf 	int err;
2323093fbe7SThomas Graf 
2338cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
2348cb08174SJohannes Berg 					  ip_tun_policy, extack);
2353093fbe7SThomas Graf 	if (err < 0)
2363093fbe7SThomas Graf 		return err;
2373093fbe7SThomas Graf 
2383093fbe7SThomas Graf 	new_state = lwtunnel_state_alloc(sizeof(*tun_info));
2393093fbe7SThomas Graf 	if (!new_state)
2403093fbe7SThomas Graf 		return -ENOMEM;
2413093fbe7SThomas Graf 
2423093fbe7SThomas Graf 	new_state->type = LWTUNNEL_ENCAP_IP;
2433093fbe7SThomas Graf 
2443093fbe7SThomas Graf 	tun_info = lwt_tun_info(new_state);
2453093fbe7SThomas Graf 
2463d25eabbSwenxu #ifdef CONFIG_DST_CACHE
2473d25eabbSwenxu 	err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
2483d25eabbSwenxu 	if (err) {
2493d25eabbSwenxu 		lwtstate_free(new_state);
2503d25eabbSwenxu 		return err;
2513d25eabbSwenxu 	}
2523d25eabbSwenxu #endif
2533d25eabbSwenxu 
254a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_ID])
25530d3d83aSLance Richardson 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
2563093fbe7SThomas Graf 
257a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_DST])
2587822ce73SHaishuang Yan 		tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
2593093fbe7SThomas Graf 
260a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_SRC])
2617822ce73SHaishuang Yan 		tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
2623093fbe7SThomas Graf 
263a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_TTL])
2647c383fb2SJiri Benc 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
2653093fbe7SThomas Graf 
266a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_TOS])
2677c383fb2SJiri Benc 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
2683093fbe7SThomas Graf 
269a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_FLAGS])
27030d3d83aSLance Richardson 		tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP_FLAGS]);
2713093fbe7SThomas Graf 
2723093fbe7SThomas Graf 	tun_info->mode = IP_TUNNEL_INFO_TX;
2733093fbe7SThomas Graf 	tun_info->options_len = 0;
2743093fbe7SThomas Graf 
2753093fbe7SThomas Graf 	*ts = new_state;
2763093fbe7SThomas Graf 
2773093fbe7SThomas Graf 	return 0;
2783093fbe7SThomas Graf }
2793093fbe7SThomas Graf 
2803d25eabbSwenxu static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
2813d25eabbSwenxu {
2823d25eabbSwenxu #ifdef CONFIG_DST_CACHE
2833d25eabbSwenxu 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
2843d25eabbSwenxu 
2853d25eabbSwenxu 	dst_cache_destroy(&tun_info->dst_cache);
2863d25eabbSwenxu #endif
2873d25eabbSwenxu }
2883d25eabbSwenxu 
2893093fbe7SThomas Graf static int ip_tun_fill_encap_info(struct sk_buff *skb,
2903093fbe7SThomas Graf 				  struct lwtunnel_state *lwtstate)
2913093fbe7SThomas Graf {
2923093fbe7SThomas Graf 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
2933093fbe7SThomas Graf 
294b46f6dedSNicolas Dichtel 	if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
295b46f6dedSNicolas Dichtel 			 LWTUNNEL_IP_PAD) ||
2967822ce73SHaishuang Yan 	    nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
2977822ce73SHaishuang Yan 	    nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
2987c383fb2SJiri Benc 	    nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
2997c383fb2SJiri Benc 	    nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
30030d3d83aSLance Richardson 	    nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags))
3013093fbe7SThomas Graf 		return -ENOMEM;
3023093fbe7SThomas Graf 
3033093fbe7SThomas Graf 	return 0;
3043093fbe7SThomas Graf }
3053093fbe7SThomas Graf 
3063093fbe7SThomas Graf static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
3073093fbe7SThomas Graf {
308b46f6dedSNicolas Dichtel 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP_ID */
309a1c234f9SJiri Benc 		+ nla_total_size(4)	/* LWTUNNEL_IP_DST */
310a1c234f9SJiri Benc 		+ nla_total_size(4)	/* LWTUNNEL_IP_SRC */
311a1c234f9SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP_TOS */
312a1c234f9SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP_TTL */
313a1c234f9SJiri Benc 		+ nla_total_size(2);	/* LWTUNNEL_IP_FLAGS */
3143093fbe7SThomas Graf }
3153093fbe7SThomas Graf 
3162d798499SJiri Benc static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
3172d798499SJiri Benc {
3180eb8eb2fSXin Long 	struct ip_tunnel_info *info_a = lwt_tun_info(a);
3190eb8eb2fSXin Long 	struct ip_tunnel_info *info_b = lwt_tun_info(b);
3200eb8eb2fSXin Long 
3210eb8eb2fSXin Long 	return memcmp(info_a, info_b, sizeof(info_a->key)) ||
3220eb8eb2fSXin Long 	       info_a->mode != info_b->mode ||
3230eb8eb2fSXin Long 	       info_a->options_len != info_b->options_len ||
3240eb8eb2fSXin Long 	       memcmp(ip_tunnel_info_opts(info_a),
3250eb8eb2fSXin Long 		      ip_tunnel_info_opts(info_b), info_a->options_len);
3262d798499SJiri Benc }
3272d798499SJiri Benc 
3283093fbe7SThomas Graf static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
3293093fbe7SThomas Graf 	.build_state = ip_tun_build_state,
3303d25eabbSwenxu 	.destroy_state = ip_tun_destroy_state,
3313093fbe7SThomas Graf 	.fill_encap = ip_tun_fill_encap_info,
3323093fbe7SThomas Graf 	.get_encap_size = ip_tun_encap_nlsize,
3332d798499SJiri Benc 	.cmp_encap = ip_tun_cmp_encap,
33488ff7334SRobert Shearman 	.owner = THIS_MODULE,
3353093fbe7SThomas Graf };
3363093fbe7SThomas Graf 
33732a2b002SJiri Benc static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
33832a2b002SJiri Benc 	[LWTUNNEL_IP6_ID]		= { .type = NLA_U64 },
33932a2b002SJiri Benc 	[LWTUNNEL_IP6_DST]		= { .len = sizeof(struct in6_addr) },
34032a2b002SJiri Benc 	[LWTUNNEL_IP6_SRC]		= { .len = sizeof(struct in6_addr) },
34132a2b002SJiri Benc 	[LWTUNNEL_IP6_HOPLIMIT]		= { .type = NLA_U8 },
34232a2b002SJiri Benc 	[LWTUNNEL_IP6_TC]		= { .type = NLA_U8 },
34332a2b002SJiri Benc 	[LWTUNNEL_IP6_FLAGS]		= { .type = NLA_U16 },
34432a2b002SJiri Benc };
34532a2b002SJiri Benc 
34630357d7dSDavid Ahern static int ip6_tun_build_state(struct nlattr *attr,
347127eb7cdSTom Herbert 			       unsigned int family, const void *cfg,
3489ae28727SDavid Ahern 			       struct lwtunnel_state **ts,
3499ae28727SDavid Ahern 			       struct netlink_ext_ack *extack)
35032a2b002SJiri Benc {
35132a2b002SJiri Benc 	struct ip_tunnel_info *tun_info;
35232a2b002SJiri Benc 	struct lwtunnel_state *new_state;
35332a2b002SJiri Benc 	struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
35432a2b002SJiri Benc 	int err;
35532a2b002SJiri Benc 
3568cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
3578cb08174SJohannes Berg 					  ip6_tun_policy, extack);
35832a2b002SJiri Benc 	if (err < 0)
35932a2b002SJiri Benc 		return err;
36032a2b002SJiri Benc 
36132a2b002SJiri Benc 	new_state = lwtunnel_state_alloc(sizeof(*tun_info));
36232a2b002SJiri Benc 	if (!new_state)
36332a2b002SJiri Benc 		return -ENOMEM;
36432a2b002SJiri Benc 
36532a2b002SJiri Benc 	new_state->type = LWTUNNEL_ENCAP_IP6;
36632a2b002SJiri Benc 
36732a2b002SJiri Benc 	tun_info = lwt_tun_info(new_state);
36832a2b002SJiri Benc 
36932a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_ID])
37030d3d83aSLance Richardson 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
37132a2b002SJiri Benc 
37232a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_DST])
37332a2b002SJiri Benc 		tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
37432a2b002SJiri Benc 
37532a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_SRC])
37632a2b002SJiri Benc 		tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
37732a2b002SJiri Benc 
37832a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_HOPLIMIT])
37932a2b002SJiri Benc 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
38032a2b002SJiri Benc 
38132a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_TC])
38232a2b002SJiri Benc 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
38332a2b002SJiri Benc 
38432a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_FLAGS])
38530d3d83aSLance Richardson 		tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]);
38632a2b002SJiri Benc 
3877f9562a1SJiri Benc 	tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
38832a2b002SJiri Benc 	tun_info->options_len = 0;
38932a2b002SJiri Benc 
39032a2b002SJiri Benc 	*ts = new_state;
39132a2b002SJiri Benc 
39232a2b002SJiri Benc 	return 0;
39332a2b002SJiri Benc }
39432a2b002SJiri Benc 
39532a2b002SJiri Benc static int ip6_tun_fill_encap_info(struct sk_buff *skb,
39632a2b002SJiri Benc 				   struct lwtunnel_state *lwtstate)
39732a2b002SJiri Benc {
39832a2b002SJiri Benc 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
39932a2b002SJiri Benc 
400b46f6dedSNicolas Dichtel 	if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
401b46f6dedSNicolas Dichtel 			 LWTUNNEL_IP6_PAD) ||
40232a2b002SJiri Benc 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
40332a2b002SJiri Benc 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
404995096a0SQuentin Armitage 	    nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
405995096a0SQuentin Armitage 	    nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
40630d3d83aSLance Richardson 	    nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags))
40732a2b002SJiri Benc 		return -ENOMEM;
40832a2b002SJiri Benc 
40932a2b002SJiri Benc 	return 0;
41032a2b002SJiri Benc }
41132a2b002SJiri Benc 
41232a2b002SJiri Benc static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
41332a2b002SJiri Benc {
414b46f6dedSNicolas Dichtel 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP6_ID */
41532a2b002SJiri Benc 		+ nla_total_size(16)	/* LWTUNNEL_IP6_DST */
41632a2b002SJiri Benc 		+ nla_total_size(16)	/* LWTUNNEL_IP6_SRC */
41732a2b002SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP6_HOPLIMIT */
41832a2b002SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP6_TC */
41932a2b002SJiri Benc 		+ nla_total_size(2);	/* LWTUNNEL_IP6_FLAGS */
42032a2b002SJiri Benc }
42132a2b002SJiri Benc 
42232a2b002SJiri Benc static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
42332a2b002SJiri Benc 	.build_state = ip6_tun_build_state,
42432a2b002SJiri Benc 	.fill_encap = ip6_tun_fill_encap_info,
42532a2b002SJiri Benc 	.get_encap_size = ip6_tun_encap_nlsize,
42632a2b002SJiri Benc 	.cmp_encap = ip_tun_cmp_encap,
42788ff7334SRobert Shearman 	.owner = THIS_MODULE,
42832a2b002SJiri Benc };
42932a2b002SJiri Benc 
430045a0fa0SThomas Graf void __init ip_tunnel_core_init(void)
4313093fbe7SThomas Graf {
432fca5fdf6SDaniel Borkmann 	/* If you land here, make sure whether increasing ip_tunnel_info's
433fca5fdf6SDaniel Borkmann 	 * options_len is a reasonable choice with its usage in front ends
434fca5fdf6SDaniel Borkmann 	 * (f.e., it's part of flow keys, etc).
435fca5fdf6SDaniel Borkmann 	 */
436fca5fdf6SDaniel Borkmann 	BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
437fca5fdf6SDaniel Borkmann 
4383093fbe7SThomas Graf 	lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
43932a2b002SJiri Benc 	lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
4403093fbe7SThomas Graf }
441e7030878SThomas Graf 
4425263a98fSDavidlohr Bueso DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
443e7030878SThomas Graf EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
444e7030878SThomas Graf 
445e7030878SThomas Graf void ip_tunnel_need_metadata(void)
446e7030878SThomas Graf {
4475263a98fSDavidlohr Bueso 	static_branch_inc(&ip_tunnel_metadata_cnt);
448e7030878SThomas Graf }
449e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
450e7030878SThomas Graf 
451e7030878SThomas Graf void ip_tunnel_unneed_metadata(void)
452e7030878SThomas Graf {
4535263a98fSDavidlohr Bueso 	static_branch_dec(&ip_tunnel_metadata_cnt);
454e7030878SThomas Graf }
455e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
456