datapath: Factor out common code from *_build_header() to ovs_tnl_send().
[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 /* Called with rcu_read_lock and BH disabled. */
213 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
214 {
215         struct lisp_port *lisp_port;
216         struct lisphdr *lisph;
217         struct iphdr *iph, *inner_iph;
218         struct ovs_key_ipv4_tunnel tun_key;
219         __be64 key;
220         struct ethhdr *ethh;
221         __be16 protocol;
222
223         lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
224         if (unlikely(!lisp_port))
225                 goto error;
226
227         if (unlikely(!pskb_may_pull(skb, LISP_HLEN)))
228                 goto error;
229
230         lisph = lisp_hdr(skb);
231
232         skb_pull_rcsum(skb, LISP_HLEN);
233
234         if (lisph->instance_id_present != 1)
235                 key = 0;
236         else
237                 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
238
239         /* Save outer tunnel values */
240         iph = ip_hdr(skb);
241         tnl_tun_key_init(&tun_key, iph, key, OVS_TNL_F_KEY);
242         OVS_CB(skb)->tun_key = &tun_key;
243
244         /* Drop non-IP inner packets */
245         inner_iph = (struct iphdr *)(lisph + 1);
246         switch (inner_iph->version) {
247         case 4:
248                 protocol = htons(ETH_P_IP);
249                 break;
250         case 6:
251                 protocol = htons(ETH_P_IPV6);
252                 break;
253         default:
254                 goto error;
255         }
256
257         /* Add Ethernet header */
258         ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
259         memset(ethh, 0, ETH_HLEN);
260         ethh->h_dest[0] = 0x02;
261         ethh->h_source[0] = 0x02;
262         ethh->h_proto = protocol;
263
264         ovs_tnl_rcv(lisp_port->vport, skb);
265         goto out;
266
267 error:
268         kfree_skb(skb);
269 out:
270         return 0;
271 }
272
273 /* Arbitrary value.  Irrelevant as long as it's not 0 since we set the handler. */
274 #define UDP_ENCAP_LISP 1
275 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
276 {
277         int err;
278         struct sockaddr_in sin;
279         struct tnl_vport *tnl_vport = tnl_vport_priv(lisp_port->vport);
280
281         err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
282                                &lisp_port->lisp_rcv_socket);
283         if (err)
284                 goto error;
285
286         /* release net ref. */
287         sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
288
289         sin.sin_family = AF_INET;
290         sin.sin_addr.s_addr = htonl(INADDR_ANY);
291         sin.sin_port = tnl_vport->dst_port;
292
293         err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
294                           sizeof(struct sockaddr_in));
295         if (err)
296                 goto error_sock;
297
298         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
299         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
300
301         udp_encap_enable();
302
303         return 0;
304
305 error_sock:
306         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
307 error:
308         pr_warn("cannot register lisp protocol handler: %d\n", err);
309         return err;
310 }
311
312
313 static void free_port_rcu(struct rcu_head *rcu)
314 {
315         struct lisp_port *lisp_port = container_of(rcu,
316                         struct lisp_port, rcu);
317
318         kfree(lisp_port);
319 }
320
321 static void lisp_tunnel_release(struct lisp_port *lisp_port)
322 {
323         if (!lisp_port)
324                 return;
325         list_del_rcu(&lisp_port->list);
326         /* Release socket */
327         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
328         call_rcu(&lisp_port->rcu, free_port_rcu);
329 }
330
331 static int lisp_tunnel_setup(struct net *net, struct vport *vport,
332                              struct nlattr *options)
333 {
334         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
335         struct lisp_port *lisp_port;
336         struct nlattr *a;
337         int err;
338         u16 dst_port;
339
340         if (!options) {
341                 err = -EINVAL;
342                 goto out;
343         }
344
345         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
346         if (a && nla_len(a) == sizeof(u16)) {
347                 dst_port = nla_get_u16(a);
348         } else {
349                 /* Require destination port from userspace. */
350                 err = -EINVAL;
351                 goto out;
352         }
353
354         /* Verify if we already have a socket created for this port */
355         lisp_port = lisp_find_port(net, htons(dst_port));
356         if (lisp_port) {
357                 err = -EEXIST;
358                 goto out;
359         }
360
361         /* Add a new socket for this port */
362         lisp_port = kzalloc(sizeof(struct lisp_port), GFP_KERNEL);
363         if (!lisp_port) {
364                 err = -ENOMEM;
365                 goto out;
366         }
367
368         tnl_vport->dst_port = htons(dst_port);
369         lisp_port->vport = vport;
370         list_add_tail_rcu(&lisp_port->list, &lisp_ports);
371
372         err = lisp_socket_init(lisp_port, net);
373         if (err)
374                 goto error;
375
376         return 0;
377
378 error:
379         list_del_rcu(&lisp_port->list);
380         kfree(lisp_port);
381 out:
382         return err;
383 }
384
385 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
386 {
387         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
388
389         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(tnl_vport->dst_port)))
390                 return -EMSGSIZE;
391         return 0;
392 }
393
394 static const struct tnl_ops ovs_lisp_tnl_ops = {
395         .ipproto        = IPPROTO_UDP,
396         .hdr_len        = lisp_hdr_len,
397         .build_header   = lisp_build_header,
398 };
399
400 static void lisp_tnl_destroy(struct vport *vport)
401 {
402         struct lisp_port *lisp_port;
403         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
404
405         lisp_port = lisp_find_port(ovs_dp_get_net(vport->dp),
406                                    tnl_vport->dst_port);
407
408         lisp_tunnel_release(lisp_port);
409         ovs_tnl_destroy(vport);
410 }
411
412 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
413 {
414         struct vport *vport;
415         int err;
416
417         vport = ovs_tnl_create(parms, &ovs_lisp_vport_ops, &ovs_lisp_tnl_ops);
418         if (IS_ERR(vport))
419                 return vport;
420
421         err = lisp_tunnel_setup(ovs_dp_get_net(parms->dp), vport,
422                                 parms->options);
423         if (err) {
424                 ovs_tnl_destroy(vport);
425                 return ERR_PTR(err);
426         }
427
428         return vport;
429 }
430
431 const struct vport_ops ovs_lisp_vport_ops = {
432         .type           = OVS_VPORT_TYPE_LISP,
433         .flags          = VPORT_F_TUN_ID,
434         .create         = lisp_tnl_create,
435         .destroy        = lisp_tnl_destroy,
436         .get_name       = ovs_tnl_get_name,
437         .get_options    = lisp_get_options,
438         .send           = lisp_tnl_send,
439 };
440 #else
441 #warning LISP tunneling will not be available on kernels before 2.6.26
442 #endif /* Linux kernel < 2.6.26 */