c252dcfdc0d809028df5118afa72947d973d5fcc
[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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/if.h>
22 #include <linux/skbuff.h>
23 #include <linux/ip.h>
24 #include <linux/if_tunnel.h>
25 #include <linux/if_vlan.h>
26 #include <linux/in.h>
27
28 #include <net/icmp.h>
29 #include <net/ip.h>
30 #include <net/protocol.h>
31
32 #include "datapath.h"
33 #include "tunnel.h"
34 #include "vport.h"
35
36 /*
37  * The GRE header is composed of a series of sections: a base and then a variable
38  * number of options.
39  */
40 #define GRE_HEADER_SECTION 4
41
42 struct gre_base_hdr {
43         __be16 flags;
44         __be16 protocol;
45 };
46
47 static int gre_hdr_len(const struct ovs_key_ipv4_tunnel *tun_key)
48 {
49         int len = GRE_HEADER_SECTION;
50
51         if (tun_key->tun_flags & OVS_TNL_F_KEY)
52                 len += GRE_HEADER_SECTION;
53         if (tun_key->tun_flags & OVS_TNL_F_CSUM)
54                 len += GRE_HEADER_SECTION;
55         return len;
56 }
57
58 static int gre64_hdr_len(const struct ovs_key_ipv4_tunnel *tun_key)
59 {
60         /* Set key for GRE64 tunnels, even when key if is zero. */
61         int len = GRE_HEADER_SECTION +          /* GRE Hdr */
62                   GRE_HEADER_SECTION +          /* GRE Key */
63                   GRE_HEADER_SECTION;           /* GRE SEQ */
64
65         if (tun_key->tun_flags & OVS_TNL_F_CSUM)
66                 len += GRE_HEADER_SECTION;
67
68         return len;
69 }
70
71 /* Returns the least-significant 32 bits of a __be64. */
72 static __be32 be64_get_low32(__be64 x)
73 {
74 #ifdef __BIG_ENDIAN
75         return (__force __be32)x;
76 #else
77         return (__force __be32)((__force u64)x >> 32);
78 #endif
79 }
80
81 static __be32 be64_get_high32(__be64 x)
82 {
83 #ifdef __BIG_ENDIAN
84         return (__force __be32)((__force u64)x >> 32);
85 #else
86         return (__force __be32)x;
87 #endif
88 }
89
90 static void __gre_build_header(struct sk_buff *skb,
91                                int tunnel_hlen,
92                                bool is_gre64)
93 {
94         const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
95         __be32 *options = (__be32 *)(skb_network_header(skb) + tunnel_hlen
96                         - GRE_HEADER_SECTION);
97         struct gre_base_hdr *greh = (struct gre_base_hdr *) skb_transport_header(skb);
98         struct dst_entry *dst = skb_dst(skb);
99
100         greh->protocol = htons(ETH_P_TEB);
101         greh->flags = 0;
102
103         /* Work backwards over the options so the checksum is last. */
104         if (tun_key->tun_flags & OVS_TNL_F_KEY || is_gre64) {
105                 greh->flags |= GRE_KEY;
106                 if (is_gre64) {
107                         /* Set higher 32 bits to seq. */
108                         *options = be64_get_high32(tun_key->tun_id);
109                         options--;
110                         greh->flags |= GRE_SEQ;
111                 }
112                 *options = be64_get_low32(tun_key->tun_id);
113                 options--;
114         }
115
116         if (tun_key->tun_flags & OVS_TNL_F_CSUM) {
117                 greh->flags |= GRE_CSUM;
118                 *options = 0;
119                 *(__sum16 *)options = csum_fold(skb_checksum(skb,
120                                                 skb_transport_offset(skb),
121                                                 skb->len - skb_transport_offset(skb),
122                                                 0));
123         }
124         /*
125          * Allow our local IP stack to fragment the outer packet even if the
126          * DF bit is set as a last resort.  We also need to force selection of
127          * an IP ID here because Linux will otherwise leave it at 0 if the
128          * packet originally had DF set.
129          */
130         skb->local_df = 1;
131         __ip_select_ident(ip_hdr(skb), dst, 0);
132 }
133
134 static void gre_build_header(const struct vport *vport,
135                              struct sk_buff *skb,
136                              int tunnel_hlen)
137 {
138         __gre_build_header(skb, tunnel_hlen, false);
139 }
140
141 static void gre64_build_header(const struct vport *vport,
142                                struct sk_buff *skb,
143                                int tunnel_hlen)
144 {
145         __gre_build_header(skb, tunnel_hlen, true);
146 }
147
148 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
149 {
150 #ifdef __BIG_ENDIAN
151         return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
152 #else
153         return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
154 #endif
155 }
156
157 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *tun_id,
158                         bool *is_gre64)
159 {
160         /* IP and ICMP protocol handlers check that the IHL is valid. */
161         struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
162         __be32 *options = (__be32 *)(greh + 1);
163         int hdr_len;
164
165         *flags = greh->flags;
166
167         if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
168                 return -EINVAL;
169
170         if (unlikely(greh->protocol != htons(ETH_P_TEB)))
171                 return -EINVAL;
172
173         hdr_len = GRE_HEADER_SECTION;
174
175         if (greh->flags & GRE_CSUM) {
176                 hdr_len += GRE_HEADER_SECTION;
177                 options++;
178         }
179
180         if (greh->flags & GRE_KEY) {
181                 __be32 seq;
182                 __be32 gre_key;
183
184                 gre_key = *options;
185                 hdr_len += GRE_HEADER_SECTION;
186                 options++;
187
188                 if (greh->flags & GRE_SEQ) {
189                         seq = *options;
190                         *is_gre64 = true;
191                 } else {
192                         seq = 0;
193                         *is_gre64 = false;
194                 }
195                 *tun_id = key_to_tunnel_id(gre_key, seq);
196         } else {
197                 *tun_id = 0;
198                 /* Ignore GRE seq if there is no key present. */
199                 *is_gre64 = false;
200         }
201
202         if (greh->flags & GRE_SEQ)
203                 hdr_len += GRE_HEADER_SECTION;
204
205         return hdr_len;
206 }
207
208 static bool check_checksum(struct sk_buff *skb)
209 {
210         struct iphdr *iph = ip_hdr(skb);
211         struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
212         __sum16 csum = 0;
213
214         if (greh->flags & GRE_CSUM) {
215                 switch (skb->ip_summed) {
216                 case CHECKSUM_COMPLETE:
217                         csum = csum_fold(skb->csum);
218
219                         if (!csum)
220                                 break;
221                         /* Fall through. */
222
223                 case CHECKSUM_NONE:
224                         skb->csum = 0;
225                         csum = __skb_checksum_complete(skb);
226                         skb->ip_summed = CHECKSUM_COMPLETE;
227                         break;
228                 }
229         }
230
231         return (csum == 0);
232 }
233
234 static u32 gre_flags_to_tunnel_flags(__be16 gre_flags, bool is_gre64)
235 {
236         u32 tunnel_flags = 0;
237
238         if (gre_flags & GRE_KEY || is_gre64)
239                 tunnel_flags = OVS_TNL_F_KEY;
240
241         if (gre_flags & GRE_CSUM)
242                 tunnel_flags |= OVS_TNL_F_CSUM;
243
244         return tunnel_flags;
245 }
246
247 /* Called with rcu_read_lock and BH disabled. */
248 static int gre_rcv(struct sk_buff *skb)
249 {
250         struct ovs_net *ovs_net;
251         struct vport *vport;
252         int hdr_len;
253         struct iphdr *iph;
254         struct ovs_key_ipv4_tunnel tun_key;
255         __be16 gre_flags;
256         u32 tnl_flags;
257         __be64 key;
258         bool is_gre64;
259
260         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
261                 goto error;
262         if (unlikely(!check_checksum(skb)))
263                 goto error;
264
265         hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &is_gre64);
266         if (unlikely(hdr_len < 0))
267                 goto error;
268
269         ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
270         if (is_gre64)
271                 vport = rcu_dereference(ovs_net->vport_net.gre64_vport);
272         else
273                 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
274         if (unlikely(!vport))
275                 goto error;
276
277         if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
278                 goto error;
279
280         iph = ip_hdr(skb);
281         tnl_flags = gre_flags_to_tunnel_flags(gre_flags, is_gre64);
282         tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
283         OVS_CB(skb)->tun_key = &tun_key;
284
285         __skb_pull(skb, hdr_len);
286         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
287
288         ovs_tnl_rcv(vport, skb);
289         return 0;
290
291 error:
292         kfree_skb(skb);
293         return 0;
294 }
295
296 static const struct net_protocol gre_protocol_handlers = {
297         .handler        =       gre_rcv,
298 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
299         .netns_ok       =       1,
300 #endif
301 };
302
303 static bool inited;
304
305 static int gre_init(void)
306 {
307         int err;
308
309         if (inited)
310                 return 0;
311
312         inited = true;
313         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
314         if (err)
315                 pr_warn("cannot register gre protocol handler\n");
316
317         return err;
318 }
319
320 static void gre_exit(void)
321 {
322         if (!inited)
323                 return;
324
325         inited = false;
326
327         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
328 }
329
330 /* GRE vport. */
331 static const struct tnl_ops gre_tnl_ops = {
332         .ipproto        = IPPROTO_GRE,
333         .hdr_len        = gre_hdr_len,
334         .build_header   = gre_build_header,
335 };
336
337 static struct vport *gre_create(const struct vport_parms *parms)
338 {
339         struct net *net = ovs_dp_get_net(parms->dp);
340         struct ovs_net *ovs_net;
341         struct vport *vport;
342
343         ovs_net = net_generic(net, ovs_net_id);
344         if (rtnl_dereference(ovs_net->vport_net.gre_vport))
345                 return ERR_PTR(-EEXIST);
346
347         vport = ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
348
349         rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
350         return vport;
351 }
352
353 static void gre_tnl_destroy(struct vport *vport)
354 {
355         struct net *net = ovs_dp_get_net(vport->dp);
356         struct ovs_net *ovs_net;
357
358         ovs_net = net_generic(net, ovs_net_id);
359
360         rcu_assign_pointer(ovs_net->vport_net.gre_vport, NULL);
361         ovs_tnl_destroy(vport);
362 }
363
364 const struct vport_ops ovs_gre_vport_ops = {
365         .type           = OVS_VPORT_TYPE_GRE,
366         .flags          = VPORT_F_TUN_ID,
367         .init           = gre_init,
368         .exit           = gre_exit,
369         .create         = gre_create,
370         .destroy        = gre_tnl_destroy,
371         .get_name       = ovs_tnl_get_name,
372         .send           = ovs_tnl_send,
373 };
374
375 /* GRE64 vport. */
376 static const struct tnl_ops gre64_tnl_ops = {
377         .ipproto        = IPPROTO_GRE,
378         .hdr_len        = gre64_hdr_len,
379         .build_header   = gre64_build_header,
380 };
381
382 static struct vport *gre64_create(const struct vport_parms *parms)
383 {
384         struct net *net = ovs_dp_get_net(parms->dp);
385         struct ovs_net *ovs_net;
386         struct vport *vport;
387
388         ovs_net = net_generic(net, ovs_net_id);
389         if (rtnl_dereference(ovs_net->vport_net.gre64_vport))
390                 return ERR_PTR(-EEXIST);
391
392         vport = ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
393
394         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, vport);
395         return vport;
396 }
397
398
399 static void gre64_tnl_destroy(struct vport *vport)
400 {
401         struct net *net = ovs_dp_get_net(vport->dp);
402         struct ovs_net *ovs_net;
403
404         ovs_net = net_generic(net, ovs_net_id);
405
406         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, NULL);
407         ovs_tnl_destroy(vport);
408 }
409
410 const struct vport_ops ovs_gre64_vport_ops = {
411         .type           = OVS_VPORT_TYPE_GRE64,
412         .flags          = VPORT_F_TUN_ID,
413         .init           = gre_init,
414         .exit           = gre_exit,
415         .create         = gre64_create,
416         .destroy        = gre64_tnl_destroy,
417         .get_name       = ovs_tnl_get_name,
418         .send           = ovs_tnl_send,
419 };