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