blob: 62a750a6a8f8cb32c9eb5d8d160aa94a20898473 [file] [log] [blame]
Pravin B Shelarc5441932013-03-25 14:49:35 +00001#ifndef __NET_IP_TUNNELS_H
2#define __NET_IP_TUNNELS_H 1
3
4#include <linux/if_tunnel.h>
5#include <linux/netdevice.h>
6#include <linux/skbuff.h>
Jiri Benc7f9562a2015-08-28 20:48:20 +02007#include <linux/socket.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +00008#include <linux/types.h>
9#include <linux/u64_stats_sync.h>
10#include <net/dsfield.h>
11#include <net/gro_cells.h>
12#include <net/inet_ecn.h>
Tom Herbert56328482014-09-17 12:25:58 -070013#include <net/netns/generic.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000014#include <net/rtnetlink.h>
Thomas Graf3093fbe2015-07-21 10:44:00 +020015#include <net/lwtunnel.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000016
17#if IS_ENABLED(CONFIG_IPV6)
18#include <net/ipv6.h>
19#include <net/ip6_fib.h>
20#include <net/ip6_route.h>
21#endif
22
23/* Keep error state on tunnel for 30 sec */
24#define IPTUNNEL_ERR_TIMEO (30*HZ)
25
Thomas Graf1d8fff92015-07-21 10:43:54 +020026/* Used to memset ip_tunnel padding. */
Jiri Benc376534a2015-08-20 13:56:22 +020027#define IP_TUNNEL_KEY_SIZE offsetofend(struct ip_tunnel_key, tp_dst)
Thomas Graf1d8fff92015-07-21 10:43:54 +020028
Jiri Bencc1ea5d62015-08-20 13:56:23 +020029/* Used to memset ipv4 address padding. */
30#define IP_TUNNEL_KEY_IPV4_PAD offsetofend(struct ip_tunnel_key, u.ipv4.dst)
31#define IP_TUNNEL_KEY_IPV4_PAD_LEN \
32 (FIELD_SIZEOF(struct ip_tunnel_key, u) - \
33 FIELD_SIZEOF(struct ip_tunnel_key, u.ipv4))
34
Thomas Graf1d8fff92015-07-21 10:43:54 +020035struct ip_tunnel_key {
36 __be64 tun_id;
Jiri Bencc1ea5d62015-08-20 13:56:23 +020037 union {
38 struct {
39 __be32 src;
40 __be32 dst;
41 } ipv4;
42 struct {
43 struct in6_addr src;
44 struct in6_addr dst;
45 } ipv6;
46 } u;
Thomas Graf1d8fff92015-07-21 10:43:54 +020047 __be16 tun_flags;
Jiri Benc7c383fb2015-08-20 13:56:24 +020048 u8 tos; /* TOS for IPv4, TC for IPv6 */
49 u8 ttl; /* TTL for IPv4, HL for IPv6 */
Thomas Graf1d8fff92015-07-21 10:43:54 +020050 __be16 tp_src;
51 __be16 tp_dst;
Jiri Bencac1cf392015-08-20 13:56:20 +020052};
Thomas Graf1d8fff92015-07-21 10:43:54 +020053
Jiri Benc46fa0622015-08-28 20:48:19 +020054/* Flags for ip_tunnel_info mode. */
55#define IP_TUNNEL_INFO_TX 0x01 /* represents tx tunnel parameters */
Jiri Benc7f9562a2015-08-28 20:48:20 +020056#define IP_TUNNEL_INFO_IPV6 0x02 /* key contains IPv6 addresses */
Thomas Grafee122c72015-07-21 10:43:58 +020057
Thomas Graf1d8fff92015-07-21 10:43:54 +020058struct ip_tunnel_info {
59 struct ip_tunnel_key key;
Thomas Graf1d8fff92015-07-21 10:43:54 +020060 u8 options_len;
Thomas Grafee122c72015-07-21 10:43:58 +020061 u8 mode;
Thomas Graf1d8fff92015-07-21 10:43:54 +020062};
63
Pravin B Shelarc5441932013-03-25 14:49:35 +000064/* 6rd prefix/relay information */
65#ifdef CONFIG_IPV6_SIT_6RD
66struct ip_tunnel_6rd_parm {
67 struct in6_addr prefix;
68 __be32 relay_prefix;
69 u16 prefixlen;
70 u16 relay_prefixlen;
71};
72#endif
73
Tom Herbert56328482014-09-17 12:25:58 -070074struct ip_tunnel_encap {
Jiri Benc6b8847c2015-08-20 13:56:21 +020075 u16 type;
76 u16 flags;
Tom Herbert56328482014-09-17 12:25:58 -070077 __be16 sport;
78 __be16 dport;
79};
80
Pravin B Shelarc5441932013-03-25 14:49:35 +000081struct ip_tunnel_prl_entry {
82 struct ip_tunnel_prl_entry __rcu *next;
83 __be32 addr;
84 u16 flags;
85 struct rcu_head rcu_head;
86};
87
Tom Herbert9a4aa9a2014-01-02 11:48:33 -080088struct ip_tunnel_dst {
89 struct dst_entry __rcu *dst;
Dmitry Popov95cb5742014-07-29 03:07:52 +040090 __be32 saddr;
Tom Herbert9a4aa9a2014-01-02 11:48:33 -080091};
92
Pravin B Shelar2e15ea32015-08-07 23:51:42 -070093struct metadata_dst;
94
Pravin B Shelarc5441932013-03-25 14:49:35 +000095struct ip_tunnel {
96 struct ip_tunnel __rcu *next;
97 struct hlist_node hash_node;
98 struct net_device *dev;
Nicolas Dichtel5e6700b2013-06-26 16:11:28 +020099 struct net *net; /* netns for packet i/o */
Pravin B Shelarc5441932013-03-25 14:49:35 +0000100
101 int err_count; /* Number of arrived ICMP errors */
102 unsigned long err_time; /* Time when the last ICMP error
103 * arrived */
104
105 /* These four fields used only by GRE */
Jiri Benc6b8847c2015-08-20 13:56:21 +0200106 u32 i_seqno; /* The last seen seqno */
107 u32 o_seqno; /* The last output seqno */
Tom Herbert56328482014-09-17 12:25:58 -0700108 int tun_hlen; /* Precalculated header length */
Pravin B Shelarc5441932013-03-25 14:49:35 +0000109 int mlink;
110
Tom Herbert9a4aa9a2014-01-02 11:48:33 -0800111 struct ip_tunnel_dst __percpu *dst_cache;
Tom Herbert7d442fa2014-01-02 11:48:26 -0800112
Pravin B Shelarc5441932013-03-25 14:49:35 +0000113 struct ip_tunnel_parm parms;
114
Tom Herbert56328482014-09-17 12:25:58 -0700115 int encap_hlen; /* Encap header length (FOU,GUE) */
116 struct ip_tunnel_encap encap;
117
118 int hlen; /* tun_hlen + encap_hlen */
119
Pravin B Shelarc5441932013-03-25 14:49:35 +0000120 /* for SIT */
121#ifdef CONFIG_IPV6_SIT_6RD
122 struct ip_tunnel_6rd_parm ip6rd;
123#endif
124 struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */
125 unsigned int prl_count; /* # of entries in PRL */
126 int ip_tnl_net_id;
127 struct gro_cells gro_cells;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700128 bool collect_md;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000129};
130
Jesse Grossf5796682014-10-03 15:35:33 -0700131#define TUNNEL_CSUM __cpu_to_be16(0x01)
132#define TUNNEL_ROUTING __cpu_to_be16(0x02)
133#define TUNNEL_KEY __cpu_to_be16(0x04)
134#define TUNNEL_SEQ __cpu_to_be16(0x08)
135#define TUNNEL_STRICT __cpu_to_be16(0x10)
136#define TUNNEL_REC __cpu_to_be16(0x20)
137#define TUNNEL_VERSION __cpu_to_be16(0x40)
138#define TUNNEL_NO_KEY __cpu_to_be16(0x80)
Pravin B Shelar9a628222013-06-17 17:50:07 -0700139#define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
Jesse Grossf5796682014-10-03 15:35:33 -0700140#define TUNNEL_OAM __cpu_to_be16(0x0200)
141#define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
Thomas Graf1dd144c2015-01-15 03:53:59 +0100142#define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
143#define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
144
145#define TUNNEL_OPTIONS_PRESENT (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT)
Pravin B Shelarc5441932013-03-25 14:49:35 +0000146
147struct tnl_ptk_info {
148 __be16 flags;
149 __be16 proto;
150 __be32 key;
151 __be32 seq;
152};
153
154#define PACKET_RCVD 0
155#define PACKET_REJECT 1
156
stephen hemminger6261d982013-08-05 22:51:37 -0700157#define IP_TNL_HASH_BITS 7
Pravin B Shelarc5441932013-03-25 14:49:35 +0000158#define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
159
160struct ip_tunnel_net {
Pravin B Shelarc5441932013-03-25 14:49:35 +0000161 struct net_device *fb_tunnel_dev;
stephen hemminger6261d982013-08-05 22:51:37 -0700162 struct hlist_head tunnels[IP_TNL_HASH_SIZE];
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700163 struct ip_tunnel __rcu *collect_md_tun;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000164};
165
Tom Herberta8c5f902014-11-12 11:54:09 -0800166struct ip_tunnel_encap_ops {
167 size_t (*encap_hlen)(struct ip_tunnel_encap *e);
168 int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
169 u8 *protocol, struct flowi4 *fl4);
170};
171
172#define MAX_IPTUN_ENCAP_OPS 8
173
174extern const struct ip_tunnel_encap_ops __rcu *
175 iptun_encaps[MAX_IPTUN_ENCAP_OPS];
176
177int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
178 unsigned int num);
179int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
180 unsigned int num);
181
Pravin B Shelar4c222792015-08-30 18:09:38 -0700182static inline void ip_tunnel_key_init(struct ip_tunnel_key *key,
183 __be32 saddr, __be32 daddr,
184 u8 tos, u8 ttl,
185 __be16 tp_src, __be16 tp_dst,
186 __be64 tun_id, __be16 tun_flags)
Thomas Graf1d8fff92015-07-21 10:43:54 +0200187{
Pravin B Shelar4c222792015-08-30 18:09:38 -0700188 key->tun_id = tun_id;
189 key->u.ipv4.src = saddr;
190 key->u.ipv4.dst = daddr;
191 memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200192 0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
Pravin B Shelar4c222792015-08-30 18:09:38 -0700193 key->tos = tos;
194 key->ttl = ttl;
195 key->tun_flags = tun_flags;
Thomas Graf1d8fff92015-07-21 10:43:54 +0200196
197 /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
198 * the upper tunnel are used.
199 * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
200 */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700201 key->tp_src = tp_src;
202 key->tp_dst = tp_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +0200203
204 /* Clear struct padding. */
Pravin B Shelar4c222792015-08-30 18:09:38 -0700205 if (sizeof(*key) != IP_TUNNEL_KEY_SIZE)
206 memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE,
207 0, sizeof(*key) - IP_TUNNEL_KEY_SIZE);
Thomas Graf1d8fff92015-07-21 10:43:54 +0200208}
209
Jiri Benc7f9562a2015-08-28 20:48:20 +0200210static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info
211 *tun_info)
212{
213 return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
214}
215
Jesse Gross5243b6a2013-06-21 16:17:11 -0700216#ifdef CONFIG_INET
217
Pravin B Shelarc5441932013-03-25 14:49:35 +0000218int ip_tunnel_init(struct net_device *dev);
219void ip_tunnel_uninit(struct net_device *dev);
220void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +0100221struct net *ip_tunnel_get_link_net(const struct net_device *dev);
Nicolas Dichtel1e995842015-04-02 17:07:02 +0200222int ip_tunnel_get_iflink(const struct net_device *dev);
Eric Dumazetd3b6f612013-06-07 13:26:05 -0700223int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
224 struct rtnl_link_ops *ops, char *devname);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000225
Nicolas Dichtel6c742e72013-08-13 17:51:11 +0200226void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000227
228void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
Nicolas Dichtelbf3d6a82013-05-27 23:48:15 +0000229 const struct iphdr *tnl_params, const u8 protocol);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000230int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
Tom Herbert56328482014-09-17 12:25:58 -0700231int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
232 u8 *protocol, struct flowi4 *fl4);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000233int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
234
235struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
236 struct rtnl_link_stats64 *tot);
237struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
238 int link, __be16 flags,
239 __be32 remote, __be32 local,
240 __be32 key);
241
242int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700243 const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
244 bool log_ecn_error);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000245int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
246 struct ip_tunnel_parm *p);
247int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
248 struct ip_tunnel_parm *p);
249void ip_tunnel_setup(struct net_device *dev, int net_id);
Nicolas Dichtelcf71d2bc2014-02-20 10:19:31 +0100250void ip_tunnel_dst_reset_all(struct ip_tunnel *t);
Tom Herbert56328482014-09-17 12:25:58 -0700251int ip_tunnel_encap_setup(struct ip_tunnel *t,
252 struct ip_tunnel_encap *ipencap);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000253
254/* Extract dsfield from inner protocol */
255static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
256 const struct sk_buff *skb)
257{
258 if (skb->protocol == htons(ETH_P_IP))
259 return iph->tos;
260 else if (skb->protocol == htons(ETH_P_IPV6))
261 return ipv6_get_dsfield((const struct ipv6hdr *)iph);
262 else
263 return 0;
264}
265
266/* Propogate ECN bits out */
267static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
268 const struct sk_buff *skb)
269{
270 u8 inner = ip_tunnel_get_dsfield(iph, skb);
271
272 return INET_ECN_encapsulate(tos, inner);
273}
274
Pravin B Shelar3d7b46c2013-06-17 17:50:02 -0700275int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
Eric Dumazetaad88722014-04-15 13:47:15 -0400276int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
Jiri Benc6b8847c2015-08-20 13:56:21 +0200277 __be32 src, __be32 dst, u8 proto,
278 u8 tos, u8 ttl, __be16 df, bool xnet);
Jiri Benc63d008a2015-09-22 18:12:11 +0200279struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
280 gfp_t flags);
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -0700281
Eric Dumazet2d26f0a2013-10-19 11:42:55 -0700282struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum,
283 int gso_type_mask);
284
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -0700285static inline void iptunnel_xmit_stats(int err,
286 struct net_device_stats *err_stats,
Li RongQing8f849852014-01-04 13:57:59 +0800287 struct pcpu_sw_netstats __percpu *stats)
Pravin B Shelarc5441932013-03-25 14:49:35 +0000288{
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -0700289 if (err > 0) {
Jason A. Donenfeldb4fe85f2015-11-12 17:35:58 +0100290 struct pcpu_sw_netstats *tstats = get_cpu_ptr(stats);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000291
Pravin B Shelarc5441932013-03-25 14:49:35 +0000292 u64_stats_update_begin(&tstats->syncp);
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -0700293 tstats->tx_bytes += err;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000294 tstats->tx_packets++;
295 u64_stats_update_end(&tstats->syncp);
Jason A. Donenfeldb4fe85f2015-11-12 17:35:58 +0100296 put_cpu_ptr(tstats);
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -0700297 } else if (err < 0) {
298 err_stats->tx_errors++;
299 err_stats->tx_aborted_errors++;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000300 } else {
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -0700301 err_stats->tx_dropped++;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000302 }
303}
Jesse Gross5243b6a2013-06-21 16:17:11 -0700304
Pravin B Shelar4c222792015-08-30 18:09:38 -0700305static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
Thomas Grafee122c72015-07-21 10:43:58 +0200306{
307 return info + 1;
308}
309
Pravin B Shelar4c222792015-08-30 18:09:38 -0700310static inline void ip_tunnel_info_opts_get(void *to,
311 const struct ip_tunnel_info *info)
312{
313 memcpy(to, info + 1, info->options_len);
314}
315
316static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
317 const void *from, int len)
318{
319 memcpy(ip_tunnel_info_opts(info), from, len);
320 info->options_len = len;
321}
322
Thomas Graf3093fbe2015-07-21 10:44:00 +0200323static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
324{
325 return (struct ip_tunnel_info *)lwtstate->data;
326}
327
Thomas Grafe7030872015-07-21 10:44:01 +0200328extern struct static_key ip_tunnel_metadata_cnt;
329
330/* Returns > 0 if metadata should be collected */
331static inline int ip_tunnel_collect_metadata(void)
332{
333 return static_key_false(&ip_tunnel_metadata_cnt);
334}
335
Thomas Graf045a0fa2015-07-23 10:08:44 +0200336void __init ip_tunnel_core_init(void);
337
Thomas Grafe7030872015-07-21 10:44:01 +0200338void ip_tunnel_need_metadata(void);
339void ip_tunnel_unneed_metadata(void);
340
Thomas Graf05283182015-07-22 14:43:58 +0200341#else /* CONFIG_INET */
342
343static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
344{
345 return NULL;
346}
347
348static inline void ip_tunnel_need_metadata(void)
349{
350}
351
352static inline void ip_tunnel_unneed_metadata(void)
353{
354}
355
Jesse Gross5243b6a2013-06-21 16:17:11 -0700356#endif /* CONFIG_INET */
357
Pravin B Shelarc5441932013-03-25 14:49:35 +0000358#endif /* __NET_IP_TUNNELS_H */