datapath: Remove vport MAC address configuration.
[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 static bool check_checksum(struct sk_buff *skb)
205 {
206         struct iphdr *iph = ip_hdr(skb);
207         struct gre_base_hdr *greh = (struct gre_base_hdr *)(iph + 1);
208         __sum16 csum = 0;
209
210         if (greh->flags & GRE_CSUM) {
211                 switch (skb->ip_summed) {
212                 case CHECKSUM_COMPLETE:
213                         csum = csum_fold(skb->csum);
214
215                         if (!csum)
216                                 break;
217                         /* Fall through. */
218
219                 case CHECKSUM_NONE:
220                         skb->csum = 0;
221                         csum = __skb_checksum_complete(skb);
222                         skb->ip_summed = CHECKSUM_COMPLETE;
223                         break;
224                 }
225         }
226
227         return (csum == 0);
228 }
229
230 static u32 gre_flags_to_tunnel_flags(const struct tnl_mutable_config *mutable,
231                                      __be16 gre_flags, __be64 *key)
232 {
233         u32 tunnel_flags = 0;
234
235         if (gre_flags & GRE_KEY) {
236                 if (mutable->flags & TNL_F_IN_KEY_MATCH ||
237                     !mutable->key.daddr)
238                         tunnel_flags = OVS_TNL_F_KEY;
239                 else
240                         *key = 0;
241         }
242
243         if (gre_flags & GRE_CSUM)
244                 tunnel_flags |= OVS_TNL_F_CSUM;
245
246         return tunnel_flags;
247 }
248
249 /* Called with rcu_read_lock and BH disabled. */
250 static int gre_rcv(struct sk_buff *skb)
251 {
252         struct vport *vport;
253         const struct tnl_mutable_config *mutable;
254         int hdr_len;
255         struct iphdr *iph;
256         struct ovs_key_ipv4_tunnel tun_key;
257         __be16 gre_flags;
258         u32 tnl_flags;
259         __be64 key;
260         u32 tunnel_type;
261
262         if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN)))
263                 goto error;
264         if (unlikely(!check_checksum(skb)))
265                 goto error;
266
267         hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &tunnel_type);
268         if (unlikely(hdr_len < 0))
269                 goto error;
270
271         if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN)))
272                 goto error;
273
274         iph = ip_hdr(skb);
275         vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr, key,
276                                   tunnel_type, &mutable);
277         if (unlikely(!vport)) {
278                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
279                 goto error;
280         }
281
282         tnl_flags = gre_flags_to_tunnel_flags(mutable, gre_flags, &key);
283         tnl_tun_key_init(&tun_key, iph, key, tnl_flags);
284         OVS_CB(skb)->tun_key = &tun_key;
285
286         __skb_pull(skb, hdr_len);
287         skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN);
288
289         ovs_tnl_rcv(vport, skb);
290         return 0;
291
292 error:
293         kfree_skb(skb);
294         return 0;
295 }
296
297 static const struct tnl_ops gre_tnl_ops = {
298         .tunnel_type    = TNL_T_PROTO_GRE,
299         .ipproto        = IPPROTO_GRE,
300         .hdr_len        = gre_hdr_len,
301         .build_header   = gre_build_header,
302 };
303
304 static struct vport *gre_create(const struct vport_parms *parms)
305 {
306         return ovs_tnl_create(parms, &ovs_gre_vport_ops, &gre_tnl_ops);
307 }
308
309 static struct vport *gre_create_ft(const struct vport_parms *parms)
310 {
311         return ovs_tnl_create(parms, &ovs_gre_ft_vport_ops, &gre_tnl_ops);
312 }
313
314 static const struct tnl_ops gre64_tnl_ops = {
315         .tunnel_type    = TNL_T_PROTO_GRE64,
316         .ipproto        = IPPROTO_GRE,
317         .hdr_len        = gre_hdr_len,
318         .build_header   = gre_build_header,
319 };
320
321 static struct vport *gre_create64(const struct vport_parms *parms)
322 {
323         return ovs_tnl_create(parms, &ovs_gre64_vport_ops, &gre64_tnl_ops);
324 }
325
326 static const struct net_protocol gre_protocol_handlers = {
327         .handler        =       gre_rcv,
328 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
329         .netns_ok       =       1,
330 #endif
331 };
332
333 static bool inited;
334
335 static int gre_init(void)
336 {
337         int err;
338
339         if (inited)
340                 return 0;
341
342         inited = true;
343         err = inet_add_protocol(&gre_protocol_handlers, IPPROTO_GRE);
344         if (err)
345                 pr_warn("cannot register gre protocol handler\n");
346
347         return err;
348 }
349
350 static void gre_exit(void)
351 {
352         if (!inited)
353                 return;
354
355         inited = false;
356
357         inet_del_protocol(&gre_protocol_handlers, IPPROTO_GRE);
358 }
359
360 const struct vport_ops ovs_gre_ft_vport_ops = {
361         .type           = OVS_VPORT_TYPE_FT_GRE,
362         .flags          = VPORT_F_TUN_ID,
363         .init           = gre_init,
364         .exit           = gre_exit,
365         .create         = gre_create_ft,
366         .destroy        = ovs_tnl_destroy,
367         .get_name       = ovs_tnl_get_name,
368         .get_options    = ovs_tnl_get_options,
369         .set_options    = ovs_tnl_set_options,
370         .send           = ovs_tnl_send,
371 };
372
373 const struct vport_ops ovs_gre_vport_ops = {
374         .type           = OVS_VPORT_TYPE_GRE,
375         .flags          = VPORT_F_TUN_ID,
376         .init           = gre_init,
377         .exit           = gre_exit,
378         .create         = gre_create,
379         .destroy        = ovs_tnl_destroy,
380         .get_name       = ovs_tnl_get_name,
381         .get_options    = ovs_tnl_get_options,
382         .set_options    = ovs_tnl_set_options,
383         .send           = ovs_tnl_send,
384 };
385
386 const struct vport_ops ovs_gre64_vport_ops = {
387         .type           = OVS_VPORT_TYPE_GRE64,
388         .flags          = VPORT_F_TUN_ID,
389         .init           = gre_init,
390         .exit           = gre_exit,
391         .create         = gre_create64,
392         .destroy        = ovs_tnl_destroy,
393         .get_name       = ovs_tnl_get_name,
394         .get_options    = ovs_tnl_get_options,
395         .set_options    = ovs_tnl_set_options,
396         .send           = ovs_tnl_send,
397 };