ad5cd23e3e0b783a87022fa23d81d1a15410b4a4
[cascardo/ovs.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 net *net, struct sk_buff *skb)
167 {
168         u32 hash = skb_get_hash(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                 if (skb->protocol == htons(ETH_P_IP))
177                         hash = jhash2((const u32 *)&pkt_key->ipv4.addr,
178                                    sizeof(pkt_key->ipv4.addr) / sizeof(u32), 0);
179                 else if (skb->protocol == htons(ETH_P_IPV6))
180                         hash = jhash2((const u32 *)&pkt_key->ipv6.addr,
181                                    sizeof(pkt_key->ipv6.addr) / sizeof(u32), 0);
182                 else
183                         pr_warn_once("LISP inner protocol is not IP when "
184                                      "calculating hash.\n");
185         }
186
187         inet_get_local_port_range(net, &low, &high);
188         range = (high - low) + 1;
189         return (((u64) hash * range) >> 32) + low;
190 }
191
192 static void lisp_build_header(const struct vport *vport,
193                               struct sk_buff *skb)
194 {
195         struct net *net = ovs_dp_get_net(vport->dp);
196         struct lisp_port *lisp_port = lisp_vport(vport);
197         struct udphdr *udph = udp_hdr(skb);
198         struct lisphdr *lisph = (struct lisphdr *)(udph + 1);
199         const struct ovs_key_ipv4_tunnel *tun_key;
200
201         tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
202         udph->dest = lisp_port->dst_port;
203         udph->source = htons(get_src_port(net, skb));
204         udph->check = 0;
205         udph->len = htons(skb->len - skb_transport_offset(skb));
206
207         lisph->nonce_present = 0;       /* We don't support echo nonce algorithm */
208         lisph->locator_status_bits_present = 1; /* Set LSB */
209         lisph->solicit_echo_nonce = 0;  /* No echo noncing */
210         lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
211         lisph->instance_id_present = 1; /* Store the tun_id as Instance ID  */
212         lisph->reserved_flags = 0;      /* Reserved flags, set to 0  */
213
214         lisph->u1.nonce[0] = 0;
215         lisph->u1.nonce[1] = 0;
216         lisph->u1.nonce[2] = 0;
217
218         tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
219         lisph->u2.word2.locator_status_bits = 1;
220 }
221
222 /* Called with rcu_read_lock and BH disabled. */
223 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
224 {
225         struct lisp_port *lisp_port;
226         struct lisphdr *lisph;
227         struct iphdr *iph, *inner_iph;
228         struct ovs_tunnel_info tun_info;
229         __be64 key;
230         struct ethhdr *ethh;
231         __be16 protocol;
232
233         lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
234         if (unlikely(!lisp_port))
235                 goto error;
236
237         if (iptunnel_pull_header(skb, LISP_HLEN, 0))
238                 goto error;
239
240         lisph = lisp_hdr(skb);
241
242         if (lisph->instance_id_present != 1)
243                 key = 0;
244         else
245                 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
246
247         /* Save outer tunnel values */
248         iph = ip_hdr(skb);
249         ovs_flow_tun_info_init(&tun_info, iph,
250                                udp_hdr(skb)->source, udp_hdr(skb)->dest,
251                                key, TUNNEL_KEY, NULL, 0);
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         skb->protocol = protocol;
266
267         /* Add Ethernet header */
268         ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
269         memset(ethh, 0, ETH_HLEN);
270         ethh->h_dest[0] = 0x02;
271         ethh->h_source[0] = 0x02;
272         ethh->h_proto = protocol;
273
274         ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
275
276         ovs_vport_receive(vport_from_priv(lisp_port), skb, &tun_info);
277         goto out;
278
279 error:
280         kfree_skb(skb);
281 out:
282         return 0;
283 }
284
285 /* Arbitrary value.  Irrelevant as long as it's not 0 since we set the handler. */
286 #define UDP_ENCAP_LISP 1
287 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
288 {
289         struct sockaddr_in sin;
290         int err;
291
292         err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
293                                &lisp_port->lisp_rcv_socket);
294         if (err)
295                 goto error;
296
297         /* release net ref. */
298         sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
299
300         sin.sin_family = AF_INET;
301         sin.sin_addr.s_addr = htonl(INADDR_ANY);
302         sin.sin_port = lisp_port->dst_port;
303
304         err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
305                           sizeof(struct sockaddr_in));
306         if (err)
307                 goto error_sock;
308
309         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
310         udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
311
312         udp_encap_enable();
313
314         return 0;
315
316 error_sock:
317         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
318 error:
319         pr_warn("cannot register lisp protocol handler: %d\n", err);
320         return err;
321 }
322
323 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
324 {
325         struct lisp_port *lisp_port = lisp_vport(vport);
326
327         if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(lisp_port->dst_port)))
328                 return -EMSGSIZE;
329         return 0;
330 }
331
332 static void lisp_tnl_destroy(struct vport *vport)
333 {
334         struct lisp_port *lisp_port = lisp_vport(vport);
335
336         list_del_rcu(&lisp_port->list);
337         /* Release socket */
338         sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
339
340         ovs_vport_deferred_free(vport);
341 }
342
343 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
344 {
345         struct net *net = ovs_dp_get_net(parms->dp);
346         struct nlattr *options = parms->options;
347         struct lisp_port *lisp_port;
348         struct vport *vport;
349         struct nlattr *a;
350         int err;
351         u16 dst_port;
352
353         if (!options) {
354                 err = -EINVAL;
355                 goto error;
356         }
357
358         a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
359         if (a && nla_len(a) == sizeof(u16)) {
360                 dst_port = nla_get_u16(a);
361         } else {
362                 /* Require destination port from userspace. */
363                 err = -EINVAL;
364                 goto error;
365         }
366
367         /* Verify if we already have a socket created for this port */
368         if (lisp_find_port(net, htons(dst_port))) {
369                 err = -EEXIST;
370                 goto error;
371         }
372
373         vport = ovs_vport_alloc(sizeof(struct lisp_port),
374                                 &ovs_lisp_vport_ops, parms);
375         if (IS_ERR(vport))
376                 return vport;
377
378         lisp_port = lisp_vport(vport);
379         lisp_port->dst_port = htons(dst_port);
380         strncpy(lisp_port->name, parms->name, IFNAMSIZ);
381
382         err = lisp_socket_init(lisp_port, net);
383         if (err)
384                 goto error_free;
385
386         list_add_tail_rcu(&lisp_port->list, &lisp_ports);
387         return vport;
388
389 error_free:
390         ovs_vport_free(vport);
391 error:
392         return ERR_PTR(err);
393 }
394
395 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,12,0)
396
397 static void lisp_fix_segment(struct sk_buff *skb)
398 {
399         struct udphdr *udph = udp_hdr(skb);
400
401         udph->len = htons(skb->len - skb_transport_offset(skb));
402 }
403
404 static int handle_offloads(struct sk_buff *skb)
405 {
406         if (skb_is_gso(skb))
407                 OVS_GSO_CB(skb)->fix_segment = lisp_fix_segment;
408         else if (skb->ip_summed != CHECKSUM_PARTIAL)
409                 skb->ip_summed = CHECKSUM_NONE;
410         return 0;
411 }
412 #else
413 static int handle_offloads(struct sk_buff *skb)
414 {
415         if (skb->encapsulation && skb_is_gso(skb)) {
416                 kfree_skb(skb);
417                 return -ENOSYS;
418         }
419
420         if (skb_is_gso(skb)) {
421                 int err = skb_unclone(skb, GFP_ATOMIC);
422                 if (unlikely(err))
423                         return err;
424
425                 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
426         } else if (skb->ip_summed != CHECKSUM_PARTIAL)
427                 skb->ip_summed = CHECKSUM_NONE;
428
429         skb->encapsulation = 1;
430         return 0;
431 }
432 #endif
433
434 static int lisp_send(struct vport *vport, struct sk_buff *skb)
435 {
436         struct ovs_key_ipv4_tunnel *tun_key;
437         int network_offset = skb_network_offset(skb);
438         struct rtable *rt;
439         int min_headroom;
440         __be32 saddr;
441         __be16 df;
442         int sent_len;
443         int err;
444
445         if (unlikely(!OVS_CB(skb)->egress_tun_info))
446                 return -EINVAL;
447
448         tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
449
450         if (skb->protocol != htons(ETH_P_IP) &&
451             skb->protocol != htons(ETH_P_IPV6)) {
452                 kfree_skb(skb);
453                 return 0;
454         }
455
456         /* Route lookup */
457         saddr = tun_key->ipv4_src;
458         rt = find_route(ovs_dp_get_net(vport->dp),
459                         &saddr, tun_key->ipv4_dst,
460                         IPPROTO_UDP, tun_key->ipv4_tos,
461                         skb->mark);
462         if (IS_ERR(rt)) {
463                 err = PTR_ERR(rt);
464                 goto error;
465         }
466
467         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
468                         + sizeof(struct iphdr) + LISP_HLEN;
469
470         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
471                 int head_delta = SKB_DATA_ALIGN(min_headroom -
472                                                 skb_headroom(skb) +
473                                                 16);
474
475                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
476                                         0, GFP_ATOMIC);
477                 if (unlikely(err))
478                         goto err_free_rt;
479         }
480
481         /* Reset l2 headers. */
482         skb_pull(skb, network_offset);
483         skb_reset_mac_header(skb);
484         vlan_set_tci(skb, 0);
485
486         skb_reset_inner_headers(skb);
487
488         __skb_push(skb, LISP_HLEN);
489         skb_reset_transport_header(skb);
490
491         lisp_build_header(vport, skb);
492
493         /* Offloading */
494         err = handle_offloads(skb);
495         if (err)
496                 goto err_free_rt;
497
498         skb->ignore_df = 1;
499
500         df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
501         sent_len = iptunnel_xmit(skb->sk, rt, skb,
502                              saddr, tun_key->ipv4_dst,
503                              IPPROTO_UDP, tun_key->ipv4_tos,
504                              tun_key->ipv4_ttl, df, false);
505
506         return sent_len > 0 ? sent_len + network_offset : sent_len;
507
508 err_free_rt:
509         ip_rt_put(rt);
510 error:
511         return err;
512 }
513
514 static const char *lisp_get_name(const struct vport *vport)
515 {
516         struct lisp_port *lisp_port = lisp_vport(vport);
517         return lisp_port->name;
518 }
519
520 static int lisp_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
521                                     struct ovs_tunnel_info *egress_tun_info)
522 {
523         struct net *net = ovs_dp_get_net(vport->dp);
524         struct lisp_port *lisp_port = lisp_vport(vport);
525
526         if (skb->protocol != htons(ETH_P_IP) &&
527             skb->protocol != htons(ETH_P_IPV6)) {
528                 return -EINVAL;
529         }
530
531         /*
532          * Get tp_src and tp_dst, refert to lisp_build_header().
533          */
534         return ovs_tunnel_get_egress_info(egress_tun_info, net,
535                                           OVS_CB(skb)->egress_tun_info,
536                                           IPPROTO_UDP, skb->mark,
537                                           htons(get_src_port(net, skb)),
538                                           lisp_port->dst_port);
539 }
540
541 const struct vport_ops ovs_lisp_vport_ops = {
542         .type                   = OVS_VPORT_TYPE_LISP,
543         .create                 = lisp_tnl_create,
544         .destroy                = lisp_tnl_destroy,
545         .get_name               = lisp_get_name,
546         .get_options            = lisp_get_options,
547         .send                   = lisp_send,
548         .get_egress_tun_info    = lisp_get_egress_tun_info,
549 };