af_vsock.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. /*
  2. * VMware vSockets Driver
  3. *
  4. * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. /* Implementation notes:
  16. *
  17. * - There are two kinds of sockets: those created by user action (such as
  18. * calling socket(2)) and those created by incoming connection request packets.
  19. *
  20. * - There are two "global" tables, one for bound sockets (sockets that have
  21. * specified an address that they are responsible for) and one for connected
  22. * sockets (sockets that have established a connection with another socket).
  23. * These tables are "global" in that all sockets on the system are placed
  24. * within them. - Note, though, that the bound table contains an extra entry
  25. * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in
  26. * that list. The bound table is used solely for lookup of sockets when packets
  27. * are received and that's not necessary for SOCK_DGRAM sockets since we create
  28. * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM
  29. * sockets out of the bound hash buckets will reduce the chance of collisions
  30. * when looking for SOCK_STREAM sockets and prevents us from having to check the
  31. * socket type in the hash table lookups.
  32. *
  33. * - Sockets created by user action will either be "client" sockets that
  34. * initiate a connection or "server" sockets that listen for connections; we do
  35. * not support simultaneous connects (two "client" sockets connecting).
  36. *
  37. * - "Server" sockets are referred to as listener sockets throughout this
  38. * implementation because they are in the TCP_LISTEN state. When a
  39. * connection request is received (the second kind of socket mentioned above),
  40. * we create a new socket and refer to it as a pending socket. These pending
  41. * sockets are placed on the pending connection list of the listener socket.
  42. * When future packets are received for the address the listener socket is
  43. * bound to, we check if the source of the packet is from one that has an
  44. * existing pending connection. If it does, we process the packet for the
  45. * pending socket. When that socket reaches the connected state, it is removed
  46. * from the listener socket's pending list and enqueued in the listener
  47. * socket's accept queue. Callers of accept(2) will accept connected sockets
  48. * from the listener socket's accept queue. If the socket cannot be accepted
  49. * for some reason then it is marked rejected. Once the connection is
  50. * accepted, it is owned by the user process and the responsibility for cleanup
  51. * falls with that user process.
  52. *
  53. * - It is possible that these pending sockets will never reach the connected
  54. * state; in fact, we may never receive another packet after the connection
  55. * request. Because of this, we must schedule a cleanup function to run in the
  56. * future, after some amount of time passes where a connection should have been
  57. * established. This function ensures that the socket is off all lists so it
  58. * cannot be retrieved, then drops all references to the socket so it is cleaned
  59. * up (sock_put() -> sk_free() -> our sk_destruct implementation). Note this
  60. * function will also cleanup rejected sockets, those that reach the connected
  61. * state but leave it before they have been accepted.
  62. *
  63. * - Lock ordering for pending or accept queue sockets is:
  64. *
  65. * lock_sock(listener);
  66. * lock_sock_nested(pending, SINGLE_DEPTH_NESTING);
  67. *
  68. * Using explicit nested locking keeps lockdep happy since normally only one
  69. * lock of a given class may be taken at a time.
  70. *
  71. * - Sockets created by user action will be cleaned up when the user process
  72. * calls close(2), causing our release implementation to be called. Our release
  73. * implementation will perform some cleanup then drop the last reference so our
  74. * sk_destruct implementation is invoked. Our sk_destruct implementation will
  75. * perform additional cleanup that's common for both types of sockets.
  76. *
  77. * - A socket's reference count is what ensures that the structure won't be
  78. * freed. Each entry in a list (such as the "global" bound and connected tables
  79. * and the listener socket's pending list and connected queue) ensures a
  80. * reference. When we defer work until process context and pass a socket as our
  81. * argument, we must ensure the reference count is increased to ensure the
  82. * socket isn't freed before the function is run; the deferred function will
  83. * then drop the reference.
  84. *
  85. * - sk->sk_state uses the TCP state constants because they are widely used by
  86. * other address families and exposed to userspace tools like ss(8):
  87. *
  88. * TCP_CLOSE - unconnected
  89. * TCP_SYN_SENT - connecting
  90. * TCP_ESTABLISHED - connected
  91. * TCP_CLOSING - disconnecting
  92. * TCP_LISTEN - listening
  93. */
  94. #include <linux/types.h>
  95. #include <linux/bitops.h>
  96. #include <linux/cred.h>
  97. #include <linux/init.h>
  98. #include <linux/io.h>
  99. #include <linux/kernel.h>
  100. #include <linux/sched/signal.h>
  101. #include <linux/kmod.h>
  102. #include <linux/list.h>
  103. #include <linux/miscdevice.h>
  104. #include <linux/module.h>
  105. #include <linux/mutex.h>
  106. #include <linux/net.h>
  107. #include <linux/poll.h>
  108. #include <linux/random.h>
  109. #include <linux/skbuff.h>
  110. #include <linux/smp.h>
  111. #include <linux/socket.h>
  112. #include <linux/stddef.h>
  113. #include <linux/unistd.h>
  114. #include <linux/wait.h>
  115. #include <linux/workqueue.h>
  116. #include <net/sock.h>
  117. #include <net/af_vsock.h>
  118. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr);
  119. static void vsock_sk_destruct(struct sock *sk);
  120. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
  121. /* Protocol family. */
  122. static struct proto vsock_proto = {
  123. .name = "AF_VSOCK",
  124. .owner = THIS_MODULE,
  125. .obj_size = sizeof(struct vsock_sock),
  126. };
  127. /* The default peer timeout indicates how long we will wait for a peer response
  128. * to a control message.
  129. */
  130. #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
  131. static const struct vsock_transport *transport;
  132. static DEFINE_MUTEX(vsock_register_mutex);
  133. /**** EXPORTS ****/
  134. /* Get the ID of the local context. This is transport dependent. */
  135. int vm_sockets_get_local_cid(void)
  136. {
  137. return transport->get_local_cid();
  138. }
  139. EXPORT_SYMBOL_GPL(vm_sockets_get_local_cid);
  140. /**** UTILS ****/
  141. /* Each bound VSocket is stored in the bind hash table and each connected
  142. * VSocket is stored in the connected hash table.
  143. *
  144. * Unbound sockets are all put on the same list attached to the end of the hash
  145. * table (vsock_unbound_sockets). Bound sockets are added to the hash table in
  146. * the bucket that their local address hashes to (vsock_bound_sockets(addr)
  147. * represents the list that addr hashes to).
  148. *
  149. * Specifically, we initialize the vsock_bind_table array to a size of
  150. * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through
  151. * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and
  152. * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function
  153. * mods with VSOCK_HASH_SIZE to ensure this.
  154. */
  155. #define MAX_PORT_RETRIES 24
  156. #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
  157. #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
  158. #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE])
  159. /* XXX This can probably be implemented in a better way. */
  160. #define VSOCK_CONN_HASH(src, dst) \
  161. (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
  162. #define vsock_connected_sockets(src, dst) \
  163. (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
  164. #define vsock_connected_sockets_vsk(vsk) \
  165. vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
  166. struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
  167. EXPORT_SYMBOL_GPL(vsock_bind_table);
  168. struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
  169. EXPORT_SYMBOL_GPL(vsock_connected_table);
  170. DEFINE_SPINLOCK(vsock_table_lock);
  171. EXPORT_SYMBOL_GPL(vsock_table_lock);
  172. /* Autobind this socket to the local address if necessary. */
  173. static int vsock_auto_bind(struct vsock_sock *vsk)
  174. {
  175. struct sock *sk = sk_vsock(vsk);
  176. struct sockaddr_vm local_addr;
  177. if (vsock_addr_bound(&vsk->local_addr))
  178. return 0;
  179. vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  180. return __vsock_bind(sk, &local_addr);
  181. }
  182. static int __init vsock_init_tables(void)
  183. {
  184. int i;
  185. for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++)
  186. INIT_LIST_HEAD(&vsock_bind_table[i]);
  187. for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++)
  188. INIT_LIST_HEAD(&vsock_connected_table[i]);
  189. return 0;
  190. }
  191. static void __vsock_insert_bound(struct list_head *list,
  192. struct vsock_sock *vsk)
  193. {
  194. sock_hold(&vsk->sk);
  195. list_add(&vsk->bound_table, list);
  196. }
  197. static void __vsock_insert_connected(struct list_head *list,
  198. struct vsock_sock *vsk)
  199. {
  200. sock_hold(&vsk->sk);
  201. list_add(&vsk->connected_table, list);
  202. }
  203. static void __vsock_remove_bound(struct vsock_sock *vsk)
  204. {
  205. list_del_init(&vsk->bound_table);
  206. sock_put(&vsk->sk);
  207. }
  208. static void __vsock_remove_connected(struct vsock_sock *vsk)
  209. {
  210. list_del_init(&vsk->connected_table);
  211. sock_put(&vsk->sk);
  212. }
  213. static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
  214. {
  215. struct vsock_sock *vsk;
  216. list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table)
  217. if (addr->svm_port == vsk->local_addr.svm_port)
  218. return sk_vsock(vsk);
  219. return NULL;
  220. }
  221. static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
  222. struct sockaddr_vm *dst)
  223. {
  224. struct vsock_sock *vsk;
  225. list_for_each_entry(vsk, vsock_connected_sockets(src, dst),
  226. connected_table) {
  227. if (vsock_addr_equals_addr(src, &vsk->remote_addr) &&
  228. dst->svm_port == vsk->local_addr.svm_port) {
  229. return sk_vsock(vsk);
  230. }
  231. }
  232. return NULL;
  233. }
  234. static void vsock_insert_unbound(struct vsock_sock *vsk)
  235. {
  236. spin_lock_bh(&vsock_table_lock);
  237. __vsock_insert_bound(vsock_unbound_sockets, vsk);
  238. spin_unlock_bh(&vsock_table_lock);
  239. }
  240. void vsock_insert_connected(struct vsock_sock *vsk)
  241. {
  242. struct list_head *list = vsock_connected_sockets(
  243. &vsk->remote_addr, &vsk->local_addr);
  244. spin_lock_bh(&vsock_table_lock);
  245. __vsock_insert_connected(list, vsk);
  246. spin_unlock_bh(&vsock_table_lock);
  247. }
  248. EXPORT_SYMBOL_GPL(vsock_insert_connected);
  249. void vsock_remove_bound(struct vsock_sock *vsk)
  250. {
  251. spin_lock_bh(&vsock_table_lock);
  252. if (__vsock_in_bound_table(vsk))
  253. __vsock_remove_bound(vsk);
  254. spin_unlock_bh(&vsock_table_lock);
  255. }
  256. EXPORT_SYMBOL_GPL(vsock_remove_bound);
  257. void vsock_remove_connected(struct vsock_sock *vsk)
  258. {
  259. spin_lock_bh(&vsock_table_lock);
  260. if (__vsock_in_connected_table(vsk))
  261. __vsock_remove_connected(vsk);
  262. spin_unlock_bh(&vsock_table_lock);
  263. }
  264. EXPORT_SYMBOL_GPL(vsock_remove_connected);
  265. struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
  266. {
  267. struct sock *sk;
  268. spin_lock_bh(&vsock_table_lock);
  269. sk = __vsock_find_bound_socket(addr);
  270. if (sk)
  271. sock_hold(sk);
  272. spin_unlock_bh(&vsock_table_lock);
  273. return sk;
  274. }
  275. EXPORT_SYMBOL_GPL(vsock_find_bound_socket);
  276. struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
  277. struct sockaddr_vm *dst)
  278. {
  279. struct sock *sk;
  280. spin_lock_bh(&vsock_table_lock);
  281. sk = __vsock_find_connected_socket(src, dst);
  282. if (sk)
  283. sock_hold(sk);
  284. spin_unlock_bh(&vsock_table_lock);
  285. return sk;
  286. }
  287. EXPORT_SYMBOL_GPL(vsock_find_connected_socket);
  288. void vsock_remove_sock(struct vsock_sock *vsk)
  289. {
  290. vsock_remove_bound(vsk);
  291. vsock_remove_connected(vsk);
  292. }
  293. EXPORT_SYMBOL_GPL(vsock_remove_sock);
  294. void vsock_for_each_connected_socket(void (*fn)(struct sock *sk))
  295. {
  296. int i;
  297. spin_lock_bh(&vsock_table_lock);
  298. for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
  299. struct vsock_sock *vsk;
  300. list_for_each_entry(vsk, &vsock_connected_table[i],
  301. connected_table)
  302. fn(sk_vsock(vsk));
  303. }
  304. spin_unlock_bh(&vsock_table_lock);
  305. }
  306. EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket);
  307. void vsock_add_pending(struct sock *listener, struct sock *pending)
  308. {
  309. struct vsock_sock *vlistener;
  310. struct vsock_sock *vpending;
  311. vlistener = vsock_sk(listener);
  312. vpending = vsock_sk(pending);
  313. sock_hold(pending);
  314. sock_hold(listener);
  315. list_add_tail(&vpending->pending_links, &vlistener->pending_links);
  316. }
  317. EXPORT_SYMBOL_GPL(vsock_add_pending);
  318. void vsock_remove_pending(struct sock *listener, struct sock *pending)
  319. {
  320. struct vsock_sock *vpending = vsock_sk(pending);
  321. list_del_init(&vpending->pending_links);
  322. sock_put(listener);
  323. sock_put(pending);
  324. }
  325. EXPORT_SYMBOL_GPL(vsock_remove_pending);
  326. void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
  327. {
  328. struct vsock_sock *vlistener;
  329. struct vsock_sock *vconnected;
  330. vlistener = vsock_sk(listener);
  331. vconnected = vsock_sk(connected);
  332. sock_hold(connected);
  333. sock_hold(listener);
  334. list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue);
  335. }
  336. EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
  337. static struct sock *vsock_dequeue_accept(struct sock *listener)
  338. {
  339. struct vsock_sock *vlistener;
  340. struct vsock_sock *vconnected;
  341. vlistener = vsock_sk(listener);
  342. if (list_empty(&vlistener->accept_queue))
  343. return NULL;
  344. vconnected = list_entry(vlistener->accept_queue.next,
  345. struct vsock_sock, accept_queue);
  346. list_del_init(&vconnected->accept_queue);
  347. sock_put(listener);
  348. /* The caller will need a reference on the connected socket so we let
  349. * it call sock_put().
  350. */
  351. return sk_vsock(vconnected);
  352. }
  353. static bool vsock_is_accept_queue_empty(struct sock *sk)
  354. {
  355. struct vsock_sock *vsk = vsock_sk(sk);
  356. return list_empty(&vsk->accept_queue);
  357. }
  358. static bool vsock_is_pending(struct sock *sk)
  359. {
  360. struct vsock_sock *vsk = vsock_sk(sk);
  361. return !list_empty(&vsk->pending_links);
  362. }
  363. static int vsock_send_shutdown(struct sock *sk, int mode)
  364. {
  365. return transport->shutdown(vsock_sk(sk), mode);
  366. }
  367. static void vsock_pending_work(struct work_struct *work)
  368. {
  369. struct sock *sk;
  370. struct sock *listener;
  371. struct vsock_sock *vsk;
  372. bool cleanup;
  373. vsk = container_of(work, struct vsock_sock, pending_work.work);
  374. sk = sk_vsock(vsk);
  375. listener = vsk->listener;
  376. cleanup = true;
  377. lock_sock(listener);
  378. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  379. if (vsock_is_pending(sk)) {
  380. vsock_remove_pending(listener, sk);
  381. listener->sk_ack_backlog--;
  382. } else if (!vsk->rejected) {
  383. /* We are not on the pending list and accept() did not reject
  384. * us, so we must have been accepted by our user process. We
  385. * just need to drop our references to the sockets and be on
  386. * our way.
  387. */
  388. cleanup = false;
  389. goto out;
  390. }
  391. /* We need to remove ourself from the global connected sockets list so
  392. * incoming packets can't find this socket, and to reduce the reference
  393. * count.
  394. */
  395. vsock_remove_connected(vsk);
  396. sk->sk_state = TCP_CLOSE;
  397. out:
  398. release_sock(sk);
  399. release_sock(listener);
  400. if (cleanup)
  401. sock_put(sk);
  402. sock_put(sk);
  403. sock_put(listener);
  404. }
  405. /**** SOCKET OPERATIONS ****/
  406. static int __vsock_bind_stream(struct vsock_sock *vsk,
  407. struct sockaddr_vm *addr)
  408. {
  409. static u32 port = 0;
  410. struct sockaddr_vm new_addr;
  411. if (!port)
  412. port = LAST_RESERVED_PORT + 1 +
  413. prandom_u32_max(U32_MAX - LAST_RESERVED_PORT);
  414. vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
  415. if (addr->svm_port == VMADDR_PORT_ANY) {
  416. bool found = false;
  417. unsigned int i;
  418. for (i = 0; i < MAX_PORT_RETRIES; i++) {
  419. if (port <= LAST_RESERVED_PORT)
  420. port = LAST_RESERVED_PORT + 1;
  421. new_addr.svm_port = port++;
  422. if (!__vsock_find_bound_socket(&new_addr)) {
  423. found = true;
  424. break;
  425. }
  426. }
  427. if (!found)
  428. return -EADDRNOTAVAIL;
  429. } else {
  430. /* If port is in reserved range, ensure caller
  431. * has necessary privileges.
  432. */
  433. if (addr->svm_port <= LAST_RESERVED_PORT &&
  434. !capable(CAP_NET_BIND_SERVICE)) {
  435. return -EACCES;
  436. }
  437. if (__vsock_find_bound_socket(&new_addr))
  438. return -EADDRINUSE;
  439. }
  440. vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);
  441. /* Remove stream sockets from the unbound list and add them to the hash
  442. * table for easy lookup by its address. The unbound list is simply an
  443. * extra entry at the end of the hash table, a trick used by AF_UNIX.
  444. */
  445. __vsock_remove_bound(vsk);
  446. __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
  447. return 0;
  448. }
  449. static int __vsock_bind_dgram(struct vsock_sock *vsk,
  450. struct sockaddr_vm *addr)
  451. {
  452. return transport->dgram_bind(vsk, addr);
  453. }
  454. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
  455. {
  456. struct vsock_sock *vsk = vsock_sk(sk);
  457. u32 cid;
  458. int retval;
  459. /* First ensure this socket isn't already bound. */
  460. if (vsock_addr_bound(&vsk->local_addr))
  461. return -EINVAL;
  462. /* Now bind to the provided address or select appropriate values if
  463. * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that
  464. * like AF_INET prevents binding to a non-local IP address (in most
  465. * cases), we only allow binding to the local CID.
  466. */
  467. cid = transport->get_local_cid();
  468. if (addr->svm_cid != cid && addr->svm_cid != VMADDR_CID_ANY)
  469. return -EADDRNOTAVAIL;
  470. switch (sk->sk_socket->type) {
  471. case SOCK_STREAM:
  472. spin_lock_bh(&vsock_table_lock);
  473. retval = __vsock_bind_stream(vsk, addr);
  474. spin_unlock_bh(&vsock_table_lock);
  475. break;
  476. case SOCK_DGRAM:
  477. retval = __vsock_bind_dgram(vsk, addr);
  478. break;
  479. default:
  480. retval = -EINVAL;
  481. break;
  482. }
  483. return retval;
  484. }
  485. static void vsock_connect_timeout(struct work_struct *work);
  486. struct sock *__vsock_create(struct net *net,
  487. struct socket *sock,
  488. struct sock *parent,
  489. gfp_t priority,
  490. unsigned short type,
  491. int kern)
  492. {
  493. struct sock *sk;
  494. struct vsock_sock *psk;
  495. struct vsock_sock *vsk;
  496. sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern);
  497. if (!sk)
  498. return NULL;
  499. sock_init_data(sock, sk);
  500. /* sk->sk_type is normally set in sock_init_data, but only if sock is
  501. * non-NULL. We make sure that our sockets always have a type by
  502. * setting it here if needed.
  503. */
  504. if (!sock)
  505. sk->sk_type = type;
  506. vsk = vsock_sk(sk);
  507. vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  508. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  509. sk->sk_destruct = vsock_sk_destruct;
  510. sk->sk_backlog_rcv = vsock_queue_rcv_skb;
  511. sock_reset_flag(sk, SOCK_DONE);
  512. INIT_LIST_HEAD(&vsk->bound_table);
  513. INIT_LIST_HEAD(&vsk->connected_table);
  514. vsk->listener = NULL;
  515. INIT_LIST_HEAD(&vsk->pending_links);
  516. INIT_LIST_HEAD(&vsk->accept_queue);
  517. vsk->rejected = false;
  518. vsk->sent_request = false;
  519. vsk->ignore_connecting_rst = false;
  520. vsk->peer_shutdown = 0;
  521. INIT_DELAYED_WORK(&vsk->connect_work, vsock_connect_timeout);
  522. INIT_DELAYED_WORK(&vsk->pending_work, vsock_pending_work);
  523. psk = parent ? vsock_sk(parent) : NULL;
  524. if (parent) {
  525. vsk->trusted = psk->trusted;
  526. vsk->owner = get_cred(psk->owner);
  527. vsk->connect_timeout = psk->connect_timeout;
  528. } else {
  529. vsk->trusted = capable(CAP_NET_ADMIN);
  530. vsk->owner = get_current_cred();
  531. vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT;
  532. }
  533. if (transport->init(vsk, psk) < 0) {
  534. sk_free(sk);
  535. return NULL;
  536. }
  537. if (sock)
  538. vsock_insert_unbound(vsk);
  539. return sk;
  540. }
  541. EXPORT_SYMBOL_GPL(__vsock_create);
  542. static void __vsock_release(struct sock *sk, int level)
  543. {
  544. if (sk) {
  545. struct sk_buff *skb;
  546. struct sock *pending;
  547. struct vsock_sock *vsk;
  548. vsk = vsock_sk(sk);
  549. pending = NULL; /* Compiler warning. */
  550. /* The release call is supposed to use lock_sock_nested()
  551. * rather than lock_sock(), if a sock lock should be acquired.
  552. */
  553. transport->release(vsk);
  554. /* When "level" is SINGLE_DEPTH_NESTING, use the nested
  555. * version to avoid the warning "possible recursive locking
  556. * detected". When "level" is 0, lock_sock_nested(sk, level)
  557. * is the same as lock_sock(sk).
  558. */
  559. lock_sock_nested(sk, level);
  560. sock_orphan(sk);
  561. sk->sk_shutdown = SHUTDOWN_MASK;
  562. while ((skb = skb_dequeue(&sk->sk_receive_queue)))
  563. kfree_skb(skb);
  564. /* Clean up any sockets that never were accepted. */
  565. while ((pending = vsock_dequeue_accept(sk)) != NULL) {
  566. __vsock_release(pending, SINGLE_DEPTH_NESTING);
  567. sock_put(pending);
  568. }
  569. release_sock(sk);
  570. sock_put(sk);
  571. }
  572. }
  573. static void vsock_sk_destruct(struct sock *sk)
  574. {
  575. struct vsock_sock *vsk = vsock_sk(sk);
  576. transport->destruct(vsk);
  577. /* When clearing these addresses, there's no need to set the family and
  578. * possibly register the address family with the kernel.
  579. */
  580. vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  581. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  582. put_cred(vsk->owner);
  583. }
  584. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  585. {
  586. int err;
  587. err = sock_queue_rcv_skb(sk, skb);
  588. if (err)
  589. kfree_skb(skb);
  590. return err;
  591. }
  592. s64 vsock_stream_has_data(struct vsock_sock *vsk)
  593. {
  594. return transport->stream_has_data(vsk);
  595. }
  596. EXPORT_SYMBOL_GPL(vsock_stream_has_data);
  597. s64 vsock_stream_has_space(struct vsock_sock *vsk)
  598. {
  599. return transport->stream_has_space(vsk);
  600. }
  601. EXPORT_SYMBOL_GPL(vsock_stream_has_space);
  602. static int vsock_release(struct socket *sock)
  603. {
  604. __vsock_release(sock->sk, 0);
  605. sock->sk = NULL;
  606. sock->state = SS_FREE;
  607. return 0;
  608. }
  609. static int
  610. vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  611. {
  612. int err;
  613. struct sock *sk;
  614. struct sockaddr_vm *vm_addr;
  615. sk = sock->sk;
  616. if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0)
  617. return -EINVAL;
  618. lock_sock(sk);
  619. err = __vsock_bind(sk, vm_addr);
  620. release_sock(sk);
  621. return err;
  622. }
  623. static int vsock_getname(struct socket *sock,
  624. struct sockaddr *addr, int peer)
  625. {
  626. int err;
  627. struct sock *sk;
  628. struct vsock_sock *vsk;
  629. struct sockaddr_vm *vm_addr;
  630. sk = sock->sk;
  631. vsk = vsock_sk(sk);
  632. err = 0;
  633. lock_sock(sk);
  634. if (peer) {
  635. if (sock->state != SS_CONNECTED) {
  636. err = -ENOTCONN;
  637. goto out;
  638. }
  639. vm_addr = &vsk->remote_addr;
  640. } else {
  641. vm_addr = &vsk->local_addr;
  642. }
  643. if (!vm_addr) {
  644. err = -EINVAL;
  645. goto out;
  646. }
  647. /* sys_getsockname() and sys_getpeername() pass us a
  648. * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately
  649. * that macro is defined in socket.c instead of .h, so we hardcode its
  650. * value here.
  651. */
  652. BUILD_BUG_ON(sizeof(*vm_addr) > 128);
  653. memcpy(addr, vm_addr, sizeof(*vm_addr));
  654. err = sizeof(*vm_addr);
  655. out:
  656. release_sock(sk);
  657. return err;
  658. }
  659. static int vsock_shutdown(struct socket *sock, int mode)
  660. {
  661. int err;
  662. struct sock *sk;
  663. /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
  664. * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
  665. * here like the other address families do. Note also that the
  666. * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
  667. * which is what we want.
  668. */
  669. mode++;
  670. if ((mode & ~SHUTDOWN_MASK) || !mode)
  671. return -EINVAL;
  672. /* If this is a STREAM socket and it is not connected then bail out
  673. * immediately. If it is a DGRAM socket then we must first kick the
  674. * socket so that it wakes up from any sleeping calls, for example
  675. * recv(), and then afterwards return the error.
  676. */
  677. sk = sock->sk;
  678. if (sock->state == SS_UNCONNECTED) {
  679. err = -ENOTCONN;
  680. if (sk->sk_type == SOCK_STREAM)
  681. return err;
  682. } else {
  683. sock->state = SS_DISCONNECTING;
  684. err = 0;
  685. }
  686. /* Receive and send shutdowns are treated alike. */
  687. mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
  688. if (mode) {
  689. lock_sock(sk);
  690. sk->sk_shutdown |= mode;
  691. sk->sk_state_change(sk);
  692. release_sock(sk);
  693. if (sk->sk_type == SOCK_STREAM) {
  694. sock_reset_flag(sk, SOCK_DONE);
  695. vsock_send_shutdown(sk, mode);
  696. }
  697. }
  698. return err;
  699. }
  700. static __poll_t vsock_poll(struct file *file, struct socket *sock,
  701. poll_table *wait)
  702. {
  703. struct sock *sk;
  704. __poll_t mask;
  705. struct vsock_sock *vsk;
  706. sk = sock->sk;
  707. vsk = vsock_sk(sk);
  708. poll_wait(file, sk_sleep(sk), wait);
  709. mask = 0;
  710. if (sk->sk_err)
  711. /* Signify that there has been an error on this socket. */
  712. mask |= EPOLLERR;
  713. /* INET sockets treat local write shutdown and peer write shutdown as a
  714. * case of EPOLLHUP set.
  715. */
  716. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  717. ((sk->sk_shutdown & SEND_SHUTDOWN) &&
  718. (vsk->peer_shutdown & SEND_SHUTDOWN))) {
  719. mask |= EPOLLHUP;
  720. }
  721. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  722. vsk->peer_shutdown & SEND_SHUTDOWN) {
  723. mask |= EPOLLRDHUP;
  724. }
  725. if (sock->type == SOCK_DGRAM) {
  726. /* For datagram sockets we can read if there is something in
  727. * the queue and write as long as the socket isn't shutdown for
  728. * sending.
  729. */
  730. if (!skb_queue_empty_lockless(&sk->sk_receive_queue) ||
  731. (sk->sk_shutdown & RCV_SHUTDOWN)) {
  732. mask |= EPOLLIN | EPOLLRDNORM;
  733. }
  734. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  735. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  736. } else if (sock->type == SOCK_STREAM) {
  737. lock_sock(sk);
  738. /* Listening sockets that have connections in their accept
  739. * queue can be read.
  740. */
  741. if (sk->sk_state == TCP_LISTEN
  742. && !vsock_is_accept_queue_empty(sk))
  743. mask |= EPOLLIN | EPOLLRDNORM;
  744. /* If there is something in the queue then we can read. */
  745. if (transport->stream_is_active(vsk) &&
  746. !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  747. bool data_ready_now = false;
  748. int ret = transport->notify_poll_in(
  749. vsk, 1, &data_ready_now);
  750. if (ret < 0) {
  751. mask |= EPOLLERR;
  752. } else {
  753. if (data_ready_now)
  754. mask |= EPOLLIN | EPOLLRDNORM;
  755. }
  756. }
  757. /* Sockets whose connections have been closed, reset, or
  758. * terminated should also be considered read, and we check the
  759. * shutdown flag for that.
  760. */
  761. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  762. vsk->peer_shutdown & SEND_SHUTDOWN) {
  763. mask |= EPOLLIN | EPOLLRDNORM;
  764. }
  765. /* Connected sockets that can produce data can be written. */
  766. if (sk->sk_state == TCP_ESTABLISHED) {
  767. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  768. bool space_avail_now = false;
  769. int ret = transport->notify_poll_out(
  770. vsk, 1, &space_avail_now);
  771. if (ret < 0) {
  772. mask |= EPOLLERR;
  773. } else {
  774. if (space_avail_now)
  775. /* Remove EPOLLWRBAND since INET
  776. * sockets are not setting it.
  777. */
  778. mask |= EPOLLOUT | EPOLLWRNORM;
  779. }
  780. }
  781. }
  782. /* Simulate INET socket poll behaviors, which sets
  783. * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read,
  784. * but local send is not shutdown.
  785. */
  786. if (sk->sk_state == TCP_CLOSE || sk->sk_state == TCP_CLOSING) {
  787. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  788. mask |= EPOLLOUT | EPOLLWRNORM;
  789. }
  790. release_sock(sk);
  791. }
  792. return mask;
  793. }
  794. static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  795. size_t len)
  796. {
  797. int err;
  798. struct sock *sk;
  799. struct vsock_sock *vsk;
  800. struct sockaddr_vm *remote_addr;
  801. if (msg->msg_flags & MSG_OOB)
  802. return -EOPNOTSUPP;
  803. /* For now, MSG_DONTWAIT is always assumed... */
  804. err = 0;
  805. sk = sock->sk;
  806. vsk = vsock_sk(sk);
  807. lock_sock(sk);
  808. err = vsock_auto_bind(vsk);
  809. if (err)
  810. goto out;
  811. /* If the provided message contains an address, use that. Otherwise
  812. * fall back on the socket's remote handle (if it has been connected).
  813. */
  814. if (msg->msg_name &&
  815. vsock_addr_cast(msg->msg_name, msg->msg_namelen,
  816. &remote_addr) == 0) {
  817. /* Ensure this address is of the right type and is a valid
  818. * destination.
  819. */
  820. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  821. remote_addr->svm_cid = transport->get_local_cid();
  822. if (!vsock_addr_bound(remote_addr)) {
  823. err = -EINVAL;
  824. goto out;
  825. }
  826. } else if (sock->state == SS_CONNECTED) {
  827. remote_addr = &vsk->remote_addr;
  828. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  829. remote_addr->svm_cid = transport->get_local_cid();
  830. /* XXX Should connect() or this function ensure remote_addr is
  831. * bound?
  832. */
  833. if (!vsock_addr_bound(&vsk->remote_addr)) {
  834. err = -EINVAL;
  835. goto out;
  836. }
  837. } else {
  838. err = -EINVAL;
  839. goto out;
  840. }
  841. if (!transport->dgram_allow(remote_addr->svm_cid,
  842. remote_addr->svm_port)) {
  843. err = -EINVAL;
  844. goto out;
  845. }
  846. err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
  847. out:
  848. release_sock(sk);
  849. return err;
  850. }
  851. static int vsock_dgram_connect(struct socket *sock,
  852. struct sockaddr *addr, int addr_len, int flags)
  853. {
  854. int err;
  855. struct sock *sk;
  856. struct vsock_sock *vsk;
  857. struct sockaddr_vm *remote_addr;
  858. sk = sock->sk;
  859. vsk = vsock_sk(sk);
  860. err = vsock_addr_cast(addr, addr_len, &remote_addr);
  861. if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) {
  862. lock_sock(sk);
  863. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY,
  864. VMADDR_PORT_ANY);
  865. sock->state = SS_UNCONNECTED;
  866. release_sock(sk);
  867. return 0;
  868. } else if (err != 0)
  869. return -EINVAL;
  870. lock_sock(sk);
  871. err = vsock_auto_bind(vsk);
  872. if (err)
  873. goto out;
  874. if (!transport->dgram_allow(remote_addr->svm_cid,
  875. remote_addr->svm_port)) {
  876. err = -EINVAL;
  877. goto out;
  878. }
  879. memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr));
  880. sock->state = SS_CONNECTED;
  881. out:
  882. release_sock(sk);
  883. return err;
  884. }
  885. static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  886. size_t len, int flags)
  887. {
  888. return transport->dgram_dequeue(vsock_sk(sock->sk), msg, len, flags);
  889. }
  890. static const struct proto_ops vsock_dgram_ops = {
  891. .family = PF_VSOCK,
  892. .owner = THIS_MODULE,
  893. .release = vsock_release,
  894. .bind = vsock_bind,
  895. .connect = vsock_dgram_connect,
  896. .socketpair = sock_no_socketpair,
  897. .accept = sock_no_accept,
  898. .getname = vsock_getname,
  899. .poll = vsock_poll,
  900. .ioctl = sock_no_ioctl,
  901. .listen = sock_no_listen,
  902. .shutdown = vsock_shutdown,
  903. .setsockopt = sock_no_setsockopt,
  904. .getsockopt = sock_no_getsockopt,
  905. .sendmsg = vsock_dgram_sendmsg,
  906. .recvmsg = vsock_dgram_recvmsg,
  907. .mmap = sock_no_mmap,
  908. .sendpage = sock_no_sendpage,
  909. };
  910. static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
  911. {
  912. if (!transport->cancel_pkt)
  913. return -EOPNOTSUPP;
  914. return transport->cancel_pkt(vsk);
  915. }
  916. static void vsock_connect_timeout(struct work_struct *work)
  917. {
  918. struct sock *sk;
  919. struct vsock_sock *vsk;
  920. int cancel = 0;
  921. vsk = container_of(work, struct vsock_sock, connect_work.work);
  922. sk = sk_vsock(vsk);
  923. lock_sock(sk);
  924. if (sk->sk_state == TCP_SYN_SENT &&
  925. (sk->sk_shutdown != SHUTDOWN_MASK)) {
  926. sk->sk_state = TCP_CLOSE;
  927. sk->sk_err = ETIMEDOUT;
  928. sk->sk_error_report(sk);
  929. cancel = 1;
  930. }
  931. release_sock(sk);
  932. if (cancel)
  933. vsock_transport_cancel_pkt(vsk);
  934. sock_put(sk);
  935. }
  936. static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
  937. int addr_len, int flags)
  938. {
  939. int err;
  940. struct sock *sk;
  941. struct vsock_sock *vsk;
  942. struct sockaddr_vm *remote_addr;
  943. long timeout;
  944. DEFINE_WAIT(wait);
  945. err = 0;
  946. sk = sock->sk;
  947. vsk = vsock_sk(sk);
  948. lock_sock(sk);
  949. /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
  950. switch (sock->state) {
  951. case SS_CONNECTED:
  952. err = -EISCONN;
  953. goto out;
  954. case SS_DISCONNECTING:
  955. err = -EINVAL;
  956. goto out;
  957. case SS_CONNECTING:
  958. /* This continues on so we can move sock into the SS_CONNECTED
  959. * state once the connection has completed (at which point err
  960. * will be set to zero also). Otherwise, we will either wait
  961. * for the connection or return -EALREADY should this be a
  962. * non-blocking call.
  963. */
  964. err = -EALREADY;
  965. break;
  966. default:
  967. if ((sk->sk_state == TCP_LISTEN) ||
  968. vsock_addr_cast(addr, addr_len, &remote_addr) != 0) {
  969. err = -EINVAL;
  970. goto out;
  971. }
  972. /* The hypervisor and well-known contexts do not have socket
  973. * endpoints.
  974. */
  975. if (!transport->stream_allow(remote_addr->svm_cid,
  976. remote_addr->svm_port)) {
  977. err = -ENETUNREACH;
  978. goto out;
  979. }
  980. /* Set the remote address that we are connecting to. */
  981. memcpy(&vsk->remote_addr, remote_addr,
  982. sizeof(vsk->remote_addr));
  983. err = vsock_auto_bind(vsk);
  984. if (err)
  985. goto out;
  986. sk->sk_state = TCP_SYN_SENT;
  987. err = transport->connect(vsk);
  988. if (err < 0)
  989. goto out;
  990. /* Mark sock as connecting and set the error code to in
  991. * progress in case this is a non-blocking connect.
  992. */
  993. sock->state = SS_CONNECTING;
  994. err = -EINPROGRESS;
  995. }
  996. /* The receive path will handle all communication until we are able to
  997. * enter the connected state. Here we wait for the connection to be
  998. * completed or a notification of an error.
  999. */
  1000. timeout = vsk->connect_timeout;
  1001. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1002. while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) {
  1003. if (flags & O_NONBLOCK) {
  1004. /* If we're not going to block, we schedule a timeout
  1005. * function to generate a timeout on the connection
  1006. * attempt, in case the peer doesn't respond in a
  1007. * timely manner. We hold on to the socket until the
  1008. * timeout fires.
  1009. */
  1010. sock_hold(sk);
  1011. schedule_delayed_work(&vsk->connect_work, timeout);
  1012. /* Skip ahead to preserve error code set above. */
  1013. goto out_wait;
  1014. }
  1015. release_sock(sk);
  1016. timeout = schedule_timeout(timeout);
  1017. lock_sock(sk);
  1018. if (signal_pending(current)) {
  1019. err = sock_intr_errno(timeout);
  1020. sk->sk_state = TCP_CLOSE;
  1021. sock->state = SS_UNCONNECTED;
  1022. vsock_transport_cancel_pkt(vsk);
  1023. goto out_wait;
  1024. } else if (timeout == 0) {
  1025. err = -ETIMEDOUT;
  1026. sk->sk_state = TCP_CLOSE;
  1027. sock->state = SS_UNCONNECTED;
  1028. vsock_transport_cancel_pkt(vsk);
  1029. goto out_wait;
  1030. }
  1031. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1032. }
  1033. if (sk->sk_err) {
  1034. err = -sk->sk_err;
  1035. sk->sk_state = TCP_CLOSE;
  1036. sock->state = SS_UNCONNECTED;
  1037. } else {
  1038. err = 0;
  1039. }
  1040. out_wait:
  1041. finish_wait(sk_sleep(sk), &wait);
  1042. out:
  1043. release_sock(sk);
  1044. return err;
  1045. }
  1046. static int vsock_accept(struct socket *sock, struct socket *newsock, int flags,
  1047. bool kern)
  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 != TCP_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 = TCP_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 == TCP_ESTABLISHED ? -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 != TCP_ESTABLISHED ||
  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 != TCP_ESTABLISHED) {
  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_device.minor = MISC_DYNAMIC_MINOR;
  1618. err = misc_register(&vsock_device);
  1619. if (err) {
  1620. pr_err("Failed to register misc device\n");
  1621. goto err_reset_transport;
  1622. }
  1623. err = proto_register(&vsock_proto, 1); /* we want our slab */
  1624. if (err) {
  1625. pr_err("Cannot register vsock protocol\n");
  1626. goto err_deregister_misc;
  1627. }
  1628. err = sock_register(&vsock_family_ops);
  1629. if (err) {
  1630. pr_err("could not register af_vsock (%d) address family: %d\n",
  1631. AF_VSOCK, err);
  1632. goto err_unregister_proto;
  1633. }
  1634. mutex_unlock(&vsock_register_mutex);
  1635. return 0;
  1636. err_unregister_proto:
  1637. proto_unregister(&vsock_proto);
  1638. err_deregister_misc:
  1639. misc_deregister(&vsock_device);
  1640. err_reset_transport:
  1641. transport = NULL;
  1642. err_busy:
  1643. mutex_unlock(&vsock_register_mutex);
  1644. return err;
  1645. }
  1646. EXPORT_SYMBOL_GPL(__vsock_core_init);
  1647. void vsock_core_exit(void)
  1648. {
  1649. mutex_lock(&vsock_register_mutex);
  1650. misc_deregister(&vsock_device);
  1651. sock_unregister(AF_VSOCK);
  1652. proto_unregister(&vsock_proto);
  1653. /* We do not want the assignment below re-ordered. */
  1654. mb();
  1655. transport = NULL;
  1656. mutex_unlock(&vsock_register_mutex);
  1657. }
  1658. EXPORT_SYMBOL_GPL(vsock_core_exit);
  1659. const struct vsock_transport *vsock_core_get_transport(void)
  1660. {
  1661. /* vsock_register_mutex not taken since only the transport uses this
  1662. * function and only while registered.
  1663. */
  1664. return transport;
  1665. }
  1666. EXPORT_SYMBOL_GPL(vsock_core_get_transport);
  1667. static void __exit vsock_exit(void)
  1668. {
  1669. /* Do nothing. This function makes this module removable. */
  1670. }
  1671. module_init(vsock_init_tables);
  1672. module_exit(vsock_exit);
  1673. MODULE_AUTHOR("VMware, Inc.");
  1674. MODULE_DESCRIPTION("VMware Virtual Socket Family");
  1675. MODULE_VERSION("1.0.2.0-k");
  1676. MODULE_LICENSE("GPL v2");