]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/vport-lisp.c
datapath: Consistently set skb->inner_protocol for tunnels.
[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/udp_tunnel.h>
35 #include <net/xfrm.h>
36
37 #include "datapath.h"
38 #include "gso.h"
39 #include "vport.h"
40
41 /*
42 * LISP encapsulation header:
43 *
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * |N|L|E|V|I|flags| Nonce/Map-Version |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Instance ID/Locator Status Bits |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 *
50 */
51
52 /**
53 * struct lisphdr - LISP header
54 * @nonce_present: Flag indicating the presence of a 24 bit nonce value.
55 * @locator_status_bits_present: Flag indicating the presence of Locator Status
56 * Bits (LSB).
57 * @solicit_echo_nonce: Flag indicating the use of the echo noncing mechanism.
58 * @map_version_present: Flag indicating the use of mapping versioning.
59 * @instance_id_present: Flag indicating the presence of a 24 bit Instance ID.
60 * @reserved_flags: 3 bits reserved for future flags.
61 * @nonce: 24 bit nonce value.
62 * @map_version: 24 bit mapping version.
63 * @locator_status_bits: Locator Status Bits: 32 bits when instance_id_present
64 * is not set, 8 bits when it is.
65 * @instance_id: 24 bit Instance ID
66 */
67 struct lisphdr {
68 #ifdef __LITTLE_ENDIAN_BITFIELD
69 __u8 reserved_flags:3;
70 __u8 instance_id_present:1;
71 __u8 map_version_present:1;
72 __u8 solicit_echo_nonce:1;
73 __u8 locator_status_bits_present:1;
74 __u8 nonce_present:1;
75 #else
76 __u8 nonce_present:1;
77 __u8 locator_status_bits_present:1;
78 __u8 solicit_echo_nonce:1;
79 __u8 map_version_present:1;
80 __u8 instance_id_present:1;
81 __u8 reserved_flags:3;
82 #endif
83 union {
84 __u8 nonce[3];
85 __u8 map_version[3];
86 } u1;
87 union {
88 __be32 locator_status_bits;
89 struct {
90 __u8 instance_id[3];
91 __u8 locator_status_bits;
92 } word2;
93 } u2;
94 };
95
96 #define LISP_HLEN (sizeof(struct udphdr) + sizeof(struct lisphdr))
97
98 /**
99 * struct lisp_port - Keeps track of open UDP ports
100 * @dst_port: lisp UDP port no.
101 * @list: list element in @lisp_ports.
102 * @lisp_rcv_socket: The socket created for this port number.
103 * @name: vport name.
104 */
105 struct lisp_port {
106 __be16 dst_port;
107 struct list_head list;
108 struct socket *lisp_rcv_socket;
109 char name[IFNAMSIZ];
110 };
111
112 static LIST_HEAD(lisp_ports);
113
114 static inline struct lisp_port *lisp_vport(const struct vport *vport)
115 {
116 return vport_priv(vport);
117 }
118
119 static struct lisp_port *lisp_find_port(struct net *net, __be16 port)
120 {
121 struct lisp_port *lisp_port;
122
123 list_for_each_entry_rcu(lisp_port, &lisp_ports, list) {
124 if (lisp_port->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 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
138 static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
139 {
140
141 #ifdef __BIG_ENDIAN
142 iid[0] = (__force __u8)(tun_id >> 16);
143 iid[1] = (__force __u8)(tun_id >> 8);
144 iid[2] = (__force __u8)tun_id;
145 #else
146 iid[0] = (__force __u8)((__force u64)tun_id >> 40);
147 iid[1] = (__force __u8)((__force u64)tun_id >> 48);
148 iid[2] = (__force __u8)((__force u64)tun_id >> 56);
149 #endif
150 }
151
152 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
153 static __be64 instance_id_to_tunnel_id(__u8 *iid)
154 {
155 #ifdef __BIG_ENDIAN
156 return (iid[0] << 16) | (iid[1] << 8) | iid[2];
157 #else
158 return (__force __be64)(((__force u64)iid[0] << 40) |
159 ((__force u64)iid[1] << 48) |
160 ((__force u64)iid[2] << 56));
161 #endif
162 }
163
164 /* Compute source UDP port for outgoing packet.
165 * Currently we use the flow hash.
166 */
167 static u16 get_src_port(struct net *net, struct sk_buff *skb)
168 {
169 u32 hash = skb_get_hash(skb);
170 unsigned int range;
171 int high;
172 int low;
173
174 if (!hash) {
175 if (skb->protocol == htons(ETH_P_IP)) {
176 struct iphdr *iph;
177 int size = (sizeof(iph->saddr) * 2) / sizeof(u32);
178
179 iph = (struct iphdr *) skb_inner_network_header(skb);
180 hash = jhash2((const u32 *)&iph->saddr, size, 0);
181 } else if (skb->protocol == htons(ETH_P_IPV6)) {
182 struct ipv6hdr *ipv6hdr;
183
184 ipv6hdr = (struct ipv6hdr *) skb_inner_network_header(skb);
185 hash = jhash2((const u32 *)&ipv6hdr->saddr,
186 (sizeof(struct in6_addr) * 2) / sizeof(u32), 0);
187 } else {
188 pr_warn_once("LISP inner protocol is not IP when "
189 "calculating hash.\n");
190 }
191 }
192
193 inet_get_local_port_range(net, &low, &high);
194 range = (high - low) + 1;
195 return (((u64) hash * range) >> 32) + low;
196 }
197
198 static void lisp_build_header(const struct vport *vport,
199 struct sk_buff *skb)
200 {
201 struct net *net = ovs_dp_get_net(vport->dp);
202 struct lisp_port *lisp_port = lisp_vport(vport);
203 struct udphdr *udph = udp_hdr(skb);
204 struct lisphdr *lisph = (struct lisphdr *)(udph + 1);
205 const struct ovs_key_ipv4_tunnel *tun_key;
206
207 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
208 udph->dest = lisp_port->dst_port;
209 udph->source = htons(get_src_port(net, skb));
210 udph->check = 0;
211 udph->len = htons(skb->len - skb_transport_offset(skb));
212
213 lisph->nonce_present = 0; /* We don't support echo nonce algorithm */
214 lisph->locator_status_bits_present = 1; /* Set LSB */
215 lisph->solicit_echo_nonce = 0; /* No echo noncing */
216 lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
217 lisph->instance_id_present = 1; /* Store the tun_id as Instance ID */
218 lisph->reserved_flags = 0; /* Reserved flags, set to 0 */
219
220 lisph->u1.nonce[0] = 0;
221 lisph->u1.nonce[1] = 0;
222 lisph->u1.nonce[2] = 0;
223
224 tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
225 lisph->u2.word2.locator_status_bits = 1;
226 }
227
228 /* Called with rcu_read_lock and BH disabled. */
229 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
230 {
231 struct lisp_port *lisp_port;
232 struct lisphdr *lisph;
233 struct iphdr *iph, *inner_iph;
234 struct ovs_tunnel_info tun_info;
235 __be64 key;
236 struct ethhdr *ethh;
237 __be16 protocol;
238
239 lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
240 if (unlikely(!lisp_port))
241 goto error;
242
243 if (iptunnel_pull_header(skb, LISP_HLEN, 0))
244 goto error;
245
246 lisph = lisp_hdr(skb);
247
248 if (lisph->instance_id_present != 1)
249 key = 0;
250 else
251 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
252
253 /* Save outer tunnel values */
254 iph = ip_hdr(skb);
255 ovs_flow_tun_info_init(&tun_info, iph,
256 udp_hdr(skb)->source, udp_hdr(skb)->dest,
257 key, TUNNEL_KEY, NULL, 0);
258
259 /* Drop non-IP inner packets */
260 inner_iph = (struct iphdr *)(lisph + 1);
261 switch (inner_iph->version) {
262 case 4:
263 protocol = htons(ETH_P_IP);
264 break;
265 case 6:
266 protocol = htons(ETH_P_IPV6);
267 break;
268 default:
269 goto error;
270 }
271 skb->protocol = protocol;
272
273 /* Add Ethernet header */
274 ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
275 memset(ethh, 0, ETH_HLEN);
276 ethh->h_dest[0] = 0x02;
277 ethh->h_source[0] = 0x02;
278 ethh->h_proto = protocol;
279
280 ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
281
282 ovs_vport_receive(vport_from_priv(lisp_port), skb, &tun_info);
283 goto out;
284
285 error:
286 kfree_skb(skb);
287 out:
288 return 0;
289 }
290
291 /* Arbitrary value. Irrelevant as long as it's not 0 since we set the handler. */
292 #define UDP_ENCAP_LISP 1
293 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
294 {
295 struct udp_port_cfg udp_conf;
296 int err;
297
298 memset(&udp_conf, 0, sizeof(udp_conf));
299
300 udp_conf.family = AF_INET;
301 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
302 udp_conf.local_udp_port = lisp_port->dst_port;
303
304 err = udp_sock_create(net, &udp_conf, &lisp_port->lisp_rcv_socket);
305 if (err < 0) {
306 pr_warn("cannot register lisp protocol handler: %d\n", err);
307 return err;
308 }
309
310 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
311 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
312
313 udp_encap_enable();
314
315 return 0;
316 }
317
318 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
319 {
320 struct lisp_port *lisp_port = lisp_vport(vport);
321
322 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(lisp_port->dst_port)))
323 return -EMSGSIZE;
324 return 0;
325 }
326
327 static void lisp_tnl_destroy(struct vport *vport)
328 {
329 struct lisp_port *lisp_port = lisp_vport(vport);
330
331 list_del_rcu(&lisp_port->list);
332 /* Release socket */
333 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
334
335 ovs_vport_deferred_free(vport);
336 }
337
338 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
339 {
340 struct net *net = ovs_dp_get_net(parms->dp);
341 struct nlattr *options = parms->options;
342 struct lisp_port *lisp_port;
343 struct vport *vport;
344 struct nlattr *a;
345 int err;
346 u16 dst_port;
347
348 if (!options) {
349 err = -EINVAL;
350 goto error;
351 }
352
353 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
354 if (a && nla_len(a) == sizeof(u16)) {
355 dst_port = nla_get_u16(a);
356 } else {
357 /* Require destination port from userspace. */
358 err = -EINVAL;
359 goto error;
360 }
361
362 /* Verify if we already have a socket created for this port */
363 if (lisp_find_port(net, htons(dst_port))) {
364 err = -EEXIST;
365 goto error;
366 }
367
368 vport = ovs_vport_alloc(sizeof(struct lisp_port),
369 &ovs_lisp_vport_ops, parms);
370 if (IS_ERR(vport))
371 return vport;
372
373 lisp_port = lisp_vport(vport);
374 lisp_port->dst_port = htons(dst_port);
375 strncpy(lisp_port->name, parms->name, IFNAMSIZ);
376
377 err = lisp_socket_init(lisp_port, net);
378 if (err)
379 goto error_free;
380
381 list_add_tail_rcu(&lisp_port->list, &lisp_ports);
382 return vport;
383
384 error_free:
385 ovs_vport_free(vport);
386 error:
387 return ERR_PTR(err);
388 }
389
390 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,12,0)
391
392 static void lisp_fix_segment(struct sk_buff *skb)
393 {
394 struct udphdr *udph = udp_hdr(skb);
395
396 udph->len = htons(skb->len - skb_transport_offset(skb));
397 }
398
399 static struct sk_buff *handle_offloads(struct sk_buff *skb)
400 {
401 return ovs_iptunnel_handle_offloads(skb, false, lisp_fix_segment);
402 }
403 #else
404 static struct sk_buff *handle_offloads(struct sk_buff *skb)
405 {
406 int err = 0;
407
408 if (skb_is_gso(skb)) {
409
410 if (skb_is_encapsulated(skb)) {
411 err = -ENOSYS;
412 goto error;
413 }
414
415 err = skb_unclone(skb, GFP_ATOMIC);
416 if (unlikely(err))
417 goto error;
418
419 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
420 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
421 skb->ip_summed = CHECKSUM_NONE;
422
423 skb->encapsulation = 1;
424 return skb;
425 error:
426 kfree_skb(skb);
427 return ERR_PTR(err);
428 }
429 #endif
430
431 static int lisp_send(struct vport *vport, struct sk_buff *skb)
432 {
433 struct ovs_key_ipv4_tunnel *tun_key;
434 int network_offset = skb_network_offset(skb);
435 struct rtable *rt;
436 int min_headroom;
437 __be32 saddr;
438 __be16 df;
439 int sent_len;
440 int err;
441
442 if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
443 err = -EINVAL;
444 goto error;
445 }
446
447 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
448
449 if (skb->protocol != htons(ETH_P_IP) &&
450 skb->protocol != htons(ETH_P_IPV6)) {
451 err = 0;
452 goto error;
453 }
454
455 /* Route lookup */
456 saddr = tun_key->ipv4_src;
457 rt = find_route(ovs_dp_get_net(vport->dp),
458 &saddr, tun_key->ipv4_dst,
459 IPPROTO_UDP, tun_key->ipv4_tos,
460 skb->mark);
461 if (IS_ERR(rt)) {
462 err = PTR_ERR(rt);
463 goto error;
464 }
465
466 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
467 + sizeof(struct iphdr) + LISP_HLEN;
468
469 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
470 int head_delta = SKB_DATA_ALIGN(min_headroom -
471 skb_headroom(skb) +
472 16);
473
474 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
475 0, GFP_ATOMIC);
476 if (unlikely(err))
477 goto err_free_rt;
478 }
479
480 /* Reset l2 headers. */
481 skb_pull(skb, network_offset);
482 skb_reset_mac_header(skb);
483 vlan_set_tci(skb, 0);
484
485 skb_reset_inner_headers(skb);
486
487 __skb_push(skb, LISP_HLEN);
488 skb_reset_transport_header(skb);
489
490 lisp_build_header(vport, skb);
491
492 /* Offloading */
493 skb = handle_offloads(skb);
494 if (IS_ERR(skb)) {
495 err = PTR_ERR(skb);
496 skb = NULL;
497 goto err_free_rt;
498 }
499
500 skb->ignore_df = 1;
501
502 ovs_skb_set_inner_protocol(skb, skb->protocol);
503
504 df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
505 sent_len = iptunnel_xmit(skb->sk, rt, skb,
506 saddr, tun_key->ipv4_dst,
507 IPPROTO_UDP, tun_key->ipv4_tos,
508 tun_key->ipv4_ttl, df, false);
509
510 return sent_len > 0 ? sent_len + network_offset : sent_len;
511
512 err_free_rt:
513 ip_rt_put(rt);
514 error:
515 kfree_skb(skb);
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 };