af_vsock.c 48 KB

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