1 // SPDX-License-Identifier: GPL-2.0-only
3 * VMware vSockets Driver
5 * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
8 /* Implementation notes:
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.
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.
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).
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.
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.
56 * - Lock ordering for pending or accept queue sockets is:
58 * lock_sock(listener);
59 * lock_sock_nested(pending, SINGLE_DEPTH_NESTING);
61 * Using explicit nested locking keeps lockdep happy since normally only one
62 * lock of a given class may be taken at a time.
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.
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.
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):
81 * TCP_CLOSE - unconnected
82 * TCP_SYN_SENT - connecting
83 * TCP_ESTABLISHED - connected
84 * TCP_CLOSING - disconnecting
85 * TCP_LISTEN - listening
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>
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 #include <uapi/asm-generic/ioctls.h>
117 static int __vsock_bind(struct sock
*sk
, struct sockaddr_vm
*addr
);
118 static void vsock_sk_destruct(struct sock
*sk
);
119 static int vsock_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
);
120 static void vsock_close(struct sock
*sk
, long timeout
);
122 /* Protocol family. */
123 struct proto vsock_proto
= {
125 .owner
= THIS_MODULE
,
126 .obj_size
= sizeof(struct vsock_sock
),
127 .close
= vsock_close
,
128 #ifdef CONFIG_BPF_SYSCALL
129 .psock_update_sk_prot
= vsock_bpf_update_proto
,
133 /* The default peer timeout indicates how long we will wait for a peer response
134 * to a control message.
136 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
138 #define VSOCK_DEFAULT_BUFFER_SIZE (1024 * 256)
139 #define VSOCK_DEFAULT_BUFFER_MAX_SIZE (1024 * 256)
140 #define VSOCK_DEFAULT_BUFFER_MIN_SIZE 128
142 /* Transport used for host->guest communication */
143 static const struct vsock_transport
*transport_h2g
;
144 /* Transport used for guest->host communication */
145 static const struct vsock_transport
*transport_g2h
;
146 /* Transport used for DGRAM communication */
147 static const struct vsock_transport
*transport_dgram
;
148 /* Transport used for local communication */
149 static const struct vsock_transport
*transport_local
;
150 static DEFINE_MUTEX(vsock_register_mutex
);
154 /* Each bound VSocket is stored in the bind hash table and each connected
155 * VSocket is stored in the connected hash table.
157 * Unbound sockets are all put on the same list attached to the end of the hash
158 * table (vsock_unbound_sockets). Bound sockets are added to the hash table in
159 * the bucket that their local address hashes to (vsock_bound_sockets(addr)
160 * represents the list that addr hashes to).
162 * Specifically, we initialize the vsock_bind_table array to a size of
163 * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through
164 * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and
165 * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function
166 * mods with VSOCK_HASH_SIZE to ensure this.
168 #define MAX_PORT_RETRIES 24
170 #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
171 #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
172 #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE])
174 /* XXX This can probably be implemented in a better way. */
175 #define VSOCK_CONN_HASH(src, dst) \
176 (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
177 #define vsock_connected_sockets(src, dst) \
178 (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
179 #define vsock_connected_sockets_vsk(vsk) \
180 vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
182 struct list_head vsock_bind_table
[VSOCK_HASH_SIZE
+ 1];
183 EXPORT_SYMBOL_GPL(vsock_bind_table
);
184 struct list_head vsock_connected_table
[VSOCK_HASH_SIZE
];
185 EXPORT_SYMBOL_GPL(vsock_connected_table
);
186 DEFINE_SPINLOCK(vsock_table_lock
);
187 EXPORT_SYMBOL_GPL(vsock_table_lock
);
189 /* Autobind this socket to the local address if necessary. */
190 static int vsock_auto_bind(struct vsock_sock
*vsk
)
192 struct sock
*sk
= sk_vsock(vsk
);
193 struct sockaddr_vm local_addr
;
195 if (vsock_addr_bound(&vsk
->local_addr
))
197 vsock_addr_init(&local_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
198 return __vsock_bind(sk
, &local_addr
);
201 static void vsock_init_tables(void)
205 for (i
= 0; i
< ARRAY_SIZE(vsock_bind_table
); i
++)
206 INIT_LIST_HEAD(&vsock_bind_table
[i
]);
208 for (i
= 0; i
< ARRAY_SIZE(vsock_connected_table
); i
++)
209 INIT_LIST_HEAD(&vsock_connected_table
[i
]);
212 static void __vsock_insert_bound(struct list_head
*list
,
213 struct vsock_sock
*vsk
)
216 list_add(&vsk
->bound_table
, list
);
219 static void __vsock_insert_connected(struct list_head
*list
,
220 struct vsock_sock
*vsk
)
223 list_add(&vsk
->connected_table
, list
);
226 static void __vsock_remove_bound(struct vsock_sock
*vsk
)
228 list_del_init(&vsk
->bound_table
);
232 static void __vsock_remove_connected(struct vsock_sock
*vsk
)
234 list_del_init(&vsk
->connected_table
);
238 static struct sock
*__vsock_find_bound_socket(struct sockaddr_vm
*addr
)
240 struct vsock_sock
*vsk
;
242 list_for_each_entry(vsk
, vsock_bound_sockets(addr
), bound_table
) {
243 if (vsock_addr_equals_addr(addr
, &vsk
->local_addr
))
244 return sk_vsock(vsk
);
246 if (addr
->svm_port
== vsk
->local_addr
.svm_port
&&
247 (vsk
->local_addr
.svm_cid
== VMADDR_CID_ANY
||
248 addr
->svm_cid
== VMADDR_CID_ANY
))
249 return sk_vsock(vsk
);
255 static struct sock
*__vsock_find_connected_socket(struct sockaddr_vm
*src
,
256 struct sockaddr_vm
*dst
)
258 struct vsock_sock
*vsk
;
260 list_for_each_entry(vsk
, vsock_connected_sockets(src
, dst
),
262 if (vsock_addr_equals_addr(src
, &vsk
->remote_addr
) &&
263 dst
->svm_port
== vsk
->local_addr
.svm_port
) {
264 return sk_vsock(vsk
);
271 static void vsock_insert_unbound(struct vsock_sock
*vsk
)
273 spin_lock_bh(&vsock_table_lock
);
274 __vsock_insert_bound(vsock_unbound_sockets
, vsk
);
275 spin_unlock_bh(&vsock_table_lock
);
278 void vsock_insert_connected(struct vsock_sock
*vsk
)
280 struct list_head
*list
= vsock_connected_sockets(
281 &vsk
->remote_addr
, &vsk
->local_addr
);
283 spin_lock_bh(&vsock_table_lock
);
284 __vsock_insert_connected(list
, vsk
);
285 spin_unlock_bh(&vsock_table_lock
);
287 EXPORT_SYMBOL_GPL(vsock_insert_connected
);
289 void vsock_remove_bound(struct vsock_sock
*vsk
)
291 spin_lock_bh(&vsock_table_lock
);
292 if (__vsock_in_bound_table(vsk
))
293 __vsock_remove_bound(vsk
);
294 spin_unlock_bh(&vsock_table_lock
);
296 EXPORT_SYMBOL_GPL(vsock_remove_bound
);
298 void vsock_remove_connected(struct vsock_sock
*vsk
)
300 spin_lock_bh(&vsock_table_lock
);
301 if (__vsock_in_connected_table(vsk
))
302 __vsock_remove_connected(vsk
);
303 spin_unlock_bh(&vsock_table_lock
);
305 EXPORT_SYMBOL_GPL(vsock_remove_connected
);
307 struct sock
*vsock_find_bound_socket(struct sockaddr_vm
*addr
)
311 spin_lock_bh(&vsock_table_lock
);
312 sk
= __vsock_find_bound_socket(addr
);
316 spin_unlock_bh(&vsock_table_lock
);
320 EXPORT_SYMBOL_GPL(vsock_find_bound_socket
);
322 struct sock
*vsock_find_connected_socket(struct sockaddr_vm
*src
,
323 struct sockaddr_vm
*dst
)
327 spin_lock_bh(&vsock_table_lock
);
328 sk
= __vsock_find_connected_socket(src
, dst
);
332 spin_unlock_bh(&vsock_table_lock
);
336 EXPORT_SYMBOL_GPL(vsock_find_connected_socket
);
338 void vsock_remove_sock(struct vsock_sock
*vsk
)
340 vsock_remove_bound(vsk
);
341 vsock_remove_connected(vsk
);
343 EXPORT_SYMBOL_GPL(vsock_remove_sock
);
345 void vsock_for_each_connected_socket(struct vsock_transport
*transport
,
346 void (*fn
)(struct sock
*sk
))
350 spin_lock_bh(&vsock_table_lock
);
352 for (i
= 0; i
< ARRAY_SIZE(vsock_connected_table
); i
++) {
353 struct vsock_sock
*vsk
;
354 list_for_each_entry(vsk
, &vsock_connected_table
[i
],
356 if (vsk
->transport
!= transport
)
363 spin_unlock_bh(&vsock_table_lock
);
365 EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket
);
367 void vsock_add_pending(struct sock
*listener
, struct sock
*pending
)
369 struct vsock_sock
*vlistener
;
370 struct vsock_sock
*vpending
;
372 vlistener
= vsock_sk(listener
);
373 vpending
= vsock_sk(pending
);
377 list_add_tail(&vpending
->pending_links
, &vlistener
->pending_links
);
379 EXPORT_SYMBOL_GPL(vsock_add_pending
);
381 void vsock_remove_pending(struct sock
*listener
, struct sock
*pending
)
383 struct vsock_sock
*vpending
= vsock_sk(pending
);
385 list_del_init(&vpending
->pending_links
);
389 EXPORT_SYMBOL_GPL(vsock_remove_pending
);
391 void vsock_enqueue_accept(struct sock
*listener
, struct sock
*connected
)
393 struct vsock_sock
*vlistener
;
394 struct vsock_sock
*vconnected
;
396 vlistener
= vsock_sk(listener
);
397 vconnected
= vsock_sk(connected
);
399 sock_hold(connected
);
401 list_add_tail(&vconnected
->accept_queue
, &vlistener
->accept_queue
);
403 EXPORT_SYMBOL_GPL(vsock_enqueue_accept
);
405 static bool vsock_use_local_transport(unsigned int remote_cid
)
407 if (!transport_local
)
410 if (remote_cid
== VMADDR_CID_LOCAL
)
414 return remote_cid
== transport_g2h
->get_local_cid();
416 return remote_cid
== VMADDR_CID_HOST
;
420 static void vsock_deassign_transport(struct vsock_sock
*vsk
)
425 vsk
->transport
->destruct(vsk
);
426 module_put(vsk
->transport
->module
);
427 vsk
->transport
= NULL
;
430 /* Assign a transport to a socket and call the .init transport callback.
432 * Note: for connection oriented socket this must be called when vsk->remote_addr
433 * is set (e.g. during the connect() or when a connection request on a listener
434 * socket is received).
435 * The vsk->remote_addr is used to decide which transport to use:
436 * - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if
437 * g2h is not loaded, will use local transport;
438 * - remote CID <= VMADDR_CID_HOST or h2g is not loaded or remote flags field
439 * includes VMADDR_FLAG_TO_HOST flag value, will use guest->host transport;
440 * - remote CID > VMADDR_CID_HOST will use host->guest transport;
442 int vsock_assign_transport(struct vsock_sock
*vsk
, struct vsock_sock
*psk
)
444 const struct vsock_transport
*new_transport
;
445 struct sock
*sk
= sk_vsock(vsk
);
446 unsigned int remote_cid
= vsk
->remote_addr
.svm_cid
;
450 /* If the packet is coming with the source and destination CIDs higher
451 * than VMADDR_CID_HOST, then a vsock channel where all the packets are
452 * forwarded to the host should be established. Then the host will
453 * need to forward the packets to the guest.
455 * The flag is set on the (listen) receive path (psk is not NULL). On
456 * the connect path the flag can be set by the user space application.
458 if (psk
&& vsk
->local_addr
.svm_cid
> VMADDR_CID_HOST
&&
459 vsk
->remote_addr
.svm_cid
> VMADDR_CID_HOST
)
460 vsk
->remote_addr
.svm_flags
|= VMADDR_FLAG_TO_HOST
;
462 remote_flags
= vsk
->remote_addr
.svm_flags
;
464 switch (sk
->sk_type
) {
466 new_transport
= transport_dgram
;
470 if (vsock_use_local_transport(remote_cid
))
471 new_transport
= transport_local
;
472 else if (remote_cid
<= VMADDR_CID_HOST
|| !transport_h2g
||
473 (remote_flags
& VMADDR_FLAG_TO_HOST
))
474 new_transport
= transport_g2h
;
476 new_transport
= transport_h2g
;
479 return -ESOCKTNOSUPPORT
;
482 if (vsk
->transport
) {
483 if (vsk
->transport
== new_transport
)
486 /* transport->release() must be called with sock lock acquired.
487 * This path can only be taken during vsock_connect(), where we
488 * have already held the sock lock. In the other cases, this
489 * function is called on a new socket which is not assigned to
492 vsk
->transport
->release(vsk
);
493 vsock_deassign_transport(vsk
);
496 /* We increase the module refcnt to prevent the transport unloading
497 * while there are open sockets assigned to it.
499 if (!new_transport
|| !try_module_get(new_transport
->module
))
502 if (sk
->sk_type
== SOCK_SEQPACKET
) {
503 if (!new_transport
->seqpacket_allow
||
504 !new_transport
->seqpacket_allow(remote_cid
)) {
505 module_put(new_transport
->module
);
506 return -ESOCKTNOSUPPORT
;
510 ret
= new_transport
->init(vsk
, psk
);
512 module_put(new_transport
->module
);
516 vsk
->transport
= new_transport
;
520 EXPORT_SYMBOL_GPL(vsock_assign_transport
);
522 bool vsock_find_cid(unsigned int cid
)
524 if (transport_g2h
&& cid
== transport_g2h
->get_local_cid())
527 if (transport_h2g
&& cid
== VMADDR_CID_HOST
)
530 if (transport_local
&& cid
== VMADDR_CID_LOCAL
)
535 EXPORT_SYMBOL_GPL(vsock_find_cid
);
537 static struct sock
*vsock_dequeue_accept(struct sock
*listener
)
539 struct vsock_sock
*vlistener
;
540 struct vsock_sock
*vconnected
;
542 vlistener
= vsock_sk(listener
);
544 if (list_empty(&vlistener
->accept_queue
))
547 vconnected
= list_entry(vlistener
->accept_queue
.next
,
548 struct vsock_sock
, accept_queue
);
550 list_del_init(&vconnected
->accept_queue
);
552 /* The caller will need a reference on the connected socket so we let
553 * it call sock_put().
556 return sk_vsock(vconnected
);
559 static bool vsock_is_accept_queue_empty(struct sock
*sk
)
561 struct vsock_sock
*vsk
= vsock_sk(sk
);
562 return list_empty(&vsk
->accept_queue
);
565 static bool vsock_is_pending(struct sock
*sk
)
567 struct vsock_sock
*vsk
= vsock_sk(sk
);
568 return !list_empty(&vsk
->pending_links
);
571 static int vsock_send_shutdown(struct sock
*sk
, int mode
)
573 struct vsock_sock
*vsk
= vsock_sk(sk
);
578 return vsk
->transport
->shutdown(vsk
, mode
);
581 static void vsock_pending_work(struct work_struct
*work
)
584 struct sock
*listener
;
585 struct vsock_sock
*vsk
;
588 vsk
= container_of(work
, struct vsock_sock
, pending_work
.work
);
590 listener
= vsk
->listener
;
594 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
596 if (vsock_is_pending(sk
)) {
597 vsock_remove_pending(listener
, sk
);
599 sk_acceptq_removed(listener
);
600 } else if (!vsk
->rejected
) {
601 /* We are not on the pending list and accept() did not reject
602 * us, so we must have been accepted by our user process. We
603 * just need to drop our references to the sockets and be on
610 /* We need to remove ourself from the global connected sockets list so
611 * incoming packets can't find this socket, and to reduce the reference
614 vsock_remove_connected(vsk
);
616 sk
->sk_state
= TCP_CLOSE
;
620 release_sock(listener
);
628 /**** SOCKET OPERATIONS ****/
630 static int __vsock_bind_connectible(struct vsock_sock
*vsk
,
631 struct sockaddr_vm
*addr
)
634 struct sockaddr_vm new_addr
;
637 port
= get_random_u32_above(LAST_RESERVED_PORT
);
639 vsock_addr_init(&new_addr
, addr
->svm_cid
, addr
->svm_port
);
641 if (addr
->svm_port
== VMADDR_PORT_ANY
) {
645 for (i
= 0; i
< MAX_PORT_RETRIES
; i
++) {
646 if (port
<= LAST_RESERVED_PORT
)
647 port
= LAST_RESERVED_PORT
+ 1;
649 new_addr
.svm_port
= port
++;
651 if (!__vsock_find_bound_socket(&new_addr
)) {
658 return -EADDRNOTAVAIL
;
660 /* If port is in reserved range, ensure caller
661 * has necessary privileges.
663 if (addr
->svm_port
<= LAST_RESERVED_PORT
&&
664 !capable(CAP_NET_BIND_SERVICE
)) {
668 if (__vsock_find_bound_socket(&new_addr
))
672 vsock_addr_init(&vsk
->local_addr
, new_addr
.svm_cid
, new_addr
.svm_port
);
674 /* Remove connection oriented sockets from the unbound list and add them
675 * to the hash table for easy lookup by its address. The unbound list
676 * is simply an extra entry at the end of the hash table, a trick used
679 __vsock_remove_bound(vsk
);
680 __vsock_insert_bound(vsock_bound_sockets(&vsk
->local_addr
), vsk
);
685 static int __vsock_bind_dgram(struct vsock_sock
*vsk
,
686 struct sockaddr_vm
*addr
)
688 return vsk
->transport
->dgram_bind(vsk
, addr
);
691 static int __vsock_bind(struct sock
*sk
, struct sockaddr_vm
*addr
)
693 struct vsock_sock
*vsk
= vsock_sk(sk
);
696 /* First ensure this socket isn't already bound. */
697 if (vsock_addr_bound(&vsk
->local_addr
))
700 /* Now bind to the provided address or select appropriate values if
701 * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that
702 * like AF_INET prevents binding to a non-local IP address (in most
703 * cases), we only allow binding to a local CID.
705 if (addr
->svm_cid
!= VMADDR_CID_ANY
&& !vsock_find_cid(addr
->svm_cid
))
706 return -EADDRNOTAVAIL
;
708 switch (sk
->sk_socket
->type
) {
711 spin_lock_bh(&vsock_table_lock
);
712 retval
= __vsock_bind_connectible(vsk
, addr
);
713 spin_unlock_bh(&vsock_table_lock
);
717 retval
= __vsock_bind_dgram(vsk
, addr
);
728 static void vsock_connect_timeout(struct work_struct
*work
);
730 static struct sock
*__vsock_create(struct net
*net
,
738 struct vsock_sock
*psk
;
739 struct vsock_sock
*vsk
;
741 sk
= sk_alloc(net
, AF_VSOCK
, priority
, &vsock_proto
, kern
);
745 sock_init_data(sock
, sk
);
747 /* sk->sk_type is normally set in sock_init_data, but only if sock is
748 * non-NULL. We make sure that our sockets always have a type by
749 * setting it here if needed.
755 vsock_addr_init(&vsk
->local_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
756 vsock_addr_init(&vsk
->remote_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
758 sk
->sk_destruct
= vsock_sk_destruct
;
759 sk
->sk_backlog_rcv
= vsock_queue_rcv_skb
;
760 sock_reset_flag(sk
, SOCK_DONE
);
762 INIT_LIST_HEAD(&vsk
->bound_table
);
763 INIT_LIST_HEAD(&vsk
->connected_table
);
764 vsk
->listener
= NULL
;
765 INIT_LIST_HEAD(&vsk
->pending_links
);
766 INIT_LIST_HEAD(&vsk
->accept_queue
);
767 vsk
->rejected
= false;
768 vsk
->sent_request
= false;
769 vsk
->ignore_connecting_rst
= false;
770 vsk
->peer_shutdown
= 0;
771 INIT_DELAYED_WORK(&vsk
->connect_work
, vsock_connect_timeout
);
772 INIT_DELAYED_WORK(&vsk
->pending_work
, vsock_pending_work
);
774 psk
= parent
? vsock_sk(parent
) : NULL
;
776 vsk
->trusted
= psk
->trusted
;
777 vsk
->owner
= get_cred(psk
->owner
);
778 vsk
->connect_timeout
= psk
->connect_timeout
;
779 vsk
->buffer_size
= psk
->buffer_size
;
780 vsk
->buffer_min_size
= psk
->buffer_min_size
;
781 vsk
->buffer_max_size
= psk
->buffer_max_size
;
782 security_sk_clone(parent
, sk
);
784 vsk
->trusted
= ns_capable_noaudit(&init_user_ns
, CAP_NET_ADMIN
);
785 vsk
->owner
= get_current_cred();
786 vsk
->connect_timeout
= VSOCK_DEFAULT_CONNECT_TIMEOUT
;
787 vsk
->buffer_size
= VSOCK_DEFAULT_BUFFER_SIZE
;
788 vsk
->buffer_min_size
= VSOCK_DEFAULT_BUFFER_MIN_SIZE
;
789 vsk
->buffer_max_size
= VSOCK_DEFAULT_BUFFER_MAX_SIZE
;
795 static bool sock_type_connectible(u16 type
)
797 return (type
== SOCK_STREAM
) || (type
== SOCK_SEQPACKET
);
800 static void __vsock_release(struct sock
*sk
, int level
)
802 struct vsock_sock
*vsk
;
803 struct sock
*pending
;
806 pending
= NULL
; /* Compiler warning. */
808 /* When "level" is SINGLE_DEPTH_NESTING, use the nested
809 * version to avoid the warning "possible recursive locking
810 * detected". When "level" is 0, lock_sock_nested(sk, level)
811 * is the same as lock_sock(sk).
813 lock_sock_nested(sk
, level
);
816 vsk
->transport
->release(vsk
);
817 else if (sock_type_connectible(sk
->sk_type
))
818 vsock_remove_sock(vsk
);
821 sk
->sk_shutdown
= SHUTDOWN_MASK
;
823 skb_queue_purge(&sk
->sk_receive_queue
);
825 /* Clean up any sockets that never were accepted. */
826 while ((pending
= vsock_dequeue_accept(sk
)) != NULL
) {
827 __vsock_release(pending
, SINGLE_DEPTH_NESTING
);
835 static void vsock_sk_destruct(struct sock
*sk
)
837 struct vsock_sock
*vsk
= vsock_sk(sk
);
839 /* Flush MSG_ZEROCOPY leftovers. */
840 __skb_queue_purge(&sk
->sk_error_queue
);
842 vsock_deassign_transport(vsk
);
844 /* When clearing these addresses, there's no need to set the family and
845 * possibly register the address family with the kernel.
847 vsock_addr_init(&vsk
->local_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
848 vsock_addr_init(&vsk
->remote_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
850 put_cred(vsk
->owner
);
853 static int vsock_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
857 err
= sock_queue_rcv_skb(sk
, skb
);
864 struct sock
*vsock_create_connected(struct sock
*parent
)
866 return __vsock_create(sock_net(parent
), NULL
, parent
, GFP_KERNEL
,
869 EXPORT_SYMBOL_GPL(vsock_create_connected
);
871 s64
vsock_stream_has_data(struct vsock_sock
*vsk
)
873 return vsk
->transport
->stream_has_data(vsk
);
875 EXPORT_SYMBOL_GPL(vsock_stream_has_data
);
877 s64
vsock_connectible_has_data(struct vsock_sock
*vsk
)
879 struct sock
*sk
= sk_vsock(vsk
);
881 if (sk
->sk_type
== SOCK_SEQPACKET
)
882 return vsk
->transport
->seqpacket_has_data(vsk
);
884 return vsock_stream_has_data(vsk
);
886 EXPORT_SYMBOL_GPL(vsock_connectible_has_data
);
888 s64
vsock_stream_has_space(struct vsock_sock
*vsk
)
890 return vsk
->transport
->stream_has_space(vsk
);
892 EXPORT_SYMBOL_GPL(vsock_stream_has_space
);
894 void vsock_data_ready(struct sock
*sk
)
896 struct vsock_sock
*vsk
= vsock_sk(sk
);
898 if (vsock_stream_has_data(vsk
) >= sk
->sk_rcvlowat
||
899 sock_flag(sk
, SOCK_DONE
))
900 sk
->sk_data_ready(sk
);
902 EXPORT_SYMBOL_GPL(vsock_data_ready
);
904 /* Dummy callback required by sockmap.
905 * See unconditional call of saved_close() in sock_map_close().
907 static void vsock_close(struct sock
*sk
, long timeout
)
911 static int vsock_release(struct socket
*sock
)
913 struct sock
*sk
= sock
->sk
;
918 sk
->sk_prot
->close(sk
, 0);
919 __vsock_release(sk
, 0);
921 sock
->state
= SS_FREE
;
927 vsock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
931 struct sockaddr_vm
*vm_addr
;
935 if (vsock_addr_cast(addr
, addr_len
, &vm_addr
) != 0)
939 err
= __vsock_bind(sk
, vm_addr
);
945 static int vsock_getname(struct socket
*sock
,
946 struct sockaddr
*addr
, int peer
)
950 struct vsock_sock
*vsk
;
951 struct sockaddr_vm
*vm_addr
;
960 if (sock
->state
!= SS_CONNECTED
) {
964 vm_addr
= &vsk
->remote_addr
;
966 vm_addr
= &vsk
->local_addr
;
974 /* sys_getsockname() and sys_getpeername() pass us a
975 * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately
976 * that macro is defined in socket.c instead of .h, so we hardcode its
979 BUILD_BUG_ON(sizeof(*vm_addr
) > 128);
980 memcpy(addr
, vm_addr
, sizeof(*vm_addr
));
981 err
= sizeof(*vm_addr
);
988 static int vsock_shutdown(struct socket
*sock
, int mode
)
993 /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
994 * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
995 * here like the other address families do. Note also that the
996 * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
997 * which is what we want.
1001 if ((mode
& ~SHUTDOWN_MASK
) || !mode
)
1004 /* If this is a connection oriented socket and it is not connected then
1005 * bail out immediately. If it is a DGRAM socket then we must first
1006 * kick the socket so that it wakes up from any sleeping calls, for
1007 * example recv(), and then afterwards return the error.
1013 if (sock
->state
== SS_UNCONNECTED
) {
1015 if (sock_type_connectible(sk
->sk_type
))
1018 sock
->state
= SS_DISCONNECTING
;
1022 /* Receive and send shutdowns are treated alike. */
1023 mode
= mode
& (RCV_SHUTDOWN
| SEND_SHUTDOWN
);
1025 sk
->sk_shutdown
|= mode
;
1026 sk
->sk_state_change(sk
);
1028 if (sock_type_connectible(sk
->sk_type
)) {
1029 sock_reset_flag(sk
, SOCK_DONE
);
1030 vsock_send_shutdown(sk
, mode
);
1039 static __poll_t
vsock_poll(struct file
*file
, struct socket
*sock
,
1044 struct vsock_sock
*vsk
;
1049 poll_wait(file
, sk_sleep(sk
), wait
);
1052 if (sk
->sk_err
|| !skb_queue_empty_lockless(&sk
->sk_error_queue
))
1053 /* Signify that there has been an error on this socket. */
1056 /* INET sockets treat local write shutdown and peer write shutdown as a
1057 * case of EPOLLHUP set.
1059 if ((sk
->sk_shutdown
== SHUTDOWN_MASK
) ||
1060 ((sk
->sk_shutdown
& SEND_SHUTDOWN
) &&
1061 (vsk
->peer_shutdown
& SEND_SHUTDOWN
))) {
1065 if (sk
->sk_shutdown
& RCV_SHUTDOWN
||
1066 vsk
->peer_shutdown
& SEND_SHUTDOWN
) {
1070 if (sk_is_readable(sk
))
1071 mask
|= EPOLLIN
| EPOLLRDNORM
;
1073 if (sock
->type
== SOCK_DGRAM
) {
1074 /* For datagram sockets we can read if there is something in
1075 * the queue and write as long as the socket isn't shutdown for
1078 if (!skb_queue_empty_lockless(&sk
->sk_receive_queue
) ||
1079 (sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
1080 mask
|= EPOLLIN
| EPOLLRDNORM
;
1083 if (!(sk
->sk_shutdown
& SEND_SHUTDOWN
))
1084 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
1086 } else if (sock_type_connectible(sk
->sk_type
)) {
1087 const struct vsock_transport
*transport
;
1091 transport
= vsk
->transport
;
1093 /* Listening sockets that have connections in their accept
1094 * queue can be read.
1096 if (sk
->sk_state
== TCP_LISTEN
1097 && !vsock_is_accept_queue_empty(sk
))
1098 mask
|= EPOLLIN
| EPOLLRDNORM
;
1100 /* If there is something in the queue then we can read. */
1101 if (transport
&& transport
->stream_is_active(vsk
) &&
1102 !(sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
1103 bool data_ready_now
= false;
1104 int target
= sock_rcvlowat(sk
, 0, INT_MAX
);
1105 int ret
= transport
->notify_poll_in(
1106 vsk
, target
, &data_ready_now
);
1111 mask
|= EPOLLIN
| EPOLLRDNORM
;
1116 /* Sockets whose connections have been closed, reset, or
1117 * terminated should also be considered read, and we check the
1118 * shutdown flag for that.
1120 if (sk
->sk_shutdown
& RCV_SHUTDOWN
||
1121 vsk
->peer_shutdown
& SEND_SHUTDOWN
) {
1122 mask
|= EPOLLIN
| EPOLLRDNORM
;
1125 /* Connected sockets that can produce data can be written. */
1126 if (transport
&& sk
->sk_state
== TCP_ESTABLISHED
) {
1127 if (!(sk
->sk_shutdown
& SEND_SHUTDOWN
)) {
1128 bool space_avail_now
= false;
1129 int ret
= transport
->notify_poll_out(
1130 vsk
, 1, &space_avail_now
);
1134 if (space_avail_now
)
1135 /* Remove EPOLLWRBAND since INET
1136 * sockets are not setting it.
1138 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1144 /* Simulate INET socket poll behaviors, which sets
1145 * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read,
1146 * but local send is not shutdown.
1148 if (sk
->sk_state
== TCP_CLOSE
|| sk
->sk_state
== TCP_CLOSING
) {
1149 if (!(sk
->sk_shutdown
& SEND_SHUTDOWN
))
1150 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1160 static int vsock_read_skb(struct sock
*sk
, skb_read_actor_t read_actor
)
1162 struct vsock_sock
*vsk
= vsock_sk(sk
);
1164 return vsk
->transport
->read_skb(vsk
, read_actor
);
1167 static int vsock_dgram_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
1172 struct vsock_sock
*vsk
;
1173 struct sockaddr_vm
*remote_addr
;
1174 const struct vsock_transport
*transport
;
1176 if (msg
->msg_flags
& MSG_OOB
)
1179 /* For now, MSG_DONTWAIT is always assumed... */
1186 transport
= vsk
->transport
;
1188 err
= vsock_auto_bind(vsk
);
1193 /* If the provided message contains an address, use that. Otherwise
1194 * fall back on the socket's remote handle (if it has been connected).
1196 if (msg
->msg_name
&&
1197 vsock_addr_cast(msg
->msg_name
, msg
->msg_namelen
,
1198 &remote_addr
) == 0) {
1199 /* Ensure this address is of the right type and is a valid
1203 if (remote_addr
->svm_cid
== VMADDR_CID_ANY
)
1204 remote_addr
->svm_cid
= transport
->get_local_cid();
1206 if (!vsock_addr_bound(remote_addr
)) {
1210 } else if (sock
->state
== SS_CONNECTED
) {
1211 remote_addr
= &vsk
->remote_addr
;
1213 if (remote_addr
->svm_cid
== VMADDR_CID_ANY
)
1214 remote_addr
->svm_cid
= transport
->get_local_cid();
1216 /* XXX Should connect() or this function ensure remote_addr is
1219 if (!vsock_addr_bound(&vsk
->remote_addr
)) {
1228 if (!transport
->dgram_allow(remote_addr
->svm_cid
,
1229 remote_addr
->svm_port
)) {
1234 err
= transport
->dgram_enqueue(vsk
, remote_addr
, msg
, len
);
1241 static int vsock_dgram_connect(struct socket
*sock
,
1242 struct sockaddr
*addr
, int addr_len
, int flags
)
1246 struct vsock_sock
*vsk
;
1247 struct sockaddr_vm
*remote_addr
;
1252 err
= vsock_addr_cast(addr
, addr_len
, &remote_addr
);
1253 if (err
== -EAFNOSUPPORT
&& remote_addr
->svm_family
== AF_UNSPEC
) {
1255 vsock_addr_init(&vsk
->remote_addr
, VMADDR_CID_ANY
,
1257 sock
->state
= SS_UNCONNECTED
;
1260 } else if (err
!= 0)
1265 err
= vsock_auto_bind(vsk
);
1269 if (!vsk
->transport
->dgram_allow(remote_addr
->svm_cid
,
1270 remote_addr
->svm_port
)) {
1275 memcpy(&vsk
->remote_addr
, remote_addr
, sizeof(vsk
->remote_addr
));
1276 sock
->state
= SS_CONNECTED
;
1278 /* sock map disallows redirection of non-TCP sockets with sk_state !=
1279 * TCP_ESTABLISHED (see sock_map_redirect_allowed()), so we set
1280 * TCP_ESTABLISHED here to allow redirection of connected vsock dgrams.
1282 * This doesn't seem to be abnormal state for datagram sockets, as the
1283 * same approach can be see in other datagram socket types as well
1284 * (such as unix sockets).
1286 sk
->sk_state
= TCP_ESTABLISHED
;
1293 int __vsock_dgram_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1294 size_t len
, int flags
)
1296 struct sock
*sk
= sock
->sk
;
1297 struct vsock_sock
*vsk
= vsock_sk(sk
);
1299 return vsk
->transport
->dgram_dequeue(vsk
, msg
, len
, flags
);
1302 int vsock_dgram_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1303 size_t len
, int flags
)
1305 #ifdef CONFIG_BPF_SYSCALL
1306 struct sock
*sk
= sock
->sk
;
1307 const struct proto
*prot
;
1309 prot
= READ_ONCE(sk
->sk_prot
);
1310 if (prot
!= &vsock_proto
)
1311 return prot
->recvmsg(sk
, msg
, len
, flags
, NULL
);
1314 return __vsock_dgram_recvmsg(sock
, msg
, len
, flags
);
1316 EXPORT_SYMBOL_GPL(vsock_dgram_recvmsg
);
1318 static int vsock_do_ioctl(struct socket
*sock
, unsigned int cmd
,
1321 struct sock
*sk
= sock
->sk
;
1322 struct vsock_sock
*vsk
;
1331 if (!vsk
->transport
|| !vsk
->transport
->unsent_bytes
) {
1336 if (sock_type_connectible(sk
->sk_type
) && sk
->sk_state
== TCP_LISTEN
) {
1341 n_bytes
= vsk
->transport
->unsent_bytes(vsk
);
1347 ret
= put_user(n_bytes
, arg
);
1357 static int vsock_ioctl(struct socket
*sock
, unsigned int cmd
,
1362 lock_sock(sock
->sk
);
1363 ret
= vsock_do_ioctl(sock
, cmd
, (int __user
*)arg
);
1364 release_sock(sock
->sk
);
1369 static const struct proto_ops vsock_dgram_ops
= {
1371 .owner
= THIS_MODULE
,
1372 .release
= vsock_release
,
1374 .connect
= vsock_dgram_connect
,
1375 .socketpair
= sock_no_socketpair
,
1376 .accept
= sock_no_accept
,
1377 .getname
= vsock_getname
,
1379 .ioctl
= vsock_ioctl
,
1380 .listen
= sock_no_listen
,
1381 .shutdown
= vsock_shutdown
,
1382 .sendmsg
= vsock_dgram_sendmsg
,
1383 .recvmsg
= vsock_dgram_recvmsg
,
1384 .mmap
= sock_no_mmap
,
1385 .read_skb
= vsock_read_skb
,
1388 static int vsock_transport_cancel_pkt(struct vsock_sock
*vsk
)
1390 const struct vsock_transport
*transport
= vsk
->transport
;
1392 if (!transport
|| !transport
->cancel_pkt
)
1395 return transport
->cancel_pkt(vsk
);
1398 static void vsock_connect_timeout(struct work_struct
*work
)
1401 struct vsock_sock
*vsk
;
1403 vsk
= container_of(work
, struct vsock_sock
, connect_work
.work
);
1407 if (sk
->sk_state
== TCP_SYN_SENT
&&
1408 (sk
->sk_shutdown
!= SHUTDOWN_MASK
)) {
1409 sk
->sk_state
= TCP_CLOSE
;
1410 sk
->sk_socket
->state
= SS_UNCONNECTED
;
1411 sk
->sk_err
= ETIMEDOUT
;
1412 sk_error_report(sk
);
1413 vsock_transport_cancel_pkt(vsk
);
1420 static int vsock_connect(struct socket
*sock
, struct sockaddr
*addr
,
1421 int addr_len
, int flags
)
1425 struct vsock_sock
*vsk
;
1426 const struct vsock_transport
*transport
;
1427 struct sockaddr_vm
*remote_addr
;
1437 /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
1438 switch (sock
->state
) {
1442 case SS_DISCONNECTING
:
1446 /* This continues on so we can move sock into the SS_CONNECTED
1447 * state once the connection has completed (at which point err
1448 * will be set to zero also). Otherwise, we will either wait
1449 * for the connection or return -EALREADY should this be a
1450 * non-blocking call.
1453 if (flags
& O_NONBLOCK
)
1457 if ((sk
->sk_state
== TCP_LISTEN
) ||
1458 vsock_addr_cast(addr
, addr_len
, &remote_addr
) != 0) {
1463 /* Set the remote address that we are connecting to. */
1464 memcpy(&vsk
->remote_addr
, remote_addr
,
1465 sizeof(vsk
->remote_addr
));
1467 err
= vsock_assign_transport(vsk
, NULL
);
1471 transport
= vsk
->transport
;
1473 /* The hypervisor and well-known contexts do not have socket
1477 !transport
->stream_allow(remote_addr
->svm_cid
,
1478 remote_addr
->svm_port
)) {
1483 if (vsock_msgzerocopy_allow(transport
)) {
1484 set_bit(SOCK_SUPPORT_ZC
, &sk
->sk_socket
->flags
);
1485 } else if (sock_flag(sk
, SOCK_ZEROCOPY
)) {
1486 /* If this option was set before 'connect()',
1487 * when transport was unknown, check that this
1488 * feature is supported here.
1494 err
= vsock_auto_bind(vsk
);
1498 sk
->sk_state
= TCP_SYN_SENT
;
1500 err
= transport
->connect(vsk
);
1504 /* Mark sock as connecting and set the error code to in
1505 * progress in case this is a non-blocking connect.
1507 sock
->state
= SS_CONNECTING
;
1511 /* The receive path will handle all communication until we are able to
1512 * enter the connected state. Here we wait for the connection to be
1513 * completed or a notification of an error.
1515 timeout
= vsk
->connect_timeout
;
1516 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1518 while (sk
->sk_state
!= TCP_ESTABLISHED
&& sk
->sk_err
== 0) {
1519 if (flags
& O_NONBLOCK
) {
1520 /* If we're not going to block, we schedule a timeout
1521 * function to generate a timeout on the connection
1522 * attempt, in case the peer doesn't respond in a
1523 * timely manner. We hold on to the socket until the
1528 /* If the timeout function is already scheduled,
1529 * reschedule it, then ungrab the socket refcount to
1532 if (mod_delayed_work(system_wq
, &vsk
->connect_work
,
1536 /* Skip ahead to preserve error code set above. */
1541 timeout
= schedule_timeout(timeout
);
1544 if (signal_pending(current
)) {
1545 err
= sock_intr_errno(timeout
);
1546 sk
->sk_state
= sk
->sk_state
== TCP_ESTABLISHED
? TCP_CLOSING
: TCP_CLOSE
;
1547 sock
->state
= SS_UNCONNECTED
;
1548 vsock_transport_cancel_pkt(vsk
);
1549 vsock_remove_connected(vsk
);
1551 } else if ((sk
->sk_state
!= TCP_ESTABLISHED
) && (timeout
== 0)) {
1553 sk
->sk_state
= TCP_CLOSE
;
1554 sock
->state
= SS_UNCONNECTED
;
1555 vsock_transport_cancel_pkt(vsk
);
1559 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1564 sk
->sk_state
= TCP_CLOSE
;
1565 sock
->state
= SS_UNCONNECTED
;
1571 finish_wait(sk_sleep(sk
), &wait
);
1577 static int vsock_accept(struct socket
*sock
, struct socket
*newsock
,
1578 struct proto_accept_arg
*arg
)
1580 struct sock
*listener
;
1582 struct sock
*connected
;
1583 struct vsock_sock
*vconnected
;
1588 listener
= sock
->sk
;
1590 lock_sock(listener
);
1592 if (!sock_type_connectible(sock
->type
)) {
1597 if (listener
->sk_state
!= TCP_LISTEN
) {
1602 /* Wait for children sockets to appear; these are the new sockets
1603 * created upon connection establishment.
1605 timeout
= sock_rcvtimeo(listener
, arg
->flags
& O_NONBLOCK
);
1606 prepare_to_wait(sk_sleep(listener
), &wait
, TASK_INTERRUPTIBLE
);
1608 while ((connected
= vsock_dequeue_accept(listener
)) == NULL
&&
1609 listener
->sk_err
== 0) {
1610 release_sock(listener
);
1611 timeout
= schedule_timeout(timeout
);
1612 finish_wait(sk_sleep(listener
), &wait
);
1613 lock_sock(listener
);
1615 if (signal_pending(current
)) {
1616 err
= sock_intr_errno(timeout
);
1618 } else if (timeout
== 0) {
1623 prepare_to_wait(sk_sleep(listener
), &wait
, TASK_INTERRUPTIBLE
);
1625 finish_wait(sk_sleep(listener
), &wait
);
1627 if (listener
->sk_err
)
1628 err
= -listener
->sk_err
;
1631 sk_acceptq_removed(listener
);
1633 lock_sock_nested(connected
, SINGLE_DEPTH_NESTING
);
1634 vconnected
= vsock_sk(connected
);
1636 /* If the listener socket has received an error, then we should
1637 * reject this socket and return. Note that we simply mark the
1638 * socket rejected, drop our reference, and let the cleanup
1639 * function handle the cleanup; the fact that we found it in
1640 * the listener's accept queue guarantees that the cleanup
1641 * function hasn't run yet.
1644 vconnected
->rejected
= true;
1646 newsock
->state
= SS_CONNECTED
;
1647 sock_graft(connected
, newsock
);
1648 if (vsock_msgzerocopy_allow(vconnected
->transport
))
1649 set_bit(SOCK_SUPPORT_ZC
,
1650 &connected
->sk_socket
->flags
);
1653 release_sock(connected
);
1654 sock_put(connected
);
1658 release_sock(listener
);
1662 static int vsock_listen(struct socket
*sock
, int backlog
)
1666 struct vsock_sock
*vsk
;
1672 if (!sock_type_connectible(sk
->sk_type
)) {
1677 if (sock
->state
!= SS_UNCONNECTED
) {
1684 if (!vsock_addr_bound(&vsk
->local_addr
)) {
1689 sk
->sk_max_ack_backlog
= backlog
;
1690 sk
->sk_state
= TCP_LISTEN
;
1699 static void vsock_update_buffer_size(struct vsock_sock
*vsk
,
1700 const struct vsock_transport
*transport
,
1703 if (val
> vsk
->buffer_max_size
)
1704 val
= vsk
->buffer_max_size
;
1706 if (val
< vsk
->buffer_min_size
)
1707 val
= vsk
->buffer_min_size
;
1709 if (val
!= vsk
->buffer_size
&&
1710 transport
&& transport
->notify_buffer_size
)
1711 transport
->notify_buffer_size(vsk
, &val
);
1713 vsk
->buffer_size
= val
;
1716 static int vsock_connectible_setsockopt(struct socket
*sock
,
1720 unsigned int optlen
)
1724 struct vsock_sock
*vsk
;
1725 const struct vsock_transport
*transport
;
1728 if (level
!= AF_VSOCK
&& level
!= SOL_SOCKET
)
1729 return -ENOPROTOOPT
;
1731 #define COPY_IN(_v) \
1733 if (optlen < sizeof(_v)) { \
1737 if (copy_from_sockptr(&_v, optval, sizeof(_v)) != 0) { \
1749 transport
= vsk
->transport
;
1751 if (level
== SOL_SOCKET
) {
1754 if (optname
!= SO_ZEROCOPY
) {
1756 return sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
1759 /* Use 'int' type here, because variable to
1760 * set this option usually has this type.
1764 if (zerocopy
< 0 || zerocopy
> 1) {
1769 if (transport
&& !vsock_msgzerocopy_allow(transport
)) {
1774 sock_valbool_flag(sk
, SOCK_ZEROCOPY
, zerocopy
);
1779 case SO_VM_SOCKETS_BUFFER_SIZE
:
1781 vsock_update_buffer_size(vsk
, transport
, val
);
1784 case SO_VM_SOCKETS_BUFFER_MAX_SIZE
:
1786 vsk
->buffer_max_size
= val
;
1787 vsock_update_buffer_size(vsk
, transport
, vsk
->buffer_size
);
1790 case SO_VM_SOCKETS_BUFFER_MIN_SIZE
:
1792 vsk
->buffer_min_size
= val
;
1793 vsock_update_buffer_size(vsk
, transport
, vsk
->buffer_size
);
1796 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW
:
1797 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
: {
1798 struct __kernel_sock_timeval tv
;
1800 err
= sock_copy_user_timeval(&tv
, optval
, optlen
,
1801 optname
== SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
);
1804 if (tv
.tv_sec
>= 0 && tv
.tv_usec
< USEC_PER_SEC
&&
1805 tv
.tv_sec
< (MAX_SCHEDULE_TIMEOUT
/ HZ
- 1)) {
1806 vsk
->connect_timeout
= tv
.tv_sec
* HZ
+
1807 DIV_ROUND_UP((unsigned long)tv
.tv_usec
, (USEC_PER_SEC
/ HZ
));
1808 if (vsk
->connect_timeout
== 0)
1809 vsk
->connect_timeout
=
1810 VSOCK_DEFAULT_CONNECT_TIMEOUT
;
1830 static int vsock_connectible_getsockopt(struct socket
*sock
,
1831 int level
, int optname
,
1832 char __user
*optval
,
1835 struct sock
*sk
= sock
->sk
;
1836 struct vsock_sock
*vsk
= vsock_sk(sk
);
1840 struct old_timeval32 tm32
;
1841 struct __kernel_old_timeval tm
;
1842 struct __kernel_sock_timeval stm
;
1845 int lv
= sizeof(v
.val64
);
1848 if (level
!= AF_VSOCK
)
1849 return -ENOPROTOOPT
;
1851 if (get_user(len
, optlen
))
1854 memset(&v
, 0, sizeof(v
));
1857 case SO_VM_SOCKETS_BUFFER_SIZE
:
1858 v
.val64
= vsk
->buffer_size
;
1861 case SO_VM_SOCKETS_BUFFER_MAX_SIZE
:
1862 v
.val64
= vsk
->buffer_max_size
;
1865 case SO_VM_SOCKETS_BUFFER_MIN_SIZE
:
1866 v
.val64
= vsk
->buffer_min_size
;
1869 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW
:
1870 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
:
1871 lv
= sock_get_timeout(vsk
->connect_timeout
, &v
,
1872 optname
== SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
);
1876 return -ENOPROTOOPT
;
1883 if (copy_to_user(optval
, &v
, len
))
1886 if (put_user(len
, optlen
))
1892 static int vsock_connectible_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
1896 struct vsock_sock
*vsk
;
1897 const struct vsock_transport
*transport
;
1898 ssize_t total_written
;
1901 struct vsock_transport_send_notify_data send_data
;
1902 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
1909 if (msg
->msg_flags
& MSG_OOB
)
1914 transport
= vsk
->transport
;
1916 /* Callers should not provide a destination with connection oriented
1919 if (msg
->msg_namelen
) {
1920 err
= sk
->sk_state
== TCP_ESTABLISHED
? -EISCONN
: -EOPNOTSUPP
;
1924 /* Send data only if both sides are not shutdown in the direction. */
1925 if (sk
->sk_shutdown
& SEND_SHUTDOWN
||
1926 vsk
->peer_shutdown
& RCV_SHUTDOWN
) {
1931 if (!transport
|| sk
->sk_state
!= TCP_ESTABLISHED
||
1932 !vsock_addr_bound(&vsk
->local_addr
)) {
1937 if (!vsock_addr_bound(&vsk
->remote_addr
)) {
1938 err
= -EDESTADDRREQ
;
1942 if (msg
->msg_flags
& MSG_ZEROCOPY
&&
1943 !vsock_msgzerocopy_allow(transport
)) {
1948 /* Wait for room in the produce queue to enqueue our user's data. */
1949 timeout
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1951 err
= transport
->notify_send_init(vsk
, &send_data
);
1955 while (total_written
< len
) {
1958 add_wait_queue(sk_sleep(sk
), &wait
);
1959 while (vsock_stream_has_space(vsk
) == 0 &&
1961 !(sk
->sk_shutdown
& SEND_SHUTDOWN
) &&
1962 !(vsk
->peer_shutdown
& RCV_SHUTDOWN
)) {
1964 /* Don't wait for non-blocking sockets. */
1967 remove_wait_queue(sk_sleep(sk
), &wait
);
1971 err
= transport
->notify_send_pre_block(vsk
, &send_data
);
1973 remove_wait_queue(sk_sleep(sk
), &wait
);
1978 timeout
= wait_woken(&wait
, TASK_INTERRUPTIBLE
, timeout
);
1980 if (signal_pending(current
)) {
1981 err
= sock_intr_errno(timeout
);
1982 remove_wait_queue(sk_sleep(sk
), &wait
);
1984 } else if (timeout
== 0) {
1986 remove_wait_queue(sk_sleep(sk
), &wait
);
1990 remove_wait_queue(sk_sleep(sk
), &wait
);
1992 /* These checks occur both as part of and after the loop
1993 * conditional since we need to check before and after
1999 } else if ((sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
2000 (vsk
->peer_shutdown
& RCV_SHUTDOWN
)) {
2005 err
= transport
->notify_send_pre_enqueue(vsk
, &send_data
);
2009 /* Note that enqueue will only write as many bytes as are free
2010 * in the produce queue, so we don't need to ensure len is
2011 * smaller than the queue size. It is the caller's
2012 * responsibility to check how many bytes we were able to send.
2015 if (sk
->sk_type
== SOCK_SEQPACKET
) {
2016 written
= transport
->seqpacket_enqueue(vsk
,
2017 msg
, len
- total_written
);
2019 written
= transport
->stream_enqueue(vsk
,
2020 msg
, len
- total_written
);
2028 total_written
+= written
;
2030 err
= transport
->notify_send_post_enqueue(
2031 vsk
, written
, &send_data
);
2038 if (total_written
> 0) {
2039 /* Return number of written bytes only if:
2040 * 1) SOCK_STREAM socket.
2041 * 2) SOCK_SEQPACKET socket when whole buffer is sent.
2043 if (sk
->sk_type
== SOCK_STREAM
|| total_written
== len
)
2044 err
= total_written
;
2047 if (sk
->sk_type
== SOCK_STREAM
)
2048 err
= sk_stream_error(sk
, msg
->msg_flags
, err
);
2054 static int vsock_connectible_wait_data(struct sock
*sk
,
2055 struct wait_queue_entry
*wait
,
2057 struct vsock_transport_recv_notify_data
*recv_data
,
2060 const struct vsock_transport
*transport
;
2061 struct vsock_sock
*vsk
;
2067 transport
= vsk
->transport
;
2070 prepare_to_wait(sk_sleep(sk
), wait
, TASK_INTERRUPTIBLE
);
2071 data
= vsock_connectible_has_data(vsk
);
2075 if (sk
->sk_err
!= 0 ||
2076 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
2077 (vsk
->peer_shutdown
& SEND_SHUTDOWN
)) {
2081 /* Don't wait for non-blocking sockets. */
2088 err
= transport
->notify_recv_pre_block(vsk
, target
, recv_data
);
2094 timeout
= schedule_timeout(timeout
);
2097 if (signal_pending(current
)) {
2098 err
= sock_intr_errno(timeout
);
2100 } else if (timeout
== 0) {
2106 finish_wait(sk_sleep(sk
), wait
);
2111 /* Internal transport error when checking for available
2112 * data. XXX This should be changed to a connection
2113 * reset in a later change.
2121 static int __vsock_stream_recvmsg(struct sock
*sk
, struct msghdr
*msg
,
2122 size_t len
, int flags
)
2124 struct vsock_transport_recv_notify_data recv_data
;
2125 const struct vsock_transport
*transport
;
2126 struct vsock_sock
*vsk
;
2135 transport
= vsk
->transport
;
2137 /* We must not copy less than target bytes into the user's buffer
2138 * before returning successfully, so we wait for the consume queue to
2139 * have that much data to consume before dequeueing. Note that this
2140 * makes it impossible to handle cases where target is greater than the
2143 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, len
);
2144 if (target
>= transport
->stream_rcvhiwat(vsk
)) {
2148 timeout
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
2151 err
= transport
->notify_recv_init(vsk
, target
, &recv_data
);
2159 err
= vsock_connectible_wait_data(sk
, &wait
, timeout
,
2160 &recv_data
, target
);
2164 err
= transport
->notify_recv_pre_dequeue(vsk
, target
,
2169 read
= transport
->stream_dequeue(vsk
, msg
, len
- copied
, flags
);
2177 err
= transport
->notify_recv_post_dequeue(vsk
, target
, read
,
2178 !(flags
& MSG_PEEK
), &recv_data
);
2182 if (read
>= target
|| flags
& MSG_PEEK
)
2190 else if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
2200 static int __vsock_seqpacket_recvmsg(struct sock
*sk
, struct msghdr
*msg
,
2201 size_t len
, int flags
)
2203 const struct vsock_transport
*transport
;
2204 struct vsock_sock
*vsk
;
2211 transport
= vsk
->transport
;
2213 timeout
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
2215 err
= vsock_connectible_wait_data(sk
, &wait
, timeout
, NULL
, 0);
2219 msg_len
= transport
->seqpacket_dequeue(vsk
, msg
, flags
);
2228 } else if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
2231 /* User sets MSG_TRUNC, so return real length of
2234 if (flags
& MSG_TRUNC
)
2237 err
= len
- msg_data_left(msg
);
2239 /* Always set MSG_TRUNC if real length of packet is
2240 * bigger than user's buffer.
2243 msg
->msg_flags
|= MSG_TRUNC
;
2251 __vsock_connectible_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
2255 struct vsock_sock
*vsk
;
2256 const struct vsock_transport
*transport
;
2261 if (unlikely(flags
& MSG_ERRQUEUE
))
2262 return sock_recv_errqueue(sk
, msg
, len
, SOL_VSOCK
, VSOCK_RECVERR
);
2269 transport
= vsk
->transport
;
2271 if (!transport
|| sk
->sk_state
!= TCP_ESTABLISHED
) {
2272 /* Recvmsg is supposed to return 0 if a peer performs an
2273 * orderly shutdown. Differentiate between that case and when a
2274 * peer has not connected or a local shutdown occurred with the
2277 if (sock_flag(sk
, SOCK_DONE
))
2285 if (flags
& MSG_OOB
) {
2290 /* We don't check peer_shutdown flag here since peer may actually shut
2291 * down, but there can be data in the queue that a local socket can
2294 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
2299 /* It is valid on Linux to pass in a zero-length receive buffer. This
2300 * is not an error. We may as well bail out now.
2307 if (sk
->sk_type
== SOCK_STREAM
)
2308 err
= __vsock_stream_recvmsg(sk
, msg
, len
, flags
);
2310 err
= __vsock_seqpacket_recvmsg(sk
, msg
, len
, flags
);
2318 vsock_connectible_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
2321 #ifdef CONFIG_BPF_SYSCALL
2322 struct sock
*sk
= sock
->sk
;
2323 const struct proto
*prot
;
2325 prot
= READ_ONCE(sk
->sk_prot
);
2326 if (prot
!= &vsock_proto
)
2327 return prot
->recvmsg(sk
, msg
, len
, flags
, NULL
);
2330 return __vsock_connectible_recvmsg(sock
, msg
, len
, flags
);
2332 EXPORT_SYMBOL_GPL(vsock_connectible_recvmsg
);
2334 static int vsock_set_rcvlowat(struct sock
*sk
, int val
)
2336 const struct vsock_transport
*transport
;
2337 struct vsock_sock
*vsk
;
2341 if (val
> vsk
->buffer_size
)
2344 transport
= vsk
->transport
;
2346 if (transport
&& transport
->notify_set_rcvlowat
) {
2349 err
= transport
->notify_set_rcvlowat(vsk
, val
);
2354 WRITE_ONCE(sk
->sk_rcvlowat
, val
? : 1);
2358 static const struct proto_ops vsock_stream_ops
= {
2360 .owner
= THIS_MODULE
,
2361 .release
= vsock_release
,
2363 .connect
= vsock_connect
,
2364 .socketpair
= sock_no_socketpair
,
2365 .accept
= vsock_accept
,
2366 .getname
= vsock_getname
,
2368 .ioctl
= vsock_ioctl
,
2369 .listen
= vsock_listen
,
2370 .shutdown
= vsock_shutdown
,
2371 .setsockopt
= vsock_connectible_setsockopt
,
2372 .getsockopt
= vsock_connectible_getsockopt
,
2373 .sendmsg
= vsock_connectible_sendmsg
,
2374 .recvmsg
= vsock_connectible_recvmsg
,
2375 .mmap
= sock_no_mmap
,
2376 .set_rcvlowat
= vsock_set_rcvlowat
,
2377 .read_skb
= vsock_read_skb
,
2380 static const struct proto_ops vsock_seqpacket_ops
= {
2382 .owner
= THIS_MODULE
,
2383 .release
= vsock_release
,
2385 .connect
= vsock_connect
,
2386 .socketpair
= sock_no_socketpair
,
2387 .accept
= vsock_accept
,
2388 .getname
= vsock_getname
,
2390 .ioctl
= vsock_ioctl
,
2391 .listen
= vsock_listen
,
2392 .shutdown
= vsock_shutdown
,
2393 .setsockopt
= vsock_connectible_setsockopt
,
2394 .getsockopt
= vsock_connectible_getsockopt
,
2395 .sendmsg
= vsock_connectible_sendmsg
,
2396 .recvmsg
= vsock_connectible_recvmsg
,
2397 .mmap
= sock_no_mmap
,
2398 .read_skb
= vsock_read_skb
,
2401 static int vsock_create(struct net
*net
, struct socket
*sock
,
2402 int protocol
, int kern
)
2404 struct vsock_sock
*vsk
;
2411 if (protocol
&& protocol
!= PF_VSOCK
)
2412 return -EPROTONOSUPPORT
;
2414 switch (sock
->type
) {
2416 sock
->ops
= &vsock_dgram_ops
;
2419 sock
->ops
= &vsock_stream_ops
;
2421 case SOCK_SEQPACKET
:
2422 sock
->ops
= &vsock_seqpacket_ops
;
2425 return -ESOCKTNOSUPPORT
;
2428 sock
->state
= SS_UNCONNECTED
;
2430 sk
= __vsock_create(net
, sock
, NULL
, GFP_KERNEL
, 0, kern
);
2436 if (sock
->type
== SOCK_DGRAM
) {
2437 ret
= vsock_assign_transport(vsk
, NULL
);
2445 /* SOCK_DGRAM doesn't have 'setsockopt' callback set in its
2446 * proto_ops, so there is no handler for custom logic.
2448 if (sock_type_connectible(sock
->type
))
2449 set_bit(SOCK_CUSTOM_SOCKOPT
, &sk
->sk_socket
->flags
);
2451 vsock_insert_unbound(vsk
);
2456 static const struct net_proto_family vsock_family_ops
= {
2458 .create
= vsock_create
,
2459 .owner
= THIS_MODULE
,
2462 static long vsock_dev_do_ioctl(struct file
*filp
,
2463 unsigned int cmd
, void __user
*ptr
)
2465 u32 __user
*p
= ptr
;
2466 u32 cid
= VMADDR_CID_ANY
;
2470 case IOCTL_VM_SOCKETS_GET_LOCAL_CID
:
2471 /* To be compatible with the VMCI behavior, we prioritize the
2472 * guest CID instead of well-know host CID (VMADDR_CID_HOST).
2475 cid
= transport_g2h
->get_local_cid();
2476 else if (transport_h2g
)
2477 cid
= transport_h2g
->get_local_cid();
2479 if (put_user(cid
, p
) != 0)
2484 retval
= -ENOIOCTLCMD
;
2490 static long vsock_dev_ioctl(struct file
*filp
,
2491 unsigned int cmd
, unsigned long arg
)
2493 return vsock_dev_do_ioctl(filp
, cmd
, (void __user
*)arg
);
2496 #ifdef CONFIG_COMPAT
2497 static long vsock_dev_compat_ioctl(struct file
*filp
,
2498 unsigned int cmd
, unsigned long arg
)
2500 return vsock_dev_do_ioctl(filp
, cmd
, compat_ptr(arg
));
2504 static const struct file_operations vsock_device_ops
= {
2505 .owner
= THIS_MODULE
,
2506 .unlocked_ioctl
= vsock_dev_ioctl
,
2507 #ifdef CONFIG_COMPAT
2508 .compat_ioctl
= vsock_dev_compat_ioctl
,
2510 .open
= nonseekable_open
,
2513 static struct miscdevice vsock_device
= {
2515 .fops
= &vsock_device_ops
,
2518 static int __init
vsock_init(void)
2522 vsock_init_tables();
2524 vsock_proto
.owner
= THIS_MODULE
;
2525 vsock_device
.minor
= MISC_DYNAMIC_MINOR
;
2526 err
= misc_register(&vsock_device
);
2528 pr_err("Failed to register misc device\n");
2529 goto err_reset_transport
;
2532 err
= proto_register(&vsock_proto
, 1); /* we want our slab */
2534 pr_err("Cannot register vsock protocol\n");
2535 goto err_deregister_misc
;
2538 err
= sock_register(&vsock_family_ops
);
2540 pr_err("could not register af_vsock (%d) address family: %d\n",
2542 goto err_unregister_proto
;
2545 vsock_bpf_build_proto();
2549 err_unregister_proto
:
2550 proto_unregister(&vsock_proto
);
2551 err_deregister_misc
:
2552 misc_deregister(&vsock_device
);
2553 err_reset_transport
:
2557 static void __exit
vsock_exit(void)
2559 misc_deregister(&vsock_device
);
2560 sock_unregister(AF_VSOCK
);
2561 proto_unregister(&vsock_proto
);
2564 const struct vsock_transport
*vsock_core_get_transport(struct vsock_sock
*vsk
)
2566 return vsk
->transport
;
2568 EXPORT_SYMBOL_GPL(vsock_core_get_transport
);
2570 int vsock_core_register(const struct vsock_transport
*t
, int features
)
2572 const struct vsock_transport
*t_h2g
, *t_g2h
, *t_dgram
, *t_local
;
2573 int err
= mutex_lock_interruptible(&vsock_register_mutex
);
2578 t_h2g
= transport_h2g
;
2579 t_g2h
= transport_g2h
;
2580 t_dgram
= transport_dgram
;
2581 t_local
= transport_local
;
2583 if (features
& VSOCK_TRANSPORT_F_H2G
) {
2591 if (features
& VSOCK_TRANSPORT_F_G2H
) {
2599 if (features
& VSOCK_TRANSPORT_F_DGRAM
) {
2607 if (features
& VSOCK_TRANSPORT_F_LOCAL
) {
2615 transport_h2g
= t_h2g
;
2616 transport_g2h
= t_g2h
;
2617 transport_dgram
= t_dgram
;
2618 transport_local
= t_local
;
2621 mutex_unlock(&vsock_register_mutex
);
2624 EXPORT_SYMBOL_GPL(vsock_core_register
);
2626 void vsock_core_unregister(const struct vsock_transport
*t
)
2628 mutex_lock(&vsock_register_mutex
);
2630 if (transport_h2g
== t
)
2631 transport_h2g
= NULL
;
2633 if (transport_g2h
== t
)
2634 transport_g2h
= NULL
;
2636 if (transport_dgram
== t
)
2637 transport_dgram
= NULL
;
2639 if (transport_local
== t
)
2640 transport_local
= NULL
;
2642 mutex_unlock(&vsock_register_mutex
);
2644 EXPORT_SYMBOL_GPL(vsock_core_unregister
);
2646 module_init(vsock_init
);
2647 module_exit(vsock_exit
);
2649 MODULE_AUTHOR("VMware, Inc.");
2650 MODULE_DESCRIPTION("VMware Virtual Socket Family");
2651 MODULE_VERSION("1.0.2.0-k");
2652 MODULE_LICENSE("GPL v2");