2 * Copyright (c) 2007-2013 Nicira, Inc.
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.
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/skbuff.h>
24 #include <linux/if_tunnel.h>
25 #include <linux/if_vlan.h>
27 #include <linux/in_route.h>
28 #include <linux/inetdevice.h>
29 #include <linux/jhash.h>
30 #include <linux/list.h>
31 #include <linux/kernel.h>
32 #include <linux/workqueue.h>
33 #include <linux/rculist.h>
34 #include <net/route.h>
39 #include <net/ip_tunnels.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/protocol.h>
48 /* Returns the least-significant 32 bits of a __be64. */
49 static __be32 be64_get_low32(__be64 x)
52 return (__force __be32)x;
54 return (__force __be32)((__force u64)x >> 32);
58 static __be16 filter_tnl_flags(__be16 flags)
60 return flags & (TUNNEL_CSUM | TUNNEL_KEY);
63 static struct sk_buff *__build_header(struct sk_buff *skb,
66 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
67 struct tnl_ptk_info tpi;
69 skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
73 tpi.flags = filter_tnl_flags(tun_key->tun_flags);
74 tpi.proto = htons(ETH_P_TEB);
75 tpi.key = be64_get_low32(tun_key->tun_id);
77 gre_build_header(skb, &tpi, tunnel_hlen);
82 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
85 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
87 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
91 /* Called with rcu_read_lock and BH disabled. */
92 static int gre_rcv(struct sk_buff *skb,
93 const struct tnl_ptk_info *tpi)
95 struct ovs_key_ipv4_tunnel tun_key;
96 struct ovs_net *ovs_net;
100 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
101 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
102 if (unlikely(!vport))
103 return PACKET_REJECT;
105 key = key_to_tunnel_id(tpi->key, tpi->seq);
106 ovs_flow_tun_key_init(&tun_key, ip_hdr(skb), key,
107 filter_tnl_flags(tpi->flags));
109 ovs_vport_receive(vport, skb, &tun_key);
113 /* Called with rcu_read_lock and BH disabled. */
114 static int gre_err(struct sk_buff *skb, u32 info,
115 const struct tnl_ptk_info *tpi)
117 struct ovs_net *ovs_net;
120 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
121 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
123 if (unlikely(!vport))
124 return PACKET_REJECT;
129 static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
131 struct net *net = ovs_dp_get_net(vport->dp);
139 if (unlikely(!OVS_CB(skb)->tun_key)) {
145 memset(&fl, 0, sizeof(fl));
146 fl.daddr = OVS_CB(skb)->tun_key->ipv4_dst;
147 fl.saddr = OVS_CB(skb)->tun_key->ipv4_src;
148 fl.flowi4_tos = RT_TOS(OVS_CB(skb)->tun_key->ipv4_tos);
149 fl.flowi4_mark = skb->mark;
150 fl.flowi4_proto = IPPROTO_GRE;
152 rt = ip_route_output_key(net, &fl);
156 tunnel_hlen = ip_gre_calc_hlen(OVS_CB(skb)->tun_key->tun_flags);
158 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
159 + tunnel_hlen + sizeof(struct iphdr)
160 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
161 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
162 int head_delta = SKB_DATA_ALIGN(min_headroom -
165 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
171 if (vlan_tx_tag_present(skb)) {
172 if (unlikely(!__vlan_put_tag(skb,
174 vlan_tx_tag_get(skb)))) {
181 /* Push Tunnel header. */
182 skb = __build_header(skb, tunnel_hlen);
183 if (unlikely(!skb)) {
188 df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
193 return iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
194 OVS_CB(skb)->tun_key->ipv4_dst, IPPROTO_GRE,
195 OVS_CB(skb)->tun_key->ipv4_tos,
196 OVS_CB(skb)->tun_key->ipv4_ttl, df, false);
203 static struct gre_cisco_protocol gre_protocol = {
205 .err_handler = gre_err,
209 static int gre_ports;
210 static int gre_init(void)
218 err = gre_cisco_register(&gre_protocol);
220 pr_warn("cannot register gre protocol handler\n");
225 static void gre_exit(void)
231 gre_cisco_unregister(&gre_protocol);
234 static const char *gre_get_name(const struct vport *vport)
236 return vport_priv(vport);
239 static struct vport *gre_create(const struct vport_parms *parms)
241 struct net *net = ovs_dp_get_net(parms->dp);
242 struct ovs_net *ovs_net;
250 ovs_net = net_generic(net, ovs_net_id);
251 if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
252 vport = ERR_PTR(-EEXIST);
256 vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
260 strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
261 rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
269 static void gre_tnl_destroy(struct vport *vport)
271 struct net *net = ovs_dp_get_net(vport->dp);
272 struct ovs_net *ovs_net;
274 ovs_net = net_generic(net, ovs_net_id);
276 RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL);
277 ovs_vport_deferred_free(vport);
281 const struct vport_ops ovs_gre_vport_ops = {
282 .type = OVS_VPORT_TYPE_GRE,
283 .create = gre_create,
284 .destroy = gre_tnl_destroy,
285 .get_name = gre_get_name,
286 .send = gre_tnl_send,