1 #ifndef __NET_IP_TUNNELS_WRAPPER_H
2 #define __NET_IP_TUNNELS_WRAPPER_H 1
4 #include <linux/version.h>
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>
13 #include <linux/if_tunnel.h>
14 #include <linux/types.h>
15 #include <net/dsfield.h>
16 #include <net/dst_cache.h>
18 #include <net/inet_ecn.h>
20 #include <net/rtnetlink.h>
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);
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)
30 return rpl___iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet);
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 *));
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.
42 #define iptunnel_handle_offloads rpl_iptunnel_handle_offloads
43 struct sk_buff *rpl_iptunnel_handle_offloads(struct sk_buff *skb,
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);
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)
70 #define PACKET_REJECT 1
73 #ifndef TUNNEL_DONT_FRAGMENT
74 #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
78 #define TUNNEL_OAM __cpu_to_be16(0x0200)
79 #define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
82 #ifndef TUNNEL_GENEVE_OPT
83 #define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
86 #ifndef TUNNEL_VXLAN_OPT
87 #define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
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)
94 #define skb_is_encapsulated ovs_skb_is_encapsulated
95 bool ovs_skb_is_encapsulated(struct sk_buff *skb);
97 /* Used to memset ip_tunnel padding. */
98 #define IP_TUNNEL_KEY_SIZE offsetofend(struct ip_tunnel_key, tp_dst)
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))
106 struct ip_tunnel_key {
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 */
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 */
130 struct ip_tunnel_info {
131 struct ip_tunnel_key key;
132 struct dst_cache dst_cache;
137 static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info *tun_info)
139 return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
142 static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
147 static inline void ip_tunnel_info_opts_get(void *to,
148 const struct ip_tunnel_info *info)
150 memcpy(to, info + 1, info->options_len);
153 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
154 const void *from, int len)
156 memcpy(ip_tunnel_info_opts(info), from, len);
157 info->options_len = len;
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)
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);
174 key->tun_flags = tun_flags;
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.
180 key->tp_src = tp_src;
181 key->tp_dst = tp_dst;
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);
189 #define ip_tunnel_collect_metadata() true
191 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
192 #define TUNNEL_NOCACHE 0
195 ip_tunnel_dst_cache_usable(const struct sk_buff *skb,
196 const struct ip_tunnel_info *info)
202 if (info->key.tun_flags & TUNNEL_NOCACHE)
209 #define ip_tunnel rpl_ip_tunnel
212 struct net_device *dev;
213 struct net *net; /* netns for packet i/o */
215 int err_count; /* Number of arrived ICMP errors */
216 unsigned long err_time; /* Time when the last ICMP error
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 */
226 struct ip_tunnel_parm parms;
228 int encap_hlen; /* Encap header length (FOU,GUE) */
229 int hlen; /* tun_hlen + encap_hlen */
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;
242 #ifndef HAVE_PCPU_SW_NETSTATS
243 #define ip_tunnel_get_stats64 rpl_ip_tunnel_get_stats64
245 #define rpl_ip_tunnel_get_stats64 ip_tunnel_get_stats64
247 struct rtnl_link_stats64 *rpl_ip_tunnel_get_stats64(struct net_device *dev,
248 struct rtnl_link_stats64 *tot);
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)
254 if (skb->protocol == htons(ETH_P_IP))
256 else if (skb->protocol == htons(ETH_P_IPV6))
257 return ipv6_get_dsfield((const struct ipv6hdr *)iph);
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)
266 u8 inner = ip_tunnel_get_dsfield(iph, skb);
268 return INET_ECN_encapsulate(tos, inner);
271 static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
274 struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
276 u64_stats_update_begin(&tstats->syncp);
277 tstats->tx_bytes += pkt_len;
278 tstats->tx_packets++;
279 u64_stats_update_end(&tstats->syncp);
282 struct net_device_stats *err_stats = &dev->stats;
285 err_stats->tx_errors++;
286 err_stats->tx_aborted_errors++;
288 err_stats->tx_dropped++;
293 #define ip_tunnel_init rpl_ip_tunnel_init
294 int rpl_ip_tunnel_init(struct net_device *dev);
296 #define ip_tunnel_uninit rpl_ip_tunnel_uninit
297 void rpl_ip_tunnel_uninit(struct net_device *dev);
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);
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);
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);
310 void rpl_ip_tunnel_dellink(struct net_device *dev);
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);
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);
320 #define ip_tunnel_setup rpl_ip_tunnel_setup
321 void rpl_ip_tunnel_setup(struct net_device *dev, int net_id);
323 #define ip_tunnel_get_iflink rpl_ip_tunnel_get_iflink
324 int rpl_ip_tunnel_get_iflink(const struct net_device *dev);
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);
329 #ifndef HAVE___IP_TUNNEL_CHANGE_MTU
330 #define __ip_tunnel_change_mtu rpl___ip_tunnel_change_mtu
331 int rpl___ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
334 static inline int iptunnel_pull_offloads(struct sk_buff *skb)
336 if (skb_is_gso(skb)) {
339 err = skb_unclone(skb, GFP_ATOMIC);
342 skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
346 skb->encapsulation = 0;
349 #endif /* USE_UPSTREAM_TUNNEL */
350 #endif /* __NET_IP_TUNNELS_H */