tunnel: Mark GRE64 tunnel protocol deprecated.
[cascardo/ovs.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 #include <linux/kconfig.h>
20 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/if.h>
24 #include <linux/skbuff.h>
25 #include <linux/ip.h>
26 #include <linux/if_tunnel.h>
27 #include <linux/if_vlan.h>
28 #include <linux/in.h>
29 #include <linux/in_route.h>
30 #include <linux/inetdevice.h>
31 #include <linux/jhash.h>
32 #include <linux/list.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/workqueue.h>
36 #include <linux/rculist.h>
37 #include <net/net_namespace.h>
38 #include <net/netns/generic.h>
39 #include <net/route.h>
40 #include <net/xfrm.h>
41
42 #include <net/icmp.h>
43 #include <net/ip.h>
44 #include <net/ip_tunnels.h>
45 #include <net/gre.h>
46 #include <net/protocol.h>
47
48 #include "datapath.h"
49 #include "vport.h"
50
51 static struct vport_ops ovs_gre_vport_ops;
52 static struct vport_ops ovs_gre64_vport_ops;
53
54 /* Returns the least-significant 32 bits of a __be64. */
55 static __be32 be64_get_low32(__be64 x)
56 {
57 #ifdef __BIG_ENDIAN
58         return (__force __be32)x;
59 #else
60         return (__force __be32)((__force u64)x >> 32);
61 #endif
62 }
63
64 static __be16 filter_tnl_flags(__be16 flags)
65 {
66         return flags & (TUNNEL_CSUM | TUNNEL_KEY);
67 }
68
69 static struct sk_buff *__build_header(struct sk_buff *skb,
70                                       int tunnel_hlen,
71                                       __be32 seq, __be16 gre64_flag)
72 {
73         const struct ovs_key_ipv4_tunnel *tun_key;
74         struct tnl_ptk_info tpi;
75
76         tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
77         skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
78         if (IS_ERR(skb))
79                 return skb;
80
81         tpi.flags = filter_tnl_flags(tun_key->tun_flags) | gre64_flag;
82
83         tpi.proto = htons(ETH_P_TEB);
84         tpi.key = be64_get_low32(tun_key->tun_id);
85         tpi.seq = seq;
86         gre_build_header(skb, &tpi, tunnel_hlen);
87
88         return skb;
89 }
90
91 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
92 {
93 #ifdef __BIG_ENDIAN
94         return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
95 #else
96         return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
97 #endif
98 }
99
100 /* Called with rcu_read_lock and BH disabled. */
101 static int gre_rcv(struct sk_buff *skb,
102                    const struct tnl_ptk_info *tpi)
103 {
104         struct ovs_tunnel_info tun_info;
105         struct ovs_net *ovs_net;
106         struct vport *vport;
107         __be64 key;
108
109         ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
110         if ((tpi->flags & TUNNEL_KEY) && (tpi->flags & TUNNEL_SEQ))
111                 vport = rcu_dereference(ovs_net->vport_net.gre64_vport);
112         else
113                 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
114         if (unlikely(!vport))
115                 return PACKET_REJECT;
116
117         key = key_to_tunnel_id(tpi->key, tpi->seq);
118         ovs_flow_tun_info_init(&tun_info, ip_hdr(skb), 0, 0, key,
119                                filter_tnl_flags(tpi->flags), NULL, 0);
120
121         ovs_vport_receive(vport, skb, &tun_info);
122         return PACKET_RCVD;
123 }
124
125 /* Called with rcu_read_lock and BH disabled. */
126 static int gre_err(struct sk_buff *skb, u32 info,
127                    const struct tnl_ptk_info *tpi)
128 {
129         struct ovs_net *ovs_net;
130         struct vport *vport;
131
132         ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
133         if ((tpi->flags & TUNNEL_KEY) && (tpi->flags & TUNNEL_SEQ))
134                 vport = rcu_dereference(ovs_net->vport_net.gre64_vport);
135         else
136                 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
137
138         if (unlikely(!vport))
139                 return PACKET_REJECT;
140         else
141                 return PACKET_RCVD;
142 }
143
144 static int __send(struct vport *vport, struct sk_buff *skb,
145                   int tunnel_hlen,
146                   __be32 seq, __be16 gre64_flag)
147 {
148         struct ovs_key_ipv4_tunnel *tun_key;
149         struct rtable *rt;
150         int min_headroom;
151         __be16 df;
152         __be32 saddr;
153         int err;
154
155         /* Route lookup */
156         tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
157         saddr = tun_key->ipv4_src;
158         rt = find_route(ovs_dp_get_net(vport->dp),
159                         &saddr, tun_key->ipv4_dst,
160                         IPPROTO_GRE, tun_key->ipv4_tos,
161                         skb->mark);
162         if (IS_ERR(rt)) {
163                 err = PTR_ERR(rt);
164                 goto error;
165         }
166
167         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
168                         + tunnel_hlen + sizeof(struct iphdr)
169                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
170
171         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
172                 int head_delta = SKB_DATA_ALIGN(min_headroom -
173                                                 skb_headroom(skb) +
174                                                 16);
175                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
176                                         0, GFP_ATOMIC);
177                 if (unlikely(err))
178                         goto err_free_rt;
179         }
180
181         if (skb_vlan_tag_present(skb)) {
182                 if (unlikely(!vlan_insert_tag_set_proto(skb,
183                                                         skb->vlan_proto,
184                                                         skb_vlan_tag_get(skb)))) {
185                         err = -ENOMEM;
186                         skb = NULL;
187                         goto err_free_rt;
188                 }
189                 vlan_set_tci(skb, 0);
190         }
191
192         /* Push Tunnel header. */
193         skb = __build_header(skb, tunnel_hlen, seq, gre64_flag);
194         if (IS_ERR(skb)) {
195                 err = PTR_ERR(skb);
196                 skb = NULL;
197                 goto err_free_rt;
198         }
199
200         df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
201         skb->ignore_df = 1;
202
203         return iptunnel_xmit(skb->sk, rt, skb, saddr,
204                              tun_key->ipv4_dst, IPPROTO_GRE,
205                              tun_key->ipv4_tos,
206                              tun_key->ipv4_ttl, df, false);
207 err_free_rt:
208         ip_rt_put(rt);
209 error:
210         kfree_skb(skb);
211         return err;
212 }
213
214 static struct gre_cisco_protocol gre_protocol = {
215         .handler        = gre_rcv,
216         .err_handler    = gre_err,
217         .priority       = 1,
218 };
219
220 static int gre_ports;
221 static int gre_init(void)
222 {
223         int err;
224
225         gre_ports++;
226         if (gre_ports > 1)
227                 return 0;
228
229         err = gre_cisco_register(&gre_protocol);
230         if (err)
231                 pr_warn("cannot register gre protocol handler\n");
232
233         return err;
234 }
235
236 static void gre_exit(void)
237 {
238         gre_ports--;
239         if (gre_ports > 0)
240                 return;
241
242         gre_cisco_unregister(&gre_protocol);
243 }
244
245 static const char *gre_get_name(const struct vport *vport)
246 {
247         return vport_priv(vport);
248 }
249
250 static struct vport *gre_create(const struct vport_parms *parms)
251 {
252         struct net *net = ovs_dp_get_net(parms->dp);
253         struct ovs_net *ovs_net;
254         struct vport *vport;
255         int err;
256
257         err = gre_init();
258         if (err)
259                 return ERR_PTR(err);
260
261         ovs_net = net_generic(net, ovs_net_id);
262         if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
263                 vport = ERR_PTR(-EEXIST);
264                 goto error;
265         }
266
267         vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
268         if (IS_ERR(vport))
269                 goto error;
270
271         strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
272         rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
273         return vport;
274
275 error:
276         gre_exit();
277         return vport;
278 }
279
280 static void gre_tnl_destroy(struct vport *vport)
281 {
282         struct net *net = ovs_dp_get_net(vport->dp);
283         struct ovs_net *ovs_net;
284
285         ovs_net = net_generic(net, ovs_net_id);
286
287         RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL);
288         ovs_vport_deferred_free(vport);
289         gre_exit();
290 }
291
292 static int gre_send(struct vport *vport, struct sk_buff *skb)
293 {
294         int hlen;
295
296         if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
297                 kfree_skb(skb);
298                 return -EINVAL;
299         }
300
301         hlen = ip_gre_calc_hlen(OVS_CB(skb)->egress_tun_info->tunnel.tun_flags);
302
303         return __send(vport, skb, hlen, 0, 0);
304 }
305
306 static int gre_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
307                                    struct ovs_tunnel_info *egress_tun_info)
308 {
309         return ovs_tunnel_get_egress_info(egress_tun_info,
310                                           ovs_dp_get_net(vport->dp),
311                                           OVS_CB(skb)->egress_tun_info,
312                                           IPPROTO_GRE, skb->mark, 0, 0);
313 }
314
315 static struct vport_ops ovs_gre_vport_ops = {
316         .type                   = OVS_VPORT_TYPE_GRE,
317         .create                 = gre_create,
318         .destroy                = gre_tnl_destroy,
319         .get_name               = gre_get_name,
320         .send                   = gre_send,
321         .get_egress_tun_info    = gre_get_egress_tun_info,
322         .owner                  = THIS_MODULE,
323 };
324
325 /* GRE64 vport. */
326 static struct vport *gre64_create(const struct vport_parms *parms)
327 {
328         struct net *net = ovs_dp_get_net(parms->dp);
329         struct ovs_net *ovs_net;
330         struct vport *vport;
331         int err;
332
333         pr_warn_once("GRE64 tunnel protocol is deprecated.");
334         err = gre_init();
335         if (err)
336                 return ERR_PTR(err);
337
338         ovs_net = net_generic(net, ovs_net_id);
339         if (ovsl_dereference(ovs_net->vport_net.gre64_vport)) {
340                 vport = ERR_PTR(-EEXIST);
341                 goto error;
342         }
343
344         vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre64_vport_ops, parms);
345         if (IS_ERR(vport))
346                 goto error;
347
348         strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
349         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, vport);
350         return vport;
351 error:
352         gre_exit();
353         return vport;
354 }
355
356 static void gre64_tnl_destroy(struct vport *vport)
357 {
358         struct net *net = ovs_dp_get_net(vport->dp);
359         struct ovs_net *ovs_net;
360
361         ovs_net = net_generic(net, ovs_net_id);
362
363         rcu_assign_pointer(ovs_net->vport_net.gre64_vport, NULL);
364         ovs_vport_deferred_free(vport);
365         gre_exit();
366 }
367
368 static __be32 be64_get_high32(__be64 x)
369 {
370 #ifdef __BIG_ENDIAN
371         return (__force __be32)((__force u64)x >> 32);
372 #else
373         return (__force __be32)x;
374 #endif
375 }
376
377 static int gre64_send(struct vport *vport, struct sk_buff *skb)
378 {
379         int hlen = GRE_HEADER_SECTION +         /* GRE Hdr */
380                    GRE_HEADER_SECTION +         /* GRE Key */
381                    GRE_HEADER_SECTION;          /* GRE SEQ */
382         __be32 seq;
383
384         if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
385                 kfree_skb(skb);
386                 return -EINVAL;
387         }
388
389         if (OVS_CB(skb)->egress_tun_info->tunnel.tun_flags & TUNNEL_CSUM)
390                 hlen += GRE_HEADER_SECTION;
391
392         seq = be64_get_high32(OVS_CB(skb)->egress_tun_info->tunnel.tun_id);
393         return __send(vport, skb, hlen, seq, (TUNNEL_KEY|TUNNEL_SEQ));
394 }
395
396 static struct vport_ops ovs_gre64_vport_ops = {
397         .type                   = OVS_VPORT_TYPE_GRE64,
398         .create                 = gre64_create,
399         .destroy                = gre64_tnl_destroy,
400         .get_name               = gre_get_name,
401         .send                   = gre64_send,
402         .get_egress_tun_info    = gre_get_egress_tun_info,
403         .owner                  = THIS_MODULE,
404 };
405
406 static int __init ovs_gre_tnl_init(void)
407 {
408         int err;
409
410         err = ovs_vport_ops_register(&ovs_gre_vport_ops);
411         if (err < 0)
412                 return err;
413
414         err = ovs_vport_ops_register(&ovs_gre64_vport_ops);
415         if (err < 0)
416                 ovs_vport_ops_unregister(&ovs_gre_vport_ops);
417
418         return err;
419 }
420
421 static void __exit ovs_gre_tnl_exit(void)
422 {
423         ovs_vport_ops_unregister(&ovs_gre64_vport_ops);
424         ovs_vport_ops_unregister(&ovs_gre_vport_ops);
425 }
426
427 module_init(ovs_gre_tnl_init);
428 module_exit(ovs_gre_tnl_exit);
429
430 MODULE_DESCRIPTION("OVS: GRE switching port");
431 MODULE_LICENSE("GPL");
432 MODULE_ALIAS("vport-type-3");
433 MODULE_ALIAS("vport-type-104");
434 #endif