af_vsock.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  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 kiocb *kiocb, struct socket *sock,
  781. struct msghdr *msg, 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->msg_iov, 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 kiocb *kiocb, struct socket *sock,
  872. struct msghdr *msg, size_t len, int flags)
  873. {
  874. return transport->dgram_dequeue(kiocb, vsock_sk(sock->sk), msg, len,
  875. flags);
  876. }
  877. static const struct proto_ops vsock_dgram_ops = {
  878. .family = PF_VSOCK,
  879. .owner = THIS_MODULE,
  880. .release = vsock_release,
  881. .bind = vsock_bind,
  882. .connect = vsock_dgram_connect,
  883. .socketpair = sock_no_socketpair,
  884. .accept = sock_no_accept,
  885. .getname = vsock_getname,
  886. .poll = vsock_poll,
  887. .ioctl = sock_no_ioctl,
  888. .listen = sock_no_listen,
  889. .shutdown = vsock_shutdown,
  890. .setsockopt = sock_no_setsockopt,
  891. .getsockopt = sock_no_getsockopt,
  892. .sendmsg = vsock_dgram_sendmsg,
  893. .recvmsg = vsock_dgram_recvmsg,
  894. .mmap = sock_no_mmap,
  895. .sendpage = sock_no_sendpage,
  896. };
  897. static void vsock_connect_timeout(struct work_struct *work)
  898. {
  899. struct sock *sk;
  900. struct vsock_sock *vsk;
  901. vsk = container_of(work, struct vsock_sock, dwork.work);
  902. sk = sk_vsock(vsk);
  903. lock_sock(sk);
  904. if (sk->sk_state == SS_CONNECTING &&
  905. (sk->sk_shutdown != SHUTDOWN_MASK)) {
  906. sk->sk_state = SS_UNCONNECTED;
  907. sk->sk_err = ETIMEDOUT;
  908. sk->sk_error_report(sk);
  909. }
  910. release_sock(sk);
  911. sock_put(sk);
  912. }
  913. static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
  914. int addr_len, int flags)
  915. {
  916. int err;
  917. struct sock *sk;
  918. struct vsock_sock *vsk;
  919. struct sockaddr_vm *remote_addr;
  920. long timeout;
  921. DEFINE_WAIT(wait);
  922. err = 0;
  923. sk = sock->sk;
  924. vsk = vsock_sk(sk);
  925. lock_sock(sk);
  926. /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
  927. switch (sock->state) {
  928. case SS_CONNECTED:
  929. err = -EISCONN;
  930. goto out;
  931. case SS_DISCONNECTING:
  932. err = -EINVAL;
  933. goto out;
  934. case SS_CONNECTING:
  935. /* This continues on so we can move sock into the SS_CONNECTED
  936. * state once the connection has completed (at which point err
  937. * will be set to zero also). Otherwise, we will either wait
  938. * for the connection or return -EALREADY should this be a
  939. * non-blocking call.
  940. */
  941. err = -EALREADY;
  942. break;
  943. default:
  944. if ((sk->sk_state == SS_LISTEN) ||
  945. vsock_addr_cast(addr, addr_len, &remote_addr) != 0) {
  946. err = -EINVAL;
  947. goto out;
  948. }
  949. /* The hypervisor and well-known contexts do not have socket
  950. * endpoints.
  951. */
  952. if (!transport->stream_allow(remote_addr->svm_cid,
  953. remote_addr->svm_port)) {
  954. err = -ENETUNREACH;
  955. goto out;
  956. }
  957. /* Set the remote address that we are connecting to. */
  958. memcpy(&vsk->remote_addr, remote_addr,
  959. sizeof(vsk->remote_addr));
  960. err = vsock_auto_bind(vsk);
  961. if (err)
  962. goto out;
  963. sk->sk_state = SS_CONNECTING;
  964. err = transport->connect(vsk);
  965. if (err < 0)
  966. goto out;
  967. /* Mark sock as connecting and set the error code to in
  968. * progress in case this is a non-blocking connect.
  969. */
  970. sock->state = SS_CONNECTING;
  971. err = -EINPROGRESS;
  972. }
  973. /* The receive path will handle all communication until we are able to
  974. * enter the connected state. Here we wait for the connection to be
  975. * completed or a notification of an error.
  976. */
  977. timeout = vsk->connect_timeout;
  978. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  979. while (sk->sk_state != SS_CONNECTED && sk->sk_err == 0) {
  980. if (flags & O_NONBLOCK) {
  981. /* If we're not going to block, we schedule a timeout
  982. * function to generate a timeout on the connection
  983. * attempt, in case the peer doesn't respond in a
  984. * timely manner. We hold on to the socket until the
  985. * timeout fires.
  986. */
  987. sock_hold(sk);
  988. INIT_DELAYED_WORK(&vsk->dwork,
  989. vsock_connect_timeout);
  990. schedule_delayed_work(&vsk->dwork, timeout);
  991. /* Skip ahead to preserve error code set above. */
  992. goto out_wait;
  993. }
  994. release_sock(sk);
  995. timeout = schedule_timeout(timeout);
  996. lock_sock(sk);
  997. if (signal_pending(current)) {
  998. err = sock_intr_errno(timeout);
  999. goto out_wait_error;
  1000. } else if (timeout == 0) {
  1001. err = -ETIMEDOUT;
  1002. goto out_wait_error;
  1003. }
  1004. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1005. }
  1006. if (sk->sk_err) {
  1007. err = -sk->sk_err;
  1008. goto out_wait_error;
  1009. } else
  1010. err = 0;
  1011. out_wait:
  1012. finish_wait(sk_sleep(sk), &wait);
  1013. out:
  1014. release_sock(sk);
  1015. return err;
  1016. out_wait_error:
  1017. sk->sk_state = SS_UNCONNECTED;
  1018. sock->state = SS_UNCONNECTED;
  1019. goto out_wait;
  1020. }
  1021. static int vsock_accept(struct socket *sock, struct socket *newsock, int flags)
  1022. {
  1023. struct sock *listener;
  1024. int err;
  1025. struct sock *connected;
  1026. struct vsock_sock *vconnected;
  1027. long timeout;
  1028. DEFINE_WAIT(wait);
  1029. err = 0;
  1030. listener = sock->sk;
  1031. lock_sock(listener);
  1032. if (sock->type != SOCK_STREAM) {
  1033. err = -EOPNOTSUPP;
  1034. goto out;
  1035. }
  1036. if (listener->sk_state != SS_LISTEN) {
  1037. err = -EINVAL;
  1038. goto out;
  1039. }
  1040. /* Wait for children sockets to appear; these are the new sockets
  1041. * created upon connection establishment.
  1042. */
  1043. timeout = sock_sndtimeo(listener, flags & O_NONBLOCK);
  1044. prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
  1045. while ((connected = vsock_dequeue_accept(listener)) == NULL &&
  1046. listener->sk_err == 0) {
  1047. release_sock(listener);
  1048. timeout = schedule_timeout(timeout);
  1049. lock_sock(listener);
  1050. if (signal_pending(current)) {
  1051. err = sock_intr_errno(timeout);
  1052. goto out_wait;
  1053. } else if (timeout == 0) {
  1054. err = -EAGAIN;
  1055. goto out_wait;
  1056. }
  1057. prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
  1058. }
  1059. if (listener->sk_err)
  1060. err = -listener->sk_err;
  1061. if (connected) {
  1062. listener->sk_ack_backlog--;
  1063. lock_sock(connected);
  1064. vconnected = vsock_sk(connected);
  1065. /* If the listener socket has received an error, then we should
  1066. * reject this socket and return. Note that we simply mark the
  1067. * socket rejected, drop our reference, and let the cleanup
  1068. * function handle the cleanup; the fact that we found it in
  1069. * the listener's accept queue guarantees that the cleanup
  1070. * function hasn't run yet.
  1071. */
  1072. if (err) {
  1073. vconnected->rejected = true;
  1074. release_sock(connected);
  1075. sock_put(connected);
  1076. goto out_wait;
  1077. }
  1078. newsock->state = SS_CONNECTED;
  1079. sock_graft(connected, newsock);
  1080. release_sock(connected);
  1081. sock_put(connected);
  1082. }
  1083. out_wait:
  1084. finish_wait(sk_sleep(listener), &wait);
  1085. out:
  1086. release_sock(listener);
  1087. return err;
  1088. }
  1089. static int vsock_listen(struct socket *sock, int backlog)
  1090. {
  1091. int err;
  1092. struct sock *sk;
  1093. struct vsock_sock *vsk;
  1094. sk = sock->sk;
  1095. lock_sock(sk);
  1096. if (sock->type != SOCK_STREAM) {
  1097. err = -EOPNOTSUPP;
  1098. goto out;
  1099. }
  1100. if (sock->state != SS_UNCONNECTED) {
  1101. err = -EINVAL;
  1102. goto out;
  1103. }
  1104. vsk = vsock_sk(sk);
  1105. if (!vsock_addr_bound(&vsk->local_addr)) {
  1106. err = -EINVAL;
  1107. goto out;
  1108. }
  1109. sk->sk_max_ack_backlog = backlog;
  1110. sk->sk_state = SS_LISTEN;
  1111. err = 0;
  1112. out:
  1113. release_sock(sk);
  1114. return err;
  1115. }
  1116. static int vsock_stream_setsockopt(struct socket *sock,
  1117. int level,
  1118. int optname,
  1119. char __user *optval,
  1120. unsigned int optlen)
  1121. {
  1122. int err;
  1123. struct sock *sk;
  1124. struct vsock_sock *vsk;
  1125. u64 val;
  1126. if (level != AF_VSOCK)
  1127. return -ENOPROTOOPT;
  1128. #define COPY_IN(_v) \
  1129. do { \
  1130. if (optlen < sizeof(_v)) { \
  1131. err = -EINVAL; \
  1132. goto exit; \
  1133. } \
  1134. if (copy_from_user(&_v, optval, sizeof(_v)) != 0) { \
  1135. err = -EFAULT; \
  1136. goto exit; \
  1137. } \
  1138. } while (0)
  1139. err = 0;
  1140. sk = sock->sk;
  1141. vsk = vsock_sk(sk);
  1142. lock_sock(sk);
  1143. switch (optname) {
  1144. case SO_VM_SOCKETS_BUFFER_SIZE:
  1145. COPY_IN(val);
  1146. transport->set_buffer_size(vsk, val);
  1147. break;
  1148. case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
  1149. COPY_IN(val);
  1150. transport->set_max_buffer_size(vsk, val);
  1151. break;
  1152. case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
  1153. COPY_IN(val);
  1154. transport->set_min_buffer_size(vsk, val);
  1155. break;
  1156. case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
  1157. struct timeval tv;
  1158. COPY_IN(tv);
  1159. if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC &&
  1160. tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) {
  1161. vsk->connect_timeout = tv.tv_sec * HZ +
  1162. DIV_ROUND_UP(tv.tv_usec, (1000000 / HZ));
  1163. if (vsk->connect_timeout == 0)
  1164. vsk->connect_timeout =
  1165. VSOCK_DEFAULT_CONNECT_TIMEOUT;
  1166. } else {
  1167. err = -ERANGE;
  1168. }
  1169. break;
  1170. }
  1171. default:
  1172. err = -ENOPROTOOPT;
  1173. break;
  1174. }
  1175. #undef COPY_IN
  1176. exit:
  1177. release_sock(sk);
  1178. return err;
  1179. }
  1180. static int vsock_stream_getsockopt(struct socket *sock,
  1181. int level, int optname,
  1182. char __user *optval,
  1183. int __user *optlen)
  1184. {
  1185. int err;
  1186. int len;
  1187. struct sock *sk;
  1188. struct vsock_sock *vsk;
  1189. u64 val;
  1190. if (level != AF_VSOCK)
  1191. return -ENOPROTOOPT;
  1192. err = get_user(len, optlen);
  1193. if (err != 0)
  1194. return err;
  1195. #define COPY_OUT(_v) \
  1196. do { \
  1197. if (len < sizeof(_v)) \
  1198. return -EINVAL; \
  1199. \
  1200. len = sizeof(_v); \
  1201. if (copy_to_user(optval, &_v, len) != 0) \
  1202. return -EFAULT; \
  1203. \
  1204. } while (0)
  1205. err = 0;
  1206. sk = sock->sk;
  1207. vsk = vsock_sk(sk);
  1208. switch (optname) {
  1209. case SO_VM_SOCKETS_BUFFER_SIZE:
  1210. val = transport->get_buffer_size(vsk);
  1211. COPY_OUT(val);
  1212. break;
  1213. case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
  1214. val = transport->get_max_buffer_size(vsk);
  1215. COPY_OUT(val);
  1216. break;
  1217. case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
  1218. val = transport->get_min_buffer_size(vsk);
  1219. COPY_OUT(val);
  1220. break;
  1221. case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
  1222. struct timeval tv;
  1223. tv.tv_sec = vsk->connect_timeout / HZ;
  1224. tv.tv_usec =
  1225. (vsk->connect_timeout -
  1226. tv.tv_sec * HZ) * (1000000 / HZ);
  1227. COPY_OUT(tv);
  1228. break;
  1229. }
  1230. default:
  1231. return -ENOPROTOOPT;
  1232. }
  1233. err = put_user(len, optlen);
  1234. if (err != 0)
  1235. return -EFAULT;
  1236. #undef COPY_OUT
  1237. return 0;
  1238. }
  1239. static int vsock_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1240. struct msghdr *msg, size_t len)
  1241. {
  1242. struct sock *sk;
  1243. struct vsock_sock *vsk;
  1244. ssize_t total_written;
  1245. long timeout;
  1246. int err;
  1247. struct vsock_transport_send_notify_data send_data;
  1248. DEFINE_WAIT(wait);
  1249. sk = sock->sk;
  1250. vsk = vsock_sk(sk);
  1251. total_written = 0;
  1252. err = 0;
  1253. if (msg->msg_flags & MSG_OOB)
  1254. return -EOPNOTSUPP;
  1255. lock_sock(sk);
  1256. /* Callers should not provide a destination with stream sockets. */
  1257. if (msg->msg_namelen) {
  1258. err = sk->sk_state == SS_CONNECTED ? -EISCONN : -EOPNOTSUPP;
  1259. goto out;
  1260. }
  1261. /* Send data only if both sides are not shutdown in the direction. */
  1262. if (sk->sk_shutdown & SEND_SHUTDOWN ||
  1263. vsk->peer_shutdown & RCV_SHUTDOWN) {
  1264. err = -EPIPE;
  1265. goto out;
  1266. }
  1267. if (sk->sk_state != SS_CONNECTED ||
  1268. !vsock_addr_bound(&vsk->local_addr)) {
  1269. err = -ENOTCONN;
  1270. goto out;
  1271. }
  1272. if (!vsock_addr_bound(&vsk->remote_addr)) {
  1273. err = -EDESTADDRREQ;
  1274. goto out;
  1275. }
  1276. /* Wait for room in the produce queue to enqueue our user's data. */
  1277. timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1278. err = transport->notify_send_init(vsk, &send_data);
  1279. if (err < 0)
  1280. goto out;
  1281. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1282. while (total_written < len) {
  1283. ssize_t written;
  1284. while (vsock_stream_has_space(vsk) == 0 &&
  1285. sk->sk_err == 0 &&
  1286. !(sk->sk_shutdown & SEND_SHUTDOWN) &&
  1287. !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
  1288. /* Don't wait for non-blocking sockets. */
  1289. if (timeout == 0) {
  1290. err = -EAGAIN;
  1291. goto out_wait;
  1292. }
  1293. err = transport->notify_send_pre_block(vsk, &send_data);
  1294. if (err < 0)
  1295. goto out_wait;
  1296. release_sock(sk);
  1297. timeout = schedule_timeout(timeout);
  1298. lock_sock(sk);
  1299. if (signal_pending(current)) {
  1300. err = sock_intr_errno(timeout);
  1301. goto out_wait;
  1302. } else if (timeout == 0) {
  1303. err = -EAGAIN;
  1304. goto out_wait;
  1305. }
  1306. prepare_to_wait(sk_sleep(sk), &wait,
  1307. TASK_INTERRUPTIBLE);
  1308. }
  1309. /* These checks occur both as part of and after the loop
  1310. * conditional since we need to check before and after
  1311. * sleeping.
  1312. */
  1313. if (sk->sk_err) {
  1314. err = -sk->sk_err;
  1315. goto out_wait;
  1316. } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
  1317. (vsk->peer_shutdown & RCV_SHUTDOWN)) {
  1318. err = -EPIPE;
  1319. goto out_wait;
  1320. }
  1321. err = transport->notify_send_pre_enqueue(vsk, &send_data);
  1322. if (err < 0)
  1323. goto out_wait;
  1324. /* Note that enqueue will only write as many bytes as are free
  1325. * in the produce queue, so we don't need to ensure len is
  1326. * smaller than the queue size. It is the caller's
  1327. * responsibility to check how many bytes we were able to send.
  1328. */
  1329. written = transport->stream_enqueue(
  1330. vsk, msg->msg_iov,
  1331. len - total_written);
  1332. if (written < 0) {
  1333. err = -ENOMEM;
  1334. goto out_wait;
  1335. }
  1336. total_written += written;
  1337. err = transport->notify_send_post_enqueue(
  1338. vsk, written, &send_data);
  1339. if (err < 0)
  1340. goto out_wait;
  1341. }
  1342. out_wait:
  1343. if (total_written > 0)
  1344. err = total_written;
  1345. finish_wait(sk_sleep(sk), &wait);
  1346. out:
  1347. release_sock(sk);
  1348. return err;
  1349. }
  1350. static int
  1351. vsock_stream_recvmsg(struct kiocb *kiocb,
  1352. struct socket *sock,
  1353. struct msghdr *msg, size_t len, int flags)
  1354. {
  1355. struct sock *sk;
  1356. struct vsock_sock *vsk;
  1357. int err;
  1358. size_t target;
  1359. ssize_t copied;
  1360. long timeout;
  1361. struct vsock_transport_recv_notify_data recv_data;
  1362. DEFINE_WAIT(wait);
  1363. sk = sock->sk;
  1364. vsk = vsock_sk(sk);
  1365. err = 0;
  1366. msg->msg_namelen = 0;
  1367. lock_sock(sk);
  1368. if (sk->sk_state != SS_CONNECTED) {
  1369. /* Recvmsg is supposed to return 0 if a peer performs an
  1370. * orderly shutdown. Differentiate between that case and when a
  1371. * peer has not connected or a local shutdown occured with the
  1372. * SOCK_DONE flag.
  1373. */
  1374. if (sock_flag(sk, SOCK_DONE))
  1375. err = 0;
  1376. else
  1377. err = -ENOTCONN;
  1378. goto out;
  1379. }
  1380. if (flags & MSG_OOB) {
  1381. err = -EOPNOTSUPP;
  1382. goto out;
  1383. }
  1384. /* We don't check peer_shutdown flag here since peer may actually shut
  1385. * down, but there can be data in the queue that a local socket can
  1386. * receive.
  1387. */
  1388. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1389. err = 0;
  1390. goto out;
  1391. }
  1392. /* It is valid on Linux to pass in a zero-length receive buffer. This
  1393. * is not an error. We may as well bail out now.
  1394. */
  1395. if (!len) {
  1396. err = 0;
  1397. goto out;
  1398. }
  1399. /* We must not copy less than target bytes into the user's buffer
  1400. * before returning successfully, so we wait for the consume queue to
  1401. * have that much data to consume before dequeueing. Note that this
  1402. * makes it impossible to handle cases where target is greater than the
  1403. * queue size.
  1404. */
  1405. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1406. if (target >= transport->stream_rcvhiwat(vsk)) {
  1407. err = -ENOMEM;
  1408. goto out;
  1409. }
  1410. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1411. copied = 0;
  1412. err = transport->notify_recv_init(vsk, target, &recv_data);
  1413. if (err < 0)
  1414. goto out;
  1415. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1416. while (1) {
  1417. s64 ready = vsock_stream_has_data(vsk);
  1418. if (ready < 0) {
  1419. /* Invalid queue pair content. XXX This should be
  1420. * changed to a connection reset in a later change.
  1421. */
  1422. err = -ENOMEM;
  1423. goto out_wait;
  1424. } else if (ready > 0) {
  1425. ssize_t read;
  1426. err = transport->notify_recv_pre_dequeue(
  1427. vsk, target, &recv_data);
  1428. if (err < 0)
  1429. break;
  1430. read = transport->stream_dequeue(
  1431. vsk, msg->msg_iov,
  1432. len - copied, flags);
  1433. if (read < 0) {
  1434. err = -ENOMEM;
  1435. break;
  1436. }
  1437. copied += read;
  1438. err = transport->notify_recv_post_dequeue(
  1439. vsk, target, read,
  1440. !(flags & MSG_PEEK), &recv_data);
  1441. if (err < 0)
  1442. goto out_wait;
  1443. if (read >= target || flags & MSG_PEEK)
  1444. break;
  1445. target -= read;
  1446. } else {
  1447. if (sk->sk_err != 0 || (sk->sk_shutdown & RCV_SHUTDOWN)
  1448. || (vsk->peer_shutdown & SEND_SHUTDOWN)) {
  1449. break;
  1450. }
  1451. /* Don't wait for non-blocking sockets. */
  1452. if (timeout == 0) {
  1453. err = -EAGAIN;
  1454. break;
  1455. }
  1456. err = transport->notify_recv_pre_block(
  1457. vsk, target, &recv_data);
  1458. if (err < 0)
  1459. break;
  1460. release_sock(sk);
  1461. timeout = schedule_timeout(timeout);
  1462. lock_sock(sk);
  1463. if (signal_pending(current)) {
  1464. err = sock_intr_errno(timeout);
  1465. break;
  1466. } else if (timeout == 0) {
  1467. err = -EAGAIN;
  1468. break;
  1469. }
  1470. prepare_to_wait(sk_sleep(sk), &wait,
  1471. TASK_INTERRUPTIBLE);
  1472. }
  1473. }
  1474. if (sk->sk_err)
  1475. err = -sk->sk_err;
  1476. else if (sk->sk_shutdown & RCV_SHUTDOWN)
  1477. err = 0;
  1478. if (copied > 0) {
  1479. /* We only do these additional bookkeeping/notification steps
  1480. * if we actually copied something out of the queue pair
  1481. * instead of just peeking ahead.
  1482. */
  1483. if (!(flags & MSG_PEEK)) {
  1484. /* If the other side has shutdown for sending and there
  1485. * is nothing more to read, then modify the socket
  1486. * state.
  1487. */
  1488. if (vsk->peer_shutdown & SEND_SHUTDOWN) {
  1489. if (vsock_stream_has_data(vsk) <= 0) {
  1490. sk->sk_state = SS_UNCONNECTED;
  1491. sock_set_flag(sk, SOCK_DONE);
  1492. sk->sk_state_change(sk);
  1493. }
  1494. }
  1495. }
  1496. err = copied;
  1497. }
  1498. out_wait:
  1499. finish_wait(sk_sleep(sk), &wait);
  1500. out:
  1501. release_sock(sk);
  1502. return err;
  1503. }
  1504. static const struct proto_ops vsock_stream_ops = {
  1505. .family = PF_VSOCK,
  1506. .owner = THIS_MODULE,
  1507. .release = vsock_release,
  1508. .bind = vsock_bind,
  1509. .connect = vsock_stream_connect,
  1510. .socketpair = sock_no_socketpair,
  1511. .accept = vsock_accept,
  1512. .getname = vsock_getname,
  1513. .poll = vsock_poll,
  1514. .ioctl = sock_no_ioctl,
  1515. .listen = vsock_listen,
  1516. .shutdown = vsock_shutdown,
  1517. .setsockopt = vsock_stream_setsockopt,
  1518. .getsockopt = vsock_stream_getsockopt,
  1519. .sendmsg = vsock_stream_sendmsg,
  1520. .recvmsg = vsock_stream_recvmsg,
  1521. .mmap = sock_no_mmap,
  1522. .sendpage = sock_no_sendpage,
  1523. };
  1524. static int vsock_create(struct net *net, struct socket *sock,
  1525. int protocol, int kern)
  1526. {
  1527. if (!sock)
  1528. return -EINVAL;
  1529. if (protocol && protocol != PF_VSOCK)
  1530. return -EPROTONOSUPPORT;
  1531. switch (sock->type) {
  1532. case SOCK_DGRAM:
  1533. sock->ops = &vsock_dgram_ops;
  1534. break;
  1535. case SOCK_STREAM:
  1536. sock->ops = &vsock_stream_ops;
  1537. break;
  1538. default:
  1539. return -ESOCKTNOSUPPORT;
  1540. }
  1541. sock->state = SS_UNCONNECTED;
  1542. return __vsock_create(net, sock, NULL, GFP_KERNEL, 0) ? 0 : -ENOMEM;
  1543. }
  1544. static const struct net_proto_family vsock_family_ops = {
  1545. .family = AF_VSOCK,
  1546. .create = vsock_create,
  1547. .owner = THIS_MODULE,
  1548. };
  1549. static long vsock_dev_do_ioctl(struct file *filp,
  1550. unsigned int cmd, void __user *ptr)
  1551. {
  1552. u32 __user *p = ptr;
  1553. int retval = 0;
  1554. switch (cmd) {
  1555. case IOCTL_VM_SOCKETS_GET_LOCAL_CID:
  1556. if (put_user(transport->get_local_cid(), p) != 0)
  1557. retval = -EFAULT;
  1558. break;
  1559. default:
  1560. pr_err("Unknown ioctl %d\n", cmd);
  1561. retval = -EINVAL;
  1562. }
  1563. return retval;
  1564. }
  1565. static long vsock_dev_ioctl(struct file *filp,
  1566. unsigned int cmd, unsigned long arg)
  1567. {
  1568. return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg);
  1569. }
  1570. #ifdef CONFIG_COMPAT
  1571. static long vsock_dev_compat_ioctl(struct file *filp,
  1572. unsigned int cmd, unsigned long arg)
  1573. {
  1574. return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg));
  1575. }
  1576. #endif
  1577. static const struct file_operations vsock_device_ops = {
  1578. .owner = THIS_MODULE,
  1579. .unlocked_ioctl = vsock_dev_ioctl,
  1580. #ifdef CONFIG_COMPAT
  1581. .compat_ioctl = vsock_dev_compat_ioctl,
  1582. #endif
  1583. .open = nonseekable_open,
  1584. };
  1585. static struct miscdevice vsock_device = {
  1586. .name = "vsock",
  1587. .fops = &vsock_device_ops,
  1588. };
  1589. static int __vsock_core_init(void)
  1590. {
  1591. int err;
  1592. vsock_init_tables();
  1593. vsock_device.minor = MISC_DYNAMIC_MINOR;
  1594. err = misc_register(&vsock_device);
  1595. if (err) {
  1596. pr_err("Failed to register misc device\n");
  1597. return -ENOENT;
  1598. }
  1599. err = proto_register(&vsock_proto, 1); /* we want our slab */
  1600. if (err) {
  1601. pr_err("Cannot register vsock protocol\n");
  1602. goto err_misc_deregister;
  1603. }
  1604. err = sock_register(&vsock_family_ops);
  1605. if (err) {
  1606. pr_err("could not register af_vsock (%d) address family: %d\n",
  1607. AF_VSOCK, err);
  1608. goto err_unregister_proto;
  1609. }
  1610. return 0;
  1611. err_unregister_proto:
  1612. proto_unregister(&vsock_proto);
  1613. err_misc_deregister:
  1614. misc_deregister(&vsock_device);
  1615. return err;
  1616. }
  1617. int vsock_core_init(const struct vsock_transport *t)
  1618. {
  1619. int retval = mutex_lock_interruptible(&vsock_register_mutex);
  1620. if (retval)
  1621. return retval;
  1622. if (transport) {
  1623. retval = -EBUSY;
  1624. goto out;
  1625. }
  1626. transport = t;
  1627. retval = __vsock_core_init();
  1628. if (retval)
  1629. transport = NULL;
  1630. out:
  1631. mutex_unlock(&vsock_register_mutex);
  1632. return retval;
  1633. }
  1634. EXPORT_SYMBOL_GPL(vsock_core_init);
  1635. void vsock_core_exit(void)
  1636. {
  1637. mutex_lock(&vsock_register_mutex);
  1638. misc_deregister(&vsock_device);
  1639. sock_unregister(AF_VSOCK);
  1640. proto_unregister(&vsock_proto);
  1641. /* We do not want the assignment below re-ordered. */
  1642. mb();
  1643. transport = NULL;
  1644. mutex_unlock(&vsock_register_mutex);
  1645. }
  1646. EXPORT_SYMBOL_GPL(vsock_core_exit);
  1647. MODULE_AUTHOR("VMware, Inc.");
  1648. MODULE_DESCRIPTION("VMware Virtual Socket Family");
  1649. MODULE_VERSION("1.0.0.0-k");
  1650. MODULE_LICENSE("GPL v2");