vmci_transport.c 59 KB

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