2 * VMware vSockets Driver
4 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation version 2 and no later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/types.h>
17 #include <linux/bitops.h>
18 #include <linux/cred.h>
19 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/kmod.h>
23 #include <linux/list.h>
24 #include <linux/miscdevice.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/net.h>
28 #include <linux/poll.h>
29 #include <linux/skbuff.h>
30 #include <linux/smp.h>
31 #include <linux/socket.h>
32 #include <linux/stddef.h>
33 #include <linux/unistd.h>
34 #include <linux/wait.h>
35 #include <linux/workqueue.h>
37 #include <net/af_vsock.h>
39 #include "vmci_transport_notify.h"
41 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
);
42 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
);
43 static void vmci_transport_peer_detach_cb(u32 sub_id
,
44 const struct vmci_event_data
*ed
,
46 static void vmci_transport_recv_pkt_work(struct work_struct
*work
);
47 static void vmci_transport_cleanup(struct work_struct
*work
);
48 static int vmci_transport_recv_listen(struct sock
*sk
,
49 struct vmci_transport_packet
*pkt
);
50 static int vmci_transport_recv_connecting_server(
53 struct vmci_transport_packet
*pkt
);
54 static int vmci_transport_recv_connecting_client(
56 struct vmci_transport_packet
*pkt
);
57 static int vmci_transport_recv_connecting_client_negotiate(
59 struct vmci_transport_packet
*pkt
);
60 static int vmci_transport_recv_connecting_client_invalid(
62 struct vmci_transport_packet
*pkt
);
63 static int vmci_transport_recv_connected(struct sock
*sk
,
64 struct vmci_transport_packet
*pkt
);
65 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
);
66 static u16
vmci_transport_new_proto_supported_versions(void);
67 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
, u16
*proto
,
70 struct vmci_transport_recv_pkt_info
{
71 struct work_struct work
;
73 struct vmci_transport_packet pkt
;
76 static LIST_HEAD(vmci_transport_cleanup_list
);
77 static DEFINE_SPINLOCK(vmci_transport_cleanup_lock
);
78 static DECLARE_WORK(vmci_transport_cleanup_work
, vmci_transport_cleanup
);
80 static struct vmci_handle vmci_transport_stream_handle
= { VMCI_INVALID_ID
,
82 static u32 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
84 static int PROTOCOL_OVERRIDE
= -1;
86 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN 128
87 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE 262144
88 #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX 262144
90 /* The default peer timeout indicates how long we will wait for a peer response
91 * to a control message.
93 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
95 /* Helper function to convert from a VMCI error code to a VSock error code. */
97 static s32
vmci_transport_error_to_vsock_error(s32 vmci_error
)
101 switch (vmci_error
) {
102 case VMCI_ERROR_NO_MEM
:
105 case VMCI_ERROR_DUPLICATE_ENTRY
:
106 case VMCI_ERROR_ALREADY_EXISTS
:
109 case VMCI_ERROR_NO_ACCESS
:
112 case VMCI_ERROR_NO_RESOURCES
:
115 case VMCI_ERROR_INVALID_RESOURCE
:
118 case VMCI_ERROR_INVALID_ARGS
:
123 return err
> 0 ? -err
: err
;
126 static u32
vmci_transport_peer_rid(u32 peer_cid
)
128 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
129 return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID
;
131 return VMCI_TRANSPORT_PACKET_RID
;
135 vmci_transport_packet_init(struct vmci_transport_packet
*pkt
,
136 struct sockaddr_vm
*src
,
137 struct sockaddr_vm
*dst
,
141 struct vmci_transport_waiting_info
*wait
,
143 struct vmci_handle handle
)
145 /* We register the stream control handler as an any cid handle so we
146 * must always send from a source address of VMADDR_CID_ANY
148 pkt
->dg
.src
= vmci_make_handle(VMADDR_CID_ANY
,
149 VMCI_TRANSPORT_PACKET_RID
);
150 pkt
->dg
.dst
= vmci_make_handle(dst
->svm_cid
,
151 vmci_transport_peer_rid(dst
->svm_cid
));
152 pkt
->dg
.payload_size
= sizeof(*pkt
) - sizeof(pkt
->dg
);
153 pkt
->version
= VMCI_TRANSPORT_PACKET_VERSION
;
155 pkt
->src_port
= src
->svm_port
;
156 pkt
->dst_port
= dst
->svm_port
;
157 memset(&pkt
->proto
, 0, sizeof(pkt
->proto
));
158 memset(&pkt
->_reserved2
, 0, sizeof(pkt
->_reserved2
));
161 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
165 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST
:
166 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
170 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
171 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
172 pkt
->u
.handle
= handle
;
175 case VMCI_TRANSPORT_PACKET_TYPE_WROTE
:
176 case VMCI_TRANSPORT_PACKET_TYPE_READ
:
177 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
181 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
185 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
:
186 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
:
187 memcpy(&pkt
->u
.wait
, wait
, sizeof(pkt
->u
.wait
));
190 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
:
191 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
199 vmci_transport_packet_get_addresses(struct vmci_transport_packet
*pkt
,
200 struct sockaddr_vm
*local
,
201 struct sockaddr_vm
*remote
)
203 vsock_addr_init(local
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
204 vsock_addr_init(remote
, pkt
->dg
.src
.context
, pkt
->src_port
);
208 __vmci_transport_send_control_pkt(struct vmci_transport_packet
*pkt
,
209 struct sockaddr_vm
*src
,
210 struct sockaddr_vm
*dst
,
211 enum vmci_transport_packet_type type
,
214 struct vmci_transport_waiting_info
*wait
,
216 struct vmci_handle handle
,
221 vmci_transport_packet_init(pkt
, src
, dst
, type
, size
, mode
, wait
,
223 err
= vmci_datagram_send(&pkt
->dg
);
224 if (convert_error
&& (err
< 0))
225 return vmci_transport_error_to_vsock_error(err
);
231 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet
*pkt
,
232 enum vmci_transport_packet_type type
,
235 struct vmci_transport_waiting_info
*wait
,
236 struct vmci_handle handle
)
238 struct vmci_transport_packet reply
;
239 struct sockaddr_vm src
, dst
;
241 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
) {
244 vmci_transport_packet_get_addresses(pkt
, &src
, &dst
);
245 return __vmci_transport_send_control_pkt(&reply
, &src
, &dst
,
254 vmci_transport_send_control_pkt_bh(struct sockaddr_vm
*src
,
255 struct sockaddr_vm
*dst
,
256 enum vmci_transport_packet_type type
,
259 struct vmci_transport_waiting_info
*wait
,
260 struct vmci_handle handle
)
262 /* Note that it is safe to use a single packet across all CPUs since
263 * two tasklets of the same type are guaranteed to not ever run
264 * simultaneously. If that ever changes, or VMCI stops using tasklets,
265 * we can use per-cpu packets.
267 static struct vmci_transport_packet pkt
;
269 return __vmci_transport_send_control_pkt(&pkt
, src
, dst
, type
,
271 VSOCK_PROTO_INVALID
, handle
,
276 vmci_transport_send_control_pkt(struct sock
*sk
,
277 enum vmci_transport_packet_type type
,
280 struct vmci_transport_waiting_info
*wait
,
282 struct vmci_handle handle
)
284 struct vmci_transport_packet
*pkt
;
285 struct vsock_sock
*vsk
;
290 if (!vsock_addr_bound(&vsk
->local_addr
))
293 if (!vsock_addr_bound(&vsk
->remote_addr
))
296 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
300 err
= __vmci_transport_send_control_pkt(pkt
, &vsk
->local_addr
,
301 &vsk
->remote_addr
, type
, size
,
302 mode
, wait
, proto
, handle
,
309 static int vmci_transport_send_reset_bh(struct sockaddr_vm
*dst
,
310 struct sockaddr_vm
*src
,
311 struct vmci_transport_packet
*pkt
)
313 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
315 return vmci_transport_send_control_pkt_bh(
317 VMCI_TRANSPORT_PACKET_TYPE_RST
, 0,
318 0, NULL
, VMCI_INVALID_HANDLE
);
321 static int vmci_transport_send_reset(struct sock
*sk
,
322 struct vmci_transport_packet
*pkt
)
324 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
326 return vmci_transport_send_control_pkt(sk
,
327 VMCI_TRANSPORT_PACKET_TYPE_RST
,
328 0, 0, NULL
, VSOCK_PROTO_INVALID
,
329 VMCI_INVALID_HANDLE
);
332 static int vmci_transport_send_negotiate(struct sock
*sk
, size_t size
)
334 return vmci_transport_send_control_pkt(
336 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
,
339 VMCI_INVALID_HANDLE
);
342 static int vmci_transport_send_negotiate2(struct sock
*sk
, size_t size
,
345 return vmci_transport_send_control_pkt(
347 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
,
348 size
, 0, NULL
, version
,
349 VMCI_INVALID_HANDLE
);
352 static int vmci_transport_send_qp_offer(struct sock
*sk
,
353 struct vmci_handle handle
)
355 return vmci_transport_send_control_pkt(
356 sk
, VMCI_TRANSPORT_PACKET_TYPE_OFFER
, 0,
358 VSOCK_PROTO_INVALID
, handle
);
361 static int vmci_transport_send_attach(struct sock
*sk
,
362 struct vmci_handle handle
)
364 return vmci_transport_send_control_pkt(
365 sk
, VMCI_TRANSPORT_PACKET_TYPE_ATTACH
,
366 0, 0, NULL
, VSOCK_PROTO_INVALID
,
370 static int vmci_transport_reply_reset(struct vmci_transport_packet
*pkt
)
372 return vmci_transport_reply_control_pkt_fast(
374 VMCI_TRANSPORT_PACKET_TYPE_RST
,
376 VMCI_INVALID_HANDLE
);
379 static int vmci_transport_send_invalid_bh(struct sockaddr_vm
*dst
,
380 struct sockaddr_vm
*src
)
382 return vmci_transport_send_control_pkt_bh(
384 VMCI_TRANSPORT_PACKET_TYPE_INVALID
,
385 0, 0, NULL
, VMCI_INVALID_HANDLE
);
388 int vmci_transport_send_wrote_bh(struct sockaddr_vm
*dst
,
389 struct sockaddr_vm
*src
)
391 return vmci_transport_send_control_pkt_bh(
393 VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
394 0, NULL
, VMCI_INVALID_HANDLE
);
397 int vmci_transport_send_read_bh(struct sockaddr_vm
*dst
,
398 struct sockaddr_vm
*src
)
400 return vmci_transport_send_control_pkt_bh(
402 VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
403 0, NULL
, VMCI_INVALID_HANDLE
);
406 int vmci_transport_send_wrote(struct sock
*sk
)
408 return vmci_transport_send_control_pkt(
409 sk
, VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
410 0, NULL
, VSOCK_PROTO_INVALID
,
411 VMCI_INVALID_HANDLE
);
414 int vmci_transport_send_read(struct sock
*sk
)
416 return vmci_transport_send_control_pkt(
417 sk
, VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
418 0, NULL
, VSOCK_PROTO_INVALID
,
419 VMCI_INVALID_HANDLE
);
422 int vmci_transport_send_waiting_write(struct sock
*sk
,
423 struct vmci_transport_waiting_info
*wait
)
425 return vmci_transport_send_control_pkt(
426 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
,
427 0, 0, wait
, VSOCK_PROTO_INVALID
,
428 VMCI_INVALID_HANDLE
);
431 int vmci_transport_send_waiting_read(struct sock
*sk
,
432 struct vmci_transport_waiting_info
*wait
)
434 return vmci_transport_send_control_pkt(
435 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
,
436 0, 0, wait
, VSOCK_PROTO_INVALID
,
437 VMCI_INVALID_HANDLE
);
440 static int vmci_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
442 return vmci_transport_send_control_pkt(
444 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
,
447 VMCI_INVALID_HANDLE
);
450 static int vmci_transport_send_conn_request(struct sock
*sk
, size_t size
)
452 return vmci_transport_send_control_pkt(sk
,
453 VMCI_TRANSPORT_PACKET_TYPE_REQUEST
,
456 VMCI_INVALID_HANDLE
);
459 static int vmci_transport_send_conn_request2(struct sock
*sk
, size_t size
,
462 return vmci_transport_send_control_pkt(
463 sk
, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
,
464 size
, 0, NULL
, version
,
465 VMCI_INVALID_HANDLE
);
468 static struct sock
*vmci_transport_get_pending(
469 struct sock
*listener
,
470 struct vmci_transport_packet
*pkt
)
472 struct vsock_sock
*vlistener
;
473 struct vsock_sock
*vpending
;
474 struct sock
*pending
;
475 struct sockaddr_vm src
;
477 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
479 vlistener
= vsock_sk(listener
);
481 list_for_each_entry(vpending
, &vlistener
->pending_links
,
483 if (vsock_addr_equals_addr(&src
, &vpending
->remote_addr
) &&
484 pkt
->dst_port
== vpending
->local_addr
.svm_port
) {
485 pending
= sk_vsock(vpending
);
497 static void vmci_transport_release_pending(struct sock
*pending
)
502 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
503 * trusted sockets 2) sockets from applications running as the same user as the
504 * VM (this is only true for the host side and only when using hosted products)
507 static bool vmci_transport_is_trusted(struct vsock_sock
*vsock
, u32 peer_cid
)
509 return vsock
->trusted
||
510 vmci_is_context_owner(peer_cid
, vsock
->owner
->uid
);
513 /* We allow sending datagrams to and receiving datagrams from a restricted VM
514 * only if it is trusted as described in vmci_transport_is_trusted.
517 static bool vmci_transport_allow_dgram(struct vsock_sock
*vsock
, u32 peer_cid
)
519 if (VMADDR_CID_HYPERVISOR
== peer_cid
)
522 if (vsock
->cached_peer
!= peer_cid
) {
523 vsock
->cached_peer
= peer_cid
;
524 if (!vmci_transport_is_trusted(vsock
, peer_cid
) &&
525 (vmci_context_get_priv_flags(peer_cid
) &
526 VMCI_PRIVILEGE_FLAG_RESTRICTED
)) {
527 vsock
->cached_peer_allow_dgram
= false;
529 vsock
->cached_peer_allow_dgram
= true;
533 return vsock
->cached_peer_allow_dgram
;
537 vmci_transport_queue_pair_alloc(struct vmci_qp
**qpair
,
538 struct vmci_handle
*handle
,
541 u32 peer
, u32 flags
, bool trusted
)
546 /* Try to allocate our queue pair as trusted. This will only
547 * work if vsock is running in the host.
550 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
,
553 VMCI_PRIVILEGE_FLAG_TRUSTED
);
554 if (err
!= VMCI_ERROR_NO_ACCESS
)
559 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
, consume_size
,
560 peer
, flags
, VMCI_NO_PRIVILEGE_FLAGS
);
563 pr_err("Could not attach to queue pair with %d\n",
565 err
= vmci_transport_error_to_vsock_error(err
);
572 vmci_transport_datagram_create_hnd(u32 resource_id
,
574 vmci_datagram_recv_cb recv_cb
,
576 struct vmci_handle
*out_handle
)
580 /* Try to allocate our datagram handler as trusted. This will only work
581 * if vsock is running in the host.
584 err
= vmci_datagram_create_handle_priv(resource_id
, flags
,
585 VMCI_PRIVILEGE_FLAG_TRUSTED
,
587 client_data
, out_handle
);
589 if (err
== VMCI_ERROR_NO_ACCESS
)
590 err
= vmci_datagram_create_handle(resource_id
, flags
,
591 recv_cb
, client_data
,
597 /* This is invoked as part of a tasklet that's scheduled when the VMCI
598 * interrupt fires. This is run in bottom-half context and if it ever needs to
599 * sleep it should defer that work to a work queue.
602 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
)
607 struct vsock_sock
*vsk
;
609 sk
= (struct sock
*)data
;
611 /* This handler is privileged when this module is running on the host.
612 * We will get datagrams from all endpoints (even VMs that are in a
613 * restricted context). If we get one from a restricted context then
614 * the destination socket must be trusted.
616 * NOTE: We access the socket struct without holding the lock here.
617 * This is ok because the field we are interested is never modified
618 * outside of the create and destruct socket functions.
621 if (!vmci_transport_allow_dgram(vsk
, dg
->src
.context
))
622 return VMCI_ERROR_NO_ACCESS
;
624 size
= VMCI_DG_SIZE(dg
);
626 /* Attach the packet to the socket's receive queue as an sk_buff. */
627 skb
= alloc_skb(size
, GFP_ATOMIC
);
629 return VMCI_ERROR_NO_MEM
;
631 /* sk_receive_skb() will do a sock_put(), so hold here. */
634 memcpy(skb
->data
, dg
, size
);
635 sk_receive_skb(sk
, skb
, 0);
640 static bool vmci_transport_stream_allow(u32 cid
, u32 port
)
642 static const u32 non_socket_contexts
[] = {
647 BUILD_BUG_ON(sizeof(cid
) != sizeof(*non_socket_contexts
));
649 for (i
= 0; i
< ARRAY_SIZE(non_socket_contexts
); i
++) {
650 if (cid
== non_socket_contexts
[i
])
657 /* This is invoked as part of a tasklet that's scheduled when the VMCI
658 * interrupt fires. This is run in bottom-half context but it defers most of
659 * its work to the packet handling work queue.
662 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
)
665 struct sockaddr_vm dst
;
666 struct sockaddr_vm src
;
667 struct vmci_transport_packet
*pkt
;
668 struct vsock_sock
*vsk
;
674 bh_process_pkt
= false;
676 /* Ignore incoming packets from contexts without sockets, or resources
677 * that aren't vsock implementations.
680 if (!vmci_transport_stream_allow(dg
->src
.context
, -1)
681 || vmci_transport_peer_rid(dg
->src
.context
) != dg
->src
.resource
)
682 return VMCI_ERROR_NO_ACCESS
;
684 if (VMCI_DG_SIZE(dg
) < sizeof(*pkt
))
685 /* Drop datagrams that do not contain full VSock packets. */
686 return VMCI_ERROR_INVALID_ARGS
;
688 pkt
= (struct vmci_transport_packet
*)dg
;
690 /* Find the socket that should handle this packet. First we look for a
691 * connected socket and if there is none we look for a socket bound to
692 * the destintation address.
694 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
695 vsock_addr_init(&dst
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
697 sk
= vsock_find_connected_socket(&src
, &dst
);
699 sk
= vsock_find_bound_socket(&dst
);
701 /* We could not find a socket for this specified
702 * address. If this packet is a RST, we just drop it.
703 * If it is another packet, we send a RST. Note that
704 * we do not send a RST reply to RSTs so that we do not
705 * continually send RSTs between two endpoints.
707 * Note that since this is a reply, dst is src and src
710 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
711 pr_err("unable to send reset\n");
713 err
= VMCI_ERROR_NOT_FOUND
;
718 /* If the received packet type is beyond all types known to this
719 * implementation, reply with an invalid message. Hopefully this will
720 * help when implementing backwards compatibility in the future.
722 if (pkt
->type
>= VMCI_TRANSPORT_PACKET_TYPE_MAX
) {
723 vmci_transport_send_invalid_bh(&dst
, &src
);
724 err
= VMCI_ERROR_INVALID_ARGS
;
728 /* This handler is privileged when this module is running on the host.
729 * We will get datagram connect requests from all endpoints (even VMs
730 * that are in a restricted context). If we get one from a restricted
731 * context then the destination socket must be trusted.
733 * NOTE: We access the socket struct without holding the lock here.
734 * This is ok because the field we are interested is never modified
735 * outside of the create and destruct socket functions.
738 if (!vmci_transport_allow_dgram(vsk
, pkt
->dg
.src
.context
)) {
739 err
= VMCI_ERROR_NO_ACCESS
;
743 /* We do most everything in a work queue, but let's fast path the
744 * notification of reads and writes to help data transfer performance.
745 * We can only do this if there is no process context code executing
746 * for this socket since that may change the state.
750 if (!sock_owned_by_user(sk
)) {
751 /* The local context ID may be out of date, update it. */
752 vsk
->local_addr
.svm_cid
= dst
.svm_cid
;
754 if (sk
->sk_state
== SS_CONNECTED
)
755 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
756 sk
, pkt
, true, &dst
, &src
,
762 if (!bh_process_pkt
) {
763 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
765 recv_pkt_info
= kmalloc(sizeof(*recv_pkt_info
), GFP_ATOMIC
);
766 if (!recv_pkt_info
) {
767 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
768 pr_err("unable to send reset\n");
770 err
= VMCI_ERROR_NO_MEM
;
774 recv_pkt_info
->sk
= sk
;
775 memcpy(&recv_pkt_info
->pkt
, pkt
, sizeof(recv_pkt_info
->pkt
));
776 INIT_WORK(&recv_pkt_info
->work
, vmci_transport_recv_pkt_work
);
778 schedule_work(&recv_pkt_info
->work
);
779 /* Clear sk so that the reference count incremented by one of
780 * the Find functions above is not decremented below. We need
781 * that reference count for the packet handler we've scheduled
794 static void vmci_transport_handle_detach(struct sock
*sk
)
796 struct vsock_sock
*vsk
;
799 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)) {
800 sock_set_flag(sk
, SOCK_DONE
);
802 /* On a detach the peer will not be sending or receiving
805 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
807 /* We should not be sending anymore since the peer won't be
808 * there to receive, but we can still receive if there is data
809 * left in our consume queue.
811 if (vsock_stream_has_data(vsk
) <= 0) {
812 if (sk
->sk_state
== SS_CONNECTING
) {
813 /* The peer may detach from a queue pair while
814 * we are still in the connecting state, i.e.,
815 * if the peer VM is killed after attaching to
816 * a queue pair, but before we complete the
817 * handshake. In that case, we treat the detach
818 * event like a reset.
821 sk
->sk_state
= SS_UNCONNECTED
;
822 sk
->sk_err
= ECONNRESET
;
823 sk
->sk_error_report(sk
);
826 sk
->sk_state
= SS_UNCONNECTED
;
828 sk
->sk_state_change(sk
);
832 static void vmci_transport_peer_detach_cb(u32 sub_id
,
833 const struct vmci_event_data
*e_data
,
836 struct vmci_transport
*trans
= client_data
;
837 const struct vmci_event_payload_qp
*e_payload
;
839 e_payload
= vmci_event_data_const_payload(e_data
);
841 /* XXX This is lame, we should provide a way to lookup sockets by
844 if (vmci_handle_is_invalid(e_payload
->handle
) ||
845 !vmci_handle_is_equal(trans
->qp_handle
, e_payload
->handle
))
848 /* We don't ask for delayed CBs when we subscribe to this event (we
849 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
850 * guarantees in that case about what context we might be running in,
851 * so it could be BH or process, blockable or non-blockable. So we
852 * need to account for all possible contexts here.
854 spin_lock_bh(&trans
->lock
);
858 /* Apart from here, trans->lock is only grabbed as part of sk destruct,
859 * where trans->sk isn't locked.
861 bh_lock_sock(trans
->sk
);
863 vmci_transport_handle_detach(trans
->sk
);
865 bh_unlock_sock(trans
->sk
);
867 spin_unlock_bh(&trans
->lock
);
870 static void vmci_transport_qp_resumed_cb(u32 sub_id
,
871 const struct vmci_event_data
*e_data
,
874 vsock_for_each_connected_socket(vmci_transport_handle_detach
);
877 static void vmci_transport_recv_pkt_work(struct work_struct
*work
)
879 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
880 struct vmci_transport_packet
*pkt
;
884 container_of(work
, struct vmci_transport_recv_pkt_info
, work
);
885 sk
= recv_pkt_info
->sk
;
886 pkt
= &recv_pkt_info
->pkt
;
890 /* The local context ID may be out of date. */
891 vsock_sk(sk
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
893 switch (sk
->sk_state
) {
894 case VSOCK_SS_LISTEN
:
895 vmci_transport_recv_listen(sk
, pkt
);
898 /* Processing of pending connections for servers goes through
899 * the listening socket, so see vmci_transport_recv_listen()
902 vmci_transport_recv_connecting_client(sk
, pkt
);
905 vmci_transport_recv_connected(sk
, pkt
);
908 /* Because this function does not run in the same context as
909 * vmci_transport_recv_stream_cb it is possible that the
910 * socket has closed. We need to let the other side know or it
911 * could be sitting in a connect and hang forever. Send a
912 * reset to prevent that.
914 vmci_transport_send_reset(sk
, pkt
);
919 kfree(recv_pkt_info
);
920 /* Release reference obtained in the stream callback when we fetched
921 * this socket out of the bound or connected list.
926 static int vmci_transport_recv_listen(struct sock
*sk
,
927 struct vmci_transport_packet
*pkt
)
929 struct sock
*pending
;
930 struct vsock_sock
*vpending
;
933 bool old_request
= false;
934 bool old_pkt_proto
= false;
938 /* Because we are in the listen state, we could be receiving a packet
939 * for ourself or any previous connection requests that we received.
940 * If it's the latter, we try to find a socket in our list of pending
941 * connections and, if we do, call the appropriate handler for the
942 * state that that socket is in. Otherwise we try to service the
943 * connection request.
945 pending
= vmci_transport_get_pending(sk
, pkt
);
949 /* The local context ID may be out of date. */
950 vsock_sk(pending
)->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
952 switch (pending
->sk_state
) {
954 err
= vmci_transport_recv_connecting_server(sk
,
959 vmci_transport_send_reset(pending
, pkt
);
964 vsock_remove_pending(sk
, pending
);
966 release_sock(pending
);
967 vmci_transport_release_pending(pending
);
972 /* The listen state only accepts connection requests. Reply with a
973 * reset unless we received a reset.
976 if (!(pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
||
977 pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)) {
978 vmci_transport_reply_reset(pkt
);
982 if (pkt
->u
.size
== 0) {
983 vmci_transport_reply_reset(pkt
);
987 /* If this socket can't accommodate this connection request, we send a
988 * reset. Otherwise we create and initialize a child socket and reply
989 * with a connection negotiation.
991 if (sk
->sk_ack_backlog
>= sk
->sk_max_ack_backlog
) {
992 vmci_transport_reply_reset(pkt
);
993 return -ECONNREFUSED
;
996 pending
= __vsock_create(sock_net(sk
), NULL
, sk
, GFP_KERNEL
,
999 vmci_transport_send_reset(sk
, pkt
);
1003 vpending
= vsock_sk(pending
);
1005 vsock_addr_init(&vpending
->local_addr
, pkt
->dg
.dst
.context
,
1007 vsock_addr_init(&vpending
->remote_addr
, pkt
->dg
.src
.context
,
1010 /* If the proposed size fits within our min/max, accept it. Otherwise
1011 * propose our own size.
1013 if (pkt
->u
.size
>= vmci_trans(vpending
)->queue_pair_min_size
&&
1014 pkt
->u
.size
<= vmci_trans(vpending
)->queue_pair_max_size
) {
1015 qp_size
= pkt
->u
.size
;
1017 qp_size
= vmci_trans(vpending
)->queue_pair_size
;
1020 /* Figure out if we are using old or new requests based on the
1021 * overrides pkt types sent by our peer.
1023 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1024 old_request
= old_pkt_proto
;
1026 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
)
1028 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)
1029 old_request
= false;
1034 /* Handle a REQUEST (or override) */
1035 u16 version
= VSOCK_PROTO_INVALID
;
1036 if (vmci_transport_proto_to_notify_struct(
1037 pending
, &version
, true))
1038 err
= vmci_transport_send_negotiate(pending
, qp_size
);
1043 /* Handle a REQUEST2 (or override) */
1044 int proto_int
= pkt
->proto
;
1046 u16 active_proto_version
= 0;
1048 /* The list of possible protocols is the intersection of all
1049 * protocols the client supports ... plus all the protocols we
1052 proto_int
&= vmci_transport_new_proto_supported_versions();
1054 /* We choose the highest possible protocol version and use that
1057 pos
= fls(proto_int
);
1059 active_proto_version
= (1 << (pos
- 1));
1060 if (vmci_transport_proto_to_notify_struct(
1061 pending
, &active_proto_version
, false))
1062 err
= vmci_transport_send_negotiate2(pending
,
1064 active_proto_version
);
1074 vmci_transport_send_reset(sk
, pkt
);
1076 err
= vmci_transport_error_to_vsock_error(err
);
1080 vsock_add_pending(sk
, pending
);
1081 sk
->sk_ack_backlog
++;
1083 pending
->sk_state
= SS_CONNECTING
;
1084 vmci_trans(vpending
)->produce_size
=
1085 vmci_trans(vpending
)->consume_size
= qp_size
;
1086 vmci_trans(vpending
)->queue_pair_size
= qp_size
;
1088 vmci_trans(vpending
)->notify_ops
->process_request(pending
);
1090 /* We might never receive another message for this socket and it's not
1091 * connected to any process, so we have to ensure it gets cleaned up
1092 * ourself. Our delayed work function will take care of that. Note
1093 * that we do not ever cancel this function since we have few
1094 * guarantees about its state when calling cancel_delayed_work().
1095 * Instead we hold a reference on the socket for that function and make
1096 * it capable of handling cases where it needs to do nothing but
1097 * release that reference.
1099 vpending
->listener
= sk
;
1102 INIT_DELAYED_WORK(&vpending
->dwork
, vsock_pending_work
);
1103 schedule_delayed_work(&vpending
->dwork
, HZ
);
1110 vmci_transport_recv_connecting_server(struct sock
*listener
,
1111 struct sock
*pending
,
1112 struct vmci_transport_packet
*pkt
)
1114 struct vsock_sock
*vpending
;
1115 struct vmci_handle handle
;
1116 struct vmci_qp
*qpair
;
1123 vpending
= vsock_sk(pending
);
1124 detach_sub_id
= VMCI_INVALID_ID
;
1126 switch (pkt
->type
) {
1127 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
1128 if (vmci_handle_is_invalid(pkt
->u
.handle
)) {
1129 vmci_transport_send_reset(pending
, pkt
);
1136 /* Close and cleanup the connection. */
1137 vmci_transport_send_reset(pending
, pkt
);
1139 err
= pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
? 0 : -EINVAL
;
1143 /* In order to complete the connection we need to attach to the offered
1144 * queue pair and send an attach notification. We also subscribe to the
1145 * detach event so we know when our peer goes away, and we do that
1146 * before attaching so we don't miss an event. If all this succeeds,
1147 * we update our state and wakeup anything waiting in accept() for a
1151 /* We don't care about attach since we ensure the other side has
1152 * attached by specifying the ATTACH_ONLY flag below.
1154 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1155 vmci_transport_peer_detach_cb
,
1156 vmci_trans(vpending
), &detach_sub_id
);
1157 if (err
< VMCI_SUCCESS
) {
1158 vmci_transport_send_reset(pending
, pkt
);
1159 err
= vmci_transport_error_to_vsock_error(err
);
1164 vmci_trans(vpending
)->detach_sub_id
= detach_sub_id
;
1166 /* Now attach to the queue pair the client created. */
1167 handle
= pkt
->u
.handle
;
1169 /* vpending->local_addr always has a context id so we do not need to
1170 * worry about VMADDR_CID_ANY in this case.
1173 vpending
->remote_addr
.svm_cid
== vpending
->local_addr
.svm_cid
;
1174 flags
= VMCI_QPFLAG_ATTACH_ONLY
;
1175 flags
|= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1177 err
= vmci_transport_queue_pair_alloc(
1180 vmci_trans(vpending
)->produce_size
,
1181 vmci_trans(vpending
)->consume_size
,
1182 pkt
->dg
.src
.context
,
1184 vmci_transport_is_trusted(
1186 vpending
->remote_addr
.svm_cid
));
1188 vmci_transport_send_reset(pending
, pkt
);
1193 vmci_trans(vpending
)->qp_handle
= handle
;
1194 vmci_trans(vpending
)->qpair
= qpair
;
1196 /* When we send the attach message, we must be ready to handle incoming
1197 * control messages on the newly connected socket. So we move the
1198 * pending socket to the connected state before sending the attach
1199 * message. Otherwise, an incoming packet triggered by the attach being
1200 * received by the peer may be processed concurrently with what happens
1201 * below after sending the attach message, and that incoming packet
1202 * will find the listening socket instead of the (currently) pending
1203 * socket. Note that enqueueing the socket increments the reference
1204 * count, so even if a reset comes before the connection is accepted,
1205 * the socket will be valid until it is removed from the queue.
1207 * If we fail sending the attach below, we remove the socket from the
1208 * connected list and move the socket to SS_UNCONNECTED before
1209 * releasing the lock, so a pending slow path processing of an incoming
1210 * packet will not see the socket in the connected state in that case.
1212 pending
->sk_state
= SS_CONNECTED
;
1214 vsock_insert_connected(vpending
);
1216 /* Notify our peer of our attach. */
1217 err
= vmci_transport_send_attach(pending
, handle
);
1219 vsock_remove_connected(vpending
);
1220 pr_err("Could not send attach\n");
1221 vmci_transport_send_reset(pending
, pkt
);
1222 err
= vmci_transport_error_to_vsock_error(err
);
1227 /* We have a connection. Move the now connected socket from the
1228 * listener's pending list to the accept queue so callers of accept()
1231 vsock_remove_pending(listener
, pending
);
1232 vsock_enqueue_accept(listener
, pending
);
1234 /* Callers of accept() will be be waiting on the listening socket, not
1235 * the pending socket.
1237 listener
->sk_data_ready(listener
);
1242 pending
->sk_err
= skerr
;
1243 pending
->sk_state
= SS_UNCONNECTED
;
1244 /* As long as we drop our reference, all necessary cleanup will handle
1245 * when the cleanup function drops its reference and our destruct
1246 * implementation is called. Note that since the listen handler will
1247 * remove pending from the pending list upon our failure, the cleanup
1248 * function won't drop the additional reference, which is why we do it
1257 vmci_transport_recv_connecting_client(struct sock
*sk
,
1258 struct vmci_transport_packet
*pkt
)
1260 struct vsock_sock
*vsk
;
1266 switch (pkt
->type
) {
1267 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
1268 if (vmci_handle_is_invalid(pkt
->u
.handle
) ||
1269 !vmci_handle_is_equal(pkt
->u
.handle
,
1270 vmci_trans(vsk
)->qp_handle
)) {
1276 /* Signify the socket is connected and wakeup the waiter in
1277 * connect(). Also place the socket in the connected table for
1278 * accounting (it can already be found since it's in the bound
1281 sk
->sk_state
= SS_CONNECTED
;
1282 sk
->sk_socket
->state
= SS_CONNECTED
;
1283 vsock_insert_connected(vsk
);
1284 sk
->sk_state_change(sk
);
1287 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
1288 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
1289 if (pkt
->u
.size
== 0
1290 || pkt
->dg
.src
.context
!= vsk
->remote_addr
.svm_cid
1291 || pkt
->src_port
!= vsk
->remote_addr
.svm_port
1292 || !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)
1293 || vmci_trans(vsk
)->qpair
1294 || vmci_trans(vsk
)->produce_size
!= 0
1295 || vmci_trans(vsk
)->consume_size
!= 0
1296 || vmci_trans(vsk
)->detach_sub_id
!= VMCI_INVALID_ID
) {
1303 err
= vmci_transport_recv_connecting_client_negotiate(sk
, pkt
);
1310 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
1311 err
= vmci_transport_recv_connecting_client_invalid(sk
, pkt
);
1318 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1319 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1320 * continue processing here after they sent an INVALID packet.
1321 * This meant that we got a RST after the INVALID. We ignore a
1322 * RST after an INVALID. The common code doesn't send the RST
1323 * ... so we can hang if an old version of the common code
1324 * fails between getting a REQUEST and sending an OFFER back.
1325 * Not much we can do about it... except hope that it doesn't
1328 if (vsk
->ignore_connecting_rst
) {
1329 vsk
->ignore_connecting_rst
= false;
1338 /* Close and cleanup the connection. */
1347 vmci_transport_send_reset(sk
, pkt
);
1349 sk
->sk_state
= SS_UNCONNECTED
;
1351 sk
->sk_error_report(sk
);
1355 static int vmci_transport_recv_connecting_client_negotiate(
1357 struct vmci_transport_packet
*pkt
)
1360 struct vsock_sock
*vsk
;
1361 struct vmci_handle handle
;
1362 struct vmci_qp
*qpair
;
1366 bool old_proto
= true;
1371 handle
= VMCI_INVALID_HANDLE
;
1372 detach_sub_id
= VMCI_INVALID_ID
;
1374 /* If we have gotten here then we should be past the point where old
1375 * linux vsock could have sent the bogus rst.
1377 vsk
->sent_request
= false;
1378 vsk
->ignore_connecting_rst
= false;
1380 /* Verify that we're OK with the proposed queue pair size */
1381 if (pkt
->u
.size
< vmci_trans(vsk
)->queue_pair_min_size
||
1382 pkt
->u
.size
> vmci_trans(vsk
)->queue_pair_max_size
) {
1387 /* At this point we know the CID the peer is using to talk to us. */
1389 if (vsk
->local_addr
.svm_cid
== VMADDR_CID_ANY
)
1390 vsk
->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
1392 /* Setup the notify ops to be the highest supported version that both
1393 * the server and the client support.
1396 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1397 old_proto
= old_pkt_proto
;
1399 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
)
1401 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
)
1407 version
= VSOCK_PROTO_INVALID
;
1409 version
= pkt
->proto
;
1411 if (!vmci_transport_proto_to_notify_struct(sk
, &version
, old_proto
)) {
1416 /* Subscribe to detach events first.
1418 * XXX We attach once for each queue pair created for now so it is easy
1419 * to find the socket (it's provided), but later we should only
1420 * subscribe once and add a way to lookup sockets by queue pair handle.
1422 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1423 vmci_transport_peer_detach_cb
,
1424 vmci_trans(vsk
), &detach_sub_id
);
1425 if (err
< VMCI_SUCCESS
) {
1426 err
= vmci_transport_error_to_vsock_error(err
);
1430 /* Make VMCI select the handle for us. */
1431 handle
= VMCI_INVALID_HANDLE
;
1432 is_local
= vsk
->remote_addr
.svm_cid
== vsk
->local_addr
.svm_cid
;
1433 flags
= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1435 err
= vmci_transport_queue_pair_alloc(&qpair
,
1439 vsk
->remote_addr
.svm_cid
,
1441 vmci_transport_is_trusted(
1444 remote_addr
.svm_cid
));
1448 err
= vmci_transport_send_qp_offer(sk
, handle
);
1450 err
= vmci_transport_error_to_vsock_error(err
);
1454 vmci_trans(vsk
)->qp_handle
= handle
;
1455 vmci_trans(vsk
)->qpair
= qpair
;
1457 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
=
1460 vmci_trans(vsk
)->detach_sub_id
= detach_sub_id
;
1462 vmci_trans(vsk
)->notify_ops
->process_negotiate(sk
);
1467 if (detach_sub_id
!= VMCI_INVALID_ID
)
1468 vmci_event_unsubscribe(detach_sub_id
);
1470 if (!vmci_handle_is_invalid(handle
))
1471 vmci_qpair_detach(&qpair
);
1477 vmci_transport_recv_connecting_client_invalid(struct sock
*sk
,
1478 struct vmci_transport_packet
*pkt
)
1481 struct vsock_sock
*vsk
= vsock_sk(sk
);
1483 if (vsk
->sent_request
) {
1484 vsk
->sent_request
= false;
1485 vsk
->ignore_connecting_rst
= true;
1487 err
= vmci_transport_send_conn_request(
1488 sk
, vmci_trans(vsk
)->queue_pair_size
);
1490 err
= vmci_transport_error_to_vsock_error(err
);
1499 static int vmci_transport_recv_connected(struct sock
*sk
,
1500 struct vmci_transport_packet
*pkt
)
1502 struct vsock_sock
*vsk
;
1503 bool pkt_processed
= false;
1505 /* In cases where we are closing the connection, it's sufficient to
1506 * mark the state change (and maybe error) and wake up any waiting
1507 * threads. Since this is a connected socket, it's owned by a user
1508 * process and will be cleaned up when the failure is passed back on
1509 * the current or next system call. Our system call implementations
1510 * must therefore check for error and state changes on entry and when
1513 switch (pkt
->type
) {
1514 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
1518 vsk
->peer_shutdown
|= pkt
->u
.mode
;
1519 sk
->sk_state_change(sk
);
1523 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1525 /* It is possible that we sent our peer a message (e.g a
1526 * WAITING_READ) right before we got notified that the peer had
1527 * detached. If that happens then we can get a RST pkt back
1528 * from our peer even though there is data available for us to
1529 * read. In that case, don't shutdown the socket completely but
1530 * instead allow the local client to finish reading data off
1531 * the queuepair. Always treat a RST pkt in connected mode like
1534 sock_set_flag(sk
, SOCK_DONE
);
1535 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
1536 if (vsock_stream_has_data(vsk
) <= 0)
1537 sk
->sk_state
= SS_DISCONNECTING
;
1539 sk
->sk_state_change(sk
);
1544 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
1545 sk
, pkt
, false, NULL
, NULL
,
1556 static int vmci_transport_socket_init(struct vsock_sock
*vsk
,
1557 struct vsock_sock
*psk
)
1559 vsk
->trans
= kmalloc(sizeof(struct vmci_transport
), GFP_KERNEL
);
1563 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1564 vmci_trans(vsk
)->qp_handle
= VMCI_INVALID_HANDLE
;
1565 vmci_trans(vsk
)->qpair
= NULL
;
1566 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
= 0;
1567 vmci_trans(vsk
)->detach_sub_id
= VMCI_INVALID_ID
;
1568 vmci_trans(vsk
)->notify_ops
= NULL
;
1569 INIT_LIST_HEAD(&vmci_trans(vsk
)->elem
);
1570 vmci_trans(vsk
)->sk
= &vsk
->sk
;
1571 spin_lock_init(&vmci_trans(vsk
)->lock
);
1573 vmci_trans(vsk
)->queue_pair_size
=
1574 vmci_trans(psk
)->queue_pair_size
;
1575 vmci_trans(vsk
)->queue_pair_min_size
=
1576 vmci_trans(psk
)->queue_pair_min_size
;
1577 vmci_trans(vsk
)->queue_pair_max_size
=
1578 vmci_trans(psk
)->queue_pair_max_size
;
1580 vmci_trans(vsk
)->queue_pair_size
=
1581 VMCI_TRANSPORT_DEFAULT_QP_SIZE
;
1582 vmci_trans(vsk
)->queue_pair_min_size
=
1583 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN
;
1584 vmci_trans(vsk
)->queue_pair_max_size
=
1585 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX
;
1591 static void vmci_transport_free_resources(struct list_head
*transport_list
)
1593 while (!list_empty(transport_list
)) {
1594 struct vmci_transport
*transport
=
1595 list_first_entry(transport_list
, struct vmci_transport
,
1597 list_del(&transport
->elem
);
1599 if (transport
->detach_sub_id
!= VMCI_INVALID_ID
) {
1600 vmci_event_unsubscribe(transport
->detach_sub_id
);
1601 transport
->detach_sub_id
= VMCI_INVALID_ID
;
1604 if (!vmci_handle_is_invalid(transport
->qp_handle
)) {
1605 vmci_qpair_detach(&transport
->qpair
);
1606 transport
->qp_handle
= VMCI_INVALID_HANDLE
;
1607 transport
->produce_size
= 0;
1608 transport
->consume_size
= 0;
1615 static void vmci_transport_cleanup(struct work_struct
*work
)
1619 spin_lock_bh(&vmci_transport_cleanup_lock
);
1620 list_replace_init(&vmci_transport_cleanup_list
, &pending
);
1621 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1622 vmci_transport_free_resources(&pending
);
1625 static void vmci_transport_destruct(struct vsock_sock
*vsk
)
1627 /* Ensure that the detach callback doesn't use the sk/vsk
1628 * we are about to destruct.
1630 spin_lock_bh(&vmci_trans(vsk
)->lock
);
1631 vmci_trans(vsk
)->sk
= NULL
;
1632 spin_unlock_bh(&vmci_trans(vsk
)->lock
);
1634 if (vmci_trans(vsk
)->notify_ops
)
1635 vmci_trans(vsk
)->notify_ops
->socket_destruct(vsk
);
1637 spin_lock_bh(&vmci_transport_cleanup_lock
);
1638 list_add(&vmci_trans(vsk
)->elem
, &vmci_transport_cleanup_list
);
1639 spin_unlock_bh(&vmci_transport_cleanup_lock
);
1640 schedule_work(&vmci_transport_cleanup_work
);
1645 static void vmci_transport_release(struct vsock_sock
*vsk
)
1647 vsock_remove_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
);
1743 dg
= (struct vmci_datagram
*)skb
->data
;
1745 /* err is 0, meaning we read zero bytes. */
1748 payload_len
= dg
->payload_size
;
1749 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1750 if (payload_len
!= skb
->len
- sizeof(*dg
)) {
1755 if (payload_len
> len
) {
1757 msg
->msg_flags
|= MSG_TRUNC
;
1760 /* Place the datagram payload in the user's iovec. */
1761 err
= skb_copy_datagram_msg(skb
, sizeof(*dg
), msg
, payload_len
);
1765 if (msg
->msg_name
) {
1766 /* Provide the address of the sender. */
1767 DECLARE_SOCKADDR(struct sockaddr_vm
*, vm_addr
, msg
->msg_name
);
1768 vsock_addr_init(vm_addr
, dg
->src
.context
, dg
->src
.resource
);
1769 msg
->msg_namelen
= sizeof(*vm_addr
);
1774 skb_free_datagram(&vsk
->sk
, skb
);
1778 static bool vmci_transport_dgram_allow(u32 cid
, u32 port
)
1780 if (cid
== VMADDR_CID_HYPERVISOR
) {
1781 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1782 * state and are allowed.
1784 return port
== VMCI_UNITY_PBRPC_REGISTER
;
1790 static int vmci_transport_connect(struct vsock_sock
*vsk
)
1793 bool old_pkt_proto
= false;
1794 struct sock
*sk
= &vsk
->sk
;
1796 if (vmci_transport_old_proto_override(&old_pkt_proto
) &&
1798 err
= vmci_transport_send_conn_request(
1799 sk
, vmci_trans(vsk
)->queue_pair_size
);
1801 sk
->sk_state
= SS_UNCONNECTED
;
1805 int supported_proto_versions
=
1806 vmci_transport_new_proto_supported_versions();
1807 err
= vmci_transport_send_conn_request2(
1808 sk
, vmci_trans(vsk
)->queue_pair_size
,
1809 supported_proto_versions
);
1811 sk
->sk_state
= SS_UNCONNECTED
;
1815 vsk
->sent_request
= true;
1821 static ssize_t
vmci_transport_stream_dequeue(
1822 struct vsock_sock
*vsk
,
1827 if (flags
& MSG_PEEK
)
1828 return vmci_qpair_peekv(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1830 return vmci_qpair_dequev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1833 static ssize_t
vmci_transport_stream_enqueue(
1834 struct vsock_sock
*vsk
,
1838 return vmci_qpair_enquev(vmci_trans(vsk
)->qpair
, msg
, len
, 0);
1841 static s64
vmci_transport_stream_has_data(struct vsock_sock
*vsk
)
1843 return vmci_qpair_consume_buf_ready(vmci_trans(vsk
)->qpair
);
1846 static s64
vmci_transport_stream_has_space(struct vsock_sock
*vsk
)
1848 return vmci_qpair_produce_free_space(vmci_trans(vsk
)->qpair
);
1851 static u64
vmci_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
1853 return vmci_trans(vsk
)->consume_size
;
1856 static bool vmci_transport_stream_is_active(struct vsock_sock
*vsk
)
1858 return !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
);
1861 static u64
vmci_transport_get_buffer_size(struct vsock_sock
*vsk
)
1863 return vmci_trans(vsk
)->queue_pair_size
;
1866 static u64
vmci_transport_get_min_buffer_size(struct vsock_sock
*vsk
)
1868 return vmci_trans(vsk
)->queue_pair_min_size
;
1871 static u64
vmci_transport_get_max_buffer_size(struct vsock_sock
*vsk
)
1873 return vmci_trans(vsk
)->queue_pair_max_size
;
1876 static void vmci_transport_set_buffer_size(struct vsock_sock
*vsk
, u64 val
)
1878 if (val
< vmci_trans(vsk
)->queue_pair_min_size
)
1879 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1880 if (val
> vmci_trans(vsk
)->queue_pair_max_size
)
1881 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1882 vmci_trans(vsk
)->queue_pair_size
= val
;
1885 static void vmci_transport_set_min_buffer_size(struct vsock_sock
*vsk
,
1888 if (val
> vmci_trans(vsk
)->queue_pair_size
)
1889 vmci_trans(vsk
)->queue_pair_size
= val
;
1890 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1893 static void vmci_transport_set_max_buffer_size(struct vsock_sock
*vsk
,
1896 if (val
< vmci_trans(vsk
)->queue_pair_size
)
1897 vmci_trans(vsk
)->queue_pair_size
= val
;
1898 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1901 static int vmci_transport_notify_poll_in(
1902 struct vsock_sock
*vsk
,
1904 bool *data_ready_now
)
1906 return vmci_trans(vsk
)->notify_ops
->poll_in(
1907 &vsk
->sk
, target
, data_ready_now
);
1910 static int vmci_transport_notify_poll_out(
1911 struct vsock_sock
*vsk
,
1913 bool *space_available_now
)
1915 return vmci_trans(vsk
)->notify_ops
->poll_out(
1916 &vsk
->sk
, target
, space_available_now
);
1919 static int vmci_transport_notify_recv_init(
1920 struct vsock_sock
*vsk
,
1922 struct vsock_transport_recv_notify_data
*data
)
1924 return vmci_trans(vsk
)->notify_ops
->recv_init(
1926 (struct vmci_transport_recv_notify_data
*)data
);
1929 static int vmci_transport_notify_recv_pre_block(
1930 struct vsock_sock
*vsk
,
1932 struct vsock_transport_recv_notify_data
*data
)
1934 return vmci_trans(vsk
)->notify_ops
->recv_pre_block(
1936 (struct vmci_transport_recv_notify_data
*)data
);
1939 static int vmci_transport_notify_recv_pre_dequeue(
1940 struct vsock_sock
*vsk
,
1942 struct vsock_transport_recv_notify_data
*data
)
1944 return vmci_trans(vsk
)->notify_ops
->recv_pre_dequeue(
1946 (struct vmci_transport_recv_notify_data
*)data
);
1949 static int vmci_transport_notify_recv_post_dequeue(
1950 struct vsock_sock
*vsk
,
1954 struct vsock_transport_recv_notify_data
*data
)
1956 return vmci_trans(vsk
)->notify_ops
->recv_post_dequeue(
1957 &vsk
->sk
, target
, copied
, data_read
,
1958 (struct vmci_transport_recv_notify_data
*)data
);
1961 static int vmci_transport_notify_send_init(
1962 struct vsock_sock
*vsk
,
1963 struct vsock_transport_send_notify_data
*data
)
1965 return vmci_trans(vsk
)->notify_ops
->send_init(
1967 (struct vmci_transport_send_notify_data
*)data
);
1970 static int vmci_transport_notify_send_pre_block(
1971 struct vsock_sock
*vsk
,
1972 struct vsock_transport_send_notify_data
*data
)
1974 return vmci_trans(vsk
)->notify_ops
->send_pre_block(
1976 (struct vmci_transport_send_notify_data
*)data
);
1979 static int vmci_transport_notify_send_pre_enqueue(
1980 struct vsock_sock
*vsk
,
1981 struct vsock_transport_send_notify_data
*data
)
1983 return vmci_trans(vsk
)->notify_ops
->send_pre_enqueue(
1985 (struct vmci_transport_send_notify_data
*)data
);
1988 static int vmci_transport_notify_send_post_enqueue(
1989 struct vsock_sock
*vsk
,
1991 struct vsock_transport_send_notify_data
*data
)
1993 return vmci_trans(vsk
)->notify_ops
->send_post_enqueue(
1995 (struct vmci_transport_send_notify_data
*)data
);
1998 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
)
2000 if (PROTOCOL_OVERRIDE
!= -1) {
2001 if (PROTOCOL_OVERRIDE
== 0)
2002 *old_pkt_proto
= true;
2004 *old_pkt_proto
= false;
2006 pr_info("Proto override in use\n");
2013 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
,
2017 struct vsock_sock
*vsk
= vsock_sk(sk
);
2019 if (old_pkt_proto
) {
2020 if (*proto
!= VSOCK_PROTO_INVALID
) {
2021 pr_err("Can't set both an old and new protocol\n");
2024 vmci_trans(vsk
)->notify_ops
= &vmci_transport_notify_pkt_ops
;
2029 case VSOCK_PROTO_PKT_ON_NOTIFY
:
2030 vmci_trans(vsk
)->notify_ops
=
2031 &vmci_transport_notify_pkt_q_state_ops
;
2034 pr_err("Unknown notify protocol version\n");
2039 vmci_trans(vsk
)->notify_ops
->socket_init(sk
);
2043 static u16
vmci_transport_new_proto_supported_versions(void)
2045 if (PROTOCOL_OVERRIDE
!= -1)
2046 return PROTOCOL_OVERRIDE
;
2048 return VSOCK_PROTO_ALL_SUPPORTED
;
2051 static u32
vmci_transport_get_local_cid(void)
2053 return vmci_get_context_id();
2056 static const struct vsock_transport vmci_transport
= {
2057 .init
= vmci_transport_socket_init
,
2058 .destruct
= vmci_transport_destruct
,
2059 .release
= vmci_transport_release
,
2060 .connect
= vmci_transport_connect
,
2061 .dgram_bind
= vmci_transport_dgram_bind
,
2062 .dgram_dequeue
= vmci_transport_dgram_dequeue
,
2063 .dgram_enqueue
= vmci_transport_dgram_enqueue
,
2064 .dgram_allow
= vmci_transport_dgram_allow
,
2065 .stream_dequeue
= vmci_transport_stream_dequeue
,
2066 .stream_enqueue
= vmci_transport_stream_enqueue
,
2067 .stream_has_data
= vmci_transport_stream_has_data
,
2068 .stream_has_space
= vmci_transport_stream_has_space
,
2069 .stream_rcvhiwat
= vmci_transport_stream_rcvhiwat
,
2070 .stream_is_active
= vmci_transport_stream_is_active
,
2071 .stream_allow
= vmci_transport_stream_allow
,
2072 .notify_poll_in
= vmci_transport_notify_poll_in
,
2073 .notify_poll_out
= vmci_transport_notify_poll_out
,
2074 .notify_recv_init
= vmci_transport_notify_recv_init
,
2075 .notify_recv_pre_block
= vmci_transport_notify_recv_pre_block
,
2076 .notify_recv_pre_dequeue
= vmci_transport_notify_recv_pre_dequeue
,
2077 .notify_recv_post_dequeue
= vmci_transport_notify_recv_post_dequeue
,
2078 .notify_send_init
= vmci_transport_notify_send_init
,
2079 .notify_send_pre_block
= vmci_transport_notify_send_pre_block
,
2080 .notify_send_pre_enqueue
= vmci_transport_notify_send_pre_enqueue
,
2081 .notify_send_post_enqueue
= vmci_transport_notify_send_post_enqueue
,
2082 .shutdown
= vmci_transport_shutdown
,
2083 .set_buffer_size
= vmci_transport_set_buffer_size
,
2084 .set_min_buffer_size
= vmci_transport_set_min_buffer_size
,
2085 .set_max_buffer_size
= vmci_transport_set_max_buffer_size
,
2086 .get_buffer_size
= vmci_transport_get_buffer_size
,
2087 .get_min_buffer_size
= vmci_transport_get_min_buffer_size
,
2088 .get_max_buffer_size
= vmci_transport_get_max_buffer_size
,
2089 .get_local_cid
= vmci_transport_get_local_cid
,
2092 static int __init
vmci_transport_init(void)
2096 /* Create the datagram handle that we will use to send and receive all
2097 * VSocket control messages for this context.
2099 err
= vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID
,
2100 VMCI_FLAG_ANYCID_DG_HND
,
2101 vmci_transport_recv_stream_cb
,
2103 &vmci_transport_stream_handle
);
2104 if (err
< VMCI_SUCCESS
) {
2105 pr_err("Unable to create datagram handle. (%d)\n", err
);
2106 return vmci_transport_error_to_vsock_error(err
);
2109 err
= vmci_event_subscribe(VMCI_EVENT_QP_RESUMED
,
2110 vmci_transport_qp_resumed_cb
,
2111 NULL
, &vmci_transport_qp_resumed_sub_id
);
2112 if (err
< VMCI_SUCCESS
) {
2113 pr_err("Unable to subscribe to resumed event. (%d)\n", err
);
2114 err
= vmci_transport_error_to_vsock_error(err
);
2115 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2116 goto err_destroy_stream_handle
;
2119 err
= vsock_core_init(&vmci_transport
);
2121 goto err_unsubscribe
;
2126 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2127 err_destroy_stream_handle
:
2128 vmci_datagram_destroy_handle(vmci_transport_stream_handle
);
2131 module_init(vmci_transport_init
);
2133 static void __exit
vmci_transport_exit(void)
2135 cancel_work_sync(&vmci_transport_cleanup_work
);
2136 vmci_transport_free_resources(&vmci_transport_cleanup_list
);
2138 if (!vmci_handle_is_invalid(vmci_transport_stream_handle
)) {
2139 if (vmci_datagram_destroy_handle(
2140 vmci_transport_stream_handle
) != VMCI_SUCCESS
)
2141 pr_err("Couldn't destroy datagram handle\n");
2142 vmci_transport_stream_handle
= VMCI_INVALID_HANDLE
;
2145 if (vmci_transport_qp_resumed_sub_id
!= VMCI_INVALID_ID
) {
2146 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2147 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2152 module_exit(vmci_transport_exit
);
2154 MODULE_AUTHOR("VMware, Inc.");
2155 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2156 MODULE_VERSION("1.0.4.0-k");
2157 MODULE_LICENSE("GPL v2");
2158 MODULE_ALIAS("vmware_vsock");
2159 MODULE_ALIAS_NETPROTO(PF_VSOCK
);