vmci_route.c 6.3 KB

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