datapath: Factor out common code from *_build_header() to ovs_tnl_send().
[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         greh->protocol = htons(ETH_P_TEB);
99         greh->flags = 0;
100
101         /* Work backwards over the options so the checksum is last. */
102         if (tun_key->tun_flags & OVS_TNL_F_KEY || is_gre64) {
103                 greh->flags |= GRE_KEY;
104                 if (is_gre64) {
105                         /* Set higher 32 bits to seq. */
106                         *options = be64_get_high32(tun_key->tun_id);
107                         options--;
108                         greh->flags |= GRE_SEQ;
109                 }
110                 *options = be64_get_low32(tun_key->tun_id);
111                 options--;
112         }
113
114         if (tun_key->tun_flags & OVS_TNL_F_CSUM) {
115                 greh->flags |= GRE_CSUM;
116                 *options = 0;
117                 *(__sum16 *)options = csum_fold(skb_checksum(skb,
118                                                 skb_transport_offset(skb),
119                                                 skb->len - skb_transport_offset(skb),
120                                                 0));
121         }
122 }
123
124 static void gre_build_header(const struct vport *vport,
125                              struct sk_buff *skb,
126                              int tunnel_hlen)
127 {
128         __gre_build_header(skb, tunnel_hlen, false);
129 }
130
131 static void gre64_build_header(const struct vport *vport,
132                                struct sk_buff *skb,
133                                int tunnel_hlen)
134 {
135         __gre_build_header(skb, tunnel_hlen, true);
136 }
137
138 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
139 {
140 #ifdef __BIG_ENDIAN
141         return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
142 #else
143         return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
144 #endif
145 }
146
147 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *tun_id,
148                         bool *is_gre64)
149 {
150         /* IP and ICMP protocol handlers check that the IHL is valid. */
151         struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
152         __be32 *options = (__be32 *)(greh + 1);
153         int hdr_len;
154
155         *flags = greh->flags;
156
157         if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
158                 return -EINVAL;
159
160         if (unlikely(greh->protocol != htons(ETH_P_TEB)))
161                 return -EINVAL;
162
163         hdr_len = GRE_HEADER_SECTION;
164
165         if (greh->flags & GRE_CSUM) {
166                 hdr_len += GRE_HEADER_SECTION;
167                 options++;
168         }
169
170         if (greh->flags & GRE_KEY) {
171                 __be32 seq;
172                 __be32 gre_key;
173
174                 gre_key = *options;
175                 hdr_len += GRE_HEADER_SECTION;
176                 options++;
177
178                 if (greh->flags & GRE_SEQ) {
179                         seq = *options;
180                         *is_gre64 = true;
181                 } else {
182                         seq = 0;
183                         *is_gre64 = false;
184                 }
185                 *tun_id = key_to_tunnel_id(gre_key, seq);
186         } else {
187                 *tun_id = 0;
188                 /* Ignore GRE seq if there is no key present. */
189                 *is_gre64 = false;
190         }
191
192         if (greh->flags & GRE_SEQ)
193                 hdr_len += GRE_HEADER_SECTION;
194
195         return hdr_len;
196 }
197
198 static bool check_checksum(struct sk_buff *skb)
199 {
200         struct iphdr *iph = ip_hdr(skb);
201         struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
202         __sum16 csum = 0;
203
204         if (greh->flags & GRE_CSUM) {
205                 switch (skb->ip_summed) {
206                 case CHECKSUM_COMPLETE:
207                         csum = csum_fold(skb->csum);
208
209                         if (!csum)
210                                 break;
211                         /* Fall through. */
212
213                 case CHECKSUM_NONE:
214                         skb->csum = 0;
215                         csum = __skb_checksum_complete(skb);
216                         skb->ip_summed = CHECKSUM_COMPLETE;
217                         break;
218                 }
219         }
220
221         return (csum == 0);
222 }
223
224 static u32 gre_flags_to_tunnel_flags(__be16 gre_flags, bool is_gre64)
225 {
226         u32 tunnel_flags = 0;
227
228         if (gre_flags & GRE_KEY || is_gre64)
229                 tunnel_flags = OVS_TNL_F_KEY;
230
231         if (gre_flags & GRE_CSUM)
232                 tunnel_flags |= OVS_TNL_F_CSUM;
233
234         return tunnel_flags;
235 }
236
237 /* Called with rcu_read_lock and BH disabled. */
238 static int gre_rcv(struct sk_buff *skb)
239 {
240         struct ovs_net *ovs_net;
241         struct vport *vport;
242         int hdr_len;
243         struct iphdr *iph;
244         struct ovs_key_ipv4_tunnel tun_key;
245         __be16 gre_flags;
246         u32 tnl_flags;
247         __be64 key;
248         bool is_gre64;
249
250         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
251                 goto error;
252         if (unlikely(!check_checksum(skb)))
253                 goto error;
254
255         hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &is_gre64);
256         if (unlikely(hdr_len < 0))
257                 goto error;
258
259         ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
260         if (is_gre64)
261                 vport = rcu_dereference(ovs_net->vport_net.gre64_vport);
262         else
263                 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
264         if (unlikely(!vport))
265                 goto error;
266
267         if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
268                 goto error;
269
270         iph = ip_hdr(skb);
271         tnl_flags = gre_flags_to_tunnel_flags(gre_flags, is_gre64);
272         tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
273         OVS_CB(skb)->tun_key = &tun_key;
274
275         __skb_pull(skb, hdr_len);
276         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
277
278         ovs_tnl_rcv(vport, skb);
279         return 0;
280
281 error:
282         kfree_skb(skb);
283         return 0;
284 }
285
286 static const struct net_protocol gre_protocol_handlers = {
287         .handler        =       gre_rcv,
288 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
289         .netns_ok       =       1,
290 #endif
291 };
292
293 static bool inited;
294
295 static int gre_init(void)
296 {
297         int err;
298
299         if (inited)
300                 return 0;
301
302         inited = true;
303         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
304         if (err)
305                 pr_warn("cannot register gre protocol handler\n");
306
307         return err;
308 }
309
310 static void gre_exit(void)
311 {
312         if (!inited)
313                 return;
314
315         inited = false;
316
317         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
318 }
319
320 /* GRE vport. */
321 static const struct tnl_ops gre_tnl_ops = {
322         .ipproto        = IPPROTO_GRE,
323         .hdr_len        = gre_hdr_len,
324         .build_header   = gre_build_header,
325 };
326
327 static struct vport *gre_create(const struct vport_parms *parms)
328 {
329         struct net *net = ovs_dp_get_net(parms->dp);
330         struct ovs_net *ovs_net;
331         struct vport *vport;
332
333         ovs_net = net_generic(net, ovs_net_id);
334         if (rtnl_dereference(ovs_net->vport_net.gre_vport))
335                 return ERR_PTR(-EEXIST);
336
337         vport = ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
338
339         rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
340         return vport;
341 }
342
343 static void gre_tnl_destroy(struct vport *vport)
344 {
345         struct net *net = ovs_dp_get_net(vport->dp);
346         struct ovs_net *ovs_net;
347
348         ovs_net = net_generic(net, ovs_net_id);
349
350         rcu_assign_pointer(ovs_net->vport_net.gre_vport, NULL);
351         ovs_tnl_destroy(vport);
352 }
353
354 const struct vport_ops ovs_gre_vport_ops = {
355         .type           = OVS_VPORT_TYPE_GRE,
356         .flags          = VPORT_F_TUN_ID,
357         .init           = gre_init,
358         .exit           = gre_exit,
359         .create         = gre_create,
360         .destroy        = gre_tnl_destroy,
361         .get_name       = ovs_tnl_get_name,
362         .send           = ovs_tnl_send,
363 };
364
365 /* GRE64 vport. */
366 static const struct tnl_ops gre64_tnl_ops = {
367         .ipproto        = IPPROTO_GRE,
368         .hdr_len        = gre64_hdr_len,
369         .build_header   = gre64_build_header,
370 };
371
372 static struct vport *gre64_create(const struct vport_parms *parms)
373 {
374         struct net *net = ovs_dp_get_net(parms->dp);
375         struct ovs_net *ovs_net;
376         struct vport *vport;
377
378         ovs_net = net_generic(net, ovs_net_id);
379         if (rtnl_dereference(ovs_net->vport_net.gre64_vport))
380                 return ERR_PTR(-EEXIST);
381
382         vport = ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
383
384         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, vport);
385         return vport;
386 }
387
388
389 static void gre64_tnl_destroy(struct vport *vport)
390 {
391         struct net *net = ovs_dp_get_net(vport->dp);
392         struct ovs_net *ovs_net;
393
394         ovs_net = net_generic(net, ovs_net_id);
395
396         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, NULL);
397         ovs_tnl_destroy(vport);
398 }
399
400 const struct vport_ops ovs_gre64_vport_ops = {
401         .type           = OVS_VPORT_TYPE_GRE64,
402         .flags          = VPORT_F_TUN_ID,
403         .init           = gre_init,
404         .exit           = gre_exit,
405         .create         = gre64_create,
406         .destroy        = gre64_tnl_destroy,
407         .get_name       = ovs_tnl_get_name,
408         .send           = ovs_tnl_send,
409 };