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/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>
36 #include <net/af_vsock.h>
38 #include "vmci_transport_notify.h"
40 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
);
41 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
);
42 static void vmci_transport_peer_detach_cb(u32 sub_id
,
43 const struct vmci_event_data
*ed
,
45 static void vmci_transport_recv_pkt_work(struct work_struct
*work
);
46 static void vmci_transport_cleanup(struct work_struct
*work
);
47 static int vmci_transport_recv_listen(struct sock
*sk
,
48 struct vmci_transport_packet
*pkt
);
49 static int vmci_transport_recv_connecting_server(
52 struct vmci_transport_packet
*pkt
);
53 static int vmci_transport_recv_connecting_client(
55 struct vmci_transport_packet
*pkt
);
56 static int vmci_transport_recv_connecting_client_negotiate(
58 struct vmci_transport_packet
*pkt
);
59 static int vmci_transport_recv_connecting_client_invalid(
61 struct vmci_transport_packet
*pkt
);
62 static int vmci_transport_recv_connected(struct sock
*sk
,
63 struct vmci_transport_packet
*pkt
);
64 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
);
65 static u16
vmci_transport_new_proto_supported_versions(void);
66 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
, u16
*proto
,
69 struct vmci_transport_recv_pkt_info
{
70 struct work_struct work
;
72 struct vmci_transport_packet pkt
;
75 static LIST_HEAD(vmci_transport_cleanup_list
);
76 static DEFINE_SPINLOCK(vmci_transport_cleanup_lock
);
77 static DECLARE_WORK(vmci_transport_cleanup_work
, vmci_transport_cleanup
);
79 static struct vmci_handle vmci_transport_stream_handle
= { VMCI_INVALID_ID
,
81 static u32 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
83 static int PROTOCOL_OVERRIDE
= -1;
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
89 /* The default peer timeout indicates how long we will wait for a peer response
90 * to a control message.
92 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
94 /* Helper function to convert from a VMCI error code to a VSock error code. */
96 static s32
vmci_transport_error_to_vsock_error(s32 vmci_error
)
99 case VMCI_ERROR_NO_MEM
:
101 case VMCI_ERROR_DUPLICATE_ENTRY
:
102 case VMCI_ERROR_ALREADY_EXISTS
:
104 case VMCI_ERROR_NO_ACCESS
:
106 case VMCI_ERROR_NO_RESOURCES
:
108 case VMCI_ERROR_INVALID_RESOURCE
:
109 return -EHOSTUNREACH
;
110 case VMCI_ERROR_INVALID_ARGS
:
117 static u32
vmci_transport_peer_rid(u32 peer_cid
)
119 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
120 return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID
;
122 return VMCI_TRANSPORT_PACKET_RID
;
126 vmci_transport_packet_init(struct vmci_transport_packet
*pkt
,
127 struct sockaddr_vm
*src
,
128 struct sockaddr_vm
*dst
,
132 struct vmci_transport_waiting_info
*wait
,
134 struct vmci_handle handle
)
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
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
,
142 vmci_transport_peer_rid(dst
->svm_cid
));
143 pkt
->dg
.payload_size
= sizeof(*pkt
) - sizeof(pkt
->dg
);
144 pkt
->version
= VMCI_TRANSPORT_PACKET_VERSION
;
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
));
152 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
156 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST
:
157 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
161 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
162 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
163 pkt
->u
.handle
= handle
;
166 case VMCI_TRANSPORT_PACKET_TYPE_WROTE
:
167 case VMCI_TRANSPORT_PACKET_TYPE_READ
:
168 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
172 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
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
));
181 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
:
182 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
190 vmci_transport_packet_get_addresses(struct vmci_transport_packet
*pkt
,
191 struct sockaddr_vm
*local
,
192 struct sockaddr_vm
*remote
)
194 vsock_addr_init(local
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
195 vsock_addr_init(remote
, pkt
->dg
.src
.context
, pkt
->src_port
);
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
,
205 struct vmci_transport_waiting_info
*wait
,
207 struct vmci_handle handle
,
212 vmci_transport_packet_init(pkt
, src
, dst
, type
, size
, mode
, wait
,
214 err
= vmci_datagram_send(&pkt
->dg
);
215 if (convert_error
&& (err
< 0))
216 return vmci_transport_error_to_vsock_error(err
);
222 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet
*pkt
,
223 enum vmci_transport_packet_type type
,
226 struct vmci_transport_waiting_info
*wait
,
227 struct vmci_handle handle
)
229 struct vmci_transport_packet reply
;
230 struct sockaddr_vm src
, dst
;
232 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
) {
235 vmci_transport_packet_get_addresses(pkt
, &src
, &dst
);
236 return __vmci_transport_send_control_pkt(&reply
, &src
, &dst
,
245 vmci_transport_send_control_pkt_bh(struct sockaddr_vm
*src
,
246 struct sockaddr_vm
*dst
,
247 enum vmci_transport_packet_type type
,
250 struct vmci_transport_waiting_info
*wait
,
251 struct vmci_handle handle
)
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.
258 static struct vmci_transport_packet pkt
;
260 return __vmci_transport_send_control_pkt(&pkt
, src
, dst
, type
,
262 VSOCK_PROTO_INVALID
, handle
,
267 vmci_transport_alloc_send_control_pkt(struct sockaddr_vm
*src
,
268 struct sockaddr_vm
*dst
,
269 enum vmci_transport_packet_type type
,
272 struct vmci_transport_waiting_info
*wait
,
274 struct vmci_handle handle
)
276 struct vmci_transport_packet
*pkt
;
279 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
283 err
= __vmci_transport_send_control_pkt(pkt
, src
, dst
, type
, size
,
284 mode
, wait
, proto
, handle
,
292 vmci_transport_send_control_pkt(struct sock
*sk
,
293 enum vmci_transport_packet_type type
,
296 struct vmci_transport_waiting_info
*wait
,
298 struct vmci_handle handle
)
300 struct vsock_sock
*vsk
;
304 if (!vsock_addr_bound(&vsk
->local_addr
))
307 if (!vsock_addr_bound(&vsk
->remote_addr
))
310 return vmci_transport_alloc_send_control_pkt(&vsk
->local_addr
,
313 wait
, proto
, handle
);
316 static int vmci_transport_send_reset_bh(struct sockaddr_vm
*dst
,
317 struct sockaddr_vm
*src
,
318 struct vmci_transport_packet
*pkt
)
320 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
322 return vmci_transport_send_control_pkt_bh(
324 VMCI_TRANSPORT_PACKET_TYPE_RST
, 0,
325 0, NULL
, VMCI_INVALID_HANDLE
);
328 static int vmci_transport_send_reset(struct sock
*sk
,
329 struct vmci_transport_packet
*pkt
)
331 struct sockaddr_vm
*dst_ptr
;
332 struct sockaddr_vm dst
;
333 struct vsock_sock
*vsk
;
335 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
340 if (!vsock_addr_bound(&vsk
->local_addr
))
343 if (vsock_addr_bound(&vsk
->remote_addr
)) {
344 dst_ptr
= &vsk
->remote_addr
;
346 vsock_addr_init(&dst
, pkt
->dg
.src
.context
,
350 return vmci_transport_alloc_send_control_pkt(&vsk
->local_addr
, dst_ptr
,
351 VMCI_TRANSPORT_PACKET_TYPE_RST
,
352 0, 0, NULL
, VSOCK_PROTO_INVALID
,
353 VMCI_INVALID_HANDLE
);
356 static int vmci_transport_send_negotiate(struct sock
*sk
, size_t size
)
358 return vmci_transport_send_control_pkt(
360 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
,
363 VMCI_INVALID_HANDLE
);
366 static int vmci_transport_send_negotiate2(struct sock
*sk
, size_t size
,
369 return vmci_transport_send_control_pkt(
371 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
,
372 size
, 0, NULL
, version
,
373 VMCI_INVALID_HANDLE
);
376 static int vmci_transport_send_qp_offer(struct sock
*sk
,
377 struct vmci_handle handle
)
379 return vmci_transport_send_control_pkt(
380 sk
, VMCI_TRANSPORT_PACKET_TYPE_OFFER
, 0,
382 VSOCK_PROTO_INVALID
, handle
);
385 static int vmci_transport_send_attach(struct sock
*sk
,
386 struct vmci_handle handle
)
388 return vmci_transport_send_control_pkt(
389 sk
, VMCI_TRANSPORT_PACKET_TYPE_ATTACH
,
390 0, 0, NULL
, VSOCK_PROTO_INVALID
,
394 static int vmci_transport_reply_reset(struct vmci_transport_packet
*pkt
)
396 return vmci_transport_reply_control_pkt_fast(
398 VMCI_TRANSPORT_PACKET_TYPE_RST
,
400 VMCI_INVALID_HANDLE
);
403 static int vmci_transport_send_invalid_bh(struct sockaddr_vm
*dst
,
404 struct sockaddr_vm
*src
)
406 return vmci_transport_send_control_pkt_bh(
408 VMCI_TRANSPORT_PACKET_TYPE_INVALID
,
409 0, 0, NULL
, VMCI_INVALID_HANDLE
);
412 int vmci_transport_send_wrote_bh(struct sockaddr_vm
*dst
,
413 struct sockaddr_vm
*src
)
415 return vmci_transport_send_control_pkt_bh(
417 VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
418 0, NULL
, VMCI_INVALID_HANDLE
);
421 int vmci_transport_send_read_bh(struct sockaddr_vm
*dst
,
422 struct sockaddr_vm
*src
)
424 return vmci_transport_send_control_pkt_bh(
426 VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
427 0, NULL
, VMCI_INVALID_HANDLE
);
430 int vmci_transport_send_wrote(struct sock
*sk
)
432 return vmci_transport_send_control_pkt(
433 sk
, VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
434 0, NULL
, VSOCK_PROTO_INVALID
,
435 VMCI_INVALID_HANDLE
);
438 int vmci_transport_send_read(struct sock
*sk
)
440 return vmci_transport_send_control_pkt(
441 sk
, VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
442 0, NULL
, VSOCK_PROTO_INVALID
,
443 VMCI_INVALID_HANDLE
);
446 int vmci_transport_send_waiting_write(struct sock
*sk
,
447 struct vmci_transport_waiting_info
*wait
)
449 return vmci_transport_send_control_pkt(
450 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
,
451 0, 0, wait
, VSOCK_PROTO_INVALID
,
452 VMCI_INVALID_HANDLE
);
455 int vmci_transport_send_waiting_read(struct sock
*sk
,
456 struct vmci_transport_waiting_info
*wait
)
458 return vmci_transport_send_control_pkt(
459 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
,
460 0, 0, wait
, VSOCK_PROTO_INVALID
,
461 VMCI_INVALID_HANDLE
);
464 static int vmci_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
466 return vmci_transport_send_control_pkt(
468 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
,
471 VMCI_INVALID_HANDLE
);
474 static int vmci_transport_send_conn_request(struct sock
*sk
, size_t size
)
476 return vmci_transport_send_control_pkt(sk
,
477 VMCI_TRANSPORT_PACKET_TYPE_REQUEST
,
480 VMCI_INVALID_HANDLE
);
483 static int vmci_transport_send_conn_request2(struct sock
*sk
, size_t size
,
486 return vmci_transport_send_control_pkt(
487 sk
, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
,
488 size
, 0, NULL
, version
,
489 VMCI_INVALID_HANDLE
);
492 static struct sock
*vmci_transport_get_pending(
493 struct sock
*listener
,
494 struct vmci_transport_packet
*pkt
)
496 struct vsock_sock
*vlistener
;
497 struct vsock_sock
*vpending
;
498 struct sock
*pending
;
499 struct sockaddr_vm src
;
501 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
503 vlistener
= vsock_sk(listener
);
505 list_for_each_entry(vpending
, &vlistener
->pending_links
,
507 if (vsock_addr_equals_addr(&src
, &vpending
->remote_addr
) &&
508 pkt
->dst_port
== vpending
->local_addr
.svm_port
) {
509 pending
= sk_vsock(vpending
);
521 static void vmci_transport_release_pending(struct sock
*pending
)
526 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
527 * trusted sockets 2) sockets from applications running as the same user as the
528 * VM (this is only true for the host side and only when using hosted products)
531 static bool vmci_transport_is_trusted(struct vsock_sock
*vsock
, u32 peer_cid
)
533 return vsock
->trusted
||
534 vmci_is_context_owner(peer_cid
, vsock
->owner
->uid
);
537 /* We allow sending datagrams to and receiving datagrams from a restricted VM
538 * only if it is trusted as described in vmci_transport_is_trusted.
541 static bool vmci_transport_allow_dgram(struct vsock_sock
*vsock
, u32 peer_cid
)
543 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
546 if (vsock
->cached_peer
!= peer_cid
) {
547 vsock
->cached_peer
= peer_cid
;
548 if (!vmci_transport_is_trusted(vsock
, peer_cid
) &&
549 (vmci_context_get_priv_flags(peer_cid
) &
550 VMCI_PRIVILEGE_FLAG_RESTRICTED
)) {
551 vsock
->cached_peer_allow_dgram
= false;
553 vsock
->cached_peer_allow_dgram
= true;
557 return vsock
->cached_peer_allow_dgram
;
561 vmci_transport_queue_pair_alloc(struct vmci_qp
**qpair
,
562 struct vmci_handle
*handle
,
565 u32 peer
, u32 flags
, bool trusted
)
570 /* Try to allocate our queue pair as trusted. This will only
571 * work if vsock is running in the host.
574 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
,
577 VMCI_PRIVILEGE_FLAG_TRUSTED
);
578 if (err
!= VMCI_ERROR_NO_ACCESS
)
583 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
, consume_size
,
584 peer
, flags
, VMCI_NO_PRIVILEGE_FLAGS
);
587 pr_err("Could not attach to queue pair with %d\n",
589 err
= vmci_transport_error_to_vsock_error(err
);
596 vmci_transport_datagram_create_hnd(u32 resource_id
,
598 vmci_datagram_recv_cb recv_cb
,
600 struct vmci_handle
*out_handle
)
604 /* Try to allocate our datagram handler as trusted. This will only work
605 * if vsock is running in the host.
608 err
= vmci_datagram_create_handle_priv(resource_id
, flags
,
609 VMCI_PRIVILEGE_FLAG_TRUSTED
,
611 client_data
, out_handle
);
613 if (err
== VMCI_ERROR_NO_ACCESS
)
614 err
= vmci_datagram_create_handle(resource_id
, flags
,
615 recv_cb
, client_data
,
621 /* This is invoked as part of a tasklet that's scheduled when the VMCI
622 * interrupt fires. This is run in bottom-half context and if it ever needs to
623 * sleep it should defer that work to a work queue.
626 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
)
631 struct vsock_sock
*vsk
;
633 sk
= (struct sock
*)data
;
635 /* This handler is privileged when this module is running on the host.
636 * We will get datagrams from all endpoints (even VMs that are in a
637 * restricted context). If we get one from a restricted context then
638 * the destination socket must be trusted.
640 * NOTE: We access the socket struct without holding the lock here.
641 * This is ok because the field we are interested is never modified
642 * outside of the create and destruct socket functions.
645 if (!vmci_transport_allow_dgram(vsk
, dg
->src
.context
))
646 return VMCI_ERROR_NO_ACCESS
;
648 size
= VMCI_DG_SIZE(dg
);
650 /* Attach the packet to the socket's receive queue as an sk_buff. */
651 skb
= alloc_skb(size
, GFP_ATOMIC
);
653 return VMCI_ERROR_NO_MEM
;
655 /* sk_receive_skb() will do a sock_put(), so hold here. */
658 memcpy(skb
->data
, dg
, size
);
659 sk_receive_skb(sk
, skb
, 0);
664 static bool vmci_transport_stream_allow(u32 cid
, u32 port
)
666 static const u32 non_socket_contexts
[] = {
671 BUILD_BUG_ON(sizeof(cid
) != sizeof(*non_socket_contexts
));
673 for (i
= 0; i
< ARRAY_SIZE(non_socket_contexts
); i
++) {
674 if (cid
== non_socket_contexts
[i
])
681 /* This is invoked as part of a tasklet that's scheduled when the VMCI
682 * interrupt fires. This is run in bottom-half context but it defers most of
683 * its work to the packet handling work queue.
686 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
)
689 struct sockaddr_vm dst
;
690 struct sockaddr_vm src
;
691 struct vmci_transport_packet
*pkt
;
692 struct vsock_sock
*vsk
;
698 bh_process_pkt
= false;
700 /* Ignore incoming packets from contexts without sockets, or resources
701 * that aren't vsock implementations.
704 if (!vmci_transport_stream_allow(dg
->src
.context
, -1)
705 || vmci_transport_peer_rid(dg
->src
.context
) != dg
->src
.resource
)
706 return VMCI_ERROR_NO_ACCESS
;
708 if (VMCI_DG_SIZE(dg
) < sizeof(*pkt
))
709 /* Drop datagrams that do not contain full VSock packets. */
710 return VMCI_ERROR_INVALID_ARGS
;
712 pkt
= (struct vmci_transport_packet
*)dg
;
714 /* Find the socket that should handle this packet. First we look for a
715 * connected socket and if there is none we look for a socket bound to
716 * the destintation address.
718 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
719 vsock_addr_init(&dst
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
721 sk
= vsock_find_connected_socket(&src
, &dst
);
723 sk
= vsock_find_bound_socket(&dst
);
725 /* We could not find a socket for this specified
726 * address. If this packet is a RST, we just drop it.
727 * If it is another packet, we send a RST. Note that
728 * we do not send a RST reply to RSTs so that we do not
729 * continually send RSTs between two endpoints.
731 * Note that since this is a reply, dst is src and src
734 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
735 pr_err("unable to send reset\n");
737 err
= VMCI_ERROR_NOT_FOUND
;
742 /* If the received packet type is beyond all types known to this
743 * implementation, reply with an invalid message. Hopefully this will
744 * help when implementing backwards compatibility in the future.
746 if (pkt
->type
>= VMCI_TRANSPORT_PACKET_TYPE_MAX
) {
747 vmci_transport_send_invalid_bh(&dst
, &src
);
748 err
= VMCI_ERROR_INVALID_ARGS
;
752 /* This handler is privileged when this module is running on the host.
753 * We will get datagram connect requests from all endpoints (even VMs
754 * that are in a restricted context). If we get one from a restricted
755 * context then the destination socket must be trusted.
757 * NOTE: We access the socket struct without holding the lock here.
758 * This is ok because the field we are interested is never modified
759 * outside of the create and destruct socket functions.
762 if (!vmci_transport_allow_dgram(vsk
, pkt
->dg
.src
.context
)) {
763 err
= VMCI_ERROR_NO_ACCESS
;
767 /* We do most everything in a work queue, but let's fast path the
768 * notification of reads and writes to help data transfer performance.
769 * We can only do this if there is no process context code executing
770 * for this socket since that may change the state.
774 if (!sock_owned_by_user(sk
)) {
775 /* The local context ID may be out of date, update it. */
776 vsk
->local_addr
.svm_cid
= dst
.svm_cid
;
778 if (sk
->sk_state
== TCP_ESTABLISHED
)
779 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
780 sk
, pkt
, true, &dst
, &src
,
786 if (!bh_process_pkt
) {
787 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
789 recv_pkt_info
= kmalloc(sizeof(*recv_pkt_info
), GFP_ATOMIC
);
790 if (!recv_pkt_info
) {
791 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
792 pr_err("unable to send reset\n");
794 err
= VMCI_ERROR_NO_MEM
;
798 recv_pkt_info
->sk
= sk
;
799 memcpy(&recv_pkt_info
->pkt
, pkt
, sizeof(recv_pkt_info
->pkt
));
800 INIT_WORK(&recv_pkt_info
->work
, vmci_transport_recv_pkt_work
);
802 schedule_work(&recv_pkt_info
->work
);
803 /* Clear sk so that the reference count incremented by one of
804 * the Find functions above is not decremented below. We need
805 * that reference count for the packet handler we've scheduled
818 static void vmci_transport_handle_detach(struct sock
*sk
)
820 struct vsock_sock
*vsk
;
823 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)) {
824 sock_set_flag(sk
, SOCK_DONE
);
826 /* On a detach the peer will not be sending or receiving
829 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
831 /* We should not be sending anymore since the peer won't be
832 * there to receive, but we can still receive if there is data
833 * left in our consume queue. If the local endpoint is a host,
834 * we can't call vsock_stream_has_data, since that may block,
835 * but a host endpoint can't read data once the VM has
836 * detached, so there is no available data in that case.
838 if (vsk
->local_addr
.svm_cid
== VMADDR_CID_HOST
||
839 vsock_stream_has_data(vsk
) <= 0) {
840 if (sk
->sk_state
== TCP_SYN_SENT
) {
841 /* The peer may detach from a queue pair while
842 * we are still in the connecting state, i.e.,
843 * if the peer VM is killed after attaching to
844 * a queue pair, but before we complete the
845 * handshake. In that case, we treat the detach
846 * event like a reset.
849 sk
->sk_state
= TCP_CLOSE
;
850 sk
->sk_err
= ECONNRESET
;
851 sk
->sk_error_report(sk
);
854 sk
->sk_state
= TCP_CLOSE
;
856 sk
->sk_state_change(sk
);
860 static void vmci_transport_peer_detach_cb(u32 sub_id
,
861 const struct vmci_event_data
*e_data
,
864 struct vmci_transport
*trans
= client_data
;
865 const struct vmci_event_payload_qp
*e_payload
;
867 e_payload
= vmci_event_data_const_payload(e_data
);
869 /* XXX This is lame, we should provide a way to lookup sockets by
872 if (vmci_handle_is_invalid(e_payload
->handle
) ||
873 !vmci_handle_is_equal(trans
->qp_handle
, e_payload
->handle
))
876 /* We don't ask for delayed CBs when we subscribe to this event (we
877 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
878 * guarantees in that case about what context we might be running in,
879 * so it could be BH or process, blockable or non-blockable. So we
880 * need to account for all possible contexts here.
882 spin_lock_bh(&trans
->lock
);
886 /* Apart from here, trans->lock is only grabbed as part of sk destruct,
887 * where trans->sk isn't locked.
889 bh_lock_sock(trans
->sk
);
891 vmci_transport_handle_detach(trans
->sk
);
893 bh_unlock_sock(trans
->sk
);
895 spin_unlock_bh(&trans
->lock
);
898 static void vmci_transport_qp_resumed_cb(u32 sub_id
,
899 const struct vmci_event_data
*e_data
,
902 vsock_for_each_connected_socket(vmci_transport_handle_detach
);
905 static void vmci_transport_recv_pkt_work(struct work_struct
*work
)
907 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
908 struct vmci_transport_packet
*pkt
;
912 container_of(work
, struct vmci_transport_recv_pkt_info
, work
);
913 sk
= recv_pkt_info
->sk
;
914 pkt
= &recv_pkt_info
->pkt
;
918 /* The local context ID may be out of date. */
919 vsock_sk(sk
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
921 switch (sk
->sk_state
) {
923 vmci_transport_recv_listen(sk
, pkt
);
926 /* Processing of pending connections for servers goes through
927 * the listening socket, so see vmci_transport_recv_listen()
930 vmci_transport_recv_connecting_client(sk
, pkt
);
932 case TCP_ESTABLISHED
:
933 vmci_transport_recv_connected(sk
, pkt
);
936 /* Because this function does not run in the same context as
937 * vmci_transport_recv_stream_cb it is possible that the
938 * socket has closed. We need to let the other side know or it
939 * could be sitting in a connect and hang forever. Send a
940 * reset to prevent that.
942 vmci_transport_send_reset(sk
, pkt
);
947 kfree(recv_pkt_info
);
948 /* Release reference obtained in the stream callback when we fetched
949 * this socket out of the bound or connected list.
954 static int vmci_transport_recv_listen(struct sock
*sk
,
955 struct vmci_transport_packet
*pkt
)
957 struct sock
*pending
;
958 struct vsock_sock
*vpending
;
961 bool old_request
= false;
962 bool old_pkt_proto
= false;
966 /* Because we are in the listen state, we could be receiving a packet
967 * for ourself or any previous connection requests that we received.
968 * If it's the latter, we try to find a socket in our list of pending
969 * connections and, if we do, call the appropriate handler for the
970 * state that that socket is in. Otherwise we try to service the
971 * connection request.
973 pending
= vmci_transport_get_pending(sk
, pkt
);
977 /* The local context ID may be out of date. */
978 vsock_sk(pending
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
980 switch (pending
->sk_state
) {
982 err
= vmci_transport_recv_connecting_server(sk
,
987 vmci_transport_send_reset(pending
, pkt
);
992 vsock_remove_pending(sk
, pending
);
994 release_sock(pending
);
995 vmci_transport_release_pending(pending
);
1000 /* The listen state only accepts connection requests. Reply with a
1001 * reset unless we received a reset.
1004 if (!(pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
||
1005 pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)) {
1006 vmci_transport_reply_reset(pkt
);
1010 if (pkt
->u
.size
== 0) {
1011 vmci_transport_reply_reset(pkt
);
1015 /* If this socket can't accommodate this connection request, we send a
1016 * reset. Otherwise we create and initialize a child socket and reply
1017 * with a connection negotiation.
1019 if (sk
->sk_ack_backlog
>= sk
->sk_max_ack_backlog
) {
1020 vmci_transport_reply_reset(pkt
);
1021 return -ECONNREFUSED
;
1024 pending
= __vsock_create(sock_net(sk
), NULL
, sk
, GFP_KERNEL
,
1027 vmci_transport_send_reset(sk
, pkt
);
1031 vpending
= vsock_sk(pending
);
1033 vsock_addr_init(&vpending
->local_addr
, pkt
->dg
.dst
.context
,
1035 vsock_addr_init(&vpending
->remote_addr
, pkt
->dg
.src
.context
,
1038 /* If the proposed size fits within our min/max, accept it. Otherwise
1039 * propose our own size.
1041 if (pkt
->u
.size
>= vmci_trans(vpending
)->queue_pair_min_size
&&
1042 pkt
->u
.size
<= vmci_trans(vpending
)->queue_pair_max_size
) {
1043 qp_size
= pkt
->u
.size
;
1045 qp_size
= vmci_trans(vpending
)->queue_pair_size
;
1048 /* Figure out if we are using old or new requests based on the
1049 * overrides pkt types sent by our peer.
1051 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1052 old_request
= old_pkt_proto
;
1054 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
)
1056 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)
1057 old_request
= false;
1062 /* Handle a REQUEST (or override) */
1063 u16 version
= VSOCK_PROTO_INVALID
;
1064 if (vmci_transport_proto_to_notify_struct(
1065 pending
, &version
, true))
1066 err
= vmci_transport_send_negotiate(pending
, qp_size
);
1071 /* Handle a REQUEST2 (or override) */
1072 int proto_int
= pkt
->proto
;
1074 u16 active_proto_version
= 0;
1076 /* The list of possible protocols is the intersection of all
1077 * protocols the client supports ... plus all the protocols we
1080 proto_int
&= vmci_transport_new_proto_supported_versions();
1082 /* We choose the highest possible protocol version and use that
1085 pos
= fls(proto_int
);
1087 active_proto_version
= (1 << (pos
- 1));
1088 if (vmci_transport_proto_to_notify_struct(
1089 pending
, &active_proto_version
, false))
1090 err
= vmci_transport_send_negotiate2(pending
,
1092 active_proto_version
);
1102 vmci_transport_send_reset(sk
, pkt
);
1104 err
= vmci_transport_error_to_vsock_error(err
);
1108 vsock_add_pending(sk
, pending
);
1109 sk
->sk_ack_backlog
++;
1111 pending
->sk_state
= TCP_SYN_SENT
;
1112 vmci_trans(vpending
)->produce_size
=
1113 vmci_trans(vpending
)->consume_size
= qp_size
;
1114 vmci_trans(vpending
)->queue_pair_size
= qp_size
;
1116 vmci_trans(vpending
)->notify_ops
->process_request(pending
);
1118 /* We might never receive another message for this socket and it's not
1119 * connected to any process, so we have to ensure it gets cleaned up
1120 * ourself. Our delayed work function will take care of that. Note
1121 * that we do not ever cancel this function since we have few
1122 * guarantees about its state when calling cancel_delayed_work().
1123 * Instead we hold a reference on the socket for that function and make
1124 * it capable of handling cases where it needs to do nothing but
1125 * release that reference.
1127 vpending
->listener
= sk
;
1130 schedule_delayed_work(&vpending
->pending_work
, HZ
);
1137 vmci_transport_recv_connecting_server(struct sock
*listener
,
1138 struct sock
*pending
,
1139 struct vmci_transport_packet
*pkt
)
1141 struct vsock_sock
*vpending
;
1142 struct vmci_handle handle
;
1143 struct vmci_qp
*qpair
;
1150 vpending
= vsock_sk(pending
);
1151 detach_sub_id
= VMCI_INVALID_ID
;
1153 switch (pkt
->type
) {
1154 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
1155 if (vmci_handle_is_invalid(pkt
->u
.handle
)) {
1156 vmci_transport_send_reset(pending
, pkt
);
1163 /* Close and cleanup the connection. */
1164 vmci_transport_send_reset(pending
, pkt
);
1166 err
= pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
? 0 : -EINVAL
;
1170 /* In order to complete the connection we need to attach to the offered
1171 * queue pair and send an attach notification. We also subscribe to the
1172 * detach event so we know when our peer goes away, and we do that
1173 * before attaching so we don't miss an event. If all this succeeds,
1174 * we update our state and wakeup anything waiting in accept() for a
1178 /* We don't care about attach since we ensure the other side has
1179 * attached by specifying the ATTACH_ONLY flag below.
1181 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1182 vmci_transport_peer_detach_cb
,
1183 vmci_trans(vpending
), &detach_sub_id
);
1184 if (err
< VMCI_SUCCESS
) {
1185 vmci_transport_send_reset(pending
, pkt
);
1186 err
= vmci_transport_error_to_vsock_error(err
);
1191 vmci_trans(vpending
)->detach_sub_id
= detach_sub_id
;
1193 /* Now attach to the queue pair the client created. */
1194 handle
= pkt
->u
.handle
;
1196 /* vpending->local_addr always has a context id so we do not need to
1197 * worry about VMADDR_CID_ANY in this case.
1200 vpending
->remote_addr
.svm_cid
== vpending
->local_addr
.svm_cid
;
1201 flags
= VMCI_QPFLAG_ATTACH_ONLY
;
1202 flags
|= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1204 err
= vmci_transport_queue_pair_alloc(
1207 vmci_trans(vpending
)->produce_size
,
1208 vmci_trans(vpending
)->consume_size
,
1209 pkt
->dg
.src
.context
,
1211 vmci_transport_is_trusted(
1213 vpending
->remote_addr
.svm_cid
));
1215 vmci_transport_send_reset(pending
, pkt
);
1220 vmci_trans(vpending
)->qp_handle
= handle
;
1221 vmci_trans(vpending
)->qpair
= qpair
;
1223 /* When we send the attach message, we must be ready to handle incoming
1224 * control messages on the newly connected socket. So we move the
1225 * pending socket to the connected state before sending the attach
1226 * message. Otherwise, an incoming packet triggered by the attach being
1227 * received by the peer may be processed concurrently with what happens
1228 * below after sending the attach message, and that incoming packet
1229 * will find the listening socket instead of the (currently) pending
1230 * socket. Note that enqueueing the socket increments the reference
1231 * count, so even if a reset comes before the connection is accepted,
1232 * the socket will be valid until it is removed from the queue.
1234 * If we fail sending the attach below, we remove the socket from the
1235 * connected list and move the socket to TCP_CLOSE before
1236 * releasing the lock, so a pending slow path processing of an incoming
1237 * packet will not see the socket in the connected state in that case.
1239 pending
->sk_state
= TCP_ESTABLISHED
;
1241 vsock_insert_connected(vpending
);
1243 /* Notify our peer of our attach. */
1244 err
= vmci_transport_send_attach(pending
, handle
);
1246 vsock_remove_connected(vpending
);
1247 pr_err("Could not send attach\n");
1248 vmci_transport_send_reset(pending
, pkt
);
1249 err
= vmci_transport_error_to_vsock_error(err
);
1254 /* We have a connection. Move the now connected socket from the
1255 * listener's pending list to the accept queue so callers of accept()
1258 vsock_remove_pending(listener
, pending
);
1259 vsock_enqueue_accept(listener
, pending
);
1261 /* Callers of accept() will be be waiting on the listening socket, not
1262 * the pending socket.
1264 listener
->sk_data_ready(listener
);
1269 pending
->sk_err
= skerr
;
1270 pending
->sk_state
= TCP_CLOSE
;
1271 /* As long as we drop our reference, all necessary cleanup will handle
1272 * when the cleanup function drops its reference and our destruct
1273 * implementation is called. Note that since the listen handler will
1274 * remove pending from the pending list upon our failure, the cleanup
1275 * function won't drop the additional reference, which is why we do it
1284 vmci_transport_recv_connecting_client(struct sock
*sk
,
1285 struct vmci_transport_packet
*pkt
)
1287 struct vsock_sock
*vsk
;
1293 switch (pkt
->type
) {
1294 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
1295 if (vmci_handle_is_invalid(pkt
->u
.handle
) ||
1296 !vmci_handle_is_equal(pkt
->u
.handle
,
1297 vmci_trans(vsk
)->qp_handle
)) {
1303 /* Signify the socket is connected and wakeup the waiter in
1304 * connect(). Also place the socket in the connected table for
1305 * accounting (it can already be found since it's in the bound
1308 sk
->sk_state
= TCP_ESTABLISHED
;
1309 sk
->sk_socket
->state
= SS_CONNECTED
;
1310 vsock_insert_connected(vsk
);
1311 sk
->sk_state_change(sk
);
1314 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
1315 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
1316 if (pkt
->u
.size
== 0
1317 || pkt
->dg
.src
.context
!= vsk
->remote_addr
.svm_cid
1318 || pkt
->src_port
!= vsk
->remote_addr
.svm_port
1319 || !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)
1320 || vmci_trans(vsk
)->qpair
1321 || vmci_trans(vsk
)->produce_size
!= 0
1322 || vmci_trans(vsk
)->consume_size
!= 0
1323 || vmci_trans(vsk
)->detach_sub_id
!= VMCI_INVALID_ID
) {
1330 err
= vmci_transport_recv_connecting_client_negotiate(sk
, pkt
);
1337 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
1338 err
= vmci_transport_recv_connecting_client_invalid(sk
, pkt
);
1345 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1346 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1347 * continue processing here after they sent an INVALID packet.
1348 * This meant that we got a RST after the INVALID. We ignore a
1349 * RST after an INVALID. The common code doesn't send the RST
1350 * ... so we can hang if an old version of the common code
1351 * fails between getting a REQUEST and sending an OFFER back.
1352 * Not much we can do about it... except hope that it doesn't
1355 if (vsk
->ignore_connecting_rst
) {
1356 vsk
->ignore_connecting_rst
= false;
1365 /* Close and cleanup the connection. */
1374 vmci_transport_send_reset(sk
, pkt
);
1376 sk
->sk_state
= TCP_CLOSE
;
1378 sk
->sk_error_report(sk
);
1382 static int vmci_transport_recv_connecting_client_negotiate(
1384 struct vmci_transport_packet
*pkt
)
1387 struct vsock_sock
*vsk
;
1388 struct vmci_handle handle
;
1389 struct vmci_qp
*qpair
;
1393 bool old_proto
= true;
1398 handle
= VMCI_INVALID_HANDLE
;
1399 detach_sub_id
= VMCI_INVALID_ID
;
1401 /* If we have gotten here then we should be past the point where old
1402 * linux vsock could have sent the bogus rst.
1404 vsk
->sent_request
= false;
1405 vsk
->ignore_connecting_rst
= false;
1407 /* Verify that we're OK with the proposed queue pair size */
1408 if (pkt
->u
.size
< vmci_trans(vsk
)->queue_pair_min_size
||
1409 pkt
->u
.size
> vmci_trans(vsk
)->queue_pair_max_size
) {
1414 /* At this point we know the CID the peer is using to talk to us. */
1416 if (vsk
->local_addr
.svm_cid
== VMADDR_CID_ANY
)
1417 vsk
->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
1419 /* Setup the notify ops to be the highest supported version that both
1420 * the server and the client support.
1423 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1424 old_proto
= old_pkt_proto
;
1426 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
)
1428 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
)
1434 version
= VSOCK_PROTO_INVALID
;
1436 version
= pkt
->proto
;
1438 if (!vmci_transport_proto_to_notify_struct(sk
, &version
, old_proto
)) {
1443 /* Subscribe to detach events first.
1445 * XXX We attach once for each queue pair created for now so it is easy
1446 * to find the socket (it's provided), but later we should only
1447 * subscribe once and add a way to lookup sockets by queue pair handle.
1449 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1450 vmci_transport_peer_detach_cb
,
1451 vmci_trans(vsk
), &detach_sub_id
);
1452 if (err
< VMCI_SUCCESS
) {
1453 err
= vmci_transport_error_to_vsock_error(err
);
1457 /* Make VMCI select the handle for us. */
1458 handle
= VMCI_INVALID_HANDLE
;
1459 is_local
= vsk
->remote_addr
.svm_cid
== vsk
->local_addr
.svm_cid
;
1460 flags
= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1462 err
= vmci_transport_queue_pair_alloc(&qpair
,
1466 vsk
->remote_addr
.svm_cid
,
1468 vmci_transport_is_trusted(
1471 remote_addr
.svm_cid
));
1475 err
= vmci_transport_send_qp_offer(sk
, handle
);
1477 err
= vmci_transport_error_to_vsock_error(err
);
1481 vmci_trans(vsk
)->qp_handle
= handle
;
1482 vmci_trans(vsk
)->qpair
= qpair
;
1484 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
=
1487 vmci_trans(vsk
)->detach_sub_id
= detach_sub_id
;
1489 vmci_trans(vsk
)->notify_ops
->process_negotiate(sk
);
1494 if (detach_sub_id
!= VMCI_INVALID_ID
)
1495 vmci_event_unsubscribe(detach_sub_id
);
1497 if (!vmci_handle_is_invalid(handle
))
1498 vmci_qpair_detach(&qpair
);
1504 vmci_transport_recv_connecting_client_invalid(struct sock
*sk
,
1505 struct vmci_transport_packet
*pkt
)
1508 struct vsock_sock
*vsk
= vsock_sk(sk
);
1510 if (vsk
->sent_request
) {
1511 vsk
->sent_request
= false;
1512 vsk
->ignore_connecting_rst
= true;
1514 err
= vmci_transport_send_conn_request(
1515 sk
, vmci_trans(vsk
)->queue_pair_size
);
1517 err
= vmci_transport_error_to_vsock_error(err
);
1526 static int vmci_transport_recv_connected(struct sock
*sk
,
1527 struct vmci_transport_packet
*pkt
)
1529 struct vsock_sock
*vsk
;
1530 bool pkt_processed
= false;
1532 /* In cases where we are closing the connection, it's sufficient to
1533 * mark the state change (and maybe error) and wake up any waiting
1534 * threads. Since this is a connected socket, it's owned by a user
1535 * process and will be cleaned up when the failure is passed back on
1536 * the current or next system call. Our system call implementations
1537 * must therefore check for error and state changes on entry and when
1540 switch (pkt
->type
) {
1541 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
1545 vsk
->peer_shutdown
|= pkt
->u
.mode
;
1546 sk
->sk_state_change(sk
);
1550 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1552 /* It is possible that we sent our peer a message (e.g a
1553 * WAITING_READ) right before we got notified that the peer had
1554 * detached. If that happens then we can get a RST pkt back
1555 * from our peer even though there is data available for us to
1556 * read. In that case, don't shutdown the socket completely but
1557 * instead allow the local client to finish reading data off
1558 * the queuepair. Always treat a RST pkt in connected mode like
1561 sock_set_flag(sk
, SOCK_DONE
);
1562 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
1563 if (vsock_stream_has_data(vsk
) <= 0)
1564 sk
->sk_state
= TCP_CLOSING
;
1566 sk
->sk_state_change(sk
);
1571 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
1572 sk
, pkt
, false, NULL
, NULL
,
1583 static int vmci_transport_socket_init(struct vsock_sock
*vsk
,
1584 struct vsock_sock
*psk
)
1586 vsk
->trans
= kmalloc(sizeof(struct vmci_transport
), GFP_KERNEL
);
1590 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1591 vmci_trans(vsk
)->qp_handle
= VMCI_INVALID_HANDLE
;
1592 vmci_trans(vsk
)->qpair
= NULL
;
1593 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
= 0;
1594 vmci_trans(vsk
)->detach_sub_id
= VMCI_INVALID_ID
;
1595 vmci_trans(vsk
)->notify_ops
= NULL
;
1596 INIT_LIST_HEAD(&vmci_trans(vsk
)->elem
);
1597 vmci_trans(vsk
)->sk
= &vsk
->sk
;
1598 spin_lock_init(&vmci_trans(vsk
)->lock
);
1600 vmci_trans(vsk
)->queue_pair_size
=
1601 vmci_trans(psk
)->queue_pair_size
;
1602 vmci_trans(vsk
)->queue_pair_min_size
=
1603 vmci_trans(psk
)->queue_pair_min_size
;
1604 vmci_trans(vsk
)->queue_pair_max_size
=
1605 vmci_trans(psk
)->queue_pair_max_size
;
1607 vmci_trans(vsk
)->queue_pair_size
=
1608 VMCI_TRANSPORT_DEFAULT_QP_SIZE
;
1609 vmci_trans(vsk
)->queue_pair_min_size
=
1610 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN
;
1611 vmci_trans(vsk
)->queue_pair_max_size
=
1612 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX
;
1618 static void vmci_transport_free_resources(struct list_head
*transport_list
)
1620 while (!list_empty(transport_list
)) {
1621 struct vmci_transport
*transport
=
1622 list_first_entry(transport_list
, struct vmci_transport
,
1624 list_del(&transport
->elem
);
1626 if (transport
->detach_sub_id
!= VMCI_INVALID_ID
) {
1627 vmci_event_unsubscribe(transport
->detach_sub_id
);
1628 transport
->detach_sub_id
= VMCI_INVALID_ID
;
1631 if (!vmci_handle_is_invalid(transport
->qp_handle
)) {
1632 vmci_qpair_detach(&transport
->qpair
);
1633 transport
->qp_handle
= VMCI_INVALID_HANDLE
;
1634 transport
->produce_size
= 0;
1635 transport
->consume_size
= 0;
1642 static void vmci_transport_cleanup(struct work_struct
*work
)
1646 spin_lock_bh(&vmci_transport_cleanup_lock
);
1647 list_replace_init(&vmci_transport_cleanup_list
, &pending
);
1648 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1649 vmci_transport_free_resources(&pending
);
1652 static void vmci_transport_destruct(struct vsock_sock
*vsk
)
1654 /* transport can be NULL if we hit a failure at init() time */
1655 if (!vmci_trans(vsk
))
1658 /* Ensure that the detach callback doesn't use the sk/vsk
1659 * we are about to destruct.
1661 spin_lock_bh(&vmci_trans(vsk
)->lock
);
1662 vmci_trans(vsk
)->sk
= NULL
;
1663 spin_unlock_bh(&vmci_trans(vsk
)->lock
);
1665 if (vmci_trans(vsk
)->notify_ops
)
1666 vmci_trans(vsk
)->notify_ops
->socket_destruct(vsk
);
1668 spin_lock_bh(&vmci_transport_cleanup_lock
);
1669 list_add(&vmci_trans(vsk
)->elem
, &vmci_transport_cleanup_list
);
1670 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1671 schedule_work(&vmci_transport_cleanup_work
);
1676 static void vmci_transport_release(struct vsock_sock
*vsk
)
1678 vsock_remove_sock(vsk
);
1680 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->dg_handle
)) {
1681 vmci_datagram_destroy_handle(vmci_trans(vsk
)->dg_handle
);
1682 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1686 static int vmci_transport_dgram_bind(struct vsock_sock
*vsk
,
1687 struct sockaddr_vm
*addr
)
1693 /* VMCI will select a resource ID for us if we provide
1696 port
= addr
->svm_port
== VMADDR_PORT_ANY
?
1697 VMCI_INVALID_ID
: addr
->svm_port
;
1699 if (port
<= LAST_RESERVED_PORT
&& !capable(CAP_NET_BIND_SERVICE
))
1702 flags
= addr
->svm_cid
== VMADDR_CID_ANY
?
1703 VMCI_FLAG_ANYCID_DG_HND
: 0;
1705 err
= vmci_transport_datagram_create_hnd(port
, flags
,
1706 vmci_transport_recv_dgram_cb
,
1708 &vmci_trans(vsk
)->dg_handle
);
1709 if (err
< VMCI_SUCCESS
)
1710 return vmci_transport_error_to_vsock_error(err
);
1711 vsock_addr_init(&vsk
->local_addr
, addr
->svm_cid
,
1712 vmci_trans(vsk
)->dg_handle
.resource
);
1717 static int vmci_transport_dgram_enqueue(
1718 struct vsock_sock
*vsk
,
1719 struct sockaddr_vm
*remote_addr
,
1724 struct vmci_datagram
*dg
;
1726 if (len
> VMCI_MAX_DG_PAYLOAD_SIZE
)
1729 if (!vmci_transport_allow_dgram(vsk
, remote_addr
->svm_cid
))
1732 /* Allocate a buffer for the user's message and our packet header. */
1733 dg
= kmalloc(len
+ sizeof(*dg
), GFP_KERNEL
);
1737 memcpy_from_msg(VMCI_DG_PAYLOAD(dg
), msg
, len
);
1739 dg
->dst
= vmci_make_handle(remote_addr
->svm_cid
,
1740 remote_addr
->svm_port
);
1741 dg
->src
= vmci_make_handle(vsk
->local_addr
.svm_cid
,
1742 vsk
->local_addr
.svm_port
);
1743 dg
->payload_size
= len
;
1745 err
= vmci_datagram_send(dg
);
1748 return vmci_transport_error_to_vsock_error(err
);
1750 return err
- sizeof(*dg
);
1753 static int vmci_transport_dgram_dequeue(struct vsock_sock
*vsk
,
1754 struct msghdr
*msg
, size_t len
,
1759 struct vmci_datagram
*dg
;
1761 struct sk_buff
*skb
;
1763 noblock
= flags
& MSG_DONTWAIT
;
1765 if (flags
& MSG_OOB
|| flags
& MSG_ERRQUEUE
)
1768 /* Retrieve the head sk_buff from the socket's receive queue. */
1770 skb
= skb_recv_datagram(&vsk
->sk
, flags
, noblock
, &err
);
1774 dg
= (struct vmci_datagram
*)skb
->data
;
1776 /* err is 0, meaning we read zero bytes. */
1779 payload_len
= dg
->payload_size
;
1780 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1781 if (payload_len
!= skb
->len
- sizeof(*dg
)) {
1786 if (payload_len
> len
) {
1788 msg
->msg_flags
|= MSG_TRUNC
;
1791 /* Place the datagram payload in the user's iovec. */
1792 err
= skb_copy_datagram_msg(skb
, sizeof(*dg
), msg
, payload_len
);
1796 if (msg
->msg_name
) {
1797 /* Provide the address of the sender. */
1798 DECLARE_SOCKADDR(struct sockaddr_vm
*, vm_addr
, msg
->msg_name
);
1799 vsock_addr_init(vm_addr
, dg
->src
.context
, dg
->src
.resource
);
1800 msg
->msg_namelen
= sizeof(*vm_addr
);
1805 skb_free_datagram(&vsk
->sk
, skb
);
1809 static bool vmci_transport_dgram_allow(u32 cid
, u32 port
)
1811 if (cid
== VMADDR_CID_HYPERVISOR
) {
1812 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1813 * state and are allowed.
1815 return port
== VMCI_UNITY_PBRPC_REGISTER
;
1821 static int vmci_transport_connect(struct vsock_sock
*vsk
)
1824 bool old_pkt_proto
= false;
1825 struct sock
*sk
= &vsk
->sk
;
1827 if (vmci_transport_old_proto_override(&old_pkt_proto
) &&
1829 err
= vmci_transport_send_conn_request(
1830 sk
, vmci_trans(vsk
)->queue_pair_size
);
1832 sk
->sk_state
= TCP_CLOSE
;
1836 int supported_proto_versions
=
1837 vmci_transport_new_proto_supported_versions();
1838 err
= vmci_transport_send_conn_request2(
1839 sk
, vmci_trans(vsk
)->queue_pair_size
,
1840 supported_proto_versions
);
1842 sk
->sk_state
= TCP_CLOSE
;
1846 vsk
->sent_request
= true;
1852 static ssize_t
vmci_transport_stream_dequeue(
1853 struct vsock_sock
*vsk
,
1858 if (flags
& MSG_PEEK
)
1859 return vmci_qpair_peekv(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1861 return vmci_qpair_dequev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1864 static ssize_t
vmci_transport_stream_enqueue(
1865 struct vsock_sock
*vsk
,
1869 return vmci_qpair_enquev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1872 static s64
vmci_transport_stream_has_data(struct vsock_sock
*vsk
)
1874 return vmci_qpair_consume_buf_ready(vmci_trans(vsk
)->qpair
);
1877 static s64
vmci_transport_stream_has_space(struct vsock_sock
*vsk
)
1879 return vmci_qpair_produce_free_space(vmci_trans(vsk
)->qpair
);
1882 static u64
vmci_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
1884 return vmci_trans(vsk
)->consume_size
;
1887 static bool vmci_transport_stream_is_active(struct vsock_sock
*vsk
)
1889 return !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
);
1892 static u64
vmci_transport_get_buffer_size(struct vsock_sock
*vsk
)
1894 return vmci_trans(vsk
)->queue_pair_size
;
1897 static u64
vmci_transport_get_min_buffer_size(struct vsock_sock
*vsk
)
1899 return vmci_trans(vsk
)->queue_pair_min_size
;
1902 static u64
vmci_transport_get_max_buffer_size(struct vsock_sock
*vsk
)
1904 return vmci_trans(vsk
)->queue_pair_max_size
;
1907 static void vmci_transport_set_buffer_size(struct vsock_sock
*vsk
, u64 val
)
1909 if (val
< vmci_trans(vsk
)->queue_pair_min_size
)
1910 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1911 if (val
> vmci_trans(vsk
)->queue_pair_max_size
)
1912 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1913 vmci_trans(vsk
)->queue_pair_size
= val
;
1916 static void vmci_transport_set_min_buffer_size(struct vsock_sock
*vsk
,
1919 if (val
> vmci_trans(vsk
)->queue_pair_size
)
1920 vmci_trans(vsk
)->queue_pair_size
= val
;
1921 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1924 static void vmci_transport_set_max_buffer_size(struct vsock_sock
*vsk
,
1927 if (val
< vmci_trans(vsk
)->queue_pair_size
)
1928 vmci_trans(vsk
)->queue_pair_size
= val
;
1929 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1932 static int vmci_transport_notify_poll_in(
1933 struct vsock_sock
*vsk
,
1935 bool *data_ready_now
)
1937 return vmci_trans(vsk
)->notify_ops
->poll_in(
1938 &vsk
->sk
, target
, data_ready_now
);
1941 static int vmci_transport_notify_poll_out(
1942 struct vsock_sock
*vsk
,
1944 bool *space_available_now
)
1946 return vmci_trans(vsk
)->notify_ops
->poll_out(
1947 &vsk
->sk
, target
, space_available_now
);
1950 static int vmci_transport_notify_recv_init(
1951 struct vsock_sock
*vsk
,
1953 struct vsock_transport_recv_notify_data
*data
)
1955 return vmci_trans(vsk
)->notify_ops
->recv_init(
1957 (struct vmci_transport_recv_notify_data
*)data
);
1960 static int vmci_transport_notify_recv_pre_block(
1961 struct vsock_sock
*vsk
,
1963 struct vsock_transport_recv_notify_data
*data
)
1965 return vmci_trans(vsk
)->notify_ops
->recv_pre_block(
1967 (struct vmci_transport_recv_notify_data
*)data
);
1970 static int vmci_transport_notify_recv_pre_dequeue(
1971 struct vsock_sock
*vsk
,
1973 struct vsock_transport_recv_notify_data
*data
)
1975 return vmci_trans(vsk
)->notify_ops
->recv_pre_dequeue(
1977 (struct vmci_transport_recv_notify_data
*)data
);
1980 static int vmci_transport_notify_recv_post_dequeue(
1981 struct vsock_sock
*vsk
,
1985 struct vsock_transport_recv_notify_data
*data
)
1987 return vmci_trans(vsk
)->notify_ops
->recv_post_dequeue(
1988 &vsk
->sk
, target
, copied
, data_read
,
1989 (struct vmci_transport_recv_notify_data
*)data
);
1992 static int vmci_transport_notify_send_init(
1993 struct vsock_sock
*vsk
,
1994 struct vsock_transport_send_notify_data
*data
)
1996 return vmci_trans(vsk
)->notify_ops
->send_init(
1998 (struct vmci_transport_send_notify_data
*)data
);
2001 static int vmci_transport_notify_send_pre_block(
2002 struct vsock_sock
*vsk
,
2003 struct vsock_transport_send_notify_data
*data
)
2005 return vmci_trans(vsk
)->notify_ops
->send_pre_block(
2007 (struct vmci_transport_send_notify_data
*)data
);
2010 static int vmci_transport_notify_send_pre_enqueue(
2011 struct vsock_sock
*vsk
,
2012 struct vsock_transport_send_notify_data
*data
)
2014 return vmci_trans(vsk
)->notify_ops
->send_pre_enqueue(
2016 (struct vmci_transport_send_notify_data
*)data
);
2019 static int vmci_transport_notify_send_post_enqueue(
2020 struct vsock_sock
*vsk
,
2022 struct vsock_transport_send_notify_data
*data
)
2024 return vmci_trans(vsk
)->notify_ops
->send_post_enqueue(
2026 (struct vmci_transport_send_notify_data
*)data
);
2029 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
)
2031 if (PROTOCOL_OVERRIDE
!= -1) {
2032 if (PROTOCOL_OVERRIDE
== 0)
2033 *old_pkt_proto
= true;
2035 *old_pkt_proto
= false;
2037 pr_info("Proto override in use\n");
2044 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
,
2048 struct vsock_sock
*vsk
= vsock_sk(sk
);
2050 if (old_pkt_proto
) {
2051 if (*proto
!= VSOCK_PROTO_INVALID
) {
2052 pr_err("Can't set both an old and new protocol\n");
2055 vmci_trans(vsk
)->notify_ops
= &vmci_transport_notify_pkt_ops
;
2060 case VSOCK_PROTO_PKT_ON_NOTIFY
:
2061 vmci_trans(vsk
)->notify_ops
=
2062 &vmci_transport_notify_pkt_q_state_ops
;
2065 pr_err("Unknown notify protocol version\n");
2070 vmci_trans(vsk
)->notify_ops
->socket_init(sk
);
2074 static u16
vmci_transport_new_proto_supported_versions(void)
2076 if (PROTOCOL_OVERRIDE
!= -1)
2077 return PROTOCOL_OVERRIDE
;
2079 return VSOCK_PROTO_ALL_SUPPORTED
;
2082 static u32
vmci_transport_get_local_cid(void)
2084 return vmci_get_context_id();
2087 static const struct vsock_transport vmci_transport
= {
2088 .init
= vmci_transport_socket_init
,
2089 .destruct
= vmci_transport_destruct
,
2090 .release
= vmci_transport_release
,
2091 .connect
= vmci_transport_connect
,
2092 .dgram_bind
= vmci_transport_dgram_bind
,
2093 .dgram_dequeue
= vmci_transport_dgram_dequeue
,
2094 .dgram_enqueue
= vmci_transport_dgram_enqueue
,
2095 .dgram_allow
= vmci_transport_dgram_allow
,
2096 .stream_dequeue
= vmci_transport_stream_dequeue
,
2097 .stream_enqueue
= vmci_transport_stream_enqueue
,
2098 .stream_has_data
= vmci_transport_stream_has_data
,
2099 .stream_has_space
= vmci_transport_stream_has_space
,
2100 .stream_rcvhiwat
= vmci_transport_stream_rcvhiwat
,
2101 .stream_is_active
= vmci_transport_stream_is_active
,
2102 .stream_allow
= vmci_transport_stream_allow
,
2103 .notify_poll_in
= vmci_transport_notify_poll_in
,
2104 .notify_poll_out
= vmci_transport_notify_poll_out
,
2105 .notify_recv_init
= vmci_transport_notify_recv_init
,
2106 .notify_recv_pre_block
= vmci_transport_notify_recv_pre_block
,
2107 .notify_recv_pre_dequeue
= vmci_transport_notify_recv_pre_dequeue
,
2108 .notify_recv_post_dequeue
= vmci_transport_notify_recv_post_dequeue
,
2109 .notify_send_init
= vmci_transport_notify_send_init
,
2110 .notify_send_pre_block
= vmci_transport_notify_send_pre_block
,
2111 .notify_send_pre_enqueue
= vmci_transport_notify_send_pre_enqueue
,
2112 .notify_send_post_enqueue
= vmci_transport_notify_send_post_enqueue
,
2113 .shutdown
= vmci_transport_shutdown
,
2114 .set_buffer_size
= vmci_transport_set_buffer_size
,
2115 .set_min_buffer_size
= vmci_transport_set_min_buffer_size
,
2116 .set_max_buffer_size
= vmci_transport_set_max_buffer_size
,
2117 .get_buffer_size
= vmci_transport_get_buffer_size
,
2118 .get_min_buffer_size
= vmci_transport_get_min_buffer_size
,
2119 .get_max_buffer_size
= vmci_transport_get_max_buffer_size
,
2120 .get_local_cid
= vmci_transport_get_local_cid
,
2123 static int __init
vmci_transport_init(void)
2127 /* Create the datagram handle that we will use to send and receive all
2128 * VSocket control messages for this context.
2130 err
= vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID
,
2131 VMCI_FLAG_ANYCID_DG_HND
,
2132 vmci_transport_recv_stream_cb
,
2134 &vmci_transport_stream_handle
);
2135 if (err
< VMCI_SUCCESS
) {
2136 pr_err("Unable to create datagram handle. (%d)\n", err
);
2137 return vmci_transport_error_to_vsock_error(err
);
2140 err
= vmci_event_subscribe(VMCI_EVENT_QP_RESUMED
,
2141 vmci_transport_qp_resumed_cb
,
2142 NULL
, &vmci_transport_qp_resumed_sub_id
);
2143 if (err
< VMCI_SUCCESS
) {
2144 pr_err("Unable to subscribe to resumed event. (%d)\n", err
);
2145 err
= vmci_transport_error_to_vsock_error(err
);
2146 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2147 goto err_destroy_stream_handle
;
2150 err
= vsock_core_init(&vmci_transport
);
2152 goto err_unsubscribe
;
2157 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2158 err_destroy_stream_handle
:
2159 vmci_datagram_destroy_handle(vmci_transport_stream_handle
);
2162 module_init(vmci_transport_init
);
2164 static void __exit
vmci_transport_exit(void)
2166 cancel_work_sync(&vmci_transport_cleanup_work
);
2167 vmci_transport_free_resources(&vmci_transport_cleanup_list
);
2169 if (!vmci_handle_is_invalid(vmci_transport_stream_handle
)) {
2170 if (vmci_datagram_destroy_handle(
2171 vmci_transport_stream_handle
) != VMCI_SUCCESS
)
2172 pr_err("Couldn't destroy datagram handle\n");
2173 vmci_transport_stream_handle
= VMCI_INVALID_HANDLE
;
2176 if (vmci_transport_qp_resumed_sub_id
!= VMCI_INVALID_ID
) {
2177 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2178 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2183 module_exit(vmci_transport_exit
);
2185 MODULE_AUTHOR("VMware, Inc.");
2186 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2187 MODULE_VERSION("1.0.5.0-k");
2188 MODULE_LICENSE("GPL v2");
2189 MODULE_ALIAS("vmware_vsock");
2190 MODULE_ALIAS_NETPROTO(PF_VSOCK
);