vmci_route.c 5.9 KB

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