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>
19 #include <linux/bitops.h>
20 #include <linux/cred.h>
21 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/kmod.h>
25 #include <linux/list.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/net.h>
30 #include <linux/poll.h>
31 #include <linux/skbuff.h>
32 #include <linux/smp.h>
33 #include <linux/socket.h>
34 #include <linux/stddef.h>
35 #include <linux/unistd.h>
36 #include <linux/wait.h>
37 #include <linux/workqueue.h>
41 #include "vmci_transport_notify.h"
43 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
);
44 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
);
45 static void vmci_transport_peer_attach_cb(u32 sub_id
,
46 const struct vmci_event_data
*ed
,
48 static void vmci_transport_peer_detach_cb(u32 sub_id
,
49 const struct vmci_event_data
*ed
,
51 static void vmci_transport_recv_pkt_work(struct work_struct
*work
);
52 static int vmci_transport_recv_listen(struct sock
*sk
,
53 struct vmci_transport_packet
*pkt
);
54 static int vmci_transport_recv_connecting_server(
57 struct vmci_transport_packet
*pkt
);
58 static int vmci_transport_recv_connecting_client(
60 struct vmci_transport_packet
*pkt
);
61 static int vmci_transport_recv_connecting_client_negotiate(
63 struct vmci_transport_packet
*pkt
);
64 static int vmci_transport_recv_connecting_client_invalid(
66 struct vmci_transport_packet
*pkt
);
67 static int vmci_transport_recv_connected(struct sock
*sk
,
68 struct vmci_transport_packet
*pkt
);
69 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
);
70 static u16
vmci_transport_new_proto_supported_versions(void);
71 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
, u16
*proto
,
74 struct vmci_transport_recv_pkt_info
{
75 struct work_struct work
;
77 struct vmci_transport_packet pkt
;
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
;
129 vmci_transport_packet_init(struct vmci_transport_packet
*pkt
,
130 struct sockaddr_vm
*src
,
131 struct sockaddr_vm
*dst
,
135 struct vmci_transport_waiting_info
*wait
,
137 struct vmci_handle handle
)
139 /* We register the stream control handler as an any cid handle so we
140 * must always send from a source address of VMADDR_CID_ANY
142 pkt
->dg
.src
= vmci_make_handle(VMADDR_CID_ANY
,
143 VMCI_TRANSPORT_PACKET_RID
);
144 pkt
->dg
.dst
= vmci_make_handle(dst
->svm_cid
,
145 VMCI_TRANSPORT_PACKET_RID
);
146 pkt
->dg
.payload_size
= sizeof(*pkt
) - sizeof(pkt
->dg
);
147 pkt
->version
= VMCI_TRANSPORT_PACKET_VERSION
;
149 pkt
->src_port
= src
->svm_port
;
150 pkt
->dst_port
= dst
->svm_port
;
151 memset(&pkt
->proto
, 0, sizeof(pkt
->proto
));
152 memset(&pkt
->_reserved2
, 0, sizeof(pkt
->_reserved2
));
155 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
159 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST
:
160 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
164 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
165 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
166 pkt
->u
.handle
= handle
;
169 case VMCI_TRANSPORT_PACKET_TYPE_WROTE
:
170 case VMCI_TRANSPORT_PACKET_TYPE_READ
:
171 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
175 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
179 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
:
180 case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
:
181 memcpy(&pkt
->u
.wait
, wait
, sizeof(pkt
->u
.wait
));
184 case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
:
185 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
193 vmci_transport_packet_get_addresses(struct vmci_transport_packet
*pkt
,
194 struct sockaddr_vm
*local
,
195 struct sockaddr_vm
*remote
)
197 vsock_addr_init(local
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
198 vsock_addr_init(remote
, pkt
->dg
.src
.context
, pkt
->src_port
);
202 __vmci_transport_send_control_pkt(struct vmci_transport_packet
*pkt
,
203 struct sockaddr_vm
*src
,
204 struct sockaddr_vm
*dst
,
205 enum vmci_transport_packet_type type
,
208 struct vmci_transport_waiting_info
*wait
,
210 struct vmci_handle handle
,
215 vmci_transport_packet_init(pkt
, src
, dst
, type
, size
, mode
, wait
,
217 err
= vmci_datagram_send(&pkt
->dg
);
218 if (convert_error
&& (err
< 0))
219 return vmci_transport_error_to_vsock_error(err
);
225 vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet
*pkt
,
226 enum vmci_transport_packet_type type
,
229 struct vmci_transport_waiting_info
*wait
,
230 struct vmci_handle handle
)
232 struct vmci_transport_packet reply
;
233 struct sockaddr_vm src
, dst
;
235 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
) {
238 vmci_transport_packet_get_addresses(pkt
, &src
, &dst
);
239 return __vmci_transport_send_control_pkt(&reply
, &src
, &dst
,
248 vmci_transport_send_control_pkt_bh(struct sockaddr_vm
*src
,
249 struct sockaddr_vm
*dst
,
250 enum vmci_transport_packet_type type
,
253 struct vmci_transport_waiting_info
*wait
,
254 struct vmci_handle handle
)
256 /* Note that it is safe to use a single packet across all CPUs since
257 * two tasklets of the same type are guaranteed to not ever run
258 * simultaneously. If that ever changes, or VMCI stops using tasklets,
259 * we can use per-cpu packets.
261 static struct vmci_transport_packet pkt
;
263 return __vmci_transport_send_control_pkt(&pkt
, src
, dst
, type
,
265 VSOCK_PROTO_INVALID
, handle
,
270 vmci_transport_send_control_pkt(struct sock
*sk
,
271 enum vmci_transport_packet_type type
,
274 struct vmci_transport_waiting_info
*wait
,
276 struct vmci_handle handle
)
278 struct vmci_transport_packet
*pkt
;
279 struct vsock_sock
*vsk
;
284 if (!vsock_addr_bound(&vsk
->local_addr
))
287 if (!vsock_addr_bound(&vsk
->remote_addr
))
290 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
294 err
= __vmci_transport_send_control_pkt(pkt
, &vsk
->local_addr
,
295 &vsk
->remote_addr
, type
, size
,
296 mode
, wait
, proto
, handle
,
303 static int vmci_transport_send_reset_bh(struct sockaddr_vm
*dst
,
304 struct sockaddr_vm
*src
,
305 struct vmci_transport_packet
*pkt
)
307 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
309 return vmci_transport_send_control_pkt_bh(
311 VMCI_TRANSPORT_PACKET_TYPE_RST
, 0,
312 0, NULL
, VMCI_INVALID_HANDLE
);
315 static int vmci_transport_send_reset(struct sock
*sk
,
316 struct vmci_transport_packet
*pkt
)
318 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
)
320 return vmci_transport_send_control_pkt(sk
,
321 VMCI_TRANSPORT_PACKET_TYPE_RST
,
322 0, 0, NULL
, VSOCK_PROTO_INVALID
,
323 VMCI_INVALID_HANDLE
);
326 static int vmci_transport_send_negotiate(struct sock
*sk
, size_t size
)
328 return vmci_transport_send_control_pkt(
330 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
,
333 VMCI_INVALID_HANDLE
);
336 static int vmci_transport_send_negotiate2(struct sock
*sk
, size_t size
,
339 return vmci_transport_send_control_pkt(
341 VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
,
342 size
, 0, NULL
, version
,
343 VMCI_INVALID_HANDLE
);
346 static int vmci_transport_send_qp_offer(struct sock
*sk
,
347 struct vmci_handle handle
)
349 return vmci_transport_send_control_pkt(
350 sk
, VMCI_TRANSPORT_PACKET_TYPE_OFFER
, 0,
352 VSOCK_PROTO_INVALID
, handle
);
355 static int vmci_transport_send_attach(struct sock
*sk
,
356 struct vmci_handle handle
)
358 return vmci_transport_send_control_pkt(
359 sk
, VMCI_TRANSPORT_PACKET_TYPE_ATTACH
,
360 0, 0, NULL
, VSOCK_PROTO_INVALID
,
364 static int vmci_transport_reply_reset(struct vmci_transport_packet
*pkt
)
366 return vmci_transport_reply_control_pkt_fast(
368 VMCI_TRANSPORT_PACKET_TYPE_RST
,
370 VMCI_INVALID_HANDLE
);
373 static int vmci_transport_send_invalid_bh(struct sockaddr_vm
*dst
,
374 struct sockaddr_vm
*src
)
376 return vmci_transport_send_control_pkt_bh(
378 VMCI_TRANSPORT_PACKET_TYPE_INVALID
,
379 0, 0, NULL
, VMCI_INVALID_HANDLE
);
382 int vmci_transport_send_wrote_bh(struct sockaddr_vm
*dst
,
383 struct sockaddr_vm
*src
)
385 return vmci_transport_send_control_pkt_bh(
387 VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
388 0, NULL
, VMCI_INVALID_HANDLE
);
391 int vmci_transport_send_read_bh(struct sockaddr_vm
*dst
,
392 struct sockaddr_vm
*src
)
394 return vmci_transport_send_control_pkt_bh(
396 VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
397 0, NULL
, VMCI_INVALID_HANDLE
);
400 int vmci_transport_send_wrote(struct sock
*sk
)
402 return vmci_transport_send_control_pkt(
403 sk
, VMCI_TRANSPORT_PACKET_TYPE_WROTE
, 0,
404 0, NULL
, VSOCK_PROTO_INVALID
,
405 VMCI_INVALID_HANDLE
);
408 int vmci_transport_send_read(struct sock
*sk
)
410 return vmci_transport_send_control_pkt(
411 sk
, VMCI_TRANSPORT_PACKET_TYPE_READ
, 0,
412 0, NULL
, VSOCK_PROTO_INVALID
,
413 VMCI_INVALID_HANDLE
);
416 int vmci_transport_send_waiting_write(struct sock
*sk
,
417 struct vmci_transport_waiting_info
*wait
)
419 return vmci_transport_send_control_pkt(
420 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE
,
421 0, 0, wait
, VSOCK_PROTO_INVALID
,
422 VMCI_INVALID_HANDLE
);
425 int vmci_transport_send_waiting_read(struct sock
*sk
,
426 struct vmci_transport_waiting_info
*wait
)
428 return vmci_transport_send_control_pkt(
429 sk
, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ
,
430 0, 0, wait
, VSOCK_PROTO_INVALID
,
431 VMCI_INVALID_HANDLE
);
434 static int vmci_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
436 return vmci_transport_send_control_pkt(
438 VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
,
441 VMCI_INVALID_HANDLE
);
444 static int vmci_transport_send_conn_request(struct sock
*sk
, size_t size
)
446 return vmci_transport_send_control_pkt(sk
,
447 VMCI_TRANSPORT_PACKET_TYPE_REQUEST
,
450 VMCI_INVALID_HANDLE
);
453 static int vmci_transport_send_conn_request2(struct sock
*sk
, size_t size
,
456 return vmci_transport_send_control_pkt(
457 sk
, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
,
458 size
, 0, NULL
, version
,
459 VMCI_INVALID_HANDLE
);
462 static struct sock
*vmci_transport_get_pending(
463 struct sock
*listener
,
464 struct vmci_transport_packet
*pkt
)
466 struct vsock_sock
*vlistener
;
467 struct vsock_sock
*vpending
;
468 struct sock
*pending
;
470 vlistener
= vsock_sk(listener
);
472 list_for_each_entry(vpending
, &vlistener
->pending_links
,
474 struct sockaddr_vm src
;
475 struct sockaddr_vm dst
;
477 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
478 vsock_addr_init(&dst
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
480 if (vsock_addr_equals_addr(&src
, &vpending
->remote_addr
) &&
481 vsock_addr_equals_addr(&dst
, &vpending
->local_addr
)) {
482 pending
= sk_vsock(vpending
);
494 static void vmci_transport_release_pending(struct sock
*pending
)
499 /* We allow two kinds of sockets to communicate with a restricted VM: 1)
500 * trusted sockets 2) sockets from applications running as the same user as the
501 * VM (this is only true for the host side and only when using hosted products)
504 static bool vmci_transport_is_trusted(struct vsock_sock
*vsock
, u32 peer_cid
)
506 return vsock
->trusted
||
507 vmci_is_context_owner(peer_cid
, vsock
->owner
->uid
);
510 /* We allow sending datagrams to and receiving datagrams from a restricted VM
511 * only if it is trusted as described in vmci_transport_is_trusted.
514 static bool vmci_transport_allow_dgram(struct vsock_sock
*vsock
, u32 peer_cid
)
516 if (vsock
->cached_peer
!= peer_cid
) {
517 vsock
->cached_peer
= peer_cid
;
518 if (!vmci_transport_is_trusted(vsock
, peer_cid
) &&
519 (vmci_context_get_priv_flags(peer_cid
) &
520 VMCI_PRIVILEGE_FLAG_RESTRICTED
)) {
521 vsock
->cached_peer_allow_dgram
= false;
523 vsock
->cached_peer_allow_dgram
= true;
527 return vsock
->cached_peer_allow_dgram
;
531 vmci_transport_queue_pair_alloc(struct vmci_qp
**qpair
,
532 struct vmci_handle
*handle
,
535 u32 peer
, u32 flags
, bool trusted
)
540 /* Try to allocate our queue pair as trusted. This will only
541 * work if vsock is running in the host.
544 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
,
547 VMCI_PRIVILEGE_FLAG_TRUSTED
);
548 if (err
!= VMCI_ERROR_NO_ACCESS
)
553 err
= vmci_qpair_alloc(qpair
, handle
, produce_size
, consume_size
,
554 peer
, flags
, VMCI_NO_PRIVILEGE_FLAGS
);
557 pr_err("Could not attach to queue pair with %d\n",
559 err
= vmci_transport_error_to_vsock_error(err
);
566 vmci_transport_datagram_create_hnd(u32 resource_id
,
568 vmci_datagram_recv_cb recv_cb
,
570 struct vmci_handle
*out_handle
)
574 /* Try to allocate our datagram handler as trusted. This will only work
575 * if vsock is running in the host.
578 err
= vmci_datagram_create_handle_priv(resource_id
, flags
,
579 VMCI_PRIVILEGE_FLAG_TRUSTED
,
581 client_data
, out_handle
);
583 if (err
== VMCI_ERROR_NO_ACCESS
)
584 err
= vmci_datagram_create_handle(resource_id
, flags
,
585 recv_cb
, client_data
,
591 /* This is invoked as part of a tasklet that's scheduled when the VMCI
592 * interrupt fires. This is run in bottom-half context and if it ever needs to
593 * sleep it should defer that work to a work queue.
596 static int vmci_transport_recv_dgram_cb(void *data
, struct vmci_datagram
*dg
)
601 struct vsock_sock
*vsk
;
603 sk
= (struct sock
*)data
;
605 /* This handler is privileged when this module is running on the host.
606 * We will get datagrams from all endpoints (even VMs that are in a
607 * restricted context). If we get one from a restricted context then
608 * the destination socket must be trusted.
610 * NOTE: We access the socket struct without holding the lock here.
611 * This is ok because the field we are interested is never modified
612 * outside of the create and destruct socket functions.
615 if (!vmci_transport_allow_dgram(vsk
, dg
->src
.context
))
616 return VMCI_ERROR_NO_ACCESS
;
618 size
= VMCI_DG_SIZE(dg
);
620 /* Attach the packet to the socket's receive queue as an sk_buff. */
621 skb
= alloc_skb(size
, GFP_ATOMIC
);
623 /* sk_receive_skb() will do a sock_put(), so hold here. */
626 memcpy(skb
->data
, dg
, size
);
627 sk_receive_skb(sk
, skb
, 0);
633 static bool vmci_transport_stream_allow(u32 cid
, u32 port
)
635 static const u32 non_socket_contexts
[] = {
636 VMADDR_CID_HYPERVISOR
,
641 BUILD_BUG_ON(sizeof(cid
) != sizeof(*non_socket_contexts
));
643 for (i
= 0; i
< ARRAY_SIZE(non_socket_contexts
); i
++) {
644 if (cid
== non_socket_contexts
[i
])
651 /* This is invoked as part of a tasklet that's scheduled when the VMCI
652 * interrupt fires. This is run in bottom-half context but it defers most of
653 * its work to the packet handling work queue.
656 static int vmci_transport_recv_stream_cb(void *data
, struct vmci_datagram
*dg
)
659 struct sockaddr_vm dst
;
660 struct sockaddr_vm src
;
661 struct vmci_transport_packet
*pkt
;
662 struct vsock_sock
*vsk
;
668 bh_process_pkt
= false;
670 /* Ignore incoming packets from contexts without sockets, or resources
671 * that aren't vsock implementations.
674 if (!vmci_transport_stream_allow(dg
->src
.context
, -1)
675 || VMCI_TRANSPORT_PACKET_RID
!= dg
->src
.resource
)
676 return VMCI_ERROR_NO_ACCESS
;
678 if (VMCI_DG_SIZE(dg
) < sizeof(*pkt
))
679 /* Drop datagrams that do not contain full VSock packets. */
680 return VMCI_ERROR_INVALID_ARGS
;
682 pkt
= (struct vmci_transport_packet
*)dg
;
684 /* Find the socket that should handle this packet. First we look for a
685 * connected socket and if there is none we look for a socket bound to
686 * the destintation address.
688 vsock_addr_init(&src
, pkt
->dg
.src
.context
, pkt
->src_port
);
689 vsock_addr_init(&dst
, pkt
->dg
.dst
.context
, pkt
->dst_port
);
691 sk
= vsock_find_connected_socket(&src
, &dst
);
693 sk
= vsock_find_bound_socket(&dst
);
695 /* We could not find a socket for this specified
696 * address. If this packet is a RST, we just drop it.
697 * If it is another packet, we send a RST. Note that
698 * we do not send a RST reply to RSTs so that we do not
699 * continually send RSTs between two endpoints.
701 * Note that since this is a reply, dst is src and src
704 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
705 pr_err("unable to send reset\n");
707 err
= VMCI_ERROR_NOT_FOUND
;
712 /* If the received packet type is beyond all types known to this
713 * implementation, reply with an invalid message. Hopefully this will
714 * help when implementing backwards compatibility in the future.
716 if (pkt
->type
>= VMCI_TRANSPORT_PACKET_TYPE_MAX
) {
717 vmci_transport_send_invalid_bh(&dst
, &src
);
718 err
= VMCI_ERROR_INVALID_ARGS
;
722 /* This handler is privileged when this module is running on the host.
723 * We will get datagram connect requests from all endpoints (even VMs
724 * that are in a restricted context). If we get one from a restricted
725 * context then the destination socket must be trusted.
727 * NOTE: We access the socket struct without holding the lock here.
728 * This is ok because the field we are interested is never modified
729 * outside of the create and destruct socket functions.
732 if (!vmci_transport_allow_dgram(vsk
, pkt
->dg
.src
.context
)) {
733 err
= VMCI_ERROR_NO_ACCESS
;
737 /* We do most everything in a work queue, but let's fast path the
738 * notification of reads and writes to help data transfer performance.
739 * We can only do this if there is no process context code executing
740 * for this socket since that may change the state.
744 if (!sock_owned_by_user(sk
) && sk
->sk_state
== SS_CONNECTED
)
745 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
746 sk
, pkt
, true, &dst
, &src
,
751 if (!bh_process_pkt
) {
752 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
754 recv_pkt_info
= kmalloc(sizeof(*recv_pkt_info
), GFP_ATOMIC
);
755 if (!recv_pkt_info
) {
756 if (vmci_transport_send_reset_bh(&dst
, &src
, pkt
) < 0)
757 pr_err("unable to send reset\n");
759 err
= VMCI_ERROR_NO_MEM
;
763 recv_pkt_info
->sk
= sk
;
764 memcpy(&recv_pkt_info
->pkt
, pkt
, sizeof(recv_pkt_info
->pkt
));
765 INIT_WORK(&recv_pkt_info
->work
, vmci_transport_recv_pkt_work
);
767 schedule_work(&recv_pkt_info
->work
);
768 /* Clear sk so that the reference count incremented by one of
769 * the Find functions above is not decremented below. We need
770 * that reference count for the packet handler we've scheduled
783 static void vmci_transport_peer_attach_cb(u32 sub_id
,
784 const struct vmci_event_data
*e_data
,
787 struct sock
*sk
= client_data
;
788 const struct vmci_event_payload_qp
*e_payload
;
789 struct vsock_sock
*vsk
;
791 e_payload
= vmci_event_data_const_payload(e_data
);
795 /* We don't ask for delayed CBs when we subscribe to this event (we
796 * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
797 * guarantees in that case about what context we might be running in,
798 * so it could be BH or process, blockable or non-blockable. So we
799 * need to account for all possible contexts here.
804 /* XXX This is lame, we should provide a way to lookup sockets by
807 if (vmci_handle_is_equal(vmci_trans(vsk
)->qp_handle
,
808 e_payload
->handle
)) {
809 /* XXX This doesn't do anything, but in the future we may want
810 * to set a flag here to verify the attach really did occur and
811 * we weren't just sent a datagram claiming it was.
821 static void vmci_transport_handle_detach(struct sock
*sk
)
823 struct vsock_sock
*vsk
;
826 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)) {
827 sock_set_flag(sk
, SOCK_DONE
);
829 /* On a detach the peer will not be sending or receiving
832 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
834 /* We should not be sending anymore since the peer won't be
835 * there to receive, but we can still receive if there is data
836 * left in our consume queue.
838 if (vsock_stream_has_data(vsk
) <= 0) {
839 if (sk
->sk_state
== SS_CONNECTING
) {
840 /* The peer may detach from a queue pair while
841 * we are still in the connecting state, i.e.,
842 * if the peer VM is killed after attaching to
843 * a queue pair, but before we complete the
844 * handshake. In that case, we treat the detach
845 * event like a reset.
848 sk
->sk_state
= SS_UNCONNECTED
;
849 sk
->sk_err
= ECONNRESET
;
850 sk
->sk_error_report(sk
);
853 sk
->sk_state
= SS_UNCONNECTED
;
855 sk
->sk_state_change(sk
);
859 static void vmci_transport_peer_detach_cb(u32 sub_id
,
860 const struct vmci_event_data
*e_data
,
863 struct sock
*sk
= client_data
;
864 const struct vmci_event_payload_qp
*e_payload
;
865 struct vsock_sock
*vsk
;
867 e_payload
= vmci_event_data_const_payload(e_data
);
869 if (vmci_handle_is_invalid(e_payload
->handle
))
872 /* Same rules for locking as for peer_attach_cb(). */
876 /* XXX This is lame, we should provide a way to lookup sockets by
879 if (vmci_handle_is_equal(vmci_trans(vsk
)->qp_handle
,
881 vmci_transport_handle_detach(sk
);
887 static void vmci_transport_qp_resumed_cb(u32 sub_id
,
888 const struct vmci_event_data
*e_data
,
891 vsock_for_each_connected_socket(vmci_transport_handle_detach
);
894 static void vmci_transport_recv_pkt_work(struct work_struct
*work
)
896 struct vmci_transport_recv_pkt_info
*recv_pkt_info
;
897 struct vmci_transport_packet
*pkt
;
901 container_of(work
, struct vmci_transport_recv_pkt_info
, work
);
902 sk
= recv_pkt_info
->sk
;
903 pkt
= &recv_pkt_info
->pkt
;
907 switch (sk
->sk_state
) {
909 vmci_transport_recv_listen(sk
, pkt
);
912 /* Processing of pending connections for servers goes through
913 * the listening socket, so see vmci_transport_recv_listen()
916 vmci_transport_recv_connecting_client(sk
, pkt
);
919 vmci_transport_recv_connected(sk
, pkt
);
922 /* Because this function does not run in the same context as
923 * vmci_transport_recv_stream_cb it is possible that the
924 * socket has closed. We need to let the other side know or it
925 * could be sitting in a connect and hang forever. Send a
926 * reset to prevent that.
928 vmci_transport_send_reset(sk
, pkt
);
934 kfree(recv_pkt_info
);
935 /* Release reference obtained in the stream callback when we fetched
936 * this socket out of the bound or connected list.
941 static int vmci_transport_recv_listen(struct sock
*sk
,
942 struct vmci_transport_packet
*pkt
)
944 struct sock
*pending
;
945 struct vsock_sock
*vpending
;
948 bool old_request
= false;
949 bool old_pkt_proto
= false;
953 /* Because we are in the listen state, we could be receiving a packet
954 * for ourself or any previous connection requests that we received.
955 * If it's the latter, we try to find a socket in our list of pending
956 * connections and, if we do, call the appropriate handler for the
957 * state that that socket is in. Otherwise we try to service the
958 * connection request.
960 pending
= vmci_transport_get_pending(sk
, pkt
);
963 switch (pending
->sk_state
) {
965 err
= vmci_transport_recv_connecting_server(sk
,
970 vmci_transport_send_reset(pending
, pkt
);
975 vsock_remove_pending(sk
, pending
);
977 release_sock(pending
);
978 vmci_transport_release_pending(pending
);
983 /* The listen state only accepts connection requests. Reply with a
984 * reset unless we received a reset.
987 if (!(pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
||
988 pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)) {
989 vmci_transport_reply_reset(pkt
);
993 if (pkt
->u
.size
== 0) {
994 vmci_transport_reply_reset(pkt
);
998 /* If this socket can't accommodate this connection request, we send a
999 * reset. Otherwise we create and initialize a child socket and reply
1000 * with a connection negotiation.
1002 if (sk
->sk_ack_backlog
>= sk
->sk_max_ack_backlog
) {
1003 vmci_transport_reply_reset(pkt
);
1004 return -ECONNREFUSED
;
1007 pending
= __vsock_create(sock_net(sk
), NULL
, sk
, GFP_KERNEL
,
1010 vmci_transport_send_reset(sk
, pkt
);
1014 vpending
= vsock_sk(pending
);
1016 vsock_addr_init(&vpending
->local_addr
, pkt
->dg
.dst
.context
,
1018 vsock_addr_init(&vpending
->remote_addr
, pkt
->dg
.src
.context
,
1021 /* If the proposed size fits within our min/max, accept it. Otherwise
1022 * propose our own size.
1024 if (pkt
->u
.size
>= vmci_trans(vpending
)->queue_pair_min_size
&&
1025 pkt
->u
.size
<= vmci_trans(vpending
)->queue_pair_max_size
) {
1026 qp_size
= pkt
->u
.size
;
1028 qp_size
= vmci_trans(vpending
)->queue_pair_size
;
1031 /* Figure out if we are using old or new requests based on the
1032 * overrides pkt types sent by our peer.
1034 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1035 old_request
= old_pkt_proto
;
1037 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST
)
1039 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_REQUEST2
)
1040 old_request
= false;
1045 /* Handle a REQUEST (or override) */
1046 u16 version
= VSOCK_PROTO_INVALID
;
1047 if (vmci_transport_proto_to_notify_struct(
1048 pending
, &version
, true))
1049 err
= vmci_transport_send_negotiate(pending
, qp_size
);
1054 /* Handle a REQUEST2 (or override) */
1055 int proto_int
= pkt
->proto
;
1057 u16 active_proto_version
= 0;
1059 /* The list of possible protocols is the intersection of all
1060 * protocols the client supports ... plus all the protocols we
1063 proto_int
&= vmci_transport_new_proto_supported_versions();
1065 /* We choose the highest possible protocol version and use that
1068 pos
= fls(proto_int
);
1070 active_proto_version
= (1 << (pos
- 1));
1071 if (vmci_transport_proto_to_notify_struct(
1072 pending
, &active_proto_version
, false))
1073 err
= vmci_transport_send_negotiate2(pending
,
1075 active_proto_version
);
1085 vmci_transport_send_reset(sk
, pkt
);
1087 err
= vmci_transport_error_to_vsock_error(err
);
1091 vsock_add_pending(sk
, pending
);
1092 sk
->sk_ack_backlog
++;
1094 pending
->sk_state
= SS_CONNECTING
;
1095 vmci_trans(vpending
)->produce_size
=
1096 vmci_trans(vpending
)->consume_size
= qp_size
;
1097 vmci_trans(vpending
)->queue_pair_size
= qp_size
;
1099 vmci_trans(vpending
)->notify_ops
->process_request(pending
);
1101 /* We might never receive another message for this socket and it's not
1102 * connected to any process, so we have to ensure it gets cleaned up
1103 * ourself. Our delayed work function will take care of that. Note
1104 * that we do not ever cancel this function since we have few
1105 * guarantees about its state when calling cancel_delayed_work().
1106 * Instead we hold a reference on the socket for that function and make
1107 * it capable of handling cases where it needs to do nothing but
1108 * release that reference.
1110 vpending
->listener
= sk
;
1113 INIT_DELAYED_WORK(&vpending
->dwork
, vsock_pending_work
);
1114 schedule_delayed_work(&vpending
->dwork
, HZ
);
1121 vmci_transport_recv_connecting_server(struct sock
*listener
,
1122 struct sock
*pending
,
1123 struct vmci_transport_packet
*pkt
)
1125 struct vsock_sock
*vpending
;
1126 struct vmci_handle handle
;
1127 struct vmci_qp
*qpair
;
1134 vpending
= vsock_sk(pending
);
1135 detach_sub_id
= VMCI_INVALID_ID
;
1137 switch (pkt
->type
) {
1138 case VMCI_TRANSPORT_PACKET_TYPE_OFFER
:
1139 if (vmci_handle_is_invalid(pkt
->u
.handle
)) {
1140 vmci_transport_send_reset(pending
, pkt
);
1147 /* Close and cleanup the connection. */
1148 vmci_transport_send_reset(pending
, pkt
);
1150 err
= pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_RST
? 0 : -EINVAL
;
1154 /* In order to complete the connection we need to attach to the offered
1155 * queue pair and send an attach notification. We also subscribe to the
1156 * detach event so we know when our peer goes away, and we do that
1157 * before attaching so we don't miss an event. If all this succeeds,
1158 * we update our state and wakeup anything waiting in accept() for a
1162 /* We don't care about attach since we ensure the other side has
1163 * attached by specifying the ATTACH_ONLY flag below.
1165 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1166 vmci_transport_peer_detach_cb
,
1167 pending
, &detach_sub_id
);
1168 if (err
< VMCI_SUCCESS
) {
1169 vmci_transport_send_reset(pending
, pkt
);
1170 err
= vmci_transport_error_to_vsock_error(err
);
1175 vmci_trans(vpending
)->detach_sub_id
= detach_sub_id
;
1177 /* Now attach to the queue pair the client created. */
1178 handle
= pkt
->u
.handle
;
1180 /* vpending->local_addr always has a context id so we do not need to
1181 * worry about VMADDR_CID_ANY in this case.
1184 vpending
->remote_addr
.svm_cid
== vpending
->local_addr
.svm_cid
;
1185 flags
= VMCI_QPFLAG_ATTACH_ONLY
;
1186 flags
|= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1188 err
= vmci_transport_queue_pair_alloc(
1191 vmci_trans(vpending
)->produce_size
,
1192 vmci_trans(vpending
)->consume_size
,
1193 pkt
->dg
.src
.context
,
1195 vmci_transport_is_trusted(
1197 vpending
->remote_addr
.svm_cid
));
1199 vmci_transport_send_reset(pending
, pkt
);
1204 vmci_trans(vpending
)->qp_handle
= handle
;
1205 vmci_trans(vpending
)->qpair
= qpair
;
1207 /* When we send the attach message, we must be ready to handle incoming
1208 * control messages on the newly connected socket. So we move the
1209 * pending socket to the connected state before sending the attach
1210 * message. Otherwise, an incoming packet triggered by the attach being
1211 * received by the peer may be processed concurrently with what happens
1212 * below after sending the attach message, and that incoming packet
1213 * will find the listening socket instead of the (currently) pending
1214 * socket. Note that enqueueing the socket increments the reference
1215 * count, so even if a reset comes before the connection is accepted,
1216 * the socket will be valid until it is removed from the queue.
1218 * If we fail sending the attach below, we remove the socket from the
1219 * connected list and move the socket to SS_UNCONNECTED before
1220 * releasing the lock, so a pending slow path processing of an incoming
1221 * packet will not see the socket in the connected state in that case.
1223 pending
->sk_state
= SS_CONNECTED
;
1225 vsock_insert_connected(vpending
);
1227 /* Notify our peer of our attach. */
1228 err
= vmci_transport_send_attach(pending
, handle
);
1230 vsock_remove_connected(vpending
);
1231 pr_err("Could not send attach\n");
1232 vmci_transport_send_reset(pending
, pkt
);
1233 err
= vmci_transport_error_to_vsock_error(err
);
1238 /* We have a connection. Move the now connected socket from the
1239 * listener's pending list to the accept queue so callers of accept()
1242 vsock_remove_pending(listener
, pending
);
1243 vsock_enqueue_accept(listener
, pending
);
1245 /* Callers of accept() will be be waiting on the listening socket, not
1246 * the pending socket.
1248 listener
->sk_state_change(listener
);
1253 pending
->sk_err
= skerr
;
1254 pending
->sk_state
= SS_UNCONNECTED
;
1255 /* As long as we drop our reference, all necessary cleanup will handle
1256 * when the cleanup function drops its reference and our destruct
1257 * implementation is called. Note that since the listen handler will
1258 * remove pending from the pending list upon our failure, the cleanup
1259 * function won't drop the additional reference, which is why we do it
1268 vmci_transport_recv_connecting_client(struct sock
*sk
,
1269 struct vmci_transport_packet
*pkt
)
1271 struct vsock_sock
*vsk
;
1277 switch (pkt
->type
) {
1278 case VMCI_TRANSPORT_PACKET_TYPE_ATTACH
:
1279 if (vmci_handle_is_invalid(pkt
->u
.handle
) ||
1280 !vmci_handle_is_equal(pkt
->u
.handle
,
1281 vmci_trans(vsk
)->qp_handle
)) {
1287 /* Signify the socket is connected and wakeup the waiter in
1288 * connect(). Also place the socket in the connected table for
1289 * accounting (it can already be found since it's in the bound
1292 sk
->sk_state
= SS_CONNECTED
;
1293 sk
->sk_socket
->state
= SS_CONNECTED
;
1294 vsock_insert_connected(vsk
);
1295 sk
->sk_state_change(sk
);
1298 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
:
1299 case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
:
1300 if (pkt
->u
.size
== 0
1301 || pkt
->dg
.src
.context
!= vsk
->remote_addr
.svm_cid
1302 || pkt
->src_port
!= vsk
->remote_addr
.svm_port
1303 || !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)
1304 || vmci_trans(vsk
)->qpair
1305 || vmci_trans(vsk
)->produce_size
!= 0
1306 || vmci_trans(vsk
)->consume_size
!= 0
1307 || vmci_trans(vsk
)->attach_sub_id
!= VMCI_INVALID_ID
1308 || vmci_trans(vsk
)->detach_sub_id
!= VMCI_INVALID_ID
) {
1315 err
= vmci_transport_recv_connecting_client_negotiate(sk
, pkt
);
1322 case VMCI_TRANSPORT_PACKET_TYPE_INVALID
:
1323 err
= vmci_transport_recv_connecting_client_invalid(sk
, pkt
);
1330 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1331 /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
1332 * continue processing here after they sent an INVALID packet.
1333 * This meant that we got a RST after the INVALID. We ignore a
1334 * RST after an INVALID. The common code doesn't send the RST
1335 * ... so we can hang if an old version of the common code
1336 * fails between getting a REQUEST and sending an OFFER back.
1337 * Not much we can do about it... except hope that it doesn't
1340 if (vsk
->ignore_connecting_rst
) {
1341 vsk
->ignore_connecting_rst
= false;
1350 /* Close and cleanup the connection. */
1359 vmci_transport_send_reset(sk
, pkt
);
1361 sk
->sk_state
= SS_UNCONNECTED
;
1363 sk
->sk_error_report(sk
);
1367 static int vmci_transport_recv_connecting_client_negotiate(
1369 struct vmci_transport_packet
*pkt
)
1372 struct vsock_sock
*vsk
;
1373 struct vmci_handle handle
;
1374 struct vmci_qp
*qpair
;
1379 bool old_proto
= true;
1384 handle
= VMCI_INVALID_HANDLE
;
1385 attach_sub_id
= VMCI_INVALID_ID
;
1386 detach_sub_id
= VMCI_INVALID_ID
;
1388 /* If we have gotten here then we should be past the point where old
1389 * linux vsock could have sent the bogus rst.
1391 vsk
->sent_request
= false;
1392 vsk
->ignore_connecting_rst
= false;
1394 /* Verify that we're OK with the proposed queue pair size */
1395 if (pkt
->u
.size
< vmci_trans(vsk
)->queue_pair_min_size
||
1396 pkt
->u
.size
> vmci_trans(vsk
)->queue_pair_max_size
) {
1401 /* At this point we know the CID the peer is using to talk to us. */
1403 if (vsk
->local_addr
.svm_cid
== VMADDR_CID_ANY
)
1404 vsk
->local_addr
.svm_cid
= pkt
->dg
.dst
.context
;
1406 /* Setup the notify ops to be the highest supported version that both
1407 * the server and the client support.
1410 if (vmci_transport_old_proto_override(&old_pkt_proto
)) {
1411 old_proto
= old_pkt_proto
;
1413 if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE
)
1415 else if (pkt
->type
== VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2
)
1421 version
= VSOCK_PROTO_INVALID
;
1423 version
= pkt
->proto
;
1425 if (!vmci_transport_proto_to_notify_struct(sk
, &version
, old_proto
)) {
1430 /* Subscribe to attach and detach events first.
1432 * XXX We attach once for each queue pair created for now so it is easy
1433 * to find the socket (it's provided), but later we should only
1434 * subscribe once and add a way to lookup sockets by queue pair handle.
1436 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_ATTACH
,
1437 vmci_transport_peer_attach_cb
,
1438 sk
, &attach_sub_id
);
1439 if (err
< VMCI_SUCCESS
) {
1440 err
= vmci_transport_error_to_vsock_error(err
);
1444 err
= vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH
,
1445 vmci_transport_peer_detach_cb
,
1446 sk
, &detach_sub_id
);
1447 if (err
< VMCI_SUCCESS
) {
1448 err
= vmci_transport_error_to_vsock_error(err
);
1452 /* Make VMCI select the handle for us. */
1453 handle
= VMCI_INVALID_HANDLE
;
1454 is_local
= vsk
->remote_addr
.svm_cid
== vsk
->local_addr
.svm_cid
;
1455 flags
= is_local
? VMCI_QPFLAG_LOCAL
: 0;
1457 err
= vmci_transport_queue_pair_alloc(&qpair
,
1461 vsk
->remote_addr
.svm_cid
,
1463 vmci_transport_is_trusted(
1466 remote_addr
.svm_cid
));
1470 err
= vmci_transport_send_qp_offer(sk
, handle
);
1472 err
= vmci_transport_error_to_vsock_error(err
);
1476 vmci_trans(vsk
)->qp_handle
= handle
;
1477 vmci_trans(vsk
)->qpair
= qpair
;
1479 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
=
1482 vmci_trans(vsk
)->attach_sub_id
= attach_sub_id
;
1483 vmci_trans(vsk
)->detach_sub_id
= detach_sub_id
;
1485 vmci_trans(vsk
)->notify_ops
->process_negotiate(sk
);
1490 if (attach_sub_id
!= VMCI_INVALID_ID
)
1491 vmci_event_unsubscribe(attach_sub_id
);
1493 if (detach_sub_id
!= VMCI_INVALID_ID
)
1494 vmci_event_unsubscribe(detach_sub_id
);
1496 if (!vmci_handle_is_invalid(handle
))
1497 vmci_qpair_detach(&qpair
);
1503 vmci_transport_recv_connecting_client_invalid(struct sock
*sk
,
1504 struct vmci_transport_packet
*pkt
)
1507 struct vsock_sock
*vsk
= vsock_sk(sk
);
1509 if (vsk
->sent_request
) {
1510 vsk
->sent_request
= false;
1511 vsk
->ignore_connecting_rst
= true;
1513 err
= vmci_transport_send_conn_request(
1514 sk
, vmci_trans(vsk
)->queue_pair_size
);
1516 err
= vmci_transport_error_to_vsock_error(err
);
1525 static int vmci_transport_recv_connected(struct sock
*sk
,
1526 struct vmci_transport_packet
*pkt
)
1528 struct vsock_sock
*vsk
;
1529 bool pkt_processed
= false;
1531 /* In cases where we are closing the connection, it's sufficient to
1532 * mark the state change (and maybe error) and wake up any waiting
1533 * threads. Since this is a connected socket, it's owned by a user
1534 * process and will be cleaned up when the failure is passed back on
1535 * the current or next system call. Our system call implementations
1536 * must therefore check for error and state changes on entry and when
1539 switch (pkt
->type
) {
1540 case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN
:
1544 vsk
->peer_shutdown
|= pkt
->u
.mode
;
1545 sk
->sk_state_change(sk
);
1549 case VMCI_TRANSPORT_PACKET_TYPE_RST
:
1551 /* It is possible that we sent our peer a message (e.g a
1552 * WAITING_READ) right before we got notified that the peer had
1553 * detached. If that happens then we can get a RST pkt back
1554 * from our peer even though there is data available for us to
1555 * read. In that case, don't shutdown the socket completely but
1556 * instead allow the local client to finish reading data off
1557 * the queuepair. Always treat a RST pkt in connected mode like
1560 sock_set_flag(sk
, SOCK_DONE
);
1561 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
1562 if (vsock_stream_has_data(vsk
) <= 0)
1563 sk
->sk_state
= SS_DISCONNECTING
;
1565 sk
->sk_state_change(sk
);
1570 vmci_trans(vsk
)->notify_ops
->handle_notify_pkt(
1571 sk
, pkt
, false, NULL
, NULL
,
1582 static int vmci_transport_socket_init(struct vsock_sock
*vsk
,
1583 struct vsock_sock
*psk
)
1585 vsk
->trans
= kmalloc(sizeof(struct vmci_transport
), GFP_KERNEL
);
1589 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1590 vmci_trans(vsk
)->qp_handle
= VMCI_INVALID_HANDLE
;
1591 vmci_trans(vsk
)->qpair
= NULL
;
1592 vmci_trans(vsk
)->produce_size
= vmci_trans(vsk
)->consume_size
= 0;
1593 vmci_trans(vsk
)->attach_sub_id
= vmci_trans(vsk
)->detach_sub_id
=
1595 vmci_trans(vsk
)->notify_ops
= NULL
;
1597 vmci_trans(vsk
)->queue_pair_size
=
1598 vmci_trans(psk
)->queue_pair_size
;
1599 vmci_trans(vsk
)->queue_pair_min_size
=
1600 vmci_trans(psk
)->queue_pair_min_size
;
1601 vmci_trans(vsk
)->queue_pair_max_size
=
1602 vmci_trans(psk
)->queue_pair_max_size
;
1604 vmci_trans(vsk
)->queue_pair_size
=
1605 VMCI_TRANSPORT_DEFAULT_QP_SIZE
;
1606 vmci_trans(vsk
)->queue_pair_min_size
=
1607 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN
;
1608 vmci_trans(vsk
)->queue_pair_max_size
=
1609 VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX
;
1615 static void vmci_transport_destruct(struct vsock_sock
*vsk
)
1617 if (vmci_trans(vsk
)->attach_sub_id
!= VMCI_INVALID_ID
) {
1618 vmci_event_unsubscribe(vmci_trans(vsk
)->attach_sub_id
);
1619 vmci_trans(vsk
)->attach_sub_id
= VMCI_INVALID_ID
;
1622 if (vmci_trans(vsk
)->detach_sub_id
!= VMCI_INVALID_ID
) {
1623 vmci_event_unsubscribe(vmci_trans(vsk
)->detach_sub_id
);
1624 vmci_trans(vsk
)->detach_sub_id
= VMCI_INVALID_ID
;
1627 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
)) {
1628 vmci_qpair_detach(&vmci_trans(vsk
)->qpair
);
1629 vmci_trans(vsk
)->qp_handle
= VMCI_INVALID_HANDLE
;
1630 vmci_trans(vsk
)->produce_size
= 0;
1631 vmci_trans(vsk
)->consume_size
= 0;
1634 if (vmci_trans(vsk
)->notify_ops
)
1635 vmci_trans(vsk
)->notify_ops
->socket_destruct(vsk
);
1641 static void vmci_transport_release(struct vsock_sock
*vsk
)
1643 if (!vmci_handle_is_invalid(vmci_trans(vsk
)->dg_handle
)) {
1644 vmci_datagram_destroy_handle(vmci_trans(vsk
)->dg_handle
);
1645 vmci_trans(vsk
)->dg_handle
= VMCI_INVALID_HANDLE
;
1649 static int vmci_transport_dgram_bind(struct vsock_sock
*vsk
,
1650 struct sockaddr_vm
*addr
)
1656 /* VMCI will select a resource ID for us if we provide
1659 port
= addr
->svm_port
== VMADDR_PORT_ANY
?
1660 VMCI_INVALID_ID
: addr
->svm_port
;
1662 if (port
<= LAST_RESERVED_PORT
&& !capable(CAP_NET_BIND_SERVICE
))
1665 flags
= addr
->svm_cid
== VMADDR_CID_ANY
?
1666 VMCI_FLAG_ANYCID_DG_HND
: 0;
1668 err
= vmci_transport_datagram_create_hnd(port
, flags
,
1669 vmci_transport_recv_dgram_cb
,
1671 &vmci_trans(vsk
)->dg_handle
);
1672 if (err
< VMCI_SUCCESS
)
1673 return vmci_transport_error_to_vsock_error(err
);
1674 vsock_addr_init(&vsk
->local_addr
, addr
->svm_cid
,
1675 vmci_trans(vsk
)->dg_handle
.resource
);
1680 static int vmci_transport_dgram_enqueue(
1681 struct vsock_sock
*vsk
,
1682 struct sockaddr_vm
*remote_addr
,
1687 struct vmci_datagram
*dg
;
1689 if (len
> VMCI_MAX_DG_PAYLOAD_SIZE
)
1692 if (!vmci_transport_allow_dgram(vsk
, remote_addr
->svm_cid
))
1695 /* Allocate a buffer for the user's message and our packet header. */
1696 dg
= kmalloc(len
+ sizeof(*dg
), GFP_KERNEL
);
1700 memcpy_fromiovec(VMCI_DG_PAYLOAD(dg
), iov
, len
);
1702 dg
->dst
= vmci_make_handle(remote_addr
->svm_cid
,
1703 remote_addr
->svm_port
);
1704 dg
->src
= vmci_make_handle(vsk
->local_addr
.svm_cid
,
1705 vsk
->local_addr
.svm_port
);
1706 dg
->payload_size
= len
;
1708 err
= vmci_datagram_send(dg
);
1711 return vmci_transport_error_to_vsock_error(err
);
1713 return err
- sizeof(*dg
);
1716 static int vmci_transport_dgram_dequeue(struct kiocb
*kiocb
,
1717 struct vsock_sock
*vsk
,
1718 struct msghdr
*msg
, size_t len
,
1723 struct vmci_datagram
*dg
;
1725 struct sk_buff
*skb
;
1727 noblock
= flags
& MSG_DONTWAIT
;
1729 if (flags
& MSG_OOB
|| flags
& MSG_ERRQUEUE
)
1732 /* Retrieve the head sk_buff from the socket's receive queue. */
1734 skb
= skb_recv_datagram(&vsk
->sk
, flags
, noblock
, &err
);
1741 dg
= (struct vmci_datagram
*)skb
->data
;
1743 /* err is 0, meaning we read zero bytes. */
1746 payload_len
= dg
->payload_size
;
1747 /* Ensure the sk_buff matches the payload size claimed in the packet. */
1748 if (payload_len
!= skb
->len
- sizeof(*dg
)) {
1753 if (payload_len
> len
) {
1755 msg
->msg_flags
|= MSG_TRUNC
;
1758 /* Place the datagram payload in the user's iovec. */
1759 err
= skb_copy_datagram_iovec(skb
, sizeof(*dg
), msg
->msg_iov
,
1764 msg
->msg_namelen
= 0;
1765 if (msg
->msg_name
) {
1766 struct sockaddr_vm
*vm_addr
;
1768 /* Provide the address of the sender. */
1769 vm_addr
= (struct sockaddr_vm
*)msg
->msg_name
;
1770 vsock_addr_init(vm_addr
, dg
->src
.context
, dg
->src
.resource
);
1771 msg
->msg_namelen
= sizeof(*vm_addr
);
1776 skb_free_datagram(&vsk
->sk
, skb
);
1780 static bool vmci_transport_dgram_allow(u32 cid
, u32 port
)
1782 if (cid
== VMADDR_CID_HYPERVISOR
) {
1783 /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
1784 * state and are allowed.
1786 return port
== VMCI_UNITY_PBRPC_REGISTER
;
1792 static int vmci_transport_connect(struct vsock_sock
*vsk
)
1795 bool old_pkt_proto
= false;
1796 struct sock
*sk
= &vsk
->sk
;
1798 if (vmci_transport_old_proto_override(&old_pkt_proto
) &&
1800 err
= vmci_transport_send_conn_request(
1801 sk
, vmci_trans(vsk
)->queue_pair_size
);
1803 sk
->sk_state
= SS_UNCONNECTED
;
1807 int supported_proto_versions
=
1808 vmci_transport_new_proto_supported_versions();
1809 err
= vmci_transport_send_conn_request2(
1810 sk
, vmci_trans(vsk
)->queue_pair_size
,
1811 supported_proto_versions
);
1813 sk
->sk_state
= SS_UNCONNECTED
;
1817 vsk
->sent_request
= true;
1823 static ssize_t
vmci_transport_stream_dequeue(
1824 struct vsock_sock
*vsk
,
1829 if (flags
& MSG_PEEK
)
1830 return vmci_qpair_peekv(vmci_trans(vsk
)->qpair
, iov
, len
, 0);
1832 return vmci_qpair_dequev(vmci_trans(vsk
)->qpair
, iov
, len
, 0);
1835 static ssize_t
vmci_transport_stream_enqueue(
1836 struct vsock_sock
*vsk
,
1840 return vmci_qpair_enquev(vmci_trans(vsk
)->qpair
, iov
, len
, 0);
1843 static s64
vmci_transport_stream_has_data(struct vsock_sock
*vsk
)
1845 return vmci_qpair_consume_buf_ready(vmci_trans(vsk
)->qpair
);
1848 static s64
vmci_transport_stream_has_space(struct vsock_sock
*vsk
)
1850 return vmci_qpair_produce_free_space(vmci_trans(vsk
)->qpair
);
1853 static u64
vmci_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
1855 return vmci_trans(vsk
)->consume_size
;
1858 static bool vmci_transport_stream_is_active(struct vsock_sock
*vsk
)
1860 return !vmci_handle_is_invalid(vmci_trans(vsk
)->qp_handle
);
1863 static u64
vmci_transport_get_buffer_size(struct vsock_sock
*vsk
)
1865 return vmci_trans(vsk
)->queue_pair_size
;
1868 static u64
vmci_transport_get_min_buffer_size(struct vsock_sock
*vsk
)
1870 return vmci_trans(vsk
)->queue_pair_min_size
;
1873 static u64
vmci_transport_get_max_buffer_size(struct vsock_sock
*vsk
)
1875 return vmci_trans(vsk
)->queue_pair_max_size
;
1878 static void vmci_transport_set_buffer_size(struct vsock_sock
*vsk
, u64 val
)
1880 if (val
< vmci_trans(vsk
)->queue_pair_min_size
)
1881 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1882 if (val
> vmci_trans(vsk
)->queue_pair_max_size
)
1883 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1884 vmci_trans(vsk
)->queue_pair_size
= val
;
1887 static void vmci_transport_set_min_buffer_size(struct vsock_sock
*vsk
,
1890 if (val
> vmci_trans(vsk
)->queue_pair_size
)
1891 vmci_trans(vsk
)->queue_pair_size
= val
;
1892 vmci_trans(vsk
)->queue_pair_min_size
= val
;
1895 static void vmci_transport_set_max_buffer_size(struct vsock_sock
*vsk
,
1898 if (val
< vmci_trans(vsk
)->queue_pair_size
)
1899 vmci_trans(vsk
)->queue_pair_size
= val
;
1900 vmci_trans(vsk
)->queue_pair_max_size
= val
;
1903 static int vmci_transport_notify_poll_in(
1904 struct vsock_sock
*vsk
,
1906 bool *data_ready_now
)
1908 return vmci_trans(vsk
)->notify_ops
->poll_in(
1909 &vsk
->sk
, target
, data_ready_now
);
1912 static int vmci_transport_notify_poll_out(
1913 struct vsock_sock
*vsk
,
1915 bool *space_available_now
)
1917 return vmci_trans(vsk
)->notify_ops
->poll_out(
1918 &vsk
->sk
, target
, space_available_now
);
1921 static int vmci_transport_notify_recv_init(
1922 struct vsock_sock
*vsk
,
1924 struct vsock_transport_recv_notify_data
*data
)
1926 return vmci_trans(vsk
)->notify_ops
->recv_init(
1928 (struct vmci_transport_recv_notify_data
*)data
);
1931 static int vmci_transport_notify_recv_pre_block(
1932 struct vsock_sock
*vsk
,
1934 struct vsock_transport_recv_notify_data
*data
)
1936 return vmci_trans(vsk
)->notify_ops
->recv_pre_block(
1938 (struct vmci_transport_recv_notify_data
*)data
);
1941 static int vmci_transport_notify_recv_pre_dequeue(
1942 struct vsock_sock
*vsk
,
1944 struct vsock_transport_recv_notify_data
*data
)
1946 return vmci_trans(vsk
)->notify_ops
->recv_pre_dequeue(
1948 (struct vmci_transport_recv_notify_data
*)data
);
1951 static int vmci_transport_notify_recv_post_dequeue(
1952 struct vsock_sock
*vsk
,
1956 struct vsock_transport_recv_notify_data
*data
)
1958 return vmci_trans(vsk
)->notify_ops
->recv_post_dequeue(
1959 &vsk
->sk
, target
, copied
, data_read
,
1960 (struct vmci_transport_recv_notify_data
*)data
);
1963 static int vmci_transport_notify_send_init(
1964 struct vsock_sock
*vsk
,
1965 struct vsock_transport_send_notify_data
*data
)
1967 return vmci_trans(vsk
)->notify_ops
->send_init(
1969 (struct vmci_transport_send_notify_data
*)data
);
1972 static int vmci_transport_notify_send_pre_block(
1973 struct vsock_sock
*vsk
,
1974 struct vsock_transport_send_notify_data
*data
)
1976 return vmci_trans(vsk
)->notify_ops
->send_pre_block(
1978 (struct vmci_transport_send_notify_data
*)data
);
1981 static int vmci_transport_notify_send_pre_enqueue(
1982 struct vsock_sock
*vsk
,
1983 struct vsock_transport_send_notify_data
*data
)
1985 return vmci_trans(vsk
)->notify_ops
->send_pre_enqueue(
1987 (struct vmci_transport_send_notify_data
*)data
);
1990 static int vmci_transport_notify_send_post_enqueue(
1991 struct vsock_sock
*vsk
,
1993 struct vsock_transport_send_notify_data
*data
)
1995 return vmci_trans(vsk
)->notify_ops
->send_post_enqueue(
1997 (struct vmci_transport_send_notify_data
*)data
);
2000 static bool vmci_transport_old_proto_override(bool *old_pkt_proto
)
2002 if (PROTOCOL_OVERRIDE
!= -1) {
2003 if (PROTOCOL_OVERRIDE
== 0)
2004 *old_pkt_proto
= true;
2006 *old_pkt_proto
= false;
2008 pr_info("Proto override in use\n");
2015 static bool vmci_transport_proto_to_notify_struct(struct sock
*sk
,
2019 struct vsock_sock
*vsk
= vsock_sk(sk
);
2021 if (old_pkt_proto
) {
2022 if (*proto
!= VSOCK_PROTO_INVALID
) {
2023 pr_err("Can't set both an old and new protocol\n");
2026 vmci_trans(vsk
)->notify_ops
= &vmci_transport_notify_pkt_ops
;
2031 case VSOCK_PROTO_PKT_ON_NOTIFY
:
2032 vmci_trans(vsk
)->notify_ops
=
2033 &vmci_transport_notify_pkt_q_state_ops
;
2036 pr_err("Unknown notify protocol version\n");
2041 vmci_trans(vsk
)->notify_ops
->socket_init(sk
);
2045 static u16
vmci_transport_new_proto_supported_versions(void)
2047 if (PROTOCOL_OVERRIDE
!= -1)
2048 return PROTOCOL_OVERRIDE
;
2050 return VSOCK_PROTO_ALL_SUPPORTED
;
2053 static u32
vmci_transport_get_local_cid(void)
2055 return vmci_get_context_id();
2058 static struct vsock_transport vmci_transport
= {
2059 .init
= vmci_transport_socket_init
,
2060 .destruct
= vmci_transport_destruct
,
2061 .release
= vmci_transport_release
,
2062 .connect
= vmci_transport_connect
,
2063 .dgram_bind
= vmci_transport_dgram_bind
,
2064 .dgram_dequeue
= vmci_transport_dgram_dequeue
,
2065 .dgram_enqueue
= vmci_transport_dgram_enqueue
,
2066 .dgram_allow
= vmci_transport_dgram_allow
,
2067 .stream_dequeue
= vmci_transport_stream_dequeue
,
2068 .stream_enqueue
= vmci_transport_stream_enqueue
,
2069 .stream_has_data
= vmci_transport_stream_has_data
,
2070 .stream_has_space
= vmci_transport_stream_has_space
,
2071 .stream_rcvhiwat
= vmci_transport_stream_rcvhiwat
,
2072 .stream_is_active
= vmci_transport_stream_is_active
,
2073 .stream_allow
= vmci_transport_stream_allow
,
2074 .notify_poll_in
= vmci_transport_notify_poll_in
,
2075 .notify_poll_out
= vmci_transport_notify_poll_out
,
2076 .notify_recv_init
= vmci_transport_notify_recv_init
,
2077 .notify_recv_pre_block
= vmci_transport_notify_recv_pre_block
,
2078 .notify_recv_pre_dequeue
= vmci_transport_notify_recv_pre_dequeue
,
2079 .notify_recv_post_dequeue
= vmci_transport_notify_recv_post_dequeue
,
2080 .notify_send_init
= vmci_transport_notify_send_init
,
2081 .notify_send_pre_block
= vmci_transport_notify_send_pre_block
,
2082 .notify_send_pre_enqueue
= vmci_transport_notify_send_pre_enqueue
,
2083 .notify_send_post_enqueue
= vmci_transport_notify_send_post_enqueue
,
2084 .shutdown
= vmci_transport_shutdown
,
2085 .set_buffer_size
= vmci_transport_set_buffer_size
,
2086 .set_min_buffer_size
= vmci_transport_set_min_buffer_size
,
2087 .set_max_buffer_size
= vmci_transport_set_max_buffer_size
,
2088 .get_buffer_size
= vmci_transport_get_buffer_size
,
2089 .get_min_buffer_size
= vmci_transport_get_min_buffer_size
,
2090 .get_max_buffer_size
= vmci_transport_get_max_buffer_size
,
2091 .get_local_cid
= vmci_transport_get_local_cid
,
2094 static int __init
vmci_transport_init(void)
2098 /* Create the datagram handle that we will use to send and receive all
2099 * VSocket control messages for this context.
2101 err
= vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID
,
2102 VMCI_FLAG_ANYCID_DG_HND
,
2103 vmci_transport_recv_stream_cb
,
2105 &vmci_transport_stream_handle
);
2106 if (err
< VMCI_SUCCESS
) {
2107 pr_err("Unable to create datagram handle. (%d)\n", err
);
2108 return vmci_transport_error_to_vsock_error(err
);
2111 err
= vmci_event_subscribe(VMCI_EVENT_QP_RESUMED
,
2112 vmci_transport_qp_resumed_cb
,
2113 NULL
, &vmci_transport_qp_resumed_sub_id
);
2114 if (err
< VMCI_SUCCESS
) {
2115 pr_err("Unable to subscribe to resumed event. (%d)\n", err
);
2116 err
= vmci_transport_error_to_vsock_error(err
);
2117 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2118 goto err_destroy_stream_handle
;
2121 err
= vsock_core_init(&vmci_transport
);
2123 goto err_unsubscribe
;
2128 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2129 err_destroy_stream_handle
:
2130 vmci_datagram_destroy_handle(vmci_transport_stream_handle
);
2133 module_init(vmci_transport_init
);
2135 static void __exit
vmci_transport_exit(void)
2137 if (!vmci_handle_is_invalid(vmci_transport_stream_handle
)) {
2138 if (vmci_datagram_destroy_handle(
2139 vmci_transport_stream_handle
) != VMCI_SUCCESS
)
2140 pr_err("Couldn't destroy datagram handle\n");
2141 vmci_transport_stream_handle
= VMCI_INVALID_HANDLE
;
2144 if (vmci_transport_qp_resumed_sub_id
!= VMCI_INVALID_ID
) {
2145 vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id
);
2146 vmci_transport_qp_resumed_sub_id
= VMCI_INVALID_ID
;
2151 module_exit(vmci_transport_exit
);
2153 MODULE_AUTHOR("VMware, Inc.");
2154 MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
2155 MODULE_LICENSE("GPL v2");
2156 MODULE_ALIAS("vmware_vsock");
2157 MODULE_ALIAS_NETPROTO(PF_VSOCK
);