]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/net/ip_tunnels.h
ip_tunnels: use offsetofend
[mirror_ubuntu-artful-kernel.git] / include / net / ip_tunnels.h
CommitLineData
c5441932
PS
1#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>
7#include <linux/types.h>
8#include <linux/u64_stats_sync.h>
9#include <net/dsfield.h>
10#include <net/gro_cells.h>
11#include <net/inet_ecn.h>
56328486 12#include <net/netns/generic.h>
c5441932 13#include <net/rtnetlink.h>
3093fbe7 14#include <net/lwtunnel.h>
c5441932
PS
15
16#if IS_ENABLED(CONFIG_IPV6)
17#include <net/ipv6.h>
18#include <net/ip6_fib.h>
19#include <net/ip6_route.h>
20#endif
21
22/* Keep error state on tunnel for 30 sec */
23#define IPTUNNEL_ERR_TIMEO (30*HZ)
24
1d8fff90 25/* Used to memset ip_tunnel padding. */
376534a3 26#define IP_TUNNEL_KEY_SIZE offsetofend(struct ip_tunnel_key, tp_dst)
1d8fff90
TG
27
28struct ip_tunnel_key {
29 __be64 tun_id;
30 __be32 ipv4_src;
31 __be32 ipv4_dst;
32 __be16 tun_flags;
6b8847c5
JB
33 u8 ipv4_tos;
34 u8 ipv4_ttl;
1d8fff90
TG
35 __be16 tp_src;
36 __be16 tp_dst;
ac1cf399 37};
1d8fff90 38
ee122c79
TG
39/* Indicates whether the tunnel info structure represents receive
40 * or transmit tunnel parameters.
41 */
42enum {
43 IP_TUNNEL_INFO_RX,
44 IP_TUNNEL_INFO_TX,
45};
46
1d8fff90
TG
47struct ip_tunnel_info {
48 struct ip_tunnel_key key;
49 const void *options;
50 u8 options_len;
ee122c79 51 u8 mode;
1d8fff90
TG
52};
53
c5441932
PS
54/* 6rd prefix/relay information */
55#ifdef CONFIG_IPV6_SIT_6RD
56struct ip_tunnel_6rd_parm {
57 struct in6_addr prefix;
58 __be32 relay_prefix;
59 u16 prefixlen;
60 u16 relay_prefixlen;
61};
62#endif
63
56328486 64struct ip_tunnel_encap {
6b8847c5
JB
65 u16 type;
66 u16 flags;
56328486
TH
67 __be16 sport;
68 __be16 dport;
69};
70
c5441932
PS
71struct ip_tunnel_prl_entry {
72 struct ip_tunnel_prl_entry __rcu *next;
73 __be32 addr;
74 u16 flags;
75 struct rcu_head rcu_head;
76};
77
9a4aa9af
TH
78struct ip_tunnel_dst {
79 struct dst_entry __rcu *dst;
95cb5745 80 __be32 saddr;
9a4aa9af
TH
81};
82
2e15ea39
PS
83struct metadata_dst;
84
c5441932
PS
85struct ip_tunnel {
86 struct ip_tunnel __rcu *next;
87 struct hlist_node hash_node;
88 struct net_device *dev;
5e6700b3 89 struct net *net; /* netns for packet i/o */
c5441932
PS
90
91 int err_count; /* Number of arrived ICMP errors */
92 unsigned long err_time; /* Time when the last ICMP error
93 * arrived */
94
95 /* These four fields used only by GRE */
6b8847c5
JB
96 u32 i_seqno; /* The last seen seqno */
97 u32 o_seqno; /* The last output seqno */
56328486 98 int tun_hlen; /* Precalculated header length */
c5441932
PS
99 int mlink;
100
9a4aa9af 101 struct ip_tunnel_dst __percpu *dst_cache;
7d442fab 102
c5441932
PS
103 struct ip_tunnel_parm parms;
104
56328486
TH
105 int encap_hlen; /* Encap header length (FOU,GUE) */
106 struct ip_tunnel_encap encap;
107
108 int hlen; /* tun_hlen + encap_hlen */
109
c5441932
PS
110 /* for SIT */
111#ifdef CONFIG_IPV6_SIT_6RD
112 struct ip_tunnel_6rd_parm ip6rd;
113#endif
114 struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */
115 unsigned int prl_count; /* # of entries in PRL */
116 int ip_tnl_net_id;
117 struct gro_cells gro_cells;
2e15ea39 118 bool collect_md;
c5441932
PS
119};
120
f5796684
JG
121#define TUNNEL_CSUM __cpu_to_be16(0x01)
122#define TUNNEL_ROUTING __cpu_to_be16(0x02)
123#define TUNNEL_KEY __cpu_to_be16(0x04)
124#define TUNNEL_SEQ __cpu_to_be16(0x08)
125#define TUNNEL_STRICT __cpu_to_be16(0x10)
126#define TUNNEL_REC __cpu_to_be16(0x20)
127#define TUNNEL_VERSION __cpu_to_be16(0x40)
128#define TUNNEL_NO_KEY __cpu_to_be16(0x80)
9a628224 129#define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
f5796684
JG
130#define TUNNEL_OAM __cpu_to_be16(0x0200)
131#define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
1dd144cf
TG
132#define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
133#define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
134
135#define TUNNEL_OPTIONS_PRESENT (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT)
c5441932
PS
136
137struct tnl_ptk_info {
138 __be16 flags;
139 __be16 proto;
140 __be32 key;
141 __be32 seq;
142};
143
144#define PACKET_RCVD 0
145#define PACKET_REJECT 1
146
6261d983 147#define IP_TNL_HASH_BITS 7
c5441932
PS
148#define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
149
150struct ip_tunnel_net {
c5441932 151 struct net_device *fb_tunnel_dev;
6261d983 152 struct hlist_head tunnels[IP_TNL_HASH_SIZE];
2e15ea39 153 struct ip_tunnel __rcu *collect_md_tun;
c5441932
PS
154};
155
a8c5f90f
TH
156struct ip_tunnel_encap_ops {
157 size_t (*encap_hlen)(struct ip_tunnel_encap *e);
158 int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
159 u8 *protocol, struct flowi4 *fl4);
160};
161
162#define MAX_IPTUN_ENCAP_OPS 8
163
164extern const struct ip_tunnel_encap_ops __rcu *
165 iptun_encaps[MAX_IPTUN_ENCAP_OPS];
166
167int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
168 unsigned int num);
169int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
170 unsigned int num);
171
1d8fff90
TG
172static inline void __ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
173 __be32 saddr, __be32 daddr,
174 u8 tos, u8 ttl,
175 __be16 tp_src, __be16 tp_dst,
176 __be64 tun_id, __be16 tun_flags,
177 const void *opts, u8 opts_len)
178{
179 tun_info->key.tun_id = tun_id;
180 tun_info->key.ipv4_src = saddr;
181 tun_info->key.ipv4_dst = daddr;
182 tun_info->key.ipv4_tos = tos;
183 tun_info->key.ipv4_ttl = ttl;
184 tun_info->key.tun_flags = tun_flags;
185
186 /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
187 * the upper tunnel are used.
188 * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
189 */
190 tun_info->key.tp_src = tp_src;
191 tun_info->key.tp_dst = tp_dst;
192
193 /* Clear struct padding. */
194 if (sizeof(tun_info->key) != IP_TUNNEL_KEY_SIZE)
195 memset((unsigned char *)&tun_info->key + IP_TUNNEL_KEY_SIZE,
196 0, sizeof(tun_info->key) - IP_TUNNEL_KEY_SIZE);
197
198 tun_info->options = opts;
199 tun_info->options_len = opts_len;
200}
201
202static inline void ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
203 const struct iphdr *iph,
204 __be16 tp_src, __be16 tp_dst,
205 __be64 tun_id, __be16 tun_flags,
206 const void *opts, u8 opts_len)
207{
208 __ip_tunnel_info_init(tun_info, iph->saddr, iph->daddr,
209 iph->tos, iph->ttl, tp_src, tp_dst,
210 tun_id, tun_flags, opts, opts_len);
211}
212
5243b6ac
JG
213#ifdef CONFIG_INET
214
c5441932
PS
215int ip_tunnel_init(struct net_device *dev);
216void ip_tunnel_uninit(struct net_device *dev);
217void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
1728d4fa 218struct net *ip_tunnel_get_link_net(const struct net_device *dev);
1e99584b 219int ip_tunnel_get_iflink(const struct net_device *dev);
d3b6f614
ED
220int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
221 struct rtnl_link_ops *ops, char *devname);
c5441932 222
6c742e71 223void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
c5441932
PS
224
225void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
bf3d6a8f 226 const struct iphdr *tnl_params, const u8 protocol);
c5441932 227int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
56328486
TH
228int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
229 u8 *protocol, struct flowi4 *fl4);
c5441932
PS
230int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
231
232struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
233 struct rtnl_link_stats64 *tot);
234struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
235 int link, __be16 flags,
236 __be32 remote, __be32 local,
237 __be32 key);
238
239int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
2e15ea39
PS
240 const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
241 bool log_ecn_error);
c5441932
PS
242int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
243 struct ip_tunnel_parm *p);
244int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
245 struct ip_tunnel_parm *p);
246void ip_tunnel_setup(struct net_device *dev, int net_id);
cf71d2bc 247void ip_tunnel_dst_reset_all(struct ip_tunnel *t);
56328486
TH
248int ip_tunnel_encap_setup(struct ip_tunnel *t,
249 struct ip_tunnel_encap *ipencap);
c5441932
PS
250
251/* Extract dsfield from inner protocol */
252static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
253 const struct sk_buff *skb)
254{
255 if (skb->protocol == htons(ETH_P_IP))
256 return iph->tos;
257 else if (skb->protocol == htons(ETH_P_IPV6))
258 return ipv6_get_dsfield((const struct ipv6hdr *)iph);
259 else
260 return 0;
261}
262
263/* Propogate ECN bits out */
264static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
265 const struct sk_buff *skb)
266{
267 u8 inner = ip_tunnel_get_dsfield(iph, skb);
268
269 return INET_ECN_encapsulate(tos, inner);
270}
271
3d7b46cd 272int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
aad88724 273int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
6b8847c5
JB
274 __be32 src, __be32 dst, u8 proto,
275 u8 tos, u8 ttl, __be16 df, bool xnet);
0e6fbc5b 276
2d26f0a3
ED
277struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum,
278 int gso_type_mask);
279
0e6fbc5b
PS
280static inline void iptunnel_xmit_stats(int err,
281 struct net_device_stats *err_stats,
8f84985f 282 struct pcpu_sw_netstats __percpu *stats)
c5441932 283{
0e6fbc5b 284 if (err > 0) {
8f84985f 285 struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats);
c5441932 286
c5441932 287 u64_stats_update_begin(&tstats->syncp);
0e6fbc5b 288 tstats->tx_bytes += err;
c5441932
PS
289 tstats->tx_packets++;
290 u64_stats_update_end(&tstats->syncp);
0e6fbc5b
PS
291 } else if (err < 0) {
292 err_stats->tx_errors++;
293 err_stats->tx_aborted_errors++;
c5441932 294 } else {
0e6fbc5b 295 err_stats->tx_dropped++;
c5441932
PS
296 }
297}
5243b6ac 298
ee122c79
TG
299static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info, size_t n)
300{
301 return info + 1;
302}
303
3093fbe7
TG
304static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
305{
306 return (struct ip_tunnel_info *)lwtstate->data;
307}
308
e7030878
TG
309extern struct static_key ip_tunnel_metadata_cnt;
310
311/* Returns > 0 if metadata should be collected */
312static inline int ip_tunnel_collect_metadata(void)
313{
314 return static_key_false(&ip_tunnel_metadata_cnt);
315}
316
045a0fa0
TG
317void __init ip_tunnel_core_init(void);
318
e7030878
TG
319void ip_tunnel_need_metadata(void);
320void ip_tunnel_unneed_metadata(void);
321
05283187
TG
322#else /* CONFIG_INET */
323
324static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
325{
326 return NULL;
327}
328
329static inline void ip_tunnel_need_metadata(void)
330{
331}
332
333static inline void ip_tunnel_unneed_metadata(void)
334{
335}
336
5243b6ac
JG
337#endif /* CONFIG_INET */
338
c5441932 339#endif /* __NET_IP_TUNNELS_H */