datapath: Remove checksum compat support
[sliver-openvswitch.git] / datapath / vport-gre.c
1 /*
2  * Copyright (c) 2007-2012 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include <linux/kconfig.h>
20 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/if.h>
24 #include <linux/skbuff.h>
25 #include <linux/ip.h>
26 #include <linux/if_tunnel.h>
27 #include <linux/if_vlan.h>
28 #include <linux/in.h>
29 #include <linux/if_vlan.h>
30 #include <linux/in.h>
31 #include <linux/in_route.h>
32 #include <linux/inetdevice.h>
33 #include <linux/jhash.h>
34 #include <linux/list.h>
35 #include <linux/kernel.h>
36 #include <linux/workqueue.h>
37 #include <linux/rculist.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
40 #include <net/route.h>
41 #include <net/xfrm.h>
42
43 #include <net/icmp.h>
44 #include <net/ip.h>
45 #include <net/ip_tunnels.h>
46 #include <net/gre.h>
47 #include <net/protocol.h>
48
49 #include "datapath.h"
50 #include "vport.h"
51
52 /* Returns the least-significant 32 bits of a __be64. */
53 static __be32 be64_get_low32(__be64 x)
54 {
55 #ifdef __BIG_ENDIAN
56         return (__force __be32)x;
57 #else
58         return (__force __be32)((__force u64)x >> 32);
59 #endif
60 }
61
62 static __be16 filter_tnl_flags(__be16 flags)
63 {
64         return flags & (TUNNEL_CSUM | TUNNEL_KEY);
65 }
66
67 static struct sk_buff *__build_header(struct sk_buff *skb,
68                                       int tunnel_hlen,
69                                       __be32 seq, __be16 gre64_flag)
70 {
71         const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
72         struct tnl_ptk_info tpi;
73
74         skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
75         if (IS_ERR(skb))
76                 return NULL;
77
78         tpi.flags = filter_tnl_flags(tun_key->tun_flags) | gre64_flag;
79
80         tpi.proto = htons(ETH_P_TEB);
81         tpi.key = be64_get_low32(tun_key->tun_id);
82         tpi.seq = seq;
83         gre_build_header(skb, &tpi, tunnel_hlen);
84
85         return skb;
86 }
87
88 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
89 {
90 #ifdef __BIG_ENDIAN
91         return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
92 #else
93         return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
94 #endif
95 }
96
97 /* Called with rcu_read_lock and BH disabled. */
98 static int gre_rcv(struct sk_buff *skb,
99                    const struct tnl_ptk_info *tpi)
100 {
101         struct ovs_key_ipv4_tunnel tun_key;
102         struct ovs_net *ovs_net;
103         struct vport *vport;
104         __be64 key;
105
106         ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
107         if ((tpi->flags & TUNNEL_KEY) && (tpi->flags & TUNNEL_SEQ))
108                 vport = rcu_dereference(ovs_net->vport_net.gre64_vport);
109         else
110                 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
111         if (unlikely(!vport))
112                 return PACKET_REJECT;
113
114         key = key_to_tunnel_id(tpi->key, tpi->seq);
115         ovs_flow_tun_key_init(&tun_key, ip_hdr(skb), key, filter_tnl_flags(tpi->flags));
116
117         ovs_vport_receive(vport, skb, &tun_key);
118         return PACKET_RCVD;
119 }
120
121 static int __send(struct vport *vport, struct sk_buff *skb,
122                   int tunnel_hlen,
123                   __be32 seq, __be16 gre64_flag)
124 {
125         struct net *net = ovs_dp_get_net(vport->dp);
126         struct rtable *rt;
127         int min_headroom;
128         __be16 df;
129         __be32 saddr;
130         int err;
131
132         /* Route lookup */
133         saddr = OVS_CB(skb)->tun_key->ipv4_src;
134         rt = find_route(ovs_dp_get_net(vport->dp),
135                         &saddr,
136                         OVS_CB(skb)->tun_key->ipv4_dst,
137                         IPPROTO_GRE,
138                         OVS_CB(skb)->tun_key->ipv4_tos,
139                         skb->mark);
140         if (IS_ERR(rt)) {
141                 err = PTR_ERR(rt);
142                 goto error;
143         }
144
145         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
146                         + tunnel_hlen + sizeof(struct iphdr)
147                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
148
149         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
150                 int head_delta = SKB_DATA_ALIGN(min_headroom -
151                                                 skb_headroom(skb) +
152                                                 16);
153                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
154                                         0, GFP_ATOMIC);
155                 if (unlikely(err))
156                         goto err_free_rt;
157         }
158
159         if (unlikely(vlan_deaccel_tag(skb))) {
160                 err = -ENOMEM;
161                 goto err_free_rt;
162         }
163
164         /* Push Tunnel header. */
165         skb = __build_header(skb, tunnel_hlen, seq, gre64_flag);
166         if (unlikely(!skb)) {
167                 err = 0;
168                 goto err_free_rt;
169         }
170
171         df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
172                 htons(IP_DF) : 0;
173
174         skb->local_df = 1;
175
176         return iptunnel_xmit(net, rt, skb, saddr,
177                              OVS_CB(skb)->tun_key->ipv4_dst, IPPROTO_GRE,
178                              OVS_CB(skb)->tun_key->ipv4_tos,
179                              OVS_CB(skb)->tun_key->ipv4_ttl, df);
180 err_free_rt:
181         ip_rt_put(rt);
182 error:
183         return err;
184 }
185
186 static struct gre_cisco_protocol gre_protocol = {
187         .handler        = gre_rcv,
188         .priority       = 1,
189 };
190
191 static int gre_ports;
192 static int gre_init(void)
193 {
194         int err;
195
196         gre_ports++;
197         if (gre_ports > 1)
198                 return 0;
199
200         err = gre_cisco_register(&gre_protocol);
201         if (err)
202                 pr_warn("cannot register gre protocol handler\n");
203
204         return err;
205 }
206
207 static void gre_exit(void)
208 {
209         gre_ports--;
210         if (gre_ports > 0)
211                 return;
212
213         gre_cisco_unregister(&gre_protocol);
214 }
215
216 static const char *gre_get_name(const struct vport *vport)
217 {
218         return vport_priv(vport);
219 }
220
221 static struct vport *gre_create(const struct vport_parms *parms)
222 {
223         struct net *net = ovs_dp_get_net(parms->dp);
224         struct ovs_net *ovs_net;
225         struct vport *vport;
226         int err;
227
228         err = gre_init();
229         if (err)
230                 return ERR_PTR(err);
231
232         ovs_net = net_generic(net, ovs_net_id);
233         if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
234                 vport = ERR_PTR(-EEXIST);
235                 goto error;
236         }
237
238         vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
239         if (IS_ERR(vport))
240                 goto error;
241
242         strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
243         rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
244         return vport;
245
246 error:
247         gre_exit();
248         return vport;
249 }
250
251 static void gre_tnl_destroy(struct vport *vport)
252 {
253         struct net *net = ovs_dp_get_net(vport->dp);
254         struct ovs_net *ovs_net;
255
256         ovs_net = net_generic(net, ovs_net_id);
257
258         rcu_assign_pointer(ovs_net->vport_net.gre_vport, NULL);
259         ovs_vport_deferred_free(vport);
260         gre_exit();
261 }
262
263 static int gre_send(struct vport *vport, struct sk_buff *skb)
264 {
265         int hlen;
266
267         if (unlikely(!OVS_CB(skb)->tun_key))
268                 return -EINVAL;
269
270         hlen = ip_gre_calc_hlen(OVS_CB(skb)->tun_key->tun_flags);
271
272         return __send(vport, skb, hlen, 0, 0);
273 }
274
275 const struct vport_ops ovs_gre_vport_ops = {
276         .type           = OVS_VPORT_TYPE_GRE,
277         .create         = gre_create,
278         .destroy        = gre_tnl_destroy,
279         .get_name       = gre_get_name,
280         .send           = gre_send,
281 };
282
283 /* GRE64 vport. */
284 static struct vport *gre64_create(const struct vport_parms *parms)
285 {
286         struct net *net = ovs_dp_get_net(parms->dp);
287         struct ovs_net *ovs_net;
288         struct vport *vport;
289         int err;
290
291         err = gre_init();
292         if (err)
293                 return ERR_PTR(err);
294
295         ovs_net = net_generic(net, ovs_net_id);
296         if (ovsl_dereference(ovs_net->vport_net.gre64_vport)) {
297                 vport = ERR_PTR(-EEXIST);
298                 goto error;
299         }
300
301         vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre64_vport_ops, parms);
302         if (IS_ERR(vport))
303                 goto error;
304
305         strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
306         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, vport);
307         return vport;
308 error:
309         gre_exit();
310         return vport;
311 }
312
313 static void gre64_tnl_destroy(struct vport *vport)
314 {
315         struct net *net = ovs_dp_get_net(vport->dp);
316         struct ovs_net *ovs_net;
317
318         ovs_net = net_generic(net, ovs_net_id);
319
320         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, NULL);
321         ovs_vport_deferred_free(vport);
322         gre_exit();
323 }
324
325 static __be32 be64_get_high32(__be64 x)
326 {
327 #ifdef __BIG_ENDIAN
328         return (__force __be32)((__force u64)x >> 32);
329 #else
330         return (__force __be32)x;
331 #endif
332 }
333
334 static int gre64_send(struct vport *vport, struct sk_buff *skb)
335 {
336         int hlen = GRE_HEADER_SECTION +         /* GRE Hdr */
337                    GRE_HEADER_SECTION +         /* GRE Key */
338                    GRE_HEADER_SECTION;          /* GRE SEQ */
339         __be32 seq;
340
341         if (unlikely(!OVS_CB(skb)->tun_key))
342                 return -EINVAL;
343
344         if (OVS_CB(skb)->tun_key->tun_flags & TUNNEL_CSUM)
345                 hlen += GRE_HEADER_SECTION;
346
347         seq = be64_get_high32(OVS_CB(skb)->tun_key->tun_id);
348         return __send(vport, skb, hlen, seq, (TUNNEL_KEY|TUNNEL_SEQ));
349 }
350
351 const struct vport_ops ovs_gre64_vport_ops = {
352         .type           = OVS_VPORT_TYPE_GRE64,
353         .create         = gre64_create,
354         .destroy        = gre64_tnl_destroy,
355         .get_name       = gre_get_name,
356         .send           = gre64_send,
357 };
358 #endif