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