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
);
121 /* Protocol family. */
122 struct proto vsock_proto
= {
124 .owner
= THIS_MODULE
,
125 .obj_size
= sizeof(struct vsock_sock
),
126 #ifdef CONFIG_BPF_SYSCALL
127 .psock_update_sk_prot
= vsock_bpf_update_proto
,
131 /* The default peer timeout indicates how long we will wait for a peer response
132 * to a control message.
134 #define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
136 #define VSOCK_DEFAULT_BUFFER_SIZE (1024 * 256)
137 #define VSOCK_DEFAULT_BUFFER_MAX_SIZE (1024 * 256)
138 #define VSOCK_DEFAULT_BUFFER_MIN_SIZE 128
140 /* Transport used for host->guest communication */
141 static const struct vsock_transport
*transport_h2g
;
142 /* Transport used for guest->host communication */
143 static const struct vsock_transport
*transport_g2h
;
144 /* Transport used for DGRAM communication */
145 static const struct vsock_transport
*transport_dgram
;
146 /* Transport used for local communication */
147 static const struct vsock_transport
*transport_local
;
148 static DEFINE_MUTEX(vsock_register_mutex
);
152 /* Each bound VSocket is stored in the bind hash table and each connected
153 * VSocket is stored in the connected hash table.
155 * Unbound sockets are all put on the same list attached to the end of the hash
156 * table (vsock_unbound_sockets). Bound sockets are added to the hash table in
157 * the bucket that their local address hashes to (vsock_bound_sockets(addr)
158 * represents the list that addr hashes to).
160 * Specifically, we initialize the vsock_bind_table array to a size of
161 * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through
162 * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and
163 * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function
164 * mods with VSOCK_HASH_SIZE to ensure this.
166 #define MAX_PORT_RETRIES 24
168 #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
169 #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)])
170 #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE])
172 /* XXX This can probably be implemented in a better way. */
173 #define VSOCK_CONN_HASH(src, dst) \
174 (((src)->svm_cid ^ (dst)->svm_port) % VSOCK_HASH_SIZE)
175 #define vsock_connected_sockets(src, dst) \
176 (&vsock_connected_table[VSOCK_CONN_HASH(src, dst)])
177 #define vsock_connected_sockets_vsk(vsk) \
178 vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr)
180 struct list_head vsock_bind_table
[VSOCK_HASH_SIZE
+ 1];
181 EXPORT_SYMBOL_GPL(vsock_bind_table
);
182 struct list_head vsock_connected_table
[VSOCK_HASH_SIZE
];
183 EXPORT_SYMBOL_GPL(vsock_connected_table
);
184 DEFINE_SPINLOCK(vsock_table_lock
);
185 EXPORT_SYMBOL_GPL(vsock_table_lock
);
187 /* Autobind this socket to the local address if necessary. */
188 static int vsock_auto_bind(struct vsock_sock
*vsk
)
190 struct sock
*sk
= sk_vsock(vsk
);
191 struct sockaddr_vm local_addr
;
193 if (vsock_addr_bound(&vsk
->local_addr
))
195 vsock_addr_init(&local_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
196 return __vsock_bind(sk
, &local_addr
);
199 static void vsock_init_tables(void)
203 for (i
= 0; i
< ARRAY_SIZE(vsock_bind_table
); i
++)
204 INIT_LIST_HEAD(&vsock_bind_table
[i
]);
206 for (i
= 0; i
< ARRAY_SIZE(vsock_connected_table
); i
++)
207 INIT_LIST_HEAD(&vsock_connected_table
[i
]);
210 static void __vsock_insert_bound(struct list_head
*list
,
211 struct vsock_sock
*vsk
)
214 list_add(&vsk
->bound_table
, list
);
217 static void __vsock_insert_connected(struct list_head
*list
,
218 struct vsock_sock
*vsk
)
221 list_add(&vsk
->connected_table
, list
);
224 static void __vsock_remove_bound(struct vsock_sock
*vsk
)
226 list_del_init(&vsk
->bound_table
);
230 static void __vsock_remove_connected(struct vsock_sock
*vsk
)
232 list_del_init(&vsk
->connected_table
);
236 static struct sock
*__vsock_find_bound_socket(struct sockaddr_vm
*addr
)
238 struct vsock_sock
*vsk
;
240 list_for_each_entry(vsk
, vsock_bound_sockets(addr
), bound_table
) {
241 if (vsock_addr_equals_addr(addr
, &vsk
->local_addr
))
242 return sk_vsock(vsk
);
244 if (addr
->svm_port
== vsk
->local_addr
.svm_port
&&
245 (vsk
->local_addr
.svm_cid
== VMADDR_CID_ANY
||
246 addr
->svm_cid
== VMADDR_CID_ANY
))
247 return sk_vsock(vsk
);
253 static struct sock
*__vsock_find_connected_socket(struct sockaddr_vm
*src
,
254 struct sockaddr_vm
*dst
)
256 struct vsock_sock
*vsk
;
258 list_for_each_entry(vsk
, vsock_connected_sockets(src
, dst
),
260 if (vsock_addr_equals_addr(src
, &vsk
->remote_addr
) &&
261 dst
->svm_port
== vsk
->local_addr
.svm_port
) {
262 return sk_vsock(vsk
);
269 static void vsock_insert_unbound(struct vsock_sock
*vsk
)
271 spin_lock_bh(&vsock_table_lock
);
272 __vsock_insert_bound(vsock_unbound_sockets
, vsk
);
273 spin_unlock_bh(&vsock_table_lock
);
276 void vsock_insert_connected(struct vsock_sock
*vsk
)
278 struct list_head
*list
= vsock_connected_sockets(
279 &vsk
->remote_addr
, &vsk
->local_addr
);
281 spin_lock_bh(&vsock_table_lock
);
282 __vsock_insert_connected(list
, vsk
);
283 spin_unlock_bh(&vsock_table_lock
);
285 EXPORT_SYMBOL_GPL(vsock_insert_connected
);
287 void vsock_remove_bound(struct vsock_sock
*vsk
)
289 spin_lock_bh(&vsock_table_lock
);
290 if (__vsock_in_bound_table(vsk
))
291 __vsock_remove_bound(vsk
);
292 spin_unlock_bh(&vsock_table_lock
);
294 EXPORT_SYMBOL_GPL(vsock_remove_bound
);
296 void vsock_remove_connected(struct vsock_sock
*vsk
)
298 spin_lock_bh(&vsock_table_lock
);
299 if (__vsock_in_connected_table(vsk
))
300 __vsock_remove_connected(vsk
);
301 spin_unlock_bh(&vsock_table_lock
);
303 EXPORT_SYMBOL_GPL(vsock_remove_connected
);
305 struct sock
*vsock_find_bound_socket(struct sockaddr_vm
*addr
)
309 spin_lock_bh(&vsock_table_lock
);
310 sk
= __vsock_find_bound_socket(addr
);
314 spin_unlock_bh(&vsock_table_lock
);
318 EXPORT_SYMBOL_GPL(vsock_find_bound_socket
);
320 struct sock
*vsock_find_connected_socket(struct sockaddr_vm
*src
,
321 struct sockaddr_vm
*dst
)
325 spin_lock_bh(&vsock_table_lock
);
326 sk
= __vsock_find_connected_socket(src
, dst
);
330 spin_unlock_bh(&vsock_table_lock
);
334 EXPORT_SYMBOL_GPL(vsock_find_connected_socket
);
336 void vsock_remove_sock(struct vsock_sock
*vsk
)
338 vsock_remove_bound(vsk
);
339 vsock_remove_connected(vsk
);
341 EXPORT_SYMBOL_GPL(vsock_remove_sock
);
343 void vsock_for_each_connected_socket(struct vsock_transport
*transport
,
344 void (*fn
)(struct sock
*sk
))
348 spin_lock_bh(&vsock_table_lock
);
350 for (i
= 0; i
< ARRAY_SIZE(vsock_connected_table
); i
++) {
351 struct vsock_sock
*vsk
;
352 list_for_each_entry(vsk
, &vsock_connected_table
[i
],
354 if (vsk
->transport
!= transport
)
361 spin_unlock_bh(&vsock_table_lock
);
363 EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket
);
365 void vsock_add_pending(struct sock
*listener
, struct sock
*pending
)
367 struct vsock_sock
*vlistener
;
368 struct vsock_sock
*vpending
;
370 vlistener
= vsock_sk(listener
);
371 vpending
= vsock_sk(pending
);
375 list_add_tail(&vpending
->pending_links
, &vlistener
->pending_links
);
377 EXPORT_SYMBOL_GPL(vsock_add_pending
);
379 void vsock_remove_pending(struct sock
*listener
, struct sock
*pending
)
381 struct vsock_sock
*vpending
= vsock_sk(pending
);
383 list_del_init(&vpending
->pending_links
);
387 EXPORT_SYMBOL_GPL(vsock_remove_pending
);
389 void vsock_enqueue_accept(struct sock
*listener
, struct sock
*connected
)
391 struct vsock_sock
*vlistener
;
392 struct vsock_sock
*vconnected
;
394 vlistener
= vsock_sk(listener
);
395 vconnected
= vsock_sk(connected
);
397 sock_hold(connected
);
399 list_add_tail(&vconnected
->accept_queue
, &vlistener
->accept_queue
);
401 EXPORT_SYMBOL_GPL(vsock_enqueue_accept
);
403 static bool vsock_use_local_transport(unsigned int remote_cid
)
405 if (!transport_local
)
408 if (remote_cid
== VMADDR_CID_LOCAL
)
412 return remote_cid
== transport_g2h
->get_local_cid();
414 return remote_cid
== VMADDR_CID_HOST
;
418 static void vsock_deassign_transport(struct vsock_sock
*vsk
)
423 vsk
->transport
->destruct(vsk
);
424 module_put(vsk
->transport
->module
);
425 vsk
->transport
= NULL
;
428 /* Assign a transport to a socket and call the .init transport callback.
430 * Note: for connection oriented socket this must be called when vsk->remote_addr
431 * is set (e.g. during the connect() or when a connection request on a listener
432 * socket is received).
433 * The vsk->remote_addr is used to decide which transport to use:
434 * - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if
435 * g2h is not loaded, will use local transport;
436 * - remote CID <= VMADDR_CID_HOST or h2g is not loaded or remote flags field
437 * includes VMADDR_FLAG_TO_HOST flag value, will use guest->host transport;
438 * - remote CID > VMADDR_CID_HOST will use host->guest transport;
440 int vsock_assign_transport(struct vsock_sock
*vsk
, struct vsock_sock
*psk
)
442 const struct vsock_transport
*new_transport
;
443 struct sock
*sk
= sk_vsock(vsk
);
444 unsigned int remote_cid
= vsk
->remote_addr
.svm_cid
;
448 /* If the packet is coming with the source and destination CIDs higher
449 * than VMADDR_CID_HOST, then a vsock channel where all the packets are
450 * forwarded to the host should be established. Then the host will
451 * need to forward the packets to the guest.
453 * The flag is set on the (listen) receive path (psk is not NULL). On
454 * the connect path the flag can be set by the user space application.
456 if (psk
&& vsk
->local_addr
.svm_cid
> VMADDR_CID_HOST
&&
457 vsk
->remote_addr
.svm_cid
> VMADDR_CID_HOST
)
458 vsk
->remote_addr
.svm_flags
|= VMADDR_FLAG_TO_HOST
;
460 remote_flags
= vsk
->remote_addr
.svm_flags
;
462 switch (sk
->sk_type
) {
464 new_transport
= transport_dgram
;
468 if (vsock_use_local_transport(remote_cid
))
469 new_transport
= transport_local
;
470 else if (remote_cid
<= VMADDR_CID_HOST
|| !transport_h2g
||
471 (remote_flags
& VMADDR_FLAG_TO_HOST
))
472 new_transport
= transport_g2h
;
474 new_transport
= transport_h2g
;
477 return -ESOCKTNOSUPPORT
;
480 if (vsk
->transport
) {
481 if (vsk
->transport
== new_transport
)
484 /* transport->release() must be called with sock lock acquired.
485 * This path can only be taken during vsock_connect(), where we
486 * have already held the sock lock. In the other cases, this
487 * function is called on a new socket which is not assigned to
490 vsk
->transport
->release(vsk
);
491 vsock_deassign_transport(vsk
);
494 /* We increase the module refcnt to prevent the transport unloading
495 * while there are open sockets assigned to it.
497 if (!new_transport
|| !try_module_get(new_transport
->module
))
500 if (sk
->sk_type
== SOCK_SEQPACKET
) {
501 if (!new_transport
->seqpacket_allow
||
502 !new_transport
->seqpacket_allow(remote_cid
)) {
503 module_put(new_transport
->module
);
504 return -ESOCKTNOSUPPORT
;
508 ret
= new_transport
->init(vsk
, psk
);
510 module_put(new_transport
->module
);
514 vsk
->transport
= new_transport
;
518 EXPORT_SYMBOL_GPL(vsock_assign_transport
);
520 bool vsock_find_cid(unsigned int cid
)
522 if (transport_g2h
&& cid
== transport_g2h
->get_local_cid())
525 if (transport_h2g
&& cid
== VMADDR_CID_HOST
)
528 if (transport_local
&& cid
== VMADDR_CID_LOCAL
)
533 EXPORT_SYMBOL_GPL(vsock_find_cid
);
535 static struct sock
*vsock_dequeue_accept(struct sock
*listener
)
537 struct vsock_sock
*vlistener
;
538 struct vsock_sock
*vconnected
;
540 vlistener
= vsock_sk(listener
);
542 if (list_empty(&vlistener
->accept_queue
))
545 vconnected
= list_entry(vlistener
->accept_queue
.next
,
546 struct vsock_sock
, accept_queue
);
548 list_del_init(&vconnected
->accept_queue
);
550 /* The caller will need a reference on the connected socket so we let
551 * it call sock_put().
554 return sk_vsock(vconnected
);
557 static bool vsock_is_accept_queue_empty(struct sock
*sk
)
559 struct vsock_sock
*vsk
= vsock_sk(sk
);
560 return list_empty(&vsk
->accept_queue
);
563 static bool vsock_is_pending(struct sock
*sk
)
565 struct vsock_sock
*vsk
= vsock_sk(sk
);
566 return !list_empty(&vsk
->pending_links
);
569 static int vsock_send_shutdown(struct sock
*sk
, int mode
)
571 struct vsock_sock
*vsk
= vsock_sk(sk
);
576 return vsk
->transport
->shutdown(vsk
, mode
);
579 static void vsock_pending_work(struct work_struct
*work
)
582 struct sock
*listener
;
583 struct vsock_sock
*vsk
;
586 vsk
= container_of(work
, struct vsock_sock
, pending_work
.work
);
588 listener
= vsk
->listener
;
592 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
594 if (vsock_is_pending(sk
)) {
595 vsock_remove_pending(listener
, sk
);
597 sk_acceptq_removed(listener
);
598 } else if (!vsk
->rejected
) {
599 /* We are not on the pending list and accept() did not reject
600 * us, so we must have been accepted by our user process. We
601 * just need to drop our references to the sockets and be on
608 /* We need to remove ourself from the global connected sockets list so
609 * incoming packets can't find this socket, and to reduce the reference
612 vsock_remove_connected(vsk
);
614 sk
->sk_state
= TCP_CLOSE
;
618 release_sock(listener
);
626 /**** SOCKET OPERATIONS ****/
628 static int __vsock_bind_connectible(struct vsock_sock
*vsk
,
629 struct sockaddr_vm
*addr
)
632 struct sockaddr_vm new_addr
;
635 port
= get_random_u32_above(LAST_RESERVED_PORT
);
637 vsock_addr_init(&new_addr
, addr
->svm_cid
, addr
->svm_port
);
639 if (addr
->svm_port
== VMADDR_PORT_ANY
) {
643 for (i
= 0; i
< MAX_PORT_RETRIES
; i
++) {
644 if (port
<= LAST_RESERVED_PORT
)
645 port
= LAST_RESERVED_PORT
+ 1;
647 new_addr
.svm_port
= port
++;
649 if (!__vsock_find_bound_socket(&new_addr
)) {
656 return -EADDRNOTAVAIL
;
658 /* If port is in reserved range, ensure caller
659 * has necessary privileges.
661 if (addr
->svm_port
<= LAST_RESERVED_PORT
&&
662 !capable(CAP_NET_BIND_SERVICE
)) {
666 if (__vsock_find_bound_socket(&new_addr
))
670 vsock_addr_init(&vsk
->local_addr
, new_addr
.svm_cid
, new_addr
.svm_port
);
672 /* Remove connection oriented sockets from the unbound list and add them
673 * to the hash table for easy lookup by its address. The unbound list
674 * is simply an extra entry at the end of the hash table, a trick used
677 __vsock_remove_bound(vsk
);
678 __vsock_insert_bound(vsock_bound_sockets(&vsk
->local_addr
), vsk
);
683 static int __vsock_bind_dgram(struct vsock_sock
*vsk
,
684 struct sockaddr_vm
*addr
)
686 return vsk
->transport
->dgram_bind(vsk
, addr
);
689 static int __vsock_bind(struct sock
*sk
, struct sockaddr_vm
*addr
)
691 struct vsock_sock
*vsk
= vsock_sk(sk
);
694 /* First ensure this socket isn't already bound. */
695 if (vsock_addr_bound(&vsk
->local_addr
))
698 /* Now bind to the provided address or select appropriate values if
699 * none are provided (VMADDR_CID_ANY and VMADDR_PORT_ANY). Note that
700 * like AF_INET prevents binding to a non-local IP address (in most
701 * cases), we only allow binding to a local CID.
703 if (addr
->svm_cid
!= VMADDR_CID_ANY
&& !vsock_find_cid(addr
->svm_cid
))
704 return -EADDRNOTAVAIL
;
706 switch (sk
->sk_socket
->type
) {
709 spin_lock_bh(&vsock_table_lock
);
710 retval
= __vsock_bind_connectible(vsk
, addr
);
711 spin_unlock_bh(&vsock_table_lock
);
715 retval
= __vsock_bind_dgram(vsk
, addr
);
726 static void vsock_connect_timeout(struct work_struct
*work
);
728 static struct sock
*__vsock_create(struct net
*net
,
736 struct vsock_sock
*psk
;
737 struct vsock_sock
*vsk
;
739 sk
= sk_alloc(net
, AF_VSOCK
, priority
, &vsock_proto
, kern
);
743 sock_init_data(sock
, sk
);
745 /* sk->sk_type is normally set in sock_init_data, but only if sock is
746 * non-NULL. We make sure that our sockets always have a type by
747 * setting it here if needed.
753 vsock_addr_init(&vsk
->local_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
754 vsock_addr_init(&vsk
->remote_addr
, VMADDR_CID_ANY
, VMADDR_PORT_ANY
);
756 sk
->sk_destruct
= vsock_sk_destruct
;
757 sk
->sk_backlog_rcv
= vsock_queue_rcv_skb
;
758 sock_reset_flag(sk
, SOCK_DONE
);
760 INIT_LIST_HEAD(&vsk
->bound_table
);
761 INIT_LIST_HEAD(&vsk
->connected_table
);
762 vsk
->listener
= NULL
;
763 INIT_LIST_HEAD(&vsk
->pending_links
);
764 INIT_LIST_HEAD(&vsk
->accept_queue
);
765 vsk
->rejected
= false;
766 vsk
->sent_request
= false;
767 vsk
->ignore_connecting_rst
= false;
768 vsk
->peer_shutdown
= 0;
769 INIT_DELAYED_WORK(&vsk
->connect_work
, vsock_connect_timeout
);
770 INIT_DELAYED_WORK(&vsk
->pending_work
, vsock_pending_work
);
772 psk
= parent
? vsock_sk(parent
) : NULL
;
774 vsk
->trusted
= psk
->trusted
;
775 vsk
->owner
= get_cred(psk
->owner
);
776 vsk
->connect_timeout
= psk
->connect_timeout
;
777 vsk
->buffer_size
= psk
->buffer_size
;
778 vsk
->buffer_min_size
= psk
->buffer_min_size
;
779 vsk
->buffer_max_size
= psk
->buffer_max_size
;
780 security_sk_clone(parent
, sk
);
782 vsk
->trusted
= ns_capable_noaudit(&init_user_ns
, CAP_NET_ADMIN
);
783 vsk
->owner
= get_current_cred();
784 vsk
->connect_timeout
= VSOCK_DEFAULT_CONNECT_TIMEOUT
;
785 vsk
->buffer_size
= VSOCK_DEFAULT_BUFFER_SIZE
;
786 vsk
->buffer_min_size
= VSOCK_DEFAULT_BUFFER_MIN_SIZE
;
787 vsk
->buffer_max_size
= VSOCK_DEFAULT_BUFFER_MAX_SIZE
;
793 static bool sock_type_connectible(u16 type
)
795 return (type
== SOCK_STREAM
) || (type
== SOCK_SEQPACKET
);
798 static void __vsock_release(struct sock
*sk
, int level
)
801 struct sock
*pending
;
802 struct vsock_sock
*vsk
;
805 pending
= NULL
; /* Compiler warning. */
807 /* When "level" is SINGLE_DEPTH_NESTING, use the nested
808 * version to avoid the warning "possible recursive locking
809 * detected". When "level" is 0, lock_sock_nested(sk, level)
810 * is the same as lock_sock(sk).
812 lock_sock_nested(sk
, level
);
815 vsk
->transport
->release(vsk
);
816 else if (sock_type_connectible(sk
->sk_type
))
817 vsock_remove_sock(vsk
);
820 sk
->sk_shutdown
= SHUTDOWN_MASK
;
822 skb_queue_purge(&sk
->sk_receive_queue
);
824 /* Clean up any sockets that never were accepted. */
825 while ((pending
= vsock_dequeue_accept(sk
)) != NULL
) {
826 __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 static int vsock_release(struct socket
*sock
)
906 __vsock_release(sock
->sk
, 0);
908 sock
->state
= SS_FREE
;
914 vsock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
918 struct sockaddr_vm
*vm_addr
;
922 if (vsock_addr_cast(addr
, addr_len
, &vm_addr
) != 0)
926 err
= __vsock_bind(sk
, vm_addr
);
932 static int vsock_getname(struct socket
*sock
,
933 struct sockaddr
*addr
, int peer
)
937 struct vsock_sock
*vsk
;
938 struct sockaddr_vm
*vm_addr
;
947 if (sock
->state
!= SS_CONNECTED
) {
951 vm_addr
= &vsk
->remote_addr
;
953 vm_addr
= &vsk
->local_addr
;
961 /* sys_getsockname() and sys_getpeername() pass us a
962 * MAX_SOCK_ADDR-sized buffer and don't set addr_len. Unfortunately
963 * that macro is defined in socket.c instead of .h, so we hardcode its
966 BUILD_BUG_ON(sizeof(*vm_addr
) > 128);
967 memcpy(addr
, vm_addr
, sizeof(*vm_addr
));
968 err
= sizeof(*vm_addr
);
975 static int vsock_shutdown(struct socket
*sock
, int mode
)
980 /* User level uses SHUT_RD (0) and SHUT_WR (1), but the kernel uses
981 * RCV_SHUTDOWN (1) and SEND_SHUTDOWN (2), so we must increment mode
982 * here like the other address families do. Note also that the
983 * increment makes SHUT_RDWR (2) into RCV_SHUTDOWN | SEND_SHUTDOWN (3),
984 * which is what we want.
988 if ((mode
& ~SHUTDOWN_MASK
) || !mode
)
991 /* If this is a connection oriented socket and it is not connected then
992 * bail out immediately. If it is a DGRAM socket then we must first
993 * kick the socket so that it wakes up from any sleeping calls, for
994 * example recv(), and then afterwards return the error.
1000 if (sock
->state
== SS_UNCONNECTED
) {
1002 if (sock_type_connectible(sk
->sk_type
))
1005 sock
->state
= SS_DISCONNECTING
;
1009 /* Receive and send shutdowns are treated alike. */
1010 mode
= mode
& (RCV_SHUTDOWN
| SEND_SHUTDOWN
);
1012 sk
->sk_shutdown
|= mode
;
1013 sk
->sk_state_change(sk
);
1015 if (sock_type_connectible(sk
->sk_type
)) {
1016 sock_reset_flag(sk
, SOCK_DONE
);
1017 vsock_send_shutdown(sk
, mode
);
1026 static __poll_t
vsock_poll(struct file
*file
, struct socket
*sock
,
1031 struct vsock_sock
*vsk
;
1036 poll_wait(file
, sk_sleep(sk
), wait
);
1039 if (sk
->sk_err
|| !skb_queue_empty_lockless(&sk
->sk_error_queue
))
1040 /* Signify that there has been an error on this socket. */
1043 /* INET sockets treat local write shutdown and peer write shutdown as a
1044 * case of EPOLLHUP set.
1046 if ((sk
->sk_shutdown
== SHUTDOWN_MASK
) ||
1047 ((sk
->sk_shutdown
& SEND_SHUTDOWN
) &&
1048 (vsk
->peer_shutdown
& SEND_SHUTDOWN
))) {
1052 if (sk
->sk_shutdown
& RCV_SHUTDOWN
||
1053 vsk
->peer_shutdown
& SEND_SHUTDOWN
) {
1057 if (sock
->type
== SOCK_DGRAM
) {
1058 /* For datagram sockets we can read if there is something in
1059 * the queue and write as long as the socket isn't shutdown for
1062 if (!skb_queue_empty_lockless(&sk
->sk_receive_queue
) ||
1063 (sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
1064 mask
|= EPOLLIN
| EPOLLRDNORM
;
1067 if (!(sk
->sk_shutdown
& SEND_SHUTDOWN
))
1068 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
1070 } else if (sock_type_connectible(sk
->sk_type
)) {
1071 const struct vsock_transport
*transport
;
1075 transport
= vsk
->transport
;
1077 /* Listening sockets that have connections in their accept
1078 * queue can be read.
1080 if (sk
->sk_state
== TCP_LISTEN
1081 && !vsock_is_accept_queue_empty(sk
))
1082 mask
|= EPOLLIN
| EPOLLRDNORM
;
1084 /* If there is something in the queue then we can read. */
1085 if (transport
&& transport
->stream_is_active(vsk
) &&
1086 !(sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
1087 bool data_ready_now
= false;
1088 int target
= sock_rcvlowat(sk
, 0, INT_MAX
);
1089 int ret
= transport
->notify_poll_in(
1090 vsk
, target
, &data_ready_now
);
1095 mask
|= EPOLLIN
| EPOLLRDNORM
;
1100 /* Sockets whose connections have been closed, reset, or
1101 * terminated should also be considered read, and we check the
1102 * shutdown flag for that.
1104 if (sk
->sk_shutdown
& RCV_SHUTDOWN
||
1105 vsk
->peer_shutdown
& SEND_SHUTDOWN
) {
1106 mask
|= EPOLLIN
| EPOLLRDNORM
;
1109 /* Connected sockets that can produce data can be written. */
1110 if (transport
&& sk
->sk_state
== TCP_ESTABLISHED
) {
1111 if (!(sk
->sk_shutdown
& SEND_SHUTDOWN
)) {
1112 bool space_avail_now
= false;
1113 int ret
= transport
->notify_poll_out(
1114 vsk
, 1, &space_avail_now
);
1118 if (space_avail_now
)
1119 /* Remove EPOLLWRBAND since INET
1120 * sockets are not setting it.
1122 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1128 /* Simulate INET socket poll behaviors, which sets
1129 * EPOLLOUT|EPOLLWRNORM when peer is closed and nothing to read,
1130 * but local send is not shutdown.
1132 if (sk
->sk_state
== TCP_CLOSE
|| sk
->sk_state
== TCP_CLOSING
) {
1133 if (!(sk
->sk_shutdown
& SEND_SHUTDOWN
))
1134 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1144 static int vsock_read_skb(struct sock
*sk
, skb_read_actor_t read_actor
)
1146 struct vsock_sock
*vsk
= vsock_sk(sk
);
1148 return vsk
->transport
->read_skb(vsk
, read_actor
);
1151 static int vsock_dgram_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
1156 struct vsock_sock
*vsk
;
1157 struct sockaddr_vm
*remote_addr
;
1158 const struct vsock_transport
*transport
;
1160 if (msg
->msg_flags
& MSG_OOB
)
1163 /* For now, MSG_DONTWAIT is always assumed... */
1170 transport
= vsk
->transport
;
1172 err
= vsock_auto_bind(vsk
);
1177 /* If the provided message contains an address, use that. Otherwise
1178 * fall back on the socket's remote handle (if it has been connected).
1180 if (msg
->msg_name
&&
1181 vsock_addr_cast(msg
->msg_name
, msg
->msg_namelen
,
1182 &remote_addr
) == 0) {
1183 /* Ensure this address is of the right type and is a valid
1187 if (remote_addr
->svm_cid
== VMADDR_CID_ANY
)
1188 remote_addr
->svm_cid
= transport
->get_local_cid();
1190 if (!vsock_addr_bound(remote_addr
)) {
1194 } else if (sock
->state
== SS_CONNECTED
) {
1195 remote_addr
= &vsk
->remote_addr
;
1197 if (remote_addr
->svm_cid
== VMADDR_CID_ANY
)
1198 remote_addr
->svm_cid
= transport
->get_local_cid();
1200 /* XXX Should connect() or this function ensure remote_addr is
1203 if (!vsock_addr_bound(&vsk
->remote_addr
)) {
1212 if (!transport
->dgram_allow(remote_addr
->svm_cid
,
1213 remote_addr
->svm_port
)) {
1218 err
= transport
->dgram_enqueue(vsk
, remote_addr
, msg
, len
);
1225 static int vsock_dgram_connect(struct socket
*sock
,
1226 struct sockaddr
*addr
, int addr_len
, int flags
)
1230 struct vsock_sock
*vsk
;
1231 struct sockaddr_vm
*remote_addr
;
1236 err
= vsock_addr_cast(addr
, addr_len
, &remote_addr
);
1237 if (err
== -EAFNOSUPPORT
&& remote_addr
->svm_family
== AF_UNSPEC
) {
1239 vsock_addr_init(&vsk
->remote_addr
, VMADDR_CID_ANY
,
1241 sock
->state
= SS_UNCONNECTED
;
1244 } else if (err
!= 0)
1249 err
= vsock_auto_bind(vsk
);
1253 if (!vsk
->transport
->dgram_allow(remote_addr
->svm_cid
,
1254 remote_addr
->svm_port
)) {
1259 memcpy(&vsk
->remote_addr
, remote_addr
, sizeof(vsk
->remote_addr
));
1260 sock
->state
= SS_CONNECTED
;
1262 /* sock map disallows redirection of non-TCP sockets with sk_state !=
1263 * TCP_ESTABLISHED (see sock_map_redirect_allowed()), so we set
1264 * TCP_ESTABLISHED here to allow redirection of connected vsock dgrams.
1266 * This doesn't seem to be abnormal state for datagram sockets, as the
1267 * same approach can be see in other datagram socket types as well
1268 * (such as unix sockets).
1270 sk
->sk_state
= TCP_ESTABLISHED
;
1277 int __vsock_dgram_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1278 size_t len
, int flags
)
1280 struct sock
*sk
= sock
->sk
;
1281 struct vsock_sock
*vsk
= vsock_sk(sk
);
1283 return vsk
->transport
->dgram_dequeue(vsk
, msg
, len
, flags
);
1286 int vsock_dgram_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1287 size_t len
, int flags
)
1289 #ifdef CONFIG_BPF_SYSCALL
1290 struct sock
*sk
= sock
->sk
;
1291 const struct proto
*prot
;
1293 prot
= READ_ONCE(sk
->sk_prot
);
1294 if (prot
!= &vsock_proto
)
1295 return prot
->recvmsg(sk
, msg
, len
, flags
, NULL
);
1298 return __vsock_dgram_recvmsg(sock
, msg
, len
, flags
);
1300 EXPORT_SYMBOL_GPL(vsock_dgram_recvmsg
);
1302 static int vsock_do_ioctl(struct socket
*sock
, unsigned int cmd
,
1305 struct sock
*sk
= sock
->sk
;
1306 struct vsock_sock
*vsk
;
1315 if (!vsk
->transport
|| !vsk
->transport
->unsent_bytes
) {
1320 if (sock_type_connectible(sk
->sk_type
) && sk
->sk_state
== TCP_LISTEN
) {
1325 n_bytes
= vsk
->transport
->unsent_bytes(vsk
);
1331 ret
= put_user(n_bytes
, arg
);
1341 static int vsock_ioctl(struct socket
*sock
, unsigned int cmd
,
1346 lock_sock(sock
->sk
);
1347 ret
= vsock_do_ioctl(sock
, cmd
, (int __user
*)arg
);
1348 release_sock(sock
->sk
);
1353 static const struct proto_ops vsock_dgram_ops
= {
1355 .owner
= THIS_MODULE
,
1356 .release
= vsock_release
,
1358 .connect
= vsock_dgram_connect
,
1359 .socketpair
= sock_no_socketpair
,
1360 .accept
= sock_no_accept
,
1361 .getname
= vsock_getname
,
1363 .ioctl
= vsock_ioctl
,
1364 .listen
= sock_no_listen
,
1365 .shutdown
= vsock_shutdown
,
1366 .sendmsg
= vsock_dgram_sendmsg
,
1367 .recvmsg
= vsock_dgram_recvmsg
,
1368 .mmap
= sock_no_mmap
,
1369 .read_skb
= vsock_read_skb
,
1372 static int vsock_transport_cancel_pkt(struct vsock_sock
*vsk
)
1374 const struct vsock_transport
*transport
= vsk
->transport
;
1376 if (!transport
|| !transport
->cancel_pkt
)
1379 return transport
->cancel_pkt(vsk
);
1382 static void vsock_connect_timeout(struct work_struct
*work
)
1385 struct vsock_sock
*vsk
;
1387 vsk
= container_of(work
, struct vsock_sock
, connect_work
.work
);
1391 if (sk
->sk_state
== TCP_SYN_SENT
&&
1392 (sk
->sk_shutdown
!= SHUTDOWN_MASK
)) {
1393 sk
->sk_state
= TCP_CLOSE
;
1394 sk
->sk_socket
->state
= SS_UNCONNECTED
;
1395 sk
->sk_err
= ETIMEDOUT
;
1396 sk_error_report(sk
);
1397 vsock_transport_cancel_pkt(vsk
);
1404 static int vsock_connect(struct socket
*sock
, struct sockaddr
*addr
,
1405 int addr_len
, int flags
)
1409 struct vsock_sock
*vsk
;
1410 const struct vsock_transport
*transport
;
1411 struct sockaddr_vm
*remote_addr
;
1421 /* XXX AF_UNSPEC should make us disconnect like AF_INET. */
1422 switch (sock
->state
) {
1426 case SS_DISCONNECTING
:
1430 /* This continues on so we can move sock into the SS_CONNECTED
1431 * state once the connection has completed (at which point err
1432 * will be set to zero also). Otherwise, we will either wait
1433 * for the connection or return -EALREADY should this be a
1434 * non-blocking call.
1437 if (flags
& O_NONBLOCK
)
1441 if ((sk
->sk_state
== TCP_LISTEN
) ||
1442 vsock_addr_cast(addr
, addr_len
, &remote_addr
) != 0) {
1447 /* Set the remote address that we are connecting to. */
1448 memcpy(&vsk
->remote_addr
, remote_addr
,
1449 sizeof(vsk
->remote_addr
));
1451 err
= vsock_assign_transport(vsk
, NULL
);
1455 transport
= vsk
->transport
;
1457 /* The hypervisor and well-known contexts do not have socket
1461 !transport
->stream_allow(remote_addr
->svm_cid
,
1462 remote_addr
->svm_port
)) {
1467 if (vsock_msgzerocopy_allow(transport
)) {
1468 set_bit(SOCK_SUPPORT_ZC
, &sk
->sk_socket
->flags
);
1469 } else if (sock_flag(sk
, SOCK_ZEROCOPY
)) {
1470 /* If this option was set before 'connect()',
1471 * when transport was unknown, check that this
1472 * feature is supported here.
1478 err
= vsock_auto_bind(vsk
);
1482 sk
->sk_state
= TCP_SYN_SENT
;
1484 err
= transport
->connect(vsk
);
1488 /* Mark sock as connecting and set the error code to in
1489 * progress in case this is a non-blocking connect.
1491 sock
->state
= SS_CONNECTING
;
1495 /* The receive path will handle all communication until we are able to
1496 * enter the connected state. Here we wait for the connection to be
1497 * completed or a notification of an error.
1499 timeout
= vsk
->connect_timeout
;
1500 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1502 while (sk
->sk_state
!= TCP_ESTABLISHED
&& sk
->sk_err
== 0) {
1503 if (flags
& O_NONBLOCK
) {
1504 /* If we're not going to block, we schedule a timeout
1505 * function to generate a timeout on the connection
1506 * attempt, in case the peer doesn't respond in a
1507 * timely manner. We hold on to the socket until the
1512 /* If the timeout function is already scheduled,
1513 * reschedule it, then ungrab the socket refcount to
1516 if (mod_delayed_work(system_wq
, &vsk
->connect_work
,
1520 /* Skip ahead to preserve error code set above. */
1525 timeout
= schedule_timeout(timeout
);
1528 if (signal_pending(current
)) {
1529 err
= sock_intr_errno(timeout
);
1530 sk
->sk_state
= sk
->sk_state
== TCP_ESTABLISHED
? TCP_CLOSING
: TCP_CLOSE
;
1531 sock
->state
= SS_UNCONNECTED
;
1532 vsock_transport_cancel_pkt(vsk
);
1533 vsock_remove_connected(vsk
);
1535 } else if ((sk
->sk_state
!= TCP_ESTABLISHED
) && (timeout
== 0)) {
1537 sk
->sk_state
= TCP_CLOSE
;
1538 sock
->state
= SS_UNCONNECTED
;
1539 vsock_transport_cancel_pkt(vsk
);
1543 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1548 sk
->sk_state
= TCP_CLOSE
;
1549 sock
->state
= SS_UNCONNECTED
;
1555 finish_wait(sk_sleep(sk
), &wait
);
1561 static int vsock_accept(struct socket
*sock
, struct socket
*newsock
,
1562 struct proto_accept_arg
*arg
)
1564 struct sock
*listener
;
1566 struct sock
*connected
;
1567 struct vsock_sock
*vconnected
;
1572 listener
= sock
->sk
;
1574 lock_sock(listener
);
1576 if (!sock_type_connectible(sock
->type
)) {
1581 if (listener
->sk_state
!= TCP_LISTEN
) {
1586 /* Wait for children sockets to appear; these are the new sockets
1587 * created upon connection establishment.
1589 timeout
= sock_rcvtimeo(listener
, arg
->flags
& O_NONBLOCK
);
1590 prepare_to_wait(sk_sleep(listener
), &wait
, TASK_INTERRUPTIBLE
);
1592 while ((connected
= vsock_dequeue_accept(listener
)) == NULL
&&
1593 listener
->sk_err
== 0) {
1594 release_sock(listener
);
1595 timeout
= schedule_timeout(timeout
);
1596 finish_wait(sk_sleep(listener
), &wait
);
1597 lock_sock(listener
);
1599 if (signal_pending(current
)) {
1600 err
= sock_intr_errno(timeout
);
1602 } else if (timeout
== 0) {
1607 prepare_to_wait(sk_sleep(listener
), &wait
, TASK_INTERRUPTIBLE
);
1609 finish_wait(sk_sleep(listener
), &wait
);
1611 if (listener
->sk_err
)
1612 err
= -listener
->sk_err
;
1615 sk_acceptq_removed(listener
);
1617 lock_sock_nested(connected
, SINGLE_DEPTH_NESTING
);
1618 vconnected
= vsock_sk(connected
);
1620 /* If the listener socket has received an error, then we should
1621 * reject this socket and return. Note that we simply mark the
1622 * socket rejected, drop our reference, and let the cleanup
1623 * function handle the cleanup; the fact that we found it in
1624 * the listener's accept queue guarantees that the cleanup
1625 * function hasn't run yet.
1628 vconnected
->rejected
= true;
1630 newsock
->state
= SS_CONNECTED
;
1631 sock_graft(connected
, newsock
);
1632 if (vsock_msgzerocopy_allow(vconnected
->transport
))
1633 set_bit(SOCK_SUPPORT_ZC
,
1634 &connected
->sk_socket
->flags
);
1637 release_sock(connected
);
1638 sock_put(connected
);
1642 release_sock(listener
);
1646 static int vsock_listen(struct socket
*sock
, int backlog
)
1650 struct vsock_sock
*vsk
;
1656 if (!sock_type_connectible(sk
->sk_type
)) {
1661 if (sock
->state
!= SS_UNCONNECTED
) {
1668 if (!vsock_addr_bound(&vsk
->local_addr
)) {
1673 sk
->sk_max_ack_backlog
= backlog
;
1674 sk
->sk_state
= TCP_LISTEN
;
1683 static void vsock_update_buffer_size(struct vsock_sock
*vsk
,
1684 const struct vsock_transport
*transport
,
1687 if (val
> vsk
->buffer_max_size
)
1688 val
= vsk
->buffer_max_size
;
1690 if (val
< vsk
->buffer_min_size
)
1691 val
= vsk
->buffer_min_size
;
1693 if (val
!= vsk
->buffer_size
&&
1694 transport
&& transport
->notify_buffer_size
)
1695 transport
->notify_buffer_size(vsk
, &val
);
1697 vsk
->buffer_size
= val
;
1700 static int vsock_connectible_setsockopt(struct socket
*sock
,
1704 unsigned int optlen
)
1708 struct vsock_sock
*vsk
;
1709 const struct vsock_transport
*transport
;
1712 if (level
!= AF_VSOCK
&& level
!= SOL_SOCKET
)
1713 return -ENOPROTOOPT
;
1715 #define COPY_IN(_v) \
1717 if (optlen < sizeof(_v)) { \
1721 if (copy_from_sockptr(&_v, optval, sizeof(_v)) != 0) { \
1733 transport
= vsk
->transport
;
1735 if (level
== SOL_SOCKET
) {
1738 if (optname
!= SO_ZEROCOPY
) {
1740 return sock_setsockopt(sock
, level
, optname
, optval
, optlen
);
1743 /* Use 'int' type here, because variable to
1744 * set this option usually has this type.
1748 if (zerocopy
< 0 || zerocopy
> 1) {
1753 if (transport
&& !vsock_msgzerocopy_allow(transport
)) {
1758 sock_valbool_flag(sk
, SOCK_ZEROCOPY
, zerocopy
);
1763 case SO_VM_SOCKETS_BUFFER_SIZE
:
1765 vsock_update_buffer_size(vsk
, transport
, val
);
1768 case SO_VM_SOCKETS_BUFFER_MAX_SIZE
:
1770 vsk
->buffer_max_size
= val
;
1771 vsock_update_buffer_size(vsk
, transport
, vsk
->buffer_size
);
1774 case SO_VM_SOCKETS_BUFFER_MIN_SIZE
:
1776 vsk
->buffer_min_size
= val
;
1777 vsock_update_buffer_size(vsk
, transport
, vsk
->buffer_size
);
1780 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW
:
1781 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
: {
1782 struct __kernel_sock_timeval tv
;
1784 err
= sock_copy_user_timeval(&tv
, optval
, optlen
,
1785 optname
== SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
);
1788 if (tv
.tv_sec
>= 0 && tv
.tv_usec
< USEC_PER_SEC
&&
1789 tv
.tv_sec
< (MAX_SCHEDULE_TIMEOUT
/ HZ
- 1)) {
1790 vsk
->connect_timeout
= tv
.tv_sec
* HZ
+
1791 DIV_ROUND_UP((unsigned long)tv
.tv_usec
, (USEC_PER_SEC
/ HZ
));
1792 if (vsk
->connect_timeout
== 0)
1793 vsk
->connect_timeout
=
1794 VSOCK_DEFAULT_CONNECT_TIMEOUT
;
1814 static int vsock_connectible_getsockopt(struct socket
*sock
,
1815 int level
, int optname
,
1816 char __user
*optval
,
1819 struct sock
*sk
= sock
->sk
;
1820 struct vsock_sock
*vsk
= vsock_sk(sk
);
1824 struct old_timeval32 tm32
;
1825 struct __kernel_old_timeval tm
;
1826 struct __kernel_sock_timeval stm
;
1829 int lv
= sizeof(v
.val64
);
1832 if (level
!= AF_VSOCK
)
1833 return -ENOPROTOOPT
;
1835 if (get_user(len
, optlen
))
1838 memset(&v
, 0, sizeof(v
));
1841 case SO_VM_SOCKETS_BUFFER_SIZE
:
1842 v
.val64
= vsk
->buffer_size
;
1845 case SO_VM_SOCKETS_BUFFER_MAX_SIZE
:
1846 v
.val64
= vsk
->buffer_max_size
;
1849 case SO_VM_SOCKETS_BUFFER_MIN_SIZE
:
1850 v
.val64
= vsk
->buffer_min_size
;
1853 case SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW
:
1854 case SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
:
1855 lv
= sock_get_timeout(vsk
->connect_timeout
, &v
,
1856 optname
== SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
);
1860 return -ENOPROTOOPT
;
1867 if (copy_to_user(optval
, &v
, len
))
1870 if (put_user(len
, optlen
))
1876 static int vsock_connectible_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
1880 struct vsock_sock
*vsk
;
1881 const struct vsock_transport
*transport
;
1882 ssize_t total_written
;
1885 struct vsock_transport_send_notify_data send_data
;
1886 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
1893 if (msg
->msg_flags
& MSG_OOB
)
1898 transport
= vsk
->transport
;
1900 /* Callers should not provide a destination with connection oriented
1903 if (msg
->msg_namelen
) {
1904 err
= sk
->sk_state
== TCP_ESTABLISHED
? -EISCONN
: -EOPNOTSUPP
;
1908 /* Send data only if both sides are not shutdown in the direction. */
1909 if (sk
->sk_shutdown
& SEND_SHUTDOWN
||
1910 vsk
->peer_shutdown
& RCV_SHUTDOWN
) {
1915 if (!transport
|| sk
->sk_state
!= TCP_ESTABLISHED
||
1916 !vsock_addr_bound(&vsk
->local_addr
)) {
1921 if (!vsock_addr_bound(&vsk
->remote_addr
)) {
1922 err
= -EDESTADDRREQ
;
1926 if (msg
->msg_flags
& MSG_ZEROCOPY
&&
1927 !vsock_msgzerocopy_allow(transport
)) {
1932 /* Wait for room in the produce queue to enqueue our user's data. */
1933 timeout
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1935 err
= transport
->notify_send_init(vsk
, &send_data
);
1939 while (total_written
< len
) {
1942 add_wait_queue(sk_sleep(sk
), &wait
);
1943 while (vsock_stream_has_space(vsk
) == 0 &&
1945 !(sk
->sk_shutdown
& SEND_SHUTDOWN
) &&
1946 !(vsk
->peer_shutdown
& RCV_SHUTDOWN
)) {
1948 /* Don't wait for non-blocking sockets. */
1951 remove_wait_queue(sk_sleep(sk
), &wait
);
1955 err
= transport
->notify_send_pre_block(vsk
, &send_data
);
1957 remove_wait_queue(sk_sleep(sk
), &wait
);
1962 timeout
= wait_woken(&wait
, TASK_INTERRUPTIBLE
, timeout
);
1964 if (signal_pending(current
)) {
1965 err
= sock_intr_errno(timeout
);
1966 remove_wait_queue(sk_sleep(sk
), &wait
);
1968 } else if (timeout
== 0) {
1970 remove_wait_queue(sk_sleep(sk
), &wait
);
1974 remove_wait_queue(sk_sleep(sk
), &wait
);
1976 /* These checks occur both as part of and after the loop
1977 * conditional since we need to check before and after
1983 } else if ((sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
1984 (vsk
->peer_shutdown
& RCV_SHUTDOWN
)) {
1989 err
= transport
->notify_send_pre_enqueue(vsk
, &send_data
);
1993 /* Note that enqueue will only write as many bytes as are free
1994 * in the produce queue, so we don't need to ensure len is
1995 * smaller than the queue size. It is the caller's
1996 * responsibility to check how many bytes we were able to send.
1999 if (sk
->sk_type
== SOCK_SEQPACKET
) {
2000 written
= transport
->seqpacket_enqueue(vsk
,
2001 msg
, len
- total_written
);
2003 written
= transport
->stream_enqueue(vsk
,
2004 msg
, len
- total_written
);
2012 total_written
+= written
;
2014 err
= transport
->notify_send_post_enqueue(
2015 vsk
, written
, &send_data
);
2022 if (total_written
> 0) {
2023 /* Return number of written bytes only if:
2024 * 1) SOCK_STREAM socket.
2025 * 2) SOCK_SEQPACKET socket when whole buffer is sent.
2027 if (sk
->sk_type
== SOCK_STREAM
|| total_written
== len
)
2028 err
= total_written
;
2031 if (sk
->sk_type
== SOCK_STREAM
)
2032 err
= sk_stream_error(sk
, msg
->msg_flags
, err
);
2038 static int vsock_connectible_wait_data(struct sock
*sk
,
2039 struct wait_queue_entry
*wait
,
2041 struct vsock_transport_recv_notify_data
*recv_data
,
2044 const struct vsock_transport
*transport
;
2045 struct vsock_sock
*vsk
;
2051 transport
= vsk
->transport
;
2054 prepare_to_wait(sk_sleep(sk
), wait
, TASK_INTERRUPTIBLE
);
2055 data
= vsock_connectible_has_data(vsk
);
2059 if (sk
->sk_err
!= 0 ||
2060 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
2061 (vsk
->peer_shutdown
& SEND_SHUTDOWN
)) {
2065 /* Don't wait for non-blocking sockets. */
2072 err
= transport
->notify_recv_pre_block(vsk
, target
, recv_data
);
2078 timeout
= schedule_timeout(timeout
);
2081 if (signal_pending(current
)) {
2082 err
= sock_intr_errno(timeout
);
2084 } else if (timeout
== 0) {
2090 finish_wait(sk_sleep(sk
), wait
);
2095 /* Internal transport error when checking for available
2096 * data. XXX This should be changed to a connection
2097 * reset in a later change.
2105 static int __vsock_stream_recvmsg(struct sock
*sk
, struct msghdr
*msg
,
2106 size_t len
, int flags
)
2108 struct vsock_transport_recv_notify_data recv_data
;
2109 const struct vsock_transport
*transport
;
2110 struct vsock_sock
*vsk
;
2119 transport
= vsk
->transport
;
2121 /* We must not copy less than target bytes into the user's buffer
2122 * before returning successfully, so we wait for the consume queue to
2123 * have that much data to consume before dequeueing. Note that this
2124 * makes it impossible to handle cases where target is greater than the
2127 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, len
);
2128 if (target
>= transport
->stream_rcvhiwat(vsk
)) {
2132 timeout
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
2135 err
= transport
->notify_recv_init(vsk
, target
, &recv_data
);
2143 err
= vsock_connectible_wait_data(sk
, &wait
, timeout
,
2144 &recv_data
, target
);
2148 err
= transport
->notify_recv_pre_dequeue(vsk
, target
,
2153 read
= transport
->stream_dequeue(vsk
, msg
, len
- copied
, flags
);
2161 err
= transport
->notify_recv_post_dequeue(vsk
, target
, read
,
2162 !(flags
& MSG_PEEK
), &recv_data
);
2166 if (read
>= target
|| flags
& MSG_PEEK
)
2174 else if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
2184 static int __vsock_seqpacket_recvmsg(struct sock
*sk
, struct msghdr
*msg
,
2185 size_t len
, int flags
)
2187 const struct vsock_transport
*transport
;
2188 struct vsock_sock
*vsk
;
2195 transport
= vsk
->transport
;
2197 timeout
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
2199 err
= vsock_connectible_wait_data(sk
, &wait
, timeout
, NULL
, 0);
2203 msg_len
= transport
->seqpacket_dequeue(vsk
, msg
, flags
);
2212 } else if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
2215 /* User sets MSG_TRUNC, so return real length of
2218 if (flags
& MSG_TRUNC
)
2221 err
= len
- msg_data_left(msg
);
2223 /* Always set MSG_TRUNC if real length of packet is
2224 * bigger than user's buffer.
2227 msg
->msg_flags
|= MSG_TRUNC
;
2235 __vsock_connectible_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
2239 struct vsock_sock
*vsk
;
2240 const struct vsock_transport
*transport
;
2245 if (unlikely(flags
& MSG_ERRQUEUE
))
2246 return sock_recv_errqueue(sk
, msg
, len
, SOL_VSOCK
, VSOCK_RECVERR
);
2253 transport
= vsk
->transport
;
2255 if (!transport
|| sk
->sk_state
!= TCP_ESTABLISHED
) {
2256 /* Recvmsg is supposed to return 0 if a peer performs an
2257 * orderly shutdown. Differentiate between that case and when a
2258 * peer has not connected or a local shutdown occurred with the
2261 if (sock_flag(sk
, SOCK_DONE
))
2269 if (flags
& MSG_OOB
) {
2274 /* We don't check peer_shutdown flag here since peer may actually shut
2275 * down, but there can be data in the queue that a local socket can
2278 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
2283 /* It is valid on Linux to pass in a zero-length receive buffer. This
2284 * is not an error. We may as well bail out now.
2291 if (sk
->sk_type
== SOCK_STREAM
)
2292 err
= __vsock_stream_recvmsg(sk
, msg
, len
, flags
);
2294 err
= __vsock_seqpacket_recvmsg(sk
, msg
, len
, flags
);
2302 vsock_connectible_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
2305 #ifdef CONFIG_BPF_SYSCALL
2306 struct sock
*sk
= sock
->sk
;
2307 const struct proto
*prot
;
2309 prot
= READ_ONCE(sk
->sk_prot
);
2310 if (prot
!= &vsock_proto
)
2311 return prot
->recvmsg(sk
, msg
, len
, flags
, NULL
);
2314 return __vsock_connectible_recvmsg(sock
, msg
, len
, flags
);
2316 EXPORT_SYMBOL_GPL(vsock_connectible_recvmsg
);
2318 static int vsock_set_rcvlowat(struct sock
*sk
, int val
)
2320 const struct vsock_transport
*transport
;
2321 struct vsock_sock
*vsk
;
2325 if (val
> vsk
->buffer_size
)
2328 transport
= vsk
->transport
;
2330 if (transport
&& transport
->notify_set_rcvlowat
) {
2333 err
= transport
->notify_set_rcvlowat(vsk
, val
);
2338 WRITE_ONCE(sk
->sk_rcvlowat
, val
? : 1);
2342 static const struct proto_ops vsock_stream_ops
= {
2344 .owner
= THIS_MODULE
,
2345 .release
= vsock_release
,
2347 .connect
= vsock_connect
,
2348 .socketpair
= sock_no_socketpair
,
2349 .accept
= vsock_accept
,
2350 .getname
= vsock_getname
,
2352 .ioctl
= vsock_ioctl
,
2353 .listen
= vsock_listen
,
2354 .shutdown
= vsock_shutdown
,
2355 .setsockopt
= vsock_connectible_setsockopt
,
2356 .getsockopt
= vsock_connectible_getsockopt
,
2357 .sendmsg
= vsock_connectible_sendmsg
,
2358 .recvmsg
= vsock_connectible_recvmsg
,
2359 .mmap
= sock_no_mmap
,
2360 .set_rcvlowat
= vsock_set_rcvlowat
,
2361 .read_skb
= vsock_read_skb
,
2364 static const struct proto_ops vsock_seqpacket_ops
= {
2366 .owner
= THIS_MODULE
,
2367 .release
= vsock_release
,
2369 .connect
= vsock_connect
,
2370 .socketpair
= sock_no_socketpair
,
2371 .accept
= vsock_accept
,
2372 .getname
= vsock_getname
,
2374 .ioctl
= vsock_ioctl
,
2375 .listen
= vsock_listen
,
2376 .shutdown
= vsock_shutdown
,
2377 .setsockopt
= vsock_connectible_setsockopt
,
2378 .getsockopt
= vsock_connectible_getsockopt
,
2379 .sendmsg
= vsock_connectible_sendmsg
,
2380 .recvmsg
= vsock_connectible_recvmsg
,
2381 .mmap
= sock_no_mmap
,
2382 .read_skb
= vsock_read_skb
,
2385 static int vsock_create(struct net
*net
, struct socket
*sock
,
2386 int protocol
, int kern
)
2388 struct vsock_sock
*vsk
;
2395 if (protocol
&& protocol
!= PF_VSOCK
)
2396 return -EPROTONOSUPPORT
;
2398 switch (sock
->type
) {
2400 sock
->ops
= &vsock_dgram_ops
;
2403 sock
->ops
= &vsock_stream_ops
;
2405 case SOCK_SEQPACKET
:
2406 sock
->ops
= &vsock_seqpacket_ops
;
2409 return -ESOCKTNOSUPPORT
;
2412 sock
->state
= SS_UNCONNECTED
;
2414 sk
= __vsock_create(net
, sock
, NULL
, GFP_KERNEL
, 0, kern
);
2420 if (sock
->type
== SOCK_DGRAM
) {
2421 ret
= vsock_assign_transport(vsk
, NULL
);
2429 /* SOCK_DGRAM doesn't have 'setsockopt' callback set in its
2430 * proto_ops, so there is no handler for custom logic.
2432 if (sock_type_connectible(sock
->type
))
2433 set_bit(SOCK_CUSTOM_SOCKOPT
, &sk
->sk_socket
->flags
);
2435 vsock_insert_unbound(vsk
);
2440 static const struct net_proto_family vsock_family_ops
= {
2442 .create
= vsock_create
,
2443 .owner
= THIS_MODULE
,
2446 static long vsock_dev_do_ioctl(struct file
*filp
,
2447 unsigned int cmd
, void __user
*ptr
)
2449 u32 __user
*p
= ptr
;
2450 u32 cid
= VMADDR_CID_ANY
;
2454 case IOCTL_VM_SOCKETS_GET_LOCAL_CID
:
2455 /* To be compatible with the VMCI behavior, we prioritize the
2456 * guest CID instead of well-know host CID (VMADDR_CID_HOST).
2459 cid
= transport_g2h
->get_local_cid();
2460 else if (transport_h2g
)
2461 cid
= transport_h2g
->get_local_cid();
2463 if (put_user(cid
, p
) != 0)
2468 retval
= -ENOIOCTLCMD
;
2474 static long vsock_dev_ioctl(struct file
*filp
,
2475 unsigned int cmd
, unsigned long arg
)
2477 return vsock_dev_do_ioctl(filp
, cmd
, (void __user
*)arg
);
2480 #ifdef CONFIG_COMPAT
2481 static long vsock_dev_compat_ioctl(struct file
*filp
,
2482 unsigned int cmd
, unsigned long arg
)
2484 return vsock_dev_do_ioctl(filp
, cmd
, compat_ptr(arg
));
2488 static const struct file_operations vsock_device_ops
= {
2489 .owner
= THIS_MODULE
,
2490 .unlocked_ioctl
= vsock_dev_ioctl
,
2491 #ifdef CONFIG_COMPAT
2492 .compat_ioctl
= vsock_dev_compat_ioctl
,
2494 .open
= nonseekable_open
,
2497 static struct miscdevice vsock_device
= {
2499 .fops
= &vsock_device_ops
,
2502 static int __init
vsock_init(void)
2506 vsock_init_tables();
2508 vsock_proto
.owner
= THIS_MODULE
;
2509 vsock_device
.minor
= MISC_DYNAMIC_MINOR
;
2510 err
= misc_register(&vsock_device
);
2512 pr_err("Failed to register misc device\n");
2513 goto err_reset_transport
;
2516 err
= proto_register(&vsock_proto
, 1); /* we want our slab */
2518 pr_err("Cannot register vsock protocol\n");
2519 goto err_deregister_misc
;
2522 err
= sock_register(&vsock_family_ops
);
2524 pr_err("could not register af_vsock (%d) address family: %d\n",
2526 goto err_unregister_proto
;
2529 vsock_bpf_build_proto();
2533 err_unregister_proto
:
2534 proto_unregister(&vsock_proto
);
2535 err_deregister_misc
:
2536 misc_deregister(&vsock_device
);
2537 err_reset_transport
:
2541 static void __exit
vsock_exit(void)
2543 misc_deregister(&vsock_device
);
2544 sock_unregister(AF_VSOCK
);
2545 proto_unregister(&vsock_proto
);
2548 const struct vsock_transport
*vsock_core_get_transport(struct vsock_sock
*vsk
)
2550 return vsk
->transport
;
2552 EXPORT_SYMBOL_GPL(vsock_core_get_transport
);
2554 int vsock_core_register(const struct vsock_transport
*t
, int features
)
2556 const struct vsock_transport
*t_h2g
, *t_g2h
, *t_dgram
, *t_local
;
2557 int err
= mutex_lock_interruptible(&vsock_register_mutex
);
2562 t_h2g
= transport_h2g
;
2563 t_g2h
= transport_g2h
;
2564 t_dgram
= transport_dgram
;
2565 t_local
= transport_local
;
2567 if (features
& VSOCK_TRANSPORT_F_H2G
) {
2575 if (features
& VSOCK_TRANSPORT_F_G2H
) {
2583 if (features
& VSOCK_TRANSPORT_F_DGRAM
) {
2591 if (features
& VSOCK_TRANSPORT_F_LOCAL
) {
2599 transport_h2g
= t_h2g
;
2600 transport_g2h
= t_g2h
;
2601 transport_dgram
= t_dgram
;
2602 transport_local
= t_local
;
2605 mutex_unlock(&vsock_register_mutex
);
2608 EXPORT_SYMBOL_GPL(vsock_core_register
);
2610 void vsock_core_unregister(const struct vsock_transport
*t
)
2612 mutex_lock(&vsock_register_mutex
);
2614 if (transport_h2g
== t
)
2615 transport_h2g
= NULL
;
2617 if (transport_g2h
== t
)
2618 transport_g2h
= NULL
;
2620 if (transport_dgram
== t
)
2621 transport_dgram
= NULL
;
2623 if (transport_local
== t
)
2624 transport_local
= NULL
;
2626 mutex_unlock(&vsock_register_mutex
);
2628 EXPORT_SYMBOL_GPL(vsock_core_unregister
);
2630 module_init(vsock_init
);
2631 module_exit(vsock_exit
);
2633 MODULE_AUTHOR("VMware, Inc.");
2634 MODULE_DESCRIPTION("VMware Virtual Socket Family");
2635 MODULE_VERSION("1.0.2.0-k");
2636 MODULE_LICENSE("GPL v2");