af_vsock.c 48 KB

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