vmci_transport.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  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);