1685a6bf8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e76ffea3SGeorge Zhang /*
3e76ffea3SGeorge Zhang  * VMware VMCI Driver
4e76ffea3SGeorge Zhang  *
5e76ffea3SGeorge Zhang  * Copyright (C) 2012 VMware, Inc. All rights reserved.
6e76ffea3SGeorge Zhang  */
7e76ffea3SGeorge Zhang 
8e76ffea3SGeorge Zhang #include <linux/vmw_vmci_defs.h>
9e76ffea3SGeorge Zhang #include <linux/vmw_vmci_api.h>
10e76ffea3SGeorge Zhang 
11e76ffea3SGeorge Zhang #include "vmci_context.h"
12e76ffea3SGeorge Zhang #include "vmci_driver.h"
13e76ffea3SGeorge Zhang #include "vmci_route.h"
14e76ffea3SGeorge Zhang 
15e76ffea3SGeorge Zhang /*
16e76ffea3SGeorge Zhang  * Make a routing decision for the given source and destination handles.
17e76ffea3SGeorge Zhang  * This will try to determine the route using the handles and the available
18e76ffea3SGeorge Zhang  * devices.  Will set the source context if it is invalid.
19e76ffea3SGeorge Zhang  */
vmci_route(struct vmci_handle * src,const struct vmci_handle * dst,bool from_guest,enum vmci_route * route)20e76ffea3SGeorge Zhang int vmci_route(struct vmci_handle *src,
21e76ffea3SGeorge Zhang 	       const struct vmci_handle *dst,
22e76ffea3SGeorge Zhang 	       bool from_guest,
23e76ffea3SGeorge Zhang 	       enum vmci_route *route)
24e76ffea3SGeorge Zhang {
25e76ffea3SGeorge Zhang 	bool has_host_device = vmci_host_code_active();
26e76ffea3SGeorge Zhang 	bool has_guest_device = vmci_guest_code_active();
27e76ffea3SGeorge Zhang 
28e76ffea3SGeorge Zhang 	*route = VMCI_ROUTE_NONE;
29e76ffea3SGeorge Zhang 
30e76ffea3SGeorge Zhang 	/*
31e76ffea3SGeorge Zhang 	 * "from_guest" is only ever set to true by
32e76ffea3SGeorge Zhang 	 * IOCTL_VMCI_DATAGRAM_SEND (or by the vmkernel equivalent),
33e76ffea3SGeorge Zhang 	 * which comes from the VMX, so we know it is coming from a
34e76ffea3SGeorge Zhang 	 * guest.
35e76ffea3SGeorge Zhang 	 *
36e76ffea3SGeorge Zhang 	 * To avoid inconsistencies, test these once.  We will test
37e76ffea3SGeorge Zhang 	 * them again when we do the actual send to ensure that we do
38e76ffea3SGeorge Zhang 	 * not touch a non-existent device.
39e76ffea3SGeorge Zhang 	 */
40e76ffea3SGeorge Zhang 
41e76ffea3SGeorge Zhang 	/* Must have a valid destination context. */
42e76ffea3SGeorge Zhang 	if (VMCI_INVALID_ID == dst->context)
43e76ffea3SGeorge Zhang 		return VMCI_ERROR_INVALID_ARGS;
44e76ffea3SGeorge Zhang 
45e76ffea3SGeorge Zhang 	/* Anywhere to hypervisor. */
46e76ffea3SGeorge Zhang 	if (VMCI_HYPERVISOR_CONTEXT_ID == dst->context) {
47e76ffea3SGeorge Zhang 
48e76ffea3SGeorge Zhang 		/*
49e76ffea3SGeorge Zhang 		 * If this message already came from a guest then we
50e76ffea3SGeorge Zhang 		 * cannot send it to the hypervisor.  It must come
51e76ffea3SGeorge Zhang 		 * from a local client.
52e76ffea3SGeorge Zhang 		 */
53e76ffea3SGeorge Zhang 		if (from_guest)
54e76ffea3SGeorge Zhang 			return VMCI_ERROR_DST_UNREACHABLE;
55e76ffea3SGeorge Zhang 
56e76ffea3SGeorge Zhang 		/*
57e76ffea3SGeorge Zhang 		 * We must be acting as a guest in order to send to
58e76ffea3SGeorge Zhang 		 * the hypervisor.
59e76ffea3SGeorge Zhang 		 */
60e76ffea3SGeorge Zhang 		if (!has_guest_device)
61e76ffea3SGeorge Zhang 			return VMCI_ERROR_DEVICE_NOT_FOUND;
62e76ffea3SGeorge Zhang 
63e76ffea3SGeorge Zhang 		/* And we cannot send if the source is the host context. */
64e76ffea3SGeorge Zhang 		if (VMCI_HOST_CONTEXT_ID == src->context)
65e76ffea3SGeorge Zhang 			return VMCI_ERROR_INVALID_ARGS;
66e76ffea3SGeorge Zhang 
67e76ffea3SGeorge Zhang 		/*
68e76ffea3SGeorge Zhang 		 * If the client passed the ANON source handle then
69e76ffea3SGeorge Zhang 		 * respect it (both context and resource are invalid).
70e76ffea3SGeorge Zhang 		 * However, if they passed only an invalid context,
71e76ffea3SGeorge Zhang 		 * then they probably mean ANY, in which case we
72e76ffea3SGeorge Zhang 		 * should set the real context here before passing it
73e76ffea3SGeorge Zhang 		 * down.
74e76ffea3SGeorge Zhang 		 */
75e76ffea3SGeorge Zhang 		if (VMCI_INVALID_ID == src->context &&
76e76ffea3SGeorge Zhang 		    VMCI_INVALID_ID != src->resource)
77e76ffea3SGeorge Zhang 			src->context = vmci_get_context_id();
78e76ffea3SGeorge Zhang 
79e76ffea3SGeorge Zhang 		/* Send from local client down to the hypervisor. */
80e76ffea3SGeorge Zhang 		*route = VMCI_ROUTE_AS_GUEST;
81e76ffea3SGeorge Zhang 		return VMCI_SUCCESS;
82e76ffea3SGeorge Zhang 	}
83e76ffea3SGeorge Zhang 
84e76ffea3SGeorge Zhang 	/* Anywhere to local client on host. */
85e76ffea3SGeorge Zhang 	if (VMCI_HOST_CONTEXT_ID == dst->context) {
86e76ffea3SGeorge Zhang 		/*
87e76ffea3SGeorge Zhang 		 * If it is not from a guest but we are acting as a
88e76ffea3SGeorge Zhang 		 * guest, then we need to send it down to the host.
89e76ffea3SGeorge Zhang 		 * Note that if we are also acting as a host then this
90e76ffea3SGeorge Zhang 		 * will prevent us from sending from local client to
91e76ffea3SGeorge Zhang 		 * local client, but we accept that restriction as a
92e76ffea3SGeorge Zhang 		 * way to remove any ambiguity from the host context.
93e76ffea3SGeorge Zhang 		 */
94e76ffea3SGeorge Zhang 		if (src->context == VMCI_HYPERVISOR_CONTEXT_ID) {
95e76ffea3SGeorge Zhang 			/*
96e76ffea3SGeorge Zhang 			 * If the hypervisor is the source, this is
97e76ffea3SGeorge Zhang 			 * host local communication. The hypervisor
98e76ffea3SGeorge Zhang 			 * may send vmci event datagrams to the host
99e76ffea3SGeorge Zhang 			 * itself, but it will never send datagrams to
100e76ffea3SGeorge Zhang 			 * an "outer host" through the guest device.
101e76ffea3SGeorge Zhang 			 */
102e76ffea3SGeorge Zhang 
103e76ffea3SGeorge Zhang 			if (has_host_device) {
104e76ffea3SGeorge Zhang 				*route = VMCI_ROUTE_AS_HOST;
105e76ffea3SGeorge Zhang 				return VMCI_SUCCESS;
106e76ffea3SGeorge Zhang 			} else {
107e76ffea3SGeorge Zhang 				return VMCI_ERROR_DEVICE_NOT_FOUND;
108e76ffea3SGeorge Zhang 			}
109e76ffea3SGeorge Zhang 		}
110e76ffea3SGeorge Zhang 
111e76ffea3SGeorge Zhang 		if (!from_guest && has_guest_device) {
112e76ffea3SGeorge Zhang 			/* If no source context then use the current. */
113e76ffea3SGeorge Zhang 			if (VMCI_INVALID_ID == src->context)
114e76ffea3SGeorge Zhang 				src->context = vmci_get_context_id();
115e76ffea3SGeorge Zhang 
116e76ffea3SGeorge Zhang 			/* Send it from local client down to the host. */
117e76ffea3SGeorge Zhang 			*route = VMCI_ROUTE_AS_GUEST;
118e76ffea3SGeorge Zhang 			return VMCI_SUCCESS;
119e76ffea3SGeorge Zhang 		}
120e76ffea3SGeorge Zhang 
121e76ffea3SGeorge Zhang 		/*
122e76ffea3SGeorge Zhang 		 * Otherwise we already received it from a guest and
123e76ffea3SGeorge Zhang 		 * it is destined for a local client on this host, or
124e76ffea3SGeorge Zhang 		 * it is from another local client on this host.  We
125e76ffea3SGeorge Zhang 		 * must be acting as a host to service it.
126e76ffea3SGeorge Zhang 		 */
127e76ffea3SGeorge Zhang 		if (!has_host_device)
128e76ffea3SGeorge Zhang 			return VMCI_ERROR_DEVICE_NOT_FOUND;
129e76ffea3SGeorge Zhang 
130e76ffea3SGeorge Zhang 		if (VMCI_INVALID_ID == src->context) {
131e76ffea3SGeorge Zhang 			/*
132e76ffea3SGeorge Zhang 			 * If it came from a guest then it must have a
133e76ffea3SGeorge Zhang 			 * valid context.  Otherwise we can use the
134e76ffea3SGeorge Zhang 			 * host context.
135e76ffea3SGeorge Zhang 			 */
136e76ffea3SGeorge Zhang 			if (from_guest)
137e76ffea3SGeorge Zhang 				return VMCI_ERROR_INVALID_ARGS;
138e76ffea3SGeorge Zhang 
139e76ffea3SGeorge Zhang 			src->context = VMCI_HOST_CONTEXT_ID;
140e76ffea3SGeorge Zhang 		}
141e76ffea3SGeorge Zhang 
142e76ffea3SGeorge Zhang 		/* Route to local client. */
143e76ffea3SGeorge Zhang 		*route = VMCI_ROUTE_AS_HOST;
144e76ffea3SGeorge Zhang 		return VMCI_SUCCESS;
145e76ffea3SGeorge Zhang 	}
146e76ffea3SGeorge Zhang 
147e76ffea3SGeorge Zhang 	/*
148e76ffea3SGeorge Zhang 	 * If we are acting as a host then this might be destined for
149e76ffea3SGeorge Zhang 	 * a guest.
150e76ffea3SGeorge Zhang 	 */
151e76ffea3SGeorge Zhang 	if (has_host_device) {
152e76ffea3SGeorge Zhang 		/* It will have a context if it is meant for a guest. */
153e76ffea3SGeorge Zhang 		if (vmci_ctx_exists(dst->context)) {
154e76ffea3SGeorge Zhang 			if (VMCI_INVALID_ID == src->context) {
155e76ffea3SGeorge Zhang 				/*
156e76ffea3SGeorge Zhang 				 * If it came from a guest then it
157e76ffea3SGeorge Zhang 				 * must have a valid context.
158e76ffea3SGeorge Zhang 				 * Otherwise we can use the host
159e76ffea3SGeorge Zhang 				 * context.
160e76ffea3SGeorge Zhang 				 */
161e76ffea3SGeorge Zhang 
162e76ffea3SGeorge Zhang 				if (from_guest)
163e76ffea3SGeorge Zhang 					return VMCI_ERROR_INVALID_ARGS;
164e76ffea3SGeorge Zhang 
165e76ffea3SGeorge Zhang 				src->context = VMCI_HOST_CONTEXT_ID;
166e76ffea3SGeorge Zhang 			} else if (VMCI_CONTEXT_IS_VM(src->context) &&
167e76ffea3SGeorge Zhang 				   src->context != dst->context) {
168e76ffea3SGeorge Zhang 				/*
169e76ffea3SGeorge Zhang 				 * VM to VM communication is not
170e76ffea3SGeorge Zhang 				 * allowed. Since we catch all
171e76ffea3SGeorge Zhang 				 * communication destined for the host
172e76ffea3SGeorge Zhang 				 * above, this must be destined for a
173e76ffea3SGeorge Zhang 				 * VM since there is a valid context.
174e76ffea3SGeorge Zhang 				 */
175e76ffea3SGeorge Zhang 
176e76ffea3SGeorge Zhang 				return VMCI_ERROR_DST_UNREACHABLE;
177e76ffea3SGeorge Zhang 			}
178e76ffea3SGeorge Zhang 
179e76ffea3SGeorge Zhang 			/* Pass it up to the guest. */
180e76ffea3SGeorge Zhang 			*route = VMCI_ROUTE_AS_HOST;
181e76ffea3SGeorge Zhang 			return VMCI_SUCCESS;
182e76ffea3SGeorge Zhang 		} else if (!has_guest_device) {
183e76ffea3SGeorge Zhang 			/*
184e76ffea3SGeorge Zhang 			 * The host is attempting to reach a CID
185e76ffea3SGeorge Zhang 			 * without an active context, and we can't
186e76ffea3SGeorge Zhang 			 * send it down, since we have no guest
187e76ffea3SGeorge Zhang 			 * device.
188e76ffea3SGeorge Zhang 			 */
189e76ffea3SGeorge Zhang 
190e76ffea3SGeorge Zhang 			return VMCI_ERROR_DST_UNREACHABLE;
191e76ffea3SGeorge Zhang 		}
192e76ffea3SGeorge Zhang 	}
193e76ffea3SGeorge Zhang 
194e76ffea3SGeorge Zhang 	/*
195e76ffea3SGeorge Zhang 	 * We must be a guest trying to send to another guest, which means
196e76ffea3SGeorge Zhang 	 * we need to send it down to the host. We do not filter out VM to
197e76ffea3SGeorge Zhang 	 * VM communication here, since we want to be able to use the guest
198e76ffea3SGeorge Zhang 	 * driver on older versions that do support VM to VM communication.
199e76ffea3SGeorge Zhang 	 */
200e76ffea3SGeorge Zhang 	if (!has_guest_device) {
201e76ffea3SGeorge Zhang 		/*
202e76ffea3SGeorge Zhang 		 * Ending up here means we have neither guest nor host
203e76ffea3SGeorge Zhang 		 * device.
204e76ffea3SGeorge Zhang 		 */
205e76ffea3SGeorge Zhang 		return VMCI_ERROR_DEVICE_NOT_FOUND;
206e76ffea3SGeorge Zhang 	}
207e76ffea3SGeorge Zhang 
208e76ffea3SGeorge Zhang 	/* If no source context then use the current context. */
209e76ffea3SGeorge Zhang 	if (VMCI_INVALID_ID == src->context)
210e76ffea3SGeorge Zhang 		src->context = vmci_get_context_id();
211e76ffea3SGeorge Zhang 
212e76ffea3SGeorge Zhang 	/*
213e76ffea3SGeorge Zhang 	 * Send it from local client down to the host, which will
214e76ffea3SGeorge Zhang 	 * route it to the other guest for us.
215e76ffea3SGeorge Zhang 	 */
216e76ffea3SGeorge Zhang 	*route = VMCI_ROUTE_AS_GUEST;
217e76ffea3SGeorge Zhang 	return VMCI_SUCCESS;
218e76ffea3SGeorge Zhang }
219