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