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