1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
11 #include "vmci_context.h"
12 #include "vmci_driver.h"
13 #include "vmci_route.h"
16 * Make a routing decision for the given source and destination handles.
17 * This will try to determine the route using the handles and the available
18 * devices. Will set the source context if it is invalid.
20 int vmci_route(struct vmci_handle
*src
,
21 const struct vmci_handle
*dst
,
23 enum vmci_route
*route
)
25 bool has_host_device
= vmci_host_code_active();
26 bool has_guest_device
= vmci_guest_code_active();
28 *route
= VMCI_ROUTE_NONE
;
31 * "from_guest" is only ever set to true by
32 * IOCTL_VMCI_DATAGRAM_SEND (or by the vmkernel equivalent),
33 * which comes from the VMX, so we know it is coming from a
36 * To avoid inconsistencies, test these once. We will test
37 * them again when we do the actual send to ensure that we do
38 * not touch a non-existent device.
41 /* Must have a valid destination context. */
42 if (VMCI_INVALID_ID
== dst
->context
)
43 return VMCI_ERROR_INVALID_ARGS
;
45 /* Anywhere to hypervisor. */
46 if (VMCI_HYPERVISOR_CONTEXT_ID
== dst
->context
) {
49 * If this message already came from a guest then we
50 * cannot send it to the hypervisor. It must come
51 * from a local client.
54 return VMCI_ERROR_DST_UNREACHABLE
;
57 * We must be acting as a guest in order to send to
60 if (!has_guest_device
)
61 return VMCI_ERROR_DEVICE_NOT_FOUND
;
63 /* And we cannot send if the source is the host context. */
64 if (VMCI_HOST_CONTEXT_ID
== src
->context
)
65 return VMCI_ERROR_INVALID_ARGS
;
68 * If the client passed the ANON source handle then
69 * respect it (both context and resource are invalid).
70 * However, if they passed only an invalid context,
71 * then they probably mean ANY, in which case we
72 * should set the real context here before passing it
75 if (VMCI_INVALID_ID
== src
->context
&&
76 VMCI_INVALID_ID
!= src
->resource
)
77 src
->context
= vmci_get_context_id();
79 /* Send from local client down to the hypervisor. */
80 *route
= VMCI_ROUTE_AS_GUEST
;
84 /* Anywhere to local client on host. */
85 if (VMCI_HOST_CONTEXT_ID
== dst
->context
) {
87 * If it is not from a guest but we are acting as a
88 * guest, then we need to send it down to the host.
89 * Note that if we are also acting as a host then this
90 * will prevent us from sending from local client to
91 * local client, but we accept that restriction as a
92 * way to remove any ambiguity from the host context.
94 if (src
->context
== VMCI_HYPERVISOR_CONTEXT_ID
) {
96 * If the hypervisor is the source, this is
97 * host local communication. The hypervisor
98 * may send vmci event datagrams to the host
99 * itself, but it will never send datagrams to
100 * an "outer host" through the guest device.
103 if (has_host_device
) {
104 *route
= VMCI_ROUTE_AS_HOST
;
107 return VMCI_ERROR_DEVICE_NOT_FOUND
;
111 if (!from_guest
&& has_guest_device
) {
112 /* If no source context then use the current. */
113 if (VMCI_INVALID_ID
== src
->context
)
114 src
->context
= vmci_get_context_id();
116 /* Send it from local client down to the host. */
117 *route
= VMCI_ROUTE_AS_GUEST
;
122 * Otherwise we already received it from a guest and
123 * it is destined for a local client on this host, or
124 * it is from another local client on this host. We
125 * must be acting as a host to service it.
127 if (!has_host_device
)
128 return VMCI_ERROR_DEVICE_NOT_FOUND
;
130 if (VMCI_INVALID_ID
== src
->context
) {
132 * If it came from a guest then it must have a
133 * valid context. Otherwise we can use the
137 return VMCI_ERROR_INVALID_ARGS
;
139 src
->context
= VMCI_HOST_CONTEXT_ID
;
142 /* Route to local client. */
143 *route
= VMCI_ROUTE_AS_HOST
;
148 * If we are acting as a host then this might be destined for
151 if (has_host_device
) {
152 /* It will have a context if it is meant for a guest. */
153 if (vmci_ctx_exists(dst
->context
)) {
154 if (VMCI_INVALID_ID
== src
->context
) {
156 * If it came from a guest then it
157 * must have a valid context.
158 * Otherwise we can use the host
163 return VMCI_ERROR_INVALID_ARGS
;
165 src
->context
= VMCI_HOST_CONTEXT_ID
;
166 } else if (VMCI_CONTEXT_IS_VM(src
->context
) &&
167 src
->context
!= dst
->context
) {
169 * VM to VM communication is not
170 * allowed. Since we catch all
171 * communication destined for the host
172 * above, this must be destined for a
173 * VM since there is a valid context.
176 return VMCI_ERROR_DST_UNREACHABLE
;
179 /* Pass it up to the guest. */
180 *route
= VMCI_ROUTE_AS_HOST
;
182 } else if (!has_guest_device
) {
184 * The host is attempting to reach a CID
185 * without an active context, and we can't
186 * send it down, since we have no guest
190 return VMCI_ERROR_DST_UNREACHABLE
;
195 * We must be a guest trying to send to another guest, which means
196 * we need to send it down to the host. We do not filter out VM to
197 * VM communication here, since we want to be able to use the guest
198 * driver on older versions that do support VM to VM communication.
200 if (!has_guest_device
) {
202 * Ending up here means we have neither guest nor host
205 return VMCI_ERROR_DEVICE_NOT_FOUND
;
208 /* If no source context then use the current context. */
209 if (VMCI_INVALID_ID
== src
->context
)
210 src
->context
= vmci_get_context_id();
213 * Send it from local client down to the host, which will
214 * route it to the other guest for us.
216 *route
= VMCI_ROUTE_AS_GUEST
;