2013-06-18 08:50:33 +08:00
|
|
|
/*
|
2014-09-16 10:28:44 +08:00
|
|
|
* Copyright (c) 2007-2014 Nicira, Inc.
|
2013-06-18 08:50:33 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/if_tunnel.h>
|
|
|
|
#include <linux/if_vlan.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/in_route.h>
|
|
|
|
#include <linux/inetdevice.h>
|
|
|
|
#include <linux/jhash.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/kernel.h>
|
2014-10-22 23:29:06 +08:00
|
|
|
#include <linux/module.h>
|
2013-06-18 08:50:33 +08:00
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/rculist.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/xfrm.h>
|
|
|
|
|
|
|
|
#include <net/icmp.h>
|
|
|
|
#include <net/ip.h>
|
|
|
|
#include <net/ip_tunnels.h>
|
|
|
|
#include <net/gre.h>
|
|
|
|
#include <net/net_namespace.h>
|
|
|
|
#include <net/netns/generic.h>
|
|
|
|
#include <net/protocol.h>
|
|
|
|
|
|
|
|
#include "datapath.h"
|
|
|
|
#include "vport.h"
|
|
|
|
|
2014-10-22 23:29:06 +08:00
|
|
|
static struct vport_ops ovs_gre_vport_ops;
|
|
|
|
|
2013-06-18 08:50:33 +08:00
|
|
|
/* Returns the least-significant 32 bits of a __be64. */
|
|
|
|
static __be32 be64_get_low32(__be64 x)
|
|
|
|
{
|
|
|
|
#ifdef __BIG_ENDIAN
|
|
|
|
return (__force __be32)x;
|
|
|
|
#else
|
|
|
|
return (__force __be32)((__force u64)x >> 32);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static __be16 filter_tnl_flags(__be16 flags)
|
|
|
|
{
|
|
|
|
return flags & (TUNNEL_CSUM | TUNNEL_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *__build_header(struct sk_buff *skb,
|
|
|
|
int tunnel_hlen)
|
|
|
|
{
|
|
|
|
struct tnl_ptk_info tpi;
|
2015-07-21 16:43:54 +08:00
|
|
|
const struct ip_tunnel_key *tun_key;
|
2014-10-04 06:35:31 +08:00
|
|
|
|
2015-07-21 16:43:54 +08:00
|
|
|
tun_key = &OVS_CB(skb)->egress_tun_info->key;
|
2013-06-18 08:50:33 +08:00
|
|
|
|
|
|
|
skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
|
|
|
|
if (IS_ERR(skb))
|
2014-12-24 08:20:32 +08:00
|
|
|
return skb;
|
2013-06-18 08:50:33 +08:00
|
|
|
|
|
|
|
tpi.flags = filter_tnl_flags(tun_key->tun_flags);
|
|
|
|
tpi.proto = htons(ETH_P_TEB);
|
|
|
|
tpi.key = be64_get_low32(tun_key->tun_id);
|
|
|
|
tpi.seq = 0;
|
|
|
|
gre_build_header(skb, &tpi, tunnel_hlen);
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
|
|
|
|
{
|
|
|
|
#ifdef __BIG_ENDIAN
|
|
|
|
return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
|
|
|
|
#else
|
|
|
|
return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called with rcu_read_lock and BH disabled. */
|
|
|
|
static int gre_rcv(struct sk_buff *skb,
|
|
|
|
const struct tnl_ptk_info *tpi)
|
|
|
|
{
|
2015-07-21 16:43:54 +08:00
|
|
|
struct ip_tunnel_info tun_info;
|
2013-06-18 08:50:33 +08:00
|
|
|
struct ovs_net *ovs_net;
|
|
|
|
struct vport *vport;
|
|
|
|
__be64 key;
|
|
|
|
|
|
|
|
ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
|
|
|
|
vport = rcu_dereference(ovs_net->vport_net.gre_vport);
|
|
|
|
if (unlikely(!vport))
|
|
|
|
return PACKET_REJECT;
|
|
|
|
|
|
|
|
key = key_to_tunnel_id(tpi->key, tpi->seq);
|
2015-07-21 16:43:54 +08:00
|
|
|
ip_tunnel_info_init(&tun_info, ip_hdr(skb), 0, 0, key,
|
|
|
|
filter_tnl_flags(tpi->flags), NULL, 0);
|
2013-06-18 08:50:33 +08:00
|
|
|
|
2014-10-04 06:35:31 +08:00
|
|
|
ovs_vport_receive(vport, skb, &tun_info);
|
2013-06-18 08:50:33 +08:00
|
|
|
return PACKET_RCVD;
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:34:53 +08:00
|
|
|
/* Called with rcu_read_lock and BH disabled. */
|
|
|
|
static int gre_err(struct sk_buff *skb, u32 info,
|
|
|
|
const struct tnl_ptk_info *tpi)
|
|
|
|
{
|
|
|
|
struct ovs_net *ovs_net;
|
|
|
|
struct vport *vport;
|
|
|
|
|
|
|
|
ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
|
|
|
|
vport = rcu_dereference(ovs_net->vport_net.gre_vport);
|
|
|
|
|
|
|
|
if (unlikely(!vport))
|
|
|
|
return PACKET_REJECT;
|
|
|
|
else
|
|
|
|
return PACKET_RCVD;
|
|
|
|
}
|
|
|
|
|
2013-06-18 08:50:33 +08:00
|
|
|
static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct net *net = ovs_dp_get_net(vport->dp);
|
2015-07-21 16:43:54 +08:00
|
|
|
const struct ip_tunnel_key *tun_key;
|
2013-06-18 08:50:33 +08:00
|
|
|
struct flowi4 fl;
|
|
|
|
struct rtable *rt;
|
|
|
|
int min_headroom;
|
|
|
|
int tunnel_hlen;
|
|
|
|
__be16 df;
|
|
|
|
int err;
|
|
|
|
|
2014-10-04 06:35:31 +08:00
|
|
|
if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
|
2013-06-18 08:50:33 +08:00
|
|
|
err = -EINVAL;
|
2014-12-24 08:20:32 +08:00
|
|
|
goto err_free_skb;
|
2013-06-18 08:50:33 +08:00
|
|
|
}
|
|
|
|
|
2015-07-21 16:43:54 +08:00
|
|
|
tun_key = &OVS_CB(skb)->egress_tun_info->key;
|
2015-01-14 13:10:35 +08:00
|
|
|
rt = ovs_tunnel_route_lookup(net, tun_key, skb->mark, &fl, IPPROTO_GRE);
|
2014-12-24 08:20:32 +08:00
|
|
|
if (IS_ERR(rt)) {
|
|
|
|
err = PTR_ERR(rt);
|
|
|
|
goto err_free_skb;
|
|
|
|
}
|
2013-06-18 08:50:33 +08:00
|
|
|
|
2014-09-16 10:28:44 +08:00
|
|
|
tunnel_hlen = ip_gre_calc_hlen(tun_key->tun_flags);
|
2013-06-18 08:50:33 +08:00
|
|
|
|
|
|
|
min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
|
|
|
|
+ tunnel_hlen + sizeof(struct iphdr)
|
2015-01-14 00:13:44 +08:00
|
|
|
+ (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
|
2013-06-18 08:50:33 +08:00
|
|
|
if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
|
|
|
|
int head_delta = SKB_DATA_ALIGN(min_headroom -
|
|
|
|
skb_headroom(skb) +
|
|
|
|
16);
|
|
|
|
err = pskb_expand_head(skb, max_t(int, head_delta, 0),
|
|
|
|
0, GFP_ATOMIC);
|
|
|
|
if (unlikely(err))
|
|
|
|
goto err_free_rt;
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:04:59 +08:00
|
|
|
skb = vlan_hwaccel_push_inside(skb);
|
|
|
|
if (unlikely(!skb)) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_free_rt;
|
2013-06-18 08:50:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push Tunnel header. */
|
|
|
|
skb = __build_header(skb, tunnel_hlen);
|
2014-12-24 08:20:32 +08:00
|
|
|
if (IS_ERR(skb)) {
|
2014-12-24 14:41:39 +08:00
|
|
|
err = PTR_ERR(skb);
|
2014-12-24 08:20:32 +08:00
|
|
|
skb = NULL;
|
2013-06-18 08:50:33 +08:00
|
|
|
goto err_free_rt;
|
|
|
|
}
|
|
|
|
|
2014-09-16 10:28:44 +08:00
|
|
|
df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
|
2013-06-18 08:50:33 +08:00
|
|
|
htons(IP_DF) : 0;
|
|
|
|
|
2014-05-05 07:39:18 +08:00
|
|
|
skb->ignore_df = 1;
|
2013-06-18 08:50:33 +08:00
|
|
|
|
2014-04-16 01:47:15 +08:00
|
|
|
return iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
|
2014-09-16 10:28:44 +08:00
|
|
|
tun_key->ipv4_dst, IPPROTO_GRE,
|
|
|
|
tun_key->ipv4_tos, tun_key->ipv4_ttl, df, false);
|
2013-06-18 08:50:33 +08:00
|
|
|
err_free_rt:
|
|
|
|
ip_rt_put(rt);
|
2014-12-24 08:20:32 +08:00
|
|
|
err_free_skb:
|
|
|
|
kfree_skb(skb);
|
2013-06-18 08:50:33 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct gre_cisco_protocol gre_protocol = {
|
|
|
|
.handler = gre_rcv,
|
2014-06-29 03:34:53 +08:00
|
|
|
.err_handler = gre_err,
|
2013-06-18 08:50:33 +08:00
|
|
|
.priority = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gre_ports;
|
|
|
|
static int gre_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
gre_ports++;
|
|
|
|
if (gre_ports > 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = gre_cisco_register(&gre_protocol);
|
|
|
|
if (err)
|
|
|
|
pr_warn("cannot register gre protocol handler\n");
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gre_exit(void)
|
|
|
|
{
|
|
|
|
gre_ports--;
|
|
|
|
if (gre_ports > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gre_cisco_unregister(&gre_protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *gre_get_name(const struct vport *vport)
|
|
|
|
{
|
|
|
|
return vport_priv(vport);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vport *gre_create(const struct vport_parms *parms)
|
|
|
|
{
|
|
|
|
struct net *net = ovs_dp_get_net(parms->dp);
|
|
|
|
struct ovs_net *ovs_net;
|
|
|
|
struct vport *vport;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = gre_init();
|
|
|
|
if (err)
|
|
|
|
return ERR_PTR(err);
|
|
|
|
|
|
|
|
ovs_net = net_generic(net, ovs_net_id);
|
|
|
|
if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
|
|
|
|
vport = ERR_PTR(-EEXIST);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
|
|
|
|
if (IS_ERR(vport))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
|
|
|
|
rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
|
|
|
|
return vport;
|
|
|
|
|
|
|
|
error:
|
|
|
|
gre_exit();
|
|
|
|
return vport;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gre_tnl_destroy(struct vport *vport)
|
|
|
|
{
|
|
|
|
struct net *net = ovs_dp_get_net(vport->dp);
|
|
|
|
struct ovs_net *ovs_net;
|
|
|
|
|
|
|
|
ovs_net = net_generic(net, ovs_net_id);
|
|
|
|
|
2014-03-24 03:22:43 +08:00
|
|
|
RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL);
|
2013-06-18 08:50:33 +08:00
|
|
|
ovs_vport_deferred_free(vport);
|
|
|
|
gre_exit();
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:51:24 +08:00
|
|
|
static int gre_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
|
2015-07-21 16:43:54 +08:00
|
|
|
struct ip_tunnel_info *egress_tun_info)
|
2014-11-06 22:51:24 +08:00
|
|
|
{
|
|
|
|
return ovs_tunnel_get_egress_info(egress_tun_info,
|
|
|
|
ovs_dp_get_net(vport->dp),
|
|
|
|
OVS_CB(skb)->egress_tun_info,
|
|
|
|
IPPROTO_GRE, skb->mark, 0, 0);
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:29:06 +08:00
|
|
|
static struct vport_ops ovs_gre_vport_ops = {
|
2013-06-18 08:50:33 +08:00
|
|
|
.type = OVS_VPORT_TYPE_GRE,
|
|
|
|
.create = gre_create,
|
|
|
|
.destroy = gre_tnl_destroy,
|
|
|
|
.get_name = gre_get_name,
|
|
|
|
.send = gre_tnl_send,
|
2014-11-06 22:51:24 +08:00
|
|
|
.get_egress_tun_info = gre_get_egress_tun_info,
|
2014-10-22 23:29:06 +08:00
|
|
|
.owner = THIS_MODULE,
|
2013-06-18 08:50:33 +08:00
|
|
|
};
|
2014-10-22 23:29:06 +08:00
|
|
|
|
|
|
|
static int __init ovs_gre_tnl_init(void)
|
|
|
|
{
|
|
|
|
return ovs_vport_ops_register(&ovs_gre_vport_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit ovs_gre_tnl_exit(void)
|
|
|
|
{
|
|
|
|
ovs_vport_ops_unregister(&ovs_gre_vport_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(ovs_gre_tnl_init);
|
|
|
|
module_exit(ovs_gre_tnl_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("OVS: GRE switching port");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("vport-type-3");
|