datapath: Allow GRE64 tunnels without any key.
[cascardo/ovs.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 #include "vport-generic.h"
36
37 /*
38  * The GRE header is composed of a series of sections: a base and then a variable
39  * number of options.
40  */
41 #define GRE_HEADER_SECTION 4
42
43 struct gre_base_hdr {
44         __be16 flags;
45         __be16 protocol;
46 };
47
48 static int gre_hdr_len(const struct tnl_mutable_config *mutable)
49 {
50         int len;
51
52         len = GRE_HEADER_SECTION;
53
54         if (mutable->flags & TNL_F_CSUM)
55                 len += GRE_HEADER_SECTION;
56
57         /* Set key for GRE64 tunnels, even when key if is zero. */
58         if (mutable->out_key ||
59             mutable->key.tunnel_type & TNL_T_PROTO_GRE64 ||
60             mutable->flags & TNL_F_OUT_KEY_ACTION) {
61
62                 len += GRE_HEADER_SECTION;
63                 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
64                         len += GRE_HEADER_SECTION;
65         }
66         return len;
67 }
68
69
70 /* Returns the least-significant 32 bits of a __be64. */
71 static __be32 be64_get_low32(__be64 x)
72 {
73 #ifdef __BIG_ENDIAN
74         return (__force __be32)x;
75 #else
76         return (__force __be32)((__force u64)x >> 32);
77 #endif
78 }
79
80 static __be32 be64_get_high32(__be64 x)
81 {
82 #ifdef __BIG_ENDIAN
83         return (__force __be32)((__force u64)x >> 32);
84 #else
85         return (__force __be32)x;
86 #endif
87 }
88
89 static void gre_build_header(const struct vport *vport,
90                              const struct tnl_mutable_config *mutable,
91                              void *header)
92 {
93         struct gre_base_hdr *greh = header;
94         __be32 *options = (__be32 *)(greh + 1);
95
96         greh->protocol = htons(ETH_P_TEB);
97         greh->flags = 0;
98
99         if (mutable->flags & TNL_F_CSUM) {
100                 greh->flags |= GRE_CSUM;
101                 *options = 0;
102                 options++;
103         }
104
105         if (mutable->flags & TNL_F_OUT_KEY_ACTION) {
106                 greh->flags |= GRE_KEY;
107                 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
108                         greh->flags |= GRE_SEQ;
109
110         } else if (mutable->out_key ||
111                    mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
112                 greh->flags |= GRE_KEY;
113                 *options = be64_get_low32(mutable->out_key);
114                 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
115                         options++;
116                         *options = be64_get_high32(mutable->out_key);
117                         greh->flags |= GRE_SEQ;
118                 }
119         }
120 }
121
122 static struct sk_buff *gre_update_header(const struct vport *vport,
123                                          const struct tnl_mutable_config *mutable,
124                                          struct dst_entry *dst,
125                                          struct sk_buff *skb)
126 {
127         __be32 *options = (__be32 *)(skb_network_header(skb) + mutable->tunnel_hlen
128                                                - GRE_HEADER_SECTION);
129
130         /* Work backwards over the options so the checksum is last. */
131         if (mutable->flags & TNL_F_OUT_KEY_ACTION) {
132                 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
133                         /* Set higher 32 bits to seq. */
134                         *options = be64_get_high32(OVS_CB(skb)->tun_id);
135                         options--;
136                 }
137                 *options = be64_get_low32(OVS_CB(skb)->tun_id);
138                 options--;
139         } else if (mutable->out_key ||
140                    mutable->key.tunnel_type & TNL_T_PROTO_GRE64) {
141                 options--;
142                 if (mutable->key.tunnel_type & TNL_T_PROTO_GRE64)
143                         options--;
144         }
145
146         if (mutable->flags & TNL_F_CSUM)
147                 *(__sum16 *)options = csum_fold(skb_checksum(skb,
148                                                 skb_transport_offset(skb),
149                                                 skb->len - skb_transport_offset(skb),
150                                                 0));
151         /*
152          * Allow our local IP stack to fragment the outer packet even if the
153          * DF bit is set as a last resort.  We also need to force selection of
154          * an IP ID here because Linux will otherwise leave it at 0 if the
155          * packet originally had DF set.
156          */
157         skb->local_df = 1;
158         __ip_select_ident(ip_hdr(skb), dst, 0);
159
160         return skb;
161 }
162
163 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
164 {
165 #ifdef __BIG_ENDIAN
166         return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
167 #else
168         return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
169 #endif
170 }
171
172 static int parse_header(struct iphdr *iph, __be16 *flags, __be64 *tun_id,
173                         u32 *tunnel_type)
174 {
175         /* IP and ICMP protocol handlers check that the IHL is valid. */
176         struct gre_base_hdr *greh = (struct gre_base_hdr *)((u8 *)iph + (iph->ihl << 2));
177         __be32 *options = (__be32 *)(greh + 1);
178         int hdr_len;
179
180         *flags = greh->flags;
181
182         if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
183                 return -EINVAL;
184
185         if (unlikely(greh->protocol != htons(ETH_P_TEB)))
186                 return -EINVAL;
187
188         hdr_len = GRE_HEADER_SECTION;
189
190         if (greh->flags & GRE_CSUM) {
191                 hdr_len += GRE_HEADER_SECTION;
192                 options++;
193         }
194
195         if (greh->flags & GRE_KEY) {
196                 __be32 seq;
197                 __be32 gre_key;
198
199                 gre_key = *options;
200                 hdr_len += GRE_HEADER_SECTION;
201                 options++;
202
203                 if (greh->flags & GRE_SEQ) {
204                         seq = *options;
205                         *tunnel_type = TNL_T_PROTO_GRE64;
206                 } else {
207                         seq = 0;
208                         *tunnel_type = TNL_T_PROTO_GRE;
209                 }
210                 *tun_id = key_to_tunnel_id(gre_key, seq);
211         } else {
212                 *tun_id = 0;
213                 /* Ignore GRE seq if there is no key present. */
214                 *tunnel_type = TNL_T_PROTO_GRE;
215         }
216
217         if (greh->flags & GRE_SEQ)
218                 hdr_len += GRE_HEADER_SECTION;
219
220         return hdr_len;
221 }
222
223 /* Called with rcu_read_lock and BH disabled. */
224 static void gre_err(struct sk_buff *skb, u32 info)
225 {
226         struct vport *vport;
227         const struct tnl_mutable_config *mutable;
228         const int type = icmp_hdr(skb)->type;
229         const int code = icmp_hdr(skb)->code;
230         int mtu = ntohs(icmp_hdr(skb)->un.frag.mtu);
231         u32 tunnel_type;
232
233         struct iphdr *iph;
234         __be16 flags;
235         __be64 key;
236         int tunnel_hdr_len, tot_hdr_len;
237         unsigned int orig_mac_header;
238         unsigned int orig_nw_header;
239
240         if (type != ICMP_DEST_UNREACH || code != ICMP_FRAG_NEEDED)
241                 return;
242
243         /*
244          * The mimimum size packet that we would actually be able to process:
245          * encapsulating IP header, minimum GRE header, Ethernet header,
246          * inner IPv4 header.
247          */
248         if (!pskb_may_pull(skb, sizeof(struct iphdr) + GRE_HEADER_SECTION +
249                                 ETH_HLEN + sizeof(struct iphdr)))
250                 return;
251
252         iph = (struct iphdr *)skb->data;
253         if (ipv4_is_multicast(iph->daddr))
254                 return;
255
256         tunnel_hdr_len = parse_header(iph, &flags, &key, &tunnel_type);
257         if (tunnel_hdr_len < 0)
258                 return;
259
260         vport = ovs_tnl_find_port(dev_net(skb->dev), iph->saddr, iph->daddr, key,
261                                   tunnel_type, &mutable);
262         if (!vport)
263                 return;
264
265         /*
266          * Packets received by this function were previously sent by us, so
267          * any comparisons should be to the output values, not the input.
268          * However, it's not really worth it to have a hash table based on
269          * output keys (especially since ICMP error handling of tunneled packets
270          * isn't that reliable anyways).  Therefore, we do a lookup based on the
271          * out key as if it were the in key and then check to see if the input
272          * and output keys are the same.
273          */
274         if (mutable->key.in_key != mutable->out_key)
275                 return;
276
277         if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
278             !!(mutable->flags & TNL_F_OUT_KEY_ACTION))
279                 return;
280
281         if ((mutable->flags & TNL_F_CSUM) && !(flags & GRE_CSUM))
282                 return;
283
284         tunnel_hdr_len += iph->ihl << 2;
285
286         orig_mac_header = skb_mac_header(skb) - skb->data;
287         orig_nw_header = skb_network_header(skb) - skb->data;
288         skb_set_mac_header(skb, tunnel_hdr_len);
289
290         tot_hdr_len = tunnel_hdr_len + ETH_HLEN;
291
292         skb->protocol = eth_hdr(skb)->h_proto;
293         if (skb->protocol == htons(ETH_P_8021Q)) {
294                 tot_hdr_len += VLAN_HLEN;
295                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
296         }
297
298         skb_set_network_header(skb, tot_hdr_len);
299         mtu -= tot_hdr_len;
300
301         if (skb->protocol == htons(ETH_P_IP))
302                 tot_hdr_len += sizeof(struct iphdr);
303 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
304         else if (skb->protocol == htons(ETH_P_IPV6))
305                 tot_hdr_len += sizeof(struct ipv6hdr);
306 #endif
307         else
308                 goto out;
309
310         if (!pskb_may_pull(skb, tot_hdr_len))
311                 goto out;
312
313         if (skb->protocol == htons(ETH_P_IP)) {
314                 if (mtu < IP_MIN_MTU) {
315                         if (ntohs(ip_hdr(skb)->tot_len) >= IP_MIN_MTU)
316                                 mtu = IP_MIN_MTU;
317                         else
318                                 goto out;
319                 }
320
321         }
322 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
323         else if (skb->protocol == htons(ETH_P_IPV6)) {
324                 if (mtu < IPV6_MIN_MTU) {
325                         unsigned int packet_length = sizeof(struct ipv6hdr) +
326                                               ntohs(ipv6_hdr(skb)->payload_len);
327
328                         if (packet_length >= IPV6_MIN_MTU
329                             || ntohs(ipv6_hdr(skb)->payload_len) == 0)
330                                 mtu = IPV6_MIN_MTU;
331                         else
332                                 goto out;
333                 }
334         }
335 #endif
336
337         __skb_pull(skb, tunnel_hdr_len);
338         ovs_tnl_frag_needed(vport, mutable, skb, mtu, key);
339         __skb_push(skb, tunnel_hdr_len);
340
341 out:
342         skb_set_mac_header(skb, orig_mac_header);
343         skb_set_network_header(skb, orig_nw_header);
344         skb->protocol = htons(ETH_P_IP);
345 }
346
347 static bool check_checksum(struct sk_buff *skb)
348 {
349         struct iphdr *iph = ip_hdr(skb);
350         struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
351         __sum16 csum = 0;
352
353         if (greh->flags & GRE_CSUM) {
354                 switch (skb->ip_summed) {
355                 case CHECKSUM_COMPLETE:
356                         csum = csum_fold(skb->csum);
357
358                         if (!csum)
359                                 break;
360                         /* Fall through. */
361
362                 case CHECKSUM_NONE:
363                         skb->csum = 0;
364                         csum = __skb_checksum_complete(skb);
365                         skb->ip_summed = CHECKSUM_COMPLETE;
366                         break;
367                 }
368         }
369
370         return (csum == 0);
371 }
372
373 /* Called with rcu_read_lock and BH disabled. */
374 static int gre_rcv(struct sk_buff *skb)
375 {
376         struct vport *vport;
377         const struct tnl_mutable_config *mutable;
378         int hdr_len;
379         struct iphdr *iph;
380         __be16 flags;
381         __be64 key;
382         u32 tunnel_type;
383
384         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
385                 goto error;
386         if (unlikely(!check_checksum(skb)))
387                 goto error;
388
389         hdr_len = parse_header(ip_hdr(skb), &flags, &key, &tunnel_type);
390         if (unlikely(hdr_len < 0))
391                 goto error;
392
393         if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
394                 goto error;
395
396         iph = ip_hdr(skb);
397         vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr, key,
398                                   tunnel_type, &mutable);
399         if (unlikely(!vport)) {
400                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
401                 goto error;
402         }
403
404         if (mutable->flags & TNL_F_IN_KEY_MATCH)
405                 OVS_CB(skb)->tun_id = key;
406         else
407                 OVS_CB(skb)->tun_id = 0;
408
409         __skb_pull(skb, hdr_len);
410         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
411
412         ovs_tnl_rcv(vport, skb, iph->tos);
413         return 0;
414
415 error:
416         kfree_skb(skb);
417         return 0;
418 }
419
420 static const struct tnl_ops gre_tnl_ops = {
421         .tunnel_type    = TNL_T_PROTO_GRE,
422         .ipproto        = IPPROTO_GRE,
423         .hdr_len        = gre_hdr_len,
424         .build_header   = gre_build_header,
425         .update_header  = gre_update_header,
426 };
427
428 static struct vport *gre_create(const struct vport_parms *parms)
429 {
430         return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
431 }
432
433 static const struct tnl_ops gre64_tnl_ops = {
434         .tunnel_type    = TNL_T_PROTO_GRE64,
435         .ipproto        = IPPROTO_GRE,
436         .hdr_len        = gre_hdr_len,
437         .build_header   = gre_build_header,
438         .update_header  = gre_update_header,
439 };
440
441 static struct vport *gre_create64(const struct vport_parms *parms)
442 {
443         return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
444 }
445
446 static const struct net_protocol gre_protocol_handlers = {
447         .handler        =       gre_rcv,
448         .err_handler    =       gre_err,
449 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
450         .netns_ok       =       1,
451 #endif
452 };
453
454 static bool inited;
455
456 static int gre_init(void)
457 {
458         int err;
459
460         if (inited)
461                 return 0;
462
463         inited = true;
464         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
465         if (err)
466                 pr_warn("cannot register gre protocol handler\n");
467
468         return err;
469 }
470
471 static void gre_exit(void)
472 {
473         if (!inited)
474                 return;
475
476         inited = false;
477
478         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
479 }
480
481 const struct vport_ops ovs_gre_vport_ops = {
482         .type           = OVS_VPORT_TYPE_GRE,
483         .flags          = VPORT_F_TUN_ID,
484         .init           = gre_init,
485         .exit           = gre_exit,
486         .create         = gre_create,
487         .destroy        = ovs_tnl_destroy,
488         .set_addr       = ovs_tnl_set_addr,
489         .get_name       = ovs_tnl_get_name,
490         .get_addr       = ovs_tnl_get_addr,
491         .get_options    = ovs_tnl_get_options,
492         .set_options    = ovs_tnl_set_options,
493         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
494         .is_running     = ovs_vport_gen_is_running,
495         .get_operstate  = ovs_vport_gen_get_operstate,
496         .send           = ovs_tnl_send,
497 };
498
499 const struct vport_ops ovs_gre64_vport_ops = {
500         .type           = OVS_VPORT_TYPE_GRE64,
501         .flags          = VPORT_F_TUN_ID,
502         .init           = gre_init,
503         .exit           = gre_exit,
504         .create         = gre_create64,
505         .destroy        = ovs_tnl_destroy,
506         .set_addr       = ovs_tnl_set_addr,
507         .get_name       = ovs_tnl_get_name,
508         .get_addr       = ovs_tnl_get_addr,
509         .get_options    = ovs_tnl_get_options,
510         .set_options    = ovs_tnl_set_options,
511         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
512         .is_running     = ovs_vport_gen_is_running,
513         .get_operstate  = ovs_vport_gen_get_operstate,
514         .send           = ovs_tnl_send,
515 };