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)
97 /* Helper function to convert from a VMCI error code to a VSock error code. */
99 static s32
vmci_transport_error_to_vsock_error(s32 vmci_error
)
103 switch (vmci_error
) {
104 case VMCI_ERROR_NO_MEM
:
107 case VMCI_ERROR_DUPLICATE_ENTRY
:
108 case VMCI_ERROR_ALREADY_EXISTS
:
111 case VMCI_ERROR_NO_ACCESS
:
114 case VMCI_ERROR_NO_RESOURCES
:
117 case VMCI_ERROR_INVALID_RESOURCE
:
120 case VMCI_ERROR_INVALID_ARGS
:
125 return err
> 0 ? -err
: err
;
128 static u32
vmci_transport_peer_rid(u32 peer_cid
)
130 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
131 return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID
;
133 return VMCI_TRANSPORT_PACKET_RID
;
137 vmci_transport_packet_init(struct vmci_transport_packet
*pkt
,
138 struct sockaddr_vm
*src
,
139 struct sockaddr_vm
*dst
,
143 struct vmci_transport_waiting_info
*wait
,
145 struct vmci_handle handle
)
147 /* We register the stream control handler as an any cid handle so we
148 * must always send from a source address of VMADDR_CID_ANY
150 pkt
->dg
.src
= vmci_make_handle(VMADDR_CID_ANY
,
151 VMCI_TRANSPORT_PACKET_RID
);
152 pkt
->dg
.dst
= vmci_make_handle(dst
->svm_cid
,
153 vmci_transport_peer_rid(dst
->svm_cid
));
154 pkt
->dg
.payload_size
= sizeof(*pkt
) - sizeof(pkt
->dg
);
155 pkt
->version
= VMCI_TRANSPORT_PACKET_VERSION
;
157 pkt
->src_port
= src
->svm_port
;
158 pkt
->dst_port
= dst
->svm_port
;
159 memset(&pkt
->proto
, 0, sizeof(pkt
->proto
));
160 memset(&pkt
->_reserved2
, 0, sizeof(pkt
->_reserved2
));
163 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
167 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST
:
168 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
172 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
173 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
174 pkt
->u
.handle
= handle
;
177 case VMCI_TRANSPORT_PACKET_TYPE_WROTE
:
178 case VMCI_TRANSPORT_PACKET_TYPE_READ
:
179 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
183 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
187 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
:
188 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
:
189 memcpy(&pkt
->u
.wait
, wait
, sizeof(pkt
->u
.wait
));
192 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
:
193 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
201 vmci_transport_packet_get_addresses(struct vmci_transport_packet
*pkt
,
202 struct sockaddr_vm
*local
,
203 struct sockaddr_vm
*remote
)
205 vsock_addr_init(local
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
206 vsock_addr_init(remote
, pkt
->dg
.src
.context
, pkt
->src_port
);
210 __vmci_transport_send_control_pkt(struct vmci_transport_packet
*pkt
,
211 struct sockaddr_vm
*src
,
212 struct sockaddr_vm
*dst
,
213 enum vmci_transport_packet_type type
,
216 struct vmci_transport_waiting_info
*wait
,
218 struct vmci_handle handle
,
223 vmci_transport_packet_init(pkt
, src
, dst
, type
, size
, mode
, wait
,
225 err
= vmci_datagram_send(&pkt
->dg
);
226 if (convert_error
&& (err
< 0))
227 return vmci_transport_error_to_vsock_error(err
);
233 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet
*pkt
,
234 enum vmci_transport_packet_type type
,
237 struct vmci_transport_waiting_info
*wait
,
238 struct vmci_handle handle
)
240 struct vmci_transport_packet reply
;
241 struct sockaddr_vm src
, dst
;
243 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
) {
246 vmci_transport_packet_get_addresses(pkt
, &src
, &dst
);
247 return __vmci_transport_send_control_pkt(&reply
, &src
, &dst
,
256 vmci_transport_send_control_pkt_bh(struct sockaddr_vm
*src
,
257 struct sockaddr_vm
*dst
,
258 enum vmci_transport_packet_type type
,
261 struct vmci_transport_waiting_info
*wait
,
262 struct vmci_handle handle
)
264 /* Note that it is safe to use a single packet across all CPUs since
265 * two tasklets of the same type are guaranteed to not ever run
266 * simultaneously. If that ever changes, or VMCI stops using tasklets,
267 * we can use per-cpu packets.
269 static struct vmci_transport_packet pkt
;
271 return __vmci_transport_send_control_pkt(&pkt
, src
, dst
, type
,
273 VSOCK_PROTO_INVALID
, handle
,
278 vmci_transport_send_control_pkt(struct sock
*sk
,
279 enum vmci_transport_packet_type type
,
282 struct vmci_transport_waiting_info
*wait
,
284 struct vmci_handle handle
)
286 struct vmci_transport_packet
*pkt
;
287 struct vsock_sock
*vsk
;
292 if (!vsock_addr_bound(&vsk
->local_addr
))
295 if (!vsock_addr_bound(&vsk
->remote_addr
))
298 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
302 err
= __vmci_transport_send_control_pkt(pkt
, &vsk
->local_addr
,
303 &vsk
->remote_addr
, type
, size
,
304 mode
, wait
, proto
, handle
,
311 static int vmci_transport_send_reset_bh(struct sockaddr_vm
*dst
,
312 struct sockaddr_vm
*src
,
313 struct vmci_transport_packet
*pkt
)
315 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
317 return vmci_transport_send_control_pkt_bh(
319 VMCI_TRANSPORT_PACKET_TYPE_RST
, 0,
320 0, NULL
, VMCI_INVALID_HANDLE
);
323 static int vmci_transport_send_reset(struct sock
*sk
,
324 struct vmci_transport_packet
*pkt
)
326 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
328 return vmci_transport_send_control_pkt(sk
,
329 VMCI_TRANSPORT_PACKET_TYPE_RST
,
330 0, 0, NULL
, VSOCK_PROTO_INVALID
,
331 VMCI_INVALID_HANDLE
);
334 static int vmci_transport_send_negotiate(struct sock
*sk
, size_t size
)
336 return vmci_transport_send_control_pkt(
338 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
,
341 VMCI_INVALID_HANDLE
);
344 static int vmci_transport_send_negotiate2(struct sock
*sk
, size_t size
,
347 return vmci_transport_send_control_pkt(
349 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
,
350 size
, 0, NULL
, version
,
351 VMCI_INVALID_HANDLE
);
354 static int vmci_transport_send_qp_offer(struct sock
*sk
,
355 struct vmci_handle handle
)
357 return vmci_transport_send_control_pkt(
358 sk
, VMCI_TRANSPORT_PACKET_TYPE_OFFER
, 0,
360 VSOCK_PROTO_INVALID
, handle
);
363 static int vmci_transport_send_attach(struct sock
*sk
,
364 struct vmci_handle handle
)
366 return vmci_transport_send_control_pkt(
367 sk
, VMCI_TRANSPORT_PACKET_TYPE_ATTACH
,
368 0, 0, NULL
, VSOCK_PROTO_INVALID
,
372 static int vmci_transport_reply_reset(struct vmci_transport_packet
*pkt
)
374 return vmci_transport_reply_control_pkt_fast(
376 VMCI_TRANSPORT_PACKET_TYPE_RST
,
378 VMCI_INVALID_HANDLE
);
381 static int vmci_transport_send_invalid_bh(struct sockaddr_vm
*dst
,
382 struct sockaddr_vm
*src
)
384 return vmci_transport_send_control_pkt_bh(
386 VMCI_TRANSPORT_PACKET_TYPE_INVALID
,
387 0, 0, NULL
, VMCI_INVALID_HANDLE
);
390 int vmci_transport_send_wrote_bh(struct sockaddr_vm
*dst
,
391 struct sockaddr_vm
*src
)
393 return vmci_transport_send_control_pkt_bh(
395 VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
396 0, NULL
, VMCI_INVALID_HANDLE
);
399 int vmci_transport_send_read_bh(struct sockaddr_vm
*dst
,
400 struct sockaddr_vm
*src
)
402 return vmci_transport_send_control_pkt_bh(
404 VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
405 0, NULL
, VMCI_INVALID_HANDLE
);
408 int vmci_transport_send_wrote(struct sock
*sk
)
410 return vmci_transport_send_control_pkt(
411 sk
, VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
412 0, NULL
, VSOCK_PROTO_INVALID
,
413 VMCI_INVALID_HANDLE
);
416 int vmci_transport_send_read(struct sock
*sk
)
418 return vmci_transport_send_control_pkt(
419 sk
, VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
420 0, NULL
, VSOCK_PROTO_INVALID
,
421 VMCI_INVALID_HANDLE
);
424 int vmci_transport_send_waiting_write(struct sock
*sk
,
425 struct vmci_transport_waiting_info
*wait
)
427 return vmci_transport_send_control_pkt(
428 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
,
429 0, 0, wait
, VSOCK_PROTO_INVALID
,
430 VMCI_INVALID_HANDLE
);
433 int vmci_transport_send_waiting_read(struct sock
*sk
,
434 struct vmci_transport_waiting_info
*wait
)
436 return vmci_transport_send_control_pkt(
437 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
,
438 0, 0, wait
, VSOCK_PROTO_INVALID
,
439 VMCI_INVALID_HANDLE
);
442 static int vmci_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
444 return vmci_transport_send_control_pkt(
446 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
,
449 VMCI_INVALID_HANDLE
);
452 static int vmci_transport_send_conn_request(struct sock
*sk
, size_t size
)
454 return vmci_transport_send_control_pkt(sk
,
455 VMCI_TRANSPORT_PACKET_TYPE_REQUEST
,
458 VMCI_INVALID_HANDLE
);
461 static int vmci_transport_send_conn_request2(struct sock
*sk
, size_t size
,
464 return vmci_transport_send_control_pkt(
465 sk
, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
,
466 size
, 0, NULL
, version
,
467 VMCI_INVALID_HANDLE
);
470 static struct sock
*vmci_transport_get_pending(
471 struct sock
*listener
,
472 struct vmci_transport_packet
*pkt
)
474 struct vsock_sock
*vlistener
;
475 struct vsock_sock
*vpending
;
476 struct sock
*pending
;
477 struct sockaddr_vm src
;
479 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
481 vlistener
= vsock_sk(listener
);
483 list_for_each_entry(vpending
, &vlistener
->pending_links
,
485 if (vsock_addr_equals_addr(&src
, &vpending
->remote_addr
) &&
486 pkt
->dst_port
== vpending
->local_addr
.svm_port
) {
487 pending
= sk_vsock(vpending
);
499 static void vmci_transport_release_pending(struct sock
*pending
)
504 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
505 * trusted sockets 2) sockets from applications running as the same user as the
506 * VM (this is only true for the host side and only when using hosted products)
509 static bool vmci_transport_is_trusted(struct vsock_sock
*vsock
, u32 peer_cid
)
511 return vsock
->trusted
||
512 vmci_is_context_owner(peer_cid
, vsock
->owner
->uid
);
515 /* We allow sending datagrams to and receiving datagrams from a restricted VM
516 * only if it is trusted as described in vmci_transport_is_trusted.
519 static bool vmci_transport_allow_dgram(struct vsock_sock
*vsock
, u32 peer_cid
)
521 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
524 if (vsock
->cached_peer
!= peer_cid
) {
525 vsock
->cached_peer
= peer_cid
;
526 if (!vmci_transport_is_trusted(vsock
, peer_cid
) &&
527 (vmci_context_get_priv_flags(peer_cid
) &
528 VMCI_PRIVILEGE_FLAG_RESTRICTED
)) {
529 vsock
->cached_peer_allow_dgram
= false;
531 vsock
->cached_peer_allow_dgram
= true;
535 return vsock
->cached_peer_allow_dgram
;
539 vmci_transport_queue_pair_alloc(struct vmci_qp
**qpair
,
540 struct vmci_handle
*handle
,
543 u32 peer
, u32 flags
, bool trusted
)
548 /* Try to allocate our queue pair as trusted. This will only
549 * work if vsock is running in the host.
552 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
,
555 VMCI_PRIVILEGE_FLAG_TRUSTED
);
556 if (err
!= VMCI_ERROR_NO_ACCESS
)
561 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
, consume_size
,
562 peer
, flags
, VMCI_NO_PRIVILEGE_FLAGS
);
565 pr_err("Could not attach to queue pair with %d\n",
567 err
= vmci_transport_error_to_vsock_error(err
);
574 vmci_transport_datagram_create_hnd(u32 resource_id
,
576 vmci_datagram_recv_cb recv_cb
,
578 struct vmci_handle
*out_handle
)
582 /* Try to allocate our datagram handler as trusted. This will only work
583 * if vsock is running in the host.
586 err
= vmci_datagram_create_handle_priv(resource_id
, flags
,
587 VMCI_PRIVILEGE_FLAG_TRUSTED
,
589 client_data
, out_handle
);
591 if (err
== VMCI_ERROR_NO_ACCESS
)
592 err
= vmci_datagram_create_handle(resource_id
, flags
,
593 recv_cb
, client_data
,
599 /* This is invoked as part of a tasklet that's scheduled when the VMCI
600 * interrupt fires. This is run in bottom-half context and if it ever needs to
601 * sleep it should defer that work to a work queue.
604 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
)
609 struct vsock_sock
*vsk
;
611 sk
= (struct sock
*)data
;
613 /* This handler is privileged when this module is running on the host.
614 * We will get datagrams from all endpoints (even VMs that are in a
615 * restricted context). If we get one from a restricted context then
616 * the destination socket must be trusted.
618 * NOTE: We access the socket struct without holding the lock here.
619 * This is ok because the field we are interested is never modified
620 * outside of the create and destruct socket functions.
623 if (!vmci_transport_allow_dgram(vsk
, dg
->src
.context
))
624 return VMCI_ERROR_NO_ACCESS
;
626 size
= VMCI_DG_SIZE(dg
);
628 /* Attach the packet to the socket's receive queue as an sk_buff. */
629 skb
= alloc_skb(size
, GFP_ATOMIC
);
631 return VMCI_ERROR_NO_MEM
;
633 /* sk_receive_skb() will do a sock_put(), so hold here. */
636 memcpy(skb
->data
, dg
, size
);
637 sk_receive_skb(sk
, skb
, 0);
642 static bool vmci_transport_stream_allow(u32 cid
, u32 port
)
644 static const u32 non_socket_contexts
[] = {
649 BUILD_BUG_ON(sizeof(cid
) != sizeof(*non_socket_contexts
));
651 for (i
= 0; i
< ARRAY_SIZE(non_socket_contexts
); i
++) {
652 if (cid
== non_socket_contexts
[i
])
659 /* This is invoked as part of a tasklet that's scheduled when the VMCI
660 * interrupt fires. This is run in bottom-half context but it defers most of
661 * its work to the packet handling work queue.
664 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
)
667 struct sockaddr_vm dst
;
668 struct sockaddr_vm src
;
669 struct vmci_transport_packet
*pkt
;
670 struct vsock_sock
*vsk
;
676 bh_process_pkt
= false;
678 /* Ignore incoming packets from contexts without sockets, or resources
679 * that aren't vsock implementations.
682 if (!vmci_transport_stream_allow(dg
->src
.context
, -1)
683 || vmci_transport_peer_rid(dg
->src
.context
) != dg
->src
.resource
)
684 return VMCI_ERROR_NO_ACCESS
;
686 if (VMCI_DG_SIZE(dg
) < sizeof(*pkt
))
687 /* Drop datagrams that do not contain full VSock packets. */
688 return VMCI_ERROR_INVALID_ARGS
;
690 pkt
= (struct vmci_transport_packet
*)dg
;
692 /* Find the socket that should handle this packet. First we look for a
693 * connected socket and if there is none we look for a socket bound to
694 * the destintation address.
696 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
697 vsock_addr_init(&dst
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
699 sk
= vsock_find_connected_socket(&src
, &dst
);
701 sk
= vsock_find_bound_socket(&dst
);
703 /* We could not find a socket for this specified
704 * address. If this packet is a RST, we just drop it.
705 * If it is another packet, we send a RST. Note that
706 * we do not send a RST reply to RSTs so that we do not
707 * continually send RSTs between two endpoints.
709 * Note that since this is a reply, dst is src and src
712 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
713 pr_err("unable to send reset\n");
715 err
= VMCI_ERROR_NOT_FOUND
;
720 /* If the received packet type is beyond all types known to this
721 * implementation, reply with an invalid message. Hopefully this will
722 * help when implementing backwards compatibility in the future.
724 if (pkt
->type
>= VMCI_TRANSPORT_PACKET_TYPE_MAX
) {
725 vmci_transport_send_invalid_bh(&dst
, &src
);
726 err
= VMCI_ERROR_INVALID_ARGS
;
730 /* This handler is privileged when this module is running on the host.
731 * We will get datagram connect requests from all endpoints (even VMs
732 * that are in a restricted context). If we get one from a restricted
733 * context then the destination socket must be trusted.
735 * NOTE: We access the socket struct without holding the lock here.
736 * This is ok because the field we are interested is never modified
737 * outside of the create and destruct socket functions.
740 if (!vmci_transport_allow_dgram(vsk
, pkt
->dg
.src
.context
)) {
741 err
= VMCI_ERROR_NO_ACCESS
;
745 /* We do most everything in a work queue, but let's fast path the
746 * notification of reads and writes to help data transfer performance.
747 * We can only do this if there is no process context code executing
748 * for this socket since that may change the state.
752 if (!sock_owned_by_user(sk
)) {
753 /* The local context ID may be out of date, update it. */
754 vsk
->local_addr
.svm_cid
= dst
.svm_cid
;
756 if (sk
->sk_state
== SS_CONNECTED
)
757 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
758 sk
, pkt
, true, &dst
, &src
,
764 if (!bh_process_pkt
) {
765 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
767 recv_pkt_info
= kmalloc(sizeof(*recv_pkt_info
), GFP_ATOMIC
);
768 if (!recv_pkt_info
) {
769 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
770 pr_err("unable to send reset\n");
772 err
= VMCI_ERROR_NO_MEM
;
776 recv_pkt_info
->sk
= sk
;
777 memcpy(&recv_pkt_info
->pkt
, pkt
, sizeof(recv_pkt_info
->pkt
));
778 INIT_WORK(&recv_pkt_info
->work
, vmci_transport_recv_pkt_work
);
780 schedule_work(&recv_pkt_info
->work
);
781 /* Clear sk so that the reference count incremented by one of
782 * the Find functions above is not decremented below. We need
783 * that reference count for the packet handler we've scheduled
796 static void vmci_transport_handle_detach(struct sock
*sk
)
798 struct vsock_sock
*vsk
;
801 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)) {
802 sock_set_flag(sk
, SOCK_DONE
);
804 /* On a detach the peer will not be sending or receiving
807 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
809 /* We should not be sending anymore since the peer won't be
810 * there to receive, but we can still receive if there is data
811 * left in our consume queue.
813 if (vsock_stream_has_data(vsk
) <= 0) {
814 if (sk
->sk_state
== SS_CONNECTING
) {
815 /* The peer may detach from a queue pair while
816 * we are still in the connecting state, i.e.,
817 * if the peer VM is killed after attaching to
818 * a queue pair, but before we complete the
819 * handshake. In that case, we treat the detach
820 * event like a reset.
823 sk
->sk_state
= SS_UNCONNECTED
;
824 sk
->sk_err
= ECONNRESET
;
825 sk
->sk_error_report(sk
);
828 sk
->sk_state
= SS_UNCONNECTED
;
830 sk
->sk_state_change(sk
);
834 static void vmci_transport_peer_detach_cb(u32 sub_id
,
835 const struct vmci_event_data
*e_data
,
838 struct vmci_transport
*trans
= client_data
;
839 const struct vmci_event_payload_qp
*e_payload
;
841 e_payload
= vmci_event_data_const_payload(e_data
);
843 /* XXX This is lame, we should provide a way to lookup sockets by
846 if (vmci_handle_is_invalid(e_payload
->handle
) ||
847 vmci_handle_is_equal(trans
->qp_handle
, e_payload
->handle
))
850 /* We don't ask for delayed CBs when we subscribe to this event (we
851 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
852 * guarantees in that case about what context we might be running in,
853 * so it could be BH or process, blockable or non-blockable. So we
854 * need to account for all possible contexts here.
856 spin_lock_bh(&trans
->lock
);
860 /* Apart from here, trans->lock is only grabbed as part of sk destruct,
861 * where trans->sk isn't locked.
863 bh_lock_sock(trans
->sk
);
865 vmci_transport_handle_detach(trans
->sk
);
867 bh_unlock_sock(trans
->sk
);
869 spin_unlock_bh(&trans
->lock
);
872 static void vmci_transport_qp_resumed_cb(u32 sub_id
,
873 const struct vmci_event_data
*e_data
,
876 vsock_for_each_connected_socket(vmci_transport_handle_detach
);
879 static void vmci_transport_recv_pkt_work(struct work_struct
*work
)
881 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
882 struct vmci_transport_packet
*pkt
;
886 container_of(work
, struct vmci_transport_recv_pkt_info
, work
);
887 sk
= recv_pkt_info
->sk
;
888 pkt
= &recv_pkt_info
->pkt
;
892 /* The local context ID may be out of date. */
893 vsock_sk(sk
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
895 switch (sk
->sk_state
) {
897 vmci_transport_recv_listen(sk
, pkt
);
900 /* Processing of pending connections for servers goes through
901 * the listening socket, so see vmci_transport_recv_listen()
904 vmci_transport_recv_connecting_client(sk
, pkt
);
907 vmci_transport_recv_connected(sk
, pkt
);
910 /* Because this function does not run in the same context as
911 * vmci_transport_recv_stream_cb it is possible that the
912 * socket has closed. We need to let the other side know or it
913 * could be sitting in a connect and hang forever. Send a
914 * reset to prevent that.
916 vmci_transport_send_reset(sk
, pkt
);
921 kfree(recv_pkt_info
);
922 /* Release reference obtained in the stream callback when we fetched
923 * this socket out of the bound or connected list.
928 static int vmci_transport_recv_listen(struct sock
*sk
,
929 struct vmci_transport_packet
*pkt
)
931 struct sock
*pending
;
932 struct vsock_sock
*vpending
;
935 bool old_request
= false;
936 bool old_pkt_proto
= false;
940 /* Because we are in the listen state, we could be receiving a packet
941 * for ourself or any previous connection requests that we received.
942 * If it's the latter, we try to find a socket in our list of pending
943 * connections and, if we do, call the appropriate handler for the
944 * state that that socket is in. Otherwise we try to service the
945 * connection request.
947 pending
= vmci_transport_get_pending(sk
, pkt
);
951 /* The local context ID may be out of date. */
952 vsock_sk(pending
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
954 switch (pending
->sk_state
) {
956 err
= vmci_transport_recv_connecting_server(sk
,
961 vmci_transport_send_reset(pending
, pkt
);
966 vsock_remove_pending(sk
, pending
);
968 release_sock(pending
);
969 vmci_transport_release_pending(pending
);
974 /* The listen state only accepts connection requests. Reply with a
975 * reset unless we received a reset.
978 if (!(pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
||
979 pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)) {
980 vmci_transport_reply_reset(pkt
);
984 if (pkt
->u
.size
== 0) {
985 vmci_transport_reply_reset(pkt
);
989 /* If this socket can't accommodate this connection request, we send a
990 * reset. Otherwise we create and initialize a child socket and reply
991 * with a connection negotiation.
993 if (sk
->sk_ack_backlog
>= sk
->sk_max_ack_backlog
) {
994 vmci_transport_reply_reset(pkt
);
995 return -ECONNREFUSED
;
998 pending
= __vsock_create(sock_net(sk
), NULL
, sk
, GFP_KERNEL
,
1001 vmci_transport_send_reset(sk
, pkt
);
1005 vpending
= vsock_sk(pending
);
1007 vsock_addr_init(&vpending
->local_addr
, pkt
->dg
.dst
.context
,
1009 vsock_addr_init(&vpending
->remote_addr
, pkt
->dg
.src
.context
,
1012 /* If the proposed size fits within our min/max, accept it. Otherwise
1013 * propose our own size.
1015 if (pkt
->u
.size
>= vmci_trans(vpending
)->queue_pair_min_size
&&
1016 pkt
->u
.size
<= vmci_trans(vpending
)->queue_pair_max_size
) {
1017 qp_size
= pkt
->u
.size
;
1019 qp_size
= vmci_trans(vpending
)->queue_pair_size
;
1022 /* Figure out if we are using old or new requests based on the
1023 * overrides pkt types sent by our peer.
1025 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1026 old_request
= old_pkt_proto
;
1028 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
)
1030 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)
1031 old_request
= false;
1036 /* Handle a REQUEST (or override) */
1037 u16 version
= VSOCK_PROTO_INVALID
;
1038 if (vmci_transport_proto_to_notify_struct(
1039 pending
, &version
, true))
1040 err
= vmci_transport_send_negotiate(pending
, qp_size
);
1045 /* Handle a REQUEST2 (or override) */
1046 int proto_int
= pkt
->proto
;
1048 u16 active_proto_version
= 0;
1050 /* The list of possible protocols is the intersection of all
1051 * protocols the client supports ... plus all the protocols we
1054 proto_int
&= vmci_transport_new_proto_supported_versions();
1056 /* We choose the highest possible protocol version and use that
1059 pos
= fls(proto_int
);
1061 active_proto_version
= (1 << (pos
- 1));
1062 if (vmci_transport_proto_to_notify_struct(
1063 pending
, &active_proto_version
, false))
1064 err
= vmci_transport_send_negotiate2(pending
,
1066 active_proto_version
);
1076 vmci_transport_send_reset(sk
, pkt
);
1078 err
= vmci_transport_error_to_vsock_error(err
);
1082 vsock_add_pending(sk
, pending
);
1083 sk
->sk_ack_backlog
++;
1085 pending
->sk_state
= SS_CONNECTING
;
1086 vmci_trans(vpending
)->produce_size
=
1087 vmci_trans(vpending
)->consume_size
= qp_size
;
1088 vmci_trans(vpending
)->queue_pair_size
= qp_size
;
1090 vmci_trans(vpending
)->notify_ops
->process_request(pending
);
1092 /* We might never receive another message for this socket and it's not
1093 * connected to any process, so we have to ensure it gets cleaned up
1094 * ourself. Our delayed work function will take care of that. Note
1095 * that we do not ever cancel this function since we have few
1096 * guarantees about its state when calling cancel_delayed_work().
1097 * Instead we hold a reference on the socket for that function and make
1098 * it capable of handling cases where it needs to do nothing but
1099 * release that reference.
1101 vpending
->listener
= sk
;
1104 INIT_DELAYED_WORK(&vpending
->dwork
, vsock_pending_work
);
1105 schedule_delayed_work(&vpending
->dwork
, HZ
);
1112 vmci_transport_recv_connecting_server(struct sock
*listener
,
1113 struct sock
*pending
,
1114 struct vmci_transport_packet
*pkt
)
1116 struct vsock_sock
*vpending
;
1117 struct vmci_handle handle
;
1118 struct vmci_qp
*qpair
;
1125 vpending
= vsock_sk(pending
);
1126 detach_sub_id
= VMCI_INVALID_ID
;
1128 switch (pkt
->type
) {
1129 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
1130 if (vmci_handle_is_invalid(pkt
->u
.handle
)) {
1131 vmci_transport_send_reset(pending
, pkt
);
1138 /* Close and cleanup the connection. */
1139 vmci_transport_send_reset(pending
, pkt
);
1141 err
= pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
? 0 : -EINVAL
;
1145 /* In order to complete the connection we need to attach to the offered
1146 * queue pair and send an attach notification. We also subscribe to the
1147 * detach event so we know when our peer goes away, and we do that
1148 * before attaching so we don't miss an event. If all this succeeds,
1149 * we update our state and wakeup anything waiting in accept() for a
1153 /* We don't care about attach since we ensure the other side has
1154 * attached by specifying the ATTACH_ONLY flag below.
1156 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1157 vmci_transport_peer_detach_cb
,
1158 vmci_trans(vpending
), &detach_sub_id
);
1159 if (err
< VMCI_SUCCESS
) {
1160 vmci_transport_send_reset(pending
, pkt
);
1161 err
= vmci_transport_error_to_vsock_error(err
);
1166 vmci_trans(vpending
)->detach_sub_id
= detach_sub_id
;
1168 /* Now attach to the queue pair the client created. */
1169 handle
= pkt
->u
.handle
;
1171 /* vpending->local_addr always has a context id so we do not need to
1172 * worry about VMADDR_CID_ANY in this case.
1175 vpending
->remote_addr
.svm_cid
== vpending
->local_addr
.svm_cid
;
1176 flags
= VMCI_QPFLAG_ATTACH_ONLY
;
1177 flags
|= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1179 err
= vmci_transport_queue_pair_alloc(
1182 vmci_trans(vpending
)->produce_size
,
1183 vmci_trans(vpending
)->consume_size
,
1184 pkt
->dg
.src
.context
,
1186 vmci_transport_is_trusted(
1188 vpending
->remote_addr
.svm_cid
));
1190 vmci_transport_send_reset(pending
, pkt
);
1195 vmci_trans(vpending
)->qp_handle
= handle
;
1196 vmci_trans(vpending
)->qpair
= qpair
;
1198 /* When we send the attach message, we must be ready to handle incoming
1199 * control messages on the newly connected socket. So we move the
1200 * pending socket to the connected state before sending the attach
1201 * message. Otherwise, an incoming packet triggered by the attach being
1202 * received by the peer may be processed concurrently with what happens
1203 * below after sending the attach message, and that incoming packet
1204 * will find the listening socket instead of the (currently) pending
1205 * socket. Note that enqueueing the socket increments the reference
1206 * count, so even if a reset comes before the connection is accepted,
1207 * the socket will be valid until it is removed from the queue.
1209 * If we fail sending the attach below, we remove the socket from the
1210 * connected list and move the socket to SS_UNCONNECTED before
1211 * releasing the lock, so a pending slow path processing of an incoming
1212 * packet will not see the socket in the connected state in that case.
1214 pending
->sk_state
= SS_CONNECTED
;
1216 vsock_insert_connected(vpending
);
1218 /* Notify our peer of our attach. */
1219 err
= vmci_transport_send_attach(pending
, handle
);
1221 vsock_remove_connected(vpending
);
1222 pr_err("Could not send attach\n");
1223 vmci_transport_send_reset(pending
, pkt
);
1224 err
= vmci_transport_error_to_vsock_error(err
);
1229 /* We have a connection. Move the now connected socket from the
1230 * listener's pending list to the accept queue so callers of accept()
1233 vsock_remove_pending(listener
, pending
);
1234 vsock_enqueue_accept(listener
, pending
);
1236 /* Callers of accept() will be be waiting on the listening socket, not
1237 * the pending socket.
1239 listener
->sk_state_change(listener
);
1244 pending
->sk_err
= skerr
;
1245 pending
->sk_state
= SS_UNCONNECTED
;
1246 /* As long as we drop our reference, all necessary cleanup will handle
1247 * when the cleanup function drops its reference and our destruct
1248 * implementation is called. Note that since the listen handler will
1249 * remove pending from the pending list upon our failure, the cleanup
1250 * function won't drop the additional reference, which is why we do it
1259 vmci_transport_recv_connecting_client(struct sock
*sk
,
1260 struct vmci_transport_packet
*pkt
)
1262 struct vsock_sock
*vsk
;
1268 switch (pkt
->type
) {
1269 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
1270 if (vmci_handle_is_invalid(pkt
->u
.handle
) ||
1271 !vmci_handle_is_equal(pkt
->u
.handle
,
1272 vmci_trans(vsk
)->qp_handle
)) {
1278 /* Signify the socket is connected and wakeup the waiter in
1279 * connect(). Also place the socket in the connected table for
1280 * accounting (it can already be found since it's in the bound
1283 sk
->sk_state
= SS_CONNECTED
;
1284 sk
->sk_socket
->state
= SS_CONNECTED
;
1285 vsock_insert_connected(vsk
);
1286 sk
->sk_state_change(sk
);
1289 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
1290 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
1291 if (pkt
->u
.size
== 0
1292 || pkt
->dg
.src
.context
!= vsk
->remote_addr
.svm_cid
1293 || pkt
->src_port
!= vsk
->remote_addr
.svm_port
1294 || !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)
1295 || vmci_trans(vsk
)->qpair
1296 || vmci_trans(vsk
)->produce_size
!= 0
1297 || vmci_trans(vsk
)->consume_size
!= 0
1298 || vmci_trans(vsk
)->detach_sub_id
!= VMCI_INVALID_ID
) {
1305 err
= vmci_transport_recv_connecting_client_negotiate(sk
, pkt
);
1312 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
1313 err
= vmci_transport_recv_connecting_client_invalid(sk
, pkt
);
1320 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1321 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1322 * continue processing here after they sent an INVALID packet.
1323 * This meant that we got a RST after the INVALID. We ignore a
1324 * RST after an INVALID. The common code doesn't send the RST
1325 * ... so we can hang if an old version of the common code
1326 * fails between getting a REQUEST and sending an OFFER back.
1327 * Not much we can do about it... except hope that it doesn't
1330 if (vsk
->ignore_connecting_rst
) {
1331 vsk
->ignore_connecting_rst
= false;
1340 /* Close and cleanup the connection. */
1349 vmci_transport_send_reset(sk
, pkt
);
1351 sk
->sk_state
= SS_UNCONNECTED
;
1353 sk
->sk_error_report(sk
);
1357 static int vmci_transport_recv_connecting_client_negotiate(
1359 struct vmci_transport_packet
*pkt
)
1362 struct vsock_sock
*vsk
;
1363 struct vmci_handle handle
;
1364 struct vmci_qp
*qpair
;
1368 bool old_proto
= true;
1373 handle
= VMCI_INVALID_HANDLE
;
1374 detach_sub_id
= VMCI_INVALID_ID
;
1376 /* If we have gotten here then we should be past the point where old
1377 * linux vsock could have sent the bogus rst.
1379 vsk
->sent_request
= false;
1380 vsk
->ignore_connecting_rst
= false;
1382 /* Verify that we're OK with the proposed queue pair size */
1383 if (pkt
->u
.size
< vmci_trans(vsk
)->queue_pair_min_size
||
1384 pkt
->u
.size
> vmci_trans(vsk
)->queue_pair_max_size
) {
1389 /* At this point we know the CID the peer is using to talk to us. */
1391 if (vsk
->local_addr
.svm_cid
== VMADDR_CID_ANY
)
1392 vsk
->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
1394 /* Setup the notify ops to be the highest supported version that both
1395 * the server and the client support.
1398 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1399 old_proto
= old_pkt_proto
;
1401 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
)
1403 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
)
1409 version
= VSOCK_PROTO_INVALID
;
1411 version
= pkt
->proto
;
1413 if (!vmci_transport_proto_to_notify_struct(sk
, &version
, old_proto
)) {
1418 /* Subscribe to detach events first.
1420 * XXX We attach once for each queue pair created for now so it is easy
1421 * to find the socket (it's provided), but later we should only
1422 * subscribe once and add a way to lookup sockets by queue pair handle.
1424 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1425 vmci_transport_peer_detach_cb
,
1426 vmci_trans(vsk
), &detach_sub_id
);
1427 if (err
< VMCI_SUCCESS
) {
1428 err
= vmci_transport_error_to_vsock_error(err
);
1432 /* Make VMCI select the handle for us. */
1433 handle
= VMCI_INVALID_HANDLE
;
1434 is_local
= vsk
->remote_addr
.svm_cid
== vsk
->local_addr
.svm_cid
;
1435 flags
= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1437 err
= vmci_transport_queue_pair_alloc(&qpair
,
1441 vsk
->remote_addr
.svm_cid
,
1443 vmci_transport_is_trusted(
1446 remote_addr
.svm_cid
));
1450 err
= vmci_transport_send_qp_offer(sk
, handle
);
1452 err
= vmci_transport_error_to_vsock_error(err
);
1456 vmci_trans(vsk
)->qp_handle
= handle
;
1457 vmci_trans(vsk
)->qpair
= qpair
;
1459 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
=
1462 vmci_trans(vsk
)->detach_sub_id
= detach_sub_id
;
1464 vmci_trans(vsk
)->notify_ops
->process_negotiate(sk
);
1469 if (detach_sub_id
!= VMCI_INVALID_ID
)
1470 vmci_event_unsubscribe(detach_sub_id
);
1472 if (!vmci_handle_is_invalid(handle
))
1473 vmci_qpair_detach(&qpair
);
1479 vmci_transport_recv_connecting_client_invalid(struct sock
*sk
,
1480 struct vmci_transport_packet
*pkt
)
1483 struct vsock_sock
*vsk
= vsock_sk(sk
);
1485 if (vsk
->sent_request
) {
1486 vsk
->sent_request
= false;
1487 vsk
->ignore_connecting_rst
= true;
1489 err
= vmci_transport_send_conn_request(
1490 sk
, vmci_trans(vsk
)->queue_pair_size
);
1492 err
= vmci_transport_error_to_vsock_error(err
);
1501 static int vmci_transport_recv_connected(struct sock
*sk
,
1502 struct vmci_transport_packet
*pkt
)
1504 struct vsock_sock
*vsk
;
1505 bool pkt_processed
= false;
1507 /* In cases where we are closing the connection, it's sufficient to
1508 * mark the state change (and maybe error) and wake up any waiting
1509 * threads. Since this is a connected socket, it's owned by a user
1510 * process and will be cleaned up when the failure is passed back on
1511 * the current or next system call. Our system call implementations
1512 * must therefore check for error and state changes on entry and when
1515 switch (pkt
->type
) {
1516 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
1520 vsk
->peer_shutdown
|= pkt
->u
.mode
;
1521 sk
->sk_state_change(sk
);
1525 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1527 /* It is possible that we sent our peer a message (e.g a
1528 * WAITING_READ) right before we got notified that the peer had
1529 * detached. If that happens then we can get a RST pkt back
1530 * from our peer even though there is data available for us to
1531 * read. In that case, don't shutdown the socket completely but
1532 * instead allow the local client to finish reading data off
1533 * the queuepair. Always treat a RST pkt in connected mode like
1536 sock_set_flag(sk
, SOCK_DONE
);
1537 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
1538 if (vsock_stream_has_data(vsk
) <= 0)
1539 sk
->sk_state
= SS_DISCONNECTING
;
1541 sk
->sk_state_change(sk
);
1546 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
1547 sk
, pkt
, false, NULL
, NULL
,
1558 static int vmci_transport_socket_init(struct vsock_sock
*vsk
,
1559 struct vsock_sock
*psk
)
1561 vsk
->trans
= kmalloc(sizeof(struct vmci_transport
), GFP_KERNEL
);
1565 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1566 vmci_trans(vsk
)->qp_handle
= VMCI_INVALID_HANDLE
;
1567 vmci_trans(vsk
)->qpair
= NULL
;
1568 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
= 0;
1569 vmci_trans(vsk
)->detach_sub_id
= VMCI_INVALID_ID
;
1570 vmci_trans(vsk
)->notify_ops
= NULL
;
1571 INIT_LIST_HEAD(&vmci_trans(vsk
)->elem
);
1572 vmci_trans(vsk
)->sk
= &vsk
->sk
;
1573 spin_lock_init(&vmci_trans(vsk
)->lock
);
1575 vmci_trans(vsk
)->queue_pair_size
=
1576 vmci_trans(psk
)->queue_pair_size
;
1577 vmci_trans(vsk
)->queue_pair_min_size
=
1578 vmci_trans(psk
)->queue_pair_min_size
;
1579 vmci_trans(vsk
)->queue_pair_max_size
=
1580 vmci_trans(psk
)->queue_pair_max_size
;
1582 vmci_trans(vsk
)->queue_pair_size
=
1583 VMCI_TRANSPORT_DEFAULT_QP_SIZE
;
1584 vmci_trans(vsk
)->queue_pair_min_size
=
1585 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN
;
1586 vmci_trans(vsk
)->queue_pair_max_size
=
1587 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX
;
1593 static void vmci_transport_free_resources(struct list_head
*transport_list
)
1595 while (!list_empty(transport_list
)) {
1596 struct vmci_transport
*transport
=
1597 list_first_entry(transport_list
, struct vmci_transport
,
1599 list_del(&transport
->elem
);
1601 if (transport
->detach_sub_id
!= VMCI_INVALID_ID
) {
1602 vmci_event_unsubscribe(transport
->detach_sub_id
);
1603 transport
->detach_sub_id
= VMCI_INVALID_ID
;
1606 if (!vmci_handle_is_invalid(transport
->qp_handle
)) {
1607 vmci_qpair_detach(&transport
->qpair
);
1608 transport
->qp_handle
= VMCI_INVALID_HANDLE
;
1609 transport
->produce_size
= 0;
1610 transport
->consume_size
= 0;
1617 static void vmci_transport_cleanup(struct work_struct
*work
)
1621 spin_lock_bh(&vmci_transport_cleanup_lock
);
1622 list_replace_init(&vmci_transport_cleanup_list
, &pending
);
1623 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1624 vmci_transport_free_resources(&pending
);
1627 static void vmci_transport_destruct(struct vsock_sock
*vsk
)
1629 /* Ensure that the detach callback doesn't use the sk/vsk
1630 * we are about to destruct.
1632 spin_lock_bh(&vmci_trans(vsk
)->lock
);
1633 vmci_trans(vsk
)->sk
= NULL
;
1634 spin_unlock_bh(&vmci_trans(vsk
)->lock
);
1636 if (vmci_trans(vsk
)->notify_ops
)
1637 vmci_trans(vsk
)->notify_ops
->socket_destruct(vsk
);
1639 spin_lock_bh(&vmci_transport_cleanup_lock
);
1640 list_add(&vmci_trans(vsk
)->elem
, &vmci_transport_cleanup_list
);
1641 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1642 schedule_work(&vmci_transport_cleanup_work
);
1647 static void vmci_transport_release(struct vsock_sock
*vsk
)
1649 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->dg_handle
)) {
1650 vmci_datagram_destroy_handle(vmci_trans(vsk
)->dg_handle
);
1651 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1655 static int vmci_transport_dgram_bind(struct vsock_sock
*vsk
,
1656 struct sockaddr_vm
*addr
)
1662 /* VMCI will select a resource ID for us if we provide
1665 port
= addr
->svm_port
== VMADDR_PORT_ANY
?
1666 VMCI_INVALID_ID
: addr
->svm_port
;
1668 if (port
<= LAST_RESERVED_PORT
&& !capable(CAP_NET_BIND_SERVICE
))
1671 flags
= addr
->svm_cid
== VMADDR_CID_ANY
?
1672 VMCI_FLAG_ANYCID_DG_HND
: 0;
1674 err
= vmci_transport_datagram_create_hnd(port
, flags
,
1675 vmci_transport_recv_dgram_cb
,
1677 &vmci_trans(vsk
)->dg_handle
);
1678 if (err
< VMCI_SUCCESS
)
1679 return vmci_transport_error_to_vsock_error(err
);
1680 vsock_addr_init(&vsk
->local_addr
, addr
->svm_cid
,
1681 vmci_trans(vsk
)->dg_handle
.resource
);
1686 static int vmci_transport_dgram_enqueue(
1687 struct vsock_sock
*vsk
,
1688 struct sockaddr_vm
*remote_addr
,
1693 struct vmci_datagram
*dg
;
1695 if (len
> VMCI_MAX_DG_PAYLOAD_SIZE
)
1698 if (!vmci_transport_allow_dgram(vsk
, remote_addr
->svm_cid
))
1701 /* Allocate a buffer for the user's message and our packet header. */
1702 dg
= kmalloc(len
+ sizeof(*dg
), GFP_KERNEL
);
1706 memcpy_from_msg(VMCI_DG_PAYLOAD(dg
), msg
, len
);
1708 dg
->dst
= vmci_make_handle(remote_addr
->svm_cid
,
1709 remote_addr
->svm_port
);
1710 dg
->src
= vmci_make_handle(vsk
->local_addr
.svm_cid
,
1711 vsk
->local_addr
.svm_port
);
1712 dg
->payload_size
= len
;
1714 err
= vmci_datagram_send(dg
);
1717 return vmci_transport_error_to_vsock_error(err
);
1719 return err
- sizeof(*dg
);
1722 static int vmci_transport_dgram_dequeue(struct vsock_sock
*vsk
,
1723 struct msghdr
*msg
, size_t len
,
1728 struct vmci_datagram
*dg
;
1730 struct sk_buff
*skb
;
1732 noblock
= flags
& MSG_DONTWAIT
;
1734 if (flags
& MSG_OOB
|| flags
& MSG_ERRQUEUE
)
1737 /* Retrieve the head sk_buff from the socket's receive queue. */
1739 skb
= skb_recv_datagram(&vsk
->sk
, flags
, noblock
, &err
);
1746 dg
= (struct vmci_datagram
*)skb
->data
;
1748 /* err is 0, meaning we read zero bytes. */
1751 payload_len
= dg
->payload_size
;
1752 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1753 if (payload_len
!= skb
->len
- sizeof(*dg
)) {
1758 if (payload_len
> len
) {
1760 msg
->msg_flags
|= MSG_TRUNC
;
1763 /* Place the datagram payload in the user's iovec. */
1764 err
= skb_copy_datagram_msg(skb
, sizeof(*dg
), msg
, payload_len
);
1768 if (msg
->msg_name
) {
1769 /* Provide the address of the sender. */
1770 DECLARE_SOCKADDR(struct sockaddr_vm
*, vm_addr
, msg
->msg_name
);
1771 vsock_addr_init(vm_addr
, dg
->src
.context
, dg
->src
.resource
);
1772 msg
->msg_namelen
= sizeof(*vm_addr
);
1777 skb_free_datagram(&vsk
->sk
, skb
);
1781 static bool vmci_transport_dgram_allow(u32 cid
, u32 port
)
1783 if (cid
== VMADDR_CID_HYPERVISOR
) {
1784 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1785 * state and are allowed.
1787 return port
== VMCI_UNITY_PBRPC_REGISTER
;
1793 static int vmci_transport_connect(struct vsock_sock
*vsk
)
1796 bool old_pkt_proto
= false;
1797 struct sock
*sk
= &vsk
->sk
;
1799 if (vmci_transport_old_proto_override(&old_pkt_proto
) &&
1801 err
= vmci_transport_send_conn_request(
1802 sk
, vmci_trans(vsk
)->queue_pair_size
);
1804 sk
->sk_state
= SS_UNCONNECTED
;
1808 int supported_proto_versions
=
1809 vmci_transport_new_proto_supported_versions();
1810 err
= vmci_transport_send_conn_request2(
1811 sk
, vmci_trans(vsk
)->queue_pair_size
,
1812 supported_proto_versions
);
1814 sk
->sk_state
= SS_UNCONNECTED
;
1818 vsk
->sent_request
= true;
1824 static ssize_t
vmci_transport_stream_dequeue(
1825 struct vsock_sock
*vsk
,
1830 if (flags
& MSG_PEEK
)
1831 return vmci_qpair_peekv(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1833 return vmci_qpair_dequev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1836 static ssize_t
vmci_transport_stream_enqueue(
1837 struct vsock_sock
*vsk
,
1841 return vmci_qpair_enquev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1844 static s64
vmci_transport_stream_has_data(struct vsock_sock
*vsk
)
1846 return vmci_qpair_consume_buf_ready(vmci_trans(vsk
)->qpair
);
1849 static s64
vmci_transport_stream_has_space(struct vsock_sock
*vsk
)
1851 return vmci_qpair_produce_free_space(vmci_trans(vsk
)->qpair
);
1854 static u64
vmci_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
1856 return vmci_trans(vsk
)->consume_size
;
1859 static bool vmci_transport_stream_is_active(struct vsock_sock
*vsk
)
1861 return !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
);
1864 static u64
vmci_transport_get_buffer_size(struct vsock_sock
*vsk
)
1866 return vmci_trans(vsk
)->queue_pair_size
;
1869 static u64
vmci_transport_get_min_buffer_size(struct vsock_sock
*vsk
)
1871 return vmci_trans(vsk
)->queue_pair_min_size
;
1874 static u64
vmci_transport_get_max_buffer_size(struct vsock_sock
*vsk
)
1876 return vmci_trans(vsk
)->queue_pair_max_size
;
1879 static void vmci_transport_set_buffer_size(struct vsock_sock
*vsk
, u64 val
)
1881 if (val
< vmci_trans(vsk
)->queue_pair_min_size
)
1882 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1883 if (val
> vmci_trans(vsk
)->queue_pair_max_size
)
1884 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1885 vmci_trans(vsk
)->queue_pair_size
= val
;
1888 static void vmci_transport_set_min_buffer_size(struct vsock_sock
*vsk
,
1891 if (val
> vmci_trans(vsk
)->queue_pair_size
)
1892 vmci_trans(vsk
)->queue_pair_size
= val
;
1893 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1896 static void vmci_transport_set_max_buffer_size(struct vsock_sock
*vsk
,
1899 if (val
< vmci_trans(vsk
)->queue_pair_size
)
1900 vmci_trans(vsk
)->queue_pair_size
= val
;
1901 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1904 static int vmci_transport_notify_poll_in(
1905 struct vsock_sock
*vsk
,
1907 bool *data_ready_now
)
1909 return vmci_trans(vsk
)->notify_ops
->poll_in(
1910 &vsk
->sk
, target
, data_ready_now
);
1913 static int vmci_transport_notify_poll_out(
1914 struct vsock_sock
*vsk
,
1916 bool *space_available_now
)
1918 return vmci_trans(vsk
)->notify_ops
->poll_out(
1919 &vsk
->sk
, target
, space_available_now
);
1922 static int vmci_transport_notify_recv_init(
1923 struct vsock_sock
*vsk
,
1925 struct vsock_transport_recv_notify_data
*data
)
1927 return vmci_trans(vsk
)->notify_ops
->recv_init(
1929 (struct vmci_transport_recv_notify_data
*)data
);
1932 static int vmci_transport_notify_recv_pre_block(
1933 struct vsock_sock
*vsk
,
1935 struct vsock_transport_recv_notify_data
*data
)
1937 return vmci_trans(vsk
)->notify_ops
->recv_pre_block(
1939 (struct vmci_transport_recv_notify_data
*)data
);
1942 static int vmci_transport_notify_recv_pre_dequeue(
1943 struct vsock_sock
*vsk
,
1945 struct vsock_transport_recv_notify_data
*data
)
1947 return vmci_trans(vsk
)->notify_ops
->recv_pre_dequeue(
1949 (struct vmci_transport_recv_notify_data
*)data
);
1952 static int vmci_transport_notify_recv_post_dequeue(
1953 struct vsock_sock
*vsk
,
1957 struct vsock_transport_recv_notify_data
*data
)
1959 return vmci_trans(vsk
)->notify_ops
->recv_post_dequeue(
1960 &vsk
->sk
, target
, copied
, data_read
,
1961 (struct vmci_transport_recv_notify_data
*)data
);
1964 static int vmci_transport_notify_send_init(
1965 struct vsock_sock
*vsk
,
1966 struct vsock_transport_send_notify_data
*data
)
1968 return vmci_trans(vsk
)->notify_ops
->send_init(
1970 (struct vmci_transport_send_notify_data
*)data
);
1973 static int vmci_transport_notify_send_pre_block(
1974 struct vsock_sock
*vsk
,
1975 struct vsock_transport_send_notify_data
*data
)
1977 return vmci_trans(vsk
)->notify_ops
->send_pre_block(
1979 (struct vmci_transport_send_notify_data
*)data
);
1982 static int vmci_transport_notify_send_pre_enqueue(
1983 struct vsock_sock
*vsk
,
1984 struct vsock_transport_send_notify_data
*data
)
1986 return vmci_trans(vsk
)->notify_ops
->send_pre_enqueue(
1988 (struct vmci_transport_send_notify_data
*)data
);
1991 static int vmci_transport_notify_send_post_enqueue(
1992 struct vsock_sock
*vsk
,
1994 struct vsock_transport_send_notify_data
*data
)
1996 return vmci_trans(vsk
)->notify_ops
->send_post_enqueue(
1998 (struct vmci_transport_send_notify_data
*)data
);
2001 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
)
2003 if (PROTOCOL_OVERRIDE
!= -1) {
2004 if (PROTOCOL_OVERRIDE
== 0)
2005 *old_pkt_proto
= true;
2007 *old_pkt_proto
= false;
2009 pr_info("Proto override in use\n");
2016 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
,
2020 struct vsock_sock
*vsk
= vsock_sk(sk
);
2022 if (old_pkt_proto
) {
2023 if (*proto
!= VSOCK_PROTO_INVALID
) {
2024 pr_err("Can't set both an old and new protocol\n");
2027 vmci_trans(vsk
)->notify_ops
= &vmci_transport_notify_pkt_ops
;
2032 case VSOCK_PROTO_PKT_ON_NOTIFY
:
2033 vmci_trans(vsk
)->notify_ops
=
2034 &vmci_transport_notify_pkt_q_state_ops
;
2037 pr_err("Unknown notify protocol version\n");
2042 vmci_trans(vsk
)->notify_ops
->socket_init(sk
);
2046 static u16
vmci_transport_new_proto_supported_versions(void)
2048 if (PROTOCOL_OVERRIDE
!= -1)
2049 return PROTOCOL_OVERRIDE
;
2051 return VSOCK_PROTO_ALL_SUPPORTED
;
2054 static u32
vmci_transport_get_local_cid(void)
2056 return vmci_get_context_id();
2059 static struct vsock_transport vmci_transport
= {
2060 .init
= vmci_transport_socket_init
,
2061 .destruct
= vmci_transport_destruct
,
2062 .release
= vmci_transport_release
,
2063 .connect
= vmci_transport_connect
,
2064 .dgram_bind
= vmci_transport_dgram_bind
,
2065 .dgram_dequeue
= vmci_transport_dgram_dequeue
,
2066 .dgram_enqueue
= vmci_transport_dgram_enqueue
,
2067 .dgram_allow
= vmci_transport_dgram_allow
,
2068 .stream_dequeue
= vmci_transport_stream_dequeue
,
2069 .stream_enqueue
= vmci_transport_stream_enqueue
,
2070 .stream_has_data
= vmci_transport_stream_has_data
,
2071 .stream_has_space
= vmci_transport_stream_has_space
,
2072 .stream_rcvhiwat
= vmci_transport_stream_rcvhiwat
,
2073 .stream_is_active
= vmci_transport_stream_is_active
,
2074 .stream_allow
= vmci_transport_stream_allow
,
2075 .notify_poll_in
= vmci_transport_notify_poll_in
,
2076 .notify_poll_out
= vmci_transport_notify_poll_out
,
2077 .notify_recv_init
= vmci_transport_notify_recv_init
,
2078 .notify_recv_pre_block
= vmci_transport_notify_recv_pre_block
,
2079 .notify_recv_pre_dequeue
= vmci_transport_notify_recv_pre_dequeue
,
2080 .notify_recv_post_dequeue
= vmci_transport_notify_recv_post_dequeue
,
2081 .notify_send_init
= vmci_transport_notify_send_init
,
2082 .notify_send_pre_block
= vmci_transport_notify_send_pre_block
,
2083 .notify_send_pre_enqueue
= vmci_transport_notify_send_pre_enqueue
,
2084 .notify_send_post_enqueue
= vmci_transport_notify_send_post_enqueue
,
2085 .shutdown
= vmci_transport_shutdown
,
2086 .set_buffer_size
= vmci_transport_set_buffer_size
,
2087 .set_min_buffer_size
= vmci_transport_set_min_buffer_size
,
2088 .set_max_buffer_size
= vmci_transport_set_max_buffer_size
,
2089 .get_buffer_size
= vmci_transport_get_buffer_size
,
2090 .get_min_buffer_size
= vmci_transport_get_min_buffer_size
,
2091 .get_max_buffer_size
= vmci_transport_get_max_buffer_size
,
2092 .get_local_cid
= vmci_transport_get_local_cid
,
2095 static int __init
vmci_transport_init(void)
2099 /* Create the datagram handle that we will use to send and receive all
2100 * VSocket control messages for this context.
2102 err
= vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID
,
2103 VMCI_FLAG_ANYCID_DG_HND
,
2104 vmci_transport_recv_stream_cb
,
2106 &vmci_transport_stream_handle
);
2107 if (err
< VMCI_SUCCESS
) {
2108 pr_err("Unable to create datagram handle. (%d)\n", err
);
2109 return vmci_transport_error_to_vsock_error(err
);
2112 err
= vmci_event_subscribe(VMCI_EVENT_QP_RESUMED
,
2113 vmci_transport_qp_resumed_cb
,
2114 NULL
, &vmci_transport_qp_resumed_sub_id
);
2115 if (err
< VMCI_SUCCESS
) {
2116 pr_err("Unable to subscribe to resumed event. (%d)\n", err
);
2117 err
= vmci_transport_error_to_vsock_error(err
);
2118 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2119 goto err_destroy_stream_handle
;
2122 err
= vsock_core_init(&vmci_transport
);
2124 goto err_unsubscribe
;
2129 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2130 err_destroy_stream_handle
:
2131 vmci_datagram_destroy_handle(vmci_transport_stream_handle
);
2134 module_init(vmci_transport_init
);
2136 static void __exit
vmci_transport_exit(void)
2138 cancel_work_sync(&vmci_transport_cleanup_work
);
2139 vmci_transport_free_resources(&vmci_transport_cleanup_list
);
2141 if (!vmci_handle_is_invalid(vmci_transport_stream_handle
)) {
2142 if (vmci_datagram_destroy_handle(
2143 vmci_transport_stream_handle
) != VMCI_SUCCESS
)
2144 pr_err("Couldn't destroy datagram handle\n");
2145 vmci_transport_stream_handle
= VMCI_INVALID_HANDLE
;
2148 if (vmci_transport_qp_resumed_sub_id
!= VMCI_INVALID_ID
) {
2149 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2150 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2155 module_exit(vmci_transport_exit
);
2157 MODULE_AUTHOR("VMware, Inc.");
2158 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2159 MODULE_VERSION("1.0.2.0-k");
2160 MODULE_LICENSE("GPL v2");
2161 MODULE_ALIAS("vmware_vsock");
2162 MODULE_ALIAS_NETPROTO(PF_VSOCK
);