vmci_transport.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  1. /*
  2. * VMware vSockets Driver
  3. *
  4. * Copyright (C) 2007-2013 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 Free
  8. * Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/bitops.h>
  17. #include <linux/cred.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/kmod.h>
  22. #include <linux/list.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/net.h>
  27. #include <linux/poll.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/smp.h>
  30. #include <linux/socket.h>
  31. #include <linux/stddef.h>
  32. #include <linux/unistd.h>
  33. #include <linux/wait.h>
  34. #include <linux/workqueue.h>
  35. #include <net/sock.h>
  36. #include <net/af_vsock.h>
  37. #include "vmci_transport_notify.h"
  38. static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg);
  39. static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg);
  40. static void vmci_transport_peer_detach_cb(u32 sub_id,
  41. const struct vmci_event_data *ed,
  42. void *client_data);
  43. static void vmci_transport_recv_pkt_work(struct work_struct *work);
  44. static void vmci_transport_cleanup(struct work_struct *work);
  45. static int vmci_transport_recv_listen(struct sock *sk,
  46. struct vmci_transport_packet *pkt);
  47. static int vmci_transport_recv_connecting_server(
  48. struct sock *sk,
  49. struct sock *pending,
  50. struct vmci_transport_packet *pkt);
  51. static int vmci_transport_recv_connecting_client(
  52. struct sock *sk,
  53. struct vmci_transport_packet *pkt);
  54. static int vmci_transport_recv_connecting_client_negotiate(
  55. struct sock *sk,
  56. struct vmci_transport_packet *pkt);
  57. static int vmci_transport_recv_connecting_client_invalid(
  58. struct sock *sk,
  59. struct vmci_transport_packet *pkt);
  60. static int vmci_transport_recv_connected(struct sock *sk,
  61. struct vmci_transport_packet *pkt);
  62. static bool vmci_transport_old_proto_override(bool *old_pkt_proto);
  63. static u16 vmci_transport_new_proto_supported_versions(void);
  64. static bool vmci_transport_proto_to_notify_struct(struct sock *sk, u16 *proto,
  65. bool old_pkt_proto);
  66. struct vmci_transport_recv_pkt_info {
  67. struct work_struct work;
  68. struct sock *sk;
  69. struct vmci_transport_packet pkt;
  70. };
  71. static LIST_HEAD(vmci_transport_cleanup_list);
  72. static DEFINE_SPINLOCK(vmci_transport_cleanup_lock);
  73. static DECLARE_WORK(vmci_transport_cleanup_work, vmci_transport_cleanup);
  74. static struct vmci_handle vmci_transport_stream_handle = { VMCI_INVALID_ID,
  75. VMCI_INVALID_ID };
  76. static u32 vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
  77. static int PROTOCOL_OVERRIDE = -1;
  78. #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN 128
  79. #define VMCI_TRANSPORT_DEFAULT_QP_SIZE 262144
  80. #define VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX 262144
  81. /* The default peer timeout indicates how long we will wait for a peer response
  82. * to a control message.
  83. */
  84. #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
  85. /* Helper function to convert from a VMCI error code to a VSock error code. */
  86. static s32 vmci_transport_error_to_vsock_error(s32 vmci_error)
  87. {
  88. int err;
  89. switch (vmci_error) {
  90. case VMCI_ERROR_NO_MEM:
  91. err = ENOMEM;
  92. break;
  93. case VMCI_ERROR_DUPLICATE_ENTRY:
  94. case VMCI_ERROR_ALREADY_EXISTS:
  95. err = EADDRINUSE;
  96. break;
  97. case VMCI_ERROR_NO_ACCESS:
  98. err = EPERM;
  99. break;
  100. case VMCI_ERROR_NO_RESOURCES:
  101. err = ENOBUFS;
  102. break;
  103. case VMCI_ERROR_INVALID_RESOURCE:
  104. err = EHOSTUNREACH;
  105. break;
  106. case VMCI_ERROR_INVALID_ARGS:
  107. default:
  108. err = EINVAL;
  109. }
  110. return err > 0 ? -err : err;
  111. }
  112. static u32 vmci_transport_peer_rid(u32 peer_cid)
  113. {
  114. if (VMADDR_CID_HYPERVISOR == peer_cid)
  115. return VMCI_TRANSPORT_HYPERVISOR_PACKET_RID;
  116. return VMCI_TRANSPORT_PACKET_RID;
  117. }
  118. static inline void
  119. vmci_transport_packet_init(struct vmci_transport_packet *pkt,
  120. struct sockaddr_vm *src,
  121. struct sockaddr_vm *dst,
  122. u8 type,
  123. u64 size,
  124. u64 mode,
  125. struct vmci_transport_waiting_info *wait,
  126. u16 proto,
  127. struct vmci_handle handle)
  128. {
  129. /* We register the stream control handler as an any cid handle so we
  130. * must always send from a source address of VMADDR_CID_ANY
  131. */
  132. pkt->dg.src = vmci_make_handle(VMADDR_CID_ANY,
  133. VMCI_TRANSPORT_PACKET_RID);
  134. pkt->dg.dst = vmci_make_handle(dst->svm_cid,
  135. vmci_transport_peer_rid(dst->svm_cid));
  136. pkt->dg.payload_size = sizeof(*pkt) - sizeof(pkt->dg);
  137. pkt->version = VMCI_TRANSPORT_PACKET_VERSION;
  138. pkt->type = type;
  139. pkt->src_port = src->svm_port;
  140. pkt->dst_port = dst->svm_port;
  141. memset(&pkt->proto, 0, sizeof(pkt->proto));
  142. memset(&pkt->_reserved2, 0, sizeof(pkt->_reserved2));
  143. switch (pkt->type) {
  144. case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
  145. pkt->u.size = 0;
  146. break;
  147. case VMCI_TRANSPORT_PACKET_TYPE_REQUEST:
  148. case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
  149. pkt->u.size = size;
  150. break;
  151. case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
  152. case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
  153. pkt->u.handle = handle;
  154. break;
  155. case VMCI_TRANSPORT_PACKET_TYPE_WROTE:
  156. case VMCI_TRANSPORT_PACKET_TYPE_READ:
  157. case VMCI_TRANSPORT_PACKET_TYPE_RST:
  158. pkt->u.size = 0;
  159. break;
  160. case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
  161. pkt->u.mode = mode;
  162. break;
  163. case VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ:
  164. case VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE:
  165. memcpy(&pkt->u.wait, wait, sizeof(pkt->u.wait));
  166. break;
  167. case VMCI_TRANSPORT_PACKET_TYPE_REQUEST2:
  168. case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
  169. pkt->u.size = size;
  170. pkt->proto = proto;
  171. break;
  172. }
  173. }
  174. static inline void
  175. vmci_transport_packet_get_addresses(struct vmci_transport_packet *pkt,
  176. struct sockaddr_vm *local,
  177. struct sockaddr_vm *remote)
  178. {
  179. vsock_addr_init(local, pkt->dg.dst.context, pkt->dst_port);
  180. vsock_addr_init(remote, pkt->dg.src.context, pkt->src_port);
  181. }
  182. static int
  183. __vmci_transport_send_control_pkt(struct vmci_transport_packet *pkt,
  184. struct sockaddr_vm *src,
  185. struct sockaddr_vm *dst,
  186. enum vmci_transport_packet_type type,
  187. u64 size,
  188. u64 mode,
  189. struct vmci_transport_waiting_info *wait,
  190. u16 proto,
  191. struct vmci_handle handle,
  192. bool convert_error)
  193. {
  194. int err;
  195. vmci_transport_packet_init(pkt, src, dst, type, size, mode, wait,
  196. proto, handle);
  197. err = vmci_datagram_send(&pkt->dg);
  198. if (convert_error && (err < 0))
  199. return vmci_transport_error_to_vsock_error(err);
  200. return err;
  201. }
  202. static int
  203. vmci_transport_reply_control_pkt_fast(struct vmci_transport_packet *pkt,
  204. enum vmci_transport_packet_type type,
  205. u64 size,
  206. u64 mode,
  207. struct vmci_transport_waiting_info *wait,
  208. struct vmci_handle handle)
  209. {
  210. struct vmci_transport_packet reply;
  211. struct sockaddr_vm src, dst;
  212. if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST) {
  213. return 0;
  214. } else {
  215. vmci_transport_packet_get_addresses(pkt, &src, &dst);
  216. return __vmci_transport_send_control_pkt(&reply, &src, &dst,
  217. type,
  218. size, mode, wait,
  219. VSOCK_PROTO_INVALID,
  220. handle, true);
  221. }
  222. }
  223. static int
  224. vmci_transport_send_control_pkt_bh(struct sockaddr_vm *src,
  225. struct sockaddr_vm *dst,
  226. enum vmci_transport_packet_type type,
  227. u64 size,
  228. u64 mode,
  229. struct vmci_transport_waiting_info *wait,
  230. struct vmci_handle handle)
  231. {
  232. /* Note that it is safe to use a single packet across all CPUs since
  233. * two tasklets of the same type are guaranteed to not ever run
  234. * simultaneously. If that ever changes, or VMCI stops using tasklets,
  235. * we can use per-cpu packets.
  236. */
  237. static struct vmci_transport_packet pkt;
  238. return __vmci_transport_send_control_pkt(&pkt, src, dst, type,
  239. size, mode, wait,
  240. VSOCK_PROTO_INVALID, handle,
  241. false);
  242. }
  243. static int
  244. vmci_transport_send_control_pkt(struct sock *sk,
  245. enum vmci_transport_packet_type type,
  246. u64 size,
  247. u64 mode,
  248. struct vmci_transport_waiting_info *wait,
  249. u16 proto,
  250. struct vmci_handle handle)
  251. {
  252. struct vmci_transport_packet *pkt;
  253. struct vsock_sock *vsk;
  254. int err;
  255. vsk = vsock_sk(sk);
  256. if (!vsock_addr_bound(&vsk->local_addr))
  257. return -EINVAL;
  258. if (!vsock_addr_bound(&vsk->remote_addr))
  259. return -EINVAL;
  260. pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
  261. if (!pkt)
  262. return -ENOMEM;
  263. err = __vmci_transport_send_control_pkt(pkt, &vsk->local_addr,
  264. &vsk->remote_addr, type, size,
  265. mode, wait, proto, handle,
  266. true);
  267. kfree(pkt);
  268. return err;
  269. }
  270. static int vmci_transport_send_reset_bh(struct sockaddr_vm *dst,
  271. struct sockaddr_vm *src,
  272. struct vmci_transport_packet *pkt)
  273. {
  274. if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
  275. return 0;
  276. return vmci_transport_send_control_pkt_bh(
  277. dst, src,
  278. VMCI_TRANSPORT_PACKET_TYPE_RST, 0,
  279. 0, NULL, VMCI_INVALID_HANDLE);
  280. }
  281. static int vmci_transport_send_reset(struct sock *sk,
  282. struct vmci_transport_packet *pkt)
  283. {
  284. if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST)
  285. return 0;
  286. return vmci_transport_send_control_pkt(sk,
  287. VMCI_TRANSPORT_PACKET_TYPE_RST,
  288. 0, 0, NULL, VSOCK_PROTO_INVALID,
  289. VMCI_INVALID_HANDLE);
  290. }
  291. static int vmci_transport_send_negotiate(struct sock *sk, size_t size)
  292. {
  293. return vmci_transport_send_control_pkt(
  294. sk,
  295. VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
  296. size, 0, NULL,
  297. VSOCK_PROTO_INVALID,
  298. VMCI_INVALID_HANDLE);
  299. }
  300. static int vmci_transport_send_negotiate2(struct sock *sk, size_t size,
  301. u16 version)
  302. {
  303. return vmci_transport_send_control_pkt(
  304. sk,
  305. VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
  306. size, 0, NULL, version,
  307. VMCI_INVALID_HANDLE);
  308. }
  309. static int vmci_transport_send_qp_offer(struct sock *sk,
  310. struct vmci_handle handle)
  311. {
  312. return vmci_transport_send_control_pkt(
  313. sk, VMCI_TRANSPORT_PACKET_TYPE_OFFER, 0,
  314. 0, NULL,
  315. VSOCK_PROTO_INVALID, handle);
  316. }
  317. static int vmci_transport_send_attach(struct sock *sk,
  318. struct vmci_handle handle)
  319. {
  320. return vmci_transport_send_control_pkt(
  321. sk, VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
  322. 0, 0, NULL, VSOCK_PROTO_INVALID,
  323. handle);
  324. }
  325. static int vmci_transport_reply_reset(struct vmci_transport_packet *pkt)
  326. {
  327. return vmci_transport_reply_control_pkt_fast(
  328. pkt,
  329. VMCI_TRANSPORT_PACKET_TYPE_RST,
  330. 0, 0, NULL,
  331. VMCI_INVALID_HANDLE);
  332. }
  333. static int vmci_transport_send_invalid_bh(struct sockaddr_vm *dst,
  334. struct sockaddr_vm *src)
  335. {
  336. return vmci_transport_send_control_pkt_bh(
  337. dst, src,
  338. VMCI_TRANSPORT_PACKET_TYPE_INVALID,
  339. 0, 0, NULL, VMCI_INVALID_HANDLE);
  340. }
  341. int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
  342. struct sockaddr_vm *src)
  343. {
  344. return vmci_transport_send_control_pkt_bh(
  345. dst, src,
  346. VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
  347. 0, NULL, VMCI_INVALID_HANDLE);
  348. }
  349. int vmci_transport_send_read_bh(struct sockaddr_vm *dst,
  350. struct sockaddr_vm *src)
  351. {
  352. return vmci_transport_send_control_pkt_bh(
  353. dst, src,
  354. VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
  355. 0, NULL, VMCI_INVALID_HANDLE);
  356. }
  357. int vmci_transport_send_wrote(struct sock *sk)
  358. {
  359. return vmci_transport_send_control_pkt(
  360. sk, VMCI_TRANSPORT_PACKET_TYPE_WROTE, 0,
  361. 0, NULL, VSOCK_PROTO_INVALID,
  362. VMCI_INVALID_HANDLE);
  363. }
  364. int vmci_transport_send_read(struct sock *sk)
  365. {
  366. return vmci_transport_send_control_pkt(
  367. sk, VMCI_TRANSPORT_PACKET_TYPE_READ, 0,
  368. 0, NULL, VSOCK_PROTO_INVALID,
  369. VMCI_INVALID_HANDLE);
  370. }
  371. int vmci_transport_send_waiting_write(struct sock *sk,
  372. struct vmci_transport_waiting_info *wait)
  373. {
  374. return vmci_transport_send_control_pkt(
  375. sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
  376. 0, 0, wait, VSOCK_PROTO_INVALID,
  377. VMCI_INVALID_HANDLE);
  378. }
  379. int vmci_transport_send_waiting_read(struct sock *sk,
  380. struct vmci_transport_waiting_info *wait)
  381. {
  382. return vmci_transport_send_control_pkt(
  383. sk, VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
  384. 0, 0, wait, VSOCK_PROTO_INVALID,
  385. VMCI_INVALID_HANDLE);
  386. }
  387. static int vmci_transport_shutdown(struct vsock_sock *vsk, int mode)
  388. {
  389. return vmci_transport_send_control_pkt(
  390. &vsk->sk,
  391. VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
  392. 0, mode, NULL,
  393. VSOCK_PROTO_INVALID,
  394. VMCI_INVALID_HANDLE);
  395. }
  396. static int vmci_transport_send_conn_request(struct sock *sk, size_t size)
  397. {
  398. return vmci_transport_send_control_pkt(sk,
  399. VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
  400. size, 0, NULL,
  401. VSOCK_PROTO_INVALID,
  402. VMCI_INVALID_HANDLE);
  403. }
  404. static int vmci_transport_send_conn_request2(struct sock *sk, size_t size,
  405. u16 version)
  406. {
  407. return vmci_transport_send_control_pkt(
  408. sk, VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
  409. size, 0, NULL, version,
  410. VMCI_INVALID_HANDLE);
  411. }
  412. static struct sock *vmci_transport_get_pending(
  413. struct sock *listener,
  414. struct vmci_transport_packet *pkt)
  415. {
  416. struct vsock_sock *vlistener;
  417. struct vsock_sock *vpending;
  418. struct sock *pending;
  419. struct sockaddr_vm src;
  420. vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
  421. vlistener = vsock_sk(listener);
  422. list_for_each_entry(vpending, &vlistener->pending_links,
  423. pending_links) {
  424. if (vsock_addr_equals_addr(&src, &vpending->remote_addr) &&
  425. pkt->dst_port == vpending->local_addr.svm_port) {
  426. pending = sk_vsock(vpending);
  427. sock_hold(pending);
  428. goto found;
  429. }
  430. }
  431. pending = NULL;
  432. found:
  433. return pending;
  434. }
  435. static void vmci_transport_release_pending(struct sock *pending)
  436. {
  437. sock_put(pending);
  438. }
  439. /* We allow two kinds of sockets to communicate with a restricted VM: 1)
  440. * trusted sockets 2) sockets from applications running as the same user as the
  441. * VM (this is only true for the host side and only when using hosted products)
  442. */
  443. static bool vmci_transport_is_trusted(struct vsock_sock *vsock, u32 peer_cid)
  444. {
  445. return vsock->trusted ||
  446. vmci_is_context_owner(peer_cid, vsock->owner->uid);
  447. }
  448. /* We allow sending datagrams to and receiving datagrams from a restricted VM
  449. * only if it is trusted as described in vmci_transport_is_trusted.
  450. */
  451. static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid)
  452. {
  453. if (VMADDR_CID_HYPERVISOR == peer_cid)
  454. return true;
  455. if (vsock->cached_peer != peer_cid) {
  456. vsock->cached_peer = peer_cid;
  457. if (!vmci_transport_is_trusted(vsock, peer_cid) &&
  458. (vmci_context_get_priv_flags(peer_cid) &
  459. VMCI_PRIVILEGE_FLAG_RESTRICTED)) {
  460. vsock->cached_peer_allow_dgram = false;
  461. } else {
  462. vsock->cached_peer_allow_dgram = true;
  463. }
  464. }
  465. return vsock->cached_peer_allow_dgram;
  466. }
  467. static int
  468. vmci_transport_queue_pair_alloc(struct vmci_qp **qpair,
  469. struct vmci_handle *handle,
  470. u64 produce_size,
  471. u64 consume_size,
  472. u32 peer, u32 flags, bool trusted)
  473. {
  474. int err = 0;
  475. if (trusted) {
  476. /* Try to allocate our queue pair as trusted. This will only
  477. * work if vsock is running in the host.
  478. */
  479. err = vmci_qpair_alloc(qpair, handle, produce_size,
  480. consume_size,
  481. peer, flags,
  482. VMCI_PRIVILEGE_FLAG_TRUSTED);
  483. if (err != VMCI_ERROR_NO_ACCESS)
  484. goto out;
  485. }
  486. err = vmci_qpair_alloc(qpair, handle, produce_size, consume_size,
  487. peer, flags, VMCI_NO_PRIVILEGE_FLAGS);
  488. out:
  489. if (err < 0) {
  490. pr_err("Could not attach to queue pair with %d\n",
  491. err);
  492. err = vmci_transport_error_to_vsock_error(err);
  493. }
  494. return err;
  495. }
  496. static int
  497. vmci_transport_datagram_create_hnd(u32 resource_id,
  498. u32 flags,
  499. vmci_datagram_recv_cb recv_cb,
  500. void *client_data,
  501. struct vmci_handle *out_handle)
  502. {
  503. int err = 0;
  504. /* Try to allocate our datagram handler as trusted. This will only work
  505. * if vsock is running in the host.
  506. */
  507. err = vmci_datagram_create_handle_priv(resource_id, flags,
  508. VMCI_PRIVILEGE_FLAG_TRUSTED,
  509. recv_cb,
  510. client_data, out_handle);
  511. if (err == VMCI_ERROR_NO_ACCESS)
  512. err = vmci_datagram_create_handle(resource_id, flags,
  513. recv_cb, client_data,
  514. out_handle);
  515. return err;
  516. }
  517. /* This is invoked as part of a tasklet that's scheduled when the VMCI
  518. * interrupt fires. This is run in bottom-half context and if it ever needs to
  519. * sleep it should defer that work to a work queue.
  520. */
  521. static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg)
  522. {
  523. struct sock *sk;
  524. size_t size;
  525. struct sk_buff *skb;
  526. struct vsock_sock *vsk;
  527. sk = (struct sock *)data;
  528. /* This handler is privileged when this module is running on the host.
  529. * We will get datagrams from all endpoints (even VMs that are in a
  530. * restricted context). If we get one from a restricted context then
  531. * the destination socket must be trusted.
  532. *
  533. * NOTE: We access the socket struct without holding the lock here.
  534. * This is ok because the field we are interested is never modified
  535. * outside of the create and destruct socket functions.
  536. */
  537. vsk = vsock_sk(sk);
  538. if (!vmci_transport_allow_dgram(vsk, dg->src.context))
  539. return VMCI_ERROR_NO_ACCESS;
  540. size = VMCI_DG_SIZE(dg);
  541. /* Attach the packet to the socket's receive queue as an sk_buff. */
  542. skb = alloc_skb(size, GFP_ATOMIC);
  543. if (!skb)
  544. return VMCI_ERROR_NO_MEM;
  545. /* sk_receive_skb() will do a sock_put(), so hold here. */
  546. sock_hold(sk);
  547. skb_put(skb, size);
  548. memcpy(skb->data, dg, size);
  549. sk_receive_skb(sk, skb, 0);
  550. return VMCI_SUCCESS;
  551. }
  552. static bool vmci_transport_stream_allow(u32 cid, u32 port)
  553. {
  554. static const u32 non_socket_contexts[] = {
  555. VMADDR_CID_RESERVED,
  556. };
  557. int i;
  558. BUILD_BUG_ON(sizeof(cid) != sizeof(*non_socket_contexts));
  559. for (i = 0; i < ARRAY_SIZE(non_socket_contexts); i++) {
  560. if (cid == non_socket_contexts[i])
  561. return false;
  562. }
  563. return true;
  564. }
  565. /* This is invoked as part of a tasklet that's scheduled when the VMCI
  566. * interrupt fires. This is run in bottom-half context but it defers most of
  567. * its work to the packet handling work queue.
  568. */
  569. static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)
  570. {
  571. struct sock *sk;
  572. struct sockaddr_vm dst;
  573. struct sockaddr_vm src;
  574. struct vmci_transport_packet *pkt;
  575. struct vsock_sock *vsk;
  576. bool bh_process_pkt;
  577. int err;
  578. sk = NULL;
  579. err = VMCI_SUCCESS;
  580. bh_process_pkt = false;
  581. /* Ignore incoming packets from contexts without sockets, or resources
  582. * that aren't vsock implementations.
  583. */
  584. if (!vmci_transport_stream_allow(dg->src.context, -1)
  585. || vmci_transport_peer_rid(dg->src.context) != dg->src.resource)
  586. return VMCI_ERROR_NO_ACCESS;
  587. if (VMCI_DG_SIZE(dg) < sizeof(*pkt))
  588. /* Drop datagrams that do not contain full VSock packets. */
  589. return VMCI_ERROR_INVALID_ARGS;
  590. pkt = (struct vmci_transport_packet *)dg;
  591. /* Find the socket that should handle this packet. First we look for a
  592. * connected socket and if there is none we look for a socket bound to
  593. * the destintation address.
  594. */
  595. vsock_addr_init(&src, pkt->dg.src.context, pkt->src_port);
  596. vsock_addr_init(&dst, pkt->dg.dst.context, pkt->dst_port);
  597. sk = vsock_find_connected_socket(&src, &dst);
  598. if (!sk) {
  599. sk = vsock_find_bound_socket(&dst);
  600. if (!sk) {
  601. /* We could not find a socket for this specified
  602. * address. If this packet is a RST, we just drop it.
  603. * If it is another packet, we send a RST. Note that
  604. * we do not send a RST reply to RSTs so that we do not
  605. * continually send RSTs between two endpoints.
  606. *
  607. * Note that since this is a reply, dst is src and src
  608. * is dst.
  609. */
  610. if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
  611. pr_err("unable to send reset\n");
  612. err = VMCI_ERROR_NOT_FOUND;
  613. goto out;
  614. }
  615. }
  616. /* If the received packet type is beyond all types known to this
  617. * implementation, reply with an invalid message. Hopefully this will
  618. * help when implementing backwards compatibility in the future.
  619. */
  620. if (pkt->type >= VMCI_TRANSPORT_PACKET_TYPE_MAX) {
  621. vmci_transport_send_invalid_bh(&dst, &src);
  622. err = VMCI_ERROR_INVALID_ARGS;
  623. goto out;
  624. }
  625. /* This handler is privileged when this module is running on the host.
  626. * We will get datagram connect requests from all endpoints (even VMs
  627. * that are in a restricted context). If we get one from a restricted
  628. * context then the destination socket must be trusted.
  629. *
  630. * NOTE: We access the socket struct without holding the lock here.
  631. * This is ok because the field we are interested is never modified
  632. * outside of the create and destruct socket functions.
  633. */
  634. vsk = vsock_sk(sk);
  635. if (!vmci_transport_allow_dgram(vsk, pkt->dg.src.context)) {
  636. err = VMCI_ERROR_NO_ACCESS;
  637. goto out;
  638. }
  639. /* We do most everything in a work queue, but let's fast path the
  640. * notification of reads and writes to help data transfer performance.
  641. * We can only do this if there is no process context code executing
  642. * for this socket since that may change the state.
  643. */
  644. bh_lock_sock(sk);
  645. if (!sock_owned_by_user(sk)) {
  646. /* The local context ID may be out of date, update it. */
  647. vsk->local_addr.svm_cid = dst.svm_cid;
  648. if (sk->sk_state == SS_CONNECTED)
  649. vmci_trans(vsk)->notify_ops->handle_notify_pkt(
  650. sk, pkt, true, &dst, &src,
  651. &bh_process_pkt);
  652. }
  653. bh_unlock_sock(sk);
  654. if (!bh_process_pkt) {
  655. struct vmci_transport_recv_pkt_info *recv_pkt_info;
  656. recv_pkt_info = kmalloc(sizeof(*recv_pkt_info), GFP_ATOMIC);
  657. if (!recv_pkt_info) {
  658. if (vmci_transport_send_reset_bh(&dst, &src, pkt) < 0)
  659. pr_err("unable to send reset\n");
  660. err = VMCI_ERROR_NO_MEM;
  661. goto out;
  662. }
  663. recv_pkt_info->sk = sk;
  664. memcpy(&recv_pkt_info->pkt, pkt, sizeof(recv_pkt_info->pkt));
  665. INIT_WORK(&recv_pkt_info->work, vmci_transport_recv_pkt_work);
  666. schedule_work(&recv_pkt_info->work);
  667. /* Clear sk so that the reference count incremented by one of
  668. * the Find functions above is not decremented below. We need
  669. * that reference count for the packet handler we've scheduled
  670. * to run.
  671. */
  672. sk = NULL;
  673. }
  674. out:
  675. if (sk)
  676. sock_put(sk);
  677. return err;
  678. }
  679. static void vmci_transport_handle_detach(struct sock *sk)
  680. {
  681. struct vsock_sock *vsk;
  682. vsk = vsock_sk(sk);
  683. if (!vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)) {
  684. sock_set_flag(sk, SOCK_DONE);
  685. /* On a detach the peer will not be sending or receiving
  686. * anymore.
  687. */
  688. vsk->peer_shutdown = SHUTDOWN_MASK;
  689. /* We should not be sending anymore since the peer won't be
  690. * there to receive, but we can still receive if there is data
  691. * left in our consume queue.
  692. */
  693. if (vsock_stream_has_data(vsk) <= 0) {
  694. if (sk->sk_state == SS_CONNECTING) {
  695. /* The peer may detach from a queue pair while
  696. * we are still in the connecting state, i.e.,
  697. * if the peer VM is killed after attaching to
  698. * a queue pair, but before we complete the
  699. * handshake. In that case, we treat the detach
  700. * event like a reset.
  701. */
  702. sk->sk_state = SS_UNCONNECTED;
  703. sk->sk_err = ECONNRESET;
  704. sk->sk_error_report(sk);
  705. return;
  706. }
  707. sk->sk_state = SS_UNCONNECTED;
  708. }
  709. sk->sk_state_change(sk);
  710. }
  711. }
  712. static void vmci_transport_peer_detach_cb(u32 sub_id,
  713. const struct vmci_event_data *e_data,
  714. void *client_data)
  715. {
  716. struct vmci_transport *trans = client_data;
  717. const struct vmci_event_payload_qp *e_payload;
  718. e_payload = vmci_event_data_const_payload(e_data);
  719. /* XXX This is lame, we should provide a way to lookup sockets by
  720. * qp_handle.
  721. */
  722. if (vmci_handle_is_invalid(e_payload->handle) ||
  723. !vmci_handle_is_equal(trans->qp_handle, e_payload->handle))
  724. return;
  725. /* We don't ask for delayed CBs when we subscribe to this event (we
  726. * pass 0 as flags to vmci_event_subscribe()). VMCI makes no
  727. * guarantees in that case about what context we might be running in,
  728. * so it could be BH or process, blockable or non-blockable. So we
  729. * need to account for all possible contexts here.
  730. */
  731. spin_lock_bh(&trans->lock);
  732. if (!trans->sk)
  733. goto out;
  734. /* Apart from here, trans->lock is only grabbed as part of sk destruct,
  735. * where trans->sk isn't locked.
  736. */
  737. bh_lock_sock(trans->sk);
  738. vmci_transport_handle_detach(trans->sk);
  739. bh_unlock_sock(trans->sk);
  740. out:
  741. spin_unlock_bh(&trans->lock);
  742. }
  743. static void vmci_transport_qp_resumed_cb(u32 sub_id,
  744. const struct vmci_event_data *e_data,
  745. void *client_data)
  746. {
  747. vsock_for_each_connected_socket(vmci_transport_handle_detach);
  748. }
  749. static void vmci_transport_recv_pkt_work(struct work_struct *work)
  750. {
  751. struct vmci_transport_recv_pkt_info *recv_pkt_info;
  752. struct vmci_transport_packet *pkt;
  753. struct sock *sk;
  754. recv_pkt_info =
  755. container_of(work, struct vmci_transport_recv_pkt_info, work);
  756. sk = recv_pkt_info->sk;
  757. pkt = &recv_pkt_info->pkt;
  758. lock_sock(sk);
  759. /* The local context ID may be out of date. */
  760. vsock_sk(sk)->local_addr.svm_cid = pkt->dg.dst.context;
  761. switch (sk->sk_state) {
  762. case VSOCK_SS_LISTEN:
  763. vmci_transport_recv_listen(sk, pkt);
  764. break;
  765. case SS_CONNECTING:
  766. /* Processing of pending connections for servers goes through
  767. * the listening socket, so see vmci_transport_recv_listen()
  768. * for that path.
  769. */
  770. vmci_transport_recv_connecting_client(sk, pkt);
  771. break;
  772. case SS_CONNECTED:
  773. vmci_transport_recv_connected(sk, pkt);
  774. break;
  775. default:
  776. /* Because this function does not run in the same context as
  777. * vmci_transport_recv_stream_cb it is possible that the
  778. * socket has closed. We need to let the other side know or it
  779. * could be sitting in a connect and hang forever. Send a
  780. * reset to prevent that.
  781. */
  782. vmci_transport_send_reset(sk, pkt);
  783. break;
  784. }
  785. release_sock(sk);
  786. kfree(recv_pkt_info);
  787. /* Release reference obtained in the stream callback when we fetched
  788. * this socket out of the bound or connected list.
  789. */
  790. sock_put(sk);
  791. }
  792. static int vmci_transport_recv_listen(struct sock *sk,
  793. struct vmci_transport_packet *pkt)
  794. {
  795. struct sock *pending;
  796. struct vsock_sock *vpending;
  797. int err;
  798. u64 qp_size;
  799. bool old_request = false;
  800. bool old_pkt_proto = false;
  801. err = 0;
  802. /* Because we are in the listen state, we could be receiving a packet
  803. * for ourself or any previous connection requests that we received.
  804. * If it's the latter, we try to find a socket in our list of pending
  805. * connections and, if we do, call the appropriate handler for the
  806. * state that that socket is in. Otherwise we try to service the
  807. * connection request.
  808. */
  809. pending = vmci_transport_get_pending(sk, pkt);
  810. if (pending) {
  811. lock_sock(pending);
  812. /* The local context ID may be out of date. */
  813. vsock_sk(pending)->local_addr.svm_cid = pkt->dg.dst.context;
  814. switch (pending->sk_state) {
  815. case SS_CONNECTING:
  816. err = vmci_transport_recv_connecting_server(sk,
  817. pending,
  818. pkt);
  819. break;
  820. default:
  821. vmci_transport_send_reset(pending, pkt);
  822. err = -EINVAL;
  823. }
  824. if (err < 0)
  825. vsock_remove_pending(sk, pending);
  826. release_sock(pending);
  827. vmci_transport_release_pending(pending);
  828. return err;
  829. }
  830. /* The listen state only accepts connection requests. Reply with a
  831. * reset unless we received a reset.
  832. */
  833. if (!(pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST ||
  834. pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)) {
  835. vmci_transport_reply_reset(pkt);
  836. return -EINVAL;
  837. }
  838. if (pkt->u.size == 0) {
  839. vmci_transport_reply_reset(pkt);
  840. return -EINVAL;
  841. }
  842. /* If this socket can't accommodate this connection request, we send a
  843. * reset. Otherwise we create and initialize a child socket and reply
  844. * with a connection negotiation.
  845. */
  846. if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) {
  847. vmci_transport_reply_reset(pkt);
  848. return -ECONNREFUSED;
  849. }
  850. pending = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL,
  851. sk->sk_type, 0);
  852. if (!pending) {
  853. vmci_transport_send_reset(sk, pkt);
  854. return -ENOMEM;
  855. }
  856. vpending = vsock_sk(pending);
  857. vsock_addr_init(&vpending->local_addr, pkt->dg.dst.context,
  858. pkt->dst_port);
  859. vsock_addr_init(&vpending->remote_addr, pkt->dg.src.context,
  860. pkt->src_port);
  861. /* If the proposed size fits within our min/max, accept it. Otherwise
  862. * propose our own size.
  863. */
  864. if (pkt->u.size >= vmci_trans(vpending)->queue_pair_min_size &&
  865. pkt->u.size <= vmci_trans(vpending)->queue_pair_max_size) {
  866. qp_size = pkt->u.size;
  867. } else {
  868. qp_size = vmci_trans(vpending)->queue_pair_size;
  869. }
  870. /* Figure out if we are using old or new requests based on the
  871. * overrides pkt types sent by our peer.
  872. */
  873. if (vmci_transport_old_proto_override(&old_pkt_proto)) {
  874. old_request = old_pkt_proto;
  875. } else {
  876. if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST)
  877. old_request = true;
  878. else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_REQUEST2)
  879. old_request = false;
  880. }
  881. if (old_request) {
  882. /* Handle a REQUEST (or override) */
  883. u16 version = VSOCK_PROTO_INVALID;
  884. if (vmci_transport_proto_to_notify_struct(
  885. pending, &version, true))
  886. err = vmci_transport_send_negotiate(pending, qp_size);
  887. else
  888. err = -EINVAL;
  889. } else {
  890. /* Handle a REQUEST2 (or override) */
  891. int proto_int = pkt->proto;
  892. int pos;
  893. u16 active_proto_version = 0;
  894. /* The list of possible protocols is the intersection of all
  895. * protocols the client supports ... plus all the protocols we
  896. * support.
  897. */
  898. proto_int &= vmci_transport_new_proto_supported_versions();
  899. /* We choose the highest possible protocol version and use that
  900. * one.
  901. */
  902. pos = fls(proto_int);
  903. if (pos) {
  904. active_proto_version = (1 << (pos - 1));
  905. if (vmci_transport_proto_to_notify_struct(
  906. pending, &active_proto_version, false))
  907. err = vmci_transport_send_negotiate2(pending,
  908. qp_size,
  909. active_proto_version);
  910. else
  911. err = -EINVAL;
  912. } else {
  913. err = -EINVAL;
  914. }
  915. }
  916. if (err < 0) {
  917. vmci_transport_send_reset(sk, pkt);
  918. sock_put(pending);
  919. err = vmci_transport_error_to_vsock_error(err);
  920. goto out;
  921. }
  922. vsock_add_pending(sk, pending);
  923. sk->sk_ack_backlog++;
  924. pending->sk_state = SS_CONNECTING;
  925. vmci_trans(vpending)->produce_size =
  926. vmci_trans(vpending)->consume_size = qp_size;
  927. vmci_trans(vpending)->queue_pair_size = qp_size;
  928. vmci_trans(vpending)->notify_ops->process_request(pending);
  929. /* We might never receive another message for this socket and it's not
  930. * connected to any process, so we have to ensure it gets cleaned up
  931. * ourself. Our delayed work function will take care of that. Note
  932. * that we do not ever cancel this function since we have few
  933. * guarantees about its state when calling cancel_delayed_work().
  934. * Instead we hold a reference on the socket for that function and make
  935. * it capable of handling cases where it needs to do nothing but
  936. * release that reference.
  937. */
  938. vpending->listener = sk;
  939. sock_hold(sk);
  940. sock_hold(pending);
  941. INIT_DELAYED_WORK(&vpending->dwork, vsock_pending_work);
  942. schedule_delayed_work(&vpending->dwork, HZ);
  943. out:
  944. return err;
  945. }
  946. static int
  947. vmci_transport_recv_connecting_server(struct sock *listener,
  948. struct sock *pending,
  949. struct vmci_transport_packet *pkt)
  950. {
  951. struct vsock_sock *vpending;
  952. struct vmci_handle handle;
  953. struct vmci_qp *qpair;
  954. bool is_local;
  955. u32 flags;
  956. u32 detach_sub_id;
  957. int err;
  958. int skerr;
  959. vpending = vsock_sk(pending);
  960. detach_sub_id = VMCI_INVALID_ID;
  961. switch (pkt->type) {
  962. case VMCI_TRANSPORT_PACKET_TYPE_OFFER:
  963. if (vmci_handle_is_invalid(pkt->u.handle)) {
  964. vmci_transport_send_reset(pending, pkt);
  965. skerr = EPROTO;
  966. err = -EINVAL;
  967. goto destroy;
  968. }
  969. break;
  970. default:
  971. /* Close and cleanup the connection. */
  972. vmci_transport_send_reset(pending, pkt);
  973. skerr = EPROTO;
  974. err = pkt->type == VMCI_TRANSPORT_PACKET_TYPE_RST ? 0 : -EINVAL;
  975. goto destroy;
  976. }
  977. /* In order to complete the connection we need to attach to the offered
  978. * queue pair and send an attach notification. We also subscribe to the
  979. * detach event so we know when our peer goes away, and we do that
  980. * before attaching so we don't miss an event. If all this succeeds,
  981. * we update our state and wakeup anything waiting in accept() for a
  982. * connection.
  983. */
  984. /* We don't care about attach since we ensure the other side has
  985. * attached by specifying the ATTACH_ONLY flag below.
  986. */
  987. err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
  988. vmci_transport_peer_detach_cb,
  989. vmci_trans(vpending), &detach_sub_id);
  990. if (err < VMCI_SUCCESS) {
  991. vmci_transport_send_reset(pending, pkt);
  992. err = vmci_transport_error_to_vsock_error(err);
  993. skerr = -err;
  994. goto destroy;
  995. }
  996. vmci_trans(vpending)->detach_sub_id = detach_sub_id;
  997. /* Now attach to the queue pair the client created. */
  998. handle = pkt->u.handle;
  999. /* vpending->local_addr always has a context id so we do not need to
  1000. * worry about VMADDR_CID_ANY in this case.
  1001. */
  1002. is_local =
  1003. vpending->remote_addr.svm_cid == vpending->local_addr.svm_cid;
  1004. flags = VMCI_QPFLAG_ATTACH_ONLY;
  1005. flags |= is_local ? VMCI_QPFLAG_LOCAL : 0;
  1006. err = vmci_transport_queue_pair_alloc(
  1007. &qpair,
  1008. &handle,
  1009. vmci_trans(vpending)->produce_size,
  1010. vmci_trans(vpending)->consume_size,
  1011. pkt->dg.src.context,
  1012. flags,
  1013. vmci_transport_is_trusted(
  1014. vpending,
  1015. vpending->remote_addr.svm_cid));
  1016. if (err < 0) {
  1017. vmci_transport_send_reset(pending, pkt);
  1018. skerr = -err;
  1019. goto destroy;
  1020. }
  1021. vmci_trans(vpending)->qp_handle = handle;
  1022. vmci_trans(vpending)->qpair = qpair;
  1023. /* When we send the attach message, we must be ready to handle incoming
  1024. * control messages on the newly connected socket. So we move the
  1025. * pending socket to the connected state before sending the attach
  1026. * message. Otherwise, an incoming packet triggered by the attach being
  1027. * received by the peer may be processed concurrently with what happens
  1028. * below after sending the attach message, and that incoming packet
  1029. * will find the listening socket instead of the (currently) pending
  1030. * socket. Note that enqueueing the socket increments the reference
  1031. * count, so even if a reset comes before the connection is accepted,
  1032. * the socket will be valid until it is removed from the queue.
  1033. *
  1034. * If we fail sending the attach below, we remove the socket from the
  1035. * connected list and move the socket to SS_UNCONNECTED before
  1036. * releasing the lock, so a pending slow path processing of an incoming
  1037. * packet will not see the socket in the connected state in that case.
  1038. */
  1039. pending->sk_state = SS_CONNECTED;
  1040. vsock_insert_connected(vpending);
  1041. /* Notify our peer of our attach. */
  1042. err = vmci_transport_send_attach(pending, handle);
  1043. if (err < 0) {
  1044. vsock_remove_connected(vpending);
  1045. pr_err("Could not send attach\n");
  1046. vmci_transport_send_reset(pending, pkt);
  1047. err = vmci_transport_error_to_vsock_error(err);
  1048. skerr = -err;
  1049. goto destroy;
  1050. }
  1051. /* We have a connection. Move the now connected socket from the
  1052. * listener's pending list to the accept queue so callers of accept()
  1053. * can find it.
  1054. */
  1055. vsock_remove_pending(listener, pending);
  1056. vsock_enqueue_accept(listener, pending);
  1057. /* Callers of accept() will be be waiting on the listening socket, not
  1058. * the pending socket.
  1059. */
  1060. listener->sk_data_ready(listener);
  1061. return 0;
  1062. destroy:
  1063. pending->sk_err = skerr;
  1064. pending->sk_state = SS_UNCONNECTED;
  1065. /* As long as we drop our reference, all necessary cleanup will handle
  1066. * when the cleanup function drops its reference and our destruct
  1067. * implementation is called. Note that since the listen handler will
  1068. * remove pending from the pending list upon our failure, the cleanup
  1069. * function won't drop the additional reference, which is why we do it
  1070. * here.
  1071. */
  1072. sock_put(pending);
  1073. return err;
  1074. }
  1075. static int
  1076. vmci_transport_recv_connecting_client(struct sock *sk,
  1077. struct vmci_transport_packet *pkt)
  1078. {
  1079. struct vsock_sock *vsk;
  1080. int err;
  1081. int skerr;
  1082. vsk = vsock_sk(sk);
  1083. switch (pkt->type) {
  1084. case VMCI_TRANSPORT_PACKET_TYPE_ATTACH:
  1085. if (vmci_handle_is_invalid(pkt->u.handle) ||
  1086. !vmci_handle_is_equal(pkt->u.handle,
  1087. vmci_trans(vsk)->qp_handle)) {
  1088. skerr = EPROTO;
  1089. err = -EINVAL;
  1090. goto destroy;
  1091. }
  1092. /* Signify the socket is connected and wakeup the waiter in
  1093. * connect(). Also place the socket in the connected table for
  1094. * accounting (it can already be found since it's in the bound
  1095. * table).
  1096. */
  1097. sk->sk_state = SS_CONNECTED;
  1098. sk->sk_socket->state = SS_CONNECTED;
  1099. vsock_insert_connected(vsk);
  1100. sk->sk_state_change(sk);
  1101. break;
  1102. case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE:
  1103. case VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2:
  1104. if (pkt->u.size == 0
  1105. || pkt->dg.src.context != vsk->remote_addr.svm_cid
  1106. || pkt->src_port != vsk->remote_addr.svm_port
  1107. || !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle)
  1108. || vmci_trans(vsk)->qpair
  1109. || vmci_trans(vsk)->produce_size != 0
  1110. || vmci_trans(vsk)->consume_size != 0
  1111. || vmci_trans(vsk)->detach_sub_id != VMCI_INVALID_ID) {
  1112. skerr = EPROTO;
  1113. err = -EINVAL;
  1114. goto destroy;
  1115. }
  1116. err = vmci_transport_recv_connecting_client_negotiate(sk, pkt);
  1117. if (err) {
  1118. skerr = -err;
  1119. goto destroy;
  1120. }
  1121. break;
  1122. case VMCI_TRANSPORT_PACKET_TYPE_INVALID:
  1123. err = vmci_transport_recv_connecting_client_invalid(sk, pkt);
  1124. if (err) {
  1125. skerr = -err;
  1126. goto destroy;
  1127. }
  1128. break;
  1129. case VMCI_TRANSPORT_PACKET_TYPE_RST:
  1130. /* Older versions of the linux code (WS 6.5 / ESX 4.0) used to
  1131. * continue processing here after they sent an INVALID packet.
  1132. * This meant that we got a RST after the INVALID. We ignore a
  1133. * RST after an INVALID. The common code doesn't send the RST
  1134. * ... so we can hang if an old version of the common code
  1135. * fails between getting a REQUEST and sending an OFFER back.
  1136. * Not much we can do about it... except hope that it doesn't
  1137. * happen.
  1138. */
  1139. if (vsk->ignore_connecting_rst) {
  1140. vsk->ignore_connecting_rst = false;
  1141. } else {
  1142. skerr = ECONNRESET;
  1143. err = 0;
  1144. goto destroy;
  1145. }
  1146. break;
  1147. default:
  1148. /* Close and cleanup the connection. */
  1149. skerr = EPROTO;
  1150. err = -EINVAL;
  1151. goto destroy;
  1152. }
  1153. return 0;
  1154. destroy:
  1155. vmci_transport_send_reset(sk, pkt);
  1156. sk->sk_state = SS_UNCONNECTED;
  1157. sk->sk_err = skerr;
  1158. sk->sk_error_report(sk);
  1159. return err;
  1160. }
  1161. static int vmci_transport_recv_connecting_client_negotiate(
  1162. struct sock *sk,
  1163. struct vmci_transport_packet *pkt)
  1164. {
  1165. int err;
  1166. struct vsock_sock *vsk;
  1167. struct vmci_handle handle;
  1168. struct vmci_qp *qpair;
  1169. u32 detach_sub_id;
  1170. bool is_local;
  1171. u32 flags;
  1172. bool old_proto = true;
  1173. bool old_pkt_proto;
  1174. u16 version;
  1175. vsk = vsock_sk(sk);
  1176. handle = VMCI_INVALID_HANDLE;
  1177. detach_sub_id = VMCI_INVALID_ID;
  1178. /* If we have gotten here then we should be past the point where old
  1179. * linux vsock could have sent the bogus rst.
  1180. */
  1181. vsk->sent_request = false;
  1182. vsk->ignore_connecting_rst = false;
  1183. /* Verify that we're OK with the proposed queue pair size */
  1184. if (pkt->u.size < vmci_trans(vsk)->queue_pair_min_size ||
  1185. pkt->u.size > vmci_trans(vsk)->queue_pair_max_size) {
  1186. err = -EINVAL;
  1187. goto destroy;
  1188. }
  1189. /* At this point we know the CID the peer is using to talk to us. */
  1190. if (vsk->local_addr.svm_cid == VMADDR_CID_ANY)
  1191. vsk->local_addr.svm_cid = pkt->dg.dst.context;
  1192. /* Setup the notify ops to be the highest supported version that both
  1193. * the server and the client support.
  1194. */
  1195. if (vmci_transport_old_proto_override(&old_pkt_proto)) {
  1196. old_proto = old_pkt_proto;
  1197. } else {
  1198. if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE)
  1199. old_proto = true;
  1200. else if (pkt->type == VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2)
  1201. old_proto = false;
  1202. }
  1203. if (old_proto)
  1204. version = VSOCK_PROTO_INVALID;
  1205. else
  1206. version = pkt->proto;
  1207. if (!vmci_transport_proto_to_notify_struct(sk, &version, old_proto)) {
  1208. err = -EINVAL;
  1209. goto destroy;
  1210. }
  1211. /* Subscribe to detach events first.
  1212. *
  1213. * XXX We attach once for each queue pair created for now so it is easy
  1214. * to find the socket (it's provided), but later we should only
  1215. * subscribe once and add a way to lookup sockets by queue pair handle.
  1216. */
  1217. err = vmci_event_subscribe(VMCI_EVENT_QP_PEER_DETACH,
  1218. vmci_transport_peer_detach_cb,
  1219. vmci_trans(vsk), &detach_sub_id);
  1220. if (err < VMCI_SUCCESS) {
  1221. err = vmci_transport_error_to_vsock_error(err);
  1222. goto destroy;
  1223. }
  1224. /* Make VMCI select the handle for us. */
  1225. handle = VMCI_INVALID_HANDLE;
  1226. is_local = vsk->remote_addr.svm_cid == vsk->local_addr.svm_cid;
  1227. flags = is_local ? VMCI_QPFLAG_LOCAL : 0;
  1228. err = vmci_transport_queue_pair_alloc(&qpair,
  1229. &handle,
  1230. pkt->u.size,
  1231. pkt->u.size,
  1232. vsk->remote_addr.svm_cid,
  1233. flags,
  1234. vmci_transport_is_trusted(
  1235. vsk,
  1236. vsk->
  1237. remote_addr.svm_cid));
  1238. if (err < 0)
  1239. goto destroy;
  1240. err = vmci_transport_send_qp_offer(sk, handle);
  1241. if (err < 0) {
  1242. err = vmci_transport_error_to_vsock_error(err);
  1243. goto destroy;
  1244. }
  1245. vmci_trans(vsk)->qp_handle = handle;
  1246. vmci_trans(vsk)->qpair = qpair;
  1247. vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size =
  1248. pkt->u.size;
  1249. vmci_trans(vsk)->detach_sub_id = detach_sub_id;
  1250. vmci_trans(vsk)->notify_ops->process_negotiate(sk);
  1251. return 0;
  1252. destroy:
  1253. if (detach_sub_id != VMCI_INVALID_ID)
  1254. vmci_event_unsubscribe(detach_sub_id);
  1255. if (!vmci_handle_is_invalid(handle))
  1256. vmci_qpair_detach(&qpair);
  1257. return err;
  1258. }
  1259. static int
  1260. vmci_transport_recv_connecting_client_invalid(struct sock *sk,
  1261. struct vmci_transport_packet *pkt)
  1262. {
  1263. int err = 0;
  1264. struct vsock_sock *vsk = vsock_sk(sk);
  1265. if (vsk->sent_request) {
  1266. vsk->sent_request = false;
  1267. vsk->ignore_connecting_rst = true;
  1268. err = vmci_transport_send_conn_request(
  1269. sk, vmci_trans(vsk)->queue_pair_size);
  1270. if (err < 0)
  1271. err = vmci_transport_error_to_vsock_error(err);
  1272. else
  1273. err = 0;
  1274. }
  1275. return err;
  1276. }
  1277. static int vmci_transport_recv_connected(struct sock *sk,
  1278. struct vmci_transport_packet *pkt)
  1279. {
  1280. struct vsock_sock *vsk;
  1281. bool pkt_processed = false;
  1282. /* In cases where we are closing the connection, it's sufficient to
  1283. * mark the state change (and maybe error) and wake up any waiting
  1284. * threads. Since this is a connected socket, it's owned by a user
  1285. * process and will be cleaned up when the failure is passed back on
  1286. * the current or next system call. Our system call implementations
  1287. * must therefore check for error and state changes on entry and when
  1288. * being awoken.
  1289. */
  1290. switch (pkt->type) {
  1291. case VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN:
  1292. if (pkt->u.mode) {
  1293. vsk = vsock_sk(sk);
  1294. vsk->peer_shutdown |= pkt->u.mode;
  1295. sk->sk_state_change(sk);
  1296. }
  1297. break;
  1298. case VMCI_TRANSPORT_PACKET_TYPE_RST:
  1299. vsk = vsock_sk(sk);
  1300. /* It is possible that we sent our peer a message (e.g a
  1301. * WAITING_READ) right before we got notified that the peer had
  1302. * detached. If that happens then we can get a RST pkt back
  1303. * from our peer even though there is data available for us to
  1304. * read. In that case, don't shutdown the socket completely but
  1305. * instead allow the local client to finish reading data off
  1306. * the queuepair. Always treat a RST pkt in connected mode like
  1307. * a clean shutdown.
  1308. */
  1309. sock_set_flag(sk, SOCK_DONE);
  1310. vsk->peer_shutdown = SHUTDOWN_MASK;
  1311. if (vsock_stream_has_data(vsk) <= 0)
  1312. sk->sk_state = SS_DISCONNECTING;
  1313. sk->sk_state_change(sk);
  1314. break;
  1315. default:
  1316. vsk = vsock_sk(sk);
  1317. vmci_trans(vsk)->notify_ops->handle_notify_pkt(
  1318. sk, pkt, false, NULL, NULL,
  1319. &pkt_processed);
  1320. if (!pkt_processed)
  1321. return -EINVAL;
  1322. break;
  1323. }
  1324. return 0;
  1325. }
  1326. static int vmci_transport_socket_init(struct vsock_sock *vsk,
  1327. struct vsock_sock *psk)
  1328. {
  1329. vsk->trans = kmalloc(sizeof(struct vmci_transport), GFP_KERNEL);
  1330. if (!vsk->trans)
  1331. return -ENOMEM;
  1332. vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
  1333. vmci_trans(vsk)->qp_handle = VMCI_INVALID_HANDLE;
  1334. vmci_trans(vsk)->qpair = NULL;
  1335. vmci_trans(vsk)->produce_size = vmci_trans(vsk)->consume_size = 0;
  1336. vmci_trans(vsk)->detach_sub_id = VMCI_INVALID_ID;
  1337. vmci_trans(vsk)->notify_ops = NULL;
  1338. INIT_LIST_HEAD(&vmci_trans(vsk)->elem);
  1339. vmci_trans(vsk)->sk = &vsk->sk;
  1340. spin_lock_init(&vmci_trans(vsk)->lock);
  1341. if (psk) {
  1342. vmci_trans(vsk)->queue_pair_size =
  1343. vmci_trans(psk)->queue_pair_size;
  1344. vmci_trans(vsk)->queue_pair_min_size =
  1345. vmci_trans(psk)->queue_pair_min_size;
  1346. vmci_trans(vsk)->queue_pair_max_size =
  1347. vmci_trans(psk)->queue_pair_max_size;
  1348. } else {
  1349. vmci_trans(vsk)->queue_pair_size =
  1350. VMCI_TRANSPORT_DEFAULT_QP_SIZE;
  1351. vmci_trans(vsk)->queue_pair_min_size =
  1352. VMCI_TRANSPORT_DEFAULT_QP_SIZE_MIN;
  1353. vmci_trans(vsk)->queue_pair_max_size =
  1354. VMCI_TRANSPORT_DEFAULT_QP_SIZE_MAX;
  1355. }
  1356. return 0;
  1357. }
  1358. static void vmci_transport_free_resources(struct list_head *transport_list)
  1359. {
  1360. while (!list_empty(transport_list)) {
  1361. struct vmci_transport *transport =
  1362. list_first_entry(transport_list, struct vmci_transport,
  1363. elem);
  1364. list_del(&transport->elem);
  1365. if (transport->detach_sub_id != VMCI_INVALID_ID) {
  1366. vmci_event_unsubscribe(transport->detach_sub_id);
  1367. transport->detach_sub_id = VMCI_INVALID_ID;
  1368. }
  1369. if (!vmci_handle_is_invalid(transport->qp_handle)) {
  1370. vmci_qpair_detach(&transport->qpair);
  1371. transport->qp_handle = VMCI_INVALID_HANDLE;
  1372. transport->produce_size = 0;
  1373. transport->consume_size = 0;
  1374. }
  1375. kfree(transport);
  1376. }
  1377. }
  1378. static void vmci_transport_cleanup(struct work_struct *work)
  1379. {
  1380. LIST_HEAD(pending);
  1381. spin_lock_bh(&vmci_transport_cleanup_lock);
  1382. list_replace_init(&vmci_transport_cleanup_list, &pending);
  1383. spin_unlock_bh(&vmci_transport_cleanup_lock);
  1384. vmci_transport_free_resources(&pending);
  1385. }
  1386. static void vmci_transport_destruct(struct vsock_sock *vsk)
  1387. {
  1388. /* Ensure that the detach callback doesn't use the sk/vsk
  1389. * we are about to destruct.
  1390. */
  1391. spin_lock_bh(&vmci_trans(vsk)->lock);
  1392. vmci_trans(vsk)->sk = NULL;
  1393. spin_unlock_bh(&vmci_trans(vsk)->lock);
  1394. if (vmci_trans(vsk)->notify_ops)
  1395. vmci_trans(vsk)->notify_ops->socket_destruct(vsk);
  1396. spin_lock_bh(&vmci_transport_cleanup_lock);
  1397. list_add(&vmci_trans(vsk)->elem, &vmci_transport_cleanup_list);
  1398. spin_unlock_bh(&vmci_transport_cleanup_lock);
  1399. schedule_work(&vmci_transport_cleanup_work);
  1400. vsk->trans = NULL;
  1401. }
  1402. static void vmci_transport_release(struct vsock_sock *vsk)
  1403. {
  1404. vsock_remove_sock(vsk);
  1405. if (!vmci_handle_is_invalid(vmci_trans(vsk)->dg_handle)) {
  1406. vmci_datagram_destroy_handle(vmci_trans(vsk)->dg_handle);
  1407. vmci_trans(vsk)->dg_handle = VMCI_INVALID_HANDLE;
  1408. }
  1409. }
  1410. static int vmci_transport_dgram_bind(struct vsock_sock *vsk,
  1411. struct sockaddr_vm *addr)
  1412. {
  1413. u32 port;
  1414. u32 flags;
  1415. int err;
  1416. /* VMCI will select a resource ID for us if we provide
  1417. * VMCI_INVALID_ID.
  1418. */
  1419. port = addr->svm_port == VMADDR_PORT_ANY ?
  1420. VMCI_INVALID_ID : addr->svm_port;
  1421. if (port <= LAST_RESERVED_PORT && !capable(CAP_NET_BIND_SERVICE))
  1422. return -EACCES;
  1423. flags = addr->svm_cid == VMADDR_CID_ANY ?
  1424. VMCI_FLAG_ANYCID_DG_HND : 0;
  1425. err = vmci_transport_datagram_create_hnd(port, flags,
  1426. vmci_transport_recv_dgram_cb,
  1427. &vsk->sk,
  1428. &vmci_trans(vsk)->dg_handle);
  1429. if (err < VMCI_SUCCESS)
  1430. return vmci_transport_error_to_vsock_error(err);
  1431. vsock_addr_init(&vsk->local_addr, addr->svm_cid,
  1432. vmci_trans(vsk)->dg_handle.resource);
  1433. return 0;
  1434. }
  1435. static int vmci_transport_dgram_enqueue(
  1436. struct vsock_sock *vsk,
  1437. struct sockaddr_vm *remote_addr,
  1438. struct msghdr *msg,
  1439. size_t len)
  1440. {
  1441. int err;
  1442. struct vmci_datagram *dg;
  1443. if (len > VMCI_MAX_DG_PAYLOAD_SIZE)
  1444. return -EMSGSIZE;
  1445. if (!vmci_transport_allow_dgram(vsk, remote_addr->svm_cid))
  1446. return -EPERM;
  1447. /* Allocate a buffer for the user's message and our packet header. */
  1448. dg = kmalloc(len + sizeof(*dg), GFP_KERNEL);
  1449. if (!dg)
  1450. return -ENOMEM;
  1451. memcpy_from_msg(VMCI_DG_PAYLOAD(dg), msg, len);
  1452. dg->dst = vmci_make_handle(remote_addr->svm_cid,
  1453. remote_addr->svm_port);
  1454. dg->src = vmci_make_handle(vsk->local_addr.svm_cid,
  1455. vsk->local_addr.svm_port);
  1456. dg->payload_size = len;
  1457. err = vmci_datagram_send(dg);
  1458. kfree(dg);
  1459. if (err < 0)
  1460. return vmci_transport_error_to_vsock_error(err);
  1461. return err - sizeof(*dg);
  1462. }
  1463. static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,
  1464. struct msghdr *msg, size_t len,
  1465. int flags)
  1466. {
  1467. int err;
  1468. int noblock;
  1469. struct vmci_datagram *dg;
  1470. size_t payload_len;
  1471. struct sk_buff *skb;
  1472. noblock = flags & MSG_DONTWAIT;
  1473. if (flags & MSG_OOB || flags & MSG_ERRQUEUE)
  1474. return -EOPNOTSUPP;
  1475. /* Retrieve the head sk_buff from the socket's receive queue. */
  1476. err = 0;
  1477. skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err);
  1478. if (!skb)
  1479. return err;
  1480. dg = (struct vmci_datagram *)skb->data;
  1481. if (!dg)
  1482. /* err is 0, meaning we read zero bytes. */
  1483. goto out;
  1484. payload_len = dg->payload_size;
  1485. /* Ensure the sk_buff matches the payload size claimed in the packet. */
  1486. if (payload_len != skb->len - sizeof(*dg)) {
  1487. err = -EINVAL;
  1488. goto out;
  1489. }
  1490. if (payload_len > len) {
  1491. payload_len = len;
  1492. msg->msg_flags |= MSG_TRUNC;
  1493. }
  1494. /* Place the datagram payload in the user's iovec. */
  1495. err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len);
  1496. if (err)
  1497. goto out;
  1498. if (msg->msg_name) {
  1499. /* Provide the address of the sender. */
  1500. DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name);
  1501. vsock_addr_init(vm_addr, dg->src.context, dg->src.resource);
  1502. msg->msg_namelen = sizeof(*vm_addr);
  1503. }
  1504. err = payload_len;
  1505. out:
  1506. skb_free_datagram(&vsk->sk, skb);
  1507. return err;
  1508. }
  1509. static bool vmci_transport_dgram_allow(u32 cid, u32 port)
  1510. {
  1511. if (cid == VMADDR_CID_HYPERVISOR) {
  1512. /* Registrations of PBRPC Servers do not modify VMX/Hypervisor
  1513. * state and are allowed.
  1514. */
  1515. return port == VMCI_UNITY_PBRPC_REGISTER;
  1516. }
  1517. return true;
  1518. }
  1519. static int vmci_transport_connect(struct vsock_sock *vsk)
  1520. {
  1521. int err;
  1522. bool old_pkt_proto = false;
  1523. struct sock *sk = &vsk->sk;
  1524. if (vmci_transport_old_proto_override(&old_pkt_proto) &&
  1525. old_pkt_proto) {
  1526. err = vmci_transport_send_conn_request(
  1527. sk, vmci_trans(vsk)->queue_pair_size);
  1528. if (err < 0) {
  1529. sk->sk_state = SS_UNCONNECTED;
  1530. return err;
  1531. }
  1532. } else {
  1533. int supported_proto_versions =
  1534. vmci_transport_new_proto_supported_versions();
  1535. err = vmci_transport_send_conn_request2(
  1536. sk, vmci_trans(vsk)->queue_pair_size,
  1537. supported_proto_versions);
  1538. if (err < 0) {
  1539. sk->sk_state = SS_UNCONNECTED;
  1540. return err;
  1541. }
  1542. vsk->sent_request = true;
  1543. }
  1544. return err;
  1545. }
  1546. static ssize_t vmci_transport_stream_dequeue(
  1547. struct vsock_sock *vsk,
  1548. struct msghdr *msg,
  1549. size_t len,
  1550. int flags)
  1551. {
  1552. if (flags & MSG_PEEK)
  1553. return vmci_qpair_peekv(vmci_trans(vsk)->qpair, msg, len, 0);
  1554. else
  1555. return vmci_qpair_dequev(vmci_trans(vsk)->qpair, msg, len, 0);
  1556. }
  1557. static ssize_t vmci_transport_stream_enqueue(
  1558. struct vsock_sock *vsk,
  1559. struct msghdr *msg,
  1560. size_t len)
  1561. {
  1562. return vmci_qpair_enquev(vmci_trans(vsk)->qpair, msg, len, 0);
  1563. }
  1564. static s64 vmci_transport_stream_has_data(struct vsock_sock *vsk)
  1565. {
  1566. return vmci_qpair_consume_buf_ready(vmci_trans(vsk)->qpair);
  1567. }
  1568. static s64 vmci_transport_stream_has_space(struct vsock_sock *vsk)
  1569. {
  1570. return vmci_qpair_produce_free_space(vmci_trans(vsk)->qpair);
  1571. }
  1572. static u64 vmci_transport_stream_rcvhiwat(struct vsock_sock *vsk)
  1573. {
  1574. return vmci_trans(vsk)->consume_size;
  1575. }
  1576. static bool vmci_transport_stream_is_active(struct vsock_sock *vsk)
  1577. {
  1578. return !vmci_handle_is_invalid(vmci_trans(vsk)->qp_handle);
  1579. }
  1580. static u64 vmci_transport_get_buffer_size(struct vsock_sock *vsk)
  1581. {
  1582. return vmci_trans(vsk)->queue_pair_size;
  1583. }
  1584. static u64 vmci_transport_get_min_buffer_size(struct vsock_sock *vsk)
  1585. {
  1586. return vmci_trans(vsk)->queue_pair_min_size;
  1587. }
  1588. static u64 vmci_transport_get_max_buffer_size(struct vsock_sock *vsk)
  1589. {
  1590. return vmci_trans(vsk)->queue_pair_max_size;
  1591. }
  1592. static void vmci_transport_set_buffer_size(struct vsock_sock *vsk, u64 val)
  1593. {
  1594. if (val < vmci_trans(vsk)->queue_pair_min_size)
  1595. vmci_trans(vsk)->queue_pair_min_size = val;
  1596. if (val > vmci_trans(vsk)->queue_pair_max_size)
  1597. vmci_trans(vsk)->queue_pair_max_size = val;
  1598. vmci_trans(vsk)->queue_pair_size = val;
  1599. }
  1600. static void vmci_transport_set_min_buffer_size(struct vsock_sock *vsk,
  1601. u64 val)
  1602. {
  1603. if (val > vmci_trans(vsk)->queue_pair_size)
  1604. vmci_trans(vsk)->queue_pair_size = val;
  1605. vmci_trans(vsk)->queue_pair_min_size = val;
  1606. }
  1607. static void vmci_transport_set_max_buffer_size(struct vsock_sock *vsk,
  1608. u64 val)
  1609. {
  1610. if (val < vmci_trans(vsk)->queue_pair_size)
  1611. vmci_trans(vsk)->queue_pair_size = val;
  1612. vmci_trans(vsk)->queue_pair_max_size = val;
  1613. }
  1614. static int vmci_transport_notify_poll_in(
  1615. struct vsock_sock *vsk,
  1616. size_t target,
  1617. bool *data_ready_now)
  1618. {
  1619. return vmci_trans(vsk)->notify_ops->poll_in(
  1620. &vsk->sk, target, data_ready_now);
  1621. }
  1622. static int vmci_transport_notify_poll_out(
  1623. struct vsock_sock *vsk,
  1624. size_t target,
  1625. bool *space_available_now)
  1626. {
  1627. return vmci_trans(vsk)->notify_ops->poll_out(
  1628. &vsk->sk, target, space_available_now);
  1629. }
  1630. static int vmci_transport_notify_recv_init(
  1631. struct vsock_sock *vsk,
  1632. size_t target,
  1633. struct vsock_transport_recv_notify_data *data)
  1634. {
  1635. return vmci_trans(vsk)->notify_ops->recv_init(
  1636. &vsk->sk, target,
  1637. (struct vmci_transport_recv_notify_data *)data);
  1638. }
  1639. static int vmci_transport_notify_recv_pre_block(
  1640. struct vsock_sock *vsk,
  1641. size_t target,
  1642. struct vsock_transport_recv_notify_data *data)
  1643. {
  1644. return vmci_trans(vsk)->notify_ops->recv_pre_block(
  1645. &vsk->sk, target,
  1646. (struct vmci_transport_recv_notify_data *)data);
  1647. }
  1648. static int vmci_transport_notify_recv_pre_dequeue(
  1649. struct vsock_sock *vsk,
  1650. size_t target,
  1651. struct vsock_transport_recv_notify_data *data)
  1652. {
  1653. return vmci_trans(vsk)->notify_ops->recv_pre_dequeue(
  1654. &vsk->sk, target,
  1655. (struct vmci_transport_recv_notify_data *)data);
  1656. }
  1657. static int vmci_transport_notify_recv_post_dequeue(
  1658. struct vsock_sock *vsk,
  1659. size_t target,
  1660. ssize_t copied,
  1661. bool data_read,
  1662. struct vsock_transport_recv_notify_data *data)
  1663. {
  1664. return vmci_trans(vsk)->notify_ops->recv_post_dequeue(
  1665. &vsk->sk, target, copied, data_read,
  1666. (struct vmci_transport_recv_notify_data *)data);
  1667. }
  1668. static int vmci_transport_notify_send_init(
  1669. struct vsock_sock *vsk,
  1670. struct vsock_transport_send_notify_data *data)
  1671. {
  1672. return vmci_trans(vsk)->notify_ops->send_init(
  1673. &vsk->sk,
  1674. (struct vmci_transport_send_notify_data *)data);
  1675. }
  1676. static int vmci_transport_notify_send_pre_block(
  1677. struct vsock_sock *vsk,
  1678. struct vsock_transport_send_notify_data *data)
  1679. {
  1680. return vmci_trans(vsk)->notify_ops->send_pre_block(
  1681. &vsk->sk,
  1682. (struct vmci_transport_send_notify_data *)data);
  1683. }
  1684. static int vmci_transport_notify_send_pre_enqueue(
  1685. struct vsock_sock *vsk,
  1686. struct vsock_transport_send_notify_data *data)
  1687. {
  1688. return vmci_trans(vsk)->notify_ops->send_pre_enqueue(
  1689. &vsk->sk,
  1690. (struct vmci_transport_send_notify_data *)data);
  1691. }
  1692. static int vmci_transport_notify_send_post_enqueue(
  1693. struct vsock_sock *vsk,
  1694. ssize_t written,
  1695. struct vsock_transport_send_notify_data *data)
  1696. {
  1697. return vmci_trans(vsk)->notify_ops->send_post_enqueue(
  1698. &vsk->sk, written,
  1699. (struct vmci_transport_send_notify_data *)data);
  1700. }
  1701. static bool vmci_transport_old_proto_override(bool *old_pkt_proto)
  1702. {
  1703. if (PROTOCOL_OVERRIDE != -1) {
  1704. if (PROTOCOL_OVERRIDE == 0)
  1705. *old_pkt_proto = true;
  1706. else
  1707. *old_pkt_proto = false;
  1708. pr_info("Proto override in use\n");
  1709. return true;
  1710. }
  1711. return false;
  1712. }
  1713. static bool vmci_transport_proto_to_notify_struct(struct sock *sk,
  1714. u16 *proto,
  1715. bool old_pkt_proto)
  1716. {
  1717. struct vsock_sock *vsk = vsock_sk(sk);
  1718. if (old_pkt_proto) {
  1719. if (*proto != VSOCK_PROTO_INVALID) {
  1720. pr_err("Can't set both an old and new protocol\n");
  1721. return false;
  1722. }
  1723. vmci_trans(vsk)->notify_ops = &vmci_transport_notify_pkt_ops;
  1724. goto exit;
  1725. }
  1726. switch (*proto) {
  1727. case VSOCK_PROTO_PKT_ON_NOTIFY:
  1728. vmci_trans(vsk)->notify_ops =
  1729. &vmci_transport_notify_pkt_q_state_ops;
  1730. break;
  1731. default:
  1732. pr_err("Unknown notify protocol version\n");
  1733. return false;
  1734. }
  1735. exit:
  1736. vmci_trans(vsk)->notify_ops->socket_init(sk);
  1737. return true;
  1738. }
  1739. static u16 vmci_transport_new_proto_supported_versions(void)
  1740. {
  1741. if (PROTOCOL_OVERRIDE != -1)
  1742. return PROTOCOL_OVERRIDE;
  1743. return VSOCK_PROTO_ALL_SUPPORTED;
  1744. }
  1745. static u32 vmci_transport_get_local_cid(void)
  1746. {
  1747. return vmci_get_context_id();
  1748. }
  1749. static const struct vsock_transport vmci_transport = {
  1750. .init = vmci_transport_socket_init,
  1751. .destruct = vmci_transport_destruct,
  1752. .release = vmci_transport_release,
  1753. .connect = vmci_transport_connect,
  1754. .dgram_bind = vmci_transport_dgram_bind,
  1755. .dgram_dequeue = vmci_transport_dgram_dequeue,
  1756. .dgram_enqueue = vmci_transport_dgram_enqueue,
  1757. .dgram_allow = vmci_transport_dgram_allow,
  1758. .stream_dequeue = vmci_transport_stream_dequeue,
  1759. .stream_enqueue = vmci_transport_stream_enqueue,
  1760. .stream_has_data = vmci_transport_stream_has_data,
  1761. .stream_has_space = vmci_transport_stream_has_space,
  1762. .stream_rcvhiwat = vmci_transport_stream_rcvhiwat,
  1763. .stream_is_active = vmci_transport_stream_is_active,
  1764. .stream_allow = vmci_transport_stream_allow,
  1765. .notify_poll_in = vmci_transport_notify_poll_in,
  1766. .notify_poll_out = vmci_transport_notify_poll_out,
  1767. .notify_recv_init = vmci_transport_notify_recv_init,
  1768. .notify_recv_pre_block = vmci_transport_notify_recv_pre_block,
  1769. .notify_recv_pre_dequeue = vmci_transport_notify_recv_pre_dequeue,
  1770. .notify_recv_post_dequeue = vmci_transport_notify_recv_post_dequeue,
  1771. .notify_send_init = vmci_transport_notify_send_init,
  1772. .notify_send_pre_block = vmci_transport_notify_send_pre_block,
  1773. .notify_send_pre_enqueue = vmci_transport_notify_send_pre_enqueue,
  1774. .notify_send_post_enqueue = vmci_transport_notify_send_post_enqueue,
  1775. .shutdown = vmci_transport_shutdown,
  1776. .set_buffer_size = vmci_transport_set_buffer_size,
  1777. .set_min_buffer_size = vmci_transport_set_min_buffer_size,
  1778. .set_max_buffer_size = vmci_transport_set_max_buffer_size,
  1779. .get_buffer_size = vmci_transport_get_buffer_size,
  1780. .get_min_buffer_size = vmci_transport_get_min_buffer_size,
  1781. .get_max_buffer_size = vmci_transport_get_max_buffer_size,
  1782. .get_local_cid = vmci_transport_get_local_cid,
  1783. };
  1784. static int __init vmci_transport_init(void)
  1785. {
  1786. int err;
  1787. /* Create the datagram handle that we will use to send and receive all
  1788. * VSocket control messages for this context.
  1789. */
  1790. err = vmci_transport_datagram_create_hnd(VMCI_TRANSPORT_PACKET_RID,
  1791. VMCI_FLAG_ANYCID_DG_HND,
  1792. vmci_transport_recv_stream_cb,
  1793. NULL,
  1794. &vmci_transport_stream_handle);
  1795. if (err < VMCI_SUCCESS) {
  1796. pr_err("Unable to create datagram handle. (%d)\n", err);
  1797. return vmci_transport_error_to_vsock_error(err);
  1798. }
  1799. err = vmci_event_subscribe(VMCI_EVENT_QP_RESUMED,
  1800. vmci_transport_qp_resumed_cb,
  1801. NULL, &vmci_transport_qp_resumed_sub_id);
  1802. if (err < VMCI_SUCCESS) {
  1803. pr_err("Unable to subscribe to resumed event. (%d)\n", err);
  1804. err = vmci_transport_error_to_vsock_error(err);
  1805. vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
  1806. goto err_destroy_stream_handle;
  1807. }
  1808. err = vsock_core_init(&vmci_transport);
  1809. if (err < 0)
  1810. goto err_unsubscribe;
  1811. return 0;
  1812. err_unsubscribe:
  1813. vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
  1814. err_destroy_stream_handle:
  1815. vmci_datagram_destroy_handle(vmci_transport_stream_handle);
  1816. return err;
  1817. }
  1818. module_init(vmci_transport_init);
  1819. static void __exit vmci_transport_exit(void)
  1820. {
  1821. cancel_work_sync(&vmci_transport_cleanup_work);
  1822. vmci_transport_free_resources(&vmci_transport_cleanup_list);
  1823. if (!vmci_handle_is_invalid(vmci_transport_stream_handle)) {
  1824. if (vmci_datagram_destroy_handle(
  1825. vmci_transport_stream_handle) != VMCI_SUCCESS)
  1826. pr_err("Couldn't destroy datagram handle\n");
  1827. vmci_transport_stream_handle = VMCI_INVALID_HANDLE;
  1828. }
  1829. if (vmci_transport_qp_resumed_sub_id != VMCI_INVALID_ID) {
  1830. vmci_event_unsubscribe(vmci_transport_qp_resumed_sub_id);
  1831. vmci_transport_qp_resumed_sub_id = VMCI_INVALID_ID;
  1832. }
  1833. vsock_core_exit();
  1834. }
  1835. module_exit(vmci_transport_exit);
  1836. MODULE_AUTHOR("VMware, Inc.");
  1837. MODULE_DESCRIPTION("VMCI transport for Virtual Sockets");
  1838. MODULE_VERSION("1.0.4.0-k");
  1839. MODULE_LICENSE("GPL v2");
  1840. MODULE_ALIAS("vmware_vsock");
  1841. MODULE_ALIAS_NETPROTO(PF_VSOCK);