af_vsock.c 47 KB

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