2 * Copyright (c) 2007-2012 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)
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/skbuff.h>
26 #include <linux/if_tunnel.h>
27 #include <linux/if_vlan.h>
29 #include <linux/if_vlan.h>
31 #include <linux/in_route.h>
32 #include <linux/inetdevice.h>
33 #include <linux/jhash.h>
34 #include <linux/list.h>
35 #include <linux/kernel.h>
36 #include <linux/workqueue.h>
37 #include <linux/rculist.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
40 #include <net/route.h>
45 #include <net/ip_tunnels.h>
47 #include <net/protocol.h>
52 /* Returns the least-significant 32 bits of a __be64. */
53 static __be32 be64_get_low32(__be64 x)
56 return (__force __be32)x;
58 return (__force __be32)((__force u64)x >> 32);
62 static __be16 filter_tnl_flags(__be16 flags)
64 return flags & (TUNNEL_CSUM | TUNNEL_KEY);
67 static struct sk_buff *__build_header(struct sk_buff *skb,
69 __be32 seq, __be16 gre64_flag)
71 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
72 struct tnl_ptk_info tpi;
74 skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
78 tpi.flags = filter_tnl_flags(tun_key->tun_flags) | gre64_flag;
80 tpi.proto = htons(ETH_P_TEB);
81 tpi.key = be64_get_low32(tun_key->tun_id);
83 gre_build_header(skb, &tpi, tunnel_hlen);
88 static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
91 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
93 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
97 /* Called with rcu_read_lock and BH disabled. */
98 static int gre_rcv(struct sk_buff *skb,
99 const struct tnl_ptk_info *tpi)
101 struct ovs_key_ipv4_tunnel tun_key;
102 struct ovs_net *ovs_net;
106 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
107 if ((tpi->flags & TUNNEL_KEY) && (tpi->flags & TUNNEL_SEQ))
108 vport = rcu_dereference(ovs_net->vport_net.gre64_vport);
110 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
111 if (unlikely(!vport))
112 return PACKET_REJECT;
114 key = key_to_tunnel_id(tpi->key, tpi->seq);
115 ovs_flow_tun_key_init(&tun_key, ip_hdr(skb), key, filter_tnl_flags(tpi->flags));
117 ovs_vport_receive(vport, skb, &tun_key);
121 static int __send(struct vport *vport, struct sk_buff *skb,
123 __be32 seq, __be16 gre64_flag)
125 struct net *net = ovs_dp_get_net(vport->dp);
133 saddr = OVS_CB(skb)->tun_key->ipv4_src;
134 rt = find_route(ovs_dp_get_net(vport->dp),
136 OVS_CB(skb)->tun_key->ipv4_dst,
138 OVS_CB(skb)->tun_key->ipv4_tos,
145 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
146 + tunnel_hlen + sizeof(struct iphdr)
147 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
149 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
150 int head_delta = SKB_DATA_ALIGN(min_headroom -
153 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
159 if (unlikely(vlan_deaccel_tag(skb))) {
164 /* Push Tunnel header. */
165 skb = __build_header(skb, tunnel_hlen, seq, gre64_flag);
166 if (unlikely(!skb)) {
171 df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
176 return iptunnel_xmit(net, rt, skb, saddr,
177 OVS_CB(skb)->tun_key->ipv4_dst, IPPROTO_GRE,
178 OVS_CB(skb)->tun_key->ipv4_tos,
179 OVS_CB(skb)->tun_key->ipv4_ttl, df);
186 static struct gre_cisco_protocol gre_protocol = {
191 static int gre_ports;
192 static int gre_init(void)
200 err = gre_cisco_register(&gre_protocol);
202 pr_warn("cannot register gre protocol handler\n");
207 static void gre_exit(void)
213 gre_cisco_unregister(&gre_protocol);
216 static const char *gre_get_name(const struct vport *vport)
218 return vport_priv(vport);
221 static struct vport *gre_create(const struct vport_parms *parms)
223 struct net *net = ovs_dp_get_net(parms->dp);
224 struct ovs_net *ovs_net;
232 ovs_net = net_generic(net, ovs_net_id);
233 if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
234 vport = ERR_PTR(-EEXIST);
238 vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
242 strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
243 rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
251 static void gre_tnl_destroy(struct vport *vport)
253 struct net *net = ovs_dp_get_net(vport->dp);
254 struct ovs_net *ovs_net;
256 ovs_net = net_generic(net, ovs_net_id);
258 rcu_assign_pointer(ovs_net->vport_net.gre_vport, NULL);
259 ovs_vport_deferred_free(vport);
263 static int gre_send(struct vport *vport, struct sk_buff *skb)
267 if (unlikely(!OVS_CB(skb)->tun_key))
270 hlen = ip_gre_calc_hlen(OVS_CB(skb)->tun_key->tun_flags);
272 return __send(vport, skb, hlen, 0, 0);
275 const struct vport_ops ovs_gre_vport_ops = {
276 .type = OVS_VPORT_TYPE_GRE,
277 .create = gre_create,
278 .destroy = gre_tnl_destroy,
279 .get_name = gre_get_name,
284 static struct vport *gre64_create(const struct vport_parms *parms)
286 struct net *net = ovs_dp_get_net(parms->dp);
287 struct ovs_net *ovs_net;
295 ovs_net = net_generic(net, ovs_net_id);
296 if (ovsl_dereference(ovs_net->vport_net.gre64_vport)) {
297 vport = ERR_PTR(-EEXIST);
301 vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre64_vport_ops, parms);
305 strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
306 rcu_assign_pointer(ovs_net->vport_net.gre64_vport, vport);
313 static void gre64_tnl_destroy(struct vport *vport)
315 struct net *net = ovs_dp_get_net(vport->dp);
316 struct ovs_net *ovs_net;
318 ovs_net = net_generic(net, ovs_net_id);
320 rcu_assign_pointer(ovs_net->vport_net.gre64_vport, NULL);
321 ovs_vport_deferred_free(vport);
325 static __be32 be64_get_high32(__be64 x)
328 return (__force __be32)((__force u64)x >> 32);
330 return (__force __be32)x;
334 static int gre64_send(struct vport *vport, struct sk_buff *skb)
336 int hlen = GRE_HEADER_SECTION + /* GRE Hdr */
337 GRE_HEADER_SECTION + /* GRE Key */
338 GRE_HEADER_SECTION; /* GRE SEQ */
341 if (unlikely(!OVS_CB(skb)->tun_key))
344 if (OVS_CB(skb)->tun_key->tun_flags & TUNNEL_CSUM)
345 hlen += GRE_HEADER_SECTION;
347 seq = be64_get_high32(OVS_CB(skb)->tun_key->tun_id);
348 return __send(vport, skb, hlen, seq, (TUNNEL_KEY|TUNNEL_SEQ));
351 const struct vport_ops ovs_gre64_vport_ops = {
352 .type = OVS_VPORT_TYPE_GRE64,
353 .create = gre64_create,
354 .destroy = gre64_tnl_destroy,
355 .get_name = gre_get_name,