2 * VMware vSockets Driver
4 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
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.
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
16 #include <linux/types.h>
17 #include <linux/bitops.h>
18 #include <linux/cred.h>
19 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/kmod.h>
23 #include <linux/list.h>
24 #include <linux/miscdevice.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/net.h>
28 #include <linux/poll.h>
29 #include <linux/skbuff.h>
30 #include <linux/smp.h>
31 #include <linux/socket.h>
32 #include <linux/stddef.h>
33 #include <linux/unistd.h>
34 #include <linux/wait.h>
35 #include <linux/workqueue.h>
37 #include <net/af_vsock.h>
39 #include "vmci_transport_notify.h"
41 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
);
42 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
);
43 static void vmci_transport_peer_detach_cb(u32 sub_id
,
44 const struct vmci_event_data
*ed
,
46 static void vmci_transport_recv_pkt_work(struct work_struct
*work
);
47 static void vmci_transport_cleanup(struct work_struct
*work
);
48 static int vmci_transport_recv_listen(struct sock
*sk
,
49 struct vmci_transport_packet
*pkt
);
50 static int vmci_transport_recv_connecting_server(
53 struct vmci_transport_packet
*pkt
);
54 static int vmci_transport_recv_connecting_client(
56 struct vmci_transport_packet
*pkt
);
57 static int vmci_transport_recv_connecting_client_negotiate(
59 struct vmci_transport_packet
*pkt
);
60 static int vmci_transport_recv_connecting_client_invalid(
62 struct vmci_transport_packet
*pkt
);
63 static int vmci_transport_recv_connected(struct sock
*sk
,
64 struct vmci_transport_packet
*pkt
);
65 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
);
66 static u16
vmci_transport_new_proto_supported_versions(void);
67 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
, u16
*proto
,
70 struct vmci_transport_recv_pkt_info
{
71 struct work_struct work
;
73 struct vmci_transport_packet pkt
;
76 static LIST_HEAD(vmci_transport_cleanup_list
);
77 static DEFINE_SPINLOCK(vmci_transport_cleanup_lock
);
78 static DECLARE_WORK(vmci_transport_cleanup_work
, vmci_transport_cleanup
);
80 static struct vmci_handle vmci_transport_stream_handle
= { VMCI_INVALID_ID
,
82 static u32 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
84 static int PROTOCOL_OVERRIDE
= -1;
86 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN 128
87 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE 262144
88 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX 262144
90 /* The default peer timeout indicates how long we will wait for a peer response
91 * to a control message.
93 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
95 /* Helper function to convert from a VMCI error code to a VSock error code. */
97 static s32
vmci_transport_error_to_vsock_error(s32 vmci_error
)
101 switch (vmci_error
) {
102 case VMCI_ERROR_NO_MEM
:
105 case VMCI_ERROR_DUPLICATE_ENTRY
:
106 case VMCI_ERROR_ALREADY_EXISTS
:
109 case VMCI_ERROR_NO_ACCESS
:
112 case VMCI_ERROR_NO_RESOURCES
:
115 case VMCI_ERROR_INVALID_RESOURCE
:
118 case VMCI_ERROR_INVALID_ARGS
:
123 return err
> 0 ? -err
: err
;
126 static u32
vmci_transport_peer_rid(u32 peer_cid
)
128 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
129 return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID
;
131 return VMCI_TRANSPORT_PACKET_RID
;
135 vmci_transport_packet_init(struct vmci_transport_packet
*pkt
,
136 struct sockaddr_vm
*src
,
137 struct sockaddr_vm
*dst
,
141 struct vmci_transport_waiting_info
*wait
,
143 struct vmci_handle handle
)
145 /* We register the stream control handler as an any cid handle so we
146 * must always send from a source address of VMADDR_CID_ANY
148 pkt
->dg
.src
= vmci_make_handle(VMADDR_CID_ANY
,
149 VMCI_TRANSPORT_PACKET_RID
);
150 pkt
->dg
.dst
= vmci_make_handle(dst
->svm_cid
,
151 vmci_transport_peer_rid(dst
->svm_cid
));
152 pkt
->dg
.payload_size
= sizeof(*pkt
) - sizeof(pkt
->dg
);
153 pkt
->version
= VMCI_TRANSPORT_PACKET_VERSION
;
155 pkt
->src_port
= src
->svm_port
;
156 pkt
->dst_port
= dst
->svm_port
;
157 memset(&pkt
->proto
, 0, sizeof(pkt
->proto
));
158 memset(&pkt
->_reserved2
, 0, sizeof(pkt
->_reserved2
));
161 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
165 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST
:
166 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
170 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
171 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
172 pkt
->u
.handle
= handle
;
175 case VMCI_TRANSPORT_PACKET_TYPE_WROTE
:
176 case VMCI_TRANSPORT_PACKET_TYPE_READ
:
177 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
181 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
185 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
:
186 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
:
187 memcpy(&pkt
->u
.wait
, wait
, sizeof(pkt
->u
.wait
));
190 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
:
191 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
199 vmci_transport_packet_get_addresses(struct vmci_transport_packet
*pkt
,
200 struct sockaddr_vm
*local
,
201 struct sockaddr_vm
*remote
)
203 vsock_addr_init(local
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
204 vsock_addr_init(remote
, pkt
->dg
.src
.context
, pkt
->src_port
);
208 __vmci_transport_send_control_pkt(struct vmci_transport_packet
*pkt
,
209 struct sockaddr_vm
*src
,
210 struct sockaddr_vm
*dst
,
211 enum vmci_transport_packet_type type
,
214 struct vmci_transport_waiting_info
*wait
,
216 struct vmci_handle handle
,
221 vmci_transport_packet_init(pkt
, src
, dst
, type
, size
, mode
, wait
,
223 err
= vmci_datagram_send(&pkt
->dg
);
224 if (convert_error
&& (err
< 0))
225 return vmci_transport_error_to_vsock_error(err
);
231 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet
*pkt
,
232 enum vmci_transport_packet_type type
,
235 struct vmci_transport_waiting_info
*wait
,
236 struct vmci_handle handle
)
238 struct vmci_transport_packet reply
;
239 struct sockaddr_vm src
, dst
;
241 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
) {
244 vmci_transport_packet_get_addresses(pkt
, &src
, &dst
);
245 return __vmci_transport_send_control_pkt(&reply
, &src
, &dst
,
254 vmci_transport_send_control_pkt_bh(struct sockaddr_vm
*src
,
255 struct sockaddr_vm
*dst
,
256 enum vmci_transport_packet_type type
,
259 struct vmci_transport_waiting_info
*wait
,
260 struct vmci_handle handle
)
262 /* Note that it is safe to use a single packet across all CPUs since
263 * two tasklets of the same type are guaranteed to not ever run
264 * simultaneously. If that ever changes, or VMCI stops using tasklets,
265 * we can use per-cpu packets.
267 static struct vmci_transport_packet pkt
;
269 return __vmci_transport_send_control_pkt(&pkt
, src
, dst
, type
,
271 VSOCK_PROTO_INVALID
, handle
,
276 vmci_transport_alloc_send_control_pkt(struct sockaddr_vm
*src
,
277 struct sockaddr_vm
*dst
,
278 enum vmci_transport_packet_type type
,
281 struct vmci_transport_waiting_info
*wait
,
283 struct vmci_handle handle
)
285 struct vmci_transport_packet
*pkt
;
288 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
292 err
= __vmci_transport_send_control_pkt(pkt
, src
, dst
, type
, size
,
293 mode
, wait
, proto
, handle
,
301 vmci_transport_send_control_pkt(struct sock
*sk
,
302 enum vmci_transport_packet_type type
,
305 struct vmci_transport_waiting_info
*wait
,
307 struct vmci_handle handle
)
309 struct vsock_sock
*vsk
;
313 if (!vsock_addr_bound(&vsk
->local_addr
))
316 if (!vsock_addr_bound(&vsk
->remote_addr
))
319 return vmci_transport_alloc_send_control_pkt(&vsk
->local_addr
,
322 wait
, proto
, handle
);
325 static int vmci_transport_send_reset_bh(struct sockaddr_vm
*dst
,
326 struct sockaddr_vm
*src
,
327 struct vmci_transport_packet
*pkt
)
329 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
331 return vmci_transport_send_control_pkt_bh(
333 VMCI_TRANSPORT_PACKET_TYPE_RST
, 0,
334 0, NULL
, VMCI_INVALID_HANDLE
);
337 static int vmci_transport_send_reset(struct sock
*sk
,
338 struct vmci_transport_packet
*pkt
)
340 struct sockaddr_vm
*dst_ptr
;
341 struct sockaddr_vm dst
;
342 struct vsock_sock
*vsk
;
344 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
349 if (!vsock_addr_bound(&vsk
->local_addr
))
352 if (vsock_addr_bound(&vsk
->remote_addr
)) {
353 dst_ptr
= &vsk
->remote_addr
;
355 vsock_addr_init(&dst
, pkt
->dg
.src
.context
,
359 return vmci_transport_alloc_send_control_pkt(&vsk
->local_addr
, dst_ptr
,
360 VMCI_TRANSPORT_PACKET_TYPE_RST
,
361 0, 0, NULL
, VSOCK_PROTO_INVALID
,
362 VMCI_INVALID_HANDLE
);
365 static int vmci_transport_send_negotiate(struct sock
*sk
, size_t size
)
367 return vmci_transport_send_control_pkt(
369 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
,
372 VMCI_INVALID_HANDLE
);
375 static int vmci_transport_send_negotiate2(struct sock
*sk
, size_t size
,
378 return vmci_transport_send_control_pkt(
380 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
,
381 size
, 0, NULL
, version
,
382 VMCI_INVALID_HANDLE
);
385 static int vmci_transport_send_qp_offer(struct sock
*sk
,
386 struct vmci_handle handle
)
388 return vmci_transport_send_control_pkt(
389 sk
, VMCI_TRANSPORT_PACKET_TYPE_OFFER
, 0,
391 VSOCK_PROTO_INVALID
, handle
);
394 static int vmci_transport_send_attach(struct sock
*sk
,
395 struct vmci_handle handle
)
397 return vmci_transport_send_control_pkt(
398 sk
, VMCI_TRANSPORT_PACKET_TYPE_ATTACH
,
399 0, 0, NULL
, VSOCK_PROTO_INVALID
,
403 static int vmci_transport_reply_reset(struct vmci_transport_packet
*pkt
)
405 return vmci_transport_reply_control_pkt_fast(
407 VMCI_TRANSPORT_PACKET_TYPE_RST
,
409 VMCI_INVALID_HANDLE
);
412 static int vmci_transport_send_invalid_bh(struct sockaddr_vm
*dst
,
413 struct sockaddr_vm
*src
)
415 return vmci_transport_send_control_pkt_bh(
417 VMCI_TRANSPORT_PACKET_TYPE_INVALID
,
418 0, 0, NULL
, VMCI_INVALID_HANDLE
);
421 int vmci_transport_send_wrote_bh(struct sockaddr_vm
*dst
,
422 struct sockaddr_vm
*src
)
424 return vmci_transport_send_control_pkt_bh(
426 VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
427 0, NULL
, VMCI_INVALID_HANDLE
);
430 int vmci_transport_send_read_bh(struct sockaddr_vm
*dst
,
431 struct sockaddr_vm
*src
)
433 return vmci_transport_send_control_pkt_bh(
435 VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
436 0, NULL
, VMCI_INVALID_HANDLE
);
439 int vmci_transport_send_wrote(struct sock
*sk
)
441 return vmci_transport_send_control_pkt(
442 sk
, VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
443 0, NULL
, VSOCK_PROTO_INVALID
,
444 VMCI_INVALID_HANDLE
);
447 int vmci_transport_send_read(struct sock
*sk
)
449 return vmci_transport_send_control_pkt(
450 sk
, VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
451 0, NULL
, VSOCK_PROTO_INVALID
,
452 VMCI_INVALID_HANDLE
);
455 int vmci_transport_send_waiting_write(struct sock
*sk
,
456 struct vmci_transport_waiting_info
*wait
)
458 return vmci_transport_send_control_pkt(
459 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
,
460 0, 0, wait
, VSOCK_PROTO_INVALID
,
461 VMCI_INVALID_HANDLE
);
464 int vmci_transport_send_waiting_read(struct sock
*sk
,
465 struct vmci_transport_waiting_info
*wait
)
467 return vmci_transport_send_control_pkt(
468 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
,
469 0, 0, wait
, VSOCK_PROTO_INVALID
,
470 VMCI_INVALID_HANDLE
);
473 static int vmci_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
475 return vmci_transport_send_control_pkt(
477 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
,
480 VMCI_INVALID_HANDLE
);
483 static int vmci_transport_send_conn_request(struct sock
*sk
, size_t size
)
485 return vmci_transport_send_control_pkt(sk
,
486 VMCI_TRANSPORT_PACKET_TYPE_REQUEST
,
489 VMCI_INVALID_HANDLE
);
492 static int vmci_transport_send_conn_request2(struct sock
*sk
, size_t size
,
495 return vmci_transport_send_control_pkt(
496 sk
, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
,
497 size
, 0, NULL
, version
,
498 VMCI_INVALID_HANDLE
);
501 static struct sock
*vmci_transport_get_pending(
502 struct sock
*listener
,
503 struct vmci_transport_packet
*pkt
)
505 struct vsock_sock
*vlistener
;
506 struct vsock_sock
*vpending
;
507 struct sock
*pending
;
508 struct sockaddr_vm src
;
510 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
512 vlistener
= vsock_sk(listener
);
514 list_for_each_entry(vpending
, &vlistener
->pending_links
,
516 if (vsock_addr_equals_addr(&src
, &vpending
->remote_addr
) &&
517 pkt
->dst_port
== vpending
->local_addr
.svm_port
) {
518 pending
= sk_vsock(vpending
);
530 static void vmci_transport_release_pending(struct sock
*pending
)
535 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
536 * trusted sockets 2) sockets from applications running as the same user as the
537 * VM (this is only true for the host side and only when using hosted products)
540 static bool vmci_transport_is_trusted(struct vsock_sock
*vsock
, u32 peer_cid
)
542 return vsock
->trusted
||
543 vmci_is_context_owner(peer_cid
, vsock
->owner
->uid
);
546 /* We allow sending datagrams to and receiving datagrams from a restricted VM
547 * only if it is trusted as described in vmci_transport_is_trusted.
550 static bool vmci_transport_allow_dgram(struct vsock_sock
*vsock
, u32 peer_cid
)
552 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
555 if (vsock
->cached_peer
!= peer_cid
) {
556 vsock
->cached_peer
= peer_cid
;
557 if (!vmci_transport_is_trusted(vsock
, peer_cid
) &&
558 (vmci_context_get_priv_flags(peer_cid
) &
559 VMCI_PRIVILEGE_FLAG_RESTRICTED
)) {
560 vsock
->cached_peer_allow_dgram
= false;
562 vsock
->cached_peer_allow_dgram
= true;
566 return vsock
->cached_peer_allow_dgram
;
570 vmci_transport_queue_pair_alloc(struct vmci_qp
**qpair
,
571 struct vmci_handle
*handle
,
574 u32 peer
, u32 flags
, bool trusted
)
579 /* Try to allocate our queue pair as trusted. This will only
580 * work if vsock is running in the host.
583 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
,
586 VMCI_PRIVILEGE_FLAG_TRUSTED
);
587 if (err
!= VMCI_ERROR_NO_ACCESS
)
592 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
, consume_size
,
593 peer
, flags
, VMCI_NO_PRIVILEGE_FLAGS
);
596 pr_err("Could not attach to queue pair with %d\n",
598 err
= vmci_transport_error_to_vsock_error(err
);
605 vmci_transport_datagram_create_hnd(u32 resource_id
,
607 vmci_datagram_recv_cb recv_cb
,
609 struct vmci_handle
*out_handle
)
613 /* Try to allocate our datagram handler as trusted. This will only work
614 * if vsock is running in the host.
617 err
= vmci_datagram_create_handle_priv(resource_id
, flags
,
618 VMCI_PRIVILEGE_FLAG_TRUSTED
,
620 client_data
, out_handle
);
622 if (err
== VMCI_ERROR_NO_ACCESS
)
623 err
= vmci_datagram_create_handle(resource_id
, flags
,
624 recv_cb
, client_data
,
630 /* This is invoked as part of a tasklet that's scheduled when the VMCI
631 * interrupt fires. This is run in bottom-half context and if it ever needs to
632 * sleep it should defer that work to a work queue.
635 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
)
640 struct vsock_sock
*vsk
;
642 sk
= (struct sock
*)data
;
644 /* This handler is privileged when this module is running on the host.
645 * We will get datagrams from all endpoints (even VMs that are in a
646 * restricted context). If we get one from a restricted context then
647 * the destination socket must be trusted.
649 * NOTE: We access the socket struct without holding the lock here.
650 * This is ok because the field we are interested is never modified
651 * outside of the create and destruct socket functions.
654 if (!vmci_transport_allow_dgram(vsk
, dg
->src
.context
))
655 return VMCI_ERROR_NO_ACCESS
;
657 size
= VMCI_DG_SIZE(dg
);
659 /* Attach the packet to the socket's receive queue as an sk_buff. */
660 skb
= alloc_skb(size
, GFP_ATOMIC
);
662 return VMCI_ERROR_NO_MEM
;
664 /* sk_receive_skb() will do a sock_put(), so hold here. */
667 memcpy(skb
->data
, dg
, size
);
668 sk_receive_skb(sk
, skb
, 0);
673 static bool vmci_transport_stream_allow(u32 cid
, u32 port
)
675 static const u32 non_socket_contexts
[] = {
680 BUILD_BUG_ON(sizeof(cid
) != sizeof(*non_socket_contexts
));
682 for (i
= 0; i
< ARRAY_SIZE(non_socket_contexts
); i
++) {
683 if (cid
== non_socket_contexts
[i
])
690 /* This is invoked as part of a tasklet that's scheduled when the VMCI
691 * interrupt fires. This is run in bottom-half context but it defers most of
692 * its work to the packet handling work queue.
695 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
)
698 struct sockaddr_vm dst
;
699 struct sockaddr_vm src
;
700 struct vmci_transport_packet
*pkt
;
701 struct vsock_sock
*vsk
;
707 bh_process_pkt
= false;
709 /* Ignore incoming packets from contexts without sockets, or resources
710 * that aren't vsock implementations.
713 if (!vmci_transport_stream_allow(dg
->src
.context
, -1)
714 || vmci_transport_peer_rid(dg
->src
.context
) != dg
->src
.resource
)
715 return VMCI_ERROR_NO_ACCESS
;
717 if (VMCI_DG_SIZE(dg
) < sizeof(*pkt
))
718 /* Drop datagrams that do not contain full VSock packets. */
719 return VMCI_ERROR_INVALID_ARGS
;
721 pkt
= (struct vmci_transport_packet
*)dg
;
723 /* Find the socket that should handle this packet. First we look for a
724 * connected socket and if there is none we look for a socket bound to
725 * the destintation address.
727 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
728 vsock_addr_init(&dst
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
730 sk
= vsock_find_connected_socket(&src
, &dst
);
732 sk
= vsock_find_bound_socket(&dst
);
734 /* We could not find a socket for this specified
735 * address. If this packet is a RST, we just drop it.
736 * If it is another packet, we send a RST. Note that
737 * we do not send a RST reply to RSTs so that we do not
738 * continually send RSTs between two endpoints.
740 * Note that since this is a reply, dst is src and src
743 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
744 pr_err("unable to send reset\n");
746 err
= VMCI_ERROR_NOT_FOUND
;
751 /* If the received packet type is beyond all types known to this
752 * implementation, reply with an invalid message. Hopefully this will
753 * help when implementing backwards compatibility in the future.
755 if (pkt
->type
>= VMCI_TRANSPORT_PACKET_TYPE_MAX
) {
756 vmci_transport_send_invalid_bh(&dst
, &src
);
757 err
= VMCI_ERROR_INVALID_ARGS
;
761 /* This handler is privileged when this module is running on the host.
762 * We will get datagram connect requests from all endpoints (even VMs
763 * that are in a restricted context). If we get one from a restricted
764 * context then the destination socket must be trusted.
766 * NOTE: We access the socket struct without holding the lock here.
767 * This is ok because the field we are interested is never modified
768 * outside of the create and destruct socket functions.
771 if (!vmci_transport_allow_dgram(vsk
, pkt
->dg
.src
.context
)) {
772 err
= VMCI_ERROR_NO_ACCESS
;
776 /* We do most everything in a work queue, but let's fast path the
777 * notification of reads and writes to help data transfer performance.
778 * We can only do this if there is no process context code executing
779 * for this socket since that may change the state.
783 if (!sock_owned_by_user(sk
)) {
784 /* The local context ID may be out of date, update it. */
785 vsk
->local_addr
.svm_cid
= dst
.svm_cid
;
787 if (sk
->sk_state
== SS_CONNECTED
)
788 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
789 sk
, pkt
, true, &dst
, &src
,
795 if (!bh_process_pkt
) {
796 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
798 recv_pkt_info
= kmalloc(sizeof(*recv_pkt_info
), GFP_ATOMIC
);
799 if (!recv_pkt_info
) {
800 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
801 pr_err("unable to send reset\n");
803 err
= VMCI_ERROR_NO_MEM
;
807 recv_pkt_info
->sk
= sk
;
808 memcpy(&recv_pkt_info
->pkt
, pkt
, sizeof(recv_pkt_info
->pkt
));
809 INIT_WORK(&recv_pkt_info
->work
, vmci_transport_recv_pkt_work
);
811 schedule_work(&recv_pkt_info
->work
);
812 /* Clear sk so that the reference count incremented by one of
813 * the Find functions above is not decremented below. We need
814 * that reference count for the packet handler we've scheduled
827 static void vmci_transport_handle_detach(struct sock
*sk
)
829 struct vsock_sock
*vsk
;
832 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)) {
833 sock_set_flag(sk
, SOCK_DONE
);
835 /* On a detach the peer will not be sending or receiving
838 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
840 /* We should not be sending anymore since the peer won't be
841 * there to receive, but we can still receive if there is data
842 * left in our consume queue.
844 if (vsock_stream_has_data(vsk
) <= 0) {
845 if (sk
->sk_state
== SS_CONNECTING
) {
846 /* The peer may detach from a queue pair while
847 * we are still in the connecting state, i.e.,
848 * if the peer VM is killed after attaching to
849 * a queue pair, but before we complete the
850 * handshake. In that case, we treat the detach
851 * event like a reset.
854 sk
->sk_state
= SS_UNCONNECTED
;
855 sk
->sk_err
= ECONNRESET
;
856 sk
->sk_error_report(sk
);
859 sk
->sk_state
= SS_UNCONNECTED
;
861 sk
->sk_state_change(sk
);
865 static void vmci_transport_peer_detach_cb(u32 sub_id
,
866 const struct vmci_event_data
*e_data
,
869 struct vmci_transport
*trans
= client_data
;
870 const struct vmci_event_payload_qp
*e_payload
;
872 e_payload
= vmci_event_data_const_payload(e_data
);
874 /* XXX This is lame, we should provide a way to lookup sockets by
877 if (vmci_handle_is_invalid(e_payload
->handle
) ||
878 !vmci_handle_is_equal(trans
->qp_handle
, e_payload
->handle
))
881 /* We don't ask for delayed CBs when we subscribe to this event (we
882 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
883 * guarantees in that case about what context we might be running in,
884 * so it could be BH or process, blockable or non-blockable. So we
885 * need to account for all possible contexts here.
887 spin_lock_bh(&trans
->lock
);
891 /* Apart from here, trans->lock is only grabbed as part of sk destruct,
892 * where trans->sk isn't locked.
894 bh_lock_sock(trans
->sk
);
896 vmci_transport_handle_detach(trans
->sk
);
898 bh_unlock_sock(trans
->sk
);
900 spin_unlock_bh(&trans
->lock
);
903 static void vmci_transport_qp_resumed_cb(u32 sub_id
,
904 const struct vmci_event_data
*e_data
,
907 vsock_for_each_connected_socket(vmci_transport_handle_detach
);
910 static void vmci_transport_recv_pkt_work(struct work_struct
*work
)
912 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
913 struct vmci_transport_packet
*pkt
;
917 container_of(work
, struct vmci_transport_recv_pkt_info
, work
);
918 sk
= recv_pkt_info
->sk
;
919 pkt
= &recv_pkt_info
->pkt
;
923 /* The local context ID may be out of date. */
924 vsock_sk(sk
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
926 switch (sk
->sk_state
) {
927 case VSOCK_SS_LISTEN
:
928 vmci_transport_recv_listen(sk
, pkt
);
931 /* Processing of pending connections for servers goes through
932 * the listening socket, so see vmci_transport_recv_listen()
935 vmci_transport_recv_connecting_client(sk
, pkt
);
938 vmci_transport_recv_connected(sk
, pkt
);
941 /* Because this function does not run in the same context as
942 * vmci_transport_recv_stream_cb it is possible that the
943 * socket has closed. We need to let the other side know or it
944 * could be sitting in a connect and hang forever. Send a
945 * reset to prevent that.
947 vmci_transport_send_reset(sk
, pkt
);
952 kfree(recv_pkt_info
);
953 /* Release reference obtained in the stream callback when we fetched
954 * this socket out of the bound or connected list.
959 static int vmci_transport_recv_listen(struct sock
*sk
,
960 struct vmci_transport_packet
*pkt
)
962 struct sock
*pending
;
963 struct vsock_sock
*vpending
;
966 bool old_request
= false;
967 bool old_pkt_proto
= false;
971 /* Because we are in the listen state, we could be receiving a packet
972 * for ourself or any previous connection requests that we received.
973 * If it's the latter, we try to find a socket in our list of pending
974 * connections and, if we do, call the appropriate handler for the
975 * state that that socket is in. Otherwise we try to service the
976 * connection request.
978 pending
= vmci_transport_get_pending(sk
, pkt
);
982 /* The local context ID may be out of date. */
983 vsock_sk(pending
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
985 switch (pending
->sk_state
) {
987 err
= vmci_transport_recv_connecting_server(sk
,
992 vmci_transport_send_reset(pending
, pkt
);
997 vsock_remove_pending(sk
, pending
);
999 release_sock(pending
);
1000 vmci_transport_release_pending(pending
);
1005 /* The listen state only accepts connection requests. Reply with a
1006 * reset unless we received a reset.
1009 if (!(pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
||
1010 pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)) {
1011 vmci_transport_reply_reset(pkt
);
1015 if (pkt
->u
.size
== 0) {
1016 vmci_transport_reply_reset(pkt
);
1020 /* If this socket can't accommodate this connection request, we send a
1021 * reset. Otherwise we create and initialize a child socket and reply
1022 * with a connection negotiation.
1024 if (sk
->sk_ack_backlog
>= sk
->sk_max_ack_backlog
) {
1025 vmci_transport_reply_reset(pkt
);
1026 return -ECONNREFUSED
;
1029 pending
= __vsock_create(sock_net(sk
), NULL
, sk
, GFP_KERNEL
,
1032 vmci_transport_send_reset(sk
, pkt
);
1036 vpending
= vsock_sk(pending
);
1038 vsock_addr_init(&vpending
->local_addr
, pkt
->dg
.dst
.context
,
1040 vsock_addr_init(&vpending
->remote_addr
, pkt
->dg
.src
.context
,
1043 /* If the proposed size fits within our min/max, accept it. Otherwise
1044 * propose our own size.
1046 if (pkt
->u
.size
>= vmci_trans(vpending
)->queue_pair_min_size
&&
1047 pkt
->u
.size
<= vmci_trans(vpending
)->queue_pair_max_size
) {
1048 qp_size
= pkt
->u
.size
;
1050 qp_size
= vmci_trans(vpending
)->queue_pair_size
;
1053 /* Figure out if we are using old or new requests based on the
1054 * overrides pkt types sent by our peer.
1056 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1057 old_request
= old_pkt_proto
;
1059 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
)
1061 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)
1062 old_request
= false;
1067 /* Handle a REQUEST (or override) */
1068 u16 version
= VSOCK_PROTO_INVALID
;
1069 if (vmci_transport_proto_to_notify_struct(
1070 pending
, &version
, true))
1071 err
= vmci_transport_send_negotiate(pending
, qp_size
);
1076 /* Handle a REQUEST2 (or override) */
1077 int proto_int
= pkt
->proto
;
1079 u16 active_proto_version
= 0;
1081 /* The list of possible protocols is the intersection of all
1082 * protocols the client supports ... plus all the protocols we
1085 proto_int
&= vmci_transport_new_proto_supported_versions();
1087 /* We choose the highest possible protocol version and use that
1090 pos
= fls(proto_int
);
1092 active_proto_version
= (1 << (pos
- 1));
1093 if (vmci_transport_proto_to_notify_struct(
1094 pending
, &active_proto_version
, false))
1095 err
= vmci_transport_send_negotiate2(pending
,
1097 active_proto_version
);
1107 vmci_transport_send_reset(sk
, pkt
);
1109 err
= vmci_transport_error_to_vsock_error(err
);
1113 vsock_add_pending(sk
, pending
);
1114 sk
->sk_ack_backlog
++;
1116 pending
->sk_state
= SS_CONNECTING
;
1117 vmci_trans(vpending
)->produce_size
=
1118 vmci_trans(vpending
)->consume_size
= qp_size
;
1119 vmci_trans(vpending
)->queue_pair_size
= qp_size
;
1121 vmci_trans(vpending
)->notify_ops
->process_request(pending
);
1123 /* We might never receive another message for this socket and it's not
1124 * connected to any process, so we have to ensure it gets cleaned up
1125 * ourself. Our delayed work function will take care of that. Note
1126 * that we do not ever cancel this function since we have few
1127 * guarantees about its state when calling cancel_delayed_work().
1128 * Instead we hold a reference on the socket for that function and make
1129 * it capable of handling cases where it needs to do nothing but
1130 * release that reference.
1132 vpending
->listener
= sk
;
1135 schedule_delayed_work(&vpending
->pending_work
, HZ
);
1142 vmci_transport_recv_connecting_server(struct sock
*listener
,
1143 struct sock
*pending
,
1144 struct vmci_transport_packet
*pkt
)
1146 struct vsock_sock
*vpending
;
1147 struct vmci_handle handle
;
1148 struct vmci_qp
*qpair
;
1155 vpending
= vsock_sk(pending
);
1156 detach_sub_id
= VMCI_INVALID_ID
;
1158 switch (pkt
->type
) {
1159 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
1160 if (vmci_handle_is_invalid(pkt
->u
.handle
)) {
1161 vmci_transport_send_reset(pending
, pkt
);
1168 /* Close and cleanup the connection. */
1169 vmci_transport_send_reset(pending
, pkt
);
1171 err
= pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
? 0 : -EINVAL
;
1175 /* In order to complete the connection we need to attach to the offered
1176 * queue pair and send an attach notification. We also subscribe to the
1177 * detach event so we know when our peer goes away, and we do that
1178 * before attaching so we don't miss an event. If all this succeeds,
1179 * we update our state and wakeup anything waiting in accept() for a
1183 /* We don't care about attach since we ensure the other side has
1184 * attached by specifying the ATTACH_ONLY flag below.
1186 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1187 vmci_transport_peer_detach_cb
,
1188 vmci_trans(vpending
), &detach_sub_id
);
1189 if (err
< VMCI_SUCCESS
) {
1190 vmci_transport_send_reset(pending
, pkt
);
1191 err
= vmci_transport_error_to_vsock_error(err
);
1196 vmci_trans(vpending
)->detach_sub_id
= detach_sub_id
;
1198 /* Now attach to the queue pair the client created. */
1199 handle
= pkt
->u
.handle
;
1201 /* vpending->local_addr always has a context id so we do not need to
1202 * worry about VMADDR_CID_ANY in this case.
1205 vpending
->remote_addr
.svm_cid
== vpending
->local_addr
.svm_cid
;
1206 flags
= VMCI_QPFLAG_ATTACH_ONLY
;
1207 flags
|= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1209 err
= vmci_transport_queue_pair_alloc(
1212 vmci_trans(vpending
)->produce_size
,
1213 vmci_trans(vpending
)->consume_size
,
1214 pkt
->dg
.src
.context
,
1216 vmci_transport_is_trusted(
1218 vpending
->remote_addr
.svm_cid
));
1220 vmci_transport_send_reset(pending
, pkt
);
1225 vmci_trans(vpending
)->qp_handle
= handle
;
1226 vmci_trans(vpending
)->qpair
= qpair
;
1228 /* When we send the attach message, we must be ready to handle incoming
1229 * control messages on the newly connected socket. So we move the
1230 * pending socket to the connected state before sending the attach
1231 * message. Otherwise, an incoming packet triggered by the attach being
1232 * received by the peer may be processed concurrently with what happens
1233 * below after sending the attach message, and that incoming packet
1234 * will find the listening socket instead of the (currently) pending
1235 * socket. Note that enqueueing the socket increments the reference
1236 * count, so even if a reset comes before the connection is accepted,
1237 * the socket will be valid until it is removed from the queue.
1239 * If we fail sending the attach below, we remove the socket from the
1240 * connected list and move the socket to SS_UNCONNECTED before
1241 * releasing the lock, so a pending slow path processing of an incoming
1242 * packet will not see the socket in the connected state in that case.
1244 pending
->sk_state
= SS_CONNECTED
;
1246 vsock_insert_connected(vpending
);
1248 /* Notify our peer of our attach. */
1249 err
= vmci_transport_send_attach(pending
, handle
);
1251 vsock_remove_connected(vpending
);
1252 pr_err("Could not send attach\n");
1253 vmci_transport_send_reset(pending
, pkt
);
1254 err
= vmci_transport_error_to_vsock_error(err
);
1259 /* We have a connection. Move the now connected socket from the
1260 * listener's pending list to the accept queue so callers of accept()
1263 vsock_remove_pending(listener
, pending
);
1264 vsock_enqueue_accept(listener
, pending
);
1266 /* Callers of accept() will be be waiting on the listening socket, not
1267 * the pending socket.
1269 listener
->sk_data_ready(listener
);
1274 pending
->sk_err
= skerr
;
1275 pending
->sk_state
= SS_UNCONNECTED
;
1276 /* As long as we drop our reference, all necessary cleanup will handle
1277 * when the cleanup function drops its reference and our destruct
1278 * implementation is called. Note that since the listen handler will
1279 * remove pending from the pending list upon our failure, the cleanup
1280 * function won't drop the additional reference, which is why we do it
1289 vmci_transport_recv_connecting_client(struct sock
*sk
,
1290 struct vmci_transport_packet
*pkt
)
1292 struct vsock_sock
*vsk
;
1298 switch (pkt
->type
) {
1299 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
1300 if (vmci_handle_is_invalid(pkt
->u
.handle
) ||
1301 !vmci_handle_is_equal(pkt
->u
.handle
,
1302 vmci_trans(vsk
)->qp_handle
)) {
1308 /* Signify the socket is connected and wakeup the waiter in
1309 * connect(). Also place the socket in the connected table for
1310 * accounting (it can already be found since it's in the bound
1313 sk
->sk_state
= SS_CONNECTED
;
1314 sk
->sk_socket
->state
= SS_CONNECTED
;
1315 vsock_insert_connected(vsk
);
1316 sk
->sk_state_change(sk
);
1319 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
1320 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
1321 if (pkt
->u
.size
== 0
1322 || pkt
->dg
.src
.context
!= vsk
->remote_addr
.svm_cid
1323 || pkt
->src_port
!= vsk
->remote_addr
.svm_port
1324 || !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)
1325 || vmci_trans(vsk
)->qpair
1326 || vmci_trans(vsk
)->produce_size
!= 0
1327 || vmci_trans(vsk
)->consume_size
!= 0
1328 || vmci_trans(vsk
)->detach_sub_id
!= VMCI_INVALID_ID
) {
1335 err
= vmci_transport_recv_connecting_client_negotiate(sk
, pkt
);
1342 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
1343 err
= vmci_transport_recv_connecting_client_invalid(sk
, pkt
);
1350 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1351 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1352 * continue processing here after they sent an INVALID packet.
1353 * This meant that we got a RST after the INVALID. We ignore a
1354 * RST after an INVALID. The common code doesn't send the RST
1355 * ... so we can hang if an old version of the common code
1356 * fails between getting a REQUEST and sending an OFFER back.
1357 * Not much we can do about it... except hope that it doesn't
1360 if (vsk
->ignore_connecting_rst
) {
1361 vsk
->ignore_connecting_rst
= false;
1370 /* Close and cleanup the connection. */
1379 vmci_transport_send_reset(sk
, pkt
);
1381 sk
->sk_state
= SS_UNCONNECTED
;
1383 sk
->sk_error_report(sk
);
1387 static int vmci_transport_recv_connecting_client_negotiate(
1389 struct vmci_transport_packet
*pkt
)
1392 struct vsock_sock
*vsk
;
1393 struct vmci_handle handle
;
1394 struct vmci_qp
*qpair
;
1398 bool old_proto
= true;
1403 handle
= VMCI_INVALID_HANDLE
;
1404 detach_sub_id
= VMCI_INVALID_ID
;
1406 /* If we have gotten here then we should be past the point where old
1407 * linux vsock could have sent the bogus rst.
1409 vsk
->sent_request
= false;
1410 vsk
->ignore_connecting_rst
= false;
1412 /* Verify that we're OK with the proposed queue pair size */
1413 if (pkt
->u
.size
< vmci_trans(vsk
)->queue_pair_min_size
||
1414 pkt
->u
.size
> vmci_trans(vsk
)->queue_pair_max_size
) {
1419 /* At this point we know the CID the peer is using to talk to us. */
1421 if (vsk
->local_addr
.svm_cid
== VMADDR_CID_ANY
)
1422 vsk
->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
1424 /* Setup the notify ops to be the highest supported version that both
1425 * the server and the client support.
1428 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1429 old_proto
= old_pkt_proto
;
1431 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
)
1433 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
)
1439 version
= VSOCK_PROTO_INVALID
;
1441 version
= pkt
->proto
;
1443 if (!vmci_transport_proto_to_notify_struct(sk
, &version
, old_proto
)) {
1448 /* Subscribe to detach events first.
1450 * XXX We attach once for each queue pair created for now so it is easy
1451 * to find the socket (it's provided), but later we should only
1452 * subscribe once and add a way to lookup sockets by queue pair handle.
1454 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1455 vmci_transport_peer_detach_cb
,
1456 vmci_trans(vsk
), &detach_sub_id
);
1457 if (err
< VMCI_SUCCESS
) {
1458 err
= vmci_transport_error_to_vsock_error(err
);
1462 /* Make VMCI select the handle for us. */
1463 handle
= VMCI_INVALID_HANDLE
;
1464 is_local
= vsk
->remote_addr
.svm_cid
== vsk
->local_addr
.svm_cid
;
1465 flags
= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1467 err
= vmci_transport_queue_pair_alloc(&qpair
,
1471 vsk
->remote_addr
.svm_cid
,
1473 vmci_transport_is_trusted(
1476 remote_addr
.svm_cid
));
1480 err
= vmci_transport_send_qp_offer(sk
, handle
);
1482 err
= vmci_transport_error_to_vsock_error(err
);
1486 vmci_trans(vsk
)->qp_handle
= handle
;
1487 vmci_trans(vsk
)->qpair
= qpair
;
1489 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
=
1492 vmci_trans(vsk
)->detach_sub_id
= detach_sub_id
;
1494 vmci_trans(vsk
)->notify_ops
->process_negotiate(sk
);
1499 if (detach_sub_id
!= VMCI_INVALID_ID
)
1500 vmci_event_unsubscribe(detach_sub_id
);
1502 if (!vmci_handle_is_invalid(handle
))
1503 vmci_qpair_detach(&qpair
);
1509 vmci_transport_recv_connecting_client_invalid(struct sock
*sk
,
1510 struct vmci_transport_packet
*pkt
)
1513 struct vsock_sock
*vsk
= vsock_sk(sk
);
1515 if (vsk
->sent_request
) {
1516 vsk
->sent_request
= false;
1517 vsk
->ignore_connecting_rst
= true;
1519 err
= vmci_transport_send_conn_request(
1520 sk
, vmci_trans(vsk
)->queue_pair_size
);
1522 err
= vmci_transport_error_to_vsock_error(err
);
1531 static int vmci_transport_recv_connected(struct sock
*sk
,
1532 struct vmci_transport_packet
*pkt
)
1534 struct vsock_sock
*vsk
;
1535 bool pkt_processed
= false;
1537 /* In cases where we are closing the connection, it's sufficient to
1538 * mark the state change (and maybe error) and wake up any waiting
1539 * threads. Since this is a connected socket, it's owned by a user
1540 * process and will be cleaned up when the failure is passed back on
1541 * the current or next system call. Our system call implementations
1542 * must therefore check for error and state changes on entry and when
1545 switch (pkt
->type
) {
1546 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
1550 vsk
->peer_shutdown
|= pkt
->u
.mode
;
1551 sk
->sk_state_change(sk
);
1555 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1557 /* It is possible that we sent our peer a message (e.g a
1558 * WAITING_READ) right before we got notified that the peer had
1559 * detached. If that happens then we can get a RST pkt back
1560 * from our peer even though there is data available for us to
1561 * read. In that case, don't shutdown the socket completely but
1562 * instead allow the local client to finish reading data off
1563 * the queuepair. Always treat a RST pkt in connected mode like
1566 sock_set_flag(sk
, SOCK_DONE
);
1567 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
1568 if (vsock_stream_has_data(vsk
) <= 0)
1569 sk
->sk_state
= SS_DISCONNECTING
;
1571 sk
->sk_state_change(sk
);
1576 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
1577 sk
, pkt
, false, NULL
, NULL
,
1588 static int vmci_transport_socket_init(struct vsock_sock
*vsk
,
1589 struct vsock_sock
*psk
)
1591 vsk
->trans
= kmalloc(sizeof(struct vmci_transport
), GFP_KERNEL
);
1595 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1596 vmci_trans(vsk
)->qp_handle
= VMCI_INVALID_HANDLE
;
1597 vmci_trans(vsk
)->qpair
= NULL
;
1598 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
= 0;
1599 vmci_trans(vsk
)->detach_sub_id
= VMCI_INVALID_ID
;
1600 vmci_trans(vsk
)->notify_ops
= NULL
;
1601 INIT_LIST_HEAD(&vmci_trans(vsk
)->elem
);
1602 vmci_trans(vsk
)->sk
= &vsk
->sk
;
1603 spin_lock_init(&vmci_trans(vsk
)->lock
);
1605 vmci_trans(vsk
)->queue_pair_size
=
1606 vmci_trans(psk
)->queue_pair_size
;
1607 vmci_trans(vsk
)->queue_pair_min_size
=
1608 vmci_trans(psk
)->queue_pair_min_size
;
1609 vmci_trans(vsk
)->queue_pair_max_size
=
1610 vmci_trans(psk
)->queue_pair_max_size
;
1612 vmci_trans(vsk
)->queue_pair_size
=
1613 VMCI_TRANSPORT_DEFAULT_QP_SIZE
;
1614 vmci_trans(vsk
)->queue_pair_min_size
=
1615 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN
;
1616 vmci_trans(vsk
)->queue_pair_max_size
=
1617 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX
;
1623 static void vmci_transport_free_resources(struct list_head
*transport_list
)
1625 while (!list_empty(transport_list
)) {
1626 struct vmci_transport
*transport
=
1627 list_first_entry(transport_list
, struct vmci_transport
,
1629 list_del(&transport
->elem
);
1631 if (transport
->detach_sub_id
!= VMCI_INVALID_ID
) {
1632 vmci_event_unsubscribe(transport
->detach_sub_id
);
1633 transport
->detach_sub_id
= VMCI_INVALID_ID
;
1636 if (!vmci_handle_is_invalid(transport
->qp_handle
)) {
1637 vmci_qpair_detach(&transport
->qpair
);
1638 transport
->qp_handle
= VMCI_INVALID_HANDLE
;
1639 transport
->produce_size
= 0;
1640 transport
->consume_size
= 0;
1647 static void vmci_transport_cleanup(struct work_struct
*work
)
1651 spin_lock_bh(&vmci_transport_cleanup_lock
);
1652 list_replace_init(&vmci_transport_cleanup_list
, &pending
);
1653 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1654 vmci_transport_free_resources(&pending
);
1657 static void vmci_transport_destruct(struct vsock_sock
*vsk
)
1659 /* Ensure that the detach callback doesn't use the sk/vsk
1660 * we are about to destruct.
1662 spin_lock_bh(&vmci_trans(vsk
)->lock
);
1663 vmci_trans(vsk
)->sk
= NULL
;
1664 spin_unlock_bh(&vmci_trans(vsk
)->lock
);
1666 if (vmci_trans(vsk
)->notify_ops
)
1667 vmci_trans(vsk
)->notify_ops
->socket_destruct(vsk
);
1669 spin_lock_bh(&vmci_transport_cleanup_lock
);
1670 list_add(&vmci_trans(vsk
)->elem
, &vmci_transport_cleanup_list
);
1671 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1672 schedule_work(&vmci_transport_cleanup_work
);
1677 static void vmci_transport_release(struct vsock_sock
*vsk
)
1679 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->dg_handle
)) {
1680 vmci_datagram_destroy_handle(vmci_trans(vsk
)->dg_handle
);
1681 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1685 static int vmci_transport_dgram_bind(struct vsock_sock
*vsk
,
1686 struct sockaddr_vm
*addr
)
1692 /* VMCI will select a resource ID for us if we provide
1695 port
= addr
->svm_port
== VMADDR_PORT_ANY
?
1696 VMCI_INVALID_ID
: addr
->svm_port
;
1698 if (port
<= LAST_RESERVED_PORT
&& !capable(CAP_NET_BIND_SERVICE
))
1701 flags
= addr
->svm_cid
== VMADDR_CID_ANY
?
1702 VMCI_FLAG_ANYCID_DG_HND
: 0;
1704 err
= vmci_transport_datagram_create_hnd(port
, flags
,
1705 vmci_transport_recv_dgram_cb
,
1707 &vmci_trans(vsk
)->dg_handle
);
1708 if (err
< VMCI_SUCCESS
)
1709 return vmci_transport_error_to_vsock_error(err
);
1710 vsock_addr_init(&vsk
->local_addr
, addr
->svm_cid
,
1711 vmci_trans(vsk
)->dg_handle
.resource
);
1716 static int vmci_transport_dgram_enqueue(
1717 struct vsock_sock
*vsk
,
1718 struct sockaddr_vm
*remote_addr
,
1723 struct vmci_datagram
*dg
;
1725 if (len
> VMCI_MAX_DG_PAYLOAD_SIZE
)
1728 if (!vmci_transport_allow_dgram(vsk
, remote_addr
->svm_cid
))
1731 /* Allocate a buffer for the user's message and our packet header. */
1732 dg
= kmalloc(len
+ sizeof(*dg
), GFP_KERNEL
);
1736 memcpy_from_msg(VMCI_DG_PAYLOAD(dg
), msg
, len
);
1738 dg
->dst
= vmci_make_handle(remote_addr
->svm_cid
,
1739 remote_addr
->svm_port
);
1740 dg
->src
= vmci_make_handle(vsk
->local_addr
.svm_cid
,
1741 vsk
->local_addr
.svm_port
);
1742 dg
->payload_size
= len
;
1744 err
= vmci_datagram_send(dg
);
1747 return vmci_transport_error_to_vsock_error(err
);
1749 return err
- sizeof(*dg
);
1752 static int vmci_transport_dgram_dequeue(struct vsock_sock
*vsk
,
1753 struct msghdr
*msg
, size_t len
,
1758 struct vmci_datagram
*dg
;
1760 struct sk_buff
*skb
;
1762 noblock
= flags
& MSG_DONTWAIT
;
1764 if (flags
& MSG_OOB
|| flags
& MSG_ERRQUEUE
)
1767 /* Retrieve the head sk_buff from the socket's receive queue. */
1769 skb
= skb_recv_datagram(&vsk
->sk
, flags
, noblock
, &err
);
1776 dg
= (struct vmci_datagram
*)skb
->data
;
1778 /* err is 0, meaning we read zero bytes. */
1781 payload_len
= dg
->payload_size
;
1782 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1783 if (payload_len
!= skb
->len
- sizeof(*dg
)) {
1788 if (payload_len
> len
) {
1790 msg
->msg_flags
|= MSG_TRUNC
;
1793 /* Place the datagram payload in the user's iovec. */
1794 err
= skb_copy_datagram_msg(skb
, sizeof(*dg
), msg
, payload_len
);
1798 if (msg
->msg_name
) {
1799 /* Provide the address of the sender. */
1800 DECLARE_SOCKADDR(struct sockaddr_vm
*, vm_addr
, msg
->msg_name
);
1801 vsock_addr_init(vm_addr
, dg
->src
.context
, dg
->src
.resource
);
1802 msg
->msg_namelen
= sizeof(*vm_addr
);
1807 skb_free_datagram(&vsk
->sk
, skb
);
1811 static bool vmci_transport_dgram_allow(u32 cid
, u32 port
)
1813 if (cid
== VMADDR_CID_HYPERVISOR
) {
1814 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1815 * state and are allowed.
1817 return port
== VMCI_UNITY_PBRPC_REGISTER
;
1823 static int vmci_transport_connect(struct vsock_sock
*vsk
)
1826 bool old_pkt_proto
= false;
1827 struct sock
*sk
= &vsk
->sk
;
1829 if (vmci_transport_old_proto_override(&old_pkt_proto
) &&
1831 err
= vmci_transport_send_conn_request(
1832 sk
, vmci_trans(vsk
)->queue_pair_size
);
1834 sk
->sk_state
= SS_UNCONNECTED
;
1838 int supported_proto_versions
=
1839 vmci_transport_new_proto_supported_versions();
1840 err
= vmci_transport_send_conn_request2(
1841 sk
, vmci_trans(vsk
)->queue_pair_size
,
1842 supported_proto_versions
);
1844 sk
->sk_state
= SS_UNCONNECTED
;
1848 vsk
->sent_request
= true;
1854 static ssize_t
vmci_transport_stream_dequeue(
1855 struct vsock_sock
*vsk
,
1860 if (flags
& MSG_PEEK
)
1861 return vmci_qpair_peekv(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1863 return vmci_qpair_dequev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1866 static ssize_t
vmci_transport_stream_enqueue(
1867 struct vsock_sock
*vsk
,
1871 return vmci_qpair_enquev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1874 static s64
vmci_transport_stream_has_data(struct vsock_sock
*vsk
)
1876 return vmci_qpair_consume_buf_ready(vmci_trans(vsk
)->qpair
);
1879 static s64
vmci_transport_stream_has_space(struct vsock_sock
*vsk
)
1881 return vmci_qpair_produce_free_space(vmci_trans(vsk
)->qpair
);
1884 static u64
vmci_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
1886 return vmci_trans(vsk
)->consume_size
;
1889 static bool vmci_transport_stream_is_active(struct vsock_sock
*vsk
)
1891 return !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
);
1894 static u64
vmci_transport_get_buffer_size(struct vsock_sock
*vsk
)
1896 return vmci_trans(vsk
)->queue_pair_size
;
1899 static u64
vmci_transport_get_min_buffer_size(struct vsock_sock
*vsk
)
1901 return vmci_trans(vsk
)->queue_pair_min_size
;
1904 static u64
vmci_transport_get_max_buffer_size(struct vsock_sock
*vsk
)
1906 return vmci_trans(vsk
)->queue_pair_max_size
;
1909 static void vmci_transport_set_buffer_size(struct vsock_sock
*vsk
, u64 val
)
1911 if (val
< vmci_trans(vsk
)->queue_pair_min_size
)
1912 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1913 if (val
> vmci_trans(vsk
)->queue_pair_max_size
)
1914 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1915 vmci_trans(vsk
)->queue_pair_size
= val
;
1918 static void vmci_transport_set_min_buffer_size(struct vsock_sock
*vsk
,
1921 if (val
> vmci_trans(vsk
)->queue_pair_size
)
1922 vmci_trans(vsk
)->queue_pair_size
= val
;
1923 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1926 static void vmci_transport_set_max_buffer_size(struct vsock_sock
*vsk
,
1929 if (val
< vmci_trans(vsk
)->queue_pair_size
)
1930 vmci_trans(vsk
)->queue_pair_size
= val
;
1931 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1934 static int vmci_transport_notify_poll_in(
1935 struct vsock_sock
*vsk
,
1937 bool *data_ready_now
)
1939 return vmci_trans(vsk
)->notify_ops
->poll_in(
1940 &vsk
->sk
, target
, data_ready_now
);
1943 static int vmci_transport_notify_poll_out(
1944 struct vsock_sock
*vsk
,
1946 bool *space_available_now
)
1948 return vmci_trans(vsk
)->notify_ops
->poll_out(
1949 &vsk
->sk
, target
, space_available_now
);
1952 static int vmci_transport_notify_recv_init(
1953 struct vsock_sock
*vsk
,
1955 struct vsock_transport_recv_notify_data
*data
)
1957 return vmci_trans(vsk
)->notify_ops
->recv_init(
1959 (struct vmci_transport_recv_notify_data
*)data
);
1962 static int vmci_transport_notify_recv_pre_block(
1963 struct vsock_sock
*vsk
,
1965 struct vsock_transport_recv_notify_data
*data
)
1967 return vmci_trans(vsk
)->notify_ops
->recv_pre_block(
1969 (struct vmci_transport_recv_notify_data
*)data
);
1972 static int vmci_transport_notify_recv_pre_dequeue(
1973 struct vsock_sock
*vsk
,
1975 struct vsock_transport_recv_notify_data
*data
)
1977 return vmci_trans(vsk
)->notify_ops
->recv_pre_dequeue(
1979 (struct vmci_transport_recv_notify_data
*)data
);
1982 static int vmci_transport_notify_recv_post_dequeue(
1983 struct vsock_sock
*vsk
,
1987 struct vsock_transport_recv_notify_data
*data
)
1989 return vmci_trans(vsk
)->notify_ops
->recv_post_dequeue(
1990 &vsk
->sk
, target
, copied
, data_read
,
1991 (struct vmci_transport_recv_notify_data
*)data
);
1994 static int vmci_transport_notify_send_init(
1995 struct vsock_sock
*vsk
,
1996 struct vsock_transport_send_notify_data
*data
)
1998 return vmci_trans(vsk
)->notify_ops
->send_init(
2000 (struct vmci_transport_send_notify_data
*)data
);
2003 static int vmci_transport_notify_send_pre_block(
2004 struct vsock_sock
*vsk
,
2005 struct vsock_transport_send_notify_data
*data
)
2007 return vmci_trans(vsk
)->notify_ops
->send_pre_block(
2009 (struct vmci_transport_send_notify_data
*)data
);
2012 static int vmci_transport_notify_send_pre_enqueue(
2013 struct vsock_sock
*vsk
,
2014 struct vsock_transport_send_notify_data
*data
)
2016 return vmci_trans(vsk
)->notify_ops
->send_pre_enqueue(
2018 (struct vmci_transport_send_notify_data
*)data
);
2021 static int vmci_transport_notify_send_post_enqueue(
2022 struct vsock_sock
*vsk
,
2024 struct vsock_transport_send_notify_data
*data
)
2026 return vmci_trans(vsk
)->notify_ops
->send_post_enqueue(
2028 (struct vmci_transport_send_notify_data
*)data
);
2031 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
)
2033 if (PROTOCOL_OVERRIDE
!= -1) {
2034 if (PROTOCOL_OVERRIDE
== 0)
2035 *old_pkt_proto
= true;
2037 *old_pkt_proto
= false;
2039 pr_info("Proto override in use\n");
2046 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
,
2050 struct vsock_sock
*vsk
= vsock_sk(sk
);
2052 if (old_pkt_proto
) {
2053 if (*proto
!= VSOCK_PROTO_INVALID
) {
2054 pr_err("Can't set both an old and new protocol\n");
2057 vmci_trans(vsk
)->notify_ops
= &vmci_transport_notify_pkt_ops
;
2062 case VSOCK_PROTO_PKT_ON_NOTIFY
:
2063 vmci_trans(vsk
)->notify_ops
=
2064 &vmci_transport_notify_pkt_q_state_ops
;
2067 pr_err("Unknown notify protocol version\n");
2072 vmci_trans(vsk
)->notify_ops
->socket_init(sk
);
2076 static u16
vmci_transport_new_proto_supported_versions(void)
2078 if (PROTOCOL_OVERRIDE
!= -1)
2079 return PROTOCOL_OVERRIDE
;
2081 return VSOCK_PROTO_ALL_SUPPORTED
;
2084 static u32
vmci_transport_get_local_cid(void)
2086 return vmci_get_context_id();
2089 static struct vsock_transport vmci_transport
= {
2090 .init
= vmci_transport_socket_init
,
2091 .destruct
= vmci_transport_destruct
,
2092 .release
= vmci_transport_release
,
2093 .connect
= vmci_transport_connect
,
2094 .dgram_bind
= vmci_transport_dgram_bind
,
2095 .dgram_dequeue
= vmci_transport_dgram_dequeue
,
2096 .dgram_enqueue
= vmci_transport_dgram_enqueue
,
2097 .dgram_allow
= vmci_transport_dgram_allow
,
2098 .stream_dequeue
= vmci_transport_stream_dequeue
,
2099 .stream_enqueue
= vmci_transport_stream_enqueue
,
2100 .stream_has_data
= vmci_transport_stream_has_data
,
2101 .stream_has_space
= vmci_transport_stream_has_space
,
2102 .stream_rcvhiwat
= vmci_transport_stream_rcvhiwat
,
2103 .stream_is_active
= vmci_transport_stream_is_active
,
2104 .stream_allow
= vmci_transport_stream_allow
,
2105 .notify_poll_in
= vmci_transport_notify_poll_in
,
2106 .notify_poll_out
= vmci_transport_notify_poll_out
,
2107 .notify_recv_init
= vmci_transport_notify_recv_init
,
2108 .notify_recv_pre_block
= vmci_transport_notify_recv_pre_block
,
2109 .notify_recv_pre_dequeue
= vmci_transport_notify_recv_pre_dequeue
,
2110 .notify_recv_post_dequeue
= vmci_transport_notify_recv_post_dequeue
,
2111 .notify_send_init
= vmci_transport_notify_send_init
,
2112 .notify_send_pre_block
= vmci_transport_notify_send_pre_block
,
2113 .notify_send_pre_enqueue
= vmci_transport_notify_send_pre_enqueue
,
2114 .notify_send_post_enqueue
= vmci_transport_notify_send_post_enqueue
,
2115 .shutdown
= vmci_transport_shutdown
,
2116 .set_buffer_size
= vmci_transport_set_buffer_size
,
2117 .set_min_buffer_size
= vmci_transport_set_min_buffer_size
,
2118 .set_max_buffer_size
= vmci_transport_set_max_buffer_size
,
2119 .get_buffer_size
= vmci_transport_get_buffer_size
,
2120 .get_min_buffer_size
= vmci_transport_get_min_buffer_size
,
2121 .get_max_buffer_size
= vmci_transport_get_max_buffer_size
,
2122 .get_local_cid
= vmci_transport_get_local_cid
,
2125 static int __init
vmci_transport_init(void)
2129 /* Create the datagram handle that we will use to send and receive all
2130 * VSocket control messages for this context.
2132 err
= vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID
,
2133 VMCI_FLAG_ANYCID_DG_HND
,
2134 vmci_transport_recv_stream_cb
,
2136 &vmci_transport_stream_handle
);
2137 if (err
< VMCI_SUCCESS
) {
2138 pr_err("Unable to create datagram handle. (%d)\n", err
);
2139 return vmci_transport_error_to_vsock_error(err
);
2142 err
= vmci_event_subscribe(VMCI_EVENT_QP_RESUMED
,
2143 vmci_transport_qp_resumed_cb
,
2144 NULL
, &vmci_transport_qp_resumed_sub_id
);
2145 if (err
< VMCI_SUCCESS
) {
2146 pr_err("Unable to subscribe to resumed event. (%d)\n", err
);
2147 err
= vmci_transport_error_to_vsock_error(err
);
2148 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2149 goto err_destroy_stream_handle
;
2152 err
= vsock_core_init(&vmci_transport
);
2154 goto err_unsubscribe
;
2159 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2160 err_destroy_stream_handle
:
2161 vmci_datagram_destroy_handle(vmci_transport_stream_handle
);
2164 module_init(vmci_transport_init
);
2166 static void __exit
vmci_transport_exit(void)
2168 cancel_work_sync(&vmci_transport_cleanup_work
);
2169 vmci_transport_free_resources(&vmci_transport_cleanup_list
);
2171 if (!vmci_handle_is_invalid(vmci_transport_stream_handle
)) {
2172 if (vmci_datagram_destroy_handle(
2173 vmci_transport_stream_handle
) != VMCI_SUCCESS
)
2174 pr_err("Couldn't destroy datagram handle\n");
2175 vmci_transport_stream_handle
= VMCI_INVALID_HANDLE
;
2178 if (vmci_transport_qp_resumed_sub_id
!= VMCI_INVALID_ID
) {
2179 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2180 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2185 module_exit(vmci_transport_exit
);
2187 MODULE_AUTHOR("VMware, Inc.");
2188 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2189 MODULE_VERSION("1.0.3.0-k");
2190 MODULE_LICENSE("GPL v2");
2191 MODULE_ALIAS("vmware_vsock");
2192 MODULE_ALIAS_NETPROTO(PF_VSOCK
);