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