net: rename vlan_tx_* helpers since "tx" is misleading there
[deliverable/linux.git] / net / openvswitch / vport-gre.c
CommitLineData
aa310701 1/*
8c8b1b83 2 * Copyright (c) 2007-2014 Nicira, Inc.
aa310701
PS
3 *
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.
7 *
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.
12 *
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
16 * 02110-1301, USA
17 */
18
aa310701
PS
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/if.h>
22#include <linux/skbuff.h>
23#include <linux/ip.h>
24#include <linux/if_tunnel.h>
25#include <linux/if_vlan.h>
26#include <linux/in.h>
aa310701
PS
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>
62b9c8d0 32#include <linux/module.h>
aa310701
PS
33#include <linux/workqueue.h>
34#include <linux/rculist.h>
35#include <net/route.h>
36#include <net/xfrm.h>
37
38#include <net/icmp.h>
39#include <net/ip.h>
40#include <net/ip_tunnels.h>
41#include <net/gre.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44#include <net/protocol.h>
45
46#include "datapath.h"
47#include "vport.h"
48
62b9c8d0
TG
49static struct vport_ops ovs_gre_vport_ops;
50
aa310701
PS
51/* Returns the least-significant 32 bits of a __be64. */
52static __be32 be64_get_low32(__be64 x)
53{
54#ifdef __BIG_ENDIAN
55 return (__force __be32)x;
56#else
57 return (__force __be32)((__force u64)x >> 32);
58#endif
59}
60
61static __be16 filter_tnl_flags(__be16 flags)
62{
63 return flags & (TUNNEL_CSUM | TUNNEL_KEY);
64}
65
66static struct sk_buff *__build_header(struct sk_buff *skb,
67 int tunnel_hlen)
68{
aa310701 69 struct tnl_ptk_info tpi;
f0b128c1
JG
70 const struct ovs_key_ipv4_tunnel *tun_key;
71
72 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
aa310701
PS
73
74 skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
75 if (IS_ERR(skb))
997e068e 76 return skb;
aa310701
PS
77
78 tpi.flags = filter_tnl_flags(tun_key->tun_flags);
79 tpi.proto = htons(ETH_P_TEB);
80 tpi.key = be64_get_low32(tun_key->tun_id);
81 tpi.seq = 0;
82 gre_build_header(skb, &tpi, tunnel_hlen);
83
84 return skb;
85}
86
87static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
88{
89#ifdef __BIG_ENDIAN
90 return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
91#else
92 return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
93#endif
94}
95
96/* Called with rcu_read_lock and BH disabled. */
97static int gre_rcv(struct sk_buff *skb,
98 const struct tnl_ptk_info *tpi)
99{
f0b128c1 100 struct ovs_tunnel_info tun_info;
aa310701
PS
101 struct ovs_net *ovs_net;
102 struct vport *vport;
103 __be64 key;
104
105 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
106 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
107 if (unlikely(!vport))
108 return PACKET_REJECT;
109
110 key = key_to_tunnel_id(tpi->key, tpi->seq);
8f0aad6f 111 ovs_flow_tun_info_init(&tun_info, ip_hdr(skb), 0, 0, key,
f5796684 112 filter_tnl_flags(tpi->flags), NULL, 0);
aa310701 113
f0b128c1 114 ovs_vport_receive(vport, skb, &tun_info);
aa310701
PS
115 return PACKET_RCVD;
116}
117
e0bb8c44
WZ
118/* Called with rcu_read_lock and BH disabled. */
119static int gre_err(struct sk_buff *skb, u32 info,
120 const struct tnl_ptk_info *tpi)
121{
122 struct ovs_net *ovs_net;
123 struct vport *vport;
124
125 ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
126 vport = rcu_dereference(ovs_net->vport_net.gre_vport);
127
128 if (unlikely(!vport))
129 return PACKET_REJECT;
130 else
131 return PACKET_RCVD;
132}
133
aa310701
PS
134static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
135{
136 struct net *net = ovs_dp_get_net(vport->dp);
8c8b1b83 137 struct ovs_key_ipv4_tunnel *tun_key;
aa310701
PS
138 struct flowi4 fl;
139 struct rtable *rt;
140 int min_headroom;
141 int tunnel_hlen;
142 __be16 df;
143 int err;
144
f0b128c1 145 if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
aa310701 146 err = -EINVAL;
997e068e 147 goto err_free_skb;
aa310701
PS
148 }
149
f0b128c1 150 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
aa310701
PS
151 /* Route lookup */
152 memset(&fl, 0, sizeof(fl));
8c8b1b83
PS
153 fl.daddr = tun_key->ipv4_dst;
154 fl.saddr = tun_key->ipv4_src;
155 fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
aa310701
PS
156 fl.flowi4_mark = skb->mark;
157 fl.flowi4_proto = IPPROTO_GRE;
158
159 rt = ip_route_output_key(net, &fl);
997e068e
PS
160 if (IS_ERR(rt)) {
161 err = PTR_ERR(rt);
162 goto err_free_skb;
163 }
aa310701 164
8c8b1b83 165 tunnel_hlen = ip_gre_calc_hlen(tun_key->tun_flags);
aa310701
PS
166
167 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
168 + tunnel_hlen + sizeof(struct iphdr)
df8a39de 169 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
aa310701
PS
170 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
171 int head_delta = SKB_DATA_ALIGN(min_headroom -
172 skb_headroom(skb) +
173 16);
174 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
175 0, GFP_ATOMIC);
176 if (unlikely(err))
177 goto err_free_rt;
178 }
179
5968250c
JP
180 skb = vlan_hwaccel_push_inside(skb);
181 if (unlikely(!skb)) {
182 err = -ENOMEM;
183 goto err_free_rt;
aa310701
PS
184 }
185
186 /* Push Tunnel header. */
187 skb = __build_header(skb, tunnel_hlen);
997e068e 188 if (IS_ERR(skb)) {
4aa61188 189 err = PTR_ERR(skb);
997e068e 190 skb = NULL;
aa310701
PS
191 goto err_free_rt;
192 }
193
8c8b1b83 194 df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
aa310701
PS
195 htons(IP_DF) : 0;
196
60ff7467 197 skb->ignore_df = 1;
aa310701 198
aad88724 199 return iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
8c8b1b83
PS
200 tun_key->ipv4_dst, IPPROTO_GRE,
201 tun_key->ipv4_tos, tun_key->ipv4_ttl, df, false);
aa310701
PS
202err_free_rt:
203 ip_rt_put(rt);
997e068e
PS
204err_free_skb:
205 kfree_skb(skb);
aa310701
PS
206 return err;
207}
208
209static struct gre_cisco_protocol gre_protocol = {
210 .handler = gre_rcv,
e0bb8c44 211 .err_handler = gre_err,
aa310701
PS
212 .priority = 1,
213};
214
215static int gre_ports;
216static int gre_init(void)
217{
218 int err;
219
220 gre_ports++;
221 if (gre_ports > 1)
222 return 0;
223
224 err = gre_cisco_register(&gre_protocol);
225 if (err)
226 pr_warn("cannot register gre protocol handler\n");
227
228 return err;
229}
230
231static void gre_exit(void)
232{
233 gre_ports--;
234 if (gre_ports > 0)
235 return;
236
237 gre_cisco_unregister(&gre_protocol);
238}
239
240static const char *gre_get_name(const struct vport *vport)
241{
242 return vport_priv(vport);
243}
244
245static struct vport *gre_create(const struct vport_parms *parms)
246{
247 struct net *net = ovs_dp_get_net(parms->dp);
248 struct ovs_net *ovs_net;
249 struct vport *vport;
250 int err;
251
252 err = gre_init();
253 if (err)
254 return ERR_PTR(err);
255
256 ovs_net = net_generic(net, ovs_net_id);
257 if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
258 vport = ERR_PTR(-EEXIST);
259 goto error;
260 }
261
262 vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
263 if (IS_ERR(vport))
264 goto error;
265
266 strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
267 rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
268 return vport;
269
270error:
271 gre_exit();
272 return vport;
273}
274
275static void gre_tnl_destroy(struct vport *vport)
276{
277 struct net *net = ovs_dp_get_net(vport->dp);
278 struct ovs_net *ovs_net;
279
280 ovs_net = net_generic(net, ovs_net_id);
281
944df8ae 282 RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL);
aa310701
PS
283 ovs_vport_deferred_free(vport);
284 gre_exit();
285}
286
8f0aad6f
WZ
287static int gre_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
288 struct ovs_tunnel_info *egress_tun_info)
289{
290 return ovs_tunnel_get_egress_info(egress_tun_info,
291 ovs_dp_get_net(vport->dp),
292 OVS_CB(skb)->egress_tun_info,
293 IPPROTO_GRE, skb->mark, 0, 0);
294}
295
62b9c8d0 296static struct vport_ops ovs_gre_vport_ops = {
aa310701
PS
297 .type = OVS_VPORT_TYPE_GRE,
298 .create = gre_create,
299 .destroy = gre_tnl_destroy,
300 .get_name = gre_get_name,
301 .send = gre_tnl_send,
8f0aad6f 302 .get_egress_tun_info = gre_get_egress_tun_info,
62b9c8d0 303 .owner = THIS_MODULE,
aa310701 304};
62b9c8d0
TG
305
306static int __init ovs_gre_tnl_init(void)
307{
308 return ovs_vport_ops_register(&ovs_gre_vport_ops);
309}
310
311static void __exit ovs_gre_tnl_exit(void)
312{
313 ovs_vport_ops_unregister(&ovs_gre_vport_ops);
314}
315
316module_init(ovs_gre_tnl_init);
317module_exit(ovs_gre_tnl_exit);
318
319MODULE_DESCRIPTION("OVS: GRE switching port");
320MODULE_LICENSE("GPL");
321MODULE_ALIAS("vport-type-3");
This page took 0.216899 seconds and 5 git commands to generate.