6775c9aaf97ef2179e87bf406c922e63b25ff49f
[cascardo/ovs.git] / datapath / linux / compat / include / net / ip_tunnels.h
1 #ifndef __NET_IP_TUNNELS_WRAPPER_H
2 #define __NET_IP_TUNNELS_WRAPPER_H 1
3
4 #include <linux/version.h>
5
6 #ifdef USE_UPSTREAM_TUNNEL
7 /* Block all ip_tunnel functions.
8  * Only function that do not depend on ip_tunnel structure can
9  * be used. Those needs to be explicitly defined in this header file. */
10 #include_next <net/ip_tunnels.h>
11 #else
12
13 #include <linux/if_tunnel.h>
14 #include <linux/types.h>
15 #include <net/dsfield.h>
16 #include <net/dst_cache.h>
17 #include <net/flow.h>
18 #include <net/inet_ecn.h>
19 #include <net/ip.h>
20 #include <net/rtnetlink.h>
21
22 #define __iptunnel_pull_header rpl___iptunnel_pull_header
23 int rpl___iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
24                            __be16 inner_proto, bool raw_proto, bool xnet);
25
26 #define iptunnel_pull_header rpl_iptunnel_pull_header
27 static inline int rpl_iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
28                                        __be16 inner_proto, bool xnet)
29 {
30         return rpl___iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet);
31 }
32
33 int ovs_iptunnel_handle_offloads(struct sk_buff *skb,
34                                  bool csum_help, int gso_type_mask,
35                                  void (*fix_segment)(struct sk_buff *));
36
37 /* This is is required to compile upstream gre.h. gre_handle_offloads()
38  * is defined in gre.h and needs iptunnel_handle_offloads(). This provides
39  * default signature for this function.
40  * rpl prefix is to make OVS build happy.
41  */
42 #define iptunnel_handle_offloads rpl_iptunnel_handle_offloads
43 struct sk_buff *rpl_iptunnel_handle_offloads(struct sk_buff *skb,
44                                          bool csum_help,
45                                          int gso_type_mask);
46
47 #define iptunnel_xmit rpl_iptunnel_xmit
48 void rpl_iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
49                        __be32 src, __be32 dst, __u8 proto, __u8 tos, __u8 ttl,
50                        __be16 df, bool xnet);
51
52 #ifndef TUNNEL_CSUM
53 #define TUNNEL_CSUM     __cpu_to_be16(0x01)
54 #define TUNNEL_ROUTING  __cpu_to_be16(0x02)
55 #define TUNNEL_KEY      __cpu_to_be16(0x04)
56 #define TUNNEL_SEQ      __cpu_to_be16(0x08)
57 #define TUNNEL_STRICT   __cpu_to_be16(0x10)
58 #define TUNNEL_REC      __cpu_to_be16(0x20)
59 #define TUNNEL_VERSION  __cpu_to_be16(0x40)
60 #define TUNNEL_NO_KEY   __cpu_to_be16(0x80)
61
62 struct tnl_ptk_info {
63         __be16 flags;
64         __be16 proto;
65         __be32 key;
66         __be32 seq;
67 };
68
69 #define PACKET_RCVD     0
70 #define PACKET_REJECT   1
71 #endif
72
73 #ifndef TUNNEL_DONT_FRAGMENT
74 #define TUNNEL_DONT_FRAGMENT    __cpu_to_be16(0x0100)
75 #endif
76
77 #ifndef TUNNEL_OAM
78 #define TUNNEL_OAM      __cpu_to_be16(0x0200)
79 #define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
80 #endif
81
82 #ifndef TUNNEL_GENEVE_OPT
83 #define TUNNEL_GENEVE_OPT       __cpu_to_be16(0x0800)
84 #endif
85
86 #ifndef TUNNEL_VXLAN_OPT
87 #define TUNNEL_VXLAN_OPT        __cpu_to_be16(0x1000)
88 #endif
89
90 /* Older kernels defined TUNNEL_OPTIONS_PRESENT to GENEVE only */
91 #undef TUNNEL_OPTIONS_PRESENT
92 #define TUNNEL_OPTIONS_PRESENT (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT)
93
94 #define skb_is_encapsulated ovs_skb_is_encapsulated
95 bool ovs_skb_is_encapsulated(struct sk_buff *skb);
96
97 /* Used to memset ip_tunnel padding. */
98 #define IP_TUNNEL_KEY_SIZE      offsetofend(struct ip_tunnel_key, tp_dst)
99
100 /* Used to memset ipv4 address padding. */
101 #define IP_TUNNEL_KEY_IPV4_PAD  offsetofend(struct ip_tunnel_key, u.ipv4.dst)
102 #define IP_TUNNEL_KEY_IPV4_PAD_LEN                              \
103         (FIELD_SIZEOF(struct ip_tunnel_key, u) -                \
104          FIELD_SIZEOF(struct ip_tunnel_key, u.ipv4))
105
106 struct ip_tunnel_key {
107         __be64                  tun_id;
108         union {
109                 struct {
110                         __be32  src;
111                         __be32  dst;
112                 } ipv4;
113                 struct {
114                         struct in6_addr src;
115                         struct in6_addr dst;
116                 } ipv6;
117         } u;
118         __be16                  tun_flags;
119         u8                      tos;            /* TOS for IPv4, TC for IPv6 */
120         u8                      ttl;            /* TTL for IPv4, HL for IPv6 */
121         __be32                  label;          /* Flow Label for IPv6 */
122         __be16                  tp_src;
123         __be16                  tp_dst;
124 };
125
126 /* Flags for ip_tunnel_info mode. */
127 #define IP_TUNNEL_INFO_TX       0x01    /* represents tx tunnel parameters */
128 #define IP_TUNNEL_INFO_IPV6     0x02    /* key contains IPv6 addresses */
129
130 struct ip_tunnel_info {
131         struct ip_tunnel_key    key;
132         struct dst_cache        dst_cache;
133         u8                      options_len;
134         u8                      mode;
135 };
136
137 static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info *tun_info)
138 {
139         return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
140 }
141
142 static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
143 {
144         return info + 1;
145 }
146
147 static inline void ip_tunnel_info_opts_get(void *to,
148                                            const struct ip_tunnel_info *info)
149 {
150         memcpy(to, info + 1, info->options_len);
151 }
152
153 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
154                                            const void *from, int len)
155 {
156         memcpy(ip_tunnel_info_opts(info), from, len);
157         info->options_len = len;
158 }
159
160 static inline void ip_tunnel_key_init(struct ip_tunnel_key *key,
161                                       __be32 saddr, __be32 daddr,
162                                       u8 tos, u8 ttl, __be32 label,
163                                       __be16 tp_src, __be16 tp_dst,
164                                       __be64 tun_id, __be16 tun_flags)
165 {
166         key->tun_id = tun_id;
167         key->u.ipv4.src = saddr;
168         key->u.ipv4.dst = daddr;
169         memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD,
170                0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
171         key->tos = tos;
172         key->ttl = ttl;
173         key->label = label;
174         key->tun_flags = tun_flags;
175
176         /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
177          * the upper tunnel are used.
178          * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
179          */
180         key->tp_src = tp_src;
181         key->tp_dst = tp_dst;
182
183         /* Clear struct padding. */
184         if (sizeof(*key) != IP_TUNNEL_KEY_SIZE)
185                 memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE,
186                        0, sizeof(*key) - IP_TUNNEL_KEY_SIZE);
187 }
188
189 #define ip_tunnel_collect_metadata() true
190
191 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
192 #define TUNNEL_NOCACHE 0
193
194 static inline bool
195 ip_tunnel_dst_cache_usable(const struct sk_buff *skb,
196                            const struct ip_tunnel_info *info)
197 {
198         if (skb->mark)
199                 return false;
200         if (!info)
201                 return true;
202         if (info->key.tun_flags & TUNNEL_NOCACHE)
203                 return false;
204
205         return true;
206 }
207 #endif
208
209 #define ip_tunnel rpl_ip_tunnel
210
211 struct ip_tunnel {
212         struct net_device       *dev;
213         struct net              *net;   /* netns for packet i/o */
214
215         int             err_count;      /* Number of arrived ICMP errors */
216         unsigned long   err_time;       /* Time when the last ICMP error
217                                          * arrived
218                                          */
219
220         /* These four fields used only by GRE */
221         u32             i_seqno;        /* The last seen seqno  */
222         u32             o_seqno;        /* The last output seqno */
223         int             tun_hlen;       /* Precalculated header length */
224         int             mlink;
225
226         struct ip_tunnel_parm parms;
227
228         int             encap_hlen;     /* Encap header length (FOU,GUE) */
229         int             hlen;           /* tun_hlen + encap_hlen */
230
231         int             ip_tnl_net_id;
232         bool            collect_md;
233 };
234
235 #define ip_tunnel_net rpl_ip_tunnel_net
236 struct ip_tunnel_net {
237         struct ip_tunnel __rcu *collect_md_tun;
238         struct rtnl_link_ops *rtnl_ops;
239 };
240
241
242 #ifndef HAVE_PCPU_SW_NETSTATS
243 #define ip_tunnel_get_stats64 rpl_ip_tunnel_get_stats64
244 #else
245 #define rpl_ip_tunnel_get_stats64 ip_tunnel_get_stats64
246 #endif
247 struct rtnl_link_stats64 *rpl_ip_tunnel_get_stats64(struct net_device *dev,
248                                                     struct rtnl_link_stats64 *tot);
249
250 #define ip_tunnel_get_dsfield rpl_ip_tunnel_get_dsfield
251 static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
252                 const struct sk_buff *skb)
253 {
254         if (skb->protocol == htons(ETH_P_IP))
255                 return iph->tos;
256         else if (skb->protocol == htons(ETH_P_IPV6))
257                 return ipv6_get_dsfield((const struct ipv6hdr *)iph);
258         else
259                 return 0;
260 }
261
262 #define ip_tunnel_ecn_encap rpl_ip_tunnel_ecn_encap
263 static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
264                 const struct sk_buff *skb)
265 {
266         u8 inner = ip_tunnel_get_dsfield(iph, skb);
267
268         return INET_ECN_encapsulate(tos, inner);
269 }
270
271 static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
272 {
273         if (pkt_len > 0) {
274                 struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
275
276                 u64_stats_update_begin(&tstats->syncp);
277                 tstats->tx_bytes += pkt_len;
278                 tstats->tx_packets++;
279                 u64_stats_update_end(&tstats->syncp);
280                 put_cpu_ptr(tstats);
281         } else {
282                 struct net_device_stats *err_stats = &dev->stats;
283
284                 if (pkt_len < 0) {
285                         err_stats->tx_errors++;
286                         err_stats->tx_aborted_errors++;
287                 } else {
288                         err_stats->tx_dropped++;
289                 }
290         }
291 }
292
293 #define ip_tunnel_init rpl_ip_tunnel_init
294 int rpl_ip_tunnel_init(struct net_device *dev);
295
296 #define ip_tunnel_uninit rpl_ip_tunnel_uninit
297 void rpl_ip_tunnel_uninit(struct net_device *dev);
298
299 #define ip_tunnel_change_mtu rpl_ip_tunnel_change_mtu
300 int rpl_ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
301
302 #define ip_tunnel_newlink rpl_ip_tunnel_newlink
303 int rpl_ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
304                           struct ip_tunnel_parm *p);
305
306 #define ip_tunnel_dellink rpl_ip_tunnel_dellink
307 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
308 void rpl_ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
309 #else
310 void rpl_ip_tunnel_dellink(struct net_device *dev);
311 #endif
312
313 #define ip_tunnel_init_net rpl_ip_tunnel_init_net
314 int rpl_ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
315                            struct rtnl_link_ops *ops, char *devname);
316
317 #define ip_tunnel_delete_net rpl_ip_tunnel_delete_net
318 void rpl_ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
319
320 #define ip_tunnel_setup rpl_ip_tunnel_setup
321 void rpl_ip_tunnel_setup(struct net_device *dev, int net_id);
322
323 #define ip_tunnel_get_iflink rpl_ip_tunnel_get_iflink
324 int rpl_ip_tunnel_get_iflink(const struct net_device *dev);
325
326 #define ip_tunnel_get_link_net rpl_ip_tunnel_get_link_net
327 struct net *rpl_ip_tunnel_get_link_net(const struct net_device *dev);
328
329 #define __ip_tunnel_change_mtu rpl___ip_tunnel_change_mtu
330 int rpl___ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
331
332 static inline int iptunnel_pull_offloads(struct sk_buff *skb)
333 {
334         if (skb_is_gso(skb)) {
335                 int err;
336
337                 err = skb_unclone(skb, GFP_ATOMIC);
338                 if (unlikely(err))
339                         return err;
340                 skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
341                                                NETIF_F_GSO_SHIFT);
342         }
343
344         skb->encapsulation = 0;
345         return 0;
346 }
347 #endif /* USE_UPSTREAM_TUNNEL */
348 #endif /* __NET_IP_TUNNELS_H */