af_vsock.c 47 KB

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