af_vsock.c 47 KB

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