2 * Copyright (c) 2007-2015 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 #include <linux/kconfig.h>
20 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/skbuff.h>
27 #include <linux/if_tunnel.h>
28 #include <linux/if_vlan.h>
30 #include <linux/in_route.h>
31 #include <linux/inetdevice.h>
32 #include <linux/jhash.h>
33 #include <linux/list.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/workqueue.h>
37 #include <linux/rculist.h>
38 #include <net/route.h>
43 #include <net/ip_tunnels.h>
45 #include <net/net_namespace.h>
46 #include <net/netns/generic.h>
47 #include <net/protocol.h>
52 static struct vport_ops ovs_gre_vport_ops;
54 /* Returns the least-significant 32 bits of a __be64. */
55 static __be32 be64_get_low32(__be64 x)
58 return (__force __be32)x;
60 return (__force __be32)((__force u64)x >> 32);
64 static __be16 filter_tnl_flags(__be16 flags)
66 return flags & (TUNNEL_CSUM | TUNNEL_KEY);
69 static struct sk_buff *__build_header(struct sk_buff *skb,
72 struct tnl_ptk_info tpi;
73 const struct ovs_key_ipv4_tunnel *tun_key;
75 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
77 skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
81 tpi.flags = filter_tnl_flags(tun_key->tun_flags);
82 tpi.proto = htons(ETH_P_TEB);
83 tpi.key = be64_get_low32(tun_key->tun_id);
85 gre_build_header(skb, &tpi, tunnel_hlen);
90 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
93 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
95 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
99 /* Called with rcu_read_lock and BH disabled. */
100 static int gre_rcv(struct sk_buff *skb,
101 const struct tnl_ptk_info *tpi)
103 struct ovs_tunnel_info tun_info;
104 struct ovs_net *ovs_net;
108 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
109 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
110 if (unlikely(!vport))
111 return PACKET_REJECT;
113 key = key_to_tunnel_id(tpi->key, tpi->seq);
114 ovs_flow_tun_info_init(&tun_info, ip_hdr(skb), 0, 0, key,
115 filter_tnl_flags(tpi->flags), NULL, 0);
117 ovs_vport_receive(vport, skb, &tun_info);
121 /* Called with rcu_read_lock and BH disabled. */
122 static int gre_err(struct sk_buff *skb, u32 info,
123 const struct tnl_ptk_info *tpi)
125 struct ovs_net *ovs_net;
128 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
129 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
131 if (unlikely(!vport))
132 return PACKET_REJECT;
137 static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
139 const struct ovs_key_ipv4_tunnel *tun_key;
147 if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
152 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
153 saddr = tun_key->ipv4_src;
154 rt = find_route(ovs_dp_get_net(vport->dp),
155 &saddr, tun_key->ipv4_dst,
156 IPPROTO_GRE, tun_key->ipv4_tos,
163 tunnel_hlen = ip_gre_calc_hlen(tun_key->tun_flags);
165 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
166 + tunnel_hlen + sizeof(struct iphdr)
167 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
168 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
169 int head_delta = SKB_DATA_ALIGN(min_headroom -
172 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
178 skb = vlan_hwaccel_push_inside(skb);
179 if (unlikely(!skb)) {
184 /* Push Tunnel header. */
185 skb = __build_header(skb, tunnel_hlen);
192 df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
197 return iptunnel_xmit(skb->sk, rt, skb, saddr,
198 tun_key->ipv4_dst, IPPROTO_GRE,
199 tun_key->ipv4_tos, tun_key->ipv4_ttl, df, false);
207 static struct gre_cisco_protocol gre_protocol = {
209 .err_handler = gre_err,
213 static int gre_ports;
214 static int gre_init(void)
222 err = gre_cisco_register(&gre_protocol);
224 pr_warn("cannot register gre protocol handler\n");
229 static void gre_exit(void)
235 gre_cisco_unregister(&gre_protocol);
238 static const char *gre_get_name(const struct vport *vport)
240 return vport_priv(vport);
243 static struct vport *gre_create(const struct vport_parms *parms)
245 struct net *net = ovs_dp_get_net(parms->dp);
246 struct ovs_net *ovs_net;
254 ovs_net = net_generic(net, ovs_net_id);
255 if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
256 vport = ERR_PTR(-EEXIST);
260 vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
264 strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
265 rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
273 static void gre_tnl_destroy(struct vport *vport)
275 struct net *net = ovs_dp_get_net(vport->dp);
276 struct ovs_net *ovs_net;
278 ovs_net = net_generic(net, ovs_net_id);
280 RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL);
281 ovs_vport_deferred_free(vport);
285 static int gre_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
286 struct ovs_tunnel_info *egress_tun_info)
288 return ovs_tunnel_get_egress_info(egress_tun_info,
289 ovs_dp_get_net(vport->dp),
290 OVS_CB(skb)->egress_tun_info,
291 IPPROTO_GRE, skb->mark, 0, 0);
294 static struct vport_ops ovs_gre_vport_ops = {
295 .type = OVS_VPORT_TYPE_GRE,
296 .create = gre_create,
297 .destroy = gre_tnl_destroy,
298 .get_name = gre_get_name,
299 .send = gre_tnl_send,
300 .get_egress_tun_info = gre_get_egress_tun_info,
301 .owner = THIS_MODULE,
304 static int __init ovs_gre_tnl_init(void)
306 return ovs_vport_ops_register(&ovs_gre_vport_ops);
309 static void __exit ovs_gre_tnl_exit(void)
311 ovs_vport_ops_unregister(&ovs_gre_vport_ops);
314 module_init(ovs_gre_tnl_init);
315 module_exit(ovs_gre_tnl_exit);
317 MODULE_DESCRIPTION("OVS: GRE switching port");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS("vport-type-3");