datapath: Add support for kernel 4.6
[cascardo/ovs.git] / datapath / linux / compat / include / net / udp_tunnel.h
1 #ifndef __NET_UDP_TUNNEL_WRAPPER_H
2 #define __NET_UDP_TUNNEL_WRAPPER_H
3
4 #include <linux/version.h>
5 #include <linux/kconfig.h>
6
7 #include <net/addrconf.h>
8 #include <net/dst_metadata.h>
9 #include <linux/netdev_features.h>
10
11 #ifdef USE_UPSTREAM_TUNNEL
12 #include_next <net/udp_tunnel.h>
13
14 #else
15
16 #include <net/addrconf.h>
17 #include <net/ip_tunnels.h>
18 #include <net/udp.h>
19
20 struct udp_port_cfg {
21         u8                      family;
22
23         /* Used only for kernel-created sockets */
24         union {
25                 struct in_addr          local_ip;
26 #if IS_ENABLED(CONFIG_IPV6)
27                 struct in6_addr         local_ip6;
28 #endif
29         };
30
31         union {
32                 struct in_addr          peer_ip;
33 #if IS_ENABLED(CONFIG_IPV6)
34                 struct in6_addr         peer_ip6;
35 #endif
36         };
37
38         __be16                  local_udp_port;
39         __be16                  peer_udp_port;
40         unsigned int            use_udp_checksums:1,
41                                 use_udp6_tx_checksums:1,
42                                 use_udp6_rx_checksums:1,
43                                 ipv6_v6only:1;
44 };
45
46 #define udp_sock_create4 rpl_udp_sock_create4
47 int rpl_udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
48                      struct socket **sockp);
49
50 #define udp_sock_create6 rpl_udp_sock_create6
51 #if IS_ENABLED(CONFIG_IPV6)
52 int rpl_udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
53                 struct socket **sockp);
54 #else
55 static inline int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
56                                    struct socket **sockp)
57 {
58         return -EPFNOSUPPORT;
59 }
60 #endif
61
62 #define udp_sock_create rpl_udp_sock_create
63 static inline int udp_sock_create(struct net *net,
64                                   struct udp_port_cfg *cfg,
65                                   struct socket **sockp)
66 {
67         if (cfg->family == AF_INET)
68                 return udp_sock_create4(net, cfg, sockp);
69
70         if (cfg->family == AF_INET6)
71                 return udp_sock_create6(net, cfg, sockp);
72
73         return -EPFNOSUPPORT;
74 }
75
76 typedef int (*udp_tunnel_encap_rcv_t)(struct sock *sk, struct sk_buff *skb);
77 typedef void (*udp_tunnel_encap_destroy_t)(struct sock *sk);
78 typedef struct sk_buff **(*udp_tunnel_gro_receive_t)(struct sock *sk,
79                                                     struct sk_buff **head,
80                                                     struct sk_buff *skb);
81 typedef int (*udp_tunnel_gro_complete_t)(struct sock *sk, struct sk_buff *skb,
82                                         int nhoff);
83
84 struct udp_tunnel_sock_cfg {
85         void *sk_user_data;     /* user data used by encap_rcv call back */
86         /* Used for setting up udp_sock fields, see udp.h for details */
87         __u8  encap_type;
88         udp_tunnel_encap_rcv_t encap_rcv;
89         udp_tunnel_encap_destroy_t encap_destroy;
90 #ifdef HAVE_UDP_TUNNEL_SOCK_CFG_GRO_RECEIVE
91         udp_tunnel_gro_receive_t gro_receive;
92         udp_tunnel_gro_complete_t gro_complete;
93 #endif
94 };
95
96 /* Setup the given (UDP) sock to receive UDP encapsulated packets */
97 #define setup_udp_tunnel_sock rpl_setup_udp_tunnel_sock
98 void rpl_setup_udp_tunnel_sock(struct net *net, struct socket *sock,
99                                struct udp_tunnel_sock_cfg *sock_cfg);
100
101 /* Transmit the skb using UDP encapsulation. */
102 #define udp_tunnel_xmit_skb rpl_udp_tunnel_xmit_skb
103 void rpl_udp_tunnel_xmit_skb(struct rtable *rt,
104                             struct sock *sk, struct sk_buff *skb,
105                             __be32 src, __be32 dst, __u8 tos, __u8 ttl,
106                             __be16 df, __be16 src_port, __be16 dst_port,
107                             bool xnet, bool nocheck);
108
109
110 #define udp_tunnel_sock_release rpl_udp_tunnel_sock_release
111 void rpl_udp_tunnel_sock_release(struct socket *sock);
112
113 #define udp_tunnel_encap_enable rpl_udp_tunnel_encap_enable
114 static inline void udp_tunnel_encap_enable(struct socket *sock)
115 {
116 #if IS_ENABLED(CONFIG_IPV6)
117         if (sock->sk->sk_family == PF_INET6)
118 #ifdef HAVE_IPV6_STUB
119                 ipv6_stub->udpv6_encap_enable();
120 #else
121                 udpv6_encap_enable();
122 #endif
123         else
124 #endif
125                 udp_encap_enable();
126 }
127
128 #if IS_ENABLED(CONFIG_IPV6)
129 #define udp_tunnel6_xmit_skb rpl_udp_tunnel6_xmit_skb
130 int rpl_udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
131                          struct sk_buff *skb,
132                          struct net_device *dev, struct in6_addr *saddr,
133                          struct in6_addr *daddr,
134                          __u8 prio, __u8 ttl, __be32 label, __be16 src_port,
135                          __be16 dst_port, bool nocheck);
136 #endif
137
138 static inline void udp_tunnel_gro_complete(struct sk_buff *skb, int nhoff)
139 {
140         struct udphdr *uh;
141
142         uh = (struct udphdr *)(skb->data + nhoff - sizeof(struct udphdr));
143         skb_shinfo(skb)->gso_type |= uh->check ?
144                 SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
145 }
146
147 void ovs_udp_gso(struct sk_buff *skb);
148 void ovs_udp_csum_gso(struct sk_buff *skb);
149
150 static inline int rpl_udp_tunnel_handle_offloads(struct sk_buff *skb,
151                                                  bool udp_csum)
152 {
153         int type = 0;
154
155         void (*fix_segment)(struct sk_buff *);
156
157         if (skb_is_gso(skb) && skb_is_encapsulated(skb)) {
158                 return -ENOSYS;
159         }
160
161         type |= udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
162         if (!udp_csum)
163                 fix_segment = ovs_udp_gso;
164         else
165                 fix_segment = ovs_udp_csum_gso;
166 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
167         /* This functuin is not used by vxlan lan tunnel. On older
168          * udp offload only supports vxlan, therefore fallback to software
169          * segmentation.
170          */
171         type = 0;
172 #endif
173
174         return ovs_iptunnel_handle_offloads(skb, udp_csum, type, fix_segment);
175 }
176
177 #define udp_tunnel_handle_offloads rpl_udp_tunnel_handle_offloads
178 static inline void ovs_udp_tun_rx_dst(struct metadata_dst *md_dst,
179                                       struct sk_buff *skb,
180                                       unsigned short family,
181                                       __be16 flags, __be64 tunnel_id, int md_size)
182 {
183         struct ip_tunnel_info *info = &md_dst->u.tun_info;
184
185         if (family == AF_INET)
186                 ovs_ip_tun_rx_dst(md_dst, skb, flags, tunnel_id, md_size);
187         else
188                 ovs_ipv6_tun_rx_dst(md_dst, skb, flags, tunnel_id, md_size);
189
190         info->key.tp_src = udp_hdr(skb)->source;
191         info->key.tp_dst = udp_hdr(skb)->dest;
192         if (udp_hdr(skb)->check)
193                 info->key.tun_flags |= TUNNEL_CSUM;
194 }
195 #endif /* USE_UPSTREAM_TUNNEL */
196
197 #endif