xref: /linux/include/net/ip_tunnels.h (revision 4cb47a8644cc9eb8ec81190a50e79e6530d0297f)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c5441932SPravin B Shelar #ifndef __NET_IP_TUNNELS_H
3c5441932SPravin B Shelar #define __NET_IP_TUNNELS_H 1
4c5441932SPravin B Shelar 
5c5441932SPravin B Shelar #include <linux/if_tunnel.h>
6c5441932SPravin B Shelar #include <linux/netdevice.h>
7c5441932SPravin B Shelar #include <linux/skbuff.h>
87f9562a1SJiri Benc #include <linux/socket.h>
9c5441932SPravin B Shelar #include <linux/types.h>
10c5441932SPravin B Shelar #include <linux/u64_stats_sync.h>
11fca5fdf6SDaniel Borkmann #include <linux/bitops.h>
12fca5fdf6SDaniel Borkmann 
13c5441932SPravin B Shelar #include <net/dsfield.h>
14c5441932SPravin B Shelar #include <net/gro_cells.h>
15c5441932SPravin B Shelar #include <net/inet_ecn.h>
1656328486STom Herbert #include <net/netns/generic.h>
17c5441932SPravin B Shelar #include <net/rtnetlink.h>
183093fbe7SThomas Graf #include <net/lwtunnel.h>
19e09acddfSPaolo Abeni #include <net/dst_cache.h>
20c5441932SPravin B Shelar 
21c5441932SPravin B Shelar #if IS_ENABLED(CONFIG_IPV6)
22c5441932SPravin B Shelar #include <net/ipv6.h>
23c5441932SPravin B Shelar #include <net/ip6_fib.h>
24c5441932SPravin B Shelar #include <net/ip6_route.h>
25c5441932SPravin B Shelar #endif
26c5441932SPravin B Shelar 
27c5441932SPravin B Shelar /* Keep error state on tunnel for 30 sec */
28c5441932SPravin B Shelar #define IPTUNNEL_ERR_TIMEO	(30*HZ)
29c5441932SPravin B Shelar 
301d8fff90SThomas Graf /* Used to memset ip_tunnel padding. */
31376534a3SJiri Benc #define IP_TUNNEL_KEY_SIZE	offsetofend(struct ip_tunnel_key, tp_dst)
321d8fff90SThomas Graf 
33c1ea5d67SJiri Benc /* Used to memset ipv4 address padding. */
34c1ea5d67SJiri Benc #define IP_TUNNEL_KEY_IPV4_PAD	offsetofend(struct ip_tunnel_key, u.ipv4.dst)
35c1ea5d67SJiri Benc #define IP_TUNNEL_KEY_IPV4_PAD_LEN				\
36c593642cSPankaj Bharadiya 	(sizeof_field(struct ip_tunnel_key, u) -		\
37c593642cSPankaj Bharadiya 	 sizeof_field(struct ip_tunnel_key, u.ipv4))
38c1ea5d67SJiri Benc 
391d8fff90SThomas Graf struct ip_tunnel_key {
401d8fff90SThomas Graf 	__be64			tun_id;
41c1ea5d67SJiri Benc 	union {
42c1ea5d67SJiri Benc 		struct {
43c1ea5d67SJiri Benc 			__be32	src;
44c1ea5d67SJiri Benc 			__be32	dst;
45c1ea5d67SJiri Benc 		} ipv4;
46c1ea5d67SJiri Benc 		struct {
47c1ea5d67SJiri Benc 			struct in6_addr src;
48c1ea5d67SJiri Benc 			struct in6_addr dst;
49c1ea5d67SJiri Benc 		} ipv6;
50c1ea5d67SJiri Benc 	} u;
511d8fff90SThomas Graf 	__be16			tun_flags;
527c383fb2SJiri Benc 	u8			tos;		/* TOS for IPv4, TC for IPv6 */
537c383fb2SJiri Benc 	u8			ttl;		/* TTL for IPv4, HL for IPv6 */
5413461144SDaniel Borkmann 	__be32			label;		/* Flow Label for IPv6 */
551d8fff90SThomas Graf 	__be16			tp_src;
561d8fff90SThomas Graf 	__be16			tp_dst;
57ac1cf399SJiri Benc };
581d8fff90SThomas Graf 
5946fa062aSJiri Benc /* Flags for ip_tunnel_info mode. */
6046fa062aSJiri Benc #define IP_TUNNEL_INFO_TX	0x01	/* represents tx tunnel parameters */
617f9562a1SJiri Benc #define IP_TUNNEL_INFO_IPV6	0x02	/* key contains IPv6 addresses */
62f35581d6SRoopa Prabhu #define IP_TUNNEL_INFO_BRIDGE	0x04	/* represents a bridged tunnel id */
63ee122c79SThomas Graf 
64fca5fdf6SDaniel Borkmann /* Maximum tunnel options length. */
65fca5fdf6SDaniel Borkmann #define IP_TUNNEL_OPTS_MAX					\
66c593642cSPankaj Bharadiya 	GENMASK((sizeof_field(struct ip_tunnel_info,		\
67fca5fdf6SDaniel Borkmann 			      options_len) * BITS_PER_BYTE) - 1, 0)
68fca5fdf6SDaniel Borkmann 
691d8fff90SThomas Graf struct ip_tunnel_info {
701d8fff90SThomas Graf 	struct ip_tunnel_key	key;
71d71785ffSPaolo Abeni #ifdef CONFIG_DST_CACHE
72d71785ffSPaolo Abeni 	struct dst_cache	dst_cache;
73d71785ffSPaolo Abeni #endif
741d8fff90SThomas Graf 	u8			options_len;
75ee122c79SThomas Graf 	u8			mode;
761d8fff90SThomas Graf };
771d8fff90SThomas Graf 
78c5441932SPravin B Shelar /* 6rd prefix/relay information */
79c5441932SPravin B Shelar #ifdef CONFIG_IPV6_SIT_6RD
80c5441932SPravin B Shelar struct ip_tunnel_6rd_parm {
81c5441932SPravin B Shelar 	struct in6_addr		prefix;
82c5441932SPravin B Shelar 	__be32			relay_prefix;
83c5441932SPravin B Shelar 	u16			prefixlen;
84c5441932SPravin B Shelar 	u16			relay_prefixlen;
85c5441932SPravin B Shelar };
86c5441932SPravin B Shelar #endif
87c5441932SPravin B Shelar 
8856328486STom Herbert struct ip_tunnel_encap {
896b8847c5SJiri Benc 	u16			type;
906b8847c5SJiri Benc 	u16			flags;
9156328486STom Herbert 	__be16			sport;
9256328486STom Herbert 	__be16			dport;
9356328486STom Herbert };
9456328486STom Herbert 
95c5441932SPravin B Shelar struct ip_tunnel_prl_entry {
96c5441932SPravin B Shelar 	struct ip_tunnel_prl_entry __rcu *next;
97c5441932SPravin B Shelar 	__be32				addr;
98c5441932SPravin B Shelar 	u16				flags;
99c5441932SPravin B Shelar 	struct rcu_head			rcu_head;
100c5441932SPravin B Shelar };
101c5441932SPravin B Shelar 
1022e15ea39SPravin B Shelar struct metadata_dst;
1032e15ea39SPravin B Shelar 
104c5441932SPravin B Shelar struct ip_tunnel {
105c5441932SPravin B Shelar 	struct ip_tunnel __rcu	*next;
106c5441932SPravin B Shelar 	struct hlist_node hash_node;
107c5441932SPravin B Shelar 	struct net_device	*dev;
1085e6700b3SNicolas Dichtel 	struct net		*net;	/* netns for packet i/o */
109c5441932SPravin B Shelar 
110c5441932SPravin B Shelar 	unsigned long	err_time;	/* Time when the last ICMP error
111c5441932SPravin B Shelar 					 * arrived */
112f38ba953Sstephen hemminger 	int		err_count;	/* Number of arrived ICMP errors */
113c5441932SPravin B Shelar 
114c5441932SPravin B Shelar 	/* These four fields used only by GRE */
1156b8847c5SJiri Benc 	u32		i_seqno;	/* The last seen seqno	*/
1166b8847c5SJiri Benc 	u32		o_seqno;	/* The last output seqno */
11756328486STom Herbert 	int		tun_hlen;	/* Precalculated header length */
118c5441932SPravin B Shelar 
119f551c91dSWilliam Tu 	/* These four fields used only by ERSPAN */
12084e54fe0SWilliam Tu 	u32		index;		/* ERSPAN type II index */
121f551c91dSWilliam Tu 	u8		erspan_ver;	/* ERSPAN version */
122f551c91dSWilliam Tu 	u8		dir;		/* ERSPAN direction */
123f551c91dSWilliam Tu 	u16		hwid;		/* ERSPAN hardware ID */
12484e54fe0SWilliam Tu 
125e09acddfSPaolo Abeni 	struct dst_cache dst_cache;
1267d442fabSTom Herbert 
127c5441932SPravin B Shelar 	struct ip_tunnel_parm parms;
128c5441932SPravin B Shelar 
129f38ba953Sstephen hemminger 	int		mlink;
13056328486STom Herbert 	int		encap_hlen;	/* Encap header length (FOU,GUE) */
13156328486STom Herbert 	int		hlen;		/* tun_hlen + encap_hlen */
132f38ba953Sstephen hemminger 	struct ip_tunnel_encap encap;
13356328486STom Herbert 
134c5441932SPravin B Shelar 	/* for SIT */
135c5441932SPravin B Shelar #ifdef CONFIG_IPV6_SIT_6RD
136c5441932SPravin B Shelar 	struct ip_tunnel_6rd_parm ip6rd;
137c5441932SPravin B Shelar #endif
138c5441932SPravin B Shelar 	struct ip_tunnel_prl_entry __rcu *prl;	/* potential router list */
139c5441932SPravin B Shelar 	unsigned int		prl_count;	/* # of entries in PRL */
140c7d03a00SAlexey Dobriyan 	unsigned int		ip_tnl_net_id;
141c5441932SPravin B Shelar 	struct gro_cells	gro_cells;
1429830ad4cSCraig Gallek 	__u32			fwmark;
1432e15ea39SPravin B Shelar 	bool			collect_md;
14422a59be8SPhilip Prindeville 	bool			ignore_df;
145c5441932SPravin B Shelar };
146c5441932SPravin B Shelar 
147c5441932SPravin B Shelar struct tnl_ptk_info {
148c5441932SPravin B Shelar 	__be16 flags;
149c5441932SPravin B Shelar 	__be16 proto;
150c5441932SPravin B Shelar 	__be32 key;
151c5441932SPravin B Shelar 	__be32 seq;
1529b8c6d7bSEric Dumazet 	int hdr_len;
153c5441932SPravin B Shelar };
154c5441932SPravin B Shelar 
155c5441932SPravin B Shelar #define PACKET_RCVD	0
156c5441932SPravin B Shelar #define PACKET_REJECT	1
157125372faSJiri Benc #define PACKET_NEXT	2
158c5441932SPravin B Shelar 
1596261d983Sstephen hemminger #define IP_TNL_HASH_BITS   7
160c5441932SPravin B Shelar #define IP_TNL_HASH_SIZE   (1 << IP_TNL_HASH_BITS)
161c5441932SPravin B Shelar 
162c5441932SPravin B Shelar struct ip_tunnel_net {
163c5441932SPravin B Shelar 	struct net_device *fb_tunnel_dev;
16479134e6cSEric Dumazet 	struct rtnl_link_ops *rtnl_link_ops;
1656261d983Sstephen hemminger 	struct hlist_head tunnels[IP_TNL_HASH_SIZE];
1662e15ea39SPravin B Shelar 	struct ip_tunnel __rcu *collect_md_tun;
16779134e6cSEric Dumazet 	int type;
168c5441932SPravin B Shelar };
169c5441932SPravin B Shelar 
1704c222798SPravin B Shelar static inline void ip_tunnel_key_init(struct ip_tunnel_key *key,
1711d8fff90SThomas Graf 				      __be32 saddr, __be32 daddr,
17213461144SDaniel Borkmann 				      u8 tos, u8 ttl, __be32 label,
1731d8fff90SThomas Graf 				      __be16 tp_src, __be16 tp_dst,
1744c222798SPravin B Shelar 				      __be64 tun_id, __be16 tun_flags)
1751d8fff90SThomas Graf {
1764c222798SPravin B Shelar 	key->tun_id = tun_id;
1774c222798SPravin B Shelar 	key->u.ipv4.src = saddr;
1784c222798SPravin B Shelar 	key->u.ipv4.dst = daddr;
1794c222798SPravin B Shelar 	memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD,
180c1ea5d67SJiri Benc 	       0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
1814c222798SPravin B Shelar 	key->tos = tos;
1824c222798SPravin B Shelar 	key->ttl = ttl;
18313461144SDaniel Borkmann 	key->label = label;
1844c222798SPravin B Shelar 	key->tun_flags = tun_flags;
1851d8fff90SThomas Graf 
1861d8fff90SThomas Graf 	/* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
1871d8fff90SThomas Graf 	 * the upper tunnel are used.
1881d8fff90SThomas Graf 	 * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
1891d8fff90SThomas Graf 	 */
1904c222798SPravin B Shelar 	key->tp_src = tp_src;
1914c222798SPravin B Shelar 	key->tp_dst = tp_dst;
1921d8fff90SThomas Graf 
1931d8fff90SThomas Graf 	/* Clear struct padding. */
1944c222798SPravin B Shelar 	if (sizeof(*key) != IP_TUNNEL_KEY_SIZE)
1954c222798SPravin B Shelar 		memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE,
1964c222798SPravin B Shelar 		       0, sizeof(*key) - IP_TUNNEL_KEY_SIZE);
1971d8fff90SThomas Graf }
1981d8fff90SThomas Graf 
199db3c6139SDaniel Borkmann static inline bool
200db3c6139SDaniel Borkmann ip_tunnel_dst_cache_usable(const struct sk_buff *skb,
201db3c6139SDaniel Borkmann 			   const struct ip_tunnel_info *info)
202db3c6139SDaniel Borkmann {
203db3c6139SDaniel Borkmann 	if (skb->mark)
204db3c6139SDaniel Borkmann 		return false;
205db3c6139SDaniel Borkmann 	if (!info)
206db3c6139SDaniel Borkmann 		return true;
207db3c6139SDaniel Borkmann 	if (info->key.tun_flags & TUNNEL_NOCACHE)
208db3c6139SDaniel Borkmann 		return false;
209db3c6139SDaniel Borkmann 
210db3c6139SDaniel Borkmann 	return true;
211db3c6139SDaniel Borkmann }
212db3c6139SDaniel Borkmann 
2137f9562a1SJiri Benc static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info
2147f9562a1SJiri Benc 					       *tun_info)
2157f9562a1SJiri Benc {
2167f9562a1SJiri Benc 	return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
2177f9562a1SJiri Benc }
2187f9562a1SJiri Benc 
219d817f432SAmir Vadai static inline __be64 key32_to_tunnel_id(__be32 key)
220d817f432SAmir Vadai {
221d817f432SAmir Vadai #ifdef __BIG_ENDIAN
222d817f432SAmir Vadai 	return (__force __be64)key;
223d817f432SAmir Vadai #else
224d817f432SAmir Vadai 	return (__force __be64)((__force u64)key << 32);
225d817f432SAmir Vadai #endif
226d817f432SAmir Vadai }
227d817f432SAmir Vadai 
228d817f432SAmir Vadai /* Returns the least-significant 32 bits of a __be64. */
229d817f432SAmir Vadai static inline __be32 tunnel_id_to_key32(__be64 tun_id)
230d817f432SAmir Vadai {
231d817f432SAmir Vadai #ifdef __BIG_ENDIAN
232d817f432SAmir Vadai 	return (__force __be32)tun_id;
233d817f432SAmir Vadai #else
234d817f432SAmir Vadai 	return (__force __be32)((__force u64)tun_id >> 32);
235d817f432SAmir Vadai #endif
236d817f432SAmir Vadai }
237d817f432SAmir Vadai 
2385243b6acSJesse Gross #ifdef CONFIG_INET
2395243b6acSJesse Gross 
240b0066da5SPetr Machata static inline void ip_tunnel_init_flow(struct flowi4 *fl4,
241b0066da5SPetr Machata 				       int proto,
242b0066da5SPetr Machata 				       __be32 daddr, __be32 saddr,
243b0066da5SPetr Machata 				       __be32 key, __u8 tos, int oif,
24424ba1440Swenxu 				       __u32 mark, __u32 tun_inner_hash)
245b0066da5SPetr Machata {
246b0066da5SPetr Machata 	memset(fl4, 0, sizeof(*fl4));
247b0066da5SPetr Machata 	fl4->flowi4_oif = oif;
248b0066da5SPetr Machata 	fl4->daddr = daddr;
249b0066da5SPetr Machata 	fl4->saddr = saddr;
250b0066da5SPetr Machata 	fl4->flowi4_tos = tos;
251b0066da5SPetr Machata 	fl4->flowi4_proto = proto;
252b0066da5SPetr Machata 	fl4->fl4_gre_key = key;
253b0066da5SPetr Machata 	fl4->flowi4_mark = mark;
25424ba1440Swenxu 	fl4->flowi4_multipath_hash = tun_inner_hash;
255b0066da5SPetr Machata }
256b0066da5SPetr Machata 
257c5441932SPravin B Shelar int ip_tunnel_init(struct net_device *dev);
258c5441932SPravin B Shelar void ip_tunnel_uninit(struct net_device *dev);
259c5441932SPravin B Shelar void  ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
2601728d4faSNicolas Dichtel struct net *ip_tunnel_get_link_net(const struct net_device *dev);
2611e99584bSNicolas Dichtel int ip_tunnel_get_iflink(const struct net_device *dev);
262c7d03a00SAlexey Dobriyan int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
263c5441932SPravin B Shelar 		       struct rtnl_link_ops *ops, char *devname);
264c5441932SPravin B Shelar 
26564bc1781SEric Dumazet void ip_tunnel_delete_nets(struct list_head *list_net, unsigned int id,
26664bc1781SEric Dumazet 			   struct rtnl_link_ops *ops);
267c5441932SPravin B Shelar 
268c5441932SPravin B Shelar void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
269bf3d6a8fSNicolas Dichtel 		    const struct iphdr *tnl_params, const u8 protocol);
270cfc7381bSAlexei Starovoitov void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
271c8b34e68Swenxu 		       const u8 proto, int tunnel_hlen);
272607259a6SChristoph Hellwig int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
273607259a6SChristoph Hellwig int ip_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
2747e059158SDavid Wragg int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
275c5441932SPravin B Shelar int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
276c5441932SPravin B Shelar 
277bc1f4470Sstephen hemminger void ip_tunnel_get_stats64(struct net_device *dev,
278c5441932SPravin B Shelar 			   struct rtnl_link_stats64 *tot);
279c5441932SPravin B Shelar struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
280c5441932SPravin B Shelar 				   int link, __be16 flags,
281c5441932SPravin B Shelar 				   __be32 remote, __be32 local,
282c5441932SPravin B Shelar 				   __be32 key);
283c5441932SPravin B Shelar 
284c5441932SPravin B Shelar int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
2852e15ea39SPravin B Shelar 		  const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
2862e15ea39SPravin B Shelar 		  bool log_ecn_error);
287c5441932SPravin B Shelar int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
2889830ad4cSCraig Gallek 			 struct ip_tunnel_parm *p, __u32 fwmark);
289c5441932SPravin B Shelar int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
2909830ad4cSCraig Gallek 		      struct ip_tunnel_parm *p, __u32 fwmark);
291c7d03a00SAlexey Dobriyan void ip_tunnel_setup(struct net_device *dev, unsigned int net_id);
29255c2bc14STom Herbert 
2932606aff9SJason A. Donenfeld extern const struct header_ops ip_tunnel_header_ops;
2942606aff9SJason A. Donenfeld __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb);
2952606aff9SJason A. Donenfeld 
29655c2bc14STom Herbert struct ip_tunnel_encap_ops {
29755c2bc14STom Herbert 	size_t (*encap_hlen)(struct ip_tunnel_encap *e);
29855c2bc14STom Herbert 	int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
29955c2bc14STom Herbert 			    u8 *protocol, struct flowi4 *fl4);
300e7cc0824SStefano Brivio 	int (*err_handler)(struct sk_buff *skb, u32 info);
30155c2bc14STom Herbert };
30255c2bc14STom Herbert 
30355c2bc14STom Herbert #define MAX_IPTUN_ENCAP_OPS 8
30455c2bc14STom Herbert 
30555c2bc14STom Herbert extern const struct ip_tunnel_encap_ops __rcu *
30655c2bc14STom Herbert 		iptun_encaps[MAX_IPTUN_ENCAP_OPS];
30755c2bc14STom Herbert 
30855c2bc14STom Herbert int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
30955c2bc14STom Herbert 			    unsigned int num);
31055c2bc14STom Herbert int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
31155c2bc14STom Herbert 			    unsigned int num);
31255c2bc14STom Herbert 
31356328486STom Herbert int ip_tunnel_encap_setup(struct ip_tunnel *t,
31456328486STom Herbert 			  struct ip_tunnel_encap *ipencap);
315c5441932SPravin B Shelar 
316cb9f1b78SWillem de Bruijn static inline bool pskb_inet_may_pull(struct sk_buff *skb)
317cb9f1b78SWillem de Bruijn {
318cb9f1b78SWillem de Bruijn 	int nhlen;
319cb9f1b78SWillem de Bruijn 
320cb9f1b78SWillem de Bruijn 	switch (skb->protocol) {
321cb9f1b78SWillem de Bruijn #if IS_ENABLED(CONFIG_IPV6)
322cb9f1b78SWillem de Bruijn 	case htons(ETH_P_IPV6):
323cb9f1b78SWillem de Bruijn 		nhlen = sizeof(struct ipv6hdr);
324cb9f1b78SWillem de Bruijn 		break;
325cb9f1b78SWillem de Bruijn #endif
326cb9f1b78SWillem de Bruijn 	case htons(ETH_P_IP):
327cb9f1b78SWillem de Bruijn 		nhlen = sizeof(struct iphdr);
328cb9f1b78SWillem de Bruijn 		break;
329cb9f1b78SWillem de Bruijn 	default:
330cb9f1b78SWillem de Bruijn 		nhlen = 0;
331cb9f1b78SWillem de Bruijn 	}
332cb9f1b78SWillem de Bruijn 
333cb9f1b78SWillem de Bruijn 	return pskb_network_may_pull(skb, nhlen);
334cb9f1b78SWillem de Bruijn }
335cb9f1b78SWillem de Bruijn 
33655c2bc14STom Herbert static inline int ip_encap_hlen(struct ip_tunnel_encap *e)
33755c2bc14STom Herbert {
33855c2bc14STom Herbert 	const struct ip_tunnel_encap_ops *ops;
33955c2bc14STom Herbert 	int hlen = -EINVAL;
34055c2bc14STom Herbert 
34155c2bc14STom Herbert 	if (e->type == TUNNEL_ENCAP_NONE)
34255c2bc14STom Herbert 		return 0;
34355c2bc14STom Herbert 
34455c2bc14STom Herbert 	if (e->type >= MAX_IPTUN_ENCAP_OPS)
34555c2bc14STom Herbert 		return -EINVAL;
34655c2bc14STom Herbert 
34755c2bc14STom Herbert 	rcu_read_lock();
34855c2bc14STom Herbert 	ops = rcu_dereference(iptun_encaps[e->type]);
34955c2bc14STom Herbert 	if (likely(ops && ops->encap_hlen))
35055c2bc14STom Herbert 		hlen = ops->encap_hlen(e);
35155c2bc14STom Herbert 	rcu_read_unlock();
35255c2bc14STom Herbert 
35355c2bc14STom Herbert 	return hlen;
35455c2bc14STom Herbert }
35555c2bc14STom Herbert 
35655c2bc14STom Herbert static inline int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
35755c2bc14STom Herbert 				  u8 *protocol, struct flowi4 *fl4)
35855c2bc14STom Herbert {
35955c2bc14STom Herbert 	const struct ip_tunnel_encap_ops *ops;
36055c2bc14STom Herbert 	int ret = -EINVAL;
36155c2bc14STom Herbert 
36255c2bc14STom Herbert 	if (t->encap.type == TUNNEL_ENCAP_NONE)
36355c2bc14STom Herbert 		return 0;
36455c2bc14STom Herbert 
36555c2bc14STom Herbert 	if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
36655c2bc14STom Herbert 		return -EINVAL;
36755c2bc14STom Herbert 
36855c2bc14STom Herbert 	rcu_read_lock();
36955c2bc14STom Herbert 	ops = rcu_dereference(iptun_encaps[t->encap.type]);
37055c2bc14STom Herbert 	if (likely(ops && ops->build_header))
37155c2bc14STom Herbert 		ret = ops->build_header(skb, &t->encap, protocol, fl4);
37255c2bc14STom Herbert 	rcu_read_unlock();
37355c2bc14STom Herbert 
37455c2bc14STom Herbert 	return ret;
37555c2bc14STom Herbert }
37655c2bc14STom Herbert 
377c5441932SPravin B Shelar /* Extract dsfield from inner protocol */
378c5441932SPravin B Shelar static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
379c5441932SPravin B Shelar 				       const struct sk_buff *skb)
380c5441932SPravin B Shelar {
381c5441932SPravin B Shelar 	if (skb->protocol == htons(ETH_P_IP))
382c5441932SPravin B Shelar 		return iph->tos;
383c5441932SPravin B Shelar 	else if (skb->protocol == htons(ETH_P_IPV6))
384c5441932SPravin B Shelar 		return ipv6_get_dsfield((const struct ipv6hdr *)iph);
385c5441932SPravin B Shelar 	else
386c5441932SPravin B Shelar 		return 0;
387c5441932SPravin B Shelar }
388c5441932SPravin B Shelar 
38972f6d71eSHangbin Liu static inline u8 ip_tunnel_get_ttl(const struct iphdr *iph,
39072f6d71eSHangbin Liu 				       const struct sk_buff *skb)
39172f6d71eSHangbin Liu {
39272f6d71eSHangbin Liu 	if (skb->protocol == htons(ETH_P_IP))
39372f6d71eSHangbin Liu 		return iph->ttl;
39472f6d71eSHangbin Liu 	else if (skb->protocol == htons(ETH_P_IPV6))
39572f6d71eSHangbin Liu 		return ((const struct ipv6hdr *)iph)->hop_limit;
39672f6d71eSHangbin Liu 	else
39772f6d71eSHangbin Liu 		return 0;
39872f6d71eSHangbin Liu }
39972f6d71eSHangbin Liu 
400c5441932SPravin B Shelar /* Propogate ECN bits out */
401c5441932SPravin B Shelar static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
402c5441932SPravin B Shelar 				     const struct sk_buff *skb)
403c5441932SPravin B Shelar {
404c5441932SPravin B Shelar 	u8 inner = ip_tunnel_get_dsfield(iph, skb);
405c5441932SPravin B Shelar 
406c5441932SPravin B Shelar 	return INET_ECN_encapsulate(tos, inner);
407c5441932SPravin B Shelar }
408c5441932SPravin B Shelar 
409a6d5bbf3SJiri Benc int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
410a6d5bbf3SJiri Benc 			   __be16 inner_proto, bool raw_proto, bool xnet);
411a6d5bbf3SJiri Benc 
412a6d5bbf3SJiri Benc static inline int iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
413a6d5bbf3SJiri Benc 				       __be16 inner_proto, bool xnet)
414a6d5bbf3SJiri Benc {
415a6d5bbf3SJiri Benc 	return __iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet);
416a6d5bbf3SJiri Benc }
417a6d5bbf3SJiri Benc 
418039f5062SPravin B Shelar void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
4196b8847c5SJiri Benc 		   __be32 src, __be32 dst, u8 proto,
4206b8847c5SJiri Benc 		   u8 tos, u8 ttl, __be16 df, bool xnet);
42163d008a4SJiri Benc struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
42263d008a4SJiri Benc 					     gfp_t flags);
423*4cb47a86SStefano Brivio int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
424*4cb47a86SStefano Brivio 			  int headroom, bool reply);
4250e6fbc5bSPravin B Shelar 
426aed069dfSAlexander Duyck int iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask);
4272d26f0a3SEric Dumazet 
428a09a4c8dSJesse Gross static inline int iptunnel_pull_offloads(struct sk_buff *skb)
429a09a4c8dSJesse Gross {
430a09a4c8dSJesse Gross 	if (skb_is_gso(skb)) {
431a09a4c8dSJesse Gross 		int err;
432a09a4c8dSJesse Gross 
433a09a4c8dSJesse Gross 		err = skb_unclone(skb, GFP_ATOMIC);
434a09a4c8dSJesse Gross 		if (unlikely(err))
435a09a4c8dSJesse Gross 			return err;
436a09a4c8dSJesse Gross 		skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
437a09a4c8dSJesse Gross 					       NETIF_F_GSO_SHIFT);
438a09a4c8dSJesse Gross 	}
439a09a4c8dSJesse Gross 
440a09a4c8dSJesse Gross 	skb->encapsulation = 0;
441a09a4c8dSJesse Gross 	return 0;
442a09a4c8dSJesse Gross }
443a09a4c8dSJesse Gross 
444039f5062SPravin B Shelar static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
445c5441932SPravin B Shelar {
446039f5062SPravin B Shelar 	if (pkt_len > 0) {
447039f5062SPravin B Shelar 		struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
448c5441932SPravin B Shelar 
449c5441932SPravin B Shelar 		u64_stats_update_begin(&tstats->syncp);
450039f5062SPravin B Shelar 		tstats->tx_bytes += pkt_len;
451c5441932SPravin B Shelar 		tstats->tx_packets++;
452c5441932SPravin B Shelar 		u64_stats_update_end(&tstats->syncp);
453b4fe85f9SJason A. Donenfeld 		put_cpu_ptr(tstats);
454039f5062SPravin B Shelar 	} else {
455039f5062SPravin B Shelar 		struct net_device_stats *err_stats = &dev->stats;
456039f5062SPravin B Shelar 
457039f5062SPravin B Shelar 		if (pkt_len < 0) {
4580e6fbc5bSPravin B Shelar 			err_stats->tx_errors++;
4590e6fbc5bSPravin B Shelar 			err_stats->tx_aborted_errors++;
460c5441932SPravin B Shelar 		} else {
4610e6fbc5bSPravin B Shelar 			err_stats->tx_dropped++;
462c5441932SPravin B Shelar 		}
463c5441932SPravin B Shelar 	}
464039f5062SPravin B Shelar }
4655243b6acSJesse Gross 
4664c222798SPravin B Shelar static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
467ee122c79SThomas Graf {
468ee122c79SThomas Graf 	return info + 1;
469ee122c79SThomas Graf }
470ee122c79SThomas Graf 
4714c222798SPravin B Shelar static inline void ip_tunnel_info_opts_get(void *to,
4724c222798SPravin B Shelar 					   const struct ip_tunnel_info *info)
4734c222798SPravin B Shelar {
4744c222798SPravin B Shelar 	memcpy(to, info + 1, info->options_len);
4754c222798SPravin B Shelar }
4764c222798SPravin B Shelar 
4774c222798SPravin B Shelar static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
478256c87c1SPieter Jansen van Vuuren 					   const void *from, int len,
479256c87c1SPieter Jansen van Vuuren 					   __be16 flags)
4804c222798SPravin B Shelar {
4814c222798SPravin B Shelar 	memcpy(ip_tunnel_info_opts(info), from, len);
4824c222798SPravin B Shelar 	info->options_len = len;
483256c87c1SPieter Jansen van Vuuren 	info->key.tun_flags |= flags;
4844c222798SPravin B Shelar }
4854c222798SPravin B Shelar 
4863093fbe7SThomas Graf static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
4873093fbe7SThomas Graf {
4883093fbe7SThomas Graf 	return (struct ip_tunnel_info *)lwtstate->data;
4893093fbe7SThomas Graf }
4903093fbe7SThomas Graf 
4915263a98fSDavidlohr Bueso DECLARE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
492e7030878SThomas Graf 
493e7030878SThomas Graf /* Returns > 0 if metadata should be collected */
494e7030878SThomas Graf static inline int ip_tunnel_collect_metadata(void)
495e7030878SThomas Graf {
4965263a98fSDavidlohr Bueso 	return static_branch_unlikely(&ip_tunnel_metadata_cnt);
497e7030878SThomas Graf }
498e7030878SThomas Graf 
499045a0fa0SThomas Graf void __init ip_tunnel_core_init(void);
500045a0fa0SThomas Graf 
501e7030878SThomas Graf void ip_tunnel_need_metadata(void);
502e7030878SThomas Graf void ip_tunnel_unneed_metadata(void);
503e7030878SThomas Graf 
50405283187SThomas Graf #else /* CONFIG_INET */
50505283187SThomas Graf 
50605283187SThomas Graf static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
50705283187SThomas Graf {
50805283187SThomas Graf 	return NULL;
50905283187SThomas Graf }
51005283187SThomas Graf 
51105283187SThomas Graf static inline void ip_tunnel_need_metadata(void)
51205283187SThomas Graf {
51305283187SThomas Graf }
51405283187SThomas Graf 
51505283187SThomas Graf static inline void ip_tunnel_unneed_metadata(void)
51605283187SThomas Graf {
51705283187SThomas Graf }
51805283187SThomas Graf 
519e28e87edSDaniel Borkmann static inline void ip_tunnel_info_opts_get(void *to,
520e28e87edSDaniel Borkmann 					   const struct ip_tunnel_info *info)
521e28e87edSDaniel Borkmann {
522e28e87edSDaniel Borkmann }
523e28e87edSDaniel Borkmann 
524e28e87edSDaniel Borkmann static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
525256c87c1SPieter Jansen van Vuuren 					   const void *from, int len,
526256c87c1SPieter Jansen van Vuuren 					   __be16 flags)
527e28e87edSDaniel Borkmann {
528e28e87edSDaniel Borkmann 	info->options_len = 0;
529256c87c1SPieter Jansen van Vuuren 	info->key.tun_flags |= flags;
530e28e87edSDaniel Borkmann }
531e28e87edSDaniel Borkmann 
5325243b6acSJesse Gross #endif /* CONFIG_INET */
5335243b6acSJesse Gross 
534c5441932SPravin B Shelar #endif /* __NET_IP_TUNNELS_H */
535