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