virtio/vsock: send credit update during setting SO_RCVLOWAT
[platform/kernel/linux-rpi.git] / net / vmw_vsock / af_vsock.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VMware vSockets Driver
4  *
5  * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
6  */
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
31  * implementation because they are in the TCP_LISTEN state.  When a
32  * 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.
45  *
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  *
56  * - 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  *
64  * - 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.
77  *
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
86  */
87
88 #include <linux/compat.h>
89 #include <linux/types.h>
90 #include <linux/bitops.h>
91 #include <linux/cred.h>
92 #include <linux/errqueue.h>
93 #include <linux/init.h>
94 #include <linux/io.h>
95 #include <linux/kernel.h>
96 #include <linux/sched/signal.h>
97 #include <linux/kmod.h>
98 #include <linux/list.h>
99 #include <linux/miscdevice.h>
100 #include <linux/module.h>
101 #include <linux/mutex.h>
102 #include <linux/net.h>
103 #include <linux/poll.h>
104 #include <linux/random.h>
105 #include <linux/skbuff.h>
106 #include <linux/smp.h>
107 #include <linux/socket.h>
108 #include <linux/stddef.h>
109 #include <linux/unistd.h>
110 #include <linux/wait.h>
111 #include <linux/workqueue.h>
112 #include <net/sock.h>
113 #include <net/af_vsock.h>
114 #include <uapi/linux/vm_sockets.h>
115
116 static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr);
117 static void vsock_sk_destruct(struct sock *sk);
118 static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
119
120 /* Protocol family. */
121 struct proto vsock_proto = {
122         .name = "AF_VSOCK",
123         .owner = THIS_MODULE,
124         .obj_size = sizeof(struct vsock_sock),
125 #ifdef CONFIG_BPF_SYSCALL
126         .psock_update_sk_prot = vsock_bpf_update_proto,
127 #endif
128 };
129
130 /* The default peer timeout indicates how long we will wait for a peer response
131  * to a control message.
132  */
133 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
134
135 #define VSOCK_DEFAULT_BUFFER_SIZE     (1024 * 256)
136 #define VSOCK_DEFAULT_BUFFER_MAX_SIZE (1024 * 256)
137 #define VSOCK_DEFAULT_BUFFER_MIN_SIZE 128
138
139 /* Transport used for host->guest communication */
140 static const struct vsock_transport *transport_h2g;
141 /* Transport used for guest->host communication */
142 static const struct vsock_transport *transport_g2h;
143 /* Transport used for DGRAM communication */
144 static const struct vsock_transport *transport_dgram;
145 /* Transport used for local communication */
146 static const struct vsock_transport *transport_local;
147 static DEFINE_MUTEX(vsock_register_mutex);
148
149 /**** UTILS ****/
150
151 /* Each bound VSocket is stored in the bind hash table and each connected
152  * VSocket is stored in the connected hash table.
153  *
154  * Unbound sockets are all put on the same list attached to the end of the hash
155  * table (vsock_unbound_sockets).  Bound sockets are added to the hash table in
156  * the bucket that their local address hashes to (vsock_bound_sockets(addr)
157  * represents the list that addr hashes to).
158  *
159  * Specifically, we initialize the vsock_bind_table array to a size of
160  * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through
161  * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and
162  * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets.  The hash function
163  * mods with VSOCK_HASH_SIZE to ensure this.
164  */
165 #define MAX_PORT_RETRIES        24
166
167 #define VSOCK_HASH(addr)        ((addr)->svm_port % VSOCK_HASH_SIZE)
168 #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
169 #define vsock_unbound_sockets     (&vsock_bind_table[VSOCK_HASH_SIZE])
170
171 /* XXX This can probably be implemented in a better way. */
172 #define VSOCK_CONN_HASH(src, dst)                               \
173         (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
174 #define vsock_connected_sockets(src, dst)               \
175         (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
176 #define vsock_connected_sockets_vsk(vsk)                                \
177         vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
178
179 struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
180 EXPORT_SYMBOL_GPL(vsock_bind_table);
181 struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
182 EXPORT_SYMBOL_GPL(vsock_connected_table);
183 DEFINE_SPINLOCK(vsock_table_lock);
184 EXPORT_SYMBOL_GPL(vsock_table_lock);
185
186 /* Autobind this socket to the local address if necessary. */
187 static int vsock_auto_bind(struct vsock_sock *vsk)
188 {
189         struct sock *sk = sk_vsock(vsk);
190         struct sockaddr_vm local_addr;
191
192         if (vsock_addr_bound(&vsk->local_addr))
193                 return 0;
194         vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
195         return __vsock_bind(sk, &local_addr);
196 }
197
198 static void vsock_init_tables(void)
199 {
200         int i;
201
202         for (i = 0; i < ARRAY_SIZE(vsock_bind_table); i++)
203                 INIT_LIST_HEAD(&vsock_bind_table[i]);
204
205         for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++)
206                 INIT_LIST_HEAD(&vsock_connected_table[i]);
207 }
208
209 static void __vsock_insert_bound(struct list_head *list,
210                                  struct vsock_sock *vsk)
211 {
212         sock_hold(&vsk->sk);
213         list_add(&vsk->bound_table, list);
214 }
215
216 static void __vsock_insert_connected(struct list_head *list,
217                                      struct vsock_sock *vsk)
218 {
219         sock_hold(&vsk->sk);
220         list_add(&vsk->connected_table, list);
221 }
222
223 static void __vsock_remove_bound(struct vsock_sock *vsk)
224 {
225         list_del_init(&vsk->bound_table);
226         sock_put(&vsk->sk);
227 }
228
229 static void __vsock_remove_connected(struct vsock_sock *vsk)
230 {
231         list_del_init(&vsk->connected_table);
232         sock_put(&vsk->sk);
233 }
234
235 static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
236 {
237         struct vsock_sock *vsk;
238
239         list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
240                 if (vsock_addr_equals_addr(addr, &vsk->local_addr))
241                         return sk_vsock(vsk);
242
243                 if (addr->svm_port == vsk->local_addr.svm_port &&
244                     (vsk->local_addr.svm_cid == VMADDR_CID_ANY ||
245                      addr->svm_cid == VMADDR_CID_ANY))
246                         return sk_vsock(vsk);
247         }
248
249         return NULL;
250 }
251
252 static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
253                                                   struct sockaddr_vm *dst)
254 {
255         struct vsock_sock *vsk;
256
257         list_for_each_entry(vsk, vsock_connected_sockets(src, dst),
258                             connected_table) {
259                 if (vsock_addr_equals_addr(src, &vsk->remote_addr) &&
260                     dst->svm_port == vsk->local_addr.svm_port) {
261                         return sk_vsock(vsk);
262                 }
263         }
264
265         return NULL;
266 }
267
268 static void vsock_insert_unbound(struct vsock_sock *vsk)
269 {
270         spin_lock_bh(&vsock_table_lock);
271         __vsock_insert_bound(vsock_unbound_sockets, vsk);
272         spin_unlock_bh(&vsock_table_lock);
273 }
274
275 void vsock_insert_connected(struct vsock_sock *vsk)
276 {
277         struct list_head *list = vsock_connected_sockets(
278                 &vsk->remote_addr, &vsk->local_addr);
279
280         spin_lock_bh(&vsock_table_lock);
281         __vsock_insert_connected(list, vsk);
282         spin_unlock_bh(&vsock_table_lock);
283 }
284 EXPORT_SYMBOL_GPL(vsock_insert_connected);
285
286 void vsock_remove_bound(struct vsock_sock *vsk)
287 {
288         spin_lock_bh(&vsock_table_lock);
289         if (__vsock_in_bound_table(vsk))
290                 __vsock_remove_bound(vsk);
291         spin_unlock_bh(&vsock_table_lock);
292 }
293 EXPORT_SYMBOL_GPL(vsock_remove_bound);
294
295 void vsock_remove_connected(struct vsock_sock *vsk)
296 {
297         spin_lock_bh(&vsock_table_lock);
298         if (__vsock_in_connected_table(vsk))
299                 __vsock_remove_connected(vsk);
300         spin_unlock_bh(&vsock_table_lock);
301 }
302 EXPORT_SYMBOL_GPL(vsock_remove_connected);
303
304 struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
305 {
306         struct sock *sk;
307
308         spin_lock_bh(&vsock_table_lock);
309         sk = __vsock_find_bound_socket(addr);
310         if (sk)
311                 sock_hold(sk);
312
313         spin_unlock_bh(&vsock_table_lock);
314
315         return sk;
316 }
317 EXPORT_SYMBOL_GPL(vsock_find_bound_socket);
318
319 struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
320                                          struct sockaddr_vm *dst)
321 {
322         struct sock *sk;
323
324         spin_lock_bh(&vsock_table_lock);
325         sk = __vsock_find_connected_socket(src, dst);
326         if (sk)
327                 sock_hold(sk);
328
329         spin_unlock_bh(&vsock_table_lock);
330
331         return sk;
332 }
333 EXPORT_SYMBOL_GPL(vsock_find_connected_socket);
334
335 void vsock_remove_sock(struct vsock_sock *vsk)
336 {
337         vsock_remove_bound(vsk);
338         vsock_remove_connected(vsk);
339 }
340 EXPORT_SYMBOL_GPL(vsock_remove_sock);
341
342 void vsock_for_each_connected_socket(struct vsock_transport *transport,
343                                      void (*fn)(struct sock *sk))
344 {
345         int i;
346
347         spin_lock_bh(&vsock_table_lock);
348
349         for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
350                 struct vsock_sock *vsk;
351                 list_for_each_entry(vsk, &vsock_connected_table[i],
352                                     connected_table) {
353                         if (vsk->transport != transport)
354                                 continue;
355
356                         fn(sk_vsock(vsk));
357                 }
358         }
359
360         spin_unlock_bh(&vsock_table_lock);
361 }
362 EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket);
363
364 void vsock_add_pending(struct sock *listener, struct sock *pending)
365 {
366         struct vsock_sock *vlistener;
367         struct vsock_sock *vpending;
368
369         vlistener = vsock_sk(listener);
370         vpending = vsock_sk(pending);
371
372         sock_hold(pending);
373         sock_hold(listener);
374         list_add_tail(&vpending->pending_links, &vlistener->pending_links);
375 }
376 EXPORT_SYMBOL_GPL(vsock_add_pending);
377
378 void vsock_remove_pending(struct sock *listener, struct sock *pending)
379 {
380         struct vsock_sock *vpending = vsock_sk(pending);
381
382         list_del_init(&vpending->pending_links);
383         sock_put(listener);
384         sock_put(pending);
385 }
386 EXPORT_SYMBOL_GPL(vsock_remove_pending);
387
388 void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
389 {
390         struct vsock_sock *vlistener;
391         struct vsock_sock *vconnected;
392
393         vlistener = vsock_sk(listener);
394         vconnected = vsock_sk(connected);
395
396         sock_hold(connected);
397         sock_hold(listener);
398         list_add_tail(&vconnected->accept_queue, &vlistener->accept_queue);
399 }
400 EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
401
402 static bool vsock_use_local_transport(unsigned int remote_cid)
403 {
404         if (!transport_local)
405                 return false;
406
407         if (remote_cid == VMADDR_CID_LOCAL)
408                 return true;
409
410         if (transport_g2h) {
411                 return remote_cid == transport_g2h->get_local_cid();
412         } else {
413                 return remote_cid == VMADDR_CID_HOST;
414         }
415 }
416
417 static void vsock_deassign_transport(struct vsock_sock *vsk)
418 {
419         if (!vsk->transport)
420                 return;
421
422         vsk->transport->destruct(vsk);
423         module_put(vsk->transport->module);
424         vsk->transport = NULL;
425 }
426
427 /* Assign a transport to a socket and call the .init transport callback.
428  *
429  * Note: for connection oriented socket this must be called when vsk->remote_addr
430  * is set (e.g. during the connect() or when a connection request on a listener
431  * socket is received).
432  * The vsk->remote_addr is used to decide which transport to use:
433  *  - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if
434  *    g2h is not loaded, will use local transport;
435  *  - remote CID <= VMADDR_CID_HOST or h2g is not loaded or remote flags field
436  *    includes VMADDR_FLAG_TO_HOST flag value, will use guest->host transport;
437  *  - remote CID > VMADDR_CID_HOST will use host->guest transport;
438  */
439 int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
440 {
441         const struct vsock_transport *new_transport;
442         struct sock *sk = sk_vsock(vsk);
443         unsigned int remote_cid = vsk->remote_addr.svm_cid;
444         __u8 remote_flags;
445         int ret;
446
447         /* If the packet is coming with the source and destination CIDs higher
448          * than VMADDR_CID_HOST, then a vsock channel where all the packets are
449          * forwarded to the host should be established. Then the host will
450          * need to forward the packets to the guest.
451          *
452          * The flag is set on the (listen) receive path (psk is not NULL). On
453          * the connect path the flag can be set by the user space application.
454          */
455         if (psk && vsk->local_addr.svm_cid > VMADDR_CID_HOST &&
456             vsk->remote_addr.svm_cid > VMADDR_CID_HOST)
457                 vsk->remote_addr.svm_flags |= VMADDR_FLAG_TO_HOST;
458
459         remote_flags = vsk->remote_addr.svm_flags;
460
461         switch (sk->sk_type) {
462         case SOCK_DGRAM:
463                 new_transport = transport_dgram;
464                 break;
465         case SOCK_STREAM:
466         case SOCK_SEQPACKET:
467                 if (vsock_use_local_transport(remote_cid))
468                         new_transport = transport_local;
469                 else if (remote_cid <= VMADDR_CID_HOST || !transport_h2g ||
470                          (remote_flags & VMADDR_FLAG_TO_HOST))
471                         new_transport = transport_g2h;
472                 else
473                         new_transport = transport_h2g;
474                 break;
475         default:
476                 return -ESOCKTNOSUPPORT;
477         }
478
479         if (vsk->transport) {
480                 if (vsk->transport == new_transport)
481                         return 0;
482
483                 /* transport->release() must be called with sock lock acquired.
484                  * This path can only be taken during vsock_connect(), where we
485                  * have already held the sock lock. In the other cases, this
486                  * function is called on a new socket which is not assigned to
487                  * any transport.
488                  */
489                 vsk->transport->release(vsk);
490                 vsock_deassign_transport(vsk);
491         }
492
493         /* We increase the module refcnt to prevent the transport unloading
494          * while there are open sockets assigned to it.
495          */
496         if (!new_transport || !try_module_get(new_transport->module))
497                 return -ENODEV;
498
499         if (sk->sk_type == SOCK_SEQPACKET) {
500                 if (!new_transport->seqpacket_allow ||
501                     !new_transport->seqpacket_allow(remote_cid)) {
502                         module_put(new_transport->module);
503                         return -ESOCKTNOSUPPORT;
504                 }
505         }
506
507         ret = new_transport->init(vsk, psk);
508         if (ret) {
509                 module_put(new_transport->module);
510                 return ret;
511         }
512
513         vsk->transport = new_transport;
514
515         return 0;
516 }
517 EXPORT_SYMBOL_GPL(vsock_assign_transport);
518
519 bool vsock_find_cid(unsigned int cid)
520 {
521         if (transport_g2h && cid == transport_g2h->get_local_cid())
522                 return true;
523
524         if (transport_h2g && cid == VMADDR_CID_HOST)
525                 return true;
526
527         if (transport_local && cid == VMADDR_CID_LOCAL)
528                 return true;
529
530         return false;
531 }
532 EXPORT_SYMBOL_GPL(vsock_find_cid);
533
534 static struct sock *vsock_dequeue_accept(struct sock *listener)
535 {
536         struct vsock_sock *vlistener;
537         struct vsock_sock *vconnected;
538
539         vlistener = vsock_sk(listener);
540
541         if (list_empty(&vlistener->accept_queue))
542                 return NULL;
543
544         vconnected = list_entry(vlistener->accept_queue.next,
545                                 struct vsock_sock, accept_queue);
546
547         list_del_init(&vconnected->accept_queue);
548         sock_put(listener);
549         /* The caller will need a reference on the connected socket so we let
550          * it call sock_put().
551          */
552
553         return sk_vsock(vconnected);
554 }
555
556 static bool vsock_is_accept_queue_empty(struct sock *sk)
557 {
558         struct vsock_sock *vsk = vsock_sk(sk);
559         return list_empty(&vsk->accept_queue);
560 }
561
562 static bool vsock_is_pending(struct sock *sk)
563 {
564         struct vsock_sock *vsk = vsock_sk(sk);
565         return !list_empty(&vsk->pending_links);
566 }
567
568 static int vsock_send_shutdown(struct sock *sk, int mode)
569 {
570         struct vsock_sock *vsk = vsock_sk(sk);
571
572         if (!vsk->transport)
573                 return -ENODEV;
574
575         return vsk->transport->shutdown(vsk, mode);
576 }
577
578 static void vsock_pending_work(struct work_struct *work)
579 {
580         struct sock *sk;
581         struct sock *listener;
582         struct vsock_sock *vsk;
583         bool cleanup;
584
585         vsk = container_of(work, struct vsock_sock, pending_work.work);
586         sk = sk_vsock(vsk);
587         listener = vsk->listener;
588         cleanup = true;
589
590         lock_sock(listener);
591         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
592
593         if (vsock_is_pending(sk)) {
594                 vsock_remove_pending(listener, sk);
595
596                 sk_acceptq_removed(listener);
597         } else if (!vsk->rejected) {
598                 /* We are not on the pending list and accept() did not reject
599                  * us, so we must have been accepted by our user process.  We
600                  * just need to drop our references to the sockets and be on
601                  * our way.
602                  */
603                 cleanup = false;
604                 goto out;
605         }
606
607         /* We need to remove ourself from the global connected sockets list so
608          * incoming packets can't find this socket, and to reduce the reference
609          * count.
610          */
611         vsock_remove_connected(vsk);
612
613         sk->sk_state = TCP_CLOSE;
614
615 out:
616         release_sock(sk);
617         release_sock(listener);
618         if (cleanup)
619                 sock_put(sk);
620
621         sock_put(sk);
622         sock_put(listener);
623 }
624
625 /**** SOCKET OPERATIONS ****/
626
627 static int __vsock_bind_connectible(struct vsock_sock *vsk,
628                                     struct sockaddr_vm *addr)
629 {
630         static u32 port;
631         struct sockaddr_vm new_addr;
632
633         if (!port)
634                 port = get_random_u32_above(LAST_RESERVED_PORT);
635
636         vsock_addr_init(&new_addr, addr->svm_cid, addr->svm_port);
637
638         if (addr->svm_port == VMADDR_PORT_ANY) {
639                 bool found = false;
640                 unsigned int i;
641
642                 for (i = 0; i < MAX_PORT_RETRIES; i++) {
643                         if (port <= LAST_RESERVED_PORT)
644                                 port = LAST_RESERVED_PORT + 1;
645
646                         new_addr.svm_port = port++;
647
648                         if (!__vsock_find_bound_socket(&new_addr)) {
649                                 found = true;
650                                 break;
651                         }
652                 }
653
654                 if (!found)
655                         return -EADDRNOTAVAIL;
656         } else {
657                 /* If port is in reserved range, ensure caller
658                  * has necessary privileges.
659                  */
660                 if (addr->svm_port <= LAST_RESERVED_PORT &&
661                     !capable(CAP_NET_BIND_SERVICE)) {
662                         return -EACCES;
663                 }
664
665                 if (__vsock_find_bound_socket(&new_addr))
666                         return -EADDRINUSE;
667         }
668
669         vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);
670
671         /* Remove connection oriented sockets from the unbound list and add them
672          * to the hash table for easy lookup by its address.  The unbound list
673          * is simply an extra entry at the end of the hash table, a trick used
674          * by AF_UNIX.
675          */
676         __vsock_remove_bound(vsk);
677         __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
678
679         return 0;
680 }
681
682 static int __vsock_bind_dgram(struct vsock_sock *vsk,
683                               struct sockaddr_vm *addr)
684 {
685         return vsk->transport->dgram_bind(vsk, addr);
686 }
687
688 static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
689 {
690         struct vsock_sock *vsk = vsock_sk(sk);
691         int retval;
692
693         /* First ensure this socket isn't already bound. */
694         if (vsock_addr_bound(&vsk->local_addr))
695                 return -EINVAL;
696
697         /* Now bind to the provided address or select appropriate values if
698          * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY).  Note that
699          * like AF_INET prevents binding to a non-local IP address (in most
700          * cases), we only allow binding to a local CID.
701          */
702         if (addr->svm_cid != VMADDR_CID_ANY && !vsock_find_cid(addr->svm_cid))
703                 return -EADDRNOTAVAIL;
704
705         switch (sk->sk_socket->type) {
706         case SOCK_STREAM:
707         case SOCK_SEQPACKET:
708                 spin_lock_bh(&vsock_table_lock);
709                 retval = __vsock_bind_connectible(vsk, addr);
710                 spin_unlock_bh(&vsock_table_lock);
711                 break;
712
713         case SOCK_DGRAM:
714                 retval = __vsock_bind_dgram(vsk, addr);
715                 break;
716
717         default:
718                 retval = -EINVAL;
719                 break;
720         }
721
722         return retval;
723 }
724
725 static void vsock_connect_timeout(struct work_struct *work);
726
727 static struct sock *__vsock_create(struct net *net,
728                                    struct socket *sock,
729                                    struct sock *parent,
730                                    gfp_t priority,
731                                    unsigned short type,
732                                    int kern)
733 {
734         struct sock *sk;
735         struct vsock_sock *psk;
736         struct vsock_sock *vsk;
737
738         sk = sk_alloc(net, AF_VSOCK, priority, &vsock_proto, kern);
739         if (!sk)
740                 return NULL;
741
742         sock_init_data(sock, sk);
743
744         /* sk->sk_type is normally set in sock_init_data, but only if sock is
745          * non-NULL. We make sure that our sockets always have a type by
746          * setting it here if needed.
747          */
748         if (!sock)
749                 sk->sk_type = type;
750
751         vsk = vsock_sk(sk);
752         vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
753         vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
754
755         sk->sk_destruct = vsock_sk_destruct;
756         sk->sk_backlog_rcv = vsock_queue_rcv_skb;
757         sock_reset_flag(sk, SOCK_DONE);
758
759         INIT_LIST_HEAD(&vsk->bound_table);
760         INIT_LIST_HEAD(&vsk->connected_table);
761         vsk->listener = NULL;
762         INIT_LIST_HEAD(&vsk->pending_links);
763         INIT_LIST_HEAD(&vsk->accept_queue);
764         vsk->rejected = false;
765         vsk->sent_request = false;
766         vsk->ignore_connecting_rst = false;
767         vsk->peer_shutdown = 0;
768         INIT_DELAYED_WORK(&vsk->connect_work, vsock_connect_timeout);
769         INIT_DELAYED_WORK(&vsk->pending_work, vsock_pending_work);
770
771         psk = parent ? vsock_sk(parent) : NULL;
772         if (parent) {
773                 vsk->trusted = psk->trusted;
774                 vsk->owner = get_cred(psk->owner);
775                 vsk->connect_timeout = psk->connect_timeout;
776                 vsk->buffer_size = psk->buffer_size;
777                 vsk->buffer_min_size = psk->buffer_min_size;
778                 vsk->buffer_max_size = psk->buffer_max_size;
779                 security_sk_clone(parent, sk);
780         } else {
781                 vsk->trusted = ns_capable_noaudit(&init_user_ns, CAP_NET_ADMIN);
782                 vsk->owner = get_current_cred();
783                 vsk->connect_timeout = VSOCK_DEFAULT_CONNECT_TIMEOUT;
784                 vsk->buffer_size = VSOCK_DEFAULT_BUFFER_SIZE;
785                 vsk->buffer_min_size = VSOCK_DEFAULT_BUFFER_MIN_SIZE;
786                 vsk->buffer_max_size = VSOCK_DEFAULT_BUFFER_MAX_SIZE;
787         }
788
789         return sk;
790 }
791
792 static bool sock_type_connectible(u16 type)
793 {
794         return (type == SOCK_STREAM) || (type == SOCK_SEQPACKET);
795 }
796
797 static void __vsock_release(struct sock *sk, int level)
798 {
799         if (sk) {
800                 struct sock *pending;
801                 struct vsock_sock *vsk;
802
803                 vsk = vsock_sk(sk);
804                 pending = NULL; /* Compiler warning. */
805
806                 /* When "level" is SINGLE_DEPTH_NESTING, use the nested
807                  * version to avoid the warning "possible recursive locking
808                  * detected". When "level" is 0, lock_sock_nested(sk, level)
809                  * is the same as lock_sock(sk).
810                  */
811                 lock_sock_nested(sk, level);
812
813                 if (vsk->transport)
814                         vsk->transport->release(vsk);
815                 else if (sock_type_connectible(sk->sk_type))
816                         vsock_remove_sock(vsk);
817
818                 sock_orphan(sk);
819                 sk->sk_shutdown = SHUTDOWN_MASK;
820
821                 skb_queue_purge(&sk->sk_receive_queue);
822
823                 /* Clean up any sockets that never were accepted. */
824                 while ((pending = vsock_dequeue_accept(sk)) != NULL) {
825                         __vsock_release(pending, SINGLE_DEPTH_NESTING);
826                         sock_put(pending);
827                 }
828
829                 release_sock(sk);
830                 sock_put(sk);
831         }
832 }
833
834 static void vsock_sk_destruct(struct sock *sk)
835 {
836         struct vsock_sock *vsk = vsock_sk(sk);
837
838         vsock_deassign_transport(vsk);
839
840         /* When clearing these addresses, there's no need to set the family and
841          * possibly register the address family with the kernel.
842          */
843         vsock_addr_init(&vsk->local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
844         vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
845
846         put_cred(vsk->owner);
847 }
848
849 static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
850 {
851         int err;
852
853         err = sock_queue_rcv_skb(sk, skb);
854         if (err)
855                 kfree_skb(skb);
856
857         return err;
858 }
859
860 struct sock *vsock_create_connected(struct sock *parent)
861 {
862         return __vsock_create(sock_net(parent), NULL, parent, GFP_KERNEL,
863                               parent->sk_type, 0);
864 }
865 EXPORT_SYMBOL_GPL(vsock_create_connected);
866
867 s64 vsock_stream_has_data(struct vsock_sock *vsk)
868 {
869         return vsk->transport->stream_has_data(vsk);
870 }
871 EXPORT_SYMBOL_GPL(vsock_stream_has_data);
872
873 s64 vsock_connectible_has_data(struct vsock_sock *vsk)
874 {
875         struct sock *sk = sk_vsock(vsk);
876
877         if (sk->sk_type == SOCK_SEQPACKET)
878                 return vsk->transport->seqpacket_has_data(vsk);
879         else
880                 return vsock_stream_has_data(vsk);
881 }
882 EXPORT_SYMBOL_GPL(vsock_connectible_has_data);
883
884 s64 vsock_stream_has_space(struct vsock_sock *vsk)
885 {
886         return vsk->transport->stream_has_space(vsk);
887 }
888 EXPORT_SYMBOL_GPL(vsock_stream_has_space);
889
890 void vsock_data_ready(struct sock *sk)
891 {
892         struct vsock_sock *vsk = vsock_sk(sk);
893
894         if (vsock_stream_has_data(vsk) >= sk->sk_rcvlowat ||
895             sock_flag(sk, SOCK_DONE))
896                 sk->sk_data_ready(sk);
897 }
898 EXPORT_SYMBOL_GPL(vsock_data_ready);
899
900 static int vsock_release(struct socket *sock)
901 {
902         __vsock_release(sock->sk, 0);
903         sock->sk = NULL;
904         sock->state = SS_FREE;
905
906         return 0;
907 }
908
909 static int
910 vsock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
911 {
912         int err;
913         struct sock *sk;
914         struct sockaddr_vm *vm_addr;
915
916         sk = sock->sk;
917
918         if (vsock_addr_cast(addr, addr_len, &vm_addr) != 0)
919                 return -EINVAL;
920
921         lock_sock(sk);
922         err = __vsock_bind(sk, vm_addr);
923         release_sock(sk);
924
925         return err;
926 }
927
928 static int vsock_getname(struct socket *sock,
929                          struct sockaddr *addr, int peer)
930 {
931         int err;
932         struct sock *sk;
933         struct vsock_sock *vsk;
934         struct sockaddr_vm *vm_addr;
935
936         sk = sock->sk;
937         vsk = vsock_sk(sk);
938         err = 0;
939
940         lock_sock(sk);
941
942         if (peer) {
943                 if (sock->state != SS_CONNECTED) {
944                         err = -ENOTCONN;
945                         goto out;
946                 }
947                 vm_addr = &vsk->remote_addr;
948         } else {
949                 vm_addr = &vsk->local_addr;
950         }
951
952         if (!vm_addr) {
953                 err = -EINVAL;
954                 goto out;
955         }
956
957         /* sys_getsockname() and sys_getpeername() pass us a
958          * MAX_SOCK_ADDR-sized buffer and don't set addr_len.  Unfortunately
959          * that macro is defined in socket.c instead of .h, so we hardcode its
960          * value here.
961          */
962         BUILD_BUG_ON(sizeof(*vm_addr) > 128);
963         memcpy(addr, vm_addr, sizeof(*vm_addr));
964         err = sizeof(*vm_addr);
965
966 out:
967         release_sock(sk);
968         return err;
969 }
970
971 static int vsock_shutdown(struct socket *sock, int mode)
972 {
973         int err;
974         struct sock *sk;
975
976         /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
977          * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
978          * here like the other address families do.  Note also that the
979          * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
980          * which is what we want.
981          */
982         mode++;
983
984         if ((mode & ~SHUTDOWN_MASK) || !mode)
985                 return -EINVAL;
986
987         /* If this is a connection oriented socket and it is not connected then
988          * bail out immediately.  If it is a DGRAM socket then we must first
989          * kick the socket so that it wakes up from any sleeping calls, for
990          * example recv(), and then afterwards return the error.
991          */
992
993         sk = sock->sk;
994
995         lock_sock(sk);
996         if (sock->state == SS_UNCONNECTED) {
997                 err = -ENOTCONN;
998                 if (sock_type_connectible(sk->sk_type))
999                         goto out;
1000         } else {
1001                 sock->state = SS_DISCONNECTING;
1002                 err = 0;
1003         }
1004
1005         /* Receive and send shutdowns are treated alike. */
1006         mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
1007         if (mode) {
1008                 sk->sk_shutdown |= mode;
1009                 sk->sk_state_change(sk);
1010
1011                 if (sock_type_connectible(sk->sk_type)) {
1012                         sock_reset_flag(sk, SOCK_DONE);
1013                         vsock_send_shutdown(sk, mode);
1014                 }
1015         }
1016
1017 out:
1018         release_sock(sk);
1019         return err;
1020 }
1021
1022 static __poll_t vsock_poll(struct file *file, struct socket *sock,
1023                                poll_table *wait)
1024 {
1025         struct sock *sk;
1026         __poll_t mask;
1027         struct vsock_sock *vsk;
1028
1029         sk = sock->sk;
1030         vsk = vsock_sk(sk);
1031
1032         poll_wait(file, sk_sleep(sk), wait);
1033         mask = 0;
1034
1035         if (sk->sk_err)
1036                 /* Signify that there has been an error on this socket. */
1037                 mask |= EPOLLERR;
1038
1039         /* INET sockets treat local write shutdown and peer write shutdown as a
1040          * case of EPOLLHUP set.
1041          */
1042         if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1043             ((sk->sk_shutdown & SEND_SHUTDOWN) &&
1044              (vsk->peer_shutdown & SEND_SHUTDOWN))) {
1045                 mask |= EPOLLHUP;
1046         }
1047
1048         if (sk->sk_shutdown & RCV_SHUTDOWN ||
1049             vsk->peer_shutdown & SEND_SHUTDOWN) {
1050                 mask |= EPOLLRDHUP;
1051         }
1052
1053         if (sock->type == SOCK_DGRAM) {
1054                 /* For datagram sockets we can read if there is something in
1055                  * the queue and write as long as the socket isn't shutdown for
1056                  * sending.
1057                  */
1058                 if (!skb_queue_empty_lockless(&sk->sk_receive_queue) ||
1059                     (sk->sk_shutdown & RCV_SHUTDOWN)) {
1060                         mask |= EPOLLIN | EPOLLRDNORM;
1061                 }
1062
1063                 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
1064                         mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1065
1066         } else if (sock_type_connectible(sk->sk_type)) {
1067                 const struct vsock_transport *transport;
1068
1069                 lock_sock(sk);
1070
1071                 transport = vsk->transport;
1072
1073                 /* Listening sockets that have connections in their accept
1074                  * queue can be read.
1075                  */
1076                 if (sk->sk_state == TCP_LISTEN
1077                     && !vsock_is_accept_queue_empty(sk))
1078                         mask |= EPOLLIN | EPOLLRDNORM;
1079
1080                 /* If there is something in the queue then we can read. */
1081                 if (transport && transport->stream_is_active(vsk) &&
1082                     !(sk->sk_shutdown & RCV_SHUTDOWN)) {
1083                         bool data_ready_now = false;
1084                         int target = sock_rcvlowat(sk, 0, INT_MAX);
1085                         int ret = transport->notify_poll_in(
1086                                         vsk, target, &data_ready_now);
1087                         if (ret < 0) {
1088                                 mask |= EPOLLERR;
1089                         } else {
1090                                 if (data_ready_now)
1091                                         mask |= EPOLLIN | EPOLLRDNORM;
1092
1093                         }
1094                 }
1095
1096                 /* Sockets whose connections have been closed, reset, or
1097                  * terminated should also be considered read, and we check the
1098                  * shutdown flag for that.
1099                  */
1100                 if (sk->sk_shutdown & RCV_SHUTDOWN ||
1101                     vsk->peer_shutdown & SEND_SHUTDOWN) {
1102                         mask |= EPOLLIN | EPOLLRDNORM;
1103                 }
1104
1105                 /* Connected sockets that can produce data can be written. */
1106                 if (transport && sk->sk_state == TCP_ESTABLISHED) {
1107                         if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
1108                                 bool space_avail_now = false;
1109                                 int ret = transport->notify_poll_out(
1110                                                 vsk, 1, &space_avail_now);
1111                                 if (ret < 0) {
1112                                         mask |= EPOLLERR;
1113                                 } else {
1114                                         if (space_avail_now)
1115                                                 /* Remove EPOLLWRBAND since INET
1116                                                  * sockets are not setting it.
1117                                                  */
1118                                                 mask |= EPOLLOUT | EPOLLWRNORM;
1119
1120                                 }
1121                         }
1122                 }
1123
1124                 /* Simulate INET socket poll behaviors, which sets
1125                  * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read,
1126                  * but local send is not shutdown.
1127                  */
1128                 if (sk->sk_state == TCP_CLOSE || sk->sk_state == TCP_CLOSING) {
1129                         if (!(sk->sk_shutdown & SEND_SHUTDOWN))
1130                                 mask |= EPOLLOUT | EPOLLWRNORM;
1131
1132                 }
1133
1134                 release_sock(sk);
1135         }
1136
1137         return mask;
1138 }
1139
1140 static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor)
1141 {
1142         struct vsock_sock *vsk = vsock_sk(sk);
1143
1144         return vsk->transport->read_skb(vsk, read_actor);
1145 }
1146
1147 static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1148                                size_t len)
1149 {
1150         int err;
1151         struct sock *sk;
1152         struct vsock_sock *vsk;
1153         struct sockaddr_vm *remote_addr;
1154         const struct vsock_transport *transport;
1155
1156         if (msg->msg_flags & MSG_OOB)
1157                 return -EOPNOTSUPP;
1158
1159         /* For now, MSG_DONTWAIT is always assumed... */
1160         err = 0;
1161         sk = sock->sk;
1162         vsk = vsock_sk(sk);
1163
1164         lock_sock(sk);
1165
1166         transport = vsk->transport;
1167
1168         err = vsock_auto_bind(vsk);
1169         if (err)
1170                 goto out;
1171
1172
1173         /* If the provided message contains an address, use that.  Otherwise
1174          * fall back on the socket's remote handle (if it has been connected).
1175          */
1176         if (msg->msg_name &&
1177             vsock_addr_cast(msg->msg_name, msg->msg_namelen,
1178                             &remote_addr) == 0) {
1179                 /* Ensure this address is of the right type and is a valid
1180                  * destination.
1181                  */
1182
1183                 if (remote_addr->svm_cid == VMADDR_CID_ANY)
1184                         remote_addr->svm_cid = transport->get_local_cid();
1185
1186                 if (!vsock_addr_bound(remote_addr)) {
1187                         err = -EINVAL;
1188                         goto out;
1189                 }
1190         } else if (sock->state == SS_CONNECTED) {
1191                 remote_addr = &vsk->remote_addr;
1192
1193                 if (remote_addr->svm_cid == VMADDR_CID_ANY)
1194                         remote_addr->svm_cid = transport->get_local_cid();
1195
1196                 /* XXX Should connect() or this function ensure remote_addr is
1197                  * bound?
1198                  */
1199                 if (!vsock_addr_bound(&vsk->remote_addr)) {
1200                         err = -EINVAL;
1201                         goto out;
1202                 }
1203         } else {
1204                 err = -EINVAL;
1205                 goto out;
1206         }
1207
1208         if (!transport->dgram_allow(remote_addr->svm_cid,
1209                                     remote_addr->svm_port)) {
1210                 err = -EINVAL;
1211                 goto out;
1212         }
1213
1214         err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
1215
1216 out:
1217         release_sock(sk);
1218         return err;
1219 }
1220
1221 static int vsock_dgram_connect(struct socket *sock,
1222                                struct sockaddr *addr, int addr_len, int flags)
1223 {
1224         int err;
1225         struct sock *sk;
1226         struct vsock_sock *vsk;
1227         struct sockaddr_vm *remote_addr;
1228
1229         sk = sock->sk;
1230         vsk = vsock_sk(sk);
1231
1232         err = vsock_addr_cast(addr, addr_len, &remote_addr);
1233         if (err == -EAFNOSUPPORT && remote_addr->svm_family == AF_UNSPEC) {
1234                 lock_sock(sk);
1235                 vsock_addr_init(&vsk->remote_addr, VMADDR_CID_ANY,
1236                                 VMADDR_PORT_ANY);
1237                 sock->state = SS_UNCONNECTED;
1238                 release_sock(sk);
1239                 return 0;
1240         } else if (err != 0)
1241                 return -EINVAL;
1242
1243         lock_sock(sk);
1244
1245         err = vsock_auto_bind(vsk);
1246         if (err)
1247                 goto out;
1248
1249         if (!vsk->transport->dgram_allow(remote_addr->svm_cid,
1250                                          remote_addr->svm_port)) {
1251                 err = -EINVAL;
1252                 goto out;
1253         }
1254
1255         memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr));
1256         sock->state = SS_CONNECTED;
1257
1258         /* sock map disallows redirection of non-TCP sockets with sk_state !=
1259          * TCP_ESTABLISHED (see sock_map_redirect_allowed()), so we set
1260          * TCP_ESTABLISHED here to allow redirection of connected vsock dgrams.
1261          *
1262          * This doesn't seem to be abnormal state for datagram sockets, as the
1263          * same approach can be see in other datagram socket types as well
1264          * (such as unix sockets).
1265          */
1266         sk->sk_state = TCP_ESTABLISHED;
1267
1268 out:
1269         release_sock(sk);
1270         return err;
1271 }
1272
1273 int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
1274                         size_t len, int flags)
1275 {
1276 #ifdef CONFIG_BPF_SYSCALL
1277         const struct proto *prot;
1278 #endif
1279         struct vsock_sock *vsk;
1280         struct sock *sk;
1281
1282         sk = sock->sk;
1283         vsk = vsock_sk(sk);
1284
1285 #ifdef CONFIG_BPF_SYSCALL
1286         prot = READ_ONCE(sk->sk_prot);
1287         if (prot != &vsock_proto)
1288                 return prot->recvmsg(sk, msg, len, flags, NULL);
1289 #endif
1290
1291         return vsk->transport->dgram_dequeue(vsk, msg, len, flags);
1292 }
1293 EXPORT_SYMBOL_GPL(vsock_dgram_recvmsg);
1294
1295 static const struct proto_ops vsock_dgram_ops = {
1296         .family = PF_VSOCK,
1297         .owner = THIS_MODULE,
1298         .release = vsock_release,
1299         .bind = vsock_bind,
1300         .connect = vsock_dgram_connect,
1301         .socketpair = sock_no_socketpair,
1302         .accept = sock_no_accept,
1303         .getname = vsock_getname,
1304         .poll = vsock_poll,
1305         .ioctl = sock_no_ioctl,
1306         .listen = sock_no_listen,
1307         .shutdown = vsock_shutdown,
1308         .sendmsg = vsock_dgram_sendmsg,
1309         .recvmsg = vsock_dgram_recvmsg,
1310         .mmap = sock_no_mmap,
1311         .read_skb = vsock_read_skb,
1312 };
1313
1314 static int vsock_transport_cancel_pkt(struct vsock_sock *vsk)
1315 {
1316         const struct vsock_transport *transport = vsk->transport;
1317
1318         if (!transport || !transport->cancel_pkt)
1319                 return -EOPNOTSUPP;
1320
1321         return transport->cancel_pkt(vsk);
1322 }
1323
1324 static void vsock_connect_timeout(struct work_struct *work)
1325 {
1326         struct sock *sk;
1327         struct vsock_sock *vsk;
1328
1329         vsk = container_of(work, struct vsock_sock, connect_work.work);
1330         sk = sk_vsock(vsk);
1331
1332         lock_sock(sk);
1333         if (sk->sk_state == TCP_SYN_SENT &&
1334             (sk->sk_shutdown != SHUTDOWN_MASK)) {
1335                 sk->sk_state = TCP_CLOSE;
1336                 sk->sk_socket->state = SS_UNCONNECTED;
1337                 sk->sk_err = ETIMEDOUT;
1338                 sk_error_report(sk);
1339                 vsock_transport_cancel_pkt(vsk);
1340         }
1341         release_sock(sk);
1342
1343         sock_put(sk);
1344 }
1345
1346 static int vsock_connect(struct socket *sock, struct sockaddr *addr,
1347                          int addr_len, int flags)
1348 {
1349         int err;
1350         struct sock *sk;
1351         struct vsock_sock *vsk;
1352         const struct vsock_transport *transport;
1353         struct sockaddr_vm *remote_addr;
1354         long timeout;
1355         DEFINE_WAIT(wait);
1356
1357         err = 0;
1358         sk = sock->sk;
1359         vsk = vsock_sk(sk);
1360
1361         lock_sock(sk);
1362
1363         /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
1364         switch (sock->state) {
1365         case SS_CONNECTED:
1366                 err = -EISCONN;
1367                 goto out;
1368         case SS_DISCONNECTING:
1369                 err = -EINVAL;
1370                 goto out;
1371         case SS_CONNECTING:
1372                 /* This continues on so we can move sock into the SS_CONNECTED
1373                  * state once the connection has completed (at which point err
1374                  * will be set to zero also).  Otherwise, we will either wait
1375                  * for the connection or return -EALREADY should this be a
1376                  * non-blocking call.
1377                  */
1378                 err = -EALREADY;
1379                 if (flags & O_NONBLOCK)
1380                         goto out;
1381                 break;
1382         default:
1383                 if ((sk->sk_state == TCP_LISTEN) ||
1384                     vsock_addr_cast(addr, addr_len, &remote_addr) != 0) {
1385                         err = -EINVAL;
1386                         goto out;
1387                 }
1388
1389                 /* Set the remote address that we are connecting to. */
1390                 memcpy(&vsk->remote_addr, remote_addr,
1391                        sizeof(vsk->remote_addr));
1392
1393                 err = vsock_assign_transport(vsk, NULL);
1394                 if (err)
1395                         goto out;
1396
1397                 transport = vsk->transport;
1398
1399                 /* The hypervisor and well-known contexts do not have socket
1400                  * endpoints.
1401                  */
1402                 if (!transport ||
1403                     !transport->stream_allow(remote_addr->svm_cid,
1404                                              remote_addr->svm_port)) {
1405                         err = -ENETUNREACH;
1406                         goto out;
1407                 }
1408
1409                 err = vsock_auto_bind(vsk);
1410                 if (err)
1411                         goto out;
1412
1413                 sk->sk_state = TCP_SYN_SENT;
1414
1415                 err = transport->connect(vsk);
1416                 if (err < 0)
1417                         goto out;
1418
1419                 /* Mark sock as connecting and set the error code to in
1420                  * progress in case this is a non-blocking connect.
1421                  */
1422                 sock->state = SS_CONNECTING;
1423                 err = -EINPROGRESS;
1424         }
1425
1426         /* The receive path will handle all communication until we are able to
1427          * enter the connected state.  Here we wait for the connection to be
1428          * completed or a notification of an error.
1429          */
1430         timeout = vsk->connect_timeout;
1431         prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1432
1433         while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) {
1434                 if (flags & O_NONBLOCK) {
1435                         /* If we're not going to block, we schedule a timeout
1436                          * function to generate a timeout on the connection
1437                          * attempt, in case the peer doesn't respond in a
1438                          * timely manner. We hold on to the socket until the
1439                          * timeout fires.
1440                          */
1441                         sock_hold(sk);
1442
1443                         /* If the timeout function is already scheduled,
1444                          * reschedule it, then ungrab the socket refcount to
1445                          * keep it balanced.
1446                          */
1447                         if (mod_delayed_work(system_wq, &vsk->connect_work,
1448                                              timeout))
1449                                 sock_put(sk);
1450
1451                         /* Skip ahead to preserve error code set above. */
1452                         goto out_wait;
1453                 }
1454
1455                 release_sock(sk);
1456                 timeout = schedule_timeout(timeout);
1457                 lock_sock(sk);
1458
1459                 if (signal_pending(current)) {
1460                         err = sock_intr_errno(timeout);
1461                         sk->sk_state = sk->sk_state == TCP_ESTABLISHED ? TCP_CLOSING : TCP_CLOSE;
1462                         sock->state = SS_UNCONNECTED;
1463                         vsock_transport_cancel_pkt(vsk);
1464                         vsock_remove_connected(vsk);
1465                         goto out_wait;
1466                 } else if ((sk->sk_state != TCP_ESTABLISHED) && (timeout == 0)) {
1467                         err = -ETIMEDOUT;
1468                         sk->sk_state = TCP_CLOSE;
1469                         sock->state = SS_UNCONNECTED;
1470                         vsock_transport_cancel_pkt(vsk);
1471                         goto out_wait;
1472                 }
1473
1474                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1475         }
1476
1477         if (sk->sk_err) {
1478                 err = -sk->sk_err;
1479                 sk->sk_state = TCP_CLOSE;
1480                 sock->state = SS_UNCONNECTED;
1481         } else {
1482                 err = 0;
1483         }
1484
1485 out_wait:
1486         finish_wait(sk_sleep(sk), &wait);
1487 out:
1488         release_sock(sk);
1489         return err;
1490 }
1491
1492 static int vsock_accept(struct socket *sock, struct socket *newsock, int flags,
1493                         bool kern)
1494 {
1495         struct sock *listener;
1496         int err;
1497         struct sock *connected;
1498         struct vsock_sock *vconnected;
1499         long timeout;
1500         DEFINE_WAIT(wait);
1501
1502         err = 0;
1503         listener = sock->sk;
1504
1505         lock_sock(listener);
1506
1507         if (!sock_type_connectible(sock->type)) {
1508                 err = -EOPNOTSUPP;
1509                 goto out;
1510         }
1511
1512         if (listener->sk_state != TCP_LISTEN) {
1513                 err = -EINVAL;
1514                 goto out;
1515         }
1516
1517         /* Wait for children sockets to appear; these are the new sockets
1518          * created upon connection establishment.
1519          */
1520         timeout = sock_rcvtimeo(listener, flags & O_NONBLOCK);
1521         prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
1522
1523         while ((connected = vsock_dequeue_accept(listener)) == NULL &&
1524                listener->sk_err == 0) {
1525                 release_sock(listener);
1526                 timeout = schedule_timeout(timeout);
1527                 finish_wait(sk_sleep(listener), &wait);
1528                 lock_sock(listener);
1529
1530                 if (signal_pending(current)) {
1531                         err = sock_intr_errno(timeout);
1532                         goto out;
1533                 } else if (timeout == 0) {
1534                         err = -EAGAIN;
1535                         goto out;
1536                 }
1537
1538                 prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
1539         }
1540         finish_wait(sk_sleep(listener), &wait);
1541
1542         if (listener->sk_err)
1543                 err = -listener->sk_err;
1544
1545         if (connected) {
1546                 sk_acceptq_removed(listener);
1547
1548                 lock_sock_nested(connected, SINGLE_DEPTH_NESTING);
1549                 vconnected = vsock_sk(connected);
1550
1551                 /* If the listener socket has received an error, then we should
1552                  * reject this socket and return.  Note that we simply mark the
1553                  * socket rejected, drop our reference, and let the cleanup
1554                  * function handle the cleanup; the fact that we found it in
1555                  * the listener's accept queue guarantees that the cleanup
1556                  * function hasn't run yet.
1557                  */
1558                 if (err) {
1559                         vconnected->rejected = true;
1560                 } else {
1561                         newsock->state = SS_CONNECTED;
1562                         sock_graft(connected, newsock);
1563                 }
1564
1565                 release_sock(connected);
1566                 sock_put(connected);
1567         }
1568
1569 out:
1570         release_sock(listener);
1571         return err;
1572 }
1573
1574 static int vsock_listen(struct socket *sock, int backlog)
1575 {
1576         int err;
1577         struct sock *sk;
1578         struct vsock_sock *vsk;
1579
1580         sk = sock->sk;
1581
1582         lock_sock(sk);
1583
1584         if (!sock_type_connectible(sk->sk_type)) {
1585                 err = -EOPNOTSUPP;
1586                 goto out;
1587         }
1588
1589         if (sock->state != SS_UNCONNECTED) {
1590                 err = -EINVAL;
1591                 goto out;
1592         }
1593
1594         vsk = vsock_sk(sk);
1595
1596         if (!vsock_addr_bound(&vsk->local_addr)) {
1597                 err = -EINVAL;
1598                 goto out;
1599         }
1600
1601         sk->sk_max_ack_backlog = backlog;
1602         sk->sk_state = TCP_LISTEN;
1603
1604         err = 0;
1605
1606 out:
1607         release_sock(sk);
1608         return err;
1609 }
1610
1611 static void vsock_update_buffer_size(struct vsock_sock *vsk,
1612                                      const struct vsock_transport *transport,
1613                                      u64 val)
1614 {
1615         if (val > vsk->buffer_max_size)
1616                 val = vsk->buffer_max_size;
1617
1618         if (val < vsk->buffer_min_size)
1619                 val = vsk->buffer_min_size;
1620
1621         if (val != vsk->buffer_size &&
1622             transport && transport->notify_buffer_size)
1623                 transport->notify_buffer_size(vsk, &val);
1624
1625         vsk->buffer_size = val;
1626 }
1627
1628 static int vsock_connectible_setsockopt(struct socket *sock,
1629                                         int level,
1630                                         int optname,
1631                                         sockptr_t optval,
1632                                         unsigned int optlen)
1633 {
1634         int err;
1635         struct sock *sk;
1636         struct vsock_sock *vsk;
1637         const struct vsock_transport *transport;
1638         u64 val;
1639
1640         if (level != AF_VSOCK)
1641                 return -ENOPROTOOPT;
1642
1643 #define COPY_IN(_v)                                       \
1644         do {                                              \
1645                 if (optlen < sizeof(_v)) {                \
1646                         err = -EINVAL;                    \
1647                         goto exit;                        \
1648                 }                                         \
1649                 if (copy_from_sockptr(&_v, optval, sizeof(_v)) != 0) {  \
1650                         err = -EFAULT;                                  \
1651                         goto exit;                                      \
1652                 }                                                       \
1653         } while (0)
1654
1655         err = 0;
1656         sk = sock->sk;
1657         vsk = vsock_sk(sk);
1658
1659         lock_sock(sk);
1660
1661         transport = vsk->transport;
1662
1663         switch (optname) {
1664         case SO_VM_SOCKETS_BUFFER_SIZE:
1665                 COPY_IN(val);
1666                 vsock_update_buffer_size(vsk, transport, val);
1667                 break;
1668
1669         case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
1670                 COPY_IN(val);
1671                 vsk->buffer_max_size = val;
1672                 vsock_update_buffer_size(vsk, transport, vsk->buffer_size);
1673                 break;
1674
1675         case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
1676                 COPY_IN(val);
1677                 vsk->buffer_min_size = val;
1678                 vsock_update_buffer_size(vsk, transport, vsk->buffer_size);
1679                 break;
1680
1681         case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW:
1682         case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD: {
1683                 struct __kernel_sock_timeval tv;
1684
1685                 err = sock_copy_user_timeval(&tv, optval, optlen,
1686                                              optname == SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD);
1687                 if (err)
1688                         break;
1689                 if (tv.tv_sec >= 0 && tv.tv_usec < USEC_PER_SEC &&
1690                     tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)) {
1691                         vsk->connect_timeout = tv.tv_sec * HZ +
1692                                 DIV_ROUND_UP((unsigned long)tv.tv_usec, (USEC_PER_SEC / HZ));
1693                         if (vsk->connect_timeout == 0)
1694                                 vsk->connect_timeout =
1695                                     VSOCK_DEFAULT_CONNECT_TIMEOUT;
1696
1697                 } else {
1698                         err = -ERANGE;
1699                 }
1700                 break;
1701         }
1702
1703         default:
1704                 err = -ENOPROTOOPT;
1705                 break;
1706         }
1707
1708 #undef COPY_IN
1709
1710 exit:
1711         release_sock(sk);
1712         return err;
1713 }
1714
1715 static int vsock_connectible_getsockopt(struct socket *sock,
1716                                         int level, int optname,
1717                                         char __user *optval,
1718                                         int __user *optlen)
1719 {
1720         struct sock *sk = sock->sk;
1721         struct vsock_sock *vsk = vsock_sk(sk);
1722
1723         union {
1724                 u64 val64;
1725                 struct old_timeval32 tm32;
1726                 struct __kernel_old_timeval tm;
1727                 struct  __kernel_sock_timeval stm;
1728         } v;
1729
1730         int lv = sizeof(v.val64);
1731         int len;
1732
1733         if (level != AF_VSOCK)
1734                 return -ENOPROTOOPT;
1735
1736         if (get_user(len, optlen))
1737                 return -EFAULT;
1738
1739         memset(&v, 0, sizeof(v));
1740
1741         switch (optname) {
1742         case SO_VM_SOCKETS_BUFFER_SIZE:
1743                 v.val64 = vsk->buffer_size;
1744                 break;
1745
1746         case SO_VM_SOCKETS_BUFFER_MAX_SIZE:
1747                 v.val64 = vsk->buffer_max_size;
1748                 break;
1749
1750         case SO_VM_SOCKETS_BUFFER_MIN_SIZE:
1751                 v.val64 = vsk->buffer_min_size;
1752                 break;
1753
1754         case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW:
1755         case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD:
1756                 lv = sock_get_timeout(vsk->connect_timeout, &v,
1757                                       optname == SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD);
1758                 break;
1759
1760         default:
1761                 return -ENOPROTOOPT;
1762         }
1763
1764         if (len < lv)
1765                 return -EINVAL;
1766         if (len > lv)
1767                 len = lv;
1768         if (copy_to_user(optval, &v, len))
1769                 return -EFAULT;
1770
1771         if (put_user(len, optlen))
1772                 return -EFAULT;
1773
1774         return 0;
1775 }
1776
1777 static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
1778                                      size_t len)
1779 {
1780         struct sock *sk;
1781         struct vsock_sock *vsk;
1782         const struct vsock_transport *transport;
1783         ssize_t total_written;
1784         long timeout;
1785         int err;
1786         struct vsock_transport_send_notify_data send_data;
1787         DEFINE_WAIT_FUNC(wait, woken_wake_function);
1788
1789         sk = sock->sk;
1790         vsk = vsock_sk(sk);
1791         total_written = 0;
1792         err = 0;
1793
1794         if (msg->msg_flags & MSG_OOB)
1795                 return -EOPNOTSUPP;
1796
1797         lock_sock(sk);
1798
1799         transport = vsk->transport;
1800
1801         /* Callers should not provide a destination with connection oriented
1802          * sockets.
1803          */
1804         if (msg->msg_namelen) {
1805                 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
1806                 goto out;
1807         }
1808
1809         /* Send data only if both sides are not shutdown in the direction. */
1810         if (sk->sk_shutdown & SEND_SHUTDOWN ||
1811             vsk->peer_shutdown & RCV_SHUTDOWN) {
1812                 err = -EPIPE;
1813                 goto out;
1814         }
1815
1816         if (!transport || sk->sk_state != TCP_ESTABLISHED ||
1817             !vsock_addr_bound(&vsk->local_addr)) {
1818                 err = -ENOTCONN;
1819                 goto out;
1820         }
1821
1822         if (!vsock_addr_bound(&vsk->remote_addr)) {
1823                 err = -EDESTADDRREQ;
1824                 goto out;
1825         }
1826
1827         /* Wait for room in the produce queue to enqueue our user's data. */
1828         timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1829
1830         err = transport->notify_send_init(vsk, &send_data);
1831         if (err < 0)
1832                 goto out;
1833
1834         while (total_written < len) {
1835                 ssize_t written;
1836
1837                 add_wait_queue(sk_sleep(sk), &wait);
1838                 while (vsock_stream_has_space(vsk) == 0 &&
1839                        sk->sk_err == 0 &&
1840                        !(sk->sk_shutdown & SEND_SHUTDOWN) &&
1841                        !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
1842
1843                         /* Don't wait for non-blocking sockets. */
1844                         if (timeout == 0) {
1845                                 err = -EAGAIN;
1846                                 remove_wait_queue(sk_sleep(sk), &wait);
1847                                 goto out_err;
1848                         }
1849
1850                         err = transport->notify_send_pre_block(vsk, &send_data);
1851                         if (err < 0) {
1852                                 remove_wait_queue(sk_sleep(sk), &wait);
1853                                 goto out_err;
1854                         }
1855
1856                         release_sock(sk);
1857                         timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
1858                         lock_sock(sk);
1859                         if (signal_pending(current)) {
1860                                 err = sock_intr_errno(timeout);
1861                                 remove_wait_queue(sk_sleep(sk), &wait);
1862                                 goto out_err;
1863                         } else if (timeout == 0) {
1864                                 err = -EAGAIN;
1865                                 remove_wait_queue(sk_sleep(sk), &wait);
1866                                 goto out_err;
1867                         }
1868                 }
1869                 remove_wait_queue(sk_sleep(sk), &wait);
1870
1871                 /* These checks occur both as part of and after the loop
1872                  * conditional since we need to check before and after
1873                  * sleeping.
1874                  */
1875                 if (sk->sk_err) {
1876                         err = -sk->sk_err;
1877                         goto out_err;
1878                 } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
1879                            (vsk->peer_shutdown & RCV_SHUTDOWN)) {
1880                         err = -EPIPE;
1881                         goto out_err;
1882                 }
1883
1884                 err = transport->notify_send_pre_enqueue(vsk, &send_data);
1885                 if (err < 0)
1886                         goto out_err;
1887
1888                 /* Note that enqueue will only write as many bytes as are free
1889                  * in the produce queue, so we don't need to ensure len is
1890                  * smaller than the queue size.  It is the caller's
1891                  * responsibility to check how many bytes we were able to send.
1892                  */
1893
1894                 if (sk->sk_type == SOCK_SEQPACKET) {
1895                         written = transport->seqpacket_enqueue(vsk,
1896                                                 msg, len - total_written);
1897                 } else {
1898                         written = transport->stream_enqueue(vsk,
1899                                         msg, len - total_written);
1900                 }
1901
1902                 if (written < 0) {
1903                         err = written;
1904                         goto out_err;
1905                 }
1906
1907                 total_written += written;
1908
1909                 err = transport->notify_send_post_enqueue(
1910                                 vsk, written, &send_data);
1911                 if (err < 0)
1912                         goto out_err;
1913
1914         }
1915
1916 out_err:
1917         if (total_written > 0) {
1918                 /* Return number of written bytes only if:
1919                  * 1) SOCK_STREAM socket.
1920                  * 2) SOCK_SEQPACKET socket when whole buffer is sent.
1921                  */
1922                 if (sk->sk_type == SOCK_STREAM || total_written == len)
1923                         err = total_written;
1924         }
1925 out:
1926         release_sock(sk);
1927         return err;
1928 }
1929
1930 static int vsock_connectible_wait_data(struct sock *sk,
1931                                        struct wait_queue_entry *wait,
1932                                        long timeout,
1933                                        struct vsock_transport_recv_notify_data *recv_data,
1934                                        size_t target)
1935 {
1936         const struct vsock_transport *transport;
1937         struct vsock_sock *vsk;
1938         s64 data;
1939         int err;
1940
1941         vsk = vsock_sk(sk);
1942         err = 0;
1943         transport = vsk->transport;
1944
1945         while (1) {
1946                 prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE);
1947                 data = vsock_connectible_has_data(vsk);
1948                 if (data != 0)
1949                         break;
1950
1951                 if (sk->sk_err != 0 ||
1952                     (sk->sk_shutdown & RCV_SHUTDOWN) ||
1953                     (vsk->peer_shutdown & SEND_SHUTDOWN)) {
1954                         break;
1955                 }
1956
1957                 /* Don't wait for non-blocking sockets. */
1958                 if (timeout == 0) {
1959                         err = -EAGAIN;
1960                         break;
1961                 }
1962
1963                 if (recv_data) {
1964                         err = transport->notify_recv_pre_block(vsk, target, recv_data);
1965                         if (err < 0)
1966                                 break;
1967                 }
1968
1969                 release_sock(sk);
1970                 timeout = schedule_timeout(timeout);
1971                 lock_sock(sk);
1972
1973                 if (signal_pending(current)) {
1974                         err = sock_intr_errno(timeout);
1975                         break;
1976                 } else if (timeout == 0) {
1977                         err = -EAGAIN;
1978                         break;
1979                 }
1980         }
1981
1982         finish_wait(sk_sleep(sk), wait);
1983
1984         if (err)
1985                 return err;
1986
1987         /* Internal transport error when checking for available
1988          * data. XXX This should be changed to a connection
1989          * reset in a later change.
1990          */
1991         if (data < 0)
1992                 return -ENOMEM;
1993
1994         return data;
1995 }
1996
1997 static int __vsock_stream_recvmsg(struct sock *sk, struct msghdr *msg,
1998                                   size_t len, int flags)
1999 {
2000         struct vsock_transport_recv_notify_data recv_data;
2001         const struct vsock_transport *transport;
2002         struct vsock_sock *vsk;
2003         ssize_t copied;
2004         size_t target;
2005         long timeout;
2006         int err;
2007
2008         DEFINE_WAIT(wait);
2009
2010         vsk = vsock_sk(sk);
2011         transport = vsk->transport;
2012
2013         /* We must not copy less than target bytes into the user's buffer
2014          * before returning successfully, so we wait for the consume queue to
2015          * have that much data to consume before dequeueing.  Note that this
2016          * makes it impossible to handle cases where target is greater than the
2017          * queue size.
2018          */
2019         target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
2020         if (target >= transport->stream_rcvhiwat(vsk)) {
2021                 err = -ENOMEM;
2022                 goto out;
2023         }
2024         timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2025         copied = 0;
2026
2027         err = transport->notify_recv_init(vsk, target, &recv_data);
2028         if (err < 0)
2029                 goto out;
2030
2031
2032         while (1) {
2033                 ssize_t read;
2034
2035                 err = vsock_connectible_wait_data(sk, &wait, timeout,
2036                                                   &recv_data, target);
2037                 if (err <= 0)
2038                         break;
2039
2040                 err = transport->notify_recv_pre_dequeue(vsk, target,
2041                                                          &recv_data);
2042                 if (err < 0)
2043                         break;
2044
2045                 read = transport->stream_dequeue(vsk, msg, len - copied, flags);
2046                 if (read < 0) {
2047                         err = read;
2048                         break;
2049                 }
2050
2051                 copied += read;
2052
2053                 err = transport->notify_recv_post_dequeue(vsk, target, read,
2054                                                 !(flags & MSG_PEEK), &recv_data);
2055                 if (err < 0)
2056                         goto out;
2057
2058                 if (read >= target || flags & MSG_PEEK)
2059                         break;
2060
2061                 target -= read;
2062         }
2063
2064         if (sk->sk_err)
2065                 err = -sk->sk_err;
2066         else if (sk->sk_shutdown & RCV_SHUTDOWN)
2067                 err = 0;
2068
2069         if (copied > 0)
2070                 err = copied;
2071
2072 out:
2073         return err;
2074 }
2075
2076 static int __vsock_seqpacket_recvmsg(struct sock *sk, struct msghdr *msg,
2077                                      size_t len, int flags)
2078 {
2079         const struct vsock_transport *transport;
2080         struct vsock_sock *vsk;
2081         ssize_t msg_len;
2082         long timeout;
2083         int err = 0;
2084         DEFINE_WAIT(wait);
2085
2086         vsk = vsock_sk(sk);
2087         transport = vsk->transport;
2088
2089         timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2090
2091         err = vsock_connectible_wait_data(sk, &wait, timeout, NULL, 0);
2092         if (err <= 0)
2093                 goto out;
2094
2095         msg_len = transport->seqpacket_dequeue(vsk, msg, flags);
2096
2097         if (msg_len < 0) {
2098                 err = msg_len;
2099                 goto out;
2100         }
2101
2102         if (sk->sk_err) {
2103                 err = -sk->sk_err;
2104         } else if (sk->sk_shutdown & RCV_SHUTDOWN) {
2105                 err = 0;
2106         } else {
2107                 /* User sets MSG_TRUNC, so return real length of
2108                  * packet.
2109                  */
2110                 if (flags & MSG_TRUNC)
2111                         err = msg_len;
2112                 else
2113                         err = len - msg_data_left(msg);
2114
2115                 /* Always set MSG_TRUNC if real length of packet is
2116                  * bigger than user's buffer.
2117                  */
2118                 if (msg_len > len)
2119                         msg->msg_flags |= MSG_TRUNC;
2120         }
2121
2122 out:
2123         return err;
2124 }
2125
2126 int
2127 vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2128                           int flags)
2129 {
2130         struct sock *sk;
2131         struct vsock_sock *vsk;
2132         const struct vsock_transport *transport;
2133 #ifdef CONFIG_BPF_SYSCALL
2134         const struct proto *prot;
2135 #endif
2136         int err;
2137
2138         sk = sock->sk;
2139
2140         if (unlikely(flags & MSG_ERRQUEUE))
2141                 return sock_recv_errqueue(sk, msg, len, SOL_VSOCK, VSOCK_RECVERR);
2142
2143         vsk = vsock_sk(sk);
2144         err = 0;
2145
2146         lock_sock(sk);
2147
2148         transport = vsk->transport;
2149
2150         if (!transport || sk->sk_state != TCP_ESTABLISHED) {
2151                 /* Recvmsg is supposed to return 0 if a peer performs an
2152                  * orderly shutdown. Differentiate between that case and when a
2153                  * peer has not connected or a local shutdown occurred with the
2154                  * SOCK_DONE flag.
2155                  */
2156                 if (sock_flag(sk, SOCK_DONE))
2157                         err = 0;
2158                 else
2159                         err = -ENOTCONN;
2160
2161                 goto out;
2162         }
2163
2164         if (flags & MSG_OOB) {
2165                 err = -EOPNOTSUPP;
2166                 goto out;
2167         }
2168
2169         /* We don't check peer_shutdown flag here since peer may actually shut
2170          * down, but there can be data in the queue that a local socket can
2171          * receive.
2172          */
2173         if (sk->sk_shutdown & RCV_SHUTDOWN) {
2174                 err = 0;
2175                 goto out;
2176         }
2177
2178         /* It is valid on Linux to pass in a zero-length receive buffer.  This
2179          * is not an error.  We may as well bail out now.
2180          */
2181         if (!len) {
2182                 err = 0;
2183                 goto out;
2184         }
2185
2186 #ifdef CONFIG_BPF_SYSCALL
2187         prot = READ_ONCE(sk->sk_prot);
2188         if (prot != &vsock_proto) {
2189                 release_sock(sk);
2190                 return prot->recvmsg(sk, msg, len, flags, NULL);
2191         }
2192 #endif
2193
2194         if (sk->sk_type == SOCK_STREAM)
2195                 err = __vsock_stream_recvmsg(sk, msg, len, flags);
2196         else
2197                 err = __vsock_seqpacket_recvmsg(sk, msg, len, flags);
2198
2199 out:
2200         release_sock(sk);
2201         return err;
2202 }
2203 EXPORT_SYMBOL_GPL(vsock_connectible_recvmsg);
2204
2205 static int vsock_set_rcvlowat(struct sock *sk, int val)
2206 {
2207         const struct vsock_transport *transport;
2208         struct vsock_sock *vsk;
2209
2210         vsk = vsock_sk(sk);
2211
2212         if (val > vsk->buffer_size)
2213                 return -EINVAL;
2214
2215         transport = vsk->transport;
2216
2217         if (transport && transport->notify_set_rcvlowat) {
2218                 int err;
2219
2220                 err = transport->notify_set_rcvlowat(vsk, val);
2221                 if (err)
2222                         return err;
2223         }
2224
2225         WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
2226         return 0;
2227 }
2228
2229 static const struct proto_ops vsock_stream_ops = {
2230         .family = PF_VSOCK,
2231         .owner = THIS_MODULE,
2232         .release = vsock_release,
2233         .bind = vsock_bind,
2234         .connect = vsock_connect,
2235         .socketpair = sock_no_socketpair,
2236         .accept = vsock_accept,
2237         .getname = vsock_getname,
2238         .poll = vsock_poll,
2239         .ioctl = sock_no_ioctl,
2240         .listen = vsock_listen,
2241         .shutdown = vsock_shutdown,
2242         .setsockopt = vsock_connectible_setsockopt,
2243         .getsockopt = vsock_connectible_getsockopt,
2244         .sendmsg = vsock_connectible_sendmsg,
2245         .recvmsg = vsock_connectible_recvmsg,
2246         .mmap = sock_no_mmap,
2247         .set_rcvlowat = vsock_set_rcvlowat,
2248         .read_skb = vsock_read_skb,
2249 };
2250
2251 static const struct proto_ops vsock_seqpacket_ops = {
2252         .family = PF_VSOCK,
2253         .owner = THIS_MODULE,
2254         .release = vsock_release,
2255         .bind = vsock_bind,
2256         .connect = vsock_connect,
2257         .socketpair = sock_no_socketpair,
2258         .accept = vsock_accept,
2259         .getname = vsock_getname,
2260         .poll = vsock_poll,
2261         .ioctl = sock_no_ioctl,
2262         .listen = vsock_listen,
2263         .shutdown = vsock_shutdown,
2264         .setsockopt = vsock_connectible_setsockopt,
2265         .getsockopt = vsock_connectible_getsockopt,
2266         .sendmsg = vsock_connectible_sendmsg,
2267         .recvmsg = vsock_connectible_recvmsg,
2268         .mmap = sock_no_mmap,
2269         .read_skb = vsock_read_skb,
2270 };
2271
2272 static int vsock_create(struct net *net, struct socket *sock,
2273                         int protocol, int kern)
2274 {
2275         struct vsock_sock *vsk;
2276         struct sock *sk;
2277         int ret;
2278
2279         if (!sock)
2280                 return -EINVAL;
2281
2282         if (protocol && protocol != PF_VSOCK)
2283                 return -EPROTONOSUPPORT;
2284
2285         switch (sock->type) {
2286         case SOCK_DGRAM:
2287                 sock->ops = &vsock_dgram_ops;
2288                 break;
2289         case SOCK_STREAM:
2290                 sock->ops = &vsock_stream_ops;
2291                 break;
2292         case SOCK_SEQPACKET:
2293                 sock->ops = &vsock_seqpacket_ops;
2294                 break;
2295         default:
2296                 return -ESOCKTNOSUPPORT;
2297         }
2298
2299         sock->state = SS_UNCONNECTED;
2300
2301         sk = __vsock_create(net, sock, NULL, GFP_KERNEL, 0, kern);
2302         if (!sk)
2303                 return -ENOMEM;
2304
2305         vsk = vsock_sk(sk);
2306
2307         if (sock->type == SOCK_DGRAM) {
2308                 ret = vsock_assign_transport(vsk, NULL);
2309                 if (ret < 0) {
2310                         sock_put(sk);
2311                         return ret;
2312                 }
2313         }
2314
2315         vsock_insert_unbound(vsk);
2316
2317         return 0;
2318 }
2319
2320 static const struct net_proto_family vsock_family_ops = {
2321         .family = AF_VSOCK,
2322         .create = vsock_create,
2323         .owner = THIS_MODULE,
2324 };
2325
2326 static long vsock_dev_do_ioctl(struct file *filp,
2327                                unsigned int cmd, void __user *ptr)
2328 {
2329         u32 __user *p = ptr;
2330         u32 cid = VMADDR_CID_ANY;
2331         int retval = 0;
2332
2333         switch (cmd) {
2334         case IOCTL_VM_SOCKETS_GET_LOCAL_CID:
2335                 /* To be compatible with the VMCI behavior, we prioritize the
2336                  * guest CID instead of well-know host CID (VMADDR_CID_HOST).
2337                  */
2338                 if (transport_g2h)
2339                         cid = transport_g2h->get_local_cid();
2340                 else if (transport_h2g)
2341                         cid = transport_h2g->get_local_cid();
2342
2343                 if (put_user(cid, p) != 0)
2344                         retval = -EFAULT;
2345                 break;
2346
2347         default:
2348                 retval = -ENOIOCTLCMD;
2349         }
2350
2351         return retval;
2352 }
2353
2354 static long vsock_dev_ioctl(struct file *filp,
2355                             unsigned int cmd, unsigned long arg)
2356 {
2357         return vsock_dev_do_ioctl(filp, cmd, (void __user *)arg);
2358 }
2359
2360 #ifdef CONFIG_COMPAT
2361 static long vsock_dev_compat_ioctl(struct file *filp,
2362                                    unsigned int cmd, unsigned long arg)
2363 {
2364         return vsock_dev_do_ioctl(filp, cmd, compat_ptr(arg));
2365 }
2366 #endif
2367
2368 static const struct file_operations vsock_device_ops = {
2369         .owner          = THIS_MODULE,
2370         .unlocked_ioctl = vsock_dev_ioctl,
2371 #ifdef CONFIG_COMPAT
2372         .compat_ioctl   = vsock_dev_compat_ioctl,
2373 #endif
2374         .open           = nonseekable_open,
2375 };
2376
2377 static struct miscdevice vsock_device = {
2378         .name           = "vsock",
2379         .fops           = &vsock_device_ops,
2380 };
2381
2382 static int __init vsock_init(void)
2383 {
2384         int err = 0;
2385
2386         vsock_init_tables();
2387
2388         vsock_proto.owner = THIS_MODULE;
2389         vsock_device.minor = MISC_DYNAMIC_MINOR;
2390         err = misc_register(&vsock_device);
2391         if (err) {
2392                 pr_err("Failed to register misc device\n");
2393                 goto err_reset_transport;
2394         }
2395
2396         err = proto_register(&vsock_proto, 1);  /* we want our slab */
2397         if (err) {
2398                 pr_err("Cannot register vsock protocol\n");
2399                 goto err_deregister_misc;
2400         }
2401
2402         err = sock_register(&vsock_family_ops);
2403         if (err) {
2404                 pr_err("could not register af_vsock (%d) address family: %d\n",
2405                        AF_VSOCK, err);
2406                 goto err_unregister_proto;
2407         }
2408
2409         vsock_bpf_build_proto();
2410
2411         return 0;
2412
2413 err_unregister_proto:
2414         proto_unregister(&vsock_proto);
2415 err_deregister_misc:
2416         misc_deregister(&vsock_device);
2417 err_reset_transport:
2418         return err;
2419 }
2420
2421 static void __exit vsock_exit(void)
2422 {
2423         misc_deregister(&vsock_device);
2424         sock_unregister(AF_VSOCK);
2425         proto_unregister(&vsock_proto);
2426 }
2427
2428 const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk)
2429 {
2430         return vsk->transport;
2431 }
2432 EXPORT_SYMBOL_GPL(vsock_core_get_transport);
2433
2434 int vsock_core_register(const struct vsock_transport *t, int features)
2435 {
2436         const struct vsock_transport *t_h2g, *t_g2h, *t_dgram, *t_local;
2437         int err = mutex_lock_interruptible(&vsock_register_mutex);
2438
2439         if (err)
2440                 return err;
2441
2442         t_h2g = transport_h2g;
2443         t_g2h = transport_g2h;
2444         t_dgram = transport_dgram;
2445         t_local = transport_local;
2446
2447         if (features & VSOCK_TRANSPORT_F_H2G) {
2448                 if (t_h2g) {
2449                         err = -EBUSY;
2450                         goto err_busy;
2451                 }
2452                 t_h2g = t;
2453         }
2454
2455         if (features & VSOCK_TRANSPORT_F_G2H) {
2456                 if (t_g2h) {
2457                         err = -EBUSY;
2458                         goto err_busy;
2459                 }
2460                 t_g2h = t;
2461         }
2462
2463         if (features & VSOCK_TRANSPORT_F_DGRAM) {
2464                 if (t_dgram) {
2465                         err = -EBUSY;
2466                         goto err_busy;
2467                 }
2468                 t_dgram = t;
2469         }
2470
2471         if (features & VSOCK_TRANSPORT_F_LOCAL) {
2472                 if (t_local) {
2473                         err = -EBUSY;
2474                         goto err_busy;
2475                 }
2476                 t_local = t;
2477         }
2478
2479         transport_h2g = t_h2g;
2480         transport_g2h = t_g2h;
2481         transport_dgram = t_dgram;
2482         transport_local = t_local;
2483
2484 err_busy:
2485         mutex_unlock(&vsock_register_mutex);
2486         return err;
2487 }
2488 EXPORT_SYMBOL_GPL(vsock_core_register);
2489
2490 void vsock_core_unregister(const struct vsock_transport *t)
2491 {
2492         mutex_lock(&vsock_register_mutex);
2493
2494         if (transport_h2g == t)
2495                 transport_h2g = NULL;
2496
2497         if (transport_g2h == t)
2498                 transport_g2h = NULL;
2499
2500         if (transport_dgram == t)
2501                 transport_dgram = NULL;
2502
2503         if (transport_local == t)
2504                 transport_local = NULL;
2505
2506         mutex_unlock(&vsock_register_mutex);
2507 }
2508 EXPORT_SYMBOL_GPL(vsock_core_unregister);
2509
2510 module_init(vsock_init);
2511 module_exit(vsock_exit);
2512
2513 MODULE_AUTHOR("VMware, Inc.");
2514 MODULE_DESCRIPTION("VMware Virtual Socket Family");
2515 MODULE_VERSION("1.0.2.0-k");
2516 MODULE_LICENSE("GPL v2");