Add support for LISP tunneling
[sliver-openvswitch.git] / datapath / vport-lisp.c
1 /*
2  * Copyright (c) 2011 Nicira, Inc.
3  * Copyright (c) 2013 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
40 /*
41  *  LISP encapsulation header:
42  *
43  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44  *  |N|L|E|V|I|flags|            Nonce/Map-Version                  |
45  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46  *  |                 Instance ID/Locator Status Bits               |
47  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48  *
49  */
50
51 /**
52  * struct lisphdr - LISP header
53  * @nonce_present: Flag indicating the presence of a 24 bit nonce value.
54  * @locator_status_bits_present: Flag indicating the presence of Locator Status
55  *                               Bits (LSB).
56  * @solicit_echo_nonce: Flag indicating the use of the echo noncing mechanism.
57  * @map_version_present: Flag indicating the use of mapping versioning.
58  * @instance_id_present: Flag indicating the presence of a 24 bit Instance ID.
59  * @reserved_flags: 3 bits reserved for future flags.
60  * @nonce: 24 bit nonce value.
61  * @map_version: 24 bit mapping version.
62  * @locator_status_bits: Locator Status Bits: 32 bits when instance_id_present
63  *                       is not set, 8 bits when it is.
64  * @instance_id: 24 bit Instance ID
65  */
66 struct lisphdr {
67 #ifdef __LITTLE_ENDIAN_BITFIELD
68         __u8 reserved_flags:3;
69         __u8 instance_id_present:1;
70         __u8 map_version_present:1;
71         __u8 solicit_echo_nonce:1;
72         __u8 locator_status_bits_present:1;
73         __u8 nonce_present:1;
74 #else
75         __u8 nonce_present:1;
76         __u8 locator_status_bits_present:1;
77         __u8 solicit_echo_nonce:1;
78         __u8 map_version_present:1;
79         __u8 instance_id_present:1;
80         __u8 reserved_flags:3;
81 #endif
82         union {
83                 __u8 nonce[3];
84                 __u8 map_version[3];
85         } u1;
86         union {
87                 __be32 locator_status_bits;
88                 struct {
89                         __u8 instance_id[3];
90                         __u8 locator_status_bits;
91                 } word2;
92         } u2;
93 };
94
95 #define LISP_HLEN (sizeof(struct udphdr) + sizeof(struct lisphdr))
96
97 static inline int lisp_hdr_len(const struct tnl_mutable_config *mutable,
98                                const struct ovs_key_ipv4_tunnel *tun_key)
99 {
100         return LISP_HLEN;
101 }
102
103 /**
104  * struct lisp_port - Keeps track of open UDP ports
105  * @list: list element.
106  * @port: The UDP port number in network byte order.
107  * @socket: The socket created for this port number.
108  * @count: How many ports are using this socket/port.
109  */
110 struct lisp_port {
111         struct list_head list;
112         __be16 port;
113         struct socket *lisp_rcv_socket;
114         int count;
115 };
116
117 static LIST_HEAD(lisp_ports);
118
119 static struct lisp_port *lisp_port_exists(struct net *net, __be16 port)
120 {
121         struct lisp_port *lisp_port;
122
123         list_for_each_entry(lisp_port, &lisp_ports, list) {
124                 if (lisp_port->port == port &&
125                         net_eq(sock_net(lisp_port->lisp_rcv_socket->sk), net))
126                         return lisp_port;
127         }
128
129         return NULL;
130 }
131
132 static inline struct lisphdr *lisp_hdr(const struct sk_buff *skb)
133 {
134         return (struct lisphdr *)(udp_hdr(skb) + 1);
135 }
136
137 static int lisp_tnl_send(struct vport *vport, struct sk_buff *skb)
138 {
139         int tnl_len;
140         int network_offset = skb_network_offset(skb);
141
142         /* We only encapsulate IPv4 and IPv6 packets */
143         switch (skb->protocol) {
144         case htons(ETH_P_IP):
145         case htons(ETH_P_IPV6):
146                 /* Pop off "inner" Ethernet header */
147                 skb_pull(skb, network_offset);
148                 tnl_len = ovs_tnl_send(vport, skb);
149                 return tnl_len > 0 ? tnl_len + network_offset : tnl_len;
150         default:
151                 kfree_skb(skb);
152                 return 0;
153         }
154 }
155
156 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
157 static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
158 {
159
160 #ifdef __BIG_ENDIAN
161         iid[0] = (__force __u8)(tun_id >> 16);
162         iid[1] = (__force __u8)(tun_id >> 8);
163         iid[2] = (__force __u8)tun_id;
164 #else
165         iid[0] = (__force __u8)((__force u64)tun_id >> 40);
166         iid[1] = (__force __u8)((__force u64)tun_id >> 48);
167         iid[2] = (__force __u8)((__force u64)tun_id >> 56);
168 #endif
169 }
170
171 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
172 static __be64 instance_id_to_tunnel_id(__u8 *iid)
173 {
174 #ifdef __BIG_ENDIAN
175         return (iid[0] << 16) | (iid[1] << 8) | iid[2];
176 #else
177         return (__force __be64)(((__force u64)iid[0] << 40) |
178                                 ((__force u64)iid[1] << 48) |
179                                 ((__force u64)iid[2] << 56));
180 #endif
181 }
182
183 static struct sk_buff *lisp_build_header(const struct vport *vport,
184                                          const struct tnl_mutable_config *mutable,
185                                          struct dst_entry *dst,
186                                          struct sk_buff *skb,
187                                          int tunnel_hlen)
188 {
189         struct udphdr *udph = udp_hdr(skb);
190         struct lisphdr *lisph = (struct lisphdr *)(udph + 1);
191         const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
192         __be64 out_key;
193         u32 flags;
194
195         tnl_get_param(mutable, tun_key, &flags, &out_key);
196
197         udph->dest = mutable->dst_port;
198         udph->source = htons(ovs_tnl_get_src_port(skb));
199         udph->check = 0;
200         udph->len = htons(skb->len - skb_transport_offset(skb));
201
202         lisph->nonce_present = 0;       /* We don't support echo nonce algorithm */
203         lisph->locator_status_bits_present = 1; /* Set LSB */
204         lisph->solicit_echo_nonce = 0;  /* No echo noncing */
205         lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
206         lisph->instance_id_present = 1; /* Store the tun_id as Instance ID  */
207         lisph->reserved_flags = 0;      /* Reserved flags, set to 0  */
208
209         lisph->u1.nonce[0] = 0;
210         lisph->u1.nonce[1] = 0;
211         lisph->u1.nonce[2] = 0;
212
213         tunnel_id_to_instance_id(out_key, &lisph->u2.word2.instance_id[0]);
214         lisph->u2.word2.locator_status_bits = 1;
215
216         /*
217          * Allow our local IP stack to fragment the outer packet even if the
218          * DF bit is set as a last resort.  We also need to force selection of
219          * an IP ID here because Linux will otherwise leave it at 0 if the
220          * packet originally had DF set.
221          */
222         skb->local_df = 1;
223         __ip_select_ident(ip_hdr(skb), dst, 0);
224
225         return skb;
226 }
227
228 /* Called with rcu_read_lock and BH disabled. */
229 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
230 {
231         struct vport *vport;
232         struct lisphdr *lisph;
233         const struct tnl_mutable_config *mutable;
234         struct iphdr *iph, *inner_iph;
235         struct ovs_key_ipv4_tunnel tun_key;
236         __be64 key;
237         u32 tunnel_flags = 0;
238         struct ethhdr *ethh;
239         __be16 protocol;
240
241         if (unlikely(!pskb_may_pull(skb, LISP_HLEN)))
242                 goto error;
243
244         lisph = lisp_hdr(skb);
245
246         skb_pull_rcsum(skb, LISP_HLEN);
247
248         if (lisph->instance_id_present != 1)
249                 key = 0;
250         else
251                 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
252
253         iph = ip_hdr(skb);
254         vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr,
255                 key, TNL_T_PROTO_LISP, &mutable);
256         if (unlikely(!vport))
257                 goto error;
258
259         if (mutable->flags & TNL_F_IN_KEY_MATCH || !mutable->key.daddr)
260                 tunnel_flags = OVS_TNL_F_KEY;
261         else
262                 key = 0;
263
264         /* Save outer tunnel values */
265         tnl_tun_key_init(&tun_key, iph, key, tunnel_flags);
266         OVS_CB(skb)->tun_key = &tun_key;
267
268         /* Drop non-IP inner packets */
269         inner_iph = (struct iphdr *)(lisph + 1);
270         switch (inner_iph->version) {
271         case 4:
272                 protocol = htons(ETH_P_IP);
273                 break;
274         case 6:
275                 protocol = htons(ETH_P_IPV6);
276                 break;
277         default:
278                 goto error;
279         }
280
281         /* Add Ethernet header */
282         ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
283         memset(ethh, 0, ETH_HLEN);
284         ethh->h_dest[0] = 0x02;
285         ethh->h_source[0] = 0x02;
286         ethh->h_proto = protocol;
287
288         ovs_tnl_rcv(vport, skb);
289         goto out;
290
291 error:
292         kfree_skb(skb);
293 out:
294         return 0;
295 }
296
297 /* Arbitrary value.  Irrelevant as long as it's not 0 since we set the handler. */
298 #define UDP_ENCAP_LISP 1
299 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
300 {
301         int err;
302         struct sockaddr_in sin;
303
304         err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
305                                &lisp_port->lisp_rcv_socket);
306         if (err)
307                 goto error;
308
309         /* release net ref. */
310         sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
311
312         sin.sin_family = AF_INET;
313         sin.sin_addr.s_addr = htonl(INADDR_ANY);
314         sin.sin_port = lisp_port->port;
315
316         err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
317                           sizeof(struct sockaddr_in));
318         if (err)
319                 goto error_sock;
320
321         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
322         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
323
324         udp_encap_enable();
325
326         return 0;
327
328 error_sock:
329         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
330 error:
331         pr_warn("cannot register lisp protocol handler: %d\n", err);
332         return err;
333 }
334
335 static void lisp_tunnel_release(struct lisp_port *lisp_port)
336 {
337         lisp_port->count--;
338
339         if (lisp_port->count == 0) {
340                 /* Release old socket */
341                 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
342                 list_del(&lisp_port->list);
343                 kfree(lisp_port);
344         }
345 }
346
347 static int lisp_tunnel_setup(struct net *net, struct nlattr *options,
348                              struct lisp_port **lport)
349 {
350         struct nlattr *a;
351         int err;
352         u16 dst_port;
353         struct lisp_port *lisp_port = NULL;
354
355         *lport = NULL;
356
357         if (!options) {
358                 err = -EINVAL;
359                 goto out;
360         }
361
362         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
363         if (a && nla_len(a) == sizeof(u16)) {
364                 dst_port = nla_get_u16(a);
365         } else {
366                 /* Require destination port from userspace. */
367                 err = -EINVAL;
368                 goto out;
369         }
370
371         /* Verify if we already have a socket created for this port */
372         lisp_port = lisp_port_exists(net, htons(dst_port));
373         if (lisp_port) {
374                 lisp_port->count++;
375                 err = 0;
376                 *lport = lisp_port;
377                 goto out;
378         }
379
380         /* Add a new socket for this port */
381         lisp_port = kzalloc(sizeof(struct lisp_port), GFP_KERNEL);
382         if (!lisp_port) {
383                 err = -ENOMEM;
384                 goto out;
385         }
386
387         lisp_port->port = htons(dst_port);
388         lisp_port->count = 1;
389         list_add_tail(&lisp_port->list, &lisp_ports);
390
391         err = lisp_socket_init(lisp_port, net);
392         if (err)
393                 goto error;
394
395         *lport = lisp_port;
396         goto out;
397
398 error:
399         list_del(&lisp_port->list);
400         kfree(lisp_port);
401 out:
402         return err;
403 }
404
405 static int lisp_tnl_set_options(struct vport *vport, struct nlattr *options)
406 {
407         int err;
408         struct net *net = ovs_dp_get_net(vport->dp);
409         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
410         struct tnl_mutable_config *config;
411         struct lisp_port *old_port = NULL;
412         struct lisp_port *lisp_port = NULL;
413
414         config = rtnl_dereference(tnl_vport->mutable);
415
416         old_port = lisp_port_exists(net, config->dst_port);
417
418         err = lisp_tunnel_setup(net, options, &lisp_port);
419         if (err)
420                 goto out;
421
422         err = ovs_tnl_set_options(vport, options);
423
424         if (err)
425                 lisp_tunnel_release(lisp_port);
426         else {
427                 /* Release old socket */
428                 lisp_tunnel_release(old_port);
429         }
430 out:
431         return err;
432 }
433
434 static const struct tnl_ops ovs_lisp_tnl_ops = {
435         .tunnel_type    = TNL_T_PROTO_LISP,
436         .ipproto        = IPPROTO_UDP,
437         .hdr_len        = lisp_hdr_len,
438         .build_header   = lisp_build_header,
439 };
440
441 static void lisp_tnl_destroy(struct vport *vport)
442 {
443         struct lisp_port *lisp_port;
444         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
445         struct tnl_mutable_config *config;
446
447         config = rtnl_dereference(tnl_vport->mutable);
448
449         lisp_port = lisp_port_exists(ovs_dp_get_net(vport->dp),
450                                      config->dst_port);
451
452         lisp_tunnel_release(lisp_port);
453
454         ovs_tnl_destroy(vport);
455 }
456
457 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
458 {
459         int err;
460         struct vport *vport;
461         struct lisp_port *lisp_port = NULL;
462
463         err = lisp_tunnel_setup(ovs_dp_get_net(parms->dp), parms->options,
464                                 &lisp_port);
465         if (err)
466                 return ERR_PTR(err);
467
468         vport = ovs_tnl_create(parms, &ovs_lisp_vport_ops, &ovs_lisp_tnl_ops);
469
470         if (IS_ERR(vport))
471                 lisp_tunnel_release(lisp_port);
472
473         return vport;
474 }
475
476 const struct vport_ops ovs_lisp_vport_ops = {
477         .type           = OVS_VPORT_TYPE_LISP,
478         .flags          = VPORT_F_TUN_ID,
479         .create         = lisp_tnl_create,
480         .destroy        = lisp_tnl_destroy,
481         .get_name       = ovs_tnl_get_name,
482         .get_options    = ovs_tnl_get_options,
483         .set_options    = lisp_tnl_set_options,
484         .send           = lisp_tnl_send,
485 };
486 #else
487 #warning LISP tunneling will not be available on kernels before 2.6.26
488 #endif /* Linux kernel < 2.6.26 */