Add support for LISP tunneling
[sliver-openvswitch.git] / datapath / vport-vxlan.c
1 /*
2  * Copyright (c) 2011 Nicira, Inc.
3  * Copyright (c) 2012 Cisco Systems, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA
18  */
19
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/version.h>
23 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
24
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/list.h>
28 #include <linux/net.h>
29 #include <linux/udp.h>
30
31 #include <net/icmp.h>
32 #include <net/ip.h>
33 #include <net/udp.h>
34
35 #include "datapath.h"
36 #include "tunnel.h"
37 #include "vport.h"
38
39 #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
40
41 /**
42  * struct vxlanhdr - VXLAN header
43  * @vx_flags: Must have the exact value %VXLAN_FLAGS.
44  * @vx_vni: VXLAN Network Identifier (VNI) in top 24 bits, low 8 bits zeroed.
45  */
46 struct vxlanhdr {
47         __be32 vx_flags;
48         __be32 vx_vni;
49 };
50
51 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
52
53 static inline int vxlan_hdr_len(const struct tnl_mutable_config *mutable,
54                                 const struct ovs_key_ipv4_tunnel *tun_key)
55 {
56         return VXLAN_HLEN;
57 }
58
59 /**
60  * struct vxlan_port - Keeps track of open UDP ports
61  * @list: list element.
62  * @port: The UDP port number in network byte order.
63  * @socket: The socket created for this port number.
64  * @count: How many ports are using this socket/port.
65  */
66 struct vxlan_port {
67         struct list_head list;
68         __be16 port;
69         struct socket *vxlan_rcv_socket;
70         int count;
71 };
72
73 static LIST_HEAD(vxlan_ports);
74
75 static struct vxlan_port *vxlan_port_exists(struct net *net, __be16 port)
76 {
77         struct vxlan_port *vxlan_port;
78
79         list_for_each_entry(vxlan_port, &vxlan_ports, list) {
80                 if (vxlan_port->port == port &&
81                         net_eq(sock_net(vxlan_port->vxlan_rcv_socket->sk), net))
82                         return vxlan_port;
83         }
84
85         return NULL;
86 }
87
88 static inline struct vxlanhdr *vxlan_hdr(const struct sk_buff *skb)
89 {
90         return (struct vxlanhdr *)(udp_hdr(skb) + 1);
91 }
92
93 static struct sk_buff *vxlan_build_header(const struct vport *vport,
94                                           const struct tnl_mutable_config *mutable,
95                                           struct dst_entry *dst,
96                                           struct sk_buff *skb,
97                                           int tunnel_hlen)
98 {
99         struct udphdr *udph = udp_hdr(skb);
100         struct vxlanhdr *vxh = (struct vxlanhdr *)(udph + 1);
101         const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
102         __be64 out_key;
103         u32 flags;
104
105         tnl_get_param(mutable, tun_key, &flags, &out_key);
106
107         udph->dest = mutable->dst_port;
108         udph->source = htons(ovs_tnl_get_src_port(skb));
109         udph->check = 0;
110         udph->len = htons(skb->len - skb_transport_offset(skb));
111
112         vxh->vx_flags = htonl(VXLAN_FLAGS);
113         vxh->vx_vni = htonl(be64_to_cpu(out_key) << 8);
114
115         /*
116          * Allow our local IP stack to fragment the outer packet even if the
117          * DF bit is set as a last resort.  We also need to force selection of
118          * an IP ID here because Linux will otherwise leave it at 0 if the
119          * packet originally had DF set.
120          */
121         skb->local_df = 1;
122         __ip_select_ident(ip_hdr(skb), dst, 0);
123
124         return skb;
125 }
126
127 /* Called with rcu_read_lock and BH disabled. */
128 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
129 {
130         struct vport *vport;
131         struct vxlanhdr *vxh;
132         const struct tnl_mutable_config *mutable;
133         struct iphdr *iph;
134         struct ovs_key_ipv4_tunnel tun_key;
135         __be64 key;
136         u32 tunnel_flags = 0;
137
138         if (unlikely(!pskb_may_pull(skb, VXLAN_HLEN + ETH_HLEN)))
139                 goto error;
140
141         vxh = vxlan_hdr(skb);
142         if (unlikely(vxh->vx_flags != htonl(VXLAN_FLAGS) ||
143                      vxh->vx_vni & htonl(0xff)))
144                 goto error;
145
146         __skb_pull(skb, VXLAN_HLEN);
147         skb_postpull_rcsum(skb, skb_transport_header(skb), VXLAN_HLEN + ETH_HLEN);
148
149         key = cpu_to_be64(ntohl(vxh->vx_vni) >> 8);
150
151         iph = ip_hdr(skb);
152         vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr,
153                 key, TNL_T_PROTO_VXLAN, &mutable);
154         if (unlikely(!vport))
155                 goto error;
156
157         if (mutable->flags & TNL_F_IN_KEY_MATCH || !mutable->key.daddr)
158                 tunnel_flags = OVS_TNL_F_KEY;
159         else
160                 key = 0;
161
162         /* Save outer tunnel values */
163         tnl_tun_key_init(&tun_key, iph, key, tunnel_flags);
164         OVS_CB(skb)->tun_key = &tun_key;
165
166         ovs_tnl_rcv(vport, skb);
167         goto out;
168
169 error:
170         kfree_skb(skb);
171 out:
172         return 0;
173 }
174
175 /* Random value.  Irrelevant as long as it's not 0 since we set the handler. */
176 #define UDP_ENCAP_VXLAN 1
177 static int vxlan_socket_init(struct vxlan_port *vxlan_port, struct net *net)
178 {
179         int err;
180         struct sockaddr_in sin;
181
182         err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
183                                &vxlan_port->vxlan_rcv_socket);
184         if (err)
185                 goto error;
186
187         /* release net ref. */
188         sk_change_net(vxlan_port->vxlan_rcv_socket->sk, net);
189
190         sin.sin_family = AF_INET;
191         sin.sin_addr.s_addr = htonl(INADDR_ANY);
192         sin.sin_port = vxlan_port->port;
193
194         err = kernel_bind(vxlan_port->vxlan_rcv_socket, (struct sockaddr *)&sin,
195                           sizeof(struct sockaddr_in));
196         if (err)
197                 goto error_sock;
198
199         udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_type = UDP_ENCAP_VXLAN;
200         udp_sk(vxlan_port->vxlan_rcv_socket->sk)->encap_rcv = vxlan_rcv;
201
202         udp_encap_enable();
203
204         return 0;
205
206 error_sock:
207         sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
208 error:
209         pr_warn("cannot register vxlan protocol handler\n");
210         return err;
211 }
212
213 static void vxlan_tunnel_release(struct vxlan_port *vxlan_port)
214 {
215         vxlan_port->count--;
216
217         if (vxlan_port->count == 0) {
218                 /* Release old socket */
219                 sk_release_kernel(vxlan_port->vxlan_rcv_socket->sk);
220                 list_del(&vxlan_port->list);
221                 kfree(vxlan_port);
222         }
223 }
224 static int vxlan_tunnel_setup(struct net *net, struct nlattr *options,
225                               struct vxlan_port **vxport)
226 {
227         struct nlattr *a;
228         int err;
229         u16 dst_port;
230         struct vxlan_port *vxlan_port = NULL;
231
232         *vxport = NULL;
233
234         if (!options) {
235                 err = -EINVAL;
236                 goto out;
237         }
238
239         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
240         if (a && nla_len(a) == sizeof(u16)) {
241                 dst_port = nla_get_u16(a);
242         } else {
243                 /* Require destination port from userspace. */
244                 err = -EINVAL;
245                 goto out;
246         }
247
248         /* Verify if we already have a socket created for this port */
249         vxlan_port = vxlan_port_exists(net, htons(dst_port));
250         if (vxlan_port) {
251                 vxlan_port->count++;
252                 err = 0;
253                 *vxport = vxlan_port;
254                 goto out;
255         }
256
257         /* Add a new socket for this port */
258         vxlan_port = kzalloc(sizeof(struct vxlan_port), GFP_KERNEL);
259         if (!vxlan_port) {
260                 err = -ENOMEM;
261                 goto out;
262         }
263
264         vxlan_port->port = htons(dst_port);
265         vxlan_port->count = 1;
266         list_add_tail(&vxlan_port->list, &vxlan_ports);
267
268         err = vxlan_socket_init(vxlan_port, net);
269         if (err)
270                 goto error;
271
272         *vxport = vxlan_port;
273         goto out;
274
275 error:
276         list_del(&vxlan_port->list);
277         kfree(vxlan_port);
278 out:
279         return err;
280 }
281
282 static int vxlan_set_options(struct vport *vport, struct nlattr *options)
283 {
284         int err;
285         struct net *net = ovs_dp_get_net(vport->dp);
286         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
287         struct tnl_mutable_config *config;
288         struct vxlan_port *old_port = NULL;
289         struct vxlan_port *vxlan_port = NULL;
290
291         config = rtnl_dereference(tnl_vport->mutable);
292
293         old_port = vxlan_port_exists(net, config->dst_port);
294
295         err = vxlan_tunnel_setup(net, options, &vxlan_port);
296         if (err)
297                 goto out;
298
299         err = ovs_tnl_set_options(vport, options);
300
301         if (err)
302                 vxlan_tunnel_release(vxlan_port);
303         else {
304                 /* Release old socket */
305                 vxlan_tunnel_release(old_port);
306         }
307 out:
308         return err;
309 }
310
311 static const struct tnl_ops ovs_vxlan_tnl_ops = {
312         .tunnel_type    = TNL_T_PROTO_VXLAN,
313         .ipproto        = IPPROTO_UDP,
314         .hdr_len        = vxlan_hdr_len,
315         .build_header   = vxlan_build_header,
316 };
317
318 static void vxlan_tnl_destroy(struct vport *vport)
319 {
320         struct vxlan_port *vxlan_port;
321         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
322         struct tnl_mutable_config *config;
323
324         config = rtnl_dereference(tnl_vport->mutable);
325
326         vxlan_port = vxlan_port_exists(ovs_dp_get_net(vport->dp),
327                                          config->dst_port);
328
329         vxlan_tunnel_release(vxlan_port);
330
331         ovs_tnl_destroy(vport);
332 }
333
334 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
335 {
336         int err;
337         struct vport *vport;
338         struct vxlan_port *vxlan_port = NULL;
339
340         err = vxlan_tunnel_setup(ovs_dp_get_net(parms->dp), parms->options,
341                                  &vxlan_port);
342         if (err)
343                 return ERR_PTR(err);
344
345         vport = ovs_tnl_create(parms, &ovs_vxlan_vport_ops, &ovs_vxlan_tnl_ops);
346
347         if (IS_ERR(vport))
348                 vxlan_tunnel_release(vxlan_port);
349
350         return vport;
351 }
352
353 const struct vport_ops ovs_vxlan_vport_ops = {
354         .type           = OVS_VPORT_TYPE_VXLAN,
355         .flags          = VPORT_F_TUN_ID,
356         .create         = vxlan_tnl_create,
357         .destroy        = vxlan_tnl_destroy,
358         .get_name       = ovs_tnl_get_name,
359         .get_options    = ovs_tnl_get_options,
360         .set_options    = vxlan_set_options,
361         .send           = ovs_tnl_send,
362 };
363 #else
364 #warning VXLAN tunneling will not be available on kernels before 2.6.26
365 #endif /* Linux kernel < 2.6.26 */