xref: /openbmc/linux/net/openvswitch/vport-gre.c (revision aad88724)
1aa310701SPravin B Shelar /*
2aa310701SPravin B Shelar  * Copyright (c) 2007-2013 Nicira, Inc.
3aa310701SPravin B Shelar  *
4aa310701SPravin B Shelar  * This program is free software; you can redistribute it and/or
5aa310701SPravin B Shelar  * modify it under the terms of version 2 of the GNU General Public
6aa310701SPravin B Shelar  * License as published by the Free Software Foundation.
7aa310701SPravin B Shelar  *
8aa310701SPravin B Shelar  * This program is distributed in the hope that it will be useful, but
9aa310701SPravin B Shelar  * WITHOUT ANY WARRANTY; without even the implied warranty of
10aa310701SPravin B Shelar  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11aa310701SPravin B Shelar  * General Public License for more details.
12aa310701SPravin B Shelar  *
13aa310701SPravin B Shelar  * You should have received a copy of the GNU General Public License
14aa310701SPravin B Shelar  * along with this program; if not, write to the Free Software
15aa310701SPravin B Shelar  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16aa310701SPravin B Shelar  * 02110-1301, USA
17aa310701SPravin B Shelar  */
18aa310701SPravin B Shelar 
19aa310701SPravin B Shelar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20aa310701SPravin B Shelar 
21aa310701SPravin B Shelar #include <linux/if.h>
22aa310701SPravin B Shelar #include <linux/skbuff.h>
23aa310701SPravin B Shelar #include <linux/ip.h>
24aa310701SPravin B Shelar #include <linux/if_tunnel.h>
25aa310701SPravin B Shelar #include <linux/if_vlan.h>
26aa310701SPravin B Shelar #include <linux/in.h>
27aa310701SPravin B Shelar #include <linux/in_route.h>
28aa310701SPravin B Shelar #include <linux/inetdevice.h>
29aa310701SPravin B Shelar #include <linux/jhash.h>
30aa310701SPravin B Shelar #include <linux/list.h>
31aa310701SPravin B Shelar #include <linux/kernel.h>
32aa310701SPravin B Shelar #include <linux/workqueue.h>
33aa310701SPravin B Shelar #include <linux/rculist.h>
34aa310701SPravin B Shelar #include <net/route.h>
35aa310701SPravin B Shelar #include <net/xfrm.h>
36aa310701SPravin B Shelar 
37aa310701SPravin B Shelar #include <net/icmp.h>
38aa310701SPravin B Shelar #include <net/ip.h>
39aa310701SPravin B Shelar #include <net/ip_tunnels.h>
40aa310701SPravin B Shelar #include <net/gre.h>
41aa310701SPravin B Shelar #include <net/net_namespace.h>
42aa310701SPravin B Shelar #include <net/netns/generic.h>
43aa310701SPravin B Shelar #include <net/protocol.h>
44aa310701SPravin B Shelar 
45aa310701SPravin B Shelar #include "datapath.h"
46aa310701SPravin B Shelar #include "vport.h"
47aa310701SPravin B Shelar 
48aa310701SPravin B Shelar /* Returns the least-significant 32 bits of a __be64. */
49aa310701SPravin B Shelar static __be32 be64_get_low32(__be64 x)
50aa310701SPravin B Shelar {
51aa310701SPravin B Shelar #ifdef __BIG_ENDIAN
52aa310701SPravin B Shelar 	return (__force __be32)x;
53aa310701SPravin B Shelar #else
54aa310701SPravin B Shelar 	return (__force __be32)((__force u64)x >> 32);
55aa310701SPravin B Shelar #endif
56aa310701SPravin B Shelar }
57aa310701SPravin B Shelar 
58aa310701SPravin B Shelar static __be16 filter_tnl_flags(__be16 flags)
59aa310701SPravin B Shelar {
60aa310701SPravin B Shelar 	return flags & (TUNNEL_CSUM | TUNNEL_KEY);
61aa310701SPravin B Shelar }
62aa310701SPravin B Shelar 
63aa310701SPravin B Shelar static struct sk_buff *__build_header(struct sk_buff *skb,
64aa310701SPravin B Shelar 				      int tunnel_hlen)
65aa310701SPravin B Shelar {
66aa310701SPravin B Shelar 	const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
67aa310701SPravin B Shelar 	struct tnl_ptk_info tpi;
68aa310701SPravin B Shelar 
69aa310701SPravin B Shelar 	skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
70aa310701SPravin B Shelar 	if (IS_ERR(skb))
71aa310701SPravin B Shelar 		return NULL;
72aa310701SPravin B Shelar 
73aa310701SPravin B Shelar 	tpi.flags = filter_tnl_flags(tun_key->tun_flags);
74aa310701SPravin B Shelar 	tpi.proto = htons(ETH_P_TEB);
75aa310701SPravin B Shelar 	tpi.key = be64_get_low32(tun_key->tun_id);
76aa310701SPravin B Shelar 	tpi.seq = 0;
77aa310701SPravin B Shelar 	gre_build_header(skb, &tpi, tunnel_hlen);
78aa310701SPravin B Shelar 
79aa310701SPravin B Shelar 	return skb;
80aa310701SPravin B Shelar }
81aa310701SPravin B Shelar 
82aa310701SPravin B Shelar static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
83aa310701SPravin B Shelar {
84aa310701SPravin B Shelar #ifdef __BIG_ENDIAN
85aa310701SPravin B Shelar 	return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
86aa310701SPravin B Shelar #else
87aa310701SPravin B Shelar 	return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
88aa310701SPravin B Shelar #endif
89aa310701SPravin B Shelar }
90aa310701SPravin B Shelar 
91aa310701SPravin B Shelar /* Called with rcu_read_lock and BH disabled. */
92aa310701SPravin B Shelar static int gre_rcv(struct sk_buff *skb,
93aa310701SPravin B Shelar 		   const struct tnl_ptk_info *tpi)
94aa310701SPravin B Shelar {
95aa310701SPravin B Shelar 	struct ovs_key_ipv4_tunnel tun_key;
96aa310701SPravin B Shelar 	struct ovs_net *ovs_net;
97aa310701SPravin B Shelar 	struct vport *vport;
98aa310701SPravin B Shelar 	__be64 key;
99aa310701SPravin B Shelar 
100aa310701SPravin B Shelar 	ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
101aa310701SPravin B Shelar 	vport = rcu_dereference(ovs_net->vport_net.gre_vport);
102aa310701SPravin B Shelar 	if (unlikely(!vport))
103aa310701SPravin B Shelar 		return PACKET_REJECT;
104aa310701SPravin B Shelar 
105aa310701SPravin B Shelar 	key = key_to_tunnel_id(tpi->key, tpi->seq);
106aa310701SPravin B Shelar 	ovs_flow_tun_key_init(&tun_key, ip_hdr(skb), key,
107aa310701SPravin B Shelar 			      filter_tnl_flags(tpi->flags));
108aa310701SPravin B Shelar 
109aa310701SPravin B Shelar 	ovs_vport_receive(vport, skb, &tun_key);
110aa310701SPravin B Shelar 	return PACKET_RCVD;
111aa310701SPravin B Shelar }
112aa310701SPravin B Shelar 
113aa310701SPravin B Shelar static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
114aa310701SPravin B Shelar {
115aa310701SPravin B Shelar 	struct net *net = ovs_dp_get_net(vport->dp);
116aa310701SPravin B Shelar 	struct flowi4 fl;
117aa310701SPravin B Shelar 	struct rtable *rt;
118aa310701SPravin B Shelar 	int min_headroom;
119aa310701SPravin B Shelar 	int tunnel_hlen;
120aa310701SPravin B Shelar 	__be16 df;
121aa310701SPravin B Shelar 	int err;
122aa310701SPravin B Shelar 
123aa310701SPravin B Shelar 	if (unlikely(!OVS_CB(skb)->tun_key)) {
124aa310701SPravin B Shelar 		err = -EINVAL;
125aa310701SPravin B Shelar 		goto error;
126aa310701SPravin B Shelar 	}
127aa310701SPravin B Shelar 
128aa310701SPravin B Shelar 	/* Route lookup */
129aa310701SPravin B Shelar 	memset(&fl, 0, sizeof(fl));
130aa310701SPravin B Shelar 	fl.daddr = OVS_CB(skb)->tun_key->ipv4_dst;
131aa310701SPravin B Shelar 	fl.saddr = OVS_CB(skb)->tun_key->ipv4_src;
132aa310701SPravin B Shelar 	fl.flowi4_tos = RT_TOS(OVS_CB(skb)->tun_key->ipv4_tos);
133aa310701SPravin B Shelar 	fl.flowi4_mark = skb->mark;
134aa310701SPravin B Shelar 	fl.flowi4_proto = IPPROTO_GRE;
135aa310701SPravin B Shelar 
136aa310701SPravin B Shelar 	rt = ip_route_output_key(net, &fl);
137aa310701SPravin B Shelar 	if (IS_ERR(rt))
138aa310701SPravin B Shelar 		return PTR_ERR(rt);
139aa310701SPravin B Shelar 
140aa310701SPravin B Shelar 	tunnel_hlen = ip_gre_calc_hlen(OVS_CB(skb)->tun_key->tun_flags);
141aa310701SPravin B Shelar 
142aa310701SPravin B Shelar 	min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
143aa310701SPravin B Shelar 			+ tunnel_hlen + sizeof(struct iphdr)
144aa310701SPravin B Shelar 			+ (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
145aa310701SPravin B Shelar 	if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
146aa310701SPravin B Shelar 		int head_delta = SKB_DATA_ALIGN(min_headroom -
147aa310701SPravin B Shelar 						skb_headroom(skb) +
148aa310701SPravin B Shelar 						16);
149aa310701SPravin B Shelar 		err = pskb_expand_head(skb, max_t(int, head_delta, 0),
150aa310701SPravin B Shelar 					0, GFP_ATOMIC);
151aa310701SPravin B Shelar 		if (unlikely(err))
152aa310701SPravin B Shelar 			goto err_free_rt;
153aa310701SPravin B Shelar 	}
154aa310701SPravin B Shelar 
155aa310701SPravin B Shelar 	if (vlan_tx_tag_present(skb)) {
156aa310701SPravin B Shelar 		if (unlikely(!__vlan_put_tag(skb,
157aa310701SPravin B Shelar 					     skb->vlan_proto,
158aa310701SPravin B Shelar 					     vlan_tx_tag_get(skb)))) {
159aa310701SPravin B Shelar 			err = -ENOMEM;
160aa310701SPravin B Shelar 			goto err_free_rt;
161aa310701SPravin B Shelar 		}
162aa310701SPravin B Shelar 		skb->vlan_tci = 0;
163aa310701SPravin B Shelar 	}
164aa310701SPravin B Shelar 
165aa310701SPravin B Shelar 	/* Push Tunnel header. */
166aa310701SPravin B Shelar 	skb = __build_header(skb, tunnel_hlen);
167aa310701SPravin B Shelar 	if (unlikely(!skb)) {
168aa310701SPravin B Shelar 		err = 0;
169aa310701SPravin B Shelar 		goto err_free_rt;
170aa310701SPravin B Shelar 	}
171aa310701SPravin B Shelar 
172aa310701SPravin B Shelar 	df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
173aa310701SPravin B Shelar 		htons(IP_DF) : 0;
174aa310701SPravin B Shelar 
175aa310701SPravin B Shelar 	skb->local_df = 1;
176aa310701SPravin B Shelar 
177aad88724SEric Dumazet 	return iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
178aa310701SPravin B Shelar 			     OVS_CB(skb)->tun_key->ipv4_dst, IPPROTO_GRE,
179aa310701SPravin B Shelar 			     OVS_CB(skb)->tun_key->ipv4_tos,
180963a88b3SNicolas Dichtel 			     OVS_CB(skb)->tun_key->ipv4_ttl, df, false);
181aa310701SPravin B Shelar err_free_rt:
182aa310701SPravin B Shelar 	ip_rt_put(rt);
183aa310701SPravin B Shelar error:
184aa310701SPravin B Shelar 	return err;
185aa310701SPravin B Shelar }
186aa310701SPravin B Shelar 
187aa310701SPravin B Shelar static struct gre_cisco_protocol gre_protocol = {
188aa310701SPravin B Shelar 	.handler        = gre_rcv,
189aa310701SPravin B Shelar 	.priority       = 1,
190aa310701SPravin B Shelar };
191aa310701SPravin B Shelar 
192aa310701SPravin B Shelar static int gre_ports;
193aa310701SPravin B Shelar static int gre_init(void)
194aa310701SPravin B Shelar {
195aa310701SPravin B Shelar 	int err;
196aa310701SPravin B Shelar 
197aa310701SPravin B Shelar 	gre_ports++;
198aa310701SPravin B Shelar 	if (gre_ports > 1)
199aa310701SPravin B Shelar 		return 0;
200aa310701SPravin B Shelar 
201aa310701SPravin B Shelar 	err = gre_cisco_register(&gre_protocol);
202aa310701SPravin B Shelar 	if (err)
203aa310701SPravin B Shelar 		pr_warn("cannot register gre protocol handler\n");
204aa310701SPravin B Shelar 
205aa310701SPravin B Shelar 	return err;
206aa310701SPravin B Shelar }
207aa310701SPravin B Shelar 
208aa310701SPravin B Shelar static void gre_exit(void)
209aa310701SPravin B Shelar {
210aa310701SPravin B Shelar 	gre_ports--;
211aa310701SPravin B Shelar 	if (gre_ports > 0)
212aa310701SPravin B Shelar 		return;
213aa310701SPravin B Shelar 
214aa310701SPravin B Shelar 	gre_cisco_unregister(&gre_protocol);
215aa310701SPravin B Shelar }
216aa310701SPravin B Shelar 
217aa310701SPravin B Shelar static const char *gre_get_name(const struct vport *vport)
218aa310701SPravin B Shelar {
219aa310701SPravin B Shelar 	return vport_priv(vport);
220aa310701SPravin B Shelar }
221aa310701SPravin B Shelar 
222aa310701SPravin B Shelar static struct vport *gre_create(const struct vport_parms *parms)
223aa310701SPravin B Shelar {
224aa310701SPravin B Shelar 	struct net *net = ovs_dp_get_net(parms->dp);
225aa310701SPravin B Shelar 	struct ovs_net *ovs_net;
226aa310701SPravin B Shelar 	struct vport *vport;
227aa310701SPravin B Shelar 	int err;
228aa310701SPravin B Shelar 
229aa310701SPravin B Shelar 	err = gre_init();
230aa310701SPravin B Shelar 	if (err)
231aa310701SPravin B Shelar 		return ERR_PTR(err);
232aa310701SPravin B Shelar 
233aa310701SPravin B Shelar 	ovs_net = net_generic(net, ovs_net_id);
234aa310701SPravin B Shelar 	if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
235aa310701SPravin B Shelar 		vport = ERR_PTR(-EEXIST);
236aa310701SPravin B Shelar 		goto error;
237aa310701SPravin B Shelar 	}
238aa310701SPravin B Shelar 
239aa310701SPravin B Shelar 	vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
240aa310701SPravin B Shelar 	if (IS_ERR(vport))
241aa310701SPravin B Shelar 		goto error;
242aa310701SPravin B Shelar 
243aa310701SPravin B Shelar 	strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
244aa310701SPravin B Shelar 	rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
245aa310701SPravin B Shelar 	return vport;
246aa310701SPravin B Shelar 
247aa310701SPravin B Shelar error:
248aa310701SPravin B Shelar 	gre_exit();
249aa310701SPravin B Shelar 	return vport;
250aa310701SPravin B Shelar }
251aa310701SPravin B Shelar 
252aa310701SPravin B Shelar static void gre_tnl_destroy(struct vport *vport)
253aa310701SPravin B Shelar {
254aa310701SPravin B Shelar 	struct net *net = ovs_dp_get_net(vport->dp);
255aa310701SPravin B Shelar 	struct ovs_net *ovs_net;
256aa310701SPravin B Shelar 
257aa310701SPravin B Shelar 	ovs_net = net_generic(net, ovs_net_id);
258aa310701SPravin B Shelar 
259aa310701SPravin B Shelar 	rcu_assign_pointer(ovs_net->vport_net.gre_vport, NULL);
260aa310701SPravin B Shelar 	ovs_vport_deferred_free(vport);
261aa310701SPravin B Shelar 	gre_exit();
262aa310701SPravin B Shelar }
263aa310701SPravin B Shelar 
264aa310701SPravin B Shelar const struct vport_ops ovs_gre_vport_ops = {
265aa310701SPravin B Shelar 	.type		= OVS_VPORT_TYPE_GRE,
266aa310701SPravin B Shelar 	.create		= gre_create,
267aa310701SPravin B Shelar 	.destroy	= gre_tnl_destroy,
268aa310701SPravin B Shelar 	.get_name	= gre_get_name,
269aa310701SPravin B Shelar 	.send		= gre_tnl_send,
270aa310701SPravin B Shelar };
271