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