4 * Copyright (C) 2012 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
8 * Free Software Foundation version 2 and no later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <linux/vmw_vmci_defs.h>
17 #include <linux/vmw_vmci_api.h>
19 #include "vmci_context.h"
20 #include "vmci_driver.h"
21 #include "vmci_route.h"
24 * Make a routing decision for the given source and destination handles.
25 * This will try to determine the route using the handles and the available
26 * devices. Will set the source context if it is invalid.
28 int vmci_route(struct vmci_handle *src,
29 const struct vmci_handle *dst,
31 enum vmci_route *route)
33 bool has_host_device = vmci_host_code_active();
34 bool has_guest_device = vmci_guest_code_active();
36 *route = VMCI_ROUTE_NONE;
39 * "from_guest" is only ever set to true by
40 * IOCTL_VMCI_DATAGRAM_SEND (or by the vmkernel equivalent),
41 * which comes from the VMX, so we know it is coming from a
44 * To avoid inconsistencies, test these once. We will test
45 * them again when we do the actual send to ensure that we do
46 * not touch a non-existent device.
49 /* Must have a valid destination context. */
50 if (VMCI_INVALID_ID == dst->context)
51 return VMCI_ERROR_INVALID_ARGS;
53 /* Anywhere to hypervisor. */
54 if (VMCI_HYPERVISOR_CONTEXT_ID == dst->context) {
57 * If this message already came from a guest then we
58 * cannot send it to the hypervisor. It must come
59 * from a local client.
62 return VMCI_ERROR_DST_UNREACHABLE;
65 * We must be acting as a guest in order to send to
68 if (!has_guest_device)
69 return VMCI_ERROR_DEVICE_NOT_FOUND;
71 /* And we cannot send if the source is the host context. */
72 if (VMCI_HOST_CONTEXT_ID == src->context)
73 return VMCI_ERROR_INVALID_ARGS;
76 * If the client passed the ANON source handle then
77 * respect it (both context and resource are invalid).
78 * However, if they passed only an invalid context,
79 * then they probably mean ANY, in which case we
80 * should set the real context here before passing it
83 if (VMCI_INVALID_ID == src->context &&
84 VMCI_INVALID_ID != src->resource)
85 src->context = vmci_get_context_id();
87 /* Send from local client down to the hypervisor. */
88 *route = VMCI_ROUTE_AS_GUEST;
92 /* Anywhere to local client on host. */
93 if (VMCI_HOST_CONTEXT_ID == dst->context) {
95 * If it is not from a guest but we are acting as a
96 * guest, then we need to send it down to the host.
97 * Note that if we are also acting as a host then this
98 * will prevent us from sending from local client to
99 * local client, but we accept that restriction as a
100 * way to remove any ambiguity from the host context.
102 if (src->context == VMCI_HYPERVISOR_CONTEXT_ID) {
104 * If the hypervisor is the source, this is
105 * host local communication. The hypervisor
106 * may send vmci event datagrams to the host
107 * itself, but it will never send datagrams to
108 * an "outer host" through the guest device.
111 if (has_host_device) {
112 *route = VMCI_ROUTE_AS_HOST;
115 return VMCI_ERROR_DEVICE_NOT_FOUND;
119 if (!from_guest && has_guest_device) {
120 /* If no source context then use the current. */
121 if (VMCI_INVALID_ID == src->context)
122 src->context = vmci_get_context_id();
124 /* Send it from local client down to the host. */
125 *route = VMCI_ROUTE_AS_GUEST;
130 * Otherwise we already received it from a guest and
131 * it is destined for a local client on this host, or
132 * it is from another local client on this host. We
133 * must be acting as a host to service it.
135 if (!has_host_device)
136 return VMCI_ERROR_DEVICE_NOT_FOUND;
138 if (VMCI_INVALID_ID == src->context) {
140 * If it came from a guest then it must have a
141 * valid context. Otherwise we can use the
145 return VMCI_ERROR_INVALID_ARGS;
147 src->context = VMCI_HOST_CONTEXT_ID;
150 /* Route to local client. */
151 *route = VMCI_ROUTE_AS_HOST;
156 * If we are acting as a host then this might be destined for
159 if (has_host_device) {
160 /* It will have a context if it is meant for a guest. */
161 if (vmci_ctx_exists(dst->context)) {
162 if (VMCI_INVALID_ID == src->context) {
164 * If it came from a guest then it
165 * must have a valid context.
166 * Otherwise we can use the host
171 return VMCI_ERROR_INVALID_ARGS;
173 src->context = VMCI_HOST_CONTEXT_ID;
174 } else if (VMCI_CONTEXT_IS_VM(src->context) &&
175 src->context != dst->context) {
177 * VM to VM communication is not
178 * allowed. Since we catch all
179 * communication destined for the host
180 * above, this must be destined for a
181 * VM since there is a valid context.
184 return VMCI_ERROR_DST_UNREACHABLE;
187 /* Pass it up to the guest. */
188 *route = VMCI_ROUTE_AS_HOST;
190 } else if (!has_guest_device) {
192 * The host is attempting to reach a CID
193 * without an active context, and we can't
194 * send it down, since we have no guest
198 return VMCI_ERROR_DST_UNREACHABLE;
203 * We must be a guest trying to send to another guest, which means
204 * we need to send it down to the host. We do not filter out VM to
205 * VM communication here, since we want to be able to use the guest
206 * driver on older versions that do support VM to VM communication.
208 if (!has_guest_device) {
210 * Ending up here means we have neither guest nor host
213 return VMCI_ERROR_DEVICE_NOT_FOUND;
216 /* If no source context then use the current context. */
217 if (VMCI_INVALID_ID == src->context)
218 src->context = vmci_get_context_id();
221 * Send it from local client down to the host, which will
222 * route it to the other guest for us.
224 *route = VMCI_ROUTE_AS_GUEST;