xref: /openbmc/linux/net/ipv4/ip_tunnel_core.c (revision 3d25eabb)
10e6fbc5bSPravin B Shelar /*
20e6fbc5bSPravin B Shelar  * Copyright (c) 2013 Nicira, Inc.
30e6fbc5bSPravin B Shelar  *
40e6fbc5bSPravin B Shelar  * This program is free software; you can redistribute it and/or
50e6fbc5bSPravin B Shelar  * modify it under the terms of version 2 of the GNU General Public
60e6fbc5bSPravin B Shelar  * License as published by the Free Software Foundation.
70e6fbc5bSPravin B Shelar  *
80e6fbc5bSPravin B Shelar  * This program is distributed in the hope that it will be useful, but
90e6fbc5bSPravin B Shelar  * WITHOUT ANY WARRANTY; without even the implied warranty of
100e6fbc5bSPravin B Shelar  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
110e6fbc5bSPravin B Shelar  * General Public License for more details.
120e6fbc5bSPravin B Shelar  *
130e6fbc5bSPravin B Shelar  * You should have received a copy of the GNU General Public License
140e6fbc5bSPravin B Shelar  * along with this program; if not, write to the Free Software
150e6fbc5bSPravin B Shelar  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
160e6fbc5bSPravin B Shelar  * 02110-1301, USA
170e6fbc5bSPravin B Shelar  */
180e6fbc5bSPravin B Shelar 
190e6fbc5bSPravin B Shelar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
200e6fbc5bSPravin B Shelar 
210e6fbc5bSPravin B Shelar #include <linux/types.h>
220e6fbc5bSPravin B Shelar #include <linux/kernel.h>
230e6fbc5bSPravin B Shelar #include <linux/skbuff.h>
240e6fbc5bSPravin B Shelar #include <linux/netdevice.h>
250e6fbc5bSPravin B Shelar #include <linux/in.h>
260e6fbc5bSPravin B Shelar #include <linux/if_arp.h>
270e6fbc5bSPravin B Shelar #include <linux/init.h>
280e6fbc5bSPravin B Shelar #include <linux/in6.h>
290e6fbc5bSPravin B Shelar #include <linux/inetdevice.h>
300e6fbc5bSPravin B Shelar #include <linux/netfilter_ipv4.h>
310e6fbc5bSPravin B Shelar #include <linux/etherdevice.h>
320e6fbc5bSPravin B Shelar #include <linux/if_ether.h>
330e6fbc5bSPravin B Shelar #include <linux/if_vlan.h>
34e7030878SThomas Graf #include <linux/static_key.h>
350e6fbc5bSPravin B Shelar 
360e6fbc5bSPravin B Shelar #include <net/ip.h>
370e6fbc5bSPravin B Shelar #include <net/icmp.h>
380e6fbc5bSPravin B Shelar #include <net/protocol.h>
390e6fbc5bSPravin B Shelar #include <net/ip_tunnels.h>
40058214a4STom Herbert #include <net/ip6_tunnel.h>
410e6fbc5bSPravin B Shelar #include <net/arp.h>
420e6fbc5bSPravin B Shelar #include <net/checksum.h>
430e6fbc5bSPravin B Shelar #include <net/dsfield.h>
440e6fbc5bSPravin B Shelar #include <net/inet_ecn.h>
450e6fbc5bSPravin B Shelar #include <net/xfrm.h>
460e6fbc5bSPravin B Shelar #include <net/net_namespace.h>
470e6fbc5bSPravin B Shelar #include <net/netns/generic.h>
480e6fbc5bSPravin B Shelar #include <net/rtnetlink.h>
4963d008a4SJiri Benc #include <net/dst_metadata.h>
500e6fbc5bSPravin B Shelar 
5155c2bc14STom Herbert const struct ip_tunnel_encap_ops __rcu *
5255c2bc14STom Herbert 		iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
5355c2bc14STom Herbert EXPORT_SYMBOL(iptun_encaps);
5455c2bc14STom Herbert 
55058214a4STom Herbert const struct ip6_tnl_encap_ops __rcu *
56058214a4STom Herbert 		ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
57058214a4STom Herbert EXPORT_SYMBOL(ip6tun_encaps);
58058214a4STom Herbert 
59039f5062SPravin B Shelar void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
600e6fbc5bSPravin B Shelar 		   __be32 src, __be32 dst, __u8 proto,
61963a88b3SNicolas Dichtel 		   __u8 tos, __u8 ttl, __be16 df, bool xnet)
620e6fbc5bSPravin B Shelar {
63bc22a0e2SNicolas Dichtel 	int pkt_len = skb->len - skb_inner_network_offset(skb);
64f859b0f6SEric W. Biederman 	struct net *net = dev_net(rt->dst.dev);
65039f5062SPravin B Shelar 	struct net_device *dev = skb->dev;
660e6fbc5bSPravin B Shelar 	struct iphdr *iph;
670e6fbc5bSPravin B Shelar 	int err;
680e6fbc5bSPravin B Shelar 
69963a88b3SNicolas Dichtel 	skb_scrub_packet(skb, xnet);
70963a88b3SNicolas Dichtel 
71bf8d85d4SEric Dumazet 	skb_clear_hash_if_not_l4(skb);
720e6fbc5bSPravin B Shelar 	skb_dst_set(skb, &rt->dst);
730e6fbc5bSPravin B Shelar 	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
740e6fbc5bSPravin B Shelar 
750e6fbc5bSPravin B Shelar 	/* Push down and install the IP header. */
7678a3694dSSteffen Klassert 	skb_push(skb, sizeof(struct iphdr));
770e6fbc5bSPravin B Shelar 	skb_reset_network_header(skb);
780e6fbc5bSPravin B Shelar 
790e6fbc5bSPravin B Shelar 	iph = ip_hdr(skb);
800e6fbc5bSPravin B Shelar 
810e6fbc5bSPravin B Shelar 	iph->version	=	4;
820e6fbc5bSPravin B Shelar 	iph->ihl	=	sizeof(struct iphdr) >> 2;
8316f7eb2bSSabrina Dubroca 	iph->frag_off	=	ip_mtu_locked(&rt->dst) ? 0 : df;
840e6fbc5bSPravin B Shelar 	iph->protocol	=	proto;
850e6fbc5bSPravin B Shelar 	iph->tos	=	tos;
860e6fbc5bSPravin B Shelar 	iph->daddr	=	dst;
870e6fbc5bSPravin B Shelar 	iph->saddr	=	src;
880e6fbc5bSPravin B Shelar 	iph->ttl	=	ttl;
89f859b0f6SEric W. Biederman 	__ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
900e6fbc5bSPravin B Shelar 
9133224b16SEric W. Biederman 	err = ip_local_out(net, sk, skb);
920e6fbc5bSPravin B Shelar 	if (unlikely(net_xmit_eval(err)))
930e6fbc5bSPravin B Shelar 		pkt_len = 0;
94039f5062SPravin B Shelar 	iptunnel_xmit_stats(dev, pkt_len);
950e6fbc5bSPravin B Shelar }
960e6fbc5bSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_xmit);
973d7b46cdSPravin B Shelar 
98a6d5bbf3SJiri Benc int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
99a6d5bbf3SJiri Benc 			   __be16 inner_proto, bool raw_proto, bool xnet)
1003d7b46cdSPravin B Shelar {
1013d7b46cdSPravin B Shelar 	if (unlikely(!pskb_may_pull(skb, hdr_len)))
1023d7b46cdSPravin B Shelar 		return -ENOMEM;
1033d7b46cdSPravin B Shelar 
1043d7b46cdSPravin B Shelar 	skb_pull_rcsum(skb, hdr_len);
1053d7b46cdSPravin B Shelar 
106a6d5bbf3SJiri Benc 	if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
1071245dfc8SLi RongQing 		struct ethhdr *eh;
1083d7b46cdSPravin B Shelar 
1093d7b46cdSPravin B Shelar 		if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1103d7b46cdSPravin B Shelar 			return -ENOMEM;
1113d7b46cdSPravin B Shelar 
1121245dfc8SLi RongQing 		eh = (struct ethhdr *)skb->data;
113d181ddcaSAlexander Duyck 		if (likely(eth_proto_is_802_3(eh->h_proto)))
1143d7b46cdSPravin B Shelar 			skb->protocol = eh->h_proto;
1153d7b46cdSPravin B Shelar 		else
1163d7b46cdSPravin B Shelar 			skb->protocol = htons(ETH_P_802_2);
1173d7b46cdSPravin B Shelar 
1183d7b46cdSPravin B Shelar 	} else {
1193d7b46cdSPravin B Shelar 		skb->protocol = inner_proto;
1203d7b46cdSPravin B Shelar 	}
1213d7b46cdSPravin B Shelar 
1227539fadcSTom Herbert 	skb_clear_hash_if_not_l4(skb);
1233e2ed0c2SMichał Mirosław 	__vlan_hwaccel_clear_tag(skb);
1243d7b46cdSPravin B Shelar 	skb_set_queue_mapping(skb, 0);
1257f290c94SJiri Benc 	skb_scrub_packet(skb, xnet);
126a09a4c8dSJesse Gross 
127a09a4c8dSJesse Gross 	return iptunnel_pull_offloads(skb);
1283d7b46cdSPravin B Shelar }
129a6d5bbf3SJiri Benc EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
1302d26f0a3SEric Dumazet 
13163d008a4SJiri Benc struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
13263d008a4SJiri Benc 					     gfp_t flags)
13363d008a4SJiri Benc {
13463d008a4SJiri Benc 	struct metadata_dst *res;
13563d008a4SJiri Benc 	struct ip_tunnel_info *dst, *src;
13663d008a4SJiri Benc 
1373fcece12SJakub Kicinski 	if (!md || md->type != METADATA_IP_TUNNEL ||
1383fcece12SJakub Kicinski 	    md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
1393fcece12SJakub Kicinski 
14063d008a4SJiri Benc 		return NULL;
14163d008a4SJiri Benc 
1423fcece12SJakub Kicinski 	res = metadata_dst_alloc(0, METADATA_IP_TUNNEL, flags);
14363d008a4SJiri Benc 	if (!res)
14463d008a4SJiri Benc 		return NULL;
14563d008a4SJiri Benc 
14663d008a4SJiri Benc 	dst = &res->u.tun_info;
14763d008a4SJiri Benc 	src = &md->u.tun_info;
14863d008a4SJiri Benc 	dst->key.tun_id = src->key.tun_id;
14963d008a4SJiri Benc 	if (src->mode & IP_TUNNEL_INFO_IPV6)
15063d008a4SJiri Benc 		memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
15163d008a4SJiri Benc 		       sizeof(struct in6_addr));
15263d008a4SJiri Benc 	else
15363d008a4SJiri Benc 		dst->key.u.ipv4.dst = src->key.u.ipv4.src;
1547bdca378Swenxu 	dst->key.tun_flags = src->key.tun_flags;
15563d008a4SJiri Benc 	dst->mode = src->mode | IP_TUNNEL_INFO_TX;
15663d008a4SJiri Benc 
15763d008a4SJiri Benc 	return res;
15863d008a4SJiri Benc }
15963d008a4SJiri Benc EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
16063d008a4SJiri Benc 
161aed069dfSAlexander Duyck int iptunnel_handle_offloads(struct sk_buff *skb,
1622d26f0a3SEric Dumazet 			     int gso_type_mask)
1632d26f0a3SEric Dumazet {
1642d26f0a3SEric Dumazet 	int err;
1652d26f0a3SEric Dumazet 
1662d26f0a3SEric Dumazet 	if (likely(!skb->encapsulation)) {
1672d26f0a3SEric Dumazet 		skb_reset_inner_headers(skb);
1682d26f0a3SEric Dumazet 		skb->encapsulation = 1;
1692d26f0a3SEric Dumazet 	}
1702d26f0a3SEric Dumazet 
1712d26f0a3SEric Dumazet 	if (skb_is_gso(skb)) {
1729580bf2eSEric Dumazet 		err = skb_header_unclone(skb, GFP_ATOMIC);
1732d26f0a3SEric Dumazet 		if (unlikely(err))
174aed069dfSAlexander Duyck 			return err;
1752d26f0a3SEric Dumazet 		skb_shinfo(skb)->gso_type |= gso_type_mask;
176aed069dfSAlexander Duyck 		return 0;
1772d26f0a3SEric Dumazet 	}
1782d26f0a3SEric Dumazet 
1796fa79666SEdward Cree 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
1802d26f0a3SEric Dumazet 		skb->ip_summed = CHECKSUM_NONE;
1816fa79666SEdward Cree 		/* We clear encapsulation here to prevent badly-written
1826fa79666SEdward Cree 		 * drivers potentially deciding to offload an inner checksum
1836fa79666SEdward Cree 		 * if we set CHECKSUM_PARTIAL on the outer header.
1846fa79666SEdward Cree 		 * This should go away when the drivers are all fixed.
1856fa79666SEdward Cree 		 */
186179bc67fSEdward Cree 		skb->encapsulation = 0;
187179bc67fSEdward Cree 	}
1882d26f0a3SEric Dumazet 
189aed069dfSAlexander Duyck 	return 0;
1902d26f0a3SEric Dumazet }
1912d26f0a3SEric Dumazet EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
192ebe44f35SDavid S. Miller 
193ebe44f35SDavid S. Miller /* Often modified stats are per cpu, other are shared (netdev->stats) */
194bc1f4470Sstephen hemminger void ip_tunnel_get_stats64(struct net_device *dev,
195ebe44f35SDavid S. Miller 			   struct rtnl_link_stats64 *tot)
196ebe44f35SDavid S. Miller {
197ebe44f35SDavid S. Miller 	int i;
198ebe44f35SDavid S. Miller 
199c24a5964SAlexander Duyck 	netdev_stats_to_stats64(tot, &dev->stats);
200c24a5964SAlexander Duyck 
201ebe44f35SDavid S. Miller 	for_each_possible_cpu(i) {
202ebe44f35SDavid S. Miller 		const struct pcpu_sw_netstats *tstats =
203ebe44f35SDavid S. Miller 						   per_cpu_ptr(dev->tstats, i);
204ebe44f35SDavid S. Miller 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
205ebe44f35SDavid S. Miller 		unsigned int start;
206ebe44f35SDavid S. Miller 
207ebe44f35SDavid S. Miller 		do {
20857a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
209ebe44f35SDavid S. Miller 			rx_packets = tstats->rx_packets;
210ebe44f35SDavid S. Miller 			tx_packets = tstats->tx_packets;
211ebe44f35SDavid S. Miller 			rx_bytes = tstats->rx_bytes;
212ebe44f35SDavid S. Miller 			tx_bytes = tstats->tx_bytes;
21357a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
214ebe44f35SDavid S. Miller 
215ebe44f35SDavid S. Miller 		tot->rx_packets += rx_packets;
216ebe44f35SDavid S. Miller 		tot->tx_packets += tx_packets;
217ebe44f35SDavid S. Miller 		tot->rx_bytes   += rx_bytes;
218ebe44f35SDavid S. Miller 		tot->tx_bytes   += tx_bytes;
219ebe44f35SDavid S. Miller 	}
220ebe44f35SDavid S. Miller }
221ebe44f35SDavid S. Miller EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
2223093fbe7SThomas Graf 
223a1c234f9SJiri Benc static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
224a1c234f9SJiri Benc 	[LWTUNNEL_IP_ID]	= { .type = NLA_U64 },
225a1c234f9SJiri Benc 	[LWTUNNEL_IP_DST]	= { .type = NLA_U32 },
226a1c234f9SJiri Benc 	[LWTUNNEL_IP_SRC]	= { .type = NLA_U32 },
227a1c234f9SJiri Benc 	[LWTUNNEL_IP_TTL]	= { .type = NLA_U8 },
228a1c234f9SJiri Benc 	[LWTUNNEL_IP_TOS]	= { .type = NLA_U8 },
229a1c234f9SJiri Benc 	[LWTUNNEL_IP_FLAGS]	= { .type = NLA_U16 },
2303093fbe7SThomas Graf };
2313093fbe7SThomas Graf 
23230357d7dSDavid Ahern static int ip_tun_build_state(struct nlattr *attr,
233127eb7cdSTom Herbert 			      unsigned int family, const void *cfg,
2349ae28727SDavid Ahern 			      struct lwtunnel_state **ts,
2359ae28727SDavid Ahern 			      struct netlink_ext_ack *extack)
2363093fbe7SThomas Graf {
2373093fbe7SThomas Graf 	struct ip_tunnel_info *tun_info;
2383093fbe7SThomas Graf 	struct lwtunnel_state *new_state;
239a1c234f9SJiri Benc 	struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
2403093fbe7SThomas Graf 	int err;
2413093fbe7SThomas Graf 
2429ae28727SDavid Ahern 	err = nla_parse_nested(tb, LWTUNNEL_IP_MAX, attr, ip_tun_policy,
2439ae28727SDavid Ahern 			       extack);
2443093fbe7SThomas Graf 	if (err < 0)
2453093fbe7SThomas Graf 		return err;
2463093fbe7SThomas Graf 
2473093fbe7SThomas Graf 	new_state = lwtunnel_state_alloc(sizeof(*tun_info));
2483093fbe7SThomas Graf 	if (!new_state)
2493093fbe7SThomas Graf 		return -ENOMEM;
2503093fbe7SThomas Graf 
2513093fbe7SThomas Graf 	new_state->type = LWTUNNEL_ENCAP_IP;
2523093fbe7SThomas Graf 
2533093fbe7SThomas Graf 	tun_info = lwt_tun_info(new_state);
2543093fbe7SThomas Graf 
2553d25eabbSwenxu #ifdef CONFIG_DST_CACHE
2563d25eabbSwenxu 	err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
2573d25eabbSwenxu 	if (err) {
2583d25eabbSwenxu 		lwtstate_free(new_state);
2593d25eabbSwenxu 		return err;
2603d25eabbSwenxu 	}
2613d25eabbSwenxu #endif
2623d25eabbSwenxu 
263a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_ID])
26430d3d83aSLance Richardson 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
2653093fbe7SThomas Graf 
266a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_DST])
2677822ce73SHaishuang Yan 		tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
2683093fbe7SThomas Graf 
269a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_SRC])
2707822ce73SHaishuang Yan 		tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
2713093fbe7SThomas Graf 
272a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_TTL])
2737c383fb2SJiri Benc 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
2743093fbe7SThomas Graf 
275a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_TOS])
2767c383fb2SJiri Benc 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
2773093fbe7SThomas Graf 
278a1c234f9SJiri Benc 	if (tb[LWTUNNEL_IP_FLAGS])
27930d3d83aSLance Richardson 		tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP_FLAGS]);
2803093fbe7SThomas Graf 
2813093fbe7SThomas Graf 	tun_info->mode = IP_TUNNEL_INFO_TX;
2823093fbe7SThomas Graf 	tun_info->options_len = 0;
2833093fbe7SThomas Graf 
2843093fbe7SThomas Graf 	*ts = new_state;
2853093fbe7SThomas Graf 
2863093fbe7SThomas Graf 	return 0;
2873093fbe7SThomas Graf }
2883093fbe7SThomas Graf 
2893d25eabbSwenxu static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
2903d25eabbSwenxu {
2913d25eabbSwenxu #ifdef CONFIG_DST_CACHE
2923d25eabbSwenxu 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
2933d25eabbSwenxu 
2943d25eabbSwenxu 	dst_cache_destroy(&tun_info->dst_cache);
2953d25eabbSwenxu #endif
2963d25eabbSwenxu }
2973d25eabbSwenxu 
2983093fbe7SThomas Graf static int ip_tun_fill_encap_info(struct sk_buff *skb,
2993093fbe7SThomas Graf 				  struct lwtunnel_state *lwtstate)
3003093fbe7SThomas Graf {
3013093fbe7SThomas Graf 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
3023093fbe7SThomas Graf 
303b46f6dedSNicolas Dichtel 	if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
304b46f6dedSNicolas Dichtel 			 LWTUNNEL_IP_PAD) ||
3057822ce73SHaishuang Yan 	    nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
3067822ce73SHaishuang Yan 	    nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
3077c383fb2SJiri Benc 	    nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
3087c383fb2SJiri Benc 	    nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
30930d3d83aSLance Richardson 	    nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags))
3103093fbe7SThomas Graf 		return -ENOMEM;
3113093fbe7SThomas Graf 
3123093fbe7SThomas Graf 	return 0;
3133093fbe7SThomas Graf }
3143093fbe7SThomas Graf 
3153093fbe7SThomas Graf static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
3163093fbe7SThomas Graf {
317b46f6dedSNicolas Dichtel 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP_ID */
318a1c234f9SJiri Benc 		+ nla_total_size(4)	/* LWTUNNEL_IP_DST */
319a1c234f9SJiri Benc 		+ nla_total_size(4)	/* LWTUNNEL_IP_SRC */
320a1c234f9SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP_TOS */
321a1c234f9SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP_TTL */
322a1c234f9SJiri Benc 		+ nla_total_size(2);	/* LWTUNNEL_IP_FLAGS */
3233093fbe7SThomas Graf }
3243093fbe7SThomas Graf 
3252d798499SJiri Benc static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
3262d798499SJiri Benc {
3272d798499SJiri Benc 	return memcmp(lwt_tun_info(a), lwt_tun_info(b),
3282d798499SJiri Benc 		      sizeof(struct ip_tunnel_info));
3292d798499SJiri Benc }
3302d798499SJiri Benc 
3313093fbe7SThomas Graf static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
3323093fbe7SThomas Graf 	.build_state = ip_tun_build_state,
3333d25eabbSwenxu 	.destroy_state = ip_tun_destroy_state,
3343093fbe7SThomas Graf 	.fill_encap = ip_tun_fill_encap_info,
3353093fbe7SThomas Graf 	.get_encap_size = ip_tun_encap_nlsize,
3362d798499SJiri Benc 	.cmp_encap = ip_tun_cmp_encap,
33788ff7334SRobert Shearman 	.owner = THIS_MODULE,
3383093fbe7SThomas Graf };
3393093fbe7SThomas Graf 
34032a2b002SJiri Benc static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
34132a2b002SJiri Benc 	[LWTUNNEL_IP6_ID]		= { .type = NLA_U64 },
34232a2b002SJiri Benc 	[LWTUNNEL_IP6_DST]		= { .len = sizeof(struct in6_addr) },
34332a2b002SJiri Benc 	[LWTUNNEL_IP6_SRC]		= { .len = sizeof(struct in6_addr) },
34432a2b002SJiri Benc 	[LWTUNNEL_IP6_HOPLIMIT]		= { .type = NLA_U8 },
34532a2b002SJiri Benc 	[LWTUNNEL_IP6_TC]		= { .type = NLA_U8 },
34632a2b002SJiri Benc 	[LWTUNNEL_IP6_FLAGS]		= { .type = NLA_U16 },
34732a2b002SJiri Benc };
34832a2b002SJiri Benc 
34930357d7dSDavid Ahern static int ip6_tun_build_state(struct nlattr *attr,
350127eb7cdSTom Herbert 			       unsigned int family, const void *cfg,
3519ae28727SDavid Ahern 			       struct lwtunnel_state **ts,
3529ae28727SDavid Ahern 			       struct netlink_ext_ack *extack)
35332a2b002SJiri Benc {
35432a2b002SJiri Benc 	struct ip_tunnel_info *tun_info;
35532a2b002SJiri Benc 	struct lwtunnel_state *new_state;
35632a2b002SJiri Benc 	struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
35732a2b002SJiri Benc 	int err;
35832a2b002SJiri Benc 
359fceb6435SJohannes Berg 	err = nla_parse_nested(tb, LWTUNNEL_IP6_MAX, attr, ip6_tun_policy,
3609ae28727SDavid Ahern 			       extack);
36132a2b002SJiri Benc 	if (err < 0)
36232a2b002SJiri Benc 		return err;
36332a2b002SJiri Benc 
36432a2b002SJiri Benc 	new_state = lwtunnel_state_alloc(sizeof(*tun_info));
36532a2b002SJiri Benc 	if (!new_state)
36632a2b002SJiri Benc 		return -ENOMEM;
36732a2b002SJiri Benc 
36832a2b002SJiri Benc 	new_state->type = LWTUNNEL_ENCAP_IP6;
36932a2b002SJiri Benc 
37032a2b002SJiri Benc 	tun_info = lwt_tun_info(new_state);
37132a2b002SJiri Benc 
37232a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_ID])
37330d3d83aSLance Richardson 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
37432a2b002SJiri Benc 
37532a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_DST])
37632a2b002SJiri Benc 		tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
37732a2b002SJiri Benc 
37832a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_SRC])
37932a2b002SJiri Benc 		tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
38032a2b002SJiri Benc 
38132a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_HOPLIMIT])
38232a2b002SJiri Benc 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
38332a2b002SJiri Benc 
38432a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_TC])
38532a2b002SJiri Benc 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
38632a2b002SJiri Benc 
38732a2b002SJiri Benc 	if (tb[LWTUNNEL_IP6_FLAGS])
38830d3d83aSLance Richardson 		tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]);
38932a2b002SJiri Benc 
3907f9562a1SJiri Benc 	tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
39132a2b002SJiri Benc 	tun_info->options_len = 0;
39232a2b002SJiri Benc 
39332a2b002SJiri Benc 	*ts = new_state;
39432a2b002SJiri Benc 
39532a2b002SJiri Benc 	return 0;
39632a2b002SJiri Benc }
39732a2b002SJiri Benc 
39832a2b002SJiri Benc static int ip6_tun_fill_encap_info(struct sk_buff *skb,
39932a2b002SJiri Benc 				   struct lwtunnel_state *lwtstate)
40032a2b002SJiri Benc {
40132a2b002SJiri Benc 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
40232a2b002SJiri Benc 
403b46f6dedSNicolas Dichtel 	if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
404b46f6dedSNicolas Dichtel 			 LWTUNNEL_IP6_PAD) ||
40532a2b002SJiri Benc 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
40632a2b002SJiri Benc 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
407995096a0SQuentin Armitage 	    nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
408995096a0SQuentin Armitage 	    nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
40930d3d83aSLance Richardson 	    nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags))
41032a2b002SJiri Benc 		return -ENOMEM;
41132a2b002SJiri Benc 
41232a2b002SJiri Benc 	return 0;
41332a2b002SJiri Benc }
41432a2b002SJiri Benc 
41532a2b002SJiri Benc static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
41632a2b002SJiri Benc {
417b46f6dedSNicolas Dichtel 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP6_ID */
41832a2b002SJiri Benc 		+ nla_total_size(16)	/* LWTUNNEL_IP6_DST */
41932a2b002SJiri Benc 		+ nla_total_size(16)	/* LWTUNNEL_IP6_SRC */
42032a2b002SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP6_HOPLIMIT */
42132a2b002SJiri Benc 		+ nla_total_size(1)	/* LWTUNNEL_IP6_TC */
42232a2b002SJiri Benc 		+ nla_total_size(2);	/* LWTUNNEL_IP6_FLAGS */
42332a2b002SJiri Benc }
42432a2b002SJiri Benc 
42532a2b002SJiri Benc static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
42632a2b002SJiri Benc 	.build_state = ip6_tun_build_state,
42732a2b002SJiri Benc 	.fill_encap = ip6_tun_fill_encap_info,
42832a2b002SJiri Benc 	.get_encap_size = ip6_tun_encap_nlsize,
42932a2b002SJiri Benc 	.cmp_encap = ip_tun_cmp_encap,
43088ff7334SRobert Shearman 	.owner = THIS_MODULE,
43132a2b002SJiri Benc };
43232a2b002SJiri Benc 
433045a0fa0SThomas Graf void __init ip_tunnel_core_init(void)
4343093fbe7SThomas Graf {
435fca5fdf6SDaniel Borkmann 	/* If you land here, make sure whether increasing ip_tunnel_info's
436fca5fdf6SDaniel Borkmann 	 * options_len is a reasonable choice with its usage in front ends
437fca5fdf6SDaniel Borkmann 	 * (f.e., it's part of flow keys, etc).
438fca5fdf6SDaniel Borkmann 	 */
439fca5fdf6SDaniel Borkmann 	BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
440fca5fdf6SDaniel Borkmann 
4413093fbe7SThomas Graf 	lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
44232a2b002SJiri Benc 	lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
4433093fbe7SThomas Graf }
444e7030878SThomas Graf 
4455263a98fSDavidlohr Bueso DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
446e7030878SThomas Graf EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
447e7030878SThomas Graf 
448e7030878SThomas Graf void ip_tunnel_need_metadata(void)
449e7030878SThomas Graf {
4505263a98fSDavidlohr Bueso 	static_branch_inc(&ip_tunnel_metadata_cnt);
451e7030878SThomas Graf }
452e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
453e7030878SThomas Graf 
454e7030878SThomas Graf void ip_tunnel_unneed_metadata(void)
455e7030878SThomas Graf {
4565263a98fSDavidlohr Bueso 	static_branch_dec(&ip_tunnel_metadata_cnt);
457e7030878SThomas Graf }
458e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
459