af_vsock.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. /*
  2. * VMware vSockets Driver
  3. *
  4. * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. /* Implementation notes:
  16. *
  17. * - There are two kinds of sockets: those created by user action (such as
  18. * calling socket(2)) and those created by incoming connection request packets.
  19. *
  20. * - There are two "global" tables, one for bound sockets (sockets that have
  21. * specified an address that they are responsible for) and one for connected
  22. * sockets (sockets that have established a connection with another socket).
  23. * These tables are "global" in that all sockets on the system are placed
  24. * within them. - Note, though, that the bound table contains an extra entry
  25. * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in
  26. * that list. The bound table is used solely for lookup of sockets when packets
  27. * are received and that's not necessary for SOCK_DGRAM sockets since we create
  28. * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM
  29. * sockets out of the bound hash buckets will reduce the chance of collisions
  30. * when looking for SOCK_STREAM sockets and prevents us from having to check the
  31. * socket type in the hash table lookups.
  32. *
  33. * - Sockets created by user action will either be "client" sockets that
  34. * initiate a connection or "server" sockets that listen for connections; we do
  35. * not support simultaneous connects (two "client" sockets connecting).
  36. *
  37. * - "Server" sockets are referred to as listener sockets throughout this
  38. * implementation because they are in the SS_LISTEN state. When a connection
  39. * request is received (the second kind of socket mentioned above), we create a
  40. * new socket and refer to it as a pending socket. These pending sockets are
  41. * placed on the pending connection list of the listener socket. When future
  42. * packets are received for the address the listener socket is bound to, we
  43. * check if the source of the packet is from one that has an existing pending
  44. * connection. If it does, we process the packet for the pending socket. When
  45. * that socket reaches the connected state, it is removed from the listener
  46. * socket's pending list and enqueued in the listener socket's accept queue.
  47. * Callers of accept(2) will accept connected sockets from the listener socket's
  48. * accept queue. If the socket cannot be accepted for some reason then it is
  49. * marked rejected. Once the connection is accepted, it is owned by the user
  50. * process and the responsibility for cleanup falls with that user process.
  51. *
  52. * - It is possible that these pending sockets will never reach the connected
  53. * state; in fact, we may never receive another packet after the connection
  54. * request. Because of this, we must schedule a cleanup function to run in the
  55. * future, after some amount of time passes where a connection should have been
  56. * established. This function ensures that the socket is off all lists so it
  57. * cannot be retrieved, then drops all references to the socket so it is cleaned
  58. * up (sock_put() -> sk_free() -> our sk_destruct implementation). Note this
  59. * function will also cleanup rejected sockets, those that reach the connected
  60. * state but leave it before they have been accepted.
  61. *
  62. * - Sockets created by user action will be cleaned up when the user process
  63. * calls close(2), causing our release implementation to be called. Our release
  64. * implementation will perform some cleanup then drop the last reference so our
  65. * sk_destruct implementation is invoked. Our sk_destruct implementation will
  66. * perform additional cleanup that's common for both types of sockets.
  67. *
  68. * - A socket's reference count is what ensures that the structure won't be
  69. * freed. Each entry in a list (such as the "global" bound and connected tables
  70. * and the listener socket's pending list and connected queue) ensures a
  71. * reference. When we defer work until process context and pass a socket as our
  72. * argument, we must ensure the reference count is increased to ensure the
  73. * socket isn't freed before the function is run; the deferred function will
  74. * then drop the reference.
  75. */
  76. #include <linux/types.h>
  77. #include <linux/bitops.h>
  78. #include <linux/cred.h>
  79. #include <linux/init.h>
  80. #include <linux/io.h>
  81. #include <linux/kernel.h>
  82. #include <linux/kmod.h>
  83. #include <linux/list.h>
  84. #include <linux/miscdevice.h>
  85. #include <linux/module.h>
  86. #include <linux/mutex.h>
  87. #include <linux/net.h>
  88. #include <linux/poll.h>
  89. #include <linux/skbuff.h>
  90. #include <linux/smp.h>
  91. #include <linux/socket.h>
  92. #include <linux/stddef.h>
  93. #include <linux/unistd.h>
  94. #include <linux/wait.h>
  95. #include <linux/workqueue.h>
  96. #include <net/sock.h>
  97. #include <net/af_vsock.h>
  98. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr);
  99. static void vsock_sk_destruct(struct sock *sk);
  100. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
  101. /* Protocol family. */
  102. static struct proto vsock_proto = {
  103. .name = "AF_VSOCK",
  104. .owner = THIS_MODULE,
  105. .obj_size = sizeof(struct vsock_sock),
  106. };
  107. /* The default peer timeout indicates how long we will wait for a peer response
  108. * to a control message.
  109. */
  110. #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
  111. #define SS_LISTEN 255
  112. static const struct vsock_transport *transport;
  113. static DEFINE_MUTEX(vsock_register_mutex);
  114. /**** EXPORTS ****/
  115. /* Get the ID of the local context. This is transport dependent. */
  116. int vm_sockets_get_local_cid(void)
  117. {
  118. return transport->get_local_cid();
  119. }
  120. EXPORT_SYMBOL_GPL(vm_sockets_get_local_cid);
  121. /**** UTILS ****/
  122. /* Each bound VSocket is stored in the bind hash table and each connected
  123. * VSocket is stored in the connected hash table.
  124. *
  125. * Unbound sockets are all put on the same list attached to the end of the hash
  126. * table (vsock_unbound_sockets). Bound sockets are added to the hash table in
  127. * the bucket that their local address hashes to (vsock_bound_sockets(addr)
  128. * represents the list that addr hashes to).
  129. *
  130. * Specifically, we initialize the vsock_bind_table array to a size of
  131. * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through
  132. * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and
  133. * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function
  134. * mods with VSOCK_HASH_SIZE to ensure this.
  135. */
  136. #define VSOCK_HASH_SIZE 251
  137. #define MAX_PORT_RETRIES 24
  138. #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
  139. #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
  140. #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE])
  141. /* XXX This can probably be implemented in a better way. */
  142. #define VSOCK_CONN_HASH(src, dst) \
  143. (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
  144. #define vsock_connected_sockets(src, dst) \
  145. (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
  146. #define vsock_connected_sockets_vsk(vsk) \
  147. vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
  148. static struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
  149. static struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
  150. static DEFINE_SPINLOCK(vsock_table_lock);
  151. /* Autobind this socket to the local address if necessary. */
  152. static int vsock_auto_bind(struct vsock_sock *vsk)
  153. {
  154. struct sock *sk = sk_vsock(vsk);
  155. struct sockaddr_vm local_addr;
  156. if (vsock_addr_bound(&vsk->local_addr))
  157. return 0;
  158. vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  159. return __vsock_bind(sk, &local_addr);
  160. }
  161. static void vsock_init_tables(void)
  162. {
  163. int i;
  164. for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++)
  165. INIT_LIST_HEAD(&vsock_bind_table[i]);
  166. for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++)
  167. INIT_LIST_HEAD(&vsock_connected_table[i]);
  168. }
  169. static void __vsock_insert_bound(struct list_head *list,
  170. struct vsock_sock *vsk)
  171. {
  172. sock_hold(&vsk->sk);
  173. list_add(&vsk->bound_table, list);
  174. }
  175. static void __vsock_insert_connected(struct list_head *list,
  176. struct vsock_sock *vsk)
  177. {
  178. sock_hold(&vsk->sk);
  179. list_add(&vsk->connected_table, list);
  180. }
  181. static void __vsock_remove_bound(struct vsock_sock *vsk)
  182. {
  183. list_del_init(&vsk->bound_table);
  184. sock_put(&vsk->sk);
  185. }
  186. static void __vsock_remove_connected(struct vsock_sock *vsk)
  187. {
  188. list_del_init(&vsk->connected_table);
  189. sock_put(&vsk->sk);
  190. }
  191. static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
  192. {
  193. struct vsock_sock *vsk;
  194. list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table)
  195. if (addr->svm_port == vsk->local_addr.svm_port)
  196. return sk_vsock(vsk);
  197. return NULL;
  198. }
  199. static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
  200. struct sockaddr_vm *dst)
  201. {
  202. struct vsock_sock *vsk;
  203. list_for_each_entry(vsk, vsock_connected_sockets(src, dst),
  204. connected_table) {
  205. if (vsock_addr_equals_addr(src, &vsk->remote_addr) &&
  206. dst->svm_port == vsk->local_addr.svm_port) {
  207. return sk_vsock(vsk);
  208. }
  209. }
  210. return NULL;
  211. }
  212. static bool __vsock_in_bound_table(struct vsock_sock *vsk)
  213. {
  214. return !list_empty(&vsk->bound_table);
  215. }
  216. static bool __vsock_in_connected_table(struct vsock_sock *vsk)
  217. {
  218. return !list_empty(&vsk->connected_table);
  219. }
  220. static void vsock_insert_unbound(struct vsock_sock *vsk)
  221. {
  222. spin_lock_bh(&vsock_table_lock);
  223. __vsock_insert_bound(vsock_unbound_sockets, vsk);
  224. spin_unlock_bh(&vsock_table_lock);
  225. }
  226. void vsock_insert_connected(struct vsock_sock *vsk)
  227. {
  228. struct list_head *list = vsock_connected_sockets(
  229. &vsk->remote_addr, &vsk->local_addr);
  230. spin_lock_bh(&vsock_table_lock);
  231. __vsock_insert_connected(list, vsk);
  232. spin_unlock_bh(&vsock_table_lock);
  233. }
  234. EXPORT_SYMBOL_GPL(vsock_insert_connected);
  235. void vsock_remove_bound(struct vsock_sock *vsk)
  236. {
  237. spin_lock_bh(&vsock_table_lock);
  238. __vsock_remove_bound(vsk);
  239. spin_unlock_bh(&vsock_table_lock);
  240. }
  241. EXPORT_SYMBOL_GPL(vsock_remove_bound);
  242. void vsock_remove_connected(struct vsock_sock *vsk)
  243. {
  244. spin_lock_bh(&vsock_table_lock);
  245. __vsock_remove_connected(vsk);
  246. spin_unlock_bh(&vsock_table_lock);
  247. }
  248. EXPORT_SYMBOL_GPL(vsock_remove_connected);
  249. struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
  250. {
  251. struct sock *sk;
  252. spin_lock_bh(&vsock_table_lock);
  253. sk = __vsock_find_bound_socket(addr);
  254. if (sk)
  255. sock_hold(sk);
  256. spin_unlock_bh(&vsock_table_lock);
  257. return sk;
  258. }
  259. EXPORT_SYMBOL_GPL(vsock_find_bound_socket);
  260. struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
  261. struct sockaddr_vm *dst)
  262. {
  263. struct sock *sk;
  264. spin_lock_bh(&vsock_table_lock);
  265. sk = __vsock_find_connected_socket(src, dst);
  266. if (sk)
  267. sock_hold(sk);
  268. spin_unlock_bh(&vsock_table_lock);
  269. return sk;
  270. }
  271. EXPORT_SYMBOL_GPL(vsock_find_connected_socket);
  272. static bool vsock_in_bound_table(struct vsock_sock *vsk)
  273. {
  274. bool ret;
  275. spin_lock_bh(&vsock_table_lock);
  276. ret = __vsock_in_bound_table(vsk);
  277. spin_unlock_bh(&vsock_table_lock);
  278. return ret;
  279. }
  280. static bool vsock_in_connected_table(struct vsock_sock *vsk)
  281. {
  282. bool ret;
  283. spin_lock_bh(&vsock_table_lock);
  284. ret = __vsock_in_connected_table(vsk);
  285. spin_unlock_bh(&vsock_table_lock);
  286. return ret;
  287. }
  288. void vsock_for_each_connected_socket(void (*fn)(struct sock *sk))
  289. {
  290. int i;
  291. spin_lock_bh(&vsock_table_lock);
  292. for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
  293. struct vsock_sock *vsk;
  294. list_for_each_entry(vsk, &vsock_connected_table[i],
  295. connected_table)
  296. fn(sk_vsock(vsk));
  297. }
  298. spin_unlock_bh(&vsock_table_lock);
  299. }
  300. EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket);
  301. void vsock_add_pending(struct sock *listener, struct sock *pending)
  302. {
  303. struct vsock_sock *vlistener;
  304. struct vsock_sock *vpending;
  305. vlistener = vsock_sk(listener);
  306. vpending = vsock_sk(pending);
  307. sock_hold(pending);
  308. sock_hold(listener);
  309. list_add_tail(&vpending->pending_links, &vlistener->pending_links);
  310. }
  311. EXPORT_SYMBOL_GPL(vsock_add_pending);
  312. void vsock_remove_pending(struct sock *listener, struct sock *pending)
  313. {
  314. struct vsock_sock *vpending = vsock_sk(pending);
  315. list_del_init(&vpending->pending_links);
  316. sock_put(listener);
  317. sock_put(pending);
  318. }
  319. EXPORT_SYMBOL_GPL(vsock_remove_pending);
  320. void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
  321. {
  322. struct vsock_sock *vlistener;
  323. struct vsock_sock *vconnected;
  324. vlistener = vsock_sk(listener);
  325. vconnected = vsock_sk(connected);
  326. sock_hold(connected);
  327. sock_hold(listener);
  328. list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue);
  329. }
  330. EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
  331. static struct sock *vsock_dequeue_accept(struct sock *listener)
  332. {
  333. struct vsock_sock *vlistener;
  334. struct vsock_sock *vconnected;
  335. vlistener = vsock_sk(listener);
  336. if (list_empty(&vlistener->accept_queue))
  337. return NULL;
  338. vconnected = list_entry(vlistener->accept_queue.next,
  339. struct vsock_sock, accept_queue);
  340. list_del_init(&vconnected->accept_queue);
  341. sock_put(listener);
  342. /* The caller will need a reference on the connected socket so we let
  343. * it call sock_put().
  344. */
  345. return sk_vsock(vconnected);
  346. }
  347. static bool vsock_is_accept_queue_empty(struct sock *sk)
  348. {
  349. struct vsock_sock *vsk = vsock_sk(sk);
  350. return list_empty(&vsk->accept_queue);
  351. }
  352. static bool vsock_is_pending(struct sock *sk)
  353. {
  354. struct vsock_sock *vsk = vsock_sk(sk);
  355. return !list_empty(&vsk->pending_links);
  356. }
  357. static int vsock_send_shutdown(struct sock *sk, int mode)
  358. {
  359. return transport->shutdown(vsock_sk(sk), mode);
  360. }
  361. void vsock_pending_work(struct work_struct *work)
  362. {
  363. struct sock *sk;
  364. struct sock *listener;
  365. struct vsock_sock *vsk;
  366. bool cleanup;
  367. vsk = container_of(work, struct vsock_sock, dwork.work);
  368. sk = sk_vsock(vsk);
  369. listener = vsk->listener;
  370. cleanup = true;
  371. lock_sock(listener);
  372. lock_sock(sk);
  373. if (vsock_is_pending(sk)) {
  374. vsock_remove_pending(listener, sk);
  375. } else if (!vsk->rejected) {
  376. /* We are not on the pending list and accept() did not reject
  377. * us, so we must have been accepted by our user process. We
  378. * just need to drop our references to the sockets and be on
  379. * our way.
  380. */
  381. cleanup = false;
  382. goto out;
  383. }
  384. listener->sk_ack_backlog--;
  385. /* We need to remove ourself from the global connected sockets list so
  386. * incoming packets can't find this socket, and to reduce the reference
  387. * count.
  388. */
  389. if (vsock_in_connected_table(vsk))
  390. vsock_remove_connected(vsk);
  391. sk->sk_state = SS_FREE;
  392. out:
  393. release_sock(sk);
  394. release_sock(listener);
  395. if (cleanup)
  396. sock_put(sk);
  397. sock_put(sk);
  398. sock_put(listener);
  399. }
  400. EXPORT_SYMBOL_GPL(vsock_pending_work);
  401. /**** SOCKET OPERATIONS ****/
  402. static int __vsock_bind_stream(struct vsock_sock *vsk,
  403. struct sockaddr_vm *addr)
  404. {
  405. static u32 port = LAST_RESERVED_PORT + 1;
  406. struct sockaddr_vm new_addr;
  407. vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
  408. if (addr->svm_port == VMADDR_PORT_ANY) {
  409. bool found = false;
  410. unsigned int i;
  411. for (i = 0; i < MAX_PORT_RETRIES; i++) {
  412. if (port <= LAST_RESERVED_PORT)
  413. port = LAST_RESERVED_PORT + 1;
  414. new_addr.svm_port = port++;
  415. if (!__vsock_find_bound_socket(&new_addr)) {
  416. found = true;
  417. break;
  418. }
  419. }
  420. if (!found)
  421. return -EADDRNOTAVAIL;
  422. } else {
  423. /* If port is in reserved range, ensure caller
  424. * has necessary privileges.
  425. */
  426. if (addr->svm_port <= LAST_RESERVED_PORT &&
  427. !capable(CAP_NET_BIND_SERVICE)) {
  428. return -EACCES;
  429. }
  430. if (__vsock_find_bound_socket(&new_addr))
  431. return -EADDRINUSE;
  432. }
  433. vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);
  434. /* Remove stream sockets from the unbound list and add them to the hash
  435. * table for easy lookup by its address. The unbound list is simply an
  436. * extra entry at the end of the hash table, a trick used by AF_UNIX.
  437. */
  438. __vsock_remove_bound(vsk);
  439. __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
  440. return 0;
  441. }
  442. static int __vsock_bind_dgram(struct vsock_sock *vsk,
  443. struct sockaddr_vm *addr)
  444. {
  445. return transport->dgram_bind(vsk, addr);
  446. }
  447. static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
  448. {
  449. struct vsock_sock *vsk = vsock_sk(sk);
  450. u32 cid;
  451. int retval;
  452. /* First ensure this socket isn't already bound. */
  453. if (vsock_addr_bound(&vsk->local_addr))
  454. return -EINVAL;
  455. /* Now bind to the provided address or select appropriate values if
  456. * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that
  457. * like AF_INET prevents binding to a non-local IP address (in most
  458. * cases), we only allow binding to the local CID.
  459. */
  460. cid = transport->get_local_cid();
  461. if (addr->svm_cid != cid && addr->svm_cid != VMADDR_CID_ANY)
  462. return -EADDRNOTAVAIL;
  463. switch (sk->sk_socket->type) {
  464. case SOCK_STREAM:
  465. spin_lock_bh(&vsock_table_lock);
  466. retval = __vsock_bind_stream(vsk, addr);
  467. spin_unlock_bh(&vsock_table_lock);
  468. break;
  469. case SOCK_DGRAM:
  470. retval = __vsock_bind_dgram(vsk, addr);
  471. break;
  472. default:
  473. retval = -EINVAL;
  474. break;
  475. }
  476. return retval;
  477. }
  478. struct sock *__vsock_create(struct net *net,
  479. struct socket *sock,
  480. struct sock *parent,
  481. gfp_t priority,
  482. unsigned short type,
  483. 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. psk = parent ? vsock_sk(parent) : NULL;
  515. if (parent) {
  516. vsk->trusted = psk->trusted;
  517. vsk->owner = get_cred(psk->owner);
  518. vsk->connect_timeout = psk->connect_timeout;
  519. } else {
  520. vsk->trusted = capable(CAP_NET_ADMIN);
  521. vsk->owner = get_current_cred();
  522. vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT;
  523. }
  524. if (transport->init(vsk, psk) < 0) {
  525. sk_free(sk);
  526. return NULL;
  527. }
  528. if (sock)
  529. vsock_insert_unbound(vsk);
  530. return sk;
  531. }
  532. EXPORT_SYMBOL_GPL(__vsock_create);
  533. static void __vsock_release(struct sock *sk)
  534. {
  535. if (sk) {
  536. struct sk_buff *skb;
  537. struct sock *pending;
  538. struct vsock_sock *vsk;
  539. vsk = vsock_sk(sk);
  540. pending = NULL; /* Compiler warning. */
  541. if (vsock_in_bound_table(vsk))
  542. vsock_remove_bound(vsk);
  543. if (vsock_in_connected_table(vsk))
  544. vsock_remove_connected(vsk);
  545. transport->release(vsk);
  546. lock_sock(sk);
  547. sock_orphan(sk);
  548. sk->sk_shutdown = SHUTDOWN_MASK;
  549. while ((skb = skb_dequeue(&sk->sk_receive_queue)))
  550. kfree_skb(skb);
  551. /* Clean up any sockets that never were accepted. */
  552. while ((pending = vsock_dequeue_accept(sk)) != NULL) {
  553. __vsock_release(pending);
  554. sock_put(pending);
  555. }
  556. release_sock(sk);
  557. sock_put(sk);
  558. }
  559. }
  560. static void vsock_sk_destruct(struct sock *sk)
  561. {
  562. struct vsock_sock *vsk = vsock_sk(sk);
  563. transport->destruct(vsk);
  564. /* When clearing these addresses, there's no need to set the family and
  565. * possibly register the address family with the kernel.
  566. */
  567. vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  568. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
  569. put_cred(vsk->owner);
  570. }
  571. static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  572. {
  573. int err;
  574. err = sock_queue_rcv_skb(sk, skb);
  575. if (err)
  576. kfree_skb(skb);
  577. return err;
  578. }
  579. s64 vsock_stream_has_data(struct vsock_sock *vsk)
  580. {
  581. return transport->stream_has_data(vsk);
  582. }
  583. EXPORT_SYMBOL_GPL(vsock_stream_has_data);
  584. s64 vsock_stream_has_space(struct vsock_sock *vsk)
  585. {
  586. return transport->stream_has_space(vsk);
  587. }
  588. EXPORT_SYMBOL_GPL(vsock_stream_has_space);
  589. static int vsock_release(struct socket *sock)
  590. {
  591. __vsock_release(sock->sk);
  592. sock->sk = NULL;
  593. sock->state = SS_FREE;
  594. return 0;
  595. }
  596. static int
  597. vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  598. {
  599. int err;
  600. struct sock *sk;
  601. struct sockaddr_vm *vm_addr;
  602. sk = sock->sk;
  603. if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0)
  604. return -EINVAL;
  605. lock_sock(sk);
  606. err = __vsock_bind(sk, vm_addr);
  607. release_sock(sk);
  608. return err;
  609. }
  610. static int vsock_getname(struct socket *sock,
  611. struct sockaddr *addr, int *addr_len, int peer)
  612. {
  613. int err;
  614. struct sock *sk;
  615. struct vsock_sock *vsk;
  616. struct sockaddr_vm *vm_addr;
  617. sk = sock->sk;
  618. vsk = vsock_sk(sk);
  619. err = 0;
  620. lock_sock(sk);
  621. if (peer) {
  622. if (sock->state != SS_CONNECTED) {
  623. err = -ENOTCONN;
  624. goto out;
  625. }
  626. vm_addr = &vsk->remote_addr;
  627. } else {
  628. vm_addr = &vsk->local_addr;
  629. }
  630. if (!vm_addr) {
  631. err = -EINVAL;
  632. goto out;
  633. }
  634. /* sys_getsockname() and sys_getpeername() pass us a
  635. * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately
  636. * that macro is defined in socket.c instead of .h, so we hardcode its
  637. * value here.
  638. */
  639. BUILD_BUG_ON(sizeof(*vm_addr) > 128);
  640. memcpy(addr, vm_addr, sizeof(*vm_addr));
  641. *addr_len = sizeof(*vm_addr);
  642. out:
  643. release_sock(sk);
  644. return err;
  645. }
  646. static int vsock_shutdown(struct socket *sock, int mode)
  647. {
  648. int err;
  649. struct sock *sk;
  650. /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
  651. * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
  652. * here like the other address families do. Note also that the
  653. * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
  654. * which is what we want.
  655. */
  656. mode++;
  657. if ((mode & ~SHUTDOWN_MASK) || !mode)
  658. return -EINVAL;
  659. /* If this is a STREAM socket and it is not connected then bail out
  660. * immediately. If it is a DGRAM socket then we must first kick the
  661. * socket so that it wakes up from any sleeping calls, for example
  662. * recv(), and then afterwards return the error.
  663. */
  664. sk = sock->sk;
  665. if (sock->state == SS_UNCONNECTED) {
  666. err = -ENOTCONN;
  667. if (sk->sk_type == SOCK_STREAM)
  668. return err;
  669. } else {
  670. sock->state = SS_DISCONNECTING;
  671. err = 0;
  672. }
  673. /* Receive and send shutdowns are treated alike. */
  674. mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
  675. if (mode) {
  676. lock_sock(sk);
  677. sk->sk_shutdown |= mode;
  678. sk->sk_state_change(sk);
  679. release_sock(sk);
  680. if (sk->sk_type == SOCK_STREAM) {
  681. sock_reset_flag(sk, SOCK_DONE);
  682. vsock_send_shutdown(sk, mode);
  683. }
  684. }
  685. return err;
  686. }
  687. static unsigned int vsock_poll(struct file *file, struct socket *sock,
  688. poll_table *wait)
  689. {
  690. struct sock *sk;
  691. unsigned int mask;
  692. struct vsock_sock *vsk;
  693. sk = sock->sk;
  694. vsk = vsock_sk(sk);
  695. poll_wait(file, sk_sleep(sk), wait);
  696. mask = 0;
  697. if (sk->sk_err)
  698. /* Signify that there has been an error on this socket. */
  699. mask |= POLLERR;
  700. /* INET sockets treat local write shutdown and peer write shutdown as a
  701. * case of POLLHUP set.
  702. */
  703. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  704. ((sk->sk_shutdown & SEND_SHUTDOWN) &&
  705. (vsk->peer_shutdown & SEND_SHUTDOWN))) {
  706. mask |= POLLHUP;
  707. }
  708. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  709. vsk->peer_shutdown & SEND_SHUTDOWN) {
  710. mask |= POLLRDHUP;
  711. }
  712. if (sock->type == SOCK_DGRAM) {
  713. /* For datagram sockets we can read if there is something in
  714. * the queue and write as long as the socket isn't shutdown for
  715. * sending.
  716. */
  717. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  718. (sk->sk_shutdown & RCV_SHUTDOWN)) {
  719. mask |= POLLIN | POLLRDNORM;
  720. }
  721. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  722. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  723. } else if (sock->type == SOCK_STREAM) {
  724. lock_sock(sk);
  725. /* Listening sockets that have connections in their accept
  726. * queue can be read.
  727. */
  728. if (sk->sk_state == SS_LISTEN
  729. && !vsock_is_accept_queue_empty(sk))
  730. mask |= POLLIN | POLLRDNORM;
  731. /* If there is something in the queue then we can read. */
  732. if (transport->stream_is_active(vsk) &&
  733. !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  734. bool data_ready_now = false;
  735. int ret = transport->notify_poll_in(
  736. vsk, 1, &data_ready_now);
  737. if (ret < 0) {
  738. mask |= POLLERR;
  739. } else {
  740. if (data_ready_now)
  741. mask |= POLLIN | POLLRDNORM;
  742. }
  743. }
  744. /* Sockets whose connections have been closed, reset, or
  745. * terminated should also be considered read, and we check the
  746. * shutdown flag for that.
  747. */
  748. if (sk->sk_shutdown & RCV_SHUTDOWN ||
  749. vsk->peer_shutdown & SEND_SHUTDOWN) {
  750. mask |= POLLIN | POLLRDNORM;
  751. }
  752. /* Connected sockets that can produce data can be written. */
  753. if (sk->sk_state == SS_CONNECTED) {
  754. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  755. bool space_avail_now = false;
  756. int ret = transport->notify_poll_out(
  757. vsk, 1, &space_avail_now);
  758. if (ret < 0) {
  759. mask |= POLLERR;
  760. } else {
  761. if (space_avail_now)
  762. /* Remove POLLWRBAND since INET
  763. * sockets are not setting it.
  764. */
  765. mask |= POLLOUT | POLLWRNORM;
  766. }
  767. }
  768. }
  769. /* Simulate INET socket poll behaviors, which sets
  770. * POLLOUT|POLLWRNORM when peer is closed and nothing to read,
  771. * but local send is not shutdown.
  772. */
  773. if (sk->sk_state == SS_UNCONNECTED) {
  774. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  775. mask |= POLLOUT | POLLWRNORM;
  776. }
  777. release_sock(sk);
  778. }
  779. return mask;
  780. }
  781. static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
  782. size_t len)
  783. {
  784. int err;
  785. struct sock *sk;
  786. struct vsock_sock *vsk;
  787. struct sockaddr_vm *remote_addr;
  788. if (msg->msg_flags & MSG_OOB)
  789. return -EOPNOTSUPP;
  790. /* For now, MSG_DONTWAIT is always assumed... */
  791. err = 0;
  792. sk = sock->sk;
  793. vsk = vsock_sk(sk);
  794. lock_sock(sk);
  795. err = vsock_auto_bind(vsk);
  796. if (err)
  797. goto out;
  798. /* If the provided message contains an address, use that. Otherwise
  799. * fall back on the socket's remote handle (if it has been connected).
  800. */
  801. if (msg->msg_name &&
  802. vsock_addr_cast(msg->msg_name, msg->msg_namelen,
  803. &remote_addr) == 0) {
  804. /* Ensure this address is of the right type and is a valid
  805. * destination.
  806. */
  807. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  808. remote_addr->svm_cid = transport->get_local_cid();
  809. if (!vsock_addr_bound(remote_addr)) {
  810. err = -EINVAL;
  811. goto out;
  812. }
  813. } else if (sock->state == SS_CONNECTED) {
  814. remote_addr = &vsk->remote_addr;
  815. if (remote_addr->svm_cid == VMADDR_CID_ANY)
  816. remote_addr->svm_cid = transport->get_local_cid();
  817. /* XXX Should connect() or this function ensure remote_addr is
  818. * bound?
  819. */
  820. if (!vsock_addr_bound(&vsk->remote_addr)) {
  821. err = -EINVAL;
  822. goto out;
  823. }
  824. } else {
  825. err = -EINVAL;
  826. goto out;
  827. }
  828. if (!transport->dgram_allow(remote_addr->svm_cid,
  829. remote_addr->svm_port)) {
  830. err = -EINVAL;
  831. goto out;
  832. }
  833. err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
  834. out:
  835. release_sock(sk);
  836. return err;
  837. }
  838. static int vsock_dgram_connect(struct socket *sock,
  839. struct sockaddr *addr, int addr_len, int flags)
  840. {
  841. int err;
  842. struct sock *sk;
  843. struct vsock_sock *vsk;
  844. struct sockaddr_vm *remote_addr;
  845. sk = sock->sk;
  846. vsk = vsock_sk(sk);
  847. err = vsock_addr_cast(addr, addr_len, &remote_addr);
  848. if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) {
  849. lock_sock(sk);
  850. vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY,
  851. VMADDR_PORT_ANY);
  852. sock->state = SS_UNCONNECTED;
  853. release_sock(sk);
  854. return 0;
  855. } else if (err != 0)
  856. return -EINVAL;
  857. lock_sock(sk);
  858. err = vsock_auto_bind(vsk);
  859. if (err)
  860. goto out;
  861. if (!transport->dgram_allow(remote_addr->svm_cid,
  862. remote_addr->svm_port)) {
  863. err = -EINVAL;
  864. goto out;
  865. }
  866. memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr));
  867. sock->state = SS_CONNECTED;
  868. out:
  869. release_sock(sk);
  870. return err;
  871. }
  872. static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
  873. size_t len, int flags)
  874. {
  875. return transport->dgram_dequeue(vsock_sk(sock->sk), msg, len, 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 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(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,
  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 socket *sock, struct msghdr *msg, size_t len,
  1352. int flags)
  1353. {
  1354. struct sock *sk;
  1355. struct vsock_sock *vsk;
  1356. int err;
  1357. size_t target;
  1358. ssize_t copied;
  1359. long timeout;
  1360. struct vsock_transport_recv_notify_data recv_data;
  1361. DEFINE_WAIT(wait);
  1362. sk = sock->sk;
  1363. vsk = vsock_sk(sk);
  1364. err = 0;
  1365. lock_sock(sk);
  1366. if (sk->sk_state != SS_CONNECTED) {
  1367. /* Recvmsg is supposed to return 0 if a peer performs an
  1368. * orderly shutdown. Differentiate between that case and when a
  1369. * peer has not connected or a local shutdown occured with the
  1370. * SOCK_DONE flag.
  1371. */
  1372. if (sock_flag(sk, SOCK_DONE))
  1373. err = 0;
  1374. else
  1375. err = -ENOTCONN;
  1376. goto out;
  1377. }
  1378. if (flags & MSG_OOB) {
  1379. err = -EOPNOTSUPP;
  1380. goto out;
  1381. }
  1382. /* We don't check peer_shutdown flag here since peer may actually shut
  1383. * down, but there can be data in the queue that a local socket can
  1384. * receive.
  1385. */
  1386. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1387. err = 0;
  1388. goto out;
  1389. }
  1390. /* It is valid on Linux to pass in a zero-length receive buffer. This
  1391. * is not an error. We may as well bail out now.
  1392. */
  1393. if (!len) {
  1394. err = 0;
  1395. goto out;
  1396. }
  1397. /* We must not copy less than target bytes into the user's buffer
  1398. * before returning successfully, so we wait for the consume queue to
  1399. * have that much data to consume before dequeueing. Note that this
  1400. * makes it impossible to handle cases where target is greater than the
  1401. * queue size.
  1402. */
  1403. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1404. if (target >= transport->stream_rcvhiwat(vsk)) {
  1405. err = -ENOMEM;
  1406. goto out;
  1407. }
  1408. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1409. copied = 0;
  1410. err = transport->notify_recv_init(vsk, target, &recv_data);
  1411. if (err < 0)
  1412. goto out;
  1413. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1414. while (1) {
  1415. s64 ready = vsock_stream_has_data(vsk);
  1416. if (ready < 0) {
  1417. /* Invalid queue pair content. XXX This should be
  1418. * changed to a connection reset in a later change.
  1419. */
  1420. err = -ENOMEM;
  1421. goto out_wait;
  1422. } else if (ready > 0) {
  1423. ssize_t read;
  1424. err = transport->notify_recv_pre_dequeue(
  1425. vsk, target, &recv_data);
  1426. if (err < 0)
  1427. break;
  1428. read = transport->stream_dequeue(
  1429. vsk, msg,
  1430. len - copied, flags);
  1431. if (read < 0) {
  1432. err = -ENOMEM;
  1433. break;
  1434. }
  1435. copied += read;
  1436. err = transport->notify_recv_post_dequeue(
  1437. vsk, target, read,
  1438. !(flags & MSG_PEEK), &recv_data);
  1439. if (err < 0)
  1440. goto out_wait;
  1441. if (read >= target || flags & MSG_PEEK)
  1442. break;
  1443. target -= read;
  1444. } else {
  1445. if (sk->sk_err != 0 || (sk->sk_shutdown & RCV_SHUTDOWN)
  1446. || (vsk->peer_shutdown & SEND_SHUTDOWN)) {
  1447. break;
  1448. }
  1449. /* Don't wait for non-blocking sockets. */
  1450. if (timeout == 0) {
  1451. err = -EAGAIN;
  1452. break;
  1453. }
  1454. err = transport->notify_recv_pre_block(
  1455. vsk, target, &recv_data);
  1456. if (err < 0)
  1457. break;
  1458. release_sock(sk);
  1459. timeout = schedule_timeout(timeout);
  1460. lock_sock(sk);
  1461. if (signal_pending(current)) {
  1462. err = sock_intr_errno(timeout);
  1463. break;
  1464. } else if (timeout == 0) {
  1465. err = -EAGAIN;
  1466. break;
  1467. }
  1468. prepare_to_wait(sk_sleep(sk), &wait,
  1469. TASK_INTERRUPTIBLE);
  1470. }
  1471. }
  1472. if (sk->sk_err)
  1473. err = -sk->sk_err;
  1474. else if (sk->sk_shutdown & RCV_SHUTDOWN)
  1475. err = 0;
  1476. if (copied > 0) {
  1477. /* We only do these additional bookkeeping/notification steps
  1478. * if we actually copied something out of the queue pair
  1479. * instead of just peeking ahead.
  1480. */
  1481. if (!(flags & MSG_PEEK)) {
  1482. /* If the other side has shutdown for sending and there
  1483. * is nothing more to read, then modify the socket
  1484. * state.
  1485. */
  1486. if (vsk->peer_shutdown & SEND_SHUTDOWN) {
  1487. if (vsock_stream_has_data(vsk) <= 0) {
  1488. sk->sk_state = SS_UNCONNECTED;
  1489. sock_set_flag(sk, SOCK_DONE);
  1490. sk->sk_state_change(sk);
  1491. }
  1492. }
  1493. }
  1494. err = copied;
  1495. }
  1496. out_wait:
  1497. finish_wait(sk_sleep(sk), &wait);
  1498. out:
  1499. release_sock(sk);
  1500. return err;
  1501. }
  1502. static const struct proto_ops vsock_stream_ops = {
  1503. .family = PF_VSOCK,
  1504. .owner = THIS_MODULE,
  1505. .release = vsock_release,
  1506. .bind = vsock_bind,
  1507. .connect = vsock_stream_connect,
  1508. .socketpair = sock_no_socketpair,
  1509. .accept = vsock_accept,
  1510. .getname = vsock_getname,
  1511. .poll = vsock_poll,
  1512. .ioctl = sock_no_ioctl,
  1513. .listen = vsock_listen,
  1514. .shutdown = vsock_shutdown,
  1515. .setsockopt = vsock_stream_setsockopt,
  1516. .getsockopt = vsock_stream_getsockopt,
  1517. .sendmsg = vsock_stream_sendmsg,
  1518. .recvmsg = vsock_stream_recvmsg,
  1519. .mmap = sock_no_mmap,
  1520. .sendpage = sock_no_sendpage,
  1521. };
  1522. static int vsock_create(struct net *net, struct socket *sock,
  1523. int protocol, int kern)
  1524. {
  1525. if (!sock)
  1526. return -EINVAL;
  1527. if (protocol && protocol != PF_VSOCK)
  1528. return -EPROTONOSUPPORT;
  1529. switch (sock->type) {
  1530. case SOCK_DGRAM:
  1531. sock->ops = &vsock_dgram_ops;
  1532. break;
  1533. case SOCK_STREAM:
  1534. sock->ops = &vsock_stream_ops;
  1535. break;
  1536. default:
  1537. return -ESOCKTNOSUPPORT;
  1538. }
  1539. sock->state = SS_UNCONNECTED;
  1540. return __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern) ? 0 : -ENOMEM;
  1541. }
  1542. static const struct net_proto_family vsock_family_ops = {
  1543. .family = AF_VSOCK,
  1544. .create = vsock_create,
  1545. .owner = THIS_MODULE,
  1546. };
  1547. static long vsock_dev_do_ioctl(struct file *filp,
  1548. unsigned int cmd, void __user *ptr)
  1549. {
  1550. u32 __user *p = ptr;
  1551. int retval = 0;
  1552. switch (cmd) {
  1553. case IOCTL_VM_SOCKETS_GET_LOCAL_CID:
  1554. if (put_user(transport->get_local_cid(), p) != 0)
  1555. retval = -EFAULT;
  1556. break;
  1557. default:
  1558. pr_err("Unknown ioctl %d\n", cmd);
  1559. retval = -EINVAL;
  1560. }
  1561. return retval;
  1562. }
  1563. static long vsock_dev_ioctl(struct file *filp,
  1564. unsigned int cmd, unsigned long arg)
  1565. {
  1566. return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg);
  1567. }
  1568. #ifdef CONFIG_COMPAT
  1569. static long vsock_dev_compat_ioctl(struct file *filp,
  1570. unsigned int cmd, unsigned long arg)
  1571. {
  1572. return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg));
  1573. }
  1574. #endif
  1575. static const struct file_operations vsock_device_ops = {
  1576. .owner = THIS_MODULE,
  1577. .unlocked_ioctl = vsock_dev_ioctl,
  1578. #ifdef CONFIG_COMPAT
  1579. .compat_ioctl = vsock_dev_compat_ioctl,
  1580. #endif
  1581. .open = nonseekable_open,
  1582. };
  1583. static struct miscdevice vsock_device = {
  1584. .name = "vsock",
  1585. .fops = &vsock_device_ops,
  1586. };
  1587. int __vsock_core_init(const struct vsock_transport *t, struct module *owner)
  1588. {
  1589. int err = mutex_lock_interruptible(&vsock_register_mutex);
  1590. if (err)
  1591. return err;
  1592. if (transport) {
  1593. err = -EBUSY;
  1594. goto err_busy;
  1595. }
  1596. /* Transport must be the owner of the protocol so that it can't
  1597. * unload while there are open sockets.
  1598. */
  1599. vsock_proto.owner = owner;
  1600. transport = t;
  1601. vsock_init_tables();
  1602. vsock_device.minor = MISC_DYNAMIC_MINOR;
  1603. err = misc_register(&vsock_device);
  1604. if (err) {
  1605. pr_err("Failed to register misc device\n");
  1606. goto err_reset_transport;
  1607. }
  1608. err = proto_register(&vsock_proto, 1); /* we want our slab */
  1609. if (err) {
  1610. pr_err("Cannot register vsock protocol\n");
  1611. goto err_deregister_misc;
  1612. }
  1613. err = sock_register(&vsock_family_ops);
  1614. if (err) {
  1615. pr_err("could not register af_vsock (%d) address family: %d\n",
  1616. AF_VSOCK, err);
  1617. goto err_unregister_proto;
  1618. }
  1619. mutex_unlock(&vsock_register_mutex);
  1620. return 0;
  1621. err_unregister_proto:
  1622. proto_unregister(&vsock_proto);
  1623. err_deregister_misc:
  1624. misc_deregister(&vsock_device);
  1625. err_reset_transport:
  1626. transport = NULL;
  1627. err_busy:
  1628. mutex_unlock(&vsock_register_mutex);
  1629. return err;
  1630. }
  1631. EXPORT_SYMBOL_GPL(__vsock_core_init);
  1632. void vsock_core_exit(void)
  1633. {
  1634. mutex_lock(&vsock_register_mutex);
  1635. misc_deregister(&vsock_device);
  1636. sock_unregister(AF_VSOCK);
  1637. proto_unregister(&vsock_proto);
  1638. /* We do not want the assignment below re-ordered. */
  1639. mb();
  1640. transport = NULL;
  1641. mutex_unlock(&vsock_register_mutex);
  1642. }
  1643. EXPORT_SYMBOL_GPL(vsock_core_exit);
  1644. MODULE_AUTHOR("VMware, Inc.");
  1645. MODULE_DESCRIPTION("VMware Virtual Socket Family");
  1646. MODULE_VERSION("1.0.1.0-k");
  1647. MODULE_LICENSE("GPL v2");