e33cffea234f48d54ff8fb40a7bf3622148ea75e
[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
24 #include <linux/in.h>
25 #include <linux/ip.h>
26 #include <linux/net.h>
27 #include <linux/rculist.h>
28 #include <linux/udp.h>
29
30 #include <net/icmp.h>
31 #include <net/ip.h>
32 #include <net/route.h>
33 #include <net/udp.h>
34 #include <net/xfrm.h>
35
36 #include "datapath.h"
37 #include "gso.h"
38 #include "vport.h"
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 /**
98  * struct lisp_port - Keeps track of open UDP ports
99  * @dst_port: lisp UDP port no.
100  * @list: list element in @lisp_ports.
101  * @lisp_rcv_socket: The socket created for this port number.
102  * @name: vport name.
103  */
104 struct lisp_port {
105         __be16 dst_port;
106         struct list_head list;
107         struct socket *lisp_rcv_socket;
108         char name[IFNAMSIZ];
109 };
110
111 static LIST_HEAD(lisp_ports);
112
113 static inline struct lisp_port *lisp_vport(const struct vport *vport)
114 {
115         return vport_priv(vport);
116 }
117
118 static struct lisp_port *lisp_find_port(struct net *net, __be16 port)
119 {
120         struct lisp_port *lisp_port;
121
122         list_for_each_entry_rcu(lisp_port, &lisp_ports, list) {
123                 if (lisp_port->dst_port == port &&
124                         net_eq(sock_net(lisp_port->lisp_rcv_socket->sk), net))
125                         return lisp_port;
126         }
127
128         return NULL;
129 }
130
131 static inline struct lisphdr *lisp_hdr(const struct sk_buff *skb)
132 {
133         return (struct lisphdr *)(udp_hdr(skb) + 1);
134 }
135
136 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
137 static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
138 {
139
140 #ifdef __BIG_ENDIAN
141         iid[0] = (__force __u8)(tun_id >> 16);
142         iid[1] = (__force __u8)(tun_id >> 8);
143         iid[2] = (__force __u8)tun_id;
144 #else
145         iid[0] = (__force __u8)((__force u64)tun_id >> 40);
146         iid[1] = (__force __u8)((__force u64)tun_id >> 48);
147         iid[2] = (__force __u8)((__force u64)tun_id >> 56);
148 #endif
149 }
150
151 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
152 static __be64 instance_id_to_tunnel_id(__u8 *iid)
153 {
154 #ifdef __BIG_ENDIAN
155         return (iid[0] << 16) | (iid[1] << 8) | iid[2];
156 #else
157         return (__force __be64)(((__force u64)iid[0] << 40) |
158                                 ((__force u64)iid[1] << 48) |
159                                 ((__force u64)iid[2] << 56));
160 #endif
161 }
162
163 /* Compute source UDP port for outgoing packet.
164  * Currently we use the flow hash.
165  */
166 static u16 get_src_port(struct sk_buff *skb)
167 {
168         u32 hash = skb_get_rxhash(skb);
169         unsigned int range;
170         int high;
171         int low;
172
173         if (!hash) {
174                 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
175
176                 hash = jhash2((const u32 *)pkt_key,
177                             sizeof(*pkt_key) / sizeof(u32), 0);
178         }
179
180         inet_get_local_port_range(&low, &high);
181         range = (high - low) + 1;
182         return (((u64) hash * range) >> 32) + low;
183 }
184
185 static void lisp_build_header(const struct vport *vport,
186                               struct sk_buff *skb)
187 {
188         struct lisp_port *lisp_port = lisp_vport(vport);
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
193         udph->dest = lisp_port->dst_port;
194         udph->source = htons(get_src_port(skb));
195         udph->check = 0;
196         udph->len = htons(skb->len - skb_transport_offset(skb));
197
198         lisph->nonce_present = 0;       /* We don't support echo nonce algorithm */
199         lisph->locator_status_bits_present = 1; /* Set LSB */
200         lisph->solicit_echo_nonce = 0;  /* No echo noncing */
201         lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
202         lisph->instance_id_present = 1; /* Store the tun_id as Instance ID  */
203         lisph->reserved_flags = 0;      /* Reserved flags, set to 0  */
204
205         lisph->u1.nonce[0] = 0;
206         lisph->u1.nonce[1] = 0;
207         lisph->u1.nonce[2] = 0;
208
209         tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
210         lisph->u2.word2.locator_status_bits = 1;
211 }
212
213 /* Called with rcu_read_lock and BH disabled. */
214 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
215 {
216         struct lisp_port *lisp_port;
217         struct lisphdr *lisph;
218         struct iphdr *iph, *inner_iph;
219         struct ovs_key_ipv4_tunnel tun_key;
220         __be64 key;
221         struct ethhdr *ethh;
222         __be16 protocol;
223
224         lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
225         if (unlikely(!lisp_port))
226                 goto error;
227
228         if (iptunnel_pull_header(skb, LISP_HLEN, 0))
229                 goto error;
230
231         lisph = lisp_hdr(skb);
232
233         if (lisph->instance_id_present != 1)
234                 key = 0;
235         else
236                 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
237
238         /* Save outer tunnel values */
239         iph = ip_hdr(skb);
240         ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
241
242         /* Drop non-IP inner packets */
243         inner_iph = (struct iphdr *)(lisph + 1);
244         switch (inner_iph->version) {
245         case 4:
246                 protocol = htons(ETH_P_IP);
247                 break;
248         case 6:
249                 protocol = htons(ETH_P_IPV6);
250                 break;
251         default:
252                 goto error;
253         }
254         skb->protocol = protocol;
255
256         /* Add Ethernet header */
257         ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
258         memset(ethh, 0, ETH_HLEN);
259         ethh->h_dest[0] = 0x02;
260         ethh->h_source[0] = 0x02;
261         ethh->h_proto = protocol;
262
263         ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
264
265         ovs_vport_receive(vport_from_priv(lisp_port), skb, &tun_key);
266         goto out;
267
268 error:
269         kfree_skb(skb);
270 out:
271         return 0;
272 }
273
274 /* Arbitrary value.  Irrelevant as long as it's not 0 since we set the handler. */
275 #define UDP_ENCAP_LISP 1
276 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
277 {
278         struct sockaddr_in sin;
279         int err;
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 = lisp_port->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 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
313 {
314         struct lisp_port *lisp_port = lisp_vport(vport);
315
316         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(lisp_port->dst_port)))
317                 return -EMSGSIZE;
318         return 0;
319 }
320
321 static void lisp_tnl_destroy(struct vport *vport)
322 {
323         struct lisp_port *lisp_port = lisp_vport(vport);
324
325         list_del_rcu(&lisp_port->list);
326         /* Release socket */
327         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
328
329         ovs_vport_deferred_free(vport);
330 }
331
332 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
333 {
334         struct net *net = ovs_dp_get_net(parms->dp);
335         struct nlattr *options = parms->options;
336         struct lisp_port *lisp_port;
337         struct vport *vport;
338         struct nlattr *a;
339         int err;
340         u16 dst_port;
341
342         if (!options) {
343                 err = -EINVAL;
344                 goto error;
345         }
346
347         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
348         if (a && nla_len(a) == sizeof(u16)) {
349                 dst_port = nla_get_u16(a);
350         } else {
351                 /* Require destination port from userspace. */
352                 err = -EINVAL;
353                 goto error;
354         }
355
356         /* Verify if we already have a socket created for this port */
357         if (lisp_find_port(net, htons(dst_port))) {
358                 err = -EEXIST;
359                 goto error;
360         }
361
362         vport = ovs_vport_alloc(sizeof(struct lisp_port),
363                                 &ovs_lisp_vport_ops, parms);
364         if (IS_ERR(vport))
365                 return vport;
366
367         lisp_port = lisp_vport(vport);
368         lisp_port->dst_port = htons(dst_port);
369         strncpy(lisp_port->name, parms->name, IFNAMSIZ);
370
371         err = lisp_socket_init(lisp_port, net);
372         if (err)
373                 goto error_free;
374
375         list_add_tail_rcu(&lisp_port->list, &lisp_ports);
376         return vport;
377
378 error_free:
379         ovs_vport_free(vport);
380 error:
381         return ERR_PTR(err);
382 }
383
384 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,12,0)
385
386 static void lisp_fix_segment(struct sk_buff *skb)
387 {
388         struct udphdr *udph = udp_hdr(skb);
389
390         udph->len = htons(skb->len - skb_transport_offset(skb));
391 }
392
393 static int handle_offloads(struct sk_buff *skb)
394 {
395         if (skb_is_gso(skb))
396                 OVS_GSO_CB(skb)->fix_segment = lisp_fix_segment;
397         else if (skb->ip_summed != CHECKSUM_PARTIAL)
398                 skb->ip_summed = CHECKSUM_NONE;
399         return 0;
400 }
401 #else
402 static int handle_offloads(struct sk_buff *skb)
403 {
404         if (skb_is_gso(skb)) {
405                 int err = skb_unclone(skb, GFP_ATOMIC);
406                 if (unlikely(err))
407                         return err;
408
409                 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
410         } else if (skb->ip_summed != CHECKSUM_PARTIAL)
411                 skb->ip_summed = CHECKSUM_NONE;
412
413         skb->encapsulation = 1;
414         return 0;
415 }
416 #endif
417
418 static int lisp_send(struct vport *vport, struct sk_buff *skb)
419 {
420         int network_offset = skb_network_offset(skb);
421         struct rtable *rt;
422         int min_headroom;
423         __be32 saddr;
424         __be16 df;
425         int sent_len;
426         int err;
427
428         if (unlikely(!OVS_CB(skb)->tun_key))
429                 return -EINVAL;
430
431         if (skb->protocol != htons(ETH_P_IP) &&
432             skb->protocol != htons(ETH_P_IPV6)) {
433                 kfree_skb(skb);
434                 return 0;
435         }
436
437         /* Route lookup */
438         saddr = OVS_CB(skb)->tun_key->ipv4_src;
439         rt = find_route(ovs_dp_get_net(vport->dp),
440                         &saddr,
441                         OVS_CB(skb)->tun_key->ipv4_dst,
442                         IPPROTO_UDP,
443                         OVS_CB(skb)->tun_key->ipv4_tos,
444                         skb->mark);
445         if (IS_ERR(rt)) {
446                 err = PTR_ERR(rt);
447                 goto error;
448         }
449
450         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
451                         + sizeof(struct iphdr) + LISP_HLEN;
452
453         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
454                 int head_delta = SKB_DATA_ALIGN(min_headroom -
455                                                 skb_headroom(skb) +
456                                                 16);
457
458                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
459                                         0, GFP_ATOMIC);
460                 if (unlikely(err))
461                         goto err_free_rt;
462         }
463
464         /* Reset l2 headers. */
465         skb_pull(skb, network_offset);
466         skb_reset_mac_header(skb);
467         vlan_set_tci(skb, 0);
468
469         skb_reset_inner_headers(skb);
470
471         __skb_push(skb, LISP_HLEN);
472         skb_reset_transport_header(skb);
473
474         lisp_build_header(vport, skb);
475
476         /* Offloading */
477         err = handle_offloads(skb);
478         if (err)
479                 goto err_free_rt;
480
481         skb->local_df = 1;
482
483         df = OVS_CB(skb)->tun_key->tun_flags &
484                                   TUNNEL_DONT_FRAGMENT ?  htons(IP_DF) : 0;
485         sent_len = iptunnel_xmit(rt, skb,
486                              saddr, OVS_CB(skb)->tun_key->ipv4_dst,
487                              IPPROTO_UDP, OVS_CB(skb)->tun_key->ipv4_tos,
488                              OVS_CB(skb)->tun_key->ipv4_ttl, df, false);
489
490         return sent_len > 0 ? sent_len + network_offset : sent_len;
491
492 err_free_rt:
493         ip_rt_put(rt);
494 error:
495         return err;
496 }
497
498 static const char *lisp_get_name(const struct vport *vport)
499 {
500         struct lisp_port *lisp_port = lisp_vport(vport);
501         return lisp_port->name;
502 }
503
504 const struct vport_ops ovs_lisp_vport_ops = {
505         .type           = OVS_VPORT_TYPE_LISP,
506         .create         = lisp_tnl_create,
507         .destroy        = lisp_tnl_destroy,
508         .get_name       = lisp_get_name,
509         .get_options    = lisp_get_options,
510         .send           = lisp_send,
511 };