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