ip_tunnels.h 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#ifndef __NET_IP_TUNNELS_H
#define __NET_IP_TUNNELS_H 1

#include <linux/if_tunnel.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/types.h>
#include <linux/u64_stats_sync.h>
#include <net/dsfield.h>
#include <net/gro_cells.h>
#include <net/inet_ecn.h>
12
#include <net/netns/generic.h>
13
#include <net/rtnetlink.h>
14
#include <net/lwtunnel.h>
15 16 17 18 19 20 21 22 23 24

#if IS_ENABLED(CONFIG_IPV6)
#include <net/ipv6.h>
#include <net/ip6_fib.h>
#include <net/ip6_route.h>
#endif

/* Keep error state on tunnel for 30 sec */
#define IPTUNNEL_ERR_TIMEO	(30*HZ)

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* Used to memset ip_tunnel padding. */
#define IP_TUNNEL_KEY_SIZE					\
	(offsetof(struct ip_tunnel_key, tp_dst) +		\
	 FIELD_SIZEOF(struct ip_tunnel_key, tp_dst))

struct ip_tunnel_key {
	__be64			tun_id;
	__be32			ipv4_src;
	__be32			ipv4_dst;
	__be16			tun_flags;
	__u8			ipv4_tos;
	__u8			ipv4_ttl;
	__be16			tp_src;
	__be16			tp_dst;
} __packed __aligned(4); /* Minimize padding. */

T
Thomas Graf 已提交
41 42 43 44 45 46 47 48
/* Indicates whether the tunnel info structure represents receive
 * or transmit tunnel parameters.
 */
enum {
	IP_TUNNEL_INFO_RX,
	IP_TUNNEL_INFO_TX,
};

49 50 51 52
struct ip_tunnel_info {
	struct ip_tunnel_key	key;
	const void		*options;
	u8			options_len;
T
Thomas Graf 已提交
53
	u8			mode;
54 55
};

56 57 58 59 60 61 62 63 64 65
/* 6rd prefix/relay information */
#ifdef CONFIG_IPV6_SIT_6RD
struct ip_tunnel_6rd_parm {
	struct in6_addr		prefix;
	__be32			relay_prefix;
	u16			prefixlen;
	u16			relay_prefixlen;
};
#endif

66 67 68 69 70 71 72
struct ip_tunnel_encap {
	__u16			type;
	__u16			flags;
	__be16			sport;
	__be16			dport;
};

73 74 75 76 77 78 79
struct ip_tunnel_prl_entry {
	struct ip_tunnel_prl_entry __rcu *next;
	__be32				addr;
	u16				flags;
	struct rcu_head			rcu_head;
};

80 81
struct ip_tunnel_dst {
	struct dst_entry __rcu 		*dst;
82
	__be32				 saddr;
83 84
};

85 86
struct metadata_dst;

87 88 89 90
struct ip_tunnel {
	struct ip_tunnel __rcu	*next;
	struct hlist_node hash_node;
	struct net_device	*dev;
N
Nicolas Dichtel 已提交
91
	struct net		*net;	/* netns for packet i/o */
92 93 94 95 96 97 98 99

	int		err_count;	/* Number of arrived ICMP errors */
	unsigned long	err_time;	/* Time when the last ICMP error
					 * arrived */

	/* These four fields used only by GRE */
	__u32		i_seqno;	/* The last seen seqno	*/
	__u32		o_seqno;	/* The last output seqno */
100
	int		tun_hlen;	/* Precalculated header length */
101 102
	int		mlink;

103
	struct ip_tunnel_dst __percpu *dst_cache;
T
Tom Herbert 已提交
104

105 106
	struct ip_tunnel_parm parms;

107 108 109 110 111
	int		encap_hlen;	/* Encap header length (FOU,GUE) */
	struct ip_tunnel_encap encap;

	int		hlen;		/* tun_hlen + encap_hlen */

112 113 114 115 116 117 118 119
	/* for SIT */
#ifdef CONFIG_IPV6_SIT_6RD
	struct ip_tunnel_6rd_parm ip6rd;
#endif
	struct ip_tunnel_prl_entry __rcu *prl;	/* potential router list */
	unsigned int		prl_count;	/* # of entries in PRL */
	int			ip_tnl_net_id;
	struct gro_cells	gro_cells;
120
	bool			collect_md;
121 122
};

123 124 125 126 127 128 129 130
#define TUNNEL_CSUM		__cpu_to_be16(0x01)
#define TUNNEL_ROUTING		__cpu_to_be16(0x02)
#define TUNNEL_KEY		__cpu_to_be16(0x04)
#define TUNNEL_SEQ		__cpu_to_be16(0x08)
#define TUNNEL_STRICT		__cpu_to_be16(0x10)
#define TUNNEL_REC		__cpu_to_be16(0x20)
#define TUNNEL_VERSION		__cpu_to_be16(0x40)
#define TUNNEL_NO_KEY		__cpu_to_be16(0x80)
131
#define TUNNEL_DONT_FRAGMENT    __cpu_to_be16(0x0100)
132 133
#define TUNNEL_OAM		__cpu_to_be16(0x0200)
#define TUNNEL_CRIT_OPT		__cpu_to_be16(0x0400)
134 135 136 137
#define TUNNEL_GENEVE_OPT	__cpu_to_be16(0x0800)
#define TUNNEL_VXLAN_OPT	__cpu_to_be16(0x1000)

#define TUNNEL_OPTIONS_PRESENT	(TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT)
138 139 140 141 142 143 144 145 146 147 148

struct tnl_ptk_info {
	__be16 flags;
	__be16 proto;
	__be32 key;
	__be32 seq;
};

#define PACKET_RCVD	0
#define PACKET_REJECT	1

149
#define IP_TNL_HASH_BITS   7
150 151 152 153
#define IP_TNL_HASH_SIZE   (1 << IP_TNL_HASH_BITS)

struct ip_tunnel_net {
	struct net_device *fb_tunnel_dev;
154
	struct hlist_head tunnels[IP_TNL_HASH_SIZE];
155
	struct ip_tunnel __rcu *collect_md_tun;
156 157
};

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
struct ip_tunnel_encap_ops {
	size_t (*encap_hlen)(struct ip_tunnel_encap *e);
	int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
			    u8 *protocol, struct flowi4 *fl4);
};

#define MAX_IPTUN_ENCAP_OPS 8

extern const struct ip_tunnel_encap_ops __rcu *
		iptun_encaps[MAX_IPTUN_ENCAP_OPS];

int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
			    unsigned int num);
int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
			    unsigned int num);

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
static inline void __ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
					 __be32 saddr, __be32 daddr,
					 u8 tos, u8 ttl,
					 __be16 tp_src, __be16 tp_dst,
					 __be64 tun_id, __be16 tun_flags,
					 const void *opts, u8 opts_len)
{
	tun_info->key.tun_id = tun_id;
	tun_info->key.ipv4_src = saddr;
	tun_info->key.ipv4_dst = daddr;
	tun_info->key.ipv4_tos = tos;
	tun_info->key.ipv4_ttl = ttl;
	tun_info->key.tun_flags = tun_flags;

	/* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
	 * the upper tunnel are used.
	 * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
	 */
	tun_info->key.tp_src = tp_src;
	tun_info->key.tp_dst = tp_dst;

	/* Clear struct padding. */
	if (sizeof(tun_info->key) != IP_TUNNEL_KEY_SIZE)
		memset((unsigned char *)&tun_info->key + IP_TUNNEL_KEY_SIZE,
		       0, sizeof(tun_info->key) - IP_TUNNEL_KEY_SIZE);

	tun_info->options = opts;
	tun_info->options_len = opts_len;
}

static inline void ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
				       const struct iphdr *iph,
				       __be16 tp_src, __be16 tp_dst,
				       __be64 tun_id, __be16 tun_flags,
				       const void *opts, u8 opts_len)
{
	__ip_tunnel_info_init(tun_info, iph->saddr, iph->daddr,
			      iph->tos, iph->ttl, tp_src, tp_dst,
			      tun_id, tun_flags, opts, opts_len);
}

215 216
#ifdef CONFIG_INET

217 218 219
int ip_tunnel_init(struct net_device *dev);
void ip_tunnel_uninit(struct net_device *dev);
void  ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
220
struct net *ip_tunnel_get_link_net(const struct net_device *dev);
221
int ip_tunnel_get_iflink(const struct net_device *dev);
222 223
int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
		       struct rtnl_link_ops *ops, char *devname);
224

N
Nicolas Dichtel 已提交
225
void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
226 227

void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
228
		    const struct iphdr *tnl_params, const u8 protocol);
229
int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
230 231
int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
		    u8 *protocol, struct flowi4 *fl4);
232 233 234 235 236 237 238 239 240 241
int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);

struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
						struct rtnl_link_stats64 *tot);
struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
				   int link, __be16 flags,
				   __be32 remote, __be32 local,
				   __be32 key);

int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
242 243
		  const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
		  bool log_ecn_error);
244 245 246 247 248
int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
			 struct ip_tunnel_parm *p);
int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
		      struct ip_tunnel_parm *p);
void ip_tunnel_setup(struct net_device *dev, int net_id);
249
void ip_tunnel_dst_reset_all(struct ip_tunnel *t);
250 251
int ip_tunnel_encap_setup(struct ip_tunnel *t,
			  struct ip_tunnel_encap *ipencap);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

/* Extract dsfield from inner protocol */
static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
				       const struct sk_buff *skb)
{
	if (skb->protocol == htons(ETH_P_IP))
		return iph->tos;
	else if (skb->protocol == htons(ETH_P_IPV6))
		return ipv6_get_dsfield((const struct ipv6hdr *)iph);
	else
		return 0;
}

/* Propogate ECN bits out */
static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
				     const struct sk_buff *skb)
{
	u8 inner = ip_tunnel_get_dsfield(iph, skb);

	return INET_ECN_encapsulate(tos, inner);
}

274
int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
275
int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
276
		  __be32 src, __be32 dst, __u8 proto,
277
		  __u8 tos, __u8 ttl, __be16 df, bool xnet);
278

279 280 281
struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum,
					 int gso_type_mask);

282 283
static inline void iptunnel_xmit_stats(int err,
				       struct net_device_stats *err_stats,
284
				       struct pcpu_sw_netstats __percpu *stats)
285
{
286
	if (err > 0) {
287
		struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats);
288 289

		u64_stats_update_begin(&tstats->syncp);
290
		tstats->tx_bytes += err;
291 292
		tstats->tx_packets++;
		u64_stats_update_end(&tstats->syncp);
293 294 295
	} else if (err < 0) {
		err_stats->tx_errors++;
		err_stats->tx_aborted_errors++;
296
	} else {
297
		err_stats->tx_dropped++;
298 299
	}
}
300

T
Thomas Graf 已提交
301 302 303 304 305
static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info, size_t n)
{
	return info + 1;
}

306 307 308 309 310
static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
{
	return (struct ip_tunnel_info *)lwtstate->data;
}

311 312 313 314 315 316 317 318
extern struct static_key ip_tunnel_metadata_cnt;

/* Returns > 0 if metadata should be collected */
static inline int ip_tunnel_collect_metadata(void)
{
	return static_key_false(&ip_tunnel_metadata_cnt);
}

319 320
void __init ip_tunnel_core_init(void);

321 322 323
void ip_tunnel_need_metadata(void);
void ip_tunnel_unneed_metadata(void);

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
#else /* CONFIG_INET */

static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
{
	return NULL;
}

static inline void ip_tunnel_need_metadata(void)
{
}

static inline void ip_tunnel_unneed_metadata(void)
{
}

339 340
#endif /* CONFIG_INET */

341
#endif /* __NET_IP_TUNNELS_H */