af_vsock.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  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. /* Implementation notes:
  16. *
  17. * - There are two kinds of sockets: those created by user action (such as
  18. * calling socket(2)) and those created by incoming connection request packets.
  19. *
  20. * - There are two "global" tables, one for bound sockets (sockets that have
  21. * specified an address that they are responsible for) and one for connected
  22. * sockets (sockets that have established a connection with another socket).
  23. * These tables are "global" in that all sockets on the system are placed
  24. * within them. - Note, though, that the bound table contains an extra entry
  25. * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in
  26. * that list. The bound table is used solely for lookup of sockets when packets
  27. * are received and that's not necessary for SOCK_DGRAM sockets since we create
  28. * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM
  29. * sockets out of the bound hash buckets will reduce the chance of collisions
  30. * when looking for SOCK_STREAM sockets and prevents us from having to check the
  31. * socket type in the hash table lookups.
  32. *
  33. * - Sockets created by user action will either be "client" sockets that
  34. * initiate a connection or "server" sockets that listen for connections; we do
  35. * not support simultaneous connects (two "client" sockets connecting).
  36. *
  37. * - "Server" sockets are referred to as listener sockets throughout this
  38. * implementation because they are in the VSOCK_SS_LISTEN state. When a
  39. * connection request is received (the second kind of socket mentioned above),
  40. * we create a new socket and refer to it as a pending socket. These pending
  41. * sockets are placed on the pending connection list of the listener socket.
  42. * When future packets are received for the address the listener socket is
  43. * bound to, we check if the source of the packet is from one that has an
  44. * existing pending connection. If it does, we process the packet for the
  45. * pending socket. When that socket reaches the connected state, it is removed
  46. * from the listener socket's pending list and enqueued in the listener
  47. * socket's accept queue. Callers of accept(2) will accept connected sockets
  48. * from the listener socket's accept queue. If the socket cannot be accepted
  49. * for some reason then it is marked rejected. Once the connection is
  50. * accepted, it is owned by the user process and the responsibility for cleanup
  51. * falls with that user process.
  52. *
  53. * - It is possible that these pending sockets will never reach the connected
  54. * state; in fact, we may never receive another packet after the connection
  55. * request. Because of this, we must schedule a cleanup function to run in the
  56. * future, after some amount of time passes where a connection should have been
  57. * established. This function ensures that the socket is off all lists so it
  58. * cannot be retrieved, then drops all references to the socket so it is cleaned
  59. * up (sock_put() -> sk_free() -> our sk_destruct implementation). Note this
  60. * function will also cleanup rejected sockets, those that reach the connected
  61. * state but leave it before they have been accepted.
  62. *
  63. * - Lock ordering for pending or accept queue sockets is:
  64. *
  65. * lock_sock(listener);
  66. * lock_sock_nested(pending, SINGLE_DEPTH_NESTING);
  67. *
  68. * Using explicit nested locking keeps lockdep happy since normally only one
  69. * lock of a given class may be taken at a time.
  70. *
  71. * - Sockets created by user action will be cleaned up when the user process
  72. * calls close(2), causing our release implementation to be called. Our release
  73. * implementation will perform some cleanup then drop the last reference so our
  74. * sk_destruct implementation is invoked. Our sk_destruct implementation will
  75. * perform additional cleanup that's common for both types of sockets.
  76. *
  77. * - A socket's reference count is what ensures that the structure won't be
  78. * freed. Each entry in a list (such as the "global" bound and connected tables
  79. * and the listener socket's pending list and connected queue) ensures a
  80. * reference. When we defer work until process context and pass a socket as our
  81. * argument, we must ensure the reference count is increased to ensure the
  82. * socket isn't freed before the function is run; the deferred function will
  83. * then drop the reference.
  84. */
  85. #include <linux/types.h>
  86. #include <linux/bitops.h>
  87. #include <linux/cred.h>
  88. #include <linux/init.h>
  89. #include <linux/io.h>
  90. #include <linux/kernel.h>
  91. #include <linux/sched/signal.h>
  92. #include <linux/kmod.h>
  93. #include <linux/list.h>
  94. #include <linux/miscdevice.h>
  95. #include <linux/module.h>
  96. #include <linux/mutex.h>
  97. #include <linux/net.h>
  98. #include <linux/poll.h>
  99. #include <linux/skbuff.h>
  100. #include <linux/smp.h>
  101. #include <linux/socket.h>
  102. #include <linux/stddef.h>
  103. #include <linux/unistd.h>
  104. #include <linux/wait.h>
  105. #include <linux/workqueue.h>
  106. #include <net/sock.h>
  107. #include <net/af_vsock.h>
  108. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr);
  109. static void vsock_sk_destruct(struct sock *sk);
  110. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
  111. /* Protocol family. */
  112. static struct proto vsock_proto = {
  113. .name = "AF_VSOCK",
  114. .owner = THIS_MODULE,
  115. .obj_size = sizeof(struct vsock_sock),
  116. };
  117. /* The default peer timeout indicates how long we will wait for a peer response
  118. * to a control message.
  119. */
  120. #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
  121. static const struct vsock_transport *transport;
  122. static DEFINE_MUTEX(vsock_register_mutex);
  123. /**** EXPORTS ****/
  124. /* Get the ID of the local context. This is transport dependent. */
  125. int vm_sockets_get_local_cid(void)
  126. {
  127. return transport->get_local_cid();
  128. }
  129. EXPORT_SYMBOL_GPL(vm_sockets_get_local_cid);
  130. /**** UTILS ****/
  131. /* Each bound VSocket is stored in the bind hash table and each connected
  132. * VSocket is stored in the connected hash table.
  133. *
  134. * Unbound sockets are all put on the same list attached to the end of the hash
  135. * table (vsock_unbound_sockets). Bound sockets are added to the hash table in
  136. * the bucket that their local address hashes to (vsock_bound_sockets(addr)
  137. * represents the list that addr hashes to).
  138. *
  139. * Specifically, we initialize the vsock_bind_table array to a size of
  140. * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through
  141. * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and
  142. * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function
  143. * mods with VSOCK_HASH_SIZE to ensure this.
  144. */
  145. #define VSOCK_HASH_SIZE 251
  146. #define MAX_PORT_RETRIES 24
  147. #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
  148. #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
  149. #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE])
  150. /* XXX This can probably be implemented in a better way. */
  151. #define VSOCK_CONN_HASH(src, dst) \
  152. (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
  153. #define vsock_connected_sockets(src, dst) \
  154. (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
  155. #define vsock_connected_sockets_vsk(vsk) \
  156. vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
  157. static struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
  158. static struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
  159. static DEFINE_SPINLOCK(vsock_table_lock);
  160. /* Autobind this socket to the local address if necessary. */
  161. static int vsock_auto_bind(struct vsock_sock *vsk)
  162. {
  163. struct sock *sk = sk_vsock(vsk);
  164. struct sockaddr_vm local_addr;
  165. if (vsock_addr_bound(&vsk->local_addr))
  166. return 0;
  167. vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  168. return __vsock_bind(sk, &local_addr);
  169. }
  170. static void vsock_init_tables(void)
  171. {
  172. int i;
  173. for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++)
  174. INIT_LIST_HEAD(&vsock_bind_table[i]);
  175. for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++)
  176. INIT_LIST_HEAD(&vsock_connected_table[i]);
  177. }
  178. static void __vsock_insert_bound(struct list_head *list,
  179. struct vsock_sock *vsk)
  180. {
  181. sock_hold(&vsk->sk);
  182. list_add(&vsk->bound_table, list);
  183. }
  184. static void __vsock_insert_connected(struct list_head *list,
  185. struct vsock_sock *vsk)
  186. {
  187. sock_hold(&vsk->sk);
  188. list_add(&vsk->connected_table, list);
  189. }
  190. static void __vsock_remove_bound(struct vsock_sock *vsk)
  191. {
  192. list_del_init(&vsk->bound_table);
  193. sock_put(&vsk->sk);
  194. }
  195. static void __vsock_remove_connected(struct vsock_sock *vsk)
  196. {
  197. list_del_init(&vsk->connected_table);
  198. sock_put(&vsk->sk);
  199. }
  200. static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
  201. {
  202. struct vsock_sock *vsk;
  203. list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table)
  204. if (addr->svm_port == vsk->local_addr.svm_port)
  205. return sk_vsock(vsk);
  206. return NULL;
  207. }
  208. static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
  209. struct sockaddr_vm *dst)
  210. {
  211. struct vsock_sock *vsk;
  212. list_for_each_entry(vsk, vsock_connected_sockets(src, dst),
  213. connected_table) {
  214. if (vsock_addr_equals_addr(src, &vsk->remote_addr) &&
  215. dst->svm_port == vsk->local_addr.svm_port) {
  216. return sk_vsock(vsk);
  217. }
  218. }
  219. return NULL;
  220. }
  221. static bool __vsock_in_bound_table(struct vsock_sock *vsk)
  222. {
  223. return !list_empty(&vsk->bound_table);
  224. }
  225. static bool __vsock_in_connected_table(struct vsock_sock *vsk)
  226. {
  227. return !list_empty(&vsk->connected_table);
  228. }
  229. static void vsock_insert_unbound(struct vsock_sock *vsk)
  230. {
  231. spin_lock_bh(&vsock_table_lock);
  232. __vsock_insert_bound(vsock_unbound_sockets, vsk);
  233. spin_unlock_bh(&vsock_table_lock);
  234. }
  235. void vsock_insert_connected(struct vsock_sock *vsk)
  236. {
  237. struct list_head *list = vsock_connected_sockets(
  238. &vsk->remote_addr, &vsk->local_addr);
  239. spin_lock_bh(&vsock_table_lock);
  240. __vsock_insert_connected(list, vsk);
  241. spin_unlock_bh(&vsock_table_lock);
  242. }
  243. EXPORT_SYMBOL_GPL(vsock_insert_connected);
  244. void vsock_remove_bound(struct vsock_sock *vsk)
  245. {
  246. spin_lock_bh(&vsock_table_lock);
  247. __vsock_remove_bound(vsk);
  248. spin_unlock_bh(&vsock_table_lock);
  249. }
  250. EXPORT_SYMBOL_GPL(vsock_remove_bound);
  251. void vsock_remove_connected(struct vsock_sock *vsk)
  252. {
  253. spin_lock_bh(&vsock_table_lock);
  254. __vsock_remove_connected(vsk);
  255. spin_unlock_bh(&vsock_table_lock);
  256. }
  257. EXPORT_SYMBOL_GPL(vsock_remove_connected);
  258. struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
  259. {
  260. struct sock *sk;
  261. spin_lock_bh(&vsock_table_lock);
  262. sk = __vsock_find_bound_socket(addr);
  263. if (sk)
  264. sock_hold(sk);
  265. spin_unlock_bh(&vsock_table_lock);
  266. return sk;
  267. }
  268. EXPORT_SYMBOL_GPL(vsock_find_bound_socket);
  269. struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
  270. struct sockaddr_vm *dst)
  271. {
  272. struct sock *sk;
  273. spin_lock_bh(&vsock_table_lock);
  274. sk = __vsock_find_connected_socket(src, dst);
  275. if (sk)
  276. sock_hold(sk);
  277. spin_unlock_bh(&vsock_table_lock);
  278. return sk;
  279. }
  280. EXPORT_SYMBOL_GPL(vsock_find_connected_socket);
  281. static bool vsock_in_bound_table(struct vsock_sock *vsk)
  282. {
  283. bool ret;
  284. spin_lock_bh(&vsock_table_lock);
  285. ret = __vsock_in_bound_table(vsk);
  286. spin_unlock_bh(&vsock_table_lock);
  287. return ret;
  288. }
  289. static bool vsock_in_connected_table(struct vsock_sock *vsk)
  290. {
  291. bool ret;
  292. spin_lock_bh(&vsock_table_lock);
  293. ret = __vsock_in_connected_table(vsk);
  294. spin_unlock_bh(&vsock_table_lock);
  295. return ret;
  296. }
  297. void vsock_remove_sock(struct vsock_sock *vsk)
  298. {
  299. if (vsock_in_bound_table(vsk))
  300. vsock_remove_bound(vsk);
  301. if (vsock_in_connected_table(vsk))
  302. vsock_remove_connected(vsk);
  303. }
  304. EXPORT_SYMBOL_GPL(vsock_remove_sock);
  305. void vsock_for_each_connected_socket(void (*fn)(struct sock *sk))
  306. {
  307. int i;
  308. spin_lock_bh(&vsock_table_lock);
  309. for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
  310. struct vsock_sock *vsk;
  311. list_for_each_entry(vsk, &vsock_connected_table[i],
  312. connected_table)
  313. fn(sk_vsock(vsk));
  314. }
  315. spin_unlock_bh(&vsock_table_lock);
  316. }
  317. EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket);
  318. void vsock_add_pending(struct sock *listener, struct sock *pending)
  319. {
  320. struct vsock_sock *vlistener;
  321. struct vsock_sock *vpending;
  322. vlistener = vsock_sk(listener);
  323. vpending = vsock_sk(pending);
  324. sock_hold(pending);
  325. sock_hold(listener);
  326. list_add_tail(&vpending->pending_links, &vlistener->pending_links);
  327. }
  328. EXPORT_SYMBOL_GPL(vsock_add_pending);
  329. void vsock_remove_pending(struct sock *listener, struct sock *pending)
  330. {
  331. struct vsock_sock *vpending = vsock_sk(pending);
  332. list_del_init(&vpending->pending_links);
  333. sock_put(listener);
  334. sock_put(pending);
  335. }
  336. EXPORT_SYMBOL_GPL(vsock_remove_pending);
  337. void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
  338. {
  339. struct vsock_sock *vlistener;
  340. struct vsock_sock *vconnected;
  341. vlistener = vsock_sk(listener);
  342. vconnected = vsock_sk(connected);
  343. sock_hold(connected);
  344. sock_hold(listener);
  345. list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue);
  346. }
  347. EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
  348. static struct sock *vsock_dequeue_accept(struct sock *listener)
  349. {
  350. struct vsock_sock *vlistener;
  351. struct vsock_sock *vconnected;
  352. vlistener = vsock_sk(listener);
  353. if (list_empty(&vlistener->accept_queue))
  354. return NULL;
  355. vconnected = list_entry(vlistener->accept_queue.next,
  356. struct vsock_sock, accept_queue);
  357. list_del_init(&vconnected->accept_queue);
  358. sock_put(listener);
  359. /* The caller will need a reference on the connected socket so we let
  360. * it call sock_put().
  361. */
  362. return sk_vsock(vconnected);
  363. }
  364. static bool vsock_is_accept_queue_empty(struct sock *sk)
  365. {
  366. struct vsock_sock *vsk = vsock_sk(sk);
  367. return list_empty(&vsk->accept_queue);
  368. }
  369. static bool vsock_is_pending(struct sock *sk)
  370. {
  371. struct vsock_sock *vsk = vsock_sk(sk);
  372. return !list_empty(&vsk->pending_links);
  373. }
  374. static int vsock_send_shutdown(struct sock *sk, int mode)
  375. {
  376. return transport->shutdown(vsock_sk(sk), mode);
  377. }
  378. void vsock_pending_work(struct work_struct *work)
  379. {
  380. struct sock *sk;
  381. struct sock *listener;
  382. struct vsock_sock *vsk;
  383. bool cleanup;
  384. vsk = container_of(work, struct vsock_sock, dwork.work);
  385. sk = sk_vsock(vsk);
  386. listener = vsk->listener;
  387. cleanup = true;
  388. lock_sock(listener);
  389. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  390. if (vsock_is_pending(sk)) {
  391. vsock_remove_pending(listener, sk);
  392. listener->sk_ack_backlog--;
  393. } else if (!vsk->rejected) {
  394. /* We are not on the pending list and accept() did not reject
  395. * us, so we must have been accepted by our user process. We
  396. * just need to drop our references to the sockets and be on
  397. * our way.
  398. */
  399. cleanup = false;
  400. goto out;
  401. }
  402. /* We need to remove ourself from the global connected sockets list so
  403. * incoming packets can't find this socket, and to reduce the reference
  404. * count.
  405. */
  406. if (vsock_in_connected_table(vsk))
  407. vsock_remove_connected(vsk);
  408. sk->sk_state = SS_FREE;
  409. out:
  410. release_sock(sk);
  411. release_sock(listener);
  412. if (cleanup)
  413. sock_put(sk);
  414. sock_put(sk);
  415. sock_put(listener);
  416. }
  417. EXPORT_SYMBOL_GPL(vsock_pending_work);
  418. /**** SOCKET OPERATIONS ****/
  419. static int __vsock_bind_stream(struct vsock_sock *vsk,
  420. struct sockaddr_vm *addr)
  421. {
  422. static u32 port = LAST_RESERVED_PORT + 1;
  423. struct sockaddr_vm new_addr;
  424. vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
  425. if (addr->svm_port == VMADDR_PORT_ANY) {
  426. bool found = false;
  427. unsigned int i;
  428. for (i = 0; i < MAX_PORT_RETRIES; i++) {
  429. if (port <= LAST_RESERVED_PORT)
  430. port = LAST_RESERVED_PORT + 1;
  431. new_addr.svm_port = port++;
  432. if (!__vsock_find_bound_socket(&new_addr)) {
  433. found = true;
  434. break;
  435. }
  436. }
  437. if (!found)
  438. return -EADDRNOTAVAIL;
  439. } else {
  440. /* If port is in reserved range, ensure caller
  441. * has necessary privileges.
  442. */
  443. if (addr->svm_port <= LAST_RESERVED_PORT &&
  444. !capable(CAP_NET_BIND_SERVICE)) {
  445. return -EACCES;
  446. }
  447. if (__vsock_find_bound_socket(&new_addr))
  448. return -EADDRINUSE;
  449. }
  450. vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);
  451. /* Remove stream sockets from the unbound list and add them to the hash
  452. * table for easy lookup by its address. The unbound list is simply an
  453. * extra entry at the end of the hash table, a trick used by AF_UNIX.
  454. */
  455. __vsock_remove_bound(vsk);
  456. __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
  457. return 0;
  458. }
  459. static int __vsock_bind_dgram(struct vsock_sock *vsk,
  460. struct sockaddr_vm *addr)
  461. {
  462. return transport->dgram_bind(vsk, addr);
  463. }
  464. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
  465. {
  466. struct vsock_sock *vsk = vsock_sk(sk);
  467. u32 cid;
  468. int retval;
  469. /* First ensure this socket isn't already bound. */
  470. if (vsock_addr_bound(&vsk->local_addr))
  471. return -EINVAL;
  472. /* Now bind to the provided address or select appropriate values if
  473. * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that
  474. * like AF_INET prevents binding to a non-local IP address (in most
  475. * cases), we only allow binding to the local CID.
  476. */
  477. cid = transport->get_local_cid();
  478. if (addr->svm_cid != cid && addr->svm_cid != VMADDR_CID_ANY)
  479. return -EADDRNOTAVAIL;
  480. switch (sk->sk_socket->type) {
  481. case SOCK_STREAM:
  482. spin_lock_bh(&vsock_table_lock);
  483. retval = __vsock_bind_stream(vsk, addr);
  484. spin_unlock_bh(&vsock_table_lock);
  485. break;
  486. case SOCK_DGRAM:
  487. retval = __vsock_bind_dgram(vsk, addr);
  488. break;
  489. default:
  490. retval = -EINVAL;
  491. break;
  492. }
  493. return retval;
  494. }
  495. struct sock *__vsock_create(struct net *net,
  496. struct socket *sock,
  497. struct sock *parent,
  498. gfp_t priority,
  499. unsigned short type,
  500. int kern)
  501. {
  502. struct sock *sk;
  503. struct vsock_sock *psk;
  504. struct vsock_sock *vsk;
  505. sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern);
  506. if (!sk)
  507. return NULL;
  508. sock_init_data(sock, sk);
  509. /* sk->sk_type is normally set in sock_init_data, but only if sock is
  510. * non-NULL. We make sure that our sockets always have a type by
  511. * setting it here if needed.
  512. */
  513. if (!sock)
  514. sk->sk_type = type;
  515. vsk = vsock_sk(sk);
  516. vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  517. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  518. sk->sk_destruct = vsock_sk_destruct;
  519. sk->sk_backlog_rcv = vsock_queue_rcv_skb;
  520. sk->sk_state = 0;
  521. sock_reset_flag(sk, SOCK_DONE);
  522. INIT_LIST_HEAD(&vsk->bound_table);
  523. INIT_LIST_HEAD(&vsk->connected_table);
  524. vsk->listener = NULL;
  525. INIT_LIST_HEAD(&vsk->pending_links);
  526. INIT_LIST_HEAD(&vsk->accept_queue);
  527. vsk->rejected = false;
  528. vsk->sent_request = false;
  529. vsk->ignore_connecting_rst = false;
  530. vsk->peer_shutdown = 0;
  531. psk = parent ? vsock_sk(parent) : NULL;
  532. if (parent) {
  533. vsk->trusted = psk->trusted;
  534. vsk->owner = get_cred(psk->owner);
  535. vsk->connect_timeout = psk->connect_timeout;
  536. } else {
  537. vsk->trusted = capable(CAP_NET_ADMIN);
  538. vsk->owner = get_current_cred();
  539. vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT;
  540. }
  541. if (transport->init(vsk, psk) < 0) {
  542. sk_free(sk);
  543. return NULL;
  544. }
  545. if (sock)
  546. vsock_insert_unbound(vsk);
  547. return sk;
  548. }
  549. EXPORT_SYMBOL_GPL(__vsock_create);
  550. static void __vsock_release(struct sock *sk)
  551. {
  552. if (sk) {
  553. struct sk_buff *skb;
  554. struct sock *pending;
  555. struct vsock_sock *vsk;
  556. vsk = vsock_sk(sk);
  557. pending = NULL; /* Compiler warning. */
  558. transport->release(vsk);
  559. lock_sock(sk);
  560. sock_orphan(sk);
  561. sk->sk_shutdown = SHUTDOWN_MASK;
  562. while ((skb = skb_dequeue(&sk->sk_receive_queue)))
  563. kfree_skb(skb);
  564. /* Clean up any sockets that never were accepted. */
  565. while ((pending = vsock_dequeue_accept(sk)) != NULL) {
  566. __vsock_release(pending);
  567. sock_put(pending);
  568. }
  569. release_sock(sk);
  570. sock_put(sk);
  571. }
  572. }
  573. static void vsock_sk_destruct(struct sock *sk)
  574. {
  575. struct vsock_sock *vsk = vsock_sk(sk);
  576. transport->destruct(vsk);
  577. /* When clearing these addresses, there's no need to set the family and
  578. * possibly register the address family with the kernel.
  579. */
  580. vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  581. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  582. put_cred(vsk->owner);
  583. }
  584. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  585. {
  586. int err;
  587. err = sock_queue_rcv_skb(sk, skb);
  588. if (err)
  589. kfree_skb(skb);
  590. return err;
  591. }
  592. s64 vsock_stream_has_data(struct vsock_sock *vsk)
  593. {
  594. return transport->stream_has_data(vsk);
  595. }
  596. EXPORT_SYMBOL_GPL(vsock_stream_has_data);
  597. s64 vsock_stream_has_space(struct vsock_sock *vsk)
  598. {
  599. return transport->stream_has_space(vsk);
  600. }
  601. EXPORT_SYMBOL_GPL(vsock_stream_has_space);
  602. static int vsock_release(struct socket *sock)
  603. {
  604. __vsock_release(sock->sk);
  605. sock->sk = NULL;
  606. sock->state = SS_FREE;
  607. return 0;
  608. }
  609. static int
  610. vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  611. {
  612. int err;
  613. struct sock *sk;
  614. struct sockaddr_vm *vm_addr;
  615. sk = sock->sk;
  616. if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0)
  617. return -EINVAL;
  618. lock_sock(sk);
  619. err = __vsock_bind(sk, vm_addr);
  620. release_sock(sk);
  621. return err;
  622. }
  623. static int vsock_getname(struct socket *sock,
  624. struct sockaddr *addr, int *addr_len, int peer)
  625. {
  626. int err;
  627. struct sock *sk;
  628. struct vsock_sock *vsk;
  629. struct sockaddr_vm *vm_addr;
  630. sk = sock->sk;
  631. vsk = vsock_sk(sk);
  632. err = 0;
  633. lock_sock(sk);
  634. if (peer) {
  635. if (sock->state != SS_CONNECTED) {
  636. err = -ENOTCONN;
  637. goto out;
  638. }
  639. vm_addr = &vsk->remote_addr;
  640. } else {
  641. vm_addr = &vsk->local_addr;
  642. }
  643. if (!vm_addr) {
  644. err = -EINVAL;
  645. goto out;
  646. }
  647. /* sys_getsockname() and sys_getpeername() pass us a
  648. * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately
  649. * that macro is defined in socket.c instead of .h, so we hardcode its
  650. * value here.
  651. */
  652. BUILD_BUG_ON(sizeof(*vm_addr) > 128);
  653. memcpy(addr, vm_addr, sizeof(*vm_addr));
  654. *addr_len = sizeof(*vm_addr);
  655. out:
  656. release_sock(sk);
  657. return err;
  658. }
  659. static int vsock_shutdown(struct socket *sock, int mode)
  660. {
  661. int err;
  662. struct sock *sk;
  663. /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
  664. * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
  665. * here like the other address families do. Note also that the
  666. * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
  667. * which is what we want.
  668. */
  669. mode++;
  670. if ((mode & ~SHUTDOWN_MASK) || !mode)
  671. return -EINVAL;
  672. /* If this is a STREAM socket and it is not connected then bail out
  673. * immediately. If it is a DGRAM socket then we must first kick the
  674. * socket so that it wakes up from any sleeping calls, for example
  675. * recv(), and then afterwards return the error.
  676. */
  677. sk = sock->sk;
  678. if (sock->state == SS_UNCONNECTED) {
  679. err = -ENOTCONN;
  680. if (sk->sk_type == SOCK_STREAM)
  681. return err;
  682. } else {
  683. sock->state = SS_DISCONNECTING;
  684. err = 0;
  685. }
  686. /* Receive and send shutdowns are treated alike. */
  687. mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
  688. if (mode) {
  689. lock_sock(sk);
  690. sk->sk_shutdown |= mode;
  691. sk->sk_state_change(sk);
  692. release_sock(sk);
  693. if (sk->sk_type == SOCK_STREAM) {
  694. sock_reset_flag(sk, SOCK_DONE);
  695. vsock_send_shutdown(sk, mode);
  696. }
  697. }
  698. return err;
  699. }
  700. static unsigned int vsock_poll(struct file *file, struct socket *sock,
  701. poll_table *wait)
  702. {
  703. struct sock *sk;
  704. unsigned int mask;
  705. struct vsock_sock *vsk;
  706. sk = sock->sk;
  707. vsk = vsock_sk(sk);
  708. poll_wait(file, sk_sleep(sk), wait);
  709. mask = 0;
  710. if (sk->sk_err)
  711. /* Signify that there has been an error on this socket. */
  712. mask |= POLLERR;
  713. /* INET sockets treat local write shutdown and peer write shutdown as a
  714. * case of POLLHUP set.
  715. */
  716. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  717. ((sk->sk_shutdown & SEND_SHUTDOWN) &&
  718. (vsk->peer_shutdown & SEND_SHUTDOWN))) {
  719. mask |= POLLHUP;
  720. }
  721. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  722. vsk->peer_shutdown & SEND_SHUTDOWN) {
  723. mask |= POLLRDHUP;
  724. }
  725. if (sock->type == SOCK_DGRAM) {
  726. /* For datagram sockets we can read if there is something in
  727. * the queue and write as long as the socket isn't shutdown for
  728. * sending.
  729. */
  730. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  731. (sk->sk_shutdown & RCV_SHUTDOWN)) {
  732. mask |= POLLIN | POLLRDNORM;
  733. }
  734. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  735. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  736. } else if (sock->type == SOCK_STREAM) {
  737. lock_sock(sk);
  738. /* Listening sockets that have connections in their accept
  739. * queue can be read.
  740. */
  741. if (sk->sk_state == VSOCK_SS_LISTEN
  742. && !vsock_is_accept_queue_empty(sk))
  743. mask |= POLLIN | POLLRDNORM;
  744. /* If there is something in the queue then we can read. */
  745. if (transport->stream_is_active(vsk) &&
  746. !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  747. bool data_ready_now = false;
  748. int ret = transport->notify_poll_in(
  749. vsk, 1, &data_ready_now);
  750. if (ret < 0) {
  751. mask |= POLLERR;
  752. } else {
  753. if (data_ready_now)
  754. mask |= POLLIN | POLLRDNORM;
  755. }
  756. }
  757. /* Sockets whose connections have been closed, reset, or
  758. * terminated should also be considered read, and we check the
  759. * shutdown flag for that.
  760. */
  761. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  762. vsk->peer_shutdown & SEND_SHUTDOWN) {
  763. mask |= POLLIN | POLLRDNORM;
  764. }
  765. /* Connected sockets that can produce data can be written. */
  766. if (sk->sk_state == SS_CONNECTED) {
  767. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  768. bool space_avail_now = false;
  769. int ret = transport->notify_poll_out(
  770. vsk, 1, &space_avail_now);
  771. if (ret < 0) {
  772. mask |= POLLERR;
  773. } else {
  774. if (space_avail_now)
  775. /* Remove POLLWRBAND since INET
  776. * sockets are not setting it.
  777. */
  778. mask |= POLLOUT | POLLWRNORM;
  779. }
  780. }
  781. }
  782. /* Simulate INET socket poll behaviors, which sets
  783. * POLLOUT|POLLWRNORM when peer is closed and nothing to read,
  784. * but local send is not shutdown.
  785. */
  786. if (sk->sk_state == SS_UNCONNECTED) {
  787. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  788. mask |= POLLOUT | POLLWRNORM;
  789. }
  790. release_sock(sk);
  791. }
  792. return mask;
  793. }
  794. static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  795. size_t len)
  796. {
  797. int err;
  798. struct sock *sk;
  799. struct vsock_sock *vsk;
  800. struct sockaddr_vm *remote_addr;
  801. if (msg->msg_flags & MSG_OOB)
  802. return -EOPNOTSUPP;
  803. /* For now, MSG_DONTWAIT is always assumed... */
  804. err = 0;
  805. sk = sock->sk;
  806. vsk = vsock_sk(sk);
  807. lock_sock(sk);
  808. err = vsock_auto_bind(vsk);
  809. if (err)
  810. goto out;
  811. /* If the provided message contains an address, use that. Otherwise
  812. * fall back on the socket's remote handle (if it has been connected).
  813. */
  814. if (msg->msg_name &&
  815. vsock_addr_cast(msg->msg_name, msg->msg_namelen,
  816. &remote_addr) == 0) {
  817. /* Ensure this address is of the right type and is a valid
  818. * destination.
  819. */
  820. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  821. remote_addr->svm_cid = transport->get_local_cid();
  822. if (!vsock_addr_bound(remote_addr)) {
  823. err = -EINVAL;
  824. goto out;
  825. }
  826. } else if (sock->state == SS_CONNECTED) {
  827. remote_addr = &vsk->remote_addr;
  828. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  829. remote_addr->svm_cid = transport->get_local_cid();
  830. /* XXX Should connect() or this function ensure remote_addr is
  831. * bound?
  832. */
  833. if (!vsock_addr_bound(&vsk->remote_addr)) {
  834. err = -EINVAL;
  835. goto out;
  836. }
  837. } else {
  838. err = -EINVAL;
  839. goto out;
  840. }
  841. if (!transport->dgram_allow(remote_addr->svm_cid,
  842. remote_addr->svm_port)) {
  843. err = -EINVAL;
  844. goto out;
  845. }
  846. err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
  847. out:
  848. release_sock(sk);
  849. return err;
  850. }
  851. static int vsock_dgram_connect(struct socket *sock,
  852. struct sockaddr *addr, int addr_len, int flags)
  853. {
  854. int err;
  855. struct sock *sk;
  856. struct vsock_sock *vsk;
  857. struct sockaddr_vm *remote_addr;
  858. sk = sock->sk;
  859. vsk = vsock_sk(sk);
  860. err = vsock_addr_cast(addr, addr_len, &remote_addr);
  861. if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) {
  862. lock_sock(sk);
  863. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY,
  864. VMADDR_PORT_ANY);
  865. sock->state = SS_UNCONNECTED;
  866. release_sock(sk);
  867. return 0;
  868. } else if (err != 0)
  869. return -EINVAL;
  870. lock_sock(sk);
  871. err = vsock_auto_bind(vsk);
  872. if (err)
  873. goto out;
  874. if (!transport->dgram_allow(remote_addr->svm_cid,
  875. remote_addr->svm_port)) {
  876. err = -EINVAL;
  877. goto out;
  878. }
  879. memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr));
  880. sock->state = SS_CONNECTED;
  881. out:
  882. release_sock(sk);
  883. return err;
  884. }
  885. static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  886. size_t len, int flags)
  887. {
  888. return transport->dgram_dequeue(vsock_sk(sock->sk), msg, len, flags);
  889. }
  890. static const struct proto_ops vsock_dgram_ops = {
  891. .family = PF_VSOCK,
  892. .owner = THIS_MODULE,
  893. .release = vsock_release,
  894. .bind = vsock_bind,
  895. .connect = vsock_dgram_connect,
  896. .socketpair = sock_no_socketpair,
  897. .accept = sock_no_accept,
  898. .getname = vsock_getname,
  899. .poll = vsock_poll,
  900. .ioctl = sock_no_ioctl,
  901. .listen = sock_no_listen,
  902. .shutdown = vsock_shutdown,
  903. .setsockopt = sock_no_setsockopt,
  904. .getsockopt = sock_no_getsockopt,
  905. .sendmsg = vsock_dgram_sendmsg,
  906. .recvmsg = vsock_dgram_recvmsg,
  907. .mmap = sock_no_mmap,
  908. .sendpage = sock_no_sendpage,
  909. };
  910. static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
  911. {
  912. if (!transport->cancel_pkt)
  913. return -EOPNOTSUPP;
  914. return transport->cancel_pkt(vsk);
  915. }
  916. static void vsock_connect_timeout(struct work_struct *work)
  917. {
  918. struct sock *sk;
  919. struct vsock_sock *vsk;
  920. int cancel = 0;
  921. vsk = container_of(work, struct vsock_sock, dwork.work);
  922. sk = sk_vsock(vsk);
  923. lock_sock(sk);
  924. if (sk->sk_state == SS_CONNECTING &&
  925. (sk->sk_shutdown != SHUTDOWN_MASK)) {
  926. sk->sk_state = SS_UNCONNECTED;
  927. sk->sk_err = ETIMEDOUT;
  928. sk->sk_error_report(sk);
  929. cancel = 1;
  930. }
  931. release_sock(sk);
  932. if (cancel)
  933. vsock_transport_cancel_pkt(vsk);
  934. sock_put(sk);
  935. }
  936. static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
  937. int addr_len, int flags)
  938. {
  939. int err;
  940. struct sock *sk;
  941. struct vsock_sock *vsk;
  942. struct sockaddr_vm *remote_addr;
  943. long timeout;
  944. DEFINE_WAIT(wait);
  945. err = 0;
  946. sk = sock->sk;
  947. vsk = vsock_sk(sk);
  948. lock_sock(sk);
  949. /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
  950. switch (sock->state) {
  951. case SS_CONNECTED:
  952. err = -EISCONN;
  953. goto out;
  954. case SS_DISCONNECTING:
  955. err = -EINVAL;
  956. goto out;
  957. case SS_CONNECTING:
  958. /* This continues on so we can move sock into the SS_CONNECTED
  959. * state once the connection has completed (at which point err
  960. * will be set to zero also). Otherwise, we will either wait
  961. * for the connection or return -EALREADY should this be a
  962. * non-blocking call.
  963. */
  964. err = -EALREADY;
  965. break;
  966. default:
  967. if ((sk->sk_state == VSOCK_SS_LISTEN) ||
  968. vsock_addr_cast(addr, addr_len, &remote_addr) != 0) {
  969. err = -EINVAL;
  970. goto out;
  971. }
  972. /* The hypervisor and well-known contexts do not have socket
  973. * endpoints.
  974. */
  975. if (!transport->stream_allow(remote_addr->svm_cid,
  976. remote_addr->svm_port)) {
  977. err = -ENETUNREACH;
  978. goto out;
  979. }
  980. /* Set the remote address that we are connecting to. */
  981. memcpy(&vsk->remote_addr, remote_addr,
  982. sizeof(vsk->remote_addr));
  983. err = vsock_auto_bind(vsk);
  984. if (err)
  985. goto out;
  986. sk->sk_state = SS_CONNECTING;
  987. err = transport->connect(vsk);
  988. if (err < 0)
  989. goto out;
  990. /* Mark sock as connecting and set the error code to in
  991. * progress in case this is a non-blocking connect.
  992. */
  993. sock->state = SS_CONNECTING;
  994. err = -EINPROGRESS;
  995. }
  996. /* The receive path will handle all communication until we are able to
  997. * enter the connected state. Here we wait for the connection to be
  998. * completed or a notification of an error.
  999. */
  1000. timeout = vsk->connect_timeout;
  1001. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1002. while (sk->sk_state != SS_CONNECTED && sk->sk_err == 0) {
  1003. if (flags & O_NONBLOCK) {
  1004. /* If we're not going to block, we schedule a timeout
  1005. * function to generate a timeout on the connection
  1006. * attempt, in case the peer doesn't respond in a
  1007. * timely manner. We hold on to the socket until the
  1008. * timeout fires.
  1009. */
  1010. sock_hold(sk);
  1011. INIT_DELAYED_WORK(&vsk->dwork,
  1012. vsock_connect_timeout);
  1013. schedule_delayed_work(&vsk->dwork, timeout);
  1014. /* Skip ahead to preserve error code set above. */
  1015. goto out_wait;
  1016. }
  1017. release_sock(sk);
  1018. timeout = schedule_timeout(timeout);
  1019. lock_sock(sk);
  1020. if (signal_pending(current)) {
  1021. err = sock_intr_errno(timeout);
  1022. sk->sk_state = SS_UNCONNECTED;
  1023. sock->state = SS_UNCONNECTED;
  1024. vsock_transport_cancel_pkt(vsk);
  1025. goto out_wait;
  1026. } else if (timeout == 0) {
  1027. err = -ETIMEDOUT;
  1028. sk->sk_state = SS_UNCONNECTED;
  1029. sock->state = SS_UNCONNECTED;
  1030. vsock_transport_cancel_pkt(vsk);
  1031. goto out_wait;
  1032. }
  1033. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1034. }
  1035. if (sk->sk_err) {
  1036. err = -sk->sk_err;
  1037. sk->sk_state = SS_UNCONNECTED;
  1038. sock->state = SS_UNCONNECTED;
  1039. } else {
  1040. err = 0;
  1041. }
  1042. out_wait:
  1043. finish_wait(sk_sleep(sk), &wait);
  1044. out:
  1045. release_sock(sk);
  1046. return err;
  1047. }
  1048. static int vsock_accept(struct socket *sock, struct socket *newsock, int flags,
  1049. bool kern)
  1050. {
  1051. struct sock *listener;
  1052. int err;
  1053. struct sock *connected;
  1054. struct vsock_sock *vconnected;
  1055. long timeout;
  1056. DEFINE_WAIT(wait);
  1057. err = 0;
  1058. listener = sock->sk;
  1059. lock_sock(listener);
  1060. if (sock->type != SOCK_STREAM) {
  1061. err = -EOPNOTSUPP;
  1062. goto out;
  1063. }
  1064. if (listener->sk_state != VSOCK_SS_LISTEN) {
  1065. err = -EINVAL;
  1066. goto out;
  1067. }
  1068. /* Wait for children sockets to appear; these are the new sockets
  1069. * created upon connection establishment.
  1070. */
  1071. timeout = sock_sndtimeo(listener, flags & O_NONBLOCK);
  1072. prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
  1073. while ((connected = vsock_dequeue_accept(listener)) == NULL &&
  1074. listener->sk_err == 0) {
  1075. release_sock(listener);
  1076. timeout = schedule_timeout(timeout);
  1077. finish_wait(sk_sleep(listener), &wait);
  1078. lock_sock(listener);
  1079. if (signal_pending(current)) {
  1080. err = sock_intr_errno(timeout);
  1081. goto out;
  1082. } else if (timeout == 0) {
  1083. err = -EAGAIN;
  1084. goto out;
  1085. }
  1086. prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
  1087. }
  1088. finish_wait(sk_sleep(listener), &wait);
  1089. if (listener->sk_err)
  1090. err = -listener->sk_err;
  1091. if (connected) {
  1092. listener->sk_ack_backlog--;
  1093. lock_sock_nested(connected, SINGLE_DEPTH_NESTING);
  1094. vconnected = vsock_sk(connected);
  1095. /* If the listener socket has received an error, then we should
  1096. * reject this socket and return. Note that we simply mark the
  1097. * socket rejected, drop our reference, and let the cleanup
  1098. * function handle the cleanup; the fact that we found it in
  1099. * the listener's accept queue guarantees that the cleanup
  1100. * function hasn't run yet.
  1101. */
  1102. if (err) {
  1103. vconnected->rejected = true;
  1104. } else {
  1105. newsock->state = SS_CONNECTED;
  1106. sock_graft(connected, newsock);
  1107. }
  1108. release_sock(connected);
  1109. sock_put(connected);
  1110. }
  1111. out:
  1112. release_sock(listener);
  1113. return err;
  1114. }
  1115. static int vsock_listen(struct socket *sock, int backlog)
  1116. {
  1117. int err;
  1118. struct sock *sk;
  1119. struct vsock_sock *vsk;
  1120. sk = sock->sk;
  1121. lock_sock(sk);
  1122. if (sock->type != SOCK_STREAM) {
  1123. err = -EOPNOTSUPP;
  1124. goto out;
  1125. }
  1126. if (sock->state != SS_UNCONNECTED) {
  1127. err = -EINVAL;
  1128. goto out;
  1129. }
  1130. vsk = vsock_sk(sk);
  1131. if (!vsock_addr_bound(&vsk->local_addr)) {
  1132. err = -EINVAL;
  1133. goto out;
  1134. }
  1135. sk->sk_max_ack_backlog = backlog;
  1136. sk->sk_state = VSOCK_SS_LISTEN;
  1137. err = 0;
  1138. out:
  1139. release_sock(sk);
  1140. return err;
  1141. }
  1142. static int vsock_stream_setsockopt(struct socket *sock,
  1143. int level,
  1144. int optname,
  1145. char __user *optval,
  1146. unsigned int optlen)
  1147. {
  1148. int err;
  1149. struct sock *sk;
  1150. struct vsock_sock *vsk;
  1151. u64 val;
  1152. if (level != AF_VSOCK)
  1153. return -ENOPROTOOPT;
  1154. #define COPY_IN(_v) \
  1155. do { \
  1156. if (optlen < sizeof(_v)) { \
  1157. err = -EINVAL; \
  1158. goto exit; \
  1159. } \
  1160. if (copy_from_user(&_v, optval, sizeof(_v)) != 0) { \
  1161. err = -EFAULT; \
  1162. goto exit; \
  1163. } \
  1164. } while (0)
  1165. err = 0;
  1166. sk = sock->sk;
  1167. vsk = vsock_sk(sk);
  1168. lock_sock(sk);
  1169. switch (optname) {
  1170. case SO_VM_SOCKETS_BUFFER_SIZE:
  1171. COPY_IN(val);
  1172. transport->set_buffer_size(vsk, val);
  1173. break;
  1174. case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
  1175. COPY_IN(val);
  1176. transport->set_max_buffer_size(vsk, val);
  1177. break;
  1178. case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
  1179. COPY_IN(val);
  1180. transport->set_min_buffer_size(vsk, val);
  1181. break;
  1182. case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
  1183. struct timeval tv;
  1184. COPY_IN(tv);
  1185. if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC &&
  1186. tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) {
  1187. vsk->connect_timeout = tv.tv_sec * HZ +
  1188. DIV_ROUND_UP(tv.tv_usec, (1000000 / HZ));
  1189. if (vsk->connect_timeout == 0)
  1190. vsk->connect_timeout =
  1191. VSOCK_DEFAULT_CONNECT_TIMEOUT;
  1192. } else {
  1193. err = -ERANGE;
  1194. }
  1195. break;
  1196. }
  1197. default:
  1198. err = -ENOPROTOOPT;
  1199. break;
  1200. }
  1201. #undef COPY_IN
  1202. exit:
  1203. release_sock(sk);
  1204. return err;
  1205. }
  1206. static int vsock_stream_getsockopt(struct socket *sock,
  1207. int level, int optname,
  1208. char __user *optval,
  1209. int __user *optlen)
  1210. {
  1211. int err;
  1212. int len;
  1213. struct sock *sk;
  1214. struct vsock_sock *vsk;
  1215. u64 val;
  1216. if (level != AF_VSOCK)
  1217. return -ENOPROTOOPT;
  1218. err = get_user(len, optlen);
  1219. if (err != 0)
  1220. return err;
  1221. #define COPY_OUT(_v) \
  1222. do { \
  1223. if (len < sizeof(_v)) \
  1224. return -EINVAL; \
  1225. \
  1226. len = sizeof(_v); \
  1227. if (copy_to_user(optval, &_v, len) != 0) \
  1228. return -EFAULT; \
  1229. \
  1230. } while (0)
  1231. err = 0;
  1232. sk = sock->sk;
  1233. vsk = vsock_sk(sk);
  1234. switch (optname) {
  1235. case SO_VM_SOCKETS_BUFFER_SIZE:
  1236. val = transport->get_buffer_size(vsk);
  1237. COPY_OUT(val);
  1238. break;
  1239. case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
  1240. val = transport->get_max_buffer_size(vsk);
  1241. COPY_OUT(val);
  1242. break;
  1243. case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
  1244. val = transport->get_min_buffer_size(vsk);
  1245. COPY_OUT(val);
  1246. break;
  1247. case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
  1248. struct timeval tv;
  1249. tv.tv_sec = vsk->connect_timeout / HZ;
  1250. tv.tv_usec =
  1251. (vsk->connect_timeout -
  1252. tv.tv_sec * HZ) * (1000000 / HZ);
  1253. COPY_OUT(tv);
  1254. break;
  1255. }
  1256. default:
  1257. return -ENOPROTOOPT;
  1258. }
  1259. err = put_user(len, optlen);
  1260. if (err != 0)
  1261. return -EFAULT;
  1262. #undef COPY_OUT
  1263. return 0;
  1264. }
  1265. static int vsock_stream_sendmsg(struct socket *sock, struct msghdr *msg,
  1266. size_t len)
  1267. {
  1268. struct sock *sk;
  1269. struct vsock_sock *vsk;
  1270. ssize_t total_written;
  1271. long timeout;
  1272. int err;
  1273. struct vsock_transport_send_notify_data send_data;
  1274. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1275. sk = sock->sk;
  1276. vsk = vsock_sk(sk);
  1277. total_written = 0;
  1278. err = 0;
  1279. if (msg->msg_flags & MSG_OOB)
  1280. return -EOPNOTSUPP;
  1281. lock_sock(sk);
  1282. /* Callers should not provide a destination with stream sockets. */
  1283. if (msg->msg_namelen) {
  1284. err = sk->sk_state == SS_CONNECTED ? -EISCONN : -EOPNOTSUPP;
  1285. goto out;
  1286. }
  1287. /* Send data only if both sides are not shutdown in the direction. */
  1288. if (sk->sk_shutdown & SEND_SHUTDOWN ||
  1289. vsk->peer_shutdown & RCV_SHUTDOWN) {
  1290. err = -EPIPE;
  1291. goto out;
  1292. }
  1293. if (sk->sk_state != SS_CONNECTED ||
  1294. !vsock_addr_bound(&vsk->local_addr)) {
  1295. err = -ENOTCONN;
  1296. goto out;
  1297. }
  1298. if (!vsock_addr_bound(&vsk->remote_addr)) {
  1299. err = -EDESTADDRREQ;
  1300. goto out;
  1301. }
  1302. /* Wait for room in the produce queue to enqueue our user's data. */
  1303. timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1304. err = transport->notify_send_init(vsk, &send_data);
  1305. if (err < 0)
  1306. goto out;
  1307. while (total_written < len) {
  1308. ssize_t written;
  1309. add_wait_queue(sk_sleep(sk), &wait);
  1310. while (vsock_stream_has_space(vsk) == 0 &&
  1311. sk->sk_err == 0 &&
  1312. !(sk->sk_shutdown & SEND_SHUTDOWN) &&
  1313. !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
  1314. /* Don't wait for non-blocking sockets. */
  1315. if (timeout == 0) {
  1316. err = -EAGAIN;
  1317. remove_wait_queue(sk_sleep(sk), &wait);
  1318. goto out_err;
  1319. }
  1320. err = transport->notify_send_pre_block(vsk, &send_data);
  1321. if (err < 0) {
  1322. remove_wait_queue(sk_sleep(sk), &wait);
  1323. goto out_err;
  1324. }
  1325. release_sock(sk);
  1326. timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
  1327. lock_sock(sk);
  1328. if (signal_pending(current)) {
  1329. err = sock_intr_errno(timeout);
  1330. remove_wait_queue(sk_sleep(sk), &wait);
  1331. goto out_err;
  1332. } else if (timeout == 0) {
  1333. err = -EAGAIN;
  1334. remove_wait_queue(sk_sleep(sk), &wait);
  1335. goto out_err;
  1336. }
  1337. }
  1338. remove_wait_queue(sk_sleep(sk), &wait);
  1339. /* These checks occur both as part of and after the loop
  1340. * conditional since we need to check before and after
  1341. * sleeping.
  1342. */
  1343. if (sk->sk_err) {
  1344. err = -sk->sk_err;
  1345. goto out_err;
  1346. } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
  1347. (vsk->peer_shutdown & RCV_SHUTDOWN)) {
  1348. err = -EPIPE;
  1349. goto out_err;
  1350. }
  1351. err = transport->notify_send_pre_enqueue(vsk, &send_data);
  1352. if (err < 0)
  1353. goto out_err;
  1354. /* Note that enqueue will only write as many bytes as are free
  1355. * in the produce queue, so we don't need to ensure len is
  1356. * smaller than the queue size. It is the caller's
  1357. * responsibility to check how many bytes we were able to send.
  1358. */
  1359. written = transport->stream_enqueue(
  1360. vsk, msg,
  1361. len - total_written);
  1362. if (written < 0) {
  1363. err = -ENOMEM;
  1364. goto out_err;
  1365. }
  1366. total_written += written;
  1367. err = transport->notify_send_post_enqueue(
  1368. vsk, written, &send_data);
  1369. if (err < 0)
  1370. goto out_err;
  1371. }
  1372. out_err:
  1373. if (total_written > 0)
  1374. err = total_written;
  1375. out:
  1376. release_sock(sk);
  1377. return err;
  1378. }
  1379. static int
  1380. vsock_stream_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1381. int flags)
  1382. {
  1383. struct sock *sk;
  1384. struct vsock_sock *vsk;
  1385. int err;
  1386. size_t target;
  1387. ssize_t copied;
  1388. long timeout;
  1389. struct vsock_transport_recv_notify_data recv_data;
  1390. DEFINE_WAIT(wait);
  1391. sk = sock->sk;
  1392. vsk = vsock_sk(sk);
  1393. err = 0;
  1394. lock_sock(sk);
  1395. if (sk->sk_state != SS_CONNECTED) {
  1396. /* Recvmsg is supposed to return 0 if a peer performs an
  1397. * orderly shutdown. Differentiate between that case and when a
  1398. * peer has not connected or a local shutdown occured with the
  1399. * SOCK_DONE flag.
  1400. */
  1401. if (sock_flag(sk, SOCK_DONE))
  1402. err = 0;
  1403. else
  1404. err = -ENOTCONN;
  1405. goto out;
  1406. }
  1407. if (flags & MSG_OOB) {
  1408. err = -EOPNOTSUPP;
  1409. goto out;
  1410. }
  1411. /* We don't check peer_shutdown flag here since peer may actually shut
  1412. * down, but there can be data in the queue that a local socket can
  1413. * receive.
  1414. */
  1415. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1416. err = 0;
  1417. goto out;
  1418. }
  1419. /* It is valid on Linux to pass in a zero-length receive buffer. This
  1420. * is not an error. We may as well bail out now.
  1421. */
  1422. if (!len) {
  1423. err = 0;
  1424. goto out;
  1425. }
  1426. /* We must not copy less than target bytes into the user's buffer
  1427. * before returning successfully, so we wait for the consume queue to
  1428. * have that much data to consume before dequeueing. Note that this
  1429. * makes it impossible to handle cases where target is greater than the
  1430. * queue size.
  1431. */
  1432. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1433. if (target >= transport->stream_rcvhiwat(vsk)) {
  1434. err = -ENOMEM;
  1435. goto out;
  1436. }
  1437. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1438. copied = 0;
  1439. err = transport->notify_recv_init(vsk, target, &recv_data);
  1440. if (err < 0)
  1441. goto out;
  1442. while (1) {
  1443. s64 ready;
  1444. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1445. ready = vsock_stream_has_data(vsk);
  1446. if (ready == 0) {
  1447. if (sk->sk_err != 0 ||
  1448. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1449. (vsk->peer_shutdown & SEND_SHUTDOWN)) {
  1450. finish_wait(sk_sleep(sk), &wait);
  1451. break;
  1452. }
  1453. /* Don't wait for non-blocking sockets. */
  1454. if (timeout == 0) {
  1455. err = -EAGAIN;
  1456. finish_wait(sk_sleep(sk), &wait);
  1457. break;
  1458. }
  1459. err = transport->notify_recv_pre_block(
  1460. vsk, target, &recv_data);
  1461. if (err < 0) {
  1462. finish_wait(sk_sleep(sk), &wait);
  1463. break;
  1464. }
  1465. release_sock(sk);
  1466. timeout = schedule_timeout(timeout);
  1467. lock_sock(sk);
  1468. if (signal_pending(current)) {
  1469. err = sock_intr_errno(timeout);
  1470. finish_wait(sk_sleep(sk), &wait);
  1471. break;
  1472. } else if (timeout == 0) {
  1473. err = -EAGAIN;
  1474. finish_wait(sk_sleep(sk), &wait);
  1475. break;
  1476. }
  1477. } else {
  1478. ssize_t read;
  1479. finish_wait(sk_sleep(sk), &wait);
  1480. if (ready < 0) {
  1481. /* Invalid queue pair content. XXX This should
  1482. * be changed to a connection reset in a later
  1483. * change.
  1484. */
  1485. err = -ENOMEM;
  1486. goto out;
  1487. }
  1488. err = transport->notify_recv_pre_dequeue(
  1489. vsk, target, &recv_data);
  1490. if (err < 0)
  1491. break;
  1492. read = transport->stream_dequeue(
  1493. vsk, msg,
  1494. len - copied, flags);
  1495. if (read < 0) {
  1496. err = -ENOMEM;
  1497. break;
  1498. }
  1499. copied += read;
  1500. err = transport->notify_recv_post_dequeue(
  1501. vsk, target, read,
  1502. !(flags & MSG_PEEK), &recv_data);
  1503. if (err < 0)
  1504. goto out;
  1505. if (read >= target || flags & MSG_PEEK)
  1506. break;
  1507. target -= read;
  1508. }
  1509. }
  1510. if (sk->sk_err)
  1511. err = -sk->sk_err;
  1512. else if (sk->sk_shutdown & RCV_SHUTDOWN)
  1513. err = 0;
  1514. if (copied > 0)
  1515. err = copied;
  1516. out:
  1517. release_sock(sk);
  1518. return err;
  1519. }
  1520. static const struct proto_ops vsock_stream_ops = {
  1521. .family = PF_VSOCK,
  1522. .owner = THIS_MODULE,
  1523. .release = vsock_release,
  1524. .bind = vsock_bind,
  1525. .connect = vsock_stream_connect,
  1526. .socketpair = sock_no_socketpair,
  1527. .accept = vsock_accept,
  1528. .getname = vsock_getname,
  1529. .poll = vsock_poll,
  1530. .ioctl = sock_no_ioctl,
  1531. .listen = vsock_listen,
  1532. .shutdown = vsock_shutdown,
  1533. .setsockopt = vsock_stream_setsockopt,
  1534. .getsockopt = vsock_stream_getsockopt,
  1535. .sendmsg = vsock_stream_sendmsg,
  1536. .recvmsg = vsock_stream_recvmsg,
  1537. .mmap = sock_no_mmap,
  1538. .sendpage = sock_no_sendpage,
  1539. };
  1540. static int vsock_create(struct net *net, struct socket *sock,
  1541. int protocol, int kern)
  1542. {
  1543. if (!sock)
  1544. return -EINVAL;
  1545. if (protocol && protocol != PF_VSOCK)
  1546. return -EPROTONOSUPPORT;
  1547. switch (sock->type) {
  1548. case SOCK_DGRAM:
  1549. sock->ops = &vsock_dgram_ops;
  1550. break;
  1551. case SOCK_STREAM:
  1552. sock->ops = &vsock_stream_ops;
  1553. break;
  1554. default:
  1555. return -ESOCKTNOSUPPORT;
  1556. }
  1557. sock->state = SS_UNCONNECTED;
  1558. return __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern) ? 0 : -ENOMEM;
  1559. }
  1560. static const struct net_proto_family vsock_family_ops = {
  1561. .family = AF_VSOCK,
  1562. .create = vsock_create,
  1563. .owner = THIS_MODULE,
  1564. };
  1565. static long vsock_dev_do_ioctl(struct file *filp,
  1566. unsigned int cmd, void __user *ptr)
  1567. {
  1568. u32 __user *p = ptr;
  1569. int retval = 0;
  1570. switch (cmd) {
  1571. case IOCTL_VM_SOCKETS_GET_LOCAL_CID:
  1572. if (put_user(transport->get_local_cid(), p) != 0)
  1573. retval = -EFAULT;
  1574. break;
  1575. default:
  1576. pr_err("Unknown ioctl %d\n", cmd);
  1577. retval = -EINVAL;
  1578. }
  1579. return retval;
  1580. }
  1581. static long vsock_dev_ioctl(struct file *filp,
  1582. unsigned int cmd, unsigned long arg)
  1583. {
  1584. return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg);
  1585. }
  1586. #ifdef CONFIG_COMPAT
  1587. static long vsock_dev_compat_ioctl(struct file *filp,
  1588. unsigned int cmd, unsigned long arg)
  1589. {
  1590. return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg));
  1591. }
  1592. #endif
  1593. static const struct file_operations vsock_device_ops = {
  1594. .owner = THIS_MODULE,
  1595. .unlocked_ioctl = vsock_dev_ioctl,
  1596. #ifdef CONFIG_COMPAT
  1597. .compat_ioctl = vsock_dev_compat_ioctl,
  1598. #endif
  1599. .open = nonseekable_open,
  1600. };
  1601. static struct miscdevice vsock_device = {
  1602. .name = "vsock",
  1603. .fops = &vsock_device_ops,
  1604. };
  1605. int __vsock_core_init(const struct vsock_transport *t, struct module *owner)
  1606. {
  1607. int err = mutex_lock_interruptible(&vsock_register_mutex);
  1608. if (err)
  1609. return err;
  1610. if (transport) {
  1611. err = -EBUSY;
  1612. goto err_busy;
  1613. }
  1614. /* Transport must be the owner of the protocol so that it can't
  1615. * unload while there are open sockets.
  1616. */
  1617. vsock_proto.owner = owner;
  1618. transport = t;
  1619. vsock_init_tables();
  1620. vsock_device.minor = MISC_DYNAMIC_MINOR;
  1621. err = misc_register(&vsock_device);
  1622. if (err) {
  1623. pr_err("Failed to register misc device\n");
  1624. goto err_reset_transport;
  1625. }
  1626. err = proto_register(&vsock_proto, 1); /* we want our slab */
  1627. if (err) {
  1628. pr_err("Cannot register vsock protocol\n");
  1629. goto err_deregister_misc;
  1630. }
  1631. err = sock_register(&vsock_family_ops);
  1632. if (err) {
  1633. pr_err("could not register af_vsock (%d) address family: %d\n",
  1634. AF_VSOCK, err);
  1635. goto err_unregister_proto;
  1636. }
  1637. mutex_unlock(&vsock_register_mutex);
  1638. return 0;
  1639. err_unregister_proto:
  1640. proto_unregister(&vsock_proto);
  1641. err_deregister_misc:
  1642. misc_deregister(&vsock_device);
  1643. err_reset_transport:
  1644. transport = NULL;
  1645. err_busy:
  1646. mutex_unlock(&vsock_register_mutex);
  1647. return err;
  1648. }
  1649. EXPORT_SYMBOL_GPL(__vsock_core_init);
  1650. void vsock_core_exit(void)
  1651. {
  1652. mutex_lock(&vsock_register_mutex);
  1653. misc_deregister(&vsock_device);
  1654. sock_unregister(AF_VSOCK);
  1655. proto_unregister(&vsock_proto);
  1656. /* We do not want the assignment below re-ordered. */
  1657. mb();
  1658. transport = NULL;
  1659. mutex_unlock(&vsock_register_mutex);
  1660. }
  1661. EXPORT_SYMBOL_GPL(vsock_core_exit);
  1662. const struct vsock_transport *vsock_core_get_transport(void)
  1663. {
  1664. /* vsock_register_mutex not taken since only the transport uses this
  1665. * function and only while registered.
  1666. */
  1667. return transport;
  1668. }
  1669. EXPORT_SYMBOL_GPL(vsock_core_get_transport);
  1670. MODULE_AUTHOR("VMware, Inc.");
  1671. MODULE_DESCRIPTION("VMware Virtual Socket Family");
  1672. MODULE_VERSION("1.0.2.0-k");
  1673. MODULE_LICENSE("GPL v2");