af_vsock.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  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 "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");