ofproto: Convert units correctly in ofport_open().
[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                        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                 goto error;
404
405         tnl_flags = gre_flags_to_tunnel_flags(mutable, gre_flags, &key);
406         tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
407         OVS_CB(skb)->tun_key = &tun_key;
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);
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 };
426
427 static struct vport *gre_create(const struct vport_parms *parms)
428 {
429         return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
430 }
431
432 static struct vport *gre_create_ft(const struct vport_parms *parms)
433 {
434         return ovs_tnl_create(parms, &ovs_gre_ft_vport_ops, &gre_tnl_ops);
435 }
436
437 static const struct tnl_ops gre64_tnl_ops = {
438         .tunnel_type    = TNL_T_PROTO_GRE64,
439         .ipproto        = IPPROTO_GRE,
440         .hdr_len        = gre_hdr_len,
441         .build_header   = gre_build_header,
442 };
443
444 static struct vport *gre_create64(const struct vport_parms *parms)
445 {
446         return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
447 }
448
449 static const struct net_protocol gre_protocol_handlers = {
450         .handler        =       gre_rcv,
451         .err_handler    =       gre_err,
452 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
453         .netns_ok       =       1,
454 #endif
455 };
456
457 static bool inited;
458
459 static int gre_init(void)
460 {
461         int err;
462
463         if (inited)
464                 return 0;
465
466         inited = true;
467         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
468         if (err)
469                 pr_warn("cannot register gre protocol handler\n");
470
471         return err;
472 }
473
474 static void gre_exit(void)
475 {
476         if (!inited)
477                 return;
478
479         inited = false;
480
481         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
482 }
483
484 const struct vport_ops ovs_gre_ft_vport_ops = {
485         .type           = OVS_VPORT_TYPE_FT_GRE,
486         .flags          = VPORT_F_TUN_ID,
487         .init           = gre_init,
488         .exit           = gre_exit,
489         .create         = gre_create_ft,
490         .destroy        = ovs_tnl_destroy,
491         .set_addr       = ovs_tnl_set_addr,
492         .get_name       = ovs_tnl_get_name,
493         .get_addr       = ovs_tnl_get_addr,
494         .get_options    = ovs_tnl_get_options,
495         .set_options    = ovs_tnl_set_options,
496         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
497         .is_running     = ovs_vport_gen_is_running,
498         .get_operstate  = ovs_vport_gen_get_operstate,
499         .send           = ovs_tnl_send,
500 };
501
502 const struct vport_ops ovs_gre_vport_ops = {
503         .type           = OVS_VPORT_TYPE_GRE,
504         .flags          = VPORT_F_TUN_ID,
505         .init           = gre_init,
506         .exit           = gre_exit,
507         .create         = gre_create,
508         .destroy        = ovs_tnl_destroy,
509         .set_addr       = ovs_tnl_set_addr,
510         .get_name       = ovs_tnl_get_name,
511         .get_addr       = ovs_tnl_get_addr,
512         .get_options    = ovs_tnl_get_options,
513         .set_options    = ovs_tnl_set_options,
514         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
515         .is_running     = ovs_vport_gen_is_running,
516         .get_operstate  = ovs_vport_gen_get_operstate,
517         .send           = ovs_tnl_send,
518 };
519
520 const struct vport_ops ovs_gre64_vport_ops = {
521         .type           = OVS_VPORT_TYPE_GRE64,
522         .flags          = VPORT_F_TUN_ID,
523         .init           = gre_init,
524         .exit           = gre_exit,
525         .create         = gre_create64,
526         .destroy        = ovs_tnl_destroy,
527         .set_addr       = ovs_tnl_set_addr,
528         .get_name       = ovs_tnl_get_name,
529         .get_addr       = ovs_tnl_get_addr,
530         .get_options    = ovs_tnl_get_options,
531         .set_options    = ovs_tnl_set_options,
532         .get_dev_flags  = ovs_vport_gen_get_dev_flags,
533         .is_running     = ovs_vport_gen_is_running,
534         .get_operstate  = ovs_vport_gen_get_operstate,
535         .send           = ovs_tnl_send,
536 };