3c134993086c0eaa5f2edfc2f25186f3f001abb6
[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 #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, __be64 *key)
357 {
358         u32 tunnel_flags = 0;
359
360         if (gre_flags & GRE_KEY) {
361                 if (mutable->flags & TNL_F_IN_KEY_MATCH ||
362                     !mutable->key.daddr)
363                         tunnel_flags = OVS_TNL_F_KEY;
364                 else
365                         *key = 0;
366         }
367
368         if (gre_flags & GRE_CSUM)
369                 tunnel_flags |= OVS_TNL_F_CSUM;
370
371         return tunnel_flags;
372 }
373
374 /* Called with rcu_read_lock and BH disabled. */
375 static int gre_rcv(struct sk_buff *skb)
376 {
377         struct vport *vport;
378         const struct tnl_mutable_config *mutable;
379         int hdr_len;
380         struct iphdr *iph;
381         struct ovs_key_ipv4_tunnel tun_key;
382         __be16 gre_flags;
383         u32 tnl_flags;
384         __be64 key;
385         u32 tunnel_type;
386
387         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
388                 goto error;
389         if (unlikely(!check_checksum(skb)))
390                 goto error;
391
392         hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &tunnel_type);
393         if (unlikely(hdr_len < 0))
394                 goto error;
395
396         if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
397                 goto error;
398
399         iph = ip_hdr(skb);
400         vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr, key,
401                                   tunnel_type, &mutable);
402         if (unlikely(!vport)) {
403                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
404                 goto error;
405         }
406
407         tnl_flags = gre_flags_to_tunnel_flags(mutable, gre_flags, &key);
408         tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
409         OVS_CB(skb)->tun_key = &tun_key;
410
411         __skb_pull(skb, hdr_len);
412         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
413
414         ovs_tnl_rcv(vport, skb);
415         return 0;
416
417 error:
418         kfree_skb(skb);
419         return 0;
420 }
421
422 static const struct tnl_ops gre_tnl_ops = {
423         .tunnel_type    = TNL_T_PROTO_GRE,
424         .ipproto        = IPPROTO_GRE,
425         .hdr_len        = gre_hdr_len,
426         .build_header   = gre_build_header,
427 };
428
429 static struct vport *gre_create(const struct vport_parms *parms)
430 {
431         return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
432 }
433
434 static struct vport *gre_create_ft(const struct vport_parms *parms)
435 {
436         return ovs_tnl_create(parms, &ovs_gre_ft_vport_ops, &gre_tnl_ops);
437 }
438
439 static const struct tnl_ops gre64_tnl_ops = {
440         .tunnel_type    = TNL_T_PROTO_GRE64,
441         .ipproto        = IPPROTO_GRE,
442         .hdr_len        = gre_hdr_len,
443         .build_header   = gre_build_header,
444 };
445
446 static struct vport *gre_create64(const struct vport_parms *parms)
447 {
448         return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
449 }
450
451 static const struct net_protocol gre_protocol_handlers = {
452         .handler        =       gre_rcv,
453         .err_handler    =       gre_err,
454 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
455         .netns_ok       =       1,
456 #endif
457 };
458
459 static bool inited;
460
461 static int gre_init(void)
462 {
463         int err;
464
465         if (inited)
466                 return 0;
467
468         inited = true;
469         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
470         if (err)
471                 pr_warn("cannot register gre protocol handler\n");
472
473         return err;
474 }
475
476 static void gre_exit(void)
477 {
478         if (!inited)
479                 return;
480
481         inited = false;
482
483         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
484 }
485
486 const struct vport_ops ovs_gre_ft_vport_ops = {
487         .type           = OVS_VPORT_TYPE_FT_GRE,
488         .flags          = VPORT_F_TUN_ID,
489         .init           = gre_init,
490         .exit           = gre_exit,
491         .create         = gre_create_ft,
492         .destroy        = ovs_tnl_destroy,
493         .set_addr       = ovs_tnl_set_addr,
494         .get_name       = ovs_tnl_get_name,
495         .get_addr       = ovs_tnl_get_addr,
496         .get_options    = ovs_tnl_get_options,
497         .set_options    = ovs_tnl_set_options,
498         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
499         .is_running     = ovs_vport_gen_is_running,
500         .get_operstate  = ovs_vport_gen_get_operstate,
501         .send           = ovs_tnl_send,
502 };
503
504 const struct vport_ops ovs_gre_vport_ops = {
505         .type           = OVS_VPORT_TYPE_GRE,
506         .flags          = VPORT_F_TUN_ID,
507         .init           = gre_init,
508         .exit           = gre_exit,
509         .create         = gre_create,
510         .destroy        = ovs_tnl_destroy,
511         .set_addr       = ovs_tnl_set_addr,
512         .get_name       = ovs_tnl_get_name,
513         .get_addr       = ovs_tnl_get_addr,
514         .get_options    = ovs_tnl_get_options,
515         .set_options    = ovs_tnl_set_options,
516         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
517         .is_running     = ovs_vport_gen_is_running,
518         .get_operstate  = ovs_vport_gen_get_operstate,
519         .send           = ovs_tnl_send,
520 };
521
522 const struct vport_ops ovs_gre64_vport_ops = {
523         .type           = OVS_VPORT_TYPE_GRE64,
524         .flags          = VPORT_F_TUN_ID,
525         .init           = gre_init,
526         .exit           = gre_exit,
527         .create         = gre_create64,
528         .destroy        = ovs_tnl_destroy,
529         .set_addr       = ovs_tnl_set_addr,
530         .get_name       = ovs_tnl_get_name,
531         .get_addr       = ovs_tnl_get_addr,
532         .get_options    = ovs_tnl_get_options,
533         .set_options    = ovs_tnl_set_options,
534         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
535         .is_running     = ovs_vport_gen_is_running,
536         .get_operstate  = ovs_vport_gen_get_operstate,
537         .send           = ovs_tnl_send,
538 };