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