blob: 3da0749a0c9774c23d82c77c9de978726af054bc [file] [log] [blame]
Thomas Gleixner685a6bf2019-05-29 16:57:36 -07001// SPDX-License-Identifier: GPL-2.0-only
Andy Kingd021c342013-02-06 14:23:56 +00002/*
3 * VMware vSockets Driver
4 *
5 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
Andy Kingd021c342013-02-06 14:23:56 +00006 */
7
8/* Implementation notes:
9 *
10 * - There are two kinds of sockets: those created by user action (such as
11 * calling socket(2)) and those created by incoming connection request packets.
12 *
13 * - There are two "global" tables, one for bound sockets (sockets that have
14 * specified an address that they are responsible for) and one for connected
15 * sockets (sockets that have established a connection with another socket).
16 * These tables are "global" in that all sockets on the system are placed
17 * within them. - Note, though, that the bound table contains an extra entry
18 * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in
19 * that list. The bound table is used solely for lookup of sockets when packets
20 * are received and that's not necessary for SOCK_DGRAM sockets since we create
21 * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM
22 * sockets out of the bound hash buckets will reduce the chance of collisions
23 * when looking for SOCK_STREAM sockets and prevents us from having to check the
24 * socket type in the hash table lookups.
25 *
26 * - Sockets created by user action will either be "client" sockets that
27 * initiate a connection or "server" sockets that listen for connections; we do
28 * not support simultaneous connects (two "client" sockets connecting).
29 *
30 * - "Server" sockets are referred to as listener sockets throughout this
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -040031 * implementation because they are in the TCP_LISTEN state. When a
Stefan Hajnocziea3803c2015-10-29 11:57:42 +000032 * connection request is received (the second kind of socket mentioned above),
33 * we create a new socket and refer to it as a pending socket. These pending
34 * sockets are placed on the pending connection list of the listener socket.
35 * When future packets are received for the address the listener socket is
36 * bound to, we check if the source of the packet is from one that has an
37 * existing pending connection. If it does, we process the packet for the
38 * pending socket. When that socket reaches the connected state, it is removed
39 * from the listener socket's pending list and enqueued in the listener
40 * socket's accept queue. Callers of accept(2) will accept connected sockets
41 * from the listener socket's accept queue. If the socket cannot be accepted
42 * for some reason then it is marked rejected. Once the connection is
43 * accepted, it is owned by the user process and the responsibility for cleanup
44 * falls with that user process.
Andy Kingd021c342013-02-06 14:23:56 +000045 *
46 * - It is possible that these pending sockets will never reach the connected
47 * state; in fact, we may never receive another packet after the connection
48 * request. Because of this, we must schedule a cleanup function to run in the
49 * future, after some amount of time passes where a connection should have been
50 * established. This function ensures that the socket is off all lists so it
51 * cannot be retrieved, then drops all references to the socket so it is cleaned
52 * up (sock_put() -> sk_free() -> our sk_destruct implementation). Note this
53 * function will also cleanup rejected sockets, those that reach the connected
54 * state but leave it before they have been accepted.
55 *
Stefan Hajnoczi4192f672016-06-23 16:28:58 +010056 * - Lock ordering for pending or accept queue sockets is:
57 *
58 * lock_sock(listener);
59 * lock_sock_nested(pending, SINGLE_DEPTH_NESTING);
60 *
61 * Using explicit nested locking keeps lockdep happy since normally only one
62 * lock of a given class may be taken at a time.
63 *
Andy Kingd021c342013-02-06 14:23:56 +000064 * - Sockets created by user action will be cleaned up when the user process
65 * calls close(2), causing our release implementation to be called. Our release
66 * implementation will perform some cleanup then drop the last reference so our
67 * sk_destruct implementation is invoked. Our sk_destruct implementation will
68 * perform additional cleanup that's common for both types of sockets.
69 *
70 * - A socket's reference count is what ensures that the structure won't be
71 * freed. Each entry in a list (such as the "global" bound and connected tables
72 * and the listener socket's pending list and connected queue) ensures a
73 * reference. When we defer work until process context and pass a socket as our
74 * argument, we must ensure the reference count is increased to ensure the
75 * socket isn't freed before the function is run; the deferred function will
76 * then drop the reference.
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -040077 *
78 * - sk->sk_state uses the TCP state constants because they are widely used by
79 * other address families and exposed to userspace tools like ss(8):
80 *
81 * TCP_CLOSE - unconnected
82 * TCP_SYN_SENT - connecting
83 * TCP_ESTABLISHED - connected
84 * TCP_CLOSING - disconnecting
85 * TCP_LISTEN - listening
Andy Kingd021c342013-02-06 14:23:56 +000086 */
87
88#include <linux/types.h>
Andy Kingd021c342013-02-06 14:23:56 +000089#include <linux/bitops.h>
90#include <linux/cred.h>
91#include <linux/init.h>
92#include <linux/io.h>
93#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010094#include <linux/sched/signal.h>
Andy Kingd021c342013-02-06 14:23:56 +000095#include <linux/kmod.h>
96#include <linux/list.h>
97#include <linux/miscdevice.h>
98#include <linux/module.h>
99#include <linux/mutex.h>
100#include <linux/net.h>
101#include <linux/poll.h>
Lepton Wu8236b082018-12-11 11:12:55 -0800102#include <linux/random.h>
Andy Kingd021c342013-02-06 14:23:56 +0000103#include <linux/skbuff.h>
104#include <linux/smp.h>
105#include <linux/socket.h>
106#include <linux/stddef.h>
107#include <linux/unistd.h>
108#include <linux/wait.h>
109#include <linux/workqueue.h>
110#include <net/sock.h>
Asias He82a54d02013-07-25 17:39:34 +0800111#include <net/af_vsock.h>
Andy Kingd021c342013-02-06 14:23:56 +0000112
113static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr);
114static void vsock_sk_destruct(struct sock *sk);
115static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
116
117/* Protocol family. */
118static struct proto vsock_proto = {
119 .name = "AF_VSOCK",
120 .owner = THIS_MODULE,
121 .obj_size = sizeof(struct vsock_sock),
122};
123
124/* The default peer timeout indicates how long we will wait for a peer response
125 * to a control message.
126 */
127#define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
128
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +0100129#define VSOCK_DEFAULT_BUFFER_SIZE (1024 * 256)
130#define VSOCK_DEFAULT_BUFFER_MAX_SIZE (1024 * 256)
131#define VSOCK_DEFAULT_BUFFER_MIN_SIZE 128
132
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100133/* Transport used for host->guest communication */
134static const struct vsock_transport *transport_h2g;
135/* Transport used for guest->host communication */
136static const struct vsock_transport *transport_g2h;
137/* Transport used for DGRAM communication */
138static const struct vsock_transport *transport_dgram;
Stefano Garzarella0e121902019-12-10 11:43:04 +0100139/* Transport used for local communication */
140static const struct vsock_transport *transport_local;
Andy Kingd021c342013-02-06 14:23:56 +0000141static DEFINE_MUTEX(vsock_register_mutex);
142
Andy Kingd021c342013-02-06 14:23:56 +0000143/**** UTILS ****/
144
145/* Each bound VSocket is stored in the bind hash table and each connected
146 * VSocket is stored in the connected hash table.
147 *
148 * Unbound sockets are all put on the same list attached to the end of the hash
149 * table (vsock_unbound_sockets). Bound sockets are added to the hash table in
150 * the bucket that their local address hashes to (vsock_bound_sockets(addr)
151 * represents the list that addr hashes to).
152 *
153 * Specifically, we initialize the vsock_bind_table array to a size of
154 * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through
155 * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and
156 * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function
Asias Hea49dd9d2013-06-20 17:20:33 +0800157 * mods with VSOCK_HASH_SIZE to ensure this.
Andy Kingd021c342013-02-06 14:23:56 +0000158 */
Andy Kingd021c342013-02-06 14:23:56 +0000159#define MAX_PORT_RETRIES 24
160
Asias Hea49dd9d2013-06-20 17:20:33 +0800161#define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
Andy Kingd021c342013-02-06 14:23:56 +0000162#define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
163#define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE])
164
165/* XXX This can probably be implemented in a better way. */
166#define VSOCK_CONN_HASH(src, dst) \
Asias Hea49dd9d2013-06-20 17:20:33 +0800167 (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
Andy Kingd021c342013-02-06 14:23:56 +0000168#define vsock_connected_sockets(src, dst) \
169 (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
170#define vsock_connected_sockets_vsk(vsk) \
171 vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
172
Stefan Hajnoczi44f20982017-10-05 16:46:50 -0400173struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
174EXPORT_SYMBOL_GPL(vsock_bind_table);
175struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
176EXPORT_SYMBOL_GPL(vsock_connected_table);
177DEFINE_SPINLOCK(vsock_table_lock);
178EXPORT_SYMBOL_GPL(vsock_table_lock);
Andy Kingd021c342013-02-06 14:23:56 +0000179
Asias Heb3a6dfe2013-06-20 17:20:30 +0800180/* Autobind this socket to the local address if necessary. */
181static int vsock_auto_bind(struct vsock_sock *vsk)
182{
183 struct sock *sk = sk_vsock(vsk);
184 struct sockaddr_vm local_addr;
185
186 if (vsock_addr_bound(&vsk->local_addr))
187 return 0;
188 vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
189 return __vsock_bind(sk, &local_addr);
190}
191
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100192static void vsock_init_tables(void)
Andy Kingd021c342013-02-06 14:23:56 +0000193{
194 int i;
195
196 for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++)
197 INIT_LIST_HEAD(&vsock_bind_table[i]);
198
199 for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++)
200 INIT_LIST_HEAD(&vsock_connected_table[i]);
201}
202
203static void __vsock_insert_bound(struct list_head *list,
204 struct vsock_sock *vsk)
205{
206 sock_hold(&vsk->sk);
207 list_add(&vsk->bound_table, list);
208}
209
210static void __vsock_insert_connected(struct list_head *list,
211 struct vsock_sock *vsk)
212{
213 sock_hold(&vsk->sk);
214 list_add(&vsk->connected_table, list);
215}
216
217static void __vsock_remove_bound(struct vsock_sock *vsk)
218{
219 list_del_init(&vsk->bound_table);
220 sock_put(&vsk->sk);
221}
222
223static void __vsock_remove_connected(struct vsock_sock *vsk)
224{
225 list_del_init(&vsk->connected_table);
226 sock_put(&vsk->sk);
227}
228
229static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
230{
231 struct vsock_sock *vsk;
232
Stefano Garzarella36c5b482019-11-14 10:57:49 +0100233 list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
234 if (vsock_addr_equals_addr(addr, &vsk->local_addr))
Andy Kingd021c342013-02-06 14:23:56 +0000235 return sk_vsock(vsk);
236
Stefano Garzarella36c5b482019-11-14 10:57:49 +0100237 if (addr->svm_port == vsk->local_addr.svm_port &&
238 (vsk->local_addr.svm_cid == VMADDR_CID_ANY ||
239 addr->svm_cid == VMADDR_CID_ANY))
240 return sk_vsock(vsk);
241 }
242
Andy Kingd021c342013-02-06 14:23:56 +0000243 return NULL;
244}
245
246static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
247 struct sockaddr_vm *dst)
248{
249 struct vsock_sock *vsk;
250
251 list_for_each_entry(vsk, vsock_connected_sockets(src, dst),
252 connected_table) {
Reilly Grant990454b2013-04-01 11:41:52 -0700253 if (vsock_addr_equals_addr(src, &vsk->remote_addr) &&
254 dst->svm_port == vsk->local_addr.svm_port) {
Andy Kingd021c342013-02-06 14:23:56 +0000255 return sk_vsock(vsk);
256 }
257 }
258
259 return NULL;
260}
261
Andy Kingd021c342013-02-06 14:23:56 +0000262static void vsock_insert_unbound(struct vsock_sock *vsk)
263{
264 spin_lock_bh(&vsock_table_lock);
265 __vsock_insert_bound(vsock_unbound_sockets, vsk);
266 spin_unlock_bh(&vsock_table_lock);
267}
268
269void vsock_insert_connected(struct vsock_sock *vsk)
270{
271 struct list_head *list = vsock_connected_sockets(
272 &vsk->remote_addr, &vsk->local_addr);
273
274 spin_lock_bh(&vsock_table_lock);
275 __vsock_insert_connected(list, vsk);
276 spin_unlock_bh(&vsock_table_lock);
277}
278EXPORT_SYMBOL_GPL(vsock_insert_connected);
279
280void vsock_remove_bound(struct vsock_sock *vsk)
281{
282 spin_lock_bh(&vsock_table_lock);
Sunil Muthuswamyd5afa822019-06-13 03:52:27 +0000283 if (__vsock_in_bound_table(vsk))
284 __vsock_remove_bound(vsk);
Andy Kingd021c342013-02-06 14:23:56 +0000285 spin_unlock_bh(&vsock_table_lock);
286}
287EXPORT_SYMBOL_GPL(vsock_remove_bound);
288
289void vsock_remove_connected(struct vsock_sock *vsk)
290{
291 spin_lock_bh(&vsock_table_lock);
Sunil Muthuswamyd5afa822019-06-13 03:52:27 +0000292 if (__vsock_in_connected_table(vsk))
293 __vsock_remove_connected(vsk);
Andy Kingd021c342013-02-06 14:23:56 +0000294 spin_unlock_bh(&vsock_table_lock);
295}
296EXPORT_SYMBOL_GPL(vsock_remove_connected);
297
298struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
299{
300 struct sock *sk;
301
302 spin_lock_bh(&vsock_table_lock);
303 sk = __vsock_find_bound_socket(addr);
304 if (sk)
305 sock_hold(sk);
306
307 spin_unlock_bh(&vsock_table_lock);
308
309 return sk;
310}
311EXPORT_SYMBOL_GPL(vsock_find_bound_socket);
312
313struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
314 struct sockaddr_vm *dst)
315{
316 struct sock *sk;
317
318 spin_lock_bh(&vsock_table_lock);
319 sk = __vsock_find_connected_socket(src, dst);
320 if (sk)
321 sock_hold(sk);
322
323 spin_unlock_bh(&vsock_table_lock);
324
325 return sk;
326}
327EXPORT_SYMBOL_GPL(vsock_find_connected_socket);
328
Stefan Hajnoczi6773b7d2016-07-28 15:36:31 +0100329void vsock_remove_sock(struct vsock_sock *vsk)
330{
Sunil Muthuswamyd5afa822019-06-13 03:52:27 +0000331 vsock_remove_bound(vsk);
332 vsock_remove_connected(vsk);
Stefan Hajnoczi6773b7d2016-07-28 15:36:31 +0100333}
334EXPORT_SYMBOL_GPL(vsock_remove_sock);
335
Andy Kingd021c342013-02-06 14:23:56 +0000336void vsock_for_each_connected_socket(void (*fn)(struct sock *sk))
337{
338 int i;
339
340 spin_lock_bh(&vsock_table_lock);
341
342 for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
343 struct vsock_sock *vsk;
344 list_for_each_entry(vsk, &vsock_connected_table[i],
Julia Lawalld9af2d62013-08-05 16:47:38 +0200345 connected_table)
Andy Kingd021c342013-02-06 14:23:56 +0000346 fn(sk_vsock(vsk));
347 }
348
349 spin_unlock_bh(&vsock_table_lock);
350}
351EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket);
352
353void vsock_add_pending(struct sock *listener, struct sock *pending)
354{
355 struct vsock_sock *vlistener;
356 struct vsock_sock *vpending;
357
358 vlistener = vsock_sk(listener);
359 vpending = vsock_sk(pending);
360
361 sock_hold(pending);
362 sock_hold(listener);
363 list_add_tail(&vpending->pending_links, &vlistener->pending_links);
364}
365EXPORT_SYMBOL_GPL(vsock_add_pending);
366
367void vsock_remove_pending(struct sock *listener, struct sock *pending)
368{
369 struct vsock_sock *vpending = vsock_sk(pending);
370
371 list_del_init(&vpending->pending_links);
372 sock_put(listener);
373 sock_put(pending);
374}
375EXPORT_SYMBOL_GPL(vsock_remove_pending);
376
377void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
378{
379 struct vsock_sock *vlistener;
380 struct vsock_sock *vconnected;
381
382 vlistener = vsock_sk(listener);
383 vconnected = vsock_sk(connected);
384
385 sock_hold(connected);
386 sock_hold(listener);
387 list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue);
388}
389EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
390
Stefano Garzarella6a2c09622019-11-14 10:57:48 +0100391static void vsock_deassign_transport(struct vsock_sock *vsk)
392{
393 if (!vsk->transport)
394 return;
395
396 vsk->transport->destruct(vsk);
397 module_put(vsk->transport->module);
398 vsk->transport = NULL;
399}
400
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100401/* Assign a transport to a socket and call the .init transport callback.
402 *
403 * Note: for stream socket this must be called when vsk->remote_addr is set
404 * (e.g. during the connect() or when a connection request on a listener
405 * socket is received).
406 * The vsk->remote_addr is used to decide which transport to use:
407 * - remote CID <= VMADDR_CID_HOST will use guest->host transport;
408 * - remote CID == local_cid (guest->host transport) will use guest->host
409 * transport for loopback (host->guest transports don't support loopback);
410 * - remote CID > VMADDR_CID_HOST will use host->guest transport;
411 */
412int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
413{
414 const struct vsock_transport *new_transport;
415 struct sock *sk = sk_vsock(vsk);
416 unsigned int remote_cid = vsk->remote_addr.svm_cid;
Stefano Garzarella039fccc2019-11-21 10:06:09 +0100417 int ret;
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100418
419 switch (sk->sk_type) {
420 case SOCK_DGRAM:
421 new_transport = transport_dgram;
422 break;
423 case SOCK_STREAM:
424 if (remote_cid <= VMADDR_CID_HOST ||
425 (transport_g2h &&
426 remote_cid == transport_g2h->get_local_cid()))
427 new_transport = transport_g2h;
428 else
429 new_transport = transport_h2g;
430 break;
431 default:
432 return -ESOCKTNOSUPPORT;
433 }
434
435 if (vsk->transport) {
436 if (vsk->transport == new_transport)
437 return 0;
438
439 vsk->transport->release(vsk);
Stefano Garzarella6a2c09622019-11-14 10:57:48 +0100440 vsock_deassign_transport(vsk);
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100441 }
442
Stefano Garzarella6a2c09622019-11-14 10:57:48 +0100443 /* We increase the module refcnt to prevent the transport unloading
444 * while there are open sockets assigned to it.
445 */
446 if (!new_transport || !try_module_get(new_transport->module))
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100447 return -ENODEV;
448
Stefano Garzarella039fccc2019-11-21 10:06:09 +0100449 ret = new_transport->init(vsk, psk);
450 if (ret) {
451 module_put(new_transport->module);
452 return ret;
453 }
454
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100455 vsk->transport = new_transport;
456
Stefano Garzarella039fccc2019-11-21 10:06:09 +0100457 return 0;
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100458}
459EXPORT_SYMBOL_GPL(vsock_assign_transport);
460
461bool vsock_find_cid(unsigned int cid)
462{
463 if (transport_g2h && cid == transport_g2h->get_local_cid())
464 return true;
465
466 if (transport_h2g && cid == VMADDR_CID_HOST)
467 return true;
468
469 return false;
470}
471EXPORT_SYMBOL_GPL(vsock_find_cid);
472
Andy Kingd021c342013-02-06 14:23:56 +0000473static struct sock *vsock_dequeue_accept(struct sock *listener)
474{
475 struct vsock_sock *vlistener;
476 struct vsock_sock *vconnected;
477
478 vlistener = vsock_sk(listener);
479
480 if (list_empty(&vlistener->accept_queue))
481 return NULL;
482
483 vconnected = list_entry(vlistener->accept_queue.next,
484 struct vsock_sock, accept_queue);
485
486 list_del_init(&vconnected->accept_queue);
487 sock_put(listener);
488 /* The caller will need a reference on the connected socket so we let
489 * it call sock_put().
490 */
491
492 return sk_vsock(vconnected);
493}
494
495static bool vsock_is_accept_queue_empty(struct sock *sk)
496{
497 struct vsock_sock *vsk = vsock_sk(sk);
498 return list_empty(&vsk->accept_queue);
499}
500
501static bool vsock_is_pending(struct sock *sk)
502{
503 struct vsock_sock *vsk = vsock_sk(sk);
504 return !list_empty(&vsk->pending_links);
505}
506
507static int vsock_send_shutdown(struct sock *sk, int mode)
508{
Stefano Garzarellafe502c42019-11-14 10:57:39 +0100509 struct vsock_sock *vsk = vsock_sk(sk);
510
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100511 if (!vsk->transport)
512 return -ENODEV;
513
Stefano Garzarellafe502c42019-11-14 10:57:39 +0100514 return vsk->transport->shutdown(vsk, mode);
Andy Kingd021c342013-02-06 14:23:56 +0000515}
516
Cong Wang455f05e2018-08-06 11:06:02 -0700517static void vsock_pending_work(struct work_struct *work)
Andy Kingd021c342013-02-06 14:23:56 +0000518{
519 struct sock *sk;
520 struct sock *listener;
521 struct vsock_sock *vsk;
522 bool cleanup;
523
Cong Wang455f05e2018-08-06 11:06:02 -0700524 vsk = container_of(work, struct vsock_sock, pending_work.work);
Andy Kingd021c342013-02-06 14:23:56 +0000525 sk = sk_vsock(vsk);
526 listener = vsk->listener;
527 cleanup = true;
528
529 lock_sock(listener);
Stefan Hajnoczi4192f672016-06-23 16:28:58 +0100530 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Andy Kingd021c342013-02-06 14:23:56 +0000531
532 if (vsock_is_pending(sk)) {
533 vsock_remove_pending(listener, sk);
Jorgen Hansen1190cfd2016-09-26 23:59:53 -0700534
Eric Dumazet7976a112019-11-05 14:11:52 -0800535 sk_acceptq_removed(listener);
Andy Kingd021c342013-02-06 14:23:56 +0000536 } else if (!vsk->rejected) {
537 /* We are not on the pending list and accept() did not reject
538 * us, so we must have been accepted by our user process. We
539 * just need to drop our references to the sockets and be on
540 * our way.
541 */
542 cleanup = false;
543 goto out;
544 }
545
Andy Kingd021c342013-02-06 14:23:56 +0000546 /* We need to remove ourself from the global connected sockets list so
547 * incoming packets can't find this socket, and to reduce the reference
548 * count.
549 */
Sunil Muthuswamyd5afa822019-06-13 03:52:27 +0000550 vsock_remove_connected(vsk);
Andy Kingd021c342013-02-06 14:23:56 +0000551
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400552 sk->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +0000553
554out:
555 release_sock(sk);
556 release_sock(listener);
557 if (cleanup)
558 sock_put(sk);
559
560 sock_put(sk);
561 sock_put(listener);
562}
Andy Kingd021c342013-02-06 14:23:56 +0000563
564/**** SOCKET OPERATIONS ****/
565
566static int __vsock_bind_stream(struct vsock_sock *vsk,
567 struct sockaddr_vm *addr)
568{
Lepton Wua22d3252019-01-09 15:45:41 -0800569 static u32 port;
Andy Kingd021c342013-02-06 14:23:56 +0000570 struct sockaddr_vm new_addr;
571
Lepton Wu8236b082018-12-11 11:12:55 -0800572 if (!port)
573 port = LAST_RESERVED_PORT + 1 +
574 prandom_u32_max(U32_MAX - LAST_RESERVED_PORT);
575
Andy Kingd021c342013-02-06 14:23:56 +0000576 vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
577
578 if (addr->svm_port == VMADDR_PORT_ANY) {
579 bool found = false;
580 unsigned int i;
581
582 for (i = 0; i < MAX_PORT_RETRIES; i++) {
583 if (port <= LAST_RESERVED_PORT)
584 port = LAST_RESERVED_PORT + 1;
585
586 new_addr.svm_port = port++;
587
588 if (!__vsock_find_bound_socket(&new_addr)) {
589 found = true;
590 break;
591 }
592 }
593
594 if (!found)
595 return -EADDRNOTAVAIL;
596 } else {
597 /* If port is in reserved range, ensure caller
598 * has necessary privileges.
599 */
600 if (addr->svm_port <= LAST_RESERVED_PORT &&
601 !capable(CAP_NET_BIND_SERVICE)) {
602 return -EACCES;
603 }
604
605 if (__vsock_find_bound_socket(&new_addr))
606 return -EADDRINUSE;
607 }
608
609 vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);
610
611 /* Remove stream sockets from the unbound list and add them to the hash
612 * table for easy lookup by its address. The unbound list is simply an
613 * extra entry at the end of the hash table, a trick used by AF_UNIX.
614 */
615 __vsock_remove_bound(vsk);
616 __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
617
618 return 0;
619}
620
621static int __vsock_bind_dgram(struct vsock_sock *vsk,
622 struct sockaddr_vm *addr)
623{
Stefano Garzarellafe502c42019-11-14 10:57:39 +0100624 return vsk->transport->dgram_bind(vsk, addr);
Andy Kingd021c342013-02-06 14:23:56 +0000625}
626
627static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
628{
629 struct vsock_sock *vsk = vsock_sk(sk);
Andy Kingd021c342013-02-06 14:23:56 +0000630 int retval;
631
632 /* First ensure this socket isn't already bound. */
633 if (vsock_addr_bound(&vsk->local_addr))
634 return -EINVAL;
635
636 /* Now bind to the provided address or select appropriate values if
637 * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that
638 * like AF_INET prevents binding to a non-local IP address (in most
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100639 * cases), we only allow binding to a local CID.
Andy Kingd021c342013-02-06 14:23:56 +0000640 */
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100641 if (addr->svm_cid != VMADDR_CID_ANY && !vsock_find_cid(addr->svm_cid))
Andy Kingd021c342013-02-06 14:23:56 +0000642 return -EADDRNOTAVAIL;
643
644 switch (sk->sk_socket->type) {
645 case SOCK_STREAM:
646 spin_lock_bh(&vsock_table_lock);
647 retval = __vsock_bind_stream(vsk, addr);
648 spin_unlock_bh(&vsock_table_lock);
649 break;
650
651 case SOCK_DGRAM:
652 retval = __vsock_bind_dgram(vsk, addr);
653 break;
654
655 default:
656 retval = -EINVAL;
657 break;
658 }
659
660 return retval;
661}
662
Cong Wang455f05e2018-08-06 11:06:02 -0700663static void vsock_connect_timeout(struct work_struct *work);
664
Stefano Garzarellab9ca2f52019-11-14 10:57:43 +0100665static struct sock *__vsock_create(struct net *net,
666 struct socket *sock,
667 struct sock *parent,
668 gfp_t priority,
669 unsigned short type,
670 int kern)
Andy Kingd021c342013-02-06 14:23:56 +0000671{
672 struct sock *sk;
673 struct vsock_sock *psk;
674 struct vsock_sock *vsk;
675
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500676 sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern);
Andy Kingd021c342013-02-06 14:23:56 +0000677 if (!sk)
678 return NULL;
679
680 sock_init_data(sock, sk);
681
682 /* sk->sk_type is normally set in sock_init_data, but only if sock is
683 * non-NULL. We make sure that our sockets always have a type by
684 * setting it here if needed.
685 */
686 if (!sock)
687 sk->sk_type = type;
688
689 vsk = vsock_sk(sk);
690 vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
691 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
692
693 sk->sk_destruct = vsock_sk_destruct;
694 sk->sk_backlog_rcv = vsock_queue_rcv_skb;
Andy Kingd021c342013-02-06 14:23:56 +0000695 sock_reset_flag(sk, SOCK_DONE);
696
697 INIT_LIST_HEAD(&vsk->bound_table);
698 INIT_LIST_HEAD(&vsk->connected_table);
699 vsk->listener = NULL;
700 INIT_LIST_HEAD(&vsk->pending_links);
701 INIT_LIST_HEAD(&vsk->accept_queue);
702 vsk->rejected = false;
703 vsk->sent_request = false;
704 vsk->ignore_connecting_rst = false;
705 vsk->peer_shutdown = 0;
Cong Wang455f05e2018-08-06 11:06:02 -0700706 INIT_DELAYED_WORK(&vsk->connect_work, vsock_connect_timeout);
707 INIT_DELAYED_WORK(&vsk->pending_work, vsock_pending_work);
Andy Kingd021c342013-02-06 14:23:56 +0000708
709 psk = parent ? vsock_sk(parent) : NULL;
710 if (parent) {
711 vsk->trusted = psk->trusted;
712 vsk->owner = get_cred(psk->owner);
713 vsk->connect_timeout = psk->connect_timeout;
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +0100714 vsk->buffer_size = psk->buffer_size;
715 vsk->buffer_min_size = psk->buffer_min_size;
716 vsk->buffer_max_size = psk->buffer_max_size;
Andy Kingd021c342013-02-06 14:23:56 +0000717 } else {
718 vsk->trusted = capable(CAP_NET_ADMIN);
719 vsk->owner = get_current_cred();
720 vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT;
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +0100721 vsk->buffer_size = VSOCK_DEFAULT_BUFFER_SIZE;
722 vsk->buffer_min_size = VSOCK_DEFAULT_BUFFER_MIN_SIZE;
723 vsk->buffer_max_size = VSOCK_DEFAULT_BUFFER_MAX_SIZE;
Andy Kingd021c342013-02-06 14:23:56 +0000724 }
725
Andy Kingd021c342013-02-06 14:23:56 +0000726 return sk;
727}
Andy Kingd021c342013-02-06 14:23:56 +0000728
Dexuan Cui0d9138f2019-09-30 18:43:50 +0000729static void __vsock_release(struct sock *sk, int level)
Andy Kingd021c342013-02-06 14:23:56 +0000730{
731 if (sk) {
Andy Kingd021c342013-02-06 14:23:56 +0000732 struct sock *pending;
733 struct vsock_sock *vsk;
734
735 vsk = vsock_sk(sk);
736 pending = NULL; /* Compiler warning. */
737
Dexuan Cui0d9138f2019-09-30 18:43:50 +0000738 /* The release call is supposed to use lock_sock_nested()
739 * rather than lock_sock(), if a sock lock should be acquired.
740 */
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100741 if (vsk->transport)
742 vsk->transport->release(vsk);
743 else if (sk->sk_type == SOCK_STREAM)
744 vsock_remove_sock(vsk);
Andy Kingd021c342013-02-06 14:23:56 +0000745
Dexuan Cui0d9138f2019-09-30 18:43:50 +0000746 /* When "level" is SINGLE_DEPTH_NESTING, use the nested
747 * version to avoid the warning "possible recursive locking
748 * detected". When "level" is 0, lock_sock_nested(sk, level)
749 * is the same as lock_sock(sk).
750 */
751 lock_sock_nested(sk, level);
Andy Kingd021c342013-02-06 14:23:56 +0000752 sock_orphan(sk);
753 sk->sk_shutdown = SHUTDOWN_MASK;
754
Christophe JAILLET3b7ad082019-11-03 07:11:11 +0100755 skb_queue_purge(&sk->sk_receive_queue);
Andy Kingd021c342013-02-06 14:23:56 +0000756
757 /* Clean up any sockets that never were accepted. */
758 while ((pending = vsock_dequeue_accept(sk)) != NULL) {
Dexuan Cui0d9138f2019-09-30 18:43:50 +0000759 __vsock_release(pending, SINGLE_DEPTH_NESTING);
Andy Kingd021c342013-02-06 14:23:56 +0000760 sock_put(pending);
761 }
762
763 release_sock(sk);
764 sock_put(sk);
765 }
766}
767
768static void vsock_sk_destruct(struct sock *sk)
769{
770 struct vsock_sock *vsk = vsock_sk(sk);
771
Stefano Garzarella6a2c09622019-11-14 10:57:48 +0100772 vsock_deassign_transport(vsk);
Andy Kingd021c342013-02-06 14:23:56 +0000773
774 /* When clearing these addresses, there's no need to set the family and
775 * possibly register the address family with the kernel.
776 */
777 vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
778 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
779
780 put_cred(vsk->owner);
781}
782
783static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
784{
785 int err;
786
787 err = sock_queue_rcv_skb(sk, skb);
788 if (err)
789 kfree_skb(skb);
790
791 return err;
792}
793
Stefano Garzarellab9ca2f52019-11-14 10:57:43 +0100794struct sock *vsock_create_connected(struct sock *parent)
795{
796 return __vsock_create(sock_net(parent), NULL, parent, GFP_KERNEL,
797 parent->sk_type, 0);
798}
799EXPORT_SYMBOL_GPL(vsock_create_connected);
800
Andy Kingd021c342013-02-06 14:23:56 +0000801s64 vsock_stream_has_data(struct vsock_sock *vsk)
802{
Stefano Garzarellafe502c42019-11-14 10:57:39 +0100803 return vsk->transport->stream_has_data(vsk);
Andy Kingd021c342013-02-06 14:23:56 +0000804}
805EXPORT_SYMBOL_GPL(vsock_stream_has_data);
806
807s64 vsock_stream_has_space(struct vsock_sock *vsk)
808{
Stefano Garzarellafe502c42019-11-14 10:57:39 +0100809 return vsk->transport->stream_has_space(vsk);
Andy Kingd021c342013-02-06 14:23:56 +0000810}
811EXPORT_SYMBOL_GPL(vsock_stream_has_space);
812
813static int vsock_release(struct socket *sock)
814{
Dexuan Cui0d9138f2019-09-30 18:43:50 +0000815 __vsock_release(sock->sk, 0);
Andy Kingd021c342013-02-06 14:23:56 +0000816 sock->sk = NULL;
817 sock->state = SS_FREE;
818
819 return 0;
820}
821
822static int
823vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
824{
825 int err;
826 struct sock *sk;
827 struct sockaddr_vm *vm_addr;
828
829 sk = sock->sk;
830
831 if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0)
832 return -EINVAL;
833
834 lock_sock(sk);
835 err = __vsock_bind(sk, vm_addr);
836 release_sock(sk);
837
838 return err;
839}
840
841static int vsock_getname(struct socket *sock,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100842 struct sockaddr *addr, int peer)
Andy Kingd021c342013-02-06 14:23:56 +0000843{
844 int err;
845 struct sock *sk;
846 struct vsock_sock *vsk;
847 struct sockaddr_vm *vm_addr;
848
849 sk = sock->sk;
850 vsk = vsock_sk(sk);
851 err = 0;
852
853 lock_sock(sk);
854
855 if (peer) {
856 if (sock->state != SS_CONNECTED) {
857 err = -ENOTCONN;
858 goto out;
859 }
860 vm_addr = &vsk->remote_addr;
861 } else {
862 vm_addr = &vsk->local_addr;
863 }
864
865 if (!vm_addr) {
866 err = -EINVAL;
867 goto out;
868 }
869
870 /* sys_getsockname() and sys_getpeername() pass us a
871 * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately
872 * that macro is defined in socket.c instead of .h, so we hardcode its
873 * value here.
874 */
875 BUILD_BUG_ON(sizeof(*vm_addr) > 128);
876 memcpy(addr, vm_addr, sizeof(*vm_addr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100877 err = sizeof(*vm_addr);
Andy Kingd021c342013-02-06 14:23:56 +0000878
879out:
880 release_sock(sk);
881 return err;
882}
883
884static int vsock_shutdown(struct socket *sock, int mode)
885{
886 int err;
887 struct sock *sk;
888
889 /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
890 * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
891 * here like the other address families do. Note also that the
892 * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
893 * which is what we want.
894 */
895 mode++;
896
897 if ((mode & ~SHUTDOWN_MASK) || !mode)
898 return -EINVAL;
899
900 /* If this is a STREAM socket and it is not connected then bail out
901 * immediately. If it is a DGRAM socket then we must first kick the
902 * socket so that it wakes up from any sleeping calls, for example
903 * recv(), and then afterwards return the error.
904 */
905
906 sk = sock->sk;
907 if (sock->state == SS_UNCONNECTED) {
908 err = -ENOTCONN;
909 if (sk->sk_type == SOCK_STREAM)
910 return err;
911 } else {
912 sock->state = SS_DISCONNECTING;
913 err = 0;
914 }
915
916 /* Receive and send shutdowns are treated alike. */
917 mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
918 if (mode) {
919 lock_sock(sk);
920 sk->sk_shutdown |= mode;
921 sk->sk_state_change(sk);
922 release_sock(sk);
923
924 if (sk->sk_type == SOCK_STREAM) {
925 sock_reset_flag(sk, SOCK_DONE);
926 vsock_send_shutdown(sk, mode);
927 }
928 }
929
930 return err;
931}
932
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700933static __poll_t vsock_poll(struct file *file, struct socket *sock,
934 poll_table *wait)
Andy Kingd021c342013-02-06 14:23:56 +0000935{
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700936 struct sock *sk;
937 __poll_t mask;
938 struct vsock_sock *vsk;
939
940 sk = sock->sk;
941 vsk = vsock_sk(sk);
942
943 poll_wait(file, sk_sleep(sk), wait);
944 mask = 0;
Andy Kingd021c342013-02-06 14:23:56 +0000945
946 if (sk->sk_err)
947 /* Signify that there has been an error on this socket. */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800948 mask |= EPOLLERR;
Andy Kingd021c342013-02-06 14:23:56 +0000949
950 /* INET sockets treat local write shutdown and peer write shutdown as a
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800951 * case of EPOLLHUP set.
Andy Kingd021c342013-02-06 14:23:56 +0000952 */
953 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
954 ((sk->sk_shutdown & SEND_SHUTDOWN) &&
955 (vsk->peer_shutdown & SEND_SHUTDOWN))) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800956 mask |= EPOLLHUP;
Andy Kingd021c342013-02-06 14:23:56 +0000957 }
958
959 if (sk->sk_shutdown & RCV_SHUTDOWN ||
960 vsk->peer_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800961 mask |= EPOLLRDHUP;
Andy Kingd021c342013-02-06 14:23:56 +0000962 }
963
964 if (sock->type == SOCK_DGRAM) {
965 /* For datagram sockets we can read if there is something in
966 * the queue and write as long as the socket isn't shutdown for
967 * sending.
968 */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700969 if (!skb_queue_empty_lockless(&sk->sk_receive_queue) ||
Andy Kingd021c342013-02-06 14:23:56 +0000970 (sk->sk_shutdown & RCV_SHUTDOWN)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800971 mask |= EPOLLIN | EPOLLRDNORM;
Andy Kingd021c342013-02-06 14:23:56 +0000972 }
973
974 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800975 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Andy Kingd021c342013-02-06 14:23:56 +0000976
977 } else if (sock->type == SOCK_STREAM) {
Stefano Garzarellafe502c42019-11-14 10:57:39 +0100978 const struct vsock_transport *transport = vsk->transport;
Andy Kingd021c342013-02-06 14:23:56 +0000979 lock_sock(sk);
980
981 /* Listening sockets that have connections in their accept
982 * queue can be read.
983 */
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400984 if (sk->sk_state == TCP_LISTEN
Andy Kingd021c342013-02-06 14:23:56 +0000985 && !vsock_is_accept_queue_empty(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800986 mask |= EPOLLIN | EPOLLRDNORM;
Andy Kingd021c342013-02-06 14:23:56 +0000987
988 /* If there is something in the queue then we can read. */
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +0100989 if (transport && transport->stream_is_active(vsk) &&
Andy Kingd021c342013-02-06 14:23:56 +0000990 !(sk->sk_shutdown & RCV_SHUTDOWN)) {
991 bool data_ready_now = false;
992 int ret = transport->notify_poll_in(
993 vsk, 1, &data_ready_now);
994 if (ret < 0) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800995 mask |= EPOLLERR;
Andy Kingd021c342013-02-06 14:23:56 +0000996 } else {
997 if (data_ready_now)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800998 mask |= EPOLLIN | EPOLLRDNORM;
Andy Kingd021c342013-02-06 14:23:56 +0000999
1000 }
1001 }
1002
1003 /* Sockets whose connections have been closed, reset, or
1004 * terminated should also be considered read, and we check the
1005 * shutdown flag for that.
1006 */
1007 if (sk->sk_shutdown & RCV_SHUTDOWN ||
1008 vsk->peer_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001009 mask |= EPOLLIN | EPOLLRDNORM;
Andy Kingd021c342013-02-06 14:23:56 +00001010 }
1011
1012 /* Connected sockets that can produce data can be written. */
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001013 if (sk->sk_state == TCP_ESTABLISHED) {
Andy Kingd021c342013-02-06 14:23:56 +00001014 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
1015 bool space_avail_now = false;
1016 int ret = transport->notify_poll_out(
1017 vsk, 1, &space_avail_now);
1018 if (ret < 0) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001019 mask |= EPOLLERR;
Andy Kingd021c342013-02-06 14:23:56 +00001020 } else {
1021 if (space_avail_now)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001022 /* Remove EPOLLWRBAND since INET
Andy Kingd021c342013-02-06 14:23:56 +00001023 * sockets are not setting it.
1024 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001025 mask |= EPOLLOUT | EPOLLWRNORM;
Andy Kingd021c342013-02-06 14:23:56 +00001026
1027 }
1028 }
1029 }
1030
1031 /* Simulate INET socket poll behaviors, which sets
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001032 * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read,
Andy Kingd021c342013-02-06 14:23:56 +00001033 * but local send is not shutdown.
1034 */
Stefan Hajnocziba3169f2018-01-26 11:48:25 +00001035 if (sk->sk_state == TCP_CLOSE || sk->sk_state == TCP_CLOSING) {
Andy Kingd021c342013-02-06 14:23:56 +00001036 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001037 mask |= EPOLLOUT | EPOLLWRNORM;
Andy Kingd021c342013-02-06 14:23:56 +00001038
1039 }
1040
1041 release_sock(sk);
1042 }
1043
1044 return mask;
1045}
1046
Ying Xue1b784142015-03-02 15:37:48 +08001047static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1048 size_t len)
Andy Kingd021c342013-02-06 14:23:56 +00001049{
1050 int err;
1051 struct sock *sk;
1052 struct vsock_sock *vsk;
1053 struct sockaddr_vm *remote_addr;
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001054 const struct vsock_transport *transport;
Andy Kingd021c342013-02-06 14:23:56 +00001055
1056 if (msg->msg_flags & MSG_OOB)
1057 return -EOPNOTSUPP;
1058
1059 /* For now, MSG_DONTWAIT is always assumed... */
1060 err = 0;
1061 sk = sock->sk;
1062 vsk = vsock_sk(sk);
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001063 transport = vsk->transport;
Andy Kingd021c342013-02-06 14:23:56 +00001064
1065 lock_sock(sk);
1066
Asias Heb3a6dfe2013-06-20 17:20:30 +08001067 err = vsock_auto_bind(vsk);
1068 if (err)
1069 goto out;
Andy Kingd021c342013-02-06 14:23:56 +00001070
Andy Kingd021c342013-02-06 14:23:56 +00001071
1072 /* If the provided message contains an address, use that. Otherwise
1073 * fall back on the socket's remote handle (if it has been connected).
1074 */
1075 if (msg->msg_name &&
1076 vsock_addr_cast(msg->msg_name, msg->msg_namelen,
1077 &remote_addr) == 0) {
1078 /* Ensure this address is of the right type and is a valid
1079 * destination.
1080 */
1081
1082 if (remote_addr->svm_cid == VMADDR_CID_ANY)
1083 remote_addr->svm_cid = transport->get_local_cid();
1084
1085 if (!vsock_addr_bound(remote_addr)) {
1086 err = -EINVAL;
1087 goto out;
1088 }
1089 } else if (sock->state == SS_CONNECTED) {
1090 remote_addr = &vsk->remote_addr;
1091
1092 if (remote_addr->svm_cid == VMADDR_CID_ANY)
1093 remote_addr->svm_cid = transport->get_local_cid();
1094
1095 /* XXX Should connect() or this function ensure remote_addr is
1096 * bound?
1097 */
1098 if (!vsock_addr_bound(&vsk->remote_addr)) {
1099 err = -EINVAL;
1100 goto out;
1101 }
1102 } else {
1103 err = -EINVAL;
1104 goto out;
1105 }
1106
1107 if (!transport->dgram_allow(remote_addr->svm_cid,
1108 remote_addr->svm_port)) {
1109 err = -EINVAL;
1110 goto out;
1111 }
1112
Al Viro0f7db232014-11-20 04:05:34 -05001113 err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
Andy Kingd021c342013-02-06 14:23:56 +00001114
1115out:
1116 release_sock(sk);
1117 return err;
1118}
1119
1120static int vsock_dgram_connect(struct socket *sock,
1121 struct sockaddr *addr, int addr_len, int flags)
1122{
1123 int err;
1124 struct sock *sk;
1125 struct vsock_sock *vsk;
1126 struct sockaddr_vm *remote_addr;
1127
1128 sk = sock->sk;
1129 vsk = vsock_sk(sk);
1130
1131 err = vsock_addr_cast(addr, addr_len, &remote_addr);
1132 if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) {
1133 lock_sock(sk);
1134 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY,
1135 VMADDR_PORT_ANY);
1136 sock->state = SS_UNCONNECTED;
1137 release_sock(sk);
1138 return 0;
1139 } else if (err != 0)
1140 return -EINVAL;
1141
1142 lock_sock(sk);
1143
Asias Heb3a6dfe2013-06-20 17:20:30 +08001144 err = vsock_auto_bind(vsk);
1145 if (err)
1146 goto out;
Andy Kingd021c342013-02-06 14:23:56 +00001147
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001148 if (!vsk->transport->dgram_allow(remote_addr->svm_cid,
1149 remote_addr->svm_port)) {
Andy Kingd021c342013-02-06 14:23:56 +00001150 err = -EINVAL;
1151 goto out;
1152 }
1153
1154 memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr));
1155 sock->state = SS_CONNECTED;
1156
1157out:
1158 release_sock(sk);
1159 return err;
1160}
1161
Ying Xue1b784142015-03-02 15:37:48 +08001162static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
1163 size_t len, int flags)
Andy Kingd021c342013-02-06 14:23:56 +00001164{
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001165 struct vsock_sock *vsk = vsock_sk(sock->sk);
1166
1167 return vsk->transport->dgram_dequeue(vsk, msg, len, flags);
Andy Kingd021c342013-02-06 14:23:56 +00001168}
1169
1170static const struct proto_ops vsock_dgram_ops = {
1171 .family = PF_VSOCK,
1172 .owner = THIS_MODULE,
1173 .release = vsock_release,
1174 .bind = vsock_bind,
1175 .connect = vsock_dgram_connect,
1176 .socketpair = sock_no_socketpair,
1177 .accept = sock_no_accept,
1178 .getname = vsock_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001179 .poll = vsock_poll,
Andy Kingd021c342013-02-06 14:23:56 +00001180 .ioctl = sock_no_ioctl,
1181 .listen = sock_no_listen,
1182 .shutdown = vsock_shutdown,
1183 .setsockopt = sock_no_setsockopt,
1184 .getsockopt = sock_no_getsockopt,
1185 .sendmsg = vsock_dgram_sendmsg,
1186 .recvmsg = vsock_dgram_recvmsg,
1187 .mmap = sock_no_mmap,
1188 .sendpage = sock_no_sendpage,
1189};
1190
Peng Tao380feae2017-03-15 09:32:17 +08001191static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
1192{
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001193 const struct vsock_transport *transport = vsk->transport;
1194
Peng Tao380feae2017-03-15 09:32:17 +08001195 if (!transport->cancel_pkt)
1196 return -EOPNOTSUPP;
1197
1198 return transport->cancel_pkt(vsk);
1199}
1200
Andy Kingd021c342013-02-06 14:23:56 +00001201static void vsock_connect_timeout(struct work_struct *work)
1202{
1203 struct sock *sk;
1204 struct vsock_sock *vsk;
Peng Tao380feae2017-03-15 09:32:17 +08001205 int cancel = 0;
Andy Kingd021c342013-02-06 14:23:56 +00001206
Cong Wang455f05e2018-08-06 11:06:02 -07001207 vsk = container_of(work, struct vsock_sock, connect_work.work);
Andy Kingd021c342013-02-06 14:23:56 +00001208 sk = sk_vsock(vsk);
1209
1210 lock_sock(sk);
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001211 if (sk->sk_state == TCP_SYN_SENT &&
Andy Kingd021c342013-02-06 14:23:56 +00001212 (sk->sk_shutdown != SHUTDOWN_MASK)) {
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001213 sk->sk_state = TCP_CLOSE;
Andy Kingd021c342013-02-06 14:23:56 +00001214 sk->sk_err = ETIMEDOUT;
1215 sk->sk_error_report(sk);
Peng Tao380feae2017-03-15 09:32:17 +08001216 cancel = 1;
Andy Kingd021c342013-02-06 14:23:56 +00001217 }
1218 release_sock(sk);
Peng Tao380feae2017-03-15 09:32:17 +08001219 if (cancel)
1220 vsock_transport_cancel_pkt(vsk);
Andy Kingd021c342013-02-06 14:23:56 +00001221
1222 sock_put(sk);
1223}
1224
1225static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
1226 int addr_len, int flags)
1227{
1228 int err;
1229 struct sock *sk;
1230 struct vsock_sock *vsk;
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001231 const struct vsock_transport *transport;
Andy Kingd021c342013-02-06 14:23:56 +00001232 struct sockaddr_vm *remote_addr;
1233 long timeout;
1234 DEFINE_WAIT(wait);
1235
1236 err = 0;
1237 sk = sock->sk;
1238 vsk = vsock_sk(sk);
1239
1240 lock_sock(sk);
1241
1242 /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
1243 switch (sock->state) {
1244 case SS_CONNECTED:
1245 err = -EISCONN;
1246 goto out;
1247 case SS_DISCONNECTING:
1248 err = -EINVAL;
1249 goto out;
1250 case SS_CONNECTING:
1251 /* This continues on so we can move sock into the SS_CONNECTED
1252 * state once the connection has completed (at which point err
1253 * will be set to zero also). Otherwise, we will either wait
1254 * for the connection or return -EALREADY should this be a
1255 * non-blocking call.
1256 */
1257 err = -EALREADY;
1258 break;
1259 default:
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001260 if ((sk->sk_state == TCP_LISTEN) ||
Andy Kingd021c342013-02-06 14:23:56 +00001261 vsock_addr_cast(addr, addr_len, &remote_addr) != 0) {
1262 err = -EINVAL;
1263 goto out;
1264 }
1265
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01001266 /* Set the remote address that we are connecting to. */
1267 memcpy(&vsk->remote_addr, remote_addr,
1268 sizeof(vsk->remote_addr));
1269
1270 err = vsock_assign_transport(vsk, NULL);
1271 if (err)
1272 goto out;
1273
1274 transport = vsk->transport;
1275
Andy Kingd021c342013-02-06 14:23:56 +00001276 /* The hypervisor and well-known contexts do not have socket
1277 * endpoints.
1278 */
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01001279 if (!transport ||
1280 !transport->stream_allow(remote_addr->svm_cid,
Andy Kingd021c342013-02-06 14:23:56 +00001281 remote_addr->svm_port)) {
1282 err = -ENETUNREACH;
1283 goto out;
1284 }
1285
Asias Heb3a6dfe2013-06-20 17:20:30 +08001286 err = vsock_auto_bind(vsk);
1287 if (err)
1288 goto out;
Andy Kingd021c342013-02-06 14:23:56 +00001289
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001290 sk->sk_state = TCP_SYN_SENT;
Andy Kingd021c342013-02-06 14:23:56 +00001291
1292 err = transport->connect(vsk);
1293 if (err < 0)
1294 goto out;
1295
1296 /* Mark sock as connecting and set the error code to in
1297 * progress in case this is a non-blocking connect.
1298 */
1299 sock->state = SS_CONNECTING;
1300 err = -EINPROGRESS;
1301 }
1302
1303 /* The receive path will handle all communication until we are able to
1304 * enter the connected state. Here we wait for the connection to be
1305 * completed or a notification of an error.
1306 */
1307 timeout = vsk->connect_timeout;
1308 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1309
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001310 while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) {
Andy Kingd021c342013-02-06 14:23:56 +00001311 if (flags & O_NONBLOCK) {
1312 /* If we're not going to block, we schedule a timeout
1313 * function to generate a timeout on the connection
1314 * attempt, in case the peer doesn't respond in a
1315 * timely manner. We hold on to the socket until the
1316 * timeout fires.
1317 */
1318 sock_hold(sk);
Cong Wang455f05e2018-08-06 11:06:02 -07001319 schedule_delayed_work(&vsk->connect_work, timeout);
Andy Kingd021c342013-02-06 14:23:56 +00001320
1321 /* Skip ahead to preserve error code set above. */
1322 goto out_wait;
1323 }
1324
1325 release_sock(sk);
1326 timeout = schedule_timeout(timeout);
1327 lock_sock(sk);
1328
1329 if (signal_pending(current)) {
1330 err = sock_intr_errno(timeout);
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001331 sk->sk_state = TCP_CLOSE;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001332 sock->state = SS_UNCONNECTED;
Peng Tao380feae2017-03-15 09:32:17 +08001333 vsock_transport_cancel_pkt(vsk);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001334 goto out_wait;
Andy Kingd021c342013-02-06 14:23:56 +00001335 } else if (timeout == 0) {
1336 err = -ETIMEDOUT;
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001337 sk->sk_state = TCP_CLOSE;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001338 sock->state = SS_UNCONNECTED;
Peng Tao380feae2017-03-15 09:32:17 +08001339 vsock_transport_cancel_pkt(vsk);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001340 goto out_wait;
Andy Kingd021c342013-02-06 14:23:56 +00001341 }
1342
1343 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1344 }
1345
1346 if (sk->sk_err) {
1347 err = -sk->sk_err;
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001348 sk->sk_state = TCP_CLOSE;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001349 sock->state = SS_UNCONNECTED;
1350 } else {
Andy Kingd021c342013-02-06 14:23:56 +00001351 err = 0;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001352 }
Andy Kingd021c342013-02-06 14:23:56 +00001353
1354out_wait:
1355 finish_wait(sk_sleep(sk), &wait);
1356out:
1357 release_sock(sk);
1358 return err;
Andy Kingd021c342013-02-06 14:23:56 +00001359}
1360
David Howellscdfbabf2017-03-09 08:09:05 +00001361static int vsock_accept(struct socket *sock, struct socket *newsock, int flags,
1362 bool kern)
Andy Kingd021c342013-02-06 14:23:56 +00001363{
1364 struct sock *listener;
1365 int err;
1366 struct sock *connected;
1367 struct vsock_sock *vconnected;
1368 long timeout;
1369 DEFINE_WAIT(wait);
1370
1371 err = 0;
1372 listener = sock->sk;
1373
1374 lock_sock(listener);
1375
1376 if (sock->type != SOCK_STREAM) {
1377 err = -EOPNOTSUPP;
1378 goto out;
1379 }
1380
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001381 if (listener->sk_state != TCP_LISTEN) {
Andy Kingd021c342013-02-06 14:23:56 +00001382 err = -EINVAL;
1383 goto out;
1384 }
1385
1386 /* Wait for children sockets to appear; these are the new sockets
1387 * created upon connection establishment.
1388 */
1389 timeout = sock_sndtimeo(listener, flags & O_NONBLOCK);
1390 prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
1391
1392 while ((connected = vsock_dequeue_accept(listener)) == NULL &&
1393 listener->sk_err == 0) {
1394 release_sock(listener);
1395 timeout = schedule_timeout(timeout);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001396 finish_wait(sk_sleep(listener), &wait);
Andy Kingd021c342013-02-06 14:23:56 +00001397 lock_sock(listener);
1398
1399 if (signal_pending(current)) {
1400 err = sock_intr_errno(timeout);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001401 goto out;
Andy Kingd021c342013-02-06 14:23:56 +00001402 } else if (timeout == 0) {
1403 err = -EAGAIN;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001404 goto out;
Andy Kingd021c342013-02-06 14:23:56 +00001405 }
1406
1407 prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
1408 }
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001409 finish_wait(sk_sleep(listener), &wait);
Andy Kingd021c342013-02-06 14:23:56 +00001410
1411 if (listener->sk_err)
1412 err = -listener->sk_err;
1413
1414 if (connected) {
Eric Dumazet7976a112019-11-05 14:11:52 -08001415 sk_acceptq_removed(listener);
Andy Kingd021c342013-02-06 14:23:56 +00001416
Stefan Hajnoczi4192f672016-06-23 16:28:58 +01001417 lock_sock_nested(connected, SINGLE_DEPTH_NESTING);
Andy Kingd021c342013-02-06 14:23:56 +00001418 vconnected = vsock_sk(connected);
1419
1420 /* If the listener socket has received an error, then we should
1421 * reject this socket and return. Note that we simply mark the
1422 * socket rejected, drop our reference, and let the cleanup
1423 * function handle the cleanup; the fact that we found it in
1424 * the listener's accept queue guarantees that the cleanup
1425 * function hasn't run yet.
1426 */
1427 if (err) {
1428 vconnected->rejected = true;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001429 } else {
1430 newsock->state = SS_CONNECTED;
1431 sock_graft(connected, newsock);
Andy Kingd021c342013-02-06 14:23:56 +00001432 }
1433
Andy Kingd021c342013-02-06 14:23:56 +00001434 release_sock(connected);
1435 sock_put(connected);
1436 }
1437
Andy Kingd021c342013-02-06 14:23:56 +00001438out:
1439 release_sock(listener);
1440 return err;
1441}
1442
1443static int vsock_listen(struct socket *sock, int backlog)
1444{
1445 int err;
1446 struct sock *sk;
1447 struct vsock_sock *vsk;
1448
1449 sk = sock->sk;
1450
1451 lock_sock(sk);
1452
1453 if (sock->type != SOCK_STREAM) {
1454 err = -EOPNOTSUPP;
1455 goto out;
1456 }
1457
1458 if (sock->state != SS_UNCONNECTED) {
1459 err = -EINVAL;
1460 goto out;
1461 }
1462
1463 vsk = vsock_sk(sk);
1464
1465 if (!vsock_addr_bound(&vsk->local_addr)) {
1466 err = -EINVAL;
1467 goto out;
1468 }
1469
1470 sk->sk_max_ack_backlog = backlog;
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001471 sk->sk_state = TCP_LISTEN;
Andy Kingd021c342013-02-06 14:23:56 +00001472
1473 err = 0;
1474
1475out:
1476 release_sock(sk);
1477 return err;
1478}
1479
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +01001480static void vsock_update_buffer_size(struct vsock_sock *vsk,
1481 const struct vsock_transport *transport,
1482 u64 val)
1483{
1484 if (val > vsk->buffer_max_size)
1485 val = vsk->buffer_max_size;
1486
1487 if (val < vsk->buffer_min_size)
1488 val = vsk->buffer_min_size;
1489
1490 if (val != vsk->buffer_size &&
1491 transport && transport->notify_buffer_size)
1492 transport->notify_buffer_size(vsk, &val);
1493
1494 vsk->buffer_size = val;
1495}
1496
Andy Kingd021c342013-02-06 14:23:56 +00001497static int vsock_stream_setsockopt(struct socket *sock,
1498 int level,
1499 int optname,
1500 char __user *optval,
1501 unsigned int optlen)
1502{
1503 int err;
1504 struct sock *sk;
1505 struct vsock_sock *vsk;
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001506 const struct vsock_transport *transport;
Andy Kingd021c342013-02-06 14:23:56 +00001507 u64 val;
1508
1509 if (level != AF_VSOCK)
1510 return -ENOPROTOOPT;
1511
1512#define COPY_IN(_v) \
1513 do { \
1514 if (optlen < sizeof(_v)) { \
1515 err = -EINVAL; \
1516 goto exit; \
1517 } \
1518 if (copy_from_user(&_v, optval, sizeof(_v)) != 0) { \
1519 err = -EFAULT; \
1520 goto exit; \
1521 } \
1522 } while (0)
1523
1524 err = 0;
1525 sk = sock->sk;
1526 vsk = vsock_sk(sk);
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001527 transport = vsk->transport;
Andy Kingd021c342013-02-06 14:23:56 +00001528
1529 lock_sock(sk);
1530
1531 switch (optname) {
1532 case SO_VM_SOCKETS_BUFFER_SIZE:
1533 COPY_IN(val);
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +01001534 vsock_update_buffer_size(vsk, transport, val);
Andy Kingd021c342013-02-06 14:23:56 +00001535 break;
1536
1537 case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
1538 COPY_IN(val);
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +01001539 vsk->buffer_max_size = val;
1540 vsock_update_buffer_size(vsk, transport, vsk->buffer_size);
Andy Kingd021c342013-02-06 14:23:56 +00001541 break;
1542
1543 case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
1544 COPY_IN(val);
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +01001545 vsk->buffer_min_size = val;
1546 vsock_update_buffer_size(vsk, transport, vsk->buffer_size);
Andy Kingd021c342013-02-06 14:23:56 +00001547 break;
1548
1549 case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
Arnd Bergmannfe0c72f2019-02-02 07:34:44 -08001550 struct __kernel_old_timeval tv;
Andy Kingd021c342013-02-06 14:23:56 +00001551 COPY_IN(tv);
1552 if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC &&
1553 tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) {
1554 vsk->connect_timeout = tv.tv_sec * HZ +
1555 DIV_ROUND_UP(tv.tv_usec, (1000000 / HZ));
1556 if (vsk->connect_timeout == 0)
1557 vsk->connect_timeout =
1558 VSOCK_DEFAULT_CONNECT_TIMEOUT;
1559
1560 } else {
1561 err = -ERANGE;
1562 }
1563 break;
1564 }
1565
1566 default:
1567 err = -ENOPROTOOPT;
1568 break;
1569 }
1570
1571#undef COPY_IN
1572
1573exit:
1574 release_sock(sk);
1575 return err;
1576}
1577
1578static int vsock_stream_getsockopt(struct socket *sock,
1579 int level, int optname,
1580 char __user *optval,
1581 int __user *optlen)
1582{
1583 int err;
1584 int len;
1585 struct sock *sk;
1586 struct vsock_sock *vsk;
1587 u64 val;
1588
1589 if (level != AF_VSOCK)
1590 return -ENOPROTOOPT;
1591
1592 err = get_user(len, optlen);
1593 if (err != 0)
1594 return err;
1595
1596#define COPY_OUT(_v) \
1597 do { \
1598 if (len < sizeof(_v)) \
1599 return -EINVAL; \
1600 \
1601 len = sizeof(_v); \
1602 if (copy_to_user(optval, &_v, len) != 0) \
1603 return -EFAULT; \
1604 \
1605 } while (0)
1606
1607 err = 0;
1608 sk = sock->sk;
1609 vsk = vsock_sk(sk);
1610
1611 switch (optname) {
1612 case SO_VM_SOCKETS_BUFFER_SIZE:
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +01001613 val = vsk->buffer_size;
Andy Kingd021c342013-02-06 14:23:56 +00001614 COPY_OUT(val);
1615 break;
1616
1617 case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +01001618 val = vsk->buffer_max_size;
Andy Kingd021c342013-02-06 14:23:56 +00001619 COPY_OUT(val);
1620 break;
1621
1622 case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
Stefano Garzarellab9f2b0f2019-11-14 10:57:42 +01001623 val = vsk->buffer_min_size;
Andy Kingd021c342013-02-06 14:23:56 +00001624 COPY_OUT(val);
1625 break;
1626
1627 case SO_VM_SOCKETS_CONNECT_TIMEOUT: {
Arnd Bergmannfe0c72f2019-02-02 07:34:44 -08001628 struct __kernel_old_timeval tv;
Andy Kingd021c342013-02-06 14:23:56 +00001629 tv.tv_sec = vsk->connect_timeout / HZ;
1630 tv.tv_usec =
1631 (vsk->connect_timeout -
1632 tv.tv_sec * HZ) * (1000000 / HZ);
1633 COPY_OUT(tv);
1634 break;
1635 }
1636 default:
1637 return -ENOPROTOOPT;
1638 }
1639
1640 err = put_user(len, optlen);
1641 if (err != 0)
1642 return -EFAULT;
1643
1644#undef COPY_OUT
1645
1646 return 0;
1647}
1648
Ying Xue1b784142015-03-02 15:37:48 +08001649static int vsock_stream_sendmsg(struct socket *sock, struct msghdr *msg,
1650 size_t len)
Andy Kingd021c342013-02-06 14:23:56 +00001651{
1652 struct sock *sk;
1653 struct vsock_sock *vsk;
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001654 const struct vsock_transport *transport;
Andy Kingd021c342013-02-06 14:23:56 +00001655 ssize_t total_written;
1656 long timeout;
1657 int err;
1658 struct vsock_transport_send_notify_data send_data;
WANG Cong499fde62017-05-19 11:21:59 -07001659 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Andy Kingd021c342013-02-06 14:23:56 +00001660
1661 sk = sock->sk;
1662 vsk = vsock_sk(sk);
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001663 transport = vsk->transport;
Andy Kingd021c342013-02-06 14:23:56 +00001664 total_written = 0;
1665 err = 0;
1666
1667 if (msg->msg_flags & MSG_OOB)
1668 return -EOPNOTSUPP;
1669
1670 lock_sock(sk);
1671
1672 /* Callers should not provide a destination with stream sockets. */
1673 if (msg->msg_namelen) {
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -04001674 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
Andy Kingd021c342013-02-06 14:23:56 +00001675 goto out;
1676 }
1677
1678 /* Send data only if both sides are not shutdown in the direction. */
1679 if (sk->sk_shutdown & SEND_SHUTDOWN ||
1680 vsk->peer_shutdown & RCV_SHUTDOWN) {
1681 err = -EPIPE;
1682 goto out;
1683 }
1684
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01001685 if (!transport || sk->sk_state != TCP_ESTABLISHED ||
Andy Kingd021c342013-02-06 14:23:56 +00001686 !vsock_addr_bound(&vsk->local_addr)) {
1687 err = -ENOTCONN;
1688 goto out;
1689 }
1690
1691 if (!vsock_addr_bound(&vsk->remote_addr)) {
1692 err = -EDESTADDRREQ;
1693 goto out;
1694 }
1695
1696 /* Wait for room in the produce queue to enqueue our user's data. */
1697 timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1698
1699 err = transport->notify_send_init(vsk, &send_data);
1700 if (err < 0)
1701 goto out;
1702
Andy Kingd021c342013-02-06 14:23:56 +00001703 while (total_written < len) {
1704 ssize_t written;
1705
WANG Cong499fde62017-05-19 11:21:59 -07001706 add_wait_queue(sk_sleep(sk), &wait);
Andy Kingd021c342013-02-06 14:23:56 +00001707 while (vsock_stream_has_space(vsk) == 0 &&
1708 sk->sk_err == 0 &&
1709 !(sk->sk_shutdown & SEND_SHUTDOWN) &&
1710 !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
1711
1712 /* Don't wait for non-blocking sockets. */
1713 if (timeout == 0) {
1714 err = -EAGAIN;
WANG Cong499fde62017-05-19 11:21:59 -07001715 remove_wait_queue(sk_sleep(sk), &wait);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001716 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001717 }
1718
1719 err = transport->notify_send_pre_block(vsk, &send_data);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001720 if (err < 0) {
WANG Cong499fde62017-05-19 11:21:59 -07001721 remove_wait_queue(sk_sleep(sk), &wait);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001722 goto out_err;
1723 }
Andy Kingd021c342013-02-06 14:23:56 +00001724
1725 release_sock(sk);
WANG Cong499fde62017-05-19 11:21:59 -07001726 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
Andy Kingd021c342013-02-06 14:23:56 +00001727 lock_sock(sk);
1728 if (signal_pending(current)) {
1729 err = sock_intr_errno(timeout);
WANG Cong499fde62017-05-19 11:21:59 -07001730 remove_wait_queue(sk_sleep(sk), &wait);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001731 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001732 } else if (timeout == 0) {
1733 err = -EAGAIN;
WANG Cong499fde62017-05-19 11:21:59 -07001734 remove_wait_queue(sk_sleep(sk), &wait);
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001735 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001736 }
Andy Kingd021c342013-02-06 14:23:56 +00001737 }
WANG Cong499fde62017-05-19 11:21:59 -07001738 remove_wait_queue(sk_sleep(sk), &wait);
Andy Kingd021c342013-02-06 14:23:56 +00001739
1740 /* These checks occur both as part of and after the loop
1741 * conditional since we need to check before and after
1742 * sleeping.
1743 */
1744 if (sk->sk_err) {
1745 err = -sk->sk_err;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001746 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001747 } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
1748 (vsk->peer_shutdown & RCV_SHUTDOWN)) {
1749 err = -EPIPE;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001750 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001751 }
1752
1753 err = transport->notify_send_pre_enqueue(vsk, &send_data);
1754 if (err < 0)
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001755 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001756
1757 /* Note that enqueue will only write as many bytes as are free
1758 * in the produce queue, so we don't need to ensure len is
1759 * smaller than the queue size. It is the caller's
1760 * responsibility to check how many bytes we were able to send.
1761 */
1762
1763 written = transport->stream_enqueue(
Al Viro0f7db232014-11-20 04:05:34 -05001764 vsk, msg,
Andy Kingd021c342013-02-06 14:23:56 +00001765 len - total_written);
1766 if (written < 0) {
1767 err = -ENOMEM;
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001768 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001769 }
1770
1771 total_written += written;
1772
1773 err = transport->notify_send_post_enqueue(
1774 vsk, written, &send_data);
1775 if (err < 0)
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001776 goto out_err;
Andy Kingd021c342013-02-06 14:23:56 +00001777
1778 }
1779
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001780out_err:
Andy Kingd021c342013-02-06 14:23:56 +00001781 if (total_written > 0)
1782 err = total_written;
Andy Kingd021c342013-02-06 14:23:56 +00001783out:
1784 release_sock(sk);
1785 return err;
1786}
1787
1788
1789static int
Ying Xue1b784142015-03-02 15:37:48 +08001790vsock_stream_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1791 int flags)
Andy Kingd021c342013-02-06 14:23:56 +00001792{
1793 struct sock *sk;
1794 struct vsock_sock *vsk;
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001795 const struct vsock_transport *transport;
Andy Kingd021c342013-02-06 14:23:56 +00001796 int err;
1797 size_t target;
1798 ssize_t copied;
1799 long timeout;
1800 struct vsock_transport_recv_notify_data recv_data;
1801
1802 DEFINE_WAIT(wait);
1803
1804 sk = sock->sk;
1805 vsk = vsock_sk(sk);
Stefano Garzarellafe502c42019-11-14 10:57:39 +01001806 transport = vsk->transport;
Andy Kingd021c342013-02-06 14:23:56 +00001807 err = 0;
1808
1809 lock_sock(sk);
1810
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01001811 if (!transport || sk->sk_state != TCP_ESTABLISHED) {
Andy Kingd021c342013-02-06 14:23:56 +00001812 /* Recvmsg is supposed to return 0 if a peer performs an
1813 * orderly shutdown. Differentiate between that case and when a
1814 * peer has not connected or a local shutdown occured with the
1815 * SOCK_DONE flag.
1816 */
1817 if (sock_flag(sk, SOCK_DONE))
1818 err = 0;
1819 else
1820 err = -ENOTCONN;
1821
1822 goto out;
1823 }
1824
1825 if (flags & MSG_OOB) {
1826 err = -EOPNOTSUPP;
1827 goto out;
1828 }
1829
1830 /* We don't check peer_shutdown flag here since peer may actually shut
1831 * down, but there can be data in the queue that a local socket can
1832 * receive.
1833 */
1834 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1835 err = 0;
1836 goto out;
1837 }
1838
1839 /* It is valid on Linux to pass in a zero-length receive buffer. This
1840 * is not an error. We may as well bail out now.
1841 */
1842 if (!len) {
1843 err = 0;
1844 goto out;
1845 }
1846
1847 /* We must not copy less than target bytes into the user's buffer
1848 * before returning successfully, so we wait for the consume queue to
1849 * have that much data to consume before dequeueing. Note that this
1850 * makes it impossible to handle cases where target is greater than the
1851 * queue size.
1852 */
1853 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1854 if (target >= transport->stream_rcvhiwat(vsk)) {
1855 err = -ENOMEM;
1856 goto out;
1857 }
1858 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1859 copied = 0;
1860
1861 err = transport->notify_recv_init(vsk, target, &recv_data);
1862 if (err < 0)
1863 goto out;
1864
Andy Kingd021c342013-02-06 14:23:56 +00001865
1866 while (1) {
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001867 s64 ready;
Andy Kingd021c342013-02-06 14:23:56 +00001868
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001869 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1870 ready = vsock_stream_has_data(vsk);
Andy Kingd021c342013-02-06 14:23:56 +00001871
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001872 if (ready == 0) {
1873 if (sk->sk_err != 0 ||
1874 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1875 (vsk->peer_shutdown & SEND_SHUTDOWN)) {
1876 finish_wait(sk_sleep(sk), &wait);
1877 break;
1878 }
1879 /* Don't wait for non-blocking sockets. */
1880 if (timeout == 0) {
1881 err = -EAGAIN;
1882 finish_wait(sk_sleep(sk), &wait);
1883 break;
1884 }
1885
1886 err = transport->notify_recv_pre_block(
1887 vsk, target, &recv_data);
1888 if (err < 0) {
1889 finish_wait(sk_sleep(sk), &wait);
1890 break;
1891 }
1892 release_sock(sk);
1893 timeout = schedule_timeout(timeout);
1894 lock_sock(sk);
1895
1896 if (signal_pending(current)) {
1897 err = sock_intr_errno(timeout);
1898 finish_wait(sk_sleep(sk), &wait);
1899 break;
1900 } else if (timeout == 0) {
1901 err = -EAGAIN;
1902 finish_wait(sk_sleep(sk), &wait);
1903 break;
1904 }
1905 } else {
Andy Kingd021c342013-02-06 14:23:56 +00001906 ssize_t read;
1907
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001908 finish_wait(sk_sleep(sk), &wait);
1909
1910 if (ready < 0) {
1911 /* Invalid queue pair content. XXX This should
1912 * be changed to a connection reset in a later
1913 * change.
1914 */
1915
1916 err = -ENOMEM;
1917 goto out;
1918 }
1919
Andy Kingd021c342013-02-06 14:23:56 +00001920 err = transport->notify_recv_pre_dequeue(
1921 vsk, target, &recv_data);
1922 if (err < 0)
1923 break;
1924
1925 read = transport->stream_dequeue(
Al Viro0f7db232014-11-20 04:05:34 -05001926 vsk, msg,
Andy Kingd021c342013-02-06 14:23:56 +00001927 len - copied, flags);
1928 if (read < 0) {
1929 err = -ENOMEM;
1930 break;
1931 }
1932
1933 copied += read;
1934
1935 err = transport->notify_recv_post_dequeue(
1936 vsk, target, read,
1937 !(flags & MSG_PEEK), &recv_data);
1938 if (err < 0)
Claudio Imbrendaf7f9b5e2016-03-22 17:05:52 +01001939 goto out;
Andy Kingd021c342013-02-06 14:23:56 +00001940
1941 if (read >= target || flags & MSG_PEEK)
1942 break;
1943
1944 target -= read;
Andy Kingd021c342013-02-06 14:23:56 +00001945 }
1946 }
1947
1948 if (sk->sk_err)
1949 err = -sk->sk_err;
1950 else if (sk->sk_shutdown & RCV_SHUTDOWN)
1951 err = 0;
1952
Ian Campbelldedc58e2016-05-04 14:21:53 +01001953 if (copied > 0)
Andy Kingd021c342013-02-06 14:23:56 +00001954 err = copied;
Andy Kingd021c342013-02-06 14:23:56 +00001955
Andy Kingd021c342013-02-06 14:23:56 +00001956out:
1957 release_sock(sk);
1958 return err;
1959}
1960
1961static const struct proto_ops vsock_stream_ops = {
1962 .family = PF_VSOCK,
1963 .owner = THIS_MODULE,
1964 .release = vsock_release,
1965 .bind = vsock_bind,
1966 .connect = vsock_stream_connect,
1967 .socketpair = sock_no_socketpair,
1968 .accept = vsock_accept,
1969 .getname = vsock_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001970 .poll = vsock_poll,
Andy Kingd021c342013-02-06 14:23:56 +00001971 .ioctl = sock_no_ioctl,
1972 .listen = vsock_listen,
1973 .shutdown = vsock_shutdown,
1974 .setsockopt = vsock_stream_setsockopt,
1975 .getsockopt = vsock_stream_getsockopt,
1976 .sendmsg = vsock_stream_sendmsg,
1977 .recvmsg = vsock_stream_recvmsg,
1978 .mmap = sock_no_mmap,
1979 .sendpage = sock_no_sendpage,
1980};
1981
1982static int vsock_create(struct net *net, struct socket *sock,
1983 int protocol, int kern)
1984{
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01001985 struct vsock_sock *vsk;
Stefano Garzarella55f3e142019-11-14 10:57:44 +01001986 struct sock *sk;
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01001987 int ret;
Stefano Garzarella55f3e142019-11-14 10:57:44 +01001988
Andy Kingd021c342013-02-06 14:23:56 +00001989 if (!sock)
1990 return -EINVAL;
1991
Andy King6cf1c5f2013-02-18 06:04:13 +00001992 if (protocol && protocol != PF_VSOCK)
Andy Kingd021c342013-02-06 14:23:56 +00001993 return -EPROTONOSUPPORT;
1994
1995 switch (sock->type) {
1996 case SOCK_DGRAM:
1997 sock->ops = &vsock_dgram_ops;
1998 break;
1999 case SOCK_STREAM:
2000 sock->ops = &vsock_stream_ops;
2001 break;
2002 default:
2003 return -ESOCKTNOSUPPORT;
2004 }
2005
2006 sock->state = SS_UNCONNECTED;
2007
Stefano Garzarella55f3e142019-11-14 10:57:44 +01002008 sk = __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern);
2009 if (!sk)
2010 return -ENOMEM;
2011
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002012 vsk = vsock_sk(sk);
2013
2014 if (sock->type == SOCK_DGRAM) {
2015 ret = vsock_assign_transport(vsk, NULL);
2016 if (ret < 0) {
2017 sock_put(sk);
2018 return ret;
2019 }
2020 }
2021
2022 vsock_insert_unbound(vsk);
Stefano Garzarella55f3e142019-11-14 10:57:44 +01002023
2024 return 0;
Andy Kingd021c342013-02-06 14:23:56 +00002025}
2026
2027static const struct net_proto_family vsock_family_ops = {
2028 .family = AF_VSOCK,
2029 .create = vsock_create,
2030 .owner = THIS_MODULE,
2031};
2032
2033static long vsock_dev_do_ioctl(struct file *filp,
2034 unsigned int cmd, void __user *ptr)
2035{
2036 u32 __user *p = ptr;
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002037 u32 cid = VMADDR_CID_ANY;
Andy Kingd021c342013-02-06 14:23:56 +00002038 int retval = 0;
2039
2040 switch (cmd) {
2041 case IOCTL_VM_SOCKETS_GET_LOCAL_CID:
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002042 /* To be compatible with the VMCI behavior, we prioritize the
2043 * guest CID instead of well-know host CID (VMADDR_CID_HOST).
2044 */
2045 if (transport_g2h)
2046 cid = transport_g2h->get_local_cid();
2047 else if (transport_h2g)
2048 cid = transport_h2g->get_local_cid();
2049
2050 if (put_user(cid, p) != 0)
Andy Kingd021c342013-02-06 14:23:56 +00002051 retval = -EFAULT;
2052 break;
2053
2054 default:
2055 pr_err("Unknown ioctl %d\n", cmd);
2056 retval = -EINVAL;
2057 }
2058
2059 return retval;
2060}
2061
2062static long vsock_dev_ioctl(struct file *filp,
2063 unsigned int cmd, unsigned long arg)
2064{
2065 return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg);
2066}
2067
2068#ifdef CONFIG_COMPAT
2069static long vsock_dev_compat_ioctl(struct file *filp,
2070 unsigned int cmd, unsigned long arg)
2071{
2072 return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg));
2073}
2074#endif
2075
2076static const struct file_operations vsock_device_ops = {
2077 .owner = THIS_MODULE,
2078 .unlocked_ioctl = vsock_dev_ioctl,
2079#ifdef CONFIG_COMPAT
2080 .compat_ioctl = vsock_dev_compat_ioctl,
2081#endif
2082 .open = nonseekable_open,
2083};
2084
2085static struct miscdevice vsock_device = {
2086 .name = "vsock",
Andy Kingd021c342013-02-06 14:23:56 +00002087 .fops = &vsock_device_ops,
2088};
2089
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002090static int __init vsock_init(void)
Andy Kingd021c342013-02-06 14:23:56 +00002091{
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002092 int err = 0;
Andy King2c4a3362014-05-01 15:20:43 -07002093
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002094 vsock_init_tables();
Andy King2c4a3362014-05-01 15:20:43 -07002095
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002096 vsock_proto.owner = THIS_MODULE;
Asias He6ad0b2f2013-04-23 20:33:52 +00002097 vsock_device.minor = MISC_DYNAMIC_MINOR;
Andy Kingd021c342013-02-06 14:23:56 +00002098 err = misc_register(&vsock_device);
2099 if (err) {
2100 pr_err("Failed to register misc device\n");
Gao fengf6a835b2015-10-18 23:35:56 +08002101 goto err_reset_transport;
Andy Kingd021c342013-02-06 14:23:56 +00002102 }
2103
2104 err = proto_register(&vsock_proto, 1); /* we want our slab */
2105 if (err) {
2106 pr_err("Cannot register vsock protocol\n");
Gao fengf6a835b2015-10-18 23:35:56 +08002107 goto err_deregister_misc;
Andy Kingd021c342013-02-06 14:23:56 +00002108 }
2109
2110 err = sock_register(&vsock_family_ops);
2111 if (err) {
2112 pr_err("could not register af_vsock (%d) address family: %d\n",
2113 AF_VSOCK, err);
2114 goto err_unregister_proto;
2115 }
2116
2117 return 0;
2118
2119err_unregister_proto:
2120 proto_unregister(&vsock_proto);
Gao fengf6a835b2015-10-18 23:35:56 +08002121err_deregister_misc:
Andy Kingd021c342013-02-06 14:23:56 +00002122 misc_deregister(&vsock_device);
Gao fengf6a835b2015-10-18 23:35:56 +08002123err_reset_transport:
Andy Kingd021c342013-02-06 14:23:56 +00002124 return err;
2125}
Andy Kingd021c342013-02-06 14:23:56 +00002126
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002127static void __exit vsock_exit(void)
Andy Kingd021c342013-02-06 14:23:56 +00002128{
Andy Kingd021c342013-02-06 14:23:56 +00002129 misc_deregister(&vsock_device);
2130 sock_unregister(AF_VSOCK);
2131 proto_unregister(&vsock_proto);
Andy Kingd021c342013-02-06 14:23:56 +00002132}
Andy Kingd021c342013-02-06 14:23:56 +00002133
Stefano Garzarelladaabfbc2019-11-14 10:57:41 +01002134const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk)
Stefan Hajnoczi0b01aeb2016-07-28 15:36:30 +01002135{
Stefano Garzarelladaabfbc2019-11-14 10:57:41 +01002136 return vsk->transport;
Stefan Hajnoczi0b01aeb2016-07-28 15:36:30 +01002137}
2138EXPORT_SYMBOL_GPL(vsock_core_get_transport);
2139
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002140int vsock_core_register(const struct vsock_transport *t, int features)
Stefan Hajnoczi05e489b2018-04-17 14:25:58 +08002141{
Stefano Garzarella0e121902019-12-10 11:43:04 +01002142 const struct vsock_transport *t_h2g, *t_g2h, *t_dgram, *t_local;
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002143 int err = mutex_lock_interruptible(&vsock_register_mutex);
Stefan Hajnoczi05e489b2018-04-17 14:25:58 +08002144
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002145 if (err)
2146 return err;
2147
2148 t_h2g = transport_h2g;
2149 t_g2h = transport_g2h;
2150 t_dgram = transport_dgram;
Stefano Garzarella0e121902019-12-10 11:43:04 +01002151 t_local = transport_local;
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002152
2153 if (features & VSOCK_TRANSPORT_F_H2G) {
2154 if (t_h2g) {
2155 err = -EBUSY;
2156 goto err_busy;
2157 }
2158 t_h2g = t;
2159 }
2160
2161 if (features & VSOCK_TRANSPORT_F_G2H) {
2162 if (t_g2h) {
2163 err = -EBUSY;
2164 goto err_busy;
2165 }
2166 t_g2h = t;
2167 }
2168
2169 if (features & VSOCK_TRANSPORT_F_DGRAM) {
2170 if (t_dgram) {
2171 err = -EBUSY;
2172 goto err_busy;
2173 }
2174 t_dgram = t;
2175 }
2176
Stefano Garzarella0e121902019-12-10 11:43:04 +01002177 if (features & VSOCK_TRANSPORT_F_LOCAL) {
2178 if (t_local) {
2179 err = -EBUSY;
2180 goto err_busy;
2181 }
2182 t_local = t;
2183 }
2184
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002185 transport_h2g = t_h2g;
2186 transport_g2h = t_g2h;
2187 transport_dgram = t_dgram;
Stefano Garzarella0e121902019-12-10 11:43:04 +01002188 transport_local = t_local;
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002189
2190err_busy:
2191 mutex_unlock(&vsock_register_mutex);
2192 return err;
2193}
2194EXPORT_SYMBOL_GPL(vsock_core_register);
2195
2196void vsock_core_unregister(const struct vsock_transport *t)
2197{
2198 mutex_lock(&vsock_register_mutex);
2199
2200 if (transport_h2g == t)
2201 transport_h2g = NULL;
2202
2203 if (transport_g2h == t)
2204 transport_g2h = NULL;
2205
2206 if (transport_dgram == t)
2207 transport_dgram = NULL;
2208
Stefano Garzarella0e121902019-12-10 11:43:04 +01002209 if (transport_local == t)
2210 transport_local = NULL;
2211
Stefano Garzarellac0cfa2d2019-11-14 10:57:46 +01002212 mutex_unlock(&vsock_register_mutex);
2213}
2214EXPORT_SYMBOL_GPL(vsock_core_unregister);
2215
2216module_init(vsock_init);
Stefan Hajnoczi05e489b2018-04-17 14:25:58 +08002217module_exit(vsock_exit);
Cong Wangc1eef222017-10-24 15:30:37 -07002218
Andy Kingd021c342013-02-06 14:23:56 +00002219MODULE_AUTHOR("VMware, Inc.");
2220MODULE_DESCRIPTION("VMware Virtual Socket Family");
Jorgen Hansen1190cfd2016-09-26 23:59:53 -07002221MODULE_VERSION("1.0.2.0-k");
Andy Kingd021c342013-02-06 14:23:56 +00002222MODULE_LICENSE("GPL v2");