blob: cb332adb84cdcadc006de6d7a8668111babd2f53 [file] [log] [blame]
Andy Kingd021c342013-02-06 14:23:56 +00001/*
2 * VMware vSockets Driver
3 *
4 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation version 2 and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#include <linux/types.h>
Andy Kingd021c342013-02-06 14:23:56 +000017#include <linux/bitops.h>
18#include <linux/cred.h>
19#include <linux/init.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/kmod.h>
23#include <linux/list.h>
Andy Kingd021c342013-02-06 14:23:56 +000024#include <linux/module.h>
25#include <linux/mutex.h>
26#include <linux/net.h>
27#include <linux/poll.h>
28#include <linux/skbuff.h>
29#include <linux/smp.h>
30#include <linux/socket.h>
31#include <linux/stddef.h>
32#include <linux/unistd.h>
33#include <linux/wait.h>
34#include <linux/workqueue.h>
35#include <net/sock.h>
Asias He82a54d02013-07-25 17:39:34 +080036#include <net/af_vsock.h>
Andy Kingd021c342013-02-06 14:23:56 +000037
Andy Kingd021c342013-02-06 14:23:56 +000038#include "vmci_transport_notify.h"
39
40static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg);
41static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg);
Andy Kingd021c342013-02-06 14:23:56 +000042static void vmci_transport_peer_detach_cb(u32 sub_id,
43 const struct vmci_event_data *ed,
44 void *client_data);
45static void vmci_transport_recv_pkt_work(struct work_struct *work);
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -070046static void vmci_transport_cleanup(struct work_struct *work);
Andy Kingd021c342013-02-06 14:23:56 +000047static int vmci_transport_recv_listen(struct sock *sk,
48 struct vmci_transport_packet *pkt);
49static int vmci_transport_recv_connecting_server(
50 struct sock *sk,
51 struct sock *pending,
52 struct vmci_transport_packet *pkt);
53static int vmci_transport_recv_connecting_client(
54 struct sock *sk,
55 struct vmci_transport_packet *pkt);
56static int vmci_transport_recv_connecting_client_negotiate(
57 struct sock *sk,
58 struct vmci_transport_packet *pkt);
59static int vmci_transport_recv_connecting_client_invalid(
60 struct sock *sk,
61 struct vmci_transport_packet *pkt);
62static int vmci_transport_recv_connected(struct sock *sk,
63 struct vmci_transport_packet *pkt);
64static bool vmci_transport_old_proto_override(bool *old_pkt_proto);
65static u16 vmci_transport_new_proto_supported_versions(void);
66static bool vmci_transport_proto_to_notify_struct(struct sock *sk, u16 *proto,
67 bool old_pkt_proto);
68
69struct vmci_transport_recv_pkt_info {
70 struct work_struct work;
71 struct sock *sk;
72 struct vmci_transport_packet pkt;
73};
74
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -070075static LIST_HEAD(vmci_transport_cleanup_list);
76static DEFINE_SPINLOCK(vmci_transport_cleanup_lock);
77static DECLARE_WORK(vmci_transport_cleanup_work, vmci_transport_cleanup);
78
Andy Kingd021c342013-02-06 14:23:56 +000079static struct vmci_handle vmci_transport_stream_handle = { VMCI_INVALID_ID,
80 VMCI_INVALID_ID };
81static u32 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
82
83static int PROTOCOL_OVERRIDE = -1;
84
85#define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN 128
86#define VMCI_TRANSPORT_DEFAULT_QP_SIZE 262144
87#define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX 262144
88
89/* The default peer timeout indicates how long we will wait for a peer response
90 * to a control message.
91 */
92#define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
93
Andy Kingd021c342013-02-06 14:23:56 +000094/* Helper function to convert from a VMCI error code to a VSock error code. */
95
96static s32 vmci_transport_error_to_vsock_error(s32 vmci_error)
97{
Andy Kingd021c342013-02-06 14:23:56 +000098 switch (vmci_error) {
99 case VMCI_ERROR_NO_MEM:
Colin Ian Kinged8bfd52017-03-29 16:33:55 +0100100 return -ENOMEM;
Andy Kingd021c342013-02-06 14:23:56 +0000101 case VMCI_ERROR_DUPLICATE_ENTRY:
102 case VMCI_ERROR_ALREADY_EXISTS:
Colin Ian Kinged8bfd52017-03-29 16:33:55 +0100103 return -EADDRINUSE;
Andy Kingd021c342013-02-06 14:23:56 +0000104 case VMCI_ERROR_NO_ACCESS:
Colin Ian Kinged8bfd52017-03-29 16:33:55 +0100105 return -EPERM;
Andy Kingd021c342013-02-06 14:23:56 +0000106 case VMCI_ERROR_NO_RESOURCES:
Colin Ian Kinged8bfd52017-03-29 16:33:55 +0100107 return -ENOBUFS;
Andy Kingd021c342013-02-06 14:23:56 +0000108 case VMCI_ERROR_INVALID_RESOURCE:
Colin Ian Kinged8bfd52017-03-29 16:33:55 +0100109 return -EHOSTUNREACH;
Andy Kingd021c342013-02-06 14:23:56 +0000110 case VMCI_ERROR_INVALID_ARGS:
111 default:
Colin Ian Kinged8bfd52017-03-29 16:33:55 +0100112 break;
Andy Kingd021c342013-02-06 14:23:56 +0000113 }
Colin Ian Kinged8bfd52017-03-29 16:33:55 +0100114 return -EINVAL;
Andy Kingd021c342013-02-06 14:23:56 +0000115}
116
Reilly Grant2a89f922013-03-14 11:55:41 +0000117static u32 vmci_transport_peer_rid(u32 peer_cid)
118{
119 if (VMADDR_CID_HYPERVISOR == peer_cid)
120 return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID;
121
122 return VMCI_TRANSPORT_PACKET_RID;
123}
124
Andy Kingd021c342013-02-06 14:23:56 +0000125static inline void
126vmci_transport_packet_init(struct vmci_transport_packet *pkt,
127 struct sockaddr_vm *src,
128 struct sockaddr_vm *dst,
129 u8 type,
130 u64 size,
131 u64 mode,
132 struct vmci_transport_waiting_info *wait,
133 u16 proto,
134 struct vmci_handle handle)
135{
136 /* We register the stream control handler as an any cid handle so we
137 * must always send from a source address of VMADDR_CID_ANY
138 */
139 pkt->dg.src = vmci_make_handle(VMADDR_CID_ANY,
140 VMCI_TRANSPORT_PACKET_RID);
141 pkt->dg.dst = vmci_make_handle(dst->svm_cid,
Reilly Grant2a89f922013-03-14 11:55:41 +0000142 vmci_transport_peer_rid(dst->svm_cid));
Andy Kingd021c342013-02-06 14:23:56 +0000143 pkt->dg.payload_size = sizeof(*pkt) - sizeof(pkt->dg);
144 pkt->version = VMCI_TRANSPORT_PACKET_VERSION;
145 pkt->type = type;
146 pkt->src_port = src->svm_port;
147 pkt->dst_port = dst->svm_port;
148 memset(&pkt->proto, 0, sizeof(pkt->proto));
149 memset(&pkt->_reserved2, 0, sizeof(pkt->_reserved2));
150
151 switch (pkt->type) {
152 case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
153 pkt->u.size = 0;
154 break;
155
156 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST:
157 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
158 pkt->u.size = size;
159 break;
160
161 case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
162 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
163 pkt->u.handle = handle;
164 break;
165
166 case VMCI_TRANSPORT_PACKET_TYPE_WROTE:
167 case VMCI_TRANSPORT_PACKET_TYPE_READ:
168 case VMCI_TRANSPORT_PACKET_TYPE_RST:
169 pkt->u.size = 0;
170 break;
171
172 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
173 pkt->u.mode = mode;
174 break;
175
176 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ:
177 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE:
178 memcpy(&pkt->u.wait, wait, sizeof(pkt->u.wait));
179 break;
180
181 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2:
182 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
183 pkt->u.size = size;
184 pkt->proto = proto;
185 break;
186 }
187}
188
189static inline void
190vmci_transport_packet_get_addresses(struct vmci_transport_packet *pkt,
191 struct sockaddr_vm *local,
192 struct sockaddr_vm *remote)
193{
194 vsock_addr_init(local, pkt->dg.dst.context, pkt->dst_port);
195 vsock_addr_init(remote, pkt->dg.src.context, pkt->src_port);
196}
197
198static int
199__vmci_transport_send_control_pkt(struct vmci_transport_packet *pkt,
200 struct sockaddr_vm *src,
201 struct sockaddr_vm *dst,
202 enum vmci_transport_packet_type type,
203 u64 size,
204 u64 mode,
205 struct vmci_transport_waiting_info *wait,
206 u16 proto,
207 struct vmci_handle handle,
208 bool convert_error)
209{
210 int err;
211
212 vmci_transport_packet_init(pkt, src, dst, type, size, mode, wait,
213 proto, handle);
214 err = vmci_datagram_send(&pkt->dg);
215 if (convert_error && (err < 0))
216 return vmci_transport_error_to_vsock_error(err);
217
218 return err;
219}
220
221static int
222vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet *pkt,
223 enum vmci_transport_packet_type type,
224 u64 size,
225 u64 mode,
226 struct vmci_transport_waiting_info *wait,
227 struct vmci_handle handle)
228{
229 struct vmci_transport_packet reply;
230 struct sockaddr_vm src, dst;
231
232 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) {
233 return 0;
234 } else {
235 vmci_transport_packet_get_addresses(pkt, &src, &dst);
236 return __vmci_transport_send_control_pkt(&reply, &src, &dst,
237 type,
238 size, mode, wait,
239 VSOCK_PROTO_INVALID,
240 handle, true);
241 }
242}
243
244static int
245vmci_transport_send_control_pkt_bh(struct sockaddr_vm *src,
246 struct sockaddr_vm *dst,
247 enum vmci_transport_packet_type type,
248 u64 size,
249 u64 mode,
250 struct vmci_transport_waiting_info *wait,
251 struct vmci_handle handle)
252{
253 /* Note that it is safe to use a single packet across all CPUs since
254 * two tasklets of the same type are guaranteed to not ever run
255 * simultaneously. If that ever changes, or VMCI stops using tasklets,
256 * we can use per-cpu packets.
257 */
258 static struct vmci_transport_packet pkt;
259
260 return __vmci_transport_send_control_pkt(&pkt, src, dst, type,
261 size, mode, wait,
262 VSOCK_PROTO_INVALID, handle,
263 false);
264}
265
266static int
267vmci_transport_send_control_pkt(struct sock *sk,
268 enum vmci_transport_packet_type type,
269 u64 size,
270 u64 mode,
271 struct vmci_transport_waiting_info *wait,
272 u16 proto,
273 struct vmci_handle handle)
274{
275 struct vmci_transport_packet *pkt;
276 struct vsock_sock *vsk;
277 int err;
278
279 vsk = vsock_sk(sk);
280
281 if (!vsock_addr_bound(&vsk->local_addr))
282 return -EINVAL;
283
284 if (!vsock_addr_bound(&vsk->remote_addr))
285 return -EINVAL;
286
287 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
288 if (!pkt)
289 return -ENOMEM;
290
291 err = __vmci_transport_send_control_pkt(pkt, &vsk->local_addr,
292 &vsk->remote_addr, type, size,
293 mode, wait, proto, handle,
294 true);
295 kfree(pkt);
296
297 return err;
298}
299
300static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst,
301 struct sockaddr_vm *src,
302 struct vmci_transport_packet *pkt)
303{
304 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
305 return 0;
306 return vmci_transport_send_control_pkt_bh(
307 dst, src,
308 VMCI_TRANSPORT_PACKET_TYPE_RST, 0,
309 0, NULL, VMCI_INVALID_HANDLE);
310}
311
312static int vmci_transport_send_reset(struct sock *sk,
313 struct vmci_transport_packet *pkt)
314{
315 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
316 return 0;
317 return vmci_transport_send_control_pkt(sk,
318 VMCI_TRANSPORT_PACKET_TYPE_RST,
319 0, 0, NULL, VSOCK_PROTO_INVALID,
320 VMCI_INVALID_HANDLE);
321}
322
323static int vmci_transport_send_negotiate(struct sock *sk, size_t size)
324{
325 return vmci_transport_send_control_pkt(
326 sk,
327 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
328 size, 0, NULL,
329 VSOCK_PROTO_INVALID,
330 VMCI_INVALID_HANDLE);
331}
332
333static int vmci_transport_send_negotiate2(struct sock *sk, size_t size,
334 u16 version)
335{
336 return vmci_transport_send_control_pkt(
337 sk,
338 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
339 size, 0, NULL, version,
340 VMCI_INVALID_HANDLE);
341}
342
343static int vmci_transport_send_qp_offer(struct sock *sk,
344 struct vmci_handle handle)
345{
346 return vmci_transport_send_control_pkt(
347 sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0,
348 0, NULL,
349 VSOCK_PROTO_INVALID, handle);
350}
351
352static int vmci_transport_send_attach(struct sock *sk,
353 struct vmci_handle handle)
354{
355 return vmci_transport_send_control_pkt(
356 sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
357 0, 0, NULL, VSOCK_PROTO_INVALID,
358 handle);
359}
360
361static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt)
362{
363 return vmci_transport_reply_control_pkt_fast(
364 pkt,
365 VMCI_TRANSPORT_PACKET_TYPE_RST,
366 0, 0, NULL,
367 VMCI_INVALID_HANDLE);
368}
369
370static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst,
371 struct sockaddr_vm *src)
372{
373 return vmci_transport_send_control_pkt_bh(
374 dst, src,
375 VMCI_TRANSPORT_PACKET_TYPE_INVALID,
376 0, 0, NULL, VMCI_INVALID_HANDLE);
377}
378
379int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
380 struct sockaddr_vm *src)
381{
382 return vmci_transport_send_control_pkt_bh(
383 dst, src,
384 VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
385 0, NULL, VMCI_INVALID_HANDLE);
386}
387
388int vmci_transport_send_read_bh(struct sockaddr_vm *dst,
389 struct sockaddr_vm *src)
390{
391 return vmci_transport_send_control_pkt_bh(
392 dst, src,
393 VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
394 0, NULL, VMCI_INVALID_HANDLE);
395}
396
397int vmci_transport_send_wrote(struct sock *sk)
398{
399 return vmci_transport_send_control_pkt(
400 sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
401 0, NULL, VSOCK_PROTO_INVALID,
402 VMCI_INVALID_HANDLE);
403}
404
405int vmci_transport_send_read(struct sock *sk)
406{
407 return vmci_transport_send_control_pkt(
408 sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
409 0, NULL, VSOCK_PROTO_INVALID,
410 VMCI_INVALID_HANDLE);
411}
412
413int vmci_transport_send_waiting_write(struct sock *sk,
414 struct vmci_transport_waiting_info *wait)
415{
416 return vmci_transport_send_control_pkt(
417 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
418 0, 0, wait, VSOCK_PROTO_INVALID,
419 VMCI_INVALID_HANDLE);
420}
421
422int vmci_transport_send_waiting_read(struct sock *sk,
423 struct vmci_transport_waiting_info *wait)
424{
425 return vmci_transport_send_control_pkt(
426 sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
427 0, 0, wait, VSOCK_PROTO_INVALID,
428 VMCI_INVALID_HANDLE);
429}
430
431static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode)
432{
433 return vmci_transport_send_control_pkt(
434 &vsk->sk,
435 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
436 0, mode, NULL,
437 VSOCK_PROTO_INVALID,
438 VMCI_INVALID_HANDLE);
439}
440
441static int vmci_transport_send_conn_request(struct sock *sk, size_t size)
442{
443 return vmci_transport_send_control_pkt(sk,
444 VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
445 size, 0, NULL,
446 VSOCK_PROTO_INVALID,
447 VMCI_INVALID_HANDLE);
448}
449
450static int vmci_transport_send_conn_request2(struct sock *sk, size_t size,
451 u16 version)
452{
453 return vmci_transport_send_control_pkt(
454 sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
455 size, 0, NULL, version,
456 VMCI_INVALID_HANDLE);
457}
458
459static struct sock *vmci_transport_get_pending(
460 struct sock *listener,
461 struct vmci_transport_packet *pkt)
462{
463 struct vsock_sock *vlistener;
464 struct vsock_sock *vpending;
465 struct sock *pending;
Reilly Grant990454b2013-04-01 11:41:52 -0700466 struct sockaddr_vm src;
467
468 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
Andy Kingd021c342013-02-06 14:23:56 +0000469
470 vlistener = vsock_sk(listener);
471
472 list_for_each_entry(vpending, &vlistener->pending_links,
473 pending_links) {
Andy Kingd021c342013-02-06 14:23:56 +0000474 if (vsock_addr_equals_addr(&src, &vpending->remote_addr) &&
Reilly Grant990454b2013-04-01 11:41:52 -0700475 pkt->dst_port == vpending->local_addr.svm_port) {
Andy Kingd021c342013-02-06 14:23:56 +0000476 pending = sk_vsock(vpending);
477 sock_hold(pending);
478 goto found;
479 }
480 }
481
482 pending = NULL;
483found:
484 return pending;
485
486}
487
488static void vmci_transport_release_pending(struct sock *pending)
489{
490 sock_put(pending);
491}
492
493/* We allow two kinds of sockets to communicate with a restricted VM: 1)
494 * trusted sockets 2) sockets from applications running as the same user as the
495 * VM (this is only true for the host side and only when using hosted products)
496 */
497
498static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid)
499{
500 return vsock->trusted ||
501 vmci_is_context_owner(peer_cid, vsock->owner->uid);
502}
503
504/* We allow sending datagrams to and receiving datagrams from a restricted VM
505 * only if it is trusted as described in vmci_transport_is_trusted.
506 */
507
508static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid)
509{
Reilly Grant2a89f922013-03-14 11:55:41 +0000510 if (VMADDR_CID_HYPERVISOR == peer_cid)
511 return true;
512
Andy Kingd021c342013-02-06 14:23:56 +0000513 if (vsock->cached_peer != peer_cid) {
514 vsock->cached_peer = peer_cid;
515 if (!vmci_transport_is_trusted(vsock, peer_cid) &&
516 (vmci_context_get_priv_flags(peer_cid) &
517 VMCI_PRIVILEGE_FLAG_RESTRICTED)) {
518 vsock->cached_peer_allow_dgram = false;
519 } else {
520 vsock->cached_peer_allow_dgram = true;
521 }
522 }
523
524 return vsock->cached_peer_allow_dgram;
525}
526
527static int
528vmci_transport_queue_pair_alloc(struct vmci_qp **qpair,
529 struct vmci_handle *handle,
530 u64 produce_size,
531 u64 consume_size,
532 u32 peer, u32 flags, bool trusted)
533{
534 int err = 0;
535
536 if (trusted) {
537 /* Try to allocate our queue pair as trusted. This will only
538 * work if vsock is running in the host.
539 */
540
541 err = vmci_qpair_alloc(qpair, handle, produce_size,
542 consume_size,
543 peer, flags,
544 VMCI_PRIVILEGE_FLAG_TRUSTED);
545 if (err != VMCI_ERROR_NO_ACCESS)
546 goto out;
547
548 }
549
550 err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size,
551 peer, flags, VMCI_NO_PRIVILEGE_FLAGS);
552out:
553 if (err < 0) {
554 pr_err("Could not attach to queue pair with %d\n",
555 err);
556 err = vmci_transport_error_to_vsock_error(err);
557 }
558
559 return err;
560}
561
562static int
563vmci_transport_datagram_create_hnd(u32 resource_id,
564 u32 flags,
565 vmci_datagram_recv_cb recv_cb,
566 void *client_data,
567 struct vmci_handle *out_handle)
568{
569 int err = 0;
570
571 /* Try to allocate our datagram handler as trusted. This will only work
572 * if vsock is running in the host.
573 */
574
575 err = vmci_datagram_create_handle_priv(resource_id, flags,
576 VMCI_PRIVILEGE_FLAG_TRUSTED,
577 recv_cb,
578 client_data, out_handle);
579
580 if (err == VMCI_ERROR_NO_ACCESS)
581 err = vmci_datagram_create_handle(resource_id, flags,
582 recv_cb, client_data,
583 out_handle);
584
585 return err;
586}
587
588/* This is invoked as part of a tasklet that's scheduled when the VMCI
589 * interrupt fires. This is run in bottom-half context and if it ever needs to
590 * sleep it should defer that work to a work queue.
591 */
592
593static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg)
594{
595 struct sock *sk;
596 size_t size;
597 struct sk_buff *skb;
598 struct vsock_sock *vsk;
599
600 sk = (struct sock *)data;
601
602 /* This handler is privileged when this module is running on the host.
603 * We will get datagrams from all endpoints (even VMs that are in a
604 * restricted context). If we get one from a restricted context then
605 * the destination socket must be trusted.
606 *
607 * NOTE: We access the socket struct without holding the lock here.
608 * This is ok because the field we are interested is never modified
609 * outside of the create and destruct socket functions.
610 */
611 vsk = vsock_sk(sk);
612 if (!vmci_transport_allow_dgram(vsk, dg->src.context))
613 return VMCI_ERROR_NO_ACCESS;
614
615 size = VMCI_DG_SIZE(dg);
616
617 /* Attach the packet to the socket's receive queue as an sk_buff. */
618 skb = alloc_skb(size, GFP_ATOMIC);
Asias Hedce1a282013-06-20 17:20:31 +0800619 if (!skb)
620 return VMCI_ERROR_NO_MEM;
621
622 /* sk_receive_skb() will do a sock_put(), so hold here. */
623 sock_hold(sk);
624 skb_put(skb, size);
625 memcpy(skb->data, dg, size);
626 sk_receive_skb(sk, skb, 0);
Andy Kingd021c342013-02-06 14:23:56 +0000627
628 return VMCI_SUCCESS;
629}
630
631static bool vmci_transport_stream_allow(u32 cid, u32 port)
632{
633 static const u32 non_socket_contexts[] = {
Andy Kingd021c342013-02-06 14:23:56 +0000634 VMADDR_CID_RESERVED,
635 };
636 int i;
637
638 BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts));
639
640 for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) {
641 if (cid == non_socket_contexts[i])
642 return false;
643 }
644
645 return true;
646}
647
648/* This is invoked as part of a tasklet that's scheduled when the VMCI
649 * interrupt fires. This is run in bottom-half context but it defers most of
650 * its work to the packet handling work queue.
651 */
652
653static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)
654{
655 struct sock *sk;
656 struct sockaddr_vm dst;
657 struct sockaddr_vm src;
658 struct vmci_transport_packet *pkt;
659 struct vsock_sock *vsk;
660 bool bh_process_pkt;
661 int err;
662
663 sk = NULL;
664 err = VMCI_SUCCESS;
665 bh_process_pkt = false;
666
667 /* Ignore incoming packets from contexts without sockets, or resources
668 * that aren't vsock implementations.
669 */
670
671 if (!vmci_transport_stream_allow(dg->src.context, -1)
Reilly Grant2a89f922013-03-14 11:55:41 +0000672 || vmci_transport_peer_rid(dg->src.context) != dg->src.resource)
Andy Kingd021c342013-02-06 14:23:56 +0000673 return VMCI_ERROR_NO_ACCESS;
674
675 if (VMCI_DG_SIZE(dg) < sizeof(*pkt))
676 /* Drop datagrams that do not contain full VSock packets. */
677 return VMCI_ERROR_INVALID_ARGS;
678
679 pkt = (struct vmci_transport_packet *)dg;
680
681 /* Find the socket that should handle this packet. First we look for a
682 * connected socket and if there is none we look for a socket bound to
683 * the destintation address.
684 */
685 vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
686 vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port);
687
688 sk = vsock_find_connected_socket(&src, &dst);
689 if (!sk) {
690 sk = vsock_find_bound_socket(&dst);
691 if (!sk) {
692 /* We could not find a socket for this specified
693 * address. If this packet is a RST, we just drop it.
694 * If it is another packet, we send a RST. Note that
695 * we do not send a RST reply to RSTs so that we do not
696 * continually send RSTs between two endpoints.
697 *
698 * Note that since this is a reply, dst is src and src
699 * is dst.
700 */
701 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
702 pr_err("unable to send reset\n");
703
704 err = VMCI_ERROR_NOT_FOUND;
705 goto out;
706 }
707 }
708
709 /* If the received packet type is beyond all types known to this
710 * implementation, reply with an invalid message. Hopefully this will
711 * help when implementing backwards compatibility in the future.
712 */
713 if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) {
714 vmci_transport_send_invalid_bh(&dst, &src);
715 err = VMCI_ERROR_INVALID_ARGS;
716 goto out;
717 }
718
719 /* This handler is privileged when this module is running on the host.
720 * We will get datagram connect requests from all endpoints (even VMs
721 * that are in a restricted context). If we get one from a restricted
722 * context then the destination socket must be trusted.
723 *
724 * NOTE: We access the socket struct without holding the lock here.
725 * This is ok because the field we are interested is never modified
726 * outside of the create and destruct socket functions.
727 */
728 vsk = vsock_sk(sk);
729 if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) {
730 err = VMCI_ERROR_NO_ACCESS;
731 goto out;
732 }
733
734 /* We do most everything in a work queue, but let's fast path the
735 * notification of reads and writes to help data transfer performance.
736 * We can only do this if there is no process context code executing
737 * for this socket since that may change the state.
738 */
739 bh_lock_sock(sk);
740
Reilly Grant990454b2013-04-01 11:41:52 -0700741 if (!sock_owned_by_user(sk)) {
742 /* The local context ID may be out of date, update it. */
743 vsk->local_addr.svm_cid = dst.svm_cid;
744
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400745 if (sk->sk_state == TCP_ESTABLISHED)
Reilly Grant990454b2013-04-01 11:41:52 -0700746 vmci_trans(vsk)->notify_ops->handle_notify_pkt(
747 sk, pkt, true, &dst, &src,
748 &bh_process_pkt);
749 }
Andy Kingd021c342013-02-06 14:23:56 +0000750
751 bh_unlock_sock(sk);
752
753 if (!bh_process_pkt) {
754 struct vmci_transport_recv_pkt_info *recv_pkt_info;
755
756 recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC);
757 if (!recv_pkt_info) {
758 if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
759 pr_err("unable to send reset\n");
760
761 err = VMCI_ERROR_NO_MEM;
762 goto out;
763 }
764
765 recv_pkt_info->sk = sk;
766 memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt));
767 INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work);
768
769 schedule_work(&recv_pkt_info->work);
770 /* Clear sk so that the reference count incremented by one of
771 * the Find functions above is not decremented below. We need
772 * that reference count for the packet handler we've scheduled
773 * to run.
774 */
775 sk = NULL;
776 }
777
778out:
779 if (sk)
780 sock_put(sk);
781
782 return err;
783}
784
Andy Kingd021c342013-02-06 14:23:56 +0000785static void vmci_transport_handle_detach(struct sock *sk)
786{
787 struct vsock_sock *vsk;
788
789 vsk = vsock_sk(sk);
790 if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) {
791 sock_set_flag(sk, SOCK_DONE);
792
793 /* On a detach the peer will not be sending or receiving
794 * anymore.
795 */
796 vsk->peer_shutdown = SHUTDOWN_MASK;
797
798 /* We should not be sending anymore since the peer won't be
799 * there to receive, but we can still receive if there is data
Jorgen Hansenafbea2c2017-11-24 06:25:28 -0800800 * left in our consume queue. If the local endpoint is a host,
801 * we can't call vsock_stream_has_data, since that may block,
802 * but a host endpoint can't read data once the VM has
803 * detached, so there is no available data in that case.
Andy Kingd021c342013-02-06 14:23:56 +0000804 */
Jorgen Hansenafbea2c2017-11-24 06:25:28 -0800805 if (vsk->local_addr.svm_cid == VMADDR_CID_HOST ||
806 vsock_stream_has_data(vsk) <= 0) {
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400807 if (sk->sk_state == TCP_SYN_SENT) {
Andy Kingd021c342013-02-06 14:23:56 +0000808 /* The peer may detach from a queue pair while
809 * we are still in the connecting state, i.e.,
810 * if the peer VM is killed after attaching to
811 * a queue pair, but before we complete the
812 * handshake. In that case, we treat the detach
813 * event like a reset.
814 */
815
Jorgen Hansen4a5def72017-11-27 05:29:32 -0800816 sk->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +0000817 sk->sk_err = ECONNRESET;
818 sk->sk_error_report(sk);
819 return;
820 }
Jorgen Hansen4a5def72017-11-27 05:29:32 -0800821 sk->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +0000822 }
823 sk->sk_state_change(sk);
824 }
825}
826
827static void vmci_transport_peer_detach_cb(u32 sub_id,
828 const struct vmci_event_data *e_data,
829 void *client_data)
830{
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -0700831 struct vmci_transport *trans = client_data;
Andy Kingd021c342013-02-06 14:23:56 +0000832 const struct vmci_event_payload_qp *e_payload;
Andy Kingd021c342013-02-06 14:23:56 +0000833
834 e_payload = vmci_event_data_const_payload(e_data);
Andy Kingd021c342013-02-06 14:23:56 +0000835
836 /* XXX This is lame, we should provide a way to lookup sockets by
837 * qp_handle.
838 */
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -0700839 if (vmci_handle_is_invalid(e_payload->handle) ||
Jorgen Hansen8ab18d72016-04-05 01:59:32 -0700840 !vmci_handle_is_equal(trans->qp_handle, e_payload->handle))
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -0700841 return;
Andy Kingd021c342013-02-06 14:23:56 +0000842
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -0700843 /* We don't ask for delayed CBs when we subscribe to this event (we
844 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
845 * guarantees in that case about what context we might be running in,
846 * so it could be BH or process, blockable or non-blockable. So we
847 * need to account for all possible contexts here.
848 */
849 spin_lock_bh(&trans->lock);
850 if (!trans->sk)
851 goto out;
852
853 /* Apart from here, trans->lock is only grabbed as part of sk destruct,
854 * where trans->sk isn't locked.
855 */
856 bh_lock_sock(trans->sk);
857
858 vmci_transport_handle_detach(trans->sk);
859
860 bh_unlock_sock(trans->sk);
861 out:
862 spin_unlock_bh(&trans->lock);
Andy Kingd021c342013-02-06 14:23:56 +0000863}
864
865static void vmci_transport_qp_resumed_cb(u32 sub_id,
866 const struct vmci_event_data *e_data,
867 void *client_data)
868{
869 vsock_for_each_connected_socket(vmci_transport_handle_detach);
870}
871
872static void vmci_transport_recv_pkt_work(struct work_struct *work)
873{
874 struct vmci_transport_recv_pkt_info *recv_pkt_info;
875 struct vmci_transport_packet *pkt;
876 struct sock *sk;
877
878 recv_pkt_info =
879 container_of(work, struct vmci_transport_recv_pkt_info, work);
880 sk = recv_pkt_info->sk;
881 pkt = &recv_pkt_info->pkt;
882
883 lock_sock(sk);
884
Reilly Grant990454b2013-04-01 11:41:52 -0700885 /* The local context ID may be out of date. */
886 vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context;
887
Andy Kingd021c342013-02-06 14:23:56 +0000888 switch (sk->sk_state) {
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400889 case TCP_LISTEN:
Andy Kingd021c342013-02-06 14:23:56 +0000890 vmci_transport_recv_listen(sk, pkt);
891 break;
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400892 case TCP_SYN_SENT:
Andy Kingd021c342013-02-06 14:23:56 +0000893 /* Processing of pending connections for servers goes through
894 * the listening socket, so see vmci_transport_recv_listen()
895 * for that path.
896 */
897 vmci_transport_recv_connecting_client(sk, pkt);
898 break;
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400899 case TCP_ESTABLISHED:
Andy Kingd021c342013-02-06 14:23:56 +0000900 vmci_transport_recv_connected(sk, pkt);
901 break;
902 default:
903 /* Because this function does not run in the same context as
904 * vmci_transport_recv_stream_cb it is possible that the
905 * socket has closed. We need to let the other side know or it
906 * could be sitting in a connect and hang forever. Send a
907 * reset to prevent that.
908 */
909 vmci_transport_send_reset(sk, pkt);
Asias He0fc93242013-06-20 17:20:32 +0800910 break;
Andy Kingd021c342013-02-06 14:23:56 +0000911 }
912
Andy Kingd021c342013-02-06 14:23:56 +0000913 release_sock(sk);
914 kfree(recv_pkt_info);
915 /* Release reference obtained in the stream callback when we fetched
916 * this socket out of the bound or connected list.
917 */
918 sock_put(sk);
919}
920
921static int vmci_transport_recv_listen(struct sock *sk,
922 struct vmci_transport_packet *pkt)
923{
924 struct sock *pending;
925 struct vsock_sock *vpending;
926 int err;
927 u64 qp_size;
928 bool old_request = false;
929 bool old_pkt_proto = false;
930
931 err = 0;
932
933 /* Because we are in the listen state, we could be receiving a packet
934 * for ourself or any previous connection requests that we received.
935 * If it's the latter, we try to find a socket in our list of pending
936 * connections and, if we do, call the appropriate handler for the
937 * state that that socket is in. Otherwise we try to service the
938 * connection request.
939 */
940 pending = vmci_transport_get_pending(sk, pkt);
941 if (pending) {
942 lock_sock(pending);
Reilly Grant990454b2013-04-01 11:41:52 -0700943
944 /* The local context ID may be out of date. */
945 vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context;
946
Andy Kingd021c342013-02-06 14:23:56 +0000947 switch (pending->sk_state) {
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400948 case TCP_SYN_SENT:
Andy Kingd021c342013-02-06 14:23:56 +0000949 err = vmci_transport_recv_connecting_server(sk,
950 pending,
951 pkt);
952 break;
953 default:
954 vmci_transport_send_reset(pending, pkt);
955 err = -EINVAL;
956 }
957
958 if (err < 0)
959 vsock_remove_pending(sk, pending);
960
961 release_sock(pending);
962 vmci_transport_release_pending(pending);
963
964 return err;
965 }
966
967 /* The listen state only accepts connection requests. Reply with a
968 * reset unless we received a reset.
969 */
970
971 if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST ||
972 pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) {
973 vmci_transport_reply_reset(pkt);
974 return -EINVAL;
975 }
976
977 if (pkt->u.size == 0) {
978 vmci_transport_reply_reset(pkt);
979 return -EINVAL;
980 }
981
982 /* If this socket can't accommodate this connection request, we send a
983 * reset. Otherwise we create and initialize a child socket and reply
984 * with a connection negotiation.
985 */
986 if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) {
987 vmci_transport_reply_reset(pkt);
988 return -ECONNREFUSED;
989 }
990
991 pending = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500992 sk->sk_type, 0);
Andy Kingd021c342013-02-06 14:23:56 +0000993 if (!pending) {
994 vmci_transport_send_reset(sk, pkt);
995 return -ENOMEM;
996 }
997
998 vpending = vsock_sk(pending);
999
1000 vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context,
1001 pkt->dst_port);
1002 vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context,
1003 pkt->src_port);
1004
1005 /* If the proposed size fits within our min/max, accept it. Otherwise
1006 * propose our own size.
1007 */
1008 if (pkt->u.size >= vmci_trans(vpending)->queue_pair_min_size &&
1009 pkt->u.size <= vmci_trans(vpending)->queue_pair_max_size) {
1010 qp_size = pkt->u.size;
1011 } else {
1012 qp_size = vmci_trans(vpending)->queue_pair_size;
1013 }
1014
1015 /* Figure out if we are using old or new requests based on the
1016 * overrides pkt types sent by our peer.
1017 */
1018 if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1019 old_request = old_pkt_proto;
1020 } else {
1021 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST)
1022 old_request = true;
1023 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)
1024 old_request = false;
1025
1026 }
1027
1028 if (old_request) {
1029 /* Handle a REQUEST (or override) */
1030 u16 version = VSOCK_PROTO_INVALID;
1031 if (vmci_transport_proto_to_notify_struct(
1032 pending, &version, true))
1033 err = vmci_transport_send_negotiate(pending, qp_size);
1034 else
1035 err = -EINVAL;
1036
1037 } else {
1038 /* Handle a REQUEST2 (or override) */
1039 int proto_int = pkt->proto;
1040 int pos;
1041 u16 active_proto_version = 0;
1042
1043 /* The list of possible protocols is the intersection of all
1044 * protocols the client supports ... plus all the protocols we
1045 * support.
1046 */
1047 proto_int &= vmci_transport_new_proto_supported_versions();
1048
1049 /* We choose the highest possible protocol version and use that
1050 * one.
1051 */
1052 pos = fls(proto_int);
1053 if (pos) {
1054 active_proto_version = (1 << (pos - 1));
1055 if (vmci_transport_proto_to_notify_struct(
1056 pending, &active_proto_version, false))
1057 err = vmci_transport_send_negotiate2(pending,
1058 qp_size,
1059 active_proto_version);
1060 else
1061 err = -EINVAL;
1062
1063 } else {
1064 err = -EINVAL;
1065 }
1066 }
1067
1068 if (err < 0) {
1069 vmci_transport_send_reset(sk, pkt);
1070 sock_put(pending);
1071 err = vmci_transport_error_to_vsock_error(err);
1072 goto out;
1073 }
1074
1075 vsock_add_pending(sk, pending);
1076 sk->sk_ack_backlog++;
1077
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001078 pending->sk_state = TCP_SYN_SENT;
Andy Kingd021c342013-02-06 14:23:56 +00001079 vmci_trans(vpending)->produce_size =
1080 vmci_trans(vpending)->consume_size = qp_size;
1081 vmci_trans(vpending)->queue_pair_size = qp_size;
1082
1083 vmci_trans(vpending)->notify_ops->process_request(pending);
1084
1085 /* We might never receive another message for this socket and it's not
1086 * connected to any process, so we have to ensure it gets cleaned up
1087 * ourself. Our delayed work function will take care of that. Note
1088 * that we do not ever cancel this function since we have few
1089 * guarantees about its state when calling cancel_delayed_work().
1090 * Instead we hold a reference on the socket for that function and make
1091 * it capable of handling cases where it needs to do nothing but
1092 * release that reference.
1093 */
1094 vpending->listener = sk;
1095 sock_hold(sk);
1096 sock_hold(pending);
Cong Wang455f05e2018-08-06 11:06:02 -07001097 schedule_delayed_work(&vpending->pending_work, HZ);
Andy Kingd021c342013-02-06 14:23:56 +00001098
1099out:
1100 return err;
1101}
1102
1103static int
1104vmci_transport_recv_connecting_server(struct sock *listener,
1105 struct sock *pending,
1106 struct vmci_transport_packet *pkt)
1107{
1108 struct vsock_sock *vpending;
1109 struct vmci_handle handle;
1110 struct vmci_qp *qpair;
1111 bool is_local;
1112 u32 flags;
1113 u32 detach_sub_id;
1114 int err;
1115 int skerr;
1116
1117 vpending = vsock_sk(pending);
1118 detach_sub_id = VMCI_INVALID_ID;
1119
1120 switch (pkt->type) {
1121 case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
1122 if (vmci_handle_is_invalid(pkt->u.handle)) {
1123 vmci_transport_send_reset(pending, pkt);
1124 skerr = EPROTO;
1125 err = -EINVAL;
1126 goto destroy;
1127 }
1128 break;
1129 default:
1130 /* Close and cleanup the connection. */
1131 vmci_transport_send_reset(pending, pkt);
1132 skerr = EPROTO;
1133 err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL;
1134 goto destroy;
1135 }
1136
1137 /* In order to complete the connection we need to attach to the offered
1138 * queue pair and send an attach notification. We also subscribe to the
1139 * detach event so we know when our peer goes away, and we do that
1140 * before attaching so we don't miss an event. If all this succeeds,
1141 * we update our state and wakeup anything waiting in accept() for a
1142 * connection.
1143 */
1144
1145 /* We don't care about attach since we ensure the other side has
1146 * attached by specifying the ATTACH_ONLY flag below.
1147 */
1148 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1149 vmci_transport_peer_detach_cb,
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001150 vmci_trans(vpending), &detach_sub_id);
Andy Kingd021c342013-02-06 14:23:56 +00001151 if (err < VMCI_SUCCESS) {
1152 vmci_transport_send_reset(pending, pkt);
1153 err = vmci_transport_error_to_vsock_error(err);
1154 skerr = -err;
1155 goto destroy;
1156 }
1157
1158 vmci_trans(vpending)->detach_sub_id = detach_sub_id;
1159
1160 /* Now attach to the queue pair the client created. */
1161 handle = pkt->u.handle;
1162
1163 /* vpending->local_addr always has a context id so we do not need to
1164 * worry about VMADDR_CID_ANY in this case.
1165 */
1166 is_local =
1167 vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid;
1168 flags = VMCI_QPFLAG_ATTACH_ONLY;
1169 flags |= is_local ? VMCI_QPFLAG_LOCAL : 0;
1170
1171 err = vmci_transport_queue_pair_alloc(
1172 &qpair,
1173 &handle,
1174 vmci_trans(vpending)->produce_size,
1175 vmci_trans(vpending)->consume_size,
1176 pkt->dg.src.context,
1177 flags,
1178 vmci_transport_is_trusted(
1179 vpending,
1180 vpending->remote_addr.svm_cid));
1181 if (err < 0) {
1182 vmci_transport_send_reset(pending, pkt);
1183 skerr = -err;
1184 goto destroy;
1185 }
1186
1187 vmci_trans(vpending)->qp_handle = handle;
1188 vmci_trans(vpending)->qpair = qpair;
1189
1190 /* When we send the attach message, we must be ready to handle incoming
1191 * control messages on the newly connected socket. So we move the
1192 * pending socket to the connected state before sending the attach
1193 * message. Otherwise, an incoming packet triggered by the attach being
1194 * received by the peer may be processed concurrently with what happens
1195 * below after sending the attach message, and that incoming packet
1196 * will find the listening socket instead of the (currently) pending
1197 * socket. Note that enqueueing the socket increments the reference
1198 * count, so even if a reset comes before the connection is accepted,
1199 * the socket will be valid until it is removed from the queue.
1200 *
1201 * If we fail sending the attach below, we remove the socket from the
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001202 * connected list and move the socket to TCP_CLOSE before
Andy Kingd021c342013-02-06 14:23:56 +00001203 * releasing the lock, so a pending slow path processing of an incoming
1204 * packet will not see the socket in the connected state in that case.
1205 */
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001206 pending->sk_state = TCP_ESTABLISHED;
Andy Kingd021c342013-02-06 14:23:56 +00001207
1208 vsock_insert_connected(vpending);
1209
1210 /* Notify our peer of our attach. */
1211 err = vmci_transport_send_attach(pending, handle);
1212 if (err < 0) {
1213 vsock_remove_connected(vpending);
1214 pr_err("Could not send attach\n");
1215 vmci_transport_send_reset(pending, pkt);
1216 err = vmci_transport_error_to_vsock_error(err);
1217 skerr = -err;
1218 goto destroy;
1219 }
1220
1221 /* We have a connection. Move the now connected socket from the
1222 * listener's pending list to the accept queue so callers of accept()
1223 * can find it.
1224 */
1225 vsock_remove_pending(listener, pending);
1226 vsock_enqueue_accept(listener, pending);
1227
1228 /* Callers of accept() will be be waiting on the listening socket, not
1229 * the pending socket.
1230 */
Stefan Hajnoczi73629452015-11-04 12:58:42 +00001231 listener->sk_data_ready(listener);
Andy Kingd021c342013-02-06 14:23:56 +00001232
1233 return 0;
1234
1235destroy:
1236 pending->sk_err = skerr;
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001237 pending->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +00001238 /* As long as we drop our reference, all necessary cleanup will handle
1239 * when the cleanup function drops its reference and our destruct
1240 * implementation is called. Note that since the listen handler will
1241 * remove pending from the pending list upon our failure, the cleanup
1242 * function won't drop the additional reference, which is why we do it
1243 * here.
1244 */
1245 sock_put(pending);
1246
1247 return err;
1248}
1249
1250static int
1251vmci_transport_recv_connecting_client(struct sock *sk,
1252 struct vmci_transport_packet *pkt)
1253{
1254 struct vsock_sock *vsk;
1255 int err;
1256 int skerr;
1257
1258 vsk = vsock_sk(sk);
1259
1260 switch (pkt->type) {
1261 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
1262 if (vmci_handle_is_invalid(pkt->u.handle) ||
1263 !vmci_handle_is_equal(pkt->u.handle,
1264 vmci_trans(vsk)->qp_handle)) {
1265 skerr = EPROTO;
1266 err = -EINVAL;
1267 goto destroy;
1268 }
1269
1270 /* Signify the socket is connected and wakeup the waiter in
1271 * connect(). Also place the socket in the connected table for
1272 * accounting (it can already be found since it's in the bound
1273 * table).
1274 */
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001275 sk->sk_state = TCP_ESTABLISHED;
Andy Kingd021c342013-02-06 14:23:56 +00001276 sk->sk_socket->state = SS_CONNECTED;
1277 vsock_insert_connected(vsk);
1278 sk->sk_state_change(sk);
1279
1280 break;
1281 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
1282 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
1283 if (pkt->u.size == 0
1284 || pkt->dg.src.context != vsk->remote_addr.svm_cid
1285 || pkt->src_port != vsk->remote_addr.svm_port
1286 || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)
1287 || vmci_trans(vsk)->qpair
1288 || vmci_trans(vsk)->produce_size != 0
1289 || vmci_trans(vsk)->consume_size != 0
Andy Kingd021c342013-02-06 14:23:56 +00001290 || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) {
1291 skerr = EPROTO;
1292 err = -EINVAL;
1293
1294 goto destroy;
1295 }
1296
1297 err = vmci_transport_recv_connecting_client_negotiate(sk, pkt);
1298 if (err) {
1299 skerr = -err;
1300 goto destroy;
1301 }
1302
1303 break;
1304 case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
1305 err = vmci_transport_recv_connecting_client_invalid(sk, pkt);
1306 if (err) {
1307 skerr = -err;
1308 goto destroy;
1309 }
1310
1311 break;
1312 case VMCI_TRANSPORT_PACKET_TYPE_RST:
1313 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1314 * continue processing here after they sent an INVALID packet.
1315 * This meant that we got a RST after the INVALID. We ignore a
1316 * RST after an INVALID. The common code doesn't send the RST
1317 * ... so we can hang if an old version of the common code
1318 * fails between getting a REQUEST and sending an OFFER back.
1319 * Not much we can do about it... except hope that it doesn't
1320 * happen.
1321 */
1322 if (vsk->ignore_connecting_rst) {
1323 vsk->ignore_connecting_rst = false;
1324 } else {
1325 skerr = ECONNRESET;
1326 err = 0;
1327 goto destroy;
1328 }
1329
1330 break;
1331 default:
1332 /* Close and cleanup the connection. */
1333 skerr = EPROTO;
1334 err = -EINVAL;
1335 goto destroy;
1336 }
1337
1338 return 0;
1339
1340destroy:
1341 vmci_transport_send_reset(sk, pkt);
1342
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001343 sk->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +00001344 sk->sk_err = skerr;
1345 sk->sk_error_report(sk);
1346 return err;
1347}
1348
1349static int vmci_transport_recv_connecting_client_negotiate(
1350 struct sock *sk,
1351 struct vmci_transport_packet *pkt)
1352{
1353 int err;
1354 struct vsock_sock *vsk;
1355 struct vmci_handle handle;
1356 struct vmci_qp *qpair;
Andy Kingd021c342013-02-06 14:23:56 +00001357 u32 detach_sub_id;
1358 bool is_local;
1359 u32 flags;
1360 bool old_proto = true;
1361 bool old_pkt_proto;
1362 u16 version;
1363
1364 vsk = vsock_sk(sk);
1365 handle = VMCI_INVALID_HANDLE;
Andy Kingd021c342013-02-06 14:23:56 +00001366 detach_sub_id = VMCI_INVALID_ID;
1367
1368 /* If we have gotten here then we should be past the point where old
1369 * linux vsock could have sent the bogus rst.
1370 */
1371 vsk->sent_request = false;
1372 vsk->ignore_connecting_rst = false;
1373
1374 /* Verify that we're OK with the proposed queue pair size */
1375 if (pkt->u.size < vmci_trans(vsk)->queue_pair_min_size ||
1376 pkt->u.size > vmci_trans(vsk)->queue_pair_max_size) {
1377 err = -EINVAL;
1378 goto destroy;
1379 }
1380
1381 /* At this point we know the CID the peer is using to talk to us. */
1382
1383 if (vsk->local_addr.svm_cid == VMADDR_CID_ANY)
1384 vsk->local_addr.svm_cid = pkt->dg.dst.context;
1385
1386 /* Setup the notify ops to be the highest supported version that both
1387 * the server and the client support.
1388 */
1389
1390 if (vmci_transport_old_proto_override(&old_pkt_proto)) {
1391 old_proto = old_pkt_proto;
1392 } else {
1393 if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE)
1394 old_proto = true;
1395 else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2)
1396 old_proto = false;
1397
1398 }
1399
1400 if (old_proto)
1401 version = VSOCK_PROTO_INVALID;
1402 else
1403 version = pkt->proto;
1404
1405 if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) {
1406 err = -EINVAL;
1407 goto destroy;
1408 }
1409
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001410 /* Subscribe to detach events first.
Andy Kingd021c342013-02-06 14:23:56 +00001411 *
1412 * XXX We attach once for each queue pair created for now so it is easy
1413 * to find the socket (it's provided), but later we should only
1414 * subscribe once and add a way to lookup sockets by queue pair handle.
1415 */
Andy Kingd021c342013-02-06 14:23:56 +00001416 err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
1417 vmci_transport_peer_detach_cb,
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001418 vmci_trans(vsk), &detach_sub_id);
Andy Kingd021c342013-02-06 14:23:56 +00001419 if (err < VMCI_SUCCESS) {
1420 err = vmci_transport_error_to_vsock_error(err);
1421 goto destroy;
1422 }
1423
1424 /* Make VMCI select the handle for us. */
1425 handle = VMCI_INVALID_HANDLE;
1426 is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid;
1427 flags = is_local ? VMCI_QPFLAG_LOCAL : 0;
1428
1429 err = vmci_transport_queue_pair_alloc(&qpair,
1430 &handle,
1431 pkt->u.size,
1432 pkt->u.size,
1433 vsk->remote_addr.svm_cid,
1434 flags,
1435 vmci_transport_is_trusted(
1436 vsk,
1437 vsk->
1438 remote_addr.svm_cid));
1439 if (err < 0)
1440 goto destroy;
1441
1442 err = vmci_transport_send_qp_offer(sk, handle);
1443 if (err < 0) {
1444 err = vmci_transport_error_to_vsock_error(err);
1445 goto destroy;
1446 }
1447
1448 vmci_trans(vsk)->qp_handle = handle;
1449 vmci_trans(vsk)->qpair = qpair;
1450
1451 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size =
1452 pkt->u.size;
1453
Andy Kingd021c342013-02-06 14:23:56 +00001454 vmci_trans(vsk)->detach_sub_id = detach_sub_id;
1455
1456 vmci_trans(vsk)->notify_ops->process_negotiate(sk);
1457
1458 return 0;
1459
1460destroy:
Andy Kingd021c342013-02-06 14:23:56 +00001461 if (detach_sub_id != VMCI_INVALID_ID)
1462 vmci_event_unsubscribe(detach_sub_id);
1463
1464 if (!vmci_handle_is_invalid(handle))
1465 vmci_qpair_detach(&qpair);
1466
1467 return err;
1468}
1469
1470static int
1471vmci_transport_recv_connecting_client_invalid(struct sock *sk,
1472 struct vmci_transport_packet *pkt)
1473{
1474 int err = 0;
1475 struct vsock_sock *vsk = vsock_sk(sk);
1476
1477 if (vsk->sent_request) {
1478 vsk->sent_request = false;
1479 vsk->ignore_connecting_rst = true;
1480
1481 err = vmci_transport_send_conn_request(
1482 sk, vmci_trans(vsk)->queue_pair_size);
1483 if (err < 0)
1484 err = vmci_transport_error_to_vsock_error(err);
1485 else
1486 err = 0;
1487
1488 }
1489
1490 return err;
1491}
1492
1493static int vmci_transport_recv_connected(struct sock *sk,
1494 struct vmci_transport_packet *pkt)
1495{
1496 struct vsock_sock *vsk;
1497 bool pkt_processed = false;
1498
1499 /* In cases where we are closing the connection, it's sufficient to
1500 * mark the state change (and maybe error) and wake up any waiting
1501 * threads. Since this is a connected socket, it's owned by a user
1502 * process and will be cleaned up when the failure is passed back on
1503 * the current or next system call. Our system call implementations
1504 * must therefore check for error and state changes on entry and when
1505 * being awoken.
1506 */
1507 switch (pkt->type) {
1508 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
1509 if (pkt->u.mode) {
1510 vsk = vsock_sk(sk);
1511
1512 vsk->peer_shutdown |= pkt->u.mode;
1513 sk->sk_state_change(sk);
1514 }
1515 break;
1516
1517 case VMCI_TRANSPORT_PACKET_TYPE_RST:
1518 vsk = vsock_sk(sk);
1519 /* It is possible that we sent our peer a message (e.g a
1520 * WAITING_READ) right before we got notified that the peer had
1521 * detached. If that happens then we can get a RST pkt back
1522 * from our peer even though there is data available for us to
1523 * read. In that case, don't shutdown the socket completely but
1524 * instead allow the local client to finish reading data off
1525 * the queuepair. Always treat a RST pkt in connected mode like
1526 * a clean shutdown.
1527 */
1528 sock_set_flag(sk, SOCK_DONE);
1529 vsk->peer_shutdown = SHUTDOWN_MASK;
1530 if (vsock_stream_has_data(vsk) <= 0)
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001531 sk->sk_state = TCP_CLOSING;
Andy Kingd021c342013-02-06 14:23:56 +00001532
1533 sk->sk_state_change(sk);
1534 break;
1535
1536 default:
1537 vsk = vsock_sk(sk);
1538 vmci_trans(vsk)->notify_ops->handle_notify_pkt(
1539 sk, pkt, false, NULL, NULL,
1540 &pkt_processed);
1541 if (!pkt_processed)
1542 return -EINVAL;
1543
1544 break;
1545 }
1546
1547 return 0;
1548}
1549
1550static int vmci_transport_socket_init(struct vsock_sock *vsk,
1551 struct vsock_sock *psk)
1552{
1553 vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL);
1554 if (!vsk->trans)
1555 return -ENOMEM;
1556
1557 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1558 vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE;
1559 vmci_trans(vsk)->qpair = NULL;
1560 vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0;
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001561 vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID;
Andy Kingd021c342013-02-06 14:23:56 +00001562 vmci_trans(vsk)->notify_ops = NULL;
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001563 INIT_LIST_HEAD(&vmci_trans(vsk)->elem);
1564 vmci_trans(vsk)->sk = &vsk->sk;
Jorgen Hansen8566b862015-10-22 08:25:25 -07001565 spin_lock_init(&vmci_trans(vsk)->lock);
Andy Kingd021c342013-02-06 14:23:56 +00001566 if (psk) {
1567 vmci_trans(vsk)->queue_pair_size =
1568 vmci_trans(psk)->queue_pair_size;
1569 vmci_trans(vsk)->queue_pair_min_size =
1570 vmci_trans(psk)->queue_pair_min_size;
1571 vmci_trans(vsk)->queue_pair_max_size =
1572 vmci_trans(psk)->queue_pair_max_size;
1573 } else {
1574 vmci_trans(vsk)->queue_pair_size =
1575 VMCI_TRANSPORT_DEFAULT_QP_SIZE;
1576 vmci_trans(vsk)->queue_pair_min_size =
1577 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN;
1578 vmci_trans(vsk)->queue_pair_max_size =
1579 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX;
1580 }
1581
1582 return 0;
1583}
1584
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001585static void vmci_transport_free_resources(struct list_head *transport_list)
1586{
1587 while (!list_empty(transport_list)) {
1588 struct vmci_transport *transport =
1589 list_first_entry(transport_list, struct vmci_transport,
1590 elem);
1591 list_del(&transport->elem);
1592
1593 if (transport->detach_sub_id != VMCI_INVALID_ID) {
1594 vmci_event_unsubscribe(transport->detach_sub_id);
1595 transport->detach_sub_id = VMCI_INVALID_ID;
1596 }
1597
1598 if (!vmci_handle_is_invalid(transport->qp_handle)) {
1599 vmci_qpair_detach(&transport->qpair);
1600 transport->qp_handle = VMCI_INVALID_HANDLE;
1601 transport->produce_size = 0;
1602 transport->consume_size = 0;
1603 }
1604
1605 kfree(transport);
1606 }
1607}
1608
1609static void vmci_transport_cleanup(struct work_struct *work)
1610{
1611 LIST_HEAD(pending);
1612
1613 spin_lock_bh(&vmci_transport_cleanup_lock);
1614 list_replace_init(&vmci_transport_cleanup_list, &pending);
1615 spin_unlock_bh(&vmci_transport_cleanup_lock);
1616 vmci_transport_free_resources(&pending);
1617}
1618
Andy Kingd021c342013-02-06 14:23:56 +00001619static void vmci_transport_destruct(struct vsock_sock *vsk)
1620{
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001621 /* Ensure that the detach callback doesn't use the sk/vsk
1622 * we are about to destruct.
1623 */
1624 spin_lock_bh(&vmci_trans(vsk)->lock);
1625 vmci_trans(vsk)->sk = NULL;
1626 spin_unlock_bh(&vmci_trans(vsk)->lock);
Andy Kingd021c342013-02-06 14:23:56 +00001627
1628 if (vmci_trans(vsk)->notify_ops)
1629 vmci_trans(vsk)->notify_ops->socket_destruct(vsk);
1630
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07001631 spin_lock_bh(&vmci_transport_cleanup_lock);
1632 list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list);
1633 spin_unlock_bh(&vmci_transport_cleanup_lock);
1634 schedule_work(&vmci_transport_cleanup_work);
1635
Andy Kingd021c342013-02-06 14:23:56 +00001636 vsk->trans = NULL;
1637}
1638
1639static void vmci_transport_release(struct vsock_sock *vsk)
1640{
Stefan Hajnoczi6773b7d2016-07-28 15:36:31 +01001641 vsock_remove_sock(vsk);
1642
Andy Kingd021c342013-02-06 14:23:56 +00001643 if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) {
1644 vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle);
1645 vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
1646 }
1647}
1648
1649static int vmci_transport_dgram_bind(struct vsock_sock *vsk,
1650 struct sockaddr_vm *addr)
1651{
1652 u32 port;
1653 u32 flags;
1654 int err;
1655
1656 /* VMCI will select a resource ID for us if we provide
1657 * VMCI_INVALID_ID.
1658 */
1659 port = addr->svm_port == VMADDR_PORT_ANY ?
1660 VMCI_INVALID_ID : addr->svm_port;
1661
1662 if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE))
1663 return -EACCES;
1664
1665 flags = addr->svm_cid == VMADDR_CID_ANY ?
1666 VMCI_FLAG_ANYCID_DG_HND : 0;
1667
1668 err = vmci_transport_datagram_create_hnd(port, flags,
1669 vmci_transport_recv_dgram_cb,
1670 &vsk->sk,
1671 &vmci_trans(vsk)->dg_handle);
1672 if (err < VMCI_SUCCESS)
1673 return vmci_transport_error_to_vsock_error(err);
1674 vsock_addr_init(&vsk->local_addr, addr->svm_cid,
1675 vmci_trans(vsk)->dg_handle.resource);
1676
1677 return 0;
1678}
1679
1680static int vmci_transport_dgram_enqueue(
1681 struct vsock_sock *vsk,
1682 struct sockaddr_vm *remote_addr,
Al Viro0f7db232014-11-20 04:05:34 -05001683 struct msghdr *msg,
Andy Kingd021c342013-02-06 14:23:56 +00001684 size_t len)
1685{
1686 int err;
1687 struct vmci_datagram *dg;
1688
1689 if (len > VMCI_MAX_DG_PAYLOAD_SIZE)
1690 return -EMSGSIZE;
1691
1692 if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid))
1693 return -EPERM;
1694
1695 /* Allocate a buffer for the user's message and our packet header. */
1696 dg = kmalloc(len + sizeof(*dg), GFP_KERNEL);
1697 if (!dg)
1698 return -ENOMEM;
1699
Al Viro0f7db232014-11-20 04:05:34 -05001700 memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len);
Andy Kingd021c342013-02-06 14:23:56 +00001701
1702 dg->dst = vmci_make_handle(remote_addr->svm_cid,
1703 remote_addr->svm_port);
1704 dg->src = vmci_make_handle(vsk->local_addr.svm_cid,
1705 vsk->local_addr.svm_port);
1706 dg->payload_size = len;
1707
1708 err = vmci_datagram_send(dg);
1709 kfree(dg);
1710 if (err < 0)
1711 return vmci_transport_error_to_vsock_error(err);
1712
1713 return err - sizeof(*dg);
1714}
1715
Ying Xue1b784142015-03-02 15:37:48 +08001716static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,
Andy Kingd021c342013-02-06 14:23:56 +00001717 struct msghdr *msg, size_t len,
1718 int flags)
1719{
1720 int err;
1721 int noblock;
1722 struct vmci_datagram *dg;
1723 size_t payload_len;
1724 struct sk_buff *skb;
1725
1726 noblock = flags & MSG_DONTWAIT;
1727
1728 if (flags & MSG_OOB || flags & MSG_ERRQUEUE)
1729 return -EOPNOTSUPP;
1730
1731 /* Retrieve the head sk_buff from the socket's receive queue. */
1732 err = 0;
1733 skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err);
Andy Kingd021c342013-02-06 14:23:56 +00001734 if (!skb)
Jorgen Hansen9c995cc2016-04-18 23:58:52 -07001735 return err;
Andy Kingd021c342013-02-06 14:23:56 +00001736
1737 dg = (struct vmci_datagram *)skb->data;
1738 if (!dg)
1739 /* err is 0, meaning we read zero bytes. */
1740 goto out;
1741
1742 payload_len = dg->payload_size;
1743 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1744 if (payload_len != skb->len - sizeof(*dg)) {
1745 err = -EINVAL;
1746 goto out;
1747 }
1748
1749 if (payload_len > len) {
1750 payload_len = len;
1751 msg->msg_flags |= MSG_TRUNC;
1752 }
1753
1754 /* Place the datagram payload in the user's iovec. */
David S. Miller51f3d022014-11-05 16:46:40 -05001755 err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len);
Andy Kingd021c342013-02-06 14:23:56 +00001756 if (err)
1757 goto out;
1758
Andy Kingd021c342013-02-06 14:23:56 +00001759 if (msg->msg_name) {
Andy Kingd021c342013-02-06 14:23:56 +00001760 /* Provide the address of the sender. */
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001761 DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name);
Andy Kingd021c342013-02-06 14:23:56 +00001762 vsock_addr_init(vm_addr, dg->src.context, dg->src.resource);
1763 msg->msg_namelen = sizeof(*vm_addr);
1764 }
1765 err = payload_len;
1766
1767out:
1768 skb_free_datagram(&vsk->sk, skb);
1769 return err;
1770}
1771
1772static bool vmci_transport_dgram_allow(u32 cid, u32 port)
1773{
1774 if (cid == VMADDR_CID_HYPERVISOR) {
1775 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1776 * state and are allowed.
1777 */
1778 return port == VMCI_UNITY_PBRPC_REGISTER;
1779 }
1780
1781 return true;
1782}
1783
1784static int vmci_transport_connect(struct vsock_sock *vsk)
1785{
1786 int err;
1787 bool old_pkt_proto = false;
1788 struct sock *sk = &vsk->sk;
1789
1790 if (vmci_transport_old_proto_override(&old_pkt_proto) &&
1791 old_pkt_proto) {
1792 err = vmci_transport_send_conn_request(
1793 sk, vmci_trans(vsk)->queue_pair_size);
1794 if (err < 0) {
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001795 sk->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +00001796 return err;
1797 }
1798 } else {
1799 int supported_proto_versions =
1800 vmci_transport_new_proto_supported_versions();
1801 err = vmci_transport_send_conn_request2(
1802 sk, vmci_trans(vsk)->queue_pair_size,
1803 supported_proto_versions);
1804 if (err < 0) {
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001805 sk->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +00001806 return err;
1807 }
1808
1809 vsk->sent_request = true;
1810 }
1811
1812 return err;
1813}
1814
1815static ssize_t vmci_transport_stream_dequeue(
1816 struct vsock_sock *vsk,
Al Viro0f7db232014-11-20 04:05:34 -05001817 struct msghdr *msg,
Andy Kingd021c342013-02-06 14:23:56 +00001818 size_t len,
1819 int flags)
1820{
1821 if (flags & MSG_PEEK)
Al Virod838df22014-11-24 19:32:50 -05001822 return vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0);
Andy Kingd021c342013-02-06 14:23:56 +00001823 else
Al Virod838df22014-11-24 19:32:50 -05001824 return vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0);
Andy Kingd021c342013-02-06 14:23:56 +00001825}
1826
1827static ssize_t vmci_transport_stream_enqueue(
1828 struct vsock_sock *vsk,
Al Viro0f7db232014-11-20 04:05:34 -05001829 struct msghdr *msg,
Andy Kingd021c342013-02-06 14:23:56 +00001830 size_t len)
1831{
Al Viro4c946d92014-11-27 19:52:04 -05001832 return vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0);
Andy Kingd021c342013-02-06 14:23:56 +00001833}
1834
1835static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk)
1836{
1837 return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair);
1838}
1839
1840static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk)
1841{
1842 return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair);
1843}
1844
1845static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk)
1846{
1847 return vmci_trans(vsk)->consume_size;
1848}
1849
1850static bool vmci_transport_stream_is_active(struct vsock_sock *vsk)
1851{
1852 return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle);
1853}
1854
1855static u64 vmci_transport_get_buffer_size(struct vsock_sock *vsk)
1856{
1857 return vmci_trans(vsk)->queue_pair_size;
1858}
1859
1860static u64 vmci_transport_get_min_buffer_size(struct vsock_sock *vsk)
1861{
1862 return vmci_trans(vsk)->queue_pair_min_size;
1863}
1864
1865static u64 vmci_transport_get_max_buffer_size(struct vsock_sock *vsk)
1866{
1867 return vmci_trans(vsk)->queue_pair_max_size;
1868}
1869
1870static void vmci_transport_set_buffer_size(struct vsock_sock *vsk, u64 val)
1871{
1872 if (val < vmci_trans(vsk)->queue_pair_min_size)
1873 vmci_trans(vsk)->queue_pair_min_size = val;
1874 if (val > vmci_trans(vsk)->queue_pair_max_size)
1875 vmci_trans(vsk)->queue_pair_max_size = val;
1876 vmci_trans(vsk)->queue_pair_size = val;
1877}
1878
1879static void vmci_transport_set_min_buffer_size(struct vsock_sock *vsk,
1880 u64 val)
1881{
1882 if (val > vmci_trans(vsk)->queue_pair_size)
1883 vmci_trans(vsk)->queue_pair_size = val;
1884 vmci_trans(vsk)->queue_pair_min_size = val;
1885}
1886
1887static void vmci_transport_set_max_buffer_size(struct vsock_sock *vsk,
1888 u64 val)
1889{
1890 if (val < vmci_trans(vsk)->queue_pair_size)
1891 vmci_trans(vsk)->queue_pair_size = val;
1892 vmci_trans(vsk)->queue_pair_max_size = val;
1893}
1894
1895static int vmci_transport_notify_poll_in(
1896 struct vsock_sock *vsk,
1897 size_t target,
1898 bool *data_ready_now)
1899{
1900 return vmci_trans(vsk)->notify_ops->poll_in(
1901 &vsk->sk, target, data_ready_now);
1902}
1903
1904static int vmci_transport_notify_poll_out(
1905 struct vsock_sock *vsk,
1906 size_t target,
1907 bool *space_available_now)
1908{
1909 return vmci_trans(vsk)->notify_ops->poll_out(
1910 &vsk->sk, target, space_available_now);
1911}
1912
1913static int vmci_transport_notify_recv_init(
1914 struct vsock_sock *vsk,
1915 size_t target,
1916 struct vsock_transport_recv_notify_data *data)
1917{
1918 return vmci_trans(vsk)->notify_ops->recv_init(
1919 &vsk->sk, target,
1920 (struct vmci_transport_recv_notify_data *)data);
1921}
1922
1923static int vmci_transport_notify_recv_pre_block(
1924 struct vsock_sock *vsk,
1925 size_t target,
1926 struct vsock_transport_recv_notify_data *data)
1927{
1928 return vmci_trans(vsk)->notify_ops->recv_pre_block(
1929 &vsk->sk, target,
1930 (struct vmci_transport_recv_notify_data *)data);
1931}
1932
1933static int vmci_transport_notify_recv_pre_dequeue(
1934 struct vsock_sock *vsk,
1935 size_t target,
1936 struct vsock_transport_recv_notify_data *data)
1937{
1938 return vmci_trans(vsk)->notify_ops->recv_pre_dequeue(
1939 &vsk->sk, target,
1940 (struct vmci_transport_recv_notify_data *)data);
1941}
1942
1943static int vmci_transport_notify_recv_post_dequeue(
1944 struct vsock_sock *vsk,
1945 size_t target,
1946 ssize_t copied,
1947 bool data_read,
1948 struct vsock_transport_recv_notify_data *data)
1949{
1950 return vmci_trans(vsk)->notify_ops->recv_post_dequeue(
1951 &vsk->sk, target, copied, data_read,
1952 (struct vmci_transport_recv_notify_data *)data);
1953}
1954
1955static int vmci_transport_notify_send_init(
1956 struct vsock_sock *vsk,
1957 struct vsock_transport_send_notify_data *data)
1958{
1959 return vmci_trans(vsk)->notify_ops->send_init(
1960 &vsk->sk,
1961 (struct vmci_transport_send_notify_data *)data);
1962}
1963
1964static int vmci_transport_notify_send_pre_block(
1965 struct vsock_sock *vsk,
1966 struct vsock_transport_send_notify_data *data)
1967{
1968 return vmci_trans(vsk)->notify_ops->send_pre_block(
1969 &vsk->sk,
1970 (struct vmci_transport_send_notify_data *)data);
1971}
1972
1973static int vmci_transport_notify_send_pre_enqueue(
1974 struct vsock_sock *vsk,
1975 struct vsock_transport_send_notify_data *data)
1976{
1977 return vmci_trans(vsk)->notify_ops->send_pre_enqueue(
1978 &vsk->sk,
1979 (struct vmci_transport_send_notify_data *)data);
1980}
1981
1982static int vmci_transport_notify_send_post_enqueue(
1983 struct vsock_sock *vsk,
1984 ssize_t written,
1985 struct vsock_transport_send_notify_data *data)
1986{
1987 return vmci_trans(vsk)->notify_ops->send_post_enqueue(
1988 &vsk->sk, written,
1989 (struct vmci_transport_send_notify_data *)data);
1990}
1991
1992static bool vmci_transport_old_proto_override(bool *old_pkt_proto)
1993{
1994 if (PROTOCOL_OVERRIDE != -1) {
1995 if (PROTOCOL_OVERRIDE == 0)
1996 *old_pkt_proto = true;
1997 else
1998 *old_pkt_proto = false;
1999
2000 pr_info("Proto override in use\n");
2001 return true;
2002 }
2003
2004 return false;
2005}
2006
2007static bool vmci_transport_proto_to_notify_struct(struct sock *sk,
2008 u16 *proto,
2009 bool old_pkt_proto)
2010{
2011 struct vsock_sock *vsk = vsock_sk(sk);
2012
2013 if (old_pkt_proto) {
2014 if (*proto != VSOCK_PROTO_INVALID) {
2015 pr_err("Can't set both an old and new protocol\n");
2016 return false;
2017 }
2018 vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops;
2019 goto exit;
2020 }
2021
2022 switch (*proto) {
2023 case VSOCK_PROTO_PKT_ON_NOTIFY:
2024 vmci_trans(vsk)->notify_ops =
2025 &vmci_transport_notify_pkt_q_state_ops;
2026 break;
2027 default:
2028 pr_err("Unknown notify protocol version\n");
2029 return false;
2030 }
2031
2032exit:
2033 vmci_trans(vsk)->notify_ops->socket_init(sk);
2034 return true;
2035}
2036
2037static u16 vmci_transport_new_proto_supported_versions(void)
2038{
2039 if (PROTOCOL_OVERRIDE != -1)
2040 return PROTOCOL_OVERRIDE;
2041
2042 return VSOCK_PROTO_ALL_SUPPORTED;
2043}
2044
2045static u32 vmci_transport_get_local_cid(void)
2046{
2047 return vmci_get_context_id();
2048}
2049
Julia Lawall56130912016-05-01 14:49:15 +02002050static const struct vsock_transport vmci_transport = {
Andy Kingd021c342013-02-06 14:23:56 +00002051 .init = vmci_transport_socket_init,
2052 .destruct = vmci_transport_destruct,
2053 .release = vmci_transport_release,
2054 .connect = vmci_transport_connect,
2055 .dgram_bind = vmci_transport_dgram_bind,
2056 .dgram_dequeue = vmci_transport_dgram_dequeue,
2057 .dgram_enqueue = vmci_transport_dgram_enqueue,
2058 .dgram_allow = vmci_transport_dgram_allow,
2059 .stream_dequeue = vmci_transport_stream_dequeue,
2060 .stream_enqueue = vmci_transport_stream_enqueue,
2061 .stream_has_data = vmci_transport_stream_has_data,
2062 .stream_has_space = vmci_transport_stream_has_space,
2063 .stream_rcvhiwat = vmci_transport_stream_rcvhiwat,
2064 .stream_is_active = vmci_transport_stream_is_active,
2065 .stream_allow = vmci_transport_stream_allow,
2066 .notify_poll_in = vmci_transport_notify_poll_in,
2067 .notify_poll_out = vmci_transport_notify_poll_out,
2068 .notify_recv_init = vmci_transport_notify_recv_init,
2069 .notify_recv_pre_block = vmci_transport_notify_recv_pre_block,
2070 .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue,
2071 .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue,
2072 .notify_send_init = vmci_transport_notify_send_init,
2073 .notify_send_pre_block = vmci_transport_notify_send_pre_block,
2074 .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue,
2075 .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue,
2076 .shutdown = vmci_transport_shutdown,
2077 .set_buffer_size = vmci_transport_set_buffer_size,
2078 .set_min_buffer_size = vmci_transport_set_min_buffer_size,
2079 .set_max_buffer_size = vmci_transport_set_max_buffer_size,
2080 .get_buffer_size = vmci_transport_get_buffer_size,
2081 .get_min_buffer_size = vmci_transport_get_min_buffer_size,
2082 .get_max_buffer_size = vmci_transport_get_max_buffer_size,
2083 .get_local_cid = vmci_transport_get_local_cid,
2084};
2085
2086static int __init vmci_transport_init(void)
2087{
2088 int err;
2089
2090 /* Create the datagram handle that we will use to send and receive all
2091 * VSocket control messages for this context.
2092 */
2093 err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID,
2094 VMCI_FLAG_ANYCID_DG_HND,
2095 vmci_transport_recv_stream_cb,
2096 NULL,
2097 &vmci_transport_stream_handle);
2098 if (err < VMCI_SUCCESS) {
2099 pr_err("Unable to create datagram handle. (%d)\n", err);
2100 return vmci_transport_error_to_vsock_error(err);
2101 }
2102
2103 err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED,
2104 vmci_transport_qp_resumed_cb,
2105 NULL, &vmci_transport_qp_resumed_sub_id);
2106 if (err < VMCI_SUCCESS) {
2107 pr_err("Unable to subscribe to resumed event. (%d)\n", err);
2108 err = vmci_transport_error_to_vsock_error(err);
2109 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2110 goto err_destroy_stream_handle;
2111 }
2112
2113 err = vsock_core_init(&vmci_transport);
2114 if (err < 0)
2115 goto err_unsubscribe;
2116
2117 return 0;
2118
2119err_unsubscribe:
2120 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2121err_destroy_stream_handle:
2122 vmci_datagram_destroy_handle(vmci_transport_stream_handle);
2123 return err;
2124}
2125module_init(vmci_transport_init);
2126
2127static void __exit vmci_transport_exit(void)
2128{
Jorgen Hansen4ef7ea92015-10-21 04:53:56 -07002129 cancel_work_sync(&vmci_transport_cleanup_work);
2130 vmci_transport_free_resources(&vmci_transport_cleanup_list);
2131
Andy Kingd021c342013-02-06 14:23:56 +00002132 if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) {
2133 if (vmci_datagram_destroy_handle(
2134 vmci_transport_stream_handle) != VMCI_SUCCESS)
2135 pr_err("Couldn't destroy datagram handle\n");
2136 vmci_transport_stream_handle = VMCI_INVALID_HANDLE;
2137 }
2138
2139 if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) {
2140 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
2141 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
2142 }
2143
2144 vsock_core_exit();
2145}
2146module_exit(vmci_transport_exit);
2147
2148MODULE_AUTHOR("VMware, Inc.");
2149MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
Jorgen Hansenafbea2c2017-11-24 06:25:28 -08002150MODULE_VERSION("1.0.5.0-k");
Andy Kingd021c342013-02-06 14:23:56 +00002151MODULE_LICENSE("GPL v2");
2152MODULE_ALIAS("vmware_vsock");
2153MODULE_ALIAS_NETPROTO(PF_VSOCK);