Tunnel: Cleanup old tunnel infrastructure.
[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/net.h>
28 #include <linux/rculist.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 ovs_key_ipv4_tunnel *tun_key)
98 {
99         return LISP_HLEN;
100 }
101
102 /**
103  * struct lisp_port - Keeps track of open UDP ports
104  * @list: list element.
105  * @vport: vport for the tunnel.
106  * @socket: The socket created for this port number.
107  */
108 struct lisp_port {
109         struct list_head list;
110         struct vport *vport;
111         struct socket *lisp_rcv_socket;
112         struct rcu_head rcu;
113 };
114
115 static LIST_HEAD(lisp_ports);
116
117 static struct lisp_port *lisp_find_port(struct net *net, __be16 port)
118 {
119         struct lisp_port *lisp_port;
120
121         list_for_each_entry_rcu(lisp_port, &lisp_ports, list) {
122                 struct tnl_vport *tnl_vport = tnl_vport_priv(lisp_port->vport);
123
124                 if (tnl_vport->dst_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 void lisp_build_header(const struct vport *vport,
184                               struct sk_buff *skb,
185                               int tunnel_hlen)
186 {
187         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
188         struct udphdr *udph = udp_hdr(skb);
189         struct lisphdr *lisph = (struct lisphdr *)(udph + 1);
190         const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
191
192         udph->dest = tnl_vport->dst_port;
193         udph->source = htons(ovs_tnl_get_src_port(skb));
194         udph->check = 0;
195         udph->len = htons(skb->len - skb_transport_offset(skb));
196
197         lisph->nonce_present = 0;       /* We don't support echo nonce algorithm */
198         lisph->locator_status_bits_present = 1; /* Set LSB */
199         lisph->solicit_echo_nonce = 0;  /* No echo noncing */
200         lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
201         lisph->instance_id_present = 1; /* Store the tun_id as Instance ID  */
202         lisph->reserved_flags = 0;      /* Reserved flags, set to 0  */
203
204         lisph->u1.nonce[0] = 0;
205         lisph->u1.nonce[1] = 0;
206         lisph->u1.nonce[2] = 0;
207
208         tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
209         lisph->u2.word2.locator_status_bits = 1;
210
211         /*
212          * Allow our local IP stack to fragment the outer packet even if the
213          * DF bit is set as a last resort.  We also need to force selection of
214          * an IP ID here because Linux will otherwise leave it at 0 if the
215          * packet originally had DF set.
216          */
217         skb->local_df = 1;
218         __ip_select_ident(ip_hdr(skb), skb_dst(skb), 0);
219 }
220
221 /* Called with rcu_read_lock and BH disabled. */
222 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
223 {
224         struct lisp_port *lisp_port;
225         struct lisphdr *lisph;
226         struct iphdr *iph, *inner_iph;
227         struct ovs_key_ipv4_tunnel tun_key;
228         __be64 key;
229         struct ethhdr *ethh;
230         __be16 protocol;
231
232         lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
233         if (unlikely(!lisp_port))
234                 goto error;
235
236         if (unlikely(!pskb_may_pull(skb, LISP_HLEN)))
237                 goto error;
238
239         lisph = lisp_hdr(skb);
240
241         skb_pull_rcsum(skb, LISP_HLEN);
242
243         if (lisph->instance_id_present != 1)
244                 key = 0;
245         else
246                 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
247
248         /* Save outer tunnel values */
249         iph = ip_hdr(skb);
250         tnl_tun_key_init(&tun_key, iph, key, OVS_TNL_F_KEY);
251         OVS_CB(skb)->tun_key = &tun_key;
252
253         /* Drop non-IP inner packets */
254         inner_iph = (struct iphdr *)(lisph + 1);
255         switch (inner_iph->version) {
256         case 4:
257                 protocol = htons(ETH_P_IP);
258                 break;
259         case 6:
260                 protocol = htons(ETH_P_IPV6);
261                 break;
262         default:
263                 goto error;
264         }
265
266         /* Add Ethernet header */
267         ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
268         memset(ethh, 0, ETH_HLEN);
269         ethh->h_dest[0] = 0x02;
270         ethh->h_source[0] = 0x02;
271         ethh->h_proto = protocol;
272
273         ovs_tnl_rcv(lisp_port->vport, skb);
274         goto out;
275
276 error:
277         kfree_skb(skb);
278 out:
279         return 0;
280 }
281
282 /* Arbitrary value.  Irrelevant as long as it's not 0 since we set the handler. */
283 #define UDP_ENCAP_LISP 1
284 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
285 {
286         int err;
287         struct sockaddr_in sin;
288         struct tnl_vport *tnl_vport = tnl_vport_priv(lisp_port->vport);
289
290         err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
291                                &lisp_port->lisp_rcv_socket);
292         if (err)
293                 goto error;
294
295         /* release net ref. */
296         sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
297
298         sin.sin_family = AF_INET;
299         sin.sin_addr.s_addr = htonl(INADDR_ANY);
300         sin.sin_port = tnl_vport->dst_port;
301
302         err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
303                           sizeof(struct sockaddr_in));
304         if (err)
305                 goto error_sock;
306
307         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
308         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
309
310         udp_encap_enable();
311
312         return 0;
313
314 error_sock:
315         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
316 error:
317         pr_warn("cannot register lisp protocol handler: %d\n", err);
318         return err;
319 }
320
321
322 static void free_port_rcu(struct rcu_head *rcu)
323 {
324         struct lisp_port *lisp_port = container_of(rcu,
325                         struct lisp_port, rcu);
326
327         kfree(lisp_port);
328 }
329
330 static void lisp_tunnel_release(struct lisp_port *lisp_port)
331 {
332         if (!lisp_port)
333                 return;
334         list_del_rcu(&lisp_port->list);
335         /* Release socket */
336         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
337         call_rcu(&lisp_port->rcu, free_port_rcu);
338 }
339
340 static int lisp_tunnel_setup(struct net *net, struct vport *vport,
341                              struct nlattr *options)
342 {
343         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
344         struct lisp_port *lisp_port;
345         struct nlattr *a;
346         int err;
347         u16 dst_port;
348
349         if (!options) {
350                 err = -EINVAL;
351                 goto out;
352         }
353
354         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
355         if (a && nla_len(a) == sizeof(u16)) {
356                 dst_port = nla_get_u16(a);
357         } else {
358                 /* Require destination port from userspace. */
359                 err = -EINVAL;
360                 goto out;
361         }
362
363         /* Verify if we already have a socket created for this port */
364         lisp_port = lisp_find_port(net, htons(dst_port));
365         if (lisp_port) {
366                 err = -EEXIST;
367                 goto out;
368         }
369
370         /* Add a new socket for this port */
371         lisp_port = kzalloc(sizeof(struct lisp_port), GFP_KERNEL);
372         if (!lisp_port) {
373                 err = -ENOMEM;
374                 goto out;
375         }
376
377         tnl_vport->dst_port = htons(dst_port);
378         lisp_port->vport = vport;
379         list_add_tail_rcu(&lisp_port->list, &lisp_ports);
380
381         err = lisp_socket_init(lisp_port, net);
382         if (err)
383                 goto error;
384
385         return 0;
386
387 error:
388         list_del_rcu(&lisp_port->list);
389         kfree(lisp_port);
390 out:
391         return err;
392 }
393
394 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
395 {
396         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
397
398         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(tnl_vport->dst_port)))
399                 return -EMSGSIZE;
400         return 0;
401 }
402
403 static const struct tnl_ops ovs_lisp_tnl_ops = {
404         .ipproto        = IPPROTO_UDP,
405         .hdr_len        = lisp_hdr_len,
406         .build_header   = lisp_build_header,
407 };
408
409 static void lisp_tnl_destroy(struct vport *vport)
410 {
411         struct lisp_port *lisp_port;
412         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
413
414         lisp_port = lisp_find_port(ovs_dp_get_net(vport->dp),
415                                    tnl_vport->dst_port);
416
417         lisp_tunnel_release(lisp_port);
418         ovs_tnl_destroy(vport);
419 }
420
421 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
422 {
423         struct vport *vport;
424         int err;
425
426         vport = ovs_tnl_create(parms, &ovs_lisp_vport_ops, &ovs_lisp_tnl_ops);
427         if (IS_ERR(vport))
428                 return vport;
429
430         err = lisp_tunnel_setup(ovs_dp_get_net(parms->dp), vport,
431                                 parms->options);
432         if (err) {
433                 ovs_tnl_destroy(vport);
434                 return ERR_PTR(err);
435         }
436
437         return vport;
438 }
439
440 const struct vport_ops ovs_lisp_vport_ops = {
441         .type           = OVS_VPORT_TYPE_LISP,
442         .flags          = VPORT_F_TUN_ID,
443         .create         = lisp_tnl_create,
444         .destroy        = lisp_tnl_destroy,
445         .get_name       = ovs_tnl_get_name,
446         .get_options    = lisp_get_options,
447         .send           = lisp_tnl_send,
448 };
449 #else
450 #warning LISP tunneling will not be available on kernels before 2.6.26
451 #endif /* Linux kernel < 2.6.26 */