af_vsock.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. /*
  2. * VMware vSockets Driver
  3. *
  4. * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. /* Implementation notes:
  16. *
  17. * - There are two kinds of sockets: those created by user action (such as
  18. * calling socket(2)) and those created by incoming connection request packets.
  19. *
  20. * - There are two "global" tables, one for bound sockets (sockets that have
  21. * specified an address that they are responsible for) and one for connected
  22. * sockets (sockets that have established a connection with another socket).
  23. * These tables are "global" in that all sockets on the system are placed
  24. * within them. - Note, though, that the bound table contains an extra entry
  25. * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in
  26. * that list. The bound table is used solely for lookup of sockets when packets
  27. * are received and that's not necessary for SOCK_DGRAM sockets since we create
  28. * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM
  29. * sockets out of the bound hash buckets will reduce the chance of collisions
  30. * when looking for SOCK_STREAM sockets and prevents us from having to check the
  31. * socket type in the hash table lookups.
  32. *
  33. * - Sockets created by user action will either be "client" sockets that
  34. * initiate a connection or "server" sockets that listen for connections; we do
  35. * not support simultaneous connects (two "client" sockets connecting).
  36. *
  37. * - "Server" sockets are referred to as listener sockets throughout this
  38. * implementation because they are in the VSOCK_SS_LISTEN state. When a
  39. * connection request is received (the second kind of socket mentioned above),
  40. * we create a new socket and refer to it as a pending socket. These pending
  41. * sockets are placed on the pending connection list of the listener socket.
  42. * When future packets are received for the address the listener socket is
  43. * bound to, we check if the source of the packet is from one that has an
  44. * existing pending connection. If it does, we process the packet for the
  45. * pending socket. When that socket reaches the connected state, it is removed
  46. * from the listener socket's pending list and enqueued in the listener
  47. * socket's accept queue. Callers of accept(2) will accept connected sockets
  48. * from the listener socket's accept queue. If the socket cannot be accepted
  49. * for some reason then it is marked rejected. Once the connection is
  50. * accepted, it is owned by the user process and the responsibility for cleanup
  51. * falls with that user process.
  52. *
  53. * - It is possible that these pending sockets will never reach the connected
  54. * state; in fact, we may never receive another packet after the connection
  55. * request. Because of this, we must schedule a cleanup function to run in the
  56. * future, after some amount of time passes where a connection should have been
  57. * established. This function ensures that the socket is off all lists so it
  58. * cannot be retrieved, then drops all references to the socket so it is cleaned
  59. * up (sock_put() -> sk_free() -> our sk_destruct implementation). Note this
  60. * function will also cleanup rejected sockets, those that reach the connected
  61. * state but leave it before they have been accepted.
  62. *
  63. * - Sockets created by user action will be cleaned up when the user process
  64. * calls close(2), causing our release implementation to be called. Our release
  65. * implementation will perform some cleanup then drop the last reference so our
  66. * sk_destruct implementation is invoked. Our sk_destruct implementation will
  67. * perform additional cleanup that's common for both types of sockets.
  68. *
  69. * - A socket's reference count is what ensures that the structure won't be
  70. * freed. Each entry in a list (such as the "global" bound and connected tables
  71. * and the listener socket's pending list and connected queue) ensures a
  72. * reference. When we defer work until process context and pass a socket as our
  73. * argument, we must ensure the reference count is increased to ensure the
  74. * socket isn't freed before the function is run; the deferred function will
  75. * then drop the reference.
  76. */
  77. #include <linux/types.h>
  78. #include <linux/bitops.h>
  79. #include <linux/cred.h>
  80. #include <linux/init.h>
  81. #include <linux/io.h>
  82. #include <linux/kernel.h>
  83. #include <linux/kmod.h>
  84. #include <linux/list.h>
  85. #include <linux/miscdevice.h>
  86. #include <linux/module.h>
  87. #include <linux/mutex.h>
  88. #include <linux/net.h>
  89. #include <linux/poll.h>
  90. #include <linux/skbuff.h>
  91. #include <linux/smp.h>
  92. #include <linux/socket.h>
  93. #include <linux/stddef.h>
  94. #include <linux/unistd.h>
  95. #include <linux/wait.h>
  96. #include <linux/workqueue.h>
  97. #include <net/sock.h>
  98. #include <net/af_vsock.h>
  99. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr);
  100. static void vsock_sk_destruct(struct sock *sk);
  101. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
  102. /* Protocol family. */
  103. static struct proto vsock_proto = {
  104. .name = "AF_VSOCK",
  105. .owner = THIS_MODULE,
  106. .obj_size = sizeof(struct vsock_sock),
  107. };
  108. /* The default peer timeout indicates how long we will wait for a peer response
  109. * to a control message.
  110. */
  111. #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
  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. static 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, pending_work.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. /**** SOCKET OPERATIONS ****/
  401. static int __vsock_bind_stream(struct vsock_sock *vsk,
  402. struct sockaddr_vm *addr)
  403. {
  404. static u32 port = LAST_RESERVED_PORT + 1;
  405. struct sockaddr_vm new_addr;
  406. vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
  407. if (addr->svm_port == VMADDR_PORT_ANY) {
  408. bool found = false;
  409. unsigned int i;
  410. for (i = 0; i < MAX_PORT_RETRIES; i++) {
  411. if (port <= LAST_RESERVED_PORT)
  412. port = LAST_RESERVED_PORT + 1;
  413. new_addr.svm_port = port++;
  414. if (!__vsock_find_bound_socket(&new_addr)) {
  415. found = true;
  416. break;
  417. }
  418. }
  419. if (!found)
  420. return -EADDRNOTAVAIL;
  421. } else {
  422. /* If port is in reserved range, ensure caller
  423. * has necessary privileges.
  424. */
  425. if (addr->svm_port <= LAST_RESERVED_PORT &&
  426. !capable(CAP_NET_BIND_SERVICE)) {
  427. return -EACCES;
  428. }
  429. if (__vsock_find_bound_socket(&new_addr))
  430. return -EADDRINUSE;
  431. }
  432. vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);
  433. /* Remove stream sockets from the unbound list and add them to the hash
  434. * table for easy lookup by its address. The unbound list is simply an
  435. * extra entry at the end of the hash table, a trick used by AF_UNIX.
  436. */
  437. __vsock_remove_bound(vsk);
  438. __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
  439. return 0;
  440. }
  441. static int __vsock_bind_dgram(struct vsock_sock *vsk,
  442. struct sockaddr_vm *addr)
  443. {
  444. return transport->dgram_bind(vsk, addr);
  445. }
  446. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
  447. {
  448. struct vsock_sock *vsk = vsock_sk(sk);
  449. u32 cid;
  450. int retval;
  451. /* First ensure this socket isn't already bound. */
  452. if (vsock_addr_bound(&vsk->local_addr))
  453. return -EINVAL;
  454. /* Now bind to the provided address or select appropriate values if
  455. * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that
  456. * like AF_INET prevents binding to a non-local IP address (in most
  457. * cases), we only allow binding to the local CID.
  458. */
  459. cid = transport->get_local_cid();
  460. if (addr->svm_cid != cid && addr->svm_cid != VMADDR_CID_ANY)
  461. return -EADDRNOTAVAIL;
  462. switch (sk->sk_socket->type) {
  463. case SOCK_STREAM:
  464. spin_lock_bh(&vsock_table_lock);
  465. retval = __vsock_bind_stream(vsk, addr);
  466. spin_unlock_bh(&vsock_table_lock);
  467. break;
  468. case SOCK_DGRAM:
  469. retval = __vsock_bind_dgram(vsk, addr);
  470. break;
  471. default:
  472. retval = -EINVAL;
  473. break;
  474. }
  475. return retval;
  476. }
  477. static void vsock_connect_timeout(struct work_struct *work);
  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. int kern)
  484. {
  485. struct sock *sk;
  486. struct vsock_sock *psk;
  487. struct vsock_sock *vsk;
  488. sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern);
  489. if (!sk)
  490. return NULL;
  491. sock_init_data(sock, sk);
  492. /* sk->sk_type is normally set in sock_init_data, but only if sock is
  493. * non-NULL. We make sure that our sockets always have a type by
  494. * setting it here if needed.
  495. */
  496. if (!sock)
  497. sk->sk_type = type;
  498. vsk = vsock_sk(sk);
  499. vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  500. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  501. sk->sk_destruct = vsock_sk_destruct;
  502. sk->sk_backlog_rcv = vsock_queue_rcv_skb;
  503. sk->sk_state = 0;
  504. sock_reset_flag(sk, SOCK_DONE);
  505. INIT_LIST_HEAD(&vsk->bound_table);
  506. INIT_LIST_HEAD(&vsk->connected_table);
  507. vsk->listener = NULL;
  508. INIT_LIST_HEAD(&vsk->pending_links);
  509. INIT_LIST_HEAD(&vsk->accept_queue);
  510. vsk->rejected = false;
  511. vsk->sent_request = false;
  512. vsk->ignore_connecting_rst = false;
  513. vsk->peer_shutdown = 0;
  514. INIT_DELAYED_WORK(&vsk->connect_work, vsock_connect_timeout);
  515. INIT_DELAYED_WORK(&vsk->pending_work, vsock_pending_work);
  516. psk = parent ? vsock_sk(parent) : NULL;
  517. if (parent) {
  518. vsk->trusted = psk->trusted;
  519. vsk->owner = get_cred(psk->owner);
  520. vsk->connect_timeout = psk->connect_timeout;
  521. } else {
  522. vsk->trusted = capable(CAP_NET_ADMIN);
  523. vsk->owner = get_current_cred();
  524. vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT;
  525. }
  526. if (transport->init(vsk, psk) < 0) {
  527. sk_free(sk);
  528. return NULL;
  529. }
  530. if (sock)
  531. vsock_insert_unbound(vsk);
  532. return sk;
  533. }
  534. EXPORT_SYMBOL_GPL(__vsock_create);
  535. static void __vsock_release(struct sock *sk)
  536. {
  537. if (sk) {
  538. struct sk_buff *skb;
  539. struct sock *pending;
  540. struct vsock_sock *vsk;
  541. vsk = vsock_sk(sk);
  542. pending = NULL; /* Compiler warning. */
  543. if (vsock_in_bound_table(vsk))
  544. vsock_remove_bound(vsk);
  545. if (vsock_in_connected_table(vsk))
  546. vsock_remove_connected(vsk);
  547. transport->release(vsk);
  548. lock_sock(sk);
  549. sock_orphan(sk);
  550. sk->sk_shutdown = SHUTDOWN_MASK;
  551. while ((skb = skb_dequeue(&sk->sk_receive_queue)))
  552. kfree_skb(skb);
  553. /* Clean up any sockets that never were accepted. */
  554. while ((pending = vsock_dequeue_accept(sk)) != NULL) {
  555. __vsock_release(pending);
  556. sock_put(pending);
  557. }
  558. release_sock(sk);
  559. sock_put(sk);
  560. }
  561. }
  562. static void vsock_sk_destruct(struct sock *sk)
  563. {
  564. struct vsock_sock *vsk = vsock_sk(sk);
  565. transport->destruct(vsk);
  566. /* When clearing these addresses, there's no need to set the family and
  567. * possibly register the address family with the kernel.
  568. */
  569. vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  570. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  571. put_cred(vsk->owner);
  572. }
  573. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  574. {
  575. int err;
  576. err = sock_queue_rcv_skb(sk, skb);
  577. if (err)
  578. kfree_skb(skb);
  579. return err;
  580. }
  581. s64 vsock_stream_has_data(struct vsock_sock *vsk)
  582. {
  583. return transport->stream_has_data(vsk);
  584. }
  585. EXPORT_SYMBOL_GPL(vsock_stream_has_data);
  586. s64 vsock_stream_has_space(struct vsock_sock *vsk)
  587. {
  588. return transport->stream_has_space(vsk);
  589. }
  590. EXPORT_SYMBOL_GPL(vsock_stream_has_space);
  591. static int vsock_release(struct socket *sock)
  592. {
  593. __vsock_release(sock->sk);
  594. sock->sk = NULL;
  595. sock->state = SS_FREE;
  596. return 0;
  597. }
  598. static int
  599. vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  600. {
  601. int err;
  602. struct sock *sk;
  603. struct sockaddr_vm *vm_addr;
  604. sk = sock->sk;
  605. if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0)
  606. return -EINVAL;
  607. lock_sock(sk);
  608. err = __vsock_bind(sk, vm_addr);
  609. release_sock(sk);
  610. return err;
  611. }
  612. static int vsock_getname(struct socket *sock,
  613. struct sockaddr *addr, int *addr_len, int peer)
  614. {
  615. int err;
  616. struct sock *sk;
  617. struct vsock_sock *vsk;
  618. struct sockaddr_vm *vm_addr;
  619. sk = sock->sk;
  620. vsk = vsock_sk(sk);
  621. err = 0;
  622. lock_sock(sk);
  623. if (peer) {
  624. if (sock->state != SS_CONNECTED) {
  625. err = -ENOTCONN;
  626. goto out;
  627. }
  628. vm_addr = &vsk->remote_addr;
  629. } else {
  630. vm_addr = &vsk->local_addr;
  631. }
  632. if (!vm_addr) {
  633. err = -EINVAL;
  634. goto out;
  635. }
  636. /* sys_getsockname() and sys_getpeername() pass us a
  637. * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately
  638. * that macro is defined in socket.c instead of .h, so we hardcode its
  639. * value here.
  640. */
  641. BUILD_BUG_ON(sizeof(*vm_addr) > 128);
  642. memcpy(addr, vm_addr, sizeof(*vm_addr));
  643. *addr_len = sizeof(*vm_addr);
  644. out:
  645. release_sock(sk);
  646. return err;
  647. }
  648. static int vsock_shutdown(struct socket *sock, int mode)
  649. {
  650. int err;
  651. struct sock *sk;
  652. /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
  653. * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
  654. * here like the other address families do. Note also that the
  655. * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
  656. * which is what we want.
  657. */
  658. mode++;
  659. if ((mode & ~SHUTDOWN_MASK) || !mode)
  660. return -EINVAL;
  661. /* If this is a STREAM socket and it is not connected then bail out
  662. * immediately. If it is a DGRAM socket then we must first kick the
  663. * socket so that it wakes up from any sleeping calls, for example
  664. * recv(), and then afterwards return the error.
  665. */
  666. sk = sock->sk;
  667. if (sock->state == SS_UNCONNECTED) {
  668. err = -ENOTCONN;
  669. if (sk->sk_type == SOCK_STREAM)
  670. return err;
  671. } else {
  672. sock->state = SS_DISCONNECTING;
  673. err = 0;
  674. }
  675. /* Receive and send shutdowns are treated alike. */
  676. mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
  677. if (mode) {
  678. lock_sock(sk);
  679. sk->sk_shutdown |= mode;
  680. sk->sk_state_change(sk);
  681. release_sock(sk);
  682. if (sk->sk_type == SOCK_STREAM) {
  683. sock_reset_flag(sk, SOCK_DONE);
  684. vsock_send_shutdown(sk, mode);
  685. }
  686. }
  687. return err;
  688. }
  689. static unsigned int vsock_poll(struct file *file, struct socket *sock,
  690. poll_table *wait)
  691. {
  692. struct sock *sk;
  693. unsigned int mask;
  694. struct vsock_sock *vsk;
  695. sk = sock->sk;
  696. vsk = vsock_sk(sk);
  697. poll_wait(file, sk_sleep(sk), wait);
  698. mask = 0;
  699. if (sk->sk_err)
  700. /* Signify that there has been an error on this socket. */
  701. mask |= POLLERR;
  702. /* INET sockets treat local write shutdown and peer write shutdown as a
  703. * case of POLLHUP set.
  704. */
  705. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  706. ((sk->sk_shutdown & SEND_SHUTDOWN) &&
  707. (vsk->peer_shutdown & SEND_SHUTDOWN))) {
  708. mask |= POLLHUP;
  709. }
  710. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  711. vsk->peer_shutdown & SEND_SHUTDOWN) {
  712. mask |= POLLRDHUP;
  713. }
  714. if (sock->type == SOCK_DGRAM) {
  715. /* For datagram sockets we can read if there is something in
  716. * the queue and write as long as the socket isn't shutdown for
  717. * sending.
  718. */
  719. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  720. (sk->sk_shutdown & RCV_SHUTDOWN)) {
  721. mask |= POLLIN | POLLRDNORM;
  722. }
  723. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  724. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  725. } else if (sock->type == SOCK_STREAM) {
  726. lock_sock(sk);
  727. /* Listening sockets that have connections in their accept
  728. * queue can be read.
  729. */
  730. if (sk->sk_state == VSOCK_SS_LISTEN
  731. && !vsock_is_accept_queue_empty(sk))
  732. mask |= POLLIN | POLLRDNORM;
  733. /* If there is something in the queue then we can read. */
  734. if (transport->stream_is_active(vsk) &&
  735. !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  736. bool data_ready_now = false;
  737. int ret = transport->notify_poll_in(
  738. vsk, 1, &data_ready_now);
  739. if (ret < 0) {
  740. mask |= POLLERR;
  741. } else {
  742. if (data_ready_now)
  743. mask |= POLLIN | POLLRDNORM;
  744. }
  745. }
  746. /* Sockets whose connections have been closed, reset, or
  747. * terminated should also be considered read, and we check the
  748. * shutdown flag for that.
  749. */
  750. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  751. vsk->peer_shutdown & SEND_SHUTDOWN) {
  752. mask |= POLLIN | POLLRDNORM;
  753. }
  754. /* Connected sockets that can produce data can be written. */
  755. if (sk->sk_state == SS_CONNECTED) {
  756. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  757. bool space_avail_now = false;
  758. int ret = transport->notify_poll_out(
  759. vsk, 1, &space_avail_now);
  760. if (ret < 0) {
  761. mask |= POLLERR;
  762. } else {
  763. if (space_avail_now)
  764. /* Remove POLLWRBAND since INET
  765. * sockets are not setting it.
  766. */
  767. mask |= POLLOUT | POLLWRNORM;
  768. }
  769. }
  770. }
  771. /* Simulate INET socket poll behaviors, which sets
  772. * POLLOUT|POLLWRNORM when peer is closed and nothing to read,
  773. * but local send is not shutdown.
  774. */
  775. if (sk->sk_state == SS_UNCONNECTED) {
  776. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  777. mask |= POLLOUT | POLLWRNORM;
  778. }
  779. release_sock(sk);
  780. }
  781. return mask;
  782. }
  783. static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  784. size_t len)
  785. {
  786. int err;
  787. struct sock *sk;
  788. struct vsock_sock *vsk;
  789. struct sockaddr_vm *remote_addr;
  790. if (msg->msg_flags & MSG_OOB)
  791. return -EOPNOTSUPP;
  792. /* For now, MSG_DONTWAIT is always assumed... */
  793. err = 0;
  794. sk = sock->sk;
  795. vsk = vsock_sk(sk);
  796. lock_sock(sk);
  797. err = vsock_auto_bind(vsk);
  798. if (err)
  799. goto out;
  800. /* If the provided message contains an address, use that. Otherwise
  801. * fall back on the socket's remote handle (if it has been connected).
  802. */
  803. if (msg->msg_name &&
  804. vsock_addr_cast(msg->msg_name, msg->msg_namelen,
  805. &remote_addr) == 0) {
  806. /* Ensure this address is of the right type and is a valid
  807. * destination.
  808. */
  809. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  810. remote_addr->svm_cid = transport->get_local_cid();
  811. if (!vsock_addr_bound(remote_addr)) {
  812. err = -EINVAL;
  813. goto out;
  814. }
  815. } else if (sock->state == SS_CONNECTED) {
  816. remote_addr = &vsk->remote_addr;
  817. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  818. remote_addr->svm_cid = transport->get_local_cid();
  819. /* XXX Should connect() or this function ensure remote_addr is
  820. * bound?
  821. */
  822. if (!vsock_addr_bound(&vsk->remote_addr)) {
  823. err = -EINVAL;
  824. goto out;
  825. }
  826. } else {
  827. err = -EINVAL;
  828. goto out;
  829. }
  830. if (!transport->dgram_allow(remote_addr->svm_cid,
  831. remote_addr->svm_port)) {
  832. err = -EINVAL;
  833. goto out;
  834. }
  835. err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
  836. out:
  837. release_sock(sk);
  838. return err;
  839. }
  840. static int vsock_dgram_connect(struct socket *sock,
  841. struct sockaddr *addr, int addr_len, int flags)
  842. {
  843. int err;
  844. struct sock *sk;
  845. struct vsock_sock *vsk;
  846. struct sockaddr_vm *remote_addr;
  847. sk = sock->sk;
  848. vsk = vsock_sk(sk);
  849. err = vsock_addr_cast(addr, addr_len, &remote_addr);
  850. if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) {
  851. lock_sock(sk);
  852. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY,
  853. VMADDR_PORT_ANY);
  854. sock->state = SS_UNCONNECTED;
  855. release_sock(sk);
  856. return 0;
  857. } else if (err != 0)
  858. return -EINVAL;
  859. lock_sock(sk);
  860. err = vsock_auto_bind(vsk);
  861. if (err)
  862. goto out;
  863. if (!transport->dgram_allow(remote_addr->svm_cid,
  864. remote_addr->svm_port)) {
  865. err = -EINVAL;
  866. goto out;
  867. }
  868. memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr));
  869. sock->state = SS_CONNECTED;
  870. out:
  871. release_sock(sk);
  872. return err;
  873. }
  874. static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  875. size_t len, int flags)
  876. {
  877. return transport->dgram_dequeue(vsock_sk(sock->sk), msg, len, flags);
  878. }
  879. static const struct proto_ops vsock_dgram_ops = {
  880. .family = PF_VSOCK,
  881. .owner = THIS_MODULE,
  882. .release = vsock_release,
  883. .bind = vsock_bind,
  884. .connect = vsock_dgram_connect,
  885. .socketpair = sock_no_socketpair,
  886. .accept = sock_no_accept,
  887. .getname = vsock_getname,
  888. .poll = vsock_poll,
  889. .ioctl = sock_no_ioctl,
  890. .listen = sock_no_listen,
  891. .shutdown = vsock_shutdown,
  892. .setsockopt = sock_no_setsockopt,
  893. .getsockopt = sock_no_getsockopt,
  894. .sendmsg = vsock_dgram_sendmsg,
  895. .recvmsg = vsock_dgram_recvmsg,
  896. .mmap = sock_no_mmap,
  897. .sendpage = sock_no_sendpage,
  898. };
  899. static void vsock_connect_timeout(struct work_struct *work)
  900. {
  901. struct sock *sk;
  902. struct vsock_sock *vsk;
  903. vsk = container_of(work, struct vsock_sock, connect_work.work);
  904. sk = sk_vsock(vsk);
  905. lock_sock(sk);
  906. if (sk->sk_state == SS_CONNECTING &&
  907. (sk->sk_shutdown != SHUTDOWN_MASK)) {
  908. sk->sk_state = SS_UNCONNECTED;
  909. sk->sk_err = ETIMEDOUT;
  910. sk->sk_error_report(sk);
  911. }
  912. release_sock(sk);
  913. sock_put(sk);
  914. }
  915. static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
  916. int addr_len, int flags)
  917. {
  918. int err;
  919. struct sock *sk;
  920. struct vsock_sock *vsk;
  921. struct sockaddr_vm *remote_addr;
  922. long timeout;
  923. DEFINE_WAIT(wait);
  924. err = 0;
  925. sk = sock->sk;
  926. vsk = vsock_sk(sk);
  927. lock_sock(sk);
  928. /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
  929. switch (sock->state) {
  930. case SS_CONNECTED:
  931. err = -EISCONN;
  932. goto out;
  933. case SS_DISCONNECTING:
  934. err = -EINVAL;
  935. goto out;
  936. case SS_CONNECTING:
  937. /* This continues on so we can move sock into the SS_CONNECTED
  938. * state once the connection has completed (at which point err
  939. * will be set to zero also). Otherwise, we will either wait
  940. * for the connection or return -EALREADY should this be a
  941. * non-blocking call.
  942. */
  943. err = -EALREADY;
  944. break;
  945. default:
  946. if ((sk->sk_state == VSOCK_SS_LISTEN) ||
  947. vsock_addr_cast(addr, addr_len, &remote_addr) != 0) {
  948. err = -EINVAL;
  949. goto out;
  950. }
  951. /* The hypervisor and well-known contexts do not have socket
  952. * endpoints.
  953. */
  954. if (!transport->stream_allow(remote_addr->svm_cid,
  955. remote_addr->svm_port)) {
  956. err = -ENETUNREACH;
  957. goto out;
  958. }
  959. /* Set the remote address that we are connecting to. */
  960. memcpy(&vsk->remote_addr, remote_addr,
  961. sizeof(vsk->remote_addr));
  962. err = vsock_auto_bind(vsk);
  963. if (err)
  964. goto out;
  965. sk->sk_state = SS_CONNECTING;
  966. err = transport->connect(vsk);
  967. if (err < 0)
  968. goto out;
  969. /* Mark sock as connecting and set the error code to in
  970. * progress in case this is a non-blocking connect.
  971. */
  972. sock->state = SS_CONNECTING;
  973. err = -EINPROGRESS;
  974. }
  975. /* The receive path will handle all communication until we are able to
  976. * enter the connected state. Here we wait for the connection to be
  977. * completed or a notification of an error.
  978. */
  979. timeout = vsk->connect_timeout;
  980. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  981. while (sk->sk_state != SS_CONNECTED && sk->sk_err == 0) {
  982. if (flags & O_NONBLOCK) {
  983. /* If we're not going to block, we schedule a timeout
  984. * function to generate a timeout on the connection
  985. * attempt, in case the peer doesn't respond in a
  986. * timely manner. We hold on to the socket until the
  987. * timeout fires.
  988. */
  989. sock_hold(sk);
  990. schedule_delayed_work(&vsk->connect_work, 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. sk->sk_state = SS_UNCONNECTED;
  1000. sock->state = SS_UNCONNECTED;
  1001. goto out_wait;
  1002. } else if (timeout == 0) {
  1003. err = -ETIMEDOUT;
  1004. sk->sk_state = SS_UNCONNECTED;
  1005. sock->state = SS_UNCONNECTED;
  1006. goto out_wait;
  1007. }
  1008. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1009. }
  1010. if (sk->sk_err) {
  1011. err = -sk->sk_err;
  1012. sk->sk_state = SS_UNCONNECTED;
  1013. sock->state = SS_UNCONNECTED;
  1014. } else {
  1015. err = 0;
  1016. }
  1017. out_wait:
  1018. finish_wait(sk_sleep(sk), &wait);
  1019. out:
  1020. release_sock(sk);
  1021. return err;
  1022. }
  1023. static int vsock_accept(struct socket *sock, struct socket *newsock, int flags)
  1024. {
  1025. struct sock *listener;
  1026. int err;
  1027. struct sock *connected;
  1028. struct vsock_sock *vconnected;
  1029. long timeout;
  1030. DEFINE_WAIT(wait);
  1031. err = 0;
  1032. listener = sock->sk;
  1033. lock_sock(listener);
  1034. if (sock->type != SOCK_STREAM) {
  1035. err = -EOPNOTSUPP;
  1036. goto out;
  1037. }
  1038. if (listener->sk_state != VSOCK_SS_LISTEN) {
  1039. err = -EINVAL;
  1040. goto out;
  1041. }
  1042. /* Wait for children sockets to appear; these are the new sockets
  1043. * created upon connection establishment.
  1044. */
  1045. timeout = sock_sndtimeo(listener, flags & O_NONBLOCK);
  1046. prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
  1047. while ((connected = vsock_dequeue_accept(listener)) == NULL &&
  1048. listener->sk_err == 0) {
  1049. release_sock(listener);
  1050. timeout = schedule_timeout(timeout);
  1051. finish_wait(sk_sleep(listener), &wait);
  1052. lock_sock(listener);
  1053. if (signal_pending(current)) {
  1054. err = sock_intr_errno(timeout);
  1055. goto out;
  1056. } else if (timeout == 0) {
  1057. err = -EAGAIN;
  1058. goto out;
  1059. }
  1060. prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
  1061. }
  1062. finish_wait(sk_sleep(listener), &wait);
  1063. if (listener->sk_err)
  1064. err = -listener->sk_err;
  1065. if (connected) {
  1066. listener->sk_ack_backlog--;
  1067. lock_sock(connected);
  1068. vconnected = vsock_sk(connected);
  1069. /* If the listener socket has received an error, then we should
  1070. * reject this socket and return. Note that we simply mark the
  1071. * socket rejected, drop our reference, and let the cleanup
  1072. * function handle the cleanup; the fact that we found it in
  1073. * the listener's accept queue guarantees that the cleanup
  1074. * function hasn't run yet.
  1075. */
  1076. if (err) {
  1077. vconnected->rejected = true;
  1078. } else {
  1079. newsock->state = SS_CONNECTED;
  1080. sock_graft(connected, newsock);
  1081. }
  1082. release_sock(connected);
  1083. sock_put(connected);
  1084. }
  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 = VSOCK_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 socket *sock, struct msghdr *msg,
  1240. 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_FUNC(wait, woken_wake_function);
  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. while (total_written < len) {
  1282. ssize_t written;
  1283. add_wait_queue(sk_sleep(sk), &wait);
  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. remove_wait_queue(sk_sleep(sk), &wait);
  1292. goto out_err;
  1293. }
  1294. err = transport->notify_send_pre_block(vsk, &send_data);
  1295. if (err < 0) {
  1296. remove_wait_queue(sk_sleep(sk), &wait);
  1297. goto out_err;
  1298. }
  1299. release_sock(sk);
  1300. timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
  1301. lock_sock(sk);
  1302. if (signal_pending(current)) {
  1303. err = sock_intr_errno(timeout);
  1304. remove_wait_queue(sk_sleep(sk), &wait);
  1305. goto out_err;
  1306. } else if (timeout == 0) {
  1307. err = -EAGAIN;
  1308. remove_wait_queue(sk_sleep(sk), &wait);
  1309. goto out_err;
  1310. }
  1311. }
  1312. remove_wait_queue(sk_sleep(sk), &wait);
  1313. /* These checks occur both as part of and after the loop
  1314. * conditional since we need to check before and after
  1315. * sleeping.
  1316. */
  1317. if (sk->sk_err) {
  1318. err = -sk->sk_err;
  1319. goto out_err;
  1320. } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
  1321. (vsk->peer_shutdown & RCV_SHUTDOWN)) {
  1322. err = -EPIPE;
  1323. goto out_err;
  1324. }
  1325. err = transport->notify_send_pre_enqueue(vsk, &send_data);
  1326. if (err < 0)
  1327. goto out_err;
  1328. /* Note that enqueue will only write as many bytes as are free
  1329. * in the produce queue, so we don't need to ensure len is
  1330. * smaller than the queue size. It is the caller's
  1331. * responsibility to check how many bytes we were able to send.
  1332. */
  1333. written = transport->stream_enqueue(
  1334. vsk, msg,
  1335. len - total_written);
  1336. if (written < 0) {
  1337. err = -ENOMEM;
  1338. goto out_err;
  1339. }
  1340. total_written += written;
  1341. err = transport->notify_send_post_enqueue(
  1342. vsk, written, &send_data);
  1343. if (err < 0)
  1344. goto out_err;
  1345. }
  1346. out_err:
  1347. if (total_written > 0)
  1348. err = total_written;
  1349. out:
  1350. release_sock(sk);
  1351. return err;
  1352. }
  1353. static int
  1354. vsock_stream_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1355. int flags)
  1356. {
  1357. struct sock *sk;
  1358. struct vsock_sock *vsk;
  1359. int err;
  1360. size_t target;
  1361. ssize_t copied;
  1362. long timeout;
  1363. struct vsock_transport_recv_notify_data recv_data;
  1364. DEFINE_WAIT(wait);
  1365. sk = sock->sk;
  1366. vsk = vsock_sk(sk);
  1367. err = 0;
  1368. lock_sock(sk);
  1369. if (sk->sk_state != SS_CONNECTED) {
  1370. /* Recvmsg is supposed to return 0 if a peer performs an
  1371. * orderly shutdown. Differentiate between that case and when a
  1372. * peer has not connected or a local shutdown occured with the
  1373. * SOCK_DONE flag.
  1374. */
  1375. if (sock_flag(sk, SOCK_DONE))
  1376. err = 0;
  1377. else
  1378. err = -ENOTCONN;
  1379. goto out;
  1380. }
  1381. if (flags & MSG_OOB) {
  1382. err = -EOPNOTSUPP;
  1383. goto out;
  1384. }
  1385. /* We don't check peer_shutdown flag here since peer may actually shut
  1386. * down, but there can be data in the queue that a local socket can
  1387. * receive.
  1388. */
  1389. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1390. err = 0;
  1391. goto out;
  1392. }
  1393. /* It is valid on Linux to pass in a zero-length receive buffer. This
  1394. * is not an error. We may as well bail out now.
  1395. */
  1396. if (!len) {
  1397. err = 0;
  1398. goto out;
  1399. }
  1400. /* We must not copy less than target bytes into the user's buffer
  1401. * before returning successfully, so we wait for the consume queue to
  1402. * have that much data to consume before dequeueing. Note that this
  1403. * makes it impossible to handle cases where target is greater than the
  1404. * queue size.
  1405. */
  1406. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1407. if (target >= transport->stream_rcvhiwat(vsk)) {
  1408. err = -ENOMEM;
  1409. goto out;
  1410. }
  1411. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1412. copied = 0;
  1413. err = transport->notify_recv_init(vsk, target, &recv_data);
  1414. if (err < 0)
  1415. goto out;
  1416. while (1) {
  1417. s64 ready;
  1418. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1419. ready = vsock_stream_has_data(vsk);
  1420. if (ready == 0) {
  1421. if (sk->sk_err != 0 ||
  1422. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1423. (vsk->peer_shutdown & SEND_SHUTDOWN)) {
  1424. finish_wait(sk_sleep(sk), &wait);
  1425. break;
  1426. }
  1427. /* Don't wait for non-blocking sockets. */
  1428. if (timeout == 0) {
  1429. err = -EAGAIN;
  1430. finish_wait(sk_sleep(sk), &wait);
  1431. break;
  1432. }
  1433. err = transport->notify_recv_pre_block(
  1434. vsk, target, &recv_data);
  1435. if (err < 0) {
  1436. finish_wait(sk_sleep(sk), &wait);
  1437. break;
  1438. }
  1439. release_sock(sk);
  1440. timeout = schedule_timeout(timeout);
  1441. lock_sock(sk);
  1442. if (signal_pending(current)) {
  1443. err = sock_intr_errno(timeout);
  1444. finish_wait(sk_sleep(sk), &wait);
  1445. break;
  1446. } else if (timeout == 0) {
  1447. err = -EAGAIN;
  1448. finish_wait(sk_sleep(sk), &wait);
  1449. break;
  1450. }
  1451. } else {
  1452. ssize_t read;
  1453. finish_wait(sk_sleep(sk), &wait);
  1454. if (ready < 0) {
  1455. /* Invalid queue pair content. XXX This should
  1456. * be changed to a connection reset in a later
  1457. * change.
  1458. */
  1459. err = -ENOMEM;
  1460. goto out;
  1461. }
  1462. err = transport->notify_recv_pre_dequeue(
  1463. vsk, target, &recv_data);
  1464. if (err < 0)
  1465. break;
  1466. read = transport->stream_dequeue(
  1467. vsk, msg,
  1468. len - copied, flags);
  1469. if (read < 0) {
  1470. err = -ENOMEM;
  1471. break;
  1472. }
  1473. copied += read;
  1474. err = transport->notify_recv_post_dequeue(
  1475. vsk, target, read,
  1476. !(flags & MSG_PEEK), &recv_data);
  1477. if (err < 0)
  1478. goto out;
  1479. if (read >= target || flags & MSG_PEEK)
  1480. break;
  1481. target -= read;
  1482. }
  1483. }
  1484. if (sk->sk_err)
  1485. err = -sk->sk_err;
  1486. else if (sk->sk_shutdown & RCV_SHUTDOWN)
  1487. err = 0;
  1488. if (copied > 0)
  1489. err = copied;
  1490. out:
  1491. release_sock(sk);
  1492. return err;
  1493. }
  1494. static const struct proto_ops vsock_stream_ops = {
  1495. .family = PF_VSOCK,
  1496. .owner = THIS_MODULE,
  1497. .release = vsock_release,
  1498. .bind = vsock_bind,
  1499. .connect = vsock_stream_connect,
  1500. .socketpair = sock_no_socketpair,
  1501. .accept = vsock_accept,
  1502. .getname = vsock_getname,
  1503. .poll = vsock_poll,
  1504. .ioctl = sock_no_ioctl,
  1505. .listen = vsock_listen,
  1506. .shutdown = vsock_shutdown,
  1507. .setsockopt = vsock_stream_setsockopt,
  1508. .getsockopt = vsock_stream_getsockopt,
  1509. .sendmsg = vsock_stream_sendmsg,
  1510. .recvmsg = vsock_stream_recvmsg,
  1511. .mmap = sock_no_mmap,
  1512. .sendpage = sock_no_sendpage,
  1513. };
  1514. static int vsock_create(struct net *net, struct socket *sock,
  1515. int protocol, int kern)
  1516. {
  1517. if (!sock)
  1518. return -EINVAL;
  1519. if (protocol && protocol != PF_VSOCK)
  1520. return -EPROTONOSUPPORT;
  1521. switch (sock->type) {
  1522. case SOCK_DGRAM:
  1523. sock->ops = &vsock_dgram_ops;
  1524. break;
  1525. case SOCK_STREAM:
  1526. sock->ops = &vsock_stream_ops;
  1527. break;
  1528. default:
  1529. return -ESOCKTNOSUPPORT;
  1530. }
  1531. sock->state = SS_UNCONNECTED;
  1532. return __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern) ? 0 : -ENOMEM;
  1533. }
  1534. static const struct net_proto_family vsock_family_ops = {
  1535. .family = AF_VSOCK,
  1536. .create = vsock_create,
  1537. .owner = THIS_MODULE,
  1538. };
  1539. static long vsock_dev_do_ioctl(struct file *filp,
  1540. unsigned int cmd, void __user *ptr)
  1541. {
  1542. u32 __user *p = ptr;
  1543. int retval = 0;
  1544. switch (cmd) {
  1545. case IOCTL_VM_SOCKETS_GET_LOCAL_CID:
  1546. if (put_user(transport->get_local_cid(), p) != 0)
  1547. retval = -EFAULT;
  1548. break;
  1549. default:
  1550. pr_err("Unknown ioctl %d\n", cmd);
  1551. retval = -EINVAL;
  1552. }
  1553. return retval;
  1554. }
  1555. static long vsock_dev_ioctl(struct file *filp,
  1556. unsigned int cmd, unsigned long arg)
  1557. {
  1558. return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg);
  1559. }
  1560. #ifdef CONFIG_COMPAT
  1561. static long vsock_dev_compat_ioctl(struct file *filp,
  1562. unsigned int cmd, unsigned long arg)
  1563. {
  1564. return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg));
  1565. }
  1566. #endif
  1567. static const struct file_operations vsock_device_ops = {
  1568. .owner = THIS_MODULE,
  1569. .unlocked_ioctl = vsock_dev_ioctl,
  1570. #ifdef CONFIG_COMPAT
  1571. .compat_ioctl = vsock_dev_compat_ioctl,
  1572. #endif
  1573. .open = nonseekable_open,
  1574. };
  1575. static struct miscdevice vsock_device = {
  1576. .name = "vsock",
  1577. .fops = &vsock_device_ops,
  1578. };
  1579. int __vsock_core_init(const struct vsock_transport *t, struct module *owner)
  1580. {
  1581. int err = mutex_lock_interruptible(&vsock_register_mutex);
  1582. if (err)
  1583. return err;
  1584. if (transport) {
  1585. err = -EBUSY;
  1586. goto err_busy;
  1587. }
  1588. /* Transport must be the owner of the protocol so that it can't
  1589. * unload while there are open sockets.
  1590. */
  1591. vsock_proto.owner = owner;
  1592. transport = t;
  1593. vsock_init_tables();
  1594. vsock_device.minor = MISC_DYNAMIC_MINOR;
  1595. err = misc_register(&vsock_device);
  1596. if (err) {
  1597. pr_err("Failed to register misc device\n");
  1598. goto err_reset_transport;
  1599. }
  1600. err = proto_register(&vsock_proto, 1); /* we want our slab */
  1601. if (err) {
  1602. pr_err("Cannot register vsock protocol\n");
  1603. goto err_deregister_misc;
  1604. }
  1605. err = sock_register(&vsock_family_ops);
  1606. if (err) {
  1607. pr_err("could not register af_vsock (%d) address family: %d\n",
  1608. AF_VSOCK, err);
  1609. goto err_unregister_proto;
  1610. }
  1611. mutex_unlock(&vsock_register_mutex);
  1612. return 0;
  1613. err_unregister_proto:
  1614. proto_unregister(&vsock_proto);
  1615. err_deregister_misc:
  1616. misc_deregister(&vsock_device);
  1617. err_reset_transport:
  1618. transport = NULL;
  1619. err_busy:
  1620. mutex_unlock(&vsock_register_mutex);
  1621. return err;
  1622. }
  1623. EXPORT_SYMBOL_GPL(__vsock_core_init);
  1624. void vsock_core_exit(void)
  1625. {
  1626. mutex_lock(&vsock_register_mutex);
  1627. misc_deregister(&vsock_device);
  1628. sock_unregister(AF_VSOCK);
  1629. proto_unregister(&vsock_proto);
  1630. /* We do not want the assignment below re-ordered. */
  1631. mb();
  1632. transport = NULL;
  1633. mutex_unlock(&vsock_register_mutex);
  1634. }
  1635. EXPORT_SYMBOL_GPL(vsock_core_exit);
  1636. MODULE_AUTHOR("VMware, Inc.");
  1637. MODULE_DESCRIPTION("VMware Virtual Socket Family");
  1638. MODULE_VERSION("1.0.1.0-k");
  1639. MODULE_LICENSE("GPL v2");