vmci_transport.c 59 KB

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