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